bundle.js 138 KB

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