angular.module('focaModalInforme') .controller('focaModalInformeProductividadController', [ '$filter', '$scope', '$uibModalInstance', 'focaModalInformeProductividadService', 'i18nService', 'focaModalService', function($filter, $scope, $uibModalInstance, focaModalInformeProductividadService, i18nService, focaModalService ) { i18nService.setCurrentLang('es'); var fecha = new Date(); $scope.generando = false; $scope.buscar = true; $scope.informe = {}; $scope.params = { fechaHasta: new Date(), fechaDesde: new Date(fecha.setMonth(fecha.getMonth() - 1)), sector: undefined, diferenciarProductos: false, diferenciarMeses: false }; $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'} ] }; }, columnDefs: [ { field: 'cliente', enableSorting: false, cellClass: function(grid, row, col) { var value = grid.getCellValue(row,col); if (value.indexOf('Vendedores') !== -1 || value.indexOf('Cobradores') !== -1 || value.indexOf('Transportistas') !== -1) return 'yellow w-100'; if (value.indexOf('Vendedor:') !== -1 || value.indexOf('Cobrador:') !== -1 || value.indexOf('Transportista:') !== -1) return 'red w-100'; if (value.indexOf('Fecha:') !== -1) return 'green w-100'; } }, { field: 'fecha', enableSorting: false, cellClass: function(grid, row, col) { if (!grid.getCellValue(row,col)) return 'd-none'; } }, { field: 'kmRecorridos', enableSorting: false, cellClass: function(grid, row, col) { if (!grid.getCellValue(row,col)) return 'd-none'; } }, { field: 'geolocalizacion', enableSorting: false, cellClass: function(grid, row, col) { if (!grid.getCellValue(row,col)) return 'd-none'; } } ] }; $scope.generarInforme = function() { //$scope.generando = true; focaModalInformeProductividadService .getInformeData($scope.params) .then(function(res) { var result = []; console.log(res); if (true) { result.push({ cliente: 'Vendedores' }); res.data.vendedores.forEach(function(vendedor) { result.push({ cliente: 'Vendedor: ' + vendedor.NOM }); vendedor.notasPedido.forEach(function(notaPedido) { var seguimiento = notaPedido.seguimiento; result.push({ cliente: notaPedido.cliente.NOM, fecha: seguimiento ? $filter('date')(seguimiento.fecha) : $filter('date')(notaPedido.fechaCarga), kmRecorridos: seguimiento ? $filter('number')(seguimiento.kmRecorridos || 0, 2) : $filter('number')(0, 2), geolocalizacion: seguimiento ? seguimiento.latitud + ', ' + seguimiento.longitud : '' }); }); }); } if (true) { result.push({ cliente: 'Cobradores' }); res.data.cobradores.forEach(function(cobrador) { result.push({ cliente: 'Cobrador: ' + cobrador.NOM }); cobrador.cobranzas.forEach(function(cobranza) { var seguimiento = cobranza.seguimiento; result.push({ cliente: cobranza.cliente.NOM, fecha: seguimiento ? $filter('date')(seguimiento.fecha, 'dd/MM/yyyy') : $filter('date')(cobranza.FEC), kmRecorridos: seguimiento ? $filter('number')(seguimiento.kmRecorridos || 0, 2) : $filter('number')(0, 2), geolocalizacion: seguimiento ? seguimiento.latitud + ', ' + seguimiento.longitud : '' }); }); }); } if (true) { result.push({ cliente: 'Transportistas' }); res.data.transportistas.forEach(function(transportista) { result.push({ cliente: 'Transportista: ' + transportista.NOM }); transportista.remitos.forEach(function(remito) { var kms = 0, geolocalizacion; remito.hojaRutaMovimiento.forEach(function(movimiento) { kms += movimiento.seguimiento.kmRecorridos || 0; geolocalizacion = geolocalizacion || movimiento.seguimiento.latitud + ', ' + movimiento.seguimiento.longitud; }); result.push({ cliente: remito.cliente.NOM, fecha: $filter('date')(remito.fechaRemito, 'dd/MM/yyyy'), kmRecorridos: $filter('number')(kms, 2), geolocalizacion: geolocalizacion }); }); }); } $scope.gridOptions.data = result; $scope.generando = false; $scope.buscar = false; }); }; $scope.volver = function() { $scope.buscar = true; }; $scope.cancel = function() { $uibModalInstance.dismiss('Cancelar'); }; $scope.clearSector = function() { $scope.params.sector = undefined; }; } ] );