controller-productividad.js 10.6 KB
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,
                    entidad:{
                        label: ''
                    }
                };
                $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.seleccionarTipo = function() {
                    var parametrosModal = {
                        searchText: $scope.params.entidad.label,
                        columnas: [
                            {
                                propiedad: 'label',
                                nombre: 'Entidad'
                            }
                        ],
                        titulo: 'Seleccionar entidad',
                        size: 'md',
                        data: [
                            {
                                id: 0,
                                label: 'Todos'
                            },
                            {
                                id: 1,
                                label: 'Vendedores'
                            },
                            {
                                id: 2,
                                label: 'Cobradores'
                            },
                            {
                                id: 3,
                                label: 'Transportistas'
                            }
                        ]
                    };
                    
                    focaModalService.modal(parametrosModal).then(function(res) {
                        $scope.params.entidad = res;
                    });
                };
                $scope.generarInforme = function() {
                    if ($scope.params.entidad.id === undefined) {
                        focaModalService.alert('Seleccione una entidad');
                        return;
                    }
                    $scope.generando = true;
                    focaModalInformeProductividadService
                        .getInformeData($scope.params)
                        .then(function(res) {
                            var result = [];

                            if ($scope.params.entidad.id === 1 || !$scope.params.entidad.id) {
                                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 ($scope.params.entidad.id === 2 || !$scope.params.entidad.id) {
                                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 ($scope.params.entidad.id === 3 || !$scope.params.entidad.id) {
                                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.clearEntidad = function() {
                    $scope.params.entidad.label = '';
                    $scope.params.entidad.id = undefined;
                };
            }
        ]
    );