bundle.js 142 KB

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