Commit 5cd82a974e7050ba41a64c2ec7ebd44fedd52a85

Authored by Jose Pinto
1 parent a1a52ea7dd
Exists in master and in 1 other branch develop

validacion capacidad cisternas igual a capacidad total vehiculo

src/js/controller.js
... ... @@ -20,7 +20,7 @@ angular.module('focaAbmVehiculo')
20 20 if(focaAbmVehiculoService.transportistaSeleccionado.COD) {
21 21 elegirTransportista(focaAbmVehiculoService.transportistaSeleccionado);
22 22 }
23   -
  23 + focaAbmVehiculoService.cleanCisternasLocal();
24 24 $scope.editar = function(id) {
25 25 $location.path('/vehiculo/' + id + '/' + $scope.idTransportista);
26 26 };
... ... @@ -83,18 +83,19 @@ angular.module('focaAbmVehiculo')
83 83 ])
84 84 .controller('focaAbmVehiculoController', [
85 85 '$scope', 'focaAbmVehiculoService', '$routeParams', '$location', '$uibModal',
86   - 'focaModalService', '$timeout', 'focaBotoneraLateralService',
  86 + 'focaModalService', '$timeout', 'focaBotoneraLateralService', '$window',
87 87 function($scope, focaAbmVehiculoService, $routeParams, $location, $uibModal,
88   - focaModalService, $timeout, focaBotoneraLateralService) {
  88 + focaModalService, $timeout, focaBotoneraLateralService, $window) {
89 89 $scope.nuevo = $routeParams.idVehiculo === '0' ? true : false;
90 90 $scope.now = new Date();
91 91 $scope.focused = 1;
92 92 $scope.transportistaStamp = '';
  93 + $scope.cisternas = [];
93 94  
94 95 $timeout(function() {
95   - focaBotoneraLateralService.showSalir(false);
  96 + focaBotoneraLateralService.showSalir(true);
96 97 focaBotoneraLateralService.showPausar(false);
97   - focaBotoneraLateralService.showCancelar(true, '/vehiculo');
  98 + focaBotoneraLateralService.showCancelar();
98 99 focaBotoneraLateralService.showGuardar(true, $scope.guardar);
99 100 });
100 101  
... ... @@ -130,7 +131,8 @@ angular.module('focaAbmVehiculo')
130 131 });
131 132 focaAbmVehiculoService.getCisternadoPorVehiculo($routeParams.idVehiculo)
132 133 .then(function(res) {
133   - $scope.cisternas = res.data;
  134 + //CONCATENA LAS CISTERNAS DEL SERVICIO Y LAS DE LA BASE DE DATOS
  135 + $scope.cisternas = focaAbmVehiculoService.getCisternasLocal().concat(res.data);
134 136 });
135 137 $scope.next = function(key) {
136 138 if (key === 13) $scope.focused++;
... ... @@ -138,21 +140,38 @@ angular.module('focaAbmVehiculo')
138 140 $scope.cancelar = function() {
139 141 $location.path('/vehiculo');
140 142 };
141   - $scope.editar = function(id) {
142   - $location.path('/vehiculo/' + $routeParams.idVehiculo + '/cisterna/' + id);
  143 + $scope.editar = function(cisterna) {
  144 + if(cisterna) {
  145 + if(cisterna.idTemp != undefined) {
  146 + $location.path('/vehiculo/' + $routeParams.idVehiculo +
  147 + '/cisterna/0/' + cisterna.idTemp);
  148 + }else {
  149 + $location.path('/vehiculo/' + $routeParams.idVehiculo +
  150 + '/cisterna/' + cisterna.id);
  151 + }
  152 + }else {
  153 + $location.path('/vehiculo/' + $routeParams.idVehiculo + '/cisterna/0/');
  154 + }
143 155 };
144 156 $scope.guardar = function() {
145 157 //Valida si existe numero de unidad
146   - existeCodigoUnidad().then(function() {
  158 + if(!validaTotalCargas()) {
  159 + focaModalService.alert('La suma de las capacidades de las cisternas' +
  160 + ' debe ser igual a la capacidad total del vehรญculo');
  161 + return;
  162 + }
  163 + validaCodigoUnidad().then(function() {
147 164 delete $scope.vehiculo.transportista;
148 165 delete $scope.vehiculo.cisternas;
149 166 focaAbmVehiculoService.guardarVehiculo($scope.vehiculo)
150   - .then(function(res){
  167 + .then(function(res) {
151 168 if($scope.nuevo) {
152 169 $location.path('/vehiculo/' + res.data.id +
153 170 '/' + res.data.idTransportista);
154   - } else {
155   - $location.path('/vehiculo');
  171 + }else {
  172 + guardarCisternas().then(function() {
  173 + $window.location.assign('/#!/vehiculo');
  174 + });
156 175 }
157 176 });
158 177 }, function(){
... ... @@ -165,54 +184,52 @@ angular.module('focaAbmVehiculo')
165 184 cisterna.id + ' ' + cisterna.codigo + ' ?').then(
166 185 function(data) {
167 186 if(data) {
168   - focaAbmVehiculoService.deleteCisterna(cisterna.id);
  187 + if(cisterna.id){
  188 + focaAbmVehiculoService.deleteCisterna(cisterna.id);
  189 + }
169 190 $scope.cisternas.splice($scope.cisternas.indexOf(cisterna), 1);
170 191 }
171 192 }
172 193 );
173 194 };
174   - $scope.seleccionarTransportista = function() {
175   - var modalInstance = $uibModal.open(
176   - {
177   - ariaLabelledBy: 'Busqueda de Transportista',
178   - templateUrl: 'modal-proveedor.html',
179   - controller: 'focaModalProveedorCtrl',
180   - size: 'lg',
181   - resolve: {
182   - transportista: function() {
183   - return true;
184   - }
185   - }
186   - }
187   - );
188   - modalInstance.result.then(
189   - function(transportista) {
190   - $scope.vehiculo.idTransportista = transportista.COD;
191   - $scope.vehiculo.transportista = transportista;
192   - }, function() {
193   -
194   - }
195   - );
196   - };
197 195  
198   - function existeCodigoUnidad(){
  196 + function validaCodigoUnidad() {
199 197 return new Promise(function(resolve, reject) {
200 198 focaAbmVehiculoService
201   - .getVehiculosPorTransportista($routeParams.idTransportista)
202   - .then(function(res){
  199 + .getVehiculosPorTransportista(parseInt($routeParams.idTransportista))
  200 + .then(function(res) {
203 201 //Valida si existe numero de unidad
204 202 var existe = res.data.filter(function(vehiculo) {
205 203 return vehiculo.codigo === $scope.vehiculo.codigo &&
206 204 vehiculo.id !== $scope.vehiculo.id;
207 205 });
208 206  
209   - if(existe.length){
  207 + if(existe.length) {
210 208 reject(existe);
211   - } else {
  209 + }else {
212 210 resolve();
213 211 }
214 212 });
215 213 });
216 214 }
  215 +
  216 + function validaTotalCargas() {
  217 + var total = 0;
  218 + $scope.cisternas.forEach(function(cisterna) {
  219 + total += parseInt(cisterna.capacidad);
  220 + });
  221 + return $scope.vehiculo.capacidad == total;
  222 + }
  223 +
  224 + function guardarCisternas() {
  225 + var promesas = [];
  226 +
  227 + focaAbmVehiculoService.getCisternasLocal().forEach(function(cisterna) {
  228 + delete cisterna.unidadMedida;
  229 + delete cisterna.idTemp;
  230 + promesas.push(focaAbmVehiculoService.guardarCisterna(cisterna));
  231 + });
  232 + return Promise.all(promesas);
  233 + }
217 234 }
218 235 ]);
src/js/controllerCisterna.js
1 1 angular.module('focaAbmVehiculo')
2 2 .controller('focaAbmVehiculoCisternaController', [
3 3 '$scope', 'focaAbmVehiculoService', '$routeParams', '$location', '$uibModal',
4   - 'focaModalService', 'focaBotoneraLateralService', '$timeout',
  4 + 'focaModalService', 'focaBotoneraLateralService', '$timeout', '$window',
5 5 function($scope, focaAbmVehiculoService, $routeParams, $location, $uibModal,
6   - focaModalService, focaBotoneraLateralService, $timeout) {
  6 + focaModalService, focaBotoneraLateralService, $timeout, $window) {
7 7 $scope.editar = false;
8 8 $scope.now = new Date();
9 9 $scope.cisterna = {
... ... @@ -17,6 +17,7 @@ angular.module('focaAbmVehiculo')
17 17 if (key === 13) $scope.focused++;
18 18 };
19 19 $scope.capacidadVechiulo = 0;
  20 + $scope.transportista = '';
20 21  
21 22 //SETEO BOTONERA LATERAL
22 23 $timeout(function() {
... ... @@ -26,16 +27,17 @@ angular.module('focaAbmVehiculo')
26 27 focaBotoneraLateralService.showGuardar(true, $scope.guardar);
27 28 });
28 29  
29   - focaAbmVehiculoService.getCisterna($routeParams.id).then(function(res) {
30   - if(res.data) {
31   - $scope.cisterna = res.data;
32   - $scope.editar = true;
33   - }else {
34   - $scope.editar = false;
35   - }
36   - });
  30 +
  31 + if($routeParams.idTemp != undefined) {
  32 + $scope.cisterna = focaAbmVehiculoService.getCisternasLocal()[$routeParams.idTemp];
  33 + }else {
  34 + focaAbmVehiculoService.getCisterna($routeParams.id).then(function(res) {
  35 + if(res.data) $scope.cisterna = res.data;
  36 + });
  37 + }
37 38 focaAbmVehiculoService.getVehiculo($routeParams.idVehiculo).then(function(res) {
38 39 var codigo = ('00000' + res.data.transportista.COD).slice(-5);
  40 + $scope.transportista = res.data.transportista.COD;
39 41 $scope.capacidadVechiulo = res.data.capacidad;
40 42 $scope.$broadcast('addCabecera', {
41 43 label: 'Transportista:',
... ... @@ -45,6 +47,10 @@ angular.module('focaAbmVehiculo')
45 47 label: 'Unidad:',
46 48 valor: res.data.codigo
47 49 });
  50 + $scope.$broadcast('addCabecera', {
  51 + label: 'Capacidad total vehรญculo:',
  52 + valor: res.data.capacidad
  53 + });
48 54 });
49 55 $scope.cancelar = function() {
50 56 $location.path('/vehiculo/' + $routeParams.idVehiculo);
... ... @@ -55,13 +61,24 @@ angular.module('focaAbmVehiculo')
55 61 return;
56 62 }
57 63 validaCodigoCapacidad().then(function() {
58   - $scope.cisterna.idVehiculo = $routeParams.idVehiculo;
  64 + $scope.cisterna.idVehiculo = parseInt($routeParams.idVehiculo);
59 65 delete $scope.cisterna.vehiculo;
60   - delete $scope.cisterna.unidadMedida;
61 66 delete $scope.cisterna.cisternaCarga.articulo;
62   - focaAbmVehiculoService.guardarCisterna($scope.cisterna).then(function(){
63   - $location.path('/vehiculo/' + $routeParams.idVehiculo);
64   - });
  67 +
  68 + if($routeParams.idTemp != undefined) {
  69 + //SI SE EDITA UNA CISTERNA LOCALMENTE
  70 + focaAbmVehiculoService
  71 + .guardarCisternaLocal($scope.cisterna, $routeParams.idTemp);
  72 + }else if($scope.cisterna.id) {
  73 + //SI SE EDITA UNA CISTERNA PREVIAMENTE GUARDADA
  74 + focaAbmVehiculoService.deleteCisterna($scope.cisterna.id);
  75 + focaAbmVehiculoService.guardarCisternaLocal($scope.cisterna);
  76 + }else {
  77 + //SI SE EDITA CREA UNA NUEVA CISTERNA
  78 + focaAbmVehiculoService.guardarCisternaLocal($scope.cisterna);
  79 + }
  80 + $window.location.assign('/#!/vehiculo/' + $routeParams.idVehiculo +
  81 + '/' + $scope.transportista);
65 82 }, function(err) {
66 83 focaModalService.alert(err);
67 84 });
... ... @@ -88,13 +105,14 @@ angular.module('focaAbmVehiculo')
88 105 focaAbmVehiculoService
89 106 .getCisternadoPorVehiculo($routeParams.idVehiculo)
90 107 .then(function(res) {
91   -
  108 + var cisternas = focaAbmVehiculoService.getCisternasLocal().concat(res.data);
92 109 var totalCargado = 0;
93 110  
94   - res.data.forEach(function(cisterna) {
  111 + cisternas.forEach(function(cisterna) {
95 112 //SI EL CODIGO YA EXISTE
96 113 if(cisterna.codigo === $scope.cisterna.codigo &&
97   - cisterna.id !== $scope.cisterna.id) {
  114 + (cisterna.id !== $scope.cisterna.id ||
  115 + cisterna.idTemp !== $scope.cisterna.idTemp)) {
98 116 reject('Cรณdigo de cisterna existente');
99 117 }
100 118 if(cisterna.id !== $scope.cisterna.id) {
... ... @@ -106,7 +124,7 @@ angular.module('focaAbmVehiculo')
106 124 totalCargado = totalCargado + parseInt($scope.cisterna.capacidad);
107 125 if(totalCargado > $scope.capacidadVechiulo) {
108 126 reject('La capacidad total de las cisternas' +
109   - ' debe ser menor o igual a la del vehiculo');
  127 + ' no debe ser mayor a la del vehiculo');
110 128 }else {
111 129 resolve();
112 130 }
... ... @@ -34,4 +34,13 @@ angular.module('focaAbmVehiculo')
34 34 templateUrl: 'src/views/foca-abm-cisterna-item.html'
35 35 });
36 36 }
  37 + ])
  38 + .config([
  39 + '$routeProvider',
  40 + function($routeProvider) {
  41 + $routeProvider.when('/vehiculo/:idVehiculo/cisterna/:id/:idTemp', {
  42 + controller: 'focaAbmVehiculoCisternaController',
  43 + templateUrl: 'src/views/foca-abm-cisterna-item.html'
  44 + });
  45 + }
37 46 ]);
1 1 angular.module('focaAbmVehiculo')
2 2 .factory('focaAbmVehiculoService', ['$http', 'API_ENDPOINT', function($http, API_ENDPOINT) {
  3 + var cisternas = [];
3 4 return {
4 5 getVehiculos: function() {
5 6 return $http.get(API_ENDPOINT.URL + '/vehiculo');
... ... @@ -22,6 +23,21 @@ angular.module('focaAbmVehiculo')
22 23 getCisterna: function(id) {
23 24 return $http.get(API_ENDPOINT.URL + '/cisterna/obtener/' + id);
24 25 },
  26 + getCisternasLocal: function() {
  27 + return cisternas;
  28 + },
  29 + guardarCisternaLocal: function(cisterna, idTemp) {
  30 + if(idTemp){
  31 + cisterna.idTemp = idTemp;
  32 + cisternas[idTemp] = cisterna;
  33 + }else {
  34 + cisterna.idTemp = cisternas.length;
  35 + cisternas.push(cisterna);
  36 + }
  37 + },
  38 + cleanCisternasLocal: function(){
  39 + cisternas = [];
  40 + },
25 41 guardarCisterna: function(cisterna) {
26 42 return $http.post(API_ENDPOINT.URL + '/cisterna/guardar', {cisterna: cisterna});
27 43 },
src/views/foca-abm-vehiculos-item.html
... ... @@ -93,7 +93,7 @@
93 93 <button
94 94 class="btn btn-outline-debo boton-accion"
95 95 title="Agregar"
96   - ng-click="editar(0)">
  96 + ng-click="editar()">
97 97 <i class="fa fa-plus"></i>
98 98 </button>
99 99 </th>
... ... @@ -107,7 +107,7 @@
107 107 <button
108 108 class="btn btn-outline-dark boton-accion"
109 109 title="Editar"
110   - ng-click="editar(cisterna.id)"
  110 + ng-click="editar(cisterna)"
111 111 >
112 112 <i class="fa fa-pencil"></i>
113 113 </button>
src/views/foca-abm-vehiculos-listado.html
... ... @@ -44,7 +44,7 @@
44 44 <td ng-bind="vehiculo.codigo" class="text-center"></td>
45 45 <td ng-bind="vehiculo.tractor"></td>
46 46 <td ng-bind="vehiculo.semi"></td>
47   - <td ng-bind="vehiculo.capacidadTotalCisternas" class="text-right"></td>
  47 + <td ng-bind="vehiculo.capacidad" class="text-right"></td>
48 48 <td class="text-center">
49 49 <button
50 50 class="btn btn-outline-dark boton-accion"