bc98a122b2708b5c92627ada64990ce04fa271af
[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 value = roundNumber(value, 2)
21
22 montant_annuel.val(roundNumber(value, 2));
23
24 var devise = input.closest('table').find('#' + prefix + '-devise').val();
25 var taux = parseFloat(DEVISES[devise]);
26 if (isNaN(taux)) taux = 0;
27 montant_annuel_euros.text(roundNumber((value * taux), 2));
28 }
29
30 function ajouterLigne(addLnk) {
31 if (formCount == null) {
32 formCount = addLnk.closest('table').find('.remunform').length;
33 }
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++;
50 }
51
52
53 function totalByIndex(selector, accessor) {
54 /*
55 aelector: the element where to display costs, and from which the index will be calculated.
56 accessor: a function to get all items that will be part of the total.
57 */
58 // $(selector).hide();
59 $(selector).each(function(){
60 var subtot = 0;
61 var i = $(this).index();
62 var prevs = accessor($(this));
63 prevs.each(function(){
64 var cell = $($(this).children('td')[i]);
65 var val;
66 if (cell.children('input').length == 1) {
67 val = cell.children('input').val();
68 } else {
69 val = cell.html();
70 }
71 subtot += clean_float(val);
72 });
73 $(this).html(roundNumber(subtot, 2));
74 });
75 }
76
77 function remun_totaux() {
78 $('#global-cost tr td.monnaie.cumulable input[type="text"], #global-cost tr td.monnaie.cumulable input[type="hidden"]').each(function() {
79 remun_line($(this));
80 });
81 totalByIndex('#global-cost td.sous-total',
82 function(x){return x.parent('tr').prevUntil(':not(.calculable)')});
83 totalByIndex('#global-cost th.total',
84 function(x){return x.parent('tr').prevAll('.sous-totaux')});
85 }
86
87
88 $(document).ready(function(){
89 /* totaux remu */
90 $('#global-cost input, #global-cost select').change(function() {
91 // remun_line($(this));
92 remun_totaux();
93 });
94
95 $('#global-cost .addlink').click(function(e){
96 e.preventDefault();
97 el = $(this);
98 ajouterLigne(el);
99 });
100 remun_totaux();
101
102
103 });