bundle.js 137 KB

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