bundle.js 135 KB

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