bundle.js 142 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011
  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. class MerchantIngredient{
  99. constructor(ingredient, quantity){
  100. this._quantity = quantity;
  101. this._ingredient = ingredient;
  102. }
  103. get ingredient(){
  104. return this._ingredient;
  105. }
  106. get quantity(){
  107. switch(this._ingredient.unit){
  108. case "g":return this._quantity;
  109. case "kg": return this._quantity / 1000;
  110. case "oz": return this._quantity / 28.3495;
  111. case "lb": return this._quantity / 453.5924;
  112. case "ml": return this._quantity * 1000;
  113. case "l": return this._quantity;
  114. case "tsp": return this._quantity * 202.8842;
  115. case "tbsp": return this._quantity * 67.6278;
  116. case "ozfl": return this._quantity * 33.8141;
  117. case "cup": return this._quantity * 4.1667;
  118. case "pt": return this._quantity * 2.1134;
  119. case "qt": return this._quantity * 1.0567;
  120. case "gal": return this._quantity / 3.7854;
  121. case "mm": return this._quantity * 1000;
  122. case "cm": return this._quantity * 100;
  123. case "m": return this._quantity;
  124. case "in": return this._quantity * 39.3701;
  125. case "ft": return this._quantity * 3.2808;
  126. default: return this._quantity;
  127. }
  128. }
  129. updateQuantity(quantity){
  130. this._quantity += this.convertToBase(quantity);
  131. }
  132. convertToBase(quantity){
  133. switch(this._ingredient.unit){
  134. case "g": return quantity;
  135. case "kg": return quantity * 1000;
  136. case "oz": return quantity * 28.3495;
  137. case "lb": return quantity * 453.5924;
  138. case "ml": return quantity / 1000;
  139. case "l": return quantity;
  140. case "tsp": return quantity / 202.8842;
  141. case "tbsp": return quantity / 67.6278;
  142. case "ozfl": return quantity / 33.8141;
  143. case "cup": return quantity / 4.1667;
  144. case "pt": return quantity / 2.1134;
  145. case "qt": return quantity / 1.0567;
  146. case "gal": return quantity * 3.7854;
  147. case "mm": return quantity / 1000;
  148. case "cm": return quantity / 100;
  149. case "m": return quantity;
  150. case "in": return quantity / 39.3701;
  151. case "ft": return quantity / 3.2808;
  152. default: return quantity;
  153. }
  154. }
  155. getQuantityDisplay(){
  156. return `${this.quantity.toFixed(2)} ${this._ingredient.unit.toUpperCase()}`;
  157. }
  158. }
  159. class Merchant{
  160. constructor(oldMerchant, transactions, modules){
  161. this._modules = modules;
  162. this._name = oldMerchant.name;
  163. this._email = oldMerchant.email;
  164. this._pos = oldMerchant.pos;
  165. this._ingredients = [];
  166. this._recipes = [];
  167. this._transactions = [];
  168. this._orders = [];
  169. //populate ingredients
  170. for(let i = 0; i < oldMerchant.inventory.length; i++){
  171. const ingredient = new modules.Ingredient(
  172. oldMerchant.inventory[i].ingredient._id,
  173. oldMerchant.inventory[i].ingredient.name,
  174. oldMerchant.inventory[i].ingredient.category,
  175. oldMerchant.inventory[i].ingredient.unitType,
  176. oldMerchant.inventory[i].defaultUnit,
  177. this,
  178. oldMerchant.inventory[i].ingredient.unitSize
  179. );
  180. const merchantIngredient = new MerchantIngredient(
  181. ingredient,
  182. oldMerchant.inventory[i].quantity,
  183. );
  184. this._ingredients.push(merchantIngredient);
  185. }
  186. //populate recipes
  187. for(let i = 0; i < oldMerchant.recipes.length; i++){
  188. let ingredients = [];
  189. for(let j = 0; j < oldMerchant.recipes[i].ingredients.length; j++){
  190. const ingredient = oldMerchant.recipes[i].ingredients[j];
  191. for(let k = 0; k < this._ingredients.length; k++){
  192. if(ingredient.ingredient === this._ingredients[k].ingredient.id){
  193. ingredients.push({
  194. ingredient: this._ingredients[k].ingredient.id,
  195. quantity: ingredient.quantity
  196. });
  197. break;
  198. }
  199. }
  200. }
  201. this._recipes.push(new this._modules.Recipe(
  202. oldMerchant.recipes[i]._id,
  203. oldMerchant.recipes[i].name,
  204. oldMerchant.recipes[i].price,
  205. ingredients,
  206. this
  207. ));
  208. }
  209. //populate transactions
  210. for(let i = 0; i < transactions.length; i++){
  211. this._transactions.push(new modules.Transaction(
  212. transactions[i]._id,
  213. transactions[i].date,
  214. transactions[i].recipes,
  215. this
  216. ));
  217. }
  218. }
  219. get modules(){
  220. return this._modules;
  221. }
  222. get name(){
  223. return this._name;
  224. }
  225. set name(name){
  226. this._name = name;
  227. }
  228. get email(){
  229. return this._email;
  230. }
  231. set email(email){
  232. this._email = email;
  233. }
  234. get pos(){
  235. return this._pos;
  236. }
  237. get ingredients(){
  238. return this._ingredients;
  239. }
  240. /*
  241. ingredient: {
  242. _id: String,
  243. name: String,
  244. category: String,
  245. unitType: String,
  246. specialUnit: String || undefined,
  247. unitSize: Number || undefined
  248. }
  249. quantity: Number
  250. defaultUnit: String
  251. */
  252. addIngredient(ingredient, quantity, defaultUnit){
  253. const createdIngredient = new this._modules.Ingredient(
  254. ingredient._id,
  255. ingredient.name,
  256. ingredient.category,
  257. ingredient.unitType,
  258. defaultUnit,
  259. this,
  260. ingredient.unitSize
  261. );
  262. const merchantIngredient = new MerchantIngredient(createdIngredient, quantity);
  263. this._ingredients.push(merchantIngredient);
  264. this._modules.home.isPopulated = false;
  265. this._modules.ingredients.isPopulated = false;
  266. }
  267. removeIngredient(ingredient){
  268. const index = this._ingredients.indexOf(ingredient);
  269. if(index === undefined){
  270. return false;
  271. }
  272. this._ingredients.splice(index, 1);
  273. this._modules.home.isPopulated = false;
  274. this._modules.ingredients.isPopulated = false;
  275. }
  276. getIngredient(id){
  277. for(let i = 0; i < this._ingredients.length; i++){
  278. if(this._ingredients[i].ingredient.id === id){
  279. return this._ingredients[i];
  280. }
  281. }
  282. }
  283. get recipes(){
  284. return this._recipes;
  285. }
  286. addRecipe(id, name, price, ingredients){
  287. let recipe = new this._modules.Recipe(id, name, price, ingredients, this);
  288. this._recipes.push(recipe);
  289. this._modules.recipeBook.isPopulated = false;
  290. }
  291. removeRecipe(recipe){
  292. const index = this._recipes.indexOf(recipe);
  293. if(index === undefined){
  294. return false;
  295. }
  296. this._recipes.splice(index, 1);
  297. this._modules.recipeBook.isPopulated = false;
  298. }
  299. /*
  300. recipe = {
  301. name: required,
  302. price: required,
  303. ingredients: [{
  304. ingredient: id of ingredient,
  305. quantity: quantity of ingredient
  306. }]
  307. }
  308. */
  309. updateRecipe(recipe){
  310. for(let i = 0; i < this._recipes.length; i++){
  311. if(this._recipes[i].id === recipe._id){
  312. this._recipes[i].name = recipe.name;
  313. this._recipes[i].price = recipe.price;
  314. this._recipes[i].removeIngredients();
  315. for(let j = 0; j < recipe.ingredients.length; j++){
  316. for(let k = 0; k < this._ingredients.length; k++){
  317. if(this._ingredients[k].ingredient.id === recipe.ingredients[j].ingredient){
  318. this._recipes[i].addIngredient(
  319. this._ingredients[k].ingredient,
  320. recipe.ingredients[j].quantity
  321. );
  322. break;
  323. }
  324. }
  325. }
  326. break;
  327. }
  328. }
  329. this._modules.recipeBook.isPopulated = false;
  330. }
  331. get transactions(){
  332. return this._transactions;
  333. }
  334. getTransactions(from = 0, to = new Date()){
  335. if(merchant._transactions.length <= 0){
  336. return [];
  337. }
  338. if(from === 0){
  339. from = this._transactions[this._transactions.length-1].date;
  340. }
  341. const {start, end} = this.getTransactionIndices(from, to);
  342. return this._transactions.slice(start, end + 1);
  343. }
  344. addTransaction(transaction){
  345. transaction = new this._modules.Transaction(
  346. transaction._id,
  347. transaction.date,
  348. transaction.recipes,
  349. this
  350. );
  351. this._transactions.push(transaction);
  352. this._transactions.sort((a, b)=>{
  353. if(a.date > b.date){
  354. return -1;
  355. }
  356. return 1;
  357. });
  358. let ingredients = {};
  359. for(let i = 0; i < transaction.recipes.length; i++){
  360. const recipe = transaction.recipes[i];
  361. for(let j = 0; j < recipe.recipe.ingredients.length; j++){
  362. const ingredient = recipe.recipe.ingredients[j];
  363. if(ingredients[ingredient.ingredient.id]){
  364. ingredients[ingredient.ingredient.id] += recipe.quantity * ingredient.quantity;
  365. }else{
  366. ingredients[ingredient.ingredient.id] = recipe.quantity * ingredient.quantity;
  367. }
  368. }
  369. }
  370. const keys = Object.keys(ingredients);
  371. for(let i = 0; i < keys.length; i++){
  372. for(let j = 0; j < this._ingredients.length; j++){
  373. if(keys[i] === this._ingredients[j].ingredient.id){
  374. this._ingredients[j].updateQuantity(-ingredients[keys[i]]);
  375. }
  376. }
  377. }
  378. this._modules.home.isPopulated = false;
  379. this._modules.ingredients.isPopulated = false;
  380. this._modules.analytics.newData = true;
  381. }
  382. removeTransaction(transaction){
  383. const index = this._transactions.indexOf(transaction);
  384. if(index === undefined){
  385. return false;
  386. }
  387. this._transactions.splice(index, 1);
  388. let ingredients = {};
  389. for(let i = 0; i < transaction.recipes.length; i++){
  390. const recipe = transaction.recipes[i];
  391. for(let j = 0; j < recipe.recipe.ingredients.length; j++){
  392. const ingredient = recipe.recipe.ingredients[j];
  393. if(ingredients[ingredient.ingredient.id]){
  394. ingredients[ingredient.ingredient.id] += ingredient.quantity * recipe.quantity;
  395. }else{
  396. ingredients[ingredient.ingredient.id] = ingredient.quantity * recipe.quantity;
  397. }
  398. }
  399. }
  400. const keys = Object.keys(ingredients);
  401. for(let i = 0; i < keys.length; i++){
  402. for(let j = 0; j < this._ingredients.length; j++){
  403. if(keys[i] === this._ingredients[j].ingredient.id){
  404. this._ingredients[j].updateQuantity(ingredients[keys[i]]);
  405. break;
  406. }
  407. }
  408. }
  409. this._modules.home.isPopulated = false;
  410. this._modules.ingredients.isPopulated = false;
  411. this._modules.analytics.newData = true;
  412. }
  413. get orders(){
  414. return this._orders;
  415. }
  416. clearOrders(){
  417. this._orders = [];
  418. }
  419. addOrder(data, isNew = false){
  420. let order = new this._modules.Order(
  421. data._id,
  422. data.name,
  423. data.date,
  424. data.taxes,
  425. data.fees,
  426. data.ingredients,
  427. this
  428. );
  429. this._orders.push(order);
  430. if(isNew){
  431. for(let i = 0; i < order.ingredients.length; i++){
  432. for(let j = 0; j < this._ingredients.length; j++){
  433. if(order.ingredients[i].ingredient === this._ingredients[j].ingredient){
  434. this._ingredients[j].updateQuantity(order.ingredients[i].quantity);
  435. break;
  436. }
  437. }
  438. }
  439. }
  440. this._modules.ingredients.isPopulated = false;
  441. this._modules.orders.isPopulated = false;
  442. }
  443. removeOrder(order){
  444. const index = this._orders.indexOf(order);
  445. if(index === undefined){
  446. return false;
  447. }
  448. this._orders.splice(index, 1);
  449. for(let i = 0; i < order.ingredients.length; i++){
  450. for(let j = 0; j < this._ingredients.length; j++){
  451. if(order.ingredients[i].ingredient === this._ingredients[j].ingredient){
  452. this._ingredients[j].updateQuantity(-order.ingredients[i].quantity);
  453. break;
  454. }
  455. }
  456. }
  457. this._modules.ingredients.isPopulated = false;
  458. this._modules.orders.isPopulated = false;
  459. }
  460. get units(){
  461. return this._units;
  462. }
  463. getRevenue(from, to = new Date()){
  464. const {start, end} = this.getTransactionIndices(from, to);
  465. let total = 0;
  466. for(let i = start; i <= end; i++){
  467. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  468. for(let k = 0; k < this.recipes.length; k++){
  469. if(this._transactions[i].recipes[j].recipe === this.recipes[k]){
  470. total += this._transactions[i].recipes[j].quantity * this.recipes[k].price;
  471. }
  472. }
  473. }
  474. }
  475. return total / 100;
  476. }
  477. /*
  478. Gets the quantity of each ingredient sold between two dates (dateRange)
  479. Inputs:
  480. dateRange: list containing a start date and an end date
  481. Return:
  482. [{
  483. ingredient: Ingredient object,
  484. quantity: quantity of ingredient sold in default unit
  485. }]
  486. */
  487. getIngredientsSold(from, to = new Date()){
  488. let recipes = this.getRecipesSold(from, to);
  489. let ingredientList = [];
  490. for(let i = 0; i < recipes.length; i++){
  491. for(let j = 0; j < recipes[i].recipe.ingredients.length; j++){
  492. let exists = false;
  493. for(let k = 0; k < ingredientList.length; k++){
  494. if(ingredientList[k].ingredient === recipes[i].recipe.ingredients[j].ingredient){
  495. exists = true;
  496. ingredientList[k].quantity += recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity;
  497. break;
  498. }
  499. }
  500. if(!exists){
  501. ingredientList.push({
  502. ingredient: recipes[i].recipe.ingredients[j].ingredient,
  503. quantity: recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity
  504. });
  505. }
  506. }
  507. }
  508. return ingredientList;
  509. }
  510. /*
  511. Gets the quantity of a single ingredient sold between two dates
  512. Inputs:
  513. ingredient = MerchantIngredient object to find
  514. from = start Date
  515. to = end Date
  516. return: quantity sold in default unit
  517. */
  518. getSingleIngredientSold(ingredient, from, to = new Date()){
  519. const {start, end} = this.getTransactionIndices(from, to);
  520. let total = 0;
  521. for(let i = start; i < end; i++){
  522. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  523. for(let k = 0; k < this._transactions[i].recipes[j].recipe.ingredients.length; k++){
  524. if(this._transactions[i].recipes[j].recipe.ingredients[k].ingredient === ingredient.ingredient){
  525. total += this._transactions[i].recipes[j].recipe.ingredients[k].quantity;
  526. break;
  527. }
  528. }
  529. }
  530. }
  531. return total;
  532. }
  533. /*
  534. Gets the number of recipes sold between two dates (dateRange)
  535. Inputs:
  536. dateRange: array containing a start date and an end date
  537. Return:
  538. [{
  539. recipe: a recipe object
  540. quantity: quantity of the recipe sold
  541. }]
  542. */
  543. getRecipesSold(from = 0, to = new Date()){
  544. if(from === 0){
  545. from = this._transactions[0].date;
  546. }
  547. const {start, end} = this.getTransactionIndices(from, to);
  548. let recipeList = [];
  549. for(let i = start; i <= end; i++){
  550. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  551. let exists = false;
  552. for(let k = 0; k < recipeList.length; k++){
  553. if(recipeList[k].recipe === this._transactions[i].recipes[j].recipe){
  554. exists = true;
  555. recipeList[k].quantity += this._transactions[i].recipes[j].quantity;
  556. break;
  557. }
  558. }
  559. if(!exists){
  560. recipeList.push({
  561. recipe: this._transactions[i].recipes[j].recipe,
  562. quantity: this._transactions[i].recipes[j].quantity
  563. });
  564. }
  565. }
  566. }
  567. return recipeList;
  568. }
  569. /*
  570. Groups all of the merchant's ingredients by their category
  571. Return: [{
  572. name: category name,
  573. ingredients: [MerchantIngredient Object]
  574. }]
  575. */
  576. categorizeIngredients(){
  577. let ingredientsByCategory = [];
  578. for(let i = 0; i < this.ingredients.length; i++){
  579. let categoryExists = false;
  580. for(let j = 0; j < ingredientsByCategory.length; j++){
  581. if(this.ingredients[i].ingredient.category === ingredientsByCategory[j].name){
  582. ingredientsByCategory[j].ingredients.push(this.ingredients[i]);
  583. categoryExists = true;
  584. break;
  585. }
  586. }
  587. if(!categoryExists){
  588. ingredientsByCategory.push({
  589. name: this.ingredients[i].ingredient.category,
  590. ingredients: [this.ingredients[i]]
  591. });
  592. }
  593. }
  594. return ingredientsByCategory;
  595. }
  596. getRecipesForIngredient(ingredient){
  597. let recipes = [];
  598. for(let i = 0; i < this._recipes.length; i++){
  599. for(let j = 0; j < this._recipes[i].ingredients.length; j++){
  600. if(this._recipes[i].ingredients[j].ingredient === ingredient){
  601. recipes.push(this._recipes[i]);
  602. break;
  603. }
  604. }
  605. }
  606. return recipes;
  607. }
  608. getTransactionIndices(from, to){
  609. let start, end;
  610. for(let i = this._transactions.length - 1; i >= 0; i--){
  611. if(this._transactions[i].date >= from){
  612. end = i;
  613. break;
  614. }
  615. }
  616. for(let i = 0; i < this._transactions.length; i++){
  617. if(this._transactions[i].date < to){
  618. start = i;
  619. break;
  620. }
  621. }
  622. if(end === undefined){
  623. return false;
  624. }
  625. return {start: start, end: end};
  626. }
  627. }
  628. module.exports = Merchant;
  629. },{}],3:[function(require,module,exports){
  630. class OrderIngredient{
  631. constructor(ingredient, quantity, pricePerUnit){
  632. this._ingredient = ingredient;
  633. this._quantity = quantity;
  634. this._pricePerUnit = pricePerUnit;
  635. }
  636. get ingredient(){
  637. return this._ingredient;
  638. }
  639. get quantity(){
  640. switch(this._ingredient.unit){
  641. case "kg": return this._quantity / 1000;
  642. case "oz": return this._quantity / 28.3495;
  643. case "lb": return this._quantity / 453.5924;
  644. case "ml": return this._quantity * 1000;
  645. case "tsp": return this._quantity * 202.8842;
  646. case "tbsp": return this._quantity * 67.6278;
  647. case "ozfl": return this._quantity * 33.8141;
  648. case "cup": return this._quantity * 4.1667;
  649. case "pt": return this._quantity * 2.1134;
  650. case "qt": return this._quantity * 1.0567;
  651. case "gal": return this._quantity / 3.7854;
  652. case "mm": return this._quantity * 1000;
  653. case "cm": return this._quantity * 100;
  654. case "in": return this._quantity * 39.3701;
  655. case "ft": return this._quantity * 3.2808;
  656. default: return this._quantity;
  657. }
  658. }
  659. updateQuantity(quantity){
  660. this._quantity += this.convertToBase(quantity);
  661. }
  662. convertToBase(quantity){
  663. switch(this._ingredient.unit){
  664. case "g": return quantity;
  665. case "kg": return quantity * 1000;
  666. case "oz": return quantity * 28.3495;
  667. case "lb": return quantity * 453.5924;
  668. case "ml": return quantity / 1000;
  669. case "l": return quantity;
  670. case "tsp": return quantity / 202.8842;
  671. case "tbsp": return quantity / 67.6278;
  672. case "ozfl": return quantity / 33.8141;
  673. case "cup": return quantity / 4.1667;
  674. case "pt": return quantity / 2.1134;
  675. case "qt": return quantity / 1.0567;
  676. case "gal": return quantity * 3.7854;
  677. case "mm": return quantity / 1000;
  678. case "cm": return quantity / 100;
  679. case "m": return quantity;
  680. case "in": return quantity / 39.3701;
  681. case "ft": return quantity / 3.2808;
  682. default: return quantity;
  683. }
  684. }
  685. get pricePerUnit(){
  686. switch(this._ingredient.unit){
  687. case "g": return this._pricePerUnit / 100;
  688. case "kg": return (this._pricePerUnit * 1000) / 100;
  689. case "oz": return (this._pricePerUnit * 28.3495) / 100;
  690. case "lb": return (this._pricePerUnit * 453.5924) / 100;
  691. case "ml": return (this._pricePerUnit / 1000) / 100;
  692. case "l": return this._pricePerUnit / 100;
  693. case "tsp": return (this._pricePerUnit / 202.8842) / 100;
  694. case "tbsp": return (this._pricePerUnit / 67.6278) / 100;
  695. case "ozfl": return (this._pricePerUnit / 33.8141) / 100;
  696. case "cup": return (this._pricePerUnit / 4.1667) / 100;
  697. case "pt": return (this._pricePerUnit / 2.1134) / 100;
  698. case "qt": return (this._pricePerUnit / 1.0567) / 100;
  699. case "gal": return (this._pricePerUnit * 3.7854) / 100;
  700. case "mm": return (this._pricePerUnit / 1000) / 100;
  701. case "cm": return (this._pricePerUnit / 100) / 100;
  702. case "m": return this._pricePerUnit / 100;
  703. case "in": return (this._pricePerUnit / 39.3701) / 100;
  704. case "ft": return (this._pricePerUnit / 3.2808) / 100;
  705. default: return this._pricePerUnit / 100;
  706. }
  707. }
  708. cost(){
  709. return (this._quantity * this._pricePerUnit) / 100;
  710. }
  711. }
  712. /*
  713. Order Object
  714. id = id of order in the database
  715. name = name/id of order, if any
  716. date = Date Object for when the order was created
  717. taxes = User entered taxes associated with the order
  718. fees = User entered fees associated with the order
  719. ingredients = [{
  720. ingredient: Ingredient Object,
  721. quantity: quantity of ingredient sold,
  722. pricePerUnit: price of purchase (per base unit)
  723. }]
  724. parent = the merchant that it belongs to
  725. */
  726. class Order{
  727. constructor(id, name, date, taxes, fees, ingredients, parent){
  728. this._id = id;
  729. this._name = name;
  730. this._date = new Date(date);
  731. this._taxes = taxes;
  732. this._fees = fees;
  733. this._ingredients = [];
  734. this._parent = parent;
  735. for(let i = 0; i < ingredients.length; i++){
  736. for(let j = 0; j < merchant.ingredients.length; j++){
  737. if(merchant.ingredients[j].ingredient.id === ingredients[i].ingredient){
  738. let thing = new OrderIngredient(
  739. merchant.ingredients[j].ingredient,
  740. ingredients[i].quantity,
  741. ingredients[i].pricePerUnit
  742. );
  743. this._ingredients.push(thing);
  744. break;
  745. }
  746. }
  747. }
  748. this._parent.modules.ingredients.isPopulated = false;
  749. }
  750. get id(){
  751. return this._id;
  752. }
  753. get name(){
  754. return this._name;
  755. }
  756. get date(){
  757. return this._date;
  758. }
  759. get taxes(){
  760. return this._taxes / 100;
  761. }
  762. get fees(){
  763. return this._fees / 100;
  764. }
  765. get parent(){
  766. return this._parent;
  767. }
  768. get ingredients(){
  769. return this._ingredients;
  770. }
  771. getIngredientCost(){
  772. let sum = 0;
  773. for(let i = 0; i < this._ingredients.length; i++){
  774. sum += this._ingredients[i].cost();
  775. }
  776. return sum;
  777. }
  778. getTotalCost(){
  779. return (this.getIngredientCost() + this.taxes + this.fees);
  780. }
  781. }
  782. module.exports = Order;
  783. },{}],4:[function(require,module,exports){
  784. class RecipeIngredient{
  785. constructor(ingredient, quantity){
  786. this._ingredient = ingredient;
  787. this._quantity = quantity;
  788. }
  789. get ingredient(){
  790. return this._ingredient;
  791. }
  792. get quantity(){
  793. switch(this._ingredient.unit){
  794. case "g":return this._quantity;
  795. case "kg": return this._quantity / 1000;
  796. case "oz": return this._quantity / 28.3495;
  797. case "lb": return this._quantity / 453.5924;
  798. case "ml": return this._quantity * 1000;
  799. case "l": return this._quantity;
  800. case "tsp": return this._quantity * 202.8842;
  801. case "tbsp": return this._quantity * 67.6278;
  802. case "ozfl": return this._quantity * 33.8141;
  803. case "cup": return this._quantity * 4.1667;
  804. case "pt": return this._quantity * 2.1134;
  805. case "qt": return this._quantity * 1.0567;
  806. case "gal": return this._quantity / 3.7854;
  807. case "mm": return this._quantity * 1000;
  808. case "cm": return this._quantity * 100;
  809. case "m": return this._quantity;
  810. case "in": return this._quantity * 39.3701;
  811. case "ft": return this._quantity * 3.2808;
  812. default: return this._quantity;
  813. }
  814. }
  815. set quantity(quantity){
  816. this_quantity = this.convertToBase(quantity);
  817. }
  818. getQuantityDisplay(){
  819. return `${this.quantity.toFixed(2)} ${this._ingredient.unit.toUpperCase()}`;
  820. }
  821. convertToBase(quantity){
  822. switch(this._ingredient.unit){
  823. case "g": return quantity;
  824. case "kg": return quantity * 1000;
  825. case "oz": return quantity * 28.3495;
  826. case "lb": return quantity * 453.5924;
  827. case "ml": return quantity / 1000;
  828. case "l": return quantity;
  829. case "tsp": return quantity / 202.8842;
  830. case "tbsp": return quantity / 67.6278;
  831. case "ozfl": return quantity / 33.8141;
  832. case "cup": return quantity / 4.1667;
  833. case "pt": return quantity / 2.1134;
  834. case "qt": return quantity / 1.0567;
  835. case "gal": return quantity * 3.7854;
  836. case "mm": return quantity / 1000;
  837. case "cm": return quantity / 100;
  838. case "m": return quantity;
  839. case "in": return quantity / 39.3701;
  840. case "ft": return quantity / 3.2808;
  841. default: return quantity;
  842. }
  843. }
  844. }
  845. /*
  846. Recipe Object
  847. id = database id of recipe
  848. name = name of recipe
  849. price = price of recipe in cents
  850. ingredients = [{
  851. ingredient: Ingredient Object,
  852. quantity: quantity of the ingredient within the recipe (stored as base unit, i.e grams)
  853. }]
  854. parent = merchant that it belongs to
  855. */
  856. class Recipe{
  857. constructor(id, name, price, ingredients, parent){
  858. this._id = id;
  859. this._name = name;
  860. this._price = price;
  861. this._parent = parent;
  862. this._ingredients = [];
  863. for(let i = 0; i < ingredients.length; i++){
  864. const ingredient = parent.getIngredient(ingredients[i].ingredient);
  865. const recipeIngredient = new RecipeIngredient(
  866. ingredient.ingredient,
  867. ingredients[i].quantity
  868. );
  869. this._ingredients.push(recipeIngredient);
  870. }
  871. }
  872. get id(){
  873. return this._id;
  874. }
  875. get name(){
  876. return this._name;
  877. }
  878. set name(name){
  879. this._name = name;
  880. }
  881. get price(){
  882. return this._price / 100;
  883. }
  884. set price(price){
  885. this._price = price;
  886. }
  887. get parent(){
  888. return this._parent;
  889. }
  890. get ingredients(){
  891. return this._ingredients;
  892. }
  893. addIngredient(ingredient, quantity){
  894. let recipeIngredient = new RecipeIngredient(ingredient, quantity);
  895. this._ingredients.push(recipeIngredient);
  896. this._parent.modules.recipeBook.isPopulated = false;
  897. this._parent.modules.analytics.isPopulated = false;
  898. }
  899. removeIngredients(){
  900. this._ingredients = [];
  901. }
  902. }
  903. module.exports = Recipe;
  904. },{}],5:[function(require,module,exports){
  905. class TransactionRecipe{
  906. constructor(recipe, quantity){
  907. this._recipe = recipe;
  908. this._quantity = quantity;
  909. }
  910. get recipe(){
  911. return this._recipe;
  912. }
  913. get quantity(){
  914. return this._quantity;
  915. }
  916. }
  917. class Transaction{
  918. constructor(id, date, recipes, parent){
  919. date = new Date(date);
  920. this._id = id;
  921. this._parent = parent;
  922. this._date = date;
  923. this._recipes = [];
  924. for(let i = 0; i < recipes.length; i++){
  925. for(let j = 0; j < parent.recipes.length; j++){
  926. if(recipes[i].recipe === parent.recipes[j].id){
  927. const transactionRecipe = new TransactionRecipe(
  928. parent.recipes[j],
  929. recipes[i].quantity
  930. )
  931. this._recipes.push(transactionRecipe);
  932. break;
  933. }
  934. }
  935. }
  936. }
  937. get id(){
  938. return this._id;
  939. }
  940. get parent(){
  941. return this._parent;
  942. }
  943. get date(){
  944. return this._date;
  945. }
  946. get recipes(){
  947. return this._recipes;
  948. }
  949. /*
  950. Gets the quantity for a given ingredient
  951. */
  952. getIngredientQuantity(ingredient){
  953. let quantity = 0;
  954. for(let i = 0; i < this._recipes.length; i++){
  955. const recipe = this._recipes[i].recipe;
  956. for(let j = 0; j < recipe.ingredients.length; j++){
  957. if(recipe.ingredients[j].ingredient === ingredient){
  958. quantity += recipe.ingredients[j].quantity * this._recipes[i].quantity;
  959. break;
  960. }
  961. }
  962. }
  963. return quantity;
  964. }
  965. }
  966. module.exports = Transaction;
  967. },{}],6:[function(require,module,exports){
  968. const home = require("./strands/home.js");
  969. const ingredients = require("./strands/ingredients.js");
  970. const recipeBook = require("./strands/recipeBook.js");
  971. const analytics = require("./strands/analytics.js");
  972. const orders = require("./strands/orders.js");
  973. const transactions = require("./strands/transactions.js");
  974. const account = require("./strands/account.js");
  975. const ingredientDetails = require("./sidebars/ingredientDetails.js");
  976. const newIngredient = require("./sidebars/newIngredient.js");
  977. const editIngredient = require("./sidebars/editIngredient.js");
  978. const newOrder = require("./sidebars/newOrder.js");
  979. const newRecipe = require("./sidebars/newRecipe.js");
  980. const editRecipe = require("./sidebars/editRecipe.js");
  981. const newTransaction = require("./sidebars/newTransaction.js");
  982. const orderDetails = require("./sidebars/orderDetails.js");
  983. const orderFilter = require("./sidebars/orderFilter.js");
  984. const orderCalculator = require("./sidebars/orderCalculator.js");
  985. const recipeDetails = require("./sidebars/recipeDetails.js");
  986. const transactionDetails = require("./sidebars/transactionDetails.js");
  987. const transactionFilter = require("./sidebars/transactionFilter.js");
  988. const Merchant = require("./classes/Merchant.js");
  989. const Ingredient = require("./classes/Ingredient.js");
  990. const Recipe = require("./classes/Recipe.js");
  991. const Order = require("./classes/Order.js");
  992. const Transaction = require("./classes/Transaction.js");
  993. merchant = new Merchant(data.merchant, data.transactions, {
  994. home: home,
  995. ingredients: ingredients,
  996. transactions: transactions,
  997. recipeBook: recipeBook,
  998. analytics: analytics,
  999. orders: orders,
  1000. Ingredient: Ingredient,
  1001. Recipe: Recipe,
  1002. Transaction: Transaction,
  1003. Order: Order
  1004. });
  1005. controller = {
  1006. openStrand: function(strand, data = undefined){
  1007. this.closeSidebar();
  1008. let strands = document.querySelectorAll(".strand");
  1009. for(let i = 0; i < strands.length; i++){
  1010. strands[i].style.display = "none";
  1011. }
  1012. let buttons = document.querySelectorAll(".menuButton");
  1013. for(let i = 0; i < buttons.length - 1; i++){
  1014. buttons[i].classList = "menuButton";
  1015. buttons[i].disabled = false;
  1016. }
  1017. let activeButton = {};
  1018. switch(strand){
  1019. case "home":
  1020. activeButton = document.getElementById("homeBtn");
  1021. document.getElementById("homeStrand").style.display = "flex";
  1022. home.display();
  1023. break;
  1024. case "ingredients":
  1025. activeButton = document.getElementById("ingredientsBtn");
  1026. document.getElementById("ingredientsStrand").style.display = "flex";
  1027. ingredients.display();
  1028. break;
  1029. case "recipeBook":
  1030. activeButton = document.getElementById("recipeBookBtn");
  1031. document.getElementById("recipeBookStrand").style.display = "flex";
  1032. recipeBook.display(Recipe);
  1033. break;
  1034. case "analytics":
  1035. activeButton = document.getElementById("analyticsBtn");
  1036. document.getElementById("analyticsStrand").style.display = "flex";
  1037. analytics.display(Transaction);
  1038. break;
  1039. case "orders":
  1040. activeButton = document.getElementById("ordersBtn");
  1041. document.getElementById("ordersStrand").style.display = "flex";
  1042. orders.display();
  1043. break;
  1044. case "transactions":
  1045. activeButton = document.getElementById("transactionsBtn");
  1046. document.getElementById("transactionsStrand").style.display = "flex";
  1047. transactions.transactions = data;
  1048. transactions.display(Transaction);
  1049. break;
  1050. case "account":
  1051. activeButton = document.getElementById("accountBtn");
  1052. document.getElementById("accountStrand").style.display = "flex";
  1053. account.display();
  1054. break;
  1055. }
  1056. activeButton.classList = "menuButton active";
  1057. activeButton.disabled = true;
  1058. if(screen.height > screen.width || screen.width < 1200){
  1059. this.closeMenu();
  1060. }
  1061. },
  1062. /*
  1063. Open a specific sidebar
  1064. Input:
  1065. sidebar: the outermost element of the sidebar (must contain class sidebar)
  1066. */
  1067. openSidebar: function(sidebar, data = {}){
  1068. this.closeSidebar();
  1069. document.getElementById("sidebarDiv").classList = "sidebar";
  1070. document.getElementById(sidebar).style.display = "flex";
  1071. switch(sidebar){
  1072. case "ingredientDetails":
  1073. ingredientDetails.display(data, ingredients);
  1074. break;
  1075. case "newIngredient":
  1076. newIngredient.display(Ingredient);
  1077. break;
  1078. case "editIngredient":
  1079. editIngredient.display(data);
  1080. break;
  1081. case "recipeDetails":
  1082. recipeDetails.display(data);
  1083. break;
  1084. case "editRecipe":
  1085. editRecipe.display(data);
  1086. break;
  1087. case "addRecipe":
  1088. newRecipe.display(Recipe);
  1089. break;
  1090. case "orderDetails":
  1091. orderDetails.display(data);
  1092. break;
  1093. case "orderFilter":
  1094. orderFilter.display(Order);
  1095. break;
  1096. case "newOrder":
  1097. newOrder.display();
  1098. break;
  1099. case "orderCalculator":
  1100. orderCalculator.display();
  1101. break;
  1102. case "transactionDetails":
  1103. transactionDetails.display(data);
  1104. break;
  1105. case "transactionFilter":
  1106. transactionFilter.display(Transaction);
  1107. break;
  1108. case "newTransaction":
  1109. newTransaction.display(Transaction);
  1110. break;
  1111. }
  1112. if(screen.height > screen.width || screen.width < 1200){
  1113. document.querySelector(".contentBlock").style.display = "none";
  1114. document.getElementById("mobileMenuSelector").style.display = "none";
  1115. document.getElementById("sidebarCloser").style.display = "block";
  1116. }
  1117. },
  1118. closeSidebar: function(){
  1119. let sidebar = document.getElementById("sidebarDiv");
  1120. for(let i = 0; i < sidebar.children.length; i++){
  1121. if(sidebar.children[i].style.display !== "none"){
  1122. sidebar.children[i].style.display = "none";
  1123. let choosables = [];
  1124. switch(sidebar.children[i].id){
  1125. case "ingredientDetails":
  1126. choosables = document.querySelectorAll(".ingredient");
  1127. break;
  1128. case "transactionDetails":
  1129. choosables = document.getElementById("transactionsList").children;
  1130. break;
  1131. case "recipeDetails":
  1132. choosables = document.getElementById("recipeList").children;
  1133. break;
  1134. case "orderDetails":
  1135. choosables = document.getElementById("orderList").children;
  1136. break;
  1137. }
  1138. for(let i = 0; i < choosables.length; i++){
  1139. choosables[i].classList.remove("active");
  1140. }
  1141. }
  1142. }
  1143. sidebar.classList = "sidebarHide";
  1144. if(screen.height > screen.width || screen.width < 1200){
  1145. document.querySelector(".contentBlock").style.display = "flex";
  1146. document.getElementById("mobileMenuSelector").style.display = "block";
  1147. document.getElementById("sidebarCloser").style.display = "none";
  1148. }
  1149. },
  1150. openModal: function(str){
  1151. let modal = document.getElementById("modal");
  1152. modal.style.display = "flex";
  1153. document.getElementById("modalClose").addEventListener("click", this.closeModal);
  1154. let content = {};
  1155. switch(str){
  1156. case "ingredientSpreadsheet":
  1157. content = document.getElementById("modalSpreadsheetUpload");
  1158. content.style.display = "flex";
  1159. document.getElementById("modalSpreadsheetTitle").innerText = "ingredients";
  1160. document.getElementById("spreadsheetDownload").href = "/ingredients/download/spreadsheet";
  1161. content.onsubmit = newIngredient.submitSpreadsheet;
  1162. break;
  1163. case "recipeSpreadsheet":
  1164. content = document.getElementById("modalSpreadsheetUpload");
  1165. content.style.display = "flex";
  1166. document.getElementById("modalSpreadsheetTitle").innerText = "recipes";
  1167. document.getElementById("spreadsheetDownload").href = "/recipes/download/spreadsheet";
  1168. content.onsubmit = newRecipe.submitSpreadsheet;
  1169. break;
  1170. case "orderSpreadsheet":
  1171. content = document.getElementById("modalSpreadsheetUpload");
  1172. content.style.display = "flex";
  1173. document.getElementById("modalSpreadsheetTitle").innerText = "orders";
  1174. document.getElementById("spreadsheetDownload").href = "/orders/download/spreadsheet";
  1175. content.onsubmit = newOrder.submitSpreadsheet;
  1176. break;
  1177. case "transactionSpreadsheet":
  1178. content = document.getElementById("modalSpreadsheetUpload");
  1179. content.style.display = "flex";
  1180. document.getElementById("modalSpreadsheetTitle").innerText = "transactions";
  1181. document.getElementById("spreadsheetDownload").href = "/transactions/download/spreadsheet";
  1182. content.onsubmit = newTransaction.submitSpreadsheet;
  1183. }
  1184. },
  1185. closeModal: function(){
  1186. let modal = document.getElementById("modal");
  1187. let modalContent = document.getElementById("modalContent");
  1188. for(let i = 0; i < modalContent.children.length; i++){
  1189. modalContent.children[i].style.display = "none";
  1190. }
  1191. modal.style.display = "none";
  1192. },
  1193. createBanner: function(text, status){
  1194. let container = document.getElementById("bannerContainer");
  1195. let template = document.getElementById("banner").content.children[0];
  1196. let banner = template.cloneNode(true);
  1197. switch(status){
  1198. case "error":
  1199. banner.children[0].style.backgroundColor = "rgb(200, 0, 0)";
  1200. banner.children[0].children[0].style.display = "block";
  1201. break;
  1202. case "alert":
  1203. banner.children[0].style.backgroundColor = "rgb(230, 210, 0)";
  1204. banner.children[0].children[1].style.display = "block";
  1205. break;
  1206. case "success":
  1207. banner.children[0].style.backgroundColor = "rgb(0, 145, 55)";
  1208. banner.children[0].children[2].style.display = "block";
  1209. break;
  1210. }
  1211. banner.children[1].innerText = text;
  1212. container.appendChild(banner);
  1213. let timer = setTimeout(()=>{
  1214. container.removeChild(banner);
  1215. }, 10000);
  1216. banner.children[2].addEventListener("click", ()=>{
  1217. container.removeChild(banner);
  1218. clearTimeout(timer);
  1219. });
  1220. },
  1221. changeMenu: function(){
  1222. let menu = document.querySelector(".menu");
  1223. let buttons = document.querySelectorAll(".menuButton");
  1224. if(!menu.classList.contains("menuMinimized")){
  1225. menu.classList = "menu menuMinimized";
  1226. for(let i = 0; i < buttons.length; i++){
  1227. buttons[i].children[1].style.display = "none";
  1228. }
  1229. document.getElementById("max").style.display = "none";
  1230. document.getElementById("min").style.display = "flex";
  1231. }else if(menu.classList.contains("menuMinimized")){
  1232. menu.classList = "menu";
  1233. for(let i = 0; i < buttons.length; i++){
  1234. buttons[i].children[1].style.display = "block";
  1235. }
  1236. setTimeout(()=>{
  1237. document.getElementById("max").style.display = "flex";
  1238. document.getElementById("min").style.display = "none";
  1239. }, 150);
  1240. }
  1241. },
  1242. openMenu: function(){
  1243. document.getElementById("menu").style.display = "flex";
  1244. document.querySelector(".contentBlock").style.display = "none";
  1245. document.getElementById("mobileMenuSelector").onclick = ()=>{this.closeMenu()};
  1246. },
  1247. closeMenu: function(){
  1248. document.getElementById("menu").style.display = "none";
  1249. document.querySelector(".contentBlock").style.display = "flex";
  1250. document.getElementById("mobileMenuSelector").onclick = ()=>{this.openMenu()};
  1251. },
  1252. updateAnalytics: function(){
  1253. analytics.isPopulated = false;
  1254. }
  1255. }
  1256. //Add click listeners for menu buttons
  1257. document.getElementById("homeBtn").onclick = ()=>{controller.openStrand("home")};
  1258. document.getElementById("ingredientsBtn").onclick = ()=>{controller.openStrand("ingredients")};
  1259. document.getElementById("recipeBookBtn").onclick = ()=>{controller.openStrand("recipeBook")};
  1260. document.getElementById("analyticsBtn").onclick = ()=>{controller.openStrand("analytics")};
  1261. document.getElementById("ordersBtn").onclick = async ()=>{controller.openStrand("orders")};
  1262. document.getElementById("transactionsBtn").onclick = ()=>{controller.openStrand("transactions", merchant.getTransactions())};
  1263. document.getElementById("accountBtn").onclick = ()=>{controller.openStrand("account")};
  1264. controller.openStrand("home");
  1265. },{"./classes/Ingredient.js":1,"./classes/Merchant.js":2,"./classes/Order.js":3,"./classes/Recipe.js":4,"./classes/Transaction.js":5,"./sidebars/editIngredient.js":7,"./sidebars/editRecipe.js":8,"./sidebars/ingredientDetails.js":9,"./sidebars/newIngredient.js":10,"./sidebars/newOrder.js":11,"./sidebars/newRecipe.js":12,"./sidebars/newTransaction.js":13,"./sidebars/orderCalculator.js":14,"./sidebars/orderDetails.js":15,"./sidebars/orderFilter.js":16,"./sidebars/recipeDetails.js":17,"./sidebars/transactionDetails.js":18,"./sidebars/transactionFilter.js":19,"./strands/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){
  1266. let editIngredient = {
  1267. display: function(ingredient){
  1268. let buttonList = document.getElementById("unitButtons");
  1269. let quantLabel = document.getElementById("editIngQuantityLabel");
  1270. let specialLabel = document.getElementById("editSpecialLabel");
  1271. //Clear any existing data
  1272. while(buttonList.children.length > 0){
  1273. buttonList.removeChild(buttonList.firstChild);
  1274. }
  1275. //Populate basic fields
  1276. document.getElementById("editIngTitle").innerText = ingredient.ingredient.name;
  1277. document.getElementById("editIngName").value = ingredient.ingredient.name;
  1278. document.getElementById("editIngCategory").value = ingredient.ingredient.category;
  1279. quantLabel.innerText = `CURRENT STOCK (${ingredient.ingredient.unit.toUpperCase()})`;
  1280. document.getElementById("editIngSubmit").onclick = ()=>{this.submit(ingredient)};
  1281. //Make any changes for special ingredients
  1282. if(ingredient.ingredient.unit === "bottle"){
  1283. // quantLabel.innerText = "CURRENT STOCK (BOTTLES):";
  1284. specialLabel.style.display = "flex";
  1285. specialLabel.innerText = `BOTTLE SIZE (${ingredient.ingredient.unitType.toUpperCase()}):`;
  1286. let sizeInput = document.createElement("input");
  1287. sizeInput.id = "editIngSpecialSize";
  1288. sizeInput.type = "number";
  1289. sizeInput.min = "0";
  1290. sizeInput.step = "0.01";
  1291. sizeInput.value = ingredient.ingredient.unitSize.toFixed(2);
  1292. specialLabel.appendChild(sizeInput);
  1293. }else{
  1294. specialLabel.style.display = "none";
  1295. }
  1296. //Populate the unit buttons
  1297. const units = ingredient.ingredient.getPotentialUnits();
  1298. for(let i = 0; i < units.length; i++){
  1299. let button = document.createElement("button");
  1300. button.classList.add("unitButton");
  1301. button.innerText = units[i].toUpperCase();
  1302. button.onclick = ()=>{this.changeUnit(button)};
  1303. buttonList.appendChild(button);
  1304. if(units[i] === ingredient.ingredient.unit){
  1305. button.classList.add("unitActive");
  1306. }
  1307. }
  1308. let quantInput = document.createElement("input");
  1309. quantInput.id = "editIngQuantity";
  1310. quantInput.type = "number";
  1311. quantInput.min = "0";
  1312. quantInput.step = "0.01";
  1313. quantInput.value = ingredient.quantity.toFixed(2);
  1314. quantLabel.appendChild(quantInput);
  1315. },
  1316. changeUnit(button){
  1317. let buttons = document.getElementById("unitButtons");
  1318. for(let i = 0; i < buttons.children.length; i++){
  1319. buttons.children[i].classList.remove("unitActive");
  1320. }
  1321. button.classList.add("unitActive");
  1322. },
  1323. submit(ingredient){
  1324. const quantity = parseFloat(document.getElementById("editIngQuantityLabel").children[0].value);
  1325. let data = {
  1326. id: ingredient.ingredient.id,
  1327. name: document.getElementById("editIngName").value,
  1328. category: document.getElementById("editIngCategory").value
  1329. }
  1330. data.quantity = quantity;
  1331. //Get the measurement unit
  1332. let units = document.getElementById("unitButtons");
  1333. for(let i = 0; i < units.children.length; i++){
  1334. if(units.children[i].classList.contains("unitActive")){
  1335. data.unit = units.children[i].innerText.toLowerCase();
  1336. break;
  1337. }
  1338. }
  1339. let loader = document.getElementById("loaderContainer");
  1340. loader.style.display = "flex";
  1341. fetch("/ingredients/update", {
  1342. method: "put",
  1343. headers: {
  1344. "Content-Type": "application/json;charset=utf-8"
  1345. },
  1346. body: JSON.stringify(data)
  1347. })
  1348. .then(response => response.json())
  1349. .then((response)=>{
  1350. if(typeof(response) === "string"){
  1351. controller.createBanner(response, "error");
  1352. }else{
  1353. merchant.removeIngredient(merchant.getIngredient(response.ingredient._id));
  1354. merchant.addIngredient(response.ingredient, response.quantity, response.unit);
  1355. controller.openStrand("ingredients");
  1356. controller.createBanner("INGREDIENT UPDATED", "success");
  1357. }
  1358. })
  1359. .catch((err)=>{
  1360. controller.createBanner("SOMETHING WENT WRONG, PLEASE REFRESH THE PAGE", "error");
  1361. })
  1362. .finally(()=>{
  1363. loader.style.display = "none";
  1364. });
  1365. }
  1366. }
  1367. module.exports = editIngredient;
  1368. },{}],8:[function(require,module,exports){
  1369. let editRecipe = {
  1370. display: function(recipe){
  1371. let nameInput = document.getElementById("editRecipeName");
  1372. if(merchant.pos === "none"){
  1373. nameInput.value = recipe.name;
  1374. }else{
  1375. document.getElementById("editRecipeNoName").innertext = recipe.name;
  1376. nameInput.parentNode.style.display = "none";
  1377. }
  1378. //Populate ingredients
  1379. let ingredientList = document.getElementById("editRecipeIngList");
  1380. while(ingredientList.children.length > 0){
  1381. ingredientList.removeChild(ingredientList.firstChild);
  1382. }
  1383. let template = document.getElementById("editRecipeIng").content.children[0];
  1384. for(let i = 0; i < recipe.ingredients.length; i++){
  1385. let ingredientDiv = template.cloneNode(true);
  1386. ingredientDiv.children[0].onclick = ()=>{ingredientDiv.parentNode.removeChild(ingredientDiv)};
  1387. ingredientDiv.children[1].innerText = recipe.ingredients[i].ingredient.getNameAndUnit();
  1388. ingredientDiv.children[2].style.display = "none";
  1389. ingredientDiv.children[3].value = recipe.ingredients[i].quantity;
  1390. ingredientDiv.ingredient = recipe.ingredients[i];
  1391. ingredientList.appendChild(ingredientDiv);
  1392. }
  1393. document.getElementById("addRecIng").onclick = ()=>{this.newIngredient()};
  1394. document.getElementById("editRecipePrice").value = recipe.price;
  1395. document.getElementById("editRecipeSubmit").onclick = ()=>{this.submit(recipe)};
  1396. document.getElementById("editRecipeCancel").onclick = ()=>{controller.openSidebar("recipeDetails", recipe)};
  1397. },
  1398. newIngredient: function(){
  1399. let ingredientList = document.getElementById("editRecipeIngList");
  1400. let ingredientDiv = document.getElementById("editRecipeIng").content.children[0].cloneNode(true);
  1401. ingredientDiv.children[0].onclick = ()=>{ingredientDiv.parentNode.removeChild(ingredientDiv)};
  1402. ingredientDiv.children[1].style.display = "none";
  1403. ingredientDiv.children[3].value = "0.00";
  1404. //Populate selector
  1405. let categories = merchant.categorizeIngredients();
  1406. for(let i = 0; i < categories.length; i++){
  1407. let group = document.createElement("optgroup");
  1408. group.label = categories[i].name;
  1409. for(let j = 0; j < categories[i].ingredients.length; j++){
  1410. let option = document.createElement("option");
  1411. option.innerText = categories[i].ingredients[j].ingredient.getNameAndUnit();
  1412. option.ingredient = categories[i].ingredients[j];
  1413. group.appendChild(option);
  1414. }
  1415. ingredientDiv.children[2].appendChild(group);
  1416. }
  1417. ingredientList.appendChild(ingredientDiv);
  1418. },
  1419. submit: function(recipe){
  1420. let data = {
  1421. id: recipe.id,
  1422. name: recipe.name,
  1423. price: document.getElementById("editRecipePrice").value * 100,
  1424. ingredients: []
  1425. }
  1426. if(merchant.pos === "none"){
  1427. data.name = document.getElementById("editRecipeName").value;
  1428. }
  1429. let ingredients = document.getElementById("editRecipeIngList").children;
  1430. for(let i = 0; i < ingredients.length; i++){
  1431. const quantity = parseFloat(ingredients[i].children[3].value);
  1432. let newIngredient = {};
  1433. let ingredient = {};
  1434. if(ingredients[i].children[1].style.display === "none"){
  1435. let selector = ingredients[i].children[2];
  1436. ingredient = selector.options[selector.selectedIndex].ingredient;
  1437. newIngredient = {
  1438. ingredient: ingredient.ingredient.id,
  1439. quantity: ingredient.convertToBase(quantity)
  1440. };
  1441. }else{
  1442. ingredient = ingredients[i].ingredient;
  1443. newIngredient = {
  1444. ingredient: ingredient.ingredient.id,
  1445. quantity: ingredients[i].ingredient.convertToBase(quantity)
  1446. };
  1447. }
  1448. data.ingredients.push(newIngredient);
  1449. }
  1450. let loader = document.getElementById("loaderContainer");
  1451. loader.style.display = "flex";
  1452. fetch("/recipe/update", {
  1453. method: "put",
  1454. headers: {
  1455. "Content-Type": "application/json;charset=utf-8"
  1456. },
  1457. body: JSON.stringify(data)
  1458. })
  1459. .then(response => response.json())
  1460. .then((response)=>{
  1461. if(typeof(response) === "string"){
  1462. controller.createBanner(response, "error");
  1463. }else{
  1464. merchant.updateRecipe(response);
  1465. controller.openStrand("recipeBook");
  1466. controller.createBanner("RECIPE UPDATED", "success");
  1467. }
  1468. })
  1469. .catch((err)=>{
  1470. controller.createBanner("SOMETHING WENT WRONG, PLEASE REFRESH THE PAGE", "error");
  1471. })
  1472. .finally(()=>{
  1473. loader.style.display = "none";
  1474. });
  1475. }
  1476. }
  1477. module.exports = editRecipe;
  1478. },{}],9:[function(require,module,exports){
  1479. let ingredientDetails = {
  1480. dailyUse: 0,
  1481. display: function(ingredient){
  1482. document.getElementById("editIngBtn").onclick = ()=>{controller.openSidebar("editIngredient", ingredient)};
  1483. document.getElementById("removeIngBtn").onclick = ()=>{this.remove(ingredient)};
  1484. document.getElementById("ingredientDetailsCategory").innerText = ingredient.ingredient.category;
  1485. document.getElementById("ingredientDetailsName").innerText = ingredient.ingredient.name;
  1486. document.getElementById("ingredientStock").innerText = ingredient.getQuantityDisplay();
  1487. //Calculate and display average daily use
  1488. let quantities = [];
  1489. let now = new Date();
  1490. for(let i = 1; i < 31; i++){
  1491. let endDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i)
  1492. let startDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i - 1);
  1493. quantities.push(merchant.getSingleIngredientSold(ingredient, startDay, endDay));
  1494. }
  1495. let sum = 0;
  1496. for(let i = 0; i < quantities.length; i++){
  1497. sum += quantities[i];
  1498. }
  1499. let dailyUse = sum / quantities.length;
  1500. const dailyUseDiv = document.getElementById("dailyUse");
  1501. dailyUseDiv.innerText = `${dailyUse.toFixed(2)} ${ingredient.ingredient.unit.toUpperCase()}`;
  1502. //Show recipes that this ingredient is a part of
  1503. let recipeList = document.getElementById("ingredientRecipeList");
  1504. let template = document.getElementById("ingredientRecipe").content.children[0];
  1505. let recipes = merchant.getRecipesForIngredient(ingredient.ingredient);
  1506. while(recipeList.children.length > 0){
  1507. recipeList.removeChild(recipeList.firstChild);
  1508. }
  1509. for(let i = 0; i < recipes.length; i++){
  1510. let recipeDiv = template.cloneNode(true);
  1511. recipeDiv.children[0].innerText = recipes[i].name;
  1512. recipeDiv.onclick = ()=>{
  1513. controller.openStrand("recipeBook");
  1514. controller.openSidebar("recipeDetails", recipes[i]);
  1515. }
  1516. recipeDiv.classList.add("choosable");
  1517. recipeList.appendChild(recipeDiv);
  1518. }
  1519. },
  1520. remove: function(ingredient){
  1521. for(let i = 0; i < merchant.recipes.length; i++){
  1522. for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
  1523. if(ingredient.ingredient === merchant.recipes[i].ingredients[j].ingredient){
  1524. controller.createBanner("MUST REMOVE INGREDIENT FROM ALL RECIPES BEFORE REMOVING FROM INVENTORY", "error");
  1525. return;
  1526. }
  1527. }
  1528. }
  1529. let loader = document.getElementById("loaderContainer");
  1530. loader.style.display = "flex";
  1531. fetch(`/ingredients/remove/${ingredient.ingredient.id}`, {
  1532. method: "delete",
  1533. })
  1534. .then((response) => response.json())
  1535. .then((response)=>{
  1536. if(typeof(response) === "string"){
  1537. controller.createBanner(response, "error");
  1538. }else{
  1539. merchant.removeIngredient(ingredient);
  1540. controller.openStrand("ingredients");
  1541. controller.createBanner("INGREDIENT REMOVED", "success");
  1542. }
  1543. })
  1544. .catch((err)=>{
  1545. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  1546. })
  1547. .finally(()=>{
  1548. loader.style.display = "none";
  1549. });
  1550. }
  1551. }
  1552. module.exports = ingredientDetails;
  1553. },{}],10:[function(require,module,exports){
  1554. let newIngredient = {
  1555. display: function(Ingredient){
  1556. const selector = document.getElementById("unitSelector");
  1557. document.getElementById("newIngName").value = "";
  1558. document.getElementById("newIngCategory").value = "";
  1559. document.getElementById("newIngQuantity").value = 0;
  1560. document.getElementById("bottleSizeLabel").style.display = "none";
  1561. selector.value = "g";
  1562. selector.onchange = ()=>{this.unitChange()};
  1563. document.getElementById("submitNewIng").onclick = ()=>{this.submit(Ingredient)};
  1564. document.getElementById("ingredientFileUpload").addEventListener("click", ()=>{controller.openModal("ingredientSpreadsheet")});
  1565. },
  1566. unitChange: function(){
  1567. const select = document.getElementById("unitSelector");
  1568. const bottleLabel = document.getElementById("bottleSizeLabel");
  1569. if(select.value === "bottle"){
  1570. bottleLabel.style.display = "block";
  1571. }else{
  1572. bottleLabel.style.display = "none";
  1573. }
  1574. },
  1575. submit: function(){
  1576. let unitSelector = document.getElementById("unitSelector");
  1577. let options = document.querySelectorAll("#unitSelector option");
  1578. const quantityValue = parseFloat(document.getElementById("newIngQuantity").value);
  1579. let unit = unitSelector.value;
  1580. let newIngredient = {
  1581. ingredient: {
  1582. name: document.getElementById("newIngName").value,
  1583. category: document.getElementById("newIngCategory").value,
  1584. unitType: options[unitSelector.selectedIndex].getAttribute("type")
  1585. },
  1586. quantity: quantityValue,
  1587. defaultUnit: unit
  1588. }
  1589. //Change the ingredient if it is a special unit type (ie "bottle")
  1590. if(unit === "bottle"){
  1591. newIngredient.ingredient.unitType = document.getElementById("bottleUnits").value;
  1592. newIngredient.ingredient.unitSize = parseFloat(document.getElementById("bottleSize").value);
  1593. }
  1594. let loader = document.getElementById("loaderContainer");
  1595. loader.style.display = "flex";
  1596. fetch("/ingredients/create", {
  1597. method: "POST",
  1598. headers: {
  1599. "Content-Type": "application/json;charset=utf-8"
  1600. },
  1601. body: JSON.stringify(newIngredient)
  1602. })
  1603. .then((response) => response.json())
  1604. .then((response)=>{
  1605. if(typeof(response) === "string"){
  1606. controller.createBanner(response, "error");
  1607. }else{
  1608. merchant.addIngredient(response.ingredient, response.quantity, response.defaultUnit);
  1609. controller.openStrand("ingredients");
  1610. controller.createBanner("INGREDIENT CREATED", "success");
  1611. }
  1612. })
  1613. .catch((err)=>{
  1614. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  1615. })
  1616. .finally(()=>{
  1617. loader.style.display = "none";
  1618. });
  1619. },
  1620. submitSpreadsheet: function(){
  1621. event.preventDefault();
  1622. controller.closeModal();
  1623. const file = document.getElementById("spreadsheetInput").files[0];
  1624. let data = new FormData();
  1625. data.append("ingredients", file);
  1626. let loader = document.getElementById("loaderContainer");
  1627. loader.style.display = "flex";
  1628. fetch("/ingredients/create/spreadsheet", {
  1629. method: "post",
  1630. body: data
  1631. })
  1632. .then(response => response.json())
  1633. .then((response)=>{
  1634. if(typeof(response) === "string"){
  1635. controller.createBanner(response, "error");
  1636. }else{
  1637. for(let i = 0; i < response.length; i++){
  1638. merchant.addIngredient(response[i].ingredient, response[i].quantity, response[i].defaultUnit);
  1639. }
  1640. controller.createBanner("INGREDIENTS SUCCESSFULLY ADDED", "success");
  1641. controller.openStrand("ingredients");
  1642. }
  1643. })
  1644. .catch((err)=>{
  1645. controller.createBanner("SOMETHING WENT WRONG. TRY REFRESHING THE PAGE", "error");
  1646. })
  1647. .finally(()=>{
  1648. loader.style.display = "none";
  1649. });
  1650. }
  1651. }
  1652. module.exports = newIngredient;
  1653. },{}],11:[function(require,module,exports){
  1654. let newOrder = {
  1655. display: function(){
  1656. document.getElementById("sidebarDiv").classList.add("sidebarWide");
  1657. document.getElementById("newOrderIngredientList").style.display = "flex";
  1658. document.getElementById("orderFileUpload").addEventListener("click", ()=>{controller.openModal("orderSpreadsheet")});
  1659. let selectedList = document.getElementById("selectedIngredientList");
  1660. while(selectedList.children.length > 0){
  1661. selectedList.removeChild(selectedList.firstChild);
  1662. }
  1663. let ingredientList = document.getElementById("newOrderIngredients");
  1664. while(ingredientList.children.length > 0){
  1665. ingredientList.removeChild(ingredientList.firstChild);
  1666. }
  1667. merchant.ingredients.sort((a, b) => (a.ingredient.name > b.ingredient.name) ? 1 : -1);
  1668. for(let i = 0; i < merchant.ingredients.length; i++){
  1669. let ingredient = document.createElement("button");
  1670. ingredient.classList = "choosable";
  1671. ingredient.innerText = merchant.ingredients[i].ingredient.name;
  1672. ingredient.onclick = ()=>{this.addIngredient(merchant.ingredients[i], ingredient)};
  1673. ingredientList.appendChild(ingredient);
  1674. }
  1675. document.getElementById("submitNewOrder").onclick = ()=>{this.submit()};
  1676. },
  1677. addIngredient: function(ingredient, element){
  1678. element.style.display = "none";
  1679. let div = document.getElementById("selectedIngredient").content.children[0].cloneNode(true);
  1680. div.ingredient = ingredient;
  1681. div.children[0].children[1].onclick = ()=>{this.removeIngredient(div, element)};
  1682. div.children[0].children[0].innerText = `${ingredient.ingredient.name} (${ingredient.ingredient.unit.toUpperCase()})`;
  1683. document.getElementById("selectedIngredientList").appendChild(div);
  1684. },
  1685. removeIngredient: function(selectedElement, element){
  1686. selectedElement.parentElement.removeChild(selectedElement);
  1687. element.style.display = "block";
  1688. },
  1689. submit: function(){
  1690. let date = document.getElementById("newOrderDate").valueAsDate;
  1691. let taxes = document.getElementById("orderTaxes").value * 100;
  1692. let fees = document.getElementById("orderFees").value * 100;
  1693. let ingredients = document.getElementById("selectedIngredientList").children;
  1694. if(date === null){
  1695. controller.createBanner("DATE IS REQUIRED FOR ORDERS", "error");
  1696. return;
  1697. }
  1698. date.setHours(0, 0, 0, 0);
  1699. let data = {
  1700. name: document.getElementById("newOrderName").value,
  1701. date: date,
  1702. taxes: taxes,
  1703. fees: fees,
  1704. ingredients: []
  1705. }
  1706. for(let i = 0; i < ingredients.length; i++){
  1707. let quantity = ingredients[i].children[1].children[0].value;
  1708. let price = ingredients[i].children[1].children[1].value;
  1709. data.ingredients.push({
  1710. ingredient: ingredients[i].ingredient.ingredient.id,
  1711. quantity: ingredients[i].ingredient.convertToBase(quantity),
  1712. pricePerUnit: this.convertPrice(ingredients[i].ingredient.ingredient, price * 100)
  1713. });
  1714. }
  1715. let loader = document.getElementById("loaderContainer");
  1716. loader.style.display = "flex";
  1717. fetch("/order/create", {
  1718. method: "post",
  1719. headers: {
  1720. "Content-Type": "application/json;charset=utf-8"
  1721. },
  1722. body: JSON.stringify(data)
  1723. })
  1724. .then((response)=>response.json())
  1725. .then((response)=>{
  1726. if(typeof(response) === "string"){
  1727. controller.createBanner(response, "error");
  1728. }else{
  1729. merchant.addOrder(response, true);
  1730. controller.openStrand("orders", merchant.orders);
  1731. controller.createBanner("NEW ORDER CREATED", "success");
  1732. }
  1733. })
  1734. .catch((err)=>{
  1735. controller.createBanner("SOMETHING WENT WRONG, PLEASE REFRESH THE PAGE", "error");
  1736. })
  1737. .finally(()=>{
  1738. loader.style.display = "none";
  1739. });
  1740. },
  1741. convertPrice: function(ingredient, price){
  1742. switch(ingredient.unit){
  1743. case "g": return price;
  1744. case "kg": return price / 1000;
  1745. case "oz": return price / 28.3495;
  1746. case "lb": return price / 453.5924;
  1747. case "ml": return price * 1000;
  1748. case "l": return price;
  1749. case "tsp": return price * 202.8842;
  1750. case "tbsp": return price * 67.6278;
  1751. case "ozfl": return price * 33.8141;
  1752. case "cup": return price * 4.1667;
  1753. case "pt": return price * 2.1134;
  1754. case "qt": return price * 1.0567;
  1755. case "gal": return price / 3.7854;
  1756. case "mm": return price * 1000;
  1757. case "cm": return price * 100;
  1758. case "m": return price;
  1759. case "in": return price * 39.3701;
  1760. case "ft": return price * 3.2808;
  1761. default: return price;
  1762. }
  1763. },
  1764. submitSpreadsheet: function(){
  1765. event.preventDefault();
  1766. controller.closeModal();
  1767. const file = document.getElementById("spreadsheetInput").files[0];
  1768. let data = new FormData();
  1769. data.append("orders", file);
  1770. data.append("timeOffset", new Date().getTimezoneOffset());
  1771. let loader = document.getElementById("loaderContainer");
  1772. loader.style.display = "flex";
  1773. fetch("/orders/create/spreadsheet", {
  1774. method: "post",
  1775. body: data
  1776. })
  1777. .then(response => response.json())
  1778. .then((response)=>{
  1779. if(typeof(response) === "string"){
  1780. controller.createBanner(response, "error");
  1781. }else{
  1782. merchant.addOrder(response, true);
  1783. controller.createBanner("ORDER CREATED AND INGREDIENTS UPDATED SUCCESSFULLY", "success");
  1784. controller.openStrand("orders");
  1785. }
  1786. })
  1787. .catch((err)=>{
  1788. controller.createBanner("UNABLE TO DISPLAY NEW ORDER. PLEASE REFRESH THE PAGE.", "error");
  1789. })
  1790. .finally(()=>{
  1791. loader.style.display = "none";
  1792. });
  1793. }
  1794. }
  1795. module.exports = newOrder;
  1796. },{}],12:[function(require,module,exports){
  1797. let newRecipe = {
  1798. display: function(Recipe){
  1799. document.getElementById("newRecipeName").value = "";
  1800. document.getElementById("newRecipePrice").value = "";
  1801. document.getElementById("ingredientCount").value = 1;
  1802. let categories = merchant.categorizeIngredients();
  1803. let ingredientsSelect = document.getElementById("recipeInputIngredients");
  1804. while(ingredientsSelect.children.length > 0){
  1805. ingredientsSelect.removeChild(ingredientsSelect.firstChild);
  1806. }
  1807. this.changeIngredientCount(categories);
  1808. document.getElementById("ingredientCount").onchange = ()=>{this.changeIngredientCount(categories)};
  1809. document.getElementById("submitNewRecipe").onclick = ()=>{this.submit(Recipe)};
  1810. document.getElementById("recipeFileUpload").onclick = ()=>{controller.openModal("recipeSpreadsheet")};
  1811. },
  1812. //Updates the number of ingredient inputs displayed for new recipes
  1813. changeIngredientCount: function(categories){
  1814. let newCount = document.getElementById("ingredientCount").value;
  1815. let ingredientsDiv = document.getElementById("recipeInputIngredients");
  1816. let template = document.getElementById("recipeInputIngredient").content.children[0];
  1817. let oldCount = ingredientsDiv.children.length;
  1818. if(newCount > oldCount){
  1819. let newDivs = newCount - oldCount;
  1820. for(let i = 0; i < newDivs; i++){
  1821. let newNode = template.cloneNode(true);
  1822. newNode.children[0].innnerText = `INGREDIENT ${i + oldCount}`;
  1823. newNode.children[2].children[0].value = 0;
  1824. for(let j = 0; j < categories.length; j++){
  1825. let optgroup = document.createElement("optgroup");
  1826. optgroup.label = categories[j].name;
  1827. for(let k = 0; k < categories[j].ingredients.length; k++){
  1828. let option = document.createElement("option");
  1829. option.innerText = categories[j].ingredients[k].ingredient.getNameAndUnit();
  1830. option.ingredient = categories[j].ingredients[k];
  1831. optgroup.appendChild(option);
  1832. }
  1833. newNode.children[1].children[0].appendChild(optgroup);
  1834. }
  1835. ingredientsDiv.appendChild(newNode);
  1836. }
  1837. for(let i = 0; i < newCount; i++){
  1838. ingredientsDiv.children[i].children[0].innerText = `INGREDIENT ${i + 1}`;
  1839. }
  1840. }else if(newCount < oldCount){
  1841. let newDivs = oldCount - newCount;
  1842. for(let i = 0; i < newDivs; i++){
  1843. ingredientsDiv.removeChild(ingredientsDiv.children[ingredientsDiv.children.length-1]);
  1844. }
  1845. }
  1846. },
  1847. submit: function(Recipe){
  1848. let newRecipe = {
  1849. name: document.getElementById("newRecipeName").value,
  1850. price: document.getElementById("newRecipePrice").value,
  1851. ingredients: []
  1852. }
  1853. let inputs = document.getElementById("recipeInputIngredients").children;
  1854. for(let i = 0; i < inputs.length; i++){
  1855. let sel = inputs[i].children[1].children[0];
  1856. let ingredient = sel.options[sel.selectedIndex].ingredient;
  1857. let newIngredient = {
  1858. ingredient: ingredient.ingredient.id,
  1859. quantity: ingredient.convertToBase(inputs[i].children[2].children[0].value)
  1860. };
  1861. newRecipe.ingredients.push(newIngredient);
  1862. }
  1863. let loader = document.getElementById("loaderContainer");
  1864. loader.style.display = "flex";
  1865. fetch("/recipe/create", {
  1866. method: "POST",
  1867. headers: {
  1868. "Content-Type": "application/json;charset=utf-8"
  1869. },
  1870. body: JSON.stringify(newRecipe)
  1871. })
  1872. .then((response) => response.json())
  1873. .then((response)=>{
  1874. if(typeof(response) === "string"){
  1875. controller.createBanner(response, "error");
  1876. }else{
  1877. let ingredients = [];
  1878. for(let i = 0; i < response.ingredients.length; i++){
  1879. for(let j = 0; j < merchant.ingredients.length; j++){
  1880. if(merchant.ingredients[j].ingredient.id === response.ingredients[i].ingredient){
  1881. ingredients.push({
  1882. ingredient: merchant.ingredients[j].ingredient.id,
  1883. quantity: response.ingredients[i].quantity
  1884. });
  1885. break;
  1886. }
  1887. }
  1888. }
  1889. merchant.addRecipe(
  1890. response._id,
  1891. response.name,
  1892. response.price,
  1893. ingredients
  1894. );
  1895. controller.createBanner("RECIPE CREATED", "success");
  1896. controller.openStrand("recipeBook");
  1897. }
  1898. })
  1899. .catch((err)=>{
  1900. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  1901. })
  1902. .finally(()=>{
  1903. loader.style.display = "none";
  1904. });
  1905. },
  1906. submitSpreadsheet: function(){
  1907. event.preventDefault();
  1908. controller.closeModal();
  1909. const file = document.getElementById("spreadsheetInput").files[0];
  1910. let data = new FormData();
  1911. data.append("recipes", file);
  1912. let loader = document.getElementById("loaderContainer");
  1913. loader.style.display = "flex";
  1914. fetch("/recipes/create/spreadsheet", {
  1915. method: "post",
  1916. body: data
  1917. })
  1918. .then(response => response.json())
  1919. .then((response)=>{
  1920. if(typeof(response) === "string"){
  1921. controller.createBanner(response, "error");
  1922. }else{
  1923. for(let i = 0; i < response.length; i++){
  1924. merchant.addRecipe(
  1925. response[i]._id,
  1926. response[i].name,
  1927. response[i].price,
  1928. response[i].ingredients
  1929. );
  1930. }
  1931. controller.createBanner("ALL INGREDIENTS SUCCESSFULLY CREATED", "success");
  1932. controller.openStrand("recipeBook");
  1933. }
  1934. })
  1935. .catch((err)=>{
  1936. controller.createBanner("UNABLE TO DISPLAY NEW RECIPES. PLEASE REFRESH THE PAGE", "error");
  1937. })
  1938. .finally(()=>{
  1939. loader.style.display = "none";
  1940. });
  1941. }
  1942. }
  1943. module.exports = newRecipe;
  1944. },{}],13:[function(require,module,exports){
  1945. let newTransaction = {
  1946. display: function(){
  1947. let recipeList = document.getElementById("newTransactionRecipes");
  1948. let template = document.getElementById("createTransaction").content.children[0];
  1949. document.getElementById("transactionFileUpload").addEventListener("click", ()=>{controller.openModal("transactionSpreadsheet")});
  1950. while(recipeList.children.length > 0){
  1951. recipeList.removeChild(recipeList.firstChild);
  1952. }
  1953. for(let i = 0; i < merchant.recipes.length; i++){
  1954. let recipeDiv = template.cloneNode(true);
  1955. recipeDiv.recipe = merchant.recipes[i];
  1956. recipeList.appendChild(recipeDiv);
  1957. recipeDiv.children[0].innerText = merchant.recipes[i].name;
  1958. }
  1959. document.getElementById("submitNewTransaction").onclick = ()=>{this.submit()};
  1960. },
  1961. submit: function(){
  1962. let recipeDivs = document.getElementById("newTransactionRecipes");
  1963. let date = document.getElementById("newTransactionDate").valueAsDate;
  1964. if(date === null){
  1965. controller.createBanner("DATE IS REQUIRED FOR TRANSACTIONS", "error");
  1966. return;
  1967. }
  1968. date.setHours(0, 0, 0, 0);
  1969. let data = {
  1970. date: date,
  1971. recipes: [],
  1972. ingredientUpdates: {}
  1973. };
  1974. for(let i = 0; i < recipeDivs.children.length; i++){
  1975. let quantity = recipeDivs.children[i].children[1].value;
  1976. const recipe = recipeDivs.children[i].recipe;
  1977. if(quantity !== "" && quantity > 0){
  1978. data.recipes.push({
  1979. recipe: recipe.id,
  1980. quantity: quantity
  1981. });
  1982. for(let j = 0; j < recipe.ingredients.length; j++){
  1983. let ingredient = recipe.ingredients[j];
  1984. if(data.ingredientUpdates[ingredient.ingredient.id]){
  1985. data.ingredientUpdates[ingredient.ingredient.id] += ingredient.convertToBase(ingredient.quantity) * quantity;
  1986. }else{
  1987. data.ingredientUpdates[ingredient.ingredient.id] = ingredient.convertToBase(ingredient.quantity) * quantity;
  1988. }
  1989. }
  1990. }else if(quantity < 0){
  1991. controller.createBanner("CANNOT HAVE NEGATIVE VALUES", "error");
  1992. return;
  1993. }
  1994. }
  1995. if(data.recipes.length > 0){
  1996. let loader = document.getElementById("loaderContainer");
  1997. loader.style.display = "flex";
  1998. fetch("/transaction/create", {
  1999. method: "post",
  2000. headers: {
  2001. "Content-Type": "application/json;charset=utf-8"
  2002. },
  2003. body: JSON.stringify(data)
  2004. })
  2005. .then(response => response.json())
  2006. .then((response)=>{
  2007. if(typeof(response) === "string"){
  2008. controller.createBanner(response, "error");
  2009. }else{
  2010. merchant.addTransaction(response);
  2011. controller.updateAnalytics();
  2012. controller.openStrand("transactions", merchant.getTransactions());
  2013. controller.createBanner("TRANSACTION CREATED", "success");
  2014. }
  2015. })
  2016. .catch((err)=>{
  2017. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  2018. })
  2019. .finally(()=>{
  2020. loader.style.display = "none";
  2021. });
  2022. }
  2023. },
  2024. submitSpreadsheet: function(){
  2025. event.preventDefault();
  2026. controller.closeModal();
  2027. const file = document.getElementById("spreadsheetInput").files[0];
  2028. let data = new FormData();
  2029. data.append("transactions", file);
  2030. data.append("timeOffset", new Date().getTimezoneOffset());
  2031. let loader = document.getElementById("loaderContainer");
  2032. loader.style.display = "flex";
  2033. fetch("/transactions/create/spreadsheet", {
  2034. method: "post",
  2035. body: data
  2036. })
  2037. .then(response => response.json())
  2038. .then((response)=>{
  2039. if(typeof(response) === "string"){
  2040. controller.createBanner(response, "error");
  2041. }else{
  2042. for(let i = 0; i < response.recipes.length; i++){
  2043. response.recipes[i].recipe = response.recipes[i].recipe._id;
  2044. }
  2045. merchant.addTransaction(response);
  2046. controller.updateAnalytics();
  2047. controller.openStrand("transactions", merchant.transactions);
  2048. controller.createBanner("TRANSACTION SUCCESSFULLY CREATED. INGREDIENTS UPDATED", "success");
  2049. }
  2050. })
  2051. .catch((err)=>{
  2052. controller.createBanner("UNABLE TO DISPLAY NEW TRANSACTIONS. PLEASE REFRESH THE PAGE", "error");
  2053. })
  2054. .finally(()=>{
  2055. loader.style.display = "none";
  2056. });
  2057. }
  2058. }
  2059. module.exports = newTransaction;
  2060. },{}],14:[function(require,module,exports){
  2061. let orderCalculator = {
  2062. display: function(){
  2063. let to = new Date();
  2064. to.setDate(to.getDate() + 7);
  2065. to.setHours(0, 0, 0, 0);
  2066. from = new Date();
  2067. from.setDate(from.getDate() + 1);
  2068. from.setHours(0, 0, 0, 0);
  2069. document.getElementById("predictDateFrom").valueAsDate = from;
  2070. document.getElementById("predictDateTo").valueAsDate = to;
  2071. document.getElementById("predictButton").onclick = ()=>{this.predict()};
  2072. let selector = document.getElementById("predictSelector");
  2073. while(selector.children.length > 0){
  2074. selector.removeChild(selector.firstChild);
  2075. }
  2076. for(let i = 0; i < merchant.ingredients.length; i++){
  2077. let option = document.createElement("option");
  2078. option.innerText = merchant.ingredients[i].ingredient.name;
  2079. option.value = merchant.ingredients[i].ingredient.id;
  2080. selector.appendChild(option);
  2081. }
  2082. },
  2083. predict: function(){
  2084. let from = document.getElementById("predictDateFrom").valueAsDate;
  2085. let to = document.getElementById("predictDateTo").valueAsDate;
  2086. let ingredient = merchant.getIngredient(document.getElementById("predictSelector").value);
  2087. let data = {
  2088. to: new Date(),
  2089. recipes: []
  2090. }
  2091. data.from = new Date(data.to.getFullYear() - 1, data.to.getMonth(), data.to.getDate());
  2092. fetch("/transaction", {
  2093. method: "post",
  2094. headers: {
  2095. "Content-Type": "application/json;charset=utf-8"
  2096. },
  2097. body: JSON.stringify(data)
  2098. })
  2099. .then(response => response.json())
  2100. .then((response)=>{
  2101. if(typeof(response) === "string"){
  2102. controller.createBanner(response, "error");
  2103. }else{
  2104. const options = {
  2105. task: "regression",
  2106. debug: false
  2107. }
  2108. const nn = ml5.neuralNetwork(options);
  2109. this.createData(nn, response, ingredient.ingredient);
  2110. nn.normalizeData();
  2111. nn.train(()=>{
  2112. let predictors = [];
  2113. while(from <= to){
  2114. predictors.push(nn.predict({
  2115. month: from.getMonth(),
  2116. day: from.getDay()
  2117. }))
  2118. from.setDate(from.getDate() + 1);
  2119. }
  2120. Promise.all(predictors)
  2121. .then((predictions)=>{
  2122. let total = 0
  2123. for(let i = 0; i < predictions.length; i++){
  2124. total += predictions[i][0].value;
  2125. }
  2126. if(isNaN(total)) total = 0;
  2127. document.getElementById("prediction").innerText = `${total.toFixed(2)} ${ingredient.ingredient.unit.toUpperCase()}`;
  2128. })
  2129. .catch((err)=>{
  2130. controller.createBanner("ERROR: UNABLE TO MAKE PREDICTION", "error");
  2131. });
  2132. });
  2133. }
  2134. })
  2135. .catch((err)=>{
  2136. controller.createBanner("ERROR: UNABLE TO MAKE PREDICTION", "error");
  2137. });
  2138. },
  2139. createData: function(nn, transactions, ingredient){
  2140. let today = new Date(transactions[transactions.length-1].date);
  2141. today.setHours(0, 0, 0, 0);
  2142. let tomorrow = new Date(transactions[transactions.length-1].date);
  2143. tomorrow.setDate(tomorrow.getDate() + 1);
  2144. tomorrow.setHours(0, 0, 0, 0);
  2145. let dailySum = 0;
  2146. for(let i = transactions.length - 1; i >= 0; i--){
  2147. transactions[i].date = new Date(transactions[i].date);
  2148. if(transactions[i].date >= tomorrow){
  2149. let inputs = {
  2150. month: today.getMonth(),
  2151. day: today.getDay()
  2152. };
  2153. let output = {quantity: dailySum};
  2154. nn.addData(inputs, output);
  2155. dailySum = 0;
  2156. today.setDate(today.getDate() + 1);
  2157. tomorrow.setDate(tomorrow.getDate() + 1);
  2158. }
  2159. for(let j = 0; j < transactions[i].recipes.length; j++){
  2160. for(let k = 0; k < merchant.recipes.length; k++){
  2161. if(merchant.recipes[k].id === transactions[i].recipes[j].recipe){
  2162. for(let l = 0; l < merchant.recipes[k].ingredients.length; l++){
  2163. if(merchant.recipes[k].ingredients[l].ingredient === ingredient){
  2164. dailySum += merchant.recipes[k].ingredients[l].quantity * transactions[i].recipes[j].quantity;
  2165. break;
  2166. }
  2167. }
  2168. break;
  2169. }
  2170. }
  2171. }
  2172. }
  2173. }
  2174. }
  2175. module.exports = orderCalculator;
  2176. },{}],15:[function(require,module,exports){
  2177. let orderDetails = {
  2178. display: function(order){
  2179. document.getElementById("removeOrderBtn").onclick = ()=>{this.remove(order)};
  2180. document.getElementById("orderDetailName").innerText = order.name;
  2181. document.getElementById("orderDetailDate").innerText = order.date.toLocaleDateString("en-US");
  2182. document.getElementById("orderDetailTax").innerText = `$${order.taxes.toFixed(2)}`;
  2183. document.getElementById("orderDetailFee").innerText = `$${order.fees.toFixed(2)}`;
  2184. let ingredientList = document.getElementById("orderIngredients");
  2185. while(ingredientList.children.length > 0){
  2186. ingredientList.removeChild(ingredientList.firstChild);
  2187. }
  2188. let template = document.getElementById("orderIngredient").content.children[0];
  2189. for(let i = 0; i < order.ingredients.length; i++){
  2190. let ingredientDiv = template.cloneNode(true);
  2191. const ingredient = order.ingredients[i].ingredient;
  2192. ingredientDiv.children[0].innerText = order.ingredients[i].ingredient.name;
  2193. ingredientDiv.children[2].innerText = `$${order.ingredients[i].cost().toFixed(2)}`;
  2194. ingredientDiv.onclick = ()=>{
  2195. controller.openStrand("ingredients");
  2196. controller.openSidebar("ingredientDetails", merchant.getIngredient(order.ingredients[i].ingredient.id));
  2197. }
  2198. let ingredientDisplay = ingredientDiv.children[1];
  2199. ingredientDisplay.innerText = `${order.ingredients[i].quantity.toFixed(2)} ${ingredient.unit.toUpperCase()} X $${order.ingredients[i].pricePerUnit.toFixed(2)}`;
  2200. ingredientList.appendChild(ingredientDiv);
  2201. }
  2202. document.getElementById("orderDetailTotal").innerText = `$${order.getIngredientCost().toFixed(2)}`;
  2203. document.querySelector("#orderTotalPrice p").innerText = `$${order.getTotalCost().toFixed(2)}`;
  2204. },
  2205. remove: function(order){
  2206. let loader = document.getElementById("loaderContainer");
  2207. loader.style.display = "flex";
  2208. fetch(`/order/${order.id}`, {
  2209. method: "DELETE",
  2210. headers: {
  2211. "Content-Type": "application/json;charset=utf-8"
  2212. }
  2213. })
  2214. .then((response) => response.json())
  2215. .then((response)=>{
  2216. if(typeof(response) === "string"){
  2217. controller.createBanner(response, "error");
  2218. }else{
  2219. merchant.removeOrder(order);
  2220. controller.openStrand("orders", merchant.orders);
  2221. controller.createBanner("ORDER REMOVED", "success");
  2222. }
  2223. })
  2224. .catch((err)=>{
  2225. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  2226. })
  2227. .finally(()=>{
  2228. loader.style.display = "none";
  2229. });
  2230. }
  2231. }
  2232. module.exports = orderDetails;
  2233. },{}],16:[function(require,module,exports){
  2234. let orderFilter = {
  2235. display: function(Order){
  2236. let now = new Date();
  2237. let past = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 30);
  2238. let ingredientList = document.getElementById("orderFilterIngredients");
  2239. document.getElementById("orderFilterDateFrom").valueAsDate = past;
  2240. document.getElementById("orderFilterDateTo").valueAsDate = now;
  2241. while(ingredientList.children.length > 0){
  2242. ingredientList.removeChild(ingredientList.firstChild);
  2243. }
  2244. for(let i = 0; i < merchant.ingredients.length; i++){
  2245. let element = document.createElement("div");
  2246. element.classList.add("choosable");
  2247. element.ingredient = merchant.ingredients[i].ingredient.id;
  2248. element.onclick = ()=>{this.toggleActive(element)};
  2249. ingredientList.appendChild(element);
  2250. let text = document.createElement("p");
  2251. text.innerText = merchant.ingredients[i].ingredient.name;
  2252. element.appendChild(text);
  2253. }
  2254. document.getElementById("orderFilterSubmit").onclick = ()=>{this.submit(Order)};
  2255. },
  2256. toggleActive: function(element){
  2257. if(element.classList.contains("active")){
  2258. element.classList.remove("active");
  2259. }else{
  2260. element.classList.add("active");
  2261. }
  2262. },
  2263. submit: function(Order){
  2264. let data = {
  2265. from: document.getElementById("orderFilterDateFrom").valueAsDate,
  2266. to: document.getElementById("orderFilterDateTo").valueAsDate,
  2267. ingredients: []
  2268. }
  2269. data.from.setHours(0, 0, 0, 0);
  2270. data.to.setHours(0, 0, 0, 0);
  2271. let ingredients = document.getElementById("orderFilterIngredients").children;
  2272. for(let i = 0; i < ingredients.length; i++){
  2273. if(ingredients[i].classList.contains("active")){
  2274. data.ingredients.push(ingredients[i].ingredient);
  2275. }
  2276. }
  2277. let loader = document.getElementById("loaderContainer");
  2278. loader.style.display = "flex";
  2279. fetch("/orders/get", {
  2280. method: "post",
  2281. headers: {
  2282. "Content-Type": "application/json;charset=utf-8"
  2283. },
  2284. body: JSON.stringify(data)
  2285. })
  2286. .then(response => response.json())
  2287. .then((response)=>{
  2288. let orders = [];
  2289. if(typeof(response) === "string"){
  2290. controller.createBanner(response, "error");
  2291. }else if(response.length === 0){
  2292. controller.createBanner("NO ORDERS MATCH YOUR SEARCH", "error");
  2293. }else{
  2294. for(let i = 0; i < response.length; i++){
  2295. orders.push(new Order(
  2296. response[i]._id,
  2297. response[i].name,
  2298. response[i].date,
  2299. response[i].taxes,
  2300. response[i].fees,
  2301. response[i].ingredients,
  2302. merchant
  2303. ));
  2304. }
  2305. }
  2306. controller.openStrand("orders", orders);
  2307. })
  2308. .catch((err)=>{
  2309. controller.createBanner("UNABLE TO DISPLAY THE ORDERS", "error");
  2310. })
  2311. .finally(()=>{
  2312. loader.style.display = "none";
  2313. });
  2314. }
  2315. }
  2316. module.exports = orderFilter;
  2317. },{}],17:[function(require,module,exports){
  2318. let recipeDetails = {
  2319. display: function(recipe){
  2320. document.getElementById("editRecipeBtn").onclick = ()=>{controller.openSidebar("editRecipe", recipe)};
  2321. document.getElementById("recipeName").innerText = recipe.name;
  2322. if(merchant.pos === "none"){
  2323. document.getElementById("removeRecipeBtn").onclick = ()=>{this.remove(recipe)};
  2324. }
  2325. //ingredient list
  2326. let ingredientsDiv = document.getElementById("recipeIngredientList");
  2327. while(ingredientsDiv.children.length > 0){
  2328. ingredientsDiv.removeChild(ingredientsDiv.firstChild);
  2329. }
  2330. let template = document.getElementById("recipeIngredient").content.children[0];
  2331. for(let i = 0; i < recipe.ingredients.length; i++){
  2332. let recipeDiv = template.cloneNode(true);
  2333. recipeDiv.children[0].innerText = recipe.ingredients[i].ingredient.name;
  2334. recipeDiv.children[1].innerText = `${recipe.ingredients[i].getQuantityDisplay()}`;
  2335. recipeDiv.onclick = ()=>{
  2336. controller.openStrand("ingredients");
  2337. controller.openSidebar("ingredientDetails", merchant.getIngredient(recipe.ingredients[i].ingredient.id));
  2338. }
  2339. ingredientsDiv.appendChild(recipeDiv);
  2340. }
  2341. document.getElementById("recipePrice").children[1].innerText = `$${recipe.price.toFixed(2)}`;
  2342. },
  2343. remove: function(recipe){
  2344. let loader = document.getElementById("loaderContainer");
  2345. loader.style.display = "flex";
  2346. fetch(`/recipe/remove/${recipe.id}`, {
  2347. method: "delete"
  2348. })
  2349. .then((response) => response.json())
  2350. .then((response)=>{
  2351. if(typeof(response) === "string"){
  2352. controller.createBanner(response, "error");
  2353. }else{
  2354. merchant.removeRecipe(recipe);
  2355. controller.createBanner("RECIPE REMOVED", "success");
  2356. controller.openStrand("recipeBook");
  2357. }
  2358. })
  2359. .catch((err)=>{
  2360. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  2361. })
  2362. .finally(()=>{
  2363. loader.style.display = "none";
  2364. });
  2365. }
  2366. }
  2367. module.exports = recipeDetails;
  2368. },{}],18:[function(require,module,exports){
  2369. let transactionDetails = {
  2370. transaction: {},
  2371. display: function(transaction){
  2372. this.transaction = transaction;
  2373. let recipeList = document.getElementById("transactionRecipes");
  2374. let template = document.getElementById("transactionRecipe").content.children[0];
  2375. let totalRecipes = 0;
  2376. let totalPrice = 0;
  2377. while(recipeList.children.length > 0){
  2378. recipeList.removeChild(recipeList.firstChild);
  2379. }
  2380. for(let i = 0; i < transaction.recipes.length; i++){
  2381. let recipe = template.cloneNode(true);
  2382. let price = transaction.recipes[i].quantity * transaction.recipes[i].recipe.price;
  2383. recipe.children[0].innerText = transaction.recipes[i].recipe.name;
  2384. recipe.children[1].innerText = `${transaction.recipes[i].quantity} x $${transaction.recipes[i].recipe.price.toFixed(2)}`;
  2385. recipe.children[2].innerText = `$${price.toFixed(2)}`;
  2386. recipe.onclick = ()=>{
  2387. controller.openStrand("recipeBook");
  2388. controller.openSidebar("recipeDetails", transaction.recipes[i].recipe);
  2389. }
  2390. recipeList.appendChild(recipe);
  2391. totalRecipes += transaction.recipes[i].quantity;
  2392. totalPrice += price;
  2393. }
  2394. let months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  2395. let days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  2396. let dateString = `${days[transaction.date.getDay()]}, ${months[transaction.date.getMonth()]} ${transaction.date.getDate()}, ${transaction.date.getFullYear()}`;
  2397. document.getElementById("transactionDate").innerText = dateString;
  2398. document.getElementById("totalRecipes").innerText = `${totalRecipes} recipes`;
  2399. document.getElementById("totalPrice").innerText = `$${totalPrice.toFixed(2)}`;
  2400. if(merchant.pos === "none"){
  2401. document.getElementById("removeTransBtn").onclick = ()=>{this.remove()};
  2402. }
  2403. },
  2404. remove: function(){
  2405. let loader = document.getElementById("loaderContainer");
  2406. loader.style.display = "flex";
  2407. fetch(`/transaction/${this.transaction.id}`, {
  2408. method: "delete",
  2409. headers: {
  2410. "Content-Type": "application/json;charset=utf-8"
  2411. },
  2412. })
  2413. .then((response)=>{
  2414. if(typeof(response) === "string"){
  2415. controller.createBanner(response, "error");
  2416. }else{
  2417. merchant.removeTransaction(this.transaction);
  2418. controller.updateAnalytics();
  2419. controller.openStrand("transactions", merchant.getTransactions());
  2420. controller.createBanner("TRANSACTION REMOVED", "success");
  2421. }
  2422. })
  2423. .catch((err)=>{
  2424. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  2425. })
  2426. .finally(()=>{
  2427. loader.style.display = "none";
  2428. });
  2429. },
  2430. }
  2431. module.exports = transactionDetails;
  2432. },{}],19:[function(require,module,exports){
  2433. let transactionFilter = {
  2434. display: function(Transaction){
  2435. //Set default dates
  2436. let today = new Date();
  2437. let monthAgo = new Date(today);
  2438. monthAgo.setMonth(today.getMonth() - 1);
  2439. document.getElementById("transFilterDateStart").valueAsDate = monthAgo;
  2440. document.getElementById("transFilterDateEnd").valueAsDate = today;
  2441. //populate recipes
  2442. let recipeList = document.getElementById("transFilterRecipeList");
  2443. while(recipeList.children.length > 0){
  2444. recipeList.removeChild(recipeList.firstChild);
  2445. }
  2446. for(let i = 0; i < merchant.recipes.length; i++){
  2447. let recipe = document.createElement("div");
  2448. recipe.innerText = merchant.recipes[i].name;
  2449. recipe.recipe = merchant.recipes[i];
  2450. recipe.classList.add("choosable");
  2451. recipe.onclick = ()=>{this.toggleActive(recipe)};
  2452. recipeList.appendChild(recipe);
  2453. }
  2454. //Submit button
  2455. document.getElementById("transFilterSubmit").onclick = ()=>{this.submit(Transaction)};
  2456. },
  2457. toggleActive: function(element){
  2458. if(element.classList.contains("active")){
  2459. element.classList.remove("active");
  2460. }else{
  2461. element.classList.add("active");
  2462. }
  2463. },
  2464. submit: function(Transaction){
  2465. let data = {
  2466. from: document.getElementById("transFilterDateStart").valueAsDate,
  2467. to: document.getElementById("transFilterDateEnd").valueAsDate,
  2468. recipes: []
  2469. }
  2470. data.from.setHours(0, 0, 0, 0);
  2471. data.to.setHours(0, 0, 0, 0);
  2472. if(data.startDate >= data.endDate){
  2473. controller.createBanner("START DATE CANNOT BE AFTER END DATE", "error");
  2474. return;
  2475. }
  2476. let recipes = document.getElementById("transFilterRecipeList").children;
  2477. for(let i = 0; i < recipes.length; i++){
  2478. if(recipes[i].classList.contains("active")){
  2479. data.recipes.push(recipes[i].recipe.id);
  2480. }
  2481. }
  2482. let loader = document.getElementById("loaderContainer");
  2483. loader.style.display = "flex";
  2484. fetch("/transaction", {
  2485. method: "post",
  2486. headers: {
  2487. "Content-Type": "application/json;charset=utf-8"
  2488. },
  2489. body: JSON.stringify(data)
  2490. })
  2491. .then(response => response.json())
  2492. .then((response)=>{
  2493. let transactions = [];
  2494. if(typeof(response) === "string"){
  2495. controller.createBanner(response, "error");
  2496. }else if(response.length === 0){
  2497. controller.createBanner("NO TRANSACTIONS MATCH YOUR SEARCH", "error");
  2498. }else{
  2499. for(let i = 0; i < response.length; i++){
  2500. transactions.push(new Transaction(
  2501. response[i]._id,
  2502. response[i].date,
  2503. response[i].recipes,
  2504. merchant
  2505. ));
  2506. }
  2507. }
  2508. controller.openStrand("transactions", transactions);
  2509. })
  2510. .catch((err)=>{
  2511. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  2512. })
  2513. .finally(()=>{
  2514. loader.style.display = "none";
  2515. });
  2516. }
  2517. }
  2518. module.exports = transactionFilter;
  2519. },{}],20:[function(require,module,exports){
  2520. let account = {
  2521. display: function(){
  2522. document.getElementById("accountStrandTitle").innerText = merchant.name;
  2523. document.getElementById("accountEmail").value = merchant.email;
  2524. document.getElementById("accountUpdate").onclick = ()=>{this.updateData()};
  2525. let passButton = document.getElementById("accountShowPassword");
  2526. let passBox = document.getElementById("changePasswordBox");
  2527. passButton.onclick = ()=>{
  2528. passButton.style.display = "none";
  2529. passBox.style.display = "flex";
  2530. };
  2531. document.getElementById("cancelPasswordChange").onclick = ()=>{
  2532. passButton.style.display = "block";
  2533. passBox.style.display = "none";
  2534. document.getElementById("accountCurrentPassword").value = "";
  2535. document.getElementById("accountNewPassword").value = "";
  2536. document.getElementById("accountConfirmPassword").value = "";
  2537. };
  2538. document.getElementById("changePasswordButton").onclick = ()=>{this.updatePassword()};
  2539. },
  2540. updateData: function(){
  2541. let data = {
  2542. email: document.getElementById("accountEmail").value
  2543. }
  2544. let loader = document.getElementById("loaderContainer");
  2545. loader.style.display = "flex";
  2546. fetch("/merchant/update", {
  2547. method: "put",
  2548. headers: {
  2549. "Content-Type": "application/json;charset=utf-8"
  2550. },
  2551. body: JSON.stringify(data)
  2552. })
  2553. .then(response => response.json())
  2554. .then((response)=>{
  2555. if(typeof(response) === "string"){
  2556. controller.createBanner(response, "error");
  2557. }else{
  2558. controller.createBanner("DATA UPDATED", "success");
  2559. if(response.email !== merchant.email){
  2560. controller.createBanner("YOU MUST VALIDATE YOUR NEW EMAIL ADDRESS BEFORE YOU CAN LOG IN AGAIN", "alert");
  2561. }
  2562. merchant.email = response.email;
  2563. document.getElementById("accountEmail").value = merchant.email;
  2564. }
  2565. })
  2566. .catch((err)=>{
  2567. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  2568. })
  2569. .finally(()=>{
  2570. loader.style.display = "none";
  2571. });
  2572. },
  2573. updatePassword: function(){
  2574. let data = {
  2575. current: document.getElementById("accountCurrentPassword").value,
  2576. new: document.getElementById("accountNewPassword").value,
  2577. confirm: document.getElementById("accountConfirmPassword").value
  2578. }
  2579. if(data.new !== data.confirm){
  2580. return controller.createBanner("PASSWORDS DO NOT MATCH");
  2581. }
  2582. let loader = document.getElementById("loaderContainer");
  2583. loader.style.display = "flex";
  2584. fetch("/merchant/password", {
  2585. method: "put",
  2586. headers: {
  2587. "Content-Type": "application/json;charset=utf-8"
  2588. },
  2589. body: JSON.stringify(data)
  2590. })
  2591. .then(response => response.json())
  2592. .then((response)=>{
  2593. if(typeof(response) === "string"){
  2594. controller.createBanner(response, "error");
  2595. }else{
  2596. window.location.href = response.redirect;
  2597. }
  2598. })
  2599. .catch((err)=>{
  2600. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  2601. })
  2602. .finally(()=>{
  2603. loader.style.display = "none";
  2604. });
  2605. }
  2606. }
  2607. module.exports = account;
  2608. },{}],21:[function(require,module,exports){
  2609. let analytics = {
  2610. isPopulated: false,
  2611. ingredient: undefined,
  2612. recipe: undefined,
  2613. transactionsByDate: [],
  2614. display: function(Transaction){
  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(Transaction)};
  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(Transaction);
  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, Transaction){
  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(Transaction){
  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, Transaction);
  2866. if(document.getElementById("analSlider").checked === true){
  2867. this.displayRecipe();
  2868. }else{
  2869. this.displayIngredient();
  2870. }
  2871. }
  2872. }
  2873. module.exports = analytics;
  2874. },{}],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.addIngredient(response[i].ingredient, response[i].quantity, response[i].defaultUnit);
  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. let recipeBook = {
  3277. isPopulated: false,
  3278. recipeDivList: [],
  3279. display: function(Recipe){
  3280. if(!this.isPopulated){
  3281. this.populateRecipes();
  3282. if(merchant.pos !== "none"){
  3283. document.getElementById("posUpdateRecipe").onclick = ()=>{this.posUpdate(Recipe)};
  3284. }
  3285. document.getElementById("recipeSearch").oninput = ()=>{this.search()};
  3286. this.populateRecipes();
  3287. this.isPopulated = true;
  3288. }
  3289. },
  3290. populateRecipes: function(){
  3291. let recipeList = document.getElementById("recipeList");
  3292. let template = document.getElementById("recipe").content.children[0];
  3293. this.recipeDivList = [];
  3294. while(recipeList.children.length > 0){
  3295. recipeList.removeChild(recipeList.firstChild);
  3296. }
  3297. for(let i = 0; i < merchant.recipes.length; i++){
  3298. let recipeDiv = template.cloneNode(true);
  3299. recipeDiv.onclick = ()=>{
  3300. controller.openSidebar("recipeDetails", merchant.recipes[i]);
  3301. recipeDiv.classList.add("active");
  3302. }
  3303. recipeDiv._name = merchant.recipes[i].name;
  3304. recipeList.appendChild(recipeDiv);
  3305. recipeDiv.children[0].innerText = merchant.recipes[i].name;
  3306. recipeDiv.children[1].innerText = `$${merchant.recipes[i].price.toFixed(2)}`;
  3307. this.recipeDivList.push(recipeDiv);
  3308. }
  3309. },
  3310. search: function(){
  3311. let input = document.getElementById("recipeSearch").value.toLowerCase();
  3312. let recipeList = document.getElementById("recipeList");
  3313. let matchingRecipes = [];
  3314. for(let i = 0; i < this.recipeDivList.length; i++){
  3315. if(this.recipeDivList[i]._name.toLowerCase().includes(input)){
  3316. matchingRecipes.push(this.recipeDivList[i]);
  3317. }
  3318. }
  3319. while(recipeList.children.length > 0){
  3320. recipeList.removeChild(recipeList.firstChild);
  3321. }
  3322. for(let i = 0; i < matchingRecipes.length; i++){
  3323. recipeList.appendChild(matchingRecipes[i]);
  3324. }
  3325. },
  3326. posUpdate: function(Recipe){
  3327. let loader = document.getElementById("loaderContainer");
  3328. loader.style.display = "flex";
  3329. let url = `/recipe/update/${merchant.pos}`;
  3330. fetch(url, {
  3331. method: "GET",
  3332. headers: {
  3333. "Content-Type": "application/json;charset=utf-8"
  3334. },
  3335. })
  3336. .then(response => response.json())
  3337. .then((response)=>{
  3338. if(typeof(response) === "string"){
  3339. controller.createBanner(response, "error");
  3340. }else{
  3341. for(let i = 0; i < response.new.length; i++){
  3342. const recipe = new Recipe(
  3343. response.new[i]._id,
  3344. response.new[i].name,
  3345. response.new[i].price,
  3346. merchant,
  3347. []
  3348. );
  3349. merchant.addRecipe(recipe);
  3350. }
  3351. for(let i = 0; i < response.removed.length; i++){
  3352. for(let j = 0; j < merchant.recipes.length; j++){
  3353. if(merchant.recipes[j].id === response.removed[i]._id){
  3354. merchant.removeRecipe(merchant.recipes[j]);
  3355. break;
  3356. }
  3357. }
  3358. }
  3359. controller.createBanner("RECIPES SUCCESSFULLY UPDATED", "success");
  3360. this.display();
  3361. }
  3362. })
  3363. .catch((err)=>{
  3364. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  3365. })
  3366. .finally(()=>{
  3367. loader.style.display = "none";
  3368. });
  3369. }
  3370. }
  3371. module.exports = recipeBook;
  3372. },{}],26:[function(require,module,exports){
  3373. let transactions = {
  3374. transactions: [],
  3375. display: function(){
  3376. document.getElementById("filterTransactionsButton").onclick = ()=>{controller.openSidebar("transactionFilter")};
  3377. document.getElementById("newTransactionButton").onclick = ()=>{controller.openSidebar("newTransaction")};
  3378. this.populateTransactions(this.transactions);
  3379. this.isPopulated = true;
  3380. },
  3381. populateTransactions: function(transactions){
  3382. let transactionsList = document.getElementById("transactionsList");
  3383. let template = document.getElementById("transaction").content.children[0];
  3384. while(transactionsList.children.length > 0){
  3385. transactionsList.removeChild(transactionsList.firstChild);
  3386. }
  3387. let i = 0;
  3388. while(i < transactions.length && i < 100){
  3389. let transactionDiv = template.cloneNode(true);
  3390. let transaction = transactions[i];
  3391. transactionDiv.onclick = ()=>{
  3392. controller.openSidebar("transactionDetails", transaction);
  3393. transactionDiv.classList.add("active");
  3394. }
  3395. transactionsList.appendChild(transactionDiv);
  3396. let totalRecipes = 0;
  3397. let totalPrice = 0;
  3398. for(let j = 0; j < transactions[i].recipes.length; j++){
  3399. totalRecipes += transactions[i].recipes[j].quantity;
  3400. totalPrice += transactions[i].recipes[j].recipe.price * transactions[i].recipes[j].quantity;
  3401. }
  3402. transactionDiv.children[0].innerText = transactions[i].date.toLocaleDateString();
  3403. transactionDiv.children[1].innerText = `${totalRecipes} recipes sold`;
  3404. transactionDiv.children[2].innerText = `$${totalPrice.toFixed(2)}`;
  3405. i++;
  3406. }
  3407. }
  3408. }
  3409. module.exports = transactions;
  3410. },{}]},{},[6]);