Commit 2a229bdfd558ffa5a033f73aa423c93b009426bc

Authored by Luigi
1 parent f23f33bcfb
Exists in master

Nombre e ID transportista

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