ingredient.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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, to;
  25. [from, to] = getInputDates("ingredient");
  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, to;
  47. [from, to] = getInputDates("ingredient");
  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. //TODO This can be made to be faster, no need to search full data list
  59. formatData: function(id, startDate, endDate){
  60. 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;
  61. let dataList = new Array(Math.abs(dateRange)).fill(0);
  62. for(let transaction of data.transactions){
  63. let diff = Math.floor((Date.UTC(transaction.date.getFullYear(), transaction.date.getMonth(), transaction.date.getDate()) - Date.UTC(endDate.getFullYear(), endDate.getMonth(), endDate.getDate())) / (1000 * 60 * 60 * 24));
  64. if(transaction.date > startDate && 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, to;
  83. [from, to] = getInputDates("ingredient");
  84. if(validator.transaction.date(from, to)){
  85. fetchData(from, to, ()=>{
  86. this.graph.clearData();
  87. let ingredientsDiv = document.querySelector("#ingredientOptions");
  88. for(let div of ingredientsDiv.children){
  89. let checkbox = div.children[0];
  90. if(checkbox.checked){
  91. this.graph.addData(this.formatData(checkbox._id, from, to), [from, to], checkbox.name);
  92. }
  93. }
  94. });
  95. }
  96. }
  97. }