Explorar o código

Update new order creation on frontend to send the correct data to the backend for bottles.
Bug Fix: displaying of orders was using incorrect units for bottles.

Lee Morgan %!s(int64=5) %!d(string=hai) anos
pai
achega
ff58406b64

+ 1 - 0
controllers/orderData.js

@@ -124,6 +124,7 @@ module.exports = {
             req.session.error = "MUST BE LOGGED IN TO DO THAT";
             return res.redirect("/");
         }
+        console.log(req.body);
 
         let newOrder = new Order(req.body);
         newOrder.merchant = req.session.user;

+ 9 - 13
views/dashboardPage/bundle.js

@@ -102,6 +102,10 @@ class Ingredient{
         this._unitSize = unitSize;
     }
 
+    getBaseUnitSize(){
+        return this._unitSize;
+    }
+
     getNameAndUnit(){
         if(this._specialUnit === "bottle"){
             return `${this._name} (BOTTLES)`;
@@ -838,7 +842,7 @@ class OrderIngredient{
 
     get quantity(){
         if(this._ingredient.specialUnit === "bottle"){
-            return this._quantity / this._ingredient.unitSize;
+            return this._quantity / this._ingredient._unitSize;
         }
 
         switch(this._ingredient.unit){
@@ -898,7 +902,7 @@ class OrderIngredient{
 
     get pricePerUnit(){
         if(this._ingredient.specialUnit === "bottle"){
-            return (this._pricePerUnit * this._ingredient.unitSize) / 100;
+            return (this._pricePerUnit * this._ingredient._unitSize) / 100;
         }
 
         switch(this._ingredient.unit){
@@ -2217,19 +2221,10 @@ let newOrder = {
             let quantity = ingredients[i].children[1].children[0].value;
             let price = ingredients[i].children[1].children[1].value;
 
-            if(quantity === "" || price === ""){
-                controller.createBanner("MUST PROVIDE QUANTITY AND PRICE PER UNIT FOR ALL INGREDIENTS", "error");
-                return;
-            }
-
-            if(quantity < 0 || price < 0){
-                controller.createBanner("QUANTITY AND PRICE MUST BE NON-NEGATIVE NUMBERS", "error");
-            }
-
             if(ingredients[i].ingredient.ingredient.specialUnit === "bottle"){
                 data.ingredients.push({
                     ingredient: ingredients[i].ingredient.ingredient.id,
-                    quantity: quantity * ingredients[i].ingredient.ingredient.unitSize,
+                    quantity: quantity * ingredients[i].ingredient.ingredient.getBaseUnitSize(),
                     pricePerUnit: this.convertPrice(ingredients[i].ingredient.ingredient, price * 100)
                 });
             }else{
@@ -2273,7 +2268,7 @@ let newOrder = {
     //TODO: Remove this function, it should be on the order
     convertPrice: function(ingredient, price){
         if(ingredient.specialUnit === "bottle"){
-            return price / ingredient.unitSize;
+            return price / ingredient.getBaseUnitSize();
         }
 
         switch(ingredient.unit){
@@ -2725,6 +2720,7 @@ let orderDetails = {
             
             let ingredientDisplay = ingredientDiv.children[1];
             if(ingredient.specialUnit === "bottle"){
+                console.log(order.ingredients[i].pricePerUnit);
                 ingredientDisplay.innerText = `${order.ingredients[i].quantity.toFixed(2)} bottles x $${order.ingredients[i].pricePerUnit.toFixed(2)}`;
             }else{
                 ingredientDisplay.innerText = `${order.ingredients[i].quantity.toFixed(2)} ${ingredient.unit.toUpperCase()} X $${order.ingredients[i].pricePerUnit.toFixed(2)}`;

+ 4 - 0
views/dashboardPage/js/classes/Ingredient.js

@@ -101,6 +101,10 @@ class Ingredient{
         this._unitSize = unitSize;
     }
 
+    getBaseUnitSize(){
+        return this._unitSize;
+    }
+
     getNameAndUnit(){
         if(this._specialUnit === "bottle"){
             return `${this._name} (BOTTLES)`;

+ 2 - 2
views/dashboardPage/js/classes/Order.js

@@ -14,7 +14,7 @@ class OrderIngredient{
 
     get quantity(){
         if(this._ingredient.specialUnit === "bottle"){
-            return this._quantity / this._ingredient.unitSize;
+            return this._quantity / this._ingredient._unitSize;
         }
 
         switch(this._ingredient.unit){
@@ -74,7 +74,7 @@ class OrderIngredient{
 
     get pricePerUnit(){
         if(this._ingredient.specialUnit === "bottle"){
-            return (this._pricePerUnit * this._ingredient.unitSize) / 100;
+            return (this._pricePerUnit * this._ingredient._unitSize) / 100;
         }
 
         switch(this._ingredient.unit){

+ 2 - 11
views/dashboardPage/js/sidebars/newOrder.js

@@ -71,19 +71,10 @@ let newOrder = {
             let quantity = ingredients[i].children[1].children[0].value;
             let price = ingredients[i].children[1].children[1].value;
 
-            if(quantity === "" || price === ""){
-                controller.createBanner("MUST PROVIDE QUANTITY AND PRICE PER UNIT FOR ALL INGREDIENTS", "error");
-                return;
-            }
-
-            if(quantity < 0 || price < 0){
-                controller.createBanner("QUANTITY AND PRICE MUST BE NON-NEGATIVE NUMBERS", "error");
-            }
-
             if(ingredients[i].ingredient.ingredient.specialUnit === "bottle"){
                 data.ingredients.push({
                     ingredient: ingredients[i].ingredient.ingredient.id,
-                    quantity: quantity * ingredients[i].ingredient.ingredient.unitSize,
+                    quantity: quantity * ingredients[i].ingredient.ingredient.getBaseUnitSize(),
                     pricePerUnit: this.convertPrice(ingredients[i].ingredient.ingredient, price * 100)
                 });
             }else{
@@ -127,7 +118,7 @@ let newOrder = {
     //TODO: Remove this function, it should be on the order
     convertPrice: function(ingredient, price){
         if(ingredient.specialUnit === "bottle"){
-            return price / ingredient.unitSize;
+            return price / ingredient.getBaseUnitSize();
         }
 
         switch(ingredient.unit){

+ 1 - 0
views/dashboardPage/js/sidebars/orderDetails.js

@@ -26,6 +26,7 @@ let orderDetails = {
             
             let ingredientDisplay = ingredientDiv.children[1];
             if(ingredient.specialUnit === "bottle"){
+                console.log(order.ingredients[i].pricePerUnit);
                 ingredientDisplay.innerText = `${order.ingredients[i].quantity.toFixed(2)} bottles x $${order.ingredients[i].pricePerUnit.toFixed(2)}`;
             }else{
                 ingredientDisplay.innerText = `${order.ingredients[i].quantity.toFixed(2)} ${ingredient.unit.toUpperCase()} X $${order.ingredients[i].pricePerUnit.toFixed(2)}`;