Procházet zdrojové kódy

Update new orders to handle special units

Lee Morgan před 5 roky
rodič
revize
84466ecb78

+ 0 - 1
controllers/ingredientData.js

@@ -36,7 +36,6 @@ module.exports = {
             req.session.error = "MUST BE LOGGED IN TO DO THAT";
             return res.redirect("/");
         }
-        console.log(req.body);
 
         let validation = Validator.ingredient(req.body.ingredient);
         if(validation !== true){

+ 2 - 0
controllers/orderData.js

@@ -126,11 +126,13 @@ module.exports = {
                 for(let i = 0; i < req.body.ingredients.length; i++){
                     for(let j = 0; j < merchant.inventory.length; j++){
                         if(req.body.ingredients[i].ingredient === merchant.inventory[j].ingredient.toString()){
+                            if(merchant.inventory[j])
                             merchant.inventory[j].quantity += parseFloat(req.body.ingredients[i].quantity);
                         }
                     }
                 }
 
+                return;
                 return merchant.save();
             })
             .then((merchant)=>{

+ 23 - 8
views/dashboardPage/bundle.js

@@ -1109,7 +1109,6 @@ controller = {
     price = price of the ingredient per unit in cents
     */
     convertPrice(unitType, unit, price){
-
         if(unitType === "mass"){
             switch(unit){
                 case "g": break;
@@ -1984,8 +1983,15 @@ let newOrder = {
 
         let div = document.getElementById("selectedIngredient").content.children[0].cloneNode(true);
         div.ingredient = ingredient;
-        div.children[0].children[0].innerText = `${ingredient.name} (${ingredient.unit.toUpperCase()})`;
         div.children[0].children[1].onclick = ()=>{this.removeIngredient(div, element)};
+
+        //Display units depending on the whether it is a special unit
+        if(ingredient.specialUnit === "bottle"){
+            div.children[0].children[0].innerText = `${ingredient.name} (BOTTLES)`;
+        }else{
+            div.children[0].children[0].innerText = `${ingredient.name} (${ingredient.unit.toUpperCase()})`;
+        }
+
         document.getElementById("selectedIngredientList").appendChild(div);
     },
 
@@ -2027,12 +2033,21 @@ let newOrder = {
                 banner.createError("QUANTITY AND PRICE MUST BE NON-NEGATIVE NUMBERS");
             }
 
-            
-            data.ingredients.push({
-                ingredient: ingredients[i].ingredient.id,
-                quantity: controller.convertToMain(ingredients[i].ingredient.unit, quantity),
-                pricePerUnit: controller.convertPrice(ingredients[i].ingredient.unitType, ingredients[i].ingredient.unit, price * 100)
-            });
+            if(ingredients[i].ingredient.specialUnit === "bottle"){
+                const ppu = controller.convertPrice("volume", ingredients[i].ingredient.unit, (price * 100) / (ingredients[i].ingredient.convert(ingredients[i].ingredient.unitSize)));
+
+                data.ingredients.push({
+                    ingredient: ingredients[i].ingredient.id,
+                    quantity: quantity * ingredients[i].ingredient.unitSize,
+                    pricePerUnit: ppu,
+                });
+            }else{
+                data.ingredients.push({
+                    ingredient: ingredients[i].ingredient.id,
+                    quantity: controller.convertToMain(ingredients[i].ingredient.unit, quantity),
+                    pricePerUnit: controller.convertPrice(ingredients[i].ingredient.unitType, ingredients[i].ingredient.unit, price * 100)
+                });
+            }
         }
 
         let loader = document.getElementById("loaderContainer");

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

@@ -285,7 +285,6 @@ controller = {
     price = price of the ingredient per unit in cents
     */
     convertPrice(unitType, unit, price){
-
         if(unitType === "mass"){
             switch(unit){
                 case "g": break;

+ 23 - 7
views/dashboardPage/js/newOrder.js

@@ -29,8 +29,15 @@ let newOrder = {
 
         let div = document.getElementById("selectedIngredient").content.children[0].cloneNode(true);
         div.ingredient = ingredient;
-        div.children[0].children[0].innerText = `${ingredient.name} (${ingredient.unit.toUpperCase()})`;
         div.children[0].children[1].onclick = ()=>{this.removeIngredient(div, element)};
+
+        //Display units depending on the whether it is a special unit
+        if(ingredient.specialUnit === "bottle"){
+            div.children[0].children[0].innerText = `${ingredient.name} (BOTTLES)`;
+        }else{
+            div.children[0].children[0].innerText = `${ingredient.name} (${ingredient.unit.toUpperCase()})`;
+        }
+
         document.getElementById("selectedIngredientList").appendChild(div);
     },
 
@@ -72,12 +79,21 @@ let newOrder = {
                 banner.createError("QUANTITY AND PRICE MUST BE NON-NEGATIVE NUMBERS");
             }
 
-            
-            data.ingredients.push({
-                ingredient: ingredients[i].ingredient.id,
-                quantity: controller.convertToMain(ingredients[i].ingredient.unit, quantity),
-                pricePerUnit: controller.convertPrice(ingredients[i].ingredient.unitType, ingredients[i].ingredient.unit, price * 100)
-            });
+            if(ingredients[i].ingredient.specialUnit === "bottle"){
+                const ppu = controller.convertPrice("volume", ingredients[i].ingredient.unit, (price * 100) / (ingredients[i].ingredient.convert(ingredients[i].ingredient.unitSize)));
+
+                data.ingredients.push({
+                    ingredient: ingredients[i].ingredient.id,
+                    quantity: quantity * ingredients[i].ingredient.unitSize,
+                    pricePerUnit: ppu,
+                });
+            }else{
+                data.ingredients.push({
+                    ingredient: ingredients[i].ingredient.id,
+                    quantity: controller.convertToMain(ingredients[i].ingredient.unit, quantity),
+                    pricePerUnit: controller.convertPrice(ingredients[i].ingredient.unitType, ingredients[i].ingredient.unit, price * 100)
+                });
+            }
         }
 
         let loader = document.getElementById("loaderContainer");

+ 1 - 1
views/dashboardPage/sidebars/newOrder.ejs

@@ -44,7 +44,7 @@
             <div>
                 <input type="number" min="0" step="0.01" placeholder="QUANTITY">
 
-                <input type="number" min="0" step="0.01" placeholder="PRICE PER UNIT">
+                <input type="number" min="0" step="0.01" placeholder="PRICE PER UNIT($)">
             </div>
         </div>
     </template>