Commit b11fcc2bf10dde9fc3a230ebd0d26b2dd7b0aea8

Authored by Jose Pinto
Exists in master and in 1 other branch develop

Merge branch 'develop' into 'master'

Develop

See merge request !17
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', 4 'focaModalService', 'focaBotoneraLateralService', '$timeout', '$localStorage',
5 function($scope, focaAbmChoferService, $location, $uibModal, focaModalService, 5 function($scope, focaAbmChoferService, $location, $uibModal, focaModalService,
6 focaBotoneraLateralService, $timeout) { 6 focaBotoneraLateralService, $timeout, $localStorage) {
7 7
8 $scope.now = new Date(); 8 $scope.now = new Date();
9 $scope.filters = ''; 9 $scope.filters = '';
10 $scope.choferes = []; 10 $scope.choferes = [];
11 $scope.botonera = [{ 11 $scope.botonera = [{
12 label: 'Transportista', 12 label: 'Transportista',
13 image: 'cliente.png' 13 image: 'cliente.png'
14 }]; 14 }];
15 15
16 //SETEO BOTONERA LATERAL 16 //SETEO BOTONERA LATERAL
17 $timeout(function() { 17 $timeout(function() {
18 focaBotoneraLateralService.showSalir(false); 18 focaBotoneraLateralService.showSalir(false);
19 focaBotoneraLateralService.showPausar(false); 19 focaBotoneraLateralService.showPausar(false);
20 focaBotoneraLateralService.showCancelar(false); 20 focaBotoneraLateralService.showCancelar(false);
21 focaBotoneraLateralService.showGuardar(false); 21 focaBotoneraLateralService.showGuardar(false);
22 focaBotoneraLateralService.addCustomButton('Salir', salir); 22 focaBotoneraLateralService.addCustomButton('Salir', salir);
23 }); 23 });
24 24
25 if(focaAbmChoferService.transportistaSeleccionado.COD) { 25 if (focaAbmChoferService.transportistaSeleccionado.COD) {
26 elegirTransportista(focaAbmChoferService.transportistaSeleccionado); 26 elegirTransportista(focaAbmChoferService.transportistaSeleccionado);
27 } 27 }
28 28
29 $scope.editar = function(id) { 29 $scope.editar = function(id) {
30 $location.path('/chofer/' + id + '/' + $scope.idTransportista); 30 $location.path('/chofer/' + id + '/' + $scope.idTransportista);
31 }; 31 };
32 32
33 $scope.solicitarConfirmacion = function(chofer) { 33 $scope.solicitarConfirmacion = function(chofer) {
34 focaModalService.confirm('¿Está seguro que desea borrar el chofer ' + 34 focaModalService.confirm('¿Está seguro que desea borrar el chofer ' +
35 chofer.nombre + ' ?').then(function(confirmed) { 35 chofer.nombre + ' ?').then(function(confirmed) {
36 if(confirmed) { 36 if (confirmed) {
37 focaAbmChoferService.deleteChofer(chofer.id); 37 focaAbmChoferService.deleteChofer(chofer.id);
38 $scope.choferes.splice($scope.choferes.indexOf(chofer), 1); 38 $scope.choferes.splice($scope.choferes.indexOf(chofer), 1);
39 } 39 }
40 }); 40 });
41 }; 41 };
42 42
43 $scope.seleccionarTransportista = function() { 43 $scope.seleccionarTransportista = function() {
44 var parametrosModal = { 44 var parametrosModal = {
45 titulo: 'Búsqueda de Transportista', 45 titulo: 'Búsqueda de Transportista',
46 query: '/transportista', 46 query: '/transportista',
47 columnas: [ 47 columnas: [
48 { 48 {
49 nombre: 'Código', 49 nombre: 'Código',
50 propiedad: 'COD' 50 propiedad: 'COD'
51 }, 51 },
52 { 52 {
53 nombre: 'Nombre', 53 nombre: 'Nombre',
54 propiedad: 'NOM' 54 propiedad: 'NOM'
55 }, 55 },
56 { 56 {
57 nombre: 'CUIT', 57 nombre: 'CUIT',
58 propiedad: 'CUIT' 58 propiedad: 'CUIT'
59 } 59 }
60 ] 60 ]
61 }; 61 };
62 focaModalService.modal(parametrosModal).then( 62 focaModalService.modal(parametrosModal).then(
63 function(transportista) { 63 function(transportista) {
64 elegirTransportista(transportista); 64 elegirTransportista(transportista);
65 focaAbmChoferService.transportistaSeleccionado = transportista; 65 focaAbmChoferService.transportistaSeleccionado = transportista;
66 }, function() {} 66 }, function() {}
67 ); 67 );
68 }; 68 };
69 69
70 function elegirTransportista(transportista) { 70 function elegirTransportista(transportista) {
71 buscar(transportista.COD); 71 buscar(transportista.COD);
72 var codigo = ('00000' + transportista.COD).slice(-5); 72 var codigo = ('00000' + transportista.COD).slice(-5);
73 $scope.idTransportista = transportista.COD; 73 $scope.idTransportista = transportista.COD;
74 $timeout(function() { 74 $timeout(function() {
75 $scope.$broadcast('addCabecera', { 75 $scope.$broadcast('addCabecera', {
76 label: 'Transportista:', 76 label: 'Transportista:',
77 valor: codigo + ' - ' + transportista.NOM 77 valor: codigo + ' - ' + transportista.NOM
78 }); 78 });
79 }); 79 });
80 } 80 }
81 81
82 function buscar(id) { 82 function buscar(id) {
83 focaAbmChoferService.getChoferPorTransportista(id).then(function(res) { 83 focaAbmChoferService.getChoferPorTransportista(id).then(function(res) {
84 $scope.choferes = res.data; 84 $scope.choferes = res.data;
85 }); 85 });
86 } 86 }
87 87
88 function salir() { 88 function salir() {
89 focaAbmChoferService.transportistaSeleccionado = {}; 89 focaAbmChoferService.transportistaSeleccionado = {};
90 $location.path('/'); 90 $location.path('/');
91 } 91 }
92
93 if ($localStorage.chofer) {
94 var chofer = JSON.parse($localStorage.chofer);
95 if (!chofer.id) { chofer.id = 0; }
96 $location.path('/chofer/' + chofer.id + '/' + chofer.idTransportista);
97 }
92 } 98 }
93 ]) 99 ])
94 .controller('focaAbmChoferController', [ 100 .controller('focaAbmChoferController', [
95 '$scope', 'focaAbmChoferService', '$routeParams', 101 '$scope', 'focaAbmChoferService', '$routeParams', '$localStorage', '$filter',
96 '$location', 'focaBotoneraLateralService', '$timeout', 'focaModalService', 102 '$location', 'focaBotoneraLateralService', '$timeout', 'focaModalService', '$window',
97 function($scope, focaAbmChoferService, $routeParams, 103 function($scope, focaAbmChoferService, $routeParams, $localStorage, $filter,
98 $location, focaBotoneraLateralService, $timeout, focaModalService) { 104 $location, focaBotoneraLateralService, $timeout, focaModalService){
99 105
100 $scope.focused = 1; 106 $scope.focused = 1;
101 $scope.nuevo = $routeParams.id === '0'; 107 $scope.nuevo = $routeParams.id === '0';
102 $scope.chofer = {}; 108 $scope.chofer = {};
103 $scope.transportistas = []; 109 $scope.transportistas = [];
104 $scope.now = new Date(); 110 $scope.now = new Date();
105 $scope.next = function(key) { 111 $scope.next = function(key) {
106 if (key === 13) $scope.focused++; 112 if (key === 13) $scope.focused++;
107 }; 113 };
108 114
109 focaAbmChoferService.getTiposDocumento().then(function(res) { 115 focaAbmChoferService.getTiposDocumento().then(function(res) {
110 $scope.tiposDocumento = res.data; 116 $scope.tiposDocumento = res.data;
111 }); 117 });
112 118
113 //SETEO BOTONERA LATERAL 119 //SETEO BOTONERA LATERAL
114 $timeout(function() { 120 $timeout(function() {
115 focaBotoneraLateralService.showSalir(false); 121 focaBotoneraLateralService.showSalir(false);
116 focaBotoneraLateralService.showPausar(true); 122 focaBotoneraLateralService.showPausar(true);
117 focaBotoneraLateralService.showCancelar(true); 123 focaBotoneraLateralService.showCancelar(false);
118 focaBotoneraLateralService.showGuardar(true, $scope.guardar); 124 focaBotoneraLateralService.showGuardar(true, $scope.guardar);
125 focaBotoneraLateralService.addCustomButton('Salir', salir);
126
119 }); 127 });
120 128
121 if($scope.nuevo) { 129 $timeout(function() {getLSChofer();});
130
131 if ($scope.nuevo) {
122 focaAbmChoferService 132 focaAbmChoferService
123 .getTransportistaPorId($routeParams.idTransportista) 133 .getTransportistaPorId($routeParams.idTransportista)
124 .then(function(res) { 134 .then(function(res) {
125 var codigo = ('00000' + res.data.COD).slice(-5); 135 var codigo = ('00000' + res.data.COD).slice(-5);
126 $scope.chofer.idTransportista = res.data.COD; 136 $scope.chofer.idTransportista = res.data.COD;
127 $scope.chofer.transportista = res.data; 137 $scope.chofer.transportista = res.data;
128 $scope.$broadcast('addCabecera', { 138 $scope.$broadcast('addCabecera', {
129 label: 'Transportista:', 139 label: 'Transportista:',
130 valor: codigo + ' - ' + res.data.NOM 140 valor: codigo + ' - ' + res.data.NOM
131 }); 141 });
132 }); 142 });
133 } 143 }
134 144
135 focaAbmChoferService.getChofer($routeParams.id).then(function(res) { 145 focaAbmChoferService.getChofer($routeParams.id).then(function(res) {
136 if (res.data) { 146 if (res.data) {
137 var codigo = ('00000' + res.data.transportista.COD).slice(-5); 147 var codigo = ('00000' + res.data.transportista.COD).slice(-5);
138 $scope.chofer = res.data; 148 $scope.chofer = res.data;
139 $scope.$broadcast('addCabecera', { 149 $scope.$broadcast('addCabecera', {
140 label: 'Transportista:', 150 label: 'Transportista:',
141 valor: codigo + ' - ' + res.data.transportista.NOM 151 valor: codigo + ' - ' + res.data.transportista.NOM
142 }); 152 });
143 } 153 }
144 }); 154 });
145 155
146 focaAbmChoferService.getTransportistas().then(function(res) { 156 focaAbmChoferService.getTransportistas().then(function(res) {
147 $scope.transportistas = res.data; 157 $scope.transportistas = res.data;
148 }); 158 });
149 159
160 function setearChofer(chofer) {
161 $scope.chofer = chofer;
162 $scope.$broadcast('addCabecera', {
163 label: 'Transportista:',
164 valor: $filter('rellenarDigitos')(chofer.idTransportista.chofer, 2) + ' - '
165 });
166 }
167 function getLSChofer() {
168 var chofer = JSON.parse($localStorage.chofer || null);
169
170 if (chofer) {
171 setearChofer(chofer);
172 delete $localStorage.chofer;
173 }
174 }
175
150 $scope.cancelar = function() { 176 $scope.cancelar = function() {
151 $location.path('/chofer'); 177 $location.path('/chofer');
152 }; 178 };
153 179
154 $scope.guardar = function(key) { 180 $scope.guardar = function(key) {
155 181
156 if (!$scope.chofer.nombre) { 182 if (!$scope.chofer.nombre) {
157 focaModalService.alert('Ingrese nombre'); 183 focaModalService.alert('Ingrese nombre');
158 return; 184 return;
159 } else if (!$scope.chofer.idTipoDocumento) { 185 } else if (!$scope.chofer.idTipoDocumento) {
160 focaModalService.alert('Seleccione tipo de documento'); 186 focaModalService.alert('Seleccione tipo de documento');
161 return; 187 return;
162 } else if (!$scope.chofer.dni){ 188 } else if (!$scope.chofer.dni) {
163 focaModalService.alert('Ingrese DNI'); 189 focaModalService.alert('Ingrese DNI');
164 return; 190 return;
165 } 191 }
166 192
167 key = (typeof key === 'undefined') ? 13 : key; 193 key = (typeof key === 'undefined') ? 13 : key;
168 if(key === 13) { 194 if (key === 13) {
169 validaDni().then(function() { 195 validaDni().then(function() {
170 $scope.chofer.idTransportista = $routeParams.idTransportista; 196 $scope.chofer.idTransportista = $routeParams.idTransportista;
171 delete $scope.chofer.transportista; 197 delete $scope.chofer.transportista;
172 focaAbmChoferService.guardarChofer($scope.chofer).then(function() { 198 focaAbmChoferService.guardarChofer($scope.chofer).then(function() {
173 $location.path('/chofer'); 199 $location.path('/chofer');
174 }); 200 });
175 }, function() { 201 }, function() {
176 focaModalService.alert('Dni existente'); 202 focaModalService.alert('Dni existente');
177 }); 203 });
178 } 204 }
179 }; 205 };
206
207 $scope.$watch('chofer', function(newValue) {
208 focaBotoneraLateralService.setPausarData({
209 label: 'chofer',
210 val: {
211 dni: newValue.dni,
212 idTransportista: newValue.idTransportista,
213 nombre: newValue.nombre,
214 telefono: newValue.telefono,
215 idTipoDocumento: newValue.idTipoDocumento,
216 id: newValue.id
217 }
218 });
219 }, true);
220
221 function salir() {
222 if ($scope.formChofer.$pristine == false) {
223 focaModalService.confirm(
224 '¿Está seguro de que desea salir? Se perderán todos los datos cargados.'
225 ).then(function(data) {
226 if (data) {
227 $location.path('/chofer');
228 }
229 });
230 } else {
231 $location.path('/chofer');
232 }
233 }
180 234
181 function validaDni() { 235 function validaDni() {
182 return new Promise(function(resolve, reject) { 236 return new Promise(function(resolve, reject) {
183 focaAbmChoferService 237 focaAbmChoferService
184 .getChoferPorDni($scope.chofer.dni) 238 .getChoferPorDni($scope.chofer.dni)
185 .then(function(res) { 239 .then(function(res) {
186 if(res.data.id && 240 if (res.data.id &&
187 $scope.chofer.id !== res.data.id) { 241 $scope.chofer.id !== res.data.id) {
188 reject(res.data); 242 reject(res.data);
189 }else { 243 } else {
190 resolve(); 244 resolve();
191 } 245 }
192 }); 246 });
193 }); 247 });
194 } 248 }
195 } 249 }
196 ]); 250 ]);
197 251