Commit 7c6be3b387e3574bc3eb67f41263440e2f78d117

Authored by Luigi
1 parent 1f9869ff74
Exists in master

Arreglo validacion DNI

Showing 1 changed file with 20 additions and 18 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', '$filter',
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, $filter) {
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 }; 48 };
49 $scope.choferes.unshift(chofer); 49 $scope.choferes.unshift(chofer);
50 $scope.crear = false; 50 $scope.crear = false;
51 }; 51 };
52 52
53 $scope.editar = function(chofer) { 53 $scope.editar = function(chofer) {
54 $scope.choferes.forEach(function(chofer) { 54 $scope.choferes.forEach(function(chofer) {
55 chofer.editando = false; 55 chofer.editando = false;
56 $scope.crear = false; 56 $scope.crear = false;
57 }); 57 });
58 chofer.editando = true; 58 chofer.editando = true;
59 $scope.inicial = angular.copy(chofer); 59 $scope.inicial = angular.copy(chofer);
60 }; 60 };
61 61
62 $scope.agregarChofer = function (chofer) { 62 $scope.agregarChofer = function (chofer) {
63 if (!chofer) { 63 if (!chofer.nombre) {
64 focaModalService.alert('Ingrese nombre'); 64 focaModalService.alert('Ingrese nombre');
65 return; 65 return;
66 } else if (!chofer.idTipoDocumento) { 66 } else if (!chofer.idTipoDocumento) {
67 focaModalService.alert('Ingrese tipo documento'); 67 focaModalService.alert('Ingrese tipo documento');
68 return; 68 return;
69 } else if (!chofer.dni) { 69 }
70 focaModalService.alert('Ingrese DNI'); 70 validaDni(chofer);
71 return;
72 } else if (!chofer.telefono) {
73 focaModalService.alert('Ingrese teléfono');
74 return;
75 }
76
77 validaDni(chofer).then(function() {
78 chofer.idTransportista = focaAbmChoferService.transportistaSeleccionado.COD;
79 delete chofer.transportista;
80 }, function() {
81 focaModalService.alert('Dni existente');
82 $scope.editando = true;
83 });
84 $scope.crear = true;
85 chofer.editando = false;
86 }; 71 };
87 72
88 $scope.tipoDocumento = function (idTipoDocumento) { 73 $scope.tipoDocumento = function (idTipoDocumento) {
89 var value = ''; 74 var value = '';
90 switch (parseInt(idTipoDocumento)) { 75 switch (parseInt(idTipoDocumento)) {
91 case 96 : 76 case 96 :
92 value = 'DNI'; 77 value = 'DNI';
93 break; 78 break;
94 case 80 : 79 case 80 :
95 value = 'CUIT'; 80 value = 'CUIT';
96 break; 81 break;
97 case 86 : 82 case 86 :
98 value = 'CUIL'; 83 value = 'CUIL';
99 break; 84 break;
100 default: 85 default:
101 value = ''; 86 value = '';
102 break; 87 break;
103 } 88 }
104 return value; 89 return value;
105 }; 90 };
106 91
107 $scope.volver = function (chofer, key) { 92 $scope.volver = function (chofer, key) {
108 if (chofer.idTransportista === undefined) { 93 if (chofer.idTransportista === undefined) {
109 $scope.choferes.shift(); 94 $scope.choferes.shift();
110 $scope.crear = true; 95 $scope.crear = true;
111 chofer.editando = false; 96 chofer.editando = false;
112 return; 97 return;
113 } else if (chofer.id !== 0 || !$scope.crear) { 98 } else if (chofer.id !== 0 || !$scope.crear) {
114 $scope.choferes[key] = $scope.inicial; 99 $scope.choferes[key] = $scope.inicial;
115 $scope.choferes[key].editando = false; 100 $scope.choferes[key].editando = false;
116 } 101 }
117 $scope.crear = true; 102 $scope.crear = true;
118 }; 103 };
119 104
120 $scope.guardar = function() { 105 $scope.guardar = function() {
121 $scope.choferes.forEach( function (chofer) { 106 $scope.choferes.forEach( function (chofer) {
122 if (chofer.id === 0) { 107 if (chofer.id === 0) {
123 delete chofer.id; 108 delete chofer.id;
124 } 109 }
125 delete chofer.transportista; 110 delete chofer.transportista;
126 delete chofer.editando; 111 delete chofer.editando;
127 }); 112 });
128 focaAbmChoferService.guardarChoferes($scope.choferes); 113 focaAbmChoferService.guardarChoferes($scope.choferes);
129 }; 114 };
130 115
131 $scope.solicitarConfirmacion = function(chofer) { 116 $scope.solicitarConfirmacion = function(chofer) {
132 focaModalService.confirm('¿Está seguro que desea borrar el chofer ' + 117 focaModalService.confirm('¿Está seguro que desea borrar el chofer ' +
133 chofer.nombre + ' ?').then(function(confirmed) { 118 chofer.nombre + ' ?').then(function(confirmed) {
134 if (confirmed) { 119 if (confirmed) {
135 chofer.desactivado = true; 120 chofer.desactivado = true;
136 } 121 }
137 }); 122 });
138 }; 123 };
139 124
140 $scope.seleccionarTransportista = function() { 125 $scope.seleccionarTransportista = function() {
141 var parametrosModal = { 126 var parametrosModal = {
142 titulo: 'Búsqueda de Transportista', 127 titulo: 'Búsqueda de Transportista',
143 query: '/transportista', 128 query: '/transportista',
144 columnas: [ 129 columnas: [
145 { 130 {
146 nombre: 'Código', 131 nombre: 'Código',
147 propiedad: 'COD' 132 propiedad: 'COD'
148 }, 133 },
149 { 134 {
150 nombre: 'Nombre', 135 nombre: 'Nombre',
151 propiedad: 'NOM' 136 propiedad: 'NOM'
152 }, 137 },
153 { 138 {
154 nombre: 'CUIT', 139 nombre: 'CUIT',
155 propiedad: 'CUIT' 140 propiedad: 'CUIT'
156 } 141 }
157 ] 142 ]
158 }; 143 };
159 focaModalService.modal(parametrosModal).then( 144 focaModalService.modal(parametrosModal).then(
160 function(transportista) { 145 function(transportista) {
161 $scope.crear = true; 146 $scope.crear = true;
162 elegirTransportista(transportista); 147 elegirTransportista(transportista);
163 focaAbmChoferService.transportistaSeleccionado = transportista; 148 focaAbmChoferService.transportistaSeleccionado = transportista;
164 }, function() {} 149 }, function() {}
165 ); 150 );
166 }; 151 };
167 152
168 function elegirTransportista(transportista) { 153 function elegirTransportista(transportista) {
169 buscar(transportista.COD); 154 buscar(transportista.COD);
170 var codigo = ('00000' + transportista.COD).slice(-5); 155 var codigo = ('00000' + transportista.COD).slice(-5);
171 $scope.idTransportista = transportista.COD; 156 $scope.idTransportista = transportista.COD;
172 $timeout(function() { 157 $timeout(function() {
173 $scope.$broadcast('addCabecera', { 158 $scope.$broadcast('addCabecera', {
174 label: 'Transportista:', 159 label: 'Transportista:',
175 valor: codigo + ' - ' + transportista.NOM 160 valor: codigo + ' - ' + transportista.NOM
176 }); 161 });
177 }); 162 });
178 } 163 }
179 164
180 function buscar(id) { 165 function buscar(id) {
181 focaAbmChoferService.getChoferPorTransportista(id).then(function(res) { 166 focaAbmChoferService.getChoferPorTransportista(id).then(function(res) {
182 $scope.choferes = res.data; 167 $scope.choferes = res.data;
183 }); 168 });
184 } 169 }
185 170
186 function salir() { 171 function salir() {
187 focaAbmChoferService.transportistaSeleccionado = {}; 172 focaAbmChoferService.transportistaSeleccionado = {};
188 $location.path('/'); 173 $location.path('/');
189 } 174 }
190 175
191 function validaDni(chofer) { 176 function validaDni(chofer) {
177 if (!chofer.dni) {
178 focaModalService.alert('Ingrese DNI');
179 return;
180 } else if (!chofer.telefono) {
181 focaModalService.alert('Ingrese teléfono');
182 return;
183 }
184
192 return new Promise(function(resolve, reject) { 185 return new Promise(function(resolve, reject) {
193 focaAbmChoferService 186 focaAbmChoferService
194 .getChoferPorDni(chofer.dni) 187 .getChoferPorDni(chofer.dni)
195 .then(function(res) { 188 .then(function(res) {
196 if (res.data.id && 189 if (res.data.id &&
197 chofer.id !== res.data.id) { 190 chofer.id !== res.data.id) {
198 reject(res.data); 191 reject(res.data);
199 } else { 192 } else {
200 resolve(); 193 resolve();
201 } 194 }
195 })
196 .then(function() {
197 chofer.idTransportista = focaAbmChoferService.transportistaSeleccionado.COD;
198 delete chofer.transportista;
199 }, function() {
200 focaModalService.alert('Dni existente');
201 $scope.editando = true;
202 }); 202 });
203 $scope.crear = true;
204 chofer.editando = false;
203 }); 205 });
204 } 206 }
205 207
206 if ($localStorage.chofer) { 208 if ($localStorage.chofer) {
207 var chofer = JSON.parse($localStorage.chofer); 209 var chofer = JSON.parse($localStorage.chofer);
208 if (!chofer.id) { chofer.id = 0; } 210 if (!chofer.id) { chofer.id = 0; }
209 $location.path('/chofer/' + chofer.id + '/' + chofer.idTransportista); 211 $location.path('/chofer/' + chofer.id + '/' + chofer.idTransportista);
210 } 212 }
211 213
212 /*$timeout(function() {getLSChofer();}); 214 /*$timeout(function() {getLSChofer();});
213 215
214 function setearChofer(chofer) { 216 function setearChofer(chofer) {
215 $scope.chofer = chofer; 217 $scope.chofer = chofer;
216 $scope.$broadcast('addCabecera', { 218 $scope.$broadcast('addCabecera', {
217 label: 'Transportista:', 219 label: 'Transportista:',
218 valor: $filter('rellenarDigitos')(chofer.idTransportista.chofer, 2) + ' - ' 220 valor: $filter('rellenarDigitos')(chofer.idTransportista.chofer, 2) + ' - '
219 }); 221 });
220 } 222 }
221 223
222 function getLSChofer() { 224 function getLSChofer() {
223 var chofer = JSON.parse($localStorage.chofer || null); 225 var chofer = JSON.parse($localStorage.chofer || null);
224 226
225 if (chofer) { 227 if (chofer) {
226 setearChofer(chofer); 228 setearChofer(chofer);
227 delete $localStorage.chofer; 229 delete $localStorage.chofer;
228 } 230 }
229 } 231 }
230 232
231 $scope.$watch('chofer', function(newValue) { 233 $scope.$watch('chofer', function(newValue) {
232 focaBotoneraLateralService.setPausarData({ 234 focaBotoneraLateralService.setPausarData({