bundle.js 139 KB

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