bundle.js 133 KB

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