bundle.js 112 KB

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