bundle.js 122 KB

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