bundle.js 140 KB

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