bundle.js 108 KB

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