Commit 941b88920bbea40572c151bbcb1ca38a28941afe
Exists in
master
Merge branch 'master' into 'master'
Master(efernandez) See merge request !9
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 | 'idRemito', | 7 | 'idRemito', |
8 | 'focaModalDetalleHojaRutaService', | 8 | 'focaModalDetalleHojaRutaService', |
9 | 'focaModalService', | 9 | 'focaModalService', |
10 | 'focaSeguimientoService', | 10 | 'focaSeguimientoService', |
11 | '$uibModal', | 11 | '$uibModal', |
12 | function($filter, $scope, $uibModalInstance, idRemito, focaModalDetalleHojaRutaService, | 12 | function($filter, $scope, $uibModalInstance, idRemito, focaModalDetalleHojaRutaService, |
13 | focaModalService, focaSeguimientoService, $uibModal) | 13 | focaModalService, focaSeguimientoService, $uibModal) |
14 | { | 14 | { |
15 | //Seteo variables | 15 | //Seteo variables |
16 | $scope.remito = {}; | 16 | $scope.remito = {}; |
17 | $scope.cisternas = []; | 17 | $scope.cisternas = []; |
18 | $scope.articuloSeleccionado = {}; | 18 | $scope.articuloSeleccionado = {}; |
19 | $scope.aDescargar = []; | 19 | $scope.aDescargar = []; |
20 | $scope.cargando = true; | 20 | $scope.cargando = true; |
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 | focaModalDetalleHojaRutaService | 108 | focaModalDetalleHojaRutaService |
109 | .postMovimientoHojaRuta(save) | 109 | .postMovimientoHojaRuta(save) |
110 | .then(guardarSeguimiento) | 110 | .then(guardarSeguimiento) |
111 | .catch(error); | 111 | .catch(error); |
112 | function guardarSeguimiento(res) { | 112 | function guardarSeguimiento(res) { |
113 | focaSeguimientoService | 113 | focaSeguimientoService |
114 | .guardarPosicion( | 114 | .guardarPosicion( |
115 | 'Entrega de producto', | 115 | 'Entrega de producto', |
116 | res.data[0].id, | 116 | res.data[0].id, |
117 | $scope.remito.observaciones); | 117 | $scope.remito.observaciones); |
118 | $scope.aDescargar = []; | 118 | $scope.aDescargar = []; |
119 | $scope.remito.observaciones = ''; | 119 | $scope.remito.observaciones = ''; |
120 | $scope.articuloSeleccionado.descargado = true; | 120 | $scope.articuloSeleccionado.descargado = true; |
121 | var siguienteArticulo = $scope.remito.articulosRemito.filter( | 121 | var siguienteArticulo = $scope.remito.articulosRemito.filter( |
122 | function(articulo) { | 122 | function(articulo) { |
123 | return articulo.id != $scope.articuloSeleccionado.id; | 123 | return articulo.id != $scope.articuloSeleccionado.id; |
124 | } | 124 | } |
125 | ); | 125 | ); |
126 | if(siguienteArticulo.length) { | 126 | if(siguienteArticulo.length) { |
127 | $scope.cambio(siguienteArticulo[0]); | 127 | $scope.cambio(siguienteArticulo[0]); |
128 | } | 128 | } |
129 | $scope.actualizarPuntoDescarga(); | 129 | $scope.actualizarPuntoDescarga(); |
130 | } | 130 | } |
131 | } | 131 | } |
132 | }; | 132 | }; |
133 | 133 | ||
134 | $scope.cancel = function() { | 134 | $scope.cancel = function() { |
135 | $uibModalInstance.dismiss('cancel'); | 135 | $uibModalInstance.dismiss('cancel'); |
136 | }; | 136 | }; |
137 | 137 | ||
138 | $scope.distribucionDisponible = function() { | 138 | $scope.distribucionDisponible = function() { |
139 | return $scope.articuloSeleccionado.cantidadDescargada && | 139 | return $scope.articuloSeleccionado.cantidadDescargada && |
140 | $scope.articuloSeleccionado.cantidadDescargada <= | 140 | $scope.articuloSeleccionado.cantidadDescargada <= |
141 | $scope.articuloSeleccionado.cantidad; | 141 | $scope.articuloSeleccionado.cantidad; |
142 | }; | 142 | }; |
143 | 143 | ||
144 | $scope.actualizarArticulo = function() { | 144 | $scope.actualizarArticulo = function() { |
145 | $scope.articuloSeleccionado.cantidadDescargada = 0; | 145 | $scope.articuloSeleccionado.cantidadDescargada = 0; |
146 | for(var i = 0; i < $scope.aDescargar.length; i++) { | 146 | for(var i = 0; i < $scope.aDescargar.length; i++) { |
147 | $scope.articuloSeleccionado.cantidadDescargada += | 147 | $scope.articuloSeleccionado.cantidadDescargada += |
148 | parseFloat($scope.aDescargar[i]) || 0; | 148 | parseFloat($scope.aDescargar[i]) || 0; |
149 | } | 149 | } |
150 | }; | 150 | }; |
151 | 151 | ||
152 | $scope.actualizarPuntoDescarga = function() { | 152 | $scope.actualizarPuntoDescarga = function() { |
153 | var modalInstance = $uibModal.open( | 153 | var modalInstance = $uibModal.open( |
154 | { | 154 | { |
155 | ariaLabelledBy: 'Actualizar punto de descarga', | 155 | ariaLabelledBy: 'Actualizar punto de descarga', |
156 | templateUrl: 'modal-actualizar-punto-descarga.html', | 156 | templateUrl: 'modal-actualizar-punto-descarga.html', |
157 | controller: 'focaModalActualizarPuntoDescargaController', | 157 | controller: 'focaModalActualizarPuntoDescargaController', |
158 | resolve: { | 158 | resolve: { |
159 | puntosDescarga: function() { | 159 | puntosDescarga: function() { |
160 | return $scope.remito.notaPedido.notaPedidoPuntoDescarga; | 160 | return $scope.remito.notaPedido.notaPedidoPuntoDescarga; |
161 | } | 161 | } |
162 | }, | 162 | }, |
163 | size: 'lg' | 163 | size: 'lg' |
164 | } | 164 | } |
165 | ); | 165 | ); |
166 | modalInstance.result.then(function() { | 166 | modalInstance.result.then(function() { |
167 | success(); | 167 | success(); |
168 | }, function() { | 168 | }, function() { |
169 | success(); | 169 | success(); |
170 | }); | 170 | }); |
171 | }; | 171 | }; |
172 | 172 | ||
173 | $scope.rechazar = function() { | 173 | $scope.rechazar = function() { |
174 | focaModalService | 174 | focaModalService |
175 | .prompt('Aclare el motivo de rechazo') | 175 | .prompt('Aclare el motivo de rechazo') |
176 | .then(function(motivo) { | 176 | .then(function(motivo) { |
177 | $scope.cargando = true; | 177 | $scope.cargando = true; |
178 | var remitoRechazado = $.extend(true, {}, $scope.remito); | 178 | var remitoRechazado = $.extend(true, {}, $scope.remito); |
179 | delete remitoRechazado.articulosRemito; | 179 | delete remitoRechazado.articulosRemito; |
180 | delete remitoRechazado.notaPedido; | 180 | delete remitoRechazado.notaPedido; |
181 | remitoRechazado.rechazado = true; | 181 | remitoRechazado.rechazado = true; |
182 | remitoRechazado.motivoRechazo = motivo; | 182 | remitoRechazado.motivoRechazo = motivo; |
183 | remitoRechazado.fechaRemito = | 183 | remitoRechazado.fechaRemito = |
184 | remitoRechazado.fechaRemito.slice(0, 19).replace('T', ' '); | 184 | remitoRechazado.fechaRemito.slice(0, 19).replace('T', ' '); |
185 | focaModalDetalleHojaRutaService | 185 | Promise.all([ |
186 | .rechazarRemito(remitoRechazado) | 186 | focaModalDetalleHojaRutaService |
187 | .then(success) | 187 | .rechazarRemito(remitoRechazado), |
188 | .catch(error); | 188 | focaSeguimientoService.guardarPosicion( |
189 | 'Entrega de producto', | ||
190 | remitoRechazado.id, | ||
191 | motivo) | ||
192 | ]).then(success).catch(error); | ||
189 | $scope.readonly = true; | 193 | $scope.readonly = true; |
190 | }); | 194 | }); |
191 | }; | 195 | }; |
192 | 196 | ||
193 | //funciones | 197 | //funciones |
194 | function error(error) { | 198 | function error(error) { |
195 | focaModalService.alert('Hubo un error ' + error); | 199 | focaModalService.alert('Hubo un error ' + error); |
196 | } | 200 | } |
197 | function success() { | 201 | function success() { |
198 | focaModalService.alert('Operación realizada con éxito'); | 202 | focaModalService.alert('Operación realizada con éxito'); |
199 | $scope.cargando = false; | 203 | $scope.cargando = false; |
200 | } | 204 | } |
201 | } | 205 | } |
202 | ] | 206 | ] |
203 | ) | 207 | ) |
204 | .controller('focaModalActualizarPuntoDescargaController', | 208 | .controller('focaModalActualizarPuntoDescargaController', |
205 | [ | 209 | [ |
206 | '$scope', | 210 | '$scope', |
207 | '$uibModalInstance', | 211 | '$uibModalInstance', |
208 | 'focaSeguimientoService', | 212 | 'focaSeguimientoService', |
209 | 'focaModalService', | 213 | 'focaModalService', |
210 | 'puntosDescarga', | 214 | 'puntosDescarga', |
211 | 'focaModalDetalleHojaRutaService', | 215 | 'focaModalDetalleHojaRutaService', |
212 | function($scope, $uibModalInstance, focaSeguimientoService, | 216 | function($scope, $uibModalInstance, focaSeguimientoService, |
213 | focaModalService, puntosDescarga, focaModalDetalleHojaRutaService) | 217 | focaModalService, puntosDescarga, focaModalDetalleHojaRutaService) |
214 | { | 218 | { |
215 | $scope.puntosDescarga = puntosDescarga; | 219 | $scope.puntosDescarga = puntosDescarga; |
216 | focaSeguimientoService.obtenerPosicion(function(res) { | 220 | focaSeguimientoService.obtenerPosicion(function(res) { |
217 | $scope.posicion = res.coords; | 221 | $scope.posicion = res.coords; |
218 | }); | 222 | }); |
219 | 223 | ||
220 | $scope.cancel = function() { | 224 | $scope.cancel = function() { |
221 | $uibModalInstance.dismiss(); | 225 | $uibModalInstance.dismiss(); |
222 | }; | 226 | }; |
223 | 227 | ||
224 | $scope.select = function(puntoDescarga) { | 228 | $scope.select = function(puntoDescarga) { |
225 | if(!$scope.posicion){ | 229 | if(!$scope.posicion){ |
226 | focaModalService.alert('No se pudo obtener la ubicacion'); | 230 | focaModalService.alert('No se pudo obtener la ubicacion'); |
227 | return; | 231 | return; |
228 | } | 232 | } |
229 | puntoDescarga.latitud = $scope.posicion.latitude; | 233 | puntoDescarga.latitud = $scope.posicion.latitude; |
230 | puntoDescarga.longitud = $scope.posicion.longitude; | 234 | puntoDescarga.longitud = $scope.posicion.longitude; |
231 | focaModalDetalleHojaRutaService | 235 | focaModalDetalleHojaRutaService |
232 | .updatePuntoDescarga(puntoDescarga) | 236 | .updatePuntoDescarga(puntoDescarga) |
233 | .then(function(res) { | 237 | .then(function(res) { |
234 | $uibModalInstance.close(res.data); | 238 | $uibModalInstance.close(res.data); |
235 | }); | 239 | }); |
236 | }; | 240 | }; |
237 | } | 241 | } |
238 | ] | 242 | ] |
239 | ); | 243 | ); |
240 | 244 |