diff --git a/src/js/controller.js b/src/js/controller.js index 99ce1bd..909620d 100644 --- a/src/js/controller.js +++ b/src/js/controller.js @@ -101,15 +101,9 @@ angular.module('focaAbmVehiculo') $scope.now = new Date(); $scope.focused = 1; $scope.creando = false; + $scope.crear = false; $scope.transportistaStamp = ''; - $scope.cisternasABorrar = []; $scope.cisternas = []; - $scope.cisterna = { - codigo: '', - capacidad: '', - idUnidadMedida: 0, - unidadMedida: {} - }; $timeout(function() { focaBotoneraLateralService.showSalir(false); @@ -180,17 +174,41 @@ angular.module('focaAbmVehiculo') } }; - $scope.editar = function(key, cisterna) { - if (key === 'nuevo') { - $scope.cisterna = {}; - $scope.creando = true; - } else { - $scope.cisterna = cisterna; - $scope.creando = true; + $scope.editar = function(cisterna) { + $scope.cisternas.forEach(function(cisterna) { + cisterna.editando = false; + $scope.crear = true; + }); + cisterna.editando = true; + $scope.inicial = angular.copy(cisterna); + }; + + $scope.volver = function(cisterna, key) { + if (cisterna.id === undefined || !$scope.crear) { + $scope.cisternas.shift(cisterna); + $scope.crear = false; + return; + } + if (cisterna.id !== undefined) { + $scope.cisternas[key] = $scope.inicial; + $scope.cisternas[key].editando = false; } + $scope.crear = false; }; - $scope.seleccionarUnidadMedida = function() { + $scope.crearCisterna = function() { + var cisterna = { + codigo: '', + capacidad: '', + idUnidadMedida: 0, + unidadMedida: {}, + editando: true, + }; + $scope.cisternas.unshift(cisterna); + $scope.crear = true; + }; + + $scope.seleccionarUnidadMedida = function(cisterna) { var modalInstance = $uibModal.open( { ariaLabelledBy: 'Busqueda de Unidades de medida', @@ -200,8 +218,8 @@ angular.module('focaAbmVehiculo') } ); modalInstance.result.then(function(unidaMedida) { - $scope.cisterna.idUnidadMedida = unidaMedida.ID; - $scope.cisterna.unidadMedida = unidaMedida; + cisterna.idUnidadMedida = unidaMedida.ID; + cisterna.unidadMedida = unidaMedida; }); }; @@ -214,6 +232,9 @@ angular.module('focaAbmVehiculo') $scope.salir(); return; } + $scope.cisternas.forEach( function(cisterna) { + if (cisterna.id === 0) cisterna.id = undefined; + }); if (!$scope.vehiculo.codigo) { focaModalService.alert('Ingrese unidad'); @@ -235,8 +256,6 @@ angular.module('focaAbmVehiculo') return; } - borrarCisternas(); - validaCodigoUnidad().then(function() { delete $scope.vehiculo.transportista; delete $scope.vehiculo.cisternas; @@ -246,7 +265,7 @@ angular.module('focaAbmVehiculo') $location.path('/vehiculo/' + res.data.id + '/' + res.data.idTransportista); } else { - guardarCisternas().then(function() { + guardarCisternas($scope.cisternas).then(function() { $location.path('/vehiculo'); }); } @@ -256,7 +275,6 @@ angular.module('focaAbmVehiculo') }); } }; - //Agregar propiedades de cisterna $scope.$watch('vehiculo', function(newValue) { focaBotoneraLateralService.setPausarData({ @@ -282,17 +300,6 @@ 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', { @@ -301,12 +308,11 @@ angular.module('focaAbmVehiculo') }); } - $scope.solicitarConfirmacionTabla = function(cisterna, idx){ + $scope.solicitarConfirmacionTabla = function(cisterna) { 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); + cisterna.desactivado = true; } }); return; @@ -352,41 +358,28 @@ angular.module('focaAbmVehiculo') desactivado: cisterna.desactivado }; }); - + return focaAbmVehiculoService.guardarCisternas(cisternas); } - $scope.agregarCisterna = function() { - if (!$scope.cisterna.codigo) { + $scope.agregarCisterna = function(cisterna) { + if (!cisterna) { + focaModalService.alert('Ingrese valores'); + return; + } else if (!cisterna.codigo) { focaModalService.alert('Ingrese codigo de cisterna'); return; - } else if (!$scope.cisterna.capacidad) { + } else if (!cisterna.capacidad) { focaModalService.alert('Ingrese capacidad'); return; - } else if (!$scope.cisterna.idUnidadMedida) { + } else if (!cisterna.idUnidadMedida) { focaModalService.alert('Ingrese unidad de medida'); return; } - $scope.creando = false; - if (!$scope.cisterna.id) { - validaCodigo() - .then(function() { - saveCisterna(); - }, function(err) { - focaModalService.alert(err); - }); - } else { - saveCisterna(); - } - }; - function saveCisterna () { - $scope.cisterna.idVehiculo = parseInt($routeParams.idVehiculo); - delete $scope.cisterna.vehiculo; - if (!$scope.cisterna.id) { - $timeout(function() { $scope.cisternas.push($scope.cisterna);}); - } - } + validaCodigo(cisterna); + cisterna.id = 0; + }; if ($routeParams.idx !== -1) { $scope.cisterna = [$routeParams.idx]; @@ -397,28 +390,32 @@ angular.module('focaAbmVehiculo') }); } - function validaCodigo() { - return new Promise(function(resolve, reject) { - focaAbmVehiculoService - .getCisternas($routeParams.idVehiculo) - .then(function(res) { - var cisternas = res; - var totalCargado = 0; - cisternas.forEach(function(cisterna, idx) { - //SI EL CODIGO YA EXISTE - if (cisterna.codigo === $scope.cisterna.codigo && - idx !== $routeParams.idx && - !cisterna.desactivado) { - reject('Código de cisterna existente'); - } - if (idx !== $routeParams.idx && - !cisterna.desactivado) { - totalCargado += cisterna.capacidad; + function validaCodigo(cisterna) { + focaAbmVehiculoService + .getCisternas($routeParams.idVehiculo) + .then(function(res) { + var cisternas = res; + var totalCargado = 0; + cisternas.forEach(function(cisternaBase, idx) { + //SI EL CODIGO YA EXISTE + if (cisternaBase.codigo === cisterna.codigo && + idx !== $routeParams.idx && + cisternaBase.desactivado === false && + cisternaBase.id !== cisterna.id + ) + { + focaModalService.alert('Código de cisterna existente'); + } + if (idx !== $routeParams.idx && + !cisternaBase.desactivado) { + totalCargado += cisternaBase.capacidad; } }); - resolve(); + $timeout( function(){ + cisterna.editando = false; + $scope.crear = false; + }); }); - }); } } ]); diff --git a/src/js/service.js b/src/js/service.js index 24f540f..12397c7 100644 --- a/src/js/service.js +++ b/src/js/service.js @@ -19,10 +19,6 @@ angular.module('focaAbmVehiculo') return $http.delete(API_ENDPOINT.URL + '/vehiculo/' + id); }, getCisternas: function(idVehiculo) { - if (cisternas.length) { - cisternasPristine = true; - return Promise.resolve(angular.copy(cisternas)); - } else { cisternasPristine = true; return new Promise(function(resolve) { $http.get(API_ENDPOINT.URL + '/cisterna/listar/' + idVehiculo) @@ -31,7 +27,6 @@ angular.module('focaAbmVehiculo') resolve(res.data); }); }); - } }, guardarCisterna: function(cisterna, idx) { if (idx !== '-1') { diff --git a/src/views/foca-abm-vehiculos-item.html b/src/views/foca-abm-vehiculos-item.html index 3dd498f..40df38e 100644 --- a/src/views/foca-abm-vehiculos-item.html +++ b/src/views/foca-abm-vehiculos-item.html @@ -74,7 +74,7 @@
Cisternas
- +
@@ -84,11 +84,12 @@ - + @@ -145,28 +146,90 @@ - - - - - + + + + + + + + + + + +
Código
+
+ + + + +
+ +
+ +
+
+
+ + +