home.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. //Inventory Check
  35. let rands = [];
  36. for(let i = 0; i < 5; i++){
  37. let rand = Math.floor(Math.random() * merchant.inventory.length);
  38. if(rands.includes(rand)){
  39. i--;
  40. }else{
  41. rands[i] = rand;
  42. }
  43. }
  44. let ul = document.querySelector("#inventoryCheckCard ul");
  45. for(let rand of rands){
  46. let li = document.createElement("li");
  47. li.classList = "flexRow";
  48. li.ingredientIndex = rand;
  49. ul.appendChild(li);
  50. let name = document.createElement("p");
  51. name.innerText = merchant.inventory[rand].ingredient.name;
  52. li.appendChild(name);
  53. let input = document.createElement("input");
  54. input.type = "number";
  55. input.value = merchant.inventory[rand].quantity;
  56. li.appendChild(input);
  57. let label = document.createElement("p");
  58. label.innerText = merchant.inventory[rand].ingredient.unit;
  59. li.appendChild(label);
  60. }
  61. this.isPopulated = true;
  62. }
  63. },
  64. calculateRevenue: function(indices){
  65. let total = 0;
  66. for(let i = indices[0]; i <= indices[1]; i++){
  67. for(let recipe of transactions[i].recipes){
  68. for(let merchRecipe of merchant.recipes){
  69. if(recipe.recipe === merchRecipe._id){
  70. total += recipe.quantity * merchRecipe.price;
  71. }
  72. }
  73. }
  74. }
  75. return total / 100;
  76. },
  77. graphData: function(indices){
  78. let dataList = new Array(30).fill(0);
  79. let currentDate = transactions[indices[0]].date;
  80. let arrayIndex = 0;
  81. for(let i = indices[0]; i <= indices[1]; i++){
  82. if(transactions[i].date.getDate() !== currentDate.getDate()){
  83. currentDate = transactions[i].date;
  84. arrayIndex++;
  85. }
  86. for(let recipe of transactions[i].recipes){
  87. for(let merchRecipe of merchant.recipes){
  88. if(recipe.recipe === merchRecipe._id){
  89. dataList[arrayIndex] = parseFloat((dataList[arrayIndex] + (recipe.quantity * merchRecipe.price) / 100).toFixed(2));
  90. break;
  91. }
  92. }
  93. }
  94. }
  95. return dataList;
  96. },
  97. updateInventory: function(){
  98. let lis = document.querySelectorAll("#inventoryCheckCard ul li");
  99. let changes = [];
  100. for(let li of lis){
  101. if(li.children[1].value >= 0){
  102. let merchIngredient = merchant.inventory[li.ingredientIndex];
  103. let change = li.children[1].value - merchIngredient.quantity;
  104. if(change !== 0){
  105. changes.push({
  106. id: merchIngredient.ingredient._id,
  107. quantityChange: change
  108. });
  109. }
  110. }else{
  111. banner.createError("Cannot have negative ingredients");
  112. return;
  113. }
  114. }
  115. if(changes.length > 0){
  116. fetch("/merchant/ingredients/update", {
  117. method: "POST",
  118. headers: {
  119. "Content-Type": "application/json;charset=utf-8"
  120. },
  121. body: JSON.stringify(changes)
  122. })
  123. .then((response)=>{
  124. banner.createError("Ingredients updated");
  125. if(typeof(response.data) === "string"){
  126. console.log(err);
  127. }else{
  128. for(let change of changes){
  129. merchant.inventory.find((item)=> item.ingredient._id === change.id).quantity += change.quantityChange;
  130. }
  131. }
  132. })
  133. .catch((err)=>{
  134. console.log(err);
  135. });
  136. }
  137. }
  138. }