bundle.js 106 KB

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