Early payment discount option moved from branch to payment terms.
[fa-stable.git] / js / payalloc.js
index a711e603cb40daf97dcc8924cefb339c5714eabd..1ea079866a9af88fcf1f24933dd9e5174645a3d3 100644 (file)
@@ -30,30 +30,33 @@ function blur_alloc(i) {
                }
 }
 
-function allocate_all(doc) {
-       var amount = get_amount('amount'+doc);
-       var unallocated = get_amount('un_allocated'+doc);
-       var total = get_amount('amount');
-       var left = 0;
-       total -=  (amount-unallocated);
-       left -= (amount-unallocated);
-       amount = unallocated;
-       if(left<0) {
-               total  += left;
-               amount += left;
-               left = 0;
+function update_totals() {
+       var amount = 0;
+       var discount = 0;
+
+       for (var i=0; i<docs.length; i++) {
+               amount += get_amount('amount'+docs[i])
+               if (document.getElementsByName('early_disc'+docs[i])[0].checked 
+                       && (get_amount('un_allocated'+docs[i]) == get_amount('amount'+docs[i])))
+                               discount += get_amount('early_disc'+docs[i]);
        }
-       price_format('amount'+doc, amount, user.pdec);
-       price_format('amount', total, user.pdec);
+       price_format('amount', amount-discount, user.pdec);
+       price_format('discount', discount, user.pdec);
+       
+}
+
+function allocate_all(doc) {
+       var unallocated = get_amount('maxval'+doc, 1);
+       price_format('amount'+doc, unallocated, user.pdec);
+       update_totals();
 }
 
 function allocate_none(doc) {
-       amount = get_amount('amount'+doc);
-       total = get_amount('amount');
        price_format('amount'+doc, 0, user.pdec);
-       price_format('amount', total-amount, user.pdec);
+       update_totals();
 }
 
+
 var allocations = {
        '.amount': function(e) {
                if(e.name == 'allocated_amount' || e.name == 'bank_amount')
@@ -70,6 +73,11 @@ var allocations = {
                                focus_alloc(this);
                        };
                }
+       },
+       '.check':function(e) {
+               e.onclick = function() {
+                       update_totals();
+               }
        }
 }