bundle.js 109 KB

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