home.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. window.homeStrandObj = {
  2. isPopulated: false,
  3. graph: {},
  4. display: function(){
  5. if(!this.isPopulated){
  6. this.drawRevenueCard();
  7. this.drawRevenueGraph();
  8. this.drawInventoryCheckCard();
  9. this.drawPopularCard();
  10. this.isPopulated = true;
  11. }
  12. },
  13. drawRevenueCard: function(){
  14. let today = new Date();
  15. let firstOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
  16. let firstOfLastMonth = new Date(today.getFullYear(), today.getMonth() - 1, 1);
  17. let lastMonthtoDay = new Date(new Date().setMonth(today.getMonth() - 1));
  18. let revenueThisMonth = this.calculateRevenue(dateIndices(firstOfMonth));
  19. let revenueLastmonthToDay = this.calculateRevenue(dateIndices(firstOfLastMonth, lastMonthtoDay));
  20. document.querySelector("#revenue").innerText = `$${revenueThisMonth.toLocaleString("en")}`;
  21. let revenueChange = ((revenueThisMonth - revenueLastmonthToDay) / revenueLastmonthToDay) * 100;
  22. let img = "";
  23. if(revenueChange >= 0){
  24. img = "/shared/images/upArrow.png";
  25. }else{
  26. img = "/shared/images/downArrow.png";
  27. }
  28. document.querySelector("#revenueChange p").innerText = `${Math.abs(revenueChange).toFixed(2)}% vs last month`;
  29. document.querySelector("#revenueChange img").src = img;
  30. },
  31. drawRevenueGraph: function(){
  32. let graphCanvas = document.querySelector("#graphCanvas");
  33. let today = new Date();
  34. graphCanvas.height = graphCanvas.parentElement.clientHeight;
  35. graphCanvas.width = graphCanvas.parentElement.clientWidth;
  36. this.graph = new LineGraph(graphCanvas);
  37. this.graph.addTitle("Revenue");
  38. let thirtyAgo = new Date(today);
  39. thirtyAgo.setDate(today.getDate() - 29);
  40. let data = this.graphData(dateIndices(thirtyAgo));
  41. if(data){
  42. this.graph.addData(
  43. data,
  44. [thirtyAgo, new Date()],
  45. "Revenue"
  46. );
  47. }else{
  48. document.querySelector("#graphCanvas").style.display = "none";
  49. let notice = document.createElement("h1");
  50. notice.innerText = "NO DATA YET";
  51. notice.classList = "notice";
  52. document.querySelector("#graphCard").appendChild(notice);
  53. }
  54. },
  55. drawInventoryCheckCard: function(){
  56. let num;
  57. if(merchant.inventory.length < 5){
  58. num = merchant.inventory.length;
  59. }else{
  60. num = 5;
  61. }
  62. let rands = [];
  63. for(let i = 0; i < num; i++){
  64. let rand = Math.floor(Math.random() * merchant.inventory.length);
  65. if(rands.includes(rand)){
  66. i--;
  67. }else{
  68. rands[i] = rand;
  69. }
  70. }
  71. let ul = document.querySelector("#inventoryCheckCard ul");
  72. while(ul.children.length > 0){
  73. ul.removeChild(ul.firstChild);
  74. }
  75. for(let i = 0; i < rands.length; i++){
  76. let li = document.createElement("li");
  77. li.classList = "flexRow";
  78. li.ingredientIndex = rands[i];
  79. ul.appendChild(li);
  80. let name = document.createElement("p");
  81. name.innerText = merchant.inventory[rands[i]].ingredient.name;
  82. li.appendChild(name);
  83. let input = document.createElement("input");
  84. input.type = "number";
  85. input.value = merchant.inventory[rands[i]].quantity;
  86. li.appendChild(input);
  87. let label = document.createElement("p");
  88. label.innerText = merchant.inventory[rands[i]].ingredient.unit;
  89. li.appendChild(label);
  90. }
  91. },
  92. drawPopularCard: function(){
  93. let dataArray = [];
  94. let now = new Date();
  95. let thisMonth = new Date(now.getFullYear(), now.getMonth(), 1);
  96. let ingredientList = ingredientsSold(dateIndices(thisMonth));
  97. if(ingredientList){
  98. for(let i = 0; i < 5; i++){
  99. let max = ingredientList[0].quantity
  100. let index = 0;
  101. for(let j = 0; j < ingredientList.length; j++){
  102. if(ingredientList[j].quantity > max){
  103. max = ingredientList[j].quantity;
  104. index = j;
  105. }
  106. }
  107. dataArray.push({
  108. num: max,
  109. label: `${ingredientList[index].name}: ${+ingredientList[index].quantity.toFixed(2)} ${ingredientList[index].unit}`
  110. });
  111. ingredientList.splice(index, 1);
  112. }
  113. let thisCanvas = document.querySelector("#popularCanvas");
  114. thisCanvas.width = thisCanvas.parentElement.clientWidth;
  115. thisCanvas.height = thisCanvas.parentElement.clientHeight * 0.75;
  116. let popularGraph = new HorizontalBarGraph(document.querySelector("#popularCanvas"));
  117. popularGraph.addData(dataArray);
  118. }else{
  119. document.querySelector("#popularCanvas").style.display = "none";
  120. let notice = document.createElement("p");
  121. notice.innerText = "N/A";
  122. notice.classList = "notice";
  123. document.querySelector("#popularIngredientsCard").appendChild(notice);
  124. }
  125. },
  126. calculateRevenue: function(indices){
  127. let total = 0;
  128. for(let i = indices[0]; i <= indices[1]; i++){
  129. for(let j = 0; j < transactions[i].recipes.length; j++){
  130. for(let k = 0; k < merchant.recipes.length; k++){
  131. if(transactions[i].recipes[j].recipe === merchant.recipes[k]._id){
  132. total += transactions[i].recipes[j].quantity * merchant.recipes[k].price;
  133. }
  134. }
  135. }
  136. }
  137. return total / 100;
  138. },
  139. /*
  140. Create the data for the revenue graph
  141. Input:
  142. dateRange: Array containing start and end indices for transactions
  143. Return: List of revenue by day between the dates specified
  144. */
  145. graphData: function(dateRange){
  146. if(!dateRange){
  147. return false;
  148. }
  149. let dataList = new Array(30).fill(0);
  150. let currentDate = transactions[dateRange[0]].date;
  151. let arrayIndex = 0;
  152. for(let i = dateRange[0]; i <= dateRange[1]; i++){
  153. if(transactions[i].date.getDate() !== currentDate.getDate()){
  154. currentDate = transactions[i].date;
  155. arrayIndex++;
  156. }
  157. for(let j = 0; j < transactions[i].recipes.length; j++){
  158. for(let merchRecipe of merchant.recipes){
  159. if(transactions[i].recipes[j].recipe === merchRecipe._id){
  160. dataList[arrayIndex] = parseFloat((dataList[arrayIndex] + (transactions[i].recipes[j].quantity * merchRecipe.price) / 100).toFixed(2));
  161. break;
  162. }
  163. }
  164. }
  165. }
  166. return dataList;
  167. },
  168. submitInventoryCheck: function(){
  169. let lis = document.querySelectorAll("#inventoryCheckCard ul li");
  170. let changes = [];
  171. for(let i = 0; i < lis.length; i++){
  172. if(lis[i].children[1].value >= 0){
  173. let merchIngredient = merchant.inventory[lis[i].ingredientIndex];
  174. let value = parseInt(lis[i].children[1].value);
  175. if(value !== merchIngredient.quantity){
  176. changes.push({
  177. id: merchIngredient.ingredient._id,
  178. quantity: value - merchIngredient.quantity
  179. });
  180. }
  181. }else{
  182. banner.createError("Cannot have negative ingredients");
  183. return;
  184. }
  185. }
  186. if(changes.length > 0){
  187. fetch("/merchant/ingredients/update", {
  188. method: "PUT",
  189. headers: {
  190. "Content-Type": "application/json;charset=utf-8"
  191. },
  192. body: JSON.stringify(changes)
  193. })
  194. .then((response) => response.json())
  195. .then((response)=>{
  196. if(typeof(response.data) === "string"){
  197. banner.createError(response.data);
  198. }else{
  199. banner.createNotification("Ingredients updated");
  200. updateInventory(changes);
  201. }
  202. })
  203. .catch((err)=>{});
  204. }
  205. }
  206. }