home.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. window.homeStrandObj = {
  2. isPopulated: false,
  3. graph: {},
  4. display: function(){
  5. if(!this.isPopulated){
  6. let today = new Date();
  7. let firstOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
  8. let firstOfLastMonth = new Date(today.getFullYear(), today.getMonth() - 1, 1);
  9. let lastMonthtoDay = new Date(new Date().setMonth(today.getMonth() - 1));
  10. let revenueThisMonth = this.calculateRevenue(dateIndices(firstOfMonth));
  11. let revenueLastmonthToDay = this.calculateRevenue(dateIndices(firstOfLastMonth, lastMonthtoDay));
  12. document.querySelector("#revenue").innerText = `$${revenueThisMonth.toLocaleString("en")}`;
  13. let revenueChange = ((revenueThisMonth - revenueLastmonthToDay) / revenueLastmonthToDay) * 100;
  14. let img = "";
  15. if(revenueChange >= 0){
  16. img = "/shared/images/upArrow.png";
  17. }else{
  18. img = "/shared/images/downArrow.png";
  19. }
  20. document.querySelector("#revenueChange p").innerText = `${Math.abs(revenueChange).toFixed(2)}% vs last month`;
  21. document.querySelector("#revenueChange img").src = img;
  22. this.graph = new LineGraph(
  23. document.querySelector("#graphCanvas"),
  24. "$",
  25. "Date"
  26. )
  27. let thirtyAgo = new Date(today);
  28. thirtyAgo.setDate(today.getDate() - 29);
  29. this.graph.addData(
  30. this.graphData(dateIndices(thirtyAgo)),
  31. [thirtyAgo, new Date()],
  32. "Revenue"
  33. );
  34. let ul = document.querySelector("#inventoryCheckCard ul");
  35. for(let i = 0; i < 5; i++){
  36. let li = document.createElement("li");
  37. li.innerText = merchant.inventory[i].ingredient.name;
  38. ul.appendChild(li);
  39. }
  40. this.isPopulated = true;
  41. }
  42. },
  43. calculateRevenue: function(indices){
  44. let total = 0;
  45. for(let i = indices[0]; i <= indices[1]; i++){
  46. for(let recipe of transactions[i].recipes){
  47. for(let merchRecipe of merchant.recipes){
  48. if(recipe.recipe === merchRecipe._id){
  49. total += recipe.quantity * merchRecipe.price;
  50. }
  51. }
  52. }
  53. }
  54. return total / 100;
  55. },
  56. graphData: function(indices){
  57. let dataList = new Array(30).fill(0);
  58. let currentDate = transactions[indices[0]].date;
  59. let arrayIndex = 0;
  60. for(let i = indices[0]; i <= indices[1]; i++){
  61. if(transactions[i].date.getDate() !== currentDate.getDate()){
  62. currentDate = transactions[i].date;
  63. arrayIndex++;
  64. }
  65. for(let recipe of transactions[i].recipes){
  66. for(let merchRecipe of merchant.recipes){
  67. if(recipe.recipe === merchRecipe._id){
  68. dataList[arrayIndex] = parseFloat((dataList[arrayIndex] + (recipe.quantity * merchRecipe.price) / 100).toFixed(2));
  69. break;
  70. }
  71. }
  72. }
  73. }
  74. return dataList;
  75. }
  76. }