Early payment discount option moved from branch to payment terms.
[fa-stable.git] / js / allocate.js
1 /**********************************************************************
2     Copyright (C) FrontAccounting, LLC.
3         Released under the terms of the GNU General Public License, GPL, 
4         as published by the Free Software Foundation, either version 3 
5         of the License, or (at your option) any later version.
6     This program is distributed in the hope that it will be useful,
7     but WITHOUT ANY WARRANTY; without even the implied warranty of
8     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
9     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
10 ***********************************************************************/
11 function focus_alloc(i) {
12     save_focus(i);
13         i.setAttribute('_last', get_amount(i.name));
14 }
15
16 function blur_alloc(i) {
17
18         var last = +i.getAttribute('_last')
19         var left = get_amount('left_to_allocate', 1); 
20         var cur = Math.min(get_amount(i.name), get_amount('maxval'+i.name.substr(6), 1), last+left)
21
22         price_format(i.name, cur, user.pdec);
23         change = cur-last;
24
25         var total = get_amount('total_allocated', 1)+change;
26                 left -= change;
27         
28         price_format('left_to_allocate', left, user.pdec, 1, 1);
29         price_format('total_allocated', total, user.pdec, 1, 1);
30 }
31
32 function update_totals() {
33         var amount = 0;
34         var discount = 0;
35
36         for (var i=0; i<docs.length; i++) {
37                 amount += get_amount('amount'+docs[i])
38                 if (document.getElementsByName('early_disc'+docs[i])[0].checked 
39                         && (get_amount('un_allocated'+docs[i]) == get_amount('amount'+docs[i])))
40                                 discount += get_amount('early_disc'+docs[i]);
41         }
42         price_format('amount', amount-discount, user.pdec);
43         price_format('discount', discount, user.pdec);
44         
45 }
46
47 function allocate_all(doc) {
48         var amount = get_amount('amount'+doc);
49         var unallocated = get_amount('un_allocated'+doc);
50         var total = get_amount('total_allocated', 1);
51         var left = get_amount('left_to_allocate', 1);
52         total -=  (amount-unallocated);
53         left += (amount-unallocated);
54         amount = unallocated;
55         if(left<0) {
56                 total  += left;
57                 amount += left;
58                 left = 0;
59         }
60         price_format('amount'+doc, amount, user.pdec);
61         price_format('left_to_allocate', left, user.pdec, 1,1);
62         price_format('total_allocated', total, user.pdec, 1, 1);
63 }
64
65 function allocate_none(doc) {
66         amount = get_amount('amount'+doc);
67         left = get_amount('left_to_allocate', 1);
68         total = get_amount('total_allocated', 1);
69         price_format('left_to_allocate',amount+left, user.pdec, 1, 1);
70         price_format('amount'+doc, 0, user.pdec);
71         price_format('total_allocated', total-amount, user.pdec, 1, 1);
72 }
73
74 var allocations = {
75         '.amount': function(e) {
76                 e.onblur = function() {
77                         blur_alloc(this);
78                   };
79                 e.onfocus = function() {
80                         focus_alloc(this);
81                 };
82         },
83         '.check':function(e) {
84                 e.onclick = function() {
85                         update_totals();
86                 }
87         }
88 }
89
90 Behaviour.register(allocations);