home.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. console.log("here");
  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 = ()=>{
  98. input.value--;
  99. input.changed = true;
  100. };
  101. input.value = ingredient.ingredient.convert(ingredient.quantity).toFixed(2);
  102. ingredientCheck.children[1].children[2].onclick = ()=>{
  103. input.value++;
  104. input.changed = true;
  105. }
  106. input.onchange = ()=>{input.changed = true};
  107. ingredientCheck.children[2].innerText = ingredient.ingredient.unit.toUpperCase();
  108. ul.appendChild(ingredientCheck);
  109. }
  110. document.getElementById("inventoryCheck").onclick = ()=>{this.submitInventoryCheck()};
  111. },
  112. drawPopularCard: function(){
  113. let thisMonth = new Date();
  114. thisMonth.setDate(1);
  115. let ingredientList = merchant.ingredientsSold(controller.transactionIndices(merchant.transactions, thisMonth));
  116. if(ingredientList !== false){
  117. ingredientList.sort((a, b)=>{
  118. if(a.quantity < b.quantity){
  119. return 1;
  120. }
  121. if(a.quantity > b.quantity){
  122. return -1;
  123. }
  124. return 0;
  125. });
  126. let quantities = [];
  127. let labels = [];
  128. let colors = [];
  129. for(let i = 4; i >= 0; i--){
  130. const ingredientName = ingredientList[i].ingredient.name;
  131. const ingredientQuantity = ingredientList[i].ingredient.convert(ingredientList[i].quantity);
  132. const unitName = ingredientList[i].ingredient.unit;
  133. quantities.push(ingredientList[i].quantity);
  134. labels.push(`${ingredientName}: ${ingredientQuantity.toFixed(2)} ${unitName.toUpperCase()}`);
  135. if(i === 0){
  136. colors.push("rgb(255, 99, 107");
  137. }else{
  138. colors.push("rgb(179, 191, 209");
  139. }
  140. }
  141. let trace = {
  142. x: quantities,
  143. type: "bar",
  144. orientation: "h",
  145. text: labels,
  146. textposition: "auto",
  147. hoverinfo: "none",
  148. marker: {
  149. color: colors
  150. }
  151. }
  152. let layout = {
  153. title: "MOST POPULAR INGREDIENTS",
  154. xaxis: {
  155. zeroline: false,
  156. title: "QUANTITY"
  157. },
  158. yaxis: {
  159. showticklabels: false
  160. }
  161. }
  162. Plotly.newPlot("popularIngredientsCard", [trace], layout);
  163. }else{
  164. document.getElementById("popularCanvas").style.display = "none";
  165. let notice = document.createElement("p");
  166. notice.innerText = "N/A";
  167. notice.classList = "notice";
  168. document.getElementById("popularIngredientsCard").appendChild(notice);
  169. }
  170. },
  171. submitInventoryCheck: function(){
  172. let lis = document.querySelectorAll("#inventoryCheckCard li");
  173. let changes = [];
  174. let fetchData = [];
  175. for(let i = 0; i < lis.length; i++){
  176. if(lis[i].children[1].children[1].value >= 0){
  177. let merchIngredient = lis[i].ingredient;
  178. if(lis[i].children[1].children[1].changed === true){
  179. let value = controller.convertToMain(merchIngredient.ingredient.unit, parseFloat(lis[i].children[1].children[1].value));
  180. changes.push({
  181. id: merchIngredient.ingredient.id,
  182. ingredient: merchIngredient.ingredient,
  183. quantity: value
  184. });
  185. fetchData.push({
  186. id: merchIngredient.ingredient.id,
  187. quantity: value
  188. });
  189. lis[i].children[1].children[1].changed = false;
  190. }
  191. }else{
  192. banner.createError("CANNOT HAVE NEGATIVE INGREDIENTS");
  193. return;
  194. }
  195. }
  196. if(fetchData.length > 0){
  197. let loader = document.getElementById("loaderContainer");
  198. loader.style.display = "flex";
  199. fetch("/merchant/ingredients/update", {
  200. method: "PUT",
  201. headers: {
  202. "Content-Type": "application/json;charset=utf-8"
  203. },
  204. body: JSON.stringify(fetchData)
  205. })
  206. .then((response) => response.json())
  207. .then((response)=>{
  208. if(typeof(response) === "string"){
  209. banner.createError(response);
  210. }else{
  211. merchant.editIngredients(changes);
  212. banner.createNotification("INGREDIENTS UPDATED");
  213. }
  214. })
  215. .catch((err)=>{})
  216. .finally(()=>{
  217. loader.style.display = "none";
  218. });
  219. }
  220. }
  221. }
  222. module.exports = home;