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