bundle.js 131 KB

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