bundle.js 110 KB

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