bundle.js 40 KB

1
  1. !function(){var e,t,n=(e=function(e,t){e.exports={LineGraph:class{constructor(e){this.canvas=e,this.context=e.getContext("2d"),this.left=e.clientWidth-.95*e.clientWidth,this.right=1*e.clientWidth,this.top=e.clientHeight-1*e.clientHeight,this.bottom=.85*e.clientHeight,this.data=[],this.max=0,this.xRange=[],this.colors=[],this.colorIndex=0;for(let t=0;t<100;t++){let e=Math.floor(200*Math.random()),t=Math.floor(200*Math.random()),n=Math.floor(200*Math.random());this.colors.push(`rgb(${e}, ${t}, ${n})`)}}addData(e,t,n){e={set:e,colorIndex:this.colorIndex,name:n},this.colorIndex++,this.data.push(e);let i=!1;for(let r=0;r<e.set.length;r++)e.set[r]>this.max&&(this.max=e.set[r],this.verticalMultiplier=(this.bottom-this.top)/this.max,this.horizontalMultiplier=(this.right-this.left)/(e.set.length-1),i=!0);0===this.xRange.length?(this.xRange=t,i=!0):(t[0]<this.xRange[0]&&(this.xRange[0]=t[0],i=!0),t[1]>this.xRange[1]&&(this.xRange[1]=t[1],i=!0)),i?this.drawGraph():this.drawLine(e)}removeData(e){for(let t=0;t<this.data.length;t++)if(this.data[t].name===e){this.data.splice(t,1);break}this.drawGraph()}clearData(){this.max=0,this.data=[],this.xRange=[]}addTitle(e){this.top=this.canvas.clientHeight-.9*this.canvas.clientHeight,this.title=e}drawGraph(){this.context.clearRect(0,0,this.canvas.width,this.canvas.height),this.drawYAxis(),this.drawXAxis();for(let e=0;e<this.data.length;e++)this.drawLine(this.data[e]);if(this.title){this.context.font="25px Saira";let e=(this.right-this.left)/2-this.context.measureText(this.title).width/2;this.context.fillText(this.title,e,this.top-10)}}drawLine(e){for(let t=0;t<e.set.length-1;t++)this.context.beginPath(),this.context.moveTo(this.left+this.horizontalMultiplier*t,this.bottom-this.verticalMultiplier*e.set[t]),this.context.lineTo(this.left+this.horizontalMultiplier*(t+1),this.bottom-this.verticalMultiplier*e.set[t+1]),this.context.strokeStyle=this.colors[e.colorIndex],this.context.lineWidth=2,this.context.stroke();this.context.strokeStyle="black",this.data.length>1&&this.drawLegend(e.colorIndex,e.name)}drawXAxis(){if(this.context.beginPath(),this.context.moveTo(this.left,this.bottom),this.context.lineTo(this.right,this.bottom),this.context.lineWidth=4,this.context.stroke(),this.context.setLineDash([5,10]),this.context.font="10px Arial",this.context.lineWidth=1,"[object Date]"===Object.prototype.toString.call(this.xRange[0])){let e=Math.abs(Math.floor((Date.UTC(this.xRange[0].getFullYear(),this.xRange[0].getMonth(),this.xRange[0].getDate())-Date.UTC(this.xRange[1].getFullYear(),this.xRange[1].getMonth(),this.xRange[1].getDate()))/864e5))+1,t=new Date(this.xRange[0]);for(let n=0;n<e;n+=Math.floor(e/10))this.context.fillText(t.toLocaleDateString("en-US",{month:"short",day:"numeric",year:"2-digit"}),this.left+this.horizontalMultiplier*n-20,this.bottom+15),0!==n&&(this.context.beginPath(),this.context.moveTo(this.left+this.horizontalMultiplier*n,this.bottom),this.context.lineTo(this.left+this.horizontalMultiplier*n,this.top),this.context.strokeStyle="#a5a5a5",this.context.stroke()),t.setDate(t.getDate()+Math.abs(e/10))}this.context.strokeStyle="black",this.context.setLineDash([])}drawYAxis(){this.context.beginPath(),this.context.moveTo(this.left,this.top),this.context.lineTo(this.left,this.bottom),this.context.lineWidth=2,this.context.stroke(),this.context.setLineDash([5,10]),this.context.font="10px Arial",this.context.lineWidth=1;let e=0,t=(this.bottom-this.top)/10,n=0;do{this.context.fillText(Math.round(e).toString(),this.left-20,this.bottom-n+3),this.context.beginPath(),this.context.moveTo(this.left,this.bottom-n),this.context.lineTo(this.right,this.bottom-n),this.context.strokeStyle="#a5a5a5",this.context.stroke(),n+=t,e+=this.max/10}while(n<=this.bottom-this.top);this.context.strokeStyle="black",this.context.setLineDash([])}drawLegend(e,t){let n;for(let i=0;i<this.data.length;i++)if(this.data[i].name===t){n=25*i;break}this.context.beginPath(),this.context.fillStyle=this.colors[e],this.context.fillRect(this.right+50,this.top+50+n,10,10),this.context.stroke(),this.context.font="15px Arial",this.context.fillText(t,this.right+65,this.top+60+n),this.context.fillStyle="black"}},HorizontalBarGraph:class{constructor(e){this.canvas=e,this.context=e.getContext("2d"),this.left=0,this.right=e.clientWidth,this.top=e.clientHeight-.99*e.clientHeight,this.bottom=e.clientHeight,this.data=[],this.max=0}addData(e){this.context.clearRect(0,0,this.canvas.width,this.canvas.height);for(let t=0;t<e.length;t++)e[t].num>this.max&&(this.max=e[t].num),this.data.push(e[t]);this.drawGraph()}drawGraph(){let e=(this.bottom-this.top)/this.data.length-2;for(let t=0;t<this.data.length;t++){let n=this.top+t*e+5,i=(this.right-this.left)*(this.data[t].num/this.max);this.data[t].num>=this.max?this.context.fillStyle="rgb(255, 99, 107)":this.context.fillStyle="rgb(179, 191, 209)",this.context.beginPath(),this.context.fillRect(this.left,n,i,e-5),this.context.stroke();let r=15;this.context.font="12px Saira",this.context.fillStyle="black",this.context.fillText(this.data[t].label,r,this.top+t*e+e/1.5)}}}}},function(n){return t||e(t={exports:{},parent:n},t.exports),t.exports}),i={isPopulated:!1,graph:{},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(merchant.transactionIndices(t)),l=merchant.revenue(merchant.transactionIndices(n,i));document.getElementById("revenue").innerText=`$${r.toLocaleString("en")}`;let s=(r-l)/l*100,a="";a=s>=0?"/shared/images/upArrow.png":"/shared/images/downArrow.png",document.querySelector("#revenueChange p").innerText=`${Math.abs(s).toFixed(2)}% vs last month`,document.querySelector("#revenueChange img").src=a},drawRevenueGraph:function(){let e=document.getElementById("graphCanvas"),t=new Date;e.height=e.parentElement.clientHeight,e.width=e.parentElement.clientWidth;let i=n({}).LineGraph;this.graph=new i(e),this.graph.addTitle("Revenue");let r=new Date(t);r.setDate(t.getDate()-29);let l=merchant.graphDailyRevenue(merchant.transactionIndices(r));if(l)this.graph.addData(l,[r,new Date],"Revenue");else{document.getElementById("graphCanvas").style.display="none";let e=document.createElement("h1");e.innerText="NO DATA YET",e.classList="notice",document.getElementById("graphCard").appendChild(e)}},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];e.ingredient=merchant.ingredients[t[r]],e.children[0].innerText=merchant.ingredients[t[r]].ingredient.name,e.children[1].children[0].onclick=(()=>{l.value--}),l.value=merchant.ingredients[t[r]].quantity.toFixed(2),e.children[1].children[2].onclick=(()=>{l.value++}),e.children[2].innerText=merchant.ingredients[t[r]].ingredient.unit.toUpperCase(),n.appendChild(e)}document.getElementById("inventoryCheck").onclick=(()=>{this.submitInventoryCheck()})},drawPopularCard:function(){let e=[],t=new Date,i=new Date(t.getFullYear(),t.getMonth(),1),r=merchant.ingredientsSold(merchant.transactionIndices(i));if(!1!==r){window.ingredientList=[...r];let t=r.length<5?r.length:5;for(let n=0;n<t;n++)try{let t=r[0].quantity,n=0;for(let e=0;e<r.length;e++)r[e].quantity>t&&(t=r[e].quantity,n=e);e.push({num:t,label:r[n].ingredient.name+": "+r[n].ingredient.convert(r[n].quantity).toFixed(2)+" "+r[n].ingredient.unit}),r.splice(n,1)}catch(l){break}let i=document.getElementById("popularCanvas");i.width=.8*i.parentElement.offsetWidth,i.height=.8*i.parentElement.offsetHeight,new(0,n({}).HorizontalBarGraph)(i).addData(e)}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 r=0;r<e.length;r++){if(!(e[r].children[1].children[1].value>=0))return void banner.createError("CANNOT HAVE NEGATIVE INGREDIENTS");{let i=e[r].ingredient,l=parseFloat(e[r].children[1].children[1].value);l!==i.quantity&&(t.push({id:i.ingredient.id,ingredient:i.ingredient,quantity:l}),n.push({id:i.ingredient.id,quantity:l}))}}let i=document.getElementById("loaderContainer");i.style.display="flex",n.length>0&&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(()=>{i.style.display="none"})}},r={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,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],s=r.cloneNode(!0);s.children[0].innerText=i.ingredient.name,s.children[2].innerText=`${i.ingredient.convert(i.quantity).toFixed(2)} ${i.ingredient.unit.toUpperCase()}`,s.onclick=(()=>{controller.openSidebar("ingredientDetails",i)}),s._name=i.ingredient.name.toLowerCase(),s._unit=i.ingredient.unit.toLowerCase(),e.children[1].appendChild(s),this.ingredients.push(s)}}}},l={display:function(){this.isPopulated||(this.populateRecipes(),"clover"===merchant.pos&&(document.getElementById("posUpdateRecipe").onclick=(()=>{this.posUpdate()})),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)}}},s=class{constructor(e,t,n,i,r){this.id=e,this.name=t,this.date=new Date(n),this.ingredients=[],this.parent=r;for(let l=0;l<i.length;l++)for(let e=0;e<r.ingredients.length;e++)i[l].ingredient===r.ingredients[e].ingredient.id&&this.ingredients.push({ingredient:r.ingredients[e].ingredient,quantity:i[l].quantity,price:i[l].price})}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}},a={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].ingredients,merchant));merchant.editOrders(n),document.getElementById("orderSubmitForm").onsubmit=(()=>{this.submitFilter(e)}),this.isFetched=!0}}).catch(e=>{console.log(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++)i+=merchant.orders[r].ingredients[e].quantity*merchant.orders[r].ingredients[e].price;n.children[0].innerText=merchant.orders[r].name,n.children[1].innerText=`${merchant.orders[r].ingredients.length} items`,n.children[2].innerText=new Date(merchant.orders[r].date).toLocaleDateString("en-US"),n.children[3].innerText=`$${(i/100).toFixed(2)}`,n.order=merchant.orders[r],n.onclick=(()=>{controller.openSidebar("orderDetails",merchant.orders[r])}),e.appendChild(n)}}},d={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,s=new Date(l.getFullYear(),l.getMonth()-1,l.getDate());for(document.getElementById("transFilDate1").valueAsDate=s,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),s=0,a=0,d=new e(t[r]._id,t[r].date,t[r].recipes,merchant);for(let e=0;e<d.recipes.length;e++)s+=d.recipes[e].quantity,a+=d.recipes[e].quantity*d.recipes[e].recipe.price;l.children[0].innerText=`${d.date.toLocaleDateString()} ${d.date.toLocaleTimeString()}`,l.children[1].innerText=`${s} recipes sold`,l.children[2].innerText=`$${(a/100).toFixed(2)}`,l.onclick=(()=>{controller.openSidebar("transactionDetails",d)}),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"))}},o={isPopulated:!1,fakeMerchant:{},chosenIngredients:[],display:function(e){if(!this.isPopulated){let t=document.getElementById("loaderContainer");t.style.display="flex",fetch("/ingredients").then(e=>e.json()).then(t=>{if("string"==typeof t)banner.createError(t);else{for(let e=0;e<merchant.ingredients.length;e++)for(let n=0;n<t.length;n++)if(merchant.ingredients[e].ingredient.id===t[n]._id){t.splice(n,1);break}for(let e=0;e<t.length;e++)t[e]={ingredient:t[e]};this.fakeMerchant=new e({name:"none",inventory:t,recipes:[]},[]),this.populateAddIngredients(!0)}}).catch(e=>{banner.createError("UNABLE TO RETRIEVE DATA")}).finally(()=>{t.style.display="none"}),this.isPopulated=!0}},populateAddIngredients:function(e=!1){let t=document.getElementById("addIngredientList"),n=document.getElementById("addIngredientsCategory"),i=document.getElementById("addIngredientsIngredient"),r=this.fakeMerchant.categorizeIngredients();for(;t.children.length>0;)t.removeChild(t.firstChild);for(let l=0;l<r.length;l++){let e=n.content.children[0].cloneNode(!0);e.children[0].children[0].innerText=r[l].name,e.children[0].children[1].onclick=(()=>{this.toggleAddIngredient(e)}),e.children[1].style.display="none",e.children[0].children[1].children[1].style.display="none",t.appendChild(e);for(let t=0;t<r[l].ingredients.length;t++){let n=i.content.children[0].cloneNode(!0);n.children[0].innerText=r[l].ingredients[t].ingredient.name,n.children[2].onclick=(()=>{this.addOne(n)}),n.ingredient=r[l].ingredients[t].ingredient,e.children[1].appendChild(n)}}if(e){let e=document.getElementById("myIngredients");for(;e.children.length>0;)e.removeChild(e.firstChild)}document.getElementById("addIngredientsBtn").onclick=(()=>{this.submit()}),document.getElementById("openNewIngredient").onclick=(()=>{controller.openSidebar("newIngredient")})},toggleAddIngredient:function(e){let t=e.children[0].children[1],n=e.children[1];"none"===n.style.display?(n.style.display="flex",t.children[0].style.display="none",t.children[1].style.display="block"):(n.style.display="none",t.children[0].style.display="block",t.children[1].style.display="none")},addOne:function(e){e.parentElement.removeChild(e),document.getElementById("myIngredients").appendChild(e),document.getElementById("myIngredientsDiv").style.display="flex";for(let r=0;r<this.fakeMerchant.ingredients.length;r++)if(this.fakeMerchant.ingredients[r].ingredient===e.ingredient){this.fakeMerchant.ingredients.splice(r,1),this.chosenIngredients.push(e.ingredient);break}let t=document.createElement("input");t.type="number",t.min="0",t.step="0.01",t.placeholder="QUANTITY",e.insertBefore(t,e.children[1]);let n=e.children[2];n.style.display="block";let i=merchant.units[e.ingredient.unitType];for(let r=0;r<i.length;r++){let t=document.createElement("option");t.innerText=i[r].toUpperCase(),t.type=e.ingredient.unitType,t.value=i[r],n.appendChild(t)}e.children[3].innerText="-",e.children[3].onclick=(()=>{this.removeOne(e)})},removeOne:function(e){e.parentElement.removeChild(e),e.removeChild(e.children[1]);let t=e.children[1];for(;t.children.length>0;)t.removeChild(t.firstChild);t.style.display="none",e.children[2].innerText="+",e.children[2].onclick=(()=>{this.addOne(e)}),0===document.getElementById("myIngredients").children.length&&(document.getElementById("myIngredientsDiv").style.display="none");for(let n=0;n<this.chosenIngredients.length;n++)if(this.chosenIngredients[n]===e.ingredient){this.chosenIngredients.splice(n,1),this.fakeMerchant.ingredients.push({ingredient:e.ingredient});break}this.populateAddIngredients()},submit:function(){let e=document.getElementById("myIngredients").children,t=[],n=[];for(let r=0;r<e.length;r++){let i=e[r].children[1].value,l=e[r].children[2].value;if(""===i)return void banner.createError("PLEASE ENTER A QUANTITY FOR EACH INGREDIENT YOU WANT TO ADD TO YOUR INVENTORY");i=controller.convertToMain(l,i);let s={ingredient:e[r].ingredient,quantity:i};s.ingredient.unit=l,t.push(s),n.push({id:e[r].ingredient.id,quantity:i,defaultUnit:l})}let i=document.getElementById("loaderContainer");i.style.display="flex",fetch("/merchant/ingredients/add",{method:"POST",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),this.isPopulated=!1,banner.createNotification("ALL INGREDIENTS ADDED"))}).catch(e=>{banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE")}).finally(()=>{i.style.display="none"})}},c={display:function(e){this.ingredient=e,document.getElementById("editIngBtn").onclick=(()=>{this.edit()}),document.getElementById("removeIngBtn").onclick=(()=>{this.remove(merchant)}),document.querySelector("#ingredientDetails p").innerText=e.ingredient.category,document.querySelector("#ingredientDetails h1").innerText=e.ingredient.name;let t=document.getElementById("ingredientStock");t.innerText=`${e.ingredient.convert(e.quantity).toFixed(2)} ${e.ingredient.unit.toUpperCase()}`,t.style.display="block";let n=document.getElementById("ingredientInput");n.value=e.ingredient.convert(e.quantity).toFixed(2),n.style.display="none";let i=[],r=new Date;for(let g=1;g<31;g++){let t=new Date(r.getFullYear(),r.getMonth(),r.getDate()-g),n=new Date(r.getFullYear(),r.getMonth(),r.getDate()-g-1),l=merchant.transactionIndices(n,t);!1===l?i.push(0):i.push(merchant.singleIngredientSold(l,e))}let l=0;for(let g=0;g<i.length;g++)l+=i[g];this.dailyUse=l/i.length,document.getElementById("dailyUse").innerText=`${e.ingredient.convert(this.dailyUse).toFixed(2)} ${e.ingredient.unit}`;let s=document.getElementById("ingredientRecipeList"),a=merchant.getRecipesForIngredient(e.ingredient);for(;s.children.length>0;)s.removeChild(s.firstChild);for(let g=0;g<a.length;g++){let e=document.createElement("li");e.innerText=a[g].name,e.onclick=(()=>{controller.openStrand("recipeBook"),controller.openSidebar("recipeDetails",a[g])}),s.appendChild(e)}let d=document.getElementById("ingredientButtons"),o=[],c=document.getElementById("displayUnitLabel"),h=document.getElementById("defaultUnit");for("other"!==this.ingredient.ingredient.unitType?(o=merchant.units[this.ingredient.ingredient.unitType],c.style.display="block",h.style.display="block"):(c.style.display="none",h.style.display="none");d.children.length>0;)d.removeChild(d.firstChild);for(let g=0;g<o.length;g++){let e=document.createElement("button");e.classList.add("unitButton"),e.innerText=o[g].toUpperCase(),e.onclick=(()=>{this.changeUnit(e,o[g])}),d.appendChild(e),o[g]===this.ingredient.ingredient.unit&&e.classList.add("unitActive")}document.getElementById("defaultUnit").onclick=(()=>{this.changeUnitDefault()}),document.getElementById("editSubmitButton").onclick=(()=>{this.editSubmit()})}},h={display:function(e){document.getElementById("newIngName").value="",document.getElementById("newIngCategory").value="",document.getElementById("newIngQuantity").value=0,document.getElementById("submitNewIng").onclick=(()=>{this.submit(e)})}},g={display:function(e){if(!this.isPopulated){let t=merchant.categorizeIngredients(),n=document.getElementById("newOrderCategories"),i=document.getElementById("addIngredientsCategory").content.children[0],r=document.getElementById("addIngredientsIngredient").content.children[0];for(let e=0;e<t.length;e++){let l=i.cloneNode(!0);l.children[0].children[0].innerText=t[e].name,l.children[0].children[1].onclick=(()=>{this.toggleAddIngredient(l)}),l.children[0].children[1].children[1].style.display="none",l.children[1].style.display="none",n.appendChild(l);for(let n=0;n<t[e].ingredients.length;n++){let i=r.cloneNode(!0);i.children[0].innerText=t[e].ingredients[n].ingredient.name,i.children[2].onclick=(()=>{this.addOne(i,l.children[1])}),i.ingredient=t[e].ingredients[n].ingredient,this.unused.push(t[e].ingredients[n]),l.children[1].appendChild(i)}}document.getElementById("submitNewOrder").onclick=(()=>{this.submit(e)}),this.isPopulated=!0}}},m={display:function(e){console.log("display");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").onclick=(()=>{this.changeRecipeCount()}),document.getElementById("submitNewRecipe").onclick=(()=>{this.submit(e)})}},p={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)})}},u={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("orderDetailTime").innerText=e.date.toLocaleTimeString("en-US");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),s=e.ingredients[r].quantity*e.ingredients[r].price/100;i+=s;let a=e.ingredients[r].ingredient,d=a.convert(e.ingredients[r].quantity).toFixed(2)+" "+a.unit.toUpperCase()+" x $"+(e.convertPrice(a.unitType,a.unit,e.ingredients[r].price)/100).toFixed(2);l.children[0].innerText=e.ingredients[r].ingredient.name,l.children[1].innerText=d,l.children[2].innerText=`$${s.toFixed(2)}`,t.appendChild(l)}document.querySelector("#orderTotalPrice p").innerText=`$${i.toFixed(2)}`}},y={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()}),document.getElementById("removeRecipeBtn").onclick=(()=>{this.remove()}),document.getElementById("addRecIng").onclick=(()=>{this.displayAddIngredient()}),document.getElementById("recipeUpdate").onclick=(()=>{this.update()})}},f={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 s=0;s<e.recipes.length;s++){let l=n.cloneNode(!0),a=e.recipes[s].quantity*e.recipes[s].recipe.price;l.children[0].innerText=e.recipes[s].recipe.name,l.children[1].innerText=`${e.recipes[s].quantity} x $${parseFloat(e.recipes[s].recipe.price/100).toFixed(2)}`,l.children[2].innerText=`$${(a/100).toFixed(2)}`,t.appendChild(l),i+=e.recipes[s].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)}`,document.getElementById("removeTransBtn").onclick=(()=>{this.remove()})}},I=class{constructor(e,t,n,i,r,l){this.id=e,this.name=t,this.category=n,this.unitType=i,this.unit=r,this.parent=l}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}},b=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}}},x=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","foot"],other:["each"]};for(let n=0;n<e.inventory.length;n++)this.ingredients.push({ingredient:new I(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),quantity:e.inventory[n].quantity});for(let n=0;n<e.recipes.length;n++)this.recipes.push(new b(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 x(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<merchant.ingredients.length;l++)if(merchant.ingredients[l].ingredient===e[i].ingredient){t?merchant.ingredients.splice(l,1):!t&&n?merchant.ingredients[l].quantity+=e[i].quantity:merchant.ingredients[l].quantity=e[i].quantity,r=!1;break}r&&merchant.ingredients.push({ingredient:e[i].ingredient,quantity:parseFloat(e[i].quantity),defaultUnit:e[i].defaultUnit})}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&&merchant.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=!1){let n=!0;for(let i=0;i<this.transactions.length;i++)if(this.transactions[i]===e){t&&this.transactions.splice(i,1),n=!1;break}n&&(this.transactions.push(e),this.transactions.sort((e,t)=>e.date>t.date?1:-1)),controller.updateData("transaction"),controller.closeSidebar()}transactionIndices(e,t=new Date){let n=[];for(let i=0;i<this.transactions.length;i++)if(this.transactions[i].date>e){n.push(i);break}for(let i=this.transactions.length-1;i>=0;i--)if(this.transactions[i].date<t){n.push(i);break}return!(n.length<2)&&n}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}graphDailyRevenue(e){if(!e)return!1;let t=new Array(30).fill(0),n=this.transactions[e[0]].date,i=0;for(let r=e[0];r<=e[1];r++){this.transactions[r].date.getDate()!==n.getDate()&&(n=this.transactions[r].date,i++);for(let e=0;e<this.transactions[r].recipes.length;e++)t[i]+=this.transactions[r].recipes[e].recipe.price/100*this.transactions[r].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;for(let i=0;i<e.length;i++)if(this.ingredients[t].ingredient.unit===e[i].name){e[i].ingredients.push(this.ingredients[t]),n=!0;break}n||e.push({name:this.ingredients[t].ingredient.unit,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}};merchant=new E(data.merchant,data.transactions),controller={openStrand:function(e){this.closeSidebar();let t=document.querySelectorAll(".strand");for(let i=0;i<t.length;i++)t[i].style.display="none";let n=document.querySelectorAll(".menuButton");for(let i=0;i<n.length-1;i++)n[i].classList="menuButton",n[i].disabled=!1;let o={};switch(e){case"home":o=document.getElementById("homeBtn"),document.getElementById("homeStrand").style.display="flex",i.display();break;case"ingredients":o=document.getElementById("ingredientsBtn"),document.getElementById("ingredientsStrand").style.display="flex",r.display();break;case"recipeBook":o=document.getElementById("recipeBookBtn"),document.getElementById("recipeBookStrand").style.display="flex",l.display();break;case"orders":o=document.getElementById("ordersBtn"),document.getElementById("ordersStrand").style.display="flex",a.display(s);break;case"transactions":o=document.getElementById("transactionsBtn"),document.getElementById("transactionsStrand").style.display="flex",d.display(x)}o.classList="menuButton active",o.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":c.display(t);break;case"addIngredients":o.display(E);break;case"newIngredient":h.display(I);break;case"recipeDetails":y.display(t);break;case"addRecipe":m.display(b);break;case"orderDetails":u.display(t);break;case"newOrder":g.display(s);break;case"transactionDetails":f.display(t);break;case"newTransaction":p.display(x)}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(e){switch(e){case"ingredient":i.drawInventoryCheckCard(),r.populateByProperty("category"),o.isPopulated=!1;break;case"recipe":d.isPopulated=!1,l.populateRecipes();break;case"order":a.populate();break;case"transaction":d.isPopulated=!1,d.display(x);break;case"unit":i.isPopulated=!1,r.populateByProperty("category")}}},window.screen.availWidth>1e3&&window.screen.availWidth<=1400&&(this.changeMenu(),document.getElementById("menuShifter2").style.display="none"),controller.openStrand("home")}();