bundle.js 140 KB

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