bundle.js 138 KB

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