From 0f4ce9054f72dd9060a7565780dd3acf94a6b72a Mon Sep 17 00:00:00 2001 From: Jose Pinto Date: Thu, 24 Jan 2019 11:16:59 -0300 Subject: [PATCH] refactor funcionamiento cisternas locales --- src/js/controller.js | 67 ++++++++++++++++++---------------- src/js/controllerCisterna.js | 62 ++++++++++++++----------------- src/js/route.js | 11 +----- src/js/service.js | 41 +++++++++++---------- src/views/foca-abm-vehiculos-item.html | 9 +++-- 5 files changed, 90 insertions(+), 100 deletions(-) diff --git a/src/js/controller.js b/src/js/controller.js index 04b24c9..b3e39b9 100644 --- a/src/js/controller.js +++ b/src/js/controller.js @@ -21,7 +21,6 @@ angular.module('focaAbmVehiculo') if(focaAbmVehiculoService.transportistaSeleccionado.COD) { elegirTransportista(focaAbmVehiculoService.transportistaSeleccionado); } - focaAbmVehiculoService.cleanCisternasLocal(); $scope.editar = function(id) { $location.path('/vehiculo/' + id + '/' + $scope.idTransportista); }; @@ -122,7 +121,7 @@ angular.module('focaAbmVehiculo') focaAbmVehiculoService.getVehiculo($routeParams.idVehiculo).then(function(res) { if(res.data) { $scope.transportistaStamp = ('00000' + res.data.transportista.COD).slice(-5); - $scope.transportistaStamp += ' - ' + res.data.transportista.NOM + $scope.transportistaStamp += ' - ' + res.data.transportista.NOM; $scope.vehiculo = res.data; $scope.$broadcast('addCabecera', { @@ -133,35 +132,32 @@ angular.module('focaAbmVehiculo') label: 'Unidad:', valor: res.data.codigo }); + focaAbmVehiculoService + .getCisternas($routeParams.idVehiculo) + .then(function(res) { + $scope.cisternas = res; + $scope.$apply(); + }); } }); - focaAbmVehiculoService.getCisternadoPorVehiculo($routeParams.idVehiculo) - .then(function(res) { - //CONCATENA LAS CISTERNAS DEL SERVICIO Y LAS DE LA BASE DE DATOS - $scope.cisternas = focaAbmVehiculoService.getCisternasLocal().concat(res.data); - }); + $scope.next = function(key) { if (key === 13) $scope.focused++; }; $scope.cancelar = function() { $location.path('/vehiculo'); }; - $scope.editar = function(cisterna) { - if(cisterna) { - if(cisterna.idTemp != undefined) { - $location.path('/vehiculo/' + $routeParams.idVehiculo + - '/cisterna/0/' + cisterna.idTemp); - }else { - $location.path('/vehiculo/' + $routeParams.idVehiculo + - '/cisterna/' + cisterna.id); - } + $scope.editar = function(key) { + if(key) { + $location.path('/vehiculo/' + $routeParams.idVehiculo + + '/cisterna/' + key); }else { $location.path('/vehiculo/' + $routeParams.idVehiculo + '/cisterna/0/'); } }; $scope.guardar = function(key) { key = (typeof key === 'undefined') ? 13 : key; - if(key === 13){ + if(key === 13) { //Valida si existe numero de unidad if(!validaTotalCargas() && !$scope.nuevo) { focaModalService.alert('La suma de las capacidades de las cisternas' + @@ -182,21 +178,23 @@ angular.module('focaAbmVehiculo') }); } }); - }, function(){ + }, function() { focaModalService.alert('Código de unidad existente'); }); } }; - $scope.solicitarConfirmacionCisterna = function(cisterna) { + $scope.solicitarConfirmacionCisterna = function(cisterna, idx) { focaModalService.confirm('¿Está seguro que desea borrar la cisterna ' + cisterna.id + ' ' + cisterna.codigo + ' ?').then( function(data) { if(data) { - if(cisterna.id) { - focaAbmVehiculoService.deleteCisterna(cisterna.id); - } - $scope.cisternas.splice($scope.cisternas.indexOf(cisterna), 1); + focaAbmVehiculoService.deleteCisterna(idx); + focaAbmVehiculoService + .getCisternas($routeParams.idVehiculo) + .then(function(res) { + $scope.cisternas = res; + }); } } ); @@ -225,21 +223,26 @@ angular.module('focaAbmVehiculo') function validaTotalCargas() { var total = 0; $scope.cisternas.forEach(function(cisterna) { - total += parseInt(cisterna.capacidad); + if(!cisterna.desactivado) { + total += parseInt(cisterna.capacidad); + } }); return $scope.vehiculo.capacidad == total; } function guardarCisternas() { - var promesas = []; - - focaAbmVehiculoService.getCisternasLocal().forEach(function(cisterna) { - delete cisterna.unidadMedida; - delete cisterna.idTemp; - delete cisterna.cisternasCarga; - promesas.push(focaAbmVehiculoService.guardarCisterna(cisterna)); + var cisternas = $scope.cisternas.map(function(cisterna) { + return { + id: cisterna.id, + capacidad: parseInt(cisterna.capacidad), + codigo: cisterna.codigo, + idUnidadMedida: cisterna.idUnidadMedida, + idVehiculo: $routeParams.idVehiculo, + desactivado: cisterna.desactivado + }; }); - return Promise.all(promesas); + + return focaAbmVehiculoService.guardarCisternas(cisternas); } } ]); diff --git a/src/js/controllerCisterna.js b/src/js/controllerCisterna.js index 310fb1f..b37cbb0 100644 --- a/src/js/controllerCisterna.js +++ b/src/js/controllerCisterna.js @@ -24,14 +24,15 @@ angular.module('focaAbmVehiculo') focaBotoneraLateralService.showGuardar(true, $scope.guardar); }); - - if($routeParams.idTemp != undefined) { - $scope.cisterna = focaAbmVehiculoService.getCisternasLocal()[$routeParams.idTemp]; - }else { - focaAbmVehiculoService.getCisterna($routeParams.id).then(function(res) { - if(res.data) $scope.cisterna = res.data; - }); + if($routeParams.idx !== -1) { + $scope.cisterna = [$routeParams.idx] + focaAbmVehiculoService + .getCisternas($routeParams.idVehiculo) + .then(function(res) { + $scope.cisterna = res[$routeParams.idx]; + }); } + focaAbmVehiculoService.getVehiculo($routeParams.idVehiculo).then(function(res) { var codigo = ('00000' + res.data.transportista.COD).slice(-5); $scope.transportista = res.data.transportista.COD; @@ -57,27 +58,19 @@ angular.module('focaAbmVehiculo') focaModalService.alert('Ingrese unidad de medida'); return; } - validaCodigoCapacidad().then(function() { - $scope.cisterna.idVehiculo = parseInt($routeParams.idVehiculo); - delete $scope.cisterna.vehiculo; - - if($routeParams.idTemp != undefined) { - //SI SE EDITA UNA CISTERNA LOCALMENTE + validaCodigoCapacidad() + .then(function() { + $scope.cisterna.idVehiculo = parseInt($routeParams.idVehiculo); + delete $scope.cisterna.vehiculo; + focaAbmVehiculoService - .guardarCisternaLocal($scope.cisterna, $routeParams.idTemp); - }else if($scope.cisterna.id) { - //SI SE EDITA UNA CISTERNA PREVIAMENTE GUARDADA - focaAbmVehiculoService.deleteCisterna($scope.cisterna.id); - focaAbmVehiculoService.guardarCisternaLocal($scope.cisterna); - }else { - //SI SE EDITA CREA UNA NUEVA CISTERNA - focaAbmVehiculoService.guardarCisternaLocal($scope.cisterna); - } - $window.location.assign('/#!/vehiculo/' + $routeParams.idVehiculo + - '/' + $scope.transportista); - }, function(err) { - focaModalService.alert(err); - }); + .guardarCisterna($scope.cisterna, $routeParams.idx); + + $window.location.assign('/#!/vehiculo/' + $routeParams.idVehiculo + + '/' + $scope.transportista); + }, function(err) { + focaModalService.alert(err); + }); }; @@ -99,19 +92,20 @@ angular.module('focaAbmVehiculo') function validaCodigoCapacidad() { return new Promise(function(resolve, reject) { focaAbmVehiculoService - .getCisternadoPorVehiculo($routeParams.idVehiculo) + .getCisternas($routeParams.idVehiculo) .then(function(res) { - var cisternas = focaAbmVehiculoService.getCisternasLocal().concat(res.data); + var cisternas = res; var totalCargado = 0; - - cisternas.forEach(function(cisterna) { + console.log(cisternas); + cisternas.forEach(function(cisterna, idx) { //SI EL CODIGO YA EXISTE if(cisterna.codigo === $scope.cisterna.codigo && - (cisterna.id !== $scope.cisterna.id || - cisterna.idTemp !== $scope.cisterna.idTemp)) { + idx != $routeParams.idx && + !cisterna.desactivado) { reject('Código de cisterna existente'); } - if(cisterna.id !== $scope.cisterna.id) { + if(idx != $routeParams.idx && + !cisterna.desactivado) { totalCargado += cisterna.capacidad; } }); diff --git a/src/js/route.js b/src/js/route.js index f6eadb5..f3e63b3 100644 --- a/src/js/route.js +++ b/src/js/route.js @@ -29,16 +29,7 @@ angular.module('focaAbmVehiculo') .config([ '$routeProvider', function($routeProvider) { - $routeProvider.when('/vehiculo/:idVehiculo/cisterna/:id', { - controller: 'focaAbmVehiculoCisternaController', - templateUrl: 'src/views/foca-abm-cisterna-item.html' - }); - } - ]) - .config([ - '$routeProvider', - function($routeProvider) { - $routeProvider.when('/vehiculo/:idVehiculo/cisterna/:id/:idTemp', { + $routeProvider.when('/vehiculo/:idVehiculo/cisterna/:idx', { controller: 'focaAbmVehiculoCisternaController', templateUrl: 'src/views/foca-abm-cisterna-item.html' }); diff --git a/src/js/service.js b/src/js/service.js index ad4a956..82bf596 100644 --- a/src/js/service.js +++ b/src/js/service.js @@ -17,32 +17,33 @@ angular.module('focaAbmVehiculo') deleteVehiculo: function(id) { return $http.delete(API_ENDPOINT.URL + '/vehiculo/' + id); }, - getCisternadoPorVehiculo: function(idVehiculo) { - return $http.get(API_ENDPOINT.URL + '/cisterna/listar/' + idVehiculo); - }, - getCisterna: function(id) { - return $http.get(API_ENDPOINT.URL + '/cisterna/obtener/' + id); - }, - getCisternasLocal: function() { - return cisternas; + getCisternas: function(idVehiculo) { + if(cisternas.length) { + return Promise.resolve(angular.copy(cisternas)); + }else { + return new Promise(function(resolve, reject) { + $http.get(API_ENDPOINT.URL + '/cisterna/listar/' + idVehiculo) + .then(function(res) { + cisternas = res.data; + resolve(res.data); + }); + }); + } }, - guardarCisternaLocal: function(cisterna, idTemp) { - if(idTemp) { - cisterna.idTemp = idTemp; - cisternas[idTemp] = cisterna; + guardarCisterna: function(cisterna, idx) { + if(idx != -1) { + //update + cisternas[idx] = cisterna; }else { - cisterna.idTemp = cisternas.length; + //insert cisternas.push(cisterna); } }, - cleanCisternasLocal: function(){ - cisternas = []; - }, - guardarCisterna: function(cisterna) { - return $http.post(API_ENDPOINT.URL + '/cisterna/guardar', {cisterna: cisterna}); + guardarCisternas: function(cisternas) { + return $http.post(API_ENDPOINT.URL + '/cisterna', {cisternas: cisternas}); }, - deleteCisterna: function(id) { - return $http.delete(API_ENDPOINT.URL + '/cisterna/borrar/' + id); + deleteCisterna: function(idx) { + cisternas[idx].desactivado = true; }, getVehiculosPorTransportista: function(id) { return $http.get(API_ENDPOINT.URL + '/vehiculo/transportista/' + id); diff --git a/src/views/foca-abm-vehiculos-item.html b/src/views/foca-abm-vehiculos-item.html index 0462654..aad18f9 100644 --- a/src/views/foca-abm-vehiculos-item.html +++ b/src/views/foca-abm-vehiculos-item.html @@ -83,28 +83,29 @@ - + -- 1.9.1