Commit a2f13b7da0899474db182468c5b1ee1c8c5142ec
1 parent
78a26d797e
Exists in
master
and in
2 other branches
valida articulos remito y capacidad vehiculo
Showing
1 changed file
with
48 additions
and
0 deletions
Show diff stats
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 | }]); |