diff --git a/src/js/controller-general-unidad.js b/src/js/controller-general-unidad.js index 623cab7..b52b6f2 100644 --- a/src/js/controller-general-unidad.js +++ b/src/js/controller-general-unidad.js @@ -20,7 +20,7 @@ angular.module('focaModalInforme') fechaDesde: new Date(fecha.setMonth(fecha.getMonth() - 1)), sector: undefined, diferenciarProductos: false, - detallarPorMes: false + diferenciarMeses: false }; $scope.gridOptions = { enableGridMenu: true, @@ -45,7 +45,7 @@ angular.module('focaModalInforme') return 'yellow w-100'; if (grid.getCellValue(row,col).indexOf('Producto:') !== -1) return 'red w-100'; - if (grid.getCellValue(row,col).indexOf('Mes:') !== -1) + if (grid.getCellValue(row,col).indexOf('Fecha:') !== -1) return 'green w-100'; } }, @@ -91,18 +91,34 @@ angular.module('focaModalInforme') $filter('rellenarDigitos')(sector.idSector, 2, '0') + ' - ' + sector.sector.trim() }); + if (!$scope.params.diferenciarProductos) + sector.productos = unirProductos(sector.productos); + sector.productos.forEach(function(producto) { //AGREGO PRODUCTOS - result.push({ - vehiculo: 'Producto: ' + - producto.idProducto + - ' - ' + producto.producto - }); - producto.fechas.forEach(function(fecha) { - //AGREGO FECHAS + if ($scope.params.diferenciarProductos){ result.push({ - vehiculo: 'Mes: ' + fecha.fecha + vehiculo: 'Producto: ' + + producto.idProducto + + ' - ' + producto.producto }); + } + if (!$scope.params.diferenciarMeses) { + producto.fechas = unirFechas(producto.fechas); + } + producto.fechas.forEach(function(fecha) { + //AGREGO FECHAS + if ($scope.params.diferenciarMeses) { + result.push({ + vehiculo: + 'Fecha: ' + fecha.month + ' - ' + fecha.year + }); + } else { + result.push({ + vehiculo: + 'Fecha: ' + fecha.year + }); + } fecha.vehiculos.forEach(function(vehiculo) { //AGREGO VEHICULOS result.push({ @@ -156,6 +172,70 @@ angular.module('focaModalInforme') $scope.cancel = function() { $uibModalInstance.dismiss('Cancelar'); }; + + function unirProductos(productos) { + var result = [{fechas: []}]; + productos.forEach(function(producto) { + producto.fechas.forEach(function(fecha) { + var existe = result[0].fechas.filter(function(result) { + return result.fecha === fecha.fecha; + }); + if (existe.length) { + existe[0].vehiculos = existe[0].vehiculos.concat(fecha.vehiculos); + } else { + result[0].fechas.push(fecha); + } + + }); + }); + result[0].fechas.forEach(function(fecha) { + fecha.vehiculos = unirVehiculos(fecha.vehiculos); + }); + return result; + } + + function unirFechas(fechas) { + var results = []; + fechas.forEach(function(fecha) { + var existe = results.filter(function(result) { + return result.year === fecha.year; + }); + + if (existe.length) { + existe[0].vehiculos = existe[0].vehiculos.concat(fecha.vehiculos); + } else { + results.push({ + year: fecha.year, + vehiculos: fecha.vehiculos + }); + } + }); + + results.forEach(function(result) { + result.vehiculos = unirVehiculos(result.vehiculos); + }); + return results; + } + + function unirVehiculos(vehiculos) { + var results = []; + vehiculos.forEach(function(vehiculo) { + var existe = results.filter(function(result) { + return result.vehiculo === vehiculo.vehiculo; + }); + + if (existe.length) { + existe[0].data.kms += vehiculo.data.kms; + existe[0].data.lts += vehiculo.data.lts; + existe[0].data.viajes += vehiculo.data.viajes; + } else { + results.push(vehiculo); + } + }); + return results; + } + + } ] ); diff --git a/src/views/informe-general-unidad.html b/src/views/informe-general-unidad.html index 4f0f3f3..4eb0ec8 100644 --- a/src/views/informe-general-unidad.html +++ b/src/views/informe-general-unidad.html @@ -91,7 +91,7 @@