bundle.js 141 KB

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