bundle.js 110 KB

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