bundle.js 132 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773
  1. (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
  2. class Ingredient{
  3. constructor(id, name, category, unitType, unit, parent, specialUnit = undefined, unitSize = undefined){
  4. if(!this.isSanitaryString(name)){
  5. banner.createError("NAME CONTAINS ILLEGAL CHARCTERS");
  6. return false;
  7. }
  8. if(!this.isSanitaryString(category)){
  9. banner.createError("CATEGORY CONTAINS ILLEGAL CHARACTERS");
  10. return false;
  11. }
  12. this._id = id;
  13. this._name = name;
  14. this._category = category;
  15. this._unitType = unitType;
  16. this._unit = unit;
  17. this._parent = parent;
  18. if(specialUnit){
  19. this._specialUnit = specialUnit;
  20. this._unitSize = unitSize;
  21. }
  22. }
  23. get id(){
  24. return this._id;
  25. }
  26. get name(){
  27. return this._name;
  28. }
  29. set name(name){
  30. if(!this.isSanitaryString(name)){
  31. return false;
  32. }
  33. this._name = name;
  34. }
  35. get category(){
  36. return this._category;
  37. }
  38. set category(category){
  39. if(!this.isSanitaryString(category)){
  40. return false;
  41. }
  42. this._category = category;
  43. }
  44. get unitType(){
  45. return this._unitType;
  46. }
  47. get unit(){
  48. return this._unit;
  49. }
  50. set unit(unit){
  51. this._unit = unit;
  52. }
  53. get parent(){
  54. return this._parent;
  55. }
  56. get specialUnit(){
  57. return this._specialUnit;
  58. }
  59. get unitSize(){
  60. switch(this._unit){
  61. case "g":return this._unitSize;
  62. case "kg": return this._unitSize / 1000;
  63. case "oz": return this._unitSize / 28.3495;
  64. case "lb": return this._unitSize / 453.5924;
  65. case "ml": return this._unitSize * 1000;
  66. case "l": return this._unitSize;
  67. case "tsp": return this._unitSize * 202.8842;
  68. case "tbsp": return this._unitSize * 67.6278;
  69. case "ozfl": return this._unitSize * 33.8141;
  70. case "cup": return this._unitSize * 4.1667;
  71. case "pt": return this._unitSize * 2.1134;
  72. case "qt": return this._unitSize * 1.0567;
  73. case "gal": return this._unitSize / 3.7854;
  74. case "mm": return this._unitSize * 1000;
  75. case "cm": return this._unitSize * 100;
  76. case "m": return this._unitSize;
  77. case "in": return this._unitSize * 39.3701;
  78. case "ft": return this._unitSize * 3.2808;
  79. default: return this._unitSize;
  80. }
  81. }
  82. set unitSize(unitSize){
  83. if(unitSize < 0){
  84. return false;
  85. }
  86. this._unitSize = unitSize;
  87. }
  88. getNameAndUnit(){
  89. if(this._specialUnit === "bottle"){
  90. return `${this._name} (BOTTLES)`;
  91. }
  92. return `${this._name} (${this._unit.toUpperCase()})`;
  93. }
  94. isSanitaryString(str){
  95. let disallowed = ["\\", "<", ">", "$", "{", "}", "(", ")"];
  96. for(let i = 0; i < disallowed.length; i++){
  97. if(str.includes(disallowed[i])){
  98. return false;
  99. }
  100. }
  101. return true;
  102. }
  103. }
  104. module.exports = Ingredient;
  105. },{}],2:[function(require,module,exports){
  106. class MerchantIngredient{
  107. constructor(ingredient, quantity){
  108. if(quantity < 0){
  109. banner.createError("QUANTITY CANNOT BE A NEGATIVE NUMBER");
  110. return false;
  111. }
  112. this._quantity = quantity;
  113. this._ingredient = ingredient;
  114. }
  115. get ingredient(){
  116. return this._ingredient;
  117. }
  118. get quantity(){
  119. if(this._ingredient.specialUnit === "bottle"){
  120. return this._quantity / this._ingredient._unitSize;
  121. }
  122. switch(this._ingredient.unit){
  123. case "g":return this._quantity;
  124. case "kg": return this._quantity / 1000;
  125. case "oz": return this._quantity / 28.3495;
  126. case "lb": return this._quantity / 453.5924;
  127. case "ml": return this._quantity * 1000;
  128. case "l": return this._quantity;
  129. case "tsp": return this._quantity * 202.8842;
  130. case "tbsp": return this._quantity * 67.6278;
  131. case "ozfl": return this._quantity * 33.8141;
  132. case "cup": return this._quantity * 4.1667;
  133. case "pt": return this._quantity * 2.1134;
  134. case "qt": return this._quantity * 1.0567;
  135. case "gal": return this._quantity / 3.7854;
  136. case "mm": return this._quantity * 1000;
  137. case "cm": return this._quantity * 100;
  138. case "m": return this._quantity;
  139. case "in": return this._quantity * 39.3701;
  140. case "ft": return this._quantity * 3.2808;
  141. default: return this._quantity;
  142. }
  143. }
  144. set quantity(quantity){
  145. if(quantity < 0){
  146. return false;
  147. }
  148. this._quantity = quantity;
  149. }
  150. convertToBase(quantity){
  151. switch(this._ingredient.unit){
  152. case "g": return quantity;
  153. case "kg": return quantity * 1000;
  154. case "oz": return quantity * 28.3495;
  155. case "lb": return quantity * 453.5924;
  156. case "ml": return quantity / 1000;
  157. case "l": return quantity;
  158. case "tsp": return quantity / 202.8842;
  159. case "tbsp": return quantity / 67.6278;
  160. case "ozfl": return quantity / 33.8141;
  161. case "cup": return quantity / 4.1667;
  162. case "pt": return quantity / 2.1134;
  163. case "qt": return quantity / 1.0567;
  164. case "gal": return quantity * 3.7854;
  165. case "mm": return quantity / 1000;
  166. case "cm": return quantity / 100;
  167. case "m": return quantity;
  168. case "in": return quantity / 39.3701;
  169. case "ft": return quantity / 3.2808;
  170. default: return quantity;
  171. }
  172. }
  173. getQuantityDisplay(){
  174. if(this._ingredient.specialUnit === "bottle"){
  175. return `${this.quantity.toFixed(2)} BOTTLES`;
  176. }
  177. return `${this.quantity.toFixed(2)} ${this._ingredient.unit.toUpperCase()}`;
  178. }
  179. }
  180. class Merchant{
  181. constructor(oldMerchant, transactions, modules){
  182. this._modules = modules;
  183. this._name = oldMerchant.name;
  184. this._pos = oldMerchant.pos;
  185. this._ingredients = [];
  186. this._recipes = [];
  187. this._transactions = [];
  188. this._orders = [];
  189. this._units = {
  190. mass: ["g", "kg", "oz", "lb"],
  191. volume: ["ml", "l", "tsp", "tbsp", "ozfl", "cup", "pt", "qt", "gal"],
  192. length: ["mm", "cm", "m", "in", "ft"],
  193. other: ["each", "bottle"]
  194. }
  195. //populate ingredients
  196. for(let i = 0; i < oldMerchant.inventory.length; i++){
  197. const ingredient = new modules.Ingredient(
  198. oldMerchant.inventory[i].ingredient._id,
  199. oldMerchant.inventory[i].ingredient.name,
  200. oldMerchant.inventory[i].ingredient.category,
  201. oldMerchant.inventory[i].ingredient.unitType,
  202. oldMerchant.inventory[i].defaultUnit,
  203. this,
  204. oldMerchant.inventory[i].ingredient.specialUnit,
  205. oldMerchant.inventory[i].ingredient.unitSize
  206. );
  207. const merchantIngredient = new MerchantIngredient(
  208. ingredient,
  209. oldMerchant.inventory[i].quantity,
  210. );
  211. this._ingredients.push(merchantIngredient);
  212. }
  213. //populate recipes
  214. for(let i = 0; i < oldMerchant.recipes.length; i++){
  215. let ingredients = [];
  216. for(let j = 0; j < oldMerchant.recipes[i].ingredients.length; j++){
  217. const ingredient = oldMerchant.recipes[i].ingredients[j];
  218. for(let k = 0; k < this._ingredients.length; k++){
  219. if(ingredient.ingredient === this._ingredients[k].ingredient.id){
  220. ingredients.push({
  221. ingredient: this._ingredients[k].ingredient,
  222. quantity: ingredient.quantity
  223. });
  224. break;
  225. }
  226. }
  227. }
  228. this._recipes.push(new this._modules.Recipe(
  229. oldMerchant.recipes[i]._id,
  230. oldMerchant.recipes[i].name,
  231. oldMerchant.recipes[i].price,
  232. ingredients,
  233. this
  234. ));
  235. }
  236. //populate transactions
  237. for(let i = 0; i < transactions.length; i++){
  238. this._transactions.push(new modules.Transaction(
  239. transactions[i]._id,
  240. transactions[i].date,
  241. transactions[i].recipes,
  242. this
  243. ));
  244. }
  245. }
  246. get modules(){
  247. return this._modules;
  248. }
  249. get name(){
  250. return this._name;
  251. }
  252. set name(name){
  253. if(this.isSanitaryString(name)){
  254. this._name = name;
  255. }
  256. return false;
  257. }
  258. get pos(){
  259. return this._pos;
  260. }
  261. get ingredients(){
  262. return this._ingredients;
  263. }
  264. addIngredient(ingredient, quantity){
  265. const merchantIngredient = new MerchantIngredient(ingredient, quantity);
  266. this._ingredients.push(merchantIngredient);
  267. this._modules.home.isPopulated = false;
  268. this._modules.ingredients.isPopulated = false;
  269. }
  270. removeIngredient(ingredient){
  271. const index = this._ingredients.indexOf(ingredient);
  272. if(index === undefined){
  273. return false;
  274. }
  275. this._ingredients.splice(index, 1);
  276. this._modules.home.isPopulated = false;
  277. this._modules.ingredients.isPopulated = false;
  278. }
  279. updateIngredient(ingredient, quantity){
  280. const index = this._ingredients.indexOf(ingredient);
  281. if(index === undefined){
  282. return false;
  283. }
  284. this._ingredients[index].quantity = quantity;
  285. this._modules.home.isPopulated = false;
  286. this._modules.ingredients.isPopulated = false;
  287. }
  288. getIngredient(id){
  289. for(let i = 0; i < this._ingredients.length; i++){
  290. if(this._ingredients[i].ingredient.id === id){
  291. return this._ingredients[i].ingredient;
  292. }
  293. }
  294. }
  295. get recipes(){
  296. return this._recipes;
  297. }
  298. addRecipe(recipe){
  299. this._recipes.push(recipe);
  300. this._modules.transactions.isPopulated = false;
  301. this._modules.recipeBook.isPopulated = false;
  302. }
  303. removeRecipe(recipe){
  304. const index = this._recipes.indexOf(recipe);
  305. if(index === undefined){
  306. return false;
  307. }
  308. this._recipes.splice(index, 1);
  309. this._modules.transactions.isPopulated = false;
  310. this._modules.recipeBook.isPopulated = false;
  311. }
  312. /*
  313. recipe = {
  314. name: required,
  315. price: required,
  316. ingredients: [{
  317. ingredient: id of ingredient,
  318. quantity: quantity of ingredient
  319. }]
  320. }
  321. */
  322. updateRecipe(recipe){
  323. for(let i = 0; i < this._recipes.length; i++){
  324. if(this._recipes[i].id === recipe._id){
  325. this._recipes[i].name = recipe.name;
  326. this._recipes[i].price = recipe.price;
  327. this._recipes[i].removeIngredients();
  328. for(let j = 0; j < recipe.ingredients.length; j++){
  329. for(let k = 0; k < this._ingredients.length; k++){
  330. if(this._ingredients[k].ingredient.id === recipe.ingredients[j].ingredient){
  331. this._recipes[i].addIngredient(
  332. this._ingredients[k].ingredient,
  333. recipe.ingredients[j].quantity
  334. );
  335. break;
  336. }
  337. }
  338. }
  339. break;
  340. }
  341. }
  342. this._modules.transactions.isPopulated = false;
  343. this._modules.recipeBook.isPopulated = false;
  344. }
  345. getTransactions(from = 0, to = new Date()){
  346. if(from === 0){
  347. from = this._transactions[0].date;
  348. }
  349. const {start, end} = this.getTransactionIndices(from, to);
  350. return this._transactions.slice(start, end);
  351. }
  352. addTransaction(transaction){
  353. this._transactions.push(transaction);
  354. this._transactions.sort((a, b)=>{
  355. if(a.date > b.date){
  356. return -1;
  357. }
  358. return 1;
  359. });
  360. let ingredients = {};
  361. for(let i = 0; i < transaction.recipes.length; i++){
  362. const recipe = transaction.recipes[i];
  363. for(let j = 0; j < recipe.recipe.ingredients.length; j++){
  364. const ingredient = recipe.ingredients[i];
  365. if(ingredients[ingredient.ingredient.id]){
  366. ingredients[ingredient.ingredient.id] += recipe.quantity * ingredient.quantity;
  367. }else{
  368. ingredients[ingredient.ingredient.id] = recipe.quantity * ingredient.quantity;
  369. }
  370. }
  371. }
  372. const keys = Object.keys(ingredients);
  373. for(let i = 0; i < keys.length; i++){
  374. for(let j = 0; j < this._ingredients.length; j++){
  375. if(keys[i] === this._ingredients[j].ingredient.id){
  376. this._ingredients.quantity -= ingredients[keys[i]];
  377. }
  378. }
  379. }
  380. this._modules.home.isPopulated = false;
  381. this._modules.ingredients.isPopulated = false;
  382. this._modules.transactions.isPopulated = false;
  383. this._modules.analytics.newData = true;
  384. }
  385. removeTransaction(transaction){
  386. const index = this._transactions.indexOf(transaction);
  387. if(index === undefined){
  388. return false;
  389. }
  390. this._transactions.splice(index, 1);
  391. let ingredients = {};
  392. for(let i = 0; i < transaction.recipes.length; i++){
  393. const recipe = transaction.recipes[i];
  394. for(let j = 0; j < recipe.recipe.ingredients.length; j++){
  395. const ingredient = recipe.recipe.ingredients[i];
  396. if(ingredients[ingredient.ingredient.id]){
  397. ingredients[ingredient.ingredient.id] += ingredient.quantity * recipe.quantity;
  398. }else{
  399. ingredients[ingredient.ingredient.id] = ingredient.quantity * recipe.quantity;
  400. }
  401. }
  402. }
  403. const keys = Object.keys(ingredients);
  404. for(let i = 0; i < keys.length; i++){
  405. for(let j = 0; j < this._ingredients.length; j++){
  406. if(keys[i] === this._ingredients[j].ingredient.id){
  407. this._ingredients.quantity += ingredients[keys[i]];
  408. }
  409. }
  410. }
  411. this._modules.home.isPopulated = false;
  412. this._modules.ingredients.isPopulated = false;
  413. this._modules.transactions.isPopulated = false;
  414. this._modules.analytics.newData = true;
  415. }
  416. get orders(){
  417. return this._orders;
  418. }
  419. addOrder(order, isNew = false){
  420. this._orders.push(order);
  421. if(isNew){
  422. for(let i = 0; i < order.ingredients.length; i++){
  423. for(let j = 0; j < this._ingredients.length; j++){
  424. if(order.ingredients[i] === this._ingredients[j].ingredient){
  425. this._ingredients[j].quantity += order.ingredients[i].quantity;
  426. break;
  427. }
  428. }
  429. }
  430. }
  431. this._modules.ingredients.isPopulated = false;
  432. this._modules.orders.isPopulated = false;
  433. }
  434. removeOrder(order){
  435. const index = this._orders.indexOf(order);
  436. if(index === undefined){
  437. return false;
  438. }
  439. this._orders.splice(index, 1);
  440. for(let i = 0; i < order.ingredients.length; i++){
  441. for(let j = 0; j < this._ingredients.length; j++){
  442. if(order.ingredients[i].ingredient === this._ingredients[j].ingredient){
  443. this._ingredients[j].quantity -= order.ingredients[i].quantity;
  444. }
  445. }
  446. }
  447. this._modules.ingredients.isPopulated = false;
  448. this._modules.orders.isPopulated = false;
  449. }
  450. get units(){
  451. return this._units;
  452. }
  453. getRevenue(from, to = new Date()){
  454. if(from === 0){
  455. from = this._transactions[0].date;
  456. }
  457. const {start, end} = this.getTransactionIndices(from, to);
  458. let total = 0;
  459. for(let i = start; i <= end; i++){
  460. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  461. for(let k = 0; k < this.recipes.length; k++){
  462. if(this._transactions[i].recipes[j].recipe === this.recipes[k]){
  463. total += this._transactions[i].recipes[j].quantity * this.recipes[k].price;
  464. }
  465. }
  466. }
  467. }
  468. return total / 100;
  469. }
  470. /*
  471. Gets the quantity of each ingredient sold between two dates (dateRange)
  472. Inputs:
  473. dateRange: list containing a start date and an end date
  474. Return:
  475. [{
  476. ingredient: Ingredient object,
  477. quantity: quantity of ingredient sold in default unit
  478. }]
  479. */
  480. getIngredientsSold(from = 0, to = new Date()){
  481. if(from = 0){
  482. from = this._ingredients[0].date;
  483. }
  484. let recipes = this.getRecipesSold(from, to);
  485. let ingredientList = [];
  486. for(let i = 0; i < recipes.length; i++){
  487. for(let j = 0; j < recipes[i].recipe.ingredients.length; j++){
  488. let exists = false;
  489. for(let k = 0; k < ingredientList.length; k++){
  490. if(ingredientList[k].ingredient === recipes[i].recipe.ingredients[j].ingredient){
  491. exists = true;
  492. ingredientList[k].quantity += recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity;
  493. }
  494. }
  495. if(!exists){
  496. ingredientList.push({
  497. ingredient: recipes[i].recipe.ingredients[j].ingredient,
  498. quantity: recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity
  499. });
  500. }
  501. }
  502. }
  503. return ingredientList;
  504. }
  505. /*
  506. Gets the quantity of a single ingredient sold between two dates
  507. Inputs:
  508. ingredient = MerchantIngredient object to find
  509. from = start Date
  510. to = end Date
  511. return: quantity sold in default unit
  512. */
  513. getSingleIngredientSold(ingredient, from = 0, to = new Date()){
  514. if(from === 0){
  515. from = this._transactions[0].date;
  516. }
  517. const {start, end} = this.getTransactionIndices(from, to);
  518. let total = 0;
  519. for(let i = start; i < end; i++){
  520. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  521. for(let k = 0; k < this._transactions[i].recipes[j].recipe.ingredients.length; k++){
  522. if(this._transactions[i].recipes[j].recipe.ingredients[k].ingredient === ingredient.ingredient){
  523. total += this._transactions[i].recipes[j].recipe.ingredients[k].quantity;
  524. break;
  525. }
  526. }
  527. }
  528. }
  529. return total;
  530. }
  531. /*
  532. Gets the number of recipes sold between two dates (dateRange)
  533. Inputs:
  534. dateRange: array containing a start date and an end date
  535. Return:
  536. [{
  537. recipe: a recipe object
  538. quantity: quantity of the recipe sold
  539. }]
  540. */
  541. getRecipesSold(from = 0, to = new Date()){
  542. if(from = 0){
  543. from = this._transactions[0].date;
  544. }
  545. const {start, end} = this.getTransactionIndices(from, to);
  546. let recipeList = [];
  547. for(let i = start; i <= end; i++){
  548. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  549. let exists = false;
  550. for(let k = 0; k < recipeList.length; k++){
  551. if(recipeList[k].recipe === this._transactions[i].recipes[j].recipe){
  552. exists = true;
  553. recipeList[k].quantity += this._transactions[i].recipes[j].quantity;
  554. break;
  555. }
  556. }
  557. if(!exists){
  558. recipeList.push({
  559. recipe: this._transactions[i].recipes[j].recipe,
  560. quantity: this._transactions[i].recipes[j].quantity
  561. });
  562. }
  563. }
  564. }
  565. return recipeList;
  566. }
  567. /*
  568. Groups all of the merchant's ingredients by their category
  569. Return: [{
  570. name: category name,
  571. ingredients: [MerchantIngredient Object]
  572. }]
  573. */
  574. categorizeIngredients(){
  575. let ingredientsByCategory = [];
  576. for(let i = 0; i < this.ingredients.length; i++){
  577. let categoryExists = false;
  578. for(let j = 0; j < ingredientsByCategory.length; j++){
  579. if(this.ingredients[i].ingredient.category === ingredientsByCategory[j].name){
  580. ingredientsByCategory[j].ingredients.push(this.ingredients[i]);
  581. categoryExists = true;
  582. break;
  583. }
  584. }
  585. if(!categoryExists){
  586. ingredientsByCategory.push({
  587. name: this.ingredients[i].ingredient.category,
  588. ingredients: [this.ingredients[i]]
  589. });
  590. }
  591. }
  592. return ingredientsByCategory;
  593. }
  594. unitizeIngredients(){
  595. let ingredientsByUnit = [];
  596. for(let i = 0; i < this.ingredients.length; i++){
  597. let unitExists = false;
  598. const innerIngredient = this.ingredients[i].ingredient;
  599. for(let j = 0; j < ingredientsByUnit.length; j++){
  600. if(innerIngredient.unit === ingredientsByUnit[j].name || innerIngredient.specialUnit === ingredientsByUnit[j].name){
  601. ingredientsByUnit[j].ingredients.push(this.ingredients[i]);
  602. unitExists = true;
  603. break;
  604. }
  605. }
  606. if(!unitExists){
  607. let unit = "";
  608. if(innerIngredient.specialUnit === "bottle"){
  609. unit = "bottle";
  610. }else{
  611. unit = innerIngredient.unit;
  612. }
  613. ingredientsByUnit.push({
  614. name: unit,
  615. ingredients: [this.ingredients[i]]
  616. });
  617. }
  618. }
  619. return ingredientsByUnit;
  620. }
  621. getRecipesForIngredient(ingredient){
  622. let recipes = [];
  623. for(let i = 0; i < this._recipes.length; i++){
  624. for(let j = 0; j < this._recipes[i].ingredients.length; j++){
  625. if(this._recipes[i].ingredients[j].ingredient === ingredient){
  626. recipes.push(this._recipes[i]);
  627. break;
  628. }
  629. }
  630. }
  631. return recipes;
  632. }
  633. getTransactionIndices(from, to){
  634. let start, end;
  635. to.setDate(to.getDate() + 1);
  636. for(let i = this._transactions.length - 1; i >= 0; i--){
  637. if(this._transactions[i].date >= from){
  638. start = i;
  639. break;
  640. }
  641. }
  642. for(let i = 0; i < this._transactions.length; i++){
  643. if(this._transactions[i].date < to){
  644. end = i;
  645. break;
  646. }
  647. }
  648. if(start === undefined){
  649. return false;
  650. }
  651. //these are switched due to the order of the transactions in the merchant
  652. return {start: end, end: start};
  653. }
  654. isSanitaryString(str){
  655. let disallowed = ["\\", "<", ">", "$", "{", "}", "(", ")"];
  656. for(let i = 0; i < disallowed.length; i++){
  657. if(str.includes(disallowed[i])){
  658. return false;
  659. }
  660. }
  661. return true;
  662. }
  663. }
  664. module.exports = Merchant;
  665. },{}],3:[function(require,module,exports){
  666. class OrderIngredient{
  667. constructor(ingredient, quantity, pricePerUnit){
  668. if(quantity < 0){
  669. return false;
  670. }
  671. this._ingredient = ingredient;
  672. this._quantity = quantity;
  673. this._pricePerUnit = pricePerUnit;
  674. }
  675. get ingredient(){
  676. return this._ingredient;
  677. }
  678. get quantity(){
  679. if(this._ingredient.specialUnit === "bottle"){
  680. return this._quantity / this._ingredient.unitSize;
  681. }
  682. switch(this._ingredient.unit){
  683. case "g":return this._quantity;
  684. case "kg": return this._quantity / 1000;
  685. case "oz": return this._quantity / 28.3495;
  686. case "lb": return this._quantity / 453.5924;
  687. case "ml": return this._quantity * 1000;
  688. case "l": return this._quantity;
  689. case "tsp": return this._quantity * 202.8842;
  690. case "tbsp": return this._quantity * 67.6278;
  691. case "ozfl": return this._quantity * 33.8141;
  692. case "cup": return this._quantity * 4.1667;
  693. case "pt": return this._quantity * 2.1134;
  694. case "qt": return this._quantity * 1.0567;
  695. case "gal": return this._quantity / 3.7854;
  696. case "mm": return this._quantity * 1000;
  697. case "cm": return this._quantity * 100;
  698. case "m": return this._quantity;
  699. case "in": return this._quantity * 39.3701;
  700. case "ft": return this._quantity * 3.2808;
  701. default: return this._quantity;
  702. }
  703. }
  704. convertToBase(quantity){
  705. switch(this._ingredient.unit){
  706. case "g": return quantity;
  707. case "kg": return quantity / 1000;
  708. case "oz": return quantity / 28.3495;
  709. case "lb": return quantity / 453.5924;
  710. case "ml": return quantity *= 1000;
  711. case "l": return quantity;
  712. case "tsp": return quantity * 202.8842;
  713. case "tbsp": return quantity * 67.6278;
  714. case "ozfl": return quantity * 33.8141;
  715. case "cup": return quantity * 4.1667;
  716. case "pt": return quantity * 2.1134;
  717. case "qt": return quantity * 1.0567;
  718. case "gal": return quantity / 3.7854;
  719. case "mm": return quantity * 1000;
  720. case "cm": return quantity * 100;
  721. case "m": return quantity;
  722. case "in": return quantity * 39.3701;
  723. case "ft": return quantity * 3.2808;
  724. default: return quantity;
  725. }
  726. }
  727. get pricePerUnit(){
  728. if(this._ingredient.specialUnit === "bottle"){
  729. return price * this._ingredient.unitSize;
  730. }
  731. switch(this._ingredient.unit){
  732. case "g": return this._pricePerUnit;
  733. case "kg": return this._pricePerUnit * 1000;
  734. case "oz": return this._pricePerUnit * 28.3495;
  735. case "lb": return this._pricePerUnit * 453.5924;
  736. case "ml": return this._pricePerUnit / 1000;
  737. case "l": return this._pricePerUnit;
  738. case "tsp": return this._pricePerUnit / 202.8842;
  739. case "tbsp": return this._pricePerUnit / 67.6278;
  740. case "ozfl": return this._pricePerUnit / 33.8141;
  741. case "cup": return this._pricePerUnit / 4.1667;
  742. case "pt": return this._pricePerUnit / 2.1134;
  743. case "qt": return this._pricePerUnit / 1.0567;
  744. case "gal": return this._pricePerUnit * 3.7854;
  745. case "mm": return this._pricePerUnit / 1000;
  746. case "cm": return this._pricePerUnit / 100;
  747. case "m": return this._pricePerUnit;
  748. case "in": return this._pricePerUnit / 39.3701;
  749. case "ft": return this._pricePerUnit / 3.2808;
  750. }
  751. }
  752. cost(){
  753. return this._quantity * this._pricePerUnit;
  754. }
  755. }
  756. /*
  757. Order Object
  758. id = id of order in the database
  759. name = name/id of order, if any
  760. date = Date Object for when the order was created
  761. taxes = User entered taxes associated with the order
  762. fees = User entered fees associated with the order
  763. ingredients = [{
  764. ingredient: Ingredient Object,
  765. quantity: quantity of ingredient sold,
  766. pricePerUnit: price of purchase (per base unit)
  767. }]
  768. parent = the merchant that it belongs to
  769. */
  770. class Order{
  771. constructor(id, name, date, taxes, fees, ingredients, parent){
  772. if(!this.isSanitaryString(name)){
  773. return false;
  774. }
  775. if(taxes < 0){
  776. return false;
  777. }
  778. this._id = id;
  779. this._name = name;
  780. this._date = new Date(date);
  781. this._taxes = taxes;
  782. this._fees = fees;
  783. this._ingredients = [];
  784. this._parent = parent;
  785. if(date > new Date()){
  786. return false;
  787. }
  788. for(let i = 0; i < ingredients.length; i++){
  789. this._ingredients.push(new OrderIngredient(
  790. ingredients[i].ingredient,
  791. ingredients[i].quantity,
  792. ingredients[i].pricePerUnit
  793. ));
  794. }
  795. this._parent.modules.ingredients.isPopulated = false;
  796. }
  797. get id(){
  798. return this._id;
  799. }
  800. get name(){
  801. return this._name;
  802. }
  803. get date(){
  804. return this._date;
  805. }
  806. get taxes(){
  807. return this._taxes;
  808. }
  809. get fees(){
  810. return this._fees;
  811. }
  812. get parent(){
  813. return this._parent;
  814. }
  815. get ingredients(){
  816. return this._ingredients;
  817. }
  818. getIngredientCost(){
  819. let sum = 0;
  820. for(let i = 0; i < this._ingredients.length; i++){
  821. sum += this._ingredients[i].cost();
  822. }
  823. return sum;
  824. }
  825. getTotalCost(){
  826. return this.getIngredientCost() + this._taxes + this._fees;
  827. }
  828. isSanitaryString(str){
  829. let disallowed = ["\\", "<", ">", "$", "{", "}", "(", ")"];
  830. for(let i = 0; i < disallowed.length; i++){
  831. if(str.includes(disallowed[i])){
  832. return false;
  833. }
  834. }
  835. return true;
  836. }
  837. }
  838. module.exports = Order;
  839. },{}],4:[function(require,module,exports){
  840. class RecipeIngredient{
  841. constructor(ingredient, quantity){
  842. if(quantity < 0){
  843. banner.createError("QUANTITY CANNOT BE A NEGATIVE NUMBER");
  844. return false;
  845. }
  846. this._ingredient = ingredient;
  847. this._quantity = quantity;
  848. }
  849. get ingredient(){
  850. return this._ingredient;
  851. }
  852. get quantity(){
  853. // if(this._ingredient.specialUnit === "bottle"){
  854. // return this._quantity / this._ingredient.unitSize;
  855. // }
  856. switch(this._ingredient.unit){
  857. case "g":return this._quantity;
  858. case "kg": return this._quantity / 1000;
  859. case "oz": return this._quantity / 28.3495;
  860. case "lb": return this._quantity / 453.5924;
  861. case "ml": return this._quantity * 1000;
  862. case "l": return this._quantity;
  863. case "tsp": return this._quantity * 202.8842;
  864. case "tbsp": return this._quantity * 67.6278;
  865. case "ozfl": return this._quantity * 33.8141;
  866. case "cup": return this._quantity * 4.1667;
  867. case "pt": return this._quantity * 2.1134;
  868. case "qt": return this._quantity * 1.0567;
  869. case "gal": return this._quantity / 3.7854;
  870. case "mm": return this._quantity * 1000;
  871. case "cm": return this._quantity * 100;
  872. case "m": return this._quantity;
  873. case "in": return this._quantity * 39.3701;
  874. case "ft": return this._quantity * 3.2808;
  875. default: return this._quantity;
  876. }
  877. }
  878. set quantity(quantity){
  879. if(quantity < 0){
  880. banner.createError("QUANTITY CANNOT BE A NEGATIVE NUMBER");
  881. return false;
  882. }
  883. this_quantity = this.convertToBase(quantity);
  884. }
  885. getQuantityDisplay(){
  886. if(this._ingredient.specialUnit === "bottle"){
  887. return `${this.quantity.toFixed(2)} BOTTLES`;
  888. }
  889. return `${this.quantity.toFixed(2)} ${this._ingredient.unit.toUpperCase()}`;
  890. }
  891. convertToBase(quantity){
  892. switch(this._ingredient.unit){
  893. case "g": return quantity;
  894. case "kg": return quantity * 1000;
  895. case "oz": return quantity * 28.3495;
  896. case "lb": return quantity * 453.5924;
  897. case "ml": return quantity / 1000;
  898. case "l": return quantity;
  899. case "tsp": return quantity / 202.8842;
  900. case "tbsp": return quantity / 67.6278;
  901. case "ozfl": return quantity / 33.8141;
  902. case "cup": return quantity / 4.1667;
  903. case "pt": return quantity / 2.1134;
  904. case "qt": return quantity / 1.0567;
  905. case "gal": return quantity * 3.7854;
  906. case "mm": return quantity / 1000;
  907. case "cm": return quantity / 100;
  908. case "m": return quantity;
  909. case "in": return quantity / 39.3701;
  910. case "ft": return quantity / 3.2808;
  911. default: return quantity;
  912. }
  913. }
  914. }
  915. /*
  916. Recipe Object
  917. id = database id of recipe
  918. name = name of recipe
  919. price = price of recipe in cents
  920. ingredients = [{
  921. ingredient: Ingredient Object,
  922. quantity: quantity of the ingredient within the recipe (stored as base unit, i.e grams)
  923. }]
  924. parent = merchant that it belongs to
  925. */
  926. class Recipe{
  927. constructor(id, name, price, ingredients, parent){
  928. if(price < 0){
  929. banner.createError("PRICE CANNOT BE A NEGATIVE NUMBER");
  930. return false;
  931. }
  932. if(!this.isSanitaryString(name)){
  933. banner.createError("NAME CONTAINS ILLEGAL CHARACTERS");
  934. return false;
  935. }
  936. this._id = id;
  937. this._name = name;
  938. this._price = price;
  939. this._parent = parent;
  940. this._ingredients = [];
  941. for(let i = 0; i < ingredients.length; i++){
  942. const recipeIngredient = new RecipeIngredient(
  943. ingredients[i].ingredient,
  944. ingredients[i].quantity
  945. );
  946. this._ingredients.push(recipeIngredient);
  947. }
  948. this._parent.modules.recipeBook.isPopulated = false;
  949. this._parent.modules.analytics.isPopulated = false;
  950. }
  951. get id(){
  952. return this._id;
  953. }
  954. get name(){
  955. return this._name;
  956. }
  957. set name(name){
  958. if(!this.isSanitaryString(name)){
  959. return false;
  960. }
  961. this._name = name;
  962. }
  963. get price(){
  964. return this._price / 100;
  965. }
  966. set price(price){
  967. if(price < 0){
  968. return false;
  969. }
  970. this._price = price;
  971. }
  972. get parent(){
  973. return this._parent;
  974. }
  975. get ingredients(){
  976. return this._ingredients;
  977. }
  978. addIngredient(ingredient, quantity){
  979. if(quantity < 0){
  980. banner.createError("QUANTITY CANNOT BE A NEGATIVE NUMBER");
  981. return false;
  982. }
  983. let recipeIngredient = new RecipeIngredient(ingredient, quantity);
  984. this._ingredients.push(recipeIngredient);
  985. this._parent.modules.recipeBook.isPopulated = false;
  986. this._parent.modules.analytics.isPopulated = false;
  987. }
  988. removeIngredients(){
  989. this._ingredients = [];
  990. }
  991. isSanitaryString(str){
  992. let disallowed = ["\\", "<", ">", "$", "{", "}", "(", ")"];
  993. for(let i = 0; i < disallowed.length; i++){
  994. if(str.includes(disallowed[i])){
  995. return false;
  996. }
  997. }
  998. return true;
  999. }
  1000. }
  1001. module.exports = Recipe;
  1002. },{}],5:[function(require,module,exports){
  1003. class TransactionRecipe{
  1004. constructor(recipe, quantity){
  1005. if(quantity < 0){
  1006. banner.createError("QUANTITY CANNOT BE A NEGATIVE NUMBER");
  1007. return false;
  1008. }
  1009. if(quantity % 1 !== 0){
  1010. banner.createError("RECIPES WITHIN A TRANSACTION MUST BE WHOLE NUMBERS");
  1011. return false;
  1012. }
  1013. this._recipe = recipe;
  1014. this._quantity = quantity;
  1015. }
  1016. get recipe(){
  1017. return this._recipe;
  1018. }
  1019. get quantity(){
  1020. return this._quantity;
  1021. }
  1022. }
  1023. class Transaction{
  1024. constructor(id, date, recipes, parent){
  1025. date = new Date(date);
  1026. if(date > new Date()){
  1027. banner.createError("DATE CANNOT BE SET TO THE FUTURE");
  1028. return false;
  1029. }
  1030. this._id = id;
  1031. this._parent = parent;
  1032. this._date = date;
  1033. this._recipes = [];
  1034. for(let i = 0; i < recipes.length; i++){
  1035. for(let j = 0; j < parent.recipes.length; j++){
  1036. if(recipes[i].recipe === parent.recipes[j].id){
  1037. const transactionRecipe = new TransactionRecipe(
  1038. parent.recipes[j],
  1039. recipes[i].quantity
  1040. )
  1041. this._recipes.push(transactionRecipe);
  1042. break;
  1043. }
  1044. }
  1045. }
  1046. }
  1047. get id(){
  1048. return this._id;
  1049. }
  1050. get parent(){
  1051. return this._parent;
  1052. }
  1053. get date(){
  1054. return this._date;
  1055. }
  1056. get recipes(){
  1057. return this._recipes;
  1058. }
  1059. }
  1060. module.exports = Transaction;
  1061. },{}],6:[function(require,module,exports){
  1062. let analytics = {
  1063. newData: false,
  1064. dateChange: false,
  1065. transactions: [],
  1066. ingredient: {},
  1067. recipe: {},
  1068. display: function(Transaction){
  1069. document.getElementById("analDateBtn").onclick = ()=>{this.changeDates(Transaction)};
  1070. if(this.transactions.length === 0 || this.newData === true){
  1071. let startDate = new Date();
  1072. startDate.setMonth(startDate.getMonth() - 1);
  1073. this.transactions = merchant.getTransactions(startDate);
  1074. }
  1075. let slider = document.getElementById("analSlider");
  1076. slider.onchange = ()=>{this.display(Transaction)};
  1077. let ingredientContent = document.getElementById("analIngredientContent");
  1078. let recipeContent = document.getElementById("analRecipeContent");
  1079. if(slider.checked){
  1080. ingredientContent.style.display = "none";
  1081. recipeContent.style.display = "flex";
  1082. this.displayRecipes();
  1083. }else{
  1084. ingredientContent.style.display = "flex";
  1085. recipeContent.style.display = "none"
  1086. this.displayIngredients();
  1087. }
  1088. },
  1089. displayIngredients: function(){
  1090. const itemsList = document.getElementById("itemsList");
  1091. while(itemsList.children.length > 0){
  1092. itemsList.removeChild(itemsList.firstChild);
  1093. }
  1094. for(let i = 0; i < merchant.ingredients.length; i++){
  1095. let li = document.createElement("li");
  1096. li.classList.add("itemButton");
  1097. li.item = merchant.ingredients[i];
  1098. li.innerText = merchant.ingredients[i].ingredient.name;
  1099. li.onclick = ()=>{
  1100. const itemsList = document.getElementById("itemsList");
  1101. for(let i = 0; i < itemsList.children.length; i++){
  1102. itemsList.children[i].classList.remove("analItemActive");
  1103. }
  1104. li.classList.add("analItemActive");
  1105. this.ingredient = merchant.ingredients[i];
  1106. this.ingredientDisplay();
  1107. };
  1108. itemsList.appendChild(li);
  1109. }
  1110. if(this.dateChange && Object.keys(this.ingredient).length !== 0){
  1111. this.ingredientDisplay();
  1112. }
  1113. this.dateChange = false;
  1114. },
  1115. displayRecipes: function(){
  1116. let recipeList = document.getElementById("analRecipeList");
  1117. while(recipeList.children.length > 0){
  1118. recipeList.removeChild(recipeList.firstChild);
  1119. }
  1120. for(let i = 0; i < merchant.recipes.length; i++){
  1121. let li = document.createElement("li");
  1122. li.classList.add("itemButton");
  1123. li.recipe = merchant.recipes[i];
  1124. li.innerText = merchant.recipes[i].name;
  1125. li.onclick = ()=>{
  1126. let recipeList = document.getElementById("analRecipeList");
  1127. for(let i = 0; i < recipeList.children.length; i++){
  1128. recipeList.children[i].classList.remove("analItemActive");
  1129. }
  1130. li.classList.add("analItemActive");
  1131. this.recipe = merchant.recipes[i];
  1132. this.recipeDisplay();
  1133. }
  1134. recipeList.appendChild(li);
  1135. }
  1136. if(this.dateChange && Object.keys(this.recipe).length !== 0){
  1137. this.recipeDisplay();
  1138. }
  1139. this.dateChange = false;
  1140. },
  1141. ingredientDisplay: function(){
  1142. //Get list of recipes that contain the ingredient
  1143. let containingRecipes = [];
  1144. for(let i = 0; i < merchant.recipes.length; i++){
  1145. for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
  1146. if(merchant.recipes[i].ingredients[j].ingredient === this.ingredient.ingredient){
  1147. containingRecipes.push({
  1148. recipe: merchant.recipes[i],
  1149. quantity: merchant.recipes[i].ingredients[j].quantity
  1150. });
  1151. break;
  1152. }
  1153. }
  1154. }
  1155. //Create Graph
  1156. let quantities = [];
  1157. let dates = [];
  1158. let currentDate = (this.transactions.length > 0) ? this.transactions[0].date : undefined;
  1159. let currentQuantity = 0;
  1160. for(let i = 0; i < this.transactions.length; i++){
  1161. if(currentDate.getDate() !== this.transactions[i].date.getDate()){
  1162. quantities.push(currentQuantity);
  1163. dates.push(currentDate);
  1164. currentQuantity = 0;
  1165. currentDate = this.transactions[i].date;
  1166. }
  1167. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  1168. for(let k = 0; k < containingRecipes.length; k++){
  1169. if(this.transactions[i].recipes[j].recipe === containingRecipes[k].recipe){
  1170. for(let l = 0; l < this.transactions[i].recipes[j].recipe.ingredients.length; l++){
  1171. const transIngredient = this.transactions[i].recipes[j].recipe.ingredients[l];
  1172. if(transIngredient.ingredient === this.ingredient.ingredient){
  1173. currentQuantity += transIngredient.quantity * this.transactions[i].recipes[j].quantity;
  1174. break;
  1175. }
  1176. }
  1177. }
  1178. }
  1179. }
  1180. if(i === this.transactions.length - 1){
  1181. quantities.push(currentQuantity);
  1182. dates.push(currentDate);
  1183. }
  1184. }
  1185. let trace = {
  1186. x: dates,
  1187. y: quantities,
  1188. mode: "lines+markers",
  1189. line: {
  1190. color: "rgb(255, 99, 107)"
  1191. }
  1192. }
  1193. const layout = {
  1194. title: this.ingredient.ingredient.name.toUpperCase(),
  1195. xaxis: {
  1196. title: "DATE"
  1197. },
  1198. yaxis: {
  1199. title: `QUANTITY (${this.ingredient.ingredient.unit.toUpperCase()})`,
  1200. }
  1201. }
  1202. Plotly.newPlot("itemUseGraph", [trace], layout);
  1203. //Create use cards
  1204. let sum = 0;
  1205. let max = 0;
  1206. let min = (quantities.length > 0) ? quantities[0] : 0;
  1207. for(let i = 0; i < quantities.length; i++){
  1208. sum += quantities[i];
  1209. if(quantities[i] > max){
  1210. max = quantities[i];
  1211. }else if(quantities[i] < min){
  1212. min = quantities[i];
  1213. }
  1214. }
  1215. document.getElementById("analMinUse").innerText = `${min.toFixed(2)} ${this.ingredient.ingredient.unit}`;
  1216. document.getElementById("analAvgUse").innerText = `${(sum / quantities.length).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  1217. document.getElementById("analMaxUse").innerText = `${max.toFixed(2)} ${this.ingredient.ingredient.unit}`;
  1218. let dayUse = [0, 0, 0, 0, 0, 0, 0];
  1219. let dayCount = [0, 0, 0, 0, 0, 0, 0];
  1220. for(let i = 0; i < quantities.length; i++){
  1221. dayUse[dates[i].getDay()] += quantities[i];
  1222. dayCount[dates[i].getDay()]++;
  1223. }
  1224. document.getElementById("analDayOne").innerText = `${(dayUse[0] / dayCount[0]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  1225. document.getElementById("analDayTwo").innerText = `${(dayUse[1] / dayCount[1]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  1226. document.getElementById("analDayThree").innerText = `${(dayUse[2] / dayCount[2]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  1227. document.getElementById("analDayFour").innerText = `${(dayUse[3] / dayCount[3]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  1228. document.getElementById("analDayFive").innerText = `${(dayUse[4] / dayCount[4]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  1229. document.getElementById("analDaySix").innerText = `${(dayUse[5] / dayCount[5]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  1230. document.getElementById("analDaySeven").innerText = `${(dayUse[6] / dayCount[6]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  1231. },
  1232. recipeDisplay: function(){
  1233. let quantities = [];
  1234. let dates = [];
  1235. let currentDate = this.transactions[0].date;
  1236. let quantity = 0;
  1237. for(let i = 0; i < this.transactions.length; i++){
  1238. if(currentDate.getDate() !== this.transactions[i].date.getDate()){
  1239. quantities.push(quantity);
  1240. quantity = 0;
  1241. dates.push(currentDate);
  1242. currentDate = this.transactions[i].date;
  1243. }
  1244. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  1245. const recipe = this.transactions[i].recipes[j];
  1246. if(recipe.recipe === this.recipe){
  1247. quantity += recipe.quantity;
  1248. }
  1249. }
  1250. if(i === this.transactions.length - 1){
  1251. quantities.push(quantity);
  1252. dates.push(currentDate);
  1253. }
  1254. }
  1255. const trace = {
  1256. x: dates,
  1257. y: quantities,
  1258. mode: "lines+markers",
  1259. line: {
  1260. color: "rgb(255, 99, 107"
  1261. }
  1262. }
  1263. const layout = {
  1264. title: this.recipe.name.toUpperCase(),
  1265. xaxis: {
  1266. title: "DATE"
  1267. },
  1268. yaxis: {
  1269. title: "Quantity"
  1270. }
  1271. }
  1272. Plotly.newPlot("recipeSalesGraph", [trace], layout);
  1273. let sum = 0;
  1274. for(let i = 0; i < quantities.length; i++){
  1275. sum += quantities[i];
  1276. }
  1277. document.getElementById("recipeAvgUse").innerText = (sum / quantities.length).toFixed(2);
  1278. document.getElementById("recipeAvgRevenue").innerText = `$${(((sum / quantities.length) * this.recipe.price) / 100).toFixed(2)}`;
  1279. },
  1280. changeDates: function(Transaction){
  1281. let dates = {
  1282. from: document.getElementById("analStartDate").valueAsDate,
  1283. to: document.getElementById("analEndDate").valueAsDate
  1284. }
  1285. if(dates.from > dates.to || dates.from === "" || dates.to === "" || dates.to > new Date()){
  1286. banner.createError("INVALID DATE");
  1287. return;
  1288. }
  1289. let loader = document.getElementById("loaderContainer");
  1290. loader.style.display = "flex";
  1291. fetch("/transaction/retrieve", {
  1292. method: "post",
  1293. headers: {
  1294. "Content-Type": "application/json;charset=utf-8"
  1295. },
  1296. body: JSON.stringify(dates)
  1297. })
  1298. .then(response => response.json())
  1299. .then((response)=>{
  1300. if(typeof(response) === "string"){
  1301. banner.createError(response.data);
  1302. }else{
  1303. this.transactions = [];
  1304. for(let i = 0; i < response.length; i++){
  1305. this.transactions.push(new Transaction(
  1306. response[i]._id,
  1307. new Date(response[i].date),
  1308. response[i].recipes,
  1309. merchant
  1310. ));
  1311. }
  1312. let isRecipe = document.getElementById("analSlider").checked;
  1313. if(isRecipe && Object.keys(this.recipe).length !== 0){
  1314. this.recipeDisplay();
  1315. }else if(!isRecipe && Object.keys(this.ingredient).length !== 0){
  1316. this.ingredientDisplay();
  1317. }
  1318. this.dateChange = true;
  1319. }
  1320. })
  1321. .catch((err)=>{
  1322. banner.createError("ERROR: UNABLE TO DISPLAY THE DATA");
  1323. })
  1324. .finally(()=>{
  1325. loader.style.display = "none";
  1326. });
  1327. }
  1328. }
  1329. module.exports = analytics;
  1330. },{}],7:[function(require,module,exports){
  1331. const home = require("./home.js");
  1332. const ingredients = require("./ingredients.js");
  1333. const recipeBook = require("./recipeBook.js");
  1334. const analytics = require("./analytics.js");
  1335. const orders = require("./orders.js");
  1336. const transactions = require("./transactions.js");
  1337. const ingredientDetails = require("./ingredientDetails.js");
  1338. const newIngredient = require("./newIngredient.js");
  1339. const editIngredient = require("./editIngredient.js");
  1340. const newOrder = require("./newOrder.js");
  1341. const newRecipe = require("./newRecipe.js");
  1342. const editRecipe = require("./editRecipe.js");
  1343. const newTransaction = require("./newTransaction.js");
  1344. const orderDetails = require("./orderDetails.js");
  1345. const recipeDetails = require("./recipeDetails.js");
  1346. const transactionDetails = require("./transactionDetails.js");
  1347. const Merchant = require("./Merchant.js");
  1348. const Ingredient = require("./Ingredient.js");
  1349. const Recipe = require("./Recipe.js");
  1350. const Order = require("./Order.js");
  1351. const Transaction = require("./Transaction.js");
  1352. merchant = new Merchant(data.merchant, data.transactions, {
  1353. home: home,
  1354. ingredients: ingredients,
  1355. transactions: transactions,
  1356. recipeBook: recipeBook,
  1357. analytics: analytics,
  1358. orders: orders,
  1359. Ingredient: Ingredient,
  1360. Recipe: Recipe,
  1361. Transaction: Transaction
  1362. });
  1363. controller = {
  1364. openStrand: function(strand){
  1365. this.closeSidebar();
  1366. let strands = document.querySelectorAll(".strand");
  1367. for(let i = 0; i < strands.length; i++){
  1368. strands[i].style.display = "none";
  1369. }
  1370. let buttons = document.querySelectorAll(".menuButton");
  1371. for(let i = 0; i < buttons.length - 1; i++){
  1372. buttons[i].classList = "menuButton";
  1373. buttons[i].disabled = false;
  1374. }
  1375. let activeButton = {};
  1376. switch(strand){
  1377. case "home":
  1378. activeButton = document.getElementById("homeBtn");
  1379. document.getElementById("homeStrand").style.display = "flex";
  1380. home.display();
  1381. break;
  1382. case "ingredients":
  1383. activeButton = document.getElementById("ingredientsBtn");
  1384. document.getElementById("ingredientsStrand").style.display = "flex";
  1385. ingredients.display();
  1386. break;
  1387. case "recipeBook":
  1388. activeButton = document.getElementById("recipeBookBtn");
  1389. document.getElementById("recipeBookStrand").style.display = "flex";
  1390. recipeBook.display(Recipe);
  1391. break;
  1392. case "analytics":
  1393. activeButton = document.getElementById("analyticsBtn");
  1394. document.getElementById("analyticsStrand").style.display = "flex";
  1395. analytics.display(Transaction);
  1396. break;
  1397. case "orders":
  1398. activeButton = document.getElementById("ordersBtn");
  1399. document.getElementById("ordersStrand").style.display = "flex";
  1400. orders.display(Order);
  1401. break;
  1402. case "transactions":
  1403. activeButton = document.getElementById("transactionsBtn");
  1404. document.getElementById("transactionsStrand").style.display = "flex";
  1405. transactions.display(Transaction);
  1406. break;
  1407. }
  1408. activeButton.classList = "menuButton active";
  1409. activeButton.disabled = true;
  1410. if(window.screen.availWidth <= 1000){
  1411. this.closeMenu();
  1412. }
  1413. },
  1414. /*
  1415. Open a specific sidebar
  1416. Input:
  1417. sidebar: the outermost element of the sidebar (must contain class sidebar)
  1418. */
  1419. openSidebar: function(sidebar, data = {}){
  1420. this.closeSidebar();
  1421. document.getElementById("sidebarDiv").classList = "sidebar";
  1422. document.getElementById(sidebar).style.display = "flex";
  1423. switch(sidebar){
  1424. case "ingredientDetails":
  1425. ingredientDetails.display(data, ingredients);
  1426. break;
  1427. case "newIngredient":
  1428. newIngredient.display(Ingredient);
  1429. break;
  1430. case "editIngredient":
  1431. editIngredient.display(data);
  1432. break;
  1433. case "recipeDetails":
  1434. recipeDetails.display(data);
  1435. break;
  1436. case "editRecipe":
  1437. editRecipe.display(data);
  1438. break;
  1439. case "addRecipe":
  1440. newRecipe.display(Recipe);
  1441. break;
  1442. case "orderDetails":
  1443. orderDetails.display(data);
  1444. break;
  1445. case "newOrder":
  1446. newOrder.display(Order);
  1447. break;
  1448. case "transactionDetails":
  1449. transactionDetails.display(data);
  1450. break;
  1451. case "newTransaction":
  1452. newTransaction.display(Transaction);
  1453. break;
  1454. }
  1455. if(window.screen.availWidth <= 1000){
  1456. document.querySelector(".contentBlock").style.display = "none";
  1457. document.getElementById("mobileMenuSelector").style.display = "none";
  1458. document.getElementById("sidebarCloser").style.display = "block";
  1459. }
  1460. },
  1461. closeSidebar: function(){
  1462. let sidebar = document.getElementById("sidebarDiv");
  1463. for(let i = 0; i < sidebar.children.length; i++){
  1464. sidebar.children[i].style.display = "none";
  1465. }
  1466. sidebar.classList = "sidebarHide";
  1467. if(window.screen.availWidth <= 1000){
  1468. document.querySelector(".contentBlock").style.display = "flex";
  1469. document.getElementById("mobileMenuSelector").style.display = "block";
  1470. document.getElementById("sidebarCloser").style.display = "none";
  1471. }
  1472. },
  1473. changeMenu: function(){
  1474. let menu = document.querySelector(".menu");
  1475. let buttons = document.querySelectorAll(".menuButton");
  1476. if(!menu.classList.contains("menuMinimized")){
  1477. menu.classList = "menu menuMinimized";
  1478. for(let i = 0; i < buttons.length; i++){
  1479. buttons[i].children[1].style.display = "none";
  1480. }
  1481. document.getElementById("max").style.display = "none";
  1482. document.getElementById("min").style.display = "flex";
  1483. }else if(menu.classList.contains("menuMinimized")){
  1484. menu.classList = "menu";
  1485. for(let i = 0; i < buttons.length; i++){
  1486. buttons[i].children[1].style.display = "block";
  1487. }
  1488. setTimeout(()=>{
  1489. document.getElementById("max").style.display = "flex";
  1490. document.getElementById("min").style.display = "none";
  1491. }, 150);
  1492. }
  1493. },
  1494. openMenu: function(){
  1495. document.getElementById("menu").style.display = "flex";
  1496. document.querySelector(".contentBlock").style.display = "none";
  1497. document.getElementById("mobileMenuSelector").onclick = ()=>{this.closeMenu()};
  1498. },
  1499. closeMenu: function(){
  1500. document.getElementById("menu").style.display = "none";
  1501. document.querySelector(".contentBlock").style.display = "flex";
  1502. document.getElementById("mobileMenuSelector").onclick = ()=>{this.openMenu()};
  1503. },
  1504. /*
  1505. Converts the price of unit back to the price per default unit
  1506. unitType = type of the unit (i.e. mass, volume)
  1507. unit = exact unit to convert to
  1508. price = price of the ingredient per unit in cents
  1509. */
  1510. reconvertPrice(unitType, unit, price){
  1511. if(unitType === "mass"){
  1512. switch(unit){
  1513. case "g": break;
  1514. case "kg": price *= 1000; break;
  1515. case "oz": price *= 28.3495; break;
  1516. case "lb": price *= 453.5924; break;
  1517. }
  1518. }else if(unitType === "volume"){
  1519. switch(unit){
  1520. case "ml": price /= 1000; break;
  1521. case "l": break;
  1522. case "tsp": price /= 202.8842; break;
  1523. case "tbsp": price /= 67.6278; break;
  1524. case "ozfl": price /= 33.8141; break;
  1525. case "cup": price /= 4.1667; break;
  1526. case "pt": price /= 2.1134; break;
  1527. case "qt": price /= 1.0567; break;
  1528. case "gal": price *= 3.7854; break;
  1529. }
  1530. }else if(unitType === "length"){
  1531. switch(unit){
  1532. case "mm": price /= 1000; break;
  1533. case "cm": price /= 100; break;
  1534. case "m": break;
  1535. case "in": price /= 39.3701; break;
  1536. case "ft": price /= 3.2808; break;
  1537. }
  1538. }
  1539. return price;
  1540. }
  1541. }
  1542. if(window.screen.availWidth > 1000 && window.screen.availWidth <= 1400){
  1543. this.changeMenu();
  1544. document.getElementById("menuShifter2").style.display = "none";
  1545. }
  1546. controller.openStrand("home");
  1547. },{"./Ingredient.js":1,"./Merchant.js":2,"./Order.js":3,"./Recipe.js":4,"./Transaction.js":5,"./analytics.js":6,"./editIngredient.js":8,"./editRecipe.js":9,"./home.js":10,"./ingredientDetails.js":11,"./ingredients.js":12,"./newIngredient.js":13,"./newOrder.js":14,"./newRecipe.js":15,"./newTransaction.js":16,"./orderDetails.js":17,"./orders.js":18,"./recipeBook.js":19,"./recipeDetails.js":20,"./transactionDetails.js":21,"./transactions.js":22}],8:[function(require,module,exports){
  1548. const Ingredient = require("./Ingredient");
  1549. let editIngredient = {
  1550. display: function(ingredient){
  1551. let buttonList = document.getElementById("unitButtons");
  1552. let quantLabel = document.getElementById("editIngQuantityLabel");
  1553. let specialLabel = document.getElementById("editSpecialLabel");
  1554. //Clear any existing data
  1555. while(buttonList.children.length > 0){
  1556. buttonList.removeChild(buttonList.firstChild);
  1557. }
  1558. //Populate basic fields
  1559. document.getElementById("editIngTitle").innerText = ingredient.ingredient.name;
  1560. document.getElementById("editIngName").value = ingredient.ingredient.name;
  1561. document.getElementById("editIngCategory").value = ingredient.ingredient.category;
  1562. quantLabel.innerText = `CURRENT STOCK (${ingredient.ingredient.unit.toUpperCase()})`;
  1563. document.getElementById("editIngSubmit").onclick = ()=>{this.submit(ingredient)};
  1564. //Populate the unit buttons
  1565. const units = merchant.units[ingredient.ingredient.unitType];
  1566. for(let i = 0; i < units.length; i++){
  1567. let button = document.createElement("button");
  1568. button.classList.add("unitButton");
  1569. button.innerText = units[i].toUpperCase();
  1570. button.onclick = ()=>{this.changeUnit(button)};
  1571. buttonList.appendChild(button);
  1572. if(units[i] === ingredient.ingredient.unit){
  1573. button.classList.add("unitActive");
  1574. }
  1575. }
  1576. //Make any changes for special ingredients
  1577. if(ingredient.ingredient.specialUnit === "bottle"){
  1578. quantLabel.innerText = "CURRENT STOCK (BOTTLES):";
  1579. specialLabel.style.display = "flex";
  1580. specialLabel.innerText = `BOTTLE SIZE (${ingredient.ingredient.unit.toUpperCase()}):`;
  1581. let sizeInput = document.createElement("input");
  1582. sizeInput.id = "editIngSpecialSize";
  1583. sizeInput.type = "number";
  1584. sizeInput.min = "0";
  1585. sizeInput.step = "0.01";
  1586. sizeInput.value = ingredient.ingredient.unitSize.toFixed(2);
  1587. specialLabel.appendChild(sizeInput);
  1588. }else{
  1589. specialLabel.style.display = "none";
  1590. }
  1591. let quantInput = document.createElement("input");
  1592. quantInput.id = "editIngQuantity";
  1593. quantInput.type = "number";
  1594. quantInput.min = "0";
  1595. quantInput.step = "0.01";
  1596. quantInput.value = ingredient.quantity.toFixed(2);
  1597. quantLabel.appendChild(quantInput);
  1598. },
  1599. changeUnit(button){
  1600. let buttons = document.getElementById("unitButtons");
  1601. for(let i = 0; i < buttons.children.length; i++){
  1602. buttons.children[i].classList.remove("unitActive");
  1603. }
  1604. button.classList.add("unitActive");
  1605. },
  1606. submit(ingredient){
  1607. const quantity = parseFloat(document.getElementById("editIngQuantityLabel").children[0].value);
  1608. let data = {
  1609. id: ingredient.ingredient.id,
  1610. name: document.getElementById("editIngName").value,
  1611. category: document.getElementById("editIngCategory").value
  1612. }
  1613. //Add data based on unit type
  1614. if(ingredient.ingredient.specialUnit === "bottle"){
  1615. let unitSize = ingredient.convertToBase(parseFloat(document.getElementById("editSpecialLabel").children[0].value));
  1616. data.quantity = quantity * unitSize;
  1617. data.unitSize = unitSize;
  1618. }else{
  1619. data.quantity = ingredient.convertToBase(quantity);
  1620. }
  1621. //Get the measurement unit
  1622. let units = document.getElementById("unitButtons");
  1623. for(let i = 0; i < units.children.length; i++){
  1624. if(units.children[i].classList.contains("unitActive")){
  1625. data.unit = units.children[i].innerText.toLowerCase();
  1626. break;
  1627. }
  1628. }
  1629. let loader = document.getElementById("loaderContainer");
  1630. loader.style.display = "flex";
  1631. fetch("/ingredients/update", {
  1632. method: "put",
  1633. headers: {
  1634. "Content-Type": "application/json;charset=utf-8"
  1635. },
  1636. body: JSON.stringify(data)
  1637. })
  1638. .then(response => response.json())
  1639. .then((response)=>{
  1640. if(typeof(response) === "string"){
  1641. banner.createError(response);
  1642. }else{
  1643. ingredient.ingredient.name = response.ingredient.name;
  1644. ingredient.ingredient.category = response.ingredient.category;
  1645. ingredient.ingredient.unitSize = response.ingredient.unitSize;
  1646. ingredient.ingredient.unit = response.unit;
  1647. merchant.updateIngredient(ingredient, response.quantity);
  1648. controller.openStrand("ingredients");
  1649. banner.createNotification("INGREDIENT UPDATED");
  1650. }
  1651. })
  1652. .catch((err)=>{
  1653. banner.createError("SOMETHING WENT WRONG, PLEASE REFRESH THE PAGE");
  1654. })
  1655. .finally(()=>{
  1656. loader.style.display = "none";
  1657. });
  1658. }
  1659. }
  1660. module.exports = editIngredient;
  1661. },{"./Ingredient":1}],9:[function(require,module,exports){
  1662. let editRecipe = {
  1663. display: function(recipe){
  1664. let nameInput = document.getElementById("editRecipeName");
  1665. if(merchant.pos === "none"){
  1666. nameInput.value = recipe.name;
  1667. }else{
  1668. document.getElementById("editRecipeNoName").innertext = recipe.name;
  1669. nameInput.parentNode.style.display = "none";
  1670. }
  1671. //Populate ingredients
  1672. let ingredientList = document.getElementById("editRecipeIngList");
  1673. while(ingredientList.children.length > 0){
  1674. ingredientList.removeChild(ingredientList.firstChild);
  1675. }
  1676. let template = document.getElementById("editRecipeIng").content.children[0];
  1677. for(let i = 0; i < recipe.ingredients.length; i++){
  1678. let ingredientDiv = template.cloneNode(true);
  1679. ingredientDiv.children[0].onclick = ()=>{ingredientDiv.parentNode.removeChild(ingredientDiv)};
  1680. ingredientDiv.children[1].innerText = recipe.ingredients[i].ingredient.getNameAndUnit();
  1681. ingredientDiv.children[2].style.display = "none";
  1682. ingredientDiv.children[3].value = recipe.ingredients[i].quantity;
  1683. ingredientDiv.ingredient = recipe.ingredients[i];
  1684. ingredientList.appendChild(ingredientDiv);
  1685. }
  1686. document.getElementById("addRecIng").onclick = ()=>{this.newIngredient()};
  1687. document.getElementById("editRecipePrice").value = recipe.price;
  1688. document.getElementById("editRecipeSubmit").onclick = ()=>{this.submit(recipe)};
  1689. document.getElementById("editRecipeCancel").onclick = ()=>{controller.openSidebar("recipeDetails", recipe)};
  1690. },
  1691. newIngredient: function(){
  1692. let ingredientList = document.getElementById("editRecipeIngList");
  1693. let ingredientDiv = document.getElementById("editRecipeIng").content.children[0].cloneNode(true);
  1694. ingredientDiv.children[0].onclick = ()=>{ingredientDiv.parentNode.removeChild(ingredientDiv)};
  1695. ingredientDiv.children[1].style.display = "none";
  1696. ingredientDiv.children[3].value = "0.00";
  1697. //Populate selector
  1698. let categories = merchant.categorizeIngredients();
  1699. for(let i = 0; i < categories.length; i++){
  1700. let group = document.createElement("optgroup");
  1701. group.label = categories[i].name;
  1702. for(let j = 0; j < categories[i].ingredients.length; j++){
  1703. let option = document.createElement("option");
  1704. option.innerText = categories[i].ingredients[j].ingredient.getNameAndUnit();
  1705. option.ingredient = categories[i].ingredients[j];
  1706. group.appendChild(option);
  1707. }
  1708. ingredientDiv.children[2].appendChild(group);
  1709. }
  1710. ingredientList.appendChild(ingredientDiv);
  1711. },
  1712. submit: function(recipe){
  1713. let data = {
  1714. id: recipe.id,
  1715. name: recipe.name,
  1716. price: document.getElementById("editRecipePrice").value * 100,
  1717. ingredients: []
  1718. }
  1719. if(merchant.pos === "none"){
  1720. data.name = document.getElementById("editRecipeName").value;
  1721. }
  1722. let ingredients = document.getElementById("editRecipeIngList").children;
  1723. for(let i = 0; i < ingredients.length; i++){
  1724. const quantity = parseFloat(ingredients[i].children[3].value);
  1725. if(ingredients[i].children[1].style.display === "none"){
  1726. let selector = ingredients[i].children[2];
  1727. let ingredient = selector.options[selector.selectedIndex].ingredient;
  1728. data.ingredients.push({
  1729. ingredient: ingredient.ingredient.id,
  1730. quantity: ingredient.convertToBase(quantity)
  1731. });
  1732. }else{
  1733. data.ingredients.push({
  1734. ingredient: ingredients[i].ingredient.ingredient.id,
  1735. quantity: ingredients[i].ingredient.convertToBase(quantity)
  1736. });
  1737. }
  1738. }
  1739. let loader = document.getElementById("loaderContainer");
  1740. loader.style.display = "flex";
  1741. fetch("/recipe/update", {
  1742. method: "put",
  1743. headers: {
  1744. "Content-Type": "application/json;charset=utf-8"
  1745. },
  1746. body: JSON.stringify(data)
  1747. })
  1748. .then(response => response.json())
  1749. .then((response)=>{
  1750. if(typeof(response) === "string"){
  1751. banner.createError(response);
  1752. }else{
  1753. merchant.updateRecipe(response);
  1754. controller.openStrand("recipeBook");
  1755. }
  1756. })
  1757. .catch((err)=>{
  1758. banner.createError("SOMETHING WENT WRONG, PLEASE REFRESH THE PAGE");
  1759. })
  1760. .finally(()=>{
  1761. loader.style.display = "none";
  1762. });
  1763. }
  1764. }
  1765. module.exports = editRecipe;
  1766. },{}],10:[function(require,module,exports){
  1767. let home = {
  1768. isPopulated: false,
  1769. display: function(){
  1770. if(!this.isPopulated){
  1771. this.drawRevenueCard();
  1772. this.drawRevenueGraph();
  1773. this.drawInventoryCheckCard();
  1774. this.drawPopularCard();
  1775. this.isPopulated = true;
  1776. }
  1777. },
  1778. drawRevenueCard: function(){
  1779. let today = new Date();
  1780. let firstOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
  1781. let firstOfLastMonth = new Date(today.getFullYear(), today.getMonth() - 1, 1);
  1782. let lastMonthToDay = new Date(new Date().setMonth(today.getMonth() - 1));
  1783. const revenueThisMonth = merchant.getRevenue(firstOfMonth);
  1784. const revenueLastMonthToDay = merchant.getRevenue(firstOfLastMonth, lastMonthToDay);
  1785. document.getElementById("revenue").innerText = `$${revenueThisMonth.toFixed(2)}`;
  1786. let revenueChange = ((revenueThisMonth - revenueLastMonthToDay) / revenueLastMonthToDay) * 100;
  1787. let img = "";
  1788. if(revenueChange >= 0){
  1789. img = "/shared/images/upArrow.png";
  1790. }else{
  1791. img = "/shared/images/downArrow.png";
  1792. }
  1793. document.querySelector("#revenueChange p").innerText = `${Math.abs(revenueChange).toFixed(2)}% vs last month`;
  1794. document.querySelector("#revenueChange img").src = img;
  1795. },
  1796. drawRevenueGraph: function(){
  1797. let monthAgo = new Date();
  1798. monthAgo.setMonth(monthAgo.getMonth() - 1);
  1799. let revenue = [];
  1800. let dates = [];
  1801. let dayRevenue = 0;
  1802. const transactions = merchant.getTransactions(monthAgo);
  1803. let currentDate = (transactions.length > 0) ? transactions[0].date : undefined;
  1804. for(let i = 0; i < transactions.length; i++){
  1805. if(transactions[i].date.getDate() !== currentDate.getDate()){
  1806. revenue.push(dayRevenue / 100);
  1807. dayRevenue = 0;
  1808. dates.push(currentDate);
  1809. currentDate = transactions[i].date;
  1810. }
  1811. for(let j = 0; j < transactions[i].recipes.length; j++){
  1812. const recipe = transactions[i].recipes[j];
  1813. dayRevenue += recipe.recipe.price * recipe.quantity;
  1814. }
  1815. }
  1816. const trace = {
  1817. x: dates,
  1818. y: revenue,
  1819. mode: "lines+markers",
  1820. line: {
  1821. color: "rgb(255, 99, 107)"
  1822. }
  1823. }
  1824. const layout = {
  1825. title: "REVENUE",
  1826. xaxis: {
  1827. title: "DATE"
  1828. },
  1829. yaxis: {
  1830. title: "$"
  1831. }
  1832. }
  1833. Plotly.newPlot("graphCard", [trace], layout);
  1834. },
  1835. drawInventoryCheckCard: function(){
  1836. let num;
  1837. if(merchant.ingredients.length < 5){
  1838. num = merchant.ingredients.length;
  1839. }else{
  1840. num = 5;
  1841. }
  1842. let rands = [];
  1843. for(let i = 0; i < num; i++){
  1844. let rand = Math.floor(Math.random() * merchant.ingredients.length);
  1845. if(rands.includes(rand)){
  1846. i--;
  1847. }else{
  1848. rands[i] = rand;
  1849. }
  1850. }
  1851. let ul = document.querySelector("#inventoryCheckCard ul");
  1852. let template = document.getElementById("ingredientCheck").content.children[0];
  1853. while(ul.children.length > 0){
  1854. ul.removeChild(ul.firstChild);
  1855. }
  1856. for(let i = 0; i < rands.length; i++){
  1857. let ingredientCheck = template.cloneNode(true);
  1858. let input = ingredientCheck.children[1].children[1];
  1859. const ingredient = merchant.ingredients[rands[i]];
  1860. ingredientCheck.ingredient = ingredient;
  1861. ingredientCheck.children[0].innerText = ingredient.ingredient.name;
  1862. ingredientCheck.children[1].children[0].onclick = ()=>{
  1863. input.value--;
  1864. input.changed = true;
  1865. };
  1866. if(ingredient.ingredient.specialUnit === "bottle"){
  1867. input.value = ingredient.quantity.toFixed(2);
  1868. ingredientCheck.children[2].innerText = "BOTTLES";
  1869. }else{
  1870. input.value = ingredient.quantity.toFixed(2);
  1871. ingredientCheck.children[2].innerText = ingredient.ingredient.unit.toUpperCase();
  1872. }
  1873. ingredientCheck.children[1].children[2].onclick = ()=>{
  1874. input.value++;
  1875. input.changed = true;
  1876. }
  1877. input.onchange = ()=>{input.changed = true};
  1878. ul.appendChild(ingredientCheck);
  1879. }
  1880. document.getElementById("inventoryCheck").onclick = ()=>{this.submitInventoryCheck()};
  1881. },
  1882. drawPopularCard: function(){
  1883. let thisMonth = new Date();
  1884. thisMonth.setDate(1);
  1885. const ingredientList = merchant.getIngredientsSold(thisMonth);
  1886. if(ingredientList !== false){
  1887. ingredientList.sort((a, b)=>{
  1888. if(a.quantity < b.quantity){
  1889. return 1;
  1890. }
  1891. if(a.quantity > b.quantity){
  1892. return -1;
  1893. }
  1894. return 0;
  1895. });
  1896. let quantities = [];
  1897. let labels = [];
  1898. let colors = [];
  1899. let count = (ingredientList.length < 5) ? ingredientList.length - 1 : 4;
  1900. for(let i = count; i >= 0; i--){
  1901. const ingredientName = ingredientList[i].ingredient.name;
  1902. const ingredientQuantity = ingredientList[i].quantity;
  1903. const unitName = ingredientList[i].ingredient.unit;
  1904. quantities.push(ingredientList[i].quantity);
  1905. labels.push(`${ingredientName}: ${ingredientQuantity.toFixed(2)} ${unitName.toUpperCase()}`);
  1906. if(i === 0){
  1907. colors.push("rgb(255, 99, 107");
  1908. }else{
  1909. colors.push("rgb(179, 191, 209");
  1910. }
  1911. }
  1912. let trace = {
  1913. x: quantities,
  1914. type: "bar",
  1915. orientation: "h",
  1916. text: labels,
  1917. textposition: "auto",
  1918. hoverinfo: "none",
  1919. marker: {
  1920. color: colors
  1921. }
  1922. }
  1923. let layout = {
  1924. title: "MOST POPULAR INGREDIENTS",
  1925. xaxis: {
  1926. zeroline: false,
  1927. title: "QUANTITY"
  1928. },
  1929. yaxis: {
  1930. showticklabels: false
  1931. }
  1932. }
  1933. Plotly.newPlot("popularIngredientsCard", [trace], layout);
  1934. }else{
  1935. document.getElementById("popularCanvas").style.display = "none";
  1936. let notice = document.createElement("p");
  1937. notice.innerText = "N/A";
  1938. notice.classList = "notice";
  1939. document.getElementById("popularIngredientsCard").appendChild(notice);
  1940. }
  1941. },
  1942. //Need to change the updating of ingredients
  1943. //should update the ingredient directly, then send that. Maybe...
  1944. submitInventoryCheck: function(){
  1945. let lis = document.querySelectorAll("#inventoryCheckCard li");
  1946. let changes = [];
  1947. let fetchData = [];
  1948. for(let i = 0; i < lis.length; i++){
  1949. if(lis[i].children[1].children[1].value >= 0){
  1950. let merchIngredient = lis[i].ingredient;
  1951. if(lis[i].children[1].children[1].changed === true){
  1952. let value = 0;
  1953. if(merchIngredient.ingredient.specialUnit === "bottle"){
  1954. value = parseFloat(lis[i].children[1].children[1].value) * merchIngredient.ingredient.unitSize;
  1955. }else{
  1956. value = controller.convertToMain(merchIngredient.ingredient.unit, parseFloat(lis[i].children[1].children[1].value));
  1957. }
  1958. changes.push({
  1959. ingredient: merchIngredient.ingredient,
  1960. quantity: value
  1961. });
  1962. fetchData.push({
  1963. id: merchIngredient.ingredient.id,
  1964. quantity: value
  1965. });
  1966. lis[i].children[1].children[1].changed = false;
  1967. }
  1968. }else{
  1969. banner.createError("CANNOT HAVE NEGATIVE INGREDIENTS");
  1970. return;
  1971. }
  1972. }
  1973. if(fetchData.length > 0){
  1974. let loader = document.getElementById("loaderContainer");
  1975. loader.style.display = "flex";
  1976. fetch("/merchant/ingredients/update", {
  1977. method: "PUT",
  1978. headers: {
  1979. "Content-Type": "application/json;charset=utf-8"
  1980. },
  1981. body: JSON.stringify(fetchData)
  1982. })
  1983. .then(response => response.json())
  1984. .then((response)=>{
  1985. if(typeof(response) === "string"){
  1986. banner.createError(response);
  1987. }else{
  1988. for(let i = 0; i < changes.length; i++){
  1989. merchant.updateIngredient(changes[i].ingredient, changes[i].quantity);
  1990. }
  1991. banner.createNotification("INGREDIENTS UPDATED");
  1992. }
  1993. })
  1994. .catch((err)=>{})
  1995. .finally(()=>{
  1996. loader.style.display = "none";
  1997. });
  1998. }
  1999. }
  2000. }
  2001. module.exports = home;
  2002. },{}],11:[function(require,module,exports){
  2003. let ingredientDetails = {
  2004. ingredient: {},
  2005. dailyUse: 0,
  2006. display: function(ingredient){
  2007. document.getElementById("editIngBtn").onclick = ()=>{controller.openSidebar("editIngredient", ingredient)};
  2008. document.getElementById("ingredientDetailsCategory").innerText = ingredient.ingredient.category;
  2009. document.getElementById("ingredientDetailsName").innerText = ingredient.ingredient.name;
  2010. document.getElementById("ingredientStock").innerText = ingredient.getQuantityDisplay();
  2011. //Calculate and display average daily use
  2012. let quantities = [];
  2013. let now = new Date();
  2014. for(let i = 1; i < 31; i++){
  2015. let endDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i)
  2016. let startDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i - 1);
  2017. quantities.push(merchant.getSingleIngredientSold(ingredient, startDay, endDay));
  2018. }
  2019. let sum = 0;
  2020. for(let i = 0; i < quantities.length; i++){
  2021. sum += quantities[i];
  2022. }
  2023. let dailyUse = sum / quantities.length;
  2024. const dailyUseDiv = document.getElementById("dailyUse");
  2025. if(ingredient.ingredient.specialUnit === "bottle"){
  2026. dailyUseDiv.innerText = `${dailyUse.toFixed(2)} BOTTLES`;
  2027. }else{
  2028. dailyUseDiv.innerText = `${dailyUse.toFixed(2)} ${ingredient.ingredient.unit.toUpperCase()}`;
  2029. }
  2030. //Show recipes that this ingredient is a part of
  2031. let ul = document.getElementById("ingredientRecipeList");
  2032. let recipes = merchant.getRecipesForIngredient(ingredient.ingredient);
  2033. while(ul.children.length > 0){
  2034. ul.removeChild(ul.firstChild);
  2035. }
  2036. for(let i = 0; i < recipes.length; i++){
  2037. let li = document.createElement("li");
  2038. li.innerText = recipes[i].name;
  2039. li.onclick = ()=>{
  2040. controller.openStrand("recipeBook");
  2041. controller.openSidebar("recipeDetails", recipes[i]);
  2042. }
  2043. ul.appendChild(li);
  2044. }
  2045. },
  2046. remove: function(ingredientsStrand){
  2047. for(let i = 0; i < merchant.recipes.length; i++){
  2048. for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
  2049. if(this.ingredient.ingredient === merchant.recipes[i].ingredients[j].ingredient){
  2050. banner.createError("MUST REMOVE INGREDIENT FROM ALL RECIPES BEFORE REMOVING FROM INVENTORY");
  2051. return;
  2052. }
  2053. }
  2054. }
  2055. let loader = document.getElementById("loaderContainer");
  2056. loader.style.display = "flex";
  2057. fetch(`/ingredients/remove/${this.ingredient.ingredient.id}`, {
  2058. method: "DELETE",
  2059. })
  2060. .then((response) => response.json())
  2061. .then((response)=>{
  2062. if(typeof(response) === "string"){
  2063. banner.createError(response);
  2064. }else{
  2065. merchant.removeIngredient(this.ingredient);
  2066. ingredientsStrand.display();
  2067. banner.createNotification("INGREDIENT REMOVED");
  2068. }
  2069. })
  2070. .catch((err)=>{})
  2071. .finally(()=>{
  2072. loader.style.display = "none";
  2073. });
  2074. }
  2075. }
  2076. module.exports = ingredientDetails;
  2077. },{}],12:[function(require,module,exports){
  2078. const { populate } = require("./orders");
  2079. let ingredients = {
  2080. isPopulated: false,
  2081. ingredients: [],
  2082. display: function(){
  2083. if(!this.isPopulated){
  2084. document.getElementById("ingredientSearch").oninput = ()=>{this.search()};
  2085. document.getElementById("ingredientClearButton").onclick = ()=>{this.clearSorting()};
  2086. document.getElementById("ingredientSelect").onchange = ()=>{this.sort()};
  2087. this.populateByProperty("category");
  2088. this.isPopulated = true;
  2089. }
  2090. },
  2091. populateByProperty: function(property){
  2092. let categories;
  2093. if(property === "category"){
  2094. categories = merchant.categorizeIngredients();
  2095. }else if(property === "unit"){
  2096. categories = merchant.unitizeIngredients();
  2097. }
  2098. let ingredientStrand = document.getElementById("categoryList");
  2099. let categoryTemplate = document.getElementById("categoryDiv").content.children[0];
  2100. let ingredientTemplate = document.getElementById("ingredient").content.children[0];
  2101. this.ingredients = [];
  2102. while(ingredientStrand.children.length > 0){
  2103. ingredientStrand.removeChild(ingredientStrand.firstChild);
  2104. }
  2105. for(let i = 0; i < categories.length; i++){
  2106. let categoryDiv = categoryTemplate.cloneNode(true);
  2107. categoryDiv.children[0].children[0].innerText = categories[i].name.toUpperCase();
  2108. categoryDiv.children[0].children[1].onclick = ()=>{
  2109. this.toggleCategory(categoryDiv.children[1], categoryDiv.children[0].children[1]);
  2110. };
  2111. categoryDiv.children[1].style.display = "none";
  2112. ingredientStrand.appendChild(categoryDiv);
  2113. for(let j = 0; j < categories[i].ingredients.length; j++){
  2114. let ingredient = categories[i].ingredients[j];
  2115. let ingredientDiv = ingredientTemplate.cloneNode(true);
  2116. ingredientDiv.children[0].innerText = ingredient.ingredient.name;
  2117. ingredientDiv.onclick = ()=>{controller.openSidebar("ingredientDetails", ingredient)};
  2118. ingredientDiv._name = ingredient.ingredient.name.toLowerCase();
  2119. ingredientDiv._unit = ingredient.ingredient.unit.toLowerCase();
  2120. if(ingredient.ingredient.specialUnit === "bottle"){
  2121. ingredientDiv.children[2].innerText = `${ingredient.quantity.toFixed(2)} BOTTLES`
  2122. }else{
  2123. ingredientDiv.children[2].innerText = `${ingredient.quantity.toFixed(2)} ${ingredient.ingredient.unit.toUpperCase()}`;
  2124. }
  2125. categoryDiv.children[1].appendChild(ingredientDiv);
  2126. this.ingredients.push(ingredientDiv);
  2127. }
  2128. }
  2129. },
  2130. displayIngredientsOnly: function(ingredients){
  2131. let ingredientDiv = document.getElementById("categoryList");
  2132. while(ingredientDiv.children.length > 0){
  2133. ingredientDiv.removeChild(ingredientDiv.firstChild);
  2134. }
  2135. for(let i = 0; i < ingredients.length; i++){
  2136. ingredientDiv.appendChild(ingredients[i]);
  2137. }
  2138. },
  2139. toggleCategory: function(div, button){
  2140. if(div.style.display === "none"){
  2141. button.innerHTML = '<svg width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="black" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="18 15 12 9 6 15"></polyline></svg>';
  2142. div.style.display = "flex";
  2143. }else if(div.style.display === "flex"){
  2144. button.innerHTML = '<svg width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="black" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="6 9 12 15 18 9"></polyline></svg>';
  2145. div.style.display = "none";
  2146. }
  2147. },
  2148. search: function(){
  2149. let input = document.getElementById("ingredientSearch").value.toLowerCase();
  2150. document.getElementById("ingredientSelect").selectedIndex = 0;
  2151. if(input === ""){
  2152. this.populateByProperty("category");
  2153. document.getElementById("ingredientClearButton").style.display = "none";
  2154. return;
  2155. }
  2156. let matchingIngredients = [];
  2157. for(let i = 0; i < this.ingredients.length; i++){
  2158. if(this.ingredients[i]._name.includes(input)){
  2159. matchingIngredients.push(this.ingredients[i]);
  2160. }
  2161. }
  2162. document.getElementById("ingredientClearButton").style.display = "inline";
  2163. this.displayIngredientsOnly(matchingIngredients);
  2164. },
  2165. sort: function(){
  2166. let sortType = document.getElementById("ingredientSelect").value;
  2167. if(sortType === ""){
  2168. return;
  2169. }
  2170. document.getElementById("ingredientSearch").value = "";
  2171. if(sortType === "category"){
  2172. this.populateByProperty("category");
  2173. return;
  2174. }
  2175. if(sortType === "unit"){
  2176. this.populateByProperty("unit");
  2177. return;
  2178. }
  2179. document.getElementById("ingredientClearButton").style.display = "inline";
  2180. let sortedIngredients = this.ingredients.slice().sort((a, b)=> (a[sortType] > b[sortType]) ? 1 : -1);
  2181. this.displayIngredientsOnly(sortedIngredients);
  2182. },
  2183. clearSorting: function(button){
  2184. document.getElementById("ingredientSearch").value = "";
  2185. document.getElementById("ingredientSelect").selectedIndex = 0;
  2186. document.getElementById("ingredientClearButton").style.display = "none";
  2187. this.populateByProperty("category");
  2188. }
  2189. }
  2190. module.exports = ingredients;
  2191. },{"./orders":18}],13:[function(require,module,exports){
  2192. const ingredients = require("./ingredients");
  2193. let newIngredient = {
  2194. display: function(Ingredient){
  2195. const selector = document.getElementById("unitSelector");
  2196. document.getElementById("newIngName").value = "";
  2197. document.getElementById("newIngCategory").value = "";
  2198. document.getElementById("newIngQuantity").value = 0;
  2199. document.getElementById("bottleSizeLabel").style.display = "none";
  2200. selector.value = "g";
  2201. selector.onchange = ()=>{this.unitChange()};
  2202. document.getElementById("submitNewIng").onclick = ()=>{this.submit(Ingredient)};
  2203. },
  2204. unitChange: function(){
  2205. const select = document.getElementById("unitSelector");
  2206. const bottleLabel = document.getElementById("bottleSizeLabel");
  2207. if(select.value === "bottle"){
  2208. bottleLabel.style.display = "block";
  2209. }else{
  2210. bottleLabel.style.display = "none";
  2211. }
  2212. },
  2213. submit: function(Ingredient){
  2214. let unitSelector = document.getElementById("unitSelector");
  2215. let options = document.querySelectorAll("#unitSelector option");
  2216. const quantityValue = parseFloat(document.getElementById("newIngQuantity").value);
  2217. let unit = unitSelector.value;
  2218. let newIngredient = {
  2219. ingredient: {
  2220. name: document.getElementById("newIngName").value,
  2221. category: document.getElementById("newIngCategory").value,
  2222. unitType: options[unitSelector.selectedIndex].getAttribute("type")
  2223. },
  2224. quantity: quantityValue,
  2225. defaultUnit: unit
  2226. }
  2227. //Change the ingredient if it is a special unit type (ie "bottle")
  2228. if(unit === "bottle"){
  2229. newIngredient.ingredient.unitType = "volume";
  2230. newIngredient.ingredient.unitSize = document.getElementById("bottleSize").value
  2231. newIngredient.defaultUnit = document.getElementById("bottleUnits").value;
  2232. newIngredient.ingredient.specialUnit = unit;
  2233. newIngredient.quantity = quantityValue;
  2234. }
  2235. let loader = document.getElementById("loaderContainer");
  2236. loader.style.display = "flex";
  2237. fetch("/ingredients/create", {
  2238. method: "POST",
  2239. headers: {
  2240. "Content-Type": "application/json;charset=utf-8"
  2241. },
  2242. body: JSON.stringify(newIngredient)
  2243. })
  2244. .then((response) => response.json())
  2245. .then((response)=>{
  2246. if(typeof(response) === "string"){
  2247. banner.createError(response);
  2248. }else{
  2249. const ingredient = new Ingredient(
  2250. response.ingredient._id,
  2251. response.ingredient.name,
  2252. response.ingredient.category,
  2253. response.ingredient.unitType,
  2254. response.defaultUnit,
  2255. merchant,
  2256. response.ingredient.specialUnit,
  2257. response.ingredient.unitSize
  2258. )
  2259. merchant.addIngredient(ingredient, response.quantity);
  2260. ingredients.display();
  2261. controller.closeSidebar();
  2262. banner.createNotification("INGREDIENT CREATED");
  2263. }
  2264. })
  2265. .catch((err)=>{
  2266. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  2267. })
  2268. .finally(()=>{
  2269. loader.style.display = "none";
  2270. });
  2271. }
  2272. }
  2273. module.exports = newIngredient;
  2274. },{"./ingredients":12}],14:[function(require,module,exports){
  2275. let newOrder = {
  2276. display: function(Order){
  2277. document.getElementById("sidebarDiv").classList.add("sidebarWide");
  2278. document.getElementById("newOrderIngredientList").style.display = "flex";
  2279. let selectedList = document.getElementById("selectedIngredientList");
  2280. while(selectedList.children.length > 0){
  2281. selectedList.removeChild(selectedList.firstChild);
  2282. }
  2283. let ingredientList = document.getElementById("newOrderIngredients");
  2284. while(ingredientList.children.length > 0){
  2285. ingredientList.removeChild(ingredientList.firstChild);
  2286. }
  2287. for(let i = 0; i < merchant.ingredients.length; i++){
  2288. let ingredient = document.createElement("button");
  2289. ingredient.classList = "newOrderIngredient";
  2290. ingredient.innerText = merchant.ingredients[i].ingredient.name;
  2291. ingredient.onclick = ()=>{this.addIngredient(merchant.ingredients[i], ingredient)};
  2292. ingredientList.appendChild(ingredient);
  2293. }
  2294. document.getElementById("submitNewOrder").onclick = ()=>{this.submit(Order)};
  2295. },
  2296. addIngredient: function(ingredient, element){
  2297. element.style.display = "none";
  2298. let div = document.getElementById("selectedIngredient").content.children[0].cloneNode(true);
  2299. div.ingredient = ingredient;
  2300. div.children[0].children[1].onclick = ()=>{this.removeIngredient(div, element)};
  2301. //Display units depending on the whether it is a special unit
  2302. if(ingredient.ingredient.specialUnit === "bottle"){
  2303. div.children[0].children[0].innerText = `${ingredient.ingredient.name} (BOTTLES)`;
  2304. }else{
  2305. div.children[0].children[0].innerText = `${ingredient.ingredient.name} (${ingredient.ingredient.unit.toUpperCase()})`;
  2306. }
  2307. document.getElementById("selectedIngredientList").appendChild(div);
  2308. },
  2309. removeIngredient: function(selectedElement, element){
  2310. selectedElement.parentElement.removeChild(selectedElement);
  2311. element.style.display = "block";
  2312. },
  2313. submit: function(Order){
  2314. let date = document.getElementById("newOrderDate").value;
  2315. let taxes = document.getElementById("orderTaxes").value * 100;
  2316. let fees = document.getElementById("orderFees").value * 100;
  2317. let ingredients = document.getElementById("selectedIngredientList").children;
  2318. if(date === ""){
  2319. banner.createError("DATE IS REQUIRED FOR ORDERS");
  2320. return;
  2321. }
  2322. let data = {
  2323. name: document.getElementById("newOrderName").value,
  2324. date: date,
  2325. taxes: taxes,
  2326. fees: fees,
  2327. ingredients: []
  2328. }
  2329. for(let i = 0; i < ingredients.length; i++){
  2330. let quantity = ingredients[i].children[1].children[0].value;
  2331. let price = ingredients[i].children[1].children[1].value;
  2332. if(quantity === "" || price === ""){
  2333. banner.createError("MUST PROVIDE QUANTITY AND PRICE PER UNIT FOR ALL INGREDIENTS");
  2334. return;
  2335. }
  2336. if(quantity < 0 || price < 0){
  2337. banner.createError("QUANTITY AND PRICE MUST BE NON-NEGATIVE NUMBERS");
  2338. }
  2339. if(ingredients[i].ingredient.ingredient.specialUnit === "bottle"){
  2340. data.ingredients.push({
  2341. ingredient: ingredients[i].ingredient.ingredient.id,
  2342. quantity: quantity * ingredients[i].ingredient.ingredient.unitSize,
  2343. pricePerUnit: this.convertPrice(ingredients[i].ingredient.ingredient, price * 100)
  2344. });
  2345. }else{
  2346. data.ingredients.push({
  2347. ingredient: ingredients[i].ingredient.ingredient.id,
  2348. quantity: ingredients[i].ingredient.convertToBase(quantity),
  2349. pricePerUnit: this.convertPrice(ingredients[i].ingredient.ingredient, price * 100)
  2350. });
  2351. }
  2352. }
  2353. let loader = document.getElementById("loaderContainer");
  2354. loader.style.display = "flex";
  2355. fetch("/order/create", {
  2356. method: "post",
  2357. headers: {
  2358. "Content-Type": "application/json;charset=utf-8"
  2359. },
  2360. body: JSON.stringify(data)
  2361. })
  2362. .then((response)=>response.json())
  2363. .then((response)=>{
  2364. if(typeof(response) === "string"){
  2365. banner.createError(response);
  2366. }else{
  2367. let order = new Order(
  2368. response._id,
  2369. response.name,
  2370. response.date,
  2371. response.taxes,
  2372. response.fees,
  2373. response.ingredients,
  2374. merchant
  2375. );
  2376. merchant.addOrder(order, true);
  2377. banner.createNotification("NEW ORDER CREATED");
  2378. }
  2379. })
  2380. .catch((err)=>{
  2381. banner.createError("SOMETHING WENT WRONG, PLEASE REFRESH THE PAGE");
  2382. })
  2383. .finally(()=>{
  2384. loader.style.display = "none";
  2385. });
  2386. },
  2387. convertPrice: function(ingredient, price){
  2388. if(ingredient.specialUnit === "bottle"){
  2389. return price / ingredient.unitSize;
  2390. }
  2391. switch(ingredient.unit){
  2392. case "g": return price;
  2393. case "kg": return price / 1000;
  2394. case "oz": return price / 28.3495;
  2395. case "lb": return price / 453.5924;
  2396. case "ml": return price * 1000;
  2397. case "l": return price;
  2398. case "tsp": return price * 202.8842;
  2399. case "tbsp": return price * 67.6278;
  2400. case "ozfl": return price * 33.8141;
  2401. case "cup": return price * 4.1667;
  2402. case "pt": return price * 2.1134;
  2403. case "qt": return price * 1.0567;
  2404. case "gal": return price / 3.7854;
  2405. case "mm": return price * 1000;
  2406. case "cm": return price * 100;
  2407. case "m": return price;
  2408. case "in": return price * 39.3701;
  2409. case "ft": return price * 3.2808;
  2410. }
  2411. }
  2412. }
  2413. module.exports = newOrder;
  2414. },{}],15:[function(require,module,exports){
  2415. let newRecipe = {
  2416. display: function(Recipe){
  2417. let ingredientsSelect = document.querySelector("#recipeInputIngredients select");
  2418. let categories = merchant.categorizeIngredients();
  2419. while(ingredientsSelect.children.length > 0){
  2420. ingredientsSelect.removeChild(ingredientsSelect.firstChild);
  2421. }
  2422. for(let i = 0; i < categories.length; i++){
  2423. let optgroup = document.createElement("optgroup");
  2424. optgroup.label = categories[i].name;
  2425. ingredientsSelect.appendChild(optgroup);
  2426. for(let j = 0; j < categories[i].ingredients.length; j++){
  2427. let option = document.createElement("option");
  2428. option.value = categories[i].ingredients[j].ingredient.id;
  2429. option.innerText = `${categories[i].ingredients[j].ingredient.name} (${categories[i].ingredients[j].ingredient.unit})`;
  2430. optgroup.appendChild(option);
  2431. }
  2432. }
  2433. document.getElementById("ingredientCount").onchange = ()=>{this.changeRecipeCount()};
  2434. document.getElementById("submitNewRecipe").onclick = ()=>{this.submit(Recipe)};
  2435. },
  2436. //Updates the number of ingredient inputs displayed for new recipes
  2437. changeRecipeCount: function(){
  2438. let newCount = document.getElementById("ingredientCount").value;
  2439. let ingredientsDiv = document.getElementById("recipeInputIngredients");
  2440. let oldCount = ingredientsDiv.children.length;
  2441. if(newCount > oldCount){
  2442. let newDivs = newCount - oldCount;
  2443. for(let i = 0; i < newDivs; i++){
  2444. let newNode = ingredientsDiv.children[0].cloneNode(true);
  2445. newNode.children[2].children[0].value = "";
  2446. ingredientsDiv.appendChild(newNode);
  2447. }
  2448. for(let i = 0; i < newCount; i++){
  2449. ingredientsDiv.children[i].children[0].innerText = `INGREDIENT ${i + 1}`;
  2450. }
  2451. }else if(newCount < oldCount){
  2452. let newDivs = oldCount - newCount;
  2453. for(let i = 0; i < newDivs; i++){
  2454. ingredientsDiv.removeChild(ingredientsDiv.children[ingredientsDiv.children.length-1]);
  2455. }
  2456. }
  2457. },
  2458. submit: function(Recipe){
  2459. let newRecipe = {
  2460. name: document.getElementById("newRecipeName").value,
  2461. price: document.getElementById("newRecipePrice").value,
  2462. ingredients: []
  2463. }
  2464. let inputs = document.querySelectorAll("#recipeInputIngredients > div");
  2465. for(let i = 0; i < inputs.length; i++){
  2466. for(let j = 0; j < merchant.ingredients.length; j++){
  2467. if(merchant.ingredients[j].ingredient.id === inputs[i].children[1].children[0].value){
  2468. newRecipe.ingredients.push({
  2469. ingredient: inputs[i].children[1].children[0].value,
  2470. quantity: merchant.ingredients[j].convertToBase(inputs[i].children[2].children[0].value)
  2471. });
  2472. break;
  2473. }
  2474. }
  2475. }
  2476. let loader = document.getElementById("loaderContainer");
  2477. loader.style.display = "flex";
  2478. fetch("/recipe/create", {
  2479. method: "POST",
  2480. headers: {
  2481. "Content-Type": "application/json;charset=utf-8"
  2482. },
  2483. body: JSON.stringify(newRecipe)
  2484. })
  2485. .then((response) => response.json())
  2486. .then((response)=>{
  2487. if(typeof(response) === "string"){
  2488. banner.createError(response);
  2489. }else{
  2490. let ingredients = [];
  2491. for(let i = 0; i < response.ingredients.length; i++){
  2492. for(let j = 0; j < merchant.ingredients.length; j++){
  2493. if(merchant.ingredients[j].ingredient.id === response.ingredients[i].ingredient){
  2494. ingredients.push({
  2495. ingredient: merchant.ingredients[j].ingredient,
  2496. quantity: response.ingredients[i].quantity
  2497. });
  2498. break;
  2499. }
  2500. }
  2501. }
  2502. merchant.addRecipe(new Recipe(
  2503. response._id,
  2504. response.name,
  2505. response.price,
  2506. ingredients,
  2507. merchant
  2508. ));
  2509. banner.createNotification("RECIPE CREATED");
  2510. controller.openStrand("recipeBook");
  2511. }
  2512. })
  2513. .catch((err)=>{
  2514. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  2515. })
  2516. .finally(()=>{
  2517. loader.style.display = "none";
  2518. });
  2519. },
  2520. }
  2521. module.exports = newRecipe;
  2522. },{}],16:[function(require,module,exports){
  2523. let newTransaction = {
  2524. display: function(Transaction){
  2525. let recipeList = document.getElementById("newTransactionRecipes");
  2526. let template = document.getElementById("createTransaction").content.children[0];
  2527. while(recipeList.children.length > 0){
  2528. recipeList.removeChild(recipeList.firstChild);
  2529. }
  2530. for(let i = 0; i < merchant.recipes.length; i++){
  2531. let recipeDiv = template.cloneNode(true);
  2532. recipeDiv.recipe = merchant.recipes[i];
  2533. recipeList.appendChild(recipeDiv);
  2534. recipeDiv.children[0].innerText = merchant.recipes[i].name;
  2535. }
  2536. document.getElementById("submitNewTransaction").onclick = ()=>{this.submit(Transaction)};
  2537. },
  2538. submit: function(Transaction){
  2539. let recipeDivs = document.getElementById("newTransactionRecipes");
  2540. let date = document.getElementById("newTransactionDate").valueAsDate;
  2541. if(date > new Date()){
  2542. banner.createError("CANNOT HAVE A DATE IN THE FUTURE");
  2543. return;
  2544. }
  2545. let data = {
  2546. date: date,
  2547. recipes: [],
  2548. ingredientUpdates: {}
  2549. };
  2550. for(let i = 0; i < recipeDivs.children.length; i++){
  2551. let quantity = recipeDivs.children[i].children[1].value;
  2552. const recipe = recipeDivs.children[i].recipe;
  2553. if(quantity !== "" && quantity > 0){
  2554. data.recipes.push({
  2555. recipe: recipe.id,
  2556. quantity: quantity
  2557. });
  2558. }else if(quantity < 0){
  2559. banner.createError("CANNOT HAVE NEGATIVE VALUES");
  2560. return;
  2561. }
  2562. }
  2563. if(data.recipes.length > 0){
  2564. let loader = document.getElementById("loaderContainer");
  2565. loader.style.display = "flex";
  2566. fetch("/transaction/create", {
  2567. method: "post",
  2568. headers: {
  2569. "Content-Type": "application/json;charset=utf-8"
  2570. },
  2571. body: JSON.stringify(data)
  2572. })
  2573. .then(response => response.json())
  2574. .then((response)=>{
  2575. if(typeof(response) === "string"){
  2576. banner.createError(response);
  2577. }else{
  2578. const transaction = new Transaction(
  2579. response._id,
  2580. response.date,
  2581. response.recipes,
  2582. merchant
  2583. );
  2584. merchant.addTransaction(transaction);
  2585. banner.createNotification("NEW TRANSACTION CREATED, INGREDIENTS UPDATED ACCORDINGLY");
  2586. }
  2587. })
  2588. .catch((err)=>{
  2589. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  2590. })
  2591. .finally(()=>{
  2592. loader.style.display = "none";
  2593. });
  2594. }
  2595. }
  2596. }
  2597. module.exports = newTransaction;
  2598. },{}],17:[function(require,module,exports){
  2599. let orderDetails = {
  2600. display: function(order){
  2601. document.getElementById("removeOrderBtn").onclick = ()=>{this.remove(order)};
  2602. document.getElementById("orderDetailName").innerText = order.name;
  2603. document.getElementById("orderDetailDate").innerText = order.date.toLocaleDateString("en-US");
  2604. document.getElementById("orderDetailTax").innerText = `$${(order.taxes / 100).toFixed(2)}`;
  2605. document.getElementById("orderDetailFee").innerText = `$${(order.fees / 100).toFixed(2)}`;
  2606. let ingredientList = document.getElementById("orderIngredients");
  2607. while(ingredientList.children.length > 0){
  2608. ingredientList.removeChild(ingredientList.firstChild);
  2609. }
  2610. let template = document.getElementById("orderIngredient").content.children[0];
  2611. for(let i = 0; i < order.ingredients.length; i++){
  2612. let ingredientDiv = template.cloneNode(true);
  2613. const ingredient = order.ingredients[i].ingredient;
  2614. ingredientDiv.children[0].innerText = order.ingredients[i].ingredient.name;
  2615. ingredientDiv.children[2].innerText = `$${(order.ingredients[i].cost() / 100).toFixed(2)}`;
  2616. const ingredientDisplay = ingredientDiv.children[1];
  2617. if(ingredient.specialUnit === "bottle"){
  2618. ingredientDisplay.innerText = `${order.ingredients[i].quantity.toFixed(2)} bottles x $${(order.ingredients.pricePerUnit / 100).toFixed(2)}`;
  2619. }else{
  2620. ingredientDisplay.innerText = `${order.ingredients[i].quantity.toFixed(2)} ${ingredient.unit.toUpperCase()} X $${(order.ingredients[i].pricePerUnit / 100).toFixed(2)}`;
  2621. }
  2622. ingredientList.appendChild(ingredientDiv);
  2623. }
  2624. document.getElementById("orderDetailTotal").innerText = `$${(order.getIngredientCost() / 100).toFixed(2)}`;
  2625. document.querySelector("#orderTotalPrice p").innerText = `$${(order.getTotalCost() / 100).toFixed(2)}`;
  2626. },
  2627. remove: function(order){
  2628. let loader = document.getElementById("loaderContainer");
  2629. loader.style.display = "flex";
  2630. fetch(`/order/${order.id}`, {
  2631. method: "DELETE",
  2632. headers: {
  2633. "Content-Type": "application/json;charset=utf-8"
  2634. }
  2635. })
  2636. .then((response) => response.json())
  2637. .then((response)=>{
  2638. if(typeof(response) === "string"){
  2639. banner.createError(response);
  2640. }else{
  2641. merchant.removeOrder(order);
  2642. banner.createNotification("ORDER REMOVED");
  2643. }
  2644. })
  2645. .catch((err)=>{
  2646. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  2647. })
  2648. .finally(()=>{
  2649. loader.style.display = "none";
  2650. });
  2651. }
  2652. }
  2653. module.exports = orderDetails;
  2654. },{}],18:[function(require,module,exports){
  2655. let orders = {
  2656. isPopulated: false,
  2657. isFetched: false,
  2658. display: async function(Order){
  2659. if(!this.isFetched){
  2660. let loader = document.getElementById("loaderContainer");
  2661. loader.style.display = "flex";
  2662. fetch("/order", {
  2663. method: "GET",
  2664. headers: {
  2665. "Content-Type": "application/json;charset=utf-8"
  2666. },
  2667. })
  2668. .then((response) => response.json())
  2669. .then((response)=>{
  2670. if(typeof(response) === "string"){
  2671. banner.createError(response);
  2672. }else{
  2673. for(let i = 0; i < response.length; i++){
  2674. let ingredients = [];
  2675. for(let j = 0; j < response[i].ingredients.length; j++){
  2676. const orderIngredient = response[i].ingredients[j];
  2677. for(let k = 0; k < merchant.ingredients.length; k++){
  2678. if(merchant.ingredients[k].ingredient.id === orderIngredient.ingredient){
  2679. ingredients.push({
  2680. ingredient: merchant.ingredients[k].ingredient,
  2681. quantity: orderIngredient.quantity,
  2682. pricePerUnit: orderIngredient.pricePerUnit
  2683. });
  2684. }
  2685. }
  2686. }
  2687. merchant.addOrder(new Order(
  2688. response[i]._id,
  2689. response[i].name,
  2690. response[i].date,
  2691. response[i].taxes,
  2692. response[i].fees,
  2693. ingredients,
  2694. merchant
  2695. ));
  2696. }
  2697. document.getElementById("orderSubmitForm").onsubmit = ()=>{this.submitFilter(Order)};
  2698. this.isFetched = true;
  2699. this.populate();
  2700. this.isPopulated = true;
  2701. }
  2702. })
  2703. .catch((err)=>{
  2704. banner.createError("SOMETHING WENT WRONG. TRY REFRESHING THE PAGE");
  2705. })
  2706. .finally(()=>{
  2707. loader.style.display = "none";
  2708. });
  2709. }
  2710. },
  2711. populate: function(){
  2712. let listDiv = document.getElementById("orderList");
  2713. let template = document.getElementById("order").content.children[0];
  2714. let dateDropdown = document.getElementById("dateDropdownOrder");
  2715. let ingredientDropdown = document.getElementById("ingredientDropdown");
  2716. dateDropdown.style.display = "none";
  2717. ingredientDropdown.style.display = "none";
  2718. document.getElementById("dateFilterBtnOrder").onclick = ()=>{this.toggleDropdown(dateDropdown)};
  2719. document.getElementById("ingredientFilterBtn").onclick = ()=>{this.toggleDropdown(ingredientDropdown)};
  2720. for(let i = 0; i < merchant.ingredients.length; i++){
  2721. let checkbox = document.createElement("input");
  2722. checkbox.type = "checkbox";
  2723. checkbox.ingredient = merchant.ingredients[i].ingredient;
  2724. ingredientDropdown.appendChild(checkbox);
  2725. let label = document.createElement("label");
  2726. label.innerText = merchant.ingredients[i].ingredient.name;
  2727. label.for = checkbox;
  2728. ingredientDropdown.appendChild(label);
  2729. let brk = document.createElement("br");
  2730. ingredientDropdown.appendChild(brk);
  2731. }
  2732. while(listDiv.children.length > 0){
  2733. listDiv.removeChild(listDiv.firstChild);
  2734. }
  2735. for(let i = 0; i < merchant.orders.length; i++){
  2736. let row = template.cloneNode(true);
  2737. row.children[0].innerText = merchant.orders[i].name;
  2738. row.children[1].innerText = `${merchant.orders[i].ingredients.length} ingredients`;
  2739. row.children[2].innerText = new Date(merchant.orders[i].date).toLocaleDateString("en-US");
  2740. row.children[3].innerText = `$${(merchant.orders[i].getTotalCost() / 100).toFixed(2)}`;
  2741. row.order = merchant.orders[i];
  2742. row.onclick = ()=>{controller.openSidebar("orderDetails", merchant.orders[i])};
  2743. listDiv.appendChild(row);
  2744. }
  2745. },
  2746. submitFilter: function(){
  2747. event.preventDefault();
  2748. let data = {
  2749. startDate: document.getElementById("orderFilDate1").valueAsDate,
  2750. endDate: document.getElementById("orderFilDate2").valueAsDate,
  2751. ingredients: []
  2752. }
  2753. if(data.startDate >= data.endDate){
  2754. banner.createError("START DATE CANNOT BE AFTER END DATE");
  2755. return;
  2756. }
  2757. let ingredientChoices = document.getElementById("ingredientDropdown");
  2758. for(let i = 0; i < ingredientChoices.children.length; i += 3){
  2759. if(ingredientChoices.children[i].checked){
  2760. data.ingredients.push(ingredientChoices.children[i].ingredient.id);
  2761. }
  2762. }
  2763. if(data.ingredients.length === 0){
  2764. for(let i = 0; i < merchant.ingredients.length; i++){
  2765. data.ingredients.push(merchant.ingredients[i].ingredient.id);
  2766. }
  2767. }
  2768. let loader = document.getElementById("loaderContainer");
  2769. loader.style.display = "flex";
  2770. fetch("/order", {
  2771. method: "POST",
  2772. headers: {
  2773. "Content-Type": "application/json;charset=utf-8"
  2774. },
  2775. body: JSON.stringify(data)
  2776. })
  2777. .then((response) => response.json())
  2778. .then((response)=>{
  2779. if(typeof(response) === "string"){
  2780. banner.createError(response);
  2781. }else{
  2782. let orderList = document.getElementById("orderList");
  2783. let template = document.getElementById("order").content.children[0];
  2784. while(orderList.children.length > 0){
  2785. orderList.removeChild(orderList.firstChild);
  2786. }
  2787. for(let i = 0; i < response.length; i++){
  2788. let orderDiv = template.cloneNode(true);
  2789. let order = new Order(
  2790. response[i]._id,
  2791. response[i].name,
  2792. response[i].date,
  2793. response[i].taxes,
  2794. response[i].fees,
  2795. response[i].ingredients,
  2796. merchant
  2797. );
  2798. let cost = 0;
  2799. for(let j = 0; j < order.ingredients.length; j++){
  2800. cost += (order.ingredients[j].price / 100) * order.ingredients[j].quantity;
  2801. }
  2802. orderDiv.children[0].innerText = order.name;
  2803. orderDiv.children[1].innerText = `${order.ingredients.length} items`;
  2804. orderDiv.children[2].innerText = order.date.toLocaleDateString();
  2805. orderDiv.children[3].innerText = `$${cost.toFixed(2)}`;
  2806. orderDiv.onclick = ()=>{controller.openSidebar("orderDetails", order)};
  2807. orderList.appendChild(orderDiv);
  2808. }
  2809. }
  2810. })
  2811. .catch((err)=>{
  2812. banner.createError("UNABLE TO DISPLAY THE ORDERS");
  2813. })
  2814. .finally(()=>{
  2815. loader.style.display = "none";
  2816. });
  2817. },
  2818. toggleDropdown: function(dropdown){
  2819. event.preventDefault();
  2820. let polyline = dropdown.parentElement.children[0].children[1].children[0].children[0];
  2821. if(dropdown.style.display === "none"){
  2822. dropdown.style.display = "block";
  2823. polyline.setAttribute("points", "18 15 12 9 6 15");
  2824. }else{
  2825. dropdown.style.display = "none";
  2826. polyline.setAttribute("points", "6 9 12 15 18 9");
  2827. }
  2828. }
  2829. }
  2830. module.exports = orders;
  2831. },{}],19:[function(require,module,exports){
  2832. let recipeBook = {
  2833. isPopulated: false,
  2834. recipeDivList: [],
  2835. display: function(Recipe){
  2836. if(!this.isPopulated){
  2837. this.populateRecipes();
  2838. if(merchant.pos !== "none"){
  2839. document.getElementById("posUpdateRecipe").onclick = ()=>{this.posUpdate(Recipe)};
  2840. }
  2841. document.getElementById("recipeSearch").oninput = ()=>{this.search()};
  2842. document.getElementById("recipeClearButton").onclick = ()=>{this.clearSorting()};
  2843. this.populateRecipes();
  2844. this.isPopulated = true;
  2845. }
  2846. },
  2847. populateRecipes: function(){
  2848. let recipeList = document.getElementById("recipeList");
  2849. let template = document.getElementById("recipe").content.children[0];
  2850. this.recipeDivList = [];
  2851. while(recipeList.children.length > 0){
  2852. recipeList.removeChild(recipeList.firstChild);
  2853. }
  2854. for(let i = 0; i < merchant.recipes.length; i++){
  2855. let recipeDiv = template.cloneNode(true);
  2856. recipeDiv.onclick = ()=>{controller.openSidebar("recipeDetails", merchant.recipes[i])};
  2857. recipeDiv._name = merchant.recipes[i].name;
  2858. recipeList.appendChild(recipeDiv);
  2859. recipeDiv.children[0].innerText = merchant.recipes[i].name;
  2860. recipeDiv.children[1].innerText = `$${merchant.recipes[i].price.toFixed(2)}`;
  2861. this.recipeDivList.push(recipeDiv);
  2862. }
  2863. },
  2864. search: function(){
  2865. let input = document.getElementById("recipeSearch").value.toLowerCase();
  2866. let recipeList = document.getElementById("recipeList");
  2867. let clearButton = document.getElementById("recipeClearButton");
  2868. let matchingRecipes = [];
  2869. for(let i = 0; i < this.recipeDivList.length; i++){
  2870. if(this.recipeDivList[i]._name.toLowerCase().includes(input)){
  2871. matchingRecipes.push(this.recipeDivList[i]);
  2872. }
  2873. }
  2874. while(recipeList.children.length > 0){
  2875. recipeList.removeChild(recipeList.firstChild);
  2876. }
  2877. for(let i = 0; i < matchingRecipes.length; i++){
  2878. recipeList.appendChild(matchingRecipes[i]);
  2879. }
  2880. if(input === ""){
  2881. clearButton.style.display = "none";
  2882. }else{
  2883. clearButton.style.display = "inline";
  2884. }
  2885. },
  2886. clearSorting: function(){
  2887. document.getElementById("recipeSearch").value = "";
  2888. this.search();
  2889. },
  2890. posUpdate: function(Recipe){
  2891. let loader = document.getElementById("loaderContainer");
  2892. loader.style.display = "flex";
  2893. let url = `/recipe/update/${merchant.pos}`;
  2894. fetch(url, {
  2895. method: "GET",
  2896. headers: {
  2897. "Content-Type": "application/json;charset=utf-8"
  2898. },
  2899. })
  2900. .then(response => response.json())
  2901. .then((response)=>{
  2902. for(let i = 0; i < response.new.length; i++){
  2903. const recipe = new Recipe(
  2904. response.new[i]._id,
  2905. response.new[i].name,
  2906. response.new[i].price,
  2907. merchant,
  2908. []
  2909. );
  2910. merchant.addRecipe(recipe);
  2911. }
  2912. for(let i = 0; i < response.removed.length; i++){
  2913. const recipe = new Recipe(
  2914. response.removed[i]._id,
  2915. response.removed[i].name,
  2916. response.removed[i].price,
  2917. merchant,
  2918. []
  2919. );
  2920. merchant.removeRecipe(recipe);
  2921. }
  2922. })
  2923. .catch((err)=>{
  2924. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  2925. })
  2926. .finally(()=>{
  2927. loader.style.display = "none";
  2928. });
  2929. }
  2930. }
  2931. module.exports = recipeBook;
  2932. },{}],20:[function(require,module,exports){
  2933. let recipeDetails = {
  2934. display: function(recipe){
  2935. document.getElementById("editRecipeBtn").onclick = ()=>{controller.openSidebar("editRecipe", recipe)};
  2936. document.getElementById("removeRecipeBtn").onclick = ()=>{this.remove(recipe)};
  2937. document.getElementById("recipeName").innerText = recipe.name;
  2938. //ingredient list
  2939. let ingredientsDiv = document.getElementById("recipeIngredientList");
  2940. while(ingredientsDiv.children.length > 0){
  2941. ingredientsDiv.removeChild(ingredientsDiv.firstChild);
  2942. }
  2943. let template = document.getElementById("recipeIngredient").content.children[0];
  2944. for(let i = 0; i < recipe.ingredients.length; i++){
  2945. let recipeDiv = template.cloneNode(true);
  2946. recipeDiv.children[0].innerText = recipe.ingredients[i].ingredient.name;
  2947. recipeDiv.children[1].innerText = `${recipe.ingredients[i].getQuantityDisplay()}`;
  2948. ingredientsDiv.appendChild(recipeDiv);
  2949. }
  2950. document.getElementById("recipePrice").children[1].innerText = `$${recipe.price.toFixed(2)}`;
  2951. },
  2952. remove: function(recipe){
  2953. let loader = document.getElementById("loaderContainer");
  2954. loader.style.display = "flex";
  2955. fetch(`/merchant/recipes/remove/${recipe.id}`, {
  2956. method: "DELETE"
  2957. })
  2958. .then((response) => response.json())
  2959. .then((response)=>{
  2960. if(typeof(response) === "string"){
  2961. banner.createError(response);
  2962. }else{
  2963. merchant.removeRecipe(recipe);
  2964. banner.createNotification("RECIPE REMOVED");
  2965. controller.openStrand("recipeBook");
  2966. }
  2967. })
  2968. .catch((err)=>{
  2969. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  2970. })
  2971. .finally(()=>{
  2972. loader.style.display = "none";
  2973. });
  2974. },
  2975. displayAddIngredient: function(){
  2976. let template = document.getElementById("addRecIngredient").content.children[0].cloneNode(true);
  2977. template.name = "new";
  2978. document.getElementById("recipeIngredientList").appendChild(template);
  2979. let categories = merchant.categorizeIngredients();
  2980. for(let i = 0; i < categories.length; i++){
  2981. let optGroup = document.createElement("optgroup");
  2982. optGroup.label = categories[i].name;
  2983. template.children[0].appendChild(optGroup);
  2984. for(let j = 0; j < categories[i].ingredients.length; j++){
  2985. let option = document.createElement("option");
  2986. option.innerText = `${categories[i].ingredients[j].ingredient.name} (${categories[i].ingredients[j].ingredient.unit})`;
  2987. option.ingredient = categories[i].ingredients[j].ingredient;
  2988. optGroup.appendChild(option);
  2989. }
  2990. }
  2991. }
  2992. }
  2993. module.exports = recipeDetails;
  2994. },{}],21:[function(require,module,exports){
  2995. let transactionDetails = {
  2996. transaction: {},
  2997. display: function(transaction){
  2998. this.transaction = transaction;
  2999. let recipeList = document.getElementById("transactionRecipes");
  3000. let template = document.getElementById("transactionRecipe").content.children[0];
  3001. let totalRecipes = 0;
  3002. let totalPrice = 0;
  3003. while(recipeList.children.length > 0){
  3004. recipeList.removeChild(recipeList.firstChild);
  3005. }
  3006. for(let i = 0; i < transaction.recipes.length; i++){
  3007. let recipe = template.cloneNode(true);
  3008. let price = transaction.recipes[i].quantity * transaction.recipes[i].recipe.price;
  3009. recipe.children[0].innerText = transaction.recipes[i].recipe.name;
  3010. recipe.children[1].innerText = `${transaction.recipes[i].quantity} x $${parseFloat(transaction.recipes[i].recipe.price / 100).toFixed(2)}`;
  3011. recipe.children[2].innerText = `$${(price / 100).toFixed(2)}`;
  3012. recipeList.appendChild(recipe);
  3013. totalRecipes += transaction.recipes[i].quantity;
  3014. totalPrice += price;
  3015. }
  3016. let months = ["January", "Fecbruary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  3017. let days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  3018. let dateString = `${days[transaction.date.getDay()]}, ${months[transaction.date.getMonth()]} ${transaction.date.getDate()}, ${transaction.date.getFullYear()}`;
  3019. document.getElementById("transactionDate").innerText = dateString;
  3020. document.getElementById("transactionTime").innerText = transaction.date.toLocaleTimeString();
  3021. document.getElementById("totalRecipes").innerText = `${totalRecipes} recipes`;
  3022. document.getElementById("totalPrice").innerText = `$${(totalPrice / 100).toFixed(2)}`;
  3023. if(merchant.pos === "none"){
  3024. document.getElementById("removeTransBtn").onclick = ()=>{this.remove()};
  3025. }
  3026. },
  3027. remove: function(){
  3028. let loader = document.getElementById("loaderContainer");
  3029. loader.style.display = "flex";
  3030. fetch(`/transaction/${this.transaction.id}`, {
  3031. method: "delete",
  3032. headers: {
  3033. "Content-Type": "application/json;charset=utf-8"
  3034. },
  3035. })
  3036. .then(response => response.json())
  3037. .then((response)=>{
  3038. if(typeof(response) === "string"){
  3039. banner.createError(response);
  3040. }else{
  3041. merchant.removeTransaction(this.transaction);
  3042. banner.createNotification("TRANSACTION REMOVED");
  3043. }
  3044. })
  3045. .catch((err)=>{
  3046. banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
  3047. })
  3048. .finally(()=>{
  3049. loader.style.display = "none";
  3050. });
  3051. },
  3052. }
  3053. module.exports = transactionDetails;
  3054. },{}],22:[function(require,module,exports){
  3055. let transactions = {
  3056. isPopulated: false,
  3057. display: function(Transaction){
  3058. if(!this.isPopulated){
  3059. let transactionsList = document.getElementById("transactionsList");
  3060. let dateDropdown = document.getElementById("dateDropdown");
  3061. let recipeDropdown = document.getElementById("recipeDropDown");
  3062. let template = document.getElementById("transaction").content.children[0];
  3063. let now = new Date();
  3064. let monthAgo = new Date(now.getFullYear(), now.getMonth() - 1, now.getDate());
  3065. document.getElementById("transFilDate1").valueAsDate = monthAgo;
  3066. document.getElementById("transFilDate2").valueAsDate = now;
  3067. dateDropdown.style.display = "none";
  3068. recipeDropdown.style.display = "none";
  3069. document.getElementById("dateFilterBtn").onclick = ()=>{this.toggleDropdown(dateDropdown)};
  3070. document.getElementById("recipeFilterBtn").onclick = ()=>{this.toggleDropdown(recipeDropdown)};
  3071. while(recipeDropdown.children.length > 0){
  3072. recipeDropdown.removeChild(recipeDropdown.firstChild);
  3073. }
  3074. for(let i = 0; i < merchant.recipes.length; i++){
  3075. let checkbox = document.createElement("input");
  3076. checkbox.type = "checkbox";
  3077. checkbox.recipe = merchant.recipes[i];
  3078. recipeDropdown.appendChild(checkbox);
  3079. let label = document.createElement("label");
  3080. label.innerText = merchant.recipes[i].name;
  3081. label.for = checkbox;
  3082. recipeDropdown.appendChild(label);
  3083. let brk = document.createElement("br");
  3084. recipeDropdown.appendChild(brk);
  3085. }
  3086. while(transactionsList.children.length > 0){
  3087. transactionsList.removeChild(transactionsList.firstChild);
  3088. }
  3089. let i = 0
  3090. const transactions = merchant.getTransactions();
  3091. while(i < transactions.length && i < 100){
  3092. let transactionDiv = template.cloneNode(true);
  3093. let transaction = transactions[i];
  3094. transactionDiv.onclick = ()=>{controller.openSidebar("transactionDetails", transaction)};
  3095. transactionsList.appendChild(transactionDiv);
  3096. let totalRecipes = 0;
  3097. let totalPrice = 0;
  3098. for(let j = 0; j < transactions[i].recipes.length; j++){
  3099. totalRecipes += transactions[i].recipes[j].quantity;
  3100. totalPrice += transactions[i].recipes[j].recipe.price * transactions[i].recipes[j].quantity;
  3101. }
  3102. transactionDiv.children[0].innerText = `${transactions[i].date.toLocaleDateString()} ${transactions[i].date.toLocaleTimeString()}`;
  3103. transactionDiv.children[1].innerText = `${totalRecipes} recipes sold`;
  3104. transactionDiv.children[2].innerText = `$${(totalPrice / 100).toFixed(2)}`;
  3105. i++;
  3106. }
  3107. document.getElementById("transFormSubmit").onsubmit = ()=>{this.submitFilter(Transaction)};
  3108. this.isPopulated = true;
  3109. }
  3110. },
  3111. submitFilter: function(Transaction){
  3112. event.preventDefault();
  3113. let data = {
  3114. startDate: document.getElementById("transFilDate1").valueAsDate,
  3115. endDate: document.getElementById("transFilDate2").valueAsDate,
  3116. recipes: []
  3117. }
  3118. if(data.startDate >= data.endDate){
  3119. banner.createError("START DATE CANNOT BE AFTER END DATE");
  3120. return;
  3121. }
  3122. let recipeChoices = document.getElementById("recipeDropDown");
  3123. for(let i = 0; i < recipeChoices.children.length; i += 3){
  3124. if(recipeChoices.children[i].checked){
  3125. data.recipes.push(recipeChoices.children[i].recipe.id);
  3126. }
  3127. }
  3128. if(data.recipes.length === 0){
  3129. for(let i = 0; i < merchant.recipes.length; i++){
  3130. data.recipes.push(merchant.recipes[i].id);
  3131. }
  3132. }
  3133. let loader = document.getElementById("loaderContainer");
  3134. loader.style.display = "flex";
  3135. fetch("/transaction", {
  3136. method: "POST",
  3137. headers: {
  3138. "Content-Type": "application/json;charset=utf-8"
  3139. },
  3140. body: JSON.stringify(data)
  3141. })
  3142. .then((response) => response.json())
  3143. .then((response)=>{
  3144. if(typeof(response) === "string"){
  3145. banner.createError(response);
  3146. }else{
  3147. let transactionList = document.getElementById("transactionsList");
  3148. let template = document.getElementById("transaction").content.children[0];
  3149. while(transactionList.children.length > 0){
  3150. transactionList.removeChild(transactionList.firstChild);
  3151. }
  3152. for(let i = 0; i < response.length; i++){
  3153. let transactionDiv = template.cloneNode(true);
  3154. let recipeCount = 0;
  3155. let cost = 0;
  3156. let transaction = new Transaction(
  3157. response[i]._id,
  3158. response[i].date,
  3159. response[i].recipes,
  3160. merchant
  3161. );
  3162. for(let j = 0; j < transaction.recipes.length; j++){
  3163. recipeCount += transaction.recipes[j].quantity;
  3164. cost += transaction.recipes[j].quantity * transaction.recipes[j].recipe.price;
  3165. }
  3166. transactionDiv.children[0].innerText = `${transaction.date.toLocaleDateString()} ${transaction.date.toLocaleTimeString()}`;
  3167. transactionDiv.children[1].innerText = `${recipeCount} recipes sold`;
  3168. transactionDiv.children[2].innerText = `$${(cost / 100).toFixed(2)}`;
  3169. transactionDiv.onclick = ()=>{controller.openSidebar("transactionDetails", transaction)};
  3170. transactionList.appendChild(transactionDiv);
  3171. }
  3172. }
  3173. })
  3174. .catch((err)=>{
  3175. banner.createError("UNABLE TO DISPLAY THE TRANSACTIONS");
  3176. })
  3177. .finally(()=>{
  3178. loader.style.display = "none";
  3179. });
  3180. },
  3181. toggleDropdown: function(dropdown){
  3182. event.preventDefault();
  3183. let polyline = dropdown.parentElement.children[0].children[1].children[0].children[0];
  3184. if(dropdown.style.display === "none"){
  3185. dropdown.style.display = "block";
  3186. polyline.setAttribute("points", "18 15 12 9 6 15");
  3187. }else{
  3188. dropdown.style.display = "none";
  3189. polyline.setAttribute("points", "6 9 12 15 18 9");
  3190. }
  3191. }
  3192. }
  3193. module.exports = transactions;
  3194. },{}]},{},[7]);