bundle.js 119 KB

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