bundle.js 135 KB

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