home.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. let layout = {
  59. title: "REVENUE",
  60. xaxis: {
  61. title: "DATE"
  62. },
  63. yaxis: {
  64. title: "$"
  65. },
  66. paper_bgcolor: "rgba(0, 0, 0, 0)"
  67. }
  68. if(screen.width < 1200){
  69. layout.margin = {
  70. l: 35,
  71. r: 0
  72. }
  73. }
  74. Plotly.newPlot("graphCard", [trace], layout);
  75. },
  76. drawInventoryCheckCard: function(){
  77. let num;
  78. if(merchant.ingredients.length < 5){
  79. num = merchant.ingredients.length;
  80. }else{
  81. num = 5;
  82. }
  83. let rands = [];
  84. for(let i = 0; i < num; i++){
  85. let rand = Math.floor(Math.random() * merchant.ingredients.length);
  86. if(rands.includes(rand)){
  87. i--;
  88. }else{
  89. rands[i] = rand;
  90. }
  91. }
  92. let ul = document.querySelector("#inventoryCheckCard ul");
  93. let template = document.getElementById("ingredientCheck").content.children[0];
  94. while(ul.children.length > 0){
  95. ul.removeChild(ul.firstChild);
  96. }
  97. for(let i = 0; i < rands.length; i++){
  98. let ingredientCheck = template.cloneNode(true);
  99. let input = ingredientCheck.children[1].children[1];
  100. const ingredient = merchant.ingredients[rands[i]];
  101. ingredientCheck.ingredient = ingredient;
  102. ingredientCheck.children[0].innerText = ingredient.ingredient.name;
  103. ingredientCheck.children[1].children[0].onclick = ()=>{
  104. input.value--;
  105. input.changed = true;
  106. };
  107. input.value = ingredient.quantity.toFixed(2);
  108. ingredientCheck.children[2].innerText = ingredient.ingredient.unit.toUpperCase();
  109. ingredientCheck.children[1].children[2].onclick = ()=>{
  110. input.value++;
  111. input.changed = true;
  112. }
  113. input.onchange = ()=>{input.changed = true};
  114. ul.appendChild(ingredientCheck);
  115. }
  116. document.getElementById("inventoryCheck").onclick = ()=>{this.submitInventoryCheck()};
  117. },
  118. drawPopularCard: function(){
  119. let thisMonth = new Date();
  120. thisMonth.setDate(1);
  121. const ingredientList = merchant.getIngredientsSold(thisMonth);
  122. if(ingredientList !== false){
  123. ingredientList.sort((a, b)=>{
  124. if(a.quantity < b.quantity){
  125. return 1;
  126. }
  127. if(a.quantity > b.quantity){
  128. return -1;
  129. }
  130. return 0;
  131. });
  132. let quantities = [];
  133. let labels = [];
  134. let colors = [];
  135. let count = (ingredientList.length < 5) ? ingredientList.length - 1 : 4;
  136. for(let i = count; i >= 0; i--){
  137. const ingredientName = ingredientList[i].ingredient.name;
  138. const ingredientQuantity = ingredientList[i].quantity;
  139. const unitName = ingredientList[i].ingredient.unit;
  140. quantities.push(ingredientList[i].quantity);
  141. labels.push(`${ingredientName}: ${ingredientQuantity.toFixed(2)} ${unitName.toUpperCase()}`);
  142. if(i === 0){
  143. colors.push("rgb(255, 99, 107");
  144. }else{
  145. colors.push("rgb(179, 191, 209");
  146. }
  147. }
  148. let trace = {
  149. x: quantities,
  150. type: "bar",
  151. orientation: "h",
  152. text: labels,
  153. textposition: "auto",
  154. hoverinfo: "none",
  155. marker: {
  156. color: colors
  157. }
  158. }
  159. let layout = {
  160. title: {
  161. text: "MOST POPULAR INGREDIENTS"
  162. },
  163. xaxis: {
  164. zeroline: false,
  165. title: "QUANTITY"
  166. },
  167. yaxis: {
  168. showticklabels: false
  169. },
  170. paper_bgcolor: "rgba(0, 0, 0, 0)"
  171. }
  172. if(screen.width < 1200){
  173. layout.margin = {
  174. l: 10,
  175. r: 10,
  176. t: 80,
  177. b: 40
  178. };
  179. }
  180. Plotly.newPlot("popularIngredientsCard", [trace], layout);
  181. }else{
  182. document.getElementById("popularCanvas").style.display = "none";
  183. let notice = document.createElement("p");
  184. notice.innerText = "N/A";
  185. notice.classList = "notice";
  186. document.getElementById("popularIngredientsCard").appendChild(notice);
  187. }
  188. },
  189. //Need to change the updating of ingredients
  190. //should update the ingredient directly, then send that. Maybe...
  191. submitInventoryCheck: function(){
  192. let lis = document.querySelectorAll("#inventoryCheckCard li");
  193. let data = [];
  194. for(let i = 0; i < lis.length; i++){
  195. if(lis[i].children[1].children[1].value >= 0){
  196. if(lis[i].children[1].children[1].changed === true){
  197. let merchIngredient = lis[i].ingredient;
  198. data.push({
  199. id: merchIngredient.ingredient.id,
  200. quantity: lis[i].children[1].children[1].value
  201. });
  202. lis[i].children[1].children[1].changed = false;
  203. }
  204. }else{
  205. controller.createBanner("CANNOT HAVE NEGATIVE INGREDIENTS", "error");
  206. return;
  207. }
  208. }
  209. if(data.length > 0){
  210. let loader = document.getElementById("loaderContainer");
  211. loader.style.display = "flex";
  212. fetch("/merchant/ingredients/update", {
  213. method: "PUT",
  214. headers: {
  215. "Content-Type": "application/json;charset=utf-8"
  216. },
  217. body: JSON.stringify(data)
  218. })
  219. .then(response => response.json())
  220. .then((response)=>{
  221. if(typeof(response) === "string"){
  222. controller.createBanner(response, "error");
  223. }else{
  224. for(let i = 0; i < response.length; i++){
  225. merchant.removeIngredient(merchant.getIngredient(response[i].ingredient._id));
  226. merchant.addIngredients(response);
  227. }
  228. controller.createBanner("INGREDIENTS UPDATED", "success");
  229. }
  230. })
  231. .catch((err)=>{
  232. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  233. })
  234. .finally(()=>{
  235. loader.style.display = "none";
  236. });
  237. }
  238. }
  239. }
  240. module.exports = home;