controller.js 5.86 KB
angular.module('focaAbmChofer')
    .controller('focaAbmChoferesController', [
        '$scope', 'focaAbmChoferService', '$location', '$uibModal',
        'focaModalService', 'focaBotoneraLateralService', '$timeout',
        function($scope, focaAbmChoferService, $location, $uibModal, focaModalService,
            focaBotoneraLateralService, $timeout) {

            $scope.now = new Date();
            $scope.filters = '';
            $scope.choferes = [];
            $scope.botonera = [{
                label: 'Transportista',
                image: 'cliente.png'
            }];
            $scope.focused = 1;

            //SETEO BOTONERA LATERAL
            $timeout(function() {
                focaBotoneraLateralService.showSalir(true);
                focaBotoneraLateralService.showPausar(false);
                focaBotoneraLateralService.showCancelar(false);
                focaBotoneraLateralService.showGuardar(false);
            });

            if(focaAbmChoferService.transportistaSeleccionado.COD) {
                elegirTransportista(focaAbmChoferService.transportistaSeleccionado);
            }

            $scope.editar = function(id) {
                $location.path('/chofer/' + id + '/' + $scope.idTransportista);
            };

            $scope.solicitarConfirmacion = function(chofer) {
                focaModalService.confirm('¿Está seguro que desea borrar el chofer ' +
                    chofer.nombre + ' ?').then(function(confirmed) {
                        if(confirmed) {
                            focaAbmChoferService.deleteChofer(chofer.id);
                            $scope.choferes.splice($scope.choferes.indexOf(chofer), 1);
                        }
                    });
            };

            $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) {
                        elegirTransportista(transportista);
                        focaAbmChoferService.transportistaSeleccionado = transportista;
                    }, function() {}
                );
            };

            function elegirTransportista(transportista) {
                buscar(transportista.COD);
                var codigo = ('00000' + transportista.COD).slice(-5);
                $scope.idTransportista = transportista.COD;
                $timeout(function() {
                    $scope.$broadcast('addCabecera', {
                        label: 'Transportista:',
                        valor: codigo + ' - ' + transportista.NOM
                    });
                });
            }

            function buscar(id) {
                focaAbmChoferService.getChoferPorTransportista(id).then(function(res) {
                    $scope.choferes = res.data;
                });
            }
        }
    ])
    .controller('focaAbmChoferController', [
        '$scope', 'focaAbmChoferService', '$routeParams',
        '$location', 'focaBotoneraLateralService', '$timeout',
        function($scope, focaAbmChoferService, $routeParams,
            $location, focaBotoneraLateralService, $timeout) {
            $scope.nuevo = $routeParams.id === '0';
            $scope.chofer = {};
            $scope.transportistas = [];
            $scope.now = new Date();
            $scope.next = function(key) {
                if (key === 13) $scope.focused++;
            };

            //SETEO BOTONERA LATERAL
            $timeout(function() {
                focaBotoneraLateralService.showSalir(false);
                focaBotoneraLateralService.showPausar(true);
                focaBotoneraLateralService.showCancelar(true);
                focaBotoneraLateralService.showGuardar(true, $scope.guardar);
            });

            if($scope.nuevo) {
                focaAbmChoferService
                    .getTransportistaPorId($routeParams.idTransportista)
                    .then(function(res) {
                        var codigo = ('00000' + res.data.COD).slice(-5);
                        $scope.chofer.idTransportista = res.data.COD;
                        $scope.chofer.transportista = res.data;
                        $scope.$broadcast('addCabecera', {
                            label: 'Transportista:',
                            valor: codigo + ' - ' + res.data.NOM
                        });
                    });
            }

            focaAbmChoferService.getChofer($routeParams.id).then(function(res) {
                if(res.data) {
                    var codigo = ('00000' + res.data.transportista.COD).slice(-5);
                    $scope.chofer = res.data;
                    $scope.$broadcast('addCabecera', {
                        label: 'Transportista:',
                        valor: codigo + ' - ' + res.data.transportista.NOM
                    });
                }
            });

            focaAbmChoferService.getTransportistas().then(function(res) {
                $scope.transportistas = res.data;
            });

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

            $scope.guardar = function() {
                $scope.chofer.idTransportista = $routeParams.idTransportista;
                delete $scope.chofer.transportista;
                focaAbmChoferService.guardarChofer($scope.chofer).then(function() {
                    $location.path('/chofer');
                });
            };
        }
    ]);