bundle.js 141 KB

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