purchase.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. window.purchaseObj = {
  2. isPopulated: false,
  3. graph: {},
  4. display: function(ingredient){
  5. clearScreen();
  6. document.querySelector("#purchaseStrand").style.display = "flex";
  7. document.querySelector("strand-selector").setAttribute("strand", "purchase");
  8. if(!this.isPopulated){
  9. let date = new Date(new Date().getFullYear(), new Date().getMonth(), 1);
  10. document.querySelector("#purchaseFrom").valueAsDate = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 12);
  11. document.querySelector("#purchaseTo").valueAsDate = new Date();
  12. let purchasesDiv = document.querySelector("#purchaseOptions");
  13. for(let item of data.merchant.inventory){
  14. console.log(item);
  15. let checkDiv = document.createElement("div");
  16. checkDiv.classList = "checkboxDiv";
  17. purchasesDiv.appendChild(checkDiv);
  18. let checkbox = document.createElement("input");
  19. checkbox.type = "checkbox";
  20. checkbox.id = `${item.ingredient.name}PurchaseCheckbox`;
  21. checkbox._id = item.ingredient._id;
  22. checkbox.name = item.ingredient.name;
  23. checkbox.onchange = ()=>{
  24. if(checkbox.checked){
  25. let from, to;
  26. [from, to] = getInputDates("purchase");
  27. this.graph.addData(this.formatData(item.ingredient._id, from, to), [from, to], item.ingredient.name);
  28. }else{
  29. this.graph.removeData(item.ingredient.name);
  30. }
  31. };
  32. checkDiv.appendChild(checkbox);
  33. let label = document.createElement("label");
  34. label.innerText = item.ingredient.name;
  35. label.setAttribute("for", `${item.ingredient.name}PurchaseCheckbox`);
  36. checkDiv.appendChild(label);
  37. }
  38. this.graph = new LineGraph(
  39. document.querySelector("#purchaseStrand canvas"),
  40. "Quantity",
  41. "Date"
  42. );
  43. this.isPopulated = true;
  44. }
  45. if(ingredient){
  46. this.graph.clearData();
  47. let from, to;
  48. [from, to] = getInputDates("purchase");
  49. this.graph.addData(this.formatData(ingredient.id, from, to), [from, to], ingredient.name);
  50. for(let label of document.querySelector("#purchaseOptions").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. //TODO This can be made to be faster, no need to search full data list
  60. formatData: function(id, from, to){
  61. let dateRange = Math.floor((Date.UTC(to.getFullYear(), to.getMonth(), to.getDate()) - Date.UTC(from.getFullYear(), from.getMonth(), from.getDate())) / (1000 * 60 * 60 * 24)) + 1;
  62. let dataList = new Array(Math.abs(dateRange)).fill(0);
  63. for(let purchase of data.purchases){
  64. let diff = Math.floor((Date.UTC(purchase.date.getFullYear(), purchase.date.getMonth(), purchase.date.getDate()) - Date.UTC(to.getFullYear(), to.getMonth(), to.getDate())) / (1000 * 60 * 60 * 24));
  65. for(let ingredient of purchase.ingredients){
  66. if(purchase.date > from && diff <=0 && id === ingredient.ingredient){
  67. dataList[dateRange - Math.abs(diff) - 1] += ingredient.quantity;
  68. }
  69. }
  70. }
  71. return dataList;
  72. },
  73. newDates: function(){
  74. let from, to;
  75. [from, to] = getInputDates("purchase");
  76. if(validator.transaction.date(from, to)){
  77. fetchData(from, to, ()=>{
  78. this.graph.clearData();
  79. let purchaseDiv = document.querySelector("#purchaseOptions");
  80. for(let div of purchaseDiv.children){
  81. let checkbox = div.children[0];
  82. if(checkbox.checked){
  83. this.graph.addData(this.formatData(checkbox._id, from, to), [from, to], checkbox.name);
  84. }
  85. }
  86. });
  87. }
  88. }
  89. }