bundle.js 141 KB

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