bundle.js 137 KB

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