home.js 8.1 KB

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