home.js 8.2 KB

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