1ea079866a9af88fcf1f24933dd9e5174645a3d3
[fa-stable.git] / js / payalloc.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                 var change = get_amount(i.name);
18                 
19                 if (i.name != 'amount' && i.name != 'charge' && i.name != 'discount')
20                         change = Math.min(change, get_amount('maxval'+i.name.substr(6), 1))
21
22                 price_format(i.name, change, user.pdec);
23                 if (i.name != 'amount' && i.name != 'charge') {
24                         if (change<0) change = 0;
25                         change = change-i.getAttribute('_last');
26                         if (i.name == 'discount') change = -change;
27
28                         var total = get_amount('amount')+change;
29                         price_format('amount', total, user.pdec, 0);
30                 }
31 }
32
33 function update_totals() {
34         var amount = 0;
35         var discount = 0;
36
37         for (var i=0; i<docs.length; i++) {
38                 amount += get_amount('amount'+docs[i])
39                 if (document.getElementsByName('early_disc'+docs[i])[0].checked 
40                         && (get_amount('un_allocated'+docs[i]) == get_amount('amount'+docs[i])))
41                                 discount += get_amount('early_disc'+docs[i]);
42         }
43         price_format('amount', amount-discount, user.pdec);
44         price_format('discount', discount, user.pdec);
45         
46 }
47
48 function allocate_all(doc) {
49         var unallocated = get_amount('maxval'+doc, 1);
50         price_format('amount'+doc, unallocated, user.pdec);
51         update_totals();
52 }
53
54 function allocate_none(doc) {
55         price_format('amount'+doc, 0, user.pdec);
56         update_totals();
57 }
58
59
60 var allocations = {
61         '.amount': function(e) {
62                 if(e.name == 'allocated_amount' || e.name == 'bank_amount')
63                 {
64                   e.onblur = function() {
65                         var dec = this.getAttribute("dec");
66                         price_format(this.name, get_amount(this.name), dec);
67                   };
68                 } else {
69                         e.onblur = function() {
70                                 blur_alloc(this);
71                         };
72                         e.onfocus = function() {
73                                 focus_alloc(this);
74                         };
75                 }
76         },
77         '.check':function(e) {
78                 e.onclick = function() {
79                         update_totals();
80                 }
81         }
82 }
83
84 Behaviour.register(allocations);