bundle.js 121 KB

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