controller.js 7.74 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'
            }];

            //SETEO BOTONERA LATERAL
            $timeout(function() {
                focaBotoneraLateralService.showSalir(false);
                focaBotoneraLateralService.showPausar(false);
                focaBotoneraLateralService.showCancelar(false);
                focaBotoneraLateralService.showGuardar(false);
                focaBotoneraLateralService.addCustomButton('Salir', salir);
            });

            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 parametrosModal = {
                    titulo: 'Búsqueda de Transportista',
                    query: '/transportista',
                    columnas: [
                        {
                            nombre: 'Código',
                            propiedad: 'COD'
                        },
                        {
                            nombre: 'Nombre',
                            propiedad: 'NOM'
                        },
                        {
                            nombre: 'CUIT',
                            propiedad: 'CUIT'
                        }
                    ]
                };
                focaModalService.modal(parametrosModal).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;
                });
            }

            function salir() {
                focaAbmChoferService.transportistaSeleccionado = {};
                $location.path('/');
            }
        }
    ])
    .controller('focaAbmChoferController', [
        '$scope', 'focaAbmChoferService', '$routeParams',
        '$location', 'focaBotoneraLateralService', '$timeout', 'focaModalService',
        function($scope, focaAbmChoferService, $routeParams,
            $location, focaBotoneraLateralService, $timeout, focaModalService) {

            $scope.focused = 1;
            $scope.nuevo = $routeParams.id === '0';
            $scope.chofer = {};
            $scope.transportistas = [];
            $scope.now = new Date();
            $scope.next = function(key) {
                if (key === 13) $scope.focused++;
            };

            focaAbmChoferService.getTiposDocumento().then(function(res) {
                $scope.tiposDocumento = res.data;
            });

            //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(key) {                

                if (!$scope.chofer.nombre) {
                    focaModalService.alert('Ingrese nombre');
                    return;
                } else if (!$scope.chofer.idTipoDocumento) {
                    focaModalService.alert('Seleccione tipo de documento');
                    return;
                } else if (!$scope.chofer.dni){
                    focaModalService.alert('Ingrese DNI');
                    return;
                }

                key = (typeof key  === 'undefined') ? 13 : key;
                if(key === 13) {
                    validaDni().then(function() {
                        $scope.chofer.idTransportista = $routeParams.idTransportista;
                        delete $scope.chofer.transportista;
                        focaAbmChoferService.guardarChofer($scope.chofer).then(function() {
                            $location.path('/chofer');
                        });
                    }, function() {
                        focaModalService.alert('Dni existente');
                    });
                }
            };

            function validaDni() {
                return new Promise(function(resolve, reject) {
                    focaAbmChoferService
                        .getChoferPorDni($scope.chofer.dni)
                        .then(function(res) {
                            if(res.data.id &&
                                $scope.chofer.id !== res.data.id) {
                                reject(res.data);
                            }else {
                                resolve();
                            }
                        });
                });
            }
        }
    ]);