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