bundle.js 108 KB

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