facture: toggle display of periodicite date fields (#326)

This commit is contained in:
Benjamin Dauvergne 2021-11-20 18:50:30 +01:00
parent 91679603b1
commit b3d0c569df
2 changed files with 36 additions and 0 deletions

View File

@ -199,11 +199,28 @@ class ClientForm(forms.ModelForm):
class ContratForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['periodicite_debut'].widget.attrs['data-depends-on'] = 'periodicite'
self.fields['periodicite_fin'].widget.attrs['data-depends-on'] = 'periodicite'
def clean(self):
super().clean()
if 'periodicite_debut' in self.changed_data and self.instance.factures.filter(
numero_d_echeance__isnull=False
):
raise ValidationError(
'Vous ne pouvez pas changer le début d\'un contrat périodique qui a déjà des factures'
)
class Meta:
fields = '__all__'
model = models.Contrat
localized_fields = ("tva", "montant_sous_traite")
class Media:
js = ['facture.js']
class PaymentForm(forms.ModelForm):
class Meta:

View File

@ -0,0 +1,19 @@
$(function () {
console.log('facture.js loading...');
$('[data-depends-on]').each(function (i, elt) {
const depends_on_name = $(elt).data('depends-on');
const $widget = $('[name=' + depends_on_name + ']');
const $form_row = $(elt).parents('.form-row');
function update_visibility() {
const current_value = $widget.val();
const state = current_value != '';
$form_row.toggle(state);
$form_row.find('label').toggleClass('required', state);
$(elt).attr('required', state);
$(elt).attr('disabled', ! state); // prevent current value from being submitted
}
update_visibility();
$widget.on('change', update_visibility);
});
console.log('facture.js loaded.');
})