bundle.js 133 KB

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