bundle.js 141 KB

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