bundle.js 108 KB

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