Commit 2f621a2c3dc48f0d3e20f208e55487a26a5f189b

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

ok validaciones vehiculo y cisterna

src/js/controller.js
1 angular.module('focaAbmVehiculo') 1 angular.module('focaAbmVehiculo')
2 .controller('focaAbmVehiculosController', [ 2 .controller('focaAbmVehiculosController', [
3 '$scope', 'focaAbmVehiculoService', '$location', 'focaModalService', 3 '$scope', 'focaAbmVehiculoService', '$location', 'focaModalService',
4 '$uibModal', 'focaBotoneraLateralService', '$timeout', '$localStorage', 4 '$uibModal', 'focaBotoneraLateralService', '$timeout', '$localStorage',
5 function($scope, focaAbmVehiculoService, $location, focaModalService, 5 function($scope, focaAbmVehiculoService, $location, focaModalService,
6 $uibModal, focaBotoneraLateralService, $timeout, $localStorage) { 6 $uibModal, focaBotoneraLateralService, $timeout, $localStorage) {
7 7
8 $scope.now = new Date(); 8 $scope.now = new Date();
9 $scope.botonera = [{ 9 $scope.botonera = [{
10 label: 'Transportista', 10 label: 'Transportista',
11 image: 'cliente.png' 11 image: 'cliente.png'
12 }]; 12 }];
13 13
14 focaAbmVehiculoService.cleanCisternas(); 14 focaAbmVehiculoService.cleanCisternas();
15 15
16 //SETEO BOTONERA LATERAL 16 //SETEO BOTONERA LATERAL
17 focaBotoneraLateralService.showSalir(false); 17 focaBotoneraLateralService.showSalir(false);
18 focaBotoneraLateralService.showPausar(false); 18 focaBotoneraLateralService.showPausar(false);
19 focaBotoneraLateralService.showCancelar(false); 19 focaBotoneraLateralService.showCancelar(false);
20 focaBotoneraLateralService.showGuardar(false); 20 focaBotoneraLateralService.showGuardar(false);
21 focaBotoneraLateralService.addCustomButton('Salir', salir); 21 focaBotoneraLateralService.addCustomButton('Salir', salir);
22 22
23 if (focaAbmVehiculoService.transportistaSeleccionado.COD) { 23 if (focaAbmVehiculoService.transportistaSeleccionado.COD) {
24 elegirTransportista(focaAbmVehiculoService.transportistaSeleccionado); 24 elegirTransportista(focaAbmVehiculoService.transportistaSeleccionado);
25 } 25 }
26 $scope.editar = function(id) { 26 $scope.editar = function(id) {
27 $location.path('/vehiculo/' + id + '/' + $scope.idTransportista); 27 $location.path('/vehiculo/' + id + '/' + $scope.idTransportista);
28 }; 28 };
29 $scope.solicitarConfirmacion = function(vehiculo) { 29 $scope.solicitarConfirmacion = function(vehiculo) {
30 focaModalService.confirm('¿Está seguro que desea borrar el vehiculo ' + 30 focaModalService.confirm('¿Está seguro que desea borrar el vehiculo ' +
31 vehiculo.id + ' ' + vehiculo.tractor + ' ?').then( 31 vehiculo.id + ' ' + vehiculo.tractor + ' ?').then(
32 function(data) { 32 function(data) {
33 if (data) { 33 if (data) {
34 focaAbmVehiculoService.deleteVehiculo(vehiculo.id); 34 focaAbmVehiculoService.deleteVehiculo(vehiculo.id);
35 $scope.vehiculos.splice($scope.vehiculos.indexOf(vehiculo), 1); 35 $scope.vehiculos.splice($scope.vehiculos.indexOf(vehiculo), 1);
36 } 36 }
37 } 37 }
38 ); 38 );
39 }; 39 };
40 $scope.seleccionarTransportista = function() { 40 $scope.seleccionarTransportista = function() {
41 var parametrosModal = { 41 var parametrosModal = {
42 titulo: 'Búsqueda de Transportista', 42 titulo: 'Búsqueda de Transportista',
43 query: '/transportista', 43 query: '/transportista',
44 columnas: [ 44 columnas: [
45 { 45 {
46 nombre: 'Código', 46 nombre: 'Código',
47 propiedad: 'COD' 47 propiedad: 'COD'
48 }, 48 },
49 { 49 {
50 nombre: 'Nombre', 50 nombre: 'Nombre',
51 propiedad: 'NOM' 51 propiedad: 'NOM'
52 }, 52 },
53 { 53 {
54 nombre: 'CUIT', 54 nombre: 'CUIT',
55 propiedad: 'CUIT' 55 propiedad: 'CUIT'
56 } 56 }
57 ] 57 ]
58 }; 58 };
59 focaModalService.modal(parametrosModal).then( 59 focaModalService.modal(parametrosModal).then(
60 function(transportista) { 60 function(transportista) {
61 elegirTransportista(transportista); 61 elegirTransportista(transportista);
62 focaAbmVehiculoService.transportistaSeleccionado = transportista; 62 focaAbmVehiculoService.transportistaSeleccionado = transportista;
63 }, function() { 63 }, function() {
64 64
65 } 65 }
66 ); 66 );
67 }; 67 };
68 function elegirTransportista(transportista) { 68 function elegirTransportista(transportista) {
69 var codigo = ('00000' + transportista.COD).slice(-5); 69 var codigo = ('00000' + transportista.COD).slice(-5);
70 $scope.idTransportista = transportista.COD; 70 $scope.idTransportista = transportista.COD;
71 $scope.filtros = transportista.NOM.trim(); 71 $scope.filtros = transportista.NOM.trim();
72 $timeout(function() { 72 $timeout(function() {
73 $scope.$broadcast('addCabecera', { 73 $scope.$broadcast('addCabecera', {
74 label: 'Transportista:', 74 label: 'Transportista:',
75 valor: codigo + ' - ' + transportista.NOM 75 valor: codigo + ' - ' + transportista.NOM
76 }); 76 });
77 }); 77 });
78 buscar(transportista.COD); 78 buscar(transportista.COD);
79 } 79 }
80 80
81 function buscar(idTransportista) { 81 function buscar(idTransportista) {
82 focaAbmVehiculoService 82 focaAbmVehiculoService
83 .getVehiculosPorTransportista(idTransportista) 83 .getVehiculosPorTransportista(idTransportista)
84 .then(function(datos) { 84 .then(function(datos) {
85 $scope.vehiculos = datos.data; 85 $scope.vehiculos = datos.data;
86 }); 86 });
87 } 87 }
88 function salir() { 88 function salir() {
89 focaAbmVehiculoService.transportistaSeleccionado = {}; 89 focaAbmVehiculoService.transportistaSeleccionado = {};
90 $location.path('/'); 90 $location.path('/');
91 } 91 }
92 } 92 }
93 ]) 93 ])
94 .controller('focaAbmVehiculoController', [ 94 .controller('focaAbmVehiculoController', [
95 '$scope', 'focaAbmVehiculoService', '$routeParams', '$location', '$uibModal', 95 '$scope', 'focaAbmVehiculoService', '$routeParams', '$location', '$uibModal',
96 'focaModalService', '$timeout', 'focaBotoneraLateralService', '$localStorage', '$filter', 96 'focaModalService', '$timeout', 'focaBotoneraLateralService', '$localStorage', '$filter',
97 function($scope, focaAbmVehiculoService, $routeParams, $location, $uibModal, 97 function($scope, focaAbmVehiculoService, $routeParams, $location, $uibModal,
98 focaModalService, $timeout, focaBotoneraLateralService, $localStorage, $filter) { 98 focaModalService, $timeout, focaBotoneraLateralService, $localStorage, $filter) {
99 $scope.nuevo = $routeParams.idVehiculo === '0' ? true : false; 99 $scope.nuevo = $routeParams.idVehiculo === '0' ? true : false;
100 $scope.now = new Date(); 100 $scope.now = new Date();
101 $scope.focused = 1; 101 $scope.focused = 1;
102 $scope.transportistaStamp = ''; 102 $scope.transportistaStamp = '';
103 $scope.cisternas = []; 103 $scope.cisternas = [];
104 104
105 $timeout(function() { 105 $timeout(function() {
106 focaBotoneraLateralService.showSalir(false); 106 focaBotoneraLateralService.showSalir(false);
107 focaBotoneraLateralService.showPausar(true); 107 focaBotoneraLateralService.showPausar(true);
108 focaBotoneraLateralService.showCancelar(false); 108 focaBotoneraLateralService.showCancelar(false);
109 focaBotoneraLateralService.showGuardar(true, $scope.guardar); 109 focaBotoneraLateralService.showGuardar(true, $scope.guardar);
110 focaBotoneraLateralService.addCustomButton('Salir', salir); 110 focaBotoneraLateralService.addCustomButton('Salir', salir);
111 }); 111 });
112 112
113 $timeout(function() {getLSVehiculo();}); 113 $timeout(function() {getLSVehiculo();});
114 114
115 if ($scope.nuevo) { 115 if ($scope.nuevo) {
116 focaAbmVehiculoService 116 focaAbmVehiculoService
117 .getTransportistaPorId($routeParams.idTransportista) 117 .getTransportistaPorId($routeParams.idTransportista)
118 .then(function(res) { 118 .then(function(res) {
119 var codigo = ('00000' + res.data.COD).slice(-5); 119 var codigo = ('00000' + res.data.COD).slice(-5);
120 $scope.vehiculo.idTransportista = res.data.COD; 120 $scope.vehiculo.idTransportista = res.data.COD;
121 $scope.vehiculo.transportista = res.data; 121 $scope.vehiculo.transportista = res.data;
122 $scope.$broadcast('addCabecera', { 122 $scope.$broadcast('addCabecera', {
123 label: 'Transportista:', 123 label: 'Transportista:',
124 valor: codigo + ' - ' + res.data.NOM 124 valor: codigo + ' - ' + res.data.NOM
125 }); 125 });
126 }); 126 });
127 } 127 }
128 $scope.vehiculo = {}; 128 $scope.vehiculo = {};
129 focaAbmVehiculoService.getVehiculo($routeParams.idVehiculo).then(function(res) { 129 focaAbmVehiculoService.getVehiculo($routeParams.idVehiculo).then(function(res) {
130 if (res.data) { 130 if (res.data) {
131 $scope.transportistaStamp = ('00000' + res.data.transportista.COD).slice(-5); 131 $scope.transportistaStamp = ('00000' + res.data.transportista.COD).slice(-5);
132 $scope.transportistaStamp += ' - ' + res.data.transportista.NOM; 132 $scope.transportistaStamp += ' - ' + res.data.transportista.NOM;
133 133
134 $scope.vehiculo = res.data; 134 $scope.vehiculo = res.data;
135 $scope.$broadcast('addCabecera', { 135 $scope.$broadcast('addCabecera', {
136 label: 'Transportista:', 136 label: 'Transportista:',
137 valor: $scope.transportistaStamp 137 valor: $scope.transportistaStamp
138 }); 138 });
139 $scope.$broadcast('addCabecera', { 139 $scope.$broadcast('addCabecera', {
140 label: 'Unidad:', 140 label: 'Unidad:',
141 valor: res.data.codigo 141 valor: res.data.codigo
142 }); 142 });
143 focaAbmVehiculoService 143 focaAbmVehiculoService
144 .getCisternas($routeParams.idVehiculo) 144 .getCisternas($routeParams.idVehiculo)
145 .then(function(res) { 145 .then(function(res) {
146 $scope.cisternas = res; 146 $scope.cisternas = res;
147 $scope.$apply(); 147 $scope.$apply();
148 }); 148 });
149 } 149 }
150 }); 150 });
151 151
152 $scope.next = function(key) { 152 $scope.next = function(key) {
153 if (key === 13) $scope.focused++; 153 if (key === 13) $scope.focused++;
154 }; 154 };
155 155
156 function salir() { 156 function salir() {
157 if (!$scope.formVehiculo.$pristine) { 157 if (!$scope.formVehiculo.$pristine && focaAbmVehiculoService.cisternasPristine) {
158 focaModalService.confirm( 158 focaModalService.confirm(
159 '¿Está seguro de que desea salir? Se perderán todos los datos cargados.' 159 '¿Está seguro de que desea salir? Se perderán todos los datos cargados.'
160 ).then(function(data) { 160 ).then(function(data) {
161 if (data) { 161 if (data) {
162 $location.path('/vehiculo'); 162 $location.path('/vehiculo');
163 } 163 }
164 }); 164 });
165 } else { 165 } else {
166 $location.path('/vehiculo'); 166 $location.path('/vehiculo');
167 } 167 }
168 } 168 }
169 169
170 $scope.editar = function(key) { 170 $scope.editar = function(key) {
171 if (key) { 171 if (key) {
172 $location.path('/vehiculo/' + $routeParams.idVehiculo + 172 $location.path('/vehiculo/' + $routeParams.idVehiculo +
173 '/cisterna/' + key); 173 '/cisterna/' + key);
174 } else { 174 } else {
175 $location.path('/vehiculo/' + $routeParams.idVehiculo + '/cisterna/0/'); 175 $location.path('/vehiculo/' + $routeParams.idVehiculo + '/cisterna/0/');
176 } 176 }
177 }; 177 };
178 $scope.guardar = function(key) { 178 $scope.guardar = function(key) {
179 179
180 key = (typeof key === 'undefined') ? 13 : key; 180 key = (typeof key === 'undefined') ? 13 : key;
181 181
182 if (key === 13) { 182 if (key === 13) {
183 183
184 if ($scope.formVehiculo.$pristine) { 184 if ($scope.formVehiculo.$pristine && focaAbmVehiculoService.cisternasPristine) {
185 $scope.cancelar(); 185 salir();
186 return; 186 return;
187 } 187 }
188 188
189 if (!$scope.vehiculo.codigo) { 189 if (!$scope.vehiculo.codigo) {
190 focaModalService.alert('Ingrese unidad'); 190 focaModalService.alert('Ingrese unidad');
191 return; 191 return;
192 } else if (!$scope.vehiculo.tractor) { 192 } else if (!$scope.vehiculo.tractor) {
193 focaModalService.alert('Ingrese dominio tractor'); 193 focaModalService.alert('Ingrese dominio tractor');
194 return; 194 return;
195 } else if (!$scope.vehiculo.semi) { 195 } else if (!$scope.vehiculo.semi) {
196 focaModalService.alert('Ingrese dominio semi'); 196 focaModalService.alert('Ingrese dominio semi');
197 return; 197 return;
198 } else if (!$scope.vehiculo.capacidad) { 198 } else if (!$scope.vehiculo.capacidad) {
199 focaModalService.alert('Ingrese capacidad total'); 199 focaModalService.alert('Ingrese capacidad total');
200 return; 200 return;
201 } 201 }
202 //Valida si existe numero de unidad 202 //Valida si existe numero de unidad
203 if (!validaTotalCargas() && !$scope.nuevo) { 203 if (!validaTotalCargas() && !$scope.nuevo) {
204 focaModalService.alert('La suma de las capacidades de las cisternas' + 204 focaModalService.alert('La suma de las capacidades de las cisternas' +
205 ' debe ser igual a la capacidad total del vehículo'); 205 ' debe ser igual a la capacidad total del vehículo');
206 return; 206 return;
207 } 207 }
208 validaCodigoUnidad().then(function() { 208 validaCodigoUnidad().then(function() {
209 delete $scope.vehiculo.transportista; 209 delete $scope.vehiculo.transportista;
210 delete $scope.vehiculo.cisternas; 210 delete $scope.vehiculo.cisternas;
211 focaAbmVehiculoService.guardarVehiculo($scope.vehiculo) 211 focaAbmVehiculoService.guardarVehiculo($scope.vehiculo)
212 .then(function(res) { 212 .then(function(res) {
213 if ($scope.nuevo) { 213 if ($scope.nuevo) {
214 $location.path('/vehiculo/' + res.data.id + 214 $location.path('/vehiculo/' + res.data.id +
215 '/' + res.data.idTransportista); 215 '/' + res.data.idTransportista);
216 } else { 216 } else {
217 guardarCisternas().then(function() { 217 guardarCisternas().then(function() {
218 $location.path('/vehiculo'); 218 $location.path('/vehiculo');
219 }); 219 });
220 } 220 }
221 }); 221 });
222 }, function() { 222 }, function() {
223 focaModalService.alert('Código de unidad existente'); 223 focaModalService.alert('Código de unidad existente');
224 }); 224 });
225 } 225 }
226 226
227 }; 227 };
228 228
229 $scope.$watch('vehiculo', function(newValue) { 229 $scope.$watch('vehiculo', function(newValue) {
230 focaBotoneraLateralService.setPausarData({ 230 focaBotoneraLateralService.setPausarData({
231 label:'vehiculo', 231 label:'vehiculo',
232 val: { 232 val: {
233 codigo: newValue.codigo, 233 codigo: newValue.codigo,
234 tractor: newValue.tractor, 234 tractor: newValue.tractor,
235 semi: newValue.semi, 235 semi: newValue.semi,
236 capacidad: newValue.capacidad, 236 capacidad: newValue.capacidad,
237 idVehiculo: newValue.idVehiculo, 237 idVehiculo: newValue.idVehiculo,
238 idTransportista: newValue.idTransportista 238 idTransportista: newValue.idTransportista
239 } 239 }
240 }); 240 });
241 }, true); 241 }, true);
242 242
243 function getLSVehiculo() { 243 function getLSVehiculo() {
244 var vehiculo = JSON.parse($localStorage.vehiculo|| null); 244 var vehiculo = JSON.parse($localStorage.vehiculo|| null);
245 if (vehiculo) { 245 if (vehiculo) {
246 setearVehiculo(vehiculo); 246 setearVehiculo(vehiculo);
247 delete $localStorage.vehiculo; 247 delete $localStorage.vehiculo;
248 } 248 }
249 } 249 }
250 250
251 function setearVehiculo(vehiculo) { 251 function setearVehiculo(vehiculo) {
252 $scope.vehiculo = vehiculo; 252 $scope.vehiculo = vehiculo;
253 $scope.$broadcast('addCabecera', { 253 $scope.$broadcast('addCabecera', {
254 label: 'Vehiculo:', 254 label: 'Vehiculo:',
255 valor: $filter('rellenarDigitos')(vehiculo.codigo) + ' - ' 255 valor: $filter('rellenarDigitos')(vehiculo.codigo) + ' - '
256 }); 256 });
257 } 257 }
258 258
259 $scope.solicitarConfirmacionCisterna = function(cisterna, idx) { 259 $scope.solicitarConfirmacionCisterna = function(cisterna, idx) {
260 focaModalService.confirm('¿Está seguro que desea borrar la cisterna ' + 260 focaModalService.confirm('¿Está seguro que desea borrar la cisterna ' +
261 cisterna.id + ' ' + cisterna.codigo + ' ?').then( 261 cisterna.id + ' ' + cisterna.codigo + ' ?').then(
262 function(data) { 262 function(data) {
263 if (data) { 263 if (data) {
264 focaAbmVehiculoService.deleteCisterna(idx); 264 focaAbmVehiculoService.deleteCisterna(idx);
265 focaAbmVehiculoService 265 focaAbmVehiculoService
266 .getCisternas($routeParams.idVehiculo) 266 .getCisternas($routeParams.idVehiculo)
267 .then(function(res) { 267 .then(function(res) {
268 $scope.cisternas = res; 268 $scope.cisternas = res;
269 }); 269 });
270 } 270 }
271 } 271 }
272 ); 272 );
273 }; 273 };
274 274
275 function validaCodigoUnidad() { 275 function validaCodigoUnidad() {
276 return new Promise(function(resolve, reject) { 276 return new Promise(function(resolve, reject) {
277 focaAbmVehiculoService 277 focaAbmVehiculoService
278 .getVehiculosPorTransportista(parseInt($routeParams.idTransportista)) 278 .getVehiculosPorTransportista(parseInt($routeParams.idTransportista))
279 .then(function(res) { 279 .then(function(res) {
280 //Valida si existe numero de unidad 280 //Valida si existe numero de unidad
281 var existe = res.data.filter(function(vehiculo) { 281 var existe = res.data.filter(function(vehiculo) {
282 return vehiculo.codigo === $scope.vehiculo.codigo && 282 return vehiculo.codigo === $scope.vehiculo.codigo &&
283 vehiculo.id !== $scope.vehiculo.id; 283 vehiculo.id !== $scope.vehiculo.id;
284 }); 284 });
285 285
286 if (existe.length) { 286 if (existe.length) {
287 reject(existe); 287 reject(existe);
288 } else { 288 } else {
289 resolve(); 289 resolve();
290 } 290 }
291 }); 291 });
292 }); 292 });
293 } 293 }
294 294
295 function validaTotalCargas() { 295 function validaTotalCargas() {
296 var total = 0; 296 var total = 0;
297 $scope.cisternas.forEach(function(cisterna) { 297 $scope.cisternas.forEach(function(cisterna) {
298 if (!cisterna.desactivado) { 298 if (!cisterna.desactivado) {
299 total += parseInt(cisterna.capacidad); 299 total += parseInt(cisterna.capacidad);
300 } 300 }
301 }); 301 });
302 return $scope.vehiculo.capacidad === total; 302 return $scope.vehiculo.capacidad == total;
303 } 303 }
304 304
305 function guardarCisternas() { 305 function guardarCisternas() {
306 var cisternas = $scope.cisternas.map(function(cisterna) { 306 var cisternas = $scope.cisternas.map(function(cisterna) {
307 return { 307 return {
308 id: cisterna.id, 308 id: cisterna.id,
309 capacidad: parseFloat(cisterna.capacidad), 309 capacidad: parseFloat(cisterna.capacidad),
310 codigo: cisterna.codigo, 310 codigo: cisterna.codigo,
311 idUnidadMedida: cisterna.idUnidadMedida, 311 idUnidadMedida: cisterna.idUnidadMedida,
312 idVehiculo: $routeParams.idVehiculo, 312 idVehiculo: $routeParams.idVehiculo,
313 desactivado: cisterna.desactivado 313 desactivado: cisterna.desactivado
314 }; 314 };
315 }); 315 });
316 316
317 return focaAbmVehiculoService.guardarCisternas(cisternas); 317 return focaAbmVehiculoService.guardarCisternas(cisternas);
318 } 318 }
319
319 } 320 }
320 ]); 321 ]);
321 322
src/js/controllerCisterna.js
1 angular.module('focaAbmVehiculo') 1 angular.module('focaAbmVehiculo')
2 .controller('focaAbmVehiculoCisternaController', [ 2 .controller('focaAbmVehiculoCisternaController', [
3 '$scope', 'focaAbmVehiculoService', '$routeParams', '$location', '$uibModal', 3 '$scope', 'focaAbmVehiculoService', '$routeParams', '$location', '$uibModal',
4 'focaModalService', 'focaBotoneraLateralService', '$timeout','$localStorage', '$filter', 4 'focaModalService', 'focaBotoneraLateralService', '$timeout', '$window',
5 function($scope, focaAbmVehiculoService, $routeParams, $location, $uibModal, 5 function($scope, focaAbmVehiculoService, $routeParams, $location, $uibModal,
6 focaModalService, focaBotoneraLateralService, $timeout, $localStorage, $filter) { 6 focaModalService, focaBotoneraLateralService, $timeout, $window) {
7 $scope.nuevo = ($routeParams.idx > -1) ? false : true; 7 $scope.nuevo = ($routeParams.idx > -1) ? false : true;
8 $scope.editar = false; 8 $scope.editar = false;
9 $scope.now = new Date(); 9 $scope.now = new Date();
10 $scope.cisterna = { 10 $scope.cisterna = {
11 codigo: '', 11 codigo: '',
12 capacidad: '', 12 capacidad: '',
13 unidadMedida: {} 13 unidadMedida: {}
14 }; 14 };
15 15
16 $scope.focused = $scope.nuevo ? 1 : 2; 16 $scope.focused = $scope.nuevo ? 1 : 2;
17 $scope.next = function(key) { 17 $scope.next = function(key) {
18 if (key === 13) $scope.focused++; 18 if (key === 13) $scope.focused++;
19 }; 19 };
20 $scope.capacidadVechiulo = 0; 20 $scope.capacidadVechiulo = 0;
21 $scope.transportista = ''; 21 $scope.transportista = '';
22 22
23 //SETEO BOTONERA LATERAL 23 //SETEO BOTONERA LATERAL
24 $timeout(function() { 24 $timeout(function() {
25 focaBotoneraLateralService.showSalir(false); 25 focaBotoneraLateralService.showSalir(false);
26 focaBotoneraLateralService.showPausar(true); 26 focaBotoneraLateralService.showPausar(true);
27 focaBotoneraLateralService.showCancelar(false); 27 focaBotoneraLateralService.showCancelar(false);
28 focaBotoneraLateralService.showGuardar(true, $scope.guardar); 28 focaBotoneraLateralService.showGuardar(true, $scope.guardar);
29 focaBotoneraLateralService.addCustomButton('Cancelar', $scope.cancelar); 29 focaBotoneraLateralService.addCustomButton('Cancelar', $scope.cancelar);
30 }); 30 });
31 31
32 if ($routeParams.idx !== -1) { 32 if ($routeParams.idx !== -1) {
33 $scope.cisterna = [$routeParams.idx]; 33 $scope.cisterna = [$routeParams.idx];
34 focaAbmVehiculoService 34 focaAbmVehiculoService
35 .getCisternas($routeParams.idVehiculo) 35 .getCisternas($routeParams.idVehiculo)
36 .then(function(res) { 36 .then(function(res) {
37 $scope.cisterna = res[$routeParams.idx]; 37 $scope.cisterna = res[$routeParams.idx];
38 }); 38 });
39 } 39 }
40 40
41 focaAbmVehiculoService.getVehiculo($routeParams.idVehiculo).then(function(res) { 41 focaAbmVehiculoService.getVehiculo($routeParams.idVehiculo).then(function(res) {
42 var codigo = ('00000' + res.data.transportista.COD).slice(-5); 42 var codigo = ('00000' + res.data.transportista.COD).slice(-5);
43 $scope.transportista = res.data.transportista.COD; 43 $scope.transportista = res.data.transportista.COD;
44 $scope.capacidadVechiulo = res.data.capacidad; 44 $scope.capacidadVechiulo = res.data.capacidad;
45 $scope.$broadcast('addCabecera', { 45 $scope.$broadcast('addCabecera', {
46 label: 'Transportista:', 46 label: 'Transportista:',
47 valor: codigo + ' - ' + res.data.transportista.NOM 47 valor: codigo + ' - ' + res.data.transportista.NOM
48 }); 48 });
49 $scope.$broadcast('addCabecera', { 49 $scope.$broadcast('addCabecera', {
50 label: 'Unidad:', 50 label: 'Unidad:',
51 valor: res.data.codigo 51 valor: res.data.codigo
52 }); 52 });
53 $scope.$broadcast('addCabecera', { 53 $scope.$broadcast('addCabecera', {
54 label: 'Capacidad total vehículo:', 54 label: 'Capacidad total vehículo:',
55 valor: res.data.capacidad 55 valor: res.data.capacidad
56 }); 56 });
57 }); 57 });
58 58
59 $scope.cancelar = function() { 59 $scope.cancelar = function() {
60 60
61 if (!$scope.formCisterna.$pristine) { 61 if (!$scope.formCisterna.$pristine) {
62 focaModalService.confirm( 62 focaModalService.confirm(
63 '¿Está seguro de que desea salir? Se perderán todos los datos cargados.' 63 '¿Está seguro de que desea salir? Se perderán todos los datos cargados.'
64 ).then(function(data) { 64 ).then(function(data) {
65 if (data) { 65 if (data) {
66 $location.path('/vehiculo'); 66 $location.path('/vehiculo');
67 } 67 }
68 }); 68 });
69 } else {
70 $window.history.back();
69 } 71 }
70 }; 72 };
71 73
72 $scope.guardar = function() { 74 $scope.guardar = function() {
73 75
74 if ($scope.formCisterna.$pristine) { 76 if ($scope.formCisterna.$pristine) {
75 $scope.cancelar(); 77 $scope.cancelar();
76 return; 78 return;
77 } 79 } else if (!$scope.cisterna.codigo) {
78
79 if (!$scope.cisterna.codigo) {
80 focaModalService.alert('Ingrese codigo de cisterna'); 80 focaModalService.alert('Ingrese codigo de cisterna');
81 return; 81 return;
82 } else if (!$scope.cisterna.capacidad) { 82 } else if (!$scope.cisterna.capacidad) {
83 focaModalService.alert('Ingrese capacidad'); 83 focaModalService.alert('Ingrese capacidad');
84 return; 84 return;
85 } else if (!$scope.cisterna.idUnidadMedida) { 85 } else if (!$scope.cisterna.idUnidadMedida) {
86 focaModalService.alert('Ingrese unidad de medida'); 86 focaModalService.alert('Ingrese unidad de medida');
87 return; 87 return;
88 } 88 }
89 89
90 validaCodigo() 90 validaCodigo()
91 .then(function() { 91 .then(function() {
92 $scope.cisterna.idVehiculo = parseInt($routeParams.idVehiculo); 92 $scope.cisterna.idVehiculo = parseInt($routeParams.idVehiculo);
93 delete $scope.cisterna.vehiculo; 93 delete $scope.cisterna.vehiculo;
94 94
95 focaAbmVehiculoService 95 focaAbmVehiculoService
96 .guardarCisterna($scope.cisterna, $routeParams.idx); 96 .guardarCisterna($scope.cisterna, $routeParams.idx);
97 97
98 $timeout(function() { 98 $timeout(function() {
99 $location.path('/vehiculo/' + $routeParams.idVehiculo + 99 $location.path('/vehiculo/' + $routeParams.idVehiculo +
100 '/' + $scope.transportista); 100 '/' + $scope.transportista);
101 }, 0); 101 }, 0);
102 }, function(err) { 102 }, function(err) {
103 focaModalService.alert(err); 103 focaModalService.alert(err);
104 }); 104 });
105 105
106 }; 106 };
107 107
108 $scope.seleccionarUnidadMedida = function() { 108 $scope.seleccionarUnidadMedida = function() {
109 var modalInstance = $uibModal.open( 109 var modalInstance = $uibModal.open(
110 { 110 {
111 ariaLabelledBy: 'Busqueda de Unidades de medida', 111 ariaLabelledBy: 'Busqueda de Unidades de medida',
112 templateUrl: 'modal-unidad-medida.html', 112 templateUrl: 'modal-unidad-medida.html',
113 controller: 'focaModalUnidadMedidaCtrl', 113 controller: 'focaModalUnidadMedidaCtrl',
114 size: 'lg' 114 size: 'lg'
115 } 115 }
116 ); 116 );
117 modalInstance.result.then(function(unidaMedida) { 117 modalInstance.result.then(function(unidaMedida) {
118 $scope.cisterna.idUnidadMedida = unidaMedida.ID; 118 $scope.cisterna.idUnidadMedida = unidaMedida.ID;
119 $scope.cisterna.unidadMedida = unidaMedida; 119 $scope.cisterna.unidadMedida = unidaMedida;
120 }); 120 });
121 }; 121 };
122 122
123 function validaCodigo() { 123 function validaCodigo() {
124 return new Promise(function(resolve, reject) { 124 return new Promise(function(resolve, reject) {
125 focaAbmVehiculoService 125 focaAbmVehiculoService
126 .getCisternas($routeParams.idVehiculo) 126 .getCisternas($routeParams.idVehiculo)
127 .then(function(res) { 127 .then(function(res) {
128 var cisternas = res; 128 var cisternas = res;
129 var totalCargado = 0; 129 var totalCargado = 0;
130 cisternas.forEach(function(cisterna, idx) { 130 cisternas.forEach(function(cisterna, idx) {
131 //SI EL CODIGO YA EXISTE 131 //SI EL CODIGO YA EXISTE
132 if (cisterna.codigo === $scope.cisterna.codigo && 132 if (cisterna.codigo === $scope.cisterna.codigo &&
133 idx !== $routeParams.idx && 133 idx != $routeParams.idx &&
134 !cisterna.desactivado) { 134 !cisterna.desactivado) {
135 reject('Código de cisterna existente'); 135 reject('Código de cisterna existente');
136 } 136 }
137 if (idx !== $routeParams.idx && 137 if (idx !== $routeParams.idx &&
138 !cisterna.desactivado) { 138 !cisterna.desactivado) {
139 totalCargado += cisterna.capacidad; 139 totalCargado += cisterna.capacidad;
140 } 140 }
141 }); 141 });
142 resolve(); 142 resolve();
143 }); 143 });
144 }); 144 });
145 } 145 }
146 } 146 }
1 angular.module('focaAbmVehiculo') 1 angular.module('focaAbmVehiculo')
2 .factory('focaAbmVehiculoService', ['$http', 'API_ENDPOINT', function($http, API_ENDPOINT) { 2 .factory('focaAbmVehiculoService', ['$http', 'API_ENDPOINT', function($http, API_ENDPOINT) {
3 var cisternas = []; 3 var cisternas = [];
4 var cisternasPristine = true;
4 return { 5 return {
5 getVehiculos: function() { 6 getVehiculos: function() {
6 return $http.get(API_ENDPOINT.URL + '/vehiculo'); 7 return $http.get(API_ENDPOINT.URL + '/vehiculo');
7 }, 8 },
8 getVehiculo: function(id) { 9 getVehiculo: function(id) {
9 return $http.get(API_ENDPOINT.URL + '/vehiculo/' + id); 10 return $http.get(API_ENDPOINT.URL + '/vehiculo/' + id);
10 }, 11 },
11 getTransportistas: function() { 12 getTransportistas: function() {
12 return $http.get(API_ENDPOINT.URL + '/transportista'); 13 return $http.get(API_ENDPOINT.URL + '/transportista');
13 }, 14 },
14 guardarVehiculo: function(vehiculo) { 15 guardarVehiculo: function(vehiculo) {
15 return $http.post(API_ENDPOINT.URL + '/vehiculo', {vehiculo: vehiculo}); 16 return $http.post(API_ENDPOINT.URL + '/vehiculo', {vehiculo: vehiculo});
16 }, 17 },
17 deleteVehiculo: function(id) { 18 deleteVehiculo: function(id) {
18 return $http.delete(API_ENDPOINT.URL + '/vehiculo/' + id); 19 return $http.delete(API_ENDPOINT.URL + '/vehiculo/' + id);
19 }, 20 },
20 getCisternas: function(idVehiculo) { 21 getCisternas: function(idVehiculo) {
21 if(cisternas.length) { 22 if (cisternas.length) {
23 cisternasPristine = true;
22 return Promise.resolve(angular.copy(cisternas)); 24 return Promise.resolve(angular.copy(cisternas));
23 }else { 25 } else {
26 cisternasPristine = true;
24 return new Promise(function(resolve) { 27 return new Promise(function(resolve) {
25 $http.get(API_ENDPOINT.URL + '/cisterna/listar/' + idVehiculo) 28 $http.get(API_ENDPOINT.URL + '/cisterna/listar/' + idVehiculo)
26 .then(function(res) { 29 .then(function(res) {
27 cisternas = res.data; 30 cisternas = res.data;
28 resolve(res.data); 31 resolve(res.data);
29 }); 32 });
30 }); 33 });
31 } 34 }
32 }, 35 },
33 guardarCisterna: function(cisterna, idx) { 36 guardarCisterna: function(cisterna, idx) {
34 if(idx !== '-1') { 37 if (idx !== '-1') {
35 //update 38 //update
36 cisternas[idx] = cisterna; 39 cisternas[idx] = cisterna;
37 }else { 40 cisternasPristine = false;
41 } else {
38 //insert 42 //insert
39 cisternas.push(cisterna); 43 cisternas.push(cisterna);
44 cisternasPristine = false;
40 } 45 }
41 }, 46 },
42 guardarCisternas: function(cisternas) { 47 guardarCisternas: function(cisternas) {
43 return $http.post(API_ENDPOINT.URL + '/cisterna', {cisternas: cisternas}); 48 return $http.post(API_ENDPOINT.URL + '/cisterna', {cisternas: cisternas});
44 }, 49 },
45 deleteCisterna: function(idx) { 50 deleteCisterna: function(idx) {
46 cisternas[idx].desactivado = true; 51 cisternas[idx].desactivado = true;
52 cisternasPristine = false;
47 }, 53 },
48 cleanCisternas: function() { 54 cleanCisternas: function() {
49 cisternas = []; 55 cisternas = [];
50 }, 56 },
51 getVehiculosPorTransportista: function(id) { 57 getVehiculosPorTransportista: function(id) {
52 return $http.get(API_ENDPOINT.URL + '/vehiculo/transportista/' + id); 58 return $http.get(API_ENDPOINT.URL + '/vehiculo/transportista/' + id);
53 }, 59 },
54 getTransportistaPorId: function(id) { 60 getTransportistaPorId: function(id) {
55 return $http.get(API_ENDPOINT.URL + '/transportista/' + id); 61 return $http.get(API_ENDPOINT.URL + '/transportista/' + id);
56 }, 62 },
57 transportistaSeleccionado: {} 63 transportistaSeleccionado: {}
58 }; 64 };
59 }]); 65 }]);
60 66