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