Просмотр исходного кода

Replace revenue graph with list of most used ingredients.

Lee Morgan 5 лет назад
Родитель
Сommit
a47f62f172

+ 5 - 1
views/dashboardPage/ejs/strands/home.ejs

@@ -16,7 +16,11 @@
             </div>
             </div>
         </div>
         </div>
 
 
-        <div id="graphCard" class="card"></div>
+        <div class="card">
+            <h3>MOST USED INGREDIENTS (30 DAYS)</h3>
+
+            <div id="mostUsedList"></div>
+        </div>
     </div>
     </div>
 
 
     <div class="flexRow">
     <div class="flexRow">

+ 47 - 42
views/dashboardPage/js/classes/Merchant.js

@@ -4,9 +4,10 @@ const Transaction = require("./Transaction.js");
 const Order = require("./Order.js");
 const Order = require("./Order.js");
 
 
 class MerchantIngredient{
 class MerchantIngredient{
-    constructor(ingredient, quantity){
+    constructor(ingredient, quantity, parent){
         this._quantity = quantity;
         this._quantity = quantity;
         this._ingredient = ingredient;
         this._ingredient = ingredient;
+        this._parent = parent;
     }
     }
 
 
     get ingredient(){
     get ingredient(){
@@ -44,6 +45,48 @@ class MerchantIngredient{
     getQuantityDisplay(){
     getQuantityDisplay(){
         return `${this.quantity.toFixed(2)} ${this._ingredient.unit.toUpperCase()}`;
         return `${this.quantity.toFixed(2)} ${this._ingredient.unit.toUpperCase()}`;
     }
     }
+
+    /*
+    Gets the quantity of a single ingredient sold between two dates
+    Inputs:
+        from = start Date
+        to = end Date
+    return: quantity sold in default unit
+    */
+    getSoldQuantity(from, to){
+        const {start, end} = this._parent.getTransactionIndices(from, to);
+
+        let traverseIngredient = (ingredient)=>{
+            if(ingredient === this.ingredient) return true;
+
+            for(let i = 0; i < ingredient.subIngredients.length; i++){
+                let next = traverseIngredient(ingredient.subIngredients[i].ingredient);
+
+                if(next === true){
+                    return ingredient.subIngredients[i].quantity;
+                }else{
+                    return ingredient.subIngredients[i].quantity * next;
+                }
+            }
+
+            return 0;
+        }
+
+        let total = 0;
+
+        for(let i = start; i <= end; i++){
+            for(let j = 0; j < this._parent.transactions[i].recipes.length; j++){
+                let transactionRecipe = this._parent.transactions[i].recipes[j];
+                for(let k = 0; k < transactionRecipe.recipe.ingredients.length; k++){
+                    let ingredient = transactionRecipe.recipe.ingredients[k];
+                    let thing = traverseIngredient(ingredient.ingredient) * ingredient.quantity * transactionRecipe.quantity;
+                    total += thing;
+                }
+            }
+        }
+
+        return total;
+    }
 }
 }
 
 
 class Merchant{
 class Merchant{
@@ -85,6 +128,7 @@ class Merchant{
             const merchantIngredient = new MerchantIngredient(
             const merchantIngredient = new MerchantIngredient(
                 ingredient,
                 ingredient,
                 ingredients[i].quantity,
                 ingredients[i].quantity,
+                this
             );
             );
 
 
             this._inventory.push(merchantIngredient);
             this._inventory.push(merchantIngredient);
@@ -230,7 +274,7 @@ class Merchant{
 
 
             createdIngredient.replaceIngredients(ingredient.ingredients);
             createdIngredient.replaceIngredients(ingredient.ingredients);
 
 
-            const merchantIngredient = new MerchantIngredient(createdIngredient, quantity);
+            const merchantIngredient = new MerchantIngredient(createdIngredient, quantity, this);
             this._inventory.push(merchantIngredient);
             this._inventory.push(merchantIngredient);
         }
         }
     }
     }
@@ -301,9 +345,7 @@ class Merchant{
             return [];
             return [];
         }
         }
 
 
-        if(from === 0){
-            from = this._transactions[this._transactions.length-1].date;
-        }
+        if(from === 0) from = this._transactions[this._transactions.length-1].date;
 
 
         const {start, end} = this.getTransactionIndices(from, to);
         const {start, end} = this.getTransactionIndices(from, to);
 
 
@@ -484,43 +526,6 @@ class Merchant{
         return ingredientList;
         return ingredientList;
     }
     }
 
 
-    /*
-    Gets the quantity of a single ingredient sold between two dates
-    Inputs:
-        ingredient = MerchantIngredient object to find
-        from = start Date
-        to = end Date
-    return: quantity sold in default unit
-    */
-    getSingleIngredientSold(ingredient, from, to = new Date()){
-        const {start, end} = this.getTransactionIndices(from, to);
-
-        let checkIngredient = (current, main)=>{
-            if(current.ingredient === main) return current.quantity;
-            
-            for(let i = 0; i < current.ingredient.subIngredients.length; i++){
-                return checkIngredient(current.ingredient.subIngredients[i], main);
-            }
-
-            return 0;
-        }
-
-        let total = 0;
-
-        for(let i = start; i < end; i++){
-            for(let j = 0; j < this._transactions[i].recipes.length; j++){
-                let transactionRecipe = this._transactions[i].recipes[j];
-                for(let k = 0; k < transactionRecipe.recipe.ingredients.length; k++){
-                    let recipeIngredient = transactionRecipe.recipe.ingredients[k];
-                    
-                    total += checkIngredient(recipeIngredient, ingredient);
-                }
-            }
-        }
-
-        return total;
-    }
-
     /*
     /*
     Gets the number of recipes sold between two dates (dateRange)
     Gets the number of recipes sold between two dates (dateRange)
     Inputs:
     Inputs:

+ 1 - 3
views/dashboardPage/js/classes/Transaction.js

@@ -45,9 +45,7 @@ class Transaction{
     */
     */
     getIngredientQuantity(ingredient){
     getIngredientQuantity(ingredient){
         let traverseIngredient = (transIngredient)=>{
         let traverseIngredient = (transIngredient)=>{
-            if(transIngredient === ingredient){
-                return true;
-            }
+            if(transIngredient === ingredient) return true;
 
 
             for(let i = 0; i < transIngredient.subIngredients.length; i++){
             for(let i = 0; i < transIngredient.subIngredients.length; i++){
                 let next = traverseIngredient(transIngredient.subIngredients[i].ingredient);
                 let next = traverseIngredient(transIngredient.subIngredients[i].ingredient);

+ 5 - 1
views/dashboardPage/js/dashboard.js

@@ -404,11 +404,13 @@ window.state = {
         ingredients.populateByProperty();
         ingredients.populateByProperty();
         analytics.isPopulated = false;
         analytics.isPopulated = false;
         home.drawInventoryCheckCard();
         home.drawInventoryCheckCard();
+        home.drawMostUsedCard();
     },
     },
 
 
     updateRecipes: function(){
     updateRecipes: function(){
         recipeBook.populateRecipes();
         recipeBook.populateRecipes();
         analytics.isPopulated = false;
         analytics.isPopulated = false;
+        home.drawMostUsedCard();
     },
     },
 
 
     updateTransactions: function(transaction){
     updateTransactions: function(transaction){
@@ -416,11 +418,13 @@ window.state = {
         ingredients.populateByProperty();
         ingredients.populateByProperty();
         analytics.isPopulated = false;
         analytics.isPopulated = false;
         home.drawRevenueGraph();
         home.drawRevenueGraph();
+        home.drawMostUsedCard();
     },
     },
 
 
     updateOrders: function(newOrders){
     updateOrders: function(newOrders){
         ingredients.isPopulated = false;
         ingredients.isPopulated = false;
-        ordersStrand.isPopulated = false;
+        orders.isPopulated = false;
+        home.drawMostUsedCard();
         if(newOrders !== undefined) orders.orders = newOrders;
         if(newOrders !== undefined) orders.orders = newOrders;
     },
     },
 
 

+ 0 - 19
views/dashboardPage/js/sidebars/ingredientDetails.js

@@ -8,25 +8,6 @@ let ingredientDetails = {
         document.getElementById("ingredientDetailsName").innerText = ingredient.ingredient.name;
         document.getElementById("ingredientDetailsName").innerText = ingredient.ingredient.name;
         document.getElementById("ingredientStock").innerText = ingredient.getQuantityDisplay();
         document.getElementById("ingredientStock").innerText = ingredient.getQuantityDisplay();
 
 
-        //Calculate and display average daily use
-        // let quantities = [];
-        // let now = new Date();
-        // for(let i = 1; i < 31; i++){
-        //     let endDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i)
-        //     let startDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i - 1);
-
-        //     quantities.push(merchant.getSingleIngredientSold(ingredient, startDay, endDay));
-        // }
-
-        // let sum = 0;
-        // for(let i = 0; i < quantities.length; i++){
-        //     sum += quantities[i];
-        // }
-
-        // let dailyUse = sum / quantities.length;
-        // const dailyUseDiv = document.getElementById("dailyUse");
-        // dailyUseDiv.innerText = `${dailyUse.toFixed(2)} ${ingredient.ingredient.unit.toUpperCase()}`;
-
         let unitCostElement = document.getElementById("unitCostDisplay");
         let unitCostElement = document.getElementById("unitCostDisplay");
         let unitCost = ingredient.ingredient.getUnitCost();
         let unitCost = ingredient.ingredient.getUnitCost();
         if(unitCost === 0){
         if(unitCost === 0){

+ 34 - 45
views/dashboardPage/js/strands/home.js

@@ -4,7 +4,6 @@ let home = {
     display: function(){
     display: function(){
         if(!this.isPopulated){
         if(!this.isPopulated){
             this.drawRevenueCard();
             this.drawRevenueCard();
-            this.drawRevenueGraph();
             this.drawInventoryCheckCard();
             this.drawInventoryCheckCard();
             this.drawPopularCard();
             this.drawPopularCard();
 
 
@@ -35,58 +34,48 @@ let home = {
         document.querySelector("#revenueChange img").src = img;
         document.querySelector("#revenueChange img").src = img;
     },
     },
 
 
-    drawRevenueGraph: function(){
-        let monthAgo = new Date();
-        monthAgo.setMonth(monthAgo.getMonth() - 1);
-        
-        let revenue = [];
-        let dates = [];
-        let dayRevenue = 0;
-        const transactions = merchant.getTransactions(monthAgo);
-        let currentDate = (transactions.length > 0) ? transactions[0].date : undefined;
-        for(let i = 0; i < transactions.length; i++){
-            if(transactions[i].date.getDate() !== currentDate.getDate()){
-                revenue.push(dayRevenue / 100);
-                dayRevenue = 0;
-                dates.push(currentDate);
-                currentDate = transactions[i].date;
-            }
-
-            for(let j = 0; j < transactions[i].recipes.length; j++){
-                const recipe = transactions[i].recipes[j];
+    drawMostUsedCard: function(){
+        let ingredients = [];
+        let from = new Date();
+        from.setDate(from.getDate() - 30);
 
 
-                dayRevenue += recipe.recipe.price * recipe.quantity;
-            }
+        for(let i = 0; i < merchant.inventory.length; i++){
+            let thing = merchant.inventory[i].getSoldQuantity(from, new Date());
+            let otherThing = merchant.inventory[i].ingredient.getUnitCost();
+            let cost = thing * otherThing;
+            
+            ingredients.push({
+                inventoryItem: merchant.inventory[i],
+                unitCost: cost
+            });
         }
         }
 
 
-        const trace = {
-            x: dates,
-            y: revenue,
-            mode: "lines+markers",
-            line: {
-                color: "rgb(255, 99, 107)"
-            }
-        }
+        ingredients.sort((a, b) => (a.unitCost > b.unitCost) ? -1 : 1);
+        let container = document.getElementById("mostUsedList");
 
 
-        let layout = {
-            title: "REVENUE",
-            xaxis: {
-                title: "DATE"
-            },
-            yaxis: {
-                title: "$"
-            },
-            paper_bgcolor: "rgba(0, 0, 0, 0)"
+        while(container.children.length > 0){
+            container.removeChild(container.firstChild);
         }
         }
 
 
-        if(screen.width < 1200){
-            layout.margin = {
-                l: 35,
-                r: 0
+        let displayCount = (merchant.inventory.length < 5) ? merchant.inventory.length : 5;
+
+        for(let i = 0; i < displayCount; i++){
+            let item = document.createElement("button");
+            item.classList.add("choosable");
+            item.onclick = ()=>{
+                controller.openStrand("ingredients");
+                controller.openSidebar("ingredientDetails", ingredients[i].inventoryItem);
             }
             }
-        }
+            container.appendChild(item);
+
+            let leftText = document.createElement("p");
+            leftText.innerText = ingredients[i].inventoryItem.ingredient.name;
+            item.appendChild(leftText);
 
 
-        Plotly.newPlot("graphCard", [trace], layout);
+            let rightText = document.createElement("p");
+            rightText.innerText = `$${ingredients[i].unitCost.toFixed(2)}`;
+            item.appendChild(rightText);
+        }
     },
     },
 
 
     drawInventoryCheckCard: function(){
     drawInventoryCheckCard: function(){