bundle.js 117 KB

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