bundle.js 130 KB

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