bundle.js 133 KB

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