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