bundle.js 142 KB

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