Commit 2cddee92b6029abbae639fb0c13a73fff6af9c1c

Authored by Nicolás Guarnieri
Exists in master and in 2 other branches develop, lab

Merge branch 'master' into 'master'

Master

See merge request !13
src/js/controller.js
... ... @@ -33,6 +33,12 @@ angular.module('focaModalDetalleCisternas')
33 33 promesaRemito = focaModalDetalleCisternasService.obtenerRemitoById(idRemito);
34 34 }
35 35 Promise.all([promesaVehiculo, promesaCisternas, promesaRemito]).then(function(res) {
  36 + if (!validarCargas(res[1].data, res[2].data)) {
  37 + focaModalService.alert('Los artículos del remito exceden ' +
  38 + 'la cantidad disponible del vehiculo');
  39 + $uibModalInstance.close();
  40 + return;
  41 + }
36 42 $scope.cargandoDatos = false;
37 43 $scope.vehiculo = res[0].data;
38 44 $scope.cisternas = res[1].data;
... ... @@ -317,4 +323,46 @@ angular.module('focaModalDetalleCisternas')
317 323 $scope.selectVehiculo(transportista.COD, transportista.NOM);
318 324 });
319 325 };
  326 + function validarCargas(cis, remito) {
  327 + var result = true;
  328 + var cisternas = angular.copy(cis);
  329 + var articulos = angular.copy(remito.articulosRemito);
  330 +
  331 + cisternas.sort(ordenarCisternas);
  332 +
  333 + articulos.forEach(function(articulo) {
  334 + cisternas.forEach(function(cisterna) {
  335 + //SI LA CISTERNA ESTA VACIA O
  336 + //SI LA CISTERNA TIENE EL MISMO PRODUCTO
  337 + //Y AUN TIENE LUGAR
  338 + if(cisterna.capacidad === cisterna.disponible ||
  339 + (cisterna.cisternaCarga.idProducto === articulo.idArticulo &&
  340 + cisterna.disponible > 0)){
  341 + var restante = articulo.cantidad - cisterna.disponible;
  342 +
  343 + if (restante > 0) {
  344 + cisterna.disponible = 0;
  345 + articulo.cantidad = restante;
  346 + } else {
  347 + cisterna.disponible = restante * -1;
  348 + articulo.cantidad = 0;
  349 + }
  350 +
  351 + }
  352 + });
  353 + //SI AUN RESTA CANTIDAD EN EL ARTICULO
  354 + if (articulo.cantidad > 0) result = false;
  355 + });
  356 + return result;
  357 + }
  358 + function ordenarCisternas(a, b) {
  359 + //DEJA LAS CISTERNAS CON CARGA PRIMERO PARA VALIDAR LAS CARGAS CORRECTAMENTE
  360 + if (a.cisternaCarga && !b.cisternaCarga) {
  361 + return -1;
  362 + } else if (!a.cisternaCarga && b.cisternaCarga) {
  363 + return 1;
  364 + } else {
  365 + return 0;
  366 + }
  367 + }
320 368 }]);