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