bundle.js 102 KB

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