diff --git a/src/js/controller.js b/src/js/controller.js index 78cf826..bfd0cd1 100644 --- a/src/js/controller.js +++ b/src/js/controller.js @@ -20,7 +20,7 @@ angular.module('focaAbmVehiculo') if(focaAbmVehiculoService.transportistaSeleccionado.COD) { elegirTransportista(focaAbmVehiculoService.transportistaSeleccionado); } - + focaAbmVehiculoService.cleanCisternasLocal(); $scope.editar = function(id) { $location.path('/vehiculo/' + id + '/' + $scope.idTransportista); }; @@ -83,18 +83,19 @@ angular.module('focaAbmVehiculo') ]) .controller('focaAbmVehiculoController', [ '$scope', 'focaAbmVehiculoService', '$routeParams', '$location', '$uibModal', - 'focaModalService', '$timeout', 'focaBotoneraLateralService', + 'focaModalService', '$timeout', 'focaBotoneraLateralService', '$window', function($scope, focaAbmVehiculoService, $routeParams, $location, $uibModal, - focaModalService, $timeout, focaBotoneraLateralService) { + focaModalService, $timeout, focaBotoneraLateralService, $window) { $scope.nuevo = $routeParams.idVehiculo === '0' ? true : false; $scope.now = new Date(); $scope.focused = 1; $scope.transportistaStamp = ''; + $scope.cisternas = []; $timeout(function() { - focaBotoneraLateralService.showSalir(false); + focaBotoneraLateralService.showSalir(true); focaBotoneraLateralService.showPausar(false); - focaBotoneraLateralService.showCancelar(true, '/vehiculo'); + focaBotoneraLateralService.showCancelar(); focaBotoneraLateralService.showGuardar(true, $scope.guardar); }); @@ -130,7 +131,8 @@ angular.module('focaAbmVehiculo') }); focaAbmVehiculoService.getCisternadoPorVehiculo($routeParams.idVehiculo) .then(function(res) { - $scope.cisternas = res.data; + //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++; @@ -138,21 +140,38 @@ angular.module('focaAbmVehiculo') $scope.cancelar = function() { $location.path('/vehiculo'); }; - $scope.editar = function(id) { - $location.path('/vehiculo/' + $routeParams.idVehiculo + '/cisterna/' + id); + $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); + } + }else { + $location.path('/vehiculo/' + $routeParams.idVehiculo + '/cisterna/0/'); + } }; $scope.guardar = function() { //Valida si existe numero de unidad - existeCodigoUnidad().then(function() { + if(!validaTotalCargas()) { + focaModalService.alert('La suma de las capacidades de las cisternas' + + ' debe ser igual a la capacidad total del vehículo'); + return; + } + validaCodigoUnidad().then(function() { delete $scope.vehiculo.transportista; delete $scope.vehiculo.cisternas; focaAbmVehiculoService.guardarVehiculo($scope.vehiculo) - .then(function(res){ + .then(function(res) { if($scope.nuevo) { $location.path('/vehiculo/' + res.data.id + '/' + res.data.idTransportista); - } else { - $location.path('/vehiculo'); + }else { + guardarCisternas().then(function() { + $window.location.assign('/#!/vehiculo'); + }); } }); }, function(){ @@ -165,54 +184,52 @@ angular.module('focaAbmVehiculo') cisterna.id + ' ' + cisterna.codigo + ' ?').then( function(data) { if(data) { - focaAbmVehiculoService.deleteCisterna(cisterna.id); + if(cisterna.id){ + focaAbmVehiculoService.deleteCisterna(cisterna.id); + } $scope.cisternas.splice($scope.cisternas.indexOf(cisterna), 1); } } ); }; - $scope.seleccionarTransportista = function() { - var modalInstance = $uibModal.open( - { - ariaLabelledBy: 'Busqueda de Transportista', - templateUrl: 'modal-proveedor.html', - controller: 'focaModalProveedorCtrl', - size: 'lg', - resolve: { - transportista: function() { - return true; - } - } - } - ); - modalInstance.result.then( - function(transportista) { - $scope.vehiculo.idTransportista = transportista.COD; - $scope.vehiculo.transportista = transportista; - }, function() { - - } - ); - }; - function existeCodigoUnidad(){ + function validaCodigoUnidad() { return new Promise(function(resolve, reject) { focaAbmVehiculoService - .getVehiculosPorTransportista($routeParams.idTransportista) - .then(function(res){ + .getVehiculosPorTransportista(parseInt($routeParams.idTransportista)) + .then(function(res) { //Valida si existe numero de unidad var existe = res.data.filter(function(vehiculo) { return vehiculo.codigo === $scope.vehiculo.codigo && vehiculo.id !== $scope.vehiculo.id; }); - if(existe.length){ + if(existe.length) { reject(existe); - } else { + }else { resolve(); } }); }); } + + function validaTotalCargas() { + var total = 0; + $scope.cisternas.forEach(function(cisterna) { + total += parseInt(cisterna.capacidad); + }); + return $scope.vehiculo.capacidad == total; + } + + function guardarCisternas() { + var promesas = []; + + focaAbmVehiculoService.getCisternasLocal().forEach(function(cisterna) { + delete cisterna.unidadMedida; + delete cisterna.idTemp; + promesas.push(focaAbmVehiculoService.guardarCisterna(cisterna)); + }); + return Promise.all(promesas); + } } ]); diff --git a/src/js/controllerCisterna.js b/src/js/controllerCisterna.js index 4f5150b..d82490d 100644 --- a/src/js/controllerCisterna.js +++ b/src/js/controllerCisterna.js @@ -1,9 +1,9 @@ angular.module('focaAbmVehiculo') .controller('focaAbmVehiculoCisternaController', [ '$scope', 'focaAbmVehiculoService', '$routeParams', '$location', '$uibModal', - 'focaModalService', 'focaBotoneraLateralService', '$timeout', + 'focaModalService', 'focaBotoneraLateralService', '$timeout', '$window', function($scope, focaAbmVehiculoService, $routeParams, $location, $uibModal, - focaModalService, focaBotoneraLateralService, $timeout) { + focaModalService, focaBotoneraLateralService, $timeout, $window) { $scope.editar = false; $scope.now = new Date(); $scope.cisterna = { @@ -17,6 +17,7 @@ angular.module('focaAbmVehiculo') if (key === 13) $scope.focused++; }; $scope.capacidadVechiulo = 0; + $scope.transportista = ''; //SETEO BOTONERA LATERAL $timeout(function() { @@ -26,16 +27,17 @@ angular.module('focaAbmVehiculo') focaBotoneraLateralService.showGuardar(true, $scope.guardar); }); - focaAbmVehiculoService.getCisterna($routeParams.id).then(function(res) { - if(res.data) { - $scope.cisterna = res.data; - $scope.editar = true; - }else { - $scope.editar = false; - } - }); + + 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; + }); + } focaAbmVehiculoService.getVehiculo($routeParams.idVehiculo).then(function(res) { var codigo = ('00000' + res.data.transportista.COD).slice(-5); + $scope.transportista = res.data.transportista.COD; $scope.capacidadVechiulo = res.data.capacidad; $scope.$broadcast('addCabecera', { label: 'Transportista:', @@ -45,6 +47,10 @@ angular.module('focaAbmVehiculo') label: 'Unidad:', valor: res.data.codigo }); + $scope.$broadcast('addCabecera', { + label: 'Capacidad total vehículo:', + valor: res.data.capacidad + }); }); $scope.cancelar = function() { $location.path('/vehiculo/' + $routeParams.idVehiculo); @@ -55,13 +61,24 @@ angular.module('focaAbmVehiculo') return; } validaCodigoCapacidad().then(function() { - $scope.cisterna.idVehiculo = $routeParams.idVehiculo; + $scope.cisterna.idVehiculo = parseInt($routeParams.idVehiculo); delete $scope.cisterna.vehiculo; - delete $scope.cisterna.unidadMedida; delete $scope.cisterna.cisternaCarga.articulo; - focaAbmVehiculoService.guardarCisterna($scope.cisterna).then(function(){ - $location.path('/vehiculo/' + $routeParams.idVehiculo); - }); + + if($routeParams.idTemp != undefined) { + //SI SE EDITA UNA CISTERNA LOCALMENTE + 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); }); @@ -88,13 +105,14 @@ angular.module('focaAbmVehiculo') focaAbmVehiculoService .getCisternadoPorVehiculo($routeParams.idVehiculo) .then(function(res) { - + var cisternas = focaAbmVehiculoService.getCisternasLocal().concat(res.data); var totalCargado = 0; - res.data.forEach(function(cisterna) { + cisternas.forEach(function(cisterna) { //SI EL CODIGO YA EXISTE if(cisterna.codigo === $scope.cisterna.codigo && - cisterna.id !== $scope.cisterna.id) { + (cisterna.id !== $scope.cisterna.id || + cisterna.idTemp !== $scope.cisterna.idTemp)) { reject('Código de cisterna existente'); } if(cisterna.id !== $scope.cisterna.id) { @@ -106,7 +124,7 @@ angular.module('focaAbmVehiculo') totalCargado = totalCargado + parseInt($scope.cisterna.capacidad); if(totalCargado > $scope.capacidadVechiulo) { reject('La capacidad total de las cisternas' + - ' debe ser menor o igual a la del vehiculo'); + ' no debe ser mayor a la del vehiculo'); }else { resolve(); } diff --git a/src/js/route.js b/src/js/route.js index 45bec14..f6eadb5 100644 --- a/src/js/route.js +++ b/src/js/route.js @@ -34,4 +34,13 @@ angular.module('focaAbmVehiculo') templateUrl: 'src/views/foca-abm-cisterna-item.html' }); } + ]) + .config([ + '$routeProvider', + function($routeProvider) { + $routeProvider.when('/vehiculo/:idVehiculo/cisterna/:id/:idTemp', { + controller: 'focaAbmVehiculoCisternaController', + templateUrl: 'src/views/foca-abm-cisterna-item.html' + }); + } ]); diff --git a/src/js/service.js b/src/js/service.js index 8df56f6..49e7760 100644 --- a/src/js/service.js +++ b/src/js/service.js @@ -1,5 +1,6 @@ angular.module('focaAbmVehiculo') .factory('focaAbmVehiculoService', ['$http', 'API_ENDPOINT', function($http, API_ENDPOINT) { + var cisternas = []; return { getVehiculos: function() { return $http.get(API_ENDPOINT.URL + '/vehiculo'); @@ -22,6 +23,21 @@ angular.module('focaAbmVehiculo') getCisterna: function(id) { return $http.get(API_ENDPOINT.URL + '/cisterna/obtener/' + id); }, + getCisternasLocal: function() { + return cisternas; + }, + guardarCisternaLocal: function(cisterna, idTemp) { + if(idTemp){ + cisterna.idTemp = idTemp; + cisternas[idTemp] = cisterna; + }else { + cisterna.idTemp = cisternas.length; + cisternas.push(cisterna); + } + }, + cleanCisternasLocal: function(){ + cisternas = []; + }, guardarCisterna: function(cisterna) { return $http.post(API_ENDPOINT.URL + '/cisterna/guardar', {cisterna: cisterna}); }, diff --git a/src/views/foca-abm-vehiculos-item.html b/src/views/foca-abm-vehiculos-item.html index c8df567..47ca4ba 100644 --- a/src/views/foca-abm-vehiculos-item.html +++ b/src/views/foca-abm-vehiculos-item.html @@ -93,7 +93,7 @@ @@ -107,7 +107,7 @@ diff --git a/src/views/foca-abm-vehiculos-listado.html b/src/views/foca-abm-vehiculos-listado.html index e689bd8..4d62f15 100644 --- a/src/views/foca-abm-vehiculos-listado.html +++ b/src/views/foca-abm-vehiculos-listado.html @@ -44,7 +44,7 @@ - +