Feat: Calculo de subtotal para muchas lineas
This commit is contained in:
		@@ -1,3 +1,6 @@
 | 
			
		||||
const quantity_lineRegexSelector = `[id^="${idPrefix}"][id$="${quantitySuffix}"]`;
 | 
			
		||||
const price_lineRegexSelector = `[id^="${idPrefix}"][id$="${priceSuffix}"]`;
 | 
			
		||||
 | 
			
		||||
function insertSubtotalField() {
 | 
			
		||||
  // Selecciona la fila de precio unitario para añadir la fila del subtotal después de ella
 | 
			
		||||
  const unitPriceRow = document.querySelector('input[id="id_saleline_set-0-unit_price"]').closest('tr');
 | 
			
		||||
@@ -13,10 +16,10 @@ function insertSubtotalField() {
 | 
			
		||||
     unitPriceRow.after(subtotalRow);
 | 
			
		||||
 }
 | 
			
		||||
 | 
			
		||||
function calculateSubtotal() {
 | 
			
		||||
     const quantityElement = document.getElementById('id_saleline_set-0-quantity');
 | 
			
		||||
     const unitPriceElement = document.getElementById('id_saleline_set-0-unit_price');
 | 
			
		||||
     const subtotalElement = document.getElementById('id_saleline_set-0-subtotal');
 | 
			
		||||
function calculateSubtotal(id) {
 | 
			
		||||
     const quantityElement = document.getElementById(`id_saleline_set-${id}-quantity`);
 | 
			
		||||
     const unitPriceElement = document.getElementById(`id_saleline_set-${id}-unit_price`);
 | 
			
		||||
     const subtotalElement = document.getElementById(`id_saleline_set-${id}-subtotal`);
 | 
			
		||||
 | 
			
		||||
     const quantity = parseFloat(quantityElement.value) || 0;
 | 
			
		||||
     const unitPrice = parseFloat(unitPriceElement.value) || 0;
 | 
			
		||||
@@ -27,9 +30,21 @@ function calculateSubtotal() {
 | 
			
		||||
 | 
			
		||||
 // Inserta el campo subtotal al cargar la página
 | 
			
		||||
 window.addEventListener('load', () => {
 | 
			
		||||
     insertSubtotalField();
 | 
			
		||||
   insertSubtotalField();
 | 
			
		||||
 | 
			
		||||
     // Agregar los eventos para calcular el subtotal cuando cambie la cantidad o el precio unitario
 | 
			
		||||
     document.getElementById('id_saleline_set-0-quantity').addEventListener('input', calculateSubtotal);
 | 
			
		||||
     document.getElementById('id_saleline_set-0-unit_price').addEventListener('input', calculateSubtotal);
 | 
			
		||||
   complete_form.addEventListener('change', function(event){
 | 
			
		||||
     const quantityInputs = document.querySelectorAll(quantity_lineRegexSelector);
 | 
			
		||||
     const ids = Array.prototype.map.call(quantityInputs, function(input) {
 | 
			
		||||
       return input.id.match(/\d+/)[0];
 | 
			
		||||
     });
 | 
			
		||||
 | 
			
		||||
     ids.forEach(function(id) {
 | 
			
		||||
       if (event.target.matches(quantity_lineRegexSelector)) {
 | 
			
		||||
         calculateSubtotal(id);
 | 
			
		||||
       }
 | 
			
		||||
       if (event.target.matches(price_lineRegexSelector)) {
 | 
			
		||||
         calculateSubtotal(id);
 | 
			
		||||
       }
 | 
			
		||||
     })
 | 
			
		||||
   });
 | 
			
		||||
 });
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user