home.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. window.homeStrandObj = {
  2. isPopulated: false,
  3. graph: {},
  4. popularWidth: 0,
  5. popularHeight: 0,
  6. display: function(){
  7. if(!this.isPopulated){
  8. this.drawRevenueCard();
  9. this.drawRevenueGraph();
  10. this.drawInventoryCheckCard();
  11. this.popularWidth = document.getElementById("popularIngredientsCard").offsetWidth;
  12. this.popularHeight = document.getElementById("popularIngredientsCard").offsetHeight;
  13. this.drawPopularCard();
  14. this.isPopulated = true;
  15. }
  16. },
  17. drawRevenueCard: function(){
  18. let today = new Date();
  19. let firstOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
  20. let firstOfLastMonth = new Date(today.getFullYear(), today.getMonth() - 1, 1);
  21. let lastMonthtoDay = new Date(new Date().setMonth(today.getMonth() - 1));
  22. let revenueThisMonth = merchant.revenue(merchant.transactionIndices(firstOfMonth));
  23. let revenueLastmonthToDay = merchant.revenue(merchant.transactionIndices(firstOfLastMonth, lastMonthtoDay));
  24. document.querySelector("#revenue").innerText = `$${revenueThisMonth.toLocaleString("en")}`;
  25. let revenueChange = ((revenueThisMonth - revenueLastmonthToDay) / revenueLastmonthToDay) * 100;
  26. let img = "";
  27. if(revenueChange >= 0){
  28. img = "/shared/images/upArrow.png";
  29. }else{
  30. img = "/shared/images/downArrow.png";
  31. }
  32. document.querySelector("#revenueChange p").innerText = `${Math.abs(revenueChange).toFixed(2)}% vs last month`;
  33. document.querySelector("#revenueChange img").src = img;
  34. },
  35. drawRevenueGraph: function(){
  36. let graphCanvas = document.querySelector("#graphCanvas");
  37. let today = new Date();
  38. graphCanvas.height = graphCanvas.parentElement.clientHeight;
  39. graphCanvas.width = graphCanvas.parentElement.clientWidth;
  40. this.graph = new LineGraph(graphCanvas);
  41. this.graph.addTitle("Revenue");
  42. let thirtyAgo = new Date(today);
  43. thirtyAgo.setDate(today.getDate() - 29);
  44. let data = merchant.graphDailyRevenue(merchant.transactionIndices(thirtyAgo));
  45. if(data){
  46. this.graph.addData(
  47. data,
  48. [thirtyAgo, new Date()],
  49. "Revenue"
  50. );
  51. }else{
  52. document.querySelector("#graphCanvas").style.display = "none";
  53. let notice = document.createElement("h1");
  54. notice.innerText = "NO DATA YET";
  55. notice.classList = "notice";
  56. document.querySelector("#graphCard").appendChild(notice);
  57. }
  58. },
  59. drawInventoryCheckCard: function(){
  60. let num;
  61. if(merchant.ingredients.length < 5){
  62. num = merchant.ingredients.length;
  63. }else{
  64. num = 5;
  65. }
  66. let rands = [];
  67. for(let i = 0; i < num; i++){
  68. let rand = Math.floor(Math.random() * merchant.ingredients.length);
  69. if(rands.includes(rand)){
  70. i--;
  71. }else{
  72. rands[i] = rand;
  73. }
  74. }
  75. let ul = document.querySelector("#inventoryCheckCard ul");
  76. let template = document.querySelector("#ingredientCheck").content.children[0];
  77. while(ul.children.length > 0){
  78. ul.removeChild(ul.firstChild);
  79. }
  80. for(let i = 0; i < rands.length; i++){
  81. let ingredientCheck = template.cloneNode(true);
  82. let input = ingredientCheck.children[1].children[1];
  83. ingredientCheck.ingredient = merchant.ingredients[rands[i]];
  84. ingredientCheck.children[0].innerText = merchant.ingredients[rands[i]].ingredient.name;
  85. ingredientCheck.children[1].children[0].onclick = ()=>{input.value--};
  86. input.value = merchant.ingredients[rands[i]].quantity.toFixed(2);
  87. ingredientCheck.children[1].children[2].onclick = ()=>{input.value++}
  88. ingredientCheck.children[2].innerText = merchant.ingredients[rands[i]].ingredient.unit.toUpperCase();
  89. ul.appendChild(ingredientCheck);
  90. }
  91. },
  92. drawPopularCard: function(){
  93. let dataArray = [];
  94. let now = new Date();
  95. let thisMonth = new Date(now.getFullYear(), now.getMonth(), 1);
  96. let ingredientList = merchant.ingredientsSold(merchant.transactionIndices(thisMonth));
  97. window.ingredientList = [...ingredientList];
  98. let iterations = (ingredientList.length < 5) ? ingredientList.length : 5;
  99. if(ingredientList.length > 0){
  100. for(let i = 0; i < iterations; i++){
  101. try{
  102. let max = ingredientList[0].quantity;
  103. let index = 0;
  104. for(let j = 0; j < ingredientList.length; j++){
  105. if(ingredientList[j].quantity > max){
  106. max = ingredientList[j].quantity;
  107. index = j;
  108. }
  109. }
  110. dataArray.push({
  111. num: max,
  112. label: ingredientList[index].ingredient.name + ": " +
  113. ingredientList[index].ingredient.convert(ingredientList[index].quantity).toFixed(2) +
  114. " " + ingredientList[index].ingredient.unit
  115. });
  116. ingredientList.splice(index, 1);
  117. }catch(err){
  118. break;
  119. }
  120. }
  121. let thisCanvas = document.getElementById("popularCanvas");
  122. thisCanvas.width = this.popularWidth;
  123. thisCanvas.height = this.popularHeight;
  124. let popularGraph = new HorizontalBarGraph(thisCanvas);
  125. popularGraph.addData(dataArray);
  126. }else{
  127. document.querySelector("#popularCanvas").style.display = "none";
  128. let notice = document.createElement("p");
  129. notice.innerText = "N/A";
  130. notice.classList = "notice";
  131. document.querySelector("#popularIngredientsCard").appendChild(notice);
  132. }
  133. },
  134. submitInventoryCheck: function(){
  135. let lis = document.querySelectorAll("#inventoryCheckCard li");
  136. let changes = [];
  137. for(let i = 0; i < lis.length; i++){
  138. if(lis[i].children[1].children[1].value >= 0){
  139. let merchIngredient = lis[i].ingredient;
  140. let value = parseFloat(lis[i].children[1].children[1].value);
  141. if(value !== merchIngredient.quantity){
  142. changes.push({
  143. id: merchIngredient.ingredient.id,
  144. ingredient: merchIngredient.ingredient,
  145. quantity: value
  146. });
  147. }
  148. }else{
  149. banner.createError("CANNOT HAVE NEGATIVE INGREDIENTS");
  150. return;
  151. }
  152. }
  153. let loader = document.getElementById("loaderContainer");
  154. loader.style.display = "flex";
  155. if(changes.length > 0){
  156. fetch("/merchant/ingredients/update", {
  157. method: "PUT",
  158. headers: {
  159. "Content-Type": "application/json;charset=utf-8"
  160. },
  161. body: JSON.stringify(changes)
  162. })
  163. .then((response) => response.json())
  164. .then((response)=>{
  165. if(typeof(response) === "string"){
  166. banner.createError(response);
  167. }else{
  168. merchant.editIngredients(changes);
  169. banner.createNotification("INGREDIENTS UPDATED");
  170. }
  171. })
  172. .catch((err)=>{})
  173. .finally(()=>{
  174. loader.style.display = "none";
  175. });
  176. }
  177. }
  178. }