| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <div id="ingredientDetails">
- <div class="sidebarIconButtons">
- <button class="iconButton" onclick="closeSidebar()">
- <svg width="30" height="30" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
- <line x1="5" y1="12" x2="19" y2="12"></line>
- <polyline points="12 5 19 12 12 19"></polyline>
- </svg>
- </button>
- <button class="iconButton" onclick="ingredientDetailsComp.edit()">
- <svg width="30" height="30" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
- <path d="M12 20h9"></path>
- <path d="M16.5 3.5a2.121 2.121 0 0 1 3 3L7 19l-4 1 1-4L16.5 3.5z"></path>
- </svg>
- </button>
- <button class="iconButton" onclick="ingredientDetailsComp.remove()">
- <svg width="30" height="30" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
- <polyline points="3 6 5 6 21 6"></polyline>
- <path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path>
- </svg>
- </button>
- </div>
- <p></p>
- <h1></h1>
- <div class="lineBorder"></div>
- <label>Current Stock
- <p id="ingredientStock"></p>
- <input id="ingredientInput" type="number" min="0" step="0.01" style="display: none;">
- </label>
- <div class="lineBorder"></div>
- <label>Average Daily Use (30 days)
- <p id="dailyUse"></p>
- </label>
- <button id="editSubmitButton" class="button" onclick="ingredientDetailsComp.editSubmit()" style="display: none;">Save Changes</button>
- <script>
- let ingredientDetailsComp = {
- ingredient: {},
- display: function(ingredient, category){
- this.ingredient = ingredient;
- sidebar = document.querySelector("#ingredientDetails");
- openSidebar(sidebar);
- document.querySelector("#ingredientDetails p").innerText = category.name;
- document.querySelector("#ingredientDetails h1").innerText = ingredient.name;
- document.querySelector("#ingredientStock").innerText = `${ingredient.quantity} ${ingredient.unit}`;
- document.querySelector("#ingredientInput").placeholder = `${ingredient.quantity} ${ingredient.unit}`;
- let quantities = [];
- let now = new Date();
- for(let i = 1; i < 31; i++){
- let endDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i)
- let startDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i - 1);
- quantities.push(ingredientSold(dateIndices(startDay, endDay), ingredient.id));
- }
- let sum = 0;
- for(let quantity of quantities){
- sum += quantity;
- }
- document.querySelector("#dailyUse").innerText = `${(sum/quantities.length).toFixed(2)} ${ingredient.unit}`;
- },
- remove: function(){
- for(let i = 0; i < merchant.recipes.length; i++){
- for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
- if(this.ingredient.id === merchant.recipes[i].ingredients[j].ingredient._id){
- banner.createError("Must remove ingredient from all recipes before removing");
- return;
- }
- }
- }
- fetch(`/merchant/ingredients/remove/${this.ingredient.id}`, {
- method: "DELETE",
- })
- .then((response) => response.json())
- .then((response)=>{
- if(typeof(response) === "string"){
- banner.createError(response);
- }else{
- banner.createNotification("Ingredient removed");
- updateInventory([this.ingredient], true);
- }
- })
- .catch((err)=>{});
- },
- edit: function(){
- document.querySelector("#ingredientStock").style.display = "none";
- document.querySelector("#ingredientInput").style.display = "block";
- document.querySelector("#editSubmitButton").style.display = "block";
- },
- editSubmit: function(){
- let data = [{
- id: this.ingredient.id,
- quantity: Number(document.querySelector("#ingredientInput").value)
- }];
- if(validator.ingredient.quantity(data[0].quantity)){
- fetch("/merchant/ingredients/update", {
- method: "PUT",
- headers: {
- "Content-Type": "application/json;charset=utf-8"
- },
- body: JSON.stringify(data)
- })
- .then((response) => response.json())
- .then((response)=>{
- if(typeof(response) === "string"){
- banner.createError(response);
- }else{
- updateInventory(data);
- banner.createNotification("Ingredient updated");
- }
- })
- .catch((err)=>{
- banner.createError("Something went wrong, try refreshing the page");
- });
- }
- }
- }
- </script>
- </div>
|