I have a form:
Where I have a 'supplier' (proveedor) select that brings me the 'withholding' (retención) percentage that belongs to them when selecting it in the following way:
$(".js-proveedor-id").on('change', function() {
var proveedor_id = $("#proveedor_id");
$.ajax({
url: "{{ route('get_proveedor_by_id') }}",
method: 'GET',
data: {
proveedor_id: proveedor_id.val(),
},
success: function(data) {
$("#retencion").val(data.retencion);
retencion = data.retencion;
}
});
totales(retencion);
});
Now, my intention is that every time the select option is changed, it brings me the value of the withholding % and performs all the invoice calculations again; but for some reason it is not taking me the change of the retention value until I change the select again.
For example: I select a provider with 100% retention and add the items I want and everything works, but if I want to change the provider and select one with 0%, no change is made until I select another one again and it reflects the calculation with 0% retention; it's as if it's keeping the previously selected value.
If I add another item after selecting another provider, the withholding % is updated; but when changing it or deleting an item, no.
My totales()
function:
function totales() {
retencion = $("#retencion").val();
$("#subtotal").html("$ " + subtotalapagar.toFixed(2));
subtotal_pagar = subtotalapagar;
$("#subtotal_pagar_html").html("$ " + subtotal_pagar.toFixed(2));
$("#subtotal_pagar").val(subtotal_pagar.toFixed(2));
impuesto = subtotalapagar * 0.16;
$("#impuesto").html("$ " + impuesto.toFixed(2));
impuesto_pagar = impuesto;
$("#impuesto_pagar_html").html("$ " + impuesto_pagar.toFixed(2));
$("#impuesto_pagar").val(impuesto_pagar.toFixed(2));
retencionapagar = impuesto_pagar * (retencion / 100);
$("#retencion").html("$ " + retencionapagar.toFixed(2));
$("#retencion_pagar_html").html("$ " + retencionapagar.toFixed(2));
$("#retencion_pagar").val(retencionapagar.toFixed(2));
total_pagar = subtotalapagar + impuesto_pagar - retencionapagar;
$("#total").html("$ " + total_pagar.toFixed(2));
$("#total_pagar_html").html("$ " + total_pagar.toFixed(2));
$("#total_pagar").val(total_pagar.toFixed(2));
tasa = $("#tasabs").val();
total_pagarbs = tasa * total_pagar;
$("#totalbs").html("Bs. " + total_pagarbs.toFixed(2));
$("#total_pagar_htmlbs").html("Bs. " + total_pagarbs.toFixed(2));
$("#total_pagarbs").val(total_pagarbs.toFixed(2));
}
and eliminar()
:
function eliminar(index) {
retencion = $("#retencion").val();
subtotalapagar = subtotalapagar - subtotal[index];
impuesto = subtotalapagar * 0.16;
retencionapagar = impuesto * (retencion / 100);
total = subtotalapagar + impuesto - retencionapagar;
total_pagar_html = total;
$("#impuesto").html("$ " + impuesto.toFixed(2));
impuesto_pagar = impuesto;
$("#impuesto_pagar_html").html("$ " + impuesto_pagar.toFixed(2));
$("#impuesto_pagar").val(impuesto_pagar.toFixed(2));
$("#retencion").html("$ " + retencionapagar.toFixed(2));
$("#retencion_pagar_html").html("$ " + retencionapagar.toFixed(2));
$("#retencion_pagar").val(retencionapagar.toFixed(2));
subtotal_pagar = subtotalapagar;
$("#subtotal").html("$ " + subtotalapagar.toFixed(2));
$("#subtotal_pagar_html").html("$ " + subtotal_pagar.toFixed(2));
$("#subtotal_pagar").val(subtotal_pagar.toFixed(2));
$("#total").html("$ " + total);
$("#total_pagar_html").html("$ " + total_pagar_html.toFixed(2));
$("#total_pagar").val(total_pagar_html.toFixed(2));
total_pagarbs = tasa * total;
$("#totalbs").html("Bs. " + total_pagarbs.toFixed(2));
$("#total_pagar_htmlbs").html("Bs. " + total_pagarbs.toFixed(2));
$("#total_pagarbs").val(total_pagarbs.toFixed(2));
$("#fila" + index).remove();
evaluar();
}
What I don't understand is why if it changes the value of the input inside the form (where it says % de retención it writes the number taken from the database) but it doesn't take that same value inside the totales()
function