bundle.js 117 KB

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