Commit 3e9bc33af9eedbe88df185b88b275130ca63a523
1 parent
6a883d52bc
Exists in
master
inteligencia para autocompletar y validaciones
Showing
1 changed file
with
24 additions
and
8 deletions
Show diff stats
src/js/controller.js
1 | angular.module('focaModalDetalleCisternas') | 1 | angular.module('focaModalDetalleCisternas') |
2 | .controller('focaDetalleVehiculo', | 2 | .controller('focaDetalleVehiculo', |
3 | ['$scope', | 3 | ['$scope', |
4 | '$uibModalInstance', | 4 | '$uibModalInstance', |
5 | 'idVehiculo', | 5 | 'idVehiculo', |
6 | 'idRemito', | 6 | 'idRemito', |
7 | 'focaModalService', | 7 | 'focaModalService', |
8 | '$filter', | 8 | '$filter', |
9 | 'focaModalDetalleCisternasService', | 9 | 'focaModalDetalleCisternasService', |
10 | 'fechaReparto', | 10 | 'fechaReparto', |
11 | function($scope, $uibModalInstance, idVehiculo, idRemito, focaModalService, $filter, | 11 | function($scope, $uibModalInstance, idVehiculo, idRemito, focaModalService, $filter, |
12 | focaModalDetalleCisternasService, fechaReparto | 12 | focaModalDetalleCisternasService, fechaReparto |
13 | ) { | 13 | ) { |
14 | //seteo variables | 14 | //seteo variables |
15 | $scope.cargandoDatos = true; | 15 | $scope.cargandoDatos = true; |
16 | $scope.idRemito = idRemito; | 16 | $scope.idRemito = idRemito; |
17 | $scope.articulos = []; | 17 | $scope.articulos = []; |
18 | $scope.vehiculo = {}; | 18 | $scope.vehiculo = {}; |
19 | $scope.cisternas = []; | 19 | $scope.cisternas = []; |
20 | $scope.cisternasCarga = []; | 20 | $scope.cisternasCarga = []; |
21 | $scope.remito = {}; | 21 | $scope.remito = {}; |
22 | $scope.aCargar = []; | 22 | $scope.aCargar = []; |
23 | var cisternaMovimientos = []; | 23 | var cisternaMovimientos = []; |
24 | var promesaRemito; | 24 | var promesaRemito; |
25 | 25 | ||
26 | if(fechaReparto) { | 26 | if(fechaReparto) { |
27 | focaModalDetalleCisternasService.fecha = fechaReparto; | 27 | focaModalDetalleCisternasService.fecha = fechaReparto; |
28 | } | 28 | } |
29 | var promesaVehiculo = focaModalDetalleCisternasService.obtenerVehiculoById(idVehiculo); | 29 | var promesaVehiculo = focaModalDetalleCisternasService.obtenerVehiculoById(idVehiculo); |
30 | var promesaCisternas = focaModalDetalleCisternasService | 30 | var promesaCisternas = focaModalDetalleCisternasService |
31 | .obtenerCisternasPorFecha(idVehiculo); | 31 | .obtenerCisternasPorFecha(idVehiculo); |
32 | if(idRemito !== -1) { | 32 | if(idRemito !== -1) { |
33 | promesaRemito = focaModalDetalleCisternasService.obtenerRemitoById(idRemito); | 33 | promesaRemito = focaModalDetalleCisternasService.obtenerRemitoById(idRemito); |
34 | } | 34 | } |
35 | Promise.all([promesaVehiculo, promesaCisternas, promesaRemito]).then(function(res) { | 35 | Promise.all([promesaVehiculo, promesaCisternas, promesaRemito]).then(function(res) { |
36 | $scope.cargandoDatos = false; | 36 | $scope.cargandoDatos = false; |
37 | $scope.vehiculo = res[0].data; | 37 | $scope.vehiculo = res[0].data; |
38 | $scope.cisternas = res[1].data; | 38 | $scope.cisternas = res[1].data; |
39 | if(!$scope.cisternas.length) { | 39 | if(!$scope.cisternas.length) { |
40 | focaModalService.alert('El vehículo no tiene cisternas'); | 40 | focaModalService.alert('El vehículo no tiene cisternas'); |
41 | $uibModalInstance.dismiss(); | 41 | $uibModalInstance.dismiss(); |
42 | return; | 42 | return; |
43 | } | 43 | } |
44 | if(!res[2]) { | 44 | if(!res[2]) { |
45 | $scope.$digest(); | 45 | $scope.$digest(); |
46 | return; | 46 | return; |
47 | } | 47 | } |
48 | $scope.remito = res[2].data; | 48 | $scope.remito = res[2].data; |
49 | if($scope.remito.idUsuarioProceso) { | 49 | if($scope.remito.idUsuarioProceso) { |
50 | focaModalService.alert('Remito ya asignado'); | 50 | focaModalService.alert('Remito ya asignado'); |
51 | $uibModalInstance.close(); | 51 | $uibModalInstance.close(); |
52 | } | 52 | } |
53 | $scope.articulos = $scope.remito.articulosRemito; | 53 | $scope.articulos = $scope.remito.articulosRemito; |
54 | if(!$scope.articulos.length) { | 54 | if(!$scope.articulos.length) { |
55 | focaModalService.alert('El remito no tiene articulos'); | 55 | focaModalService.alert('El remito no tiene articulos'); |
56 | $uibModalInstance.dismiss(); | 56 | $uibModalInstance.dismiss(); |
57 | return; | 57 | return; |
58 | } | 58 | } |
59 | $scope.seleccionarArticulo($scope.articulos[0]); | 59 | $scope.seleccionarArticulo($scope.articulos[0]); |
60 | var tieneUsuario = $scope.cisternas.filter(function(cisterna) { | 60 | var tieneUsuario = $scope.cisternas.filter(function(cisterna) { |
61 | if(cisterna.cisternaCarga && cisterna.cisternaCarga.idUsuarioProceso) { | 61 | if(cisterna.cisternaCarga && cisterna.cisternaCarga.idUsuarioProceso) { |
62 | return cisterna.cisternaCarga.idUsuarioProceso !== | 62 | return cisterna.cisternaCarga.idUsuarioProceso !== |
63 | focaModalDetalleCisternasService.idUsuario; | 63 | focaModalDetalleCisternasService.idUsuario; |
64 | } | 64 | } |
65 | }); | 65 | }); |
66 | if(tieneUsuario.length) { | 66 | if(tieneUsuario.length) { |
67 | focaModalService.alert('Otro usario esta usando este vehículo'); | 67 | focaModalService.alert('Otro usario esta usando este vehículo'); |
68 | $uibModalInstance.close(); | 68 | $uibModalInstance.close(); |
69 | } | 69 | } |
70 | $scope.$digest(); | 70 | $scope.$digest(); |
71 | }); | 71 | }); |
72 | $scope.aceptar = function() { | 72 | $scope.aceptar = function() { |
73 | $scope.cargando = true; | 73 | $scope.cargando = true; |
74 | for(var i = 0; i < $scope.cisternasCarga.length; i++) { | 74 | for(var i = 0; i < $scope.cisternasCarga.length; i++) { |
75 | $scope.cisternasCarga[i].idUsuarioProceso = | 75 | $scope.cisternasCarga[i].idUsuarioProceso = |
76 | focaModalDetalleCisternasService.idUsuario; | 76 | focaModalDetalleCisternasService.idUsuario; |
77 | delete $scope.cisternasCarga[i].articulo; | 77 | delete $scope.cisternasCarga[i].articulo; |
78 | } | 78 | } |
79 | var cisterna = { | 79 | var cisterna = { |
80 | cisternaMovimientos: cisternaMovimientos, | 80 | cisternaMovimientos: cisternaMovimientos, |
81 | cisternaCargas: $scope.cisternasCarga, | 81 | cisternaCargas: $scope.cisternasCarga, |
82 | idVehiculo: $scope.vehiculo.id, | 82 | idVehiculo: $scope.vehiculo.id, |
83 | fechaReparto: focaModalDetalleCisternasService.fecha | 83 | fechaReparto: focaModalDetalleCisternasService.fecha |
84 | }; | 84 | }; |
85 | if(!focaModalDetalleCisternasService.idUsuario) { | 85 | if(!focaModalDetalleCisternasService.idUsuario) { |
86 | focaModalService.alert('No logeado como vendedor'); | 86 | focaModalService.alert('No logeado como vendedor'); |
87 | $scope.cargando = false; | 87 | $scope.cargando = false; |
88 | return; | 88 | return; |
89 | } | 89 | } |
90 | focaModalDetalleCisternasService.guardarCisternas(cisterna, $scope.remito.id) | 90 | focaModalDetalleCisternasService.guardarCisternas(cisterna, $scope.remito.id) |
91 | .then(function() { | 91 | .then(function() { |
92 | focaModalService.alert('Cisternas cargadas con éxito').then(function() { | 92 | focaModalService.alert('Cisternas cargadas con éxito').then(function() { |
93 | $scope.cargando = false; | 93 | $scope.cargando = false; |
94 | $uibModalInstance.close(); | 94 | $uibModalInstance.close(); |
95 | }); | 95 | }); |
96 | }).catch(function(error) { | 96 | }).catch(function(error) { |
97 | $scope.cargando = false; | 97 | $scope.cargando = false; |
98 | $uibModalInstance.close(); | 98 | $uibModalInstance.close(); |
99 | if (error.status === 403) { | 99 | if (error.status === 403) { |
100 | focaModalService.alert('ERROR: ' + error.data); | 100 | focaModalService.alert('ERROR: ' + error.data); |
101 | return; | 101 | return; |
102 | } | 102 | } |
103 | focaModalService.alert('Hubo un error al cargar las cisternas'); | 103 | focaModalService.alert('Hubo un error al cargar las cisternas'); |
104 | }); | 104 | }); |
105 | }; | 105 | }; |
106 | $scope.cancelar = function() { | 106 | $scope.cancelar = function() { |
107 | $uibModalInstance.dismiss(); | 107 | $uibModalInstance.dismiss(); |
108 | }; | 108 | }; |
109 | $scope.cargarACisternas = function() { | 109 | $scope.cargarACisternas = function() { |
110 | for(var i = 0; i < $scope.cisternas.length; i++) { | 110 | for(var i = 0; i < $scope.cisternas.length; i++) { |
111 | var cisterna = $scope.cisternas[i]; | 111 | var cisterna = $scope.cisternas[i]; |
112 | var aCargar = parseFloat($scope.aCargar[i]); | 112 | var aCargar = parseFloat($scope.aCargar[i]); |
113 | var fechaReparto = focaModalDetalleCisternasService.fecha; | 113 | var fechaReparto = focaModalDetalleCisternasService.fecha; |
114 | //validaciones | 114 | //validaciones |
115 | if(!aCargar) { | 115 | if(!aCargar) { |
116 | continue; | 116 | continue; |
117 | } | 117 | } |
118 | //cargar | 118 | //cargar |
119 | if(cisterna.cisternaCarga.cantidad) { | 119 | if(cisterna.cisternaCarga.cantidad) { |
120 | cisterna.cisternaCarga.cantidad += aCargar; | 120 | cisterna.cisternaCarga.cantidad += aCargar; |
121 | }else { | 121 | }else { |
122 | cisterna.cisternaCarga.cantidad = aCargar; | 122 | cisterna.cisternaCarga.cantidad = aCargar; |
123 | cisterna.cisternaCarga.idProducto = $scope.articuloSeleccionado.idArticulo; | 123 | cisterna.cisternaCarga.idProducto = $scope.articuloSeleccionado.idArticulo; |
124 | } | 124 | } |
125 | cisterna.disponible = cisterna.capacidad - cisterna.cisternaCarga.cantidad; | 125 | cisterna.disponible = cisterna.capacidad - cisterna.cisternaCarga.cantidad; |
126 | 126 | ||
127 | cisterna.cisternaCarga.articulo = { | 127 | cisterna.cisternaCarga.articulo = { |
128 | DetArt: $scope.articuloSeleccionado.descripcion | 128 | DetArt: $scope.articuloSeleccionado.descripcion |
129 | }; | 129 | }; |
130 | $filter('filter')($scope.articulos, {id: $scope.articuloSeleccionado.id})[0] | 130 | $filter('filter')($scope.articulos, {id: $scope.articuloSeleccionado.id})[0] |
131 | .cargado = true; | 131 | .cargado = true; |
132 | 132 | ||
133 | $scope.calcularPorcentaje(cisterna); | 133 | $scope.calcularPorcentaje(cisterna); |
134 | //Guardar | 134 | //Guardar |
135 | var now = new Date(); | 135 | var now = new Date(); |
136 | var cisternaMovimiento = { | 136 | var cisternaMovimiento = { |
137 | fecha: now.toISOString().slice(0, 19).replace('T', ' '), | 137 | fecha: now.toISOString().slice(0, 19).replace('T', ' '), |
138 | cantidad: aCargar, | 138 | cantidad: aCargar, |
139 | metodo: 'carga', | 139 | metodo: 'carga', |
140 | idCisternaCarga: cisterna.cisternaCarga.id, | 140 | idCisternaCarga: cisterna.cisternaCarga.id, |
141 | idRemito: $scope.remito.id | 141 | idRemito: $scope.remito.id |
142 | }; | 142 | }; |
143 | cisterna.cisternaCarga.fechaReparto = fechaReparto; | 143 | cisterna.cisternaCarga.fechaReparto = fechaReparto; |
144 | cisterna.cisternaCarga.idCisterna = cisterna.id; | 144 | cisterna.cisternaCarga.idCisterna = cisterna.id; |
145 | $scope.cisternasCarga.push(cisterna.cisternaCarga); | 145 | $scope.cisternasCarga.push(cisterna.cisternaCarga); |
146 | cisternaMovimientos.push(cisternaMovimiento); | 146 | cisternaMovimientos.push(cisternaMovimiento); |
147 | } | 147 | } |
148 | var articuloSiguiente = $scope.articulos.filter( | 148 | var articuloSiguiente = $scope.articulos.filter( |
149 | function(filter) { | 149 | function(filter) { |
150 | return filter.cargado !== true; | 150 | return filter.cargado !== true; |
151 | } | 151 | } |
152 | ); | 152 | ); |
153 | if(articuloSiguiente.length > 0) { | 153 | if(articuloSiguiente.length > 0) { |
154 | $scope.seleccionarArticulo(articuloSiguiente[0]); | 154 | $scope.seleccionarArticulo(articuloSiguiente[0]); |
155 | } | 155 | } |
156 | }; | 156 | }; |
157 | $scope.calcularPorcentaje = function(cisterna) { | 157 | $scope.calcularPorcentaje = function(cisterna) { |
158 | if(!cisterna.cisternaCarga) { | 158 | if(!cisterna.cisternaCarga) { |
159 | cisterna.cisternaCarga = { | 159 | cisterna.cisternaCarga = { |
160 | cantidad: 0 | 160 | cantidad: 0 |
161 | }; | 161 | }; |
162 | } | 162 | } |
163 | var porcentaje = (cisterna.cisternaCarga.cantidad * 100 / | 163 | var porcentaje = (cisterna.cisternaCarga.cantidad * 100 / |
164 | cisterna.capacidad) + '%'; | 164 | cisterna.capacidad) + '%'; |
165 | var elementHtml = document.getElementById(cisterna.id); | 165 | var elementHtml = document.getElementById(cisterna.id); |
166 | if(elementHtml) { | 166 | if(elementHtml) { |
167 | elementHtml.style.width = porcentaje; | 167 | elementHtml.style.width = porcentaje; |
168 | } | 168 | } |
169 | }; | 169 | }; |
170 | $scope.seleccionarArticulo = function(articulo) { | 170 | $scope.seleccionarArticulo = function(articulo) { |
171 | $scope.articuloSeleccionado = articulo; | 171 | $scope.articuloSeleccionado = articulo; |
172 | $scope.cisternaDisponible(); | 172 | $scope.cisternaDisponible(); |
173 | $scope.autoCompletar(); | 173 | $scope.autoCompletar(); |
174 | $scope.actualizarArticulo(); | 174 | $scope.actualizarArticulo(); |
175 | }; | 175 | }; |
176 | $scope.actualizarArticulo = function() { | 176 | $scope.actualizarArticulo = function() { |
177 | $scope.articuloSeleccionado.cantidadCargada = 0; | 177 | $scope.articuloSeleccionado.cantidadCargada = 0; |
178 | for(var i = 0; i < $scope.aCargar.length; i++) { | 178 | for(var i = 0; i < $scope.aCargar.length; i++) { |
179 | $scope.articuloSeleccionado.cantidadCargada += | 179 | $scope.articuloSeleccionado.cantidadCargada += |
180 | parseFloat($scope.aCargar[i]) || 0; | 180 | parseFloat($scope.aCargar[i]) || 0; |
181 | } | 181 | } |
182 | }; | 182 | }; |
183 | $scope.autoCompletar = function() { | 183 | $scope.autoCompletar = function() { |
184 | $scope.aCargar = []; | ||
185 | var disponible = $filter('filter')($scope.cisternas, {disabled: false}); | ||
186 | var index = $scope.cisternas.indexOf(disponible[0]); | ||
187 | $scope.aCargar[index] = $scope.articuloSeleccionado.cantidad; | ||
188 | }; | ||
189 | $scope.cisternaDisponible = function() { | ||
190 | for(var i = 0; i < $scope.cisternas.length; i++) { | 184 | for(var i = 0; i < $scope.cisternas.length; i++) { |
191 | if($scope.articuloSeleccionado.cantidad > $scope.cisternas[i].disponible) { | 185 | if($scope.cisternas[i].disabled) { |
192 | $scope.cisternas[i].disabled = true; | ||
193 | continue; | 186 | continue; |
194 | } | 187 | } |
188 | var aCargar = $scope.articuloSeleccionado.cantidad - | ||
189 | ($scope.articuloSeleccionado.cantidadCargada || 0); | ||
190 | if(aCargar > $scope.cisternas[i].disponible) { | ||
191 | aCargar = $scope.cisternas[i].disponible; | ||
192 | } | ||
193 | if(aCargar) { | ||
194 | $scope.aCargar[i] = aCargar; | ||
195 | $scope.actualizarArticulo(); | ||
196 | } | ||
197 | } | ||
198 | }; | ||
199 | $scope.cisternaDisponible = function() { | ||
200 | for(var i = 0; i < $scope.cisternas.length; i++) { | ||
201 | //Puede meter un porcentaje del total | ||
202 | // if($scope.articuloSeleccionado.cantidad > $scope.cisternas[i].disponible) { | ||
203 | // $scope.cisternas[i].disabled = true; | ||
204 | // continue; | ||
205 | // } | ||
195 | if($scope.cisternas[i].cisternaCarga && | 206 | if($scope.cisternas[i].cisternaCarga && |
196 | $scope.cisternas[i].cisternaCarga.idProducto && | 207 | $scope.cisternas[i].cisternaCarga.idProducto && |
197 | $scope.articuloSeleccionado.idArticulo !== | 208 | $scope.articuloSeleccionado.idArticulo !== |
198 | $scope.cisternas[i].cisternaCarga.idProducto) | 209 | $scope.cisternas[i].cisternaCarga.idProducto) |
199 | { | 210 | { |
200 | $scope.cisternas[i].disabled = true; | 211 | $scope.cisternas[i].disabled = true; |
201 | continue; | 212 | continue; |
202 | } | 213 | } |
203 | $scope.cisternas[i].disabled = false; | 214 | $scope.cisternas[i].disabled = false; |
204 | } | 215 | } |
205 | }; | 216 | }; |
206 | $scope.rellenarInput = function(input) { | 217 | $scope.rellenarInput = function(input) { |
207 | if(!$scope.articuloSeleccionado) return; | 218 | if(!$scope.articuloSeleccionado) return; |
208 | if($scope.articuloSeleccionado.cantidad - | 219 | if($scope.articuloSeleccionado.cantidad - |
209 | $scope.articuloSeleccionado.cantidadCargada === 0) { | 220 | $scope.articuloSeleccionado.cantidadCargada === 0) { |
210 | return input; | 221 | return input; |
211 | } | 222 | } |
212 | if(!input) input = 0; | 223 | if(!input) input = 0; |
213 | input = parseFloat(input); | 224 | input = parseFloat(input); |
214 | input += parseFloat($scope.articuloSeleccionado.cantidad - | 225 | input += parseFloat($scope.articuloSeleccionado.cantidad - |
215 | $scope.articuloSeleccionado.cantidadCargada); | 226 | $scope.articuloSeleccionado.cantidadCargada); |
216 | return input; | 227 | return input; |
217 | }; | 228 | }; |
218 | $scope.distribucionDisponible = function() { | 229 | $scope.distribucionDisponible = function() { |
219 | if(!$scope.articuloSeleccionado || $scope.articuloSeleccionado.cantidad - | 230 | if(!$scope.articuloSeleccionado || $scope.articuloSeleccionado.cantidad - |
220 | $scope.articuloSeleccionado.cantidadCargada !== 0 || | 231 | $scope.articuloSeleccionado.cantidadCargada !== 0 || |
221 | !$scope.tieneArticulosPendientes()) { | 232 | !$scope.tieneArticulosPendientes()) { |
222 | return false; | 233 | return false; |
223 | } | 234 | } |
235 | for(var i = 0; i < $scope.cisternas.length; i++) { | ||
236 | if($scope.aCargar[i] > $scope.cisternas[i].disponible) { | ||
237 | return false; | ||
238 | } | ||
239 | } | ||
224 | return true; | 240 | return true; |
225 | }; | 241 | }; |
226 | $scope.tieneArticulosPendientes = function() { | 242 | $scope.tieneArticulosPendientes = function() { |
227 | var algunValorNegativo = $scope.aCargar.filter(function(p) { | 243 | var algunValorNegativo = $scope.aCargar.filter(function(p) { |
228 | return p < 0; | 244 | return p < 0; |
229 | }); | 245 | }); |
230 | if(algunValorNegativo.length) { | 246 | if(algunValorNegativo.length) { |
231 | return false; | 247 | return false; |
232 | } | 248 | } |
233 | var articulosDescargados = $scope.articulos.filter(function(filter) { | 249 | var articulosDescargados = $scope.articulos.filter(function(filter) { |
234 | return filter.cargado === true; | 250 | return filter.cargado === true; |
235 | }); | 251 | }); |
236 | if(articulosDescargados.length === $scope.articulos.length) { | 252 | if(articulosDescargados.length === $scope.articulos.length) { |