bundle.js 131 KB

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