bundle.js 141 KB

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