Kaynağa Gözat

Display bottle size in ingredient edit sidebar.

Lee Morgan 5 yıl önce
ebeveyn
işleme
0d5bf98a3a

+ 2 - 1
models/ingredient.js

@@ -32,7 +32,8 @@ const IngredientSchema = new mongoose.Schema({
     convert: {
         toMass: Number,
         toVolume: Number,
-        toLength: Number
+        toLength: Number,
+        toBottle: Number
     },
     ingredients: [{
         ingredient: {

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

@@ -21,7 +21,7 @@
 
     <label id="editIngQuantityLabel"></label>
 
-    <label id="editSpecialLabel"></label>
+    <label id="editSpecialLabel" style="display:none"></label>
 
     <div class="lineBorder"></div>
 

+ 3 - 0
views/dashboardPage/js/classes/Merchant.js

@@ -30,6 +30,9 @@ class MerchantIngredient{
             case "m":
                 convertMultiplier = this._ingredient.convert.toLength;
                 break;
+            case "bottle":
+                convertMultiplier = this._ingredient.convert.toBottle;
+                break;
         }
         
         return this._quantity * controller.unitMultiplier(controller.getBaseUnit(this._ingredient.unit), this._ingredient.unit) * convertMultiplier;

+ 27 - 4
views/dashboardPage/js/sidebars/editIngredient.js

@@ -85,19 +85,42 @@ let editIngredient = {
         quantInput.step = "0.01";
         quantInput.value = ingredient.quantity.toFixed(2);
         quantLabel.appendChild(quantInput);
+        
+        let bottleLabel = document.getElementById("editSpecialLabel");
+        if(ingredient.ingredient.unit === "bottle"){
+            bottleLabel.innerText = `BOTTLE SIZE (${ingredient.ingredient.altUnit.toUpperCase()})`;
+            let bottleInput = document.createElement("input");
+            bottleInput.id = "editSpecialInput";
+            bottleInput.type = "number";
+            bottleInput.min = "0";
+            bottleInput.value = 1 / ingredient.ingredient.convert.toBottle * controller.unitMultiplier(controller.getBaseUnit(ingredient.ingredient.altUnit), ingredient.ingredient.altUnit);
+            bottleLabel.appendChild(bottleInput);
+            bottleLabel.style.display = "flex";
+        }else{
+            bottleLabel.style.display = "none";
+        }
     },
 
     changeUnit: function(button, ingredient){
         //update current stock quantity
-        let label = document.getElementById("editIngQuantityLabel");
-        if(ingredient.ingredient.unit !== "bottle") label.innerText = `CURRENT STOCK (${button.innerText})`;
-        if(ingredient.ingredient.unit === "bottle") label.innerText = "CURRENT STOCK (bottle)";
+        let label = {};
+        let newValue = 0;
+        if(ingredient.ingredient.unit === "bottle"){
+            label = document.getElementById("editSpecialLabel");
+            label.innerText = `BOTTLE SIZE (${button.innerText})`;
+            newValue = controller.unitMultiplier(controller.getBaseUnit(ingredient.ingredient.altUnit), button.innerText.toLowerCase()) / ingredient.ingredient.convert.toBottle;
+        }else{
+            label = document.getElementById("editIngQuantityLabel");
+            label.innerText = `CURRENT STOCK (${button.innerText})`;
+            newValue = ingredient.quantity * controller.unitMultiplier(controller.getBaseUnit(ingredient.ingredient.unit), button.innerText.toLowerCase());
+        }
+        
         let input = document.createElement("input");
         input.id = "editIngQuantity";
         input.type = "number";
         input.min = "0";
         input.step = "0.01";
-        input.value = (ingredient.quantity * controller.unitMultiplier(ingredient.ingredient.unit, button.innerText.toLowerCase())).toFixed(2);
+        input.value = newValue.toFixed(2);
         label.appendChild(input);
         
         //update conversions

+ 8 - 10
views/dashboardPage/js/sidebars/newIngredient.js

@@ -89,7 +89,8 @@ let newIngredient = {
                 convert: {
                     toMass: massRight / massLeft,
                     toVolume: volumeRight / volumeLeft,
-                    toLength: lengthRight / lengthLeft
+                    toLength: lengthRight / lengthLeft,
+                    toBottle: 0
                 }
             },
             quantity: controller.baseUnit(quantityValue, unit)
@@ -101,18 +102,15 @@ let newIngredient = {
 
         let convert = newIngredient.ingredient.convert;
         switch(controller.getUnitType(unit)){
-            case "mass": newIngredient.ingredient.convert.toMass = 1; break;
-            case "volume": newIngredient.ingredient.convert.toVolume = 1; break;
-            case "length": newIngredient.ingredient.convert.toLength = 1; break;
-            // case "mass": convert.toMass = controller.unitMultiplier(unit, "g"); break;
-            // case "volume": convert.toVolume = controller.unitMultiplier(unit, "l"); break;
-            // case "length": convert.toLength = controller.unitMultiplier(unit, "m"); break;
-            case "bottle": 
+            case "mass": convert.toMass = 1; break;
+            case "volume": convert.toVolume = 1; break;
+            case "length": convert.toLength = 1; break;
+            case "bottle":
                 let bottleQuant = document.getElementById("bottleSize").value;
                 let bottleUnit = document.getElementById("bottleUnits").value;
-                newIngredient.ingredient.convert.toVolume = 1 / controller.toBase(bottleQuant, bottleUnit);
-                newIngredient.quantity = quantityValue * controller.toBase(bottleQuant, bottleUnit);
                 newIngredient.ingredient.altUnit = bottleUnit;
+                newIngredient.ingredient.convert.toVolume = 1;
+                newIngredient.ingredient.convert.toBottle = 1 / controller.toBase((bottleQuant), bottleUnit);
                 break;
         }