home.js 6.5 KB

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