home.js 8.3 KB

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