bundle.js 131 KB

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