home.js 8.5 KB

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