bundle.js 109 KB

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