bundle.js 134 KB

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