Переглянути джерело

Add 3 tables to data strand

Lee Morgan 6 роки тому
батько
коміт
24d734ae38

+ 16 - 0
controllers/transactionData.js

@@ -1,4 +1,5 @@
 const Transaction = require("../models/transaction");
+const Purchase = require("../models/purchase");
 
 module.exports = {
     getTransactions: function(req, res){
@@ -18,6 +19,21 @@ module.exports = {
             .catch((err)=>{
                 return res.json("Error: could not retrieve sales data");
             });
+    },
+
+    getPurchases: function(req, res){
+        if(!req.session.user){
+            req.session.error = "You must be logged in to view that page";
+            return res.redirect("/");
+        }
+
+        Purchase.find({merchant: req.session.user})
+            .then((purchases)=>{
+                return res.json(purchases);
+            })
+            .catch((err)=>{
+                return res.json("Error: could not retrieve purchases data");
+            })
     }
 }
 

+ 1 - 0
routes.js

@@ -42,4 +42,5 @@ module.exports = function(app){
 
     //Transactions
     app.get("/transactions", transactionData.getTransactions);
+    app.get("/purchases", transactionData.getPurchases);
 }

+ 88 - 3
views/inventoryPage/data.js

@@ -2,6 +2,10 @@ window.dataObj = {
     display: function(){
         clearScreen();
         document.querySelector("#dataStrand").style.display = "flex";
+
+        //Fill in month
+        let months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
+        document.querySelector("#month").innerText = `Month of ${months[new Date().getMonth()]}`;
     },
 
     populate: function(transactions){
@@ -12,7 +16,8 @@ window.dataObj = {
                 id: recipe._id,
                 name: recipe.name,
                 quantity: 0,
-                ingredients: recipe.ingredients 
+                ingredients: recipe.ingredients,
+                price: recipe.price / 100
             });
         }
 
@@ -34,7 +39,8 @@ window.dataObj = {
                 id: item.ingredient._id,
                 name: item.ingredient.name,
                 quantity: 0,
-                quantityRemaining: item.quantity
+                quantityRemaining: item.quantity,
+                unit: item.ingredient.unit
             });
         }
 
@@ -43,7 +49,7 @@ window.dataObj = {
             for(let recipeIngredient of recipe.ingredients){
                 for(let newIngredient of ingredients){
                     if(newIngredient.id === recipeIngredient.ingredient._id){
-                        newIngredient.quantity += recipeIngredient.quantity;
+                        newIngredient.quantity += (recipeIngredient.quantity * recipe.quantity);
                         break;
                     }
                 }
@@ -51,6 +57,85 @@ window.dataObj = {
         }
 
         //Populate Ingredients table
+        let ingredientsBody = document.querySelector("#ingredientsData tbody");
         
+        for(let ingredient of ingredients){
+            let row = document.createElement("tr");
+            ingredientsBody.appendChild(row);
+
+            let name = document.createElement("td");
+            name.innerText = `${ingredient.name} (${ingredient.unit})`;
+            row.appendChild(name);
+
+            let used = document.createElement("td");
+            used.innerText = ingredient.quantity;
+            row.appendChild(used);
+
+            let remaining = document.createElement("td");
+            remaining.innerText = ingredient.quantityRemaining;
+            row.appendChild(remaining);
+        }
+
+        //Populate recipes table
+        let recipesBody = document.querySelector("#recipesData tbody");
+
+        for(let recipe of recipes){
+            let row = document.createElement("tr");
+            recipesBody.appendChild(row);
+
+            let name = document.createElement("td");
+            name.innerText = recipe.name;
+            row.appendChild(name);
+
+            let quantity = document.createElement("td");
+            quantity.innerText = recipe.quantity;
+            row.appendChild(quantity);
+
+            let revenue = document.createElement("td");
+            revenue.innerText = `$${recipe.quantity * recipe.price}`;
+            row.appendChild(revenue);
+        }
+    },
+
+    populatePurchases: function(purchases){
+        //Create object for each merchant ingredient
+        let ingredients = [];
+
+        for(let item of merchant.inventory){
+            ingredients.push({
+                id: item.ingredient._id,
+                name: item.ingredient.name,
+                amount: 0,
+                unit: item.ingredient.unit
+            });
+        }
+
+        //Populate amount of ingredients purchased
+        for(let purchase of purchases){
+            for(let purchaseIngredient of purchase.ingredients){
+                for(let newIngredient of ingredients){
+                    if(newIngredient.id === purchaseIngredient.ingredient){
+                        newIngredient.amount += purchaseIngredient.quantity;
+                        break;
+                    }
+                }
+            }
+        }
+
+        //Populate purchases table
+        let purchasesBody = document.querySelector("#purchasesData tbody");
+        
+        for(let ingredient of ingredients){
+            let row = document.createElement("tr");
+            purchasesBody.appendChild(row);
+            
+            let name = document.createElement("td");
+            name.innerText = `${ingredient.name} (${ingredient.unit})`;
+            row.appendChild(name);
+
+            let amount = document.createElement("td");
+            amount.innerText = ingredient.amount;
+            row.appendChild(amount);
+        }
     }
 }

+ 12 - 0
views/inventoryPage/inventory.css

@@ -91,6 +91,18 @@
         margin: 15px;
     }
 
+    .tables{
+        display: flex;
+        justify-content: space-around;
+        width: 100%;
+    }
+
+    .dataTable{
+        display: flex;
+        flex-direction: column;
+        align-items: center;
+    }
+
 /* Account Strand */
 #accountStrand{
     flex-direction: column;

+ 59 - 14
views/inventoryPage/inventory.ejs

@@ -56,23 +56,56 @@
         </div>
 
         <div id="dataStrand" class="strand">
-            <!-- Ingredients used -->
-            <table>
-                <thead>
-                    <tr>
-                        <th>Ingredient</th>
-                        <th>Amount used</th>
-                        <th>Amount remaining</th>
-                    </tr>
-                </thead>
-                <tbody></tbody>
-            </table>
+            <h2 id="month"></h2>
+            <div class="tables">
+                <!-- Ingredients used -->
+                <div id="ingredientsData" class="dataTable">
+                    <h3>Ingredients</h3>
+                    <table>
+                        <thead>
+                            <tr>
+                                <th>Ingredient</th>
+                                <th>Used</th>
+                                <th>Remaining</th>
+                            </tr>
+                        </thead>
+                        <tbody></tbody>
+                    </table>
+                </div>
 
-            <!-- Recipes used -->
+                <!-- Recipes used -->
+                <div id="recipesData" class="dataTable">
+                    <h3>Recipes</h3>
+
+                    <table>
+                        <thead>
+                            <tr>
+                                <th>Recipe</th>
+                                <th># sold</th>
+                                <th>Revenue</th>
+                            </tr>
+                        </thead>
+                        <tbody></tbody>
+                    </table>
+                </div>
 
-            <!-- Purchases -->
+                <!-- Purchases -->
+                <div id="purchasesData" class="dataTable">
+                    <h3>Purchases</h3>
+
+                    <table>
+                        <thead>
+                            <tr>
+                                <th>Ingredient</th>
+                                <th>Amount</th>
+                            </tr>
+                        </thead>
+                        <tbody></tbody>
+                    </table>
+                </div>
 
-            <!-- Totals -->
+                <!-- Totals -->
+            </div>
         </div>
 
         <div id="accountStrand" class="strand">
@@ -260,6 +293,18 @@
                 .catch((err)=>{
                     banner.createError("Error: unable to render sales data");
                 });
+
+            axios.get("/purchases")
+                .then((response)=>{
+                    if(typeof(response.data) === "string"){
+                        banner.createError(response.data);
+                    }else{
+                        dataObj.populatePurchases(response.data);
+                    }
+                })
+                .catch((err)=>{
+                    banner.createError("Error: unable to render purchases data");
+                })
         </script>
     </body>
 </html>