Commit 88750e8ea3a2cf499a7241741946ae53c8e2726e

Authored by Eric Fernandez
1 parent e80bc1985b
Exists in master and in 2 other branches develop, lab

creación de choferes correcto

Showing 1 changed file with 12 additions and 4 deletions   Show diff stats
src/js/controller.js
1 angular.module('focaAbmChofer') 1 angular.module('focaAbmChofer')
2 .controller('focaAbmChoferesController', [ 2 .controller('focaAbmChoferesController', [
3 '$scope', 'focaAbmChoferService', '$location', '$uibModal', 3 '$scope', 'focaAbmChoferService', '$location', '$uibModal',
4 'focaModalService', 'focaBotoneraLateralService', '$timeout', '$localStorage', 4 'focaModalService', 'focaBotoneraLateralService', '$timeout', '$localStorage',
5 '$routeParams', '$filter', 5 '$routeParams',
6 function($scope, focaAbmChoferService, $location, $uibModal, focaModalService, 6 function($scope, focaAbmChoferService, $location, $uibModal, focaModalService,
7 focaBotoneraLateralService, $timeout, $localStorage, $routeParams, $filter) { 7 focaBotoneraLateralService, $timeout, $localStorage, $routeParams) {
8 8
9 $scope.focused = 1; 9 $scope.focused = 1;
10 $scope.now = new Date(); 10 $scope.now = new Date();
11 $scope.nuevo = $routeParams.id === '0'; 11 $scope.nuevo = $routeParams.id === '0';
12 $scope.filters = ''; 12 $scope.filters = '';
13 $scope.choferes = []; 13 $scope.choferes = [];
14 $scope.creando = false; 14 $scope.creando = false;
15 $scope.crear = false; 15 $scope.crear = false;
16 $scope.transportistas = []; 16 $scope.transportistas = [];
17 $scope.botonera = [{ 17 $scope.botonera = [{
18 label: 'Transportista', 18 label: 'Transportista',
19 image: 'cliente.png' 19 image: 'cliente.png'
20 }]; 20 }];
21 $scope.next = function(key) { 21 $scope.next = function(key) {
22 if (key === 13) $scope.focused++; 22 if (key === 13) $scope.focused++;
23 }; 23 };
24 24
25 //SETEO BOTONERA LATERAL 25 //SETEO BOTONERA LATERAL
26 $timeout(function() { 26 $timeout(function() {
27 focaBotoneraLateralService.showSalir(false); 27 focaBotoneraLateralService.showSalir(false);
28 focaBotoneraLateralService.showPausar(false); 28 focaBotoneraLateralService.showPausar(false);
29 focaBotoneraLateralService.showCancelar(false); 29 focaBotoneraLateralService.showCancelar(false);
30 focaBotoneraLateralService.showGuardar(true, $scope.guardar); 30 focaBotoneraLateralService.showGuardar(true, $scope.guardar);
31 focaBotoneraLateralService.addCustomButton('Salir', salir); 31 focaBotoneraLateralService.addCustomButton('Salir', salir);
32 }); 32 });
33 33
34 if (focaAbmChoferService.transportistaSeleccionado.COD) { 34 if (focaAbmChoferService.transportistaSeleccionado.COD) {
35 elegirTransportista(focaAbmChoferService.transportistaSeleccionado); 35 elegirTransportista(focaAbmChoferService.transportistaSeleccionado);
36 } 36 }
37 37
38 focaAbmChoferService.getTiposDocumento().then(function(res) { 38 focaAbmChoferService.getTiposDocumento().then(function(res) {
39 $scope.tiposDocumento = res.data; 39 $scope.tiposDocumento = res.data;
40 }); 40 });
41 41
42 $scope.crearChofer = function () { 42 $scope.crearChofer = function () {
43 var chofer = { 43 var chofer = {
44 id: 0, 44 id: 0,
45 nombre: '', 45 nombre: '',
46 telefono: '', 46 telefono: '',
47 editando: true, 47 editando: true,
48 desactivado: false
48 }; 49 };
49 $scope.choferes.unshift(chofer); 50 $scope.choferes.unshift(chofer);
50 $scope.crear = false; 51 $scope.crear = false;
51 }; 52 };
52 53
53 $scope.editar = function(chofer) { 54 $scope.editar = function(chofer) {
54 $scope.choferes.forEach(function(chofer) { 55 $scope.choferes.forEach(function(chofer) {
55 chofer.editando = false; 56 chofer.editando = false;
56 $scope.crear = false; 57 $scope.crear = false;
57 }); 58 });
58 chofer.editando = true; 59 chofer.editando = true;
59 $scope.inicial = angular.copy(chofer); 60 $scope.inicial = angular.copy(chofer);
60 }; 61 };
61 62
62 $scope.agregarChofer = function (chofer) { 63 $scope.agregarChofer = function (chofer) {
63 if (!chofer.nombre) { 64 if (!chofer.nombre) {
64 focaModalService.alert('Ingrese nombre'); 65 focaModalService.alert('Ingrese nombre');
65 return; 66 return;
66 } else if (!chofer.idTipoDocumento) { 67 } else if (!chofer.idTipoDocumento) {
67 focaModalService.alert('Ingrese tipo documento'); 68 focaModalService.alert('Ingrese tipo documento');
68 return; 69 return;
69 } 70 }
70 validaDni(chofer); 71 validaDni(chofer);
71 }; 72 };
72 73
73 $scope.tipoDocumento = function (idTipoDocumento) { 74 $scope.tipoDocumento = function (idTipoDocumento) {
74 var value = ''; 75 var value = '';
75 switch (parseInt(idTipoDocumento)) { 76 switch (parseInt(idTipoDocumento)) {
76 case 96 : 77 case 96 :
77 value = 'DNI'; 78 value = 'DNI';
78 break; 79 break;
79 case 80 : 80 case 80 :
80 value = 'CUIT'; 81 value = 'CUIT';
81 break; 82 break;
82 case 86 : 83 case 86 :
83 value = 'CUIL'; 84 value = 'CUIL';
84 break; 85 break;
85 default: 86 default:
86 value = ''; 87 value = '';
87 break; 88 break;
88 } 89 }
89 return value; 90 return value;
90 }; 91 };
91 92
92 $scope.volver = function (chofer, key) { 93 $scope.volver = function (chofer, key) {
93 if (chofer.idTransportista === undefined) { 94 if (chofer.idTransportista === undefined) {
94 $scope.choferes.shift(); 95 $scope.choferes.shift();
95 $scope.crear = true; 96 $scope.crear = true;
96 chofer.editando = false; 97 chofer.editando = false;
97 return; 98 return;
98 } else if (chofer.id !== 0 || !$scope.crear) { 99 } else if (chofer.id !== 0 || !$scope.crear) {
99 $scope.choferes[key] = $scope.inicial; 100 $scope.choferes[key] = $scope.inicial;
100 $scope.choferes[key].editando = false; 101 $scope.choferes[key].editando = false;
101 } 102 }
102 $scope.crear = true; 103 $scope.crear = true;
103 }; 104 };
104 105
105 $scope.guardar = function() { 106 $scope.guardar = function() {
106 $scope.choferes.forEach( function (chofer) { 107 $scope.choferes.forEach( function (chofer) {
107 if (chofer.id === 0) { 108 if (chofer.id === 0) {
108 delete chofer.id; 109 delete chofer.id;
109 } 110 }
110 delete chofer.transportista; 111 delete chofer.transportista;
111 delete chofer.editando; 112 delete chofer.editando;
112 }); 113 });
113 focaAbmChoferService.guardarChoferes($scope.choferes); 114 focaAbmChoferService
115 .guardarChoferes($scope.choferes)
116 .then(salir)
117 .catch(e => {
118 console.error(e);
119 focaModalService.alert('Hubo un error al guardar');
120 });
114 }; 121 };
115 122
116 $scope.solicitarConfirmacion = function(chofer) { 123 $scope.solicitarConfirmacion = function(chofer) {
117 focaModalService.confirm('¿Está seguro que desea borrar el chofer ' + 124 focaModalService.confirm('¿Está seguro que desea borrar el chofer ' +
118 chofer.nombre + ' ?').then(function(confirmed) { 125 chofer.nombre + ' ?').then(function(confirmed) {
119 if (confirmed) { 126 if (confirmed) {
120 chofer.desactivado = true; 127 chofer.desactivado = true;
121 } 128 }
122 }); 129 });
123 }; 130 };
124 131
125 $scope.seleccionarTransportista = function() { 132 $scope.seleccionarTransportista = function() {
126 var parametrosModal = { 133 var parametrosModal = {
127 titulo: 'Búsqueda de Transportista', 134 titulo: 'Búsqueda de Transportista',
128 query: '/transportista', 135 query: '/transportista',
129 columnas: [ 136 columnas: [
130 { 137 {
131 nombre: 'Código', 138 nombre: 'Código',
132 propiedad: 'COD' 139 propiedad: 'COD'
133 }, 140 },
134 { 141 {
135 nombre: 'Nombre', 142 nombre: 'Nombre',
136 propiedad: 'NOM' 143 propiedad: 'NOM'
137 }, 144 },
138 { 145 {
139 nombre: 'CUIT', 146 nombre: 'CUIT',
140 propiedad: 'CUIT' 147 propiedad: 'CUIT'
141 } 148 }
142 ] 149 ]
143 }; 150 };
144 focaModalService.modal(parametrosModal).then( 151 focaModalService.modal(parametrosModal).then(
145 function(transportista) { 152 function(transportista) {
146 $scope.crear = true; 153 $scope.crear = true;
147 elegirTransportista(transportista); 154 elegirTransportista(transportista);
148 focaAbmChoferService.transportistaSeleccionado = transportista; 155 focaAbmChoferService.transportistaSeleccionado = transportista;
149 }, function() {} 156 }, function() {}
150 ); 157 );
151 }; 158 };
152 159
153 function elegirTransportista(transportista) { 160 function elegirTransportista(transportista) {
154 buscar(transportista.COD); 161 buscar(transportista.COD);
155 var codigo = ('00000' + transportista.COD).slice(-5); 162 var codigo = ('00000' + transportista.COD).slice(-5);
156 $scope.idTransportista = transportista.COD; 163 $scope.idTransportista = transportista.COD;
157 $timeout(function() { 164 $timeout(function() {
158 $scope.$broadcast('addCabecera', { 165 $scope.$broadcast('addCabecera', {
159 label: 'Transportista:', 166 label: 'Transportista:',
160 valor: codigo + ' - ' + transportista.NOM 167 valor: codigo + ' - ' + transportista.NOM
161 }); 168 });
162 }); 169 });
163 } 170 }
164 171
165 function buscar(id) { 172 function buscar(id) {
166 focaAbmChoferService.getChoferPorTransportista(id).then(function(res) { 173 focaAbmChoferService.getChoferPorTransportista(id).then(function(res) {
167 $scope.choferes = res.data; 174 $scope.choferes = res.data;
168 }); 175 });
169 } 176 }
170 177
171 function salir() { 178 function salir() {
172 focaAbmChoferService.transportistaSeleccionado = {}; 179 focaAbmChoferService.transportistaSeleccionado = {};
173 $location.path('/'); 180 $location.path('/');
174 } 181 }
175 182
176 function validaDni(chofer) { 183 function validaDni(chofer) {
177 if (!chofer.dni) { 184 if (!chofer.dni) {
178 focaModalService.alert('Ingrese DNI'); 185 focaModalService.alert('Ingrese DNI');
179 return; 186 return;
180 } else if (!chofer.telefono) { 187 } else if (!chofer.telefono) {
181 focaModalService.alert('Ingrese teléfono'); 188 focaModalService.alert('Ingrese teléfono');
182 return; 189 return;
183 } 190 }
184 191
185 return new Promise(function(resolve, reject) { 192 return new Promise(function(resolve, reject) {
186 focaAbmChoferService 193 focaAbmChoferService
187 .getChoferPorDni(chofer.dni) 194 .getChoferPorDni(chofer.dni)
188 .then(function(res) { 195 .then(function(res) {
189 if (res.data.id && 196 if (res.data.id &&
190 chofer.id !== res.data.id) { 197 chofer.id !== res.data.id) {
191 reject(res.data); 198 reject(res.data);
192 } else { 199 } else {
193 resolve(); 200 resolve();
194 } 201 }
195 }) 202 })
196 .then(function() { 203 .then(function() {
197 chofer.idTransportista = focaAbmChoferService.transportistaSeleccionado.COD; 204 chofer.idTransportista = focaAbmChoferService
205 .transportistaSeleccionado.COD;
198 delete chofer.transportista; 206 delete chofer.transportista;
199 }, function() { 207 }, function() {
200 focaModalService.alert('Dni existente'); 208 focaModalService.alert('Dni existente');
201 $scope.editando = true; 209 $scope.editando = true;
202 }); 210 });
203 $scope.crear = true; 211 $scope.crear = true;
204 chofer.editando = false; 212 chofer.editando = false;
205 }); 213 });
206 } 214 }
207 215
208 if ($localStorage.chofer) { 216 if ($localStorage.chofer) {
209 var chofer = JSON.parse($localStorage.chofer); 217 var chofer = JSON.parse($localStorage.chofer);
210 if (!chofer.id) { chofer.id = 0; } 218 if (!chofer.id) { chofer.id = 0; }
211 $location.path('/chofer/' + chofer.id + '/' + chofer.idTransportista); 219 $location.path('/chofer/' + chofer.id + '/' + chofer.idTransportista);
212 } 220 }
213 } 221 }
214 ]); 222 ]);
215 223