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