bundle.js 139 KB

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