bundle.js 139 KB

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