Răsfoiți Sursa

Add backend to save non-pos transactions and update merchant inventory.

Lee Morgan 6 ani în urmă
părinte
comite
de01ded00a
4 a modificat fișierele cu 78 adăugiri și 5 ștergeri
  1. 45 1
      controllers/home.js
  2. 27 0
      models/nonPosTransaction.js
  3. 3 0
      routes.js
  4. 3 4
      views/inventoryPage/enterTransactions.js

+ 45 - 1
controllers/home.js

@@ -5,6 +5,7 @@ const Merchant = require("../models/merchant");
 const Ingredient = require("../models/ingredient");
 const Recipe = require("../models/recipe");
 const Transaction = require("../models/transaction");
+const nonPosTransaction = require("../models/nonPosTransaction");
 
 const token = "b48068eb-411a-918e-ea64-52007147e42c";
 
@@ -28,7 +29,6 @@ module.exports = {
                             let transactions = [];
 
                             for(let order of result.data.elements){
-                                console.log(order);
                                 let newTransaction = new Transaction({
                                     merchant: merchant._id,
                                     date: new Date(order.createdTime),
@@ -554,6 +554,50 @@ module.exports = {
             });  
     },
 
+    createTransaction: function(req, res){
+        let transaction = new nonPosTransaction({
+            date: Date.now(),
+            author: "None",
+            merchant: req.session.user,
+            recipes: req.body
+        });
+
+        //Calculate all ingredients used, store to list
+        Merchant.findOne({_id: req.session.user})
+            .populate("recipes")
+            .then((merchant)=>{
+                for(let reqRecipe of req.body){
+                    let merchRecipe = merchant.recipes.find(r => r._id.toString() === reqRecipe.id);
+                    for(let recipeIngredient of merchRecipe.ingredients){
+                        let merchInvIngredient = merchant.inventory.find(i => i.ingredient.toString() === recipeIngredient.ingredient.toString());
+                        merchInvIngredient.quantity -= recipeIngredient.quantity * reqRecipe.quantity;
+                    }
+                }
+
+                merchant.save()
+                    .then((merchant)=>{
+                        res.json();
+                    })
+                    .catch((err)=>{
+                        console.log(err);
+                        return res.render("error");
+                    });
+            })
+            .catch((err)=>{
+                console.log(err);
+                return res.render("error");
+            });
+
+        transaction.save()
+            .then((transaction)=>{
+                return;
+            })
+            .catch((err)=>{
+                console.log(err);
+                return res.render("error");
+            });
+    },
+
     getCloverRecipes: function(req, res){
         if(!req.session.user){
             return res.render("error");

+ 27 - 0
models/nonPosTransaction.js

@@ -0,0 +1,27 @@
+const mongoose = require("mongoose");
+
+let NonPosTransactionSchema = new mongoose.Schema({
+    date: {
+        type: Date,
+        required: [true, "Must provide a date and time"]
+    },
+    author: String,
+    merchant: {
+        type: mongoose.Schema.Types.ObjectId,
+        ref: "Merchant",
+        required: [true, "Log must contain a reference to a merchant"]
+    },
+    recipes: [{
+        recipe: {
+            type: mongoose.Schema.Types.ObjectId,
+            ref: "Recipe"
+        },
+        quantity: {
+            type: Number,
+            required: [true, "Must provide the number sold for each recipe"],
+            min: [0, "Must be a positive number"]
+        }
+    }]
+});
+
+module.exports = mongoose.model("NonPOSTransaction", NonPosTransactionSchema);

+ 3 - 0
routes.js

@@ -24,6 +24,9 @@ module.exports = function(app){
     app.post("/ingredients/create", home.createNewIngredients);
     app.post("/ingredients/createone", home.createIngredient);  //also adds to merchant
 
+    //Transactions
+    app.post("/transactions/create", home.createTransaction);
+
     //Clover API
     app.get("/getrecipes", home.getCloverRecipes);
 

+ 3 - 4
views/inventoryPage/enterTransactions.js

@@ -30,7 +30,6 @@ let enterTransactionsObj = {
             input.type = "number";
             input.step = "1";
             input.value = "0";
-            input.id = "transactionQuantity";
             quantity.appendChild(input);
         }
     },
@@ -41,16 +40,16 @@ let enterTransactionsObj = {
         let recipesSold = [];
 
         for(let row of tbody.children){
-            let quantity = document.querySelector("#transactionQuantity").value;
+            let quantity = row.children[1].children[0].value;
             
-            if(quantity >= 0){
+            if(quantity > 0){
                 let recipe = {
                     id: row._id,
                     quantity: quantity
                 }
 
                 recipesSold.push(recipe);
-            }else{
+            }else if(quantity < 0){
                 banner.createError("Cannot have negative quantities");
                 break;
             }