ingredient.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. window.ingredientObj = {
  2. isPopulated: false,
  3. graph: {},
  4. display: function(ingredient){
  5. clearScreen();
  6. document.querySelector("#ingredientStrand").style.display = "flex";
  7. document.querySelector("strand-selector").setAttribute("strand", "ingredient");
  8. if(!this.isPopulated){
  9. document.querySelector("#ingredientFrom").valueAsDate = new Date(new Date().getFullYear(), new Date().getMonth(), 1);
  10. document.querySelector("#ingredientTo").valueAsDate = new Date();
  11. let ingredientsDiv = document.querySelector("#ingredientOptions");
  12. for(let item of data.merchant.inventory){
  13. let checkDiv = document.createElement("div");
  14. checkDiv.classList = "checkboxDiv";
  15. ingredientsDiv.appendChild(checkDiv);
  16. let checkbox = document.createElement("input");
  17. checkbox.type = "checkbox";
  18. checkbox.id = `${item.ingredient.name}Checkbox`;
  19. checkbox._id = item.ingredient._id;
  20. checkbox.name = item.ingredient.name;
  21. checkbox.onchange = ()=>{
  22. if(checkbox.checked){
  23. let from = document.querySelector("#ingredientFrom").valueAsDate;
  24. let to = document.querySelector("#ingredientTo").valueAsDate;
  25. this.graph.addData(this.formatData(item.ingredient._id, from, to), [from, to], item.ingredient.name);
  26. }else{
  27. this.graph.removeData(item.ingredient.name);
  28. }
  29. };
  30. checkDiv.appendChild(checkbox);
  31. let label = document.createElement("label");
  32. label.innerText = item.ingredient.name;
  33. label.setAttribute("for", `${item.ingredient.name}Checkbox`);
  34. checkDiv.appendChild(label);
  35. }
  36. this.graph = new LineGraph(
  37. document.querySelector("#ingredientStrand canvas"),
  38. "Quantity",
  39. "Date",
  40. );
  41. this.isPopulated = true;
  42. }
  43. if(ingredient){
  44. this.graph.clearData();
  45. let from = document.querySelector("#ingredientFrom").valueAsDate;
  46. let to = document.querySelector("#ingredientTo").valueAsDate;
  47. this.graph.addData(this.formatData(ingredient.id, from, to), [from, to], ingredient.name);
  48. for(let label of document.querySelector("#ingredientOptions").children){
  49. if(label.innerText === ingredient.name){
  50. label.children[0].checked = true;
  51. }else{
  52. label.children[0].checked = false;
  53. }
  54. }
  55. }
  56. },
  57. formatData: function(id, startDate, endDate){
  58. let dateRange = Math.floor((Date.UTC(endDate.getFullYear(), endDate.getMonth(), endDate.getDate()) - Date.UTC(startDate.getFullYear(), startDate.getMonth(), startDate.getDate())) / (1000 * 60 * 60 * 24)) + 1;
  59. let dataList = new Array(Math.abs(dateRange)).fill(0);
  60. for(let transaction of data.transactions){
  61. let transDate = new Date(transaction.date);
  62. let diff = Math.floor((Date.UTC(transDate.getFullYear(), transDate.getMonth(), transDate.getDate()) - Date.UTC(endDate.getFullYear(), endDate.getMonth(), endDate.getDate())) / (1000 * 60 * 60 * 24));
  63. if(diff <= 0){
  64. for(let recipe of transaction.recipes){
  65. for(let merchRecipe of data.merchant.recipes){
  66. if(merchRecipe._id === recipe.recipe){
  67. for(let ingredient of merchRecipe.ingredients){
  68. if(ingredient.ingredient === id){
  69. dataList[dateRange - Math.abs(diff) - 1] += ingredient.quantity * recipe.quantity;
  70. }
  71. }
  72. break;
  73. }
  74. }
  75. }
  76. }
  77. }
  78. return dataList;
  79. },
  80. newDates: function(){
  81. let from = document.querySelector("#ingredientFrom").valueAsDate;
  82. let to = document.querySelector("#ingredientTo").valueAsDate;
  83. if(from === "" || to === ""){
  84. banner.createError("Invalid date");
  85. return;
  86. }else{
  87. from = new Date(from);
  88. to = new Date(to);
  89. }
  90. if(validator.transaction.date(from, to)){
  91. window.fetchData(from, to, ()=>{
  92. this.graph.clearData();
  93. let ingredientsDiv = document.querySelector("#ingredientOptions");
  94. for(let div of ingredientsDiv.children){
  95. let checkbox = div.children[0];
  96. if(checkbox.checked){
  97. this.graph.addData(this.formatData(checkbox._id, from, to), [from, to], checkbox.name);
  98. }
  99. }
  100. });
  101. }
  102. }
  103. }