bundle.js 132 KB

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