controller.js
6.14 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
angular.module('focaModalDetalleHojaRuta')
.controller('focaModalDetalleHojaRutaController',
[
'$filter',
'$scope',
'$uibModalInstance',
'idRemito',
'focaModalDetalleHojaRutaService',
'focaModalService',
'focaSeguimientoService',
function($filter, $scope, $uibModalInstance, idRemito, focaModalDetalleHojaRutaService,
focaModalService, focaSeguimientoService)
{
//Seteo variables
$scope.remito = {};
$scope.cisternas = [];
$scope.articuloSeleccionado = {};
$scope.aDescargar = [];
$scope.cargando = true;
//Datos
var promesaRemito = focaModalDetalleHojaRutaService
.getRemitoById(idRemito);
var promesaCisternas = focaModalDetalleHojaRutaService
.getCisternasByIdRemito(idRemito);
Promise.all([promesaRemito, promesaCisternas]).then(function(res) {
$scope.cargando = false;
$scope.remito = res[0].data;
$scope.cisternas = res[1].data;
$scope.cambio($scope.remito.articulosRemito[0]);
$scope.$digest();
}, function() {
focaModalService.alert('El servicio no responde intente más tarde');
$uibModalInstance.dismiss();
});
$scope.cambio = function(articulo) {
$scope.aDescargar = [];
$scope.articuloSeleccionado = articulo;
};
$scope.descargar = function(key) {
var hojaRutaMovimientos = [];
var cisternaMovimientos = [];
var cisternaCargas = [];
var totalADescargar = 0;
for (var i = 0; i < $scope.aDescargar.length; i++) {
totalADescargar += $scope.aDescargar[i] || 0;
}
if(key === 13) {
focaModalService
.confirm('¿Desea descargar ' + totalADescargar + ' litros de ' +
$scope.articuloSeleccionado.descripcion + '?')
.then(descargar);
}
function descargar() {
for (var i = 0; i < $scope.cisternas.length; i++) {
var descarga = $scope.aDescargar[i];
if(!descarga) continue;
var cisternaCarga = $scope.cisternas[i].cisternaCarga;
cisternaCarga.cantidad -= descarga;
//Guardar
var now = new Date();
var cisternaMovimiento = {
fecha: now.toISOString().slice(0, 19).replace('T', ' '),
cantidad: descarga,
metodo: 'descarga',
idCisternaCarga: cisternaCarga.id,
idRemito: $scope.remito.id
};
var hojaRutaMovimiento = {
reciboDescarga: $scope.numeroRecibo,
idRemito: $scope.remito.id
};
delete cisternaCarga.articulo;
cisternaCargas.push(cisternaCarga);
cisternaMovimientos.push(cisternaMovimiento);
hojaRutaMovimientos.push(hojaRutaMovimiento);
}
var save = {
cisternaCargas: cisternaCargas,
cisternaMovimientos: cisternaMovimientos,
hojaRutaMovimientos: hojaRutaMovimientos
};
focaModalDetalleHojaRutaService
.postMovimientoHojaRuta(save)
.then(guardarSeguimiento)
.catch(error);
function guardarSeguimiento(res) {
focaSeguimientoService
.guardarPosicion(
'Entrega de producto',
res.data[0].id,
$scope.remito.observaciones);
$scope.aDescargar = [];
$scope.remito.observaciones = '';
success();
}
}
};
$scope.cancel = function() {
$uibModalInstance.dismiss('cancel');
};
$scope.distribucionDisponible = function() {
return $scope.articuloSeleccionado.cantidadDescargada ===
$scope.articuloSeleccionado.cantidad;
};
$scope.actualizarArticulo = function() {
$scope.articuloSeleccionado.cantidadDescargada = 0;
for(var i = 0; i < $scope.aDescargar.length; i++) {
$scope.articuloSeleccionado.cantidadDescargada +=
parseFloat($scope.aDescargar[i]) || 0;
}
};
$scope.rechazar = function() {
focaModalService
.prompt('Aclare el motivo de rechazo')
.then(function() {
focaModalService.alert('desarrollo');
$uibModalInstance.dismiss();
});
};
//funciones
function error(error) {
focaModalService.alert('Hubo un error ' + error);
}
function success() {
focaModalService.alert('Operación realizada con éxito');
}
}
]
);