controller-chofer.js
7.22 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
angular.module('focaModalInforme')
.controller('focaModalInformeChoferController',
[
'$filter',
'$scope',
'$uibModalInstance',
'focaModalInformeChoferService',
'i18nService',
function($filter, $scope, $uibModalInstance,
focaModalInformeChoferService, i18nService
) {
var fecha = new Date();
$scope.generando = false;
$scope.fechaHasta = new Date();
$scope.fechaDesde = new Date(fecha.setMonth(fecha.getMonth() - 1));
$scope.buscar = true;
$scope.informe = {};
i18nService.setCurrentLang('es');
$scope.gridOptions = {
enableGridMenu: true,
exporterMenuCsv: false,
exporterPdfPageSize: 'A4',
exporterPdfFooter: function(currentPage, pageCount) {
return {
columns: [
{text: $filter('date')(new Date(), 'dd/MM/yyyy'),
margin: [40, 0]},
{text: currentPage + ' de ' + pageCount,
margin: [28, 0], alignment: 'right'}
]
};
}
};
$scope.generarInforme = function() {
$scope.generando = true;
focaModalInformeChoferService
.getDescargas(
$scope.fechaDesde.toISOString().split('.')[0],
$scope.fechaHasta.toISOString().split('.')[0]
)
.then(function(res) {
var promesas = [];
$scope.gridOptions.data = orderData(res.data);
res.data.forEach(function(descargas) {
var idsRemito = descargas.map(function(descarga) {
return descarga.remito.id;
});
idsRemito = eliminarDuplicados(idsRemito);
promesas.push(
focaModalInformeChoferService
.getDistanciaPorIdRemito(idsRemito));
});
return Promise.all(promesas);
})
.then(function(res) {
res.forEach(function(movimiento, idx) {
//Calculo lts/km y los agrego a la grilla
var ltsPorKm = 0;
if (movimiento.data.totalKms) {
ltsPorKm =
$scope.gridOptions.data[idx].litros /
movimiento.data.totalKms;
} else {
ltsPorKm = $scope.gridOptions.data[idx].litros;
}
$scope.gridOptions.data[idx].kmRecorridos =
$filter('number')(movimiento.data.totalKms);
$scope.gridOptions.data[idx].ltsPorKm =
$filter('number')(ltsPorKm, 7);
});
//TODO: sacar id empresa hardcodeado
return focaModalInformeChoferService.getEmpresa(1);
})
.then(function(res) {
//Seteo filename para PDF y Excel, y cabecera de PDF
var filenameStamp = 'Informe choferes - ' +
$filter('date')(new Date(), 'dd/MM/yyyy');
$scope.informe.nombreEmpresa = res.data.NOM.trim();
$scope.informe.direccionEmpresa = res.data.DIR.trim();
$scope.gridOptions.exporterPdfFilename = filenameStamp + '.pdf';
$scope.gridOptions.exporterExcelFilename = filenameStamp + '.xlsx';
$scope.gridOptions.exporterPdfHeader = {
columns: [
{
text: $scope.informe.nombreEmpresa,
margin: [40, 0],
fontSize: 9
},
{
text: '\nInforme de choferes',
margin: [-170, -4, 0, 0],
fontSize: 12
},
{
text: [
'\n\nFiltros: ',
'Fecha desde: ',
$filter('date')($scope.fechaDesde, 'dd/MM/yyyy'),
' Fecha hasta: ',
$filter('date')($scope.fechaHasta, 'dd/MM/yyyy')
],
margin: [-380, 2, 0, 0],
fontSize: 9
},
{
text: $scope.informe.direccionEmpresa,
margin: [28, 0],
alignment: 'right',
fontSize: 9
}
]
};
$scope.buscar = false;
$scope.generando = false;
});
};
$scope.volver = function() {
$scope.buscar = true;
};
$scope.cancel = function() {
$uibModalInstance.dismiss('Cancelar');
};
function orderData(data) {
var result = [];
data.forEach(function(descargas) {
var row = {
chofer: descargas[0].remito.hojaRuta.chofer.nombre,
litros: 0
};
descargas.forEach(function(descarga) {
row.litros += descarga.cantidad;
});
result.push(row);
});
return result;
}
function eliminarDuplicados(datos) {
var result = [];
datos.forEach(function(dato) {
if (result.indexOf(dato) === -1) {
result.push(dato);
}
});
return result;
}
}
]
);