diff --git a/src/js/controller.js b/src/js/controller.js index 3dfe254..b0fb006 100644 --- a/src/js/controller.js +++ b/src/js/controller.js @@ -33,6 +33,12 @@ angular.module('focaModalDetalleCisternas') promesaRemito = focaModalDetalleCisternasService.obtenerRemitoById(idRemito); } Promise.all([promesaVehiculo, promesaCisternas, promesaRemito]).then(function(res) { + if(!validarCargas(res[1].data, res[2].data)){ + focaModalService.alert('Los artículos del remito exceden ' + + 'la cantidad disponible del vehiculo'); + $uibModalInstance.close(); + return; + } $scope.cargandoDatos = false; $scope.vehiculo = res[0].data; $scope.cisternas = res[1].data; @@ -317,4 +323,46 @@ angular.module('focaModalDetalleCisternas') $scope.selectVehiculo(transportista.COD, transportista.NOM); }); }; + function validarCargas(cis, remito) { + var result = true; + var cisternas = angular.copy(cis); + var articulos = angular.copy(remito.articulosRemito); + + cisternas.sort(ordenarCisternas); + + articulos.forEach(function(articulo) { + cisternas.forEach(function(cisterna) { + //SI LA CISTERNA ESTA VACIA O + //SI LA CISTERNA TIENE EL MISMO PRODUCTO + //Y AUN TIENE LUGAR + if(cisterna.capacidad === cisterna.disponible || + (cisterna.cisternaCarga.idProducto === articulo.idArticulo && + cisterna.disponible > 0)){ + var restante = articulo.cantidad - cisterna.disponible; + + if (restante > 0) { + cisterna.disponible = 0; + articulo.cantidad = restante; + } else { + cisterna.disponible = restante * -1; + articulo.cantidad = 0; + } + + } + }); + //SI AUN RESTA CANTIDAD EN EL ARTICULO + if (articulo.cantidad > 0) result = false; + }); + return result; + } + function ordenarCisternas(a, b){ + //DEJA LAS CISTERNAS CON CARGA PRIMERO PARA VALIDAR LAS CARGAS CORRECTAMENTE + if (a.cisternaCarga && !b.cisternaCarga) { + return -1; + } else if (!a.cisternaCarga && b.cisternaCarga) { + return 1; + } else { + return 0; + } + } }]);