controller.js 7.52 KB
angular.module('focaLogisticaPedidoRuta') .controller('focaLogisticaPedidoRutaController', [
    '$scope', 'focaLogisticaPedidoRutaService', '$location', '$uibModal', '$filter',
        'focaModalService', 'focaBotoneraLateralService', '$interval',
    function($scope, focaLogisticaPedidoRutaService, $location, $uibModal, $filter,
        focaModalService, focaBotoneraLateralService, $interval
    ) {
        $scope.actividad = 'Logistica';

        //Datos Pantalla
        $scope.titulo = 'Logistica de Pedidos';
        $scope.botonera = ['Transportista'];
        var cabecera = '';

        $scope.idVendedor = 0;
        $scope.marcadores = [];
        $scope.vehiculos = [];
        getSeguimiento();
        $scope.arrastrando = false;
        $scope.general = function() {
            $scope.idVendedor = 0;
            getSeguimiento();
        };

        //SETEO BOTONERA LATERAL
        focaBotoneraLateralService.showSalir(true);
        focaBotoneraLateralService.showPausar(false);
        focaBotoneraLateralService.showGuardar(false);


        $scope.general = function() {
            $scope.idVendedor = 0;
            getSeguimiento();
            $scope.$broadcast('removeCabecera', cabecera);
            $scope.$broadcast('addCabecera',{
                label: 'General',
                valor: ''
            });
        };

        $scope.cargar = function(idVehiculo, punto) {
            var idRemito;
            if(punto === -1) {
                idRemito = -1;
            }else {
                idRemito = JSON.parse(punto).notaPedido.remito.id;
            }
            var modalInstance = $uibModal.open(
                {
                    ariaLabelledBy: 'Busqueda de Vehiculo',
                    templateUrl: 'foca-detalle-vehiculo.html',
                    controller: 'focaDetalleVehiculo',
                    size: 'lg',
                    resolve: {
                        idVehiculo: function() {return idVehiculo;},
                        idRemito: function() {return idRemito;}
                    }
                }
            );
            modalInstance.result.then(function() {
            }, function() {
            });
        };

        $scope.quitarVehiculo = function(vehiculo) {
            focaModalService.confirm('Esta seguro que desea eliminar el vehículo ' +
                vehiculo.codigo + '?').then(function() {
                    eliminarVehiculo(vehiculo);
                });
        };

        $scope.hacerHojaRuta = function(vehiculo) {
            var modalInstance = $uibModal.open(
                {
                    ariaLabelledBy: 'Creación hoja ruta',
                    templateUrl: 'foca-modal-crear-hoja-ruta.html',
                    controller: 'focaModalCrearHojaRuta',
                    size: 'lg',
                    resolve: {
                        idVehiculo: function() {return vehiculo.id;}
                    }
                }
            );
            modalInstance.result.then(function() {
                
            }, function() {
                //usar cuando se cancela el modal
            });
        };

        $scope.arrastra = function() {
            $scope.arrastrando = true;
            $scope.$digest();
        };

        $scope.noArrastra = function() {
            $scope.arrastrando = false;
            $scope.$digest();
        };

        $scope.individual = function() {
            $scope.idVendedor = -1;
        };

        $scope.mostrarDetalle = function() {
            $scope.detalle = true;
        };

        $scope.salir = function() {
            $location.path('/');
        };

        $scope.search = function(key) {
            if (key === 13) {
                $scope.idVendedor = $scope.idVendedorInput;
                getSeguimiento();
                $scope.$broadcast('removeCabecera', 'General');
                $scope.$broadcast('addCabecera', {
                    label: cabecera,
                    valor: $scope.idVendedorInput
                });
            }
        };

        $scope.fecha = function() {
            getSeguimiento();
        };

        $scope.seleccionarTransportista = function() {
            var modalInstance = $uibModal.open(
                {
                    ariaLabelledBy: 'Busqueda de Transportista',
                    templateUrl: 'modal-proveedor.html',
                    controller: 'focaModalProveedorCtrl',
                    size: 'lg',
                    resolve: {
                        transportista: function() {
                            return true;
                        }
                    }
                }
            );
            modalInstance.result.then(function(transportista) {
                $scope.seleccionarVehiculo(transportista.COD);
            });
        };

        $scope.seleccionarVehiculo = function(idTransportista) {
            var query = '/vehiculo/transportista/' + idTransportista;
            var columnas = {
                nombre: ['Código', 'tractor', 'Semi', 'Capacidad'],
                propiedad: ['codigo', 'tractor', 'semi', 'capacidadTotalCisternas']
            };
            var titulo = 'Búsqueda de vehiculos';
            focaModalService.modal(columnas, query, titulo).then(
                function(vehiculo) {
                    var existe = $filter('filter')($scope.vehiculos, {id: vehiculo.id});
                    if(existe.length) {
                        focaModalService.alert('El vehiculo ya ha sido cargado');
                        return;
                    }
                    if(!vehiculo.cisternas.length) {
                        focaModalService.alert('El vehiculo no tiene cisternas');
                        return;
                    }
                    $scope.vehiculos.push(vehiculo);
                }, function() {
                    // funcion ejecutada cuando se cancela el modal
                });
        };

        function getSeguimiento() {
            var desde = new Date('1900/01/01');
            var hasta = new Date('2099/01/01');
            if ($scope.now) {
                var now = $scope.now;
                desde = new Date(new Date(now.setHours(0)).setMinutes(0));
                desde = desde.setDate(desde.getDate() - 1);
                desde = new Date(desde);
            }
            var datos = {
                actividad: $scope.actividad,
                idUsuario: $scope.idVendedor,
                fechaDesde: desde,
                fechaHasta: hasta
            };

            $scope.datosBuscados = {
                actividad: $scope.actividad,
                individual: $scope.idVendedor ? true : false
            };

            focaLogisticaPedidoRutaService.obtenerActividad(datos).then(function(datos) {
                if(JSON.stringify(datos.data) !== JSON.stringify($scope.marcadores)) {
                    $scope.marcadores = datos.data;
                }
            });
        }

        function eliminarVehiculo(vehiculo) {
            focaLogisticaPedidoRutaService.getRemitos(vehiculo.id).then(function(res) {
                if(!focaLogisticaPedidoRutaService.obtenerRemitosDeCarga(res.data).length) {
                    $scope.vehiculos.splice($scope.vehiculos.indexOf(vehiculo), 1);
                }else {
                    focaModalService.alert('No ha sido posible eliminar el vehiculo porque ' +
                        'tiene remitos asociados').then(function() {
                            $scope.hacerHojaRuta(vehiculo);
                        });
                }
            });
        }
        $interval(function() {
            getSeguimiento();
        }, 5000);
    }
]);