bundle.js 134 KB

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