bundle.js 113 KB

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