bundle.js 118 KB

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