ingredient.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. //A grabastic bag of bullshit to get rid of
  9. let now = new Date();
  10. let startOfMonth = new Date(now.getFullYear(), now.getMonth(), 1);
  11. if(!this.isPopulated){
  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.onchange = ()=>{
  21. if(checkbox.checked){
  22. this.graph.addData(this.formatData(item.ingredient._id, startOfMonth, new Date()), [startOfMonth, new Date()], item.ingredient._id);
  23. }else{
  24. this.graph.removeData(item.ingredient._id);
  25. }
  26. };
  27. checkDiv.appendChild(checkbox);
  28. let label = document.createElement("label");
  29. label.innerText = item.ingredient.name;
  30. label.setAttribute("for", `${item.ingredient.name}Checkbox`);
  31. checkDiv.appendChild(label);
  32. }
  33. let startDate = new Date();
  34. startDate.setFullYear(new Date().getFullYear() - 1);
  35. this.graph = new LineGraph(
  36. document.querySelector("#ingredientStrand canvas"),
  37. "Quantity",
  38. "Date",
  39. );
  40. this.isPopulated = true;
  41. }
  42. if(ingredient){
  43. this.graph.clearData();
  44. this.graph.addData(this.formatData(ingredient.id, startOfMonth, new Date()), [startOfMonth, new Date()], ingredient.id);
  45. for(let label of document.querySelector("#ingredientOptions").children){
  46. if(label.innerText === ingredient.name){
  47. label.children[0].checked = true;
  48. }else{
  49. label.children[0].checked = false;
  50. }
  51. }
  52. }
  53. },
  54. formatData: function(id, startDate, endDate){
  55. let dateRange = Math.floor((Date.UTC(endDate.getFullYear(), endDate.getMonth(), endDate.getDate()) - Date.UTC(startDate.getFullYear(), startDate.getMonth(), startDate.getDate())) / (1000 * 60 * 60 * 24));
  56. let dataList = new Array(Math.abs(dateRange)).fill(0);
  57. for(let transaction of data.transactions){
  58. let transDate = new Date(transaction.date);
  59. let diff = Math.floor((Date.UTC(transDate.getFullYear(), transDate.getMonth(), transDate.getDate()) - Date.UTC(endDate.getFullYear(), endDate.getMonth(), endDate.getDate())) / (1000 * 60 * 60 * 24));
  60. if(diff <= 0){
  61. for(let recipe of transaction.recipes){
  62. for(let merchRecipe of data.merchant.recipes){
  63. if(merchRecipe._id === recipe.recipe){
  64. for(let ingredient of merchRecipe.ingredients){
  65. if(ingredient.ingredient === id){
  66. dataList[dateRange - Math.abs(diff)] += ingredient.quantity;
  67. }
  68. }
  69. break;
  70. }
  71. }
  72. }
  73. }
  74. }
  75. return dataList;
  76. }
  77. }