ingredient.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. window.ingredientObj = {
  2. isPopulated: false,
  3. graph: {},
  4. display: function(type, 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 d = new Date();
  10. d.setDate(d.getDate() - 20);
  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("ingredient", 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. type: "date",
  41. start: d,
  42. end: new Date()
  43. }
  44. );
  45. this.isPopulated = true;
  46. }
  47. if(ingredient){
  48. this.graph.clear();
  49. this.graph.addData(this.formatData(ingredient.id, d, new Date()));
  50. for(let label of document.querySelector("#ingredientOptions").children){
  51. if(label.innerText === ingredient.name){
  52. label.children[0].checked = true;
  53. }else{
  54. label.children[0].checked = false;
  55. }
  56. }
  57. }
  58. },
  59. formatData: function(id, startDate, endDate){
  60. let dataList;
  61. let dateRange = Math.floor((Date.UTC(endDate.getFullYear(), endDate.getMonth(), endDate.getDate()) - Date.UTC(startDate.getFullYear(), startDate.getMonth(), startDate.getDate())) / (1000 * 60 * 60 * 24));
  62. dataList = new Array(Math.abs(dateRange)).fill(0);
  63. for(let transaction of data.transactions){
  64. let transDate = new Date(transaction.date);
  65. let diff = Math.floor((Date.UTC(transDate.getFullYear(), transDate.getMonth(), transDate.getDate()) - Date.UTC(endDate.getFullYear(), endDate.getMonth(), endDate.getDate())) / (1000 * 60 * 60 * 24));
  66. if(diff <= 0){
  67. for(let recipe of transaction.recipes){
  68. for(let merchRecipe of data.merchant.recipes){
  69. if(merchRecipe._id === recipe.recipe){
  70. for(let ingredient of merchRecipe.ingredients){
  71. if(ingredient.ingredient === id){
  72. dataList[dateRange - Math.abs(diff)] += ingredient.quantity;
  73. }
  74. }
  75. break;
  76. }
  77. }
  78. }
  79. }
  80. }
  81. return {id: id, set: dataList};
  82. }
  83. }