home.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. const revenueThisMonth = merchant.getRevenue(firstOfMonth);
  18. const revenueLastMonthToDay = merchant.getRevenue(firstOfLastMonth, lastMonthToDay);
  19. document.getElementById("revenue").innerText = `$${revenueThisMonth.toFixed(2)}`;
  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 revenue = [];
  34. let dates = [];
  35. let dayRevenue = 0;
  36. const transactions = merchant.getTransactions(monthAgo);
  37. let currentDate = (transactions.length > 0) ? transactions[0].date : undefined;
  38. for(let i = 0; i < transactions.length; i++){
  39. if(transactions[i].date.getDate() !== currentDate.getDate()){
  40. revenue.push(dayRevenue / 100);
  41. dayRevenue = 0;
  42. dates.push(currentDate);
  43. currentDate = transactions[i].date;
  44. }
  45. for(let j = 0; j < transactions[i].recipes.length; j++){
  46. const recipe = 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 = ()=>{
  97. input.value--;
  98. input.changed = true;
  99. };
  100. if(ingredient.ingredient.specialUnit === "bottle"){
  101. input.value = ingredient.quantity.toFixed(2);
  102. ingredientCheck.children[2].innerText = "BOTTLES";
  103. }else{
  104. input.value = ingredient.quantity.toFixed(2);
  105. ingredientCheck.children[2].innerText = ingredient.ingredient.unit.toUpperCase();
  106. }
  107. ingredientCheck.children[1].children[2].onclick = ()=>{
  108. input.value++;
  109. input.changed = true;
  110. }
  111. input.onchange = ()=>{input.changed = true};
  112. ul.appendChild(ingredientCheck);
  113. }
  114. document.getElementById("inventoryCheck").onclick = ()=>{this.submitInventoryCheck()};
  115. },
  116. drawPopularCard: function(){
  117. let thisMonth = new Date();
  118. thisMonth.setDate(1);
  119. const ingredientList = merchant.getIngredientsSold(thisMonth);
  120. if(ingredientList !== false){
  121. ingredientList.sort((a, b)=>{
  122. if(a.quantity < b.quantity){
  123. return 1;
  124. }
  125. if(a.quantity > b.quantity){
  126. return -1;
  127. }
  128. return 0;
  129. });
  130. let quantities = [];
  131. let labels = [];
  132. let colors = [];
  133. let count = (ingredientList.length < 5) ? ingredientList.length - 1 : 4;
  134. for(let i = count; i >= 0; i--){
  135. const ingredientName = ingredientList[i].ingredient.name;
  136. const ingredientQuantity = ingredientList[i].quantity;
  137. const unitName = ingredientList[i].ingredient.unit;
  138. quantities.push(ingredientList[i].quantity);
  139. labels.push(`${ingredientName}: ${ingredientQuantity.toFixed(2)} ${unitName.toUpperCase()}`);
  140. if(i === 0){
  141. colors.push("rgb(255, 99, 107");
  142. }else{
  143. colors.push("rgb(179, 191, 209");
  144. }
  145. }
  146. let trace = {
  147. x: quantities,
  148. type: "bar",
  149. orientation: "h",
  150. text: labels,
  151. textposition: "auto",
  152. hoverinfo: "none",
  153. marker: {
  154. color: colors
  155. }
  156. }
  157. let layout = {
  158. title: "MOST POPULAR INGREDIENTS",
  159. xaxis: {
  160. zeroline: false,
  161. title: "QUANTITY"
  162. },
  163. yaxis: {
  164. showticklabels: false
  165. }
  166. }
  167. Plotly.newPlot("popularIngredientsCard", [trace], layout);
  168. }else{
  169. document.getElementById("popularCanvas").style.display = "none";
  170. let notice = document.createElement("p");
  171. notice.innerText = "N/A";
  172. notice.classList = "notice";
  173. document.getElementById("popularIngredientsCard").appendChild(notice);
  174. }
  175. },
  176. //Need to change the updating of ingredients
  177. //should update the ingredient directly, then send that. Maybe...
  178. submitInventoryCheck: function(){
  179. let lis = document.querySelectorAll("#inventoryCheckCard li");
  180. let data = [];
  181. for(let i = 0; i < lis.length; i++){
  182. if(lis[i].children[1].children[1].value >= 0){
  183. if(lis[i].children[1].children[1].changed === true){
  184. let merchIngredient = lis[i].ingredient;
  185. data.push({
  186. id: merchIngredient.ingredient.id,
  187. quantity: lis[i].children[1].children[1].value
  188. });
  189. lis[i].children[1].children[1].changed = false;
  190. }
  191. }else{
  192. controller.createBanner("CANNOT HAVE NEGATIVE INGREDIENTS", "error");
  193. return;
  194. }
  195. }
  196. if(data.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(data)
  205. })
  206. .then(response => response.json())
  207. .then((response)=>{
  208. if(typeof(response) === "string"){
  209. controller.createBanner(response, "error");
  210. }else{
  211. for(let i = 0; i < response.length; i++){
  212. merchant.removeIngredient(merchant.getIngredient(response[i].ingredient._id));
  213. merchant.addIngredient(response[i].ingredient, response[i].quantity, response[i].defaultUnit);
  214. }
  215. controller.createBanner("INGREDIENTS UPDATED", "success");
  216. }
  217. })
  218. .catch((err)=>{
  219. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  220. })
  221. .finally(()=>{
  222. loader.style.display = "none";
  223. });
  224. }
  225. }
  226. }
  227. module.exports = home;