I have a small bug with a Sales system, for which I show and explain first the two functions where I have the problem, the following function is executed when you add details to the sale:
//*** Declaración de Variables necesarias para trabajar con las ventas y sus detalles ***//
//*** Valor actual de Impuesto ***//
var impuesto=18;
var cont=0;
var detalles=0;
var cantidad=1;
//*** Función que es invocada por el boton(+) de la ventana modal Articulos ***//
//*** Se encarga de agregar el articulo seleccionado a la ventana detalles de la venta ***//
function agregarDetalle(idarticulo,articulo,stock,precio_venta)
{
//*** Valores iniciales ***//
var descuento=0;
if (idarticulo!="")
{
var subtotal=cantidad*precio_venta;
var fila='<tr class="filas" id="fila'+cont+'">'+
'<td><button type="button" class="btn btn-danger" onclick="eliminarDetalle('+cont+')">X</button></td>'+
'<td><input type="hidden" name="idarticulo[]" value="'+idarticulo+'">'+articulo+'</td>'+
'<td><input type="hidden" name="stock[]" id="stock[]" value="'+stock+'">'+stock+'</td>'+
'<td><input type="number" onchange="modificarSubtotales()" name="cantidad[]" id="cantidad[]" value="'+cantidad+'"></td>'+
'<td><input type="number" name="precio_venta[]" id="precio_venta[]" value="'+precio_venta+'"></td>'+
'<td><input type="number" name="descuento[]" value="'+descuento+'"></td>'+
'<td><span name="subtotal" id="subtotal'+cont+'">'+subtotal+'</span></td>'+
'<td><button type="button" onclick="modificarSubtotales()" class="btn btn-info"><i class="fa fa-refresh"></i></button></td>'+
'</tr>';
cont++;
detalles=detalles+1;
//*** Codigo que agrega al objeto con el id detalles todo lo que contiene la variable fila ***//
$('#detalles').append(fila);
//*** Codigo para actualizar Subtotales desde un primer instante ***//
modificarSubtotales();
}
else
{
alert("Error al ingresar el detalle, revisar los datos del artículo");
}
}
The following function is executed when the details are updated or the value is changed in the cantidad
field:
function modificarSubtotales()
{
//*** Declarar arrays para almacenar cantidad, precio_compra_subtotal ***//
//*** Por ejemplo el array cant almacenara todas las cantidades de la venta ***//
var cant = document.getElementsByName("cantidad[]");
var stock = document.getElementsByName("stock[]");
var prec = document.getElementsByName("precio_venta[]");
var desc = document.getElementsByName("descuento[]");
var sub = document.getElementsByName("subtotal");
//*** Recorrer elementos de detalles para actualizar el Subtotal ***//
//*** El bucle se ejecutara hasta llegar a la cantidad de indices que tiene la variable cant ***//
for (var i = 0; i <cant.length; i++) {
var inpST=stock[i];
//*** Variables para almacenar las cantidades de los detalles respecto a sus indices [i] ***//
var inpC=cant[i];
var inpP=prec[i];
var inpD=desc[i];
var inpS=sub[i];
//*** Condicional que compara y valida el stock respecto a la cantidad***//
if (inpST.value >= inpC.value) {
//*** Actualizar Subtotal ***//
inpS.value=(inpC.value * inpP.value)-inpD.value;
document.getElementsByName("subtotal")[i].innerHTML = inpS.value;
calcularTotales();
alert("Exito: cant: "+inpC.value+" < stock: "+inpST.value);
}else{
alert("Error: cant: "+inpC.value+" > stock: "+inpST.value);
}
}
}
The problem basically is with the conditional that compares the stock
with the cantidad
that is being added, in theory if the cantidad
does not exceed stock
should run the alert("exito")
but this does not happen for example I leave a capture:
As seen in the capture, the system shows the alert("error")
, which does not have logic because of the aforementioned because in that particular case the value of the field cantidad
does not exceed stock
then the alert
correct should be the alert("exito")
, I'm coming to believe that the error is in the loop for
and not in the conditional, but already inspect the code to verify the indexes of the details that are added and not I see nothing strange.