}
function remun_line(input) {
- var idParts = input.attr('id').split('-');
- var prefix = idParts[0] + "-" + idParts[1];
- var field = idParts[2];
+ var idParts = input.attr('id').split('-');
+ var prefix = idParts[0] + "-" + idParts[1];
+ var field = idParts[2];
- var montant_annuel = $('#' + prefix + '-montant');
- var montant_annuel_euros = $('#' + prefix + '-montant_annuel_euros');
+ var montant_annuel = $('#' + prefix + '-montant');
+ var montant_annuel_euros = $('#' + prefix + '-montant_annuel_euros');
- /* auto calcul a besoin d'un type (autrement ca devient un champs requis)*/
- if ($('#' + prefix + '-type').val() == '') {
- montant_annuel.val('');
- montant_annuel_euros.val('');
- return;
- }
+ /* auto calcul a besoin d'un type (autrement ca devient un champs requis)*/
+ // if ($('#' + prefix + '-type').val() == '') {
+ // montant_annuel.val('');
+ // montant_annuel_euros.val('');
+ // return;
+ // }
- value = (montant_annuel.val());
- value = roundNumber(value, 2)
+ value = (montant_annuel.val());
+ value = roundNumber(value, 2)
- montant_annuel.val(roundNumber(value, 0));
+ montant_annuel.val(roundNumber(value, 2));
- var devise = $('#' + prefix + '-devise').val();
- var taux = parseFloat(DEVISES[devise]);
- if (isNaN(taux)) taux = 0;
- montant_annuel_euros.text(roundNumber((value * taux), 0))
+ var devise = $('#' + prefix + '-devise').val();
+ var taux = parseFloat(DEVISES[devise]);
+ if (isNaN(taux)) taux = 0;
+ montant_annuel_euros.text(roundNumber((value * taux), 2));
}
function totalByIndex(selector, accessor) {
}
subtot += clean_float(val);
});
- $(this).html(subtot);
+ $(this).html(roundNumber(subtot, 2));
});
}
class FlexibleRemunForm(forms.ModelForm):
-
+ # Utilisé dans templats.
montant_mensuel = forms.DecimalField(required=False)
montant = forms.DecimalField(required=True, label='Montant annuel')
class ReadOnlyRemunForm(FlexibleRemunForm):
+ # Utilisé dans templats.
+
def __init__(self, *a, **kw):
- super(FlexibleRemunForm, self).__init__(*a, **kw)
+ super (ReadOnlyRemunForm, self).__init__(*a, **kw)
for field in self.fields:
field = self.fields[field]
if not isinstance(field.widget, (
# Add extra forms (n extra for each grop).
tmp_extras = []
for i in xrange(len(self.groups) * self.extra):
- tmp_extras.insert(0, self._construct_form(i))
+ tmp_extras.insert(0,
+ self._construct_form(self.initial_form_count() + i))
for g in self.groups:
for i in xrange(self.extra):
exclude=None,
can_order=False,
can_delete=True,
+ read_only=False,
extra=2,
max_num=None,
formfield_callback=None,
}
FormSet = modelformset_factory(model, **kwargs)
FormSet.fk = fk
+ FormSet.read_only = read_only
def grouper(form):
rtype = form.initial['type']
rtype.nature_remuneration
)
- def __init__(inst, *a, **kw):
- super(FormSet, inst).__init__(*a, **kw)
- # Warning, this constructor can be improved and made more
- # efficient.. it's a bit of a hack that will help creating a
- # "grouped" formset that works inside Django. Although the
- # code makes several iterations, I've timed it at 1ms to
- # for it's execution.
- # # Set initial data.
- # d_inst = kw.get('instance', None)
-
- # Set initial for all form type.
- # for form, tr in zip(inst.forms, trs):
- # form.initial['type'] = tr
-
- # # Set form grouping.
+ # Monkey patch FormSet.
+ def __init__(inst, *a, **kw):
+ super(inst.__class__, inst).__init__(*a, **kw)
inst.set_groups(groups, grouper, choice_overrides)
- # # Create groups list of choices for type field.
- # inst.group_type_choices = {}
- # for group_key in inst.groups:
- # inst.group_type_choices[group_key] = []
-
- # # Add choices based on form.initial which have been set above.
- # for form in inst.forms:
- # inst.group_type_choices[
- # form.initial['type'].nature_remuneration].append(
- # (form.initial['type'].id, form.initial['type'].nom)
- # )
-
- # # Finally... change the choices of each form in the formset:
- # for form in inst.forms:
- # form.fields['type'].choices = inst.group_type_choices[
- # form.initial['type'].nature_remuneration]
-
-
FormSet.__init__ = __init__
return FormSet
-ReadOnlyRemunForm = remun_formset_factory(
- dae.Dossier,
- dae.Remuneration,
- form=ReadOnlyRemunForm,
- groups = [
- (u'Traitement', u'Traitement',),
- (u'Indemnité', u'Indemnité',),
- (u'Charges', u'Charges',),
- (u'Accessoire', u'Accessoire',),
- ]
- )
-
-null_choice = ('', '-' * 10)
-RemunForm = remun_formset_factory(
- dae.Dossier,
- dae.Remuneration,
- form=FlexibleRemunForm,
- groups = [
- (u'Traitement', u'Traitement',),
- (u'Indemnité', u'Indemnité',),
- (u'Charges', u'Charges',),
- (u'Accessoire', u'Accessoire',),
- ],
- choice_overrides = {
- u'Traitement': {
- 'type': [null_choice] + list(
- rh.TypeRemuneration.objects.filter(
- nature_remuneration=u'Traitement').values_list(
- 'id', 'nom')
- )
- },
- u'Indemnité': {
- 'type': [null_choice] + list(
- rh.TypeRemuneration.objects.filter(
- nature_remuneration=u'Indemnité').values_list(
- 'id', 'nom')
- )
- },
- u'Charges': {
- 'type': [null_choice] + list(
- rh.TypeRemuneration.objects.filter(
- nature_remuneration=u'Charges').values_list(
- 'id', 'nom')
- )
- },
- u'Accessoire': {
- 'type': [null_choice] + list(
- rh.TypeRemuneration.objects.filter(
- nature_remuneration=u'Accessoire').values_list(
- 'id', 'nom')
- )
- },
- }
- )
+def remun_formset_factory_factory(read_only=False):
+ """
+ Don't we love factory factories?
+ """
+
+ null_choice = ('', '-' * 10)
+ extras = 2 if not read_only else 0
+ can_delete = False if read_only else True
+ form_class = ReadOnlyRemunForm if read_only else FlexibleRemunForm
+
+ return remun_formset_factory(
+ dae.Dossier,
+ dae.Remuneration,
+ form=form_class,
+ extra=extras,
+ can_delete=can_delete,
+ read_only=read_only,
+ groups = [
+ (u'Traitement', u'Traitement',),
+ (u'Indemnité', u'Indemnité',),
+ (u'Charges', u'Charges',),
+ (u'Accessoire', u'Accessoire',),
+ (u'RAS', u'Rémunération autre source',),
+ ],
+ choice_overrides = {
+ u'Traitement': {
+ 'type': [null_choice] + list(
+ rh.TypeRemuneration.objects.filter(
+ nature_remuneration=u'Traitement').values_list(
+ 'id', 'nom')
+ )
+ },
+ u'Indemnité': {
+ 'type': [null_choice] + list(
+ rh.TypeRemuneration.objects.filter(
+ nature_remuneration=u'Indemnité').values_list(
+ 'id', 'nom')
+ )
+ },
+ u'Charges': {
+ 'type': [null_choice] + list(
+ rh.TypeRemuneration.objects.filter(
+ nature_remuneration=u'Charges').values_list(
+ 'id', 'nom')
+ )
+ },
+ u'Accessoire': {
+ 'type': [null_choice] + list(
+ rh.TypeRemuneration.objects.filter(
+ nature_remuneration=u'Accessoire').values_list(
+ 'id', 'nom')
+ )
+ },
+ u'RAS': {
+ 'type': [null_choice] + list(
+ rh.TypeRemuneration.objects.filter(
+ nature_remuneration=u'RAS').values_list(
+ 'id', 'nom')
+ )
+ },
+ }
+ )
-RemunForm = ReadOnlyRemunForm
+RemunForm = remun_formset_factory_factory(read_only=False)
+ReadOnlyRemunForm = remun_formset_factory_factory(read_only=True)
class PosteForm(forms.ModelForm):
<th>Annuel</th>
<th>Annuel Euros</th>
<th>Précision</th>
+ {% if not remunForm.read_only %}
<th>Supprimer</th>
+ {% endif %}
</tr>
{% for group in remunForm.group_list %}
{% if group.key != 'RAS' %}
<td>{{ f.type.errors }} {{ f.type }}</td>
<td>{{ f.devise.errors }} {{ f.devise }}</td>
<td class="monnaie cumulable">{{ f.montant.errors }} {{ f.montant }}</td>
- <td class="euro cumulable" id="{{ f.prefix }}-montant_annuel_euros"></td>
+ <td class="euro cumulable" id="id_{{ f.prefix }}-montant_annuel_euros"></td>
<td>{{ f.commentaire.errors }} {{ f.commentaire }}</td>
+ {% if not remunForm.read_only %}
<td>{{ f.DELETE }}</td>
+ {% endif %}
</tr>
{% endfor %}
<tr class="sous-totaux">
- <td>Sous-total -
- <a class="addlink" href="/admin/dae/poste/add/">
+ <td>Sous-total{% if not remunForm.read_only %} -
+ <a class="addlink" href="#">
[<span class="icon">Ajouter une ligne</span>]
</a>
+ {% endif %}
</td>
<td><!-- Laisser ce td pour que le javascript fonctionne bien. --></td>
<td class="sous-total"></td>
<td class="sous-total"></td>
- <td colspan="2"></td>
+ <td> </td>
+ {% if not remunForm.read_only %}
+ <td> </td>
+ {% endif %}
</tr>
{% endif %}
{% endfor %}
<th><!-- Laisser ce td pour que le javascript fonctionne bien. --></th>
<th class="total"></th>
<th class="total"></th>
- <th colspan="2"></th>
+ <th> </th>
+ {% if not remunForm.read_only %}
+ <th> </th>
+ {% endif %}
</tr>
<tr>
<th><strong>Rémunération autre source</strong></th>
<th><!-- Laisser ce td pour que le javascript fonctionne bien. --></th>
<th></th>
<th></th>
- <th colspan="2"></th>
+ <th> </th>
+ {% if not remunForm.read_only %}
+ <th> </th>
+ {% endif %}
</tr>
{% for f in remunForm.groups.RAS.forms %}
<tr class="calculable remunform">
<td class="monnaie cumulable">{{ f.montant.errors }} {{ f.montant }}</td>
<td class="euro cumulable" id="id_{{ f.prefix }}-montant_annuel_euros"></td>
<td>{{ f.commentaire.errors }} {{ f.commentaire }}</td>
+ {% if not remunForm.read_only %}
<td>{{ f.DELETE }}</td>
+ {% endif %}
</tr>
{% endfor %}
<tr class="sous-totaux">
- <td>Sous-total -
+ <td>Sous-total{% if not remunForm.read_only %} -
<a class="addlink" href="#">
[<span class="icon">Ajouter une ligne</span>]
</a>
+ {% endif %}
</td>
<td><!-- Laisser ce td pour que le javascript fonctionne bien. --></td>
<td class="sous-total"></td>
<td class="sous-total"></td>
- <td colspan="2"></td>
+ <td> </td>
+ {% if not remunForm.read_only %}
+ <td> </td>
+ {% endif %}
</tr>
</table>
from django.utils.safestring import mark_safe
from django.forms.widgets import Select, TextInput
+from django.db.models.query import QuerySet
-class ReadOnlyChoiceWidget(Select):
- def render(self, name, value, attrs=None, choices=()):
- try:
- key = long(value)
- except ValueError:
- key = ''
+class ReadOnlyChoiceWidget(TextInput):
+ def __init__(self, *a, **kw):
+ choices = kw.pop('choices', [])
+ super(ReadOnlyChoiceWidget, self).__init__(*a, **kw)
+ self.choices = choices
+
+ def render(self, name, value, attrs=None):
+ if isinstance(self.choices, QuerySet) and value:
+ display = self.choices.get(id=value)
+ elif value:
+ display = dict(self.choices)[value]
+ else:
+ display = ''
return mark_safe(
- '%(display)s</span><input type="hidden" '
- 'name="%(name)s" id="%(name)s" value="%(value)s" />' % {
- 'display': dict(self.choices)[key],
+ '%(display)s<input id="id_%(name)s" type="hidden" '
+ 'name="%(name)s" value="%(value)s" />' % {
+ 'display': display,
'name': name,
'value': value,
})
class ReadOnlyWidget(TextInput):
def render(self, name, value, attrs=None):
return mark_safe(
- '%(display)s<input id="%(name)s" type="hidden" '
+ '%(display)s<input id="id_%(name)s" type="hidden" '
'name="%(name)s" value="%(value)s" />' % {
'display': value,
'name': name,