home.js 9.3 KB

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