bundle.js 140 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000
  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. addOrder(data, isNew = false){
  417. let order = new this._modules.Order(
  418. data._id,
  419. data.name,
  420. data.date,
  421. data.taxes,
  422. data.fees,
  423. data.ingredients,
  424. this
  425. );
  426. this._orders.push(order);
  427. if(isNew){
  428. for(let i = 0; i < order.ingredients.length; i++){
  429. for(let j = 0; j < this._ingredients.length; j++){
  430. if(order.ingredients[i].ingredient === this._ingredients[j].ingredient){
  431. this._ingredients[j].updateQuantity(order.ingredients[i].quantity);
  432. break;
  433. }
  434. }
  435. }
  436. }
  437. this._modules.ingredients.isPopulated = false;
  438. this._modules.orders.isPopulated = false;
  439. }
  440. setOrders(orders){
  441. this._orders = orders;
  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. merchant.setOrders(data);
  1133. orders.display(Order);
  1134. break;
  1135. case "transactions":
  1136. activeButton = document.getElementById("transactionsBtn");
  1137. document.getElementById("transactionsStrand").style.display = "flex";
  1138. transactions.transactions = data;
  1139. transactions.display(Transaction);
  1140. break;
  1141. }
  1142. activeButton.classList = "menuButton active";
  1143. activeButton.disabled = true;
  1144. if(screen.height > screen.width || screen.width < 1200){
  1145. this.closeMenu();
  1146. }
  1147. },
  1148. /*
  1149. Open a specific sidebar
  1150. Input:
  1151. sidebar: the outermost element of the sidebar (must contain class sidebar)
  1152. */
  1153. openSidebar: function(sidebar, data = {}){
  1154. this.closeSidebar();
  1155. document.getElementById("sidebarDiv").classList = "sidebar";
  1156. document.getElementById(sidebar).style.display = "flex";
  1157. switch(sidebar){
  1158. case "ingredientDetails":
  1159. ingredientDetails.display(data, ingredients);
  1160. break;
  1161. case "newIngredient":
  1162. newIngredient.display(Ingredient);
  1163. break;
  1164. case "editIngredient":
  1165. editIngredient.display(data);
  1166. break;
  1167. case "recipeDetails":
  1168. recipeDetails.display(data);
  1169. break;
  1170. case "editRecipe":
  1171. editRecipe.display(data);
  1172. break;
  1173. case "addRecipe":
  1174. newRecipe.display(Recipe);
  1175. break;
  1176. case "orderDetails":
  1177. orderDetails.display(data);
  1178. break;
  1179. case "orderFilter":
  1180. orderFilter.display(Order);
  1181. break;
  1182. case "newOrder":
  1183. newOrder.display();
  1184. break;
  1185. case "orderCalculator":
  1186. orderCalculator.display();
  1187. break;
  1188. case "transactionDetails":
  1189. transactionDetails.display(data);
  1190. break;
  1191. case "transactionFilter":
  1192. transactionFilter.display(Transaction);
  1193. break;
  1194. case "newTransaction":
  1195. newTransaction.display(Transaction);
  1196. break;
  1197. }
  1198. if(screen.height > screen.width || screen.width < 1200){
  1199. document.querySelector(".contentBlock").style.display = "none";
  1200. document.getElementById("mobileMenuSelector").style.display = "none";
  1201. document.getElementById("sidebarCloser").style.display = "block";
  1202. }
  1203. },
  1204. closeSidebar: function(){
  1205. let sidebar = document.getElementById("sidebarDiv");
  1206. for(let i = 0; i < sidebar.children.length; i++){
  1207. if(sidebar.children[i].style.display !== "none"){
  1208. sidebar.children[i].style.display = "none";
  1209. let choosables = [];
  1210. switch(sidebar.children[i].id){
  1211. case "ingredientDetails":
  1212. choosables = document.querySelectorAll(".ingredient");
  1213. break;
  1214. case "transactionDetails":
  1215. choosables = document.getElementById("transactionsList").children;
  1216. break;
  1217. case "recipeDetails":
  1218. choosables = document.getElementById("recipeList").children;
  1219. break;
  1220. case "orderDetails":
  1221. choosables = document.getElementById("orderList").children;
  1222. break;
  1223. }
  1224. for(let i = 0; i < choosables.length; i++){
  1225. choosables[i].classList.remove("active");
  1226. }
  1227. }
  1228. }
  1229. sidebar.classList = "sidebarHide";
  1230. if(screen.height > screen.width || screen.width < 1200){
  1231. document.querySelector(".contentBlock").style.display = "flex";
  1232. document.getElementById("mobileMenuSelector").style.display = "block";
  1233. document.getElementById("sidebarCloser").style.display = "none";
  1234. }
  1235. },
  1236. openModal: function(str){
  1237. let modal = document.getElementById("modal");
  1238. modal.style.display = "flex";
  1239. document.getElementById("modalClose").addEventListener("click", this.closeModal);
  1240. let content = {};
  1241. switch(str){
  1242. case "ingredientSpreadsheet":
  1243. content = document.getElementById("modalSpreadsheetUpload");
  1244. content.style.display = "flex";
  1245. document.getElementById("modalSpreadsheetTitle").innerText = "ingredients";
  1246. document.getElementById("spreadsheetDownload").href = "/ingredients/download/spreadsheet";
  1247. content.onsubmit = newIngredient.submitSpreadsheet;
  1248. break;
  1249. case "recipeSpreadsheet":
  1250. content = document.getElementById("modalSpreadsheetUpload");
  1251. content.style.display = "flex";
  1252. document.getElementById("modalSpreadsheetTitle").innerText = "recipes";
  1253. document.getElementById("spreadsheetDownload").href = "/recipes/download/spreadsheet";
  1254. content.onsubmit = newRecipe.submitSpreadsheet;
  1255. break;
  1256. case "orderSpreadsheet":
  1257. content = document.getElementById("modalSpreadsheetUpload");
  1258. content.style.display = "flex";
  1259. document.getElementById("modalSpreadsheetTitle").innerText = "orders";
  1260. document.getElementById("spreadsheetDownload").href = "/orders/download/spreadsheet";
  1261. content.onsubmit = newOrder.submitSpreadsheet;
  1262. break;
  1263. case "transactionSpreadsheet":
  1264. content = document.getElementById("modalSpreadsheetUpload");
  1265. content.style.display = "flex";
  1266. document.getElementById("modalSpreadsheetTitle").innerText = "transactions";
  1267. document.getElementById("spreadsheetDownload").href = "/transactions/download/spreadsheet";
  1268. content.onsubmit = newTransaction.submitSpreadsheet;
  1269. }
  1270. },
  1271. closeModal: function(){
  1272. let modal = document.getElementById("modal");
  1273. let modalContent = document.getElementById("modalContent");
  1274. for(let i = 0; i < modalContent.children.length; i++){
  1275. modalContent.children[i].style.display = "none";
  1276. }
  1277. modal.style.display = "none";
  1278. },
  1279. createBanner: function(text, status){
  1280. let container = document.getElementById("bannerContainer");
  1281. let template = document.getElementById("banner").content.children[0];
  1282. let banner = template.cloneNode(true);
  1283. switch(status){
  1284. case "error":
  1285. banner.children[0].style.backgroundColor = "rgb(200, 0, 0)";
  1286. banner.children[0].children[0].style.display = "block";
  1287. break;
  1288. case "alert":
  1289. banner.children[0].style.backgroundColor = "rgb(230, 210, 0)";
  1290. banner.children[0].children[1].style.display = "block";
  1291. break;
  1292. case "success":
  1293. banner.children[0].style.backgroundColor = "rgb(0, 145, 55)";
  1294. banner.children[0].children[2].style.display = "block";
  1295. break;
  1296. }
  1297. banner.children[1].innerText = text;
  1298. container.appendChild(banner);
  1299. let timer = setTimeout(()=>{
  1300. container.removeChild(banner);
  1301. }, 10000);
  1302. banner.children[2].addEventListener("click", ()=>{
  1303. container.removeChild(banner);
  1304. clearTimeout(timer);
  1305. });
  1306. },
  1307. changeMenu: function(){
  1308. let menu = document.querySelector(".menu");
  1309. let buttons = document.querySelectorAll(".menuButton");
  1310. if(!menu.classList.contains("menuMinimized")){
  1311. menu.classList = "menu menuMinimized";
  1312. for(let i = 0; i < buttons.length; i++){
  1313. buttons[i].children[1].style.display = "none";
  1314. }
  1315. document.getElementById("max").style.display = "none";
  1316. document.getElementById("min").style.display = "flex";
  1317. }else if(menu.classList.contains("menuMinimized")){
  1318. menu.classList = "menu";
  1319. for(let i = 0; i < buttons.length; i++){
  1320. buttons[i].children[1].style.display = "block";
  1321. }
  1322. setTimeout(()=>{
  1323. document.getElementById("max").style.display = "flex";
  1324. document.getElementById("min").style.display = "none";
  1325. }, 150);
  1326. }
  1327. },
  1328. openMenu: function(){
  1329. document.getElementById("menu").style.display = "flex";
  1330. document.querySelector(".contentBlock").style.display = "none";
  1331. document.getElementById("mobileMenuSelector").onclick = ()=>{this.closeMenu()};
  1332. },
  1333. closeMenu: function(){
  1334. document.getElementById("menu").style.display = "none";
  1335. document.querySelector(".contentBlock").style.display = "flex";
  1336. document.getElementById("mobileMenuSelector").onclick = ()=>{this.openMenu()};
  1337. },
  1338. updateAnalytics: function(){
  1339. analytics.isPopulated = false;
  1340. }
  1341. }
  1342. //Add click listeners for menu buttons
  1343. document.getElementById("homeBtn").onclick = ()=>{controller.openStrand("home")};
  1344. document.getElementById("ingredientsBtn").onclick = ()=>{controller.openStrand("ingredients")};
  1345. document.getElementById("recipeBookBtn").onclick = ()=>{controller.openStrand("recipeBook")};
  1346. document.getElementById("analyticsBtn").onclick = ()=>{controller.openStrand("analytics")};
  1347. document.getElementById("ordersBtn").onclick = async ()=>{
  1348. if(merchant.orders.length === 0){
  1349. merchant.setOrders(await orders.getOrders(Order));
  1350. }
  1351. controller.openStrand("orders", merchant.orders);
  1352. }
  1353. document.getElementById("transactionsBtn").onclick = ()=>{controller.openStrand("transactions", merchant.getTransactions())};
  1354. controller.openStrand("home");
  1355. },{"./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){
  1356. let editIngredient = {
  1357. display: function(ingredient){
  1358. let buttonList = document.getElementById("unitButtons");
  1359. let quantLabel = document.getElementById("editIngQuantityLabel");
  1360. let specialLabel = document.getElementById("editSpecialLabel");
  1361. //Clear any existing data
  1362. while(buttonList.children.length > 0){
  1363. buttonList.removeChild(buttonList.firstChild);
  1364. }
  1365. //Populate basic fields
  1366. document.getElementById("editIngTitle").innerText = ingredient.ingredient.name;
  1367. document.getElementById("editIngName").value = ingredient.ingredient.name;
  1368. document.getElementById("editIngCategory").value = ingredient.ingredient.category;
  1369. quantLabel.innerText = `CURRENT STOCK (${ingredient.ingredient.unit.toUpperCase()})`;
  1370. document.getElementById("editIngSubmit").onclick = ()=>{this.submit(ingredient)};
  1371. //Make any changes for special ingredients
  1372. if(ingredient.ingredient.unit === "bottle"){
  1373. // quantLabel.innerText = "CURRENT STOCK (BOTTLES):";
  1374. specialLabel.style.display = "flex";
  1375. specialLabel.innerText = `BOTTLE SIZE (${ingredient.ingredient.unitType.toUpperCase()}):`;
  1376. let sizeInput = document.createElement("input");
  1377. sizeInput.id = "editIngSpecialSize";
  1378. sizeInput.type = "number";
  1379. sizeInput.min = "0";
  1380. sizeInput.step = "0.01";
  1381. sizeInput.value = ingredient.ingredient.unitSize.toFixed(2);
  1382. specialLabel.appendChild(sizeInput);
  1383. }else{
  1384. specialLabel.style.display = "none";
  1385. }
  1386. //Populate the unit buttons
  1387. const units = ingredient.ingredient.getPotentialUnits();
  1388. for(let i = 0; i < units.length; i++){
  1389. let button = document.createElement("button");
  1390. button.classList.add("unitButton");
  1391. button.innerText = units[i].toUpperCase();
  1392. button.onclick = ()=>{this.changeUnit(button)};
  1393. buttonList.appendChild(button);
  1394. if(units[i] === ingredient.ingredient.unitType){
  1395. button.classList.add("unitActive");
  1396. }
  1397. }
  1398. let quantInput = document.createElement("input");
  1399. quantInput.id = "editIngQuantity";
  1400. quantInput.type = "number";
  1401. quantInput.min = "0";
  1402. quantInput.step = "0.01";
  1403. quantInput.value = ingredient.quantity.toFixed(2);
  1404. quantLabel.appendChild(quantInput);
  1405. },
  1406. changeUnit(button){
  1407. let buttons = document.getElementById("unitButtons");
  1408. for(let i = 0; i < buttons.children.length; i++){
  1409. buttons.children[i].classList.remove("unitActive");
  1410. }
  1411. button.classList.add("unitActive");
  1412. },
  1413. submit(ingredient){
  1414. const quantity = parseFloat(document.getElementById("editIngQuantityLabel").children[0].value);
  1415. let data = {
  1416. id: ingredient.ingredient.id,
  1417. name: document.getElementById("editIngName").value,
  1418. category: document.getElementById("editIngCategory").value
  1419. }
  1420. data.quantity = quantity;
  1421. //Get the measurement unit
  1422. let units = document.getElementById("unitButtons");
  1423. for(let i = 0; i < units.children.length; i++){
  1424. if(units.children[i].classList.contains("unitActive")){
  1425. data.unit = units.children[i].innerText.toLowerCase();
  1426. break;
  1427. }
  1428. }
  1429. let loader = document.getElementById("loaderContainer");
  1430. loader.style.display = "flex";
  1431. fetch("/ingredients/update", {
  1432. method: "put",
  1433. headers: {
  1434. "Content-Type": "application/json;charset=utf-8"
  1435. },
  1436. body: JSON.stringify(data)
  1437. })
  1438. .then(response => response.json())
  1439. .then((response)=>{
  1440. if(typeof(response) === "string"){
  1441. controller.createBanner(response, "error");
  1442. }else{
  1443. merchant.removeIngredient(merchant.getIngredient(response.ingredient._id));
  1444. merchant.addIngredient(response.ingredient, response.quantity, response.unit);
  1445. controller.openStrand("ingredients");
  1446. controller.createBanner("INGREDIENT UPDATED", "success");
  1447. }
  1448. })
  1449. .catch((err)=>{
  1450. controller.createBanner("SOMETHING WENT WRONG, PLEASE REFRESH THE PAGE", "error");
  1451. })
  1452. .finally(()=>{
  1453. loader.style.display = "none";
  1454. });
  1455. }
  1456. }
  1457. module.exports = editIngredient;
  1458. },{}],8:[function(require,module,exports){
  1459. let editRecipe = {
  1460. display: function(recipe){
  1461. let nameInput = document.getElementById("editRecipeName");
  1462. if(merchant.pos === "none"){
  1463. nameInput.value = recipe.name;
  1464. }else{
  1465. document.getElementById("editRecipeNoName").innertext = recipe.name;
  1466. nameInput.parentNode.style.display = "none";
  1467. }
  1468. //Populate ingredients
  1469. let ingredientList = document.getElementById("editRecipeIngList");
  1470. while(ingredientList.children.length > 0){
  1471. ingredientList.removeChild(ingredientList.firstChild);
  1472. }
  1473. let template = document.getElementById("editRecipeIng").content.children[0];
  1474. for(let i = 0; i < recipe.ingredients.length; i++){
  1475. let ingredientDiv = template.cloneNode(true);
  1476. ingredientDiv.children[0].onclick = ()=>{ingredientDiv.parentNode.removeChild(ingredientDiv)};
  1477. ingredientDiv.children[1].innerText = recipe.ingredients[i].ingredient.getNameAndUnit();
  1478. ingredientDiv.children[2].style.display = "none";
  1479. ingredientDiv.children[3].value = recipe.ingredients[i].quantity;
  1480. ingredientDiv.ingredient = recipe.ingredients[i];
  1481. ingredientList.appendChild(ingredientDiv);
  1482. }
  1483. document.getElementById("addRecIng").onclick = ()=>{this.newIngredient()};
  1484. document.getElementById("editRecipePrice").value = recipe.price;
  1485. document.getElementById("editRecipeSubmit").onclick = ()=>{this.submit(recipe)};
  1486. document.getElementById("editRecipeCancel").onclick = ()=>{controller.openSidebar("recipeDetails", recipe)};
  1487. },
  1488. newIngredient: function(){
  1489. let ingredientList = document.getElementById("editRecipeIngList");
  1490. let ingredientDiv = document.getElementById("editRecipeIng").content.children[0].cloneNode(true);
  1491. ingredientDiv.children[0].onclick = ()=>{ingredientDiv.parentNode.removeChild(ingredientDiv)};
  1492. ingredientDiv.children[1].style.display = "none";
  1493. ingredientDiv.children[3].value = "0.00";
  1494. //Populate selector
  1495. let categories = merchant.categorizeIngredients();
  1496. for(let i = 0; i < categories.length; i++){
  1497. let group = document.createElement("optgroup");
  1498. group.label = categories[i].name;
  1499. for(let j = 0; j < categories[i].ingredients.length; j++){
  1500. let option = document.createElement("option");
  1501. option.innerText = categories[i].ingredients[j].ingredient.getNameAndUnit();
  1502. option.ingredient = categories[i].ingredients[j];
  1503. group.appendChild(option);
  1504. }
  1505. ingredientDiv.children[2].appendChild(group);
  1506. }
  1507. ingredientList.appendChild(ingredientDiv);
  1508. },
  1509. submit: function(recipe){
  1510. let data = {
  1511. id: recipe.id,
  1512. name: recipe.name,
  1513. price: document.getElementById("editRecipePrice").value * 100,
  1514. ingredients: []
  1515. }
  1516. if(merchant.pos === "none"){
  1517. data.name = document.getElementById("editRecipeName").value;
  1518. }
  1519. let ingredients = document.getElementById("editRecipeIngList").children;
  1520. for(let i = 0; i < ingredients.length; i++){
  1521. const quantity = parseFloat(ingredients[i].children[3].value);
  1522. let newIngredient = {};
  1523. let ingredient = {};
  1524. if(ingredients[i].children[1].style.display === "none"){
  1525. let selector = ingredients[i].children[2];
  1526. ingredient = selector.options[selector.selectedIndex].ingredient;
  1527. newIngredient = {
  1528. ingredient: ingredient.ingredient.id,
  1529. quantity: ingredient.convertToBase(quantity)
  1530. };
  1531. }else{
  1532. ingredient = ingredients[i].ingredient;
  1533. newIngredient = {
  1534. ingredient: ingredient.ingredient.id,
  1535. quantity: ingredients[i].ingredient.convertToBase(quantity)
  1536. };
  1537. }
  1538. data.ingredients.push(newIngredient);
  1539. }
  1540. let loader = document.getElementById("loaderContainer");
  1541. loader.style.display = "flex";
  1542. fetch("/recipe/update", {
  1543. method: "put",
  1544. headers: {
  1545. "Content-Type": "application/json;charset=utf-8"
  1546. },
  1547. body: JSON.stringify(data)
  1548. })
  1549. .then(response => response.json())
  1550. .then((response)=>{
  1551. if(typeof(response) === "string"){
  1552. controller.createBanner(response, "error");
  1553. }else{
  1554. merchant.updateRecipe(response);
  1555. controller.openStrand("recipeBook");
  1556. controller.createBanner("RECIPE UPDATED", "success");
  1557. }
  1558. })
  1559. .catch((err)=>{
  1560. controller.createBanner("SOMETHING WENT WRONG, PLEASE REFRESH THE PAGE", "error");
  1561. })
  1562. .finally(()=>{
  1563. loader.style.display = "none";
  1564. });
  1565. }
  1566. }
  1567. module.exports = editRecipe;
  1568. },{}],9:[function(require,module,exports){
  1569. let ingredientDetails = {
  1570. dailyUse: 0,
  1571. display: function(ingredient){
  1572. document.getElementById("editIngBtn").onclick = ()=>{controller.openSidebar("editIngredient", ingredient)};
  1573. document.getElementById("removeIngBtn").onclick = ()=>{this.remove(ingredient)};
  1574. document.getElementById("ingredientDetailsCategory").innerText = ingredient.ingredient.category;
  1575. document.getElementById("ingredientDetailsName").innerText = ingredient.ingredient.name;
  1576. document.getElementById("ingredientStock").innerText = ingredient.getQuantityDisplay();
  1577. //Calculate and display average daily use
  1578. let quantities = [];
  1579. let now = new Date();
  1580. for(let i = 1; i < 31; i++){
  1581. let endDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i)
  1582. let startDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i - 1);
  1583. quantities.push(merchant.getSingleIngredientSold(ingredient, startDay, endDay));
  1584. }
  1585. let sum = 0;
  1586. for(let i = 0; i < quantities.length; i++){
  1587. sum += quantities[i];
  1588. }
  1589. let dailyUse = sum / quantities.length;
  1590. const dailyUseDiv = document.getElementById("dailyUse");
  1591. dailyUseDiv.innerText = `${dailyUse.toFixed(2)} ${ingredient.ingredient.unit.toUpperCase()}`;
  1592. //Show recipes that this ingredient is a part of
  1593. let recipeList = document.getElementById("ingredientRecipeList");
  1594. let template = document.getElementById("ingredientRecipe").content.children[0];
  1595. let recipes = merchant.getRecipesForIngredient(ingredient.ingredient);
  1596. while(recipeList.children.length > 0){
  1597. recipeList.removeChild(recipeList.firstChild);
  1598. }
  1599. for(let i = 0; i < recipes.length; i++){
  1600. let recipeDiv = template.cloneNode(true);
  1601. recipeDiv.children[0].innerText = recipes[i].name;
  1602. recipeDiv.onclick = ()=>{
  1603. controller.openStrand("recipeBook");
  1604. controller.openSidebar("recipeDetails", recipes[i]);
  1605. }
  1606. recipeDiv.classList.add("choosable");
  1607. recipeList.appendChild(recipeDiv);
  1608. }
  1609. },
  1610. remove: function(ingredient){
  1611. for(let i = 0; i < merchant.recipes.length; i++){
  1612. for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
  1613. if(ingredient.ingredient === merchant.recipes[i].ingredients[j].ingredient){
  1614. controller.createBanner("MUST REMOVE INGREDIENT FROM ALL RECIPES BEFORE REMOVING FROM INVENTORY", "error");
  1615. return;
  1616. }
  1617. }
  1618. }
  1619. let loader = document.getElementById("loaderContainer");
  1620. loader.style.display = "flex";
  1621. fetch(`/ingredients/remove/${ingredient.ingredient.id}`, {
  1622. method: "delete",
  1623. })
  1624. .then((response) => response.json())
  1625. .then((response)=>{
  1626. if(typeof(response) === "string"){
  1627. controller.createBanner(response, "error");
  1628. }else{
  1629. merchant.removeIngredient(ingredient);
  1630. controller.openStrand("ingredients");
  1631. controller.createBanner("INGREDIENT REMOVED", "success");
  1632. }
  1633. })
  1634. .catch((err)=>{
  1635. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  1636. })
  1637. .finally(()=>{
  1638. loader.style.display = "none";
  1639. });
  1640. }
  1641. }
  1642. module.exports = ingredientDetails;
  1643. },{}],10:[function(require,module,exports){
  1644. let newIngredient = {
  1645. display: function(Ingredient){
  1646. const selector = document.getElementById("unitSelector");
  1647. document.getElementById("newIngName").value = "";
  1648. document.getElementById("newIngCategory").value = "";
  1649. document.getElementById("newIngQuantity").value = 0;
  1650. document.getElementById("bottleSizeLabel").style.display = "none";
  1651. selector.value = "g";
  1652. selector.onchange = ()=>{this.unitChange()};
  1653. document.getElementById("submitNewIng").onclick = ()=>{this.submit(Ingredient)};
  1654. document.getElementById("ingredientFileUpload").addEventListener("click", ()=>{controller.openModal("ingredientSpreadsheet")});
  1655. },
  1656. unitChange: function(){
  1657. const select = document.getElementById("unitSelector");
  1658. const bottleLabel = document.getElementById("bottleSizeLabel");
  1659. if(select.value === "bottle"){
  1660. bottleLabel.style.display = "block";
  1661. }else{
  1662. bottleLabel.style.display = "none";
  1663. }
  1664. },
  1665. submit: function(){
  1666. let unitSelector = document.getElementById("unitSelector");
  1667. let options = document.querySelectorAll("#unitSelector option");
  1668. const quantityValue = parseFloat(document.getElementById("newIngQuantity").value);
  1669. let unit = unitSelector.value;
  1670. let newIngredient = {
  1671. ingredient: {
  1672. name: document.getElementById("newIngName").value,
  1673. category: document.getElementById("newIngCategory").value,
  1674. unitType: options[unitSelector.selectedIndex].getAttribute("type")
  1675. },
  1676. quantity: quantityValue,
  1677. defaultUnit: unit
  1678. }
  1679. //Change the ingredient if it is a special unit type (ie "bottle")
  1680. if(unit === "bottle"){
  1681. newIngredient.ingredient.unitType = document.getElementById("bottleUnits").value;
  1682. newIngredient.ingredient.unitSize = parseFloat(document.getElementById("bottleSize").value);
  1683. }
  1684. let loader = document.getElementById("loaderContainer");
  1685. loader.style.display = "flex";
  1686. fetch("/ingredients/create", {
  1687. method: "POST",
  1688. headers: {
  1689. "Content-Type": "application/json;charset=utf-8"
  1690. },
  1691. body: JSON.stringify(newIngredient)
  1692. })
  1693. .then((response) => response.json())
  1694. .then((response)=>{
  1695. if(typeof(response) === "string"){
  1696. controller.createBanner(response, "error");
  1697. }else{
  1698. merchant.addIngredient(response.ingredient, response.quantity, response.defaultUnit);
  1699. controller.openStrand("ingredients");
  1700. controller.createBanner("INGREDIENT CREATED", "success");
  1701. }
  1702. })
  1703. .catch((err)=>{
  1704. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  1705. })
  1706. .finally(()=>{
  1707. loader.style.display = "none";
  1708. });
  1709. },
  1710. submitSpreadsheet: function(){
  1711. event.preventDefault();
  1712. controller.closeModal();
  1713. const file = document.getElementById("spreadsheetInput").files[0];
  1714. let data = new FormData();
  1715. data.append("ingredients", file);
  1716. let loader = document.getElementById("loaderContainer");
  1717. loader.style.display = "flex";
  1718. fetch("/ingredients/create/spreadsheet", {
  1719. method: "post",
  1720. body: data
  1721. })
  1722. .then(response => response.json())
  1723. .then((response)=>{
  1724. if(typeof(response) === "string"){
  1725. controller.createBanner(response, "error");
  1726. }else{
  1727. for(let i = 0; i < response.length; i++){
  1728. merchant.addIngredient(response[i].ingredient, response[i].quantity, response[i].defaultUnit);
  1729. }
  1730. controller.createBanner("INGREDIENTS SUCCESSFULLY ADDED", "success");
  1731. controller.openStrand("ingredients");
  1732. }
  1733. })
  1734. .catch((err)=>{
  1735. controller.createBanner("SOMETHING WENT WRONG. TRY REFRESHING THE PAGE", "error");
  1736. })
  1737. .finally(()=>{
  1738. loader.style.display = "none";
  1739. });
  1740. }
  1741. }
  1742. module.exports = newIngredient;
  1743. },{}],11:[function(require,module,exports){
  1744. let newOrder = {
  1745. display: function(){
  1746. document.getElementById("sidebarDiv").classList.add("sidebarWide");
  1747. document.getElementById("newOrderIngredientList").style.display = "flex";
  1748. document.getElementById("orderFileUpload").addEventListener("click", ()=>{controller.openModal("orderSpreadsheet")});
  1749. let selectedList = document.getElementById("selectedIngredientList");
  1750. while(selectedList.children.length > 0){
  1751. selectedList.removeChild(selectedList.firstChild);
  1752. }
  1753. let ingredientList = document.getElementById("newOrderIngredients");
  1754. while(ingredientList.children.length > 0){
  1755. ingredientList.removeChild(ingredientList.firstChild);
  1756. }
  1757. for(let i = 0; i < merchant.ingredients.length; i++){
  1758. let ingredient = document.createElement("button");
  1759. ingredient.classList = "choosable";
  1760. ingredient.innerText = merchant.ingredients[i].ingredient.name;
  1761. ingredient.onclick = ()=>{this.addIngredient(merchant.ingredients[i], ingredient)};
  1762. ingredientList.appendChild(ingredient);
  1763. }
  1764. document.getElementById("submitNewOrder").onclick = ()=>{this.submit()};
  1765. },
  1766. addIngredient: function(ingredient, element){
  1767. element.style.display = "none";
  1768. let div = document.getElementById("selectedIngredient").content.children[0].cloneNode(true);
  1769. div.ingredient = ingredient;
  1770. div.children[0].children[1].onclick = ()=>{this.removeIngredient(div, element)};
  1771. div.children[0].children[0].innerText = `${ingredient.ingredient.name} (${ingredient.ingredient.unit.toUpperCase()})`;
  1772. document.getElementById("selectedIngredientList").appendChild(div);
  1773. },
  1774. removeIngredient: function(selectedElement, element){
  1775. selectedElement.parentElement.removeChild(selectedElement);
  1776. element.style.display = "block";
  1777. },
  1778. submit: function(){
  1779. let date = document.getElementById("newOrderDate").valueAsDate;
  1780. let taxes = document.getElementById("orderTaxes").value * 100;
  1781. let fees = document.getElementById("orderFees").value * 100;
  1782. let ingredients = document.getElementById("selectedIngredientList").children;
  1783. if(date === null){
  1784. controller.createBanner("DATE IS REQUIRED FOR ORDERS", "error");
  1785. return;
  1786. }
  1787. date.setHours(0, 0, 0, 0);
  1788. let data = {
  1789. name: document.getElementById("newOrderName").value,
  1790. date: date,
  1791. taxes: taxes,
  1792. fees: fees,
  1793. ingredients: []
  1794. }
  1795. for(let i = 0; i < ingredients.length; i++){
  1796. let quantity = ingredients[i].children[1].children[0].value;
  1797. let price = ingredients[i].children[1].children[1].value;
  1798. data.ingredients.push({
  1799. ingredient: ingredients[i].ingredient.ingredient.id,
  1800. quantity: ingredients[i].ingredient.convertToBase(quantity),
  1801. pricePerUnit: this.convertPrice(ingredients[i].ingredient.ingredient, price * 100)
  1802. });
  1803. }
  1804. let loader = document.getElementById("loaderContainer");
  1805. loader.style.display = "flex";
  1806. fetch("/order/create", {
  1807. method: "post",
  1808. headers: {
  1809. "Content-Type": "application/json;charset=utf-8"
  1810. },
  1811. body: JSON.stringify(data)
  1812. })
  1813. .then((response)=>response.json())
  1814. .then((response)=>{
  1815. if(typeof(response) === "string"){
  1816. controller.createBanner(response, "error");
  1817. }else{
  1818. merchant.addOrder(response, true);
  1819. controller.openStrand("orders", merchant.orders);
  1820. controller.createBanner("NEW ORDER CREATED", "success");
  1821. }
  1822. })
  1823. .catch((err)=>{
  1824. controller.createBanner("SOMETHING WENT WRONG, PLEASE REFRESH THE PAGE", "error");
  1825. })
  1826. .finally(()=>{
  1827. loader.style.display = "none";
  1828. });
  1829. },
  1830. convertPrice: function(ingredient, price){
  1831. switch(ingredient.unit){
  1832. case "g": return price;
  1833. case "kg": return price / 1000;
  1834. case "oz": return price / 28.3495;
  1835. case "lb": return price / 453.5924;
  1836. case "ml": return price * 1000;
  1837. case "l": return price;
  1838. case "tsp": return price * 202.8842;
  1839. case "tbsp": return price * 67.6278;
  1840. case "ozfl": return price * 33.8141;
  1841. case "cup": return price * 4.1667;
  1842. case "pt": return price * 2.1134;
  1843. case "qt": return price * 1.0567;
  1844. case "gal": return price / 3.7854;
  1845. case "mm": return price * 1000;
  1846. case "cm": return price * 100;
  1847. case "m": return price;
  1848. case "in": return price * 39.3701;
  1849. case "ft": return price * 3.2808;
  1850. default: return price;
  1851. }
  1852. },
  1853. submitSpreadsheet: function(){
  1854. event.preventDefault();
  1855. controller.closeModal();
  1856. const file = document.getElementById("spreadsheetInput").files[0];
  1857. let data = new FormData();
  1858. data.append("orders", file);
  1859. data.append("timeOffset", new Date().getTimezoneOffset());
  1860. let loader = document.getElementById("loaderContainer");
  1861. loader.style.display = "flex";
  1862. fetch("/orders/create/spreadsheet", {
  1863. method: "post",
  1864. body: data
  1865. })
  1866. .then(response => response.json())
  1867. .then((response)=>{
  1868. if(typeof(response) === "string"){
  1869. controller.createBanner(response, "error");
  1870. }else{
  1871. merchant.addOrder(response, true);
  1872. controller.createBanner("ORDER CREATED AND INGREDIENTS UPDATED SUCCESSFULLY", "success");
  1873. controller.openStrand("orders");
  1874. }
  1875. })
  1876. .catch((err)=>{
  1877. controller.createBanner("UNABLE TO DISPLAY NEW ORDER. PLEASE REFRESH THE PAGE.", "error");
  1878. })
  1879. .finally(()=>{
  1880. loader.style.display = "none";
  1881. });
  1882. }
  1883. }
  1884. module.exports = newOrder;
  1885. },{}],12:[function(require,module,exports){
  1886. let newRecipe = {
  1887. display: function(Recipe){
  1888. document.getElementById("newRecipeName").value = "";
  1889. document.getElementById("newRecipePrice").value = "";
  1890. document.getElementById("ingredientCount").value = 1;
  1891. let categories = merchant.categorizeIngredients();
  1892. let ingredientsSelect = document.getElementById("recipeInputIngredients");
  1893. while(ingredientsSelect.children.length > 0){
  1894. ingredientsSelect.removeChild(ingredientsSelect.firstChild);
  1895. }
  1896. this.changeIngredientCount(categories);
  1897. document.getElementById("ingredientCount").onchange = ()=>{this.changeIngredientCount(categories)};
  1898. document.getElementById("submitNewRecipe").onclick = ()=>{this.submit(Recipe)};
  1899. document.getElementById("recipeFileUpload").onclick = ()=>{controller.openModal("recipeSpreadsheet")};
  1900. },
  1901. //Updates the number of ingredient inputs displayed for new recipes
  1902. changeIngredientCount: function(categories){
  1903. let newCount = document.getElementById("ingredientCount").value;
  1904. let ingredientsDiv = document.getElementById("recipeInputIngredients");
  1905. let template = document.getElementById("recipeInputIngredient").content.children[0];
  1906. let oldCount = ingredientsDiv.children.length;
  1907. if(newCount > oldCount){
  1908. let newDivs = newCount - oldCount;
  1909. for(let i = 0; i < newDivs; i++){
  1910. let newNode = template.cloneNode(true);
  1911. newNode.children[0].innnerText = `INGREDIENT ${i + oldCount}`;
  1912. newNode.children[2].children[0].value = 0;
  1913. for(let j = 0; j < categories.length; j++){
  1914. let optgroup = document.createElement("optgroup");
  1915. optgroup.label = categories[j].name;
  1916. for(let k = 0; k < categories[j].ingredients.length; k++){
  1917. let option = document.createElement("option");
  1918. option.innerText = categories[j].ingredients[k].ingredient.getNameAndUnit();
  1919. option.ingredient = categories[j].ingredients[k];
  1920. optgroup.appendChild(option);
  1921. }
  1922. newNode.children[1].children[0].appendChild(optgroup);
  1923. }
  1924. ingredientsDiv.appendChild(newNode);
  1925. }
  1926. for(let i = 0; i < newCount; i++){
  1927. ingredientsDiv.children[i].children[0].innerText = `INGREDIENT ${i + 1}`;
  1928. }
  1929. }else if(newCount < oldCount){
  1930. let newDivs = oldCount - newCount;
  1931. for(let i = 0; i < newDivs; i++){
  1932. ingredientsDiv.removeChild(ingredientsDiv.children[ingredientsDiv.children.length-1]);
  1933. }
  1934. }
  1935. },
  1936. submit: function(Recipe){
  1937. let newRecipe = {
  1938. name: document.getElementById("newRecipeName").value,
  1939. price: document.getElementById("newRecipePrice").value,
  1940. ingredients: []
  1941. }
  1942. let inputs = document.getElementById("recipeInputIngredients").children;
  1943. for(let i = 0; i < inputs.length; i++){
  1944. let sel = inputs[i].children[1].children[0];
  1945. let ingredient = sel.options[sel.selectedIndex].ingredient;
  1946. let newIngredient = {
  1947. ingredient: ingredient.ingredient.id,
  1948. quantity: ingredient.convertToBase(inputs[i].children[2].children[0].value)
  1949. };
  1950. newRecipe.ingredients.push(newIngredient);
  1951. }
  1952. let loader = document.getElementById("loaderContainer");
  1953. loader.style.display = "flex";
  1954. fetch("/recipe/create", {
  1955. method: "POST",
  1956. headers: {
  1957. "Content-Type": "application/json;charset=utf-8"
  1958. },
  1959. body: JSON.stringify(newRecipe)
  1960. })
  1961. .then((response) => response.json())
  1962. .then((response)=>{
  1963. if(typeof(response) === "string"){
  1964. controller.createBanner(response, "error");
  1965. }else{
  1966. let ingredients = [];
  1967. for(let i = 0; i < response.ingredients.length; i++){
  1968. for(let j = 0; j < merchant.ingredients.length; j++){
  1969. if(merchant.ingredients[j].ingredient.id === response.ingredients[i].ingredient){
  1970. ingredients.push({
  1971. ingredient: merchant.ingredients[j].ingredient.id,
  1972. quantity: response.ingredients[i].quantity
  1973. });
  1974. break;
  1975. }
  1976. }
  1977. }
  1978. merchant.addRecipe(
  1979. response._id,
  1980. response.name,
  1981. response.price,
  1982. ingredients
  1983. );
  1984. controller.createBanner("RECIPE CREATED", "success");
  1985. controller.openStrand("recipeBook");
  1986. }
  1987. })
  1988. .catch((err)=>{
  1989. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  1990. })
  1991. .finally(()=>{
  1992. loader.style.display = "none";
  1993. });
  1994. },
  1995. submitSpreadsheet: function(){
  1996. event.preventDefault();
  1997. controller.closeModal();
  1998. const file = document.getElementById("spreadsheetInput").files[0];
  1999. let data = new FormData();
  2000. data.append("recipes", file);
  2001. let loader = document.getElementById("loaderContainer");
  2002. loader.style.display = "flex";
  2003. fetch("/recipes/create/spreadsheet", {
  2004. method: "post",
  2005. body: data
  2006. })
  2007. .then(response => response.json())
  2008. .then((response)=>{
  2009. if(typeof(response) === "string"){
  2010. controller.createBanner(response, "error");
  2011. }else{
  2012. for(let i = 0; i < response.length; i++){
  2013. merchant.addRecipe(
  2014. response[i]._id,
  2015. response[i].name,
  2016. response[i].price,
  2017. response[i].ingredients
  2018. );
  2019. }
  2020. controller.createBanner("ALL INGREDIENTS SUCCESSFULLY CREATED", "success");
  2021. controller.openStrand("recipeBook");
  2022. }
  2023. })
  2024. .catch((err)=>{
  2025. controller.createBanner("UNABLE TO DISPLAY NEW RECIPES. PLEASE REFRESH THE PAGE", "error");
  2026. })
  2027. .finally(()=>{
  2028. loader.style.display = "none";
  2029. });
  2030. }
  2031. }
  2032. module.exports = newRecipe;
  2033. },{}],13:[function(require,module,exports){
  2034. let newTransaction = {
  2035. display: function(){
  2036. let recipeList = document.getElementById("newTransactionRecipes");
  2037. let template = document.getElementById("createTransaction").content.children[0];
  2038. document.getElementById("transactionFileUpload").addEventListener("click", ()=>{controller.openModal("transactionSpreadsheet")});
  2039. while(recipeList.children.length > 0){
  2040. recipeList.removeChild(recipeList.firstChild);
  2041. }
  2042. for(let i = 0; i < merchant.recipes.length; i++){
  2043. let recipeDiv = template.cloneNode(true);
  2044. recipeDiv.recipe = merchant.recipes[i];
  2045. recipeList.appendChild(recipeDiv);
  2046. recipeDiv.children[0].innerText = merchant.recipes[i].name;
  2047. }
  2048. document.getElementById("submitNewTransaction").onclick = ()=>{this.submit()};
  2049. },
  2050. submit: function(){
  2051. let recipeDivs = document.getElementById("newTransactionRecipes");
  2052. let date = document.getElementById("newTransactionDate").valueAsDate;
  2053. if(date === null){
  2054. controller.createBanner("DATE IS REQUIRED FOR TRANSACTIONS", "error");
  2055. return;
  2056. }
  2057. date.setHours(0, 0, 0, 0);
  2058. let data = {
  2059. date: date,
  2060. recipes: [],
  2061. ingredientUpdates: {}
  2062. };
  2063. for(let i = 0; i < recipeDivs.children.length; i++){
  2064. let quantity = recipeDivs.children[i].children[1].value;
  2065. const recipe = recipeDivs.children[i].recipe;
  2066. if(quantity !== "" && quantity > 0){
  2067. data.recipes.push({
  2068. recipe: recipe.id,
  2069. quantity: quantity
  2070. });
  2071. for(let j = 0; j < recipe.ingredients.length; j++){
  2072. let ingredient = recipe.ingredients[j];
  2073. if(data.ingredientUpdates[ingredient.ingredient.id]){
  2074. data.ingredientUpdates[ingredient.ingredient.id] += ingredient.convertToBase(ingredient.quantity) * quantity;
  2075. }else{
  2076. data.ingredientUpdates[ingredient.ingredient.id] = ingredient.convertToBase(ingredient.quantity) * quantity;
  2077. }
  2078. }
  2079. }else if(quantity < 0){
  2080. controller.createBanner("CANNOT HAVE NEGATIVE VALUES", "error");
  2081. return;
  2082. }
  2083. }
  2084. if(data.recipes.length > 0){
  2085. let loader = document.getElementById("loaderContainer");
  2086. loader.style.display = "flex";
  2087. fetch("/transaction/create", {
  2088. method: "post",
  2089. headers: {
  2090. "Content-Type": "application/json;charset=utf-8"
  2091. },
  2092. body: JSON.stringify(data)
  2093. })
  2094. .then(response => response.json())
  2095. .then((response)=>{
  2096. if(typeof(response) === "string"){
  2097. controller.createBanner(response, "error");
  2098. }else{
  2099. merchant.addTransaction(response);
  2100. controller.updateAnalytics();
  2101. controller.openStrand("transactions", merchant.getTransactions());
  2102. controller.createBanner("TRANSACTION CREATED", "success");
  2103. }
  2104. })
  2105. .catch((err)=>{
  2106. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  2107. })
  2108. .finally(()=>{
  2109. loader.style.display = "none";
  2110. });
  2111. }
  2112. },
  2113. submitSpreadsheet: function(){
  2114. event.preventDefault();
  2115. controller.closeModal();
  2116. const file = document.getElementById("spreadsheetInput").files[0];
  2117. let data = new FormData();
  2118. data.append("transactions", file);
  2119. data.append("timeOffset", new Date().getTimezoneOffset());
  2120. let loader = document.getElementById("loaderContainer");
  2121. loader.style.display = "flex";
  2122. fetch("/transactions/create/spreadsheet", {
  2123. method: "post",
  2124. body: data
  2125. })
  2126. .then(response => response.json())
  2127. .then((response)=>{
  2128. if(typeof(response) === "string"){
  2129. controller.createBanner(response, "error");
  2130. }else{
  2131. for(let i = 0; i < response.recipes.length; i++){
  2132. response.recipes[i].recipe = response.recipes[i].recipe._id;
  2133. }
  2134. merchant.addTransaction(response);
  2135. controller.updateAnalytics();
  2136. controller.openStrand("transactions", merchant.transactions);
  2137. controller.createBanner("TRANSACTION SUCCESSFULLY CREATED. INGREDIENTS UPDATED", "success");
  2138. }
  2139. })
  2140. .catch((err)=>{
  2141. controller.createBanner("UNABLE TO DISPLAY NEW TRANSACTIONS. PLEASE REFRESH THE PAGE", "error");
  2142. })
  2143. .finally(()=>{
  2144. loader.style.display = "none";
  2145. });
  2146. }
  2147. }
  2148. module.exports = newTransaction;
  2149. },{}],14:[function(require,module,exports){
  2150. let orderCalculator = {
  2151. display: function(){
  2152. let to = new Date();
  2153. to.setDate(to.getDate() + 7);
  2154. to.setHours(0, 0, 0, 0);
  2155. from = new Date();
  2156. from.setDate(from.getDate() + 1);
  2157. from.setHours(0, 0, 0, 0);
  2158. document.getElementById("predictDateFrom").valueAsDate = from;
  2159. document.getElementById("predictDateTo").valueAsDate = to;
  2160. document.getElementById("predictButton").addEventListener("click", ()=>{this.predict()});
  2161. let selector = document.getElementById("predictSelector");
  2162. for(let i = 0; i < merchant.ingredients.length; i++){
  2163. let option = document.createElement("option");
  2164. option.innerText = merchant.ingredients[i].ingredient.name;
  2165. option.value = merchant.ingredients[i].ingredient.id;
  2166. selector.appendChild(option);
  2167. }
  2168. },
  2169. predict: function(){
  2170. let from = document.getElementById("predictDateFrom").valueAsDate;
  2171. let to = document.getElementById("predictDateTo").valueAsDate;
  2172. let ingredient = merchant.getIngredient(document.getElementById("predictSelector").value);
  2173. let data = {
  2174. to: new Date(),
  2175. recipes: []
  2176. }
  2177. data.from = new Date(data.to.getFullYear() - 1, data.to.getMonth(), data.to.getDate());
  2178. fetch("/transaction", {
  2179. method: "post",
  2180. headers: {
  2181. "Content-Type": "application/json;charset=utf-8"
  2182. },
  2183. body: JSON.stringify(data)
  2184. })
  2185. .then(response => response.json())
  2186. .then((response)=>{
  2187. if(typeof(response) === "string"){
  2188. controller.createBanner(response, "error");
  2189. }else{
  2190. const options = {
  2191. task: "regression",
  2192. debug: false
  2193. }
  2194. const nn = ml5.neuralNetwork(options);
  2195. this.createData(nn, response, ingredient.ingredient);
  2196. nn.normalizeData();
  2197. nn.train(()=>{
  2198. let predictors = [];
  2199. while(from <= to){
  2200. predictors.push(nn.predict({
  2201. month: from.getMonth(),
  2202. day: from.getDay()
  2203. }))
  2204. from.setDate(from.getDate() + 1);
  2205. }
  2206. Promise.all(predictors)
  2207. .then((predictions)=>{
  2208. let total = 0
  2209. for(let i = 0; i < predictions.length; i++){
  2210. total += predictions[i][0].value;
  2211. }
  2212. if(isNaN(total)) total = 0;
  2213. document.getElementById("prediction").innerText = `${total.toFixed(2)} ${ingredient.ingredient.unit.toUpperCase()}`;
  2214. })
  2215. .catch((err)=>{
  2216. controller.createBanner("ERROR: UNABLE TO MAKE PREDICTION", "error");
  2217. });
  2218. });
  2219. }
  2220. })
  2221. .catch((err)=>{
  2222. controller.createBanner("ERROR: UNABLE TO MAKE PREDICTION", "error");
  2223. });
  2224. },
  2225. createData: function(nn, transactions, ingredient){
  2226. let today = new Date(transactions[transactions.length-1].date);
  2227. today.setHours(0, 0, 0, 0);
  2228. let tomorrow = new Date(transactions[transactions.length-1].date);
  2229. tomorrow.setDate(tomorrow.getDate() + 1);
  2230. tomorrow.setHours(0, 0, 0, 0);
  2231. let dailySum = 0;
  2232. for(let i = transactions.length - 1; i >= 0; i--){
  2233. transactions[i].date = new Date(transactions[i].date);
  2234. if(transactions[i].date >= tomorrow){
  2235. let inputs = {
  2236. month: today.getMonth(),
  2237. day: today.getDay()
  2238. };
  2239. let output = {quantity: dailySum};
  2240. nn.addData(inputs, output);
  2241. dailySum = 0;
  2242. today.setDate(today.getDate() + 1);
  2243. tomorrow.setDate(tomorrow.getDate() + 1);
  2244. }
  2245. for(let j = 0; j < transactions[i].recipes.length; j++){
  2246. for(let k = 0; k < merchant.recipes.length; k++){
  2247. if(merchant.recipes[k].id === transactions[i].recipes[j].recipe){
  2248. for(let l = 0; l < merchant.recipes[k].ingredients.length; l++){
  2249. if(merchant.recipes[k].ingredients[l].ingredient === ingredient){
  2250. dailySum += merchant.recipes[k].ingredients[l].quantity * transactions[i].recipes[j].quantity;
  2251. }
  2252. break;
  2253. }
  2254. break;
  2255. }
  2256. }
  2257. }
  2258. }
  2259. }
  2260. }
  2261. module.exports = orderCalculator;
  2262. },{}],15:[function(require,module,exports){
  2263. let orderDetails = {
  2264. display: function(order){
  2265. document.getElementById("removeOrderBtn").onclick = ()=>{this.remove(order)};
  2266. document.getElementById("orderDetailName").innerText = order.name;
  2267. document.getElementById("orderDetailDate").innerText = order.date.toLocaleDateString("en-US");
  2268. document.getElementById("orderDetailTax").innerText = `$${order.taxes.toFixed(2)}`;
  2269. document.getElementById("orderDetailFee").innerText = `$${order.fees.toFixed(2)}`;
  2270. let ingredientList = document.getElementById("orderIngredients");
  2271. while(ingredientList.children.length > 0){
  2272. ingredientList.removeChild(ingredientList.firstChild);
  2273. }
  2274. let template = document.getElementById("orderIngredient").content.children[0];
  2275. for(let i = 0; i < order.ingredients.length; i++){
  2276. let ingredientDiv = template.cloneNode(true);
  2277. const ingredient = order.ingredients[i].ingredient;
  2278. ingredientDiv.children[0].innerText = order.ingredients[i].ingredient.name;
  2279. ingredientDiv.children[2].innerText = `$${order.ingredients[i].cost().toFixed(2)}`;
  2280. ingredientDiv.onclick = ()=>{
  2281. controller.openStrand("ingredients");
  2282. controller.openSidebar("ingredientDetails", merchant.getIngredient(order.ingredients[i].ingredient.id));
  2283. }
  2284. let ingredientDisplay = ingredientDiv.children[1];
  2285. ingredientDisplay.innerText = `${order.ingredients[i].quantity.toFixed(2)} ${ingredient.unit.toUpperCase()} X $${order.ingredients[i].pricePerUnit.toFixed(2)}`;
  2286. ingredientList.appendChild(ingredientDiv);
  2287. }
  2288. document.getElementById("orderDetailTotal").innerText = `$${order.getIngredientCost().toFixed(2)}`;
  2289. document.querySelector("#orderTotalPrice p").innerText = `$${order.getTotalCost().toFixed(2)}`;
  2290. },
  2291. remove: function(order){
  2292. let loader = document.getElementById("loaderContainer");
  2293. loader.style.display = "flex";
  2294. fetch(`/order/${order.id}`, {
  2295. method: "DELETE",
  2296. headers: {
  2297. "Content-Type": "application/json;charset=utf-8"
  2298. }
  2299. })
  2300. .then((response) => response.json())
  2301. .then((response)=>{
  2302. if(typeof(response) === "string"){
  2303. controller.createBanner(response, "error");
  2304. }else{
  2305. merchant.removeOrder(order);
  2306. controller.openStrand("orders", merchant.orders);
  2307. controller.createBanner("ORDER REMOVED", "success");
  2308. }
  2309. })
  2310. .catch((err)=>{
  2311. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  2312. })
  2313. .finally(()=>{
  2314. loader.style.display = "none";
  2315. });
  2316. }
  2317. }
  2318. module.exports = orderDetails;
  2319. },{}],16:[function(require,module,exports){
  2320. let orderFilter = {
  2321. display: function(Order){
  2322. let now = new Date();
  2323. let past = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 30);
  2324. let ingredientList = document.getElementById("orderFilterIngredients");
  2325. document.getElementById("orderFilterDateFrom").valueAsDate = past;
  2326. document.getElementById("orderFilterDateTo").valueAsDate = now;
  2327. while(ingredientList.children.length > 0){
  2328. ingredientList.removeChild(ingredientList.firstChild);
  2329. }
  2330. for(let i = 0; i < merchant.ingredients.length; i++){
  2331. let element = document.createElement("div");
  2332. element.classList.add("choosable");
  2333. element.ingredient = merchant.ingredients[i].ingredient.id;
  2334. element.onclick = ()=>{this.toggleActive(element)};
  2335. ingredientList.appendChild(element);
  2336. let text = document.createElement("p");
  2337. text.innerText = merchant.ingredients[i].ingredient.name;
  2338. element.appendChild(text);
  2339. }
  2340. document.getElementById("orderFilterSubmit").onclick = ()=>{this.submit(Order)};
  2341. },
  2342. toggleActive: function(element){
  2343. if(element.classList.contains("active")){
  2344. element.classList.remove("active");
  2345. }else{
  2346. element.classList.add("active");
  2347. }
  2348. },
  2349. submit: function(Order){
  2350. let data = {
  2351. from: document.getElementById("orderFilterDateFrom").valueAsDate,
  2352. to: document.getElementById("orderFilterDateTo").valueAsDate,
  2353. ingredients: []
  2354. }
  2355. data.from.setHours(0, 0, 0, 0);
  2356. data.to.setHours(0, 0, 0, 0);
  2357. let ingredients = document.getElementById("orderFilterIngredients").children;
  2358. for(let i = 0; i < ingredients.length; i++){
  2359. if(ingredients[i].classList.contains("active")){
  2360. data.ingredients.push(ingredients[i].ingredient);
  2361. }
  2362. }
  2363. let loader = document.getElementById("loaderContainer");
  2364. loader.style.display = "flex";
  2365. fetch("/orders/get", {
  2366. method: "post",
  2367. headers: {
  2368. "Content-Type": "application/json;charset=utf-8"
  2369. },
  2370. body: JSON.stringify(data)
  2371. })
  2372. .then(response => response.json())
  2373. .then((response)=>{
  2374. let orders = [];
  2375. if(typeof(response) === "string"){
  2376. controller.createBanner(response, "error");
  2377. }else if(response.length === 0){
  2378. controller.createBanner("NO ORDERS MATCH YOUR SEARCH", "error");
  2379. }else{
  2380. for(let i = 0; i < response.length; i++){
  2381. orders.push(new Order(
  2382. response[i]._id,
  2383. response[i].name,
  2384. response[i].date,
  2385. response[i].taxes,
  2386. response[i].fees,
  2387. response[i].ingredients,
  2388. merchant
  2389. ));
  2390. }
  2391. }
  2392. controller.openStrand("orders", orders);
  2393. })
  2394. .catch((err)=>{
  2395. controller.createBanner("UNABLE TO DISPLAY THE ORDERS", "error");
  2396. })
  2397. .finally(()=>{
  2398. loader.style.display = "none";
  2399. });
  2400. }
  2401. }
  2402. module.exports = orderFilter;
  2403. },{}],17:[function(require,module,exports){
  2404. let recipeDetails = {
  2405. display: function(recipe){
  2406. document.getElementById("editRecipeBtn").onclick = ()=>{controller.openSidebar("editRecipe", recipe)};
  2407. document.getElementById("recipeName").innerText = recipe.name;
  2408. if(merchant.pos === "none"){
  2409. document.getElementById("removeRecipeBtn").onclick = ()=>{this.remove(recipe)};
  2410. }
  2411. //ingredient list
  2412. let ingredientsDiv = document.getElementById("recipeIngredientList");
  2413. while(ingredientsDiv.children.length > 0){
  2414. ingredientsDiv.removeChild(ingredientsDiv.firstChild);
  2415. }
  2416. let template = document.getElementById("recipeIngredient").content.children[0];
  2417. for(let i = 0; i < recipe.ingredients.length; i++){
  2418. let recipeDiv = template.cloneNode(true);
  2419. recipeDiv.children[0].innerText = recipe.ingredients[i].ingredient.name;
  2420. recipeDiv.children[1].innerText = `${recipe.ingredients[i].getQuantityDisplay()}`;
  2421. recipeDiv.onclick = ()=>{
  2422. controller.openStrand("ingredients");
  2423. controller.openSidebar("ingredientDetails", merchant.getIngredient(recipe.ingredients[i].ingredient.id));
  2424. }
  2425. ingredientsDiv.appendChild(recipeDiv);
  2426. }
  2427. document.getElementById("recipePrice").children[1].innerText = `$${recipe.price.toFixed(2)}`;
  2428. },
  2429. remove: function(recipe){
  2430. let loader = document.getElementById("loaderContainer");
  2431. loader.style.display = "flex";
  2432. fetch(`/recipe/remove/${recipe.id}`, {
  2433. method: "delete"
  2434. })
  2435. .then((response) => response.json())
  2436. .then((response)=>{
  2437. if(typeof(response) === "string"){
  2438. controller.createBanner(response, "error");
  2439. }else{
  2440. merchant.removeRecipe(recipe);
  2441. controller.createBanner("RECIPE REMOVED", "success");
  2442. controller.openStrand("recipeBook");
  2443. }
  2444. })
  2445. .catch((err)=>{
  2446. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  2447. })
  2448. .finally(()=>{
  2449. loader.style.display = "none";
  2450. });
  2451. }
  2452. }
  2453. module.exports = recipeDetails;
  2454. },{}],18:[function(require,module,exports){
  2455. let transactionDetails = {
  2456. transaction: {},
  2457. display: function(transaction){
  2458. this.transaction = transaction;
  2459. let recipeList = document.getElementById("transactionRecipes");
  2460. let template = document.getElementById("transactionRecipe").content.children[0];
  2461. let totalRecipes = 0;
  2462. let totalPrice = 0;
  2463. while(recipeList.children.length > 0){
  2464. recipeList.removeChild(recipeList.firstChild);
  2465. }
  2466. for(let i = 0; i < transaction.recipes.length; i++){
  2467. let recipe = template.cloneNode(true);
  2468. let price = transaction.recipes[i].quantity * transaction.recipes[i].recipe.price;
  2469. recipe.children[0].innerText = transaction.recipes[i].recipe.name;
  2470. recipe.children[1].innerText = `${transaction.recipes[i].quantity} x $${transaction.recipes[i].recipe.price.toFixed(2)}`;
  2471. recipe.children[2].innerText = `$${price.toFixed(2)}`;
  2472. recipe.onclick = ()=>{
  2473. controller.openStrand("recipeBook");
  2474. controller.openSidebar("recipeDetails", transaction.recipes[i].recipe);
  2475. }
  2476. recipeList.appendChild(recipe);
  2477. totalRecipes += transaction.recipes[i].quantity;
  2478. totalPrice += price;
  2479. }
  2480. let months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  2481. let days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  2482. let dateString = `${days[transaction.date.getDay()]}, ${months[transaction.date.getMonth()]} ${transaction.date.getDate()}, ${transaction.date.getFullYear()}`;
  2483. document.getElementById("transactionDate").innerText = dateString;
  2484. document.getElementById("totalRecipes").innerText = `${totalRecipes} recipes`;
  2485. document.getElementById("totalPrice").innerText = `$${totalPrice.toFixed(2)}`;
  2486. if(merchant.pos === "none"){
  2487. document.getElementById("removeTransBtn").onclick = ()=>{this.remove()};
  2488. }
  2489. },
  2490. remove: function(){
  2491. let loader = document.getElementById("loaderContainer");
  2492. loader.style.display = "flex";
  2493. fetch(`/transaction/${this.transaction.id}`, {
  2494. method: "delete",
  2495. headers: {
  2496. "Content-Type": "application/json;charset=utf-8"
  2497. },
  2498. })
  2499. .then((response)=>{
  2500. if(typeof(response) === "string"){
  2501. controller.createBanner(response, "error");
  2502. }else{
  2503. merchant.removeTransaction(this.transaction);
  2504. controller.updateAnalytics();
  2505. controller.openStrand("transactions", merchant.getTransactions());
  2506. controller.createBanner("TRANSACTION REMOVED", "success");
  2507. }
  2508. })
  2509. .catch((err)=>{
  2510. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  2511. })
  2512. .finally(()=>{
  2513. loader.style.display = "none";
  2514. });
  2515. },
  2516. }
  2517. module.exports = transactionDetails;
  2518. },{}],19:[function(require,module,exports){
  2519. let transactionFilter = {
  2520. display: function(Transaction){
  2521. //Set default dates
  2522. let today = new Date();
  2523. let monthAgo = new Date(today);
  2524. monthAgo.setMonth(today.getMonth() - 1);
  2525. document.getElementById("transFilterDateStart").valueAsDate = monthAgo;
  2526. document.getElementById("transFilterDateEnd").valueAsDate = today;
  2527. //populate recipes
  2528. let recipeList = document.getElementById("transFilterRecipeList");
  2529. while(recipeList.children.length > 0){
  2530. recipeList.removeChild(recipeList.firstChild);
  2531. }
  2532. for(let i = 0; i < merchant.recipes.length; i++){
  2533. let recipe = document.createElement("div");
  2534. recipe.innerText = merchant.recipes[i].name;
  2535. recipe.recipe = merchant.recipes[i];
  2536. recipe.classList.add("choosable");
  2537. recipe.onclick = ()=>{this.toggleActive(recipe)};
  2538. recipeList.appendChild(recipe);
  2539. }
  2540. //Submit button
  2541. document.getElementById("transFilterSubmit").onclick = ()=>{this.submit(Transaction)};
  2542. },
  2543. toggleActive: function(element){
  2544. if(element.classList.contains("active")){
  2545. element.classList.remove("active");
  2546. }else{
  2547. element.classList.add("active");
  2548. }
  2549. },
  2550. submit: function(Transaction){
  2551. let data = {
  2552. from: document.getElementById("transFilterDateStart").valueAsDate,
  2553. to: document.getElementById("transFilterDateEnd").valueAsDate,
  2554. recipes: []
  2555. }
  2556. data.from.setHours(0, 0, 0, 0);
  2557. data.to.setHours(0, 0, 0, 0);
  2558. if(data.startDate >= data.endDate){
  2559. controller.createBanner("START DATE CANNOT BE AFTER END DATE", "error");
  2560. return;
  2561. }
  2562. let recipes = document.getElementById("transFilterRecipeList").children;
  2563. for(let i = 0; i < recipes.length; i++){
  2564. if(recipes[i].classList.contains("active")){
  2565. data.recipes.push(recipes[i].recipe.id);
  2566. }
  2567. }
  2568. let loader = document.getElementById("loaderContainer");
  2569. loader.style.display = "flex";
  2570. fetch("/transaction", {
  2571. method: "post",
  2572. headers: {
  2573. "Content-Type": "application/json;charset=utf-8"
  2574. },
  2575. body: JSON.stringify(data)
  2576. })
  2577. .then(response => response.json())
  2578. .then((response)=>{
  2579. let transactions = [];
  2580. if(typeof(response) === "string"){
  2581. controller.createBanner(response, "error");
  2582. }else if(response.length === 0){
  2583. controller.createBanner("NO TRANSACTIONS MATCH YOUR SEARCH", "error");
  2584. }else{
  2585. for(let i = 0; i < response.length; i++){
  2586. transactions.push(new Transaction(
  2587. response[i]._id,
  2588. response[i].date,
  2589. response[i].recipes,
  2590. merchant
  2591. ));
  2592. }
  2593. }
  2594. controller.openStrand("transactions", transactions);
  2595. })
  2596. .catch((err)=>{
  2597. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  2598. })
  2599. .finally(()=>{
  2600. loader.style.display = "none";
  2601. });
  2602. }
  2603. }
  2604. module.exports = transactionFilter;
  2605. },{}],20:[function(require,module,exports){
  2606. let analytics = {
  2607. isPopulated: false,
  2608. ingredient: undefined,
  2609. recipe: undefined,
  2610. transactionsByDate: [],
  2611. display: function(Transaction){
  2612. if(!this.isPopulated){
  2613. document.getElementById("analRecipeContent").style.display = "none";
  2614. let to = new Date()
  2615. let from = new Date(to.getFullYear(), to.getMonth() - 1, to.getDate());
  2616. document.getElementById("analStartDate").valueAsDate = from;
  2617. document.getElementById("analEndDate").valueAsDate = to;
  2618. let analSlider = document.getElementById("analSlider");
  2619. analSlider.onclick = ()=>{this.switchDisplay()};
  2620. analSlider.checked = false;
  2621. document.getElementById("analDateBtn").onclick = ()=>{this.newDates(Transaction)};
  2622. this.populateButtons();
  2623. if(merchant.ingredients.length > 0){
  2624. this.ingredient = merchant.ingredients[0].ingredient;
  2625. }
  2626. if(merchant.recipes.length > 0){
  2627. this.recipe = merchant.recipes[0];
  2628. }
  2629. this.newDates(Transaction);
  2630. this.isPopulated = true;
  2631. }
  2632. },
  2633. populateButtons: function(){
  2634. let ingredientButtons = document.getElementById("analIngredientList");
  2635. let recipeButtons = document.getElementById("analRecipeList");
  2636. while(ingredientButtons.children.length > 0){
  2637. ingredientButtons.removeChild(ingredientButtons.firstChild);
  2638. }
  2639. for(let i = 0; i < merchant.ingredients.length; i++){
  2640. let button = document.createElement("button");
  2641. button.innerText = merchant.ingredients[i].ingredient.name;
  2642. button.classList.add("choosable");
  2643. button.onclick = ()=>{
  2644. this.ingredient = merchant.ingredients[i].ingredient;
  2645. this.displayIngredient();
  2646. };
  2647. ingredientButtons.appendChild(button);
  2648. }
  2649. while(recipeButtons.children.length > 0){
  2650. recipeButtons.removeChild(recipeButtons.firstChild);
  2651. }
  2652. for(let i = 0; i < merchant.recipes.length; i++){
  2653. let button = document.createElement("button");
  2654. button.innerText = merchant.recipes[i].name;
  2655. button.classList.add("choosable");
  2656. button.onclick = ()=>{
  2657. this.recipe = merchant.recipes[i];
  2658. this.displayRecipe();
  2659. };
  2660. recipeButtons.appendChild(button);
  2661. }
  2662. },
  2663. getData: function(from, to, Transaction){
  2664. let data = {
  2665. from: from,
  2666. to: to,
  2667. recipes: []
  2668. }
  2669. let loader = document.getElementById("loaderContainer");
  2670. loader.style.display = "flex";
  2671. return fetch("/transaction", {
  2672. method: "post",
  2673. headers: {
  2674. "Content-Type": "application/json;charset=utf-8"
  2675. },
  2676. body: JSON.stringify(data)
  2677. })
  2678. .then(response => response.json())
  2679. .then((response)=>{
  2680. if(typeof(response) === "string"){
  2681. controller.createBanner(response, "error");
  2682. }else{
  2683. this.transactionsByDate = [];
  2684. response.reverse();
  2685. let startOfDay = new Date(from.getTime());
  2686. startOfDay.setHours(0, 0, 0, 0);
  2687. let endOfDay = new Date(from.getTime());
  2688. endOfDay.setDate(endOfDay.getDate() + 1);
  2689. endOfDay.setHours(0, 0, 0, 0);
  2690. let transactionIndex = 0;
  2691. while(startOfDay <= to){
  2692. let currentTransactions = [];
  2693. while(transactionIndex < response.length && new Date(response[transactionIndex].date) < endOfDay){
  2694. currentTransactions.push(new Transaction(
  2695. response[transactionIndex]._id,
  2696. response[transactionIndex].date,
  2697. response[transactionIndex].recipes,
  2698. merchant
  2699. ));
  2700. transactionIndex++;
  2701. }
  2702. let thing = {
  2703. date: new Date(startOfDay.getTime()),
  2704. transactions: currentTransactions
  2705. };
  2706. this.transactionsByDate.push(thing);
  2707. startOfDay.setDate(startOfDay.getDate() + 1);
  2708. endOfDay.setDate(endOfDay.getDate() + 1);
  2709. }
  2710. }
  2711. })
  2712. .catch((err)=>{
  2713. controller.createBanner("UNABLE TO UPDATE THE PAGE", "error");
  2714. })
  2715. .finally(()=>{
  2716. loader.style.display = "none";
  2717. });
  2718. },
  2719. displayIngredient: function(){
  2720. if(this.ingredient === undefined || this.transactionsByDate.length === 0){
  2721. return;
  2722. }
  2723. //break down data into dates and quantities
  2724. let dates = [];
  2725. let quantities = [];
  2726. for(let i = 0; i < this.transactionsByDate.length; i++){
  2727. dates.push(this.transactionsByDate[i].date);
  2728. let sum = 0;
  2729. for(let j = 0; j < this.transactionsByDate[i].transactions.length; j++){
  2730. let transaction = this.transactionsByDate[i].transactions[j];
  2731. sum += transaction.getIngredientQuantity(this.ingredient);
  2732. }
  2733. quantities.push(sum);
  2734. }
  2735. //create and display the graph
  2736. let trace = {
  2737. x: dates,
  2738. y: quantities,
  2739. mode: "lines+markers",
  2740. line: {
  2741. color: "rgb(255, 99, 107)"
  2742. }
  2743. }
  2744. let yaxis = `QUANTITY (${this.ingredient.unit.toUpperCase()})`;
  2745. const layout = {
  2746. title: this.ingredient.name.toUpperCase(),
  2747. xaxis: {title: "DATE"},
  2748. yaxis: {title: yaxis},
  2749. margin: {
  2750. l: 40,
  2751. r: 10,
  2752. b: 20,
  2753. t: 30
  2754. }
  2755. }
  2756. Plotly.newPlot("itemUseGraph", [trace], layout);
  2757. //Create min/max/avg
  2758. //Current ingredient is stored on the "analMinUse" element
  2759. let min = quantities[0];
  2760. let max = quantities[0];
  2761. let sum = 0;
  2762. for(let i = 0; i < quantities.length; i++){
  2763. if(quantities[i] < min){
  2764. min = quantities[i];
  2765. }
  2766. if(quantities[i] > max){
  2767. max = quantities[i];
  2768. }
  2769. sum += quantities[i];
  2770. }
  2771. document.getElementById("analMinUse").innerText = `${min.toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2772. document.getElementById("analAvgUse").innerText = `${(sum / quantities.length).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2773. document.getElementById("analMaxUse").innerText = `${max.toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2774. //Create weekday averages
  2775. let dayUse = [0, 0, 0, 0, 0, 0, 0];
  2776. let dayCount = [0, 0, 0, 0, 0, 0, 0];
  2777. for(let i = 0; i < quantities.length; i++){
  2778. dayUse[dates[i].getDay()] += quantities[i];
  2779. dayCount[dates[i].getDay()]++;
  2780. }
  2781. document.getElementById("analDayOne").innerText = `${(dayUse[0] / dayCount[0]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2782. document.getElementById("analDayTwo").innerText = `${(dayUse[1] / dayCount[1]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2783. document.getElementById("analDayThree").innerText = `${(dayUse[2] / dayCount[2]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2784. document.getElementById("analDayFour").innerText = `${(dayUse[3] / dayCount[3]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2785. document.getElementById("analDayFive").innerText = `${(dayUse[4] / dayCount[4]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2786. document.getElementById("analDaySix").innerText = `${(dayUse[5] / dayCount[5]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2787. document.getElementById("analDaySeven").innerText = `${(dayUse[6] / dayCount[6]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2788. },
  2789. displayRecipe: function(){
  2790. if(this.recipe === undefined || this.transactionsByDate.length === 0){
  2791. return;
  2792. }
  2793. //break down data into dates and quantities
  2794. let dates = [];
  2795. let quantities = [];
  2796. for(let i = 0; i < this.transactionsByDate.length; i++){
  2797. dates.push(this.transactionsByDate[i].date);
  2798. let sum = 0;
  2799. for(let j = 0; j < this.transactionsByDate[i].transactions.length; j++){
  2800. const transaction = this.transactionsByDate[i].transactions[j];
  2801. for(let k = 0; k < transaction.recipes.length; k++){
  2802. if(transaction.recipes[k].recipe === this.recipe){
  2803. sum += transaction.recipes[k].quantity;
  2804. }
  2805. }
  2806. }
  2807. quantities.push(sum);
  2808. }
  2809. //create and display the graph
  2810. const trace = {
  2811. x: dates,
  2812. y: quantities,
  2813. mode: "lines+markers",
  2814. line: {
  2815. color: "rgb(255, 99, 107)"
  2816. }
  2817. }
  2818. const layout = {
  2819. title: this.recipe.name.toUpperCase(),
  2820. xaxis: {title: "DATE"},
  2821. yaxis: {title: "QUANTITY"},
  2822. margin: {
  2823. l: 40,
  2824. r: 10,
  2825. b: 20,
  2826. t: 30
  2827. }
  2828. }
  2829. Plotly.newPlot("recipeSalesGraph", [trace], layout);
  2830. //Display the boxes at the bottom
  2831. //Current recipe is stored on the "recipeAvgUse" element
  2832. let avg = 0;
  2833. for(let i = 0; i < quantities.length; i++){
  2834. avg += quantities[i];
  2835. }
  2836. avg = avg / quantities.length;
  2837. document.getElementById("recipeAvgUse").innerText = avg.toFixed(2);
  2838. document.getElementById("recipeAvgRevenue").innerText = `$${(avg * this.recipe.price).toFixed(2)}`;
  2839. },
  2840. switchDisplay: function(){
  2841. const checkbox = document.getElementById("analSlider");
  2842. let ingredient = document.getElementById("analIngredientContent");
  2843. let recipe = document.getElementById("analRecipeContent");
  2844. if(checkbox.checked === true){
  2845. ingredient.style.display = "none";
  2846. recipe.style.display = "flex";
  2847. this.displayRecipe();
  2848. }else{
  2849. ingredient.style.display = "flex";
  2850. recipe.style.display = "none";
  2851. this.displayIngredient();
  2852. }
  2853. },
  2854. newDates: async function(Transaction){
  2855. const from = document.getElementById("analStartDate").valueAsDate;
  2856. const to = document.getElementById("analEndDate").valueAsDate;
  2857. from.setHours(0, 0, 0, 0);
  2858. to.setDate(to.getDate() + 1);
  2859. to.setHours(0, 0, 0, 0);
  2860. await this.getData(from, to, Transaction);
  2861. if(document.getElementById("analSlider").checked === true){
  2862. this.displayRecipe();
  2863. }else{
  2864. this.displayIngredient();
  2865. }
  2866. }
  2867. }
  2868. module.exports = analytics;
  2869. },{}],21:[function(require,module,exports){
  2870. let home = {
  2871. isPopulated: false,
  2872. display: function(){
  2873. if(!this.isPopulated){
  2874. this.drawRevenueCard();
  2875. this.drawRevenueGraph();
  2876. this.drawInventoryCheckCard();
  2877. this.drawPopularCard();
  2878. this.isPopulated = true;
  2879. }
  2880. },
  2881. drawRevenueCard: function(){
  2882. let today = new Date();
  2883. let firstOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
  2884. let firstOfLastMonth = new Date(today.getFullYear(), today.getMonth() - 1, 1);
  2885. let lastMonthToDay = new Date(new Date().setMonth(today.getMonth() - 1));
  2886. const revenueThisMonth = merchant.getRevenue(firstOfMonth);
  2887. const revenueLastMonthToDay = merchant.getRevenue(firstOfLastMonth, lastMonthToDay);
  2888. document.getElementById("revenue").innerText = `$${revenueThisMonth.toFixed(2)}`;
  2889. let revenueChange = ((revenueThisMonth - revenueLastMonthToDay) / revenueLastMonthToDay) * 100;
  2890. let img = "";
  2891. if(revenueChange >= 0){
  2892. img = "/shared/images/upArrow.png";
  2893. }else{
  2894. img = "/shared/images/downArrow.png";
  2895. }
  2896. document.querySelector("#revenueChange p").innerText = `${Math.abs(revenueChange).toFixed(2)}% vs last month`;
  2897. document.querySelector("#revenueChange img").src = img;
  2898. },
  2899. drawRevenueGraph: function(){
  2900. let monthAgo = new Date();
  2901. monthAgo.setMonth(monthAgo.getMonth() - 1);
  2902. let revenue = [];
  2903. let dates = [];
  2904. let dayRevenue = 0;
  2905. const transactions = merchant.getTransactions(monthAgo);
  2906. let currentDate = (transactions.length > 0) ? transactions[0].date : undefined;
  2907. for(let i = 0; i < transactions.length; i++){
  2908. if(transactions[i].date.getDate() !== currentDate.getDate()){
  2909. revenue.push(dayRevenue / 100);
  2910. dayRevenue = 0;
  2911. dates.push(currentDate);
  2912. currentDate = transactions[i].date;
  2913. }
  2914. for(let j = 0; j < transactions[i].recipes.length; j++){
  2915. const recipe = transactions[i].recipes[j];
  2916. dayRevenue += recipe.recipe.price * recipe.quantity;
  2917. }
  2918. }
  2919. const trace = {
  2920. x: dates,
  2921. y: revenue,
  2922. mode: "lines+markers",
  2923. line: {
  2924. color: "rgb(255, 99, 107)"
  2925. }
  2926. }
  2927. let layout = {
  2928. title: "REVENUE",
  2929. xaxis: {
  2930. title: "DATE"
  2931. },
  2932. yaxis: {
  2933. title: "$"
  2934. }
  2935. }
  2936. if(screen.width < 1400){
  2937. layout.margin = {
  2938. l: 35,
  2939. r: 0
  2940. }
  2941. }
  2942. Plotly.newPlot("graphCard", [trace], layout);
  2943. },
  2944. drawInventoryCheckCard: function(){
  2945. let num;
  2946. if(merchant.ingredients.length < 5){
  2947. num = merchant.ingredients.length;
  2948. }else{
  2949. num = 5;
  2950. }
  2951. let rands = [];
  2952. for(let i = 0; i < num; i++){
  2953. let rand = Math.floor(Math.random() * merchant.ingredients.length);
  2954. if(rands.includes(rand)){
  2955. i--;
  2956. }else{
  2957. rands[i] = rand;
  2958. }
  2959. }
  2960. let ul = document.querySelector("#inventoryCheckCard ul");
  2961. let template = document.getElementById("ingredientCheck").content.children[0];
  2962. while(ul.children.length > 0){
  2963. ul.removeChild(ul.firstChild);
  2964. }
  2965. for(let i = 0; i < rands.length; i++){
  2966. let ingredientCheck = template.cloneNode(true);
  2967. let input = ingredientCheck.children[1].children[1];
  2968. const ingredient = merchant.ingredients[rands[i]];
  2969. ingredientCheck.ingredient = ingredient;
  2970. ingredientCheck.children[0].innerText = ingredient.ingredient.name;
  2971. ingredientCheck.children[1].children[0].onclick = ()=>{
  2972. input.value--;
  2973. input.changed = true;
  2974. };
  2975. input.value = ingredient.quantity.toFixed(2);
  2976. ingredientCheck.children[2].innerText = ingredient.ingredient.unit.toUpperCase();
  2977. ingredientCheck.children[1].children[2].onclick = ()=>{
  2978. input.value++;
  2979. input.changed = true;
  2980. }
  2981. input.onchange = ()=>{input.changed = true};
  2982. ul.appendChild(ingredientCheck);
  2983. }
  2984. document.getElementById("inventoryCheck").onclick = ()=>{this.submitInventoryCheck()};
  2985. },
  2986. drawPopularCard: function(){
  2987. let thisMonth = new Date();
  2988. thisMonth.setDate(1);
  2989. const ingredientList = merchant.getIngredientsSold(thisMonth);
  2990. if(ingredientList !== false){
  2991. ingredientList.sort((a, b)=>{
  2992. if(a.quantity < b.quantity){
  2993. return 1;
  2994. }
  2995. if(a.quantity > b.quantity){
  2996. return -1;
  2997. }
  2998. return 0;
  2999. });
  3000. let quantities = [];
  3001. let labels = [];
  3002. let colors = [];
  3003. let count = (ingredientList.length < 5) ? ingredientList.length - 1 : 4;
  3004. for(let i = count; i >= 0; i--){
  3005. const ingredientName = ingredientList[i].ingredient.name;
  3006. const ingredientQuantity = ingredientList[i].quantity;
  3007. const unitName = ingredientList[i].ingredient.unit;
  3008. quantities.push(ingredientList[i].quantity);
  3009. labels.push(`${ingredientName}: ${ingredientQuantity.toFixed(2)} ${unitName.toUpperCase()}`);
  3010. if(i === 0){
  3011. colors.push("rgb(255, 99, 107");
  3012. }else{
  3013. colors.push("rgb(179, 191, 209");
  3014. }
  3015. }
  3016. let trace = {
  3017. x: quantities,
  3018. type: "bar",
  3019. orientation: "h",
  3020. text: labels,
  3021. textposition: "auto",
  3022. hoverinfo: "none",
  3023. marker: {
  3024. color: colors
  3025. }
  3026. }
  3027. let layout = {
  3028. title: {
  3029. text: "MOST POPULAR INGREDIENTS"
  3030. },
  3031. xaxis: {
  3032. zeroline: false,
  3033. title: "QUANTITY"
  3034. },
  3035. yaxis: {
  3036. showticklabels: false
  3037. }
  3038. }
  3039. if(screen.width < 1400){
  3040. layout.margin = {
  3041. l: 10,
  3042. r: 10,
  3043. t: 80,
  3044. b: 40
  3045. };
  3046. }
  3047. Plotly.newPlot("popularIngredientsCard", [trace], layout);
  3048. }else{
  3049. document.getElementById("popularCanvas").style.display = "none";
  3050. let notice = document.createElement("p");
  3051. notice.innerText = "N/A";
  3052. notice.classList = "notice";
  3053. document.getElementById("popularIngredientsCard").appendChild(notice);
  3054. }
  3055. },
  3056. //Need to change the updating of ingredients
  3057. //should update the ingredient directly, then send that. Maybe...
  3058. submitInventoryCheck: function(){
  3059. let lis = document.querySelectorAll("#inventoryCheckCard li");
  3060. let data = [];
  3061. for(let i = 0; i < lis.length; i++){
  3062. if(lis[i].children[1].children[1].value >= 0){
  3063. if(lis[i].children[1].children[1].changed === true){
  3064. let merchIngredient = lis[i].ingredient;
  3065. data.push({
  3066. id: merchIngredient.ingredient.id,
  3067. quantity: lis[i].children[1].children[1].value
  3068. });
  3069. lis[i].children[1].children[1].changed = false;
  3070. }
  3071. }else{
  3072. controller.createBanner("CANNOT HAVE NEGATIVE INGREDIENTS", "error");
  3073. return;
  3074. }
  3075. }
  3076. if(data.length > 0){
  3077. let loader = document.getElementById("loaderContainer");
  3078. loader.style.display = "flex";
  3079. fetch("/merchant/ingredients/update", {
  3080. method: "PUT",
  3081. headers: {
  3082. "Content-Type": "application/json;charset=utf-8"
  3083. },
  3084. body: JSON.stringify(data)
  3085. })
  3086. .then(response => response.json())
  3087. .then((response)=>{
  3088. if(typeof(response) === "string"){
  3089. controller.createBanner(response, "error");
  3090. }else{
  3091. for(let i = 0; i < response.length; i++){
  3092. merchant.removeIngredient(merchant.getIngredient(response[i].ingredient._id));
  3093. merchant.addIngredient(response[i].ingredient, response[i].quantity, response[i].defaultUnit);
  3094. }
  3095. controller.createBanner("INGREDIENTS UPDATED", "success");
  3096. }
  3097. })
  3098. .catch((err)=>{
  3099. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  3100. })
  3101. .finally(()=>{
  3102. loader.style.display = "none";
  3103. });
  3104. }
  3105. }
  3106. }
  3107. module.exports = home;
  3108. },{}],22:[function(require,module,exports){
  3109. let ingredients = {
  3110. isPopulated: false,
  3111. ingredients: [],
  3112. display: function(){
  3113. if(!this.isPopulated){
  3114. document.getElementById("ingredientSearch").oninput = ()=>{this.search()};
  3115. this.populateByProperty();
  3116. this.isPopulated = true;
  3117. }
  3118. },
  3119. populateByProperty: function(){
  3120. let categories;
  3121. categories = merchant.categorizeIngredients();
  3122. let ingredientStrand = document.getElementById("categoryList");
  3123. let categoryTemplate = document.getElementById("categoryDiv").content.children[0];
  3124. let ingredientTemplate = document.getElementById("ingredient").content.children[0];
  3125. this.ingredients = [];
  3126. while(ingredientStrand.children.length > 0){
  3127. ingredientStrand.removeChild(ingredientStrand.firstChild);
  3128. }
  3129. for(let i = 0; i < categories.length; i++){
  3130. let categoryDiv = categoryTemplate.cloneNode(true);
  3131. categoryDiv.children[0].children[0].innerText = categories[i].name.toUpperCase();
  3132. categoryDiv.children[0].onclick = ()=>{
  3133. this.toggleCategory(categoryDiv.children[1], categoryDiv.children[0].children[1]);
  3134. };
  3135. categoryDiv.children[1].style.display = "none";
  3136. ingredientStrand.appendChild(categoryDiv);
  3137. for(let j = 0; j < categories[i].ingredients.length; j++){
  3138. let ingredient = categories[i].ingredients[j];
  3139. let ingredientDiv = ingredientTemplate.cloneNode(true);
  3140. ingredientDiv.children[0].innerText = ingredient.ingredient.name;
  3141. ingredientDiv.onclick = ()=>{
  3142. controller.openSidebar("ingredientDetails", ingredient);
  3143. ingredientDiv.classList.add("active");
  3144. };
  3145. ingredientDiv._name = ingredient.ingredient.name.toLowerCase();
  3146. ingredientDiv._unit = ingredient.ingredient.unit.toLowerCase();
  3147. ingredientDiv.children[2].innerText = `${ingredient.quantity.toFixed(2)} ${ingredient.ingredient.unit.toUpperCase()}`;
  3148. categoryDiv.children[1].appendChild(ingredientDiv);
  3149. this.ingredients.push(ingredientDiv);
  3150. }
  3151. }
  3152. },
  3153. displayIngredientsOnly: function(ingredients){
  3154. let ingredientDiv = document.getElementById("categoryList");
  3155. while(ingredientDiv.children.length > 0){
  3156. ingredientDiv.removeChild(ingredientDiv.firstChild);
  3157. }
  3158. for(let i = 0; i < ingredients.length; i++){
  3159. ingredientDiv.appendChild(ingredients[i]);
  3160. }
  3161. },
  3162. toggleCategory: function(div, button){
  3163. if(div.style.display === "none"){
  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="18 15 12 9 6 15"></polyline></svg>';
  3165. div.style.display = "flex";
  3166. }else if(div.style.display === "flex"){
  3167. 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>';
  3168. div.style.display = "none";
  3169. }
  3170. },
  3171. search: function(){
  3172. let input = document.getElementById("ingredientSearch").value.toLowerCase();
  3173. if(input === ""){
  3174. this.populateByProperty();
  3175. return;
  3176. }
  3177. let matchingIngredients = [];
  3178. for(let i = 0; i < this.ingredients.length; i++){
  3179. if(this.ingredients[i]._name.includes(input)){
  3180. matchingIngredients.push(this.ingredients[i]);
  3181. }
  3182. }
  3183. this.displayIngredientsOnly(matchingIngredients);
  3184. }
  3185. }
  3186. module.exports = ingredients;
  3187. },{}],23:[function(require,module,exports){
  3188. let orders = {
  3189. display: function(){
  3190. document.getElementById("orderFilterBtn").addEventListener("click", ()=>{controller.openSidebar("orderFilter")});
  3191. document.getElementById("newOrderBtn").addEventListener("click", ()=>{controller.openSidebar("newOrder")});
  3192. document.getElementById("orderCalcBtn").addEventListener("click", ()=>{controller.openSidebar("orderCalculator")});
  3193. let orderList = document.getElementById("orderList");
  3194. let template = document.getElementById("order").content.children[0];
  3195. while(orderList.children.length > 0){
  3196. orderList.removeChild(orderList.firstChild);
  3197. }
  3198. for(let i = 0; i < merchant.orders.length; i++){
  3199. let orderDiv = template.cloneNode(true);
  3200. orderDiv.order = merchant.orders[i];
  3201. orderDiv.children[0].innerText = merchant.orders[i].name;
  3202. orderDiv.children[1].innerText = `${merchant.orders[i].ingredients.length} ingredients`;
  3203. orderDiv.children[2].innerText = merchant.orders[i].date.toLocaleDateString("en-US");
  3204. orderDiv.children[3].innerText = `$${merchant.orders[i].getTotalCost().toFixed(2)}`;
  3205. orderDiv.onclick = ()=>{
  3206. controller.openSidebar("orderDetails", merchant.orders[i]);
  3207. orderDiv.classList.add("active");
  3208. }
  3209. orderList.appendChild(orderDiv);
  3210. }
  3211. },
  3212. getOrders: function(Order){
  3213. let loader = document.getElementById("loaderContainer");
  3214. loader.style.display = "flex";
  3215. let to = new Date();
  3216. let from = new Date(to.getFullYear(), to.getMonth(), to.getDate() - 30);
  3217. from.setHours(0, 0, 0, 0);
  3218. let body = {
  3219. to: to.toUTCString(),
  3220. from: from.toUTCString(),
  3221. ingredients: []};
  3222. return fetch("/orders/get", {
  3223. method: "post",
  3224. body: JSON.stringify(body),
  3225. headers: {
  3226. "Content-Type": "application/json;charset=utf-8"
  3227. }
  3228. })
  3229. .then(response => response.json())
  3230. .then((response)=>{
  3231. if(typeof(response) === "string"){
  3232. controller.createBanner(response, "error");
  3233. }else{
  3234. let orders = [];
  3235. for(let i = 0; i < response.length; i++){
  3236. orders.push(new Order(
  3237. response[i]._id,
  3238. response[i].name,
  3239. response[i].date,
  3240. response[i].taxes,
  3241. response[i].fees,
  3242. response[i].ingredients,
  3243. merchant
  3244. ));
  3245. }
  3246. if(merchant.orders.length === 0){
  3247. merchant.setOrders(orders);
  3248. }
  3249. return orders;
  3250. }
  3251. })
  3252. .catch((err)=>{
  3253. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  3254. })
  3255. .finally(()=>{
  3256. loader.style.display = "none";
  3257. });
  3258. }
  3259. }
  3260. module.exports = orders;
  3261. },{}],24:[function(require,module,exports){
  3262. let recipeBook = {
  3263. isPopulated: false,
  3264. recipeDivList: [],
  3265. display: function(Recipe){
  3266. if(!this.isPopulated){
  3267. this.populateRecipes();
  3268. if(merchant.pos !== "none"){
  3269. document.getElementById("posUpdateRecipe").onclick = ()=>{this.posUpdate(Recipe)};
  3270. }
  3271. document.getElementById("recipeSearch").oninput = ()=>{this.search()};
  3272. this.populateRecipes();
  3273. this.isPopulated = true;
  3274. }
  3275. },
  3276. populateRecipes: function(){
  3277. let recipeList = document.getElementById("recipeList");
  3278. let template = document.getElementById("recipe").content.children[0];
  3279. this.recipeDivList = [];
  3280. while(recipeList.children.length > 0){
  3281. recipeList.removeChild(recipeList.firstChild);
  3282. }
  3283. for(let i = 0; i < merchant.recipes.length; i++){
  3284. let recipeDiv = template.cloneNode(true);
  3285. recipeDiv.onclick = ()=>{
  3286. controller.openSidebar("recipeDetails", merchant.recipes[i]);
  3287. recipeDiv.classList.add("active");
  3288. }
  3289. recipeDiv._name = merchant.recipes[i].name;
  3290. recipeList.appendChild(recipeDiv);
  3291. recipeDiv.children[0].innerText = merchant.recipes[i].name;
  3292. recipeDiv.children[1].innerText = `$${merchant.recipes[i].price.toFixed(2)}`;
  3293. this.recipeDivList.push(recipeDiv);
  3294. }
  3295. },
  3296. search: function(){
  3297. let input = document.getElementById("recipeSearch").value.toLowerCase();
  3298. let recipeList = document.getElementById("recipeList");
  3299. let matchingRecipes = [];
  3300. for(let i = 0; i < this.recipeDivList.length; i++){
  3301. if(this.recipeDivList[i]._name.toLowerCase().includes(input)){
  3302. matchingRecipes.push(this.recipeDivList[i]);
  3303. }
  3304. }
  3305. while(recipeList.children.length > 0){
  3306. recipeList.removeChild(recipeList.firstChild);
  3307. }
  3308. for(let i = 0; i < matchingRecipes.length; i++){
  3309. recipeList.appendChild(matchingRecipes[i]);
  3310. }
  3311. },
  3312. posUpdate: function(Recipe){
  3313. let loader = document.getElementById("loaderContainer");
  3314. loader.style.display = "flex";
  3315. let url = `/recipe/update/${merchant.pos}`;
  3316. fetch(url, {
  3317. method: "GET",
  3318. headers: {
  3319. "Content-Type": "application/json;charset=utf-8"
  3320. },
  3321. })
  3322. .then(response => response.json())
  3323. .then((response)=>{
  3324. if(typeof(response) === "string"){
  3325. controller.createBanner(response, "error");
  3326. }else{
  3327. for(let i = 0; i < response.new.length; i++){
  3328. const recipe = new Recipe(
  3329. response.new[i]._id,
  3330. response.new[i].name,
  3331. response.new[i].price,
  3332. merchant,
  3333. []
  3334. );
  3335. merchant.addRecipe(recipe);
  3336. }
  3337. for(let i = 0; i < response.removed.length; i++){
  3338. for(let j = 0; j < merchant.recipes.length; j++){
  3339. if(merchant.recipes[j].id === response.removed[i]._id){
  3340. merchant.removeRecipe(merchant.recipes[j]);
  3341. break;
  3342. }
  3343. }
  3344. }
  3345. controller.createBanner("RECIPES SUCCESSFULLY UPDATED", "success");
  3346. this.display();
  3347. }
  3348. })
  3349. .catch((err)=>{
  3350. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  3351. })
  3352. .finally(()=>{
  3353. loader.style.display = "none";
  3354. });
  3355. }
  3356. }
  3357. module.exports = recipeBook;
  3358. },{}],25:[function(require,module,exports){
  3359. let transactions = {
  3360. transactions: [],
  3361. display: function(){
  3362. document.getElementById("filterTransactionsButton").onclick = ()=>{controller.openSidebar("transactionFilter")};
  3363. document.getElementById("newTransactionButton").onclick = ()=>{controller.openSidebar("newTransaction")};
  3364. this.populateTransactions(this.transactions);
  3365. this.isPopulated = true;
  3366. },
  3367. populateTransactions: function(transactions){
  3368. let transactionsList = document.getElementById("transactionsList");
  3369. let template = document.getElementById("transaction").content.children[0];
  3370. while(transactionsList.children.length > 0){
  3371. transactionsList.removeChild(transactionsList.firstChild);
  3372. }
  3373. let i = 0;
  3374. while(i < transactions.length && i < 100){
  3375. let transactionDiv = template.cloneNode(true);
  3376. let transaction = transactions[i];
  3377. transactionDiv.onclick = ()=>{
  3378. controller.openSidebar("transactionDetails", transaction);
  3379. transactionDiv.classList.add("active");
  3380. }
  3381. transactionsList.appendChild(transactionDiv);
  3382. let totalRecipes = 0;
  3383. let totalPrice = 0;
  3384. for(let j = 0; j < transactions[i].recipes.length; j++){
  3385. totalRecipes += transactions[i].recipes[j].quantity;
  3386. totalPrice += transactions[i].recipes[j].recipe.price * transactions[i].recipes[j].quantity;
  3387. }
  3388. transactionDiv.children[0].innerText = transactions[i].date.toLocaleDateString();
  3389. transactionDiv.children[1].innerText = `${totalRecipes} recipes sold`;
  3390. transactionDiv.children[2].innerText = `$${totalPrice.toFixed(2)}`;
  3391. i++;
  3392. }
  3393. }
  3394. }
  3395. module.exports = transactions;
  3396. },{}]},{},[6]);