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