Commit a7117a0720918ba2d625f5be98e9e5a4f0ea45d9
1 parent
65c9405b5c
Exists in
develop
Fix en orden
Showing
1 changed file
with
1 additions
and
1 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 | 'orden', | 11 | 'orden', |
12 | function($scope, $uibModalInstance, idVehiculo, idRemito, focaModalService, $filter, | 12 | function($scope, $uibModalInstance, idVehiculo, idRemito, focaModalService, $filter, |
13 | focaModalDetalleCisternasService, fechaReparto, orden | 13 | focaModalDetalleCisternasService, fechaReparto, orden |
14 | ) { | 14 | ) { |
15 | //seteo variables | 15 | //seteo variables |
16 | $scope.orden = orden; | 16 | $scope.orden = orden ? orden : null; |
17 | $scope.cargandoDatos = true; | 17 | $scope.cargandoDatos = true; |
18 | $scope.idRemito = idRemito; | 18 | $scope.idRemito = idRemito; |
19 | $scope.articulos = []; | 19 | $scope.articulos = []; |
20 | $scope.vehiculo = {}; | 20 | $scope.vehiculo = {}; |
21 | $scope.cisternas = []; | 21 | $scope.cisternas = []; |
22 | $scope.cisternasCarga = []; | 22 | $scope.cisternasCarga = []; |
23 | $scope.remito = {}; | 23 | $scope.remito = {}; |
24 | $scope.aCargar = []; | 24 | $scope.aCargar = []; |
25 | var cisternaMovimientos = []; | 25 | var cisternaMovimientos = []; |
26 | var promesaRemito; | 26 | var promesaRemito; |
27 | 27 | ||
28 | if(fechaReparto) { | 28 | if(fechaReparto) { |
29 | focaModalDetalleCisternasService.fecha = fechaReparto; | 29 | focaModalDetalleCisternasService.fecha = fechaReparto; |
30 | } | 30 | } |
31 | var promesaVehiculo = focaModalDetalleCisternasService.obtenerVehiculoById(idVehiculo); | 31 | var promesaVehiculo = focaModalDetalleCisternasService.obtenerVehiculoById(idVehiculo); |
32 | var promesaCisternas = focaModalDetalleCisternasService | 32 | var promesaCisternas = focaModalDetalleCisternasService |
33 | .obtenerCisternasPorFecha(idVehiculo, $scope.orden); | 33 | .obtenerCisternasPorFecha(idVehiculo, $scope.orden); |
34 | 34 | ||
35 | if (idRemito !== -1) { | 35 | if (idRemito !== -1) { |
36 | promesaRemito = focaModalDetalleCisternasService.obtenerRemitoById(idRemito); | 36 | promesaRemito = focaModalDetalleCisternasService.obtenerRemitoById(idRemito); |
37 | } | 37 | } |
38 | 38 | ||
39 | Promise.all([promesaVehiculo, promesaCisternas, promesaRemito]).then(function(res) { | 39 | Promise.all([promesaVehiculo, promesaCisternas, promesaRemito]).then(function(res) { |
40 | if (idRemito !== -1 && !validarCargas(res[1].data, res[2].data)) { | 40 | if (idRemito !== -1 && !validarCargas(res[1].data, res[2].data)) { |
41 | focaModalService | 41 | focaModalService |
42 | .alert('Los artículos del remito exceden la capacidad disponible del ' + | 42 | .alert('Los artículos del remito exceden la capacidad disponible del ' + |
43 | 'vehiculo') | 43 | 'vehiculo') |
44 | .then(function() { | 44 | .then(function() { |
45 | $uibModalInstance.dismiss(); | 45 | $uibModalInstance.dismiss(); |
46 | }, function() { | 46 | }, function() { |
47 | $uibModalInstance.dismiss(); | 47 | $uibModalInstance.dismiss(); |
48 | }); | 48 | }); |
49 | return; | 49 | return; |
50 | } | 50 | } |
51 | 51 | ||
52 | $scope.cargandoDatos = false; | 52 | $scope.cargandoDatos = false; |
53 | $scope.vehiculo = res[0].data; | 53 | $scope.vehiculo = res[0].data; |
54 | $scope.cisternas = res[1].data; | 54 | $scope.cisternas = res[1].data; |
55 | 55 | ||
56 | if (!$scope.cisternas.length) { | 56 | if (!$scope.cisternas.length) { |
57 | focaModalService.alert('El vehículo no tiene cisternas') | 57 | focaModalService.alert('El vehículo no tiene cisternas') |
58 | .then(function () { | 58 | .then(function () { |
59 | $uibModalInstance.dismiss(); | 59 | $uibModalInstance.dismiss(); |
60 | }, function() { | 60 | }, function() { |
61 | $uibModalInstance.dismiss(); | 61 | $uibModalInstance.dismiss(); |
62 | }); | 62 | }); |
63 | return; | 63 | return; |
64 | } | 64 | } |
65 | 65 | ||
66 | if(!res[2]) { | 66 | if(!res[2]) { |
67 | $scope.$digest(); | 67 | $scope.$digest(); |
68 | return; | 68 | return; |
69 | } | 69 | } |
70 | 70 | ||
71 | $scope.remito = res[2].data; | 71 | $scope.remito = res[2].data; |
72 | 72 | ||
73 | if ($scope.remito.idUsuarioProceso) { | 73 | if ($scope.remito.idUsuarioProceso) { |
74 | 74 | ||
75 | focaModalService.alert('Remito ya asignado').then(function() { | 75 | focaModalService.alert('Remito ya asignado').then(function() { |
76 | 76 | ||
77 | $uibModalInstance.dismiss(); | 77 | $uibModalInstance.dismiss(); |
78 | }); | 78 | }); |
79 | } | 79 | } |
80 | 80 | ||
81 | $scope.articulos = $scope.remito.articulosRemito; | 81 | $scope.articulos = $scope.remito.articulosRemito; |
82 | 82 | ||
83 | if (!$scope.articulos.length) { | 83 | if (!$scope.articulos.length) { |
84 | 84 | ||
85 | focaModalService.alert('El remito no tiene articulos').then(function() { | 85 | focaModalService.alert('El remito no tiene articulos').then(function() { |
86 | 86 | ||
87 | $uibModalInstance.dismiss(); | 87 | $uibModalInstance.dismiss(); |
88 | }); | 88 | }); |
89 | return; | 89 | return; |
90 | } | 90 | } |
91 | 91 | ||
92 | $scope.seleccionarArticulo($scope.articulos[0]); | 92 | $scope.seleccionarArticulo($scope.articulos[0]); |
93 | var tieneUsuario = $scope.cisternas.filter(function(cisterna) { | 93 | var tieneUsuario = $scope.cisternas.filter(function(cisterna) { |
94 | if(cisterna.cisternaCarga && cisterna.cisternaCarga.idUsuarioProceso) { | 94 | if(cisterna.cisternaCarga && cisterna.cisternaCarga.idUsuarioProceso) { |
95 | return cisterna.cisternaCarga.idUsuarioProceso !== | 95 | return cisterna.cisternaCarga.idUsuarioProceso !== |
96 | focaModalDetalleCisternasService.idUsuario; | 96 | focaModalDetalleCisternasService.idUsuario; |
97 | } | 97 | } |
98 | }); | 98 | }); |
99 | if(tieneUsuario.length) { | 99 | if(tieneUsuario.length) { |
100 | focaModalService.alert('Otro usario esta usando este vehículo'); | 100 | focaModalService.alert('Otro usario esta usando este vehículo'); |
101 | $uibModalInstance.close(); | 101 | $uibModalInstance.close(); |
102 | } | 102 | } |
103 | $scope.$digest(); | 103 | $scope.$digest(); |
104 | }); | 104 | }); |
105 | 105 | ||
106 | $scope.aceptar = function() { | 106 | $scope.aceptar = function() { |
107 | 107 | ||
108 | $scope.cargando = true; | 108 | $scope.cargando = true; |
109 | for(var i = 0; i < $scope.cisternasCarga.length; i++) { | 109 | for(var i = 0; i < $scope.cisternasCarga.length; i++) { |
110 | $scope.cisternasCarga[i].idUsuarioProceso = | 110 | $scope.cisternasCarga[i].idUsuarioProceso = |
111 | focaModalDetalleCisternasService.idUsuario; | 111 | focaModalDetalleCisternasService.idUsuario; |
112 | delete $scope.cisternasCarga[i].articulo; | 112 | delete $scope.cisternasCarga[i].articulo; |
113 | delete $scope.cisternasCarga[i].remitos; | 113 | delete $scope.cisternasCarga[i].remitos; |
114 | } | 114 | } |
115 | 115 | ||
116 | var cisterna = { | 116 | var cisterna = { |
117 | cisternaMovimientos: cisternaMovimientos, | 117 | cisternaMovimientos: cisternaMovimientos, |
118 | cisternaCargas: $scope.cisternasCarga, | 118 | cisternaCargas: $scope.cisternasCarga, |
119 | idVehiculo: $scope.vehiculo.id, | 119 | idVehiculo: $scope.vehiculo.id, |
120 | fechaReparto: focaModalDetalleCisternasService.fecha | 120 | fechaReparto: focaModalDetalleCisternasService.fecha |
121 | }; | 121 | }; |
122 | 122 | ||
123 | if(!focaModalDetalleCisternasService.idUsuario) { | 123 | if(!focaModalDetalleCisternasService.idUsuario) { |
124 | focaModalService.alert('No logeado como vendedor'); | 124 | focaModalService.alert('No logeado como vendedor'); |
125 | $scope.cargando = false; | 125 | $scope.cargando = false; |
126 | return; | 126 | return; |
127 | } | 127 | } |
128 | 128 | ||
129 | focaModalDetalleCisternasService.guardarCisternas(cisterna, $scope.remito.id) | 129 | focaModalDetalleCisternasService.guardarCisternas(cisterna, $scope.remito.id) |
130 | .then(function() { | 130 | .then(function() { |
131 | 131 | ||
132 | $scope.cargando = false; | 132 | $scope.cargando = false; |
133 | $uibModalInstance.close(); | 133 | $uibModalInstance.close(); |
134 | 134 | ||
135 | }).catch(function(error) { | 135 | }).catch(function(error) { |
136 | 136 | ||
137 | $scope.cargando = false; | 137 | $scope.cargando = false; |
138 | $uibModalInstance.close(); | 138 | $uibModalInstance.close(); |
139 | 139 | ||
140 | if (error.status === 403) { | 140 | if (error.status === 403) { |
141 | focaModalService.alert('ERROR: ' + error.data); | 141 | focaModalService.alert('ERROR: ' + error.data); |
142 | return; | 142 | return; |
143 | } | 143 | } |
144 | 144 | ||
145 | focaModalService.alert('Hubo un error al cargar las cisternas'); | 145 | focaModalService.alert('Hubo un error al cargar las cisternas'); |
146 | }); | 146 | }); |
147 | }; | 147 | }; |
148 | 148 | ||
149 | $scope.cancelar = function() { | 149 | $scope.cancelar = function() { |
150 | $uibModalInstance.dismiss(); | 150 | $uibModalInstance.dismiss(); |
151 | }; | 151 | }; |
152 | 152 | ||
153 | $scope.cargarACisternas = function() { | 153 | $scope.cargarACisternas = function() { |
154 | 154 | ||
155 | for (var i = 0; i < $scope.cisternas.length; i++) { | 155 | for (var i = 0; i < $scope.cisternas.length; i++) { |
156 | var cisterna = $scope.cisternas[i]; | 156 | var cisterna = $scope.cisternas[i]; |
157 | var aCargar = parseFloat($scope.aCargar[i]); | 157 | var aCargar = parseFloat($scope.aCargar[i]); |
158 | var fechaReparto = focaModalDetalleCisternasService.fecha; | 158 | var fechaReparto = focaModalDetalleCisternasService.fecha; |
159 | 159 | ||
160 | //validaciones | 160 | //validaciones |
161 | if (!aCargar || cisterna.disabled) { | 161 | if (!aCargar || cisterna.disabled) { |
162 | continue; | 162 | continue; |
163 | } | 163 | } |
164 | 164 | ||
165 | //cargar | 165 | //cargar |
166 | if (cisterna.cisternaCarga.cantidad) { | 166 | if (cisterna.cisternaCarga.cantidad) { |
167 | cisterna.cisternaCarga.cantidad += aCargar; | 167 | cisterna.cisternaCarga.cantidad += aCargar; |
168 | } else { | 168 | } else { |
169 | cisterna.cisternaCarga.cantidad = aCargar; | 169 | cisterna.cisternaCarga.cantidad = aCargar; |
170 | cisterna.cisternaCarga.idProducto = $scope.articuloSeleccionado.idArticulo; | 170 | cisterna.cisternaCarga.idProducto = $scope.articuloSeleccionado.idArticulo; |
171 | } | 171 | } |
172 | 172 | ||
173 | cisterna.disponible = cisterna.capacidad - cisterna.cisternaCarga.cantidad; | 173 | cisterna.disponible = cisterna.capacidad - cisterna.cisternaCarga.cantidad; |
174 | 174 | ||
175 | cisterna.cisternaCarga.articulo = { | 175 | cisterna.cisternaCarga.articulo = { |
176 | DetArt: $scope.articuloSeleccionado.descripcion | 176 | DetArt: $scope.articuloSeleccionado.descripcion |
177 | }; | 177 | }; |
178 | 178 | ||
179 | $filter('filter')($scope.articulos, {id: $scope.articuloSeleccionado.id})[0] | 179 | $filter('filter')($scope.articulos, {id: $scope.articuloSeleccionado.id})[0] |
180 | .cargado = true; | 180 | .cargado = true; |
181 | 181 | ||
182 | $scope.calcularPorcentaje(cisterna); | 182 | $scope.calcularPorcentaje(cisterna); |
183 | //Guardar | 183 | //Guardar |
184 | var now = new Date(); | 184 | var now = new Date(); |
185 | var cisternaMovimiento = { | 185 | var cisternaMovimiento = { |
186 | fecha: now.toISOString().slice(0, 19).replace('T', ' '), | 186 | fecha: now.toISOString().slice(0, 19).replace('T', ' '), |
187 | cantidad: aCargar, | 187 | cantidad: aCargar, |
188 | metodo: 'carga', | 188 | metodo: 'carga', |
189 | idCisternaCarga: cisterna.cisternaCarga.id, | 189 | idCisternaCarga: cisterna.cisternaCarga.id, |
190 | idRemito: $scope.remito.id | 190 | idRemito: $scope.remito.id |
191 | }; | 191 | }; |
192 | cisterna.cisternaCarga.fechaReparto = fechaReparto; | 192 | cisterna.cisternaCarga.fechaReparto = fechaReparto; |
193 | cisterna.cisternaCarga.idCisterna = cisterna.id; | 193 | cisterna.cisternaCarga.idCisterna = cisterna.id; |
194 | $scope.cisternasCarga.push(cisterna.cisternaCarga); | 194 | $scope.cisternasCarga.push(cisterna.cisternaCarga); |
195 | cisternaMovimientos.push(cisternaMovimiento); | 195 | cisternaMovimientos.push(cisternaMovimiento); |
196 | } | 196 | } |
197 | 197 | ||
198 | var articuloSiguiente = $scope.articulos.filter( | 198 | var articuloSiguiente = $scope.articulos.filter( |
199 | function(filter) { | 199 | function(filter) { |
200 | return filter.cargado !== true; | 200 | return filter.cargado !== true; |
201 | } | 201 | } |
202 | ); | 202 | ); |
203 | 203 | ||
204 | if(articuloSiguiente.length > 0) { | 204 | if(articuloSiguiente.length > 0) { |
205 | $scope.seleccionarArticulo(articuloSiguiente[0]); | 205 | $scope.seleccionarArticulo(articuloSiguiente[0]); |
206 | } | 206 | } |
207 | }; | 207 | }; |
208 | $scope.calcularPorcentaje = function(cisterna) { | 208 | $scope.calcularPorcentaje = function(cisterna) { |
209 | if(!cisterna.cisternaCarga) { | 209 | if(!cisterna.cisternaCarga) { |
210 | cisterna.cisternaCarga = { | 210 | cisterna.cisternaCarga = { |
211 | cantidad: 0 | 211 | cantidad: 0 |
212 | }; | 212 | }; |
213 | } | 213 | } |
214 | var porcentaje = (cisterna.cisternaCarga.cantidad * 100 / | 214 | var porcentaje = (cisterna.cisternaCarga.cantidad * 100 / |
215 | cisterna.capacidad) + '%'; | 215 | cisterna.capacidad) + '%'; |
216 | var elementHtml = document.getElementById(cisterna.id); | 216 | var elementHtml = document.getElementById(cisterna.id); |
217 | if(elementHtml) { | 217 | if(elementHtml) { |
218 | elementHtml.style.width = porcentaje; | 218 | elementHtml.style.width = porcentaje; |
219 | } | 219 | } |
220 | }; | 220 | }; |
221 | 221 | ||
222 | $scope.seleccionarArticulo = function(articulo) { | 222 | $scope.seleccionarArticulo = function(articulo) { |
223 | $scope.articuloSeleccionado = articulo; | 223 | $scope.articuloSeleccionado = articulo; |
224 | $scope.cisternaDisponible(); | 224 | $scope.cisternaDisponible(); |
225 | $scope.autoCompletar(); | 225 | $scope.autoCompletar(); |
226 | // $scope.actualizarArticulo(); | 226 | // $scope.actualizarArticulo(); |
227 | }; | 227 | }; |
228 | 228 | ||
229 | $scope.actualizarArticulo = function() { | 229 | $scope.actualizarArticulo = function() { |
230 | $scope.articuloSeleccionado.cantidadCargada = 0; | 230 | $scope.articuloSeleccionado.cantidadCargada = 0; |
231 | for (var i = 0; i < $scope.aCargar.length; i++) { | 231 | for (var i = 0; i < $scope.aCargar.length; i++) { |
232 | if (!$scope.cisternas[i].disabled) { | 232 | if (!$scope.cisternas[i].disabled) { |
233 | 233 | ||
234 | $scope.articuloSeleccionado.cantidadCargada += | 234 | $scope.articuloSeleccionado.cantidadCargada += |
235 | parseFloat($scope.aCargar[i]) || 0; | 235 | parseFloat($scope.aCargar[i]) || 0; |
236 | } | 236 | } |
237 | } | 237 | } |
238 | }; | 238 | }; |
239 | 239 | ||
240 | $scope.autoCompletar = function() { | 240 | $scope.autoCompletar = function() { |
241 | 241 | ||
242 | var arrayMismoProducto = []; | 242 | var arrayMismoProducto = []; |
243 | var arrayVacioProducto = []; | 243 | var arrayVacioProducto = []; |
244 | 244 | ||
245 | for (var i = 0; i < $scope.cisternas.length; i++) { | 245 | for (var i = 0; i < $scope.cisternas.length; i++) { |
246 | var cisterna = $scope.cisternas[i]; | 246 | var cisterna = $scope.cisternas[i]; |
247 | cisterna.posicion = i; | 247 | cisterna.posicion = i; |
248 | 248 | ||
249 | if (!cisterna.disabled && cisterna.disponible > 0) { | 249 | if (!cisterna.disabled && cisterna.disponible > 0) { |
250 | if (cisterna.cisternaCarga.idProducto) { | 250 | if (cisterna.cisternaCarga.idProducto) { |
251 | arrayMismoProducto.push(cisterna); | 251 | arrayMismoProducto.push(cisterna); |
252 | } else { | 252 | } else { |
253 | arrayVacioProducto.push(cisterna); | 253 | arrayVacioProducto.push(cisterna); |
254 | } | 254 | } |
255 | } | 255 | } |
256 | } | 256 | } |
257 | 257 | ||
258 | arrayMismoProducto.sort(function(a,b) { | 258 | arrayMismoProducto.sort(function(a,b) { |
259 | return a.disponible - b.disponible; | 259 | return a.disponible - b.disponible; |
260 | }); | 260 | }); |
261 | 261 | ||
262 | var cisternas = arrayMismoProducto.concat(arrayVacioProducto); | 262 | var cisternas = arrayMismoProducto.concat(arrayVacioProducto); |
263 | 263 | ||
264 | for (var j = 0; j < cisternas.length; j++) { | 264 | for (var j = 0; j < cisternas.length; j++) { |
265 | var aCargar = $scope.articuloSeleccionado.cantidad - | 265 | var aCargar = $scope.articuloSeleccionado.cantidad - |
266 | ($scope.articuloSeleccionado.cantidadCargada || 0); | 266 | ($scope.articuloSeleccionado.cantidadCargada || 0); |
267 | 267 | ||
268 | if (aCargar > cisternas[j].disponible) { | 268 | if (aCargar > cisternas[j].disponible) { |
269 | aCargar = cisternas[j].disponible; | 269 | aCargar = cisternas[j].disponible; |
270 | } | 270 | } |
271 | 271 | ||
272 | if (aCargar > 0) { | 272 | if (aCargar > 0) { |
273 | $scope.aCargar[cisternas[j].posicion] = aCargar; | 273 | $scope.aCargar[cisternas[j].posicion] = aCargar; |
274 | $scope.actualizarArticulo(); | 274 | $scope.actualizarArticulo(); |
275 | } | 275 | } |
276 | } | 276 | } |
277 | }; | 277 | }; |
278 | $scope.cisternaDisponible = function() { | 278 | $scope.cisternaDisponible = function() { |
279 | for(var i = 0; i < $scope.cisternas.length; i++) { | 279 | for(var i = 0; i < $scope.cisternas.length; i++) { |
280 | //Puede meter un porcentaje del total | 280 | //Puede meter un porcentaje del total |
281 | // if($scope.articuloSeleccionado.cantidad > $scope.cisternas[i].disponible) { | 281 | // if($scope.articuloSeleccionado.cantidad > $scope.cisternas[i].disponible) { |
282 | // $scope.cisternas[i].disabled = true; | 282 | // $scope.cisternas[i].disabled = true; |
283 | // continue; | 283 | // continue; |
284 | // } | 284 | // } |
285 | if ($scope.cisternas[i].cisternaCarga && | 285 | if ($scope.cisternas[i].cisternaCarga && |
286 | $scope.cisternas[i].cisternaCarga.idProducto && | 286 | $scope.cisternas[i].cisternaCarga.idProducto && |
287 | $scope.articuloSeleccionado.idArticulo !== | 287 | $scope.articuloSeleccionado.idArticulo !== |
288 | $scope.cisternas[i].cisternaCarga.idProducto) | 288 | $scope.cisternas[i].cisternaCarga.idProducto) |
289 | { | 289 | { |
290 | $scope.cisternas[i].disabled = true; | 290 | $scope.cisternas[i].disabled = true; |
291 | continue; | 291 | continue; |
292 | } | 292 | } |
293 | $scope.cisternas[i].disabled = false; | 293 | $scope.cisternas[i].disabled = false; |
294 | } | 294 | } |
295 | }; | 295 | }; |
296 | $scope.rellenarInput = function(input, cisterna) { | 296 | $scope.rellenarInput = function(input, cisterna) { |
297 | if(!$scope.articuloSeleccionado) return; | 297 | if(!$scope.articuloSeleccionado) return; |
298 | if($scope.articuloSeleccionado.cantidad - | 298 | if($scope.articuloSeleccionado.cantidad - |
299 | $scope.articuloSeleccionado.cantidadCargada === 0) { | 299 | $scope.articuloSeleccionado.cantidadCargada === 0) { |
300 | return input; | 300 | return input; |
301 | } | 301 | } |
302 | if(!input) input = 0; | 302 | if(!input) input = 0; |
303 | input = parseFloat(input); | 303 | input = parseFloat(input); |
304 | input += parseFloat($scope.articuloSeleccionado.cantidad - | 304 | input += parseFloat($scope.articuloSeleccionado.cantidad - |
305 | $scope.articuloSeleccionado.cantidadCargada); | 305 | $scope.articuloSeleccionado.cantidadCargada); |
306 | if(input <= 0) return; | 306 | if(input <= 0) return; |
307 | if(input > cisterna.disponible) { | 307 | if(input > cisterna.disponible) { |
308 | input = cisterna.disponible; | 308 | input = cisterna.disponible; |
309 | } | 309 | } |
310 | return input; | 310 | return input; |
311 | }; | 311 | }; |
312 | $scope.distribucionDisponible = function() { | 312 | $scope.distribucionDisponible = function() { |
313 | if(!$scope.articuloSeleccionado || $scope.articuloSeleccionado.cantidad - | 313 | if(!$scope.articuloSeleccionado || $scope.articuloSeleccionado.cantidad - |
314 | $scope.articuloSeleccionado.cantidadCargada !== 0 || | 314 | $scope.articuloSeleccionado.cantidadCargada !== 0 || |
315 | !$scope.tieneArticulosPendientes()) { | 315 | !$scope.tieneArticulosPendientes()) { |
316 | return false; | 316 | return false; |
317 | } | 317 | } |
318 | for(var i = 0; i < $scope.cisternas.length; i++) { | 318 | for(var i = 0; i < $scope.cisternas.length; i++) { |
319 | if ($scope.aCargar[i] > $scope.cisternas[i].disponible && | 319 | if ($scope.aCargar[i] > $scope.cisternas[i].disponible && |
320 | !$scope.cisternas[i].disabled) | 320 | !$scope.cisternas[i].disabled) |
321 | { | 321 | { |
322 | return false; | 322 | return false; |
323 | } | 323 | } |
324 | } | 324 | } |
325 | return true; | 325 | return true; |
326 | }; | 326 | }; |
327 | 327 | ||
328 | $scope.tieneArticulosPendientes = function() { | 328 | $scope.tieneArticulosPendientes = function() { |
329 | var algunValorNegativo = $scope.aCargar.filter(function(p) { | 329 | var algunValorNegativo = $scope.aCargar.filter(function(p) { |
330 | return p < 0; | 330 | return p < 0; |
331 | }); | 331 | }); |
332 | if(algunValorNegativo.length) { | 332 | if(algunValorNegativo.length) { |
333 | return false; | 333 | return false; |
334 | } | 334 | } |
335 | var articulosDescargados = $scope.articulos.filter(function(filter) { | 335 | var articulosDescargados = $scope.articulos.filter(function(filter) { |
336 | return filter.cargado === true; | 336 | return filter.cargado === true; |
337 | }); | 337 | }); |
338 | if(articulosDescargados.length === $scope.articulos.length) { | 338 | if(articulosDescargados.length === $scope.articulos.length) { |
339 | $scope.aCargar = []; | 339 | $scope.aCargar = []; |
340 | return false; | 340 | return false; |
341 | } | 341 | } |
342 | return true; | 342 | return true; |
343 | }; | 343 | }; |
344 | 344 | ||
345 | $scope.verRemitos = function(data) { | 345 | $scope.verRemitos = function(data) { |
346 | var parametrosModal = { | 346 | var parametrosModal = { |
347 | titulo: 'Remitos cargados', | 347 | titulo: 'Remitos cargados', |
348 | data: data, | 348 | data: data, |
349 | soloMostrar: true, | 349 | soloMostrar: true, |
350 | columnas: [ | 350 | columnas: [ |
351 | { | 351 | { |
352 | nombre: 'Fecha', | 352 | nombre: 'Fecha', |
353 | propiedad: 'fechaRemito', | 353 | propiedad: 'fechaRemito', |
354 | filtro: { | 354 | filtro: { |
355 | nombre: 'date', | 355 | nombre: 'date', |
356 | parametro:'dd/MM/yyyy' | 356 | parametro:'dd/MM/yyyy' |
357 | } | 357 | } |
358 | }, | 358 | }, |
359 | { | 359 | { |
360 | nombre: 'Cliente', | 360 | nombre: 'Cliente', |
361 | propiedad: 'nombreCliente' | 361 | propiedad: 'nombreCliente' |
362 | }, | 362 | }, |
363 | { | 363 | { |
364 | nombre: 'Comprobante', | 364 | nombre: 'Comprobante', |
365 | propiedad: ['sucursal', 'numeroRemito'], | 365 | propiedad: ['sucursal', 'numeroRemito'], |
366 | filtro: { | 366 | filtro: { |
367 | nombre: 'comprobante' | 367 | nombre: 'comprobante' |
368 | } | 368 | } |
369 | }, | 369 | }, |
370 | { | 370 | { |
371 | nombre: 'Importe', | 371 | nombre: 'Importe', |
372 | propiedad: 'total' | 372 | propiedad: 'total' |
373 | } | 373 | } |
374 | ] | 374 | ] |
375 | }; | 375 | }; |
376 | 376 | ||
377 | focaModalService.modal(parametrosModal).then(function(transportista) { | 377 | focaModalService.modal(parametrosModal).then(function(transportista) { |
378 | $scope.selectVehiculo(transportista.COD, transportista.NOM); | 378 | $scope.selectVehiculo(transportista.COD, transportista.NOM); |
379 | }); | 379 | }); |
380 | }; | 380 | }; |
381 | 381 | ||
382 | function aplicarOrden(cisternas, orden) { | 382 | function aplicarOrden(cisternas, orden) { |
383 | 383 | ||
384 | cisternas.forEach(function (cisterna) { | 384 | cisternas.forEach(function (cisterna) { |
385 | 385 | ||
386 | if (!cisterna.cisternaCarga) { | 386 | if (!cisterna.cisternaCarga) { |
387 | cisterna.cisternaCarga = { orden: orden }; | 387 | cisterna.cisternaCarga = { orden: orden }; |
388 | cisterna.disponible = cisterna.capacidad; | 388 | cisterna.disponible = cisterna.capacidad; |
389 | } | 389 | } |
390 | 390 | ||
391 | }); | 391 | }); |
392 | 392 | ||
393 | } | 393 | } |
394 | 394 | ||
395 | function validarCargas(cis, remito) { | 395 | function validarCargas(cis, remito) { |
396 | 396 | ||
397 | aplicarOrden(cis, $scope.orden); | 397 | aplicarOrden(cis, $scope.orden); |
398 | 398 | ||
399 | var result = true; | 399 | var result = true; |
400 | var cisternas = angular.copy(cis); | 400 | var cisternas = angular.copy(cis); |
401 | var articulos = angular.copy(remito.articulosRemito); | 401 | var articulos = angular.copy(remito.articulosRemito); |
402 | 402 | ||
403 | cisternas.sort(ordenarCisternas); | 403 | cisternas.sort(ordenarCisternas); |
404 | 404 | ||
405 | articulos.forEach(function(articulo) { | 405 | articulos.forEach(function(articulo) { |
406 | cisternas.forEach(function(cisterna) { | 406 | cisternas.forEach(function(cisterna) { |
407 | if(!cisterna.cisternaCarga) cisterna.cisternaCarga = {}; | 407 | if(!cisterna.cisternaCarga) cisterna.cisternaCarga = {}; |
408 | //SI LA CISTERNA ESTA VACIA O | 408 | //SI LA CISTERNA ESTA VACIA O |
409 | //SI LA CISTERNA TIENE EL MISMO PRODUCTO | 409 | //SI LA CISTERNA TIENE EL MISMO PRODUCTO |
410 | //Y AUN TIENE LUGAR | 410 | //Y AUN TIENE LUGAR |
411 | if(cisterna.capacidad === cisterna.disponible || | 411 | if(cisterna.capacidad === cisterna.disponible || |
412 | (cisterna.cisternaCarga.idProducto === articulo.idArticulo && | 412 | (cisterna.cisternaCarga.idProducto === articulo.idArticulo && |
413 | cisterna.disponible > 0)){ | 413 | cisterna.disponible > 0)){ |
414 | var restante = articulo.cantidad - cisterna.disponible; | 414 | var restante = articulo.cantidad - cisterna.disponible; |
415 | 415 | ||
416 | if (restante > 0) { | 416 | if (restante > 0) { |
417 | cisterna.disponible = 0; | 417 | cisterna.disponible = 0; |
418 | articulo.cantidad = restante; | 418 | articulo.cantidad = restante; |
419 | } else { | 419 | } else { |
420 | cisterna.disponible = restante * -1; | 420 | cisterna.disponible = restante * -1; |
421 | articulo.cantidad = 0; | 421 | articulo.cantidad = 0; |
422 | } | 422 | } |
423 | 423 | ||
424 | } | 424 | } |
425 | }); | 425 | }); |
426 | //SI AUN RESTA CANTIDAD EN EL ARTICULO | 426 | //SI AUN RESTA CANTIDAD EN EL ARTICULO |
427 | if (articulo.cantidad > 0) result = false; | 427 | if (articulo.cantidad > 0) result = false; |
428 | }); | 428 | }); |
429 | return result; | 429 | return result; |
430 | } | 430 | } |
431 | function ordenarCisternas(a, b) { | 431 | function ordenarCisternas(a, b) { |
432 | //DEJA LAS CISTERNAS CON CARGA PRIMERO PARA VALIDAR LAS CARGAS CORRECTAMENTE | 432 | //DEJA LAS CISTERNAS CON CARGA PRIMERO PARA VALIDAR LAS CARGAS CORRECTAMENTE |
433 | if (a.cisternaCarga && !b.cisternaCarga) { | 433 | if (a.cisternaCarga && !b.cisternaCarga) { |
434 | return -1; | 434 | return -1; |
435 | } else if (!a.cisternaCarga && b.cisternaCarga) { | 435 | } else if (!a.cisternaCarga && b.cisternaCarga) { |
436 | return 1; | 436 | return 1; |
437 | } else { | 437 | } else { |
438 | return 0; | 438 | return 0; |
439 | } | 439 | } |
440 | } | 440 | } |
441 | }]); | 441 | }]); |
442 | 442 |