Commit 8b6bb977b08187edb52aaeeea78f09f462ff77e2

Authored by Eric Fernandez
Exists in master and in 1 other branch develop

Merge branch 'develop' into 'master'

Develop

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