bundle.js 60 KB

1
  1. !function(){var e={isPopulated:!1,display:function(){this.isPopulated||(this.drawRevenueCard(),this.drawRevenueGraph(),this.drawInventoryCheckCard(),this.drawPopularCard(),this.isPopulated=!0)},drawRevenueCard:function(){let e=new Date,t=new Date(e.getFullYear(),e.getMonth(),1),n=new Date(e.getFullYear(),e.getMonth()-1,1),i=new Date((new Date).setMonth(e.getMonth()-1)),r=merchant.revenue(controller.transactionIndices(merchant.transactions,t)),l=merchant.revenue(controller.transactionIndices(merchant.transactions,n,i));document.getElementById("revenue").innerText="$"+r.toLocaleString("en");let d=(r-l)/l*100,a="";a=d>=0?"/shared/images/upArrow.png":"/shared/images/downArrow.png",document.querySelector("#revenueChange p").innerText=Math.abs(d).toFixed(2)+"% vs last month",document.querySelector("#revenueChange img").src=a},drawRevenueGraph:function(){let e=new Date;e.setMonth(e.getMonth()-1);let t=controller.transactionIndices(merchant.transactions,e),n=[],i=[],r=0,l=merchant.transactions[t[0]].date;for(let a=t[0];a<t[1];a++){merchant.transactions[a].date.getDate()!==l.getDate()&&(n.push(r/100),r=0,i.push(l),l=merchant.transactions[a].date);for(let e=0;e<merchant.transactions[a].recipes.length;e++){const t=merchant.transactions[a].recipes[e];r+=t.recipe.price*t.quantity}}const d={x:i,y:n,mode:"lines+markers",line:{color:"rgb(255, 99, 107)"}};Plotly.newPlot("graphCard",[d],{title:"REVENUE",xaxis:{title:"DATE"},yaxis:{title:"$"}})},drawInventoryCheckCard:function(){let e;e=merchant.ingredients.length<5?merchant.ingredients.length:5;let t=[];for(let r=0;r<e;r++){let e=Math.floor(Math.random()*merchant.ingredients.length);t.includes(e)?r--:t[r]=e}let n=document.querySelector("#inventoryCheckCard ul"),i=document.getElementById("ingredientCheck").content.children[0];for(;n.children.length>0;)n.removeChild(n.firstChild);for(let r=0;r<t.length;r++){let e=i.cloneNode(!0),l=e.children[1].children[1];const d=merchant.ingredients[t[r]];e.ingredient=d,e.children[0].innerText=d.ingredient.name,e.children[1].children[0].onclick=()=>{l.value--,l.changed=!0},"bottle"===d.ingredient.specialUnit?(l.value=(d.quantity/d.ingredient.unitSize).toFixed(2),e.children[2].innerText="BOTTLES"):(l.value=d.ingredient.convert(d.quantity).toFixed(2),e.children[2].innerText=d.ingredient.unit.toUpperCase()),e.children[1].children[2].onclick=()=>{l.value++,l.changed=!0},l.onchange=()=>{l.changed=!0},n.appendChild(e)}document.getElementById("inventoryCheck").onclick=()=>{this.submitInventoryCheck()}},drawPopularCard:function(){let e=new Date;e.setDate(1);let t=merchant.ingredientsSold(controller.transactionIndices(merchant.transactions,e));if(!1!==t){t.sort((e,t)=>e.quantity<t.quantity?1:e.quantity>t.quantity?-1:0);let e=[],n=[],i=[];for(let d=t.length<5?t.length-1:4;d>=0;d--){const r=t[d].ingredient.name,l=t[d].ingredient.convert(t[d].quantity),a=t[d].ingredient.unit;e.push(t[d].quantity),n.push(`${r}: ${l.toFixed(2)} ${a.toUpperCase()}`),0===d?i.push("rgb(255, 99, 107"):i.push("rgb(179, 191, 209")}let r={x:e,type:"bar",orientation:"h",text:n,textposition:"auto",hoverinfo:"none",marker:{color:i}},l={title:"MOST POPULAR INGREDIENTS",xaxis:{zeroline:!1,title:"QUANTITY"},yaxis:{showticklabels:!1}};Plotly.newPlot("popularIngredientsCard",[r],l)}else{document.getElementById("popularCanvas").style.display="none";let e=document.createElement("p");e.innerText="N/A",e.classList="notice",document.getElementById("popularIngredientsCard").appendChild(e)}},submitInventoryCheck:function(){let e=document.querySelectorAll("#inventoryCheckCard li"),t=[],n=[];for(let i=0;i<e.length;i++){if(!(e[i].children[1].children[1].value>=0))return void banner.createError("CANNOT HAVE NEGATIVE INGREDIENTS");{let r=e[i].ingredient;if(!0===e[i].children[1].children[1].changed){let l=0;l="bottle"===r.ingredient.specialUnit?parseFloat(e[i].children[1].children[1].value)*r.ingredient.unitSize:controller.convertToMain(r.ingredient.unit,parseFloat(e[i].children[1].children[1].value)),t.push({ingredient:r.ingredient,quantity:l}),n.push({id:r.ingredient.id,quantity:l}),e[i].children[1].children[1].changed=!1}}}if(n.length>0){let e=document.getElementById("loaderContainer");e.style.display="flex",fetch("/merchant/ingredients/update",{method:"PUT",headers:{"Content-Type":"application/json;charset=utf-8"},body:JSON.stringify(n)}).then(e=>e.json()).then(e=>{"string"==typeof e?banner.createError(e):(merchant.editIngredients(t),banner.createNotification("INGREDIENTS UPDATED"))}).catch(e=>{}).finally(()=>{e.style.display="none"})}}};var t={isPopulated:!1,ingredients:[],display:function(){this.isPopulated||(this.populateByProperty("category"),document.getElementById("ingredientSearch").oninput=()=>{this.search()},document.getElementById("ingredientClearButton").onclick=()=>{this.clearSorting()},document.getElementById("ingredientSelect").onchange=()=>{this.sort()},this.isPopulated=!0)},populateByProperty:function(e){let t;"category"===e?t=merchant.categorizeIngredients():"unit"===e&&(t=merchant.unitizeIngredients());let n=document.getElementById("categoryList"),i=document.getElementById("categoryDiv").content.children[0],r=document.getElementById("ingredient").content.children[0];for(this.ingredients=[];n.children.length>0;)n.removeChild(n.firstChild);for(let l=0;l<t.length;l++){let e=i.cloneNode(!0);e.children[0].children[0].innerText=t[l].name.toUpperCase(),e.children[0].children[1].onclick=()=>{this.toggleCategory(e.children[1],e.children[0].children[1])},e.children[1].style.display="none",n.appendChild(e);for(let n=0;n<t[l].ingredients.length;n++){let i=t[l].ingredients[n],d=r.cloneNode(!0);d.children[0].innerText=i.ingredient.name,d.onclick=()=>{controller.openSidebar("ingredientDetails",i)},d._name=i.ingredient.name.toLowerCase(),d._unit=i.ingredient.unit.toLowerCase(),"bottle"===i.ingredient.specialUnit?d.children[2].innerText=(i.quantity/i.ingredient.unitSize).toFixed(2)+" BOTTLES":d.children[2].innerText=`${i.ingredient.convert(i.quantity).toFixed(2)} ${i.ingredient.unit.toUpperCase()}`,e.children[1].appendChild(d),this.ingredients.push(d)}}},displayIngredientsOnly:function(e){let t=document.getElementById("categoryList");for(;t.children.length>0;)t.removeChild(t.firstChild);for(let n=0;n<e.length;n++)t.appendChild(e[n])},toggleCategory:function(e,t){"none"===e.style.display?(t.innerHTML='<svg width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="black" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="18 15 12 9 6 15"></polyline></svg>',e.style.display="flex"):"flex"===e.style.display&&(t.innerHTML='<svg width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="black" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="6 9 12 15 18 9"></polyline></svg>',e.style.display="none")},search:function(){let e=document.getElementById("ingredientSearch").value.toLowerCase();if(document.getElementById("ingredientSelect").selectedIndex=0,""===e)return this.populateByProperty("category"),void(document.getElementById("ingredientClearButton").style.display="none");let t=[];for(let n=0;n<this.ingredients.length;n++)this.ingredients[n]._name.includes(e)&&t.push(this.ingredients[n]);document.getElementById("ingredientClearButton").style.display="inline",this.displayIngredientsOnly(t)},sort:function(){let e=document.getElementById("ingredientSelect").value;if(""===e)return;if(document.getElementById("ingredientSearch").value="","category"===e)return void this.populateByProperty("category");if("unit"===e)return void this.populateByProperty("unit");document.getElementById("ingredientClearButton").style.display="inline";let t=this.ingredients.slice().sort((t,n)=>t[e]>n[e]?1:-1);this.displayIngredientsOnly(t)},clearSorting:function(e){document.getElementById("ingredientSearch").value="",document.getElementById("ingredientSelect").selectedIndex=0,document.getElementById("ingredientClearButton").style.display="none",this.populateByProperty("category")}};var n={isPopulated:!1,recipeDivList:[],display:function(e){this.isPopulated||(this.populateRecipes(),"none"!==merchant.pos&&(document.getElementById("posUpdateRecipe").onclick=()=>{this.posUpdate(e)}),document.getElementById("recipeSearch").oninput=()=>{this.search()},document.getElementById("recipeClearButton").onclick=()=>{this.clearSorting()},this.isPopulated=!0)},populateRecipes:function(){let e=document.getElementById("recipeList"),t=document.getElementById("recipe").content.children[0];for(this.recipeDivList=[];e.children.length>0;)e.removeChild(e.firstChild);for(let n=0;n<merchant.recipes.length;n++){let i=t.cloneNode(!0);i.onclick=()=>{controller.openSidebar("recipeDetails",merchant.recipes[n])},i._name=merchant.recipes[n].name,e.appendChild(i),i.children[0].innerText=merchant.recipes[n].name,i.children[1].innerText="$"+(merchant.recipes[n].price/100).toFixed(2),this.recipeDivList.push(i)}},search:function(){let e=document.getElementById("recipeSearch").value.toLowerCase(),t=document.getElementById("recipeList"),n=document.getElementById("recipeClearButton"),i=[];for(let r=0;r<this.recipeDivList.length;r++)this.recipeDivList[r]._name.toLowerCase().includes(e)&&i.push(this.recipeDivList[r]);for(;t.children.length>0;)t.removeChild(t.firstChild);for(let r=0;r<i.length;r++)t.appendChild(i[r]);n.style.display=""===e?"none":"inline"},clearSorting:function(){document.getElementById("recipeSearch").value="",this.search()},posUpdate:function(e){let t=document.getElementById("loaderContainer");t.style.display="flex";let n="/recipe/update/"+merchant.pos;fetch(n,{method:"GET",headers:{"Content-Type":"application/json;charset=utf-8"}}).then(e=>e.json()).then(t=>{let n=[];for(let r=0;r<t.new.length;r++)n.push(new e(t.new[r]._id,t.new[r].name,t.new[r].price,merchant,[]));n.length>0&&merchant.editRecipes(n);let i=[];for(let e=0;e<t.removed.length;e++)for(let n=0;n<merchant.recipes.length;n++)if(t.removed[e]._id===merchant.recipes[n].id){i.push(merchant.recipes[n],!0);break}i.length>0&&merchant.editRecipes(i,!0)}).catch(e=>{banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE")}).finally(()=>{t.style.display="none"})}};var i={newData:!1,dateChange:!1,transactions:[],ingredient:{},recipe:{},display:function(e){if(document.getElementById("analDateBtn").onclick=()=>{this.changeDates(e)},0===this.transactions.length||!0===this.newData){let e=new Date;e.setMonth(e.getMonth()-1);const t=controller.transactionIndices(merchant.transactions,e);this.transactions=merchant.transactions.slice(t[0],t[1]+1)}let t=document.getElementById("analSlider");t.onchange=()=>{this.display(e)};let n=document.getElementById("analIngredientContent"),i=document.getElementById("analRecipeContent");t.checked?(n.style.display="none",i.style.display="flex",this.displayRecipes()):(n.style.display="flex",i.style.display="none",this.displayIngredients())},displayIngredients:function(){const e=document.getElementById("itemsList");for(;e.children.length>0;)e.removeChild(e.firstChild);for(let t=0;t<merchant.ingredients.length;t++){let n=document.createElement("li");n.classList.add("itemButton"),n.item=merchant.ingredients[t],n.innerText=merchant.ingredients[t].ingredient.name,n.onclick=()=>{const e=document.getElementById("itemsList");for(let t=0;t<e.children.length;t++)e.children[t].classList.remove("analItemActive");n.classList.add("analItemActive"),this.ingredient=merchant.ingredients[t],this.ingredientDisplay()},e.appendChild(n)}this.dateChange&&0!==Object.keys(this.ingredient).length&&this.ingredientDisplay(),this.dateChange=!1},displayRecipes:function(){let e=document.getElementById("analRecipeList");for(;e.children.length>0;)e.removeChild(e.firstChild);for(let t=0;t<merchant.recipes.length;t++){let n=document.createElement("li");n.classList.add("itemButton"),n.recipe=merchant.recipes[t],n.innerText=merchant.recipes[t].name,n.onclick=()=>{let e=document.getElementById("analRecipeList");for(let t=0;t<e.children.length;t++)e.children[t].classList.remove("analItemActive");n.classList.add("analItemActive"),this.recipe=merchant.recipes[t],this.recipeDisplay()},e.appendChild(n)}this.dateChange&&0!==Object.keys(this.recipe).length&&this.recipeDisplay(),this.dateChange=!1},ingredientDisplay:function(){let e=[];for(let g=0;g<merchant.recipes.length;g++)for(let t=0;t<merchant.recipes[g].ingredients.length;t++)if(merchant.recipes[g].ingredients[t].ingredient===this.ingredient.ingredient){e.push({recipe:merchant.recipes[g],quantity:merchant.recipes[g].ingredients[t].quantity});break}let t=[],n=[],i=this.transactions[0].date,r=0;for(let g=0;g<this.transactions.length;g++){i.getDate()!==this.transactions[g].date.getDate()&&(t.push(this.ingredient.ingredient.convert(r)),n.push(i),r=0,i=this.transactions[g].date);for(let t=0;t<this.transactions[g].recipes.length;t++)for(let n=0;n<e.length;n++)if(this.transactions[g].recipes[t].recipe===e[n].recipe)for(let e=0;e<this.transactions[g].recipes[t].recipe.ingredients.length;e++){const n=this.transactions[g].recipes[t].recipe.ingredients[e];if(n.ingredient===this.ingredient.ingredient){r+=n.quantity*this.transactions[g].recipes[t].quantity;break}}g===this.transactions.length-1&&(t.push(this.ingredient.ingredient.convert(r)),n.push(i))}let l={x:n,y:t,mode:"lines+markers",line:{color:"rgb(255, 99, 107)"}};const d={title:this.ingredient.ingredient.name.toUpperCase(),xaxis:{title:"DATE"},yaxis:{title:`QUANTITY (${this.ingredient.ingredient.unit.toUpperCase()})`}};Plotly.newPlot("itemUseGraph",[l],d);let a=0,s=0,c=t[0];for(let g=0;g<t.length;g++)a+=t[g],t[g]>s?s=t[g]:t[g]<c&&(c=t[g]);document.getElementById("analMinUse").innerText=`${c.toFixed(2)} ${this.ingredient.ingredient.unit}`,document.getElementById("analAvgUse").innerText=`${(a/t.length).toFixed(2)} ${this.ingredient.ingredient.unit}`,document.getElementById("analMaxUse").innerText=`${s.toFixed(2)} ${this.ingredient.ingredient.unit}`;let o=[0,0,0,0,0,0,0],h=[0,0,0,0,0,0,0];for(let g=0;g<t.length;g++)o[n[g].getDay()]+=t[g],h[n[g].getDay()]++;document.getElementById("analDayOne").innerText=`${(o[0]/h[0]).toFixed(2)} ${this.ingredient.ingredient.unit}`,document.getElementById("analDayTwo").innerText=`${(o[1]/h[1]).toFixed(2)} ${this.ingredient.ingredient.unit}`,document.getElementById("analDayThree").innerText=`${(o[2]/h[2]).toFixed(2)} ${this.ingredient.ingredient.unit}`,document.getElementById("analDayFour").innerText=`${(o[3]/h[3]).toFixed(2)} ${this.ingredient.ingredient.unit}`,document.getElementById("analDayFive").innerText=`${(o[4]/h[4]).toFixed(2)} ${this.ingredient.ingredient.unit}`,document.getElementById("analDaySix").innerText=`${(o[5]/h[5]).toFixed(2)} ${this.ingredient.ingredient.unit}`,document.getElementById("analDaySeven").innerText=`${(o[6]/h[6]).toFixed(2)} ${this.ingredient.ingredient.unit}`},recipeDisplay:function(){let e=[],t=[],n=this.transactions[0].date,i=0;for(let a=0;a<this.transactions.length;a++){n.getDate()!==this.transactions[a].date.getDate()&&(e.push(i),i=0,t.push(n),n=this.transactions[a].date);for(let e=0;e<this.transactions[a].recipes.length;e++){const t=this.transactions[a].recipes[e];t.recipe===this.recipe&&(i+=t.quantity)}a===this.transactions.length-1&&(e.push(i),t.push(n))}const r={x:t,y:e,mode:"lines+markers",line:{color:"rgb(255, 99, 107"}},l={title:this.recipe.name.toUpperCase(),xaxis:{title:"DATE"},yaxis:{title:"Quantity"}};Plotly.newPlot("recipeSalesGraph",[r],l);let d=0;for(let a=0;a<e.length;a++)d+=e[a];document.getElementById("recipeAvgUse").innerText=(d/e.length).toFixed(2),document.getElementById("recipeAvgRevenue").innerText="$"+(d/e.length*this.recipe.price/100).toFixed(2)},changeDates:function(e){let t={from:document.getElementById("analStartDate").valueAsDate,to:document.getElementById("analEndDate").valueAsDate};if(t.from>t.to||""===t.from||""===t.to||t.to>new Date)return void banner.createError("INVALID DATE");let n=document.getElementById("loaderContainer");n.style.display="flex",fetch("/transaction/retrieve",{method:"post",headers:{"Content-Type":"application/json;charset=utf-8"},body:JSON.stringify(t)}).then(e=>e.json()).then(t=>{if("string"==typeof t)banner.createError(t.data);else{this.transactions=[];for(let i=0;i<t.length;i++)this.transactions.push(new e(t[i]._id,new Date(t[i].date),t[i].recipes,merchant));let n=document.getElementById("analSlider").checked;n&&0!==Object.keys(this.recipe).length?this.recipeDisplay():n||0===Object.keys(this.ingredient).length||this.ingredientDisplay(),this.dateChange=!0}}).catch(e=>{banner.createError("ERROR: UNABLE TO DISPLAY THE DATA")}).finally(()=>{n.style.display="none"})}};var r={isFetched:!1,display:async function(e){if(!this.isFetched){let t=document.getElementById("loaderContainer");t.style.display="flex",fetch("/order",{method:"GET",headers:{"Content-Type":"application/json;charset=utf-8"}}).then(e=>e.json()).then(t=>{if("string"==typeof t)banner.createError(t);else{let n=[];for(let i=0;i<t.length;i++)n.push(new e(t[i]._id,t[i].name,t[i].date,t[i].taxes,t[i].fees,t[i].ingredients,merchant));merchant.editOrders(n),document.getElementById("orderSubmitForm").onsubmit=()=>{this.submitFilter(e)},this.isFetched=!0}}).catch(e=>{banner.createError("SOMETHING WENT WRONG. TRY REFRESHING THE PAGE")}).finally(()=>{t.style.display="none"})}},populate:function(){let e=document.getElementById("orderList"),t=document.getElementById("order").content.children[0],n=document.getElementById("dateDropdownOrder"),i=document.getElementById("ingredientDropdown");n.style.display="none",i.style.display="none",document.getElementById("dateFilterBtnOrder").onclick=()=>{this.toggleDropdown(n)},document.getElementById("ingredientFilterBtn").onclick=()=>{this.toggleDropdown(i)};for(let r=0;r<merchant.ingredients.length;r++){let e=document.createElement("input");e.type="checkbox",e.ingredient=merchant.ingredients[r].ingredient,i.appendChild(e);let t=document.createElement("label");t.innerText=merchant.ingredients[r].ingredient.name,t.for=e,i.appendChild(t);let n=document.createElement("br");i.appendChild(n)}for(;e.children.length>0;)e.removeChild(e.firstChild);for(let r=0;r<merchant.orders.length;r++){let n=t.cloneNode(!0),i=0;for(let e=0;e<merchant.orders[r].ingredients.length;e++){const t=merchant.orders[r].ingredients[e];i+=t.pricePerUnit*t.quantity}n.children[0].innerText=merchant.orders[r].name,n.children[1].innerText=merchant.orders[r].ingredients.length+" ingredients",n.children[2].innerText=new Date(merchant.orders[r].date).toLocaleDateString("en-US"),n.children[3].innerText="$"+(i/100+merchant.orders[r].taxes/100+merchant.orders[r].fees/100).toFixed(2),n.order=merchant.orders[r],n.onclick=()=>{controller.openSidebar("orderDetails",merchant.orders[r])},e.appendChild(n)}},submitFilter:function(){event.preventDefault();let e={startDate:document.getElementById("orderFilDate1").valueAsDate,endDate:document.getElementById("orderFilDate2").valueAsDate,ingredients:[]};if(e.startDate>=e.endDate)return void banner.createError("START DATE CANNOT BE AFTER END DATE");let t=document.getElementById("ingredientDropdown");for(let i=0;i<t.children.length;i+=3)t.children[i].checked&&e.ingredients.push(t.children[i].ingredient.id);if(0===e.ingredients.length)for(let i=0;i<merchant.ingredients.length;i++)e.ingredients.push(merchant.ingredients[i].ingredient.id);let n=document.getElementById("loaderContainer");n.style.display="flex",fetch("/order",{method:"POST",headers:{"Content-Type":"application/json;charset=utf-8"},body:JSON.stringify(e)}).then(e=>e.json()).then(e=>{if("string"==typeof e)banner.createError(e);else{let t=document.getElementById("orderList"),n=document.getElementById("order").content.children[0];for(;t.children.length>0;)t.removeChild(t.firstChild);for(let i=0;i<e.length;i++){let r=n.cloneNode(!0),l=new Order(e[i]._id,e[i].name,e[i].date,e[i].taxes,e[i].fees,e[i].ingredients,merchant),d=0;for(let e=0;e<l.ingredients.length;e++)d+=l.ingredients[e].price/100*l.ingredients[e].quantity;r.children[0].innerText=l.name,r.children[1].innerText=l.ingredients.length+" items",r.children[2].innerText=l.date.toLocaleDateString(),r.children[3].innerText="$"+d.toFixed(2),r.onclick=()=>{controller.openSidebar("orderDetails",l)},t.appendChild(r)}}}).catch(e=>{banner.createError("UNABLE TO DISPLAY THE ORDERS")}).finally(()=>{n.style.display="none"})},toggleDropdown:function(e){event.preventDefault();let t=e.parentElement.children[0].children[1].children[0].children[0];"none"===e.style.display?(e.style.display="block",t.setAttribute("points","18 15 12 9 6 15")):(e.style.display="none",t.setAttribute("points","6 9 12 15 18 9"))}};var l={isPopulated:!1,display:function(e){if(!this.isPopulated){let t=document.getElementById("transactionsList"),n=document.getElementById("dateDropdown"),i=document.getElementById("recipeDropDown"),r=document.getElementById("transaction").content.children[0],l=new Date,d=new Date(l.getFullYear(),l.getMonth()-1,l.getDate());for(document.getElementById("transFilDate1").valueAsDate=d,document.getElementById("transFilDate2").valueAsDate=l,n.style.display="none",i.style.display="none",document.getElementById("dateFilterBtn").onclick=()=>{this.toggleDropdown(n)},document.getElementById("recipeFilterBtn").onclick=()=>{this.toggleDropdown(i)};i.children.length>0;)i.removeChild(i.firstChild);for(let e=0;e<merchant.recipes.length;e++){let t=document.createElement("input");t.type="checkbox",t.recipe=merchant.recipes[e],i.appendChild(t);let n=document.createElement("label");n.innerText=merchant.recipes[e].name,n.for=t,i.appendChild(n);let r=document.createElement("br");i.appendChild(r)}for(;t.children.length>0;)t.removeChild(t.firstChild);let a=0;for(;a<merchant.transactions.length&&a<100;){let e=r.cloneNode(!0),n=merchant.transactions[a];e.onclick=()=>{controller.openSidebar("transactionDetails",n)},t.appendChild(e);let i=0,l=0;for(let t=0;t<merchant.transactions[a].recipes.length;t++)i+=merchant.transactions[a].recipes[t].quantity,l+=merchant.transactions[a].recipes[t].recipe.price*merchant.transactions[a].recipes[t].quantity;e.children[0].innerText=`${merchant.transactions[a].date.toLocaleDateString()} ${merchant.transactions[a].date.toLocaleTimeString()}`,e.children[1].innerText=i+" recipes sold",e.children[2].innerText="$"+(l/100).toFixed(2),a++}document.getElementById("transFormSubmit").onsubmit=()=>{this.submitFilter(e)},this.isPopulated=!0}},submitFilter:function(e){event.preventDefault();let t={startDate:document.getElementById("transFilDate1").valueAsDate,endDate:document.getElementById("transFilDate2").valueAsDate,recipes:[]};if(t.startDate>=t.endDate)return void banner.createError("START DATE CANNOT BE AFTER END DATE");let n=document.getElementById("recipeDropDown");for(let r=0;r<n.children.length;r+=3)n.children[r].checked&&t.recipes.push(n.children[r].recipe.id);if(0===t.recipes.length)for(let r=0;r<merchant.recipes.length;r++)t.recipes.push(merchant.recipes[r].id);let i=document.getElementById("loaderContainer");i.style.display="flex",fetch("/transaction",{method:"POST",headers:{"Content-Type":"application/json;charset=utf-8"},body:JSON.stringify(t)}).then(e=>e.json()).then(t=>{if("string"==typeof t)banner.createError(t);else{let n=document.getElementById("transactionsList"),i=document.getElementById("transaction").content.children[0];for(;n.children.length>0;)n.removeChild(n.firstChild);for(let r=0;r<t.length;r++){let l=i.cloneNode(!0),d=0,a=0,s=new e(t[r]._id,t[r].date,t[r].recipes,merchant);for(let e=0;e<s.recipes.length;e++)d+=s.recipes[e].quantity,a+=s.recipes[e].quantity*s.recipes[e].recipe.price;l.children[0].innerText=`${s.date.toLocaleDateString()} ${s.date.toLocaleTimeString()}`,l.children[1].innerText=d+" recipes sold",l.children[2].innerText="$"+(a/100).toFixed(2),l.onclick=()=>{controller.openSidebar("transactionDetails",s)},n.appendChild(l)}}}).catch(e=>{banner.createError("UNABLE TO DISPLAY THE TRANSACTIONS")}).finally(()=>{i.style.display="none"})},toggleDropdown:function(e){event.preventDefault();let t=e.parentElement.children[0].children[1].children[0].children[0];"none"===e.style.display?(e.style.display="block",t.setAttribute("points","18 15 12 9 6 15")):(e.style.display="none",t.setAttribute("points","6 9 12 15 18 9"))}};var d={ingredient:{},dailyUse:0,display:function(e){if(this.ingredient=e,"bottle"===this.ingredient.ingredient.specialUnit)try{let e=document.getElementById("ingredientUnitSizeLabel");e.parentElement.removeChild(e);let t=document.getElementById("bottleBorder");t.parentElement.removeChild(t)}catch(p){}document.getElementById("ingredientDetailsCategory").innerText=e.ingredient.category;let t=document.getElementById("detailsCategoryInput");t.value="",t.placeholder=e.ingredient.category,document.getElementById("ingredientDetailsName").innerText=e.ingredient.name;let n=document.getElementById("ingredientDetailsNameIn");n.value="",n.placeholder=e.ingredient.name;let i=document.getElementById("ingredientInput"),r=document.getElementById("ingredientStock");if(i.value="","bottle"===e.ingredient.specialUnit){let t=e.ingredient.convert(e.quantity);r.innerText=`${t.toFixed(2)} ${e.ingredient.unit.toUpperCase()}`,r.innerText=(e.quantity/e.ingredient.unitSize).toFixed(2)+" BOTTLES",i.placeholder=t.toFixed(2)}else r.innerText=`${e.ingredient.convert(e.quantity).toFixed(2)} ${e.ingredient.unit.toUpperCase()}`,i.placeholder=e.ingredient.convert(e.quantity).toFixed(2);let l=[],d=new Date;for(let y=1;y<31;y++){let t=new Date(d.getFullYear(),d.getMonth(),d.getDate()-y),n=new Date(d.getFullYear(),d.getMonth(),d.getDate()-y-1),i=controller.transactionIndices(merchant.transactions,n,t);!1===i?l.push(0):l.push(merchant.singleIngredientSold(i,e))}let a=0;for(let y=0;y<l.length;y++)a+=l[y];let s=a/l.length;document.getElementById("dailyUse").innerText=`${e.ingredient.convert(s).toFixed(2)} ${e.ingredient.unit.toUpperCase()}`;let c=document.getElementById("ingredientRecipeList"),o=merchant.getRecipesForIngredient(e.ingredient);for(;c.children.length>0;)c.removeChild(c.firstChild);for(let y=0;y<o.length;y++){let e=document.createElement("li");e.innerText=o[y].name,e.onclick=()=>{controller.openStrand("recipeBook"),controller.openSidebar("recipeDetails",o[y])},c.appendChild(e)}let h=document.getElementById("ingredientButtons"),g=[];for("other"!==this.ingredient.ingredient.unitType&&(g=merchant.units[this.ingredient.ingredient.unitType]);h.children.length>0;)h.removeChild(h.firstChild);for(let y=0;y<g.length;y++){let e=document.createElement("button");e.classList.add("unitButton"),e.innerText=g[y].toUpperCase(),e.onclick=()=>{this.changeUnit(e,g[y])},h.appendChild(e),g[y]===this.ingredient.ingredient.unit&&e.classList.add("unitActive")}let u=document.querySelectorAll(".editAdd"),m=document.querySelectorAll(".editRemove");for(let y=0;y<u.length;y++)u[y].style.display="none";for(let y=0;y<m.length;y++)m[y].style.display="block";document.getElementById("editSubmitButton").onclick=()=>{this.editSubmit()},document.getElementById("editCancelButton").onclick=()=>{this.display(this.ingredient)},document.getElementById("editIngBtn").onclick=()=>{this.edit()},document.getElementById("removeIngBtn").onclick=()=>{this.remove()}},remove:function(){for(let t=0;t<merchant.recipes.length;t++)for(let e=0;e<merchant.recipes[t].ingredients.length;e++)if(this.ingredient.ingredient===merchant.recipes[t].ingredients[e].ingredient)return void banner.createError("MUST REMOVE INGREDIENT FROM ALL RECIPES BEFORE REMOVING FROM INVENTORY");let e=document.getElementById("loaderContainer");e.style.display="flex",fetch("/ingredients/remove/"+this.ingredient.ingredient.id,{method:"DELETE"}).then(e=>e.json()).then(e=>{"string"==typeof e?banner.createError(e):(banner.createNotification("INGREDIENT REMOVED"),merchant.editIngredients([this.ingredient],!0))}).catch(e=>{}).finally(()=>{e.style.display="none"})},edit:function(){let e=document.querySelectorAll(".editAdd"),t=document.querySelectorAll(".editRemove");for(let n=0;n<e.length;n++)e[n].style.display="flex";for(let n=0;n<t.length;n++)t[n].style.display="none";if("bottle"===this.ingredient.ingredient.specialUnit){const e=document.getElementById("ingredientDetails"),t=document.getElementById("displayUnitLabel");let n=document.createElement("label");n.innerText=`BOTTLE SIZE (${this.ingredient.ingredient.unit.toUpperCase()})`,n.id="ingredientUnitSizeLabel",e.insertBefore(n,t);const i=this.ingredient.ingredient.convert(this.ingredient.ingredient.unitSize);let r=document.createElement("input");r.type="number",r.value=i.toFixed(2),r.id="ingredientUnitSizeIn",r.min="0",r.step="0.01",n.appendChild(r);let l=document.createElement("div");l.classList.add("lineBorder"),l.id="bottleBorder",e.insertBefore(l,t)}},editSubmit:function(){let e=document.querySelectorAll(".unitButton");for(let d=0;d<e.length;d++)if(e[d].classList.contains("unitActive")){const t=e[d].innerText.toLowerCase();this.ingredient.ingredient.unit=t;break}const t=document.getElementById("ingredientInput");if(""!==t.value)if("bottle"===this.ingredient.ingredient.specialUnit){let e=controller.convertToMain(this.ingredient.ingredient.unit,t.value*this.ingredient.ingredient.unitSize);this.ingredient.quantity=e/this.ingredient.ingredient.unitSize}else this.ingredient.quantity=controller.convertToMain(this.ingredient.ingredient.unit,Number(t.value));const n=document.getElementById("detailsCategoryInput");this.ingredient.ingredient.category=""===n.value?this.ingredient.ingredient.category:n.value;const i=document.getElementById("ingredientDetailsNameIn");this.ingredient.ingredient.name=""===i.value?this.ingredient.ingredient.name:i.value;let r={id:this.ingredient.ingredient.id,name:this.ingredient.ingredient.name,quantity:this.ingredient.quantity,category:this.ingredient.ingredient.category,defaultUnit:this.ingredient.ingredient.unit};if("bottle"===this.ingredient.ingredient.specialUnit){const e=document.getElementById("ingredientUnitSizeIn").value;this.ingredient.ingredient.unitSize=controller.convertToMain(this.ingredient.ingredient.unit,e),r.unitSize=this.ingredient.ingredient.unitSize}let l=document.getElementById("loaderContainer");l.style.display="flex",fetch("/ingredients/update",{method:"PUT",headers:{"Content-Type":"application/json;charset=utf-8"},body:JSON.stringify(r)}).then(e=>e.json()).then(e=>{"string"==typeof e?banner.createError(e):(merchant.editIngredients([this.ingredient]),this.display(this.ingredient),banner.createNotification("INGREDIENT UPDATED"))}).catch(e=>{banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE")}).finally(()=>{l.style.display="none"})},changeUnit:function(e,t){let n=document.querySelectorAll(".unitButton");for(let i=0;i<n.length;i++)n[i].classList.remove("unitActive");e.classList.add("unitActive")},changeUnitDefault:function(){let e=document.getElementById("loaderContainer");e.style.display="flex";let t=this.ingredient.ingredient.id,n=this.ingredient.ingredient.unit;fetch(`/merchant/ingredients/update/${t}/${n}`,{method:"put",headers:{"Content-Type":"application/json;charset=utf-8"}}).then(e=>{"string"==typeof e?banner.createError(e):banner.createNotification("INGREDIENT DEFAULT UNIT UPDATED")}).catch(e=>{banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE")}).finally(()=>{e.style.display="none"})}};var a={display:function(e){const t=document.getElementById("unitSelector");document.getElementById("newIngName").value="",document.getElementById("newIngCategory").value="",document.getElementById("newIngQuantity").value=0,document.getElementById("bottleSizeLabel").style.display="none",t.value="g",t.onchange=()=>{this.unitChange()},document.getElementById("submitNewIng").onclick=()=>{this.submit(e)}},unitChange:function(){const e=document.getElementById("unitSelector"),t=document.getElementById("bottleSizeLabel");"bottle"===e.value?t.style.display="block":t.style.display="none"},submit:function(e){let t=document.getElementById("unitSelector"),n=document.querySelectorAll("#unitSelector option");const i=document.getElementById("newIngQuantity").value;let r=t.value,l={ingredient:{name:document.getElementById("newIngName").value,category:document.getElementById("newIngCategory").value,unitType:n[t.selectedIndex].getAttribute("type")},quantity:controller.convertToMain(r,i),defaultUnit:r};if("bottle"===r){const e=document.getElementById("bottleUnits").value,t=controller.convertToMain(e,document.getElementById("bottleSize").value);l.ingredient.unitType="volume",l.ingredient.unitSize=t,l.defaultUnit=e,l.ingredient.specialUnit=r,l.quantity=i*t}let d=document.getElementById("loaderContainer");d.style.display="flex",fetch("/ingredients/create",{method:"POST",headers:{"Content-Type":"application/json;charset=utf-8"},body:JSON.stringify(l)}).then(e=>e.json()).then(t=>{"string"==typeof t?banner.createError(t):(merchant.editIngredients([{ingredient:new e(t.ingredient._id,t.ingredient.name,t.ingredient.category,t.ingredient.unitType,t.defaultUnit,merchant,t.ingredient.specialUnit,t.ingredient.unitSize),quantity:t.quantity}]),banner.createNotification("INGREDIENT CREATED"))}).catch(e=>{banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE")}).finally(()=>{d.style.display="none"})}};var s={display:function(e){document.getElementById("sidebarDiv").classList.add("sidebarWide"),document.getElementById("newOrderIngredientList").style.display="flex";let t=document.getElementById("selectedIngredientList");for(;t.children.length>0;)t.removeChild(t.firstChild);let n=document.getElementById("newOrderIngredients");for(;n.children.length>0;)n.removeChild(n.firstChild);for(let i=0;i<merchant.ingredients.length;i++){let e=document.createElement("button");e.classList="newOrderIngredient",e.innerText=merchant.ingredients[i].ingredient.name,e.onclick=()=>{this.addIngredient(merchant.ingredients[i].ingredient,e)},n.appendChild(e)}document.getElementById("submitNewOrder").onclick=()=>{this.submit(e)}},addIngredient:function(e,t){t.style.display="none";let n=document.getElementById("selectedIngredient").content.children[0].cloneNode(!0);n.ingredient=e,n.children[0].children[1].onclick=()=>{this.removeIngredient(n,t)},"bottle"===e.specialUnit?n.children[0].children[0].innerText=e.name+" (BOTTLES)":n.children[0].children[0].innerText=`${e.name} (${e.unit.toUpperCase()})`,document.getElementById("selectedIngredientList").appendChild(n)},removeIngredient:function(e,t){e.parentElement.removeChild(e),t.style.display="block"},submit:function(e){let t=document.getElementById("newOrderDate").value,n=100*document.getElementById("orderTaxes").value,i=100*document.getElementById("orderFees").value,r=document.getElementById("selectedIngredientList").children;if(""===t)return void banner.createError("DATE IS REQUIRED FOR ORDERS");let l={name:document.getElementById("newOrderName").value,date:t,taxes:n,fees:i,ingredients:[]};for(let a=0;a<r.length;a++){let e=r[a].children[1].children[0].value,t=r[a].children[1].children[1].value;if(""===e||""===t)return void banner.createError("MUST PROVIDE QUANTITY AND PRICE PER UNIT FOR ALL INGREDIENTS");if((e<0||t<0)&&banner.createError("QUANTITY AND PRICE MUST BE NON-NEGATIVE NUMBERS"),"bottle"===r[a].ingredient.specialUnit){const n=controller.convertPrice("volume",r[a].ingredient.unit,100*t/r[a].ingredient.convert(r[a].ingredient.unitSize));l.ingredients.push({ingredient:r[a].ingredient.id,quantity:e*r[a].ingredient.unitSize,pricePerUnit:n})}else l.ingredients.push({ingredient:r[a].ingredient.id,quantity:controller.convertToMain(r[a].ingredient.unit,e),pricePerUnit:controller.convertPrice(r[a].ingredient.unitType,r[a].ingredient.unit,100*t)})}let d=document.getElementById("loaderContainer");d.style.display="flex",fetch("/order/create",{method:"post",headers:{"Content-Type":"application/json;charset=utf-8"},body:JSON.stringify(l)}).then(e=>e.json()).then(t=>{if("string"==typeof t)banner.createError(t);else{let n=new e(t._id,t.name,t.date,t.taxes,t.fees,t.ingredients,merchant);merchant.editOrders([n]),merchant.editIngredients(n.ingredients,!1,!0),banner.createNotification("NEW ORDER CREATED")}}).catch(e=>{banner.createError("SOMETHING WENT WRONG, PLEASE REFRESH THE PAGE")}).finally(()=>{d.style.display="none"})}};var c={display:function(e){let t=document.querySelector("#recipeInputIngredients select"),n=merchant.categorizeIngredients();for(;t.children.length>0;)t.removeChild(t.firstChild);for(let i=0;i<n.length;i++){let e=document.createElement("optgroup");e.label=n[i].name,t.appendChild(e);for(let t=0;t<n[i].ingredients.length;t++){let r=document.createElement("option");r.value=n[i].ingredients[t].ingredient.id,r.innerText=`${n[i].ingredients[t].ingredient.name} (${n[i].ingredients[t].ingredient.unit})`,e.appendChild(r)}}document.getElementById("ingredientCount").onchange=()=>{this.changeRecipeCount()},document.getElementById("submitNewRecipe").onclick=()=>{this.submit(e)}},changeRecipeCount:function(){let e=document.getElementById("ingredientCount").value,t=document.getElementById("recipeInputIngredients"),n=t.children.length;if(e>n){let i=e-n;for(let e=0;e<i;e++){let e=t.children[0].cloneNode(!0);e.children[2].children[0].value="",t.appendChild(e)}for(let n=0;n<e;n++)t.children[n].children[0].innerText="INGREDIENT "+(n+1)}else if(e<n){let i=n-e;for(let e=0;e<i;e++)t.removeChild(t.children[t.children.length-1])}},submit:function(e){let t={name:document.getElementById("newRecipeName").value,price:document.getElementById("newRecipePrice").value,ingredients:[]},n=document.querySelectorAll("#recipeInputIngredients > div");for(let r=0;r<n.length;r++)for(let e=0;e<merchant.ingredients.length;e++)if(merchant.ingredients[e].ingredient.id===n[r].children[1].children[0].value){t.ingredients.push({ingredient:n[r].children[1].children[0].value,quantity:controller.convertToMain(merchant.ingredients[e].ingredient.unit,n[r].children[2].children[0].value)});break}let i=document.getElementById("loaderContainer");i.style.display="flex",fetch("/recipe/create",{method:"POST",headers:{"Content-Type":"application/json;charset=utf-8"},body:JSON.stringify(t)}).then(e=>e.json()).then(t=>{if("string"==typeof t)banner.createError(t);else{let n=new e(t._id,t.name,t.price,t.ingredients,merchant);merchant.editRecipes([n]),banner.createNotification("RECIPE CREATED")}}).catch(e=>{banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE")}).finally(()=>{i.style.display="none"})}};var o={display:function(e){let t=document.getElementById("newTransactionRecipes"),n=document.getElementById("createTransaction").content.children[0];for(;t.children.length>0;)t.removeChild(t.firstChild);for(let i=0;i<merchant.recipes.length;i++){let e=n.cloneNode(!0);e.recipe=merchant.recipes[i],t.appendChild(e),e.children[0].innerText=merchant.recipes[i].name}document.getElementById("submitNewTransaction").onclick=()=>{this.submit(e)}},submit:function(e){let t=document.getElementById("newTransactionRecipes"),n=document.getElementById("newTransactionDate").valueAsDate;if(n>new Date)return void banner.createError("CANNOT HAVE A DATE IN THE FUTURE");let i={date:n,recipes:[],ingredientUpdates:{}};for(let r=0;r<t.children.length;r++){let e=t.children[r].children[1].value;const n=t.children[r].recipe;if(""!==e&&e>0){i.recipes.push({recipe:n.id,quantity:e});for(let t=0;t<n.ingredients.length;t++){const r=n.ingredients[t];i.ingredientUpdates[r.ingredient.id]?i.ingredientUpdates[r.ingredient.id]+=r.quantity*e:i.ingredientUpdates[r.ingredient.id]=r.quantity*e}}else if(e<0)return void banner.createError("CANNOT HAVE NEGATIVE VALUES")}if(i.recipes.length>0){let t=document.getElementById("loaderContainer");t.style.display="flex",fetch("/transaction/create",{method:"post",headers:{"Content-Type":"application/json;charset=utf-8"},body:JSON.stringify(i)}).then(e=>e.json()).then(t=>{if("string"==typeof t)banner.createError(t);else{let n=new e(t._id,t.date,t.recipes,merchant);merchant.editTransactions(n,i.ingredientUpdates),banner.createNotification("NEW TRANSACTION CREATED, INGREDIENTS UPDATED ACCORDINGLY")}}).catch(e=>{banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE")}).finally(()=>{t.style.display="none"})}}};var h={display:function(e){document.getElementById("removeOrderBtn").onclick=()=>{this.remove(e)},document.getElementById("orderDetailName").innerText=e.name,document.getElementById("orderDetailDate").innerText=e.date.toLocaleDateString("en-US"),document.getElementById("orderDetailTax").innerText="$"+(e.taxes/100).toFixed(2),document.getElementById("orderDetailFee").innerText="$"+(e.fees/100).toFixed(2);let t=document.getElementById("orderIngredients");for(;t.children.length>0;)t.removeChild(t.firstChild);let n=document.getElementById("orderIngredient").content.children[0],i=0;for(let r=0;r<e.ingredients.length;r++){let l=n.cloneNode(!0),d=e.ingredients[r].pricePerUnit*e.ingredients[r].quantity;i+=d;const a=e.ingredients[r].ingredient;l.children[0].innerText=e.ingredients[r].ingredient.name,l.children[2].innerText="$"+(d/100).toFixed(2);const s=l.children[1];if("bottle"===a.specialUnit){const t=e.ingredients[r].quantity/a.unitSize,n=e.ingredients[r].pricePerUnit*e.ingredients[r].quantity/t;s.innerText=`${t.toFixed(0)} bottles x $${(n/100).toFixed(2)}`}else{const t=a.convert(e.ingredients[r].quantity),n=controller.reconvertPrice(e.ingredients[r].ingredient.unitType,e.ingredients[r].ingredient.unit,e.ingredients[r].pricePerUnit);s.innerText=`${t.toFixed(2)} ${a.unit.toUpperCase()} x $${(n/100).toFixed(2)}`}t.appendChild(l)}document.getElementById("orderDetailTotal").innerText="$"+(i/100).toFixed(2),document.querySelector("#orderTotalPrice p").innerText="$"+((i+e.taxes+e.fees)/100).toFixed(2)},remove:function(e){let t=document.getElementById("loaderContainer");t.style.display="flex",fetch("/order/"+e.id,{method:"DELETE",headers:{"Content-Type":"application/json;charset=utf-8"}}).then(e=>e.json()).then(t=>{if("string"==typeof t)banner.createError(t);else{for(let t=0;t<e.ingredients.length;t++)e.ingredients[t].quantity=-e.ingredients[t].quantity;merchant.editOrders([e],!0),merchant.editIngredients(e.ingredients,!1,!0),banner.createNotification("ORDER REMOVED")}}).catch(e=>{banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE")}).finally(()=>{t.style.display="none"})}};var g={recipe:{},display:function(e){this.recipe=e,document.getElementById("recipeName").style.display="block",document.getElementById("recipeNameIn").style.display="none",document.querySelector("#recipeDetails h1").innerText=e.name;let t=document.getElementById("recipeIngredientList");for(;t.children.length>0;)t.removeChild(t.firstChild);let n=document.getElementById("recipeIngredient").content.children[0];for(let r=0;r<e.ingredients.length;r++)ingredientDiv=n.cloneNode(!0),ingredientDiv.children[0].innerText=e.ingredients[r].ingredient.name,ingredientDiv.children[2].innerText=`${e.ingredients[r].ingredient.convert(e.ingredients[r].quantity).toFixed(2)} ${e.ingredients[r].ingredient.unit}`,ingredientDiv.ingredient=e.ingredients[r].ingredient,ingredientDiv.name=e.ingredients[r].ingredient.name,t.appendChild(ingredientDiv);document.getElementById("addRecIng").style.display="none";let i=document.getElementById("recipePrice");i.children[1].style.display="block",i.children[2].style.display="none",i.children[1].innerText="$"+(e.price/100).toFixed(2),document.getElementById("recipeUpdate").style.display="none",document.getElementById("editRecipeBtn").onclick=()=>{this.edit()},"none"===merchant.pos&&(document.getElementById("removeRecipeBtn").onclick=()=>{this.remove()}),document.getElementById("addRecIng").onclick=()=>{this.displayAddIngredient()},document.getElementById("recipeUpdate").onclick=()=>{this.update()}},edit:function(){let e=document.getElementById("recipeIngredientList");if("none"===merchant.pos){let e=document.getElementById("recipeName"),t=document.getElementById("recipeNameIn");e.style.display="none",t.style.display="block",t.value=this.recipe.name;let n=document.getElementById("recipePrice");n.children[1].style.display="none",n.children[2].style.display="block",n.children[2].value=parseFloat((this.recipe.price/100).toFixed(2))}for(let t=0;t<e.children.length;t++){let n=e.children[t];n.children[2].innerText=this.recipe.ingredients[t].ingredient.unit,n.children[1].style.display="block",n.children[1].value=this.recipe.ingredients[t].ingredient.convert(this.recipe.ingredients[t].quantity).toFixed(2),n.children[3].style.display="block",n.children[3].onclick=()=>{n.parentElement.removeChild(n)}}document.getElementById("addRecIng").style.display="flex",document.getElementById("recipeUpdate").style.display="flex"},update:function(){this.recipe.name=document.getElementById("recipeNameIn").value||this.recipe.name,this.recipe.price=Math.round(100*document.getElementById("recipePrice").children[2].value)||this.recipe.price,this.recipe.ingredients=[];let e=document.getElementById("recipeIngredientList").children;for(let i=0;i<e.length;i++)if("new"===e[i].name){let t=e[i].children[0];this.recipe.ingredients.push({ingredient:t.options[t.selectedIndex].ingredient,quantity:controller.convertToMain(t.options[t.selectedIndex].ingredient.unit,e[i].children[1].value)})}else this.recipe.ingredients.push({ingredient:e[i].ingredient,quantity:controller.convertToMain(e[i].ingredient.unit,e[i].children[1].value)});let t={id:this.recipe.id,name:this.recipe.name,price:this.recipe.price,ingredients:[]};for(let i=0;i<this.recipe.ingredients.length;i++)t.ingredients.push({ingredient:this.recipe.ingredients[i].ingredient.id,quantity:this.recipe.ingredients[i].quantity});let n=document.getElementById("loaderContainer");n.style.display="flex",fetch("/recipe/update",{method:"PUT",headers:{"Content-Type":"application/json;charset=utf-8"},body:JSON.stringify(t)}).then(e=>e.json()).then(e=>{"string"==typeof e?banner.createError(e):(window.merchant.editRecipes([this.recipe]),banner.createNotification("RECIPE UPDATE"))}).catch(e=>{banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE")}).finally(()=>{n.style.display="none"})},remove:function(){fetch("/merchant/recipes/remove/"+this.recipe.id,{method:"DELETE"}).then(e=>e.json()).then(e=>{"string"==typeof e?banner.createError(e):(merchant.editRecipes([this.recipe],!0),banner.createNotification("RECIPE REMOVED"))}).catch(e=>{banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE")})},displayAddIngredient:function(){let e=document.getElementById("addRecIngredient").content.children[0].cloneNode(!0);e.name="new",document.getElementById("recipeIngredientList").appendChild(e);let t=window.merchant.categorizeIngredients();for(let n=0;n<t.length;n++){let i=document.createElement("optgroup");i.label=t[n].name,e.children[0].appendChild(i);for(let e=0;e<t[n].ingredients.length;e++){let r=document.createElement("option");r.innerText=`${t[n].ingredients[e].ingredient.name} (${t[n].ingredients[e].ingredient.unit})`,r.ingredient=t[n].ingredients[e].ingredient,i.appendChild(r)}}}};var u={transaction:{},display:function(e){this.transaction=e;let t=document.getElementById("transactionRecipes"),n=document.getElementById("transactionRecipe").content.children[0],i=0,r=0;for(;t.children.length>0;)t.removeChild(t.firstChild);for(let d=0;d<e.recipes.length;d++){let l=n.cloneNode(!0),a=e.recipes[d].quantity*e.recipes[d].recipe.price;l.children[0].innerText=e.recipes[d].recipe.name,l.children[1].innerText=`${e.recipes[d].quantity} x $${parseFloat(e.recipes[d].recipe.price/100).toFixed(2)}`,l.children[2].innerText="$"+(a/100).toFixed(2),t.appendChild(l),i+=e.recipes[d].quantity,r+=a}let l=`${["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][e.date.getDay()]}, ${["January","Fecbruary","March","April","May","June","July","August","September","October","November","December"][e.date.getMonth()]} ${e.date.getDate()}, ${e.date.getFullYear()}`;document.getElementById("transactionDate").innerText=l,document.getElementById("transactionTime").innerText=e.date.toLocaleTimeString(),document.getElementById("totalRecipes").innerText=i+" recipes",document.getElementById("totalPrice").innerText="$"+(r/100).toFixed(2),"none"===merchant.pos&&(document.getElementById("removeTransBtn").onclick=()=>{this.remove()})},remove:function(){let e=document.getElementById("loaderContainer");e.style.display="flex",fetch("/transaction/"+this.transaction.id,{method:"delete",headers:{"Content-Type":"application/json;charset=utf-8"}}).then(e=>e.json()).then(e=>{if("string"==typeof e)banner.createError(e);else{let e={};for(let t=0;t<this.transaction.recipes.length;t++){let n=this.transaction.recipes[t];for(let t=0;t<n.recipe.ingredients.length;t++)ingredient=n.recipe.ingredients[t],e[ingredient.ingredient.id]=ingredient.quantity*n.quantity}merchant.editTransactions(this.transaction,e,!0),banner.createNotification("TRANSACTION REMOVED")}}).catch(e=>{banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE")}).finally(()=>{e.style.display="none"})}},m=class{constructor(e,t,n,i,r,l,d,a){this.id=e,this.name=t,this.category=n,this.unitType=i,this.unit=r,this.parent=l,d&&(this.specialUnit=d,this.unitSize=a)}convert(e){if("mass"===this.unitType)switch(this.unit){case"g":break;case"kg":e/=1e3;break;case"oz":e/=28.3495;break;case"lb":e/=453.5924}else if("volume"===this.unitType)switch(this.unit){case"ml":e*=1e3;break;case"l":break;case"tsp":e*=202.8842;break;case"tbsp":e*=67.6278;break;case"ozfl":e*=33.8141;break;case"cup":e*=4.1667;break;case"pt":e*=2.1134;break;case"qt":e*=1.0567;break;case"gal":e/=3.7854}else if("length"===this.unitType)switch(this.unit){case"mm":e*=1e3;break;case"cm":e*=100;break;case"m":break;case"in":e*=39.3701;break;case"ft":e*=3.2808}return e}},p=class{constructor(e,t,n,i,r){this.id=e,this.name=t,this.price=n,this.parent=r,this.ingredients=[];for(let l=0;l<i.length;l++)for(let e=0;e<r.ingredients.length;e++)if(i[l].ingredient===r.ingredients[e].ingredient.id){this.ingredients.push({ingredient:r.ingredients[e].ingredient,quantity:i[l].quantity});break}}},y=class{constructor(e,t,n,i){this.id=e,this.parent=i,this.date=new Date(t),this.recipes=[];for(let r=0;r<n.length;r++)for(let e=0;e<i.recipes.length;e++)if(n[r].recipe===i.recipes[e].id){this.recipes.push({recipe:i.recipes[e],quantity:n[r].quantity});break}}},E=class{constructor(e,t){this.name=e.name,this.pos=e.pos,this.ingredients=[],this.recipes=[],this.transactions=[],this.orders=[],this.units={mass:["g","kg","oz","lb"],volume:["ml","l","tsp","tbsp","ozfl","cup","pt","qt","gal"],length:["mm","cm","m","in","ft"],other:["each","bottle"]};for(let n=0;n<e.inventory.length;n++)this.ingredients.push({ingredient:new m(e.inventory[n].ingredient._id,e.inventory[n].ingredient.name,e.inventory[n].ingredient.category,e.inventory[n].ingredient.unitType,e.inventory[n].defaultUnit,this,e.inventory[n].ingredient.specialUnit,e.inventory[n].ingredient.unitSize),quantity:e.inventory[n].quantity});for(let n=0;n<e.recipes.length;n++)this.recipes.push(new p(e.recipes[n]._id,e.recipes[n].name,e.recipes[n].price,e.recipes[n].ingredients,this));for(let n=0;n<t.length;n++)this.transactions.push(new y(t[n]._id,t[n].date,t[n].recipes,this))}editIngredients(e,t=!1,n=!1){for(let i=0;i<e.length;i++){let r=!0;for(let l=0;l<this.ingredients.length;l++)if(this.ingredients[l].ingredient===e[i].ingredient){t&&!n?this.ingredients.splice(l,1):!t&&n?this.ingredients[l].quantity+=e[i].quantity:this.ingredients[l].quantity=e[i].quantity,r=!1;break}r&&this.ingredients.push({ingredient:e[i].ingredient,quantity:parseFloat(e[i].quantity)})}controller.updateData("ingredient"),controller.closeSidebar()}editRecipes(e,t=!1){let n=!0;for(let i=0;i<e.length;i++){for(let r=0;r<this.recipes.length;r++)if(e[i]===this.recipes[r]){t?this.recipes.splice(r,1):this.recipes[r]=e[i],n=!1;break}n&&this.recipes.push(e[i])}controller.updateData("recipe"),controller.closeSidebar()}editOrders(e,t=!1){for(let n=0;n<e.length;n++){let i=!0;for(let r=0;r<this.orders.length;r++)if(e[n]===this.orders[r]){t?this.orders.splice(r,1):this.orders[r]=e[n],i=!1;break}i&&this.orders.push(e[n])}controller.updateData("order"),controller.closeSidebar()}editTransactions(e,t,n=!1){let i=!0;for(let l=0;l<this.transactions.length;l++)if(this.transactions[l]===e){n&&this.transactions.splice(l,1),i=!1;break}i&&(this.transactions.push(e),this.transactions.sort((e,t)=>e.date>t.date?-1:1));let r=Object.keys(t);for(let l=0;l<r.length;l++)for(let e=0;e<this.ingredients.length;e++)if(this.ingredients[e].ingredient.id===r[l]){!1===n?this.ingredients[e].quantity-=t[r[l]]:this.ingredients[e].quantity+=t[r[l]];break}controller.updateData("ingredient"),controller.updateData("transaction"),controller.closeSidebar()}revenue(e){let t=0;for(let n=e[0];n<=e[1];n++)for(let e=0;e<this.transactions[n].recipes.length;e++)for(let i=0;i<this.recipes.length;i++)this.transactions[n].recipes[e].recipe===this.recipes[i]&&(t+=this.transactions[n].recipes[e].quantity*this.recipes[i].price);return t/100}ingredientsSold(e){if(!e)return!1;let t=this.recipesSold(e),n=[];for(let i=0;i<t.length;i++)for(let e=0;e<t[i].recipe.ingredients.length;e++){let r=!1;for(let l=0;l<n.length;l++)if(n[l].ingredient===t[i].recipe.ingredients[e].ingredient){r=!0,n[l].quantity+=t[i].quantity*t[i].recipe.ingredients[e].quantity;break}r||n.push({ingredient:t[i].recipe.ingredients[e].ingredient,quantity:t[i].quantity*t[i].recipe.ingredients[e].quantity})}return n}singleIngredientSold(e,t){let n=0;for(let i=e[0];i<e[1];i++)for(let e=0;e<this.transactions[i].recipes.length;e++)for(let r=0;r<this.transactions[i].recipes[e].recipe.ingredients.length;r++)if(this.transactions[i].recipes[e].recipe.ingredients[r].ingredient===t.ingredient){n+=this.transactions[i].recipes[e].recipe.ingredients[r].quantity;break}return n}recipesSold(e){let t=[];for(let n=e[0];n<=e[1];n++)for(let e=0;e<this.transactions[n].recipes.length;e++){let i=!1;for(let r=0;r<t.length;r++)if(t[r].recipe===this.transactions[n].recipes[e].recipe){i=!0,t[r].quantity+=this.transactions[n].recipes[e].quantity;break}i||t.push({recipe:this.transactions[n].recipes[e].recipe,quantity:this.transactions[n].recipes[e].quantity})}return t}categorizeIngredients(){let e=[];for(let t=0;t<this.ingredients.length;t++){let n=!1;for(let i=0;i<e.length;i++)if(this.ingredients[t].ingredient.category===e[i].name){e[i].ingredients.push(this.ingredients[t]),n=!0;break}n||e.push({name:this.ingredients[t].ingredient.category,ingredients:[this.ingredients[t]]})}return e}unitizeIngredients(){let e=[];for(let t=0;t<this.ingredients.length;t++){let n=!1;const i=this.ingredients[t].ingredient;for(let r=0;r<e.length;r++)if(i.unit===e[r].name||i.specialUnit===e[r].name){e[r].ingredients.push(this.ingredients[t]),n=!0;break}if(!n){let n="";n="bottle"===i.specialUnit?"bottle":i.unit,e.push({name:n,ingredients:[this.ingredients[t]]})}}return e}getRecipesForIngredient(e){let t=[];for(let n=0;n<this.recipes.length;n++)for(let i=0;i<this.recipes[n].ingredients.length;i++)this.recipes[n].ingredients[i].ingredient===e&&t.push(this.recipes[n]);return t}},f=class{constructor(e,t,n,i,r,l,d){this.id=e,this.name=t,this.date=new Date(n),this.taxes=i,this.fees=r,this.ingredients=[],this.parent=d;for(let a=0;a<l.length;a++)for(let e=0;e<d.ingredients.length;e++)if(l[a].ingredient===d.ingredients[e].ingredient.id){this.ingredients.push({ingredient:d.ingredients[e].ingredient,quantity:l[a].quantity,pricePerUnit:l[a].pricePerUnit});break}}};merchant=new E(data.merchant,data.transactions),controller={openStrand:function(d){this.closeSidebar();let a=document.querySelectorAll(".strand");for(let e=0;e<a.length;e++)a[e].style.display="none";let s=document.querySelectorAll(".menuButton");for(let e=0;e<s.length-1;e++)s[e].classList="menuButton",s[e].disabled=!1;let c={};switch(d){case"home":c=document.getElementById("homeBtn"),document.getElementById("homeStrand").style.display="flex",e.display();break;case"ingredients":c=document.getElementById("ingredientsBtn"),document.getElementById("ingredientsStrand").style.display="flex",t.display();break;case"recipeBook":c=document.getElementById("recipeBookBtn"),document.getElementById("recipeBookStrand").style.display="flex",n.display(p);break;case"analytics":c=document.getElementById("analyticsBtn"),document.getElementById("analyticsStrand").style.display="flex",i.display(y);break;case"orders":c=document.getElementById("ordersBtn"),document.getElementById("ordersStrand").style.display="flex",r.display(f);break;case"transactions":c=document.getElementById("transactionsBtn"),document.getElementById("transactionsStrand").style.display="flex",l.display(y)}c.classList="menuButton active",c.disabled=!0,window.screen.availWidth<=1e3&&this.closeMenu()},openSidebar:function(e,t={}){switch(this.closeSidebar(),document.getElementById("sidebarDiv").classList="sidebar",document.getElementById(e).style.display="flex",e){case"ingredientDetails":d.display(t);break;case"addIngredients":addIngredients.display(E);break;case"newIngredient":a.display(m);break;case"recipeDetails":g.display(t);break;case"addRecipe":c.display(p);break;case"orderDetails":h.display(t);break;case"newOrder":s.display(f);break;case"transactionDetails":u.display(t);break;case"newTransaction":o.display(y)}window.screen.availWidth<=1e3&&(document.querySelector(".contentBlock").style.display="none",document.getElementById("mobileMenuSelector").style.display="none",document.getElementById("sidebarCloser").style.display="block")},closeSidebar:function(){let e=document.getElementById("sidebarDiv");for(let t=0;t<e.children.length;t++)e.children[t].style.display="none";e.classList="sidebarHide",window.screen.availWidth<=1e3&&(document.querySelector(".contentBlock").style.display="flex",document.getElementById("mobileMenuSelector").style.display="block",document.getElementById("sidebarCloser").style.display="none")},changeMenu:function(){let e=document.querySelector(".menu"),t=document.querySelectorAll(".menuButton");if(e.classList.contains("menuMinimized")){if(e.classList.contains("menuMinimized")){e.classList="menu";for(let e=0;e<t.length;e++)t[e].children[1].style.display="block";setTimeout(()=>{document.getElementById("max").style.display="flex",document.getElementById("min").style.display="none"},150)}}else{e.classList="menu menuMinimized";for(let e=0;e<t.length;e++)t[e].children[1].style.display="none";document.getElementById("max").style.display="none",document.getElementById("min").style.display="flex"}},openMenu:function(){document.getElementById("menu").style.display="flex",document.querySelector(".contentBlock").style.display="none",document.getElementById("mobileMenuSelector").onclick=()=>{this.closeMenu()}},closeMenu:function(){document.getElementById("menu").style.display="none",document.querySelector(".contentBlock").style.display="flex",document.getElementById("mobileMenuSelector").onclick=()=>{this.openMenu()}},convertToMain:function(e,t){let n=0;if(merchant.units.mass.includes(e))switch(e){case"g":n=t;break;case"kg":n=1e3*t;break;case"oz":n=28.3495*t;break;case"lb":n=453.5924*t}else if(merchant.units.volume.includes(e))switch(e){case"ml":n=t/1e3;break;case"l":n=t;break;case"tsp":n=t/202.8842;break;case"tbsp":n=t/67.6278;break;case"ozfl":n=t/33.8141;break;case"cup":n=t/4.1667;break;case"pt":n=t/2.1134;break;case"qt":n=t/1.0567;break;case"gal":n=3.7854*t}else if(merchant.units.length.includes(e))switch(e){case"mm":n=t/1e3;break;case"cm":n=t/100;break;case"m":n=t;break;case"in":n=t/39.3701;break;case"ft":n=t/3.2808}else n=t;return n},updateData:function(d){switch(d){case"ingredient":e.isPopulated=!1,t.populateByProperty("category"),e.isPopulated=!1;break;case"recipe":l.isPopulated=!1,n.populateRecipes();break;case"order":r.populate();break;case"transaction":l.isPopulated=!1,l.display(y),i.newData=!0}},transactionIndices(e,t,n=new Date){let i=[];for(let r=0;r<e.length;r++)if(e[r].date<n){i.push(r);break}for(let r=e.length-1;r>=0;r--)if(e[r].date>t){i.push(r);break}return!(i.length<2)&&i},convertPrice(e,t,n){if("mass"===e)switch(t){case"g":break;case"kg":n/=1e3;break;case"oz":n/=28.3495;break;case"lb":n/=453.5924}else if("volume"===e)switch(t){case"ml":n*=1e3;break;case"l":break;case"tsp":n*=202.8842;break;case"tbsp":n*=67.6278;break;case"ozfl":n*=33.8141;break;case"cup":n*=4.1667;break;case"pt":n*=2.1134;break;case"qt":n*=1.0567;break;case"gal":n/=3.7854}else if("length"===e)switch(t){case"mm":n*=1e3;break;case"cm":n*=100;break;case"m":break;case"in":n*=39.3701;break;case"ft":n*=3.2808}return n},reconvertPrice(e,t,n){if("mass"===e)switch(t){case"g":break;case"kg":n*=1e3;break;case"oz":n*=28.3495;break;case"lb":n*=453.5924}else if("volume"===e)switch(t){case"ml":n/=1e3;break;case"l":break;case"tsp":n/=202.8842;break;case"tbsp":n/=67.6278;break;case"ozfl":n/=33.8141;break;case"cup":n/=4.1667;break;case"pt":n/=2.1134;break;case"qt":n/=1.0567;break;case"gal":n*=3.7854}else if("length"===e)switch(t){case"mm":n/=1e3;break;case"cm":n/=100;break;case"m":break;case"in":n/=39.3701;break;case"ft":n/=3.2808}return n}},window.screen.availWidth>1e3&&window.screen.availWidth<=1400&&(this.changeMenu(),document.getElementById("menuShifter2").style.display="none"),controller.openStrand("home")}();