TVA en sélection des taux officiels français, TTC non éditable
Le champ TVA devient un select (0 %, 2,1 %, 5,5 %, 10 %, 20 %) plutôt qu'une saisie libre, avec une option "Autre (préciser)" qui révèle le champ décimal existant -- indispensable pour les lignes migrées, dont le taux rétro-calculé (figli_compta_ledger_update_8005()) est souvent un taux effectif non standard qu'il ne faut surtout pas forcer à s'aligner sur l'une des 5 valeurs officielles. Montant TTC passe de readonly à #disabled : non focusable, non éditable même via les flèches d'un input number, et Form API rejette toute valeur soumise malgré tout au profit de #default_value. Le select est un élément de formulaire à part (pas un widget de champ) pour éviter que WidgetBase::extractFormValues() ne s'étouffe sur une clé étrangère mêlée aux valeurs de field_tva -- un #validate callback recopie la valeur choisie dans field_tva au moment opportun (après tous les #validate, avant la reconstruction de l'entité au submit). L'affichage/masquage du champ "Autre" repose sur du JS simple plutôt que sur #states : #states pose bien l'attribut data-drupal-states mais ne bascule jamais la visibilité dans cette modale AJAX précise, pour une raison non identifiée (un #states pourtant fonctionnel existe juste au-dessus, sur field_entree_liee, qui surveille un vrai champ Field API plutôt qu'un select ajouté à la main).
This commit is contained in:
@@ -25,17 +25,29 @@
|
||||
once('figli-ledger-tva-calc', '[name="field_montant_ht[0][value]"]', context).forEach(function (ht) {
|
||||
var form = ht.closest('form');
|
||||
var tva = form.querySelector('[name="field_tva[0][value]"]');
|
||||
var rate = form.querySelector('[name="field_tva_rate"]');
|
||||
var ttc = form.querySelector('[name="field_montant_ttc[0][value]"]');
|
||||
if (!tva || !ttc) {
|
||||
return;
|
||||
}
|
||||
|
||||
// The rate select (when not "Autre") is the actual source of
|
||||
// truth here -- figli_compta_ledger_apply_tva_rate() copies its
|
||||
// value into field_tva server-side on submit, but that happens
|
||||
// too late to matter for a live client-side preview.
|
||||
function currentTva() {
|
||||
if (rate && rate.value !== 'autre') {
|
||||
return parseFloat(rate.value);
|
||||
}
|
||||
return parseFloat(tva.value);
|
||||
}
|
||||
|
||||
function recompute() {
|
||||
var htValue = parseFloat(ht.value);
|
||||
if (isNaN(htValue)) {
|
||||
return;
|
||||
}
|
||||
var tvaValue = parseFloat(tva.value);
|
||||
var tvaValue = currentTva();
|
||||
if (isNaN(tvaValue)) {
|
||||
tvaValue = 0;
|
||||
}
|
||||
@@ -48,12 +60,48 @@
|
||||
// the centime (see figli_compta_ledger_update_8005()'s
|
||||
// docblock) -- showing a recomputed number the instant the
|
||||
// modal opens, before the associate has touched anything, would
|
||||
// misrepresent what's actually saved. Only recompute once HT or
|
||||
// TVA are actually edited, matching the same "only overwrite
|
||||
// Montant TTC when HT/TVA actually changed" guard in
|
||||
// figli_compta_ledger_node_presave().
|
||||
// misrepresent what's actually saved. Only recompute once HT,
|
||||
// TVA, or the rate select are actually edited, matching the
|
||||
// same "only overwrite Montant TTC when HT/TVA actually
|
||||
// changed" guard in figli_compta_ledger_node_presave(). Harmless
|
||||
// no-op on a genuinely new/blank line either way (HT is empty,
|
||||
// recompute() bails out above).
|
||||
ht.addEventListener('input', recompute);
|
||||
tva.addEventListener('input', recompute);
|
||||
if (rate) {
|
||||
rate.addEventListener('change', recompute);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Shows/hides the real "Taux exact (%)" field_tva input based on the
|
||||
* field_tva_rate select added in figli_compta_ledger_form_alter() --
|
||||
* plain JS rather than #states: tested and confirmed #states' own
|
||||
* data-drupal-states attribute gets attached correctly here but never
|
||||
* actually toggles visibility, for reasons not tracked down (a
|
||||
* working #states dependency already exists a few fields up, on
|
||||
* field_entree_liee, watching a real Field API element rather than
|
||||
* this manually-added select -- that one difference is the only lead
|
||||
* so far). Not worth blocking on since this form already needs custom
|
||||
* JS anyway for the TTC live preview above.
|
||||
*/
|
||||
Drupal.behaviors.figliLedgerTvaRateToggle = {
|
||||
attach: function (context) {
|
||||
once('figli-ledger-tva-rate-toggle', '[name="field_tva_rate"]', context).forEach(function (select) {
|
||||
var form = select.closest('form');
|
||||
var exact = form.querySelector('.field--name-field-tva');
|
||||
if (!exact) {
|
||||
return;
|
||||
}
|
||||
|
||||
function toggle() {
|
||||
exact.style.display = select.value === 'autre' ? '' : 'none';
|
||||
}
|
||||
|
||||
select.addEventListener('change', toggle);
|
||||
toggle();
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user