bundle.js 141 KB

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