Commit fb66976cf049405afde26c0a962f6c6d5dca61c0

Authored by Luigi
1 parent 701f6c9c2e
Exists in master

Cambio en idUnidadMedida

Showing 1 changed file with 1 additions and 1 deletions   Show diff stats
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.nuevoCisterna = ($routeParams.idx > -1) ? false : true; 100 $scope.nuevoCisterna = ($routeParams.idx > -1) ? false : true;
101 $scope.now = new Date(); 101 $scope.now = new Date();
102 $scope.focused = 1; 102 $scope.focused = 1;
103 $scope.creando = false; 103 $scope.creando = false;
104 $scope.transportistaStamp = ''; 104 $scope.transportistaStamp = '';
105 $scope.cisternas = []; 105 $scope.cisternas = [];
106 $scope.cisterna = { 106 $scope.cisterna = {
107 codigo: '', 107 codigo: '',
108 capacidad: '', 108 capacidad: '',
109 idUnidadMedida: {}, 109 idUnidadMedida: 0,
110 unidadMedida: {} 110 unidadMedida: {}
111 }; 111 };
112 112
113 $timeout(function() { 113 $timeout(function() {
114 focaBotoneraLateralService.showSalir(false); 114 focaBotoneraLateralService.showSalir(false);
115 focaBotoneraLateralService.showPausar(true); 115 focaBotoneraLateralService.showPausar(true);
116 focaBotoneraLateralService.showCancelar(false); 116 focaBotoneraLateralService.showCancelar(false);
117 focaBotoneraLateralService.showGuardar(true, $scope.guardar); 117 focaBotoneraLateralService.showGuardar(true, $scope.guardar);
118 focaBotoneraLateralService.addCustomButton('Salir', $scope.salir); 118 focaBotoneraLateralService.addCustomButton('Salir', $scope.salir);
119 }); 119 });
120 120
121 if ($scope.nuevo) { 121 if ($scope.nuevo) {
122 focaAbmVehiculoService 122 focaAbmVehiculoService
123 .getTransportistaPorId($routeParams.idTransportista) 123 .getTransportistaPorId($routeParams.idTransportista)
124 .then(function(res) { 124 .then(function(res) {
125 var codigo = ('00000' + res.data.COD).slice(-5); 125 var codigo = ('00000' + res.data.COD).slice(-5);
126 $scope.vehiculo.idTransportista = res.data.COD; 126 $scope.vehiculo.idTransportista = res.data.COD;
127 $scope.vehiculo.transportista = res.data; 127 $scope.vehiculo.transportista = res.data;
128 $scope.$broadcast('addCabecera', { 128 $scope.$broadcast('addCabecera', {
129 label: 'Transportista:', 129 label: 'Transportista:',
130 valor: codigo + ' - ' + res.data.NOM 130 valor: codigo + ' - ' + res.data.NOM
131 }); 131 });
132 }); 132 });
133 } 133 }
134 $scope.vehiculo = {}; 134 $scope.vehiculo = {};
135 135
136 focaAbmVehiculoService.getVehiculo($routeParams.idVehiculo).then(function(res) { 136 focaAbmVehiculoService.getVehiculo($routeParams.idVehiculo).then(function(res) {
137 137
138 if (res.data) { 138 if (res.data) {
139 var vehiculoSeteado = getLSVehiculo(); 139 var vehiculoSeteado = getLSVehiculo();
140 if (vehiculoSeteado === false) { 140 if (vehiculoSeteado === false) {
141 $scope.transportistaStamp = ('00000' + res.data.transportista.COD).slice(-5); 141 $scope.transportistaStamp = ('00000' + res.data.transportista.COD).slice(-5);
142 $scope.transportistaStamp += ' - ' + res.data.transportista.NOM; 142 $scope.transportistaStamp += ' - ' + res.data.transportista.NOM;
143 143
144 $scope.vehiculo = res.data; 144 $scope.vehiculo = res.data;
145 $scope.$broadcast('addCabecera', { 145 $scope.$broadcast('addCabecera', {
146 label: 'Transportista:', 146 label: 'Transportista:',
147 valor: $scope.transportistaStamp 147 valor: $scope.transportistaStamp
148 }); 148 });
149 $scope.$broadcast('addCabecera', { 149 $scope.$broadcast('addCabecera', {
150 label: 'Unidad:', 150 label: 'Unidad:',
151 valor: res.data.codigo 151 valor: res.data.codigo
152 }); 152 });
153 } 153 }
154 154
155 focaAbmVehiculoService 155 focaAbmVehiculoService
156 .getCisternas($routeParams.idVehiculo) 156 .getCisternas($routeParams.idVehiculo)
157 .then(function(res) { 157 .then(function(res) {
158 $scope.cisternas = res; 158 $scope.cisternas = res;
159 $scope.$apply(); 159 $scope.$apply();
160 }); 160 });
161 } 161 }
162 }); 162 });
163 163
164 $scope.next = function(key) { 164 $scope.next = function(key) {
165 if (key === 13) $scope.focused++; 165 if (key === 13) $scope.focused++;
166 }; 166 };
167 167
168 $scope.salir = function() { 168 $scope.salir = function() {
169 if (!$scope.formVehiculo.$pristine && focaAbmVehiculoService.cisternasPristine) { 169 if (!$scope.formVehiculo.$pristine && focaAbmVehiculoService.cisternasPristine) {
170 focaModalService.confirm( 170 focaModalService.confirm(
171 '¿Está seguro de que desea salir? Se perderán todos los datos cargados.' 171 '¿Está seguro de que desea salir? Se perderán todos los datos cargados.'
172 ).then(function(data) { 172 ).then(function(data) {
173 if (data) { 173 if (data) {
174 $location.path('/vehiculo'); 174 $location.path('/vehiculo');
175 } 175 }
176 }); 176 });
177 } else { 177 } else {
178 $location.path('/vehiculo'); 178 $location.path('/vehiculo');
179 } 179 }
180 }; 180 };
181 181
182 $scope.editar = function(key, cisterna) { 182 $scope.editar = function(key, cisterna) {
183 if (key === 'nuevo') { 183 if (key === 'nuevo') {
184 $scope.cisterna = {}; 184 $scope.cisterna = {};
185 $scope.creando = true; 185 $scope.creando = true;
186 } else { 186 } else {
187 $scope.cisterna = cisterna; 187 $scope.cisterna = cisterna;
188 $scope.creando = true; 188 $scope.creando = true;
189 } 189 }
190 }; 190 };
191 191
192 $scope.seleccionarUnidadMedida = function() { 192 $scope.seleccionarUnidadMedida = function() {
193 var modalInstance = $uibModal.open( 193 var modalInstance = $uibModal.open(
194 { 194 {
195 ariaLabelledBy: 'Busqueda de Unidades de medida', 195 ariaLabelledBy: 'Busqueda de Unidades de medida',
196 templateUrl: 'modal-unidad-medida.html', 196 templateUrl: 'modal-unidad-medida.html',
197 controller: 'focaModalUnidadMedidaCtrl', 197 controller: 'focaModalUnidadMedidaCtrl',
198 size: 'lg' 198 size: 'lg'
199 } 199 }
200 ); 200 );
201 modalInstance.result.then(function(unidaMedida) { 201 modalInstance.result.then(function(unidaMedida) {
202 $scope.cisterna.idUnidadMedida = unidaMedida.ID; 202 $scope.cisterna.idUnidadMedida = unidaMedida.ID;
203 $scope.cisterna.unidadMedida = unidaMedida; 203 $scope.cisterna.unidadMedida = unidaMedida;
204 }); 204 });
205 }; 205 };
206 206
207 $scope.guardar = function(key) { 207 $scope.guardar = function(key) {
208 208
209 key = (typeof key === 'undefined') ? 13 : key; 209 key = (typeof key === 'undefined') ? 13 : key;
210 210
211 if (key === 13) { 211 if (key === 13) {
212 212
213 if ($scope.formVehiculo.$pristine && focaAbmVehiculoService.cisternasPristine) { 213 if ($scope.formVehiculo.$pristine && focaAbmVehiculoService.cisternasPristine) {
214 $scope.salir(); 214 $scope.salir();
215 return; 215 return;
216 } 216 }
217 217
218 if (!$scope.vehiculo.codigo) { 218 if (!$scope.vehiculo.codigo) {
219 focaModalService.alert('Ingrese unidad'); 219 focaModalService.alert('Ingrese unidad');
220 return; 220 return;
221 } else if (!$scope.vehiculo.tractor) { 221 } else if (!$scope.vehiculo.tractor) {
222 focaModalService.alert('Ingrese dominio tractor'); 222 focaModalService.alert('Ingrese dominio tractor');
223 return; 223 return;
224 } else if (!$scope.vehiculo.semi) { 224 } else if (!$scope.vehiculo.semi) {
225 focaModalService.alert('Ingrese dominio semi'); 225 focaModalService.alert('Ingrese dominio semi');
226 return; 226 return;
227 } else if (!$scope.vehiculo.capacidad) { 227 } else if (!$scope.vehiculo.capacidad) {
228 focaModalService.alert('Ingrese capacidad total'); 228 focaModalService.alert('Ingrese capacidad total');
229 return; 229 return;
230 } 230 }
231 //Valida si existe numero de unidad 231 //Valida si existe numero de unidad
232 if (!validaTotalCargas() && !$scope.nuevo) { 232 if (!validaTotalCargas() && !$scope.nuevo) {
233 focaModalService.alert('La suma de las capacidades de las cisternas' + 233 focaModalService.alert('La suma de las capacidades de las cisternas' +
234 ' debe ser igual a la capacidad total del vehículo'); 234 ' debe ser igual a la capacidad total del vehículo');
235 return; 235 return;
236 } 236 }
237 validaCodigoUnidad().then(function() { 237 validaCodigoUnidad().then(function() {
238 delete $scope.vehiculo.transportista; 238 delete $scope.vehiculo.transportista;
239 delete $scope.vehiculo.cisternas; 239 delete $scope.vehiculo.cisternas;
240 focaAbmVehiculoService.guardarVehiculo($scope.vehiculo) 240 focaAbmVehiculoService.guardarVehiculo($scope.vehiculo)
241 .then(function(res) { 241 .then(function(res) {
242 if ($scope.nuevo) { 242 if ($scope.nuevo) {
243 $location.path('/vehiculo/' + res.data.id + 243 $location.path('/vehiculo/' + res.data.id +
244 '/' + res.data.idTransportista); 244 '/' + res.data.idTransportista);
245 } else { 245 } else {
246 guardarCisternas().then(function() { 246 guardarCisternas().then(function() {
247 $location.path('/vehiculo'); 247 $location.path('/vehiculo');
248 }); 248 });
249 } 249 }
250 }); 250 });
251 }, function() { 251 }, function() {
252 focaModalService.alert('Código de unidad existente'); 252 focaModalService.alert('Código de unidad existente');
253 }); 253 });
254 } 254 }
255 }; 255 };
256 256
257 //Agregar propiedades de cisterna 257 //Agregar propiedades de cisterna
258 $scope.$watch('vehiculo', function(newValue) { 258 $scope.$watch('vehiculo', function(newValue) {
259 focaBotoneraLateralService.setPausarData({ 259 focaBotoneraLateralService.setPausarData({
260 label:'vehiculo', 260 label:'vehiculo',
261 val: { 261 val: {
262 codigo: newValue.codigo, 262 codigo: newValue.codigo,
263 tractor: newValue.tractor, 263 tractor: newValue.tractor,
264 semi: newValue.semi, 264 semi: newValue.semi,
265 capacidad: newValue.capacidad, 265 capacidad: newValue.capacidad,
266 idVehiculo: newValue.idVehiculo, 266 idVehiculo: newValue.idVehiculo,
267 idTransportista: newValue.idTransportista 267 idTransportista: newValue.idTransportista
268 } 268 }
269 }); 269 });
270 }, true); 270 }, true);
271 271
272 function getLSVehiculo() { 272 function getLSVehiculo() {
273 var vehiculo = JSON.parse($localStorage.vehiculo|| null); 273 var vehiculo = JSON.parse($localStorage.vehiculo|| null);
274 if (vehiculo) { 274 if (vehiculo) {
275 setearVehiculo(vehiculo); 275 setearVehiculo(vehiculo);
276 delete $localStorage.vehiculo; 276 delete $localStorage.vehiculo;
277 return true; 277 return true;
278 } 278 }
279 return false; 279 return false;
280 } 280 }
281 281
282 function setearVehiculo(vehiculo) { 282 function setearVehiculo(vehiculo) {
283 $scope.vehiculo = vehiculo; 283 $scope.vehiculo = vehiculo;
284 $scope.$broadcast('addCabecera', { 284 $scope.$broadcast('addCabecera', {
285 label: 'Vehiculo:', 285 label: 'Vehiculo:',
286 valor: $filter('rellenarDigitos')(vehiculo.codigo) + ' - ' 286 valor: $filter('rellenarDigitos')(vehiculo.codigo) + ' - '
287 }); 287 });
288 } 288 }
289 289
290 $scope.solicitarConfirmacionCisterna = function(cisterna, idx) { 290 $scope.solicitarConfirmacionCisterna = function(cisterna, idx) {
291 focaModalService.confirm('¿Está seguro que desea borrar la cisterna ' + 291 focaModalService.confirm('¿Está seguro que desea borrar la cisterna ' +
292 cisterna.id + ' ' + cisterna.codigo + ' ?').then( 292 cisterna.id + ' ' + cisterna.codigo + ' ?').then(
293 function(data) { 293 function(data) {
294 if (data) { 294 if (data) {
295 focaAbmVehiculoService.deleteCisterna(idx); 295 focaAbmVehiculoService.deleteCisterna(idx);
296 focaAbmVehiculoService 296 focaAbmVehiculoService
297 .getCisternas($routeParams.idVehiculo) 297 .getCisternas($routeParams.idVehiculo)
298 .then(function(res) { 298 .then(function(res) {
299 $scope.cisternas = res; 299 $scope.cisternas = res;
300 }); 300 });
301 } 301 }
302 } 302 }
303 ); 303 );
304 }; 304 };
305 305
306 function validaCodigoUnidad() { 306 function validaCodigoUnidad() {
307 return new Promise(function(resolve, reject) { 307 return new Promise(function(resolve, reject) {
308 focaAbmVehiculoService 308 focaAbmVehiculoService
309 .getVehiculosPorTransportista(parseInt($routeParams.idTransportista)) 309 .getVehiculosPorTransportista(parseInt($routeParams.idTransportista))
310 .then(function(res) { 310 .then(function(res) {
311 //Valida si existe numero de unidad 311 //Valida si existe numero de unidad
312 var existe = res.data.filter(function(vehiculo) { 312 var existe = res.data.filter(function(vehiculo) {
313 return vehiculo.codigo === $scope.vehiculo.codigo && 313 return vehiculo.codigo === $scope.vehiculo.codigo &&
314 vehiculo.id !== $scope.vehiculo.id; 314 vehiculo.id !== $scope.vehiculo.id;
315 }); 315 });
316 316
317 if (existe.length) { 317 if (existe.length) {
318 reject(existe); 318 reject(existe);
319 } else { 319 } else {
320 resolve(); 320 resolve();
321 } 321 }
322 }); 322 });
323 }); 323 });
324 } 324 }
325 325
326 function validaTotalCargas() { 326 function validaTotalCargas() {
327 var total = 0; 327 var total = 0;
328 $scope.cisternas.forEach(function(cisterna) { 328 $scope.cisternas.forEach(function(cisterna) {
329 if (!cisterna.desactivado) { 329 if (!cisterna.desactivado) {
330 total += parseInt(cisterna.capacidad); 330 total += parseInt(cisterna.capacidad);
331 } 331 }
332 }); 332 });
333 return $scope.vehiculo.capacidad === total; 333 return $scope.vehiculo.capacidad === total;
334 } 334 }
335 function guardarCisternas() { 335 function guardarCisternas() {
336 var cisternas = $scope.cisternas.map(function(cisterna) { 336 var cisternas = $scope.cisternas.map(function(cisterna) {
337 return { 337 return {
338 id: cisterna.id, 338 id: cisterna.id,
339 capacidad: parseFloat(cisterna.capacidad), 339 capacidad: parseFloat(cisterna.capacidad),
340 codigo: cisterna.codigo, 340 codigo: cisterna.codigo,
341 idUnidadMedida: cisterna.idUnidadMedida, 341 idUnidadMedida: cisterna.idUnidadMedida,
342 idVehiculo: $routeParams.idVehiculo, 342 idVehiculo: $routeParams.idVehiculo,
343 desactivado: cisterna.desactivado 343 desactivado: cisterna.desactivado
344 }; 344 };
345 }); 345 });
346 346
347 return focaAbmVehiculoService.guardarCisternas(cisternas); 347 return focaAbmVehiculoService.guardarCisternas(cisternas);
348 } 348 }
349 349
350 $scope.guardarCisterna = function() { 350 $scope.guardarCisterna = function() {
351 if (!$scope.cisterna.codigo) { 351 if (!$scope.cisterna.codigo) {
352 focaModalService.alert('Ingrese codigo de cisterna'); 352 focaModalService.alert('Ingrese codigo de cisterna');
353 return; 353 return;
354 } else if (!$scope.cisterna.capacidad) { 354 } else if (!$scope.cisterna.capacidad) {
355 focaModalService.alert('Ingrese capacidad'); 355 focaModalService.alert('Ingrese capacidad');
356 return; 356 return;
357 } else if (!$scope.cisterna.idUnidadMedida) { 357 } else if (!$scope.cisterna.idUnidadMedida) {
358 focaModalService.alert('Ingrese unidad de medida'); 358 focaModalService.alert('Ingrese unidad de medida');
359 return; 359 return;
360 } 360 }
361 $scope.creando = false; 361 $scope.creando = false;
362 if (!$scope.cisterna.id) { 362 if (!$scope.cisterna.id) {
363 validaCodigo() 363 validaCodigo()
364 .then(function() { 364 .then(function() {
365 saveCisterna(); 365 saveCisterna();
366 }, function(err) { 366 }, function(err) {
367 focaModalService.alert(err); 367 focaModalService.alert(err);
368 }); 368 });
369 } else { 369 } else {
370 saveCisterna(); 370 saveCisterna();
371 } 371 }
372 }; 372 };
373 373
374 function saveCisterna () { 374 function saveCisterna () {
375 $scope.cisterna.idVehiculo = parseInt($routeParams.idVehiculo); 375 $scope.cisterna.idVehiculo = parseInt($routeParams.idVehiculo);
376 delete $scope.cisterna.vehiculo; 376 delete $scope.cisterna.vehiculo;
377 var auxCisternas = $scope.cisternas; 377 var auxCisternas = $scope.cisternas;
378 if (!$scope.cisterna.id) { 378 if (!$scope.cisterna.id) {
379 auxCisternas.push($scope.cisterna); 379 auxCisternas.push($scope.cisterna);
380 } 380 }
381 guardarCisternas(auxCisternas) 381 guardarCisternas(auxCisternas)
382 .then(function() { 382 .then(function() {
383 focaAbmVehiculoService 383 focaAbmVehiculoService
384 .getCisternas($routeParams.idVehiculo) 384 .getCisternas($routeParams.idVehiculo)
385 .then(function(res) { 385 .then(function(res) {
386 $scope.cisternas = res; 386 $scope.cisternas = res;
387 $scope.cisterna = {}; 387 $scope.cisterna = {};
388 }); 388 });
389 }) 389 })
390 .catch(function (err) { 390 .catch(function (err) {
391 console.log('Err: ', err); 391 console.log('Err: ', err);
392 }); 392 });
393 } 393 }
394 394
395 if ($routeParams.idx !== -1) { 395 if ($routeParams.idx !== -1) {
396 $scope.cisterna = [$routeParams.idx]; 396 $scope.cisterna = [$routeParams.idx];
397 focaAbmVehiculoService 397 focaAbmVehiculoService
398 .getCisternas($routeParams.idVehiculo) 398 .getCisternas($routeParams.idVehiculo)
399 .then(function(res) { 399 .then(function(res) {
400 $scope.cisterna = res[$routeParams.idx]; 400 $scope.cisterna = res[$routeParams.idx];
401 }); 401 });
402 } 402 }
403 403
404 function validaCodigo() { 404 function validaCodigo() {
405 return new Promise(function(resolve, reject) { 405 return new Promise(function(resolve, reject) {
406 focaAbmVehiculoService 406 focaAbmVehiculoService
407 .getCisternas($routeParams.idVehiculo) 407 .getCisternas($routeParams.idVehiculo)
408 .then(function(res) { 408 .then(function(res) {
409 var cisternas = res; 409 var cisternas = res;
410 var totalCargado = 0; 410 var totalCargado = 0;
411 cisternas.forEach(function(cisterna, idx) { 411 cisternas.forEach(function(cisterna, idx) {
412 //SI EL CODIGO YA EXISTE 412 //SI EL CODIGO YA EXISTE
413 if (cisterna.codigo === $scope.cisterna.codigo && 413 if (cisterna.codigo === $scope.cisterna.codigo &&
414 idx !== $routeParams.idx && 414 idx !== $routeParams.idx &&
415 !cisterna.desactivado) { 415 !cisterna.desactivado) {
416 reject('Código de cisterna existente'); 416 reject('Código de cisterna existente');
417 } 417 }
418 if (idx !== $routeParams.idx && 418 if (idx !== $routeParams.idx &&
419 !cisterna.desactivado) { 419 !cisterna.desactivado) {
420 totalCargado += cisterna.capacidad; 420 totalCargado += cisterna.capacidad;
421 } 421 }
422 }); 422 });
423 resolve(); 423 resolve();
424 }); 424 });
425 }); 425 });
426 } 426 }
427 } 427 }
428 ]); 428 ]);
429 429