Commit b47480a700360f0fa8a71778fd633546e97a851c
1 parent
58888f8022
Exists in
master
Timeout para agregar cisterna
Showing
1 changed file
with
1 additions
and
2 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', '$localStorage', | 4 | '$uibModal', 'focaBotoneraLateralService', '$timeout', '$localStorage', |
5 | function($scope, focaAbmVehiculoService, $location, focaModalService, | 5 | function($scope, focaAbmVehiculoService, $location, focaModalService, |
6 | $uibModal, focaBotoneraLateralService, $timeout, $localStorage) { | 6 | $uibModal, focaBotoneraLateralService, $timeout, $localStorage) { |
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 | focaAbmVehiculoService.cleanCisternas(); | 14 | focaAbmVehiculoService.cleanCisternas(); |
15 | 15 | ||
16 | //SETEO BOTONERA LATERAL | 16 | //SETEO BOTONERA LATERAL |
17 | focaBotoneraLateralService.showSalir(false); | 17 | focaBotoneraLateralService.showSalir(false); |
18 | focaBotoneraLateralService.showPausar(false); | 18 | focaBotoneraLateralService.showPausar(false); |
19 | focaBotoneraLateralService.showCancelar(false); | 19 | focaBotoneraLateralService.showCancelar(false); |
20 | focaBotoneraLateralService.showGuardar(false); | 20 | focaBotoneraLateralService.showGuardar(false); |
21 | focaBotoneraLateralService.addCustomButton('Salir', salir); | 21 | focaBotoneraLateralService.addCustomButton('Salir', salir); |
22 | 22 | ||
23 | if (focaAbmVehiculoService.transportistaSeleccionado.COD) { | 23 | if (focaAbmVehiculoService.transportistaSeleccionado.COD) { |
24 | elegirTransportista(focaAbmVehiculoService.transportistaSeleccionado); | 24 | elegirTransportista(focaAbmVehiculoService.transportistaSeleccionado); |
25 | } | 25 | } |
26 | $scope.editar = function(id) { | 26 | $scope.editar = function(id) { |
27 | $location.path('/vehiculo/' + id + '/' + $scope.idTransportista); | 27 | $location.path('/vehiculo/' + id + '/' + $scope.idTransportista); |
28 | }; | 28 | }; |
29 | $scope.solicitarConfirmacion = function(vehiculo) { | 29 | $scope.solicitarConfirmacion = function(vehiculo) { |
30 | focaModalService.confirm('¿Está seguro que desea borrar el vehiculo ' + | 30 | focaModalService.confirm('¿Está seguro que desea borrar el vehiculo ' + |
31 | vehiculo.id + ' ' + vehiculo.tractor + ' ?').then( | 31 | vehiculo.id + ' ' + vehiculo.tractor + ' ?').then( |
32 | function(data) { | 32 | function(data) { |
33 | if (data) { | 33 | if (data) { |
34 | focaAbmVehiculoService.deleteVehiculo(vehiculo.id); | 34 | focaAbmVehiculoService.deleteVehiculo(vehiculo.id); |
35 | $scope.vehiculos.splice($scope.vehiculos.indexOf(vehiculo), 1); | 35 | $scope.vehiculos.splice($scope.vehiculos.indexOf(vehiculo), 1); |
36 | } | 36 | } |
37 | } | 37 | } |
38 | ); | 38 | ); |
39 | }; | 39 | }; |
40 | $scope.seleccionarTransportista = function() { | 40 | $scope.seleccionarTransportista = function() { |
41 | var parametrosModal = { | 41 | var parametrosModal = { |
42 | titulo: 'Búsqueda de Transportista', | 42 | titulo: 'Búsqueda de Transportista', |
43 | query: '/transportista', | 43 | query: '/transportista', |
44 | columnas: [ | 44 | columnas: [ |
45 | { | 45 | { |
46 | nombre: 'Código', | 46 | nombre: 'Código', |
47 | propiedad: 'COD' | 47 | propiedad: 'COD' |
48 | }, | 48 | }, |
49 | { | 49 | { |
50 | nombre: 'Nombre', | 50 | nombre: 'Nombre', |
51 | propiedad: 'NOM' | 51 | propiedad: 'NOM' |
52 | }, | 52 | }, |
53 | { | 53 | { |
54 | nombre: 'CUIT', | 54 | nombre: 'CUIT', |
55 | propiedad: 'CUIT' | 55 | propiedad: 'CUIT' |
56 | } | 56 | } |
57 | ] | 57 | ] |
58 | }; | 58 | }; |
59 | focaModalService.modal(parametrosModal).then( | 59 | focaModalService.modal(parametrosModal).then( |
60 | function(transportista) { | 60 | function(transportista) { |
61 | elegirTransportista(transportista); | 61 | elegirTransportista(transportista); |
62 | focaAbmVehiculoService.transportistaSeleccionado = transportista; | 62 | focaAbmVehiculoService.transportistaSeleccionado = transportista; |
63 | }, function() { | 63 | }, function() { |
64 | 64 | ||
65 | } | 65 | } |
66 | ); | 66 | ); |
67 | }; | 67 | }; |
68 | function elegirTransportista(transportista) { | 68 | function elegirTransportista(transportista) { |
69 | var codigo = ('00000' + transportista.COD).slice(-5); | 69 | var codigo = ('00000' + transportista.COD).slice(-5); |
70 | $scope.idTransportista = transportista.COD; | 70 | $scope.idTransportista = transportista.COD; |
71 | $scope.filtros = transportista.NOM.trim(); | 71 | $scope.filtros = transportista.NOM.trim(); |
72 | $timeout(function() { | 72 | $timeout(function() { |
73 | $scope.$broadcast('addCabecera', { | 73 | $scope.$broadcast('addCabecera', { |
74 | label: 'Transportista:', | 74 | label: 'Transportista:', |
75 | valor: codigo + ' - ' + transportista.NOM | 75 | valor: codigo + ' - ' + transportista.NOM |
76 | }); | 76 | }); |
77 | }); | 77 | }); |
78 | buscar(transportista.COD); | 78 | buscar(transportista.COD); |
79 | } | 79 | } |
80 | 80 | ||
81 | function buscar(idTransportista) { | 81 | function buscar(idTransportista) { |
82 | focaAbmVehiculoService | 82 | focaAbmVehiculoService |
83 | .getVehiculosPorTransportista(idTransportista) | 83 | .getVehiculosPorTransportista(idTransportista) |
84 | .then(function(datos) { | 84 | .then(function(datos) { |
85 | $scope.vehiculos = datos.data; | 85 | $scope.vehiculos = datos.data; |
86 | }); | 86 | }); |
87 | } | 87 | } |
88 | function salir() { | 88 | function salir() { |
89 | focaAbmVehiculoService.transportistaSeleccionado = {}; | 89 | focaAbmVehiculoService.transportistaSeleccionado = {}; |
90 | $location.path('/'); | 90 | $location.path('/'); |
91 | } | 91 | } |
92 | } | 92 | } |
93 | ]) | 93 | ]) |
94 | .controller('focaAbmVehiculoController', [ | 94 | .controller('focaAbmVehiculoController', [ |
95 | '$scope', 'focaAbmVehiculoService', '$routeParams', '$location', '$uibModal', | 95 | '$scope', 'focaAbmVehiculoService', '$routeParams', '$location', '$uibModal', |
96 | 'focaModalService', '$timeout', 'focaBotoneraLateralService', '$localStorage', '$filter', | 96 | 'focaModalService', '$timeout', 'focaBotoneraLateralService', '$localStorage', '$filter', |
97 | function($scope, focaAbmVehiculoService, $routeParams, $location, $uibModal, | 97 | function($scope, focaAbmVehiculoService, $routeParams, $location, $uibModal, |
98 | focaModalService, $timeout, focaBotoneraLateralService, $localStorage, $filter) { | 98 | focaModalService, $timeout, focaBotoneraLateralService, $localStorage, $filter) { |
99 | $scope.nuevo = $routeParams.idVehiculo === '0' ? true : false; | 99 | $scope.nuevo = $routeParams.idVehiculo === '0' ? true : false; |
100 | $scope.nuevoCisterna = ($routeParams.idx > -1) ? false : true; | 100 | $scope.nuevoCisterna = ($routeParams.idx > -1) ? false : true; |
101 | $scope.now = new Date(); | 101 | $scope.now = new Date(); |
102 | $scope.focused = 1; | 102 | $scope.focused = 1; |
103 | $scope.creando = false; | 103 | $scope.creando = false; |
104 | $scope.transportistaStamp = ''; | 104 | $scope.transportistaStamp = ''; |
105 | $scope.cisternasABorrar = []; | 105 | $scope.cisternasABorrar = []; |
106 | $scope.cisternas = []; | 106 | $scope.cisternas = []; |
107 | $scope.cisterna = { | 107 | $scope.cisterna = { |
108 | codigo: '', | 108 | codigo: '', |
109 | capacidad: '', | 109 | capacidad: '', |
110 | idUnidadMedida: 0, | 110 | idUnidadMedida: 0, |
111 | unidadMedida: {} | 111 | unidadMedida: {} |
112 | }; | 112 | }; |
113 | 113 | ||
114 | $timeout(function() { | 114 | $timeout(function() { |
115 | focaBotoneraLateralService.showSalir(false); | 115 | focaBotoneraLateralService.showSalir(false); |
116 | focaBotoneraLateralService.showPausar(true); | 116 | focaBotoneraLateralService.showPausar(true); |
117 | focaBotoneraLateralService.showCancelar(false); | 117 | focaBotoneraLateralService.showCancelar(false); |
118 | focaBotoneraLateralService.showGuardar(true, $scope.guardar); | 118 | focaBotoneraLateralService.showGuardar(true, $scope.guardar); |
119 | focaBotoneraLateralService.addCustomButton('Salir', $scope.salir); | 119 | focaBotoneraLateralService.addCustomButton('Salir', $scope.salir); |
120 | }); | 120 | }); |
121 | 121 | ||
122 | if ($scope.nuevo) { | 122 | if ($scope.nuevo) { |
123 | focaAbmVehiculoService | 123 | focaAbmVehiculoService |
124 | .getTransportistaPorId($routeParams.idTransportista) | 124 | .getTransportistaPorId($routeParams.idTransportista) |
125 | .then(function(res) { | 125 | .then(function(res) { |
126 | var codigo = ('00000' + res.data.COD).slice(-5); | 126 | var codigo = ('00000' + res.data.COD).slice(-5); |
127 | $scope.vehiculo.idTransportista = res.data.COD; | 127 | $scope.vehiculo.idTransportista = res.data.COD; |
128 | $scope.vehiculo.transportista = res.data; | 128 | $scope.vehiculo.transportista = res.data; |
129 | $scope.$broadcast('addCabecera', { | 129 | $scope.$broadcast('addCabecera', { |
130 | label: 'Transportista:', | 130 | label: 'Transportista:', |
131 | valor: codigo + ' - ' + res.data.NOM | 131 | valor: codigo + ' - ' + res.data.NOM |
132 | }); | 132 | }); |
133 | }); | 133 | }); |
134 | } | 134 | } |
135 | $scope.vehiculo = {}; | 135 | $scope.vehiculo = {}; |
136 | 136 | ||
137 | focaAbmVehiculoService.getVehiculo($routeParams.idVehiculo).then(function(res) { | 137 | focaAbmVehiculoService.getVehiculo($routeParams.idVehiculo).then(function(res) { |
138 | 138 | ||
139 | if (res.data) { | 139 | if (res.data) { |
140 | var vehiculoSeteado = getLSVehiculo(); | 140 | var vehiculoSeteado = getLSVehiculo(); |
141 | if (vehiculoSeteado === false) { | 141 | if (vehiculoSeteado === false) { |
142 | $scope.transportistaStamp = ('00000' + res.data.transportista.COD).slice(-5); | 142 | $scope.transportistaStamp = ('00000' + res.data.transportista.COD).slice(-5); |
143 | $scope.transportistaStamp += ' - ' + res.data.transportista.NOM; | 143 | $scope.transportistaStamp += ' - ' + res.data.transportista.NOM; |
144 | 144 | ||
145 | $scope.vehiculo = res.data; | 145 | $scope.vehiculo = res.data; |
146 | $scope.$broadcast('addCabecera', { | 146 | $scope.$broadcast('addCabecera', { |
147 | label: 'Transportista:', | 147 | label: 'Transportista:', |
148 | valor: $scope.transportistaStamp | 148 | valor: $scope.transportistaStamp |
149 | }); | 149 | }); |
150 | $scope.$broadcast('addCabecera', { | 150 | $scope.$broadcast('addCabecera', { |
151 | label: 'Unidad:', | 151 | label: 'Unidad:', |
152 | valor: res.data.codigo | 152 | valor: res.data.codigo |
153 | }); | 153 | }); |
154 | } | 154 | } |
155 | 155 | ||
156 | focaAbmVehiculoService | 156 | focaAbmVehiculoService |
157 | .getCisternas($routeParams.idVehiculo) | 157 | .getCisternas($routeParams.idVehiculo) |
158 | .then(function(res) { | 158 | .then(function(res) { |
159 | $scope.cisternas = res; | 159 | $scope.cisternas = res; |
160 | $scope.$apply(); | 160 | $scope.$apply(); |
161 | }); | 161 | }); |
162 | } | 162 | } |
163 | }); | 163 | }); |
164 | 164 | ||
165 | $scope.next = function(key) { | 165 | $scope.next = function(key) { |
166 | if (key === 13) $scope.focused++; | 166 | if (key === 13) $scope.focused++; |
167 | }; | 167 | }; |
168 | 168 | ||
169 | $scope.salir = function() { | 169 | $scope.salir = function() { |
170 | if (!$scope.formVehiculo.$pristine && focaAbmVehiculoService.cisternasPristine) { | 170 | if (!$scope.formVehiculo.$pristine && focaAbmVehiculoService.cisternasPristine) { |
171 | focaModalService.confirm( | 171 | focaModalService.confirm( |
172 | '¿Está seguro de que desea salir? Se perderán todos los datos cargados.' | 172 | '¿Está seguro de que desea salir? Se perderán todos los datos cargados.' |
173 | ).then(function(data) { | 173 | ).then(function(data) { |
174 | if (data) { | 174 | if (data) { |
175 | $location.path('/vehiculo'); | 175 | $location.path('/vehiculo'); |
176 | } | 176 | } |
177 | }); | 177 | }); |
178 | } else { | 178 | } else { |
179 | $location.path('/vehiculo'); | 179 | $location.path('/vehiculo'); |
180 | } | 180 | } |
181 | }; | 181 | }; |
182 | 182 | ||
183 | $scope.editar = function(key, cisterna) { | 183 | $scope.editar = function(key, cisterna) { |
184 | if (key === 'nuevo') { | 184 | if (key === 'nuevo') { |
185 | $scope.cisterna = {}; | 185 | $scope.cisterna = {}; |
186 | $scope.creando = true; | 186 | $scope.creando = true; |
187 | } else { | 187 | } else { |
188 | $scope.cisterna = cisterna; | 188 | $scope.cisterna = cisterna; |
189 | $scope.creando = true; | 189 | $scope.creando = true; |
190 | } | 190 | } |
191 | }; | 191 | }; |
192 | 192 | ||
193 | $scope.seleccionarUnidadMedida = function() { | 193 | $scope.seleccionarUnidadMedida = function() { |
194 | var modalInstance = $uibModal.open( | 194 | var modalInstance = $uibModal.open( |
195 | { | 195 | { |
196 | ariaLabelledBy: 'Busqueda de Unidades de medida', | 196 | ariaLabelledBy: 'Busqueda de Unidades de medida', |
197 | templateUrl: 'modal-unidad-medida.html', | 197 | templateUrl: 'modal-unidad-medida.html', |
198 | controller: 'focaModalUnidadMedidaCtrl', | 198 | controller: 'focaModalUnidadMedidaCtrl', |
199 | size: 'lg' | 199 | size: 'lg' |
200 | } | 200 | } |
201 | ); | 201 | ); |
202 | modalInstance.result.then(function(unidaMedida) { | 202 | modalInstance.result.then(function(unidaMedida) { |
203 | $scope.cisterna.idUnidadMedida = unidaMedida.ID; | 203 | $scope.cisterna.idUnidadMedida = unidaMedida.ID; |
204 | $scope.cisterna.unidadMedida = unidaMedida; | 204 | $scope.cisterna.unidadMedida = unidaMedida; |
205 | }); | 205 | }); |
206 | }; | 206 | }; |
207 | 207 | ||
208 | $scope.guardar = function(key) { | 208 | $scope.guardar = function(key) { |
209 | key = (typeof key === 'undefined') ? 13 : key; | 209 | key = (typeof key === 'undefined') ? 13 : key; |
210 | 210 | ||
211 | if (key === 13) { | 211 | if (key === 13) { |
212 | 212 | ||
213 | if ($scope.formVehiculo.$pristine && focaAbmVehiculoService.cisternasPristine) { | 213 | if ($scope.formVehiculo.$pristine && focaAbmVehiculoService.cisternasPristine) { |
214 | $scope.salir(); | 214 | $scope.salir(); |
215 | return; | 215 | return; |
216 | } | 216 | } |
217 | 217 | ||
218 | if (!$scope.vehiculo.codigo) { | 218 | if (!$scope.vehiculo.codigo) { |
219 | focaModalService.alert('Ingrese unidad'); | 219 | focaModalService.alert('Ingrese unidad'); |
220 | return; | 220 | return; |
221 | } else if (!$scope.vehiculo.tractor) { | 221 | } else if (!$scope.vehiculo.tractor) { |
222 | focaModalService.alert('Ingrese dominio tractor'); | 222 | focaModalService.alert('Ingrese dominio tractor'); |
223 | return; | 223 | return; |
224 | } else if (!$scope.vehiculo.semi) { | 224 | } else if (!$scope.vehiculo.semi) { |
225 | focaModalService.alert('Ingrese dominio semi'); | 225 | focaModalService.alert('Ingrese dominio semi'); |
226 | return; | 226 | return; |
227 | } else if (!$scope.vehiculo.capacidad) { | 227 | } else if (!$scope.vehiculo.capacidad) { |
228 | focaModalService.alert('Ingrese capacidad total'); | 228 | focaModalService.alert('Ingrese capacidad total'); |
229 | return; | 229 | return; |
230 | } | 230 | } |
231 | //Valida si existe numero de unidad | 231 | //Valida si existe numero de unidad |
232 | if (!validaTotalCargas() && !$scope.nuevo) { | 232 | if (!validaTotalCargas() && !$scope.nuevo) { |
233 | focaModalService.alert('La suma de las capacidades de las cisternas' + | 233 | focaModalService.alert('La suma de las capacidades de las cisternas' + |
234 | ' debe ser igual a la capacidad total del vehículo'); | 234 | ' debe ser igual a la capacidad total del vehículo'); |
235 | return; | 235 | return; |
236 | } | 236 | } |
237 | 237 | ||
238 | borrarCisternas(); | 238 | borrarCisternas(); |
239 | 239 | ||
240 | validaCodigoUnidad().then(function() { | 240 | validaCodigoUnidad().then(function() { |
241 | delete $scope.vehiculo.transportista; | 241 | delete $scope.vehiculo.transportista; |
242 | delete $scope.vehiculo.cisternas; | 242 | delete $scope.vehiculo.cisternas; |
243 | focaAbmVehiculoService.guardarVehiculo($scope.vehiculo) | 243 | focaAbmVehiculoService.guardarVehiculo($scope.vehiculo) |
244 | .then(function(res) { | 244 | .then(function(res) { |
245 | if ($scope.nuevo) { | 245 | if ($scope.nuevo) { |
246 | $location.path('/vehiculo/' + res.data.id + | 246 | $location.path('/vehiculo/' + res.data.id + |
247 | '/' + res.data.idTransportista); | 247 | '/' + res.data.idTransportista); |
248 | } else { | 248 | } else { |
249 | guardarCisternas().then(function() { | 249 | guardarCisternas().then(function() { |
250 | $location.path('/vehiculo'); | 250 | $location.path('/vehiculo'); |
251 | }); | 251 | }); |
252 | } | 252 | } |
253 | }); | 253 | }); |
254 | }, function() { | 254 | }, function() { |
255 | focaModalService.alert('Código de unidad existente'); | 255 | focaModalService.alert('Código de unidad existente'); |
256 | }); | 256 | }); |
257 | } | 257 | } |
258 | }; | 258 | }; |
259 | 259 | ||
260 | //Agregar propiedades de cisterna | 260 | //Agregar propiedades de cisterna |
261 | $scope.$watch('vehiculo', function(newValue) { | 261 | $scope.$watch('vehiculo', function(newValue) { |
262 | focaBotoneraLateralService.setPausarData({ | 262 | focaBotoneraLateralService.setPausarData({ |
263 | label:'vehiculo', | 263 | label:'vehiculo', |
264 | val: { | 264 | val: { |
265 | codigo: newValue.codigo, | 265 | codigo: newValue.codigo, |
266 | tractor: newValue.tractor, | 266 | tractor: newValue.tractor, |
267 | semi: newValue.semi, | 267 | semi: newValue.semi, |
268 | capacidad: newValue.capacidad, | 268 | capacidad: newValue.capacidad, |
269 | idVehiculo: newValue.idVehiculo, | 269 | idVehiculo: newValue.idVehiculo, |
270 | idTransportista: newValue.idTransportista | 270 | idTransportista: newValue.idTransportista |
271 | } | 271 | } |
272 | }); | 272 | }); |
273 | }, true); | 273 | }, true); |
274 | 274 | ||
275 | function getLSVehiculo() { | 275 | function getLSVehiculo() { |
276 | var vehiculo = JSON.parse($localStorage.vehiculo|| null); | 276 | var vehiculo = JSON.parse($localStorage.vehiculo|| null); |
277 | if (vehiculo) { | 277 | if (vehiculo) { |
278 | setearVehiculo(vehiculo); | 278 | setearVehiculo(vehiculo); |
279 | delete $localStorage.vehiculo; | 279 | delete $localStorage.vehiculo; |
280 | return true; | 280 | return true; |
281 | } | 281 | } |
282 | return false; | 282 | return false; |
283 | } | 283 | } |
284 | 284 | ||
285 | function borrarCisternas() { | 285 | function borrarCisternas() { |
286 | $scope.cisternasABorrar.forEach(cisternaABorrar => { | 286 | $scope.cisternasABorrar.forEach(cisternaABorrar => { |
287 | focaAbmVehiculoService.deleteCisterna(cisternaABorrar); | 287 | focaAbmVehiculoService.deleteCisterna(cisternaABorrar); |
288 | focaAbmVehiculoService | 288 | focaAbmVehiculoService |
289 | .getCisternas($routeParams.idVehiculo) | 289 | .getCisternas($routeParams.idVehiculo) |
290 | .then(function(res) { | 290 | .then(function(res) { |
291 | $scope.cisternas = res; | 291 | $scope.cisternas = res; |
292 | }); | 292 | }); |
293 | }); | 293 | }); |
294 | } | 294 | } |
295 | 295 | ||
296 | function setearVehiculo(vehiculo) { | 296 | function setearVehiculo(vehiculo) { |
297 | $scope.vehiculo = vehiculo; | 297 | $scope.vehiculo = vehiculo; |
298 | $scope.$broadcast('addCabecera', { | 298 | $scope.$broadcast('addCabecera', { |
299 | label: 'Vehiculo:', | 299 | label: 'Vehiculo:', |
300 | valor: $filter('rellenarDigitos')(vehiculo.codigo) + ' - ' | 300 | valor: $filter('rellenarDigitos')(vehiculo.codigo) + ' - ' |
301 | }); | 301 | }); |
302 | } | 302 | } |
303 | 303 | ||
304 | $scope.solicitarConfirmacionTabla = function(cisterna, idx){ | 304 | $scope.solicitarConfirmacionTabla = function(cisterna, idx){ |
305 | focaModalService.confirm('¿Está seguro que desea borrar la cisterna ' + cisterna.id + ' ' + cisterna.codigo + ' ?') | 305 | focaModalService.confirm('¿Está seguro que desea borrar la cisterna ' + cisterna.id + ' ' + cisterna.codigo + ' ?') |
306 | .then( function(data) { | 306 | .then( function(data) { |
307 | if (data) { | 307 | if (data) { |
308 | $scope.cisternasABorrar.push(cisterna); | 308 | $scope.cisternasABorrar.push(cisterna); |
309 | $scope.cisternas.splice(idx,1); | 309 | $scope.cisternas.splice(idx,1); |
310 | } | 310 | } |
311 | }); | 311 | }); |
312 | return; | 312 | return; |
313 | }; | 313 | }; |
314 | 314 | ||
315 | function validaCodigoUnidad() { | 315 | function validaCodigoUnidad() { |
316 | return new Promise(function(resolve, reject) { | 316 | return new Promise(function(resolve, reject) { |
317 | focaAbmVehiculoService | 317 | focaAbmVehiculoService |
318 | .getVehiculosPorTransportista(parseInt($routeParams.idTransportista)) | 318 | .getVehiculosPorTransportista(parseInt($routeParams.idTransportista)) |
319 | .then(function(res) { | 319 | .then(function(res) { |
320 | //Valida si existe numero de unidad | 320 | //Valida si existe numero de unidad |
321 | var existe = res.data.filter(function(vehiculo) { | 321 | var existe = res.data.filter(function(vehiculo) { |
322 | return vehiculo.codigo === $scope.vehiculo.codigo && | 322 | return vehiculo.codigo === $scope.vehiculo.codigo && |
323 | vehiculo.id !== $scope.vehiculo.id; | 323 | vehiculo.id !== $scope.vehiculo.id; |
324 | }); | 324 | }); |
325 | 325 | ||
326 | if (existe.length) { | 326 | if (existe.length) { |
327 | reject(existe); | 327 | reject(existe); |
328 | } else { | 328 | } else { |
329 | resolve(); | 329 | resolve(); |
330 | } | 330 | } |
331 | }); | 331 | }); |
332 | }); | 332 | }); |
333 | } | 333 | } |
334 | 334 | ||
335 | function validaTotalCargas() { | 335 | function validaTotalCargas() { |
336 | var total = 0; | 336 | var total = 0; |
337 | $scope.cisternas.forEach(function(cisterna) { | 337 | $scope.cisternas.forEach(function(cisterna) { |
338 | if (!cisterna.desactivado) { | 338 | if (!cisterna.desactivado) { |
339 | total += parseInt(cisterna.capacidad); | 339 | total += parseInt(cisterna.capacidad); |
340 | } | 340 | } |
341 | }); | 341 | }); |
342 | return $scope.vehiculo.capacidad >= total; | 342 | return $scope.vehiculo.capacidad >= total; |
343 | } | 343 | } |
344 | function guardarCisternas() { | 344 | function guardarCisternas() { |
345 | var cisternas = $scope.cisternas.map(function(cisterna) { | 345 | var cisternas = $scope.cisternas.map(function(cisterna) { |
346 | return { | 346 | return { |
347 | id: cisterna.id, | 347 | id: cisterna.id, |
348 | capacidad: parseFloat(cisterna.capacidad), | 348 | capacidad: parseFloat(cisterna.capacidad), |
349 | codigo: cisterna.codigo, | 349 | codigo: cisterna.codigo, |
350 | idUnidadMedida: cisterna.idUnidadMedida, | 350 | idUnidadMedida: cisterna.idUnidadMedida, |
351 | idVehiculo: $routeParams.idVehiculo, | 351 | idVehiculo: $routeParams.idVehiculo, |
352 | desactivado: cisterna.desactivado | 352 | desactivado: cisterna.desactivado |
353 | }; | 353 | }; |
354 | }); | 354 | }); |
355 | 355 | ||
356 | return focaAbmVehiculoService.guardarCisternas(cisternas); | 356 | return focaAbmVehiculoService.guardarCisternas(cisternas); |
357 | } | 357 | } |
358 | 358 | ||
359 | $scope.agregarCisterna = function() { | 359 | $scope.agregarCisterna = function() { |
360 | if (!$scope.cisterna.codigo) { | 360 | if (!$scope.cisterna.codigo) { |
361 | focaModalService.alert('Ingrese codigo de cisterna'); | 361 | focaModalService.alert('Ingrese codigo de cisterna'); |
362 | return; | 362 | return; |
363 | } else if (!$scope.cisterna.capacidad) { | 363 | } else if (!$scope.cisterna.capacidad) { |
364 | focaModalService.alert('Ingrese capacidad'); | 364 | focaModalService.alert('Ingrese capacidad'); |
365 | return; | 365 | return; |
366 | } else if (!$scope.cisterna.idUnidadMedida) { | 366 | } else if (!$scope.cisterna.idUnidadMedida) { |
367 | focaModalService.alert('Ingrese unidad de medida'); | 367 | focaModalService.alert('Ingrese unidad de medida'); |
368 | return; | 368 | return; |
369 | } | 369 | } |
370 | $scope.creando = false; | 370 | $scope.creando = false; |
371 | if (!$scope.cisterna.id) { | 371 | if (!$scope.cisterna.id) { |
372 | validaCodigo() | 372 | validaCodigo() |
373 | .then(function() { | 373 | .then(function() { |
374 | saveCisterna(); | 374 | saveCisterna(); |
375 | }, function(err) { | 375 | }, function(err) { |
376 | focaModalService.alert(err); | 376 | focaModalService.alert(err); |
377 | }); | 377 | }); |
378 | } else { | 378 | } else { |
379 | saveCisterna(); | 379 | saveCisterna(); |
380 | } | 380 | } |
381 | }; | 381 | }; |
382 | 382 | ||
383 | function saveCisterna () { | 383 | function saveCisterna () { |
384 | $scope.cisterna.idVehiculo = parseInt($routeParams.idVehiculo); | 384 | $scope.cisterna.idVehiculo = parseInt($routeParams.idVehiculo); |
385 | delete $scope.cisterna.vehiculo; | 385 | delete $scope.cisterna.vehiculo; |
386 | if (!$scope.cisterna.id) { | 386 | if (!$scope.cisterna.id) { |
387 | $scope.cisternas.push($scope.cisterna); | 387 | $timeout(function() { $scope.cisternas.push($scope.cisterna);}); |
388 | $scope.$apply(); | ||
389 | } | 388 | } |
390 | } | 389 | } |
391 | 390 | ||
392 | if ($routeParams.idx !== -1) { | 391 | if ($routeParams.idx !== -1) { |
393 | $scope.cisterna = [$routeParams.idx]; | 392 | $scope.cisterna = [$routeParams.idx]; |
394 | focaAbmVehiculoService | 393 | focaAbmVehiculoService |
395 | .getCisternas($routeParams.idVehiculo) | 394 | .getCisternas($routeParams.idVehiculo) |
396 | .then(function(res) { | 395 | .then(function(res) { |
397 | $scope.cisterna = res[$routeParams.idx]; | 396 | $scope.cisterna = res[$routeParams.idx]; |
398 | }); | 397 | }); |
399 | } | 398 | } |
400 | 399 | ||
401 | function validaCodigo() { | 400 | function validaCodigo() { |
402 | return new Promise(function(resolve, reject) { | 401 | return new Promise(function(resolve, reject) { |
403 | focaAbmVehiculoService | 402 | focaAbmVehiculoService |
404 | .getCisternas($routeParams.idVehiculo) | 403 | .getCisternas($routeParams.idVehiculo) |
405 | .then(function(res) { | 404 | .then(function(res) { |
406 | var cisternas = res; | 405 | var cisternas = res; |
407 | var totalCargado = 0; | 406 | var totalCargado = 0; |
408 | cisternas.forEach(function(cisterna, idx) { | 407 | cisternas.forEach(function(cisterna, idx) { |
409 | //SI EL CODIGO YA EXISTE | 408 | //SI EL CODIGO YA EXISTE |
410 | if (cisterna.codigo === $scope.cisterna.codigo && | 409 | if (cisterna.codigo === $scope.cisterna.codigo && |
411 | idx !== $routeParams.idx && | 410 | idx !== $routeParams.idx && |
412 | !cisterna.desactivado) { | 411 | !cisterna.desactivado) { |
413 | reject('Código de cisterna existente'); | 412 | reject('Código de cisterna existente'); |
414 | } | 413 | } |
415 | if (idx !== $routeParams.idx && | 414 | if (idx !== $routeParams.idx && |
416 | !cisterna.desactivado) { | 415 | !cisterna.desactivado) { |
417 | totalCargado += cisterna.capacidad; | 416 | totalCargado += cisterna.capacidad; |
418 | } | 417 | } |
419 | }); | 418 | }); |
420 | resolve(); | 419 | resolve(); |
421 | }); | 420 | }); |
422 | }); | 421 | }); |
423 | } | 422 | } |
424 | } | 423 | } |
425 | ]); | 424 | ]); |
426 | 425 |