bundle.js 130 KB

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