bundle.js 142 KB

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