bundle.js 137 KB

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