ingredient.js 5.2 KB

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