bundle.js 137 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912
  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(window.screen.availWidth <= 1000){
  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. if(window.screen.availWidth > 1000 && window.screen.availWidth <= 1400){
  1345. this.changeMenu();
  1346. document.getElementById("menuShifter2").style.display = "none";
  1347. }
  1348. //Add click listeners for menu buttons
  1349. document.getElementById("homeBtn").onclick = ()=>{controller.openStrand("home")};
  1350. document.getElementById("ingredientsBtn").onclick = ()=>{controller.openStrand("ingredients")};
  1351. document.getElementById("recipeBookBtn").onclick = ()=>{controller.openStrand("recipeBook")};
  1352. document.getElementById("analyticsBtn").onclick = ()=>{controller.openStrand("analytics")};
  1353. document.getElementById("ordersBtn").onclick = async ()=>{
  1354. if(merchant.orders.length === 0){
  1355. merchant.setOrders(await orders.getOrders(Order));
  1356. }
  1357. controller.openStrand("orders", merchant.orders);
  1358. }
  1359. document.getElementById("transactionsBtn").onclick = ()=>{controller.openStrand("transactions", merchant.getTransactions())};
  1360. controller.openStrand("home");
  1361. },{"./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){
  1362. let editIngredient = {
  1363. display: function(ingredient){
  1364. let buttonList = document.getElementById("unitButtons");
  1365. let quantLabel = document.getElementById("editIngQuantityLabel");
  1366. let specialLabel = document.getElementById("editSpecialLabel");
  1367. //Clear any existing data
  1368. while(buttonList.children.length > 0){
  1369. buttonList.removeChild(buttonList.firstChild);
  1370. }
  1371. //Populate basic fields
  1372. document.getElementById("editIngTitle").innerText = ingredient.ingredient.name;
  1373. document.getElementById("editIngName").value = ingredient.ingredient.name;
  1374. document.getElementById("editIngCategory").value = ingredient.ingredient.category;
  1375. quantLabel.innerText = `CURRENT STOCK (${ingredient.ingredient.unit.toUpperCase()})`;
  1376. document.getElementById("editIngSubmit").onclick = ()=>{this.submit(ingredient)};
  1377. //Make any changes for special ingredients
  1378. if(ingredient.ingredient.unit === "bottle"){
  1379. // quantLabel.innerText = "CURRENT STOCK (BOTTLES):";
  1380. specialLabel.style.display = "flex";
  1381. specialLabel.innerText = `BOTTLE SIZE (${ingredient.ingredient.unitType.toUpperCase()}):`;
  1382. let sizeInput = document.createElement("input");
  1383. sizeInput.id = "editIngSpecialSize";
  1384. sizeInput.type = "number";
  1385. sizeInput.min = "0";
  1386. sizeInput.step = "0.01";
  1387. sizeInput.value = ingredient.ingredient.unitSize.toFixed(2);
  1388. specialLabel.appendChild(sizeInput);
  1389. }else{
  1390. specialLabel.style.display = "none";
  1391. }
  1392. //Populate the unit buttons
  1393. const units = ingredient.ingredient.getPotentialUnits();
  1394. for(let i = 0; i < units.length; i++){
  1395. let button = document.createElement("button");
  1396. button.classList.add("unitButton");
  1397. button.innerText = units[i].toUpperCase();
  1398. button.onclick = ()=>{this.changeUnit(button)};
  1399. buttonList.appendChild(button);
  1400. if(units[i] === ingredient.ingredient.unitType){
  1401. button.classList.add("unitActive");
  1402. }
  1403. }
  1404. let quantInput = document.createElement("input");
  1405. quantInput.id = "editIngQuantity";
  1406. quantInput.type = "number";
  1407. quantInput.min = "0";
  1408. quantInput.step = "0.01";
  1409. quantInput.value = ingredient.quantity.toFixed(2);
  1410. quantLabel.appendChild(quantInput);
  1411. },
  1412. changeUnit(button){
  1413. let buttons = document.getElementById("unitButtons");
  1414. for(let i = 0; i < buttons.children.length; i++){
  1415. buttons.children[i].classList.remove("unitActive");
  1416. }
  1417. button.classList.add("unitActive");
  1418. },
  1419. submit(ingredient){
  1420. const quantity = parseFloat(document.getElementById("editIngQuantityLabel").children[0].value);
  1421. let data = {
  1422. id: ingredient.ingredient.id,
  1423. name: document.getElementById("editIngName").value,
  1424. category: document.getElementById("editIngCategory").value
  1425. }
  1426. data.quantity = quantity;
  1427. //Get the measurement unit
  1428. let units = document.getElementById("unitButtons");
  1429. for(let i = 0; i < units.children.length; i++){
  1430. if(units.children[i].classList.contains("unitActive")){
  1431. data.unit = units.children[i].innerText.toLowerCase();
  1432. break;
  1433. }
  1434. }
  1435. let loader = document.getElementById("loaderContainer");
  1436. loader.style.display = "flex";
  1437. fetch("/ingredients/update", {
  1438. method: "put",
  1439. headers: {
  1440. "Content-Type": "application/json;charset=utf-8"
  1441. },
  1442. body: JSON.stringify(data)
  1443. })
  1444. .then(response => response.json())
  1445. .then((response)=>{
  1446. if(typeof(response) === "string"){
  1447. controller.createBanner(response, "error");
  1448. }else{
  1449. merchant.removeIngredient(merchant.getIngredient(response.ingredient._id));
  1450. merchant.addIngredient(response.ingredient, response.quantity, response.unit);
  1451. controller.openStrand("ingredients");
  1452. controller.createBanner("INGREDIENT UPDATED", "success");
  1453. }
  1454. })
  1455. .catch((err)=>{
  1456. controller.createBanner("SOMETHING WENT WRONG, PLEASE REFRESH THE PAGE", "error");
  1457. })
  1458. .finally(()=>{
  1459. loader.style.display = "none";
  1460. });
  1461. }
  1462. }
  1463. module.exports = editIngredient;
  1464. },{}],8:[function(require,module,exports){
  1465. let editRecipe = {
  1466. display: function(recipe){
  1467. let nameInput = document.getElementById("editRecipeName");
  1468. if(merchant.pos === "none"){
  1469. nameInput.value = recipe.name;
  1470. }else{
  1471. document.getElementById("editRecipeNoName").innertext = recipe.name;
  1472. nameInput.parentNode.style.display = "none";
  1473. }
  1474. //Populate ingredients
  1475. let ingredientList = document.getElementById("editRecipeIngList");
  1476. while(ingredientList.children.length > 0){
  1477. ingredientList.removeChild(ingredientList.firstChild);
  1478. }
  1479. let template = document.getElementById("editRecipeIng").content.children[0];
  1480. for(let i = 0; i < recipe.ingredients.length; i++){
  1481. let ingredientDiv = template.cloneNode(true);
  1482. ingredientDiv.children[0].onclick = ()=>{ingredientDiv.parentNode.removeChild(ingredientDiv)};
  1483. ingredientDiv.children[1].innerText = recipe.ingredients[i].ingredient.getNameAndUnit();
  1484. ingredientDiv.children[2].style.display = "none";
  1485. ingredientDiv.children[3].value = recipe.ingredients[i].quantity;
  1486. ingredientDiv.ingredient = recipe.ingredients[i];
  1487. ingredientList.appendChild(ingredientDiv);
  1488. }
  1489. document.getElementById("addRecIng").onclick = ()=>{this.newIngredient()};
  1490. document.getElementById("editRecipePrice").value = recipe.price;
  1491. document.getElementById("editRecipeSubmit").onclick = ()=>{this.submit(recipe)};
  1492. document.getElementById("editRecipeCancel").onclick = ()=>{controller.openSidebar("recipeDetails", recipe)};
  1493. },
  1494. newIngredient: function(){
  1495. let ingredientList = document.getElementById("editRecipeIngList");
  1496. let ingredientDiv = document.getElementById("editRecipeIng").content.children[0].cloneNode(true);
  1497. ingredientDiv.children[0].onclick = ()=>{ingredientDiv.parentNode.removeChild(ingredientDiv)};
  1498. ingredientDiv.children[1].style.display = "none";
  1499. ingredientDiv.children[3].value = "0.00";
  1500. //Populate selector
  1501. let categories = merchant.categorizeIngredients();
  1502. for(let i = 0; i < categories.length; i++){
  1503. let group = document.createElement("optgroup");
  1504. group.label = categories[i].name;
  1505. for(let j = 0; j < categories[i].ingredients.length; j++){
  1506. let option = document.createElement("option");
  1507. option.innerText = categories[i].ingredients[j].ingredient.getNameAndUnit();
  1508. option.ingredient = categories[i].ingredients[j];
  1509. group.appendChild(option);
  1510. }
  1511. ingredientDiv.children[2].appendChild(group);
  1512. }
  1513. ingredientList.appendChild(ingredientDiv);
  1514. },
  1515. submit: function(recipe){
  1516. let data = {
  1517. id: recipe.id,
  1518. name: recipe.name,
  1519. price: document.getElementById("editRecipePrice").value * 100,
  1520. ingredients: []
  1521. }
  1522. if(merchant.pos === "none"){
  1523. data.name = document.getElementById("editRecipeName").value;
  1524. }
  1525. let ingredients = document.getElementById("editRecipeIngList").children;
  1526. for(let i = 0; i < ingredients.length; i++){
  1527. const quantity = parseFloat(ingredients[i].children[3].value);
  1528. let newIngredient = {};
  1529. let ingredient = {};
  1530. if(ingredients[i].children[1].style.display === "none"){
  1531. let selector = ingredients[i].children[2];
  1532. ingredient = selector.options[selector.selectedIndex].ingredient;
  1533. newIngredient = {
  1534. ingredient: ingredient.ingredient.id,
  1535. quantity: ingredient.convertToBase(quantity)
  1536. };
  1537. }else{
  1538. ingredient = ingredients[i].ingredient;
  1539. newIngredient = {
  1540. ingredient: ingredient.ingredient.id,
  1541. quantity: ingredients[i].ingredient.convertToBase(quantity)
  1542. };
  1543. }
  1544. data.ingredients.push(newIngredient);
  1545. }
  1546. let loader = document.getElementById("loaderContainer");
  1547. loader.style.display = "flex";
  1548. fetch("/recipe/update", {
  1549. method: "put",
  1550. headers: {
  1551. "Content-Type": "application/json;charset=utf-8"
  1552. },
  1553. body: JSON.stringify(data)
  1554. })
  1555. .then(response => response.json())
  1556. .then((response)=>{
  1557. if(typeof(response) === "string"){
  1558. controller.createBanner(response, "error");
  1559. }else{
  1560. merchant.updateRecipe(response);
  1561. controller.openStrand("recipeBook");
  1562. controller.createBanner("RECIPE UPDATED", "success");
  1563. }
  1564. })
  1565. .catch((err)=>{
  1566. controller.createBanner("SOMETHING WENT WRONG, PLEASE REFRESH THE PAGE", "error");
  1567. })
  1568. .finally(()=>{
  1569. loader.style.display = "none";
  1570. });
  1571. }
  1572. }
  1573. module.exports = editRecipe;
  1574. },{}],9:[function(require,module,exports){
  1575. let ingredientDetails = {
  1576. dailyUse: 0,
  1577. display: function(ingredient){
  1578. document.getElementById("editIngBtn").onclick = ()=>{controller.openSidebar("editIngredient", ingredient)};
  1579. document.getElementById("removeIngBtn").onclick = ()=>{this.remove(ingredient)};
  1580. document.getElementById("ingredientDetailsCategory").innerText = ingredient.ingredient.category;
  1581. document.getElementById("ingredientDetailsName").innerText = ingredient.ingredient.name;
  1582. document.getElementById("ingredientStock").innerText = ingredient.getQuantityDisplay();
  1583. //Calculate and display average daily use
  1584. let quantities = [];
  1585. let now = new Date();
  1586. for(let i = 1; i < 31; i++){
  1587. let endDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i)
  1588. let startDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i - 1);
  1589. quantities.push(merchant.getSingleIngredientSold(ingredient, startDay, endDay));
  1590. }
  1591. let sum = 0;
  1592. for(let i = 0; i < quantities.length; i++){
  1593. sum += quantities[i];
  1594. }
  1595. let dailyUse = sum / quantities.length;
  1596. const dailyUseDiv = document.getElementById("dailyUse");
  1597. dailyUseDiv.innerText = `${dailyUse.toFixed(2)} ${ingredient.ingredient.unit.toUpperCase()}`;
  1598. //Show recipes that this ingredient is a part of
  1599. let recipeList = document.getElementById("ingredientRecipeList");
  1600. let template = document.getElementById("ingredientRecipe").content.children[0];
  1601. let recipes = merchant.getRecipesForIngredient(ingredient.ingredient);
  1602. while(recipeList.children.length > 0){
  1603. recipeList.removeChild(recipeList.firstChild);
  1604. }
  1605. for(let i = 0; i < recipes.length; i++){
  1606. let recipeDiv = template.cloneNode(true);
  1607. recipeDiv.children[0].innerText = recipes[i].name;
  1608. recipeDiv.onclick = ()=>{
  1609. controller.openStrand("recipeBook");
  1610. controller.openSidebar("recipeDetails", recipes[i]);
  1611. }
  1612. recipeDiv.classList.add("choosable");
  1613. recipeList.appendChild(recipeDiv);
  1614. }
  1615. },
  1616. remove: function(ingredient){
  1617. for(let i = 0; i < merchant.recipes.length; i++){
  1618. for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
  1619. if(ingredient.ingredient === merchant.recipes[i].ingredients[j].ingredient){
  1620. controller.createBanner("MUST REMOVE INGREDIENT FROM ALL RECIPES BEFORE REMOVING FROM INVENTORY", "error");
  1621. return;
  1622. }
  1623. }
  1624. }
  1625. let loader = document.getElementById("loaderContainer");
  1626. loader.style.display = "flex";
  1627. fetch(`/ingredients/remove/${ingredient.ingredient.id}`, {
  1628. method: "delete",
  1629. })
  1630. .then((response) => response.json())
  1631. .then((response)=>{
  1632. if(typeof(response) === "string"){
  1633. controller.createBanner(response, "error");
  1634. }else{
  1635. merchant.removeIngredient(ingredient);
  1636. controller.openStrand("ingredients");
  1637. controller.createBanner("INGREDIENT REMOVED", "success");
  1638. }
  1639. })
  1640. .catch((err)=>{
  1641. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  1642. })
  1643. .finally(()=>{
  1644. loader.style.display = "none";
  1645. });
  1646. }
  1647. }
  1648. module.exports = ingredientDetails;
  1649. },{}],10:[function(require,module,exports){
  1650. let newIngredient = {
  1651. display: function(Ingredient){
  1652. const selector = document.getElementById("unitSelector");
  1653. document.getElementById("newIngName").value = "";
  1654. document.getElementById("newIngCategory").value = "";
  1655. document.getElementById("newIngQuantity").value = 0;
  1656. document.getElementById("bottleSizeLabel").style.display = "none";
  1657. selector.value = "g";
  1658. selector.onchange = ()=>{this.unitChange()};
  1659. document.getElementById("submitNewIng").onclick = ()=>{this.submit(Ingredient)};
  1660. document.getElementById("ingredientFileUpload").addEventListener("click", ()=>{controller.openModal("ingredientSpreadsheet")});
  1661. },
  1662. unitChange: function(){
  1663. const select = document.getElementById("unitSelector");
  1664. const bottleLabel = document.getElementById("bottleSizeLabel");
  1665. if(select.value === "bottle"){
  1666. bottleLabel.style.display = "block";
  1667. }else{
  1668. bottleLabel.style.display = "none";
  1669. }
  1670. },
  1671. submit: function(){
  1672. let unitSelector = document.getElementById("unitSelector");
  1673. let options = document.querySelectorAll("#unitSelector option");
  1674. const quantityValue = parseFloat(document.getElementById("newIngQuantity").value);
  1675. let unit = unitSelector.value;
  1676. let newIngredient = {
  1677. ingredient: {
  1678. name: document.getElementById("newIngName").value,
  1679. category: document.getElementById("newIngCategory").value,
  1680. unitType: options[unitSelector.selectedIndex].getAttribute("type")
  1681. },
  1682. quantity: quantityValue,
  1683. defaultUnit: unit
  1684. }
  1685. //Change the ingredient if it is a special unit type (ie "bottle")
  1686. if(unit === "bottle"){
  1687. newIngredient.ingredient.unitType = document.getElementById("bottleUnits").value;
  1688. newIngredient.ingredient.unitSize = parseFloat(document.getElementById("bottleSize").value);
  1689. }
  1690. let loader = document.getElementById("loaderContainer");
  1691. loader.style.display = "flex";
  1692. fetch("/ingredients/create", {
  1693. method: "POST",
  1694. headers: {
  1695. "Content-Type": "application/json;charset=utf-8"
  1696. },
  1697. body: JSON.stringify(newIngredient)
  1698. })
  1699. .then((response) => response.json())
  1700. .then((response)=>{
  1701. if(typeof(response) === "string"){
  1702. controller.createBanner(response, "error");
  1703. }else{
  1704. merchant.addIngredient(response.ingredient, response.quantity, response.defaultUnit);
  1705. controller.openStrand("ingredients");
  1706. controller.createBanner("INGREDIENT CREATED", "success");
  1707. }
  1708. })
  1709. .catch((err)=>{
  1710. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  1711. })
  1712. .finally(()=>{
  1713. loader.style.display = "none";
  1714. });
  1715. },
  1716. submitSpreadsheet: function(){
  1717. event.preventDefault();
  1718. controller.closeModal();
  1719. const file = document.getElementById("spreadsheetInput").files[0];
  1720. let data = new FormData();
  1721. data.append("ingredients", file);
  1722. let loader = document.getElementById("loaderContainer");
  1723. loader.style.display = "flex";
  1724. fetch("/ingredients/create/spreadsheet", {
  1725. method: "post",
  1726. body: data
  1727. })
  1728. .then(response => response.json())
  1729. .then((response)=>{
  1730. if(typeof(response) === "string"){
  1731. controller.createBanner(response, "error");
  1732. }else{
  1733. for(let i = 0; i < response.length; i++){
  1734. merchant.addIngredient(response[i].ingredient, response[i].quantity, response[i].defaultUnit);
  1735. }
  1736. controller.createBanner("INGREDIENTS SUCCESSFULLY ADDED", "success");
  1737. controller.openStrand("ingredients");
  1738. }
  1739. })
  1740. .catch((err)=>{
  1741. controller.createBanner("SOMETHING WENT WRONG. TRY REFRESHING THE PAGE", "error");
  1742. })
  1743. .finally(()=>{
  1744. loader.style.display = "none";
  1745. });
  1746. }
  1747. }
  1748. module.exports = newIngredient;
  1749. },{}],11:[function(require,module,exports){
  1750. let newOrder = {
  1751. display: function(){
  1752. document.getElementById("sidebarDiv").classList.add("sidebarWide");
  1753. document.getElementById("newOrderIngredientList").style.display = "flex";
  1754. document.getElementById("orderFileUpload").addEventListener("click", ()=>{controller.openModal("orderSpreadsheet")});
  1755. let selectedList = document.getElementById("selectedIngredientList");
  1756. while(selectedList.children.length > 0){
  1757. selectedList.removeChild(selectedList.firstChild);
  1758. }
  1759. let ingredientList = document.getElementById("newOrderIngredients");
  1760. while(ingredientList.children.length > 0){
  1761. ingredientList.removeChild(ingredientList.firstChild);
  1762. }
  1763. for(let i = 0; i < merchant.ingredients.length; i++){
  1764. let ingredient = document.createElement("button");
  1765. ingredient.classList = "choosable";
  1766. ingredient.innerText = merchant.ingredients[i].ingredient.name;
  1767. ingredient.onclick = ()=>{this.addIngredient(merchant.ingredients[i], ingredient)};
  1768. ingredientList.appendChild(ingredient);
  1769. }
  1770. document.getElementById("submitNewOrder").onclick = ()=>{this.submit()};
  1771. },
  1772. addIngredient: function(ingredient, element){
  1773. element.style.display = "none";
  1774. let div = document.getElementById("selectedIngredient").content.children[0].cloneNode(true);
  1775. div.ingredient = ingredient;
  1776. div.children[0].children[1].onclick = ()=>{this.removeIngredient(div, element)};
  1777. div.children[0].children[0].innerText = `${ingredient.ingredient.name} (${ingredient.ingredient.unit.toUpperCase()})`;
  1778. document.getElementById("selectedIngredientList").appendChild(div);
  1779. },
  1780. removeIngredient: function(selectedElement, element){
  1781. selectedElement.parentElement.removeChild(selectedElement);
  1782. element.style.display = "block";
  1783. },
  1784. submit: function(){
  1785. let date = document.getElementById("newOrderDate").valueAsDate;
  1786. let taxes = document.getElementById("orderTaxes").value * 100;
  1787. let fees = document.getElementById("orderFees").value * 100;
  1788. let ingredients = document.getElementById("selectedIngredientList").children;
  1789. if(date === null){
  1790. controller.createBanner("DATE IS REQUIRED FOR ORDERS", "error");
  1791. return;
  1792. }
  1793. date.setHours(0, 0, 0, 0);
  1794. let data = {
  1795. name: document.getElementById("newOrderName").value,
  1796. date: date,
  1797. taxes: taxes,
  1798. fees: fees,
  1799. ingredients: []
  1800. }
  1801. for(let i = 0; i < ingredients.length; i++){
  1802. let quantity = ingredients[i].children[1].children[0].value;
  1803. let price = ingredients[i].children[1].children[1].value;
  1804. data.ingredients.push({
  1805. ingredient: ingredients[i].ingredient.ingredient.id,
  1806. quantity: ingredients[i].ingredient.convertToBase(quantity),
  1807. pricePerUnit: this.convertPrice(ingredients[i].ingredient.ingredient, price * 100)
  1808. });
  1809. }
  1810. let loader = document.getElementById("loaderContainer");
  1811. loader.style.display = "flex";
  1812. fetch("/order/create", {
  1813. method: "post",
  1814. headers: {
  1815. "Content-Type": "application/json;charset=utf-8"
  1816. },
  1817. body: JSON.stringify(data)
  1818. })
  1819. .then((response)=>response.json())
  1820. .then((response)=>{
  1821. if(typeof(response) === "string"){
  1822. controller.createBanner(response, "error");
  1823. }else{
  1824. merchant.addOrder(response, true);
  1825. controller.openStrand("orders", merchant.orders);
  1826. controller.createBanner("NEW ORDER CREATED", "success");
  1827. }
  1828. })
  1829. .catch((err)=>{
  1830. controller.createBanner("SOMETHING WENT WRONG, PLEASE REFRESH THE PAGE", "error");
  1831. })
  1832. .finally(()=>{
  1833. loader.style.display = "none";
  1834. });
  1835. },
  1836. convertPrice: function(ingredient, price){
  1837. switch(ingredient.unit){
  1838. case "g": return price;
  1839. case "kg": return price / 1000;
  1840. case "oz": return price / 28.3495;
  1841. case "lb": return price / 453.5924;
  1842. case "ml": return price * 1000;
  1843. case "l": return price;
  1844. case "tsp": return price * 202.8842;
  1845. case "tbsp": return price * 67.6278;
  1846. case "ozfl": return price * 33.8141;
  1847. case "cup": return price * 4.1667;
  1848. case "pt": return price * 2.1134;
  1849. case "qt": return price * 1.0567;
  1850. case "gal": return price / 3.7854;
  1851. case "mm": return price * 1000;
  1852. case "cm": return price * 100;
  1853. case "m": return price;
  1854. case "in": return price * 39.3701;
  1855. case "ft": return price * 3.2808;
  1856. default: return price;
  1857. }
  1858. },
  1859. submitSpreadsheet: function(){
  1860. event.preventDefault();
  1861. controller.closeModal();
  1862. const file = document.getElementById("spreadsheetInput").files[0];
  1863. let data = new FormData();
  1864. data.append("orders", file);
  1865. data.append("timeOffset", new Date().getTimezoneOffset());
  1866. let loader = document.getElementById("loaderContainer");
  1867. loader.style.display = "flex";
  1868. fetch("/orders/create/spreadsheet", {
  1869. method: "post",
  1870. body: data
  1871. })
  1872. .then(response => response.json())
  1873. .then((response)=>{
  1874. if(typeof(response) === "string"){
  1875. controller.createBanner(response, "error");
  1876. }else{
  1877. merchant.addOrder(response, true);
  1878. controller.createBanner("ORDER CREATED AND INGREDIENTS UPDATED SUCCESSFULLY", "success");
  1879. controller.openStrand("orders");
  1880. }
  1881. })
  1882. .catch((err)=>{
  1883. controller.createBanner("UNABLE TO DISPLAY NEW ORDER. PLEASE REFRESH THE PAGE.", "error");
  1884. })
  1885. .finally(()=>{
  1886. loader.style.display = "none";
  1887. });
  1888. }
  1889. }
  1890. module.exports = newOrder;
  1891. },{}],12:[function(require,module,exports){
  1892. let newRecipe = {
  1893. display: function(Recipe){
  1894. document.getElementById("newRecipeName").value = "";
  1895. document.getElementById("newRecipePrice").value = "";
  1896. document.getElementById("ingredientCount").value = 1;
  1897. let categories = merchant.categorizeIngredients();
  1898. let ingredientsSelect = document.getElementById("recipeInputIngredients");
  1899. while(ingredientsSelect.children.length > 0){
  1900. ingredientsSelect.removeChild(ingredientsSelect.firstChild);
  1901. }
  1902. this.changeIngredientCount(categories);
  1903. document.getElementById("ingredientCount").onchange = ()=>{this.changeIngredientCount(categories)};
  1904. document.getElementById("submitNewRecipe").onclick = ()=>{this.submit(Recipe)};
  1905. document.getElementById("recipeFileUpload").onclick = ()=>{controller.openModal("recipeSpreadsheet")};
  1906. },
  1907. //Updates the number of ingredient inputs displayed for new recipes
  1908. changeIngredientCount: function(categories){
  1909. let newCount = document.getElementById("ingredientCount").value;
  1910. let ingredientsDiv = document.getElementById("recipeInputIngredients");
  1911. let template = document.getElementById("recipeInputIngredient").content.children[0];
  1912. let oldCount = ingredientsDiv.children.length;
  1913. if(newCount > oldCount){
  1914. let newDivs = newCount - oldCount;
  1915. for(let i = 0; i < newDivs; i++){
  1916. let newNode = template.cloneNode(true);
  1917. newNode.children[0].innnerText = `INGREDIENT ${i + oldCount}`;
  1918. newNode.children[2].children[0].value = 0;
  1919. for(let j = 0; j < categories.length; j++){
  1920. let optgroup = document.createElement("optgroup");
  1921. optgroup.label = categories[j].name;
  1922. for(let k = 0; k < categories[j].ingredients.length; k++){
  1923. let option = document.createElement("option");
  1924. option.innerText = categories[j].ingredients[k].ingredient.getNameAndUnit();
  1925. option.ingredient = categories[j].ingredients[k];
  1926. optgroup.appendChild(option);
  1927. }
  1928. newNode.children[1].children[0].appendChild(optgroup);
  1929. }
  1930. ingredientsDiv.appendChild(newNode);
  1931. }
  1932. for(let i = 0; i < newCount; i++){
  1933. ingredientsDiv.children[i].children[0].innerText = `INGREDIENT ${i + 1}`;
  1934. }
  1935. }else if(newCount < oldCount){
  1936. let newDivs = oldCount - newCount;
  1937. for(let i = 0; i < newDivs; i++){
  1938. ingredientsDiv.removeChild(ingredientsDiv.children[ingredientsDiv.children.length-1]);
  1939. }
  1940. }
  1941. },
  1942. submit: function(Recipe){
  1943. let newRecipe = {
  1944. name: document.getElementById("newRecipeName").value,
  1945. price: document.getElementById("newRecipePrice").value,
  1946. ingredients: []
  1947. }
  1948. let inputs = document.getElementById("recipeInputIngredients").children;
  1949. for(let i = 0; i < inputs.length; i++){
  1950. let sel = inputs[i].children[1].children[0];
  1951. let ingredient = sel.options[sel.selectedIndex].ingredient;
  1952. let newIngredient = {
  1953. ingredient: ingredient.ingredient.id,
  1954. quantity: ingredient.convertToBase(inputs[i].children[2].children[0].value)
  1955. };
  1956. newRecipe.ingredients.push(newIngredient);
  1957. }
  1958. let loader = document.getElementById("loaderContainer");
  1959. loader.style.display = "flex";
  1960. fetch("/recipe/create", {
  1961. method: "POST",
  1962. headers: {
  1963. "Content-Type": "application/json;charset=utf-8"
  1964. },
  1965. body: JSON.stringify(newRecipe)
  1966. })
  1967. .then((response) => response.json())
  1968. .then((response)=>{
  1969. if(typeof(response) === "string"){
  1970. controller.createBanner(response, "error");
  1971. }else{
  1972. let ingredients = [];
  1973. for(let i = 0; i < response.ingredients.length; i++){
  1974. for(let j = 0; j < merchant.ingredients.length; j++){
  1975. if(merchant.ingredients[j].ingredient.id === response.ingredients[i].ingredient){
  1976. ingredients.push({
  1977. ingredient: merchant.ingredients[j].ingredient.id,
  1978. quantity: response.ingredients[i].quantity
  1979. });
  1980. break;
  1981. }
  1982. }
  1983. }
  1984. merchant.addRecipe(
  1985. response._id,
  1986. response.name,
  1987. response.price,
  1988. ingredients
  1989. );
  1990. controller.createBanner("RECIPE CREATED", "success");
  1991. controller.openStrand("recipeBook");
  1992. }
  1993. })
  1994. .catch((err)=>{
  1995. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  1996. })
  1997. .finally(()=>{
  1998. loader.style.display = "none";
  1999. });
  2000. },
  2001. submitSpreadsheet: function(){
  2002. event.preventDefault();
  2003. controller.closeModal();
  2004. const file = document.getElementById("spreadsheetInput").files[0];
  2005. let data = new FormData();
  2006. data.append("recipes", file);
  2007. let loader = document.getElementById("loaderContainer");
  2008. loader.style.display = "flex";
  2009. fetch("/recipes/create/spreadsheet", {
  2010. method: "post",
  2011. body: data
  2012. })
  2013. .then(response => response.json())
  2014. .then((response)=>{
  2015. if(typeof(response) === "string"){
  2016. controller.createBanner(response, "error");
  2017. }else{
  2018. for(let i = 0; i < response.length; i++){
  2019. merchant.addRecipe(
  2020. response[i]._id,
  2021. response[i].name,
  2022. response[i].price,
  2023. response[i].ingredients
  2024. );
  2025. }
  2026. controller.createBanner("ALL INGREDIENTS SUCCESSFULLY CREATED", "success");
  2027. controller.openStrand("recipeBook");
  2028. }
  2029. })
  2030. .catch((err)=>{
  2031. controller.createBanner("UNABLE TO DISPLAY NEW RECIPES. PLEASE REFRESH THE PAGE", "error");
  2032. })
  2033. .finally(()=>{
  2034. loader.style.display = "none";
  2035. });
  2036. }
  2037. }
  2038. module.exports = newRecipe;
  2039. },{}],13:[function(require,module,exports){
  2040. let newTransaction = {
  2041. display: function(){
  2042. let recipeList = document.getElementById("newTransactionRecipes");
  2043. let template = document.getElementById("createTransaction").content.children[0];
  2044. document.getElementById("transactionFileUpload").addEventListener("click", ()=>{controller.openModal("transactionSpreadsheet")});
  2045. while(recipeList.children.length > 0){
  2046. recipeList.removeChild(recipeList.firstChild);
  2047. }
  2048. for(let i = 0; i < merchant.recipes.length; i++){
  2049. let recipeDiv = template.cloneNode(true);
  2050. recipeDiv.recipe = merchant.recipes[i];
  2051. recipeList.appendChild(recipeDiv);
  2052. recipeDiv.children[0].innerText = merchant.recipes[i].name;
  2053. }
  2054. document.getElementById("submitNewTransaction").onclick = ()=>{this.submit()};
  2055. },
  2056. submit: function(){
  2057. let recipeDivs = document.getElementById("newTransactionRecipes");
  2058. let date = document.getElementById("newTransactionDate").valueAsDate;
  2059. if(date === null){
  2060. controller.createBanner("DATE IS REQUIRED FOR TRANSACTIONS", "error");
  2061. return;
  2062. }
  2063. date.setHours(0, 0, 0, 0);
  2064. let data = {
  2065. date: date,
  2066. recipes: [],
  2067. ingredientUpdates: {}
  2068. };
  2069. for(let i = 0; i < recipeDivs.children.length; i++){
  2070. let quantity = recipeDivs.children[i].children[1].value;
  2071. const recipe = recipeDivs.children[i].recipe;
  2072. if(quantity !== "" && quantity > 0){
  2073. data.recipes.push({
  2074. recipe: recipe.id,
  2075. quantity: quantity
  2076. });
  2077. for(let j = 0; j < recipe.ingredients.length; j++){
  2078. let ingredient = recipe.ingredients[j];
  2079. if(data.ingredientUpdates[ingredient.ingredient.id]){
  2080. data.ingredientUpdates[ingredient.ingredient.id] += ingredient.convertToBase(ingredient.quantity) * quantity;
  2081. }else{
  2082. data.ingredientUpdates[ingredient.ingredient.id] = ingredient.convertToBase(ingredient.quantity) * quantity;
  2083. }
  2084. }
  2085. }else if(quantity < 0){
  2086. controller.createBanner("CANNOT HAVE NEGATIVE VALUES", "error");
  2087. return;
  2088. }
  2089. }
  2090. if(data.recipes.length > 0){
  2091. let loader = document.getElementById("loaderContainer");
  2092. loader.style.display = "flex";
  2093. fetch("/transaction/create", {
  2094. method: "post",
  2095. headers: {
  2096. "Content-Type": "application/json;charset=utf-8"
  2097. },
  2098. body: JSON.stringify(data)
  2099. })
  2100. .then(response => response.json())
  2101. .then((response)=>{
  2102. if(typeof(response) === "string"){
  2103. controller.createBanner(response, "error");
  2104. }else{
  2105. merchant.addTransaction(response);
  2106. controller.updateAnalytics();
  2107. controller.openStrand("transactions", merchant.getTransactions());
  2108. controller.createBanner("TRANSACTION CREATED", "success");
  2109. }
  2110. })
  2111. .catch((err)=>{
  2112. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  2113. })
  2114. .finally(()=>{
  2115. loader.style.display = "none";
  2116. });
  2117. }
  2118. },
  2119. submitSpreadsheet: function(){
  2120. event.preventDefault();
  2121. controller.closeModal();
  2122. const file = document.getElementById("spreadsheetInput").files[0];
  2123. let data = new FormData();
  2124. data.append("transactions", file);
  2125. data.append("timeOffset", new Date().getTimezoneOffset());
  2126. let loader = document.getElementById("loaderContainer");
  2127. loader.style.display = "flex";
  2128. fetch("/transactions/create/spreadsheet", {
  2129. method: "post",
  2130. body: data
  2131. })
  2132. .then(response => response.json())
  2133. .then((response)=>{
  2134. if(typeof(response) === "string"){
  2135. controller.createBanner(response, "error");
  2136. }else{
  2137. for(let i = 0; i < response.recipes.length; i++){
  2138. response.recipes[i].recipe = response.recipes[i].recipe._id;
  2139. }
  2140. merchant.addTransaction(response);
  2141. controller.updateAnalytics();
  2142. controller.openStrand("transactions", merchant.transactions);
  2143. controller.createBanner("TRANSACTION SUCCESSFULLY CREATED. INGREDIENTS UPDATED", "success");
  2144. }
  2145. })
  2146. .catch((err)=>{
  2147. controller.createBanner("UNABLE TO DISPLAY NEW TRANSACTIONS. PLEASE REFRESH THE PAGE", "error");
  2148. })
  2149. .finally(()=>{
  2150. loader.style.display = "none";
  2151. });
  2152. }
  2153. }
  2154. module.exports = newTransaction;
  2155. },{}],14:[function(require,module,exports){
  2156. let orderCalculator = {
  2157. display: function(){
  2158. let calculatorItems = document.getElementById("calculatorItemsBody");
  2159. let template = document.getElementById("calculatorItem").content.children[0];
  2160. let calculations = this.predict();
  2161. while(calculatorItems.children.length > 0){
  2162. calculatorItems.removeChild(calculatorItems.firstChild);
  2163. }
  2164. for(let i = 0; i < calculations.length; i++){
  2165. let outputString = `${calculations[i].output.toFixed(2)} ${calculations[i].ingredient.unit.toUpperCase()}`;
  2166. let item = template.cloneNode(true);
  2167. item.children[0].innerText = calculations[i].ingredient.name,
  2168. item.children[1].innerText = outputString;
  2169. calculatorItems.appendChild(item);
  2170. }
  2171. },
  2172. predict: function(){
  2173. let now = new Date();
  2174. let yesterday = new Date();
  2175. yesterday.setHours(0, 0, 0, 0);
  2176. let monthAgo = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 30);
  2177. let weekAgo = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 7);
  2178. let calculations = [];
  2179. let month = merchant.getIngredientsSold(monthAgo, yesterday);
  2180. let week = merchant.getIngredientsSold(weekAgo, yesterday);
  2181. let weights = {
  2182. month: 0.33,
  2183. week: 0.67
  2184. }
  2185. for(let i = 0; i < month.length; i++){
  2186. for(let j = 0; j < week.length; j++){
  2187. if(month[i].ingredient.id === week[j].ingredient.id){
  2188. let monthAverage = (month[i].quantity / 30) * weights.month;
  2189. let weekAverage = (week[i].quantity / 7) * weights.week;
  2190. let calc = {
  2191. ingredient: month[i].ingredient,
  2192. output: monthAverage + weekAverage
  2193. };
  2194. calculations.push(calc);
  2195. }
  2196. }
  2197. }
  2198. return calculations;
  2199. }
  2200. }
  2201. module.exports = orderCalculator;
  2202. },{}],15:[function(require,module,exports){
  2203. let orderDetails = {
  2204. display: function(order){
  2205. document.getElementById("removeOrderBtn").onclick = ()=>{this.remove(order)};
  2206. document.getElementById("orderDetailName").innerText = order.name;
  2207. document.getElementById("orderDetailDate").innerText = order.date.toLocaleDateString("en-US");
  2208. document.getElementById("orderDetailTax").innerText = `$${order.taxes.toFixed(2)}`;
  2209. document.getElementById("orderDetailFee").innerText = `$${order.fees.toFixed(2)}`;
  2210. let ingredientList = document.getElementById("orderIngredients");
  2211. while(ingredientList.children.length > 0){
  2212. ingredientList.removeChild(ingredientList.firstChild);
  2213. }
  2214. let template = document.getElementById("orderIngredient").content.children[0];
  2215. for(let i = 0; i < order.ingredients.length; i++){
  2216. let ingredientDiv = template.cloneNode(true);
  2217. const ingredient = order.ingredients[i].ingredient;
  2218. ingredientDiv.children[0].innerText = order.ingredients[i].ingredient.name;
  2219. ingredientDiv.children[2].innerText = `$${order.ingredients[i].cost().toFixed(2)}`;
  2220. ingredientDiv.onclick = ()=>{
  2221. controller.openStrand("ingredients");
  2222. controller.openSidebar("ingredientDetails", merchant.getIngredient(order.ingredients[i].ingredient.id));
  2223. }
  2224. let ingredientDisplay = ingredientDiv.children[1];
  2225. ingredientDisplay.innerText = `${order.ingredients[i].quantity.toFixed(2)} ${ingredient.unit.toUpperCase()} X $${order.ingredients[i].pricePerUnit.toFixed(2)}`;
  2226. ingredientList.appendChild(ingredientDiv);
  2227. }
  2228. document.getElementById("orderDetailTotal").innerText = `$${order.getIngredientCost().toFixed(2)}`;
  2229. document.querySelector("#orderTotalPrice p").innerText = `$${order.getTotalCost().toFixed(2)}`;
  2230. },
  2231. remove: function(order){
  2232. let loader = document.getElementById("loaderContainer");
  2233. loader.style.display = "flex";
  2234. fetch(`/order/${order.id}`, {
  2235. method: "DELETE",
  2236. headers: {
  2237. "Content-Type": "application/json;charset=utf-8"
  2238. }
  2239. })
  2240. .then((response) => response.json())
  2241. .then((response)=>{
  2242. if(typeof(response) === "string"){
  2243. controller.createBanner(response, "error");
  2244. }else{
  2245. merchant.removeOrder(order);
  2246. controller.openStrand("orders", merchant.orders);
  2247. controller.createBanner("ORDER REMOVED", "success");
  2248. }
  2249. })
  2250. .catch((err)=>{
  2251. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  2252. })
  2253. .finally(()=>{
  2254. loader.style.display = "none";
  2255. });
  2256. }
  2257. }
  2258. module.exports = orderDetails;
  2259. },{}],16:[function(require,module,exports){
  2260. let orderFilter = {
  2261. display: function(Order){
  2262. let now = new Date();
  2263. let past = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 30);
  2264. let ingredientList = document.getElementById("orderFilterIngredients");
  2265. document.getElementById("orderFilterDateFrom").valueAsDate = past;
  2266. document.getElementById("orderFilterDateTo").valueAsDate = now;
  2267. while(ingredientList.children.length > 0){
  2268. ingredientList.removeChild(ingredientList.firstChild);
  2269. }
  2270. for(let i = 0; i < merchant.ingredients.length; i++){
  2271. let element = document.createElement("div");
  2272. element.classList.add("choosable");
  2273. element.ingredient = merchant.ingredients[i].ingredient.id;
  2274. element.onclick = ()=>{this.toggleActive(element)};
  2275. ingredientList.appendChild(element);
  2276. let text = document.createElement("p");
  2277. text.innerText = merchant.ingredients[i].ingredient.name;
  2278. element.appendChild(text);
  2279. }
  2280. document.getElementById("orderFilterSubmit").onclick = ()=>{this.submit(Order)};
  2281. },
  2282. toggleActive: function(element){
  2283. if(element.classList.contains("active")){
  2284. element.classList.remove("active");
  2285. }else{
  2286. element.classList.add("active");
  2287. }
  2288. },
  2289. submit: function(Order){
  2290. let data = {
  2291. from: document.getElementById("orderFilterDateFrom").valueAsDate,
  2292. to: document.getElementById("orderFilterDateTo").valueAsDate,
  2293. ingredients: []
  2294. }
  2295. data.from.setHours(0, 0, 0, 0);
  2296. data.to.setHours(0, 0, 0, 0);
  2297. let ingredients = document.getElementById("orderFilterIngredients").children;
  2298. for(let i = 0; i < ingredients.length; i++){
  2299. if(ingredients[i].classList.contains("active")){
  2300. data.ingredients.push(ingredients[i].ingredient);
  2301. }
  2302. }
  2303. let loader = document.getElementById("loaderContainer");
  2304. loader.style.display = "flex";
  2305. fetch("/orders/get", {
  2306. method: "post",
  2307. headers: {
  2308. "Content-Type": "application/json;charset=utf-8"
  2309. },
  2310. body: JSON.stringify(data)
  2311. })
  2312. .then(response => response.json())
  2313. .then((response)=>{
  2314. let orders = [];
  2315. if(typeof(response) === "string"){
  2316. controller.createBanner(response, "error");
  2317. }else if(response.length === 0){
  2318. controller.createBanner("NO ORDERS MATCH YOUR SEARCH", "error");
  2319. }else{
  2320. for(let i = 0; i < response.length; i++){
  2321. orders.push(new Order(
  2322. response[i]._id,
  2323. response[i].name,
  2324. response[i].date,
  2325. response[i].taxes,
  2326. response[i].fees,
  2327. response[i].ingredients,
  2328. merchant
  2329. ));
  2330. }
  2331. }
  2332. controller.openStrand("orders", orders);
  2333. })
  2334. .catch((err)=>{
  2335. controller.createBanner("UNABLE TO DISPLAY THE ORDERS", "error");
  2336. })
  2337. .finally(()=>{
  2338. loader.style.display = "none";
  2339. });
  2340. }
  2341. }
  2342. module.exports = orderFilter;
  2343. },{}],17:[function(require,module,exports){
  2344. let recipeDetails = {
  2345. display: function(recipe){
  2346. document.getElementById("editRecipeBtn").onclick = ()=>{controller.openSidebar("editRecipe", recipe)};
  2347. document.getElementById("recipeName").innerText = recipe.name;
  2348. if(merchant.pos === "none"){
  2349. document.getElementById("removeRecipeBtn").onclick = ()=>{this.remove(recipe)};
  2350. }
  2351. //ingredient list
  2352. let ingredientsDiv = document.getElementById("recipeIngredientList");
  2353. while(ingredientsDiv.children.length > 0){
  2354. ingredientsDiv.removeChild(ingredientsDiv.firstChild);
  2355. }
  2356. let template = document.getElementById("recipeIngredient").content.children[0];
  2357. for(let i = 0; i < recipe.ingredients.length; i++){
  2358. let recipeDiv = template.cloneNode(true);
  2359. recipeDiv.children[0].innerText = recipe.ingredients[i].ingredient.name;
  2360. recipeDiv.children[1].innerText = `${recipe.ingredients[i].getQuantityDisplay()}`;
  2361. recipeDiv.onclick = ()=>{
  2362. controller.openStrand("ingredients");
  2363. controller.openSidebar("ingredientDetails", merchant.getIngredient(recipe.ingredients[i].ingredient.id));
  2364. }
  2365. ingredientsDiv.appendChild(recipeDiv);
  2366. }
  2367. document.getElementById("recipePrice").children[1].innerText = `$${recipe.price.toFixed(2)}`;
  2368. },
  2369. remove: function(recipe){
  2370. let loader = document.getElementById("loaderContainer");
  2371. loader.style.display = "flex";
  2372. fetch(`/recipe/remove/${recipe.id}`, {
  2373. method: "delete"
  2374. })
  2375. .then((response) => response.json())
  2376. .then((response)=>{
  2377. if(typeof(response) === "string"){
  2378. controller.createBanner(response, "error");
  2379. }else{
  2380. merchant.removeRecipe(recipe);
  2381. controller.createBanner("RECIPE REMOVED", "success");
  2382. controller.openStrand("recipeBook");
  2383. }
  2384. })
  2385. .catch((err)=>{
  2386. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  2387. })
  2388. .finally(()=>{
  2389. loader.style.display = "none";
  2390. });
  2391. }
  2392. }
  2393. module.exports = recipeDetails;
  2394. },{}],18:[function(require,module,exports){
  2395. let transactionDetails = {
  2396. transaction: {},
  2397. display: function(transaction){
  2398. this.transaction = transaction;
  2399. let recipeList = document.getElementById("transactionRecipes");
  2400. let template = document.getElementById("transactionRecipe").content.children[0];
  2401. let totalRecipes = 0;
  2402. let totalPrice = 0;
  2403. while(recipeList.children.length > 0){
  2404. recipeList.removeChild(recipeList.firstChild);
  2405. }
  2406. for(let i = 0; i < transaction.recipes.length; i++){
  2407. let recipe = template.cloneNode(true);
  2408. let price = transaction.recipes[i].quantity * transaction.recipes[i].recipe.price;
  2409. recipe.children[0].innerText = transaction.recipes[i].recipe.name;
  2410. recipe.children[1].innerText = `${transaction.recipes[i].quantity} x $${transaction.recipes[i].recipe.price.toFixed(2)}`;
  2411. recipe.children[2].innerText = `$${price.toFixed(2)}`;
  2412. recipe.onclick = ()=>{
  2413. controller.openStrand("recipeBook");
  2414. controller.openSidebar("recipeDetails", transaction.recipes[i].recipe);
  2415. }
  2416. recipeList.appendChild(recipe);
  2417. totalRecipes += transaction.recipes[i].quantity;
  2418. totalPrice += price;
  2419. }
  2420. let months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  2421. let days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  2422. let dateString = `${days[transaction.date.getDay()]}, ${months[transaction.date.getMonth()]} ${transaction.date.getDate()}, ${transaction.date.getFullYear()}`;
  2423. document.getElementById("transactionDate").innerText = dateString;
  2424. document.getElementById("totalRecipes").innerText = `${totalRecipes} recipes`;
  2425. document.getElementById("totalPrice").innerText = `$${totalPrice.toFixed(2)}`;
  2426. if(merchant.pos === "none"){
  2427. document.getElementById("removeTransBtn").onclick = ()=>{this.remove()};
  2428. }
  2429. },
  2430. remove: function(){
  2431. let loader = document.getElementById("loaderContainer");
  2432. loader.style.display = "flex";
  2433. fetch(`/transaction/${this.transaction.id}`, {
  2434. method: "delete",
  2435. headers: {
  2436. "Content-Type": "application/json;charset=utf-8"
  2437. },
  2438. })
  2439. .then((response)=>{
  2440. if(typeof(response) === "string"){
  2441. controller.createBanner(response, "error");
  2442. }else{
  2443. merchant.removeTransaction(this.transaction);
  2444. controller.updateAnalytics();
  2445. controller.openStrand("transactions", merchant.getTransactions());
  2446. controller.createBanner("TRANSACTION REMOVED", "success");
  2447. }
  2448. })
  2449. .catch((err)=>{
  2450. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  2451. })
  2452. .finally(()=>{
  2453. loader.style.display = "none";
  2454. });
  2455. },
  2456. }
  2457. module.exports = transactionDetails;
  2458. },{}],19:[function(require,module,exports){
  2459. let transactionFilter = {
  2460. display: function(Transaction){
  2461. //Set default dates
  2462. let today = new Date();
  2463. let monthAgo = new Date(today);
  2464. monthAgo.setMonth(today.getMonth() - 1);
  2465. document.getElementById("transFilterDateStart").valueAsDate = monthAgo;
  2466. document.getElementById("transFilterDateEnd").valueAsDate = today;
  2467. //populate recipes
  2468. let recipeList = document.getElementById("transFilterRecipeList");
  2469. while(recipeList.children.length > 0){
  2470. recipeList.removeChild(recipeList.firstChild);
  2471. }
  2472. for(let i = 0; i < merchant.recipes.length; i++){
  2473. let recipe = document.createElement("div");
  2474. recipe.innerText = merchant.recipes[i].name;
  2475. recipe.recipe = merchant.recipes[i];
  2476. recipe.classList.add("choosable");
  2477. recipe.onclick = ()=>{this.toggleActive(recipe)};
  2478. recipeList.appendChild(recipe);
  2479. }
  2480. //Submit button
  2481. document.getElementById("transFilterSubmit").onclick = ()=>{this.submit(Transaction)};
  2482. },
  2483. toggleActive: function(element){
  2484. if(element.classList.contains("active")){
  2485. element.classList.remove("active");
  2486. }else{
  2487. element.classList.add("active");
  2488. }
  2489. },
  2490. submit: function(Transaction){
  2491. let data = {
  2492. from: document.getElementById("transFilterDateStart").valueAsDate,
  2493. to: document.getElementById("transFilterDateEnd").valueAsDate,
  2494. recipes: []
  2495. }
  2496. data.from.setHours(0, 0, 0, 0);
  2497. data.to.setHours(0, 0, 0, 0);
  2498. if(data.startDate >= data.endDate){
  2499. controller.createBanner("START DATE CANNOT BE AFTER END DATE", "error");
  2500. return;
  2501. }
  2502. let recipes = document.getElementById("transFilterRecipeList").children;
  2503. for(let i = 0; i < recipes.length; i++){
  2504. if(recipes[i].classList.contains("active")){
  2505. data.recipes.push(recipes[i].recipe.id);
  2506. }
  2507. }
  2508. let loader = document.getElementById("loaderContainer");
  2509. loader.style.display = "flex";
  2510. fetch("/transaction", {
  2511. method: "post",
  2512. headers: {
  2513. "Content-Type": "application/json;charset=utf-8"
  2514. },
  2515. body: JSON.stringify(data)
  2516. })
  2517. .then(response => response.json())
  2518. .then((response)=>{
  2519. let transactions = [];
  2520. if(typeof(response) === "string"){
  2521. controller.createBanner(response, "error");
  2522. }else if(response.length === 0){
  2523. controller.createBanner("NO TRANSACTIONS MATCH YOUR SEARCH", "error");
  2524. }else{
  2525. for(let i = 0; i < response.length; i++){
  2526. transactions.push(new Transaction(
  2527. response[i]._id,
  2528. response[i].date,
  2529. response[i].recipes,
  2530. merchant
  2531. ));
  2532. }
  2533. }
  2534. controller.openStrand("transactions", transactions);
  2535. })
  2536. .catch((err)=>{
  2537. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  2538. })
  2539. .finally(()=>{
  2540. loader.style.display = "none";
  2541. });
  2542. }
  2543. }
  2544. module.exports = transactionFilter;
  2545. },{}],20:[function(require,module,exports){
  2546. let analytics = {
  2547. isPopulated: false,
  2548. ingredient: undefined,
  2549. recipe: undefined,
  2550. transactionsByDate: [],
  2551. display: function(Transaction){
  2552. if(!this.isPopulated){
  2553. document.getElementById("analRecipeContent").style.display = "none";
  2554. let to = new Date()
  2555. let from = new Date(to.getFullYear(), to.getMonth() - 1, to.getDate());
  2556. document.getElementById("analStartDate").valueAsDate = from;
  2557. document.getElementById("analEndDate").valueAsDate = to;
  2558. let analSlider = document.getElementById("analSlider");
  2559. analSlider.onclick = ()=>{this.switchDisplay()};
  2560. analSlider.checked = false;
  2561. document.getElementById("analDateBtn").onclick = ()=>{this.newDates(Transaction)};
  2562. this.populateButtons();
  2563. if(merchant.ingredients.length > 0){
  2564. this.ingredient = merchant.ingredients[0].ingredient;
  2565. }
  2566. if(merchant.recipes.length > 0){
  2567. this.recipe = merchant.recipes[0];
  2568. }
  2569. this.newDates(Transaction);
  2570. this.isPopulated = true;
  2571. }
  2572. },
  2573. populateButtons: function(){
  2574. let ingredientButtons = document.getElementById("analIngredientList");
  2575. let recipeButtons = document.getElementById("analRecipeList");
  2576. while(ingredientButtons.children.length > 0){
  2577. ingredientButtons.removeChild(ingredientButtons.firstChild);
  2578. }
  2579. for(let i = 0; i < merchant.ingredients.length; i++){
  2580. let button = document.createElement("button");
  2581. button.innerText = merchant.ingredients[i].ingredient.name;
  2582. button.classList.add("choosable");
  2583. button.onclick = ()=>{
  2584. this.ingredient = merchant.ingredients[i].ingredient;
  2585. this.displayIngredient();
  2586. };
  2587. ingredientButtons.appendChild(button);
  2588. }
  2589. while(recipeButtons.children.length > 0){
  2590. recipeButtons.removeChild(recipeButtons.firstChild);
  2591. }
  2592. for(let i = 0; i < merchant.recipes.length; i++){
  2593. let button = document.createElement("button");
  2594. button.innerText = merchant.recipes[i].name;
  2595. button.classList.add("choosable");
  2596. button.onclick = ()=>{
  2597. this.recipe = merchant.recipes[i];
  2598. this.displayRecipe();
  2599. };
  2600. recipeButtons.appendChild(button);
  2601. }
  2602. },
  2603. getData: function(from, to, Transaction){
  2604. let data = {
  2605. from: from,
  2606. to: to,
  2607. recipes: []
  2608. }
  2609. let loader = document.getElementById("loaderContainer");
  2610. loader.style.display = "flex";
  2611. return fetch("/transaction", {
  2612. method: "post",
  2613. headers: {
  2614. "Content-Type": "application/json;charset=utf-8"
  2615. },
  2616. body: JSON.stringify(data)
  2617. })
  2618. .then(response => response.json())
  2619. .then((response)=>{
  2620. if(typeof(response) === "string"){
  2621. controller.createBanner(response, "error");
  2622. }else{
  2623. this.transactionsByDate = [];
  2624. response.reverse();
  2625. let startOfDay = new Date(from.getTime());
  2626. startOfDay.setHours(0, 0, 0, 0);
  2627. let endOfDay = new Date(from.getTime());
  2628. endOfDay.setDate(endOfDay.getDate() + 1);
  2629. endOfDay.setHours(0, 0, 0, 0);
  2630. let transactionIndex = 0;
  2631. while(startOfDay <= to){
  2632. let currentTransactions = [];
  2633. while(transactionIndex < response.length && new Date(response[transactionIndex].date) < endOfDay){
  2634. currentTransactions.push(new Transaction(
  2635. response[transactionIndex]._id,
  2636. response[transactionIndex].date,
  2637. response[transactionIndex].recipes,
  2638. merchant
  2639. ));
  2640. transactionIndex++;
  2641. }
  2642. let thing = {
  2643. date: new Date(startOfDay.getTime()),
  2644. transactions: currentTransactions
  2645. };
  2646. this.transactionsByDate.push(thing);
  2647. startOfDay.setDate(startOfDay.getDate() + 1);
  2648. endOfDay.setDate(endOfDay.getDate() + 1);
  2649. }
  2650. }
  2651. })
  2652. .catch((err)=>{
  2653. controller.createBanner("UNABLE TO UPDATE THE PAGE", "error");
  2654. })
  2655. .finally(()=>{
  2656. loader.style.display = "none";
  2657. });
  2658. },
  2659. displayIngredient: function(){
  2660. if(this.ingredient === undefined || this.transactionsByDate.length === 0){
  2661. return;
  2662. }
  2663. //break down data into dates and quantities
  2664. let dates = [];
  2665. let quantities = [];
  2666. for(let i = 0; i < this.transactionsByDate.length; i++){
  2667. dates.push(this.transactionsByDate[i].date);
  2668. let sum = 0;
  2669. for(let j = 0; j < this.transactionsByDate[i].transactions.length; j++){
  2670. let transaction = this.transactionsByDate[i].transactions[j];
  2671. sum += transaction.getIngredientQuantity(this.ingredient);
  2672. }
  2673. quantities.push(sum);
  2674. }
  2675. //create and display the graph
  2676. let trace = {
  2677. x: dates,
  2678. y: quantities,
  2679. mode: "lines+markers",
  2680. line: {
  2681. color: "rgb(255, 99, 107)"
  2682. }
  2683. }
  2684. let yaxis = `QUANTITY (${this.ingredient.unit.toUpperCase()})`;
  2685. const layout = {
  2686. title: this.ingredient.name.toUpperCase(),
  2687. xaxis: {title: "DATE"},
  2688. yaxis: {title: yaxis}
  2689. }
  2690. Plotly.newPlot("itemUseGraph", [trace], layout);
  2691. //Create min/max/avg
  2692. //Current ingredient is stored on the "analMinUse" element
  2693. let min = quantities[0];
  2694. let max = quantities[0];
  2695. let sum = 0;
  2696. for(let i = 0; i < quantities.length; i++){
  2697. if(quantities[i] < min){
  2698. min = quantities[i];
  2699. }
  2700. if(quantities[i] > max){
  2701. max = quantities[i];
  2702. }
  2703. sum += quantities[i];
  2704. }
  2705. document.getElementById("analMinUse").innerText = `${min.toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2706. document.getElementById("analAvgUse").innerText = `${(sum / quantities.length).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2707. document.getElementById("analMaxUse").innerText = `${max.toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2708. //Create weekday averages
  2709. let dayUse = [0, 0, 0, 0, 0, 0, 0];
  2710. let dayCount = [0, 0, 0, 0, 0, 0, 0];
  2711. for(let i = 0; i < quantities.length; i++){
  2712. dayUse[dates[i].getDay()] += quantities[i];
  2713. dayCount[dates[i].getDay()]++;
  2714. }
  2715. document.getElementById("analDayOne").innerText = `${(dayUse[0] / dayCount[0]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2716. document.getElementById("analDayTwo").innerText = `${(dayUse[1] / dayCount[1]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2717. document.getElementById("analDayThree").innerText = `${(dayUse[2] / dayCount[2]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2718. document.getElementById("analDayFour").innerText = `${(dayUse[3] / dayCount[3]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2719. document.getElementById("analDayFive").innerText = `${(dayUse[4] / dayCount[4]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2720. document.getElementById("analDaySix").innerText = `${(dayUse[5] / dayCount[5]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2721. document.getElementById("analDaySeven").innerText = `${(dayUse[6] / dayCount[6]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2722. },
  2723. displayRecipe: function(){
  2724. if(this.recipe === undefined || this.transactionsByDate.length === 0){
  2725. return;
  2726. }
  2727. //break down data into dates and quantities
  2728. let dates = [];
  2729. let quantities = [];
  2730. for(let i = 0; i < this.transactionsByDate.length; i++){
  2731. dates.push(this.transactionsByDate[i].date);
  2732. let sum = 0;
  2733. for(let j = 0; j < this.transactionsByDate[i].transactions.length; j++){
  2734. const transaction = this.transactionsByDate[i].transactions[j];
  2735. for(let k = 0; k < transaction.recipes.length; k++){
  2736. if(transaction.recipes[k].recipe === this.recipe){
  2737. sum += transaction.recipes[k].quantity;
  2738. }
  2739. }
  2740. }
  2741. quantities.push(sum);
  2742. }
  2743. //create and display the graph
  2744. const trace = {
  2745. x: dates,
  2746. y: quantities,
  2747. mode: "lines+markers",
  2748. line: {
  2749. color: "rgb(255, 99, 107)"
  2750. }
  2751. }
  2752. const layout = {
  2753. title: this.recipe.name.toUpperCase(),
  2754. xaxis: {title: "DATE"},
  2755. yaxis: {title: "QUANTITY"}
  2756. }
  2757. Plotly.newPlot("recipeSalesGraph", [trace], layout);
  2758. //Display the boxes at the bottom
  2759. //Current recipe is stored on the "recipeAvgUse" element
  2760. let avg = 0;
  2761. for(let i = 0; i < quantities.length; i++){
  2762. avg += quantities[i];
  2763. }
  2764. avg = avg / quantities.length;
  2765. document.getElementById("recipeAvgUse").innerText = avg.toFixed(2);
  2766. document.getElementById("recipeAvgRevenue").innerText = `$${(avg * this.recipe.price).toFixed(2)}`;
  2767. },
  2768. switchDisplay: function(){
  2769. const checkbox = document.getElementById("analSlider");
  2770. let ingredient = document.getElementById("analIngredientContent");
  2771. let recipe = document.getElementById("analRecipeContent");
  2772. if(checkbox.checked === true){
  2773. ingredient.style.display = "none";
  2774. recipe.style.display = "flex";
  2775. this.displayRecipe();
  2776. }else{
  2777. ingredient.style.display = "flex";
  2778. recipe.style.display = "none";
  2779. this.displayIngredient();
  2780. }
  2781. },
  2782. newDates: async function(Transaction){
  2783. const from = document.getElementById("analStartDate").valueAsDate;
  2784. const to = document.getElementById("analEndDate").valueAsDate;
  2785. from.setHours(0, 0, 0, 0);
  2786. to.setDate(to.getDate() + 1);
  2787. to.setHours(0, 0, 0, 0);
  2788. await this.getData(from, to, Transaction);
  2789. if(document.getElementById("analSlider").checked === true){
  2790. this.displayRecipe();
  2791. }else{
  2792. this.displayIngredient();
  2793. }
  2794. }
  2795. }
  2796. module.exports = analytics;
  2797. },{}],21:[function(require,module,exports){
  2798. let home = {
  2799. isPopulated: false,
  2800. display: function(){
  2801. if(!this.isPopulated){
  2802. this.drawRevenueCard();
  2803. this.drawRevenueGraph();
  2804. this.drawInventoryCheckCard();
  2805. this.drawPopularCard();
  2806. this.isPopulated = true;
  2807. }
  2808. },
  2809. drawRevenueCard: function(){
  2810. let today = new Date();
  2811. let firstOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
  2812. let firstOfLastMonth = new Date(today.getFullYear(), today.getMonth() - 1, 1);
  2813. let lastMonthToDay = new Date(new Date().setMonth(today.getMonth() - 1));
  2814. const revenueThisMonth = merchant.getRevenue(firstOfMonth);
  2815. const revenueLastMonthToDay = merchant.getRevenue(firstOfLastMonth, lastMonthToDay);
  2816. document.getElementById("revenue").innerText = `$${revenueThisMonth.toFixed(2)}`;
  2817. let revenueChange = ((revenueThisMonth - revenueLastMonthToDay) / revenueLastMonthToDay) * 100;
  2818. let img = "";
  2819. if(revenueChange >= 0){
  2820. img = "/shared/images/upArrow.png";
  2821. }else{
  2822. img = "/shared/images/downArrow.png";
  2823. }
  2824. document.querySelector("#revenueChange p").innerText = `${Math.abs(revenueChange).toFixed(2)}% vs last month`;
  2825. document.querySelector("#revenueChange img").src = img;
  2826. },
  2827. drawRevenueGraph: function(){
  2828. let monthAgo = new Date();
  2829. monthAgo.setMonth(monthAgo.getMonth() - 1);
  2830. let revenue = [];
  2831. let dates = [];
  2832. let dayRevenue = 0;
  2833. const transactions = merchant.getTransactions(monthAgo);
  2834. let currentDate = (transactions.length > 0) ? transactions[0].date : undefined;
  2835. for(let i = 0; i < transactions.length; i++){
  2836. if(transactions[i].date.getDate() !== currentDate.getDate()){
  2837. revenue.push(dayRevenue / 100);
  2838. dayRevenue = 0;
  2839. dates.push(currentDate);
  2840. currentDate = transactions[i].date;
  2841. }
  2842. for(let j = 0; j < transactions[i].recipes.length; j++){
  2843. const recipe = transactions[i].recipes[j];
  2844. dayRevenue += recipe.recipe.price * recipe.quantity;
  2845. }
  2846. }
  2847. const trace = {
  2848. x: dates,
  2849. y: revenue,
  2850. mode: "lines+markers",
  2851. line: {
  2852. color: "rgb(255, 99, 107)"
  2853. }
  2854. }
  2855. const layout = {
  2856. title: "REVENUE",
  2857. xaxis: {
  2858. title: "DATE"
  2859. },
  2860. yaxis: {
  2861. title: "$"
  2862. }
  2863. }
  2864. Plotly.newPlot("graphCard", [trace], layout);
  2865. },
  2866. drawInventoryCheckCard: function(){
  2867. let num;
  2868. if(merchant.ingredients.length < 5){
  2869. num = merchant.ingredients.length;
  2870. }else{
  2871. num = 5;
  2872. }
  2873. let rands = [];
  2874. for(let i = 0; i < num; i++){
  2875. let rand = Math.floor(Math.random() * merchant.ingredients.length);
  2876. if(rands.includes(rand)){
  2877. i--;
  2878. }else{
  2879. rands[i] = rand;
  2880. }
  2881. }
  2882. let ul = document.querySelector("#inventoryCheckCard ul");
  2883. let template = document.getElementById("ingredientCheck").content.children[0];
  2884. while(ul.children.length > 0){
  2885. ul.removeChild(ul.firstChild);
  2886. }
  2887. for(let i = 0; i < rands.length; i++){
  2888. let ingredientCheck = template.cloneNode(true);
  2889. let input = ingredientCheck.children[1].children[1];
  2890. const ingredient = merchant.ingredients[rands[i]];
  2891. ingredientCheck.ingredient = ingredient;
  2892. ingredientCheck.children[0].innerText = ingredient.ingredient.name;
  2893. ingredientCheck.children[1].children[0].onclick = ()=>{
  2894. input.value--;
  2895. input.changed = true;
  2896. };
  2897. input.value = ingredient.quantity.toFixed(2);
  2898. ingredientCheck.children[2].innerText = ingredient.ingredient.unit.toUpperCase();
  2899. ingredientCheck.children[1].children[2].onclick = ()=>{
  2900. input.value++;
  2901. input.changed = true;
  2902. }
  2903. input.onchange = ()=>{input.changed = true};
  2904. ul.appendChild(ingredientCheck);
  2905. }
  2906. document.getElementById("inventoryCheck").onclick = ()=>{this.submitInventoryCheck()};
  2907. },
  2908. drawPopularCard: function(){
  2909. let thisMonth = new Date();
  2910. thisMonth.setDate(1);
  2911. const ingredientList = merchant.getIngredientsSold(thisMonth);
  2912. if(ingredientList !== false){
  2913. ingredientList.sort((a, b)=>{
  2914. if(a.quantity < b.quantity){
  2915. return 1;
  2916. }
  2917. if(a.quantity > b.quantity){
  2918. return -1;
  2919. }
  2920. return 0;
  2921. });
  2922. let quantities = [];
  2923. let labels = [];
  2924. let colors = [];
  2925. let count = (ingredientList.length < 5) ? ingredientList.length - 1 : 4;
  2926. for(let i = count; i >= 0; i--){
  2927. const ingredientName = ingredientList[i].ingredient.name;
  2928. const ingredientQuantity = ingredientList[i].quantity;
  2929. const unitName = ingredientList[i].ingredient.unit;
  2930. quantities.push(ingredientList[i].quantity);
  2931. labels.push(`${ingredientName}: ${ingredientQuantity.toFixed(2)} ${unitName.toUpperCase()}`);
  2932. if(i === 0){
  2933. colors.push("rgb(255, 99, 107");
  2934. }else{
  2935. colors.push("rgb(179, 191, 209");
  2936. }
  2937. }
  2938. let trace = {
  2939. x: quantities,
  2940. type: "bar",
  2941. orientation: "h",
  2942. text: labels,
  2943. textposition: "auto",
  2944. hoverinfo: "none",
  2945. marker: {
  2946. color: colors
  2947. }
  2948. }
  2949. let layout = {
  2950. title: "MOST POPULAR INGREDIENTS",
  2951. xaxis: {
  2952. zeroline: false,
  2953. title: "QUANTITY"
  2954. },
  2955. yaxis: {
  2956. showticklabels: false
  2957. }
  2958. }
  2959. if(screen.width < 1000){
  2960. layout.margin = {
  2961. l: 10,
  2962. r: 10,
  2963. t: 100,
  2964. b: 100
  2965. };
  2966. }
  2967. Plotly.newPlot("popularIngredientsCard", [trace], layout);
  2968. }else{
  2969. document.getElementById("popularCanvas").style.display = "none";
  2970. let notice = document.createElement("p");
  2971. notice.innerText = "N/A";
  2972. notice.classList = "notice";
  2973. document.getElementById("popularIngredientsCard").appendChild(notice);
  2974. }
  2975. },
  2976. //Need to change the updating of ingredients
  2977. //should update the ingredient directly, then send that. Maybe...
  2978. submitInventoryCheck: function(){
  2979. let lis = document.querySelectorAll("#inventoryCheckCard li");
  2980. let data = [];
  2981. for(let i = 0; i < lis.length; i++){
  2982. if(lis[i].children[1].children[1].value >= 0){
  2983. if(lis[i].children[1].children[1].changed === true){
  2984. let merchIngredient = lis[i].ingredient;
  2985. data.push({
  2986. id: merchIngredient.ingredient.id,
  2987. quantity: lis[i].children[1].children[1].value
  2988. });
  2989. lis[i].children[1].children[1].changed = false;
  2990. }
  2991. }else{
  2992. controller.createBanner("CANNOT HAVE NEGATIVE INGREDIENTS", "error");
  2993. return;
  2994. }
  2995. }
  2996. if(data.length > 0){
  2997. let loader = document.getElementById("loaderContainer");
  2998. loader.style.display = "flex";
  2999. fetch("/merchant/ingredients/update", {
  3000. method: "PUT",
  3001. headers: {
  3002. "Content-Type": "application/json;charset=utf-8"
  3003. },
  3004. body: JSON.stringify(data)
  3005. })
  3006. .then(response => response.json())
  3007. .then((response)=>{
  3008. if(typeof(response) === "string"){
  3009. controller.createBanner(response, "error");
  3010. }else{
  3011. for(let i = 0; i < response.length; i++){
  3012. merchant.removeIngredient(merchant.getIngredient(response[i].ingredient._id));
  3013. merchant.addIngredient(response[i].ingredient, response[i].quantity, response[i].defaultUnit);
  3014. }
  3015. controller.createBanner("INGREDIENTS UPDATED", "success");
  3016. }
  3017. })
  3018. .catch((err)=>{
  3019. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  3020. })
  3021. .finally(()=>{
  3022. loader.style.display = "none";
  3023. });
  3024. }
  3025. }
  3026. }
  3027. module.exports = home;
  3028. },{}],22:[function(require,module,exports){
  3029. let ingredients = {
  3030. isPopulated: false,
  3031. ingredients: [],
  3032. display: function(){
  3033. if(!this.isPopulated){
  3034. document.getElementById("ingredientSearch").oninput = ()=>{this.search()};
  3035. this.populateByProperty();
  3036. this.isPopulated = true;
  3037. }
  3038. },
  3039. populateByProperty: function(){
  3040. let categories;
  3041. categories = merchant.categorizeIngredients();
  3042. let ingredientStrand = document.getElementById("categoryList");
  3043. let categoryTemplate = document.getElementById("categoryDiv").content.children[0];
  3044. let ingredientTemplate = document.getElementById("ingredient").content.children[0];
  3045. this.ingredients = [];
  3046. while(ingredientStrand.children.length > 0){
  3047. ingredientStrand.removeChild(ingredientStrand.firstChild);
  3048. }
  3049. for(let i = 0; i < categories.length; i++){
  3050. let categoryDiv = categoryTemplate.cloneNode(true);
  3051. categoryDiv.children[0].children[0].innerText = categories[i].name.toUpperCase();
  3052. categoryDiv.children[0].onclick = ()=>{
  3053. this.toggleCategory(categoryDiv.children[1], categoryDiv.children[0].children[1]);
  3054. };
  3055. categoryDiv.children[1].style.display = "none";
  3056. ingredientStrand.appendChild(categoryDiv);
  3057. for(let j = 0; j < categories[i].ingredients.length; j++){
  3058. let ingredient = categories[i].ingredients[j];
  3059. let ingredientDiv = ingredientTemplate.cloneNode(true);
  3060. ingredientDiv.children[0].innerText = ingredient.ingredient.name;
  3061. ingredientDiv.onclick = ()=>{
  3062. controller.openSidebar("ingredientDetails", ingredient);
  3063. ingredientDiv.classList.add("active");
  3064. };
  3065. ingredientDiv._name = ingredient.ingredient.name.toLowerCase();
  3066. ingredientDiv._unit = ingredient.ingredient.unit.toLowerCase();
  3067. ingredientDiv.children[2].innerText = `${ingredient.quantity.toFixed(2)} ${ingredient.ingredient.unit.toUpperCase()}`;
  3068. categoryDiv.children[1].appendChild(ingredientDiv);
  3069. this.ingredients.push(ingredientDiv);
  3070. }
  3071. }
  3072. },
  3073. displayIngredientsOnly: function(ingredients){
  3074. let ingredientDiv = document.getElementById("categoryList");
  3075. while(ingredientDiv.children.length > 0){
  3076. ingredientDiv.removeChild(ingredientDiv.firstChild);
  3077. }
  3078. for(let i = 0; i < ingredients.length; i++){
  3079. ingredientDiv.appendChild(ingredients[i]);
  3080. }
  3081. },
  3082. toggleCategory: function(div, button){
  3083. if(div.style.display === "none"){
  3084. 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>';
  3085. div.style.display = "flex";
  3086. }else if(div.style.display === "flex"){
  3087. 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>';
  3088. div.style.display = "none";
  3089. }
  3090. },
  3091. search: function(){
  3092. let input = document.getElementById("ingredientSearch").value.toLowerCase();
  3093. if(input === ""){
  3094. this.populateByProperty();
  3095. return;
  3096. }
  3097. let matchingIngredients = [];
  3098. for(let i = 0; i < this.ingredients.length; i++){
  3099. if(this.ingredients[i]._name.includes(input)){
  3100. matchingIngredients.push(this.ingredients[i]);
  3101. }
  3102. }
  3103. this.displayIngredientsOnly(matchingIngredients);
  3104. }
  3105. }
  3106. module.exports = ingredients;
  3107. },{}],23:[function(require,module,exports){
  3108. let orders = {
  3109. display: function(){
  3110. document.getElementById("orderFilterBtn").addEventListener("click", ()=>{controller.openSidebar("orderFilter")});
  3111. document.getElementById("newOrderBtn").addEventListener("click", ()=>{controller.openSidebar("newOrder")});
  3112. document.getElementById("orderCalcBtn").addEventListener("click", ()=>{controller.openSidebar("orderCalculator")});
  3113. let orderList = document.getElementById("orderList");
  3114. let template = document.getElementById("order").content.children[0];
  3115. while(orderList.children.length > 0){
  3116. orderList.removeChild(orderList.firstChild);
  3117. }
  3118. for(let i = 0; i < merchant.orders.length; i++){
  3119. let orderDiv = template.cloneNode(true);
  3120. orderDiv.order = merchant.orders[i];
  3121. orderDiv.children[0].innerText = merchant.orders[i].name;
  3122. orderDiv.children[1].innerText = `${merchant.orders[i].ingredients.length} ingredients`;
  3123. orderDiv.children[2].innerText = merchant.orders[i].date.toLocaleDateString("en-US");
  3124. orderDiv.children[3].innerText = `$${merchant.orders[i].getTotalCost().toFixed(2)}`;
  3125. orderDiv.onclick = ()=>{
  3126. controller.openSidebar("orderDetails", merchant.orders[i]);
  3127. orderDiv.classList.add("active");
  3128. }
  3129. orderList.appendChild(orderDiv);
  3130. }
  3131. },
  3132. getOrders: function(Order){
  3133. let loader = document.getElementById("loaderContainer");
  3134. loader.style.display = "flex";
  3135. let to = new Date();
  3136. let from = new Date(to.getFullYear(), to.getMonth(), to.getDate() - 30);
  3137. from.setHours(0, 0, 0, 0);
  3138. let body = {
  3139. to: to.toUTCString(),
  3140. from: from.toUTCString(),
  3141. ingredients: []};
  3142. return fetch("/orders/get", {
  3143. method: "post",
  3144. body: JSON.stringify(body),
  3145. headers: {
  3146. "Content-Type": "application/json;charset=utf-8"
  3147. }
  3148. })
  3149. .then(response => response.json())
  3150. .then((response)=>{
  3151. if(typeof(response) === "string"){
  3152. controller.createBanner(response, "error");
  3153. }else{
  3154. let orders = [];
  3155. for(let i = 0; i < response.length; i++){
  3156. orders.push(new Order(
  3157. response[i]._id,
  3158. response[i].name,
  3159. response[i].date,
  3160. response[i].taxes,
  3161. response[i].fees,
  3162. response[i].ingredients,
  3163. merchant
  3164. ));
  3165. }
  3166. if(merchant.orders.length === 0){
  3167. merchant.setOrders(orders);
  3168. }
  3169. return orders;
  3170. }
  3171. })
  3172. .catch((err)=>{
  3173. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  3174. })
  3175. .finally(()=>{
  3176. loader.style.display = "none";
  3177. });
  3178. }
  3179. }
  3180. module.exports = orders;
  3181. },{}],24:[function(require,module,exports){
  3182. let recipeBook = {
  3183. isPopulated: false,
  3184. recipeDivList: [],
  3185. display: function(Recipe){
  3186. if(!this.isPopulated){
  3187. this.populateRecipes();
  3188. if(merchant.pos !== "none"){
  3189. document.getElementById("posUpdateRecipe").onclick = ()=>{this.posUpdate(Recipe)};
  3190. }
  3191. document.getElementById("recipeSearch").oninput = ()=>{this.search()};
  3192. this.populateRecipes();
  3193. this.isPopulated = true;
  3194. }
  3195. },
  3196. populateRecipes: function(){
  3197. let recipeList = document.getElementById("recipeList");
  3198. let template = document.getElementById("recipe").content.children[0];
  3199. this.recipeDivList = [];
  3200. while(recipeList.children.length > 0){
  3201. recipeList.removeChild(recipeList.firstChild);
  3202. }
  3203. for(let i = 0; i < merchant.recipes.length; i++){
  3204. let recipeDiv = template.cloneNode(true);
  3205. recipeDiv.onclick = ()=>{
  3206. controller.openSidebar("recipeDetails", merchant.recipes[i]);
  3207. recipeDiv.classList.add("active");
  3208. }
  3209. recipeDiv._name = merchant.recipes[i].name;
  3210. recipeList.appendChild(recipeDiv);
  3211. recipeDiv.children[0].innerText = merchant.recipes[i].name;
  3212. recipeDiv.children[1].innerText = `$${merchant.recipes[i].price.toFixed(2)}`;
  3213. this.recipeDivList.push(recipeDiv);
  3214. }
  3215. },
  3216. search: function(){
  3217. let input = document.getElementById("recipeSearch").value.toLowerCase();
  3218. let recipeList = document.getElementById("recipeList");
  3219. let matchingRecipes = [];
  3220. for(let i = 0; i < this.recipeDivList.length; i++){
  3221. if(this.recipeDivList[i]._name.toLowerCase().includes(input)){
  3222. matchingRecipes.push(this.recipeDivList[i]);
  3223. }
  3224. }
  3225. while(recipeList.children.length > 0){
  3226. recipeList.removeChild(recipeList.firstChild);
  3227. }
  3228. for(let i = 0; i < matchingRecipes.length; i++){
  3229. recipeList.appendChild(matchingRecipes[i]);
  3230. }
  3231. },
  3232. posUpdate: function(Recipe){
  3233. let loader = document.getElementById("loaderContainer");
  3234. loader.style.display = "flex";
  3235. let url = `/recipe/update/${merchant.pos}`;
  3236. fetch(url, {
  3237. method: "GET",
  3238. headers: {
  3239. "Content-Type": "application/json;charset=utf-8"
  3240. },
  3241. })
  3242. .then(response => response.json())
  3243. .then((response)=>{
  3244. if(typeof(response) === "string"){
  3245. controller.createBanner(response, "error");
  3246. }else{
  3247. for(let i = 0; i < response.new.length; i++){
  3248. const recipe = new Recipe(
  3249. response.new[i]._id,
  3250. response.new[i].name,
  3251. response.new[i].price,
  3252. merchant,
  3253. []
  3254. );
  3255. merchant.addRecipe(recipe);
  3256. }
  3257. for(let i = 0; i < response.removed.length; i++){
  3258. for(let j = 0; j < merchant.recipes.length; j++){
  3259. if(merchant.recipes[j].id === response.removed[i]._id){
  3260. merchant.removeRecipe(merchant.recipes[j]);
  3261. break;
  3262. }
  3263. }
  3264. }
  3265. controller.createBanner("RECIPES SUCCESSFULLY UPDATED", "success");
  3266. this.display();
  3267. }
  3268. })
  3269. .catch((err)=>{
  3270. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  3271. })
  3272. .finally(()=>{
  3273. loader.style.display = "none";
  3274. });
  3275. }
  3276. }
  3277. module.exports = recipeBook;
  3278. },{}],25:[function(require,module,exports){
  3279. let transactions = {
  3280. transactions: [],
  3281. display: function(){
  3282. document.getElementById("filterTransactionsButton").onclick = ()=>{controller.openSidebar("transactionFilter")};
  3283. document.getElementById("newTransactionButton").onclick = ()=>{controller.openSidebar("newTransaction")};
  3284. this.populateTransactions(this.transactions);
  3285. this.isPopulated = true;
  3286. },
  3287. populateTransactions: function(transactions){
  3288. let transactionsList = document.getElementById("transactionsList");
  3289. let template = document.getElementById("transaction").content.children[0];
  3290. while(transactionsList.children.length > 0){
  3291. transactionsList.removeChild(transactionsList.firstChild);
  3292. }
  3293. let i = 0;
  3294. while(i < transactions.length && i < 100){
  3295. let transactionDiv = template.cloneNode(true);
  3296. let transaction = transactions[i];
  3297. transactionDiv.onclick = ()=>{
  3298. controller.openSidebar("transactionDetails", transaction);
  3299. transactionDiv.classList.add("active");
  3300. }
  3301. transactionsList.appendChild(transactionDiv);
  3302. let totalRecipes = 0;
  3303. let totalPrice = 0;
  3304. for(let j = 0; j < transactions[i].recipes.length; j++){
  3305. totalRecipes += transactions[i].recipes[j].quantity;
  3306. totalPrice += transactions[i].recipes[j].recipe.price * transactions[i].recipes[j].quantity;
  3307. }
  3308. transactionDiv.children[0].innerText = transactions[i].date.toLocaleDateString();
  3309. transactionDiv.children[1].innerText = `${totalRecipes} recipes sold`;
  3310. transactionDiv.children[2].innerText = `$${totalPrice.toFixed(2)}`;
  3311. i++;
  3312. }
  3313. }
  3314. }
  3315. module.exports = transactions;
  3316. },{}]},{},[6]);