home.js 8.0 KB

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