diff --git a/src/js/controller.js b/src/js/controller.js index 52e4fa8..8ee1457 100644 --- a/src/js/controller.js +++ b/src/js/controller.js @@ -102,6 +102,7 @@ angular.module('focaAbmVehiculo') $scope.focused = 1; $scope.creando = false; $scope.transportistaStamp = ''; + $scope.cisternasABorrar = []; $scope.cisternas = []; $scope.cisterna = { codigo: '', @@ -205,7 +206,6 @@ angular.module('focaAbmVehiculo') }; $scope.guardar = function(key) { - key = (typeof key === 'undefined') ? 13 : key; if (key === 13) { @@ -231,9 +231,12 @@ angular.module('focaAbmVehiculo') //Valida si existe numero de unidad if (!validaTotalCargas() && !$scope.nuevo) { focaModalService.alert('La suma de las capacidades de las cisternas' + - ' debe ser igual a la capacidad total del vehículo'); + ' debe ser igual a la capacidad total del vehículo'); return; } + + borrarCisternas(); + validaCodigoUnidad().then(function() { delete $scope.vehiculo.transportista; delete $scope.vehiculo.cisternas; @@ -279,6 +282,17 @@ angular.module('focaAbmVehiculo') return false; } + function borrarCisternas() { + $scope.cisternasABorrar.forEach(cisternaABorrar => { + focaAbmVehiculoService.deleteCisterna(cisternaABorrar); + focaAbmVehiculoService + .getCisternas($routeParams.idVehiculo) + .then(function(res) { + $scope.cisternas = res; + }); + }); + } + function setearVehiculo(vehiculo) { $scope.vehiculo = vehiculo; $scope.$broadcast('addCabecera', { @@ -287,20 +301,15 @@ angular.module('focaAbmVehiculo') }); } - $scope.solicitarConfirmacionCisterna = function(cisterna, idx) { - focaModalService.confirm('¿Está seguro que desea borrar la cisterna ' + - cisterna.id + ' ' + cisterna.codigo + ' ?').then( - function(data) { - if (data) { - focaAbmVehiculoService.deleteCisterna(idx); - focaAbmVehiculoService - .getCisternas($routeParams.idVehiculo) - .then(function(res) { - $scope.cisternas = res; - }); - } + $scope.solicitarConfirmacionTabla = function(cisterna, idx){ + focaModalService.confirm('¿Está seguro que desea borrar la cisterna ' + cisterna.id + ' ' + cisterna.codigo + ' ?') + .then( function(data) { + if (data) { + $scope.cisternasABorrar.push(cisterna); + $scope.cisternas.splice(idx,1); } - ); + }); + return; }; function validaCodigoUnidad() { @@ -330,7 +339,7 @@ angular.module('focaAbmVehiculo') total += parseInt(cisterna.capacidad); } }); - return $scope.vehiculo.capacidad === total; + return $scope.vehiculo.capacidad >= total; } function guardarCisternas() { var cisternas = $scope.cisternas.map(function(cisterna) { @@ -347,7 +356,7 @@ angular.module('focaAbmVehiculo') return focaAbmVehiculoService.guardarCisternas(cisternas); } - $scope.guardarCisterna = function() { + $scope.agregarCisterna = function() { if (!$scope.cisterna.codigo) { focaModalService.alert('Ingrese codigo de cisterna'); return; @@ -374,22 +383,10 @@ angular.module('focaAbmVehiculo') function saveCisterna () { $scope.cisterna.idVehiculo = parseInt($routeParams.idVehiculo); delete $scope.cisterna.vehiculo; - var auxCisternas = $scope.cisternas; if (!$scope.cisterna.id) { - auxCisternas.push($scope.cisterna); + $scope.cisternas.push($scope.cisterna); + $scope.$apply(); } - guardarCisternas(auxCisternas) - .then(function() { - focaAbmVehiculoService - .getCisternas($routeParams.idVehiculo) - .then(function(res) { - $scope.cisternas = res; - $scope.cisterna = {}; - }); - }) - .catch(function (err) { - console.log('Err: ', err); - }); } if ($routeParams.idx !== -1) { diff --git a/src/js/service.js b/src/js/service.js index 6822b6b..24f540f 100644 --- a/src/js/service.js +++ b/src/js/service.js @@ -47,10 +47,10 @@ angular.module('focaAbmVehiculo') guardarCisternas: function(cisternas) { return $http.post(API_ENDPOINT.URL + '/cisterna', {cisternas: cisternas}); }, - deleteCisterna: function(idx) { - cisternas[idx].desactivado = true; + deleteCisterna: function(cisterna) { + cisterna.desactivado = true; cisternasPristine = false; - return $http.delete(API_ENDPOINT.URL + '/cisterna/' + cisternas[idx].id); + return $http.delete(API_ENDPOINT.URL + '/cisterna/' + cisterna.id); }, cleanCisternas: function() { cisternas = []; diff --git a/src/views/foca-abm-vehiculos-item.html b/src/views/foca-abm-vehiculos-item.html index 81edb6c..3dd498f 100644 --- a/src/views/foca-abm-vehiculos-item.html +++ b/src/views/foca-abm-vehiculos-item.html @@ -138,7 +138,7 @@ @@ -161,7 +161,7 @@