bundle.js 130 KB

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