home.js 6.6 KB

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