bundle.js 114 KB

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