controllerDetalleVehiculo.js
6.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
angular.module('focaLogisticaPedidoRuta')
.controller('focaDetalleVehiculo',
['$scope',
'$uibModalInstance',
'idVehiculo',
'idRemito',
'focaModalService',
'$filter',
'focaLogisticaPedidoRutaService',
function($scope, $uibModalInstance, idVehiculo, idRemito, focaModalService, $filter,
focaLogisticaPedidoRutaService
) {
//seteo variables
$scope.idRemito = idRemito;
$scope.articulos = [];
$scope.vehiculo = {};
$scope.remito = {};
$scope.aCargar = [];
var cisternaMovimientos = [];
focaLogisticaPedidoRutaService.obtenerVehiculoById(idVehiculo).then(
function(res) {
$scope.vehiculo = res.data;
}
);
if (idRemito !== -1) {
focaLogisticaPedidoRutaService.obtenerRemitoById(idRemito).then(
function(res) {
$scope.remito = res.data;
if($scope.remito.idUsuarioProceso) {
focaModalService.alert('El remito esta siendo cargado por otro usario');
$uibModalInstance.close();
}
$scope.articulos = res.data.articulosRemito;
}
);
}
$scope.aceptar = function() {
$scope.cargando = true;
var cisternaCargas = [];
for (var i = 0; i < $scope.vehiculo.cisternas.length; i++) {
delete $scope.vehiculo.cisternas[i].cisternaCarga.articulo;
cisternaCargas.push($scope.vehiculo.cisternas[i].cisternaCarga);
}
var cisterna = {
cisternaMovimientos: cisternaMovimientos,
cisternaCargas: cisternaCargas,
idVehiculo: $scope.vehiculo.id
};
focaLogisticaPedidoRutaService.guardarCisternas(cisterna, $scope.remito.id)
.then(function() {
focaModalService.alert('Cisternas cargadas con éxito').then(function() {
$scope.cargando = false;
$uibModalInstance.close();
});
}).catch(function(error) {
$scope.cargando = false;
$uibModalInstance.close();
if (error.status === 403.1) {
focaModalService.alert('ERROR: El vehículo esta en uso');
}
if(error.status === 403.2) {
focaModalService.alert('ERROR: Otro usario ya cargó este remito');
return;
}
focaModalService.alert('Hubo un error al cargar las cisternas');
});
};
$scope.cancelar = function() {
$uibModalInstance.close();
};
$scope.cargarACisternas = function(vehiculo) {
for(var i = 0; i < vehiculo.cisternas.length; i++) {
var cisterna = vehiculo.cisternas[i];
var aCargar = parseFloat($scope.aCargar[i]);
//validaciones
if(!aCargar) {
continue;
}
if(aCargar > cisterna.disponible) {
focaModalService.alert('La cantidad cargada supera la capacidad de la' +
'cisterna ' + cisterna.codigo);
return;
}
//cargar
if(cisterna.cisternaCarga.cantidad) {
cisterna.cisternaCarga.cantidad += aCargar;
}else {
cisterna.cisternaCarga.cantidad = aCargar;
cisterna.cisternaCarga.idProducto = $scope.articuloSeleccionado.idArticulo;
}
cisterna.disponible = cisterna.capacidad - cisterna.cisternaCarga.cantidad;
cisterna.cisternaCarga.articulo = {
DetArt: $scope.articuloSeleccionado.descripcion
};
$filter('filter')($scope.articulos, {id: $scope.articuloSeleccionado.id})[0]
.cargado = true;
$scope.calcularPorcentaje(cisterna);
//Guardar
var now = new Date();
var cisternaMovimiento = {
fecha: now.toISOString().slice(0, 19).replace('T', ' '),
cantidad: aCargar,
metodo: 'carga',
idCisternaCarga: cisterna.cisternaCarga.id,
idRemito: $scope.remito.id
};
cisternaMovimientos.push(cisternaMovimiento);
}
var articuloSiguiente = $scope.articulos.filter(
function(filter) {
return filter.cargado !== true;
}
);
if(articuloSiguiente.length > 0) {
$scope.cambioArticulo(articuloSiguiente[0]);
}
$scope.aCargar = [];
};
$scope.calcularPorcentaje = function(cisterna) {
if(!cisterna.cisternaCarga.cantidad) {
cisterna.cisternaCarga.cantidad = 0;
}
var porcentaje = (cisterna.cisternaCarga.cantidad * 100 /
cisterna.capacidad) + '%';
var elementHtml = document.getElementById(cisterna.id);
if(elementHtml) {
elementHtml.style.width = porcentaje;
}
};
$scope.cambioArticulo = function(articulo) {
articulo.checked = true;
$scope.articuloSeleccionado = articulo;
};
$scope.actualizarArticulo = function () {
$scope.articuloSeleccionado.cantidadCargada = 0;
for (var i = 0; i < $scope.aCargar.length; i++) {
$scope.articuloSeleccionado.cantidadCargada +=
parseFloat($scope.aCargar[i]) || 0;
}
};
$scope.tieneArticulosPendientes = function() {
var articulosDescargados = $scope.articulos.filter(function(filter) {
return filter.cargado === true;
});
if(articulosDescargados.length === $scope.articulos.length) {
return false;
}
return true;
};
}]);