Explorar el Código

Fix allowances to display correct information on edit page.

Lee Morgan hace 11 meses
padre
commit
fc0b250167

+ 8 - 0
src/views/js/Format.js

@@ -30,4 +30,12 @@ export default class Format{
     static centsToDollars(num){
         return num / 100;
     }
+
+    static percentToAmount(percent, income){
+        return (percent / 100) * income;
+    }
+
+    static amountToPercent(amount, income){
+        return (amount / income) * 100;
+    }
 }

+ 3 - 4
src/views/js/data/Account.js

@@ -58,10 +58,9 @@ export default class Account{
     getIncomeSum(raw = false){
         let total = 0;
         for(let i = 0; i < this._income.length; i++){
-            if(this._income[i].active) total += this._income[i].amountRaw;
+            if(this._income[i].active) total += this._income[i].amount;
         }
-        if(raw) return total;
-        return Format.centsToDollars(total);
+        return total;
     }
 
     getCategory(category, categoryId){
@@ -93,7 +92,7 @@ export default class Account{
         let income = 0;
         for(let i = 0; i < this._income.length; i++){
             if(this._income[i].active){
-                income += this._income[i].amountRaw;
+                income += this._income[i].amount;
             }
         }
         return income;

+ 1 - 1
src/views/js/data/Allowance.js

@@ -21,7 +21,7 @@ export default class Allowance{
         this._name = v;
     }
 
-    set amount(v){
+    get amount(){
         return this._amount;
     }
 

+ 18 - 4
src/views/js/pages/EditAllowance.js

@@ -23,18 +23,32 @@ export default class EditAllowance extends Page{
         changePage("viewAllowances");
     }
 
+    amountDisplay(displayPercent, allowance){
+        if(displayPercent && allowance.isPercent){
+            return allowance.amount;
+        }
+        if(displayPercent && !allowance.isPercent){
+            return Format.amountToPercent(allowance.amount, this.incomeTotal).toFixed(2);
+        }
+        if(!displayPercent && allowance.isPercent){
+            return Format.centsToDollars(Format.percentToAmount(allowance.amount, this.incomeTotal));
+        }
+        if(!displayPercent && !allowance.isPercent){
+            return Format.centsToDollars(allowance.amount);
+        }
+    }
+
     updateAmount(allowance){
-        let text, titleText, step, value;
+        let value = this.amountDisplay(event.target.checked, allowance);
+        let text, titleText, step;
         if(event.target.checked){
             text = "Amount (%)";
             titleText = "(Percent of Income)";
             step = "1";
-            value = allowance.rawAmount();
         }else{
             text = "Amount ($)";
             titleText = "(Fixed Amount)";
             step = "0.01";
-            value = Format.centsToDollars(allowance.currencyAmount(this.incomeTotal));
         }
 
         new Elem(this.container.querySelector("h2"))
@@ -62,7 +76,7 @@ export default class EditAllowance extends Page{
     }
 
     render(allowance){
-        let value = allowance.isPercent ? allowance.rawAmount() : Format.currency(allowance.currencyAmount(this.incomeTotal));
+        const value = this.amountDisplay(allowance.isPercent, allowance);
         new Elem("form")
             .addClass("standardForm")
             .onsubmit(()=>{this.submit(allowance)})

+ 0 - 1
src/views/js/pages/EditBill.js

@@ -5,7 +5,6 @@ import Format from "../Format.js";
 export default class EditBill extends Page{
     constructor(bill){
         super("EditBill", ["home", "back-viewBills", "logout"]);
-        console.log("billing");
 
         this.render(bill);
     }