bundle.js 130 KB

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