Browse Source

Add creation of transaction from a spreadsheet.
Creating a transaction automatically updates merchant ingredients.

Lee Morgan 5 years ago
parent
commit
54eb454a51

+ 74 - 1
controllers/transactionData.js

@@ -220,7 +220,80 @@ module.exports = {
             header: 1
         });
 
-        console.log(array);
+        let locations = {};
+        for(let i = 0; i < array[0].length; i++){
+            switch(array[0][i].toLowerCase()){
+                case "date": locations.date = i; break;
+                case "recipes": locations.recipes = i; break;
+                case "quantity": locations.quantity = i; break;
+            }
+        }
+
+        Merchant.findOne({_id: req.session.user})
+            .populate("recipes")
+            .populate("inventory.ingredient")
+            .then((merchant)=>{       
+                let transaction = new Transaction({
+                    merchant: req.session.user,
+                    recipes: []
+                });
+
+                if(array[1][0] === undefined){
+                    transaction.date = new Date(array[1][0]);
+                }else{
+                    transaction.date = new Date();
+                }
+                
+                let ingredients = [];
+                for(let i = 1; i < array.length; i++){
+                    let exists = false;
+                    for(let j = 0; j < merchant.recipes.length; j++){
+                        if(merchant.recipes[j].name.toLowerCase() === array[i][locations.recipes]){
+                            transaction.recipes.push({
+                                recipe: merchant.recipes[j],
+                                quantity: array[i][locations.quantity]
+                            });
+
+                            for(let k = 0; k < merchant.recipes[j].ingredients.length; k++){
+                                ingredients.push({
+                                    id: merchant.recipes[j].ingredients[k].ingredient,
+                                    quantity: array[i][locations.quantity] * merchant.recipes[j].ingredients[k].quantity
+                                });
+                            }
+
+                            exists = true;
+                            break;
+                        }
+                    }
+
+                    if(exists !== true){
+                        throw `COULD NOT FIND RECIPE ${array[i][locations.recipes]}`;
+                    }
+                }
+
+                for(let i = 0; i < ingredients.length; i++){
+                    for(let j = 0; j < merchant.inventory.length; j++){
+                        
+                        if(merchant.inventory[j].ingredient._id.toString() === ingredients[i].id.toString()){
+                            merchant.inventory[j].quantity -= ingredients[i].quantity;
+
+                            break;
+                        }
+                    }
+                }
+
+                return Promise.all([transaction.save(), merchant.save()]);
+            })
+            .then((response)=>{
+                return res.json(response[0]);
+            })
+            .catch((err)=>{
+                if(typeof(err) === "string"){
+                    return res.json(err);
+                }
+
+                return res.json("ERROR: UNABLE TO CREATE YOUR TRANSACTION");
+            });
     },
 
     /*

+ 0 - 1
models/merchant.js

@@ -35,7 +35,6 @@ const MerchantSchema = new mongoose.Schema({
         quantity: {
             type: Number,
             required: true,
-            min: 0
         },
         defaultUnit: {
             type: String,

BIN
uploads/245c49431e93d192f097804ebcf4de40


BIN
uploads/3b2843da45a9c40c9bc4d4f74a5b3104


+ 1 - 1
views/dashboardPage/bundle.js

@@ -2566,7 +2566,7 @@ let newTransaction = {
                 if(typeof(response) === "string"){
                     banner.createError(response);
                 }else{
-
+                    console.log(response);
                 }
             })
             .catch((err)=>{

+ 1 - 1
views/dashboardPage/js/sidebars/newTransaction.js

@@ -115,7 +115,7 @@ let newTransaction = {
                 if(typeof(response) === "string"){
                     banner.createError(response);
                 }else{
-
+                    console.log(response);
                 }
             })
             .catch((err)=>{