bundle.js 139 KB

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