bundle.js 115 KB

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