| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- let home = {
- isPopulated: false,
- display: function(){
- if(!this.isPopulated){
- this.mostUsedRecipes();
- this.drawInventoryCheckCard();
- this.drawPopularCard();
- this.isPopulated = true;
- }
- },
- mostUsedRecipes: function(){
- let from = new Date();
- from.setDate(from.getDate() - 30);
- let recipes = merchant.getRecipesSold(from, new Date());
- recipes.sort((a, b) => (a.quantity > b.quantity) ? -1 : 1);
- let displayCount = (recipes.length < 10) ? recipes.length : 10;
- let container = document.getElementById("mostUsedRecipeBody");
- while(container.children.length > 0){
- container.removeChild(container.firstChild);
- }
- for(let i = 0; i < displayCount; i++){
- let item = document.createElement("tr");
- item.classList.add("choosable");
- item.onclick = ()=>{
- controller.openStrand("recipeBook");
- controller.openSidebar("recipeDetails", recipes[i].recipe);
- };
- container.appendChild(item);
- let leftText = document.createElement("td");
- leftText.innerText = recipes[i].recipe.name;
- item.appendChild(leftText);
- let centerText = document.createElement("td");
- centerText.innerText = recipes[i].quantity;
- item.appendChild(centerText);
- let rightText = document.createElement("td");
- rightText.innerText = `$${(recipes[i].quantity * recipes[i].recipe.price).toFixed(2)}`;
- item.appendChild(rightText);
- }
- },
- mostUsedIngredients: function(){
- let ingredients = [];
- let from = new Date();
- from.setDate(from.getDate() - 30);
- for(let i = 0; i < merchant.inventory.length; i++){
- let unitCost = merchant.inventory[i].ingredient.getUnitCost();
- let totalCost = unitCost * merchant.inventory[i].getSoldQuantity(from, new Date());
-
- ingredients.push({
- inventoryItem: merchant.inventory[i],
- unitCost: unitCost,
- totalCost: totalCost
- });
- }
- ingredients.sort((a, b) => (a.totalCost > b.totalCost) ? -1 : 1);
- let container = document.getElementById("mostUsedBody");
- while(container.children.length > 0){
- container.removeChild(container.firstChild);
- }
- let displayCount = (merchant.inventory.length < 10) ? merchant.inventory.length : 10;
- for(let i = 0; i < displayCount; i++){
- if(ingredients[i].totalCost === 0) break;
- let item = document.createElement("tr");
- item.classList.add("choosable");
- item.onclick = ()=>{
- controller.openStrand("ingredients");
- controller.openSidebar("ingredientDetails", ingredients[i].inventoryItem);
- }
- container.appendChild(item);
- let leftText = document.createElement("td");
- leftText.innerText = ingredients[i].inventoryItem.ingredient.name;
- item.appendChild(leftText);
- let centerText = document.createElement("td");
- centerText.innerText = `$${ingredients[i].unitCost.toFixed(2)}`;
- item.appendChild(centerText);
- let rightText = document.createElement("td");
- rightText.innerText = `$${ingredients[i].totalCost.toFixed(2)}`;
- item.appendChild(rightText);
- }
- },
- drawInventoryCheckCard: function(){
- let num;
- if(merchant.inventory.length < 5){
- num = merchant.inventory.length;
- }else{
- num = 5;
- }
- let rands = [];
- for(let i = 0; i < num; i++){
- let rand = Math.floor(Math.random() * merchant.inventory.length);
- if(rands.includes(rand)){
- i--;
- }else{
- rands[i] = rand;
- }
- }
- let ul = document.querySelector("#inventoryCheckCard ul");
- let template = document.getElementById("ingredientCheck").content.children[0];
- while(ul.children.length > 0){
- ul.removeChild(ul.firstChild);
- }
- for(let i = 0; i < rands.length; i++){
- let ingredientCheck = template.cloneNode(true);
- let input = ingredientCheck.children[1].children[1];
- const ingredient = merchant.inventory[rands[i]];
- ingredientCheck.ingredient = ingredient;
- ingredientCheck.children[0].innerText = ingredient.ingredient.name;
- ingredientCheck.children[1].children[0].onclick = ()=>{
- input.value--;
- input.changed = true;
- };
- input.value = ingredient.quantity.toFixed(2);
- ingredientCheck.children[2].innerText = ingredient.ingredient.unit.toUpperCase();
-
- ingredientCheck.children[1].children[2].onclick = ()=>{
- input.value++;
- input.changed = true;
- }
- input.onchange = ()=>{input.changed = true};
-
- ul.appendChild(ingredientCheck);
- }
- document.getElementById("inventoryCheck").onclick = ()=>{this.submitInventoryCheck()};
- },
- drawPopularCard: function(){
- let thisMonth = new Date();
- thisMonth.setDate(1);
- const ingredientList = merchant.getIngredientsSold(thisMonth);
- if(ingredientList !== false){
- ingredientList.sort((a, b)=>{
- if(a.quantity < b.quantity){
- return 1;
- }
- if(a.quantity > b.quantity){
- return -1;
- }
- return 0;
- });
- let quantities = [];
- let labels = [];
- let colors = [];
- let count = (ingredientList.length < 5) ? ingredientList.length - 1 : 4;
- for(let i = count; i >= 0; i--){
- const ingredientName = ingredientList[i].ingredient.name;
- const ingredientQuantity = ingredientList[i].quantity;
- const unitName = ingredientList[i].ingredient.unit;
- quantities.push(ingredientList[i].quantity);
- labels.push(`${ingredientName}: ${ingredientQuantity.toFixed(2)} ${unitName.toUpperCase()}`);
- if(i === 0){
- colors.push("rgb(255, 99, 107");
- }else{
- colors.push("rgb(179, 191, 209");
- }
- }
- let trace = {
- x: quantities,
- type: "bar",
- orientation: "h",
- text: labels,
- textposition: "auto",
- hoverinfo: "none",
- marker: {
- color: colors
- }
- }
- let layout = {
- title: {
- text: "MOST POPULAR INGREDIENTS"
- },
- xaxis: {
- zeroline: false,
- title: "QUANTITY"
- },
- yaxis: {
- showticklabels: false
- },
- paper_bgcolor: "rgba(0, 0, 0, 0)"
- }
- if(screen.width < 1200){
- layout.margin = {
- l: 10,
- r: 10,
- t: 80,
- b: 40
- };
- }
-
- Plotly.newPlot("popularIngredientsCard", [trace], layout);
- }else{
- document.getElementById("popularCanvas").style.display = "none";
- let notice = document.createElement("p");
- notice.innerText = "N/A";
- notice.classList = "notice";
- document.getElementById("popularIngredientsCard").appendChild(notice);
- }
- },
- //Need to change the updating of ingredients
- //should update the ingredient directly, then send that. Maybe...
- submitInventoryCheck: function(){
- let lis = document.querySelectorAll("#inventoryCheckCard li");
- let data = [];
- for(let i = 0; i < lis.length; i++){
- if(lis[i].children[1].children[1].value >= 0){
- if(lis[i].children[1].children[1].changed === true){
- let merchIngredient = lis[i].ingredient;
- data.push({
- id: merchIngredient.ingredient.id,
- quantity: lis[i].children[1].children[1].value
- });
- lis[i].children[1].children[1].changed = false;
- }
- }else{
- controller.createBanner("CANNOT HAVE NEGATIVE INGREDIENTS", "error");
- return;
- }
- }
-
- if(data.length > 0){
- let loader = document.getElementById("loaderContainer");
- loader.style.display = "flex";
- fetch("/merchant/ingredients/update", {
- method: "PUT",
- headers: {
- "Content-Type": "application/json;charset=utf-8"
- },
- body: JSON.stringify(data)
- })
- .then(response => response.json())
- .then((response)=>{
- if(typeof(response) === "string"){
- controller.createBanner(response, "error");
- }else{
- for(let i = 0; i < response.length; i++){
- merchant.removeIngredient(merchant.getIngredient(response[i].ingredient._id));
- }
- merchant.addIngredients(response);
- state.updateIngredients();
- controller.createBanner("INGREDIENTS UPDATED", "success");
- }
- })
- .catch((err)=>{
- controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
- })
- .finally(()=>{
- loader.style.display = "none";
- });
- }
- }
- }
- module.exports = home;
|