Commit 6aaf27eae052cfe25b13dd5edb729558cb89d32f
Exists in
master
and in
1 other branch
Merge branch 'master' into 'master'
Master See merge request !20
Showing
1 changed file
Show diff stats
src/js/controller.js
1 | angular.module('focaAbmVehiculo') | 1 | angular.module('focaAbmVehiculo') |
2 | .controller('focaAbmVehiculosController', [ | 2 | .controller('focaAbmVehiculosController', [ |
3 | '$scope', 'focaAbmVehiculoService', '$location', 'focaModalService', | 3 | '$scope', 'focaAbmVehiculoService', '$location', 'focaModalService', |
4 | '$uibModal', 'focaBotoneraLateralService', '$timeout', | 4 | '$uibModal', 'focaBotoneraLateralService', '$timeout', |
5 | function($scope, focaAbmVehiculoService, $location, focaModalService, | 5 | function($scope, focaAbmVehiculoService, $location, focaModalService, |
6 | $uibModal, focaBotoneraLateralService, $timeout) { | 6 | $uibModal, focaBotoneraLateralService, $timeout) { |
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 | //SETEO BOTONERA LATERAL | 14 | //SETEO BOTONERA LATERAL |
15 | focaBotoneraLateralService.showSalir(false); | 15 | focaBotoneraLateralService.showSalir(false); |
16 | focaBotoneraLateralService.showPausar(false); | 16 | focaBotoneraLateralService.showPausar(false); |
17 | focaBotoneraLateralService.showCancelar(false); | 17 | focaBotoneraLateralService.showCancelar(false); |
18 | focaBotoneraLateralService.showGuardar(false); | 18 | focaBotoneraLateralService.showGuardar(false); |
19 | focaBotoneraLateralService.addCustomButton('Salir', salir); | 19 | focaBotoneraLateralService.addCustomButton('Salir', salir); |
20 | 20 | ||
21 | if(focaAbmVehiculoService.transportistaSeleccionado.COD) { | 21 | if(focaAbmVehiculoService.transportistaSeleccionado.COD) { |
22 | elegirTransportista(focaAbmVehiculoService.transportistaSeleccionado); | 22 | elegirTransportista(focaAbmVehiculoService.transportistaSeleccionado); |
23 | } | 23 | } |
24 | focaAbmVehiculoService.cleanCisternasLocal(); | 24 | focaAbmVehiculoService.cleanCisternasLocal(); |
25 | $scope.editar = function(id) { | 25 | $scope.editar = function(id) { |
26 | $location.path('/vehiculo/' + id + '/' + $scope.idTransportista); | 26 | $location.path('/vehiculo/' + id + '/' + $scope.idTransportista); |
27 | }; | 27 | }; |
28 | $scope.solicitarConfirmacion = function(vehiculo) { | 28 | $scope.solicitarConfirmacion = function(vehiculo) { |
29 | focaModalService.confirm('¿Está seguro que desea borrar el vehiculo ' + | 29 | focaModalService.confirm('¿Está seguro que desea borrar el vehiculo ' + |
30 | vehiculo.id + ' ' + vehiculo.tractor + ' ?').then( | 30 | vehiculo.id + ' ' + vehiculo.tractor + ' ?').then( |
31 | function(data) { | 31 | function(data) { |
32 | if(data) { | 32 | if(data) { |
33 | focaAbmVehiculoService.deleteVehiculo(vehiculo.id); | 33 | focaAbmVehiculoService.deleteVehiculo(vehiculo.id); |
34 | $scope.vehiculos.splice($scope.vehiculos.indexOf(vehiculo), 1); | 34 | $scope.vehiculos.splice($scope.vehiculos.indexOf(vehiculo), 1); |
35 | } | 35 | } |
36 | } | 36 | } |
37 | ); | 37 | ); |
38 | }; | 38 | }; |
39 | $scope.seleccionarTransportista = function() { | 39 | $scope.seleccionarTransportista = function() { |
40 | var modalInstance = $uibModal.open( | 40 | var modalInstance = $uibModal.open( |
41 | { | 41 | { |
42 | ariaLabelledBy: 'Busqueda de Transportista', | 42 | ariaLabelledBy: 'Busqueda de Transportista', |
43 | templateUrl: 'modal-proveedor.html', | 43 | templateUrl: 'modal-proveedor.html', |
44 | controller: 'focaModalProveedorCtrl', | 44 | controller: 'focaModalProveedorCtrl', |
45 | size: 'lg', | 45 | size: 'lg', |
46 | resolve: { | 46 | resolve: { |
47 | transportista: function() { | 47 | transportista: function() { |
48 | return true; | 48 | return true; |
49 | } | 49 | } |
50 | } | 50 | } |
51 | } | 51 | } |
52 | ); | 52 | ); |
53 | modalInstance.result.then( | 53 | modalInstance.result.then( |
54 | function(transportista) { | 54 | function(transportista) { |
55 | elegirTransportista(transportista); | 55 | elegirTransportista(transportista); |
56 | focaAbmVehiculoService.transportistaSeleccionado = transportista; | 56 | focaAbmVehiculoService.transportistaSeleccionado = transportista; |
57 | }, function() { | 57 | }, function() { |
58 | 58 | ||
59 | } | 59 | } |
60 | ); | 60 | ); |
61 | }; | 61 | }; |
62 | 62 | ||
63 | function elegirTransportista(transportista) { | 63 | function elegirTransportista(transportista) { |
64 | var codigo = ('00000' + transportista.COD).slice(-5); | 64 | var codigo = ('00000' + transportista.COD).slice(-5); |
65 | $scope.idTransportista = transportista.COD; | 65 | $scope.idTransportista = transportista.COD; |
66 | $scope.filtros = transportista.NOM.trim(); | 66 | $scope.filtros = transportista.NOM.trim(); |
67 | $timeout(function() { | 67 | $timeout(function() { |
68 | $scope.$broadcast('addCabecera', { | 68 | $scope.$broadcast('addCabecera', { |
69 | label: 'Transportista:', | 69 | label: 'Transportista:', |
70 | valor: codigo + ' - ' + transportista.NOM | 70 | valor: codigo + ' - ' + transportista.NOM |
71 | }); | 71 | }); |
72 | }); | 72 | }); |
73 | buscar(transportista.COD); | 73 | buscar(transportista.COD); |
74 | } | 74 | } |
75 | 75 | ||
76 | function buscar(idTransportista) { | 76 | function buscar(idTransportista) { |
77 | focaAbmVehiculoService | 77 | focaAbmVehiculoService |
78 | .getVehiculosPorTransportista(idTransportista) | 78 | .getVehiculosPorTransportista(idTransportista) |
79 | .then(function(datos) { | 79 | .then(function(datos) { |
80 | $scope.vehiculos = datos.data; | 80 | $scope.vehiculos = datos.data; |
81 | }); | 81 | }); |
82 | } | 82 | } |
83 | function salir() { | 83 | function salir() { |
84 | focaAbmVehiculoService.transportistaSeleccionado = {}; | 84 | focaAbmVehiculoService.transportistaSeleccionado = {}; |
85 | $location.path('/'); | 85 | $location.path('/'); |
86 | } | 86 | } |
87 | } | 87 | } |
88 | ]) | 88 | ]) |
89 | .controller('focaAbmVehiculoController', [ | 89 | .controller('focaAbmVehiculoController', [ |
90 | '$scope', 'focaAbmVehiculoService', '$routeParams', '$location', '$uibModal', | 90 | '$scope', 'focaAbmVehiculoService', '$routeParams', '$location', '$uibModal', |
91 | 'focaModalService', '$timeout', 'focaBotoneraLateralService', '$window', | 91 | 'focaModalService', '$timeout', 'focaBotoneraLateralService', '$window', |
92 | function($scope, focaAbmVehiculoService, $routeParams, $location, $uibModal, | 92 | function($scope, focaAbmVehiculoService, $routeParams, $location, $uibModal, |
93 | focaModalService, $timeout, focaBotoneraLateralService, $window) { | 93 | focaModalService, $timeout, focaBotoneraLateralService, $window) { |
94 | $scope.nuevo = $routeParams.idVehiculo === '0' ? true : false; | 94 | $scope.nuevo = $routeParams.idVehiculo === '0' ? true : false; |
95 | $scope.now = new Date(); | 95 | $scope.now = new Date(); |
96 | $scope.focused = 1; | 96 | $scope.focused = 1; |
97 | $scope.transportistaStamp = ''; | 97 | $scope.transportistaStamp = ''; |
98 | $scope.cisternas = []; | 98 | $scope.cisternas = []; |
99 | 99 | ||
100 | $timeout(function() { | 100 | $timeout(function() { |
101 | focaBotoneraLateralService.showSalir(false); | 101 | focaBotoneraLateralService.showSalir(false); |
102 | focaBotoneraLateralService.showPausar(false); | 102 | focaBotoneraLateralService.showPausar(false); |
103 | focaBotoneraLateralService.showCancelar(false); | 103 | focaBotoneraLateralService.showCancelar(false); |
104 | focaBotoneraLateralService.showGuardar(true, $scope.guardar); | 104 | focaBotoneraLateralService.showGuardar(true, $scope.guardar); |
105 | focaBotoneraLateralService.addCustomButton('Salir', $scope.cancelar); | 105 | focaBotoneraLateralService.addCustomButton('Salir', $scope.cancelar); |
106 | }); | 106 | }); |
107 | 107 | ||
108 | if($scope.nuevo) { | 108 | if($scope.nuevo) { |
109 | focaAbmVehiculoService | 109 | focaAbmVehiculoService |
110 | .getTransportistaPorId($routeParams.idTransportista) | 110 | .getTransportistaPorId($routeParams.idTransportista) |
111 | .then(function(res) { | 111 | .then(function(res) { |
112 | var codigo = ('00000' + res.data.COD).slice(-5); | 112 | var codigo = ('00000' + res.data.COD).slice(-5); |
113 | $scope.vehiculo.idTransportista = res.data.COD; | 113 | $scope.vehiculo.idTransportista = res.data.COD; |
114 | $scope.vehiculo.transportista = res.data; | 114 | $scope.vehiculo.transportista = res.data; |
115 | $scope.$broadcast('addCabecera', { | 115 | $scope.$broadcast('addCabecera', { |
116 | label: 'Transportista:', | 116 | label: 'Transportista:', |
117 | valor: codigo + ' - ' + res.data.NOM | 117 | valor: codigo + ' - ' + res.data.NOM |
118 | }); | 118 | }); |
119 | }); | 119 | }); |
120 | } | 120 | } |
121 | $scope.vehiculo = {}; | 121 | $scope.vehiculo = {}; |
122 | focaAbmVehiculoService.getVehiculo($routeParams.idVehiculo).then(function(res) { | 122 | focaAbmVehiculoService.getVehiculo($routeParams.idVehiculo).then(function(res) { |
123 | if(res.data) { | 123 | if(res.data) { |
124 | $scope.transportistaStamp = ('00000' + res.data.transportista.COD).slice(-5); | 124 | $scope.transportistaStamp = ('00000' + res.data.transportista.COD).slice(-5); |
125 | $scope.transportistaStamp += ' - ' + res.data.transportista.NOM | 125 | $scope.transportistaStamp += ' - ' + res.data.transportista.NOM |
126 | 126 | ||
127 | $scope.vehiculo = res.data; | 127 | $scope.vehiculo = res.data; |
128 | $scope.$broadcast('addCabecera', { | 128 | $scope.$broadcast('addCabecera', { |
129 | label: 'Transportista:', | 129 | label: 'Transportista:', |
130 | valor: $scope.transportistaStamp | 130 | valor: $scope.transportistaStamp |
131 | }); | 131 | }); |
132 | $scope.$broadcast('addCabecera', { | 132 | $scope.$broadcast('addCabecera', { |
133 | label: 'Unidad:', | 133 | label: 'Unidad:', |
134 | valor: res.data.codigo | 134 | valor: res.data.codigo |
135 | }); | 135 | }); |
136 | } | 136 | } |
137 | }); | 137 | }); |
138 | focaAbmVehiculoService.getCisternadoPorVehiculo($routeParams.idVehiculo) | 138 | focaAbmVehiculoService.getCisternadoPorVehiculo($routeParams.idVehiculo) |
139 | .then(function(res) { | 139 | .then(function(res) { |
140 | //CONCATENA LAS CISTERNAS DEL SERVICIO Y LAS DE LA BASE DE DATOS | 140 | //CONCATENA LAS CISTERNAS DEL SERVICIO Y LAS DE LA BASE DE DATOS |
141 | $scope.cisternas = focaAbmVehiculoService.getCisternasLocal().concat(res.data); | 141 | $scope.cisternas = focaAbmVehiculoService.getCisternasLocal().concat(res.data); |
142 | }); | 142 | }); |
143 | $scope.next = function(key) { | 143 | $scope.next = function(key) { |
144 | if (key === 13) $scope.focused++; | 144 | if (key === 13) $scope.focused++; |
145 | }; | 145 | }; |
146 | $scope.cancelar = function() { | 146 | $scope.cancelar = function() { |
147 | $location.path('/vehiculo'); | 147 | $location.path('/vehiculo'); |
148 | }; | 148 | }; |
149 | $scope.editar = function(cisterna) { | 149 | $scope.editar = function(cisterna) { |
150 | if(cisterna) { | 150 | if(cisterna) { |
151 | if(cisterna.idTemp != undefined) { | 151 | if(cisterna.idTemp != undefined) { |
152 | $location.path('/vehiculo/' + $routeParams.idVehiculo + | 152 | $location.path('/vehiculo/' + $routeParams.idVehiculo + |
153 | '/cisterna/0/' + cisterna.idTemp); | 153 | '/cisterna/0/' + cisterna.idTemp); |
154 | }else { | 154 | }else { |
155 | $location.path('/vehiculo/' + $routeParams.idVehiculo + | 155 | $location.path('/vehiculo/' + $routeParams.idVehiculo + |
156 | '/cisterna/' + cisterna.id); | 156 | '/cisterna/' + cisterna.id); |
157 | } | 157 | } |
158 | }else { | 158 | }else { |
159 | $location.path('/vehiculo/' + $routeParams.idVehiculo + '/cisterna/0/'); | 159 | $location.path('/vehiculo/' + $routeParams.idVehiculo + '/cisterna/0/'); |
160 | } | 160 | } |
161 | }; | 161 | }; |
162 | $scope.guardar = function(key) { | 162 | $scope.guardar = function(key) { |
163 | key = (typeof key === 'undefined') ? 13 : key; | 163 | key = (typeof key === 'undefined') ? 13 : key; |
164 | if(key === 13){ | 164 | if(key === 13){ |
165 | //Valida si existe numero de unidad | 165 | //Valida si existe numero de unidad |
166 | if(!validaTotalCargas() && !$scope.nuevo) { | 166 | if(!validaTotalCargas() && !$scope.nuevo) { |
167 | focaModalService.alert('La suma de las capacidades de las cisternas' + | 167 | focaModalService.alert('La suma de las capacidades de las cisternas' + |
168 | ' debe ser igual a la capacidad total del vehículo'); | 168 | ' debe ser igual a la capacidad total del vehículo'); |
169 | return; | 169 | return; |
170 | } | 170 | } |
171 | validaCodigoUnidad().then(function() { | 171 | validaCodigoUnidad().then(function() { |
172 | delete $scope.vehiculo.transportista; | 172 | delete $scope.vehiculo.transportista; |
173 | delete $scope.vehiculo.cisternas; | 173 | delete $scope.vehiculo.cisternas; |
174 | focaAbmVehiculoService.guardarVehiculo($scope.vehiculo) | 174 | focaAbmVehiculoService.guardarVehiculo($scope.vehiculo) |
175 | .then(function(res) { | 175 | .then(function(res) { |
176 | if($scope.nuevo) { | 176 | if($scope.nuevo) { |
177 | $location.path('/vehiculo/' + res.data.id + | 177 | $location.path('/vehiculo/' + res.data.id + |
178 | '/' + res.data.idTransportista); | 178 | '/' + res.data.idTransportista); |
179 | }else { | 179 | }else { |
180 | guardarCisternas().then(function() { | 180 | guardarCisternas().then(function() { |
181 | $window.location.assign('/#!/vehiculo'); | 181 | $window.location.assign('/#!/vehiculo'); |
182 | }); | 182 | }); |
183 | } | 183 | } |
184 | }); | 184 | }); |
185 | }, function(){ | 185 | }, function(){ |
186 | focaModalService.alert('Código de unidad existente'); | 186 | focaModalService.alert('Código de unidad existente'); |
187 | }); | 187 | }); |
188 | } | 188 | } |
189 | 189 | ||
190 | }; | 190 | }; |
191 | $scope.solicitarConfirmacionCisterna = function(cisterna) { | 191 | $scope.solicitarConfirmacionCisterna = function(cisterna) { |
192 | focaModalService.confirm('¿Está seguro que desea borrar la cisterna ' + | 192 | focaModalService.confirm('¿Está seguro que desea borrar la cisterna ' + |
193 | cisterna.id + ' ' + cisterna.codigo + ' ?').then( | 193 | cisterna.id + ' ' + cisterna.codigo + ' ?').then( |
194 | function(data) { | 194 | function(data) { |
195 | if(data) { | 195 | if(data) { |
196 | if(cisterna.id) { | 196 | if(cisterna.id) { |
197 | focaAbmVehiculoService.deleteCisterna(cisterna.id); | 197 | focaAbmVehiculoService.deleteCisterna(cisterna.id); |
198 | } | 198 | } |
199 | $scope.cisternas.splice($scope.cisternas.indexOf(cisterna), 1); | 199 | $scope.cisternas.splice($scope.cisternas.indexOf(cisterna), 1); |
200 | } | 200 | } |
201 | } | 201 | } |
202 | ); | 202 | ); |
203 | }; | 203 | }; |
204 | 204 | ||
205 | function validaCodigoUnidad() { | 205 | function validaCodigoUnidad() { |
206 | return new Promise(function(resolve, reject) { | 206 | return new Promise(function(resolve, reject) { |
207 | focaAbmVehiculoService | 207 | focaAbmVehiculoService |
208 | .getVehiculosPorTransportista(parseInt($routeParams.idTransportista)) | 208 | .getVehiculosPorTransportista(parseInt($routeParams.idTransportista)) |
209 | .then(function(res) { | 209 | .then(function(res) { |
210 | //Valida si existe numero de unidad | 210 | //Valida si existe numero de unidad |
211 | var existe = res.data.filter(function(vehiculo) { | 211 | var existe = res.data.filter(function(vehiculo) { |
212 | return vehiculo.codigo === $scope.vehiculo.codigo && | 212 | return vehiculo.codigo === $scope.vehiculo.codigo && |
213 | vehiculo.id !== $scope.vehiculo.id; | 213 | vehiculo.id !== $scope.vehiculo.id; |
214 | }); | 214 | }); |
215 | 215 | ||
216 | if(existe.length) { | 216 | if(existe.length) { |
217 | reject(existe); | 217 | reject(existe); |
218 | }else { | 218 | }else { |
219 | resolve(); | 219 | resolve(); |
220 | } | 220 | } |
221 | }); | 221 | }); |
222 | }); | 222 | }); |
223 | } | 223 | } |
224 | 224 | ||
225 | function validaTotalCargas() { | 225 | function validaTotalCargas() { |
226 | var total = 0; | 226 | var total = 0; |
227 | $scope.cisternas.forEach(function(cisterna) { | 227 | $scope.cisternas.forEach(function(cisterna) { |
228 | total += parseInt(cisterna.capacidad); | 228 | total += parseInt(cisterna.capacidad); |
229 | }); | 229 | }); |
230 | return $scope.vehiculo.capacidad == total; | 230 | return $scope.vehiculo.capacidad == total; |
231 | } | 231 | } |
232 | 232 | ||
233 | function guardarCisternas() { | 233 | function guardarCisternas() { |
234 | var promesas = []; | 234 | var promesas = []; |
235 | 235 | ||
236 | focaAbmVehiculoService.getCisternasLocal().forEach(function(cisterna) { | 236 | focaAbmVehiculoService.getCisternasLocal().forEach(function(cisterna) { |
237 | delete cisterna.unidadMedida; | 237 | delete cisterna.unidadMedida; |
238 | delete cisterna.idTemp; | 238 | delete cisterna.idTemp; |
239 | delete cisterna.cisternasCarga; | ||
239 | promesas.push(focaAbmVehiculoService.guardarCisterna(cisterna)); | 240 | promesas.push(focaAbmVehiculoService.guardarCisterna(cisterna)); |
240 | }); | 241 | }); |
241 | return Promise.all(promesas); | 242 | return Promise.all(promesas); |
242 | } | 243 | } |
243 | } | 244 | } |
244 | ]); | 245 | ]); |
245 | 246 |