Commit 2ed23925a5e8e45960650196e79c33f88acb165f
Exists in
master
Merge branch 'master' into 'develop'
Master(efernandez) See merge request !16
Showing
1 changed file
Show diff stats
src/js/controller.js
1 | angular.module('focaModalDetalleHojaRuta') | 1 | angular.module('focaModalDetalleHojaRuta') |
2 | .controller('focaModalDetalleHojaRutaController', | 2 | .controller('focaModalDetalleHojaRutaController', |
3 | [ | 3 | [ |
4 | '$filter', | 4 | '$filter', |
5 | '$scope', | 5 | '$scope', |
6 | '$uibModalInstance', | 6 | '$uibModalInstance', |
7 | 'parametros', | 7 | 'parametros', |
8 | 'focaModalDetalleHojaRutaService', | 8 | 'focaModalDetalleHojaRutaService', |
9 | 'focaModalService', | 9 | 'focaModalService', |
10 | 'focaSeguimientoService', | 10 | 'focaSeguimientoService', |
11 | '$uibModal', | 11 | '$uibModal', |
12 | function($filter, $scope, $uibModalInstance, parametros, focaModalDetalleHojaRutaService, | 12 | function($filter, $scope, $uibModalInstance, parametros, focaModalDetalleHojaRutaService, |
13 | focaModalService, focaSeguimientoService, $uibModal) | 13 | focaModalService, focaSeguimientoService, $uibModal) |
14 | { | 14 | { |
15 | //Seteo variables | 15 | //Seteo variables |
16 | $scope.remito = parametros.remito; | 16 | $scope.remito = parametros.remito; |
17 | $scope.cisternas = parametros.remito.cisternas; | 17 | $scope.cisternas = parametros.remito.cisternas; |
18 | $scope.articuloSeleccionado = {}; | 18 | $scope.articuloSeleccionado = {}; |
19 | $scope.aDescargar = []; | 19 | $scope.aDescargar = []; |
20 | $scope.cargando = false; | 20 | $scope.cargando = false; |
21 | 21 | ||
22 | //Datos | 22 | //Datos |
23 | // var promesaRemito = focaModalDetalleHojaRutaService | 23 | // var promesaRemito = focaModalDetalleHojaRutaService |
24 | // .getRemitoById(idRemito); | 24 | // .getRemitoById(idRemito); |
25 | // var promesaCisternas = focaModalDetalleHojaRutaService | 25 | // var promesaCisternas = focaModalDetalleHojaRutaService |
26 | // .getCisternasByIdRemito(idRemito); | 26 | // .getCisternasByIdRemito(idRemito); |
27 | 27 | ||
28 | // Promise.all([promesaRemito, promesaCisternas]).then(function(res) { | 28 | // Promise.all([promesaRemito, promesaCisternas]).then(function(res) { |
29 | // $scope.cargando = false; | 29 | // $scope.cargando = false; |
30 | // $scope.remito = res[0].data; | 30 | // $scope.remito = res[0].data; |
31 | // $scope.cisternas = res[1].data; | 31 | // $scope.cisternas = res[1].data; |
32 | // var articuloAChequear = $scope.remito.articulosRemito.filter( | 32 | // var articuloAChequear = $scope.remito.articulosRemito.filter( |
33 | // function(articulo) { | 33 | // function(articulo) { |
34 | // return !articulo.descargado; | 34 | // return !articulo.descargado; |
35 | // }); | 35 | // }); |
36 | // if(!articuloAChequear.length || $scope.remito.rechazado) { | 36 | // if(!articuloAChequear.length || $scope.remito.rechazado) { |
37 | // $scope.readonly = true; | 37 | // $scope.readonly = true; |
38 | // $scope.cambio($scope.remito.articulosRemito[0]); | 38 | // $scope.cambio($scope.remito.articulosRemito[0]); |
39 | // }else { | 39 | // }else { |
40 | // $scope.cambio(articuloAChequear[0]); | 40 | // $scope.cambio(articuloAChequear[0]); |
41 | // } | 41 | // } |
42 | // $scope.$digest(); | 42 | // $scope.$digest(); |
43 | // }, function() { | 43 | // }, function() { |
44 | // focaModalService.alert('El servicio no responde intente más tarde'); | 44 | // focaModalService.alert('El servicio no responde intente más tarde'); |
45 | // $uibModalInstance.dismiss(); | 45 | // $uibModalInstance.dismiss(); |
46 | // }); | 46 | // }); |
47 | $scope.cambio = function(articulo) { | 47 | $scope.cambio = function(articulo) { |
48 | if(!$scope.articuloSeleccionado.descargado) { | 48 | if(!$scope.articuloSeleccionado.descargado) { |
49 | $scope.articuloSeleccionado.cantidadDescargada = 0; | 49 | $scope.articuloSeleccionado.cantidadDescargada = 0; |
50 | } | 50 | } |
51 | $scope.aDescargar = []; | 51 | $scope.aDescargar = []; |
52 | $scope.articuloSeleccionado = articulo; | 52 | $scope.articuloSeleccionado = articulo; |
53 | }; | 53 | }; |
54 | $scope.descargar = function(key) { | 54 | $scope.descargar = function(key) { |
55 | if(key === 13) { | 55 | if(key === 13) { |
56 | $scope.cargando = true; | 56 | $scope.cargando = true; |
57 | var hojaRutaMovimientos = []; | 57 | var hojaRutaMovimientos = []; |
58 | var cisternaMovimientos = []; | 58 | var cisternaMovimientos = []; |
59 | var cisternaCargas = []; | 59 | var cisternaCargas = []; |
60 | var totalADescargar = 0; | 60 | var totalADescargar = 0; |
61 | for(var i = 0; i < $scope.aDescargar.length; i++) { | 61 | for(var i = 0; i < $scope.aDescargar.length; i++) { |
62 | totalADescargar += $scope.aDescargar[i] || 0; | 62 | totalADescargar += $scope.aDescargar[i] || 0; |
63 | } | 63 | } |
64 | focaModalService | 64 | focaModalService |
65 | .confirm('¿Desea descargar ' + totalADescargar + ' litros de ' + | 65 | .confirm('¿Desea descargar ' + totalADescargar + ' litros de ' + |
66 | $scope.articuloSeleccionado.descripcion + '?') | 66 | $scope.articuloSeleccionado.descripcion + '?') |
67 | .then(descargar, function() { | 67 | .then(descargar, function() { |
68 | $scope.cargando = false; | 68 | $scope.cargando = false; |
69 | }); | 69 | }); |
70 | } | 70 | } |
71 | function descargar() { | 71 | function descargar() { |
72 | for(var i = 0; i < $scope.cisternas.length; i++) { | 72 | for(var i = 0; i < $scope.cisternas.length; i++) { |
73 | var descarga = $scope.aDescargar[i]; | 73 | var descarga = $scope.aDescargar[i]; |
74 | var cisternaCarga = $scope.cisternas[i].cisternaCarga; | 74 | var cisternaCarga = $scope.cisternas[i].cisternaCarga; |
75 | if(!descarga) continue; | 75 | if(!descarga) continue; |
76 | if(descarga > cisternaCarga.cantidad) { | 76 | if(descarga > cisternaCarga.cantidad) { |
77 | focaModalService.alert('La cantidad a descargar no debe ser ' + | 77 | focaModalService.alert('La cantidad a descargar no debe ser ' + |
78 | 'mayor a la cantidad de la cisterna'); | 78 | 'mayor a la cantidad de la cisterna'); |
79 | $scope.cargando = false; | 79 | $scope.cargando = false; |
80 | return; | 80 | return; |
81 | } | 81 | } |
82 | cisternaCarga.cantidad -= descarga; | 82 | cisternaCarga.cantidad -= descarga; |
83 | 83 | ||
84 | //Guardar | 84 | //Guardar |
85 | var now = new Date(); | 85 | var now = new Date(); |
86 | var cisternaMovimiento = { | 86 | var cisternaMovimiento = { |
87 | fecha: now.toISOString().slice(0, 19).replace('T', ' '), | 87 | fecha: now.toISOString().slice(0, 19).replace('T', ' '), |
88 | cantidad: descarga, | 88 | cantidad: descarga, |
89 | metodo: 'descarga', | 89 | metodo: 'descarga', |
90 | idCisternaCarga: cisternaCarga.id, | 90 | idCisternaCarga: cisternaCarga.id, |
91 | idRemito: $scope.remito.id | 91 | idRemito: $scope.remito.id |
92 | }; | 92 | }; |
93 | var hojaRutaMovimiento = { | 93 | var hojaRutaMovimiento = { |
94 | reciboDescarga: $scope.numeroRecibo, | 94 | reciboDescarga: $scope.numeroRecibo, |
95 | idRemito: $scope.remito.id | 95 | idRemito: $scope.remito.id |
96 | }; | 96 | }; |
97 | delete cisternaCarga.articulo; | 97 | delete cisternaCarga.articulo; |
98 | cisternaCargas.push(cisternaCarga); | 98 | cisternaCargas.push(cisternaCarga); |
99 | cisternaMovimientos.push(cisternaMovimiento); | 99 | cisternaMovimientos.push(cisternaMovimiento); |
100 | hojaRutaMovimientos.push(hojaRutaMovimiento); | 100 | hojaRutaMovimientos.push(hojaRutaMovimiento); |
101 | } | 101 | } |
102 | var save = { | 102 | var save = { |
103 | cisternaCargas: cisternaCargas, | 103 | cisternaCargas: cisternaCargas, |
104 | cisternaMovimientos: cisternaMovimientos, | 104 | cisternaMovimientos: cisternaMovimientos, |
105 | hojaRutaMovimientos: hojaRutaMovimientos, | 105 | hojaRutaMovimientos: hojaRutaMovimientos, |
106 | articulo: $scope.articuloSeleccionado | 106 | articulo: $scope.articuloSeleccionado |
107 | }; | 107 | }; |
108 | 108 | ||
109 | focaSeguimientoService.obtenerPosicion(getPosition) | 109 | focaSeguimientoService.obtenerPosicion(getPosition) |
110 | 110 | ||
111 | var posicion; | 111 | var posicion; |
112 | 112 | ||
113 | function getPosition(position) { | 113 | function getPosition(position) { |
114 | 114 | ||
115 | posicion = { | 115 | posicion = { |
116 | latitud: position.coords.latitude, | 116 | latitud: position.coords.latitude, |
117 | longitud: position.coords.longitude, | 117 | longitud: position.coords.longitude, |
118 | actividad: 'Entrega de producto', | 118 | actividad: 'Entrega de producto', |
119 | observaciones: $scope.remito.observaciones | 119 | observaciones: $scope.remito.observaciones |
120 | } | 120 | } |
121 | } | 121 | } |
122 | 122 | ||
123 | $scope.aDescargar = []; | 123 | $scope.aDescargar = []; |
124 | $scope.remito.observaciones = ''; | 124 | $scope.remito.observaciones = ''; |
125 | $scope.articuloSeleccionado.descargado = true; | 125 | |
126 | if ($scope.articuloSeleccionado.cantidadDescargada === | ||
127 | $scope.articuloSeleccionado.cantidad ) { | ||
128 | |||
129 | $scope.articuloSeleccionado.descargado = true; | ||
130 | } | ||
126 | 131 | ||
127 | var siguienteArticulo = $scope.remito.articulosRemito.filter( | 132 | var siguienteArticulo = $scope.remito.articulosRemito.filter( |
128 | function(articulo) { | 133 | function(articulo) { |
129 | return articulo.id != $scope.articuloSeleccionado.id; | 134 | return articulo.id != $scope.articuloSeleccionado.id; |
130 | } | 135 | } |
131 | ); | 136 | ); |
132 | 137 | ||
133 | if (siguienteArticulo.length) { | 138 | if (siguienteArticulo.length) { |
134 | $scope.cambio(siguienteArticulo[0]); | 139 | $scope.cambio(siguienteArticulo[0]); |
135 | } | 140 | } |
136 | 141 | ||
137 | $scope.actualizarPuntoDescarga(); | 142 | $scope.actualizarPuntoDescarga(); |
138 | 143 | ||
139 | focaModalDetalleHojaRutaService | 144 | focaModalDetalleHojaRutaService |
140 | .postMovimientoHojaRuta(save) | 145 | .postMovimientoHojaRuta(save) |
141 | .then(guardarSeguimiento); | 146 | .then(guardarSeguimiento); |
142 | 147 | ||
143 | 148 | ||
144 | function guardarSeguimiento(res) { | 149 | function guardarSeguimiento(res) { |
145 | 150 | ||
146 | posicion.idComprobante = res.data[0].id; | 151 | posicion.idComprobante = res.data[0].id; |
147 | focaModalDetalleHojaRutaService.guardarPosicion({ posicion: posicion }); | 152 | focaModalDetalleHojaRutaService.guardarPosicion({ posicion: posicion }); |
148 | } | 153 | } |
149 | } | 154 | } |
150 | }; | 155 | }; |
151 | 156 | ||
152 | $scope.cancel = function() { | 157 | $scope.cancel = function() { |
153 | $uibModalInstance.dismiss('cancel'); | 158 | $uibModalInstance.dismiss('cancel'); |
154 | }; | 159 | }; |
155 | 160 | ||
156 | $scope.distribucionDisponible = function() { | 161 | $scope.distribucionDisponible = function() { |
157 | return $scope.articuloSeleccionado.cantidadDescargada && | 162 | return $scope.articuloSeleccionado.cantidadDescargada && |
158 | $scope.articuloSeleccionado.cantidadDescargada <= | 163 | $scope.articuloSeleccionado.cantidadDescargada <= |
159 | $scope.articuloSeleccionado.cantidad; | 164 | $scope.articuloSeleccionado.cantidad; |
160 | }; | 165 | }; |
161 | 166 | ||
162 | $scope.actualizarArticulo = function() { | 167 | $scope.actualizarArticulo = function() { |
163 | $scope.articuloSeleccionado.cantidadDescargada = 0; | 168 | $scope.articuloSeleccionado.cantidadDescargada = 0; |
164 | for(var i = 0; i < $scope.aDescargar.length; i++) { | 169 | for(var i = 0; i < $scope.aDescargar.length; i++) { |
165 | $scope.articuloSeleccionado.cantidadDescargada += | 170 | $scope.articuloSeleccionado.cantidadDescargada += |
166 | parseFloat($scope.aDescargar[i]) || 0; | 171 | parseFloat($scope.aDescargar[i]) || 0; |
167 | } | 172 | } |
168 | }; | 173 | }; |
169 | 174 | ||
170 | $scope.actualizarPuntoDescarga = function() { | 175 | $scope.actualizarPuntoDescarga = function() { |
171 | var modalInstance = $uibModal.open( | 176 | var modalInstance = $uibModal.open( |
172 | { | 177 | { |
173 | ariaLabelledBy: 'Actualizar punto de descarga', | 178 | ariaLabelledBy: 'Actualizar punto de descarga', |
174 | templateUrl: 'modal-actualizar-punto-descarga.html', | 179 | templateUrl: 'modal-actualizar-punto-descarga.html', |
175 | controller: 'focaModalActualizarPuntoDescargaController', | 180 | controller: 'focaModalActualizarPuntoDescargaController', |
176 | resolve: { | 181 | resolve: { |
177 | notaPedido: function() { | 182 | notaPedido: function() { |
178 | return $scope.remito.notaPedido; | 183 | return $scope.remito.notaPedido; |
179 | } | 184 | } |
180 | }, | 185 | }, |
181 | size: 'lg' | 186 | size: 'lg' |
182 | } | 187 | } |
183 | ); | 188 | ); |
184 | modalInstance.result.then(function() { | 189 | modalInstance.result.then(function() { |
185 | success(); | 190 | success().then(function() { |
191 | $uibModalInstance.close($scope.remito); | ||
192 | }); | ||
186 | }, function() { | 193 | }, function() { |
187 | success(); | 194 | success().then(function() { |
195 | $uibModalInstance.close($scope.remito); | ||
196 | }); | ||
188 | }); | 197 | }); |
189 | }; | 198 | }; |
190 | 199 | ||
191 | $scope.rechazar = function() { | 200 | $scope.rechazar = function() { |
192 | focaModalService | 201 | focaModalService |
193 | .prompt({ | 202 | .prompt({ |
194 | titulo: 'Aclare el motivo de rechazo' | 203 | titulo: 'Aclare el motivo de rechazo' |
195 | }) | 204 | }) |
196 | .then(function(motivo) { | 205 | .then(function(motivo) { |
197 | $scope.cargando = true; | 206 | $scope.cargando = true; |
198 | var remitoRechazado = $.extend(true, {}, $scope.remito); | 207 | var remitoRechazado = $.extend(true, {}, $scope.remito); |
199 | delete remitoRechazado.articulosRemito; | 208 | delete remitoRechazado.articulosRemito; |
200 | delete remitoRechazado.notaPedido; | 209 | delete remitoRechazado.notaPedido; |
201 | remitoRechazado.rechazado = true; | 210 | remitoRechazado.rechazado = true; |
202 | remitoRechazado.motivoRechazo = motivo; | 211 | remitoRechazado.motivoRechazo = motivo; |
203 | remitoRechazado.fechaRemito = | 212 | remitoRechazado.fechaRemito = |
204 | remitoRechazado.fechaRemito.slice(0, 19).replace('T', ' '); | 213 | remitoRechazado.fechaRemito.slice(0, 19).replace('T', ' '); |
205 | focaModalDetalleHojaRutaService.rechazarRemito(remitoRechazado) | 214 | focaModalDetalleHojaRutaService.rechazarRemito(remitoRechazado) |
206 | .then(function(res) { | 215 | .then(function(res) { |
207 | focaSeguimientoService.guardarPosicion( | 216 | focaSeguimientoService.guardarPosicion( |
208 | 'Entrega de producto', | 217 | 'Entrega de producto', |
209 | res.data[1].id, | 218 | res.data[1].id, |
210 | motivo); | 219 | motivo); |
211 | success(); | 220 | success(); |
212 | }) | 221 | }) |
213 | .catch(error); | 222 | .catch(error); |
214 | $scope.readonly = true; | 223 | $scope.readonly = true; |
215 | }); | 224 | }); |
216 | }; | 225 | }; |
217 | 226 | ||
218 | //funciones | 227 | //funciones |
219 | function error(error) { | 228 | function error(error) { |
220 | focaModalService.alert('Hubo un error ' + error); | 229 | focaModalService.alert('Hubo un error ' + error); |
221 | } | 230 | } |
222 | function success() { | 231 | function success() { |
223 | focaModalService.alert('Operación realizada con éxito'); | ||
224 | $scope.cargando = false; | 232 | $scope.cargando = false; |
233 | return focaModalService.alert('Operación realizada con éxito'); | ||
225 | } | 234 | } |
226 | 235 | ||
227 | var articuloAChequear = $scope.remito.articulosRemito.filter( | 236 | var articuloAChequear = $scope.remito.articulosRemito.filter( |
228 | function(articulo) { | 237 | function(articulo) { |
229 | return !articulo.descargado; | 238 | return !articulo.descargado; |
230 | }); | 239 | }); |
240 | |||
231 | if (!articuloAChequear.length || $scope.remito.rechazado) { | 241 | if (!articuloAChequear.length || $scope.remito.rechazado) { |
232 | $scope.readonly = true; | 242 | $scope.readonly = true; |
233 | $scope.cambio($scope.remito.articulosRemito[0]); | 243 | $scope.cambio($scope.remito.articulosRemito[0]); |
234 | } else { | 244 | } else { |
235 | $scope.cambio(articuloAChequear[0]); | 245 | $scope.cambio(articuloAChequear[0]); |
236 | } | 246 | } |
247 | |||
237 | } | 248 | } |
238 | ] | 249 | ]) |
239 | ) | ||
240 | .controller('focaModalActualizarPuntoDescargaController', | 250 | .controller('focaModalActualizarPuntoDescargaController', |
241 | [ | 251 | [ |
242 | '$scope', | 252 | '$scope', |
243 | '$uibModalInstance', | 253 | '$uibModalInstance', |
244 | 'focaSeguimientoService', | 254 | 'focaSeguimientoService', |
245 | 'focaModalService', | 255 | 'focaModalService', |
246 | 'notaPedido', | 256 | 'notaPedido', |
247 | 'focaModalDetalleHojaRutaService', | 257 | 'focaModalDetalleHojaRutaService', |
248 | function($scope, $uibModalInstance, focaSeguimientoService, | 258 | function($scope, $uibModalInstance, focaSeguimientoService, |
249 | focaModalService, notaPedido, focaModalDetalleHojaRutaService) | 259 | focaModalService, notaPedido, focaModalDetalleHojaRutaService) |
250 | { | 260 | { |
251 | $scope.notaPedido = notaPedido; | 261 | $scope.notaPedido = notaPedido; |
252 | $scope.descripcion = ''; | 262 | $scope.descripcion = ''; |
253 | focaSeguimientoService.obtenerPosicion(function(res) { | 263 | focaSeguimientoService.obtenerPosicion(function(res) { |
254 | $scope.posicion = res.coords; | 264 | $scope.posicion = res.coords; |
255 | }); | 265 | }); |
256 | 266 | ||
257 | $scope.cancel = function() { | 267 | $scope.cancel = function() { |
258 | if ($scope.ingreso) { | 268 | if ($scope.ingreso) { |
259 | $scope.ingreso = false; | 269 | $scope.ingreso = false; |
260 | } else { | 270 | } else { |
261 | $uibModalInstance.dismiss(); | 271 | $uibModalInstance.dismiss(); |
262 | } | 272 | } |
263 | }; | 273 | }; |
264 | 274 | ||
265 | $scope.select = function(puntoDescarga) { | 275 | $scope.select = function(puntoDescarga) { |
266 | if (!$scope.posicion) { | 276 | if (!$scope.posicion) { |
267 | focaModalService.alert('No se pudo obtener la ubicación'); | 277 | focaModalService.alert('No se pudo obtener la ubicación'); |
268 | return; | 278 | return; |
269 | } | 279 | } |
270 | puntoDescarga.latitud = $scope.posicion.latitude; | 280 | puntoDescarga.latitud = $scope.posicion.latitude; |
271 | puntoDescarga.longitud = $scope.posicion.longitude; | 281 | puntoDescarga.longitud = $scope.posicion.longitude; |
272 | focaModalDetalleHojaRutaService.guardarPuntoDescarga(puntoDescarga); | 282 | focaModalDetalleHojaRutaService.guardarPuntoDescarga(puntoDescarga); |
273 | $uibModalInstance.close(); | 283 | $uibModalInstance.close(); |
274 | }; | 284 | }; |
275 | 285 | ||
276 | $scope.guardar = function() { | 286 | $scope.guardar = function() { |
277 | if (!$scope.posicion) { | 287 | if (!$scope.posicion) { |
278 | focaModalService.alert('No se pudo obtener la ubicación'); | 288 | focaModalService.alert('No se pudo obtener la ubicación'); |
279 | return; | 289 | return; |
280 | } | 290 | } |
281 | focaModalDetalleHojaRutaService | 291 | focaModalDetalleHojaRutaService |
282 | .guardarPuntoDescarga({ | 292 | .guardarPuntoDescarga({ |
283 | id: 0, | 293 | id: 0, |
284 | id_cliente: $scope.notaPedido.idCliente, | 294 | id_cliente: $scope.notaPedido.idCliente, |
285 | id_da_config_0: $scope.notaPedido.idDomicilio, | 295 | id_da_config_0: $scope.notaPedido.idDomicilio, |
286 | descripcion: $scope.descripcion, | 296 | descripcion: $scope.descripcion, |
287 | latitud: $scope.posicion.latitude, | 297 | latitud: $scope.posicion.latitude, |
288 | longitud: $scope.posicion.longitude | 298 | longitud: $scope.posicion.longitude |
289 | }) | 299 | }) |
290 | .then(function(res) { | 300 | .then(function(res) { |
291 | $uibModalInstance.close(res.data); | 301 | $uibModalInstance.close(res.data); |
292 | }); | 302 | }); |
293 | |||
294 | }; | 303 | }; |
295 | 304 | ||
296 | } | 305 | } |