bundle.js 141 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000
  1. (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
  2. class Ingredient{
  3. constructor(id, name, category, unitType, unit, parent, 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. quantity += recipe.ingredients[j].quantity * this._recipes[i].quantity;
  1100. break;
  1101. }
  1102. }
  1103. }
  1104. return quantity;
  1105. }
  1106. }
  1107. module.exports = Transaction;
  1108. },{}],6:[function(require,module,exports){
  1109. const home = require("./strands/home.js");
  1110. const ingredients = require("./strands/ingredients.js");
  1111. const recipeBook = require("./strands/recipeBook.js");
  1112. const analytics = require("./strands/analytics.js");
  1113. const orders = require("./strands/orders.js");
  1114. const transactions = require("./strands/transactions.js");
  1115. const ingredientDetails = require("./sidebars/ingredientDetails.js");
  1116. const newIngredient = require("./sidebars/newIngredient.js");
  1117. const editIngredient = require("./sidebars/editIngredient.js");
  1118. const newOrder = require("./sidebars/newOrder.js");
  1119. const newRecipe = require("./sidebars/newRecipe.js");
  1120. const editRecipe = require("./sidebars/editRecipe.js");
  1121. const newTransaction = require("./sidebars/newTransaction.js");
  1122. const orderDetails = require("./sidebars/orderDetails.js");
  1123. const orderFilter = require("./sidebars/orderFilter.js");
  1124. const orderCalculator = require("./sidebars/orderCalculator.js");
  1125. const recipeDetails = require("./sidebars/recipeDetails.js");
  1126. const transactionDetails = require("./sidebars/transactionDetails.js");
  1127. const transactionFilter = require("./sidebars/transactionFilter.js");
  1128. const Merchant = require("./classes/Merchant.js");
  1129. const Ingredient = require("./classes/Ingredient.js");
  1130. const Recipe = require("./classes/Recipe.js");
  1131. const Order = require("./classes/Order.js");
  1132. const Transaction = require("./classes/Transaction.js");
  1133. merchant = new Merchant(data.merchant, data.transactions, {
  1134. home: home,
  1135. ingredients: ingredients,
  1136. transactions: transactions,
  1137. recipeBook: recipeBook,
  1138. analytics: analytics,
  1139. orders: orders,
  1140. Ingredient: Ingredient,
  1141. Recipe: Recipe,
  1142. Transaction: Transaction
  1143. });
  1144. controller = {
  1145. openStrand: function(strand, data = undefined){
  1146. this.closeSidebar();
  1147. let strands = document.querySelectorAll(".strand");
  1148. for(let i = 0; i < strands.length; i++){
  1149. strands[i].style.display = "none";
  1150. }
  1151. let buttons = document.querySelectorAll(".menuButton");
  1152. for(let i = 0; i < buttons.length - 1; i++){
  1153. buttons[i].classList = "menuButton";
  1154. buttons[i].disabled = false;
  1155. }
  1156. let activeButton = {};
  1157. switch(strand){
  1158. case "home":
  1159. activeButton = document.getElementById("homeBtn");
  1160. document.getElementById("homeStrand").style.display = "flex";
  1161. home.display();
  1162. break;
  1163. case "ingredients":
  1164. activeButton = document.getElementById("ingredientsBtn");
  1165. document.getElementById("ingredientsStrand").style.display = "flex";
  1166. ingredients.display();
  1167. break;
  1168. case "recipeBook":
  1169. activeButton = document.getElementById("recipeBookBtn");
  1170. document.getElementById("recipeBookStrand").style.display = "flex";
  1171. recipeBook.display(Recipe);
  1172. break;
  1173. case "analytics":
  1174. activeButton = document.getElementById("analyticsBtn");
  1175. document.getElementById("analyticsStrand").style.display = "flex";
  1176. analytics.display(Transaction);
  1177. break;
  1178. case "orders":
  1179. activeButton = document.getElementById("ordersBtn");
  1180. document.getElementById("ordersStrand").style.display = "flex";
  1181. orders.orders = data;
  1182. orders.display(Order);
  1183. break;
  1184. case "transactions":
  1185. activeButton = document.getElementById("transactionsBtn");
  1186. document.getElementById("transactionsStrand").style.display = "flex";
  1187. transactions.transactions = data;
  1188. transactions.display(Transaction);
  1189. break;
  1190. }
  1191. activeButton.classList = "menuButton active";
  1192. activeButton.disabled = true;
  1193. if(window.screen.availWidth <= 1000){
  1194. this.closeMenu();
  1195. }
  1196. },
  1197. /*
  1198. Open a specific sidebar
  1199. Input:
  1200. sidebar: the outermost element of the sidebar (must contain class sidebar)
  1201. */
  1202. openSidebar: function(sidebar, data = {}){
  1203. this.closeSidebar();
  1204. document.getElementById("sidebarDiv").classList = "sidebar";
  1205. document.getElementById(sidebar).style.display = "flex";
  1206. switch(sidebar){
  1207. case "ingredientDetails":
  1208. ingredientDetails.display(data, ingredients);
  1209. break;
  1210. case "newIngredient":
  1211. newIngredient.display(Ingredient);
  1212. break;
  1213. case "editIngredient":
  1214. editIngredient.display(data);
  1215. break;
  1216. case "recipeDetails":
  1217. recipeDetails.display(data);
  1218. break;
  1219. case "editRecipe":
  1220. editRecipe.display(data);
  1221. break;
  1222. case "addRecipe":
  1223. newRecipe.display(Recipe);
  1224. break;
  1225. case "orderDetails":
  1226. orderDetails.display(data);
  1227. break;
  1228. case "orderFilter":
  1229. orderFilter.display(Order);
  1230. break;
  1231. case "newOrder":
  1232. newOrder.display();
  1233. break;
  1234. case "orderCalculator":
  1235. orderCalculator.display();
  1236. break;
  1237. case "transactionDetails":
  1238. transactionDetails.display(data);
  1239. break;
  1240. case "transactionFilter":
  1241. transactionFilter.display();
  1242. break;
  1243. case "newTransaction":
  1244. newTransaction.display(Transaction);
  1245. break;
  1246. }
  1247. if(window.screen.availWidth <= 1000){
  1248. document.querySelector(".contentBlock").style.display = "none";
  1249. document.getElementById("mobileMenuSelector").style.display = "none";
  1250. document.getElementById("sidebarCloser").style.display = "block";
  1251. }
  1252. },
  1253. closeSidebar: function(){
  1254. let sidebar = document.getElementById("sidebarDiv");
  1255. for(let i = 0; i < sidebar.children.length; i++){
  1256. if(sidebar.children[i].style.display !== "none"){
  1257. sidebar.children[i].style.display = "none";
  1258. let choosables = [];
  1259. switch(sidebar.children[i].id){
  1260. case "ingredientDetails":
  1261. choosables = document.querySelectorAll(".ingredient");
  1262. break;
  1263. case "transactionDetails":
  1264. choosables = document.getElementById("transactionsList").children;
  1265. break;
  1266. case "recipeDetails":
  1267. choosables = document.getElementById("recipeList").children;
  1268. break;
  1269. case "orderDetails":
  1270. choosables = document.getElementById("orderList").children;
  1271. break;
  1272. }
  1273. for(let i = 0; i < choosables.length; i++){
  1274. choosables[i].classList.remove("active");
  1275. }
  1276. }
  1277. }
  1278. sidebar.classList = "sidebarHide";
  1279. if(window.screen.availWidth <= 1000){
  1280. document.querySelector(".contentBlock").style.display = "flex";
  1281. document.getElementById("mobileMenuSelector").style.display = "block";
  1282. document.getElementById("sidebarCloser").style.display = "none";
  1283. }
  1284. },
  1285. openModal: function(str){
  1286. let modal = document.getElementById("modal");
  1287. modal.style.display = "flex";
  1288. document.getElementById("modalClose").addEventListener("click", this.closeModal);
  1289. let content = {};
  1290. switch(str){
  1291. case "ingredientSpreadsheet":
  1292. content = document.getElementById("modalSpreadsheetUpload");
  1293. content.style.display = "flex";
  1294. document.getElementById("modalSpreadsheetTitle").innerText = "ingredients";
  1295. document.getElementById("spreadsheetDownload").href = "/ingredients/download/spreadsheet";
  1296. content.onsubmit = newIngredient.submitSpreadsheet;
  1297. break;
  1298. case "recipeSpreadsheet":
  1299. content = document.getElementById("modalSpreadsheetUpload");
  1300. content.style.display = "flex";
  1301. document.getElementById("modalSpreadsheetTitle").innerText = "recipes";
  1302. document.getElementById("spreadsheetDownload").href = "/recipes/download/spreadsheet";
  1303. content.onsubmit = newRecipe.submitSpreadsheet;
  1304. break;
  1305. case "orderSpreadsheet":
  1306. content = document.getElementById("modalSpreadsheetUpload");
  1307. content.style.display = "flex";
  1308. document.getElementById("modalSpreadsheetTitle").innerText = "orders";
  1309. document.getElementById("spreadsheetDownload").href = "/orders/download/spreadsheet";
  1310. content.onsubmit = newOrder.submitSpreadsheet;
  1311. break;
  1312. case "transactionSpreadsheet":
  1313. content = document.getElementById("modalSpreadsheetUpload");
  1314. content.style.display = "flex";
  1315. document.getElementById("modalSpreadsheetTitle").innerText = "transactions";
  1316. document.getElementById("spreadsheetDownload").href = "/transactions/download/spreadsheet";
  1317. content.onsubmit = newTransaction.submitSpreadsheet;
  1318. }
  1319. },
  1320. closeModal: function(){
  1321. let modal = document.getElementById("modal");
  1322. let modalContent = document.getElementById("modalContent");
  1323. for(let i = 0; i < modalContent.children.length; i++){
  1324. modalContent.children[i].style.display = "none";
  1325. }
  1326. modal.style.display = "none";
  1327. },
  1328. createBanner: function(text, status){
  1329. let container = document.getElementById("bannerContainer");
  1330. let template = document.getElementById("banner").content.children[0];
  1331. let banner = template.cloneNode(true);
  1332. switch(status){
  1333. case "error":
  1334. banner.children[0].style.backgroundColor = "rgb(200, 0, 0)";
  1335. banner.children[0].children[0].style.display = "block";
  1336. break;
  1337. case "alert":
  1338. banner.children[0].style.backgroundColor = "rgb(230, 210, 0)";
  1339. banner.children[0].children[1].style.display = "block";
  1340. break;
  1341. case "success":
  1342. banner.children[0].style.backgroundColor = "rgb(0, 145, 55)";
  1343. banner.children[0].children[2].style.display = "block";
  1344. break;
  1345. }
  1346. banner.children[1].innerText = text;
  1347. container.appendChild(banner);
  1348. let timer = setTimeout(()=>{
  1349. container.removeChild(banner);
  1350. }, 10000);
  1351. banner.children[2].addEventListener("click", ()=>{
  1352. container.removeChild(banner);
  1353. clearTimeout(timer);
  1354. });
  1355. },
  1356. changeMenu: function(){
  1357. let menu = document.querySelector(".menu");
  1358. let buttons = document.querySelectorAll(".menuButton");
  1359. if(!menu.classList.contains("menuMinimized")){
  1360. menu.classList = "menu menuMinimized";
  1361. for(let i = 0; i < buttons.length; i++){
  1362. buttons[i].children[1].style.display = "none";
  1363. }
  1364. document.getElementById("max").style.display = "none";
  1365. document.getElementById("min").style.display = "flex";
  1366. }else if(menu.classList.contains("menuMinimized")){
  1367. menu.classList = "menu";
  1368. for(let i = 0; i < buttons.length; i++){
  1369. buttons[i].children[1].style.display = "block";
  1370. }
  1371. setTimeout(()=>{
  1372. document.getElementById("max").style.display = "flex";
  1373. document.getElementById("min").style.display = "none";
  1374. }, 150);
  1375. }
  1376. },
  1377. openMenu: function(){
  1378. document.getElementById("menu").style.display = "flex";
  1379. document.querySelector(".contentBlock").style.display = "none";
  1380. document.getElementById("mobileMenuSelector").onclick = ()=>{this.closeMenu()};
  1381. },
  1382. closeMenu: function(){
  1383. document.getElementById("menu").style.display = "none";
  1384. document.querySelector(".contentBlock").style.display = "flex";
  1385. document.getElementById("mobileMenuSelector").onclick = ()=>{this.openMenu()};
  1386. },
  1387. updateAnalytics: function(){
  1388. analytics.isPopulated = false;
  1389. }
  1390. }
  1391. if(window.screen.availWidth > 1000 && window.screen.availWidth <= 1400){
  1392. this.changeMenu();
  1393. document.getElementById("menuShifter2").style.display = "none";
  1394. }
  1395. //Add click listeners for menu buttons
  1396. document.getElementById("homeBtn").onclick = ()=>{controller.openStrand("home")};
  1397. document.getElementById("ingredientsBtn").onclick = ()=>{controller.openStrand("ingredients")};
  1398. document.getElementById("recipeBookBtn").onclick = ()=>{controller.openStrand("recipeBook")};
  1399. document.getElementById("analyticsBtn").onclick = ()=>{controller.openStrand("analytics")};
  1400. document.getElementById("ordersBtn").onclick = async ()=>{
  1401. if(merchant.orders.length === 0){
  1402. merchant.setOrders(await orders.getOrders(Order));
  1403. }
  1404. controller.openStrand("orders", merchant.orders);
  1405. }
  1406. document.getElementById("transactionsBtn").onclick = ()=>{controller.openStrand("transactions", merchant.getTransactions())};
  1407. controller.openStrand("home");
  1408. },{"./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){
  1409. let editIngredient = {
  1410. display: function(ingredient){
  1411. let buttonList = document.getElementById("unitButtons");
  1412. let quantLabel = document.getElementById("editIngQuantityLabel");
  1413. let specialLabel = document.getElementById("editSpecialLabel");
  1414. //Clear any existing data
  1415. while(buttonList.children.length > 0){
  1416. buttonList.removeChild(buttonList.firstChild);
  1417. }
  1418. //Populate basic fields
  1419. document.getElementById("editIngTitle").innerText = ingredient.ingredient.name;
  1420. document.getElementById("editIngName").value = ingredient.ingredient.name;
  1421. document.getElementById("editIngCategory").value = ingredient.ingredient.category;
  1422. quantLabel.innerText = `CURRENT STOCK (${ingredient.ingredient.unit.toUpperCase()})`;
  1423. document.getElementById("editIngSubmit").onclick = ()=>{this.submit(ingredient)};
  1424. //Populate the unit buttons
  1425. const units = merchant.units[ingredient.ingredient.unitType];
  1426. for(let i = 0; i < units.length; i++){
  1427. let button = document.createElement("button");
  1428. button.classList.add("unitButton");
  1429. button.innerText = units[i].toUpperCase();
  1430. button.onclick = ()=>{this.changeUnit(button)};
  1431. buttonList.appendChild(button);
  1432. if(units[i] === ingredient.ingredient.unit){
  1433. button.classList.add("unitActive");
  1434. }
  1435. }
  1436. //Make any changes for special ingredients
  1437. if(ingredient.ingredient.specialUnit === "bottle"){
  1438. quantLabel.innerText = "CURRENT STOCK (BOTTLES):";
  1439. specialLabel.style.display = "flex";
  1440. specialLabel.innerText = `BOTTLE SIZE (${ingredient.ingredient.unit.toUpperCase()}):`;
  1441. let sizeInput = document.createElement("input");
  1442. sizeInput.id = "editIngSpecialSize";
  1443. sizeInput.type = "number";
  1444. sizeInput.min = "0";
  1445. sizeInput.step = "0.01";
  1446. sizeInput.value = ingredient.ingredient.unitSize.toFixed(2);
  1447. specialLabel.appendChild(sizeInput);
  1448. }else{
  1449. specialLabel.style.display = "none";
  1450. }
  1451. let quantInput = document.createElement("input");
  1452. quantInput.id = "editIngQuantity";
  1453. quantInput.type = "number";
  1454. quantInput.min = "0";
  1455. quantInput.step = "0.01";
  1456. quantInput.value = ingredient.quantity.toFixed(2);
  1457. quantLabel.appendChild(quantInput);
  1458. },
  1459. changeUnit(button){
  1460. let buttons = document.getElementById("unitButtons");
  1461. for(let i = 0; i < buttons.children.length; i++){
  1462. buttons.children[i].classList.remove("unitActive");
  1463. }
  1464. button.classList.add("unitActive");
  1465. },
  1466. submit(ingredient){
  1467. const quantity = parseFloat(document.getElementById("editIngQuantityLabel").children[0].value);
  1468. let data = {
  1469. id: ingredient.ingredient.id,
  1470. name: document.getElementById("editIngName").value,
  1471. category: document.getElementById("editIngCategory").value
  1472. }
  1473. //Add data based on unit type
  1474. if(ingredient.ingredient.specialUnit === "bottle"){
  1475. let unitSize = ingredient.convertToBase(parseFloat(document.getElementById("editSpecialLabel").children[0].value));
  1476. data.quantity = quantity * unitSize;
  1477. data.unitSize = unitSize;
  1478. }else{
  1479. data.quantity = quantity;
  1480. }
  1481. //Get the measurement unit
  1482. let units = document.getElementById("unitButtons");
  1483. for(let i = 0; i < units.children.length; i++){
  1484. if(units.children[i].classList.contains("unitActive")){
  1485. data.unit = units.children[i].innerText.toLowerCase();
  1486. break;
  1487. }
  1488. }
  1489. let loader = document.getElementById("loaderContainer");
  1490. loader.style.display = "flex";
  1491. fetch("/ingredients/update", {
  1492. method: "put",
  1493. headers: {
  1494. "Content-Type": "application/json;charset=utf-8"
  1495. },
  1496. body: JSON.stringify(data)
  1497. })
  1498. .then(response => response.json())
  1499. .then((response)=>{
  1500. if(typeof(response) === "string"){
  1501. controller.createBanner(response, "error");
  1502. }else{
  1503. merchant.removeIngredient(merchant.getIngredient(response.ingredient._id));
  1504. merchant.addIngredient(response.ingredient, response.quantity, response.unit);
  1505. controller.openStrand("ingredients");
  1506. controller.createBanner("INGREDIENT UPDATED", "success");
  1507. }
  1508. })
  1509. .catch((err)=>{
  1510. controller.createBanner("SOMETHING WENT WRONG, PLEASE REFRESH THE PAGE", "error");
  1511. })
  1512. .finally(()=>{
  1513. loader.style.display = "none";
  1514. });
  1515. }
  1516. }
  1517. module.exports = editIngredient;
  1518. },{}],8:[function(require,module,exports){
  1519. let editRecipe = {
  1520. display: function(recipe){
  1521. let nameInput = document.getElementById("editRecipeName");
  1522. if(merchant.pos === "none"){
  1523. nameInput.value = recipe.name;
  1524. }else{
  1525. document.getElementById("editRecipeNoName").innertext = recipe.name;
  1526. nameInput.parentNode.style.display = "none";
  1527. }
  1528. //Populate ingredients
  1529. let ingredientList = document.getElementById("editRecipeIngList");
  1530. while(ingredientList.children.length > 0){
  1531. ingredientList.removeChild(ingredientList.firstChild);
  1532. }
  1533. let template = document.getElementById("editRecipeIng").content.children[0];
  1534. for(let i = 0; i < recipe.ingredients.length; i++){
  1535. let ingredientDiv = template.cloneNode(true);
  1536. ingredientDiv.children[0].onclick = ()=>{ingredientDiv.parentNode.removeChild(ingredientDiv)};
  1537. ingredientDiv.children[1].innerText = recipe.ingredients[i].ingredient.getNameAndUnit();
  1538. ingredientDiv.children[2].style.display = "none";
  1539. ingredientDiv.children[3].value = recipe.ingredients[i].quantity;
  1540. ingredientDiv.ingredient = recipe.ingredients[i];
  1541. ingredientList.appendChild(ingredientDiv);
  1542. }
  1543. document.getElementById("addRecIng").onclick = ()=>{this.newIngredient()};
  1544. document.getElementById("editRecipePrice").value = recipe.price;
  1545. document.getElementById("editRecipeSubmit").onclick = ()=>{this.submit(recipe)};
  1546. document.getElementById("editRecipeCancel").onclick = ()=>{controller.openSidebar("recipeDetails", recipe)};
  1547. },
  1548. newIngredient: function(){
  1549. let ingredientList = document.getElementById("editRecipeIngList");
  1550. let ingredientDiv = document.getElementById("editRecipeIng").content.children[0].cloneNode(true);
  1551. ingredientDiv.children[0].onclick = ()=>{ingredientDiv.parentNode.removeChild(ingredientDiv)};
  1552. ingredientDiv.children[1].style.display = "none";
  1553. ingredientDiv.children[3].value = "0.00";
  1554. //Populate selector
  1555. let categories = merchant.categorizeIngredients();
  1556. for(let i = 0; i < categories.length; i++){
  1557. let group = document.createElement("optgroup");
  1558. group.label = categories[i].name;
  1559. for(let j = 0; j < categories[i].ingredients.length; j++){
  1560. let option = document.createElement("option");
  1561. option.innerText = categories[i].ingredients[j].ingredient.getNameAndUnit();
  1562. option.ingredient = categories[i].ingredients[j];
  1563. group.appendChild(option);
  1564. }
  1565. ingredientDiv.children[2].appendChild(group);
  1566. }
  1567. ingredientList.appendChild(ingredientDiv);
  1568. },
  1569. submit: function(recipe){
  1570. let data = {
  1571. id: recipe.id,
  1572. name: recipe.name,
  1573. price: document.getElementById("editRecipePrice").value * 100,
  1574. ingredients: []
  1575. }
  1576. if(merchant.pos === "none"){
  1577. data.name = document.getElementById("editRecipeName").value;
  1578. }
  1579. let ingredients = document.getElementById("editRecipeIngList").children;
  1580. for(let i = 0; i < ingredients.length; i++){
  1581. const quantity = parseFloat(ingredients[i].children[3].value);
  1582. let newIngredient = {};
  1583. let ingredient = {};
  1584. if(ingredients[i].children[1].style.display === "none"){
  1585. let selector = ingredients[i].children[2];
  1586. ingredient = selector.options[selector.selectedIndex].ingredient;
  1587. newIngredient = {
  1588. ingredient: ingredient.ingredient.id,
  1589. quantity: ingredient.convertToBase(quantity)
  1590. };
  1591. }else{
  1592. ingredient = ingredients[i].ingredient;
  1593. newIngredient = {
  1594. ingredient: ingredient.ingredient.id,
  1595. quantity: ingredients[i].ingredient.convertToBase(quantity)
  1596. };
  1597. }
  1598. if(ingredient.ingredient.specialUnit === "bottle"){
  1599. newIngredient.quantity = quantity;
  1600. }
  1601. data.ingredients.push(newIngredient);
  1602. }
  1603. let loader = document.getElementById("loaderContainer");
  1604. loader.style.display = "flex";
  1605. fetch("/recipe/update", {
  1606. method: "put",
  1607. headers: {
  1608. "Content-Type": "application/json;charset=utf-8"
  1609. },
  1610. body: JSON.stringify(data)
  1611. })
  1612. .then(response => response.json())
  1613. .then((response)=>{
  1614. if(typeof(response) === "string"){
  1615. controller.createBanner(response, "error");
  1616. }else{
  1617. merchant.updateRecipe(response);
  1618. controller.openStrand("recipeBook");
  1619. controller.createBanner("RECIPE UPDATED", "success");
  1620. }
  1621. })
  1622. .catch((err)=>{
  1623. controller.createBanner("SOMETHING WENT WRONG, PLEASE REFRESH THE PAGE", "error");
  1624. })
  1625. .finally(()=>{
  1626. loader.style.display = "none";
  1627. });
  1628. }
  1629. }
  1630. module.exports = editRecipe;
  1631. },{}],9:[function(require,module,exports){
  1632. let ingredientDetails = {
  1633. dailyUse: 0,
  1634. display: function(ingredient){
  1635. document.getElementById("editIngBtn").onclick = ()=>{controller.openSidebar("editIngredient", ingredient)};
  1636. document.getElementById("removeIngBtn").onclick = ()=>{this.remove(ingredient)};
  1637. document.getElementById("ingredientDetailsCategory").innerText = ingredient.ingredient.category;
  1638. document.getElementById("ingredientDetailsName").innerText = ingredient.ingredient.name;
  1639. document.getElementById("ingredientStock").innerText = ingredient.getQuantityDisplay();
  1640. //Calculate and display average daily use
  1641. let quantities = [];
  1642. let now = new Date();
  1643. for(let i = 1; i < 31; i++){
  1644. let endDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i)
  1645. let startDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i - 1);
  1646. quantities.push(merchant.getSingleIngredientSold(ingredient, startDay, endDay));
  1647. }
  1648. let sum = 0;
  1649. for(let i = 0; i < quantities.length; i++){
  1650. sum += quantities[i];
  1651. }
  1652. let dailyUse = sum / quantities.length;
  1653. const dailyUseDiv = document.getElementById("dailyUse");
  1654. if(ingredient.ingredient.specialUnit === "bottle"){
  1655. dailyUseDiv.innerText = `${dailyUse.toFixed(2)} BOTTLES`;
  1656. }else{
  1657. dailyUseDiv.innerText = `${dailyUse.toFixed(2)} ${ingredient.ingredient.unit.toUpperCase()}`;
  1658. }
  1659. //Show recipes that this ingredient is a part of
  1660. let recipeList = document.getElementById("ingredientRecipeList");
  1661. let template = document.getElementById("ingredientRecipe").content.children[0];
  1662. let recipes = merchant.getRecipesForIngredient(ingredient.ingredient);
  1663. while(recipeList.children.length > 0){
  1664. recipeList.removeChild(recipeList.firstChild);
  1665. }
  1666. for(let i = 0; i < recipes.length; i++){
  1667. let recipeDiv = template.cloneNode(true);
  1668. recipeDiv.children[0].innerText = recipes[i].name;
  1669. recipeDiv.onclick = ()=>{
  1670. controller.openStrand("recipeBook");
  1671. controller.openSidebar("recipeDetails", recipes[i]);
  1672. }
  1673. recipeDiv.classList.add("choosable");
  1674. recipeList.appendChild(recipeDiv);
  1675. }
  1676. },
  1677. remove: function(ingredient){
  1678. for(let i = 0; i < merchant.recipes.length; i++){
  1679. for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
  1680. if(ingredient.ingredient === merchant.recipes[i].ingredients[j].ingredient){
  1681. controller.createBanner("MUST REMOVE INGREDIENT FROM ALL RECIPES BEFORE REMOVING FROM INVENTORY", "error");
  1682. return;
  1683. }
  1684. }
  1685. }
  1686. let loader = document.getElementById("loaderContainer");
  1687. loader.style.display = "flex";
  1688. fetch(`/ingredients/remove/${ingredient.ingredient.id}`, {
  1689. method: "delete",
  1690. })
  1691. .then((response) => response.json())
  1692. .then((response)=>{
  1693. if(typeof(response) === "string"){
  1694. controller.createBanner(response, "error");
  1695. }else{
  1696. merchant.removeIngredient(ingredient);
  1697. controller.openStrand("ingredients");
  1698. controller.createBanner("INGREDIENT REMOVED", "success");
  1699. }
  1700. })
  1701. .catch((err)=>{
  1702. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  1703. })
  1704. .finally(()=>{
  1705. loader.style.display = "none";
  1706. });
  1707. }
  1708. }
  1709. module.exports = ingredientDetails;
  1710. },{}],10:[function(require,module,exports){
  1711. let newIngredient = {
  1712. display: function(Ingredient){
  1713. const selector = document.getElementById("unitSelector");
  1714. document.getElementById("newIngName").value = "";
  1715. document.getElementById("newIngCategory").value = "";
  1716. document.getElementById("newIngQuantity").value = 0;
  1717. document.getElementById("bottleSizeLabel").style.display = "none";
  1718. selector.value = "g";
  1719. selector.onchange = ()=>{this.unitChange()};
  1720. document.getElementById("submitNewIng").onclick = ()=>{this.submit(Ingredient)};
  1721. document.getElementById("ingredientFileUpload").addEventListener("click", ()=>{controller.openModal("ingredientSpreadsheet")});
  1722. },
  1723. unitChange: function(){
  1724. const select = document.getElementById("unitSelector");
  1725. const bottleLabel = document.getElementById("bottleSizeLabel");
  1726. if(select.value === "bottle"){
  1727. bottleLabel.style.display = "block";
  1728. }else{
  1729. bottleLabel.style.display = "none";
  1730. }
  1731. },
  1732. submit: function(){
  1733. let unitSelector = document.getElementById("unitSelector");
  1734. let options = document.querySelectorAll("#unitSelector option");
  1735. const quantityValue = parseFloat(document.getElementById("newIngQuantity").value);
  1736. let unit = unitSelector.value;
  1737. let newIngredient = {
  1738. ingredient: {
  1739. name: document.getElementById("newIngName").value,
  1740. category: document.getElementById("newIngCategory").value,
  1741. unitType: options[unitSelector.selectedIndex].getAttribute("type")
  1742. },
  1743. quantity: quantityValue,
  1744. defaultUnit: unit
  1745. }
  1746. //Change the ingredient if it is a special unit type (ie "bottle")
  1747. if(unit === "bottle"){
  1748. newIngredient.ingredient.unitType = "volume";
  1749. newIngredient.ingredient.unitSize = document.getElementById("bottleSize").value;
  1750. newIngredient.defaultUnit = document.getElementById("bottleUnits").value;
  1751. newIngredient.ingredient.specialUnit = unit;
  1752. newIngredient.quantity = quantityValue;
  1753. }
  1754. let loader = document.getElementById("loaderContainer");
  1755. loader.style.display = "flex";
  1756. fetch("/ingredients/create", {
  1757. method: "POST",
  1758. headers: {
  1759. "Content-Type": "application/json;charset=utf-8"
  1760. },
  1761. body: JSON.stringify(newIngredient)
  1762. })
  1763. .then((response) => response.json())
  1764. .then((response)=>{
  1765. if(typeof(response) === "string"){
  1766. controller.createBanner(response, "error");
  1767. }else{
  1768. merchant.addIngredient(response.ingredient, response.quantity, response.defaultUnit);
  1769. controller.openStrand("ingredients");
  1770. controller.createBanner("INGREDIENT CREATED", "success");
  1771. }
  1772. })
  1773. .catch((err)=>{
  1774. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  1775. })
  1776. .finally(()=>{
  1777. loader.style.display = "none";
  1778. });
  1779. },
  1780. submitSpreadsheet: function(){
  1781. event.preventDefault();
  1782. controller.closeModal();
  1783. const file = document.getElementById("spreadsheetInput").files[0];
  1784. let data = new FormData();
  1785. data.append("ingredients", file);
  1786. let loader = document.getElementById("loaderContainer");
  1787. loader.style.display = "flex";
  1788. fetch("/ingredients/create/spreadsheet", {
  1789. method: "post",
  1790. body: data
  1791. })
  1792. .then(response => response.json())
  1793. .then((response)=>{
  1794. if(typeof(response) === "string"){
  1795. controller.createBanner(response, "error");
  1796. }else{
  1797. for(let i = 0; i < response.length; i++){
  1798. merchant.addIngredient(response[i].ingredient, response[i].quantity, response[i].defaultUnit);
  1799. controller.createBanner("INGREDINETS SUCCESSFULLY ADDED", "success");
  1800. controller.openStrand("ingredients");
  1801. }
  1802. }
  1803. })
  1804. .catch((err)=>{
  1805. controller.createBanner("SOMETHING WENT WRONG. TRY REFRESHING THE PAGE", "error");
  1806. })
  1807. .finally(()=>{
  1808. loader.style.display = "none";
  1809. });
  1810. }
  1811. }
  1812. module.exports = newIngredient;
  1813. },{}],11:[function(require,module,exports){
  1814. let newOrder = {
  1815. display: function(){
  1816. document.getElementById("sidebarDiv").classList.add("sidebarWide");
  1817. document.getElementById("newOrderIngredientList").style.display = "flex";
  1818. document.getElementById("orderFileUpload").addEventListener("click", ()=>{controller.openModal("orderSpreadsheet")});
  1819. let selectedList = document.getElementById("selectedIngredientList");
  1820. while(selectedList.children.length > 0){
  1821. selectedList.removeChild(selectedList.firstChild);
  1822. }
  1823. let ingredientList = document.getElementById("newOrderIngredients");
  1824. while(ingredientList.children.length > 0){
  1825. ingredientList.removeChild(ingredientList.firstChild);
  1826. }
  1827. for(let i = 0; i < merchant.ingredients.length; i++){
  1828. let ingredient = document.createElement("button");
  1829. ingredient.classList = "choosable";
  1830. ingredient.innerText = merchant.ingredients[i].ingredient.name;
  1831. ingredient.onclick = ()=>{this.addIngredient(merchant.ingredients[i], ingredient)};
  1832. ingredientList.appendChild(ingredient);
  1833. }
  1834. document.getElementById("submitNewOrder").onclick = ()=>{this.submit()};
  1835. },
  1836. addIngredient: function(ingredient, element){
  1837. element.style.display = "none";
  1838. let div = document.getElementById("selectedIngredient").content.children[0].cloneNode(true);
  1839. div.ingredient = ingredient;
  1840. div.children[0].children[1].onclick = ()=>{this.removeIngredient(div, element)};
  1841. //TODO: this should be handled by the class
  1842. //Display units depending on the whether it is a special unit
  1843. if(ingredient.ingredient.specialUnit === "bottle"){
  1844. div.children[0].children[0].innerText = `${ingredient.ingredient.name} (BOTTLES)`;
  1845. }else{
  1846. div.children[0].children[0].innerText = `${ingredient.ingredient.name} (${ingredient.ingredient.unit.toUpperCase()})`;
  1847. }
  1848. document.getElementById("selectedIngredientList").appendChild(div);
  1849. },
  1850. removeIngredient: function(selectedElement, element){
  1851. selectedElement.parentElement.removeChild(selectedElement);
  1852. element.style.display = "block";
  1853. },
  1854. submit: function(){
  1855. let date = document.getElementById("newOrderDate").value;
  1856. let taxes = document.getElementById("orderTaxes").value * 100;
  1857. let fees = document.getElementById("orderFees").value * 100;
  1858. let ingredients = document.getElementById("selectedIngredientList").children;
  1859. if(date === ""){
  1860. controller.createBanner("DATE IS REQUIRED FOR ORDERS", "error");
  1861. return;
  1862. }
  1863. let data = {
  1864. name: document.getElementById("newOrderName").value,
  1865. date: date,
  1866. taxes: taxes,
  1867. fees: fees,
  1868. ingredients: []
  1869. }
  1870. for(let i = 0; i < ingredients.length; i++){
  1871. let quantity = ingredients[i].children[1].children[0].value;
  1872. let price = ingredients[i].children[1].children[1].value;
  1873. if(ingredients[i].ingredient.ingredient.specialUnit === "bottle"){
  1874. data.ingredients.push({
  1875. ingredient: ingredients[i].ingredient.ingredient.id,
  1876. quantity: quantity * ingredients[i].ingredient.ingredient.getBaseUnitSize(),
  1877. pricePerUnit: this.convertPrice(ingredients[i].ingredient.ingredient, price * 100)
  1878. });
  1879. }else{
  1880. data.ingredients.push({
  1881. ingredient: ingredients[i].ingredient.ingredient.id,
  1882. quantity: ingredients[i].ingredient.convertToBase(quantity),
  1883. pricePerUnit: this.convertPrice(ingredients[i].ingredient.ingredient, price * 100)
  1884. });
  1885. }
  1886. }
  1887. let loader = document.getElementById("loaderContainer");
  1888. loader.style.display = "flex";
  1889. fetch("/order/create", {
  1890. method: "post",
  1891. headers: {
  1892. "Content-Type": "application/json;charset=utf-8"
  1893. },
  1894. body: JSON.stringify(data)
  1895. })
  1896. .then((response)=>response.json())
  1897. .then((response)=>{
  1898. if(typeof(response) === "string"){
  1899. controller.createBanner(response, "error");
  1900. }else{
  1901. merchant.addOrder(response, true);
  1902. controller.openStrand("orders", merchant.orders);
  1903. controller.createBanner("NEW ORDER CREATED", "success");
  1904. }
  1905. })
  1906. .catch((err)=>{
  1907. controller.createBanner("SOMETHING WENT WRONG, PLEASE REFRESH THE PAGE", "error");
  1908. })
  1909. .finally(()=>{
  1910. loader.style.display = "none";
  1911. });
  1912. },
  1913. //TODO: Remove this function, it should be on the order
  1914. convertPrice: function(ingredient, price){
  1915. if(ingredient.specialUnit === "bottle"){
  1916. return price / ingredient.getBaseUnitSize();
  1917. }
  1918. switch(ingredient.unit){
  1919. case "g": return price;
  1920. case "kg": return price / 1000;
  1921. case "oz": return price / 28.3495;
  1922. case "lb": return price / 453.5924;
  1923. case "ml": return price * 1000;
  1924. case "l": return price;
  1925. case "tsp": return price * 202.8842;
  1926. case "tbsp": return price * 67.6278;
  1927. case "ozfl": return price * 33.8141;
  1928. case "cup": return price * 4.1667;
  1929. case "pt": return price * 2.1134;
  1930. case "qt": return price * 1.0567;
  1931. case "gal": return price / 3.7854;
  1932. case "mm": return price * 1000;
  1933. case "cm": return price * 100;
  1934. case "m": return price;
  1935. case "in": return price * 39.3701;
  1936. case "ft": return price * 3.2808;
  1937. }
  1938. },
  1939. submitSpreadsheet: function(){
  1940. event.preventDefault();
  1941. controller.closeModal();
  1942. const file = document.getElementById("spreadsheetInput").files[0];
  1943. let data = new FormData();
  1944. data.append("orders", file);
  1945. data.append("timeOffset", new Date().getTimezoneOffset());
  1946. let loader = document.getElementById("loaderContainer");
  1947. loader.style.display = "flex";
  1948. fetch("/orders/create/spreadsheet", {
  1949. method: "post",
  1950. body: data
  1951. })
  1952. .then(response => response.json())
  1953. .then((response)=>{
  1954. if(typeof(response) === "string"){
  1955. controller.createBanner(response, "error");
  1956. }else{
  1957. for(let i = 0; i < response.length; i++){
  1958. merchant.addOrder(response[i], true);
  1959. }
  1960. controller.createBanner("ORDER CREATED AND INGREDIENTS UPDATED SUCCESSFULLY", "success");
  1961. controller.openStrand("orders");
  1962. }
  1963. })
  1964. .catch((err)=>{
  1965. controller.createBanner("UNABLE TO DISPLAY NEW ORDER. PLEASE REFRESH THE PAGE.", "error");
  1966. })
  1967. .finally(()=>{
  1968. loader.style.display = "none";
  1969. });
  1970. }
  1971. }
  1972. module.exports = newOrder;
  1973. },{}],12:[function(require,module,exports){
  1974. let newRecipe = {
  1975. display: function(Recipe){
  1976. document.getElementById("newRecipeName").value = "";
  1977. document.getElementById("newRecipePrice").value = "";
  1978. document.getElementById("ingredientCount").value = 1;
  1979. let categories = merchant.categorizeIngredients();
  1980. let ingredientsSelect = document.getElementById("recipeInputIngredients");
  1981. while(ingredientsSelect.children.length > 0){
  1982. ingredientsSelect.removeChild(ingredientsSelect.firstChild);
  1983. }
  1984. this.changeIngredientCount(categories);
  1985. document.getElementById("ingredientCount").onchange = ()=>{this.changeIngredientCount(categories)};
  1986. document.getElementById("submitNewRecipe").onclick = ()=>{this.submit(Recipe)};
  1987. document.getElementById("recipeFileUpload").onclick = ()=>{controller.openModal("recipeSpreadsheet")};
  1988. },
  1989. //Updates the number of ingredient inputs displayed for new recipes
  1990. changeIngredientCount: function(categories){
  1991. let newCount = document.getElementById("ingredientCount").value;
  1992. let ingredientsDiv = document.getElementById("recipeInputIngredients");
  1993. let template = document.getElementById("recipeInputIngredient").content.children[0];
  1994. let oldCount = ingredientsDiv.children.length;
  1995. if(newCount > oldCount){
  1996. let newDivs = newCount - oldCount;
  1997. for(let i = 0; i < newDivs; i++){
  1998. let newNode = template.cloneNode(true);
  1999. newNode.children[0].innnerText = `INGREDIENT ${i + oldCount}`;
  2000. newNode.children[2].children[0].value = 0;
  2001. for(let j = 0; j < categories.length; j++){
  2002. let optgroup = document.createElement("optgroup");
  2003. optgroup.label = categories[j].name;
  2004. for(let k = 0; k < categories[j].ingredients.length; k++){
  2005. let option = document.createElement("option");
  2006. option.innerText = categories[j].ingredients[k].ingredient.getNameAndUnit();
  2007. option.ingredient = categories[j].ingredients[k];
  2008. optgroup.appendChild(option);
  2009. }
  2010. newNode.children[1].children[0].appendChild(optgroup);
  2011. }
  2012. ingredientsDiv.appendChild(newNode);
  2013. }
  2014. for(let i = 0; i < newCount; i++){
  2015. ingredientsDiv.children[i].children[0].innerText = `INGREDIENT ${i + 1}`;
  2016. }
  2017. }else if(newCount < oldCount){
  2018. let newDivs = oldCount - newCount;
  2019. for(let i = 0; i < newDivs; i++){
  2020. ingredientsDiv.removeChild(ingredientsDiv.children[ingredientsDiv.children.length-1]);
  2021. }
  2022. }
  2023. },
  2024. submit: function(Recipe){
  2025. let newRecipe = {
  2026. name: document.getElementById("newRecipeName").value,
  2027. price: document.getElementById("newRecipePrice").value,
  2028. ingredients: []
  2029. }
  2030. let inputs = document.getElementById("recipeInputIngredients").children;
  2031. for(let i = 0; i < inputs.length; i++){
  2032. let sel = inputs[i].children[1].children[0];
  2033. let ingredient = sel.options[sel.selectedIndex].ingredient;
  2034. let newIngredient = {
  2035. ingredient: ingredient.ingredient.id,
  2036. quantity: ingredient.convertToBase(inputs[i].children[2].children[0].value)
  2037. };
  2038. if(ingredient.ingredient.specialUnit === "bottle"){
  2039. newIngredient.quantity = inputs[i].children[2].children[0].value;
  2040. }
  2041. newRecipe.ingredients.push(newIngredient);
  2042. }
  2043. let loader = document.getElementById("loaderContainer");
  2044. loader.style.display = "flex";
  2045. fetch("/recipe/create", {
  2046. method: "POST",
  2047. headers: {
  2048. "Content-Type": "application/json;charset=utf-8"
  2049. },
  2050. body: JSON.stringify(newRecipe)
  2051. })
  2052. .then((response) => response.json())
  2053. .then((response)=>{
  2054. if(typeof(response) === "string"){
  2055. controller.createBanner(response, "error");
  2056. }else{
  2057. let ingredients = [];
  2058. for(let i = 0; i < response.ingredients.length; i++){
  2059. for(let j = 0; j < merchant.ingredients.length; j++){
  2060. if(merchant.ingredients[j].ingredient.id === response.ingredients[i].ingredient){
  2061. ingredients.push({
  2062. ingredient: merchant.ingredients[j].ingredient.id,
  2063. quantity: response.ingredients[i].quantity
  2064. });
  2065. break;
  2066. }
  2067. }
  2068. }
  2069. merchant.addRecipe(
  2070. response._id,
  2071. response.name,
  2072. response.price,
  2073. ingredients
  2074. );
  2075. controller.createBanner("RECIPE CREATED", "success");
  2076. controller.openStrand("recipeBook");
  2077. }
  2078. })
  2079. .catch((err)=>{
  2080. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  2081. })
  2082. .finally(()=>{
  2083. loader.style.display = "none";
  2084. });
  2085. },
  2086. submitSpreadsheet: function(){
  2087. event.preventDefault();
  2088. controller.closeModal();
  2089. const file = document.getElementById("spreadsheetInput").files[0];
  2090. let data = new FormData();
  2091. data.append("recipes", file);
  2092. let loader = document.getElementById("loaderContainer");
  2093. loader.style.display = "flex";
  2094. fetch("/recipes/create/spreadsheet", {
  2095. method: "post",
  2096. body: data
  2097. })
  2098. .then(response => response.json())
  2099. .then((response)=>{
  2100. if(typeof(response) === "string"){
  2101. controller.createBanner(response, "error");
  2102. }else{
  2103. for(let i = 0; i < response.length; i++){
  2104. merchant.addRecipe(
  2105. response[i]._id,
  2106. response[i].name,
  2107. response[i].price,
  2108. response[i].ingredients
  2109. );
  2110. }
  2111. controller.createBanner("ALL INGREDIENTS SUCCESSFULLY CREATED", "success");
  2112. controller.openStrand("recipeBook");
  2113. }
  2114. })
  2115. .catch((err)=>{
  2116. controller.createBanner("UNABLE TO DISPLAY NEW RECIPES. PLEASE REFRESH THE PAGE", "error");
  2117. })
  2118. .finally(()=>{
  2119. loader.style.display = "none";
  2120. });
  2121. }
  2122. }
  2123. module.exports = newRecipe;
  2124. },{}],13:[function(require,module,exports){
  2125. let newTransaction = {
  2126. display: function(Transaction){
  2127. let recipeList = document.getElementById("newTransactionRecipes");
  2128. let template = document.getElementById("createTransaction").content.children[0];
  2129. document.getElementById("transactionFileUpload").addEventListener("click", ()=>{controller.openModal("transactionSpreadsheet")});
  2130. while(recipeList.children.length > 0){
  2131. recipeList.removeChild(recipeList.firstChild);
  2132. }
  2133. for(let i = 0; i < merchant.recipes.length; i++){
  2134. let recipeDiv = template.cloneNode(true);
  2135. recipeDiv.recipe = merchant.recipes[i];
  2136. recipeList.appendChild(recipeDiv);
  2137. recipeDiv.children[0].innerText = merchant.recipes[i].name;
  2138. }
  2139. document.getElementById("submitNewTransaction").onclick = ()=>{this.submit(Transaction)};
  2140. },
  2141. submit: function(Transaction){
  2142. let recipeDivs = document.getElementById("newTransactionRecipes");
  2143. let date = document.getElementById("newTransactionDate").valueAsDate;
  2144. if(date > new Date()){
  2145. controller.createBanner("CANNOT HAVE A DATE IN THE FUTURE", "error");
  2146. return;
  2147. }
  2148. let data = {
  2149. date: date,
  2150. recipes: [],
  2151. ingredientUpdates: {}
  2152. };
  2153. for(let i = 0; i < recipeDivs.children.length; i++){
  2154. let quantity = recipeDivs.children[i].children[1].value;
  2155. const recipe = recipeDivs.children[i].recipe;
  2156. if(quantity !== "" && quantity > 0){
  2157. data.recipes.push({
  2158. recipe: recipe.id,
  2159. quantity: quantity
  2160. });
  2161. for(let j = 0; j < recipe.ingredients.length; j++){
  2162. let ingredient = recipe.ingredients[j];
  2163. if(data.ingredientUpdates[ingredient.ingredient.id]){
  2164. data.ingredientUpdates[ingredient.ingredient.id] += ingredient.convertToBase(ingredient.quantity) * quantity;
  2165. }else{
  2166. data.ingredientUpdates[ingredient.ingredient.id] = ingredient.convertToBase(ingredient.quantity) * quantity;
  2167. }
  2168. }
  2169. }else if(quantity < 0){
  2170. controller.createBanner("CANNOT HAVE NEGATIVE VALUES", "error");
  2171. return;
  2172. }
  2173. }
  2174. if(data.recipes.length > 0){
  2175. let loader = document.getElementById("loaderContainer");
  2176. loader.style.display = "flex";
  2177. fetch("/transaction/create", {
  2178. method: "post",
  2179. headers: {
  2180. "Content-Type": "application/json;charset=utf-8"
  2181. },
  2182. body: JSON.stringify(data)
  2183. })
  2184. .then(response => response.json())
  2185. .then((response)=>{
  2186. if(typeof(response) === "string"){
  2187. controller.createBanner(response, "error");
  2188. }else{
  2189. merchant.addTransaction(response);
  2190. controller.updateAnalytics();
  2191. controller.openStrand("transactions", merchant.getTransactions());
  2192. controller.createBanner("TRANSACTION CREATED", "success");
  2193. }
  2194. })
  2195. .catch((err)=>{
  2196. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  2197. })
  2198. .finally(()=>{
  2199. loader.style.display = "none";
  2200. });
  2201. }
  2202. },
  2203. submitSpreadsheet: function(){
  2204. event.preventDefault();
  2205. controller.closeModal();
  2206. const file = document.getElementById("spreadsheetInput").files[0];
  2207. let data = new FormData();
  2208. data.append("transactions", file);
  2209. data.append("timeOffset", new Date().getTimezoneOffset());
  2210. let loader = document.getElementById("loaderContainer");
  2211. loader.style.display = "flex";
  2212. fetch("/transactions/create/spreadsheet", {
  2213. method: "post",
  2214. body: data
  2215. })
  2216. .then(response => response.json())
  2217. .then((response)=>{
  2218. if(typeof(response) === "string"){
  2219. controller.createBanner(response, "error");
  2220. }else{
  2221. for(let i = 0; i < response.recipes.length; i++){
  2222. response.recipes[i].recipe = response.recipes[i].recipe._id;
  2223. }
  2224. merchant.addTransaction(response);
  2225. controller.updateAnalytics();
  2226. controller.openStrand("transactions", merchant.transactions);
  2227. controller.createBanner("TRANSACTION SUCCESSFULLY CREATED. INGREDIENTS UPDATED", "success");
  2228. }
  2229. })
  2230. .catch((err)=>{
  2231. controller.createBanner("UNABLE TO DISPLAY NEW TRANSACTIONS. PLEASE REFRESH THE PAGE", "error");
  2232. })
  2233. .finally(()=>{
  2234. loader.style.display = "none";
  2235. });
  2236. }
  2237. }
  2238. module.exports = newTransaction;
  2239. },{}],14:[function(require,module,exports){
  2240. let orderCalculator = {
  2241. display: function(){
  2242. let calculatorItems = document.getElementById("calculatorItemsBody");
  2243. let template = document.getElementById("calculatorItem").content.children[0];
  2244. let calculations = this.predict();
  2245. while(calculatorItems.children.length > 0){
  2246. calculatorItems.removeChild(calculatorItems.firstChild);
  2247. }
  2248. for(let i = 0; i < calculations.length; i++){
  2249. let outputString = `${calculations[i].output.toFixed(2)} ${calculations[i].ingredient.unit.toUpperCase()}`
  2250. if(calculations[i].ingredient.specialUnit === "bottle"){
  2251. outputString = `${calculations[i].output.toFixed(2)} BOTTLES`;
  2252. }
  2253. let item = template.cloneNode(true);
  2254. item.children[0].innerText = calculations[i].ingredient.name,
  2255. item.children[1].innerText = outputString;
  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. let yaxis = `QUANTITY (${this.ingredient.unit.toUpperCase()})`;
  2769. if(this.ingredient.specialUnit === "bottle"){
  2770. yaxis = `QUANTITY (${this.ingredient.specialUnit.toUpperCase()})`
  2771. }
  2772. const layout = {
  2773. title: this.ingredient.name.toUpperCase(),
  2774. xaxis: {title: "DATE"},
  2775. yaxis: {title: yaxis}
  2776. }
  2777. Plotly.newPlot("itemUseGraph", [trace], layout);
  2778. //Create min/max/avg
  2779. //Current ingredient is stored on the "analMinUse" element
  2780. let min = quantities[0];
  2781. let max = quantities[0];
  2782. let sum = 0;
  2783. for(let i = 0; i < quantities.length; i++){
  2784. if(quantities[i] < min){
  2785. min = quantities[i];
  2786. }
  2787. if(quantities[i] > max){
  2788. max = quantities[i];
  2789. }
  2790. sum += quantities[i];
  2791. }
  2792. document.getElementById("analMinUse").innerText = `${min.toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2793. document.getElementById("analAvgUse").innerText = `${(sum / quantities.length).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2794. document.getElementById("analMaxUse").innerText = `${max.toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2795. //Create weekday averages
  2796. let dayUse = [0, 0, 0, 0, 0, 0, 0];
  2797. let dayCount = [0, 0, 0, 0, 0, 0, 0];
  2798. for(let i = 0; i < quantities.length; i++){
  2799. dayUse[dates[i].getDay()] += quantities[i];
  2800. dayCount[dates[i].getDay()]++;
  2801. }
  2802. document.getElementById("analDayOne").innerText = `${(dayUse[0] / dayCount[0]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2803. document.getElementById("analDayTwo").innerText = `${(dayUse[1] / dayCount[1]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2804. document.getElementById("analDayThree").innerText = `${(dayUse[2] / dayCount[2]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2805. document.getElementById("analDayFour").innerText = `${(dayUse[3] / dayCount[3]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2806. document.getElementById("analDayFive").innerText = `${(dayUse[4] / dayCount[4]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2807. document.getElementById("analDaySix").innerText = `${(dayUse[5] / dayCount[5]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2808. document.getElementById("analDaySeven").innerText = `${(dayUse[6] / dayCount[6]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  2809. },
  2810. displayRecipe: function(){
  2811. if(this.recipe === undefined || this.transactionsByDate.length === 0){
  2812. return;
  2813. }
  2814. //break down data into dates and quantities
  2815. let dates = [];
  2816. let quantities = [];
  2817. for(let i = 0; i < this.transactionsByDate.length; i++){
  2818. dates.push(this.transactionsByDate[i].date);
  2819. let sum = 0;
  2820. for(let j = 0; j < this.transactionsByDate[i].transactions.length; j++){
  2821. const transaction = this.transactionsByDate[i].transactions[j];
  2822. for(let k = 0; k < transaction.recipes.length; k++){
  2823. if(transaction.recipes[k].recipe === this.recipe){
  2824. sum += transaction.recipes[k].quantity;
  2825. }
  2826. }
  2827. }
  2828. quantities.push(sum);
  2829. }
  2830. //create and display the graph
  2831. const trace = {
  2832. x: dates,
  2833. y: quantities,
  2834. mode: "lines+markers",
  2835. line: {
  2836. color: "rgb(255, 99, 107)"
  2837. }
  2838. }
  2839. const layout = {
  2840. title: this.recipe.name.toUpperCase(),
  2841. xaxis: {title: "DATE"},
  2842. yaxis: {title: "QUANTITY"}
  2843. }
  2844. Plotly.newPlot("recipeSalesGraph", [trace], layout);
  2845. //Display the boxes at the bottom
  2846. //Current recipe is stored on the "recipeAvgUse" element
  2847. let avg = 0;
  2848. for(let i = 0; i < quantities.length; i++){
  2849. avg += quantities[i];
  2850. }
  2851. avg = avg / quantities.length;
  2852. document.getElementById("recipeAvgUse").innerText = avg.toFixed(2);
  2853. document.getElementById("recipeAvgRevenue").innerText = `$${(avg * this.recipe.price).toFixed(2)}`;
  2854. },
  2855. switchDisplay: function(){
  2856. const checkbox = document.getElementById("analSlider");
  2857. let ingredient = document.getElementById("analIngredientContent");
  2858. let recipe = document.getElementById("analRecipeContent");
  2859. if(checkbox.checked === true){
  2860. ingredient.style.display = "none";
  2861. recipe.style.display = "flex";
  2862. this.displayRecipe();
  2863. }else{
  2864. ingredient.style.display = "flex";
  2865. recipe.style.display = "none";
  2866. this.displayIngredient();
  2867. }
  2868. },
  2869. newDates: async function(Transaction){
  2870. const from = document.getElementById("analStartDate").valueAsDate;
  2871. const to = document.getElementById("analEndDate").valueAsDate;
  2872. from.setHours(0, 0, 0, 0);
  2873. to.setDate(to.getDate() + 1);
  2874. to.setHours(0, 0, 0, 0);
  2875. await this.getData(from, to, Transaction);
  2876. if(document.getElementById("analSlider").checked === true){
  2877. this.displayRecipe();
  2878. }else{
  2879. this.displayIngredient();
  2880. }
  2881. }
  2882. }
  2883. module.exports = analytics;
  2884. },{}],21:[function(require,module,exports){
  2885. let home = {
  2886. isPopulated: false,
  2887. display: function(){
  2888. if(!this.isPopulated){
  2889. this.drawRevenueCard();
  2890. this.drawRevenueGraph();
  2891. this.drawInventoryCheckCard();
  2892. this.drawPopularCard();
  2893. this.isPopulated = true;
  2894. }
  2895. },
  2896. drawRevenueCard: function(){
  2897. let today = new Date();
  2898. let firstOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
  2899. let firstOfLastMonth = new Date(today.getFullYear(), today.getMonth() - 1, 1);
  2900. let lastMonthToDay = new Date(new Date().setMonth(today.getMonth() - 1));
  2901. const revenueThisMonth = merchant.getRevenue(firstOfMonth);
  2902. const revenueLastMonthToDay = merchant.getRevenue(firstOfLastMonth, lastMonthToDay);
  2903. document.getElementById("revenue").innerText = `$${revenueThisMonth.toFixed(2)}`;
  2904. let revenueChange = ((revenueThisMonth - revenueLastMonthToDay) / revenueLastMonthToDay) * 100;
  2905. let img = "";
  2906. if(revenueChange >= 0){
  2907. img = "/shared/images/upArrow.png";
  2908. }else{
  2909. img = "/shared/images/downArrow.png";
  2910. }
  2911. document.querySelector("#revenueChange p").innerText = `${Math.abs(revenueChange).toFixed(2)}% vs last month`;
  2912. document.querySelector("#revenueChange img").src = img;
  2913. },
  2914. drawRevenueGraph: function(){
  2915. let monthAgo = new Date();
  2916. monthAgo.setMonth(monthAgo.getMonth() - 1);
  2917. let revenue = [];
  2918. let dates = [];
  2919. let dayRevenue = 0;
  2920. const transactions = merchant.getTransactions(monthAgo);
  2921. let currentDate = (transactions.length > 0) ? transactions[0].date : undefined;
  2922. for(let i = 0; i < transactions.length; i++){
  2923. if(transactions[i].date.getDate() !== currentDate.getDate()){
  2924. revenue.push(dayRevenue / 100);
  2925. dayRevenue = 0;
  2926. dates.push(currentDate);
  2927. currentDate = transactions[i].date;
  2928. }
  2929. for(let j = 0; j < transactions[i].recipes.length; j++){
  2930. const recipe = transactions[i].recipes[j];
  2931. dayRevenue += recipe.recipe.price * recipe.quantity;
  2932. }
  2933. }
  2934. const trace = {
  2935. x: dates,
  2936. y: revenue,
  2937. mode: "lines+markers",
  2938. line: {
  2939. color: "rgb(255, 99, 107)"
  2940. }
  2941. }
  2942. const layout = {
  2943. title: "REVENUE",
  2944. xaxis: {
  2945. title: "DATE"
  2946. },
  2947. yaxis: {
  2948. title: "$"
  2949. }
  2950. }
  2951. Plotly.newPlot("graphCard", [trace], layout);
  2952. },
  2953. drawInventoryCheckCard: function(){
  2954. let num;
  2955. if(merchant.ingredients.length < 5){
  2956. num = merchant.ingredients.length;
  2957. }else{
  2958. num = 5;
  2959. }
  2960. let rands = [];
  2961. for(let i = 0; i < num; i++){
  2962. let rand = Math.floor(Math.random() * merchant.ingredients.length);
  2963. if(rands.includes(rand)){
  2964. i--;
  2965. }else{
  2966. rands[i] = rand;
  2967. }
  2968. }
  2969. let ul = document.querySelector("#inventoryCheckCard ul");
  2970. let template = document.getElementById("ingredientCheck").content.children[0];
  2971. while(ul.children.length > 0){
  2972. ul.removeChild(ul.firstChild);
  2973. }
  2974. for(let i = 0; i < rands.length; i++){
  2975. let ingredientCheck = template.cloneNode(true);
  2976. let input = ingredientCheck.children[1].children[1];
  2977. const ingredient = merchant.ingredients[rands[i]];
  2978. ingredientCheck.ingredient = ingredient;
  2979. ingredientCheck.children[0].innerText = ingredient.ingredient.name;
  2980. ingredientCheck.children[1].children[0].onclick = ()=>{
  2981. input.value--;
  2982. input.changed = true;
  2983. };
  2984. if(ingredient.ingredient.specialUnit === "bottle"){
  2985. input.value = ingredient.quantity.toFixed(2);
  2986. ingredientCheck.children[2].innerText = "BOTTLES";
  2987. }else{
  2988. input.value = ingredient.quantity.toFixed(2);
  2989. ingredientCheck.children[2].innerText = ingredient.ingredient.unit.toUpperCase();
  2990. }
  2991. ingredientCheck.children[1].children[2].onclick = ()=>{
  2992. input.value++;
  2993. input.changed = true;
  2994. }
  2995. input.onchange = ()=>{input.changed = true};
  2996. ul.appendChild(ingredientCheck);
  2997. }
  2998. document.getElementById("inventoryCheck").onclick = ()=>{this.submitInventoryCheck()};
  2999. },
  3000. drawPopularCard: function(){
  3001. let thisMonth = new Date();
  3002. thisMonth.setDate(1);
  3003. const ingredientList = merchant.getIngredientsSold(thisMonth);
  3004. if(ingredientList !== false){
  3005. ingredientList.sort((a, b)=>{
  3006. if(a.quantity < b.quantity){
  3007. return 1;
  3008. }
  3009. if(a.quantity > b.quantity){
  3010. return -1;
  3011. }
  3012. return 0;
  3013. });
  3014. let quantities = [];
  3015. let labels = [];
  3016. let colors = [];
  3017. let count = (ingredientList.length < 5) ? ingredientList.length - 1 : 4;
  3018. for(let i = count; i >= 0; i--){
  3019. const ingredientName = ingredientList[i].ingredient.name;
  3020. const ingredientQuantity = ingredientList[i].quantity;
  3021. const unitName = ingredientList[i].ingredient.unit;
  3022. quantities.push(ingredientList[i].quantity);
  3023. labels.push(`${ingredientName}: ${ingredientQuantity.toFixed(2)} ${unitName.toUpperCase()}`);
  3024. if(i === 0){
  3025. colors.push("rgb(255, 99, 107");
  3026. }else{
  3027. colors.push("rgb(179, 191, 209");
  3028. }
  3029. }
  3030. let trace = {
  3031. x: quantities,
  3032. type: "bar",
  3033. orientation: "h",
  3034. text: labels,
  3035. textposition: "auto",
  3036. hoverinfo: "none",
  3037. marker: {
  3038. color: colors
  3039. }
  3040. }
  3041. let layout = {
  3042. title: "MOST POPULAR INGREDIENTS",
  3043. xaxis: {
  3044. zeroline: false,
  3045. title: "QUANTITY"
  3046. },
  3047. yaxis: {
  3048. showticklabels: false
  3049. }
  3050. }
  3051. Plotly.newPlot("popularIngredientsCard", [trace], layout);
  3052. }else{
  3053. document.getElementById("popularCanvas").style.display = "none";
  3054. let notice = document.createElement("p");
  3055. notice.innerText = "N/A";
  3056. notice.classList = "notice";
  3057. document.getElementById("popularIngredientsCard").appendChild(notice);
  3058. }
  3059. },
  3060. //Need to change the updating of ingredients
  3061. //should update the ingredient directly, then send that. Maybe...
  3062. submitInventoryCheck: function(){
  3063. let lis = document.querySelectorAll("#inventoryCheckCard li");
  3064. let data = [];
  3065. for(let i = 0; i < lis.length; i++){
  3066. if(lis[i].children[1].children[1].value >= 0){
  3067. if(lis[i].children[1].children[1].changed === true){
  3068. let merchIngredient = lis[i].ingredient;
  3069. data.push({
  3070. id: merchIngredient.ingredient.id,
  3071. quantity: lis[i].children[1].children[1].value
  3072. });
  3073. lis[i].children[1].children[1].changed = false;
  3074. }
  3075. }else{
  3076. controller.createBanner("CANNOT HAVE NEGATIVE INGREDIENTS", "error");
  3077. return;
  3078. }
  3079. }
  3080. if(data.length > 0){
  3081. let loader = document.getElementById("loaderContainer");
  3082. loader.style.display = "flex";
  3083. fetch("/merchant/ingredients/update", {
  3084. method: "PUT",
  3085. headers: {
  3086. "Content-Type": "application/json;charset=utf-8"
  3087. },
  3088. body: JSON.stringify(data)
  3089. })
  3090. .then(response => response.json())
  3091. .then((response)=>{
  3092. if(typeof(response) === "string"){
  3093. controller.createBanner(response, "error");
  3094. }else{
  3095. for(let i = 0; i < response.length; i++){
  3096. merchant.removeIngredient(merchant.getIngredient(response[i].ingredient._id));
  3097. merchant.addIngredient(response[i].ingredient, response[i].quantity, response[i].defaultUnit);
  3098. }
  3099. controller.createBanner("INGREDIENTS UPDATED", "success");
  3100. }
  3101. })
  3102. .catch((err)=>{
  3103. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  3104. })
  3105. .finally(()=>{
  3106. loader.style.display = "none";
  3107. });
  3108. }
  3109. }
  3110. }
  3111. module.exports = home;
  3112. },{}],22:[function(require,module,exports){
  3113. let ingredients = {
  3114. isPopulated: false,
  3115. ingredients: [],
  3116. display: function(){
  3117. if(!this.isPopulated){
  3118. document.getElementById("ingredientSearch").oninput = ()=>{this.search()};
  3119. this.populateByProperty();
  3120. this.isPopulated = true;
  3121. }
  3122. },
  3123. populateByProperty: function(){
  3124. let categories;
  3125. categories = merchant.categorizeIngredients();
  3126. let ingredientStrand = document.getElementById("categoryList");
  3127. let categoryTemplate = document.getElementById("categoryDiv").content.children[0];
  3128. let ingredientTemplate = document.getElementById("ingredient").content.children[0];
  3129. this.ingredients = [];
  3130. while(ingredientStrand.children.length > 0){
  3131. ingredientStrand.removeChild(ingredientStrand.firstChild);
  3132. }
  3133. for(let i = 0; i < categories.length; i++){
  3134. let categoryDiv = categoryTemplate.cloneNode(true);
  3135. categoryDiv.children[0].children[0].innerText = categories[i].name.toUpperCase();
  3136. categoryDiv.children[0].onclick = ()=>{
  3137. this.toggleCategory(categoryDiv.children[1], categoryDiv.children[0].children[1]);
  3138. };
  3139. categoryDiv.children[1].style.display = "none";
  3140. ingredientStrand.appendChild(categoryDiv);
  3141. for(let j = 0; j < categories[i].ingredients.length; j++){
  3142. let ingredient = categories[i].ingredients[j];
  3143. let ingredientDiv = ingredientTemplate.cloneNode(true);
  3144. ingredientDiv.children[0].innerText = ingredient.ingredient.name;
  3145. ingredientDiv.onclick = ()=>{
  3146. controller.openSidebar("ingredientDetails", ingredient);
  3147. ingredientDiv.classList.add("active");
  3148. };
  3149. ingredientDiv._name = ingredient.ingredient.name.toLowerCase();
  3150. ingredientDiv._unit = ingredient.ingredient.unit.toLowerCase();
  3151. if(ingredient.ingredient.specialUnit === "bottle"){
  3152. ingredientDiv.children[2].innerText = `${ingredient.quantity.toFixed(2)} BOTTLES`
  3153. }else{
  3154. ingredientDiv.children[2].innerText = `${ingredient.quantity.toFixed(2)} ${ingredient.ingredient.unit.toUpperCase()}`;
  3155. }
  3156. categoryDiv.children[1].appendChild(ingredientDiv);
  3157. this.ingredients.push(ingredientDiv);
  3158. }
  3159. }
  3160. },
  3161. displayIngredientsOnly: function(ingredients){
  3162. let ingredientDiv = document.getElementById("categoryList");
  3163. while(ingredientDiv.children.length > 0){
  3164. ingredientDiv.removeChild(ingredientDiv.firstChild);
  3165. }
  3166. for(let i = 0; i < ingredients.length; i++){
  3167. ingredientDiv.appendChild(ingredients[i]);
  3168. }
  3169. },
  3170. toggleCategory: function(div, button){
  3171. if(div.style.display === "none"){
  3172. 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>';
  3173. div.style.display = "flex";
  3174. }else if(div.style.display === "flex"){
  3175. 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>';
  3176. div.style.display = "none";
  3177. }
  3178. },
  3179. search: function(){
  3180. let input = document.getElementById("ingredientSearch").value.toLowerCase();
  3181. if(input === ""){
  3182. this.populateByProperty();
  3183. return;
  3184. }
  3185. let matchingIngredients = [];
  3186. for(let i = 0; i < this.ingredients.length; i++){
  3187. if(this.ingredients[i]._name.includes(input)){
  3188. matchingIngredients.push(this.ingredients[i]);
  3189. }
  3190. }
  3191. this.displayIngredientsOnly(matchingIngredients);
  3192. }
  3193. }
  3194. module.exports = ingredients;
  3195. },{}],23:[function(require,module,exports){
  3196. let orders = {
  3197. display: function(){
  3198. document.getElementById("orderFilterBtn").addEventListener("click", ()=>{controller.openSidebar("orderFilter")});
  3199. document.getElementById("newOrderBtn").addEventListener("click", ()=>{controller.openSidebar("newOrder")});
  3200. document.getElementById("orderCalcBtn").addEventListener("click", ()=>{controller.openSidebar("orderCalculator")});
  3201. let orderList = document.getElementById("orderList");
  3202. let template = document.getElementById("order").content.children[0];
  3203. while(orderList.children.length > 0){
  3204. orderList.removeChild(orderList.firstChild);
  3205. }
  3206. for(let i = 0; i < merchant.orders.length; i++){
  3207. let orderDiv = template.cloneNode(true);
  3208. orderDiv.order = merchant.orders[i];
  3209. orderDiv.children[0].innerText = merchant.orders[i].name;
  3210. orderDiv.children[1].innerText = `${merchant.orders[i].ingredients.length} ingredients`;
  3211. orderDiv.children[2].innerText = merchant.orders[i].date.toLocaleDateString("en-US");
  3212. orderDiv.children[3].innerText = `$${merchant.orders[i].getTotalCost().toFixed(2)}`;
  3213. orderDiv.onclick = ()=>{
  3214. controller.openSidebar("orderDetails", merchant.orders[i]);
  3215. orderDiv.classList.add("active");
  3216. }
  3217. orderList.appendChild(orderDiv);
  3218. }
  3219. },
  3220. getOrders: function(Order){
  3221. let loader = document.getElementById("loaderContainer");
  3222. loader.style.display = "flex";
  3223. return fetch("/order", {
  3224. method: "get",
  3225. headers: {
  3226. "Content-Type": "application/json;charset=utf-8"
  3227. }
  3228. })
  3229. .then(response => response.json())
  3230. .then((response)=>{
  3231. if(typeof(response) === "string"){
  3232. controller.createBanner(response, "error");
  3233. }else{
  3234. let orders = [];
  3235. for(let i = 0; i < response.length; i++){
  3236. orders.push(new Order(
  3237. response[i]._id,
  3238. response[i].name,
  3239. response[i].date,
  3240. response[i].taxes,
  3241. response[i].fees,
  3242. response[i].ingredients,
  3243. merchant
  3244. ));
  3245. }
  3246. if(merchant.orders.length === 0){
  3247. merchant.setOrders(orders);
  3248. }
  3249. return orders;
  3250. }
  3251. })
  3252. .catch((err)=>{
  3253. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  3254. })
  3255. .finally(()=>{
  3256. loader.style.display = "none";
  3257. });
  3258. }
  3259. }
  3260. module.exports = orders;
  3261. },{}],24:[function(require,module,exports){
  3262. let recipeBook = {
  3263. isPopulated: false,
  3264. recipeDivList: [],
  3265. display: function(Recipe){
  3266. if(!this.isPopulated){
  3267. this.populateRecipes();
  3268. if(merchant.pos !== "none"){
  3269. document.getElementById("posUpdateRecipe").onclick = ()=>{this.posUpdate(Recipe)};
  3270. }
  3271. document.getElementById("recipeSearch").oninput = ()=>{this.search()};
  3272. this.populateRecipes();
  3273. this.isPopulated = true;
  3274. }
  3275. },
  3276. populateRecipes: function(){
  3277. let recipeList = document.getElementById("recipeList");
  3278. let template = document.getElementById("recipe").content.children[0];
  3279. this.recipeDivList = [];
  3280. while(recipeList.children.length > 0){
  3281. recipeList.removeChild(recipeList.firstChild);
  3282. }
  3283. for(let i = 0; i < merchant.recipes.length; i++){
  3284. let recipeDiv = template.cloneNode(true);
  3285. recipeDiv.onclick = ()=>{
  3286. controller.openSidebar("recipeDetails", merchant.recipes[i]);
  3287. recipeDiv.classList.add("active");
  3288. }
  3289. recipeDiv._name = merchant.recipes[i].name;
  3290. recipeList.appendChild(recipeDiv);
  3291. recipeDiv.children[0].innerText = merchant.recipes[i].name;
  3292. recipeDiv.children[1].innerText = `$${merchant.recipes[i].price.toFixed(2)}`;
  3293. this.recipeDivList.push(recipeDiv);
  3294. }
  3295. },
  3296. search: function(){
  3297. let input = document.getElementById("recipeSearch").value.toLowerCase();
  3298. let recipeList = document.getElementById("recipeList");
  3299. let matchingRecipes = [];
  3300. for(let i = 0; i < this.recipeDivList.length; i++){
  3301. if(this.recipeDivList[i]._name.toLowerCase().includes(input)){
  3302. matchingRecipes.push(this.recipeDivList[i]);
  3303. }
  3304. }
  3305. while(recipeList.children.length > 0){
  3306. recipeList.removeChild(recipeList.firstChild);
  3307. }
  3308. for(let i = 0; i < matchingRecipes.length; i++){
  3309. recipeList.appendChild(matchingRecipes[i]);
  3310. }
  3311. },
  3312. posUpdate: function(Recipe){
  3313. let loader = document.getElementById("loaderContainer");
  3314. loader.style.display = "flex";
  3315. let url = `/recipe/update/${merchant.pos}`;
  3316. fetch(url, {
  3317. method: "GET",
  3318. headers: {
  3319. "Content-Type": "application/json;charset=utf-8"
  3320. },
  3321. })
  3322. .then(response => response.json())
  3323. .then((response)=>{
  3324. if(typeof(response) === "string"){
  3325. controller.createBanner(response, "error");
  3326. }else{
  3327. for(let i = 0; i < response.new.length; i++){
  3328. const recipe = new Recipe(
  3329. response.new[i]._id,
  3330. response.new[i].name,
  3331. response.new[i].price,
  3332. merchant,
  3333. []
  3334. );
  3335. merchant.addRecipe(recipe);
  3336. }
  3337. for(let i = 0; i < response.removed.length; i++){
  3338. for(let j = 0; j < merchant.recipes.length; j++){
  3339. if(merchant.recipes[j].id === response.removed[i]._id){
  3340. merchant.removeRecipe(merchant.recipes[j]);
  3341. break;
  3342. }
  3343. }
  3344. }
  3345. controller.createBanner("RECIPES SUCCESSFULLY UPDATED", "success");
  3346. this.display();
  3347. }
  3348. })
  3349. .catch((err)=>{
  3350. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  3351. })
  3352. .finally(()=>{
  3353. loader.style.display = "none";
  3354. });
  3355. }
  3356. }
  3357. module.exports = recipeBook;
  3358. },{}],25:[function(require,module,exports){
  3359. let transactions = {
  3360. transactions: [],
  3361. display: function(Transaction){
  3362. document.getElementById("filterTransactionsButton").onclick = ()=>{controller.openSidebar("transactionFilter")};
  3363. document.getElementById("newTransactionButton").onclick = ()=>{controller.openSidebar("newTransaction")};
  3364. this.populateTransactions(this.transactions);
  3365. this.isPopulated = true;
  3366. },
  3367. populateTransactions: function(transactions){
  3368. let transactionsList = document.getElementById("transactionsList");
  3369. let template = document.getElementById("transaction").content.children[0];
  3370. while(transactionsList.children.length > 0){
  3371. transactionsList.removeChild(transactionsList.firstChild);
  3372. }
  3373. let i = 0;
  3374. while(i < transactions.length && i < 100){
  3375. let transactionDiv = template.cloneNode(true);
  3376. let transaction = transactions[i];
  3377. transactionDiv.onclick = ()=>{
  3378. controller.openSidebar("transactionDetails", transaction);
  3379. transactionDiv.classList.add("active");
  3380. }
  3381. transactionsList.appendChild(transactionDiv);
  3382. let totalRecipes = 0;
  3383. let totalPrice = 0;
  3384. for(let j = 0; j < transactions[i].recipes.length; j++){
  3385. totalRecipes += transactions[i].recipes[j].quantity;
  3386. totalPrice += transactions[i].recipes[j].recipe.price * transactions[i].recipes[j].quantity;
  3387. }
  3388. transactionDiv.children[0].innerText = `${transactions[i].date.toLocaleDateString()} ${transactions[i].date.toLocaleTimeString()}`;
  3389. transactionDiv.children[1].innerText = `${totalRecipes} recipes sold`;
  3390. transactionDiv.children[2].innerText = `$${totalPrice.toFixed(2)}`;
  3391. i++;
  3392. }
  3393. }
  3394. }
  3395. module.exports = transactions;
  3396. },{}]},{},[6]);