bundle.js 113 KB

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