Finished with new calcs for remunerations
[auf_rh_dae.git] / project / assets / js / remun.js
1 var namePatt = new RegExp(/-(\d+)-/);
2 formCount = null;
3
4 function remun_line(input) {
5 var idParts = input.attr('id').split('-');
6 var prefix = idParts[0] + "-" + idParts[1];
7 var field = idParts[2];
8
9 var montant_annuel = input.closest('table').find('#' + prefix + '-montant');
10 var montant_annuel_euros = input.closest('table').find('#' + prefix + '-montant_annuel_euros');
11
12 /* auto calcul a besoin d'un type (autrement ca devient un champs requis)*/
13 // if ($('#' + prefix + '-type').val() == '') {
14 // montant_annuel.val('');
15 // montant_annuel_euros.val('');
16 // return;
17 // }
18
19 value = montant_annuel.val();
20
21 montant_annuel.val(roundNumber(value, 2));
22
23 var devise = input.closest('table').find('#' + prefix + '-devise').val();
24 var taux = parseFloat(DEVISES[devise]);
25 if (isNaN(taux)) taux = 0;
26 montant_annuel_euros.text(roundNumber((value * taux), 2));
27 }
28
29 function ajouterLigne(addLnk) {
30 if (formCount == null) {
31 formCount = addLnk.closest('table').find('.remunform').length;
32 }
33 var prev = addLnk.parent('td').parent('tr').prev('tr');
34 var copyOfLastInGroup = prev.clone();
35 copyOfLastInGroup.find('input, select, td.cumulable').each(function(i, e){
36 var origName = $(e).attr('id');
37 var origId = $(e).attr('id');
38 $(e).attr('name', origName.replace(namePatt, '-' + formCount + '-'))
39 $(e).attr('id', origId.replace(namePatt, '-' + formCount + '-'))
40 $(e).val('');
41 });
42
43 prev.after(copyOfLastInGroup);
44
45 copyOfLastInGroup.find('input, select').change(function(e){
46 remun_totaux();
47 });
48 formCount++;
49 }
50
51
52 function totalByIndex(selector, accessor) {
53 /*
54 aelector: the element where to display costs, and from which the index will be calculated.
55 accessor: a function to get all items that will be part of the total.
56 */
57 // $(selector).hide();
58 $(selector).each(function(){
59 var subtot = 0;
60 var i = $(this).index();
61 var prevs = accessor($(this));
62 prevs.each(function(){
63 var cell = $($(this).children('td')[i]);
64 var val;
65 if (cell.children('input').length == 1) {
66 val = cell.children('input').val();
67 } else {
68 val = cell.html();
69 }
70 subtot += clean_float(val);
71 });
72 $(this).html(roundNumber(subtot, 2));
73 });
74 }
75
76 function remun_totaux() {
77 $('#global-cost tr td.monnaie.cumulable input[type="text"], #global-cost tr td.monnaie.cumulable input[type="hidden"]').each(function() {
78 remun_line($(this));
79 });
80 totalByIndex('#global-cost td.sous-total',
81 function(x){return x.parent('tr').prevUntil(':not(.calculable)')});
82 totalByIndex('#global-cost th.total',
83 function(x){return x.parent('tr').prevAll('.sous-totaux')});
84 }
85
86
87 $(document).ready(function(){
88 /* totaux remu */
89 $('#global-cost input, #global-cost select').change(function() {
90 // remun_line($(this));
91 remun_totaux();
92 });
93
94 $('#global-cost .addlink').click(function(e){
95 e.preventDefault();
96 el = $(this);
97 ajouterLigne(el);
98 });
99 remun_totaux();
100
101
102 });