|
|
@@ -1297,6 +1297,70 @@
|
|
|
console.log("Modal result chiusa: ricarica la pagina");
|
|
|
window.location.reload();
|
|
|
});
|
|
|
+
|
|
|
+ document.addEventListener('DOMContentLoaded', function() {
|
|
|
+ // Function to calculate and update the amount field
|
|
|
+ function calculateAmount(rowIndex) {
|
|
|
+ const imponibileField = document.getElementById(`rows.${rowIndex}.imponibile`);
|
|
|
+ const aliquotaField = document.getElementById(`rows.${rowIndex}.aliquota_iva`);
|
|
|
+ const amountField = document.getElementById(`rows.${rowIndex}.amount`);
|
|
|
+
|
|
|
+ if (imponibileField && aliquotaField && amountField) {
|
|
|
+ // Get imponibile value (remove formatting)
|
|
|
+ let imponibile = imponibileField.value.replace('€', '').replace(/\./g, '').replace(',', '.').trim();
|
|
|
+ imponibile = parseFloat(imponibile) || 0;
|
|
|
+
|
|
|
+ // Get aliquota value (remove % sign)
|
|
|
+ let aliquota = aliquotaField.value.replace('%', '').trim();
|
|
|
+ aliquota = parseFloat(aliquota) || 0;
|
|
|
+
|
|
|
+ // Calculate amount: imponibile + (imponibile * aliquota / 100)
|
|
|
+ const imposta = imponibile * (aliquota / 100);
|
|
|
+ const totalAmount = imponibile + imposta;
|
|
|
+
|
|
|
+ // Format and set the amount value
|
|
|
+ let formattedAmount = "€ " + totalAmount.toFixed(2).replace('.', ',').replace(/\B(?=(\d{3})+(?!\d))/g, ".");
|
|
|
+ amountField.value = formattedAmount;
|
|
|
+
|
|
|
+ // Trigger the Livewire update to sync with backend
|
|
|
+ // This uses the Livewire.find method to access component methods
|
|
|
+ const component = Livewire.find(document.querySelector('[wire\\:id]').getAttribute('wire:id'));
|
|
|
+ if (component) {
|
|
|
+ component.set(`rows.${rowIndex}.amount`, formattedAmount);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Attach event listeners to imponibile and aliquota fields
|
|
|
+ function setupCalculationListeners() {
|
|
|
+ const rows = document.querySelectorAll('input[id^="rows."][id$=".imponibile"], input[id^="rows."][id$=".aliquota_iva"]');
|
|
|
+
|
|
|
+ rows.forEach(field => {
|
|
|
+ field.addEventListener('blur', function() {
|
|
|
+ // Extract row index from the field ID
|
|
|
+ const rowIndex = this.id.match(/rows\.(\d+)\./)[1];
|
|
|
+ calculateAmount(rowIndex);
|
|
|
+ });
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // Initial setup
|
|
|
+ setupCalculationListeners();
|
|
|
+
|
|
|
+ // Re-attach listeners after Livewire updates
|
|
|
+ document.addEventListener('livewire:load', function() {
|
|
|
+ setupCalculationListeners();
|
|
|
+ });
|
|
|
+
|
|
|
+ document.addEventListener('livewire:update', function() {
|
|
|
+ setupCalculationListeners();
|
|
|
+ });
|
|
|
+
|
|
|
+ // Also attach to rows added dynamically
|
|
|
+ Livewire.on('load-select', function() {
|
|
|
+ setTimeout(setupCalculationListeners, 300); // Give time for DOM to update
|
|
|
+ });
|
|
|
+});
|
|
|
</script>
|
|
|
|
|
|
@endpush
|