home.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. window.homeStrandObj = {
  2. isPopulated: false,
  3. graph: {},
  4. display: function(){
  5. if(!this.isPopulated){
  6. this.drawRevenueCard();
  7. this.drawRevenueGraph();
  8. this.drawInventoryCheckCard();
  9. this.drawPopularCard();
  10. this.isPopulated = true;
  11. }
  12. },
  13. drawRevenueCard: function(){
  14. let today = new Date();
  15. let firstOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
  16. let firstOfLastMonth = new Date(today.getFullYear(), today.getMonth() - 1, 1);
  17. let lastMonthtoDay = new Date(new Date().setMonth(today.getMonth() - 1));
  18. let revenueThisMonth = this.calculateRevenue(dateIndices(firstOfMonth));
  19. let revenueLastmonthToDay = this.calculateRevenue(dateIndices(firstOfLastMonth, lastMonthtoDay));
  20. document.querySelector("#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 graphCanvas = document.querySelector("#graphCanvas");
  33. let today = new Date();
  34. graphCanvas.height = graphCanvas.parentElement.clientHeight;
  35. graphCanvas.width = graphCanvas.parentElement.clientWidth;
  36. this.graph = new LineGraph(graphCanvas);
  37. this.graph.addTitle("Revenue");
  38. let thirtyAgo = new Date(today);
  39. thirtyAgo.setDate(today.getDate() - 29);
  40. let data = this.graphData(dateIndices(thirtyAgo));
  41. if(data){
  42. this.graph.addData(
  43. data,
  44. [thirtyAgo, new Date()],
  45. "Revenue"
  46. );
  47. }else{
  48. document.querySelector("#graphCanvas").style.display = "none";
  49. let notice = document.createElement("h1");
  50. notice.innerText = "NO DATA YET";
  51. notice.classList = "notice";
  52. document.querySelector("#graphCard").appendChild(notice);
  53. }
  54. },
  55. drawInventoryCheckCard: function(){
  56. let num;
  57. if(merchant.inventory.length < 5){
  58. num = merchant.inventory.length;
  59. }else{
  60. num = 5;
  61. }
  62. let rands = [];
  63. for(let i = 0; i < num; i++){
  64. let rand = Math.floor(Math.random() * merchant.inventory.length);
  65. if(rands.includes(rand)){
  66. i--;
  67. }else{
  68. rands[i] = rand;
  69. }
  70. }
  71. let ul = document.querySelector("#inventoryCheckCard ul");
  72. let template = document.querySelector("#ingredientCheck").content.children[0];
  73. while(ul.children.length > 0){
  74. ul.removeChild(ul.firstChild);
  75. }
  76. for(let i = 0; i < rands.length; i++){
  77. let ingredientCheck = template.cloneNode(true);
  78. let input = ingredientCheck.children[1].children[1];
  79. ingredientCheck.ingredientIndex = rands[i];
  80. ingredientCheck.children[0].innerText = merchant.inventory[rands[i]].ingredient.name;
  81. ingredientCheck.children[1].children[0].onclick = ()=>{input.value--};
  82. input.value = merchant.inventory[rands[i]].quantity;
  83. ingredientCheck.children[1].children[2].onclick = ()=>{input.value++}
  84. ingredientCheck.children[2].innerText = merchant.inventory[rands[i]].ingredient.unit;
  85. ul.appendChild(ingredientCheck);
  86. }
  87. },
  88. drawPopularCard: function(){
  89. let dataArray = [];
  90. let now = new Date();
  91. let thisMonth = new Date(now.getFullYear(), now.getMonth(), 1);
  92. let ingredientList = ingredientsSold(dateIndices(thisMonth));
  93. if(ingredientList){
  94. for(let i = 0; i < 5; i++){
  95. let max = ingredientList[0].quantity
  96. let index = 0;
  97. for(let j = 0; j < ingredientList.length; j++){
  98. if(ingredientList[j].quantity > max){
  99. max = ingredientList[j].quantity;
  100. index = j;
  101. }
  102. }
  103. dataArray.push({
  104. num: max,
  105. label: `${ingredientList[index].name}: ${+ingredientList[index].quantity.toFixed(2)} ${ingredientList[index].unit}`
  106. });
  107. ingredientList.splice(index, 1);
  108. }
  109. let thisCanvas = document.querySelector("#popularCanvas");
  110. thisCanvas.width = thisCanvas.parentElement.clientWidth;
  111. thisCanvas.height = thisCanvas.parentElement.clientHeight * 0.75;
  112. let popularGraph = new HorizontalBarGraph(document.querySelector("#popularCanvas"));
  113. popularGraph.addData(dataArray);
  114. }else{
  115. document.querySelector("#popularCanvas").style.display = "none";
  116. let notice = document.createElement("p");
  117. notice.innerText = "N/A";
  118. notice.classList = "notice";
  119. document.querySelector("#popularIngredientsCard").appendChild(notice);
  120. }
  121. },
  122. calculateRevenue: function(indices){
  123. let total = 0;
  124. for(let i = indices[0]; i <= indices[1]; i++){
  125. for(let j = 0; j < transactions[i].recipes.length; j++){
  126. for(let k = 0; k < merchant.recipes.length; k++){
  127. if(transactions[i].recipes[j].recipe === merchant.recipes[k]._id){
  128. total += transactions[i].recipes[j].quantity * merchant.recipes[k].price;
  129. }
  130. }
  131. }
  132. }
  133. return total / 100;
  134. },
  135. /*
  136. Create the data for the revenue graph
  137. Input:
  138. dateRange: Array containing start and end indices for transactions
  139. Return: List of revenue by day between the dates specified
  140. */
  141. graphData: function(dateRange){
  142. if(!dateRange){
  143. return false;
  144. }
  145. let dataList = new Array(30).fill(0);
  146. let currentDate = transactions[dateRange[0]].date;
  147. let arrayIndex = 0;
  148. for(let i = dateRange[0]; i <= dateRange[1]; i++){
  149. if(transactions[i].date.getDate() !== currentDate.getDate()){
  150. currentDate = transactions[i].date;
  151. arrayIndex++;
  152. }
  153. for(let j = 0; j < transactions[i].recipes.length; j++){
  154. for(let merchRecipe of merchant.recipes){
  155. if(transactions[i].recipes[j].recipe === merchRecipe._id){
  156. dataList[arrayIndex] = parseFloat((dataList[arrayIndex] + (transactions[i].recipes[j].quantity * merchRecipe.price) / 100).toFixed(2));
  157. break;
  158. }
  159. }
  160. }
  161. }
  162. return dataList;
  163. },
  164. submitInventoryCheck: function(){
  165. let lis = document.querySelectorAll("#inventoryCheckCard li");
  166. let changes = [];
  167. for(let i = 0; i < lis.length; i++){
  168. if(lis[i].children[1].children[1].value >= 0){
  169. let merchIngredient = merchant.inventory[lis[i].ingredientIndex];
  170. let value = parseInt(lis[i].children[1].children[1].value);
  171. if(value !== merchIngredient.quantity){
  172. changes.push({
  173. id: merchIngredient.ingredient._id,
  174. quantity: value - merchIngredient.quantity
  175. });
  176. }
  177. }else{
  178. banner.createError("Cannot have negative ingredients");
  179. return;
  180. }
  181. }
  182. if(changes.length > 0){
  183. fetch("/merchant/ingredients/update", {
  184. method: "PUT",
  185. headers: {
  186. "Content-Type": "application/json;charset=utf-8"
  187. },
  188. body: JSON.stringify(changes)
  189. })
  190. .then((response) => response.json())
  191. .then((response)=>{
  192. if(typeof(response.data) === "string"){
  193. banner.createError(response.data);
  194. }else{
  195. banner.createNotification("Ingredients updated");
  196. updateInventory(changes);
  197. }
  198. })
  199. .catch((err)=>{});
  200. }
  201. }
  202. }