bundle.js 131 KB

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