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

            $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('/');
            }
            
            if ($localStorage.chofer) {
                var chofer = JSON.parse($localStorage.chofer);
                if (!chofer.id) { chofer.id = 0; }
                $location.path('/chofer/' + chofer.id + '/' + chofer.idTransportista);
            }
        }
    ])
    .controller('focaAbmChoferController', [
        '$scope', 'focaAbmChoferService', '$routeParams', '$localStorage', '$filter',
        '$location', 'focaBotoneraLateralService', '$timeout', 'focaModalService', '$window',
        function($scope, focaAbmChoferService, $routeParams, $localStorage, $filter,
            $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(false);
                focaBotoneraLateralService.showGuardar(true, $scope.guardar);
                focaBotoneraLateralService.addCustomButton('Cancelar', cancelar);

            });

            $timeout(function() {getLSChofer();});
            
            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;
            });

            function setearChofer(chofer) {
                $scope.chofer = chofer;                
                $scope.$broadcast('addCabecera', {
                    label: 'Transportista:',
                    valor: $filter('rellenarDigitos')(chofer.idTransportista.chofer, 2) + ' - '
                });                
            }
            function getLSChofer() {
                var chofer = JSON.parse($localStorage.chofer || null);
                
                if (chofer) {
                    setearChofer(chofer);
                    delete $localStorage.chofer;
                }
            }

            $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');
                    });
                }
            };
            
            $scope.$watch('chofer', function(newValue) {
                focaBotoneraLateralService.setPausarData({
                    label: 'chofer',
                    val: {
                        dni: newValue.dni,
                        idTransportista: newValue.idTransportista,
                        nombre: newValue.nombre,
                        telefono: newValue.telefono,
                        idTipoDocumento: newValue.idTipoDocumento,
                        id: newValue.id
                    }
                });
            }, true);

            function cancelar() {
                if ($scope.formChofer.$pristine == false) {
                    focaModalService.confirm(
                        '¿Está seguro de que desea salir? Se perderán todos los datos cargados.'
                    ).then(function(data) {
                        if (data) {
                            $location.path('/chofer');
                        }
                    });
                } else {
                    $location.path('/chofer');
                }
            }

            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();
                            }
                        });
                });
            }
        }
    ]);