home.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. let home = {
  2. isPopulated: false,
  3. display: function(){
  4. if(!this.isPopulated){
  5. this.drawRevenueCard();
  6. this.drawRevenueGraph();
  7. this.drawInventoryCheckCard();
  8. this.drawPopularCard();
  9. this.isPopulated = true;
  10. }
  11. },
  12. drawRevenueCard: function(){
  13. let today = new Date();
  14. let firstOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
  15. let firstOfLastMonth = new Date(today.getFullYear(), today.getMonth() - 1, 1);
  16. let lastMonthToDay = new Date(new Date().setMonth(today.getMonth() - 1));
  17. let revenueThisMonth = merchant.revenue(controller.transactionIndices(merchant.transactions, firstOfMonth));
  18. let revenueLastmonthToDay = merchant.revenue(controller.transactionIndices(merchant.transactions, firstOfLastMonth, lastMonthToDay));
  19. document.getElementById("revenue").innerText = `$${revenueThisMonth.toLocaleString("en")}`;
  20. let revenueChange = ((revenueThisMonth - revenueLastmonthToDay) / revenueLastmonthToDay) * 100;
  21. let img = "";
  22. if(revenueChange >= 0){
  23. img = "/shared/images/upArrow.png";
  24. }else{
  25. img = "/shared/images/downArrow.png";
  26. }
  27. document.querySelector("#revenueChange p").innerText = `${Math.abs(revenueChange).toFixed(2)}% vs last month`;
  28. document.querySelector("#revenueChange img").src = img;
  29. },
  30. drawRevenueGraph: function(){
  31. let monthAgo = new Date();
  32. monthAgo.setMonth(monthAgo.getMonth() - 1);
  33. let dateIndices = controller.transactionIndices(merchant.transactions, monthAgo);
  34. let revenue = [];
  35. let dates = [];
  36. let dayRevenue = 0;
  37. let currentDate = merchant.transactions[dateIndices[0]].date;
  38. for(let i = dateIndices[0]; i < dateIndices[1]; i++){
  39. if(merchant.transactions[i].date.getDate() !== currentDate.getDate()){
  40. revenue.push(dayRevenue / 100);
  41. dayRevenue = 0;
  42. dates.push(currentDate);
  43. currentDate = merchant.transactions[i].date;
  44. }
  45. for(let j = 0; j < merchant.transactions[i].recipes.length; j++){
  46. const recipe = merchant.transactions[i].recipes[j];
  47. dayRevenue += recipe.recipe.price * recipe.quantity;
  48. }
  49. }
  50. const trace = {
  51. x: dates,
  52. y: revenue,
  53. mode: "lines+markers",
  54. line: {
  55. color: "rgb(255, 99, 107)"
  56. }
  57. }
  58. const layout = {
  59. title: "REVENUE",
  60. xaxis: {
  61. title: "DATE"
  62. },
  63. yaxis: {
  64. title: "$"
  65. }
  66. }
  67. Plotly.newPlot("graphCard", [trace], layout);
  68. },
  69. drawInventoryCheckCard: function(){
  70. let num;
  71. if(merchant.ingredients.length < 5){
  72. num = merchant.ingredients.length;
  73. }else{
  74. num = 5;
  75. }
  76. let rands = [];
  77. for(let i = 0; i < num; i++){
  78. let rand = Math.floor(Math.random() * merchant.ingredients.length);
  79. if(rands.includes(rand)){
  80. i--;
  81. }else{
  82. rands[i] = rand;
  83. }
  84. }
  85. let ul = document.querySelector("#inventoryCheckCard ul");
  86. let template = document.getElementById("ingredientCheck").content.children[0];
  87. while(ul.children.length > 0){
  88. ul.removeChild(ul.firstChild);
  89. }
  90. for(let i = 0; i < rands.length; i++){
  91. let ingredientCheck = template.cloneNode(true);
  92. let input = ingredientCheck.children[1].children[1];
  93. const ingredient = merchant.ingredients[rands[i]];
  94. ingredientCheck.ingredient = ingredient;
  95. ingredientCheck.children[0].innerText = ingredient.ingredient.name;
  96. ingredientCheck.children[1].children[0].onclick = ()=>{input.value--};
  97. input.value = ingredient.ingredient.convert(ingredient.quantity).toFixed(2);
  98. ingredientCheck.children[1].children[2].onclick = ()=>{input.value++}
  99. ingredientCheck.children[2].innerText = ingredient.ingredient.unit.toUpperCase();
  100. ul.appendChild(ingredientCheck);
  101. }
  102. document.getElementById("inventoryCheck").onclick = ()=>{this.submitInventoryCheck()};
  103. },
  104. drawPopularCard: function(){
  105. let thisMonth = new Date();
  106. thisMonth.setDate(1);
  107. let ingredientList = merchant.ingredientsSold(controller.transactionIndices(merchant.transactions, thisMonth));
  108. if(ingredientList !== false){
  109. ingredientList.sort((a, b) => a.quantity < b.quantity);
  110. let quantities = [];
  111. let names = [];
  112. let labels = [];
  113. let colors = [];
  114. for(let i = 4; i >= 0; i--){
  115. quantities.push(ingredientList[i].quantity);
  116. names.push(ingredientList[i].ingredient.name.toUpperCase());
  117. labels.push(`${ingredientList[i].ingredient.convert(ingredientList[i].quantity).toFixed(2)} ${ingredientList[i].ingredient.unit.toUpperCase()}`);
  118. if(i === 0){
  119. colors.push("rgb(255, 99, 107");
  120. }else{
  121. colors.push("rgb(179, 191, 209");
  122. }
  123. }
  124. let trace = {
  125. x: quantities,
  126. y: names,
  127. type: "bar",
  128. orientation: "h",
  129. text: labels,
  130. textposition: "auto",
  131. hoverinfo: "none",
  132. marker: {
  133. color: colors
  134. }
  135. }
  136. let layout = {
  137. title: "MOST POPULAR INGREDIENTS",
  138. xaxis: {
  139. zeroline: false,
  140. title: "QUANTITY IN GRAMS"
  141. }
  142. }
  143. Plotly.newPlot("popularIngredientsCard", [trace], layout);
  144. }else{
  145. document.getElementById("popularCanvas").style.display = "none";
  146. let notice = document.createElement("p");
  147. notice.innerText = "N/A";
  148. notice.classList = "notice";
  149. document.getElementById("popularIngredientsCard").appendChild(notice);
  150. }
  151. },
  152. submitInventoryCheck: function(){
  153. let lis = document.querySelectorAll("#inventoryCheckCard li");
  154. let changes = [];
  155. let fetchData = [];
  156. for(let i = 0; i < lis.length; i++){
  157. if(lis[i].children[1].children[1].value >= 0){
  158. let merchIngredient = lis[i].ingredient;
  159. let value = parseFloat(lis[i].children[1].children[1].value);
  160. if(value !== merchIngredient.quantity){
  161. changes.push({
  162. id: merchIngredient.ingredient.id,
  163. ingredient: merchIngredient.ingredient,
  164. quantity: value
  165. });
  166. fetchData.push({
  167. id: merchIngredient.ingredient.id,
  168. quantity: value
  169. });
  170. }
  171. }else{
  172. banner.createError("CANNOT HAVE NEGATIVE INGREDIENTS");
  173. return;
  174. }
  175. }
  176. let loader = document.getElementById("loaderContainer");
  177. loader.style.display = "flex";
  178. if(fetchData.length > 0){
  179. fetch("/merchant/ingredients/update", {
  180. method: "PUT",
  181. headers: {
  182. "Content-Type": "application/json;charset=utf-8"
  183. },
  184. body: JSON.stringify(fetchData)
  185. })
  186. .then((response) => response.json())
  187. .then((response)=>{
  188. if(typeof(response) === "string"){
  189. banner.createError(response);
  190. }else{
  191. merchant.editIngredients(changes);
  192. banner.createNotification("INGREDIENTS UPDATED");
  193. }
  194. })
  195. .catch((err)=>{})
  196. .finally(()=>{
  197. loader.style.display = "none";
  198. });
  199. }
  200. }
  201. }
  202. module.exports = home;