bundle.js 141 KB

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