home.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. 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. //Most Popular ingredients
  62. let dataArray = [];
  63. let now = new Date();
  64. let thisMonth = new Date(now.getFullYear(), now.getMonth(), 1);
  65. let ingredientList = ingredientsSold(dateIndices(thisMonth));
  66. for(let i = 0; i < 5; i++){
  67. let max = ingredientList[0].quantity
  68. let index = 0;
  69. for(let j = 0; j < ingredientList.length; j++){
  70. if(ingredientList[j].quantity > max){
  71. max = ingredientList[j].quantity;
  72. index = j;
  73. }
  74. }
  75. dataArray.push({
  76. num: max,
  77. label: `${ingredientList[index].name}: ${ingredientList[index].quantity} ${ingredientList[index].unit}`
  78. });
  79. ingredientList.splice(index, 1);
  80. }
  81. let thisCanvas = document.querySelector("#popularCanvas");
  82. thisCanvas.width = thisCanvas.parentElement.clientWidth;
  83. thisCanvas.height = thisCanvas.parentElement.clientHeight;
  84. let popularGraph = new HorizontalBarGraph(document.querySelector("#popularCanvas"));
  85. popularGraph.addData(dataArray);
  86. this.isPopulated = true;
  87. }
  88. },
  89. calculateRevenue: function(indices){
  90. let total = 0;
  91. for(let i = indices[0]; i <= indices[1]; i++){
  92. for(let recipe of transactions[i].recipes){
  93. for(let merchRecipe of merchant.recipes){
  94. if(recipe.recipe === merchRecipe._id){
  95. total += recipe.quantity * merchRecipe.price;
  96. }
  97. }
  98. }
  99. }
  100. return total / 100;
  101. },
  102. graphData: function(indices){
  103. let dataList = new Array(30).fill(0);
  104. let currentDate = transactions[indices[0]].date;
  105. let arrayIndex = 0;
  106. for(let i = indices[0]; i <= indices[1]; i++){
  107. if(transactions[i].date.getDate() !== currentDate.getDate()){
  108. currentDate = transactions[i].date;
  109. arrayIndex++;
  110. }
  111. for(let recipe of transactions[i].recipes){
  112. for(let merchRecipe of merchant.recipes){
  113. if(recipe.recipe === merchRecipe._id){
  114. dataList[arrayIndex] = parseFloat((dataList[arrayIndex] + (recipe.quantity * merchRecipe.price) / 100).toFixed(2));
  115. break;
  116. }
  117. }
  118. }
  119. }
  120. return dataList;
  121. },
  122. updateInventory: function(){
  123. let lis = document.querySelectorAll("#inventoryCheckCard ul li");
  124. let changes = [];
  125. for(let li of lis){
  126. if(li.children[1].value >= 0){
  127. let merchIngredient = merchant.inventory[li.ingredientIndex];
  128. let change = li.children[1].value - merchIngredient.quantity;
  129. if(change !== 0){
  130. changes.push({
  131. id: merchIngredient.ingredient._id,
  132. quantityChange: change
  133. });
  134. }
  135. }else{
  136. banner.createError("Cannot have negative ingredients");
  137. return;
  138. }
  139. }
  140. if(changes.length > 0){
  141. fetch("/merchant/ingredients/update", {
  142. method: "POST",
  143. headers: {
  144. "Content-Type": "application/json;charset=utf-8"
  145. },
  146. body: JSON.stringify(changes)
  147. })
  148. .then((response)=>{
  149. banner.createError("Ingredients updated");
  150. 1
  151. if(typeof(response.data) === "string"){
  152. console.log(err);
  153. }else{
  154. for(let change of changes){
  155. merchant.inventory.find((item)=> item.ingredient._id === change.id).quantity += change.quantityChange;
  156. }
  157. }
  158. })
  159. .catch((err)=>{
  160. console.log(err);
  161. });
  162. }
  163. }
  164. }