Forráskód Böngészése

Create ability to enter purchases

Lee Morgan 6 éve
szülő
commit
bd5441789f

+ 62 - 0
controllers/otherData.js

@@ -3,6 +3,7 @@ const bcrypt = require("bcryptjs");
 const Error = require("../models/error");
 const Error = require("../models/error");
 const NonPosTransaction = require("../models/nonPosTransaction");
 const NonPosTransaction = require("../models/nonPosTransaction");
 const Merchant = require("../models/merchant");
 const Merchant = require("../models/merchant");
+const Purchase = require("../models/purchase");
 
 
 module.exports = {
 module.exports = {
     //POST - Update non-pos merchant inventory and create a transaction
     //POST - Update non-pos merchant inventory and create a transaction
@@ -77,6 +78,67 @@ module.exports = {
             });
             });
     },
     },
 
 
+    //POST - Creates a new purchase for a merchant
+    //Inputs:
+    //  req.body: list of purchases (ingredient id and quantity)
+    createPurchase: function(req, res){
+        if(!req.session.user){
+            req.session.error = "Must be logged in to do that";
+            return res.redirect("/");
+        }
+
+        Merchant.findOne({_id: req.session.user})
+            .then((merchant)=>{
+                for(let purchase of req.body){
+                    let merchantIngredient = merchant.inventory.find(i => i.ingredient._id.toString() === purchase.ingredient);
+                    merchantIngredient.quantity += Number(purchase.quantity);
+                }
+                
+                merchant.save()
+                    .then((merchant)=>{
+                        res.json({});
+                    })
+                    .catch((err)=>{
+                        let errorMessage = "Error: Unable to save data";
+                        let error = new Error({
+                            code: 547,
+                            displayMessage: errorMessage,
+                            error: err
+                        });
+                        error.save();
+
+                        return res.json(errorMessage);
+                    });
+            })
+            .catch((err)=>{
+                let errorMessage = "Error: Unable to retrieve user data";
+                let error = new Error({
+                    code: 626,
+                    displayMessage: errorMessage,
+                    error: err
+                });
+                error.save();
+
+                return res.json(errorMessage);
+            });
+
+            let purchase = new Purchase({
+                merchant: req.session.user,
+                date: Date.now(),
+                ingredients: req.body
+            });
+
+            purchase.save()
+                .catch((err)=>{
+                    let error = new Error({
+                        code: 120,
+                        displayMessage: "none",
+                        error: err
+                    });
+                    error.save();
+                });
+    },
+
     //POST - logs the user in
     //POST - logs the user in
     //Inputs:
     //Inputs:
     //  req.body.email
     //  req.body.email

+ 1 - 0
routes.js

@@ -29,6 +29,7 @@ module.exports = function(app){
 
 
     //Other
     //Other
     app.post("/transactions/create", otherData.createTransaction);  //Creates transaction for non-pos merchant
     app.post("/transactions/create", otherData.createTransaction);  //Creates transaction for non-pos merchant
+    app.post("/purchases/create", otherData.createPurchase);
     app.post("/login", otherData.login);
     app.post("/login", otherData.login);
     app.get("/logout", otherData.logout);
     app.get("/logout", otherData.logout);
     app.post("/email", otherData.checkUniqueEmail);
     app.post("/email", otherData.checkUniqueEmail);

+ 2 - 0
views/inventoryPage/controller.js

@@ -2,6 +2,7 @@ let controller = {
     inventoryStrand: document.querySelector("#inventoryStrand"),
     inventoryStrand: document.querySelector("#inventoryStrand"),
     addIngredientStrand: document.querySelector("#addIngredientStrand"),
     addIngredientStrand: document.querySelector("#addIngredientStrand"),
     enterTransactionsStrand: document.querySelector("#enterTransactionsStrand"),
     enterTransactionsStrand: document.querySelector("#enterTransactionsStrand"),
+    enterPurchaseStrand: document.querySelector("#enterPurchaseStrand"),
 
 
     onStart: function(){
     onStart: function(){
         if(error){
         if(error){
@@ -15,6 +16,7 @@ let controller = {
         this.inventoryStrand.style.display = "none";
         this.inventoryStrand.style.display = "none";
         this.addIngredientStrand.style.display = "none";
         this.addIngredientStrand.style.display = "none";
         this.enterTransactionsStrand.style.display = "none";
         this.enterTransactionsStrand.style.display = "none";
+        this.enterPurchaseStrand.style.display = "none";
     }
     }
 }
 }
 
 

+ 84 - 0
views/inventoryPage/enterPurchase.js

@@ -0,0 +1,84 @@
+let enterPurchaseObj = {
+    isPopulated: false,
+
+    display: function(){
+        controller.clearScreen();
+        controller.enterPurchaseStrand.style.display = "flex";
+
+        if(!this.isPopulated){
+            this.populateTable();
+        }
+    },
+
+    populateTable: function(){
+        let tbody = document.querySelector("#enterPurchaseStrand tbody");
+
+        while(tbody.children.length > 0){
+            tbody.removeChild(tbody.firstChild);
+        }
+
+        for(let item of merchant.inventory){
+            let row = document.createElement("tr");
+            row._id = item.ingredient._id;
+            tbody.appendChild(row);
+
+            let nameTd = document.createElement("td");
+            nameTd.innerText = item.ingredient.name;
+            row.appendChild(nameTd);
+
+            let quantityTd = document.createElement("td");
+            row.appendChild(quantityTd);
+
+            let quantityInput = document.createElement("input");
+            quantityInput.type = "number";
+            quantityInput.step = "1";
+            quantityInput.value = 0;
+            quantityTd.appendChild(quantityInput);
+        }
+    },
+
+    submit: function(){
+        let tbody = document.querySelector("#enterPurchaseStrand tbody");
+
+        let purchases = [];
+
+        for(let row of tbody.children){
+            let quantity = row.children[1].children[0].value;
+            if(validator.ingredient.quantity(quantity)){
+                if(quantity > 0){
+                    let purchase = {
+                        ingredient: row._id,
+                        quantity: row.children[1].children[0].value
+                    }
+
+                    purchases.push(purchase);
+                }else if(quantity < 0){
+                    banner.createError("Cannot contain negative numbers");
+                    return;
+                }
+            }else{
+                return;
+            }
+        }
+
+        axios.post("/purchases/create", purchases)
+            .then((response)=>{
+                if(typeof(response.data) === "string"){
+                    banner.createError(response.data);
+                }else{
+                    for(let purchase of purchases){
+                        let merchantIngredient = merchant.inventory.find(i => i.ingredient._id === purchase.ingredient);
+                        merchantIngredient.quantity = Number(merchantIngredient.quantity) + Number(purchase.quantity);
+                    }
+
+                    inventoryObj.isPopulated = false;
+                    this.isPopulated = false;
+                    inventoryObj.display();
+                }
+            })
+            .catch((err)=>{
+                console.log(err);
+                banner.createError("Something went wrong and changes could not be made");
+            });
+    }
+}

+ 10 - 0
views/inventoryPage/inventory.css

@@ -55,6 +55,16 @@
         margin: 10px;
         margin: 10px;
     }
     }
 
 
+#enterPurchaseStrand{
+    display: none;
+    flex-direction: column;
+    align-items: center;
+}
+
+    #enterPurchaseStrand > *{
+        margin: 10px;
+    }
+
 @media screen and (max-width: 600px){
 @media screen and (max-width: 600px){
     /* inventoryStrand */
     /* inventoryStrand */
     #inventoryStrand h1{
     #inventoryStrand h1{

+ 21 - 0
views/inventoryPage/inventory.ejs

@@ -20,6 +20,8 @@
 
 
                 <button class="button" onclick="addIngredientObj.display()">Add Ingredient</button>
                 <button class="button" onclick="addIngredientObj.display()">Add Ingredient</button>
 
 
+                <button class="button" onclick="enterPurchaseObj.display()">Enter Purchases</button>
+
                 <% if(merchant.pos === "none"){ %>
                 <% if(merchant.pos === "none"){ %>
                     <button class="button" onclick="enterTransactionsObj.display()">Enter Transactions</button>
                     <button class="button" onclick="enterTransactionsObj.display()">Enter Transactions</button>
                 <% } %>
                 <% } %>
@@ -101,6 +103,24 @@
             <button class="button" onclick="enterTransactionsObj.submit()">Submit</button>
             <button class="button" onclick="enterTransactionsObj.submit()">Submit</button>
         </div>
         </div>
 
 
+        <div id="enterPurchaseStrand">
+            <h1>Enter Purchases</h1>
+            
+            <button class="button" onclick="inventoryObj.display()">Back to inventory</button>
+
+            <table>
+                <thead>
+                    <tr>
+                        <th>Ingredient</th>
+                        <th>Amount</th>
+                    </tr>
+                </thead>
+                <tbody></tbody>
+            </table>
+
+            <button class="button" onclick="enterPurchaseObj.submit()">Submit</button>
+        </div>
+
         <script>
         <script>
                 <% if(locals.error){ %>
                 <% if(locals.error){ %>
                     let error = <%- JSON.stringify(error) %>;
                     let error = <%- JSON.stringify(error) %>;
@@ -117,6 +137,7 @@
         <script src="/inventoryPage/recipe.js"></script>
         <script src="/inventoryPage/recipe.js"></script>
         <script src="/inventoryPage/addIngredient.js"></script>
         <script src="/inventoryPage/addIngredient.js"></script>
         <script src="/inventoryPage/enterTransactions.js"></script>
         <script src="/inventoryPage/enterTransactions.js"></script>
+        <script src="/inventoryPage/enterPurchase.js"></script>
         <script src="/inventoryPage/controller.js"></script>
         <script src="/inventoryPage/controller.js"></script>
     </body>
     </body>
 </html>
 </html>