Commit 701f6c9c2e89f2a090d831fc491e2a9be4cd1685

Authored by Luigi
1 parent 107f38cfbc
Exists in master

Arreglo npm test

Showing 2 changed files with 45 additions and 4 deletions   Show diff stats
spec/controllerSpec.js
1 describe('Controladores abm vehículo', function() { 1 describe('Controladores abm vehículo', function() {
2 2
3 var $controller; 3 var $controller;
4 4
5 beforeEach(function() { 5 beforeEach(function() {
6 module('focaAbmVehiculo'); 6 module('focaAbmVehiculo');
7 inject(function(_$controller_) { 7 inject(function(_$controller_) {
8 $controller = _$controller_; 8 $controller = _$controller_;
9 }); 9 });
10 }); 10 });
11 11
12 describe('Controlador focaAbmVehiculosController', function() { 12 describe('Controlador focaAbmVehiculosController', function() {
13 13
14 it('Existe el controller focaAbmVehiculosController', function() { 14 it('Existe el controller focaAbmVehiculosController', function() {
15 //arrange 15 //arrange
16 var controller = $controller('focaAbmVehiculosController', { 16 var controller = $controller('focaAbmVehiculosController', {
17 $scope: {}, 17 $scope: {},
18 focaAbmVehiculoService: { 18 focaAbmVehiculoService: {
19 cleanCisternas: function() { return;}, 19 cleanCisternas: function() { return;},
20 transportistaSeleccionado: {} 20 transportistaSeleccionado: {}
21 }, 21 },
22 $location: {}, 22 $location: {},
23 focaModalService: {}, 23 focaModalService: {},
24 $uibModal: {}, 24 $uibModal: {},
25 focaBotoneraLateralService: { 25 focaBotoneraLateralService: {
26 showSalir: function() { return; }, 26 showSalir: function() { return; },
27 showPausar: function() { return; }, 27 showPausar: function() { return; },
28 showCancelar: function() { return; }, 28 showCancelar: function() { return; },
29 showGuardar: function() { return; }, 29 showGuardar: function() { return; },
30 addCustomButton: function() { return; } 30 addCustomButton: function() { return; }
31 }, 31 },
32 $localStorage: {}, 32 $localStorage: {},
33 $timeout: {} 33 $timeout: {}
34 }); 34 });
35 35
36 //assert 36 //assert
37 expect(typeof controller).toEqual('object'); 37 expect(typeof controller).toEqual('object');
38 }); 38 });
39 39
40 it('Crea fecha nueva', function() { 40 it('Crea fecha nueva', function() {
41 //arrange 41 //arrange
42 var scope = { 42 var scope = {
43 $watch: function() {} 43 $watch: function() {}
44 }; 44 };
45 var controller = $controller('focaAbmVehiculosController', { 45 var controller = $controller('focaAbmVehiculosController', {
46 $scope: scope, 46 $scope: scope,
47 focaAbmVehiculoService: { 47 focaAbmVehiculoService: {
48 cleanCisternas: function() { return;}, 48 cleanCisternas: function() { return;},
49 transportistaSeleccionado: {} 49 transportistaSeleccionado: {}
50 }, 50 },
51 $location: {}, 51 $location: {},
52 focaModalService: {}, 52 focaModalService: {},
53 $uibModal: {}, 53 $uibModal: {},
54 focaBotoneraLateralService: { 54 focaBotoneraLateralService: {
55 showSalir: function() { return; }, 55 showSalir: function() { return; },
56 showPausar: function() { return; }, 56 showPausar: function() { return; },
57 showCancelar: function() { return; }, 57 showCancelar: function() { return; },
58 showGuardar: function() { return; }, 58 showGuardar: function() { return; },
59 addCustomButton: function() { return; } 59 addCustomButton: function() { return; }
60 }, 60 },
61 $localStorage: {}, 61 $localStorage: {},
62 $timeout: {} 62 $timeout: {}
63 }); 63 });
64 64
65 //act 65 //act
66 var date = scope.now; 66 var date = scope.now;
67 67
68 //assert 68 //assert
69 expect(angular.isDate(date)).toBe(true); 69 expect(angular.isDate(date)).toBe(true);
70 }); 70 });
71 71
72 it('$scope setea botonera lateral', function() { 72 it('$scope setea botonera lateral', function() {
73 //arrange 73 //arrange
74 var scope = { 74 var scope = {
75 $watch: function() {} 75 $watch: function() {}
76 }; 76 };
77 var controller = $controller('focaAbmVehiculosController', { 77 var controller = $controller('focaAbmVehiculosController', {
78 $scope: scope, 78 $scope: scope,
79 focaAbmVehiculoService: { 79 focaAbmVehiculoService: {
80 cleanCisternas: function() { return;}, 80 cleanCisternas: function() { return;},
81 transportistaSeleccionado: {} 81 transportistaSeleccionado: {}
82 }, 82 },
83 $location: {}, 83 $location: {},
84 focaModalService: {}, 84 focaModalService: {},
85 $uibModal: {}, 85 $uibModal: {},
86 focaBotoneraLateralService: { 86 focaBotoneraLateralService: {
87 showSalir: function() { return; }, 87 showSalir: function() { return; },
88 showPausar: function() { return; }, 88 showPausar: function() { return; },
89 showCancelar: function() { return; }, 89 showCancelar: function() { return; },
90 showGuardar: function() { return; }, 90 showGuardar: function() { return; },
91 addCustomButton: function() { return; } 91 addCustomButton: function() { return; }
92 }, 92 },
93 $timeout: {}, 93 $timeout: {},
94 $localStorage: {} 94 $localStorage: {}
95 }); 95 });
96 96
97 //act 97 //act
98 var botonera = scope.botonera; 98 var botonera = scope.botonera;
99 99
100 //assert 100 //assert
101 expect(angular.isArray(botonera)).toBe(true); 101 expect(angular.isArray(botonera)).toBe(true);
102 }); 102 });
103 103
104 it('$scope.editar lleva a la ruta correcta', function() { 104 it('$scope.editar lleva a la ruta correcta', function() {
105 inject(function($location) { 105 inject(function($location) {
106 //arrange 106 //arrange
107 var scope = { 107 var scope = {
108 $watch: function() {} 108 $watch: function() {}
109 }; 109 };
110 var controller = $controller('focaAbmVehiculosController', { 110 var controller = $controller('focaAbmVehiculosController', {
111 $scope: scope, 111 $scope: scope,
112 focaAbmVehiculoService: { 112 focaAbmVehiculoService: {
113 cleanCisternas: function() { return;}, 113 cleanCisternas: function() { return;},
114 transportistaSeleccionado: {} 114 transportistaSeleccionado: {}
115 }, 115 },
116 $location: $location, 116 $location: $location,
117 focaModalService: {}, 117 focaModalService: {},
118 $uibModal: {}, 118 $uibModal: {},
119 focaBotoneraLateralService: { 119 focaBotoneraLateralService: {
120 showSalir: function() { return; }, 120 showSalir: function() { return; },
121 showPausar: function() { return; }, 121 showPausar: function() { return; },
122 showCancelar: function() { return; }, 122 showCancelar: function() { return; },
123 showGuardar: function() { return; }, 123 showGuardar: function() { return; },
124 addCustomButton: function() { return; } 124 addCustomButton: function() { return; }
125 }, 125 },
126 $timeout: {}, 126 $timeout: {},
127 $localStorage: {} 127 $localStorage: {}
128 }); 128 });
129 129
130 //act 130 //act
131 scope.editar(1); 131 scope.editar(1);
132 var esperado = '/vehiculo/' + 1 + '/' + scope.idTransportista; 132 var esperado = '/vehiculo/' + 1 + '/' + scope.idTransportista;
133 133
134 //assert 134 //assert
135 expect($location.url()).toEqual(esperado); 135 expect($location.url()).toEqual(esperado);
136 }); 136 });
137 }); 137 });
138 138
139 it('Solicita confirmacion', function() { 139 it('Solicita confirmacion', function() {
140 //arrange 140 //arrange
141 var scope = { 141 var scope = {
142 $watch: function() {} 142 $watch: function() {}
143 }; 143 };
144 var focaModalService = { 144 var focaModalService = {
145 confirm: function() {} 145 confirm: function() {}
146 }; 146 };
147 var controller = $controller('focaAbmVehiculosController', { 147 var controller = $controller('focaAbmVehiculosController', {
148 $scope: scope, 148 $scope: scope,
149 focaAbmVehiculoService: { 149 focaAbmVehiculoService: {
150 cleanCisternas: function() { return;}, 150 cleanCisternas: function() { return;},
151 transportistaSeleccionado: {} 151 transportistaSeleccionado: {}
152 }, 152 },
153 $location: {}, 153 $location: {},
154 focaModalService: focaModalService, 154 focaModalService: focaModalService,
155 $uibModal: {}, 155 $uibModal: {},
156 focaBotoneraLateralService: { 156 focaBotoneraLateralService: {
157 showSalir: function() { return; }, 157 showSalir: function() { return; },
158 showPausar: function() { return; }, 158 showPausar: function() { return; },
159 showCancelar: function() { return; }, 159 showCancelar: function() { return; },
160 showGuardar: function() { return; }, 160 showGuardar: function() { return; },
161 addCustomButton: function() { return; } 161 addCustomButton: function() { return; }
162 }, 162 },
163 $timeout: {}, 163 $timeout: {},
164 $localStorage: {} 164 $localStorage: {}
165 }); 165 });
166 166
167 //act 167 //act
168 spyOn(focaModalService, 'confirm').and.returnValue({then: function() {}}); 168 spyOn(focaModalService, 'confirm').and.returnValue({then: function() {}});
169 scope.solicitarConfirmacion({id: 1, tractor: 'abc'}); 169 scope.solicitarConfirmacion({id: 1, tractor: 'abc'});
170 170
171 //assert 171 //assert
172 expect(focaModalService.confirm).toHaveBeenCalled(); 172 expect(focaModalService.confirm).toHaveBeenCalled();
173 }); 173 });
174 174
175 it('Elimina vehículo al confirmar', function(done) { 175 it('Elimina vehículo al confirmar', function(done) {
176 176
177 //arrange 177 //arrange
178 var scope = { 178 var scope = {
179 vehiculos: [] 179 vehiculos: []
180 }; 180 };
181 var focaModalService = { 181 var focaModalService = {
182 confirm: function() { return; } 182 confirm: function() { return; }
183 }; 183 };
184 var focaAbmVehiculoService = { 184 var focaAbmVehiculoService = {
185 cleanCisternas: function() { return;}, 185 cleanCisternas: function() { return;},
186 transportistaSeleccionado: {}, 186 transportistaSeleccionado: {},
187 deleteVehiculo: function() { return; } 187 deleteVehiculo: function() { return; }
188 }; 188 };
189 var controller = $controller('focaAbmVehiculosController', { 189 var controller = $controller('focaAbmVehiculosController', {
190 $scope: scope, 190 $scope: scope,
191 focaAbmVehiculoService: focaAbmVehiculoService, 191 focaAbmVehiculoService: focaAbmVehiculoService,
192 $location: {}, 192 $location: {},
193 focaModalService: focaModalService, 193 focaModalService: focaModalService,
194 $uibModal: {}, 194 $uibModal: {},
195 focaBotoneraLateralService: { 195 focaBotoneraLateralService: {
196 showSalir: function() { return; }, 196 showSalir: function() { return; },
197 showPausar: function() { return; }, 197 showPausar: function() { return; },
198 showCancelar: function() { return; }, 198 showCancelar: function() { return; },
199 showGuardar: function() { return; }, 199 showGuardar: function() { return; },
200 addCustomButton: function() { return; } 200 addCustomButton: function() { return; }
201 }, 201 },
202 $timeout: {}, 202 $timeout: {},
203 $localStorage: {} 203 $localStorage: {}
204 }); 204 });
205 var promesa = Promise.resolve(true); 205 var promesa = Promise.resolve(true);
206 206
207 //act 207 //act
208 spyOn(focaModalService, 'confirm').and.returnValue(promesa); 208 spyOn(focaModalService, 'confirm').and.returnValue(promesa);
209 spyOn(focaAbmVehiculoService, 'deleteVehiculo'); 209 spyOn(focaAbmVehiculoService, 'deleteVehiculo');
210 scope.solicitarConfirmacion({id: 1, tractor: 'abc'}); 210 scope.solicitarConfirmacion({id: 1, tractor: 'abc'});
211 211
212 //assert 212 //assert
213 promesa.then( 213 promesa.then(
214 function() { 214 function() {
215 expect(focaAbmVehiculoService.deleteVehiculo).toHaveBeenCalled(); 215 expect(focaAbmVehiculoService.deleteVehiculo).toHaveBeenCalled();
216 done(); 216 done();
217 } 217 }
218 ); 218 );
219 }); 219 });
220 220
221 it('Se selecciona transportista', function() { 221 it('Se selecciona transportista', function() {
222 //arrange 222 //arrange
223 var scope = { 223 var scope = {
224 $watch: function() {} 224 $watch: function() {}
225 }; 225 };
226 var focaModalService = { 226 var focaModalService = {
227 modal: function() {} 227 modal: function() {}
228 }; 228 };
229 var controller = $controller('focaAbmVehiculosController', { 229 var controller = $controller('focaAbmVehiculosController', {
230 $scope: scope, 230 $scope: scope,
231 focaAbmVehiculoService: { 231 focaAbmVehiculoService: {
232 cleanCisternas: function() { return;}, 232 cleanCisternas: function() { return;},
233 transportistaSeleccionado: {} 233 transportistaSeleccionado: {}
234 }, 234 },
235 $location: {}, 235 $location: {},
236 focaModalService: focaModalService, 236 focaModalService: focaModalService,
237 $uibModal: {}, 237 $uibModal: {},
238 focaBotoneraLateralService: { 238 focaBotoneraLateralService: {
239 showSalir: function() { return; }, 239 showSalir: function() { return; },
240 showPausar: function() { return; }, 240 showPausar: function() { return; },
241 showCancelar: function() { return; }, 241 showCancelar: function() { return; },
242 showGuardar: function() { return; }, 242 showGuardar: function() { return; },
243 addCustomButton: function() { return; } 243 addCustomButton: function() { return; }
244 }, 244 },
245 $timeout: {}, 245 $timeout: {},
246 $localStorage: {} 246 $localStorage: {}
247 }); 247 });
248 248
249 //act 249 //act
250 spyOn(focaModalService, 'modal').and.returnValue({then: function() {}}); 250 spyOn(focaModalService, 'modal').and.returnValue({then: function() {}});
251 scope.seleccionarTransportista(); 251 scope.seleccionarTransportista();
252 252
253 //assert 253 //assert
254 expect(focaModalService.modal).toHaveBeenCalled(); 254 expect(focaModalService.modal).toHaveBeenCalled();
255 }); 255 });
256 256
257 it('Se setea el transportista seleccionado al service', function(done) { 257 it('Se setea el transportista seleccionado al service', function(done) {
258 inject(function($timeout) { 258 inject(function($timeout) {
259 259
260 //arrange 260 //arrange
261 var scope = { 261 var scope = {
262 $watch: function() {} 262 $watch: function() {}
263 }; 263 };
264 var focaModalService = { 264 var focaModalService = {
265 modal: function() { return; } 265 modal: function() { return; }
266 }; 266 };
267 var focaAbmVehiculoService = { 267 var focaAbmVehiculoService = {
268 cleanCisternas: function() { return;}, 268 cleanCisternas: function() { return;},
269 transportistaSeleccionado: {}, 269 transportistaSeleccionado: {},
270 deleteVehiculo: function() { return; }, 270 deleteVehiculo: function() { return; },
271 getVehiculosPorTransportista: function() { 271 getVehiculosPorTransportista: function() {
272 return { 272 return {
273 then: function() { return; } 273 then: function() { return; }
274 }; 274 };
275 } 275 }
276 }; 276 };
277 var controller = $controller('focaAbmVehiculosController', { 277 var controller = $controller('focaAbmVehiculosController', {
278 $scope: scope, 278 $scope: scope,
279 focaAbmVehiculoService: focaAbmVehiculoService, 279 focaAbmVehiculoService: focaAbmVehiculoService,
280 $location: {}, 280 $location: {},
281 focaModalService: focaModalService, 281 focaModalService: focaModalService,
282 $uibModal: {}, 282 $uibModal: {},
283 focaBotoneraLateralService: { 283 focaBotoneraLateralService: {
284 showSalir: function() { return; }, 284 showSalir: function() { return; },
285 showPausar: function() { return; }, 285 showPausar: function() { return; },
286 showCancelar: function() { return; }, 286 showCancelar: function() { return; },
287 showGuardar: function() { return; }, 287 showGuardar: function() { return; },
288 addCustomButton: function() { return; } 288 addCustomButton: function() { return; }
289 }, 289 },
290 $localStorage: {}, 290 $localStorage: {},
291 $timeout: $timeout 291 $timeout: $timeout
292 }); 292 });
293 var promesa = Promise.resolve({COD: '', NOM: ''}); 293 var promesa = Promise.resolve({COD: '', NOM: ''});
294 294
295 //act 295 //act
296 spyOn(focaModalService, 'modal').and.returnValue(promesa); 296 spyOn(focaModalService, 'modal').and.returnValue(promesa);
297 scope.seleccionarTransportista(); 297 scope.seleccionarTransportista();
298 298
299 //assert 299 //assert
300 promesa.then( 300 promesa.then(
301 function() { 301 function() {
302 expect(focaAbmVehiculoService.transportistaSeleccionado) 302 expect(focaAbmVehiculoService.transportistaSeleccionado)
303 .toEqual(jasmine.objectContaining({ 303 .toEqual(jasmine.objectContaining({
304 COD: '', NOM: '' 304 COD: '', NOM: ''
305 })); 305 }));
306 done(); 306 done();
307 } 307 }
308 ); 308 );
309 }); 309 });
310 }); 310 });
311 }); 311 });
312 312
313 describe('Controlador focaAbmVehiculoController', function() { 313 describe('Controlador focaAbmVehiculoController', function() {
314 314
315 var $timeout; 315 var $timeout;
316 beforeEach(inject(function(_$timeout_) { 316 beforeEach(inject(function(_$timeout_) {
317 $timeout = _$timeout_; 317 $timeout = _$timeout_;
318 })); 318 }));
319 319
320 it('Existe el controller focaAbmVehiculoController', function() { 320 it('Existe el controller focaAbmVehiculoController', function() {
321 321
322 //arrange 322 //arrange
323 var controller = $controller('focaAbmVehiculoController', { 323 var controller = $controller('focaAbmVehiculoController', {
324 $scope: { 324 $scope: {
325 $watch: function() {} 325 $watch: function() {}
326 }, 326 },
327 focaAbmVehiculoService: { 327 focaAbmVehiculoService: {
328 getCisternas: function() {
329 return {
330 then: function() { return; }
331 };
332 },
328 getVehiculo: function() { 333 getVehiculo: function() {
329 return { 334 return {
330 then: function() { return; } 335 then: function() { return; }
331 }; 336 };
332 } 337 }
333 }, 338 },
334 $routeParams: {}, 339 $routeParams: {},
335 $location: {}, 340 $location: {},
336 $uibModal: {}, 341 $uibModal: {},
337 focaModalService: {}, 342 focaModalService: {},
338 $timeout: $timeout, 343 $timeout: $timeout,
339 focaBotoneraLateralService: { 344 focaBotoneraLateralService: {
340 showSalir: function() { return; }, 345 showSalir: function() { return; },
341 showPausar: function() { return; }, 346 showPausar: function() { return; },
342 showCancelar: function() { return; }, 347 showCancelar: function() { return; },
343 showGuardar: function() { return; }, 348 showGuardar: function() { return; },
344 addCustomButton: function() { return; } 349 addCustomButton: function() { return; }
345 }, 350 },
346 $localStorage: {}, 351 $localStorage: {},
347 $window: {} 352 $window: {}
348 }); 353 });
349 354
350 //assert 355 //assert
351 expect(typeof controller).toEqual('object'); 356 expect(typeof controller).toEqual('object');
352 357
353 }); 358 });
354 359
355 it('Se busca el transportista cuando es nuevo', function() { 360 it('Se busca el transportista cuando es nuevo', function() {
356 361
357 //arrange 362 //arrange
358 var scope = { 363 var scope = {
359 $watch: function() {} 364 $watch: function() {}
360 }; 365 };
361 var focaAbmVehiculoService = { 366 var focaAbmVehiculoService = {
367
368 getCisternas: function() {
369 return {
370 then: function() { return; }
371 };
372 },
362 getVehiculo: function() { 373 getVehiculo: function() {
363 return { 374 return {
364 then: function() { return; } 375 then: function() { return; }
365 }; 376 };
366 }, 377 },
367 getTransportistaPorId: function() { return; } 378 getTransportistaPorId: function() { return; }
368 }; 379 };
369 380
370 spyOn(focaAbmVehiculoService, 'getTransportistaPorId') 381 spyOn(focaAbmVehiculoService, 'getTransportistaPorId')
371 .and.returnValue({then: function() { return; }}); 382 .and.returnValue({then: function() { return; }});
372 383
373 //act 384 //act
374 var controller = $controller('focaAbmVehiculoController', { 385 var controller = $controller('focaAbmVehiculoController', {
375 $scope: scope, 386 $scope: scope,
376 focaAbmVehiculoService: focaAbmVehiculoService, 387 focaAbmVehiculoService: focaAbmVehiculoService,
377 $routeParams: { 388 $routeParams: {
378 idVehiculo: '0' 389 idVehiculo: '0'
379 }, 390 },
380 $location: {}, 391 $location: {},
381 $uibModal: {}, 392 $uibModal: {},
382 focaModalService: {}, 393 focaModalService: {},
383 $timeout: $timeout, 394 $timeout: $timeout,
384 focaBotoneraLateralService: { 395 focaBotoneraLateralService: {
385 showSalir: function() { return; }, 396 showSalir: function() { return; },
386 showPausar: function() { return; }, 397 showPausar: function() { return; },
387 showCancelar: function() { return; }, 398 showCancelar: function() { return; },
388 showGuardar: function() { return; }, 399 showGuardar: function() { return; },
389 addCustomButton: function() { return; } 400 addCustomButton: function() { return; }
390 }, 401 },
391 $localStorage: {}, 402 $localStorage: {},
392 $window: {} 403 $window: {}
393 }); 404 });
394 405
395 //assert 406 //assert
396 expect(focaAbmVehiculoService.getTransportistaPorId).toHaveBeenCalled(); 407 expect(focaAbmVehiculoService.getTransportistaPorId).toHaveBeenCalled();
397 }); 408 });
398 409
399 it('No se busca el transportista cuando es nuevo', function() { 410 it('No se busca el transportista cuando es nuevo', function() {
400 411
401 //arrange 412 //arrange
402 var scope = { 413 var scope = {
403 $watch: function() {} 414 $watch: function() {}
404 }; 415 };
405 var focaAbmVehiculoService = { 416 var focaAbmVehiculoService = {
417
418 getCisternas: function() {
419 return {
420 then: function() { return; }
421 };
422 },
423
406 getVehiculo: function() { 424 getVehiculo: function() {
407 return { 425 return {
408 then: function() { return; } 426 then: function() { return; }
409 }; 427 };
410 }, 428 },
411 getTransportistaPorId: function() { return; } 429 getTransportistaPorId: function() { return; }
412 }; 430 };
413 431
414 spyOn(focaAbmVehiculoService, 'getTransportistaPorId') 432 spyOn(focaAbmVehiculoService, 'getTransportistaPorId')
415 .and.returnValue({then: function() { return; }}); 433 .and.returnValue({then: function() { return; }});
416 434
417 //act 435 //act
418 var controller = $controller('focaAbmVehiculoController', { 436 var controller = $controller('focaAbmVehiculoController', {
419 $scope: scope, 437 $scope: scope,
420 focaAbmVehiculoService: focaAbmVehiculoService, 438 focaAbmVehiculoService: focaAbmVehiculoService,
421 $routeParams: {}, 439 $routeParams: {},
422 $location: {}, 440 $location: {},
423 $uibModal: {}, 441 $uibModal: {},
424 focaModalService: {}, 442 focaModalService: {},
425 $timeout: $timeout, 443 $timeout: $timeout,
426 focaBotoneraLateralService: { 444 focaBotoneraLateralService: {
427 showSalir: function() { return; }, 445 showSalir: function() { return; },
428 showPausar: function() { return; }, 446 showPausar: function() { return; },
429 showCancelar: function() { return; }, 447 showCancelar: function() { return; },
430 showGuardar: function() { return; }, 448 showGuardar: function() { return; },
431 addCustomButton: function() { return; } 449 addCustomButton: function() { return; }
432 }, 450 },
433 $localStorage: {}, 451 $localStorage: {},
434 $window: {} 452 $window: {}
435 }); 453 });
436 454
437 //assert 455 //assert
438 expect(focaAbmVehiculoService.getTransportistaPorId).not.toHaveBeenCalled(); 456 expect(focaAbmVehiculoService.getTransportistaPorId).not.toHaveBeenCalled();
439 }); 457 });
440 458
441 it('Cancelar lleva a la ruta /vehiculo', function() { 459 it('Cancelar lleva a la ruta /vehiculo', function() {
442 460
443 inject(function($location) { 461 inject(function($location) {
444 //arrange 462 //arrange
445 var scope = { 463 var scope = {
446 $watch: function() {}, 464 $watch: function() {},
447 cancelar: function() {}, 465 cancelar: function() {},
448 formVehiculo: { 466 formVehiculo: {
449 $pristine: true 467 $pristine: true
450 } 468 }
451 }; 469 };
452 var controller = $controller('focaAbmVehiculoController', { 470 var controller = $controller('focaAbmVehiculoController', {
453 $scope: scope, 471 $scope: scope,
454 focaAbmVehiculoService: { 472 focaAbmVehiculoService: {
473 getCisternas: function() {
474 return {
475 then: function() { return; }
476 };
477 },
455 getVehiculo: function() { 478 getVehiculo: function() {
456 return { 479 return {
457 then: function() { return; } 480 then: function() { return; }
458 }; 481 };
459 } 482 }
460 }, 483 },
461 $routeParams: {}, 484 $routeParams: {},
462 $location: $location, 485 $location: $location,
463 $uibModal: {}, 486 $uibModal: {},
464 focaModalService: {}, 487 focaModalService: {},
465 $timeout: $timeout, 488 $timeout: $timeout,
466 focaBotoneraLateralService: { 489 focaBotoneraLateralService: {
467 showSalir: function() { return; }, 490 showSalir: function() { return; },
468 showPausar: function() { return; }, 491 showPausar: function() { return; },
469 showCancelar: function() { return; }, 492 showCancelar: function() { return; },
470 showGuardar: function() { return; }, 493 showGuardar: function() { return; },
471 addCustomButton: function() { return; } 494 addCustomButton: function() { return; }
472 }, 495 },
473 $localStorage: {}, 496 $localStorage: {},
474 $window: {}, 497 $window: {},
475 498
476 }); 499 });
477 500
478 //act 501 //act
479 scope.salir(); 502 scope.salir();
480 503
481 //assert 504 //assert
482 expect($location.url()).toEqual('/vehiculo'); 505 expect($location.url()).toEqual('/vehiculo');
483 }); 506 });
484 }); 507 });
485 508
486 it('Editar lleva a la ruta correcta cuando recibe variable key', function() { 509 it('Editar lleva a la ruta correcta cuando recibe variable key', function() {
487 510
488 inject(function($location) { 511 inject(function($location) {
489 //arrange 512 //arrange
490 var scope = { 513 var scope = {
491 $watch: function() {} 514 $watch: function() {}
492 }; 515 };
493 var controller = $controller('focaAbmVehiculoController', { 516 var controller = $controller('focaAbmVehiculoController', {
494 $scope: scope, 517 $scope: scope,
495 focaAbmVehiculoService: { 518 focaAbmVehiculoService: {
519 getCisternas: function() {
520 return {
521 then: function() { return; }
522 };
523 },
496 getVehiculo: function() { 524 getVehiculo: function() {
497 return { 525 return {
498 then: function() { return; } 526 then: function() { return; }
499 }; 527 };
500 } 528 }
501 }, 529 },
502 $routeParams: { 530 $routeParams: {
503 idVehiculo: 1 531 idVehiculo: 1
504 }, 532 },
505 $location: $location, 533 $location: $location,
506 $uibModal: {}, 534 $uibModal: {},
507 focaModalService: {}, 535 focaModalService: {},
508 $timeout: $timeout, 536 $timeout: $timeout,
509 focaBotoneraLateralService: { 537 focaBotoneraLateralService: {
510 showSalir: function() { return; }, 538 showSalir: function() { return; },
511 showPausar: function() { return; }, 539 showPausar: function() { return; },
512 showCancelar: function() { return; }, 540 showCancelar: function() { return; },
513 showGuardar: function() { return; }, 541 showGuardar: function() { return; },
514 addCustomButton: function() { return; } 542 addCustomButton: function() { return; }
515 }, 543 },
516 $localStorage: {}, 544 $localStorage: {},
517 $window: {} 545 $window: {}
518 }); 546 });
519 547
520 //act 548 //act
521 scope.editar('testing'); 549 scope.editar('testing');
522 var esperado = '/vehiculo/1/cisterna/testing'; 550 var esperado = '/vehiculo/1/cisterna/testing';
523 551
524 //assert 552 //assert
525 expect($location.url()).toEqual(esperado); 553 expect($location.url()).toEqual(esperado);
526 }); 554 });
527 }); 555 });
528 556
529 it('Editar lleva a la ruta correcta cuando no recibe variable key', function() { 557 it('Editar lleva a la ruta correcta cuando no recibe variable key', function() {
530 558
531 inject(function($location) { 559 inject(function($location) {
532 //arrange 560 //arrange
533 var scope = { 561 var scope = {
534 $watch: function() {} 562 $watch: function() {}
535 }; 563 };
536 var controller = $controller('focaAbmVehiculoController', { 564 var controller = $controller('focaAbmVehiculoController', {
537 $scope: scope, 565 $scope: scope,
538 focaAbmVehiculoService: { 566 focaAbmVehiculoService: {
567 getCisternas: function() {
568 return {
569 then: function() { return; }
570 };
571 },
539 getVehiculo: function() { 572 getVehiculo: function() {
540 return { 573 return {
541 then: function() { return; } 574 then: function() { return; }
542 }; 575 };
543 } 576 }
544 }, 577 },
545 $routeParams: { 578 $routeParams: {
546 idVehiculo: 1 579 idVehiculo: 1
547 }, 580 },
548 $location: $location, 581 $location: $location,
549 $uibModal: {}, 582 $uibModal: {},
550 focaModalService: {}, 583 focaModalService: {},
551 $timeout: $timeout, 584 $timeout: $timeout,
552 focaBotoneraLateralService: { 585 focaBotoneraLateralService: {
553 showSalir: function() { return; }, 586 showSalir: function() { return; },
554 showPausar: function() { return; }, 587 showPausar: function() { return; },
555 showCancelar: function() { return; }, 588 showCancelar: function() { return; },
556 showGuardar: function() { return; }, 589 showGuardar: function() { return; },
557 addCustomButton: function() { return; } 590 addCustomButton: function() { return; }
558 }, 591 },
559 $localStorage: {}, 592 $localStorage: {},
560 $window: {} 593 $window: {}
561 }); 594 });
562 595
563 //act 596 //act
564 scope.editar(); 597 scope.editar();
565 var esperado = '/vehiculo/1/cisterna/0/'; 598 var esperado = '/vehiculo/1/cisterna/0/';
566 599
567 //assert 600 //assert
568 expect($location.url()).toEqual(esperado); 601 expect($location.url()).toEqual(esperado);
569 }); 602 });
570 }); 603 });
571 604
572 it('$scope.solicitarConfirmacionCisterna ejecuta modal de confirmación', function() { 605 it('$scope.solicitarConfirmacionCisterna ejecuta modal de confirmación', function() {
573 606
574 //arrange 607 //arrange
575 var focaModalService = { 608 var focaModalService = {
576 confirm: function() {} 609 confirm: function() {}
577 }; 610 };
578 var scope = { 611 var scope = {
579 $watch: function() {} 612 $watch: function() {}
580 }; 613 };
581 var controller = $controller('focaAbmVehiculoController', { 614 var controller = $controller('focaAbmVehiculoController', {
582 $scope: scope, 615 $scope: scope,
583 focaAbmVehiculoService: { 616 focaAbmVehiculoService: {
617 getCisternas: function() {
618 return {
619 then: function() { return; }
620 };
621 },
584 getVehiculo: function() { 622 getVehiculo: function() {
585 return { 623 return {
586 then: function() { return; } 624 then: function() { return; }
587 }; 625 };
588 } 626 }
589 }, 627 },
590 $routeParams: { 628 $routeParams: {
591 idVehiculo: 1 629 idVehiculo: 1
592 }, 630 },
593 $location: {}, 631 $location: {},
594 $uibModal: {}, 632 $uibModal: {},
595 focaModalService: focaModalService, 633 focaModalService: focaModalService,
596 $timeout: $timeout, 634 $timeout: $timeout,
597 focaBotoneraLateralService: { 635 focaBotoneraLateralService: {
598 showSalir: function() { return; }, 636 showSalir: function() { return; },
599 showPausar: function() { return; }, 637 showPausar: function() { return; },
600 showCancelar: function() { return; }, 638 showCancelar: function() { return; },
601 showGuardar: function() { return; }, 639 showGuardar: function() { return; },
602 addCustomButton: function() { return; } 640 addCustomButton: function() { return; }
603 }, 641 },
604 $localStorage: {}, 642 $localStorage: {},
605 $window: {} 643 $window: {},
606 }); 644 });
607 645
608 //act 646 //act
609 spyOn(focaModalService, 'confirm').and.returnValue({then: function() {}}); 647 spyOn(focaModalService, 'confirm').and.returnValue({then: function() {}});
610 scope.solicitarConfirmacionCisterna({id: 1, codigo: 'abc'}); 648 scope.solicitarConfirmacionCisterna({id: 1, codigo: 'abc'});
611 649
612 //assert 650 //assert
613 expect(focaModalService.confirm).toHaveBeenCalled(); 651 expect(focaModalService.confirm).toHaveBeenCalled();
614 }); 652 });
615 653
616 it('Elimina y obtiene cisternas al dar confirmar', function(done) { 654 it('Elimina y obtiene cisternas al dar confirmar', function(done) {
617 655
618 //arrange 656 //arrange
619 var scope = { 657 var scope = {
620 $watch: function() {} 658 $watch: function() {}
621 }; 659 };
622 var focaModalService = { 660 var focaModalService = {
623 confirm: function() {} 661 confirm: function() {}
624 }; 662 };
625 var focaAbmVehiculoService = { 663 var focaAbmVehiculoService = {
626 getVehiculo: function() { 664 getVehiculo: function() {
627 return { 665 return {
628 then: function() { return; } 666 then: function() { return; }
629 }; 667 };
630 }, 668 },
631 getCisternas: function() { return; }, 669 getCisternas: function() {
670 return {
671 then: function() { return; }
672 };
673 },
632 deleteCisterna: function() { return; } 674 deleteCisterna: function() { return; }
633 }; 675 };
634 var controller = $controller('focaAbmVehiculoController', { 676 var controller = $controller('focaAbmVehiculoController', {
635 $scope: scope, 677 $scope: scope,
636 focaAbmVehiculoService: focaAbmVehiculoService, 678 focaAbmVehiculoService: focaAbmVehiculoService,
637 $routeParams: {}, 679 $routeParams: {},
638 $location: {}, 680 $location: {},
639 $uibModal: {}, 681 $uibModal: {},
640 focaModalService: focaModalService, 682 focaModalService: focaModalService,
641 $timeout: $timeout, 683 $timeout: $timeout,
642 focaBotoneraLateralService: { 684 focaBotoneraLateralService: {
643 showSalir: function() { return; }, 685 showSalir: function() { return; },
644 showPausar: function() { return; }, 686 showPausar: function() { return; },
645 showCancelar: function() { return; }, 687 showCancelar: function() { return; },
646 showGuardar: function() { return; }, 688 showGuardar: function() { return; },
647 addCustomButton: function() { return; } 689 addCustomButton: function() { return; }
648 }, 690 },
649 $localStorage: {}, 691 $localStorage: {},
650 $window: {} 692 $window: {}
651 }); 693 });
652 var promesa = Promise.resolve(true); 694 var promesa = Promise.resolve(true);
653 695
654 //act 696 //act
655 spyOn(focaModalService, 'confirm').and.returnValue(promesa); 697 spyOn(focaModalService, 'confirm').and.returnValue(promesa);
656 spyOn(focaAbmVehiculoService, 'deleteCisterna'); 698 spyOn(focaAbmVehiculoService, 'deleteCisterna');
657 spyOn(focaAbmVehiculoService, 'getCisternas').and.returnValue({ then: function() {} }); 699 spyOn(focaAbmVehiculoService, 'getCisternas').and.returnValue({ then: function() {} });
658 scope.solicitarConfirmacionCisterna({id: 1, codigo: 'abc'}); 700 scope.solicitarConfirmacionCisterna({id: 1, codigo: 'abc'});
659 701
660 //assert 702 //assert
661 promesa.then( 703 promesa.then(
662 function() { 704 function() {
663 expect(focaAbmVehiculoService.deleteCisterna).toHaveBeenCalled(); 705 expect(focaAbmVehiculoService.deleteCisterna).toHaveBeenCalled();
664 expect(focaAbmVehiculoService.getCisternas).toHaveBeenCalled(); 706 expect(focaAbmVehiculoService.getCisternas).toHaveBeenCalled();
665 done(); 707 done();
666 } 708 }
667 ); 709 );
668 }); 710 });
669 }); 711 });
670 }); 712 });
671 713
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.editando = false;
105 $scope.transportistaStamp = ''; 104 $scope.transportistaStamp = '';
106 $scope.cisternas = []; 105 $scope.cisternas = [];
107 $scope.cisterna = { 106 $scope.cisterna = {
108 codigo: '', 107 codigo: '',
109 capacidad: '', 108 capacidad: '',
110 idUnidadMedida: {}, 109 idUnidadMedida: {},
111 unidadMedida: {} 110 unidadMedida: {}
112 }; 111 };
113 112
114 $timeout(function() { 113 $timeout(function() {
115 focaBotoneraLateralService.showSalir(false); 114 focaBotoneraLateralService.showSalir(false);
116 focaBotoneraLateralService.showPausar(true); 115 focaBotoneraLateralService.showPausar(true);
117 focaBotoneraLateralService.showCancelar(false); 116 focaBotoneraLateralService.showCancelar(false);
118 focaBotoneraLateralService.showGuardar(true, $scope.guardar); 117 focaBotoneraLateralService.showGuardar(true, $scope.guardar);
119 focaBotoneraLateralService.addCustomButton('Salir', $scope.salir); 118 focaBotoneraLateralService.addCustomButton('Salir', $scope.salir);
120 }); 119 });
121 120
122 if ($scope.nuevo) { 121 if ($scope.nuevo) {
123 focaAbmVehiculoService 122 focaAbmVehiculoService
124 .getTransportistaPorId($routeParams.idTransportista) 123 .getTransportistaPorId($routeParams.idTransportista)
125 .then(function(res) { 124 .then(function(res) {
126 var codigo = ('00000' + res.data.COD).slice(-5); 125 var codigo = ('00000' + res.data.COD).slice(-5);
127 $scope.vehiculo.idTransportista = res.data.COD; 126 $scope.vehiculo.idTransportista = res.data.COD;
128 $scope.vehiculo.transportista = res.data; 127 $scope.vehiculo.transportista = res.data;
129 $scope.$broadcast('addCabecera', { 128 $scope.$broadcast('addCabecera', {
130 label: 'Transportista:', 129 label: 'Transportista:',
131 valor: codigo + ' - ' + res.data.NOM 130 valor: codigo + ' - ' + res.data.NOM
132 }); 131 });
133 }); 132 });
134 } 133 }
135 $scope.vehiculo = {}; 134 $scope.vehiculo = {};
136 135
137 focaAbmVehiculoService.getVehiculo($routeParams.idVehiculo).then(function(res) { 136 focaAbmVehiculoService.getVehiculo($routeParams.idVehiculo).then(function(res) {
138 137
139 if (res.data) { 138 if (res.data) {
140 var vehiculoSeteado = getLSVehiculo(); 139 var vehiculoSeteado = getLSVehiculo();
141 if (vehiculoSeteado === false) { 140 if (vehiculoSeteado === false) {
142 $scope.transportistaStamp = ('00000' + res.data.transportista.COD).slice(-5); 141 $scope.transportistaStamp = ('00000' + res.data.transportista.COD).slice(-5);
143 $scope.transportistaStamp += ' - ' + res.data.transportista.NOM; 142 $scope.transportistaStamp += ' - ' + res.data.transportista.NOM;
144 143
145 $scope.vehiculo = res.data; 144 $scope.vehiculo = res.data;
146 $scope.$broadcast('addCabecera', { 145 $scope.$broadcast('addCabecera', {
147 label: 'Transportista:', 146 label: 'Transportista:',
148 valor: $scope.transportistaStamp 147 valor: $scope.transportistaStamp
149 }); 148 });
150 $scope.$broadcast('addCabecera', { 149 $scope.$broadcast('addCabecera', {
151 label: 'Unidad:', 150 label: 'Unidad:',
152 valor: res.data.codigo 151 valor: res.data.codigo
153 }); 152 });
154 } 153 }
155 154
156 focaAbmVehiculoService 155 focaAbmVehiculoService
157 .getCisternas($routeParams.idVehiculo) 156 .getCisternas($routeParams.idVehiculo)
158 .then(function(res) { 157 .then(function(res) {
159 $scope.cisternas = res; 158 $scope.cisternas = res;
160 $scope.$apply(); 159 $scope.$apply();
161 }); 160 });
162 } 161 }
163 }); 162 });
164 163
165 $scope.next = function(key) { 164 $scope.next = function(key) {
166 if (key === 13) $scope.focused++; 165 if (key === 13) $scope.focused++;
167 }; 166 };
168 167
169 $scope.salir = function() { 168 $scope.salir = function() {
170 if (!$scope.formVehiculo.$pristine && focaAbmVehiculoService.cisternasPristine) { 169 if (!$scope.formVehiculo.$pristine && focaAbmVehiculoService.cisternasPristine) {
171 focaModalService.confirm( 170 focaModalService.confirm(
172 '¿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.'
173 ).then(function(data) { 172 ).then(function(data) {
174 if (data) { 173 if (data) {
175 $location.path('/vehiculo'); 174 $location.path('/vehiculo');
176 } 175 }
177 }); 176 });
178 } else { 177 } else {
179 $location.path('/vehiculo'); 178 $location.path('/vehiculo');
180 } 179 }
181 }; 180 };
182 181
183 $scope.editar = function(key, cisterna) { 182 $scope.editar = function(key, cisterna) {
184 if (key === 'nuevo') { 183 if (key === 'nuevo') {
185 $scope.cisterna = {}; 184 $scope.cisterna = {};
186 $scope.creando = true; 185 $scope.creando = true;
187 } else { 186 } else {
188 $scope.cisterna = cisterna; 187 $scope.cisterna = cisterna;
189 $scope.creando = true; 188 $scope.creando = true;
190 } 189 }
191 }; 190 };
192 191
193 $scope.seleccionarUnidadMedida = function() { 192 $scope.seleccionarUnidadMedida = function() {
194 var modalInstance = $uibModal.open( 193 var modalInstance = $uibModal.open(
195 { 194 {
196 ariaLabelledBy: 'Busqueda de Unidades de medida', 195 ariaLabelledBy: 'Busqueda de Unidades de medida',
197 templateUrl: 'modal-unidad-medida.html', 196 templateUrl: 'modal-unidad-medida.html',
198 controller: 'focaModalUnidadMedidaCtrl', 197 controller: 'focaModalUnidadMedidaCtrl',
199 size: 'lg' 198 size: 'lg'
200 } 199 }
201 ); 200 );
202 modalInstance.result.then(function(unidaMedida) { 201 modalInstance.result.then(function(unidaMedida) {
203 $scope.cisterna.idUnidadMedida = unidaMedida.ID; 202 $scope.cisterna.idUnidadMedida = unidaMedida.ID;
204 $scope.cisterna.unidadMedida = unidaMedida; 203 $scope.cisterna.unidadMedida = unidaMedida;
205 }); 204 });
206 }; 205 };
207 206
208 $scope.guardar = function(key) { 207 $scope.guardar = function(key) {
209 208
210 key = (typeof key === 'undefined') ? 13 : key; 209 key = (typeof key === 'undefined') ? 13 : key;
211 210
212 if (key === 13) { 211 if (key === 13) {
213 212
214 if ($scope.formVehiculo.$pristine && focaAbmVehiculoService.cisternasPristine) { 213 if ($scope.formVehiculo.$pristine && focaAbmVehiculoService.cisternasPristine) {
215 $scope.salir(); 214 $scope.salir();
216 return; 215 return;
217 } 216 }
218 217
219 if (!$scope.vehiculo.codigo) { 218 if (!$scope.vehiculo.codigo) {
220 focaModalService.alert('Ingrese unidad'); 219 focaModalService.alert('Ingrese unidad');
221 return; 220 return;
222 } else if (!$scope.vehiculo.tractor) { 221 } else if (!$scope.vehiculo.tractor) {
223 focaModalService.alert('Ingrese dominio tractor'); 222 focaModalService.alert('Ingrese dominio tractor');
224 return; 223 return;
225 } else if (!$scope.vehiculo.semi) { 224 } else if (!$scope.vehiculo.semi) {
226 focaModalService.alert('Ingrese dominio semi'); 225 focaModalService.alert('Ingrese dominio semi');
227 return; 226 return;
228 } else if (!$scope.vehiculo.capacidad) { 227 } else if (!$scope.vehiculo.capacidad) {
229 focaModalService.alert('Ingrese capacidad total'); 228 focaModalService.alert('Ingrese capacidad total');
230 return; 229 return;
231 } 230 }
232 //Valida si existe numero de unidad 231 //Valida si existe numero de unidad
233 if (!validaTotalCargas() && !$scope.nuevo) { 232 if (!validaTotalCargas() && !$scope.nuevo) {
234 focaModalService.alert('La suma de las capacidades de las cisternas' + 233 focaModalService.alert('La suma de las capacidades de las cisternas' +
235 ' debe ser igual a la capacidad total del vehículo'); 234 ' debe ser igual a la capacidad total del vehículo');
236 return; 235 return;
237 } 236 }
238 validaCodigoUnidad().then(function() { 237 validaCodigoUnidad().then(function() {
239 delete $scope.vehiculo.transportista; 238 delete $scope.vehiculo.transportista;
240 delete $scope.vehiculo.cisternas; 239 delete $scope.vehiculo.cisternas;
241 focaAbmVehiculoService.guardarVehiculo($scope.vehiculo) 240 focaAbmVehiculoService.guardarVehiculo($scope.vehiculo)
242 .then(function(res) { 241 .then(function(res) {
243 if ($scope.nuevo) { 242 if ($scope.nuevo) {
244 $location.path('/vehiculo/' + res.data.id + 243 $location.path('/vehiculo/' + res.data.id +
245 '/' + res.data.idTransportista); 244 '/' + res.data.idTransportista);
246 } else { 245 } else {
247 guardarCisternas().then(function() { 246 guardarCisternas().then(function() {
248 $location.path('/vehiculo'); 247 $location.path('/vehiculo');
249 }); 248 });
250 } 249 }
251 }); 250 });
252 }, function() { 251 }, function() {
253 focaModalService.alert('Código de unidad existente'); 252 focaModalService.alert('Código de unidad existente');
254 }); 253 });
255 } 254 }
256 }; 255 };
257 256
258 //Agregar propiedades de cisterna 257 //Agregar propiedades de cisterna
259 $scope.$watch('vehiculo', function(newValue) { 258 $scope.$watch('vehiculo', function(newValue) {
260 focaBotoneraLateralService.setPausarData({ 259 focaBotoneraLateralService.setPausarData({
261 label:'vehiculo', 260 label:'vehiculo',
262 val: { 261 val: {
263 codigo: newValue.codigo, 262 codigo: newValue.codigo,
264 tractor: newValue.tractor, 263 tractor: newValue.tractor,
265 semi: newValue.semi, 264 semi: newValue.semi,
266 capacidad: newValue.capacidad, 265 capacidad: newValue.capacidad,
267 idVehiculo: newValue.idVehiculo, 266 idVehiculo: newValue.idVehiculo,
268 idTransportista: newValue.idTransportista 267 idTransportista: newValue.idTransportista
269 } 268 }
270 }); 269 });
271 }, true); 270 }, true);
272 271
273 function getLSVehiculo() { 272 function getLSVehiculo() {
274 var vehiculo = JSON.parse($localStorage.vehiculo|| null); 273 var vehiculo = JSON.parse($localStorage.vehiculo|| null);
275 if (vehiculo) { 274 if (vehiculo) {
276 setearVehiculo(vehiculo); 275 setearVehiculo(vehiculo);
277 delete $localStorage.vehiculo; 276 delete $localStorage.vehiculo;
278 return true; 277 return true;
279 } 278 }
280 return false; 279 return false;
281 } 280 }
282 281
283 function setearVehiculo(vehiculo) { 282 function setearVehiculo(vehiculo) {
284 $scope.vehiculo = vehiculo; 283 $scope.vehiculo = vehiculo;
285 $scope.$broadcast('addCabecera', { 284 $scope.$broadcast('addCabecera', {
286 label: 'Vehiculo:', 285 label: 'Vehiculo:',
287 valor: $filter('rellenarDigitos')(vehiculo.codigo) + ' - ' 286 valor: $filter('rellenarDigitos')(vehiculo.codigo) + ' - '
288 }); 287 });
289 } 288 }
290 289
291 $scope.solicitarConfirmacionCisterna = function(cisterna, idx) { 290 $scope.solicitarConfirmacionCisterna = function(cisterna, idx) {
292 focaModalService.confirm('¿Está seguro que desea borrar la cisterna ' + 291 focaModalService.confirm('¿Está seguro que desea borrar la cisterna ' +
293 cisterna.id + ' ' + cisterna.codigo + ' ?').then( 292 cisterna.id + ' ' + cisterna.codigo + ' ?').then(
294 function(data) { 293 function(data) {
295 if (data) { 294 if (data) {
296 focaAbmVehiculoService.deleteCisterna(idx); 295 focaAbmVehiculoService.deleteCisterna(idx);
297 focaAbmVehiculoService 296 focaAbmVehiculoService
298 .getCisternas($routeParams.idVehiculo) 297 .getCisternas($routeParams.idVehiculo)
299 .then(function(res) { 298 .then(function(res) {
300 $scope.cisternas = res; 299 $scope.cisternas = res;
301 }); 300 });
302 } 301 }
303 } 302 }
304 ); 303 );
305 }; 304 };
306 305
307 function validaCodigoUnidad() { 306 function validaCodigoUnidad() {
308 return new Promise(function(resolve, reject) { 307 return new Promise(function(resolve, reject) {
309 focaAbmVehiculoService 308 focaAbmVehiculoService
310 .getVehiculosPorTransportista(parseInt($routeParams.idTransportista)) 309 .getVehiculosPorTransportista(parseInt($routeParams.idTransportista))
311 .then(function(res) { 310 .then(function(res) {
312 //Valida si existe numero de unidad 311 //Valida si existe numero de unidad
313 var existe = res.data.filter(function(vehiculo) { 312 var existe = res.data.filter(function(vehiculo) {
314 return vehiculo.codigo === $scope.vehiculo.codigo && 313 return vehiculo.codigo === $scope.vehiculo.codigo &&
315 vehiculo.id !== $scope.vehiculo.id; 314 vehiculo.id !== $scope.vehiculo.id;
316 }); 315 });
317 316
318 if (existe.length) { 317 if (existe.length) {
319 reject(existe); 318 reject(existe);
320 } else { 319 } else {
321 resolve(); 320 resolve();
322 } 321 }
323 }); 322 });
324 }); 323 });
325 } 324 }
326 325
327 function validaTotalCargas() { 326 function validaTotalCargas() {
328 var total = 0; 327 var total = 0;
329 $scope.cisternas.forEach(function(cisterna) { 328 $scope.cisternas.forEach(function(cisterna) {
330 if (!cisterna.desactivado) { 329 if (!cisterna.desactivado) {
331 total += parseInt(cisterna.capacidad); 330 total += parseInt(cisterna.capacidad);
332 } 331 }
333 }); 332 });
334 return $scope.vehiculo.capacidad === total; 333 return $scope.vehiculo.capacidad === total;
335 } 334 }
336 function guardarCisternas() { 335 function guardarCisternas() {
337 var cisternas = $scope.cisternas.map(function(cisterna) { 336 var cisternas = $scope.cisternas.map(function(cisterna) {
338 return { 337 return {
339 id: cisterna.id, 338 id: cisterna.id,
340 capacidad: parseFloat(cisterna.capacidad), 339 capacidad: parseFloat(cisterna.capacidad),
341 codigo: cisterna.codigo, 340 codigo: cisterna.codigo,
342 idUnidadMedida: cisterna.idUnidadMedida, 341 idUnidadMedida: cisterna.idUnidadMedida,
343 idVehiculo: $routeParams.idVehiculo, 342 idVehiculo: $routeParams.idVehiculo,
344 desactivado: cisterna.desactivado 343 desactivado: cisterna.desactivado
345 }; 344 };
346 }); 345 });
347 346
348 return focaAbmVehiculoService.guardarCisternas(cisternas); 347 return focaAbmVehiculoService.guardarCisternas(cisternas);
349 } 348 }
350 349
351 $scope.guardarCisterna = function() { 350 $scope.guardarCisterna = function() {
352 if (!$scope.cisterna.codigo) { 351 if (!$scope.cisterna.codigo) {
353 focaModalService.alert('Ingrese codigo de cisterna'); 352 focaModalService.alert('Ingrese codigo de cisterna');
354 return; 353 return;
355 } else if (!$scope.cisterna.capacidad) { 354 } else if (!$scope.cisterna.capacidad) {
356 focaModalService.alert('Ingrese capacidad'); 355 focaModalService.alert('Ingrese capacidad');
357 return; 356 return;
358 } else if (!$scope.cisterna.idUnidadMedida) { 357 } else if (!$scope.cisterna.idUnidadMedida) {
359 focaModalService.alert('Ingrese unidad de medida'); 358 focaModalService.alert('Ingrese unidad de medida');
360 return; 359 return;
361 } 360 }
362 $scope.creando = false; 361 $scope.creando = false;
363 if (!$scope.cisterna.id) { 362 if (!$scope.cisterna.id) {
364 validaCodigo() 363 validaCodigo()
365 .then(function() { 364 .then(function() {
366 saveCisterna(); 365 saveCisterna();
367 }, function(err) { 366 }, function(err) {
368 focaModalService.alert(err); 367 focaModalService.alert(err);
369 }); 368 });
370 } else { 369 } else {
371 saveCisterna(); 370 saveCisterna();
372 } 371 }
373 }; 372 };
374 373
375 function saveCisterna () { 374 function saveCisterna () {
376 $scope.cisterna.idVehiculo = parseInt($routeParams.idVehiculo); 375 $scope.cisterna.idVehiculo = parseInt($routeParams.idVehiculo);
377 delete $scope.cisterna.vehiculo; 376 delete $scope.cisterna.vehiculo;
378 var auxCisternas = $scope.cisternas; 377 var auxCisternas = $scope.cisternas;
379 if (!$scope.cisterna.id) { 378 if (!$scope.cisterna.id) {
380 auxCisternas.push($scope.cisterna); 379 auxCisternas.push($scope.cisterna);
381 } 380 }
382 guardarCisternas(auxCisternas) 381 guardarCisternas(auxCisternas)
383 .then(function() { 382 .then(function() {
384 focaAbmVehiculoService 383 focaAbmVehiculoService
385 .getCisternas($routeParams.idVehiculo) 384 .getCisternas($routeParams.idVehiculo)
386 .then(function(res) { 385 .then(function(res) {
387 $scope.cisternas = res; 386 $scope.cisternas = res;
388 $scope.cisterna = {}; 387 $scope.cisterna = {};
389 }); 388 });
390 }) 389 })
391 .catch(function (err) { 390 .catch(function (err) {
392 console.log('Err: ', err); 391 console.log('Err: ', err);
393 }); 392 });
394 } 393 }
395 394
396 if ($routeParams.idx !== -1) { 395 if ($routeParams.idx !== -1) {
397 $scope.cisterna = [$routeParams.idx]; 396 $scope.cisterna = [$routeParams.idx];
398 focaAbmVehiculoService 397 focaAbmVehiculoService
399 .getCisternas($routeParams.idVehiculo) 398 .getCisternas($routeParams.idVehiculo)
400 .then(function(res) { 399 .then(function(res) {
401 $scope.cisterna = res[$routeParams.idx]; 400 $scope.cisterna = res[$routeParams.idx];
402 }); 401 });
403 } 402 }
404 403
405 function validaCodigo() { 404 function validaCodigo() {
406 return new Promise(function(resolve, reject) { 405 return new Promise(function(resolve, reject) {
407 focaAbmVehiculoService 406 focaAbmVehiculoService
408 .getCisternas($routeParams.idVehiculo) 407 .getCisternas($routeParams.idVehiculo)
409 .then(function(res) { 408 .then(function(res) {
410 var cisternas = res; 409 var cisternas = res;
411 var totalCargado = 0; 410 var totalCargado = 0;
412 cisternas.forEach(function(cisterna, idx) { 411 cisternas.forEach(function(cisterna, idx) {
413 //SI EL CODIGO YA EXISTE 412 //SI EL CODIGO YA EXISTE
414 if (cisterna.codigo === $scope.cisterna.codigo && 413 if (cisterna.codigo === $scope.cisterna.codigo &&
415 idx !== $routeParams.idx && 414 idx !== $routeParams.idx &&
416 !cisterna.desactivado) { 415 !cisterna.desactivado) {
417 reject('Código de cisterna existente'); 416 reject('Código de cisterna existente');
418 } 417 }
419 if (idx !== $routeParams.idx && 418 if (idx !== $routeParams.idx &&
420 !cisterna.desactivado) { 419 !cisterna.desactivado) {
421 totalCargado += cisterna.capacidad; 420 totalCargado += cisterna.capacidad;
422 } 421 }
423 }); 422 });
424 resolve(); 423 resolve();
425 }); 424 });
426 }); 425 });
427 } 426 }
428 } 427 }
429 ]); 428 ]);
430 429