| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- window.ingredientObj = {
- isPopulated: false,
- graph: {},
- display: function(ingredient){
- clearScreen();
- document.querySelector("#ingredientStrand").style.display = "flex";
- document.querySelector("strand-selector").setAttribute("strand", "ingredient");
- //A grabastic bag of bullshit to get rid of
- let now = new Date();
- let startOfMonth = new Date(now.getFullYear(), now.getMonth(), 1);
- if(!this.isPopulated){
- let ingredientsDiv = document.querySelector("#ingredientOptions");
- for(let item of data.merchant.inventory){
- let checkDiv = document.createElement("div");
- checkDiv.classList = "checkboxDiv";
- ingredientsDiv.appendChild(checkDiv);
- let checkbox = document.createElement("input");
- checkbox.type = "checkbox";
- checkbox.id = `${item.ingredient.name}Checkbox`;
- checkbox.onchange = ()=>{
- if(checkbox.checked){
- this.graph.addData(this.formatData(item.ingredient._id, startOfMonth, new Date()), [startOfMonth, new Date()], item.ingredient._id);
- }else{
- this.graph.removeData(item.ingredient._id);
- }
- };
- checkDiv.appendChild(checkbox);
- let label = document.createElement("label");
- label.innerText = item.ingredient.name;
- label.setAttribute("for", `${item.ingredient.name}Checkbox`);
- checkDiv.appendChild(label);
- }
- let startDate = new Date();
- startDate.setFullYear(new Date().getFullYear() - 1);
- this.graph = new LineGraph(
- document.querySelector("#ingredientStrand canvas"),
- "Quantity",
- "Date",
- );
- this.isPopulated = true;
- }
- if(ingredient){
- this.graph.clearData();
-
- this.graph.addData(this.formatData(ingredient.id, startOfMonth, new Date()), [startOfMonth, new Date()], ingredient.id);
- for(let label of document.querySelector("#ingredientOptions").children){
- if(label.innerText === ingredient.name){
- label.children[0].checked = true;
- }else{
- label.children[0].checked = false;
- }
- }
- }
- },
- formatData: function(id, startDate, endDate){
- let dateRange = Math.floor((Date.UTC(endDate.getFullYear(), endDate.getMonth(), endDate.getDate()) - Date.UTC(startDate.getFullYear(), startDate.getMonth(), startDate.getDate())) / (1000 * 60 * 60 * 24));
- let dataList = new Array(Math.abs(dateRange)).fill(0);
- for(let transaction of data.transactions){
- let transDate = new Date(transaction.date);
- let diff = Math.floor((Date.UTC(transDate.getFullYear(), transDate.getMonth(), transDate.getDate()) - Date.UTC(endDate.getFullYear(), endDate.getMonth(), endDate.getDate())) / (1000 * 60 * 60 * 24));
- if(diff <= 0){
- for(let recipe of transaction.recipes){
- for(let merchRecipe of data.merchant.recipes){
- if(merchRecipe._id === recipe.recipe){
- for(let ingredient of merchRecipe.ingredients){
- if(ingredient.ingredient === id){
- dataList[dateRange - Math.abs(diff)] += ingredient.quantity;
- }
- }
- break;
- }
- }
- }
- }
- }
- return dataList;
- }
- }
|