bundle.js 134 KB

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