controller.js 7.95 KB
angular.module('focaAbmVehiculo')
    .controller('focaAbmVehiculosController', [
        '$scope', 'focaAbmVehiculoService', '$location', 'focaModalService',
        '$uibModal', 'focaBotoneraLateralService', '$timeout',
        function($scope, focaAbmVehiculoService, $location, focaModalService,
            $uibModal, focaBotoneraLateralService, $timeout) {

            $scope.now = new Date();
            $scope.botonera = ['Transportista'];

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

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

            $scope.editar = function(id) {
                $location.path('/vehiculo/' + id + '/' + $scope.idTransportista);
            };
            $scope.solicitarConfirmacion = function(vehiculo) {
                focaModalService.confirm('¿Está seguro que desea borrar el vehiculo ' +
                vehiculo.id + ' ' + vehiculo.tractor + ' ?').then(
                        function(data) {
                            if(data) {
                                focaAbmVehiculoService.deleteVehiculo(vehiculo.id);
                                $scope.vehiculos.splice($scope.vehiculos.indexOf(vehiculo), 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);
                        focaAbmVehiculoService.transportistaSeleccionado = transportista;
                    }, function() {

                    }
                );
            };

            function elegirTransportista(transportista) {
                $scope.idTransportista = transportista.COD;
                $scope.filtros = transportista.NOM.trim();
                $timeout(function() {
                    $scope.$broadcast('addCabecera', {
                        label: 'Transportista:',
                        valor: transportista.COD + ' - ' + transportista.NOM
                    });
                });
                buscar(transportista.COD);
            }
            
            function buscar(idTransportista) {
                focaAbmVehiculoService
                    .getVehiculosPorTransportista(idTransportista)
                    .then(function(datos) {
                        $scope.vehiculos = datos.data;
                    });
            }
            
        }
    ])
    .controller('focaAbmVehiculoController', [
        '$scope', 'focaAbmVehiculoService', '$routeParams', '$location', '$uibModal',
        'focaModalService', '$timeout', 'focaBotoneraLateralService',
        function($scope, focaAbmVehiculoService, $routeParams, $location, $uibModal,
        focaModalService, $timeout, focaBotoneraLateralService) {
            $scope.nuevo = $routeParams.idVehiculo == 0 ? true : false;
            $scope.now = new Date();
            $scope.focused = 1;

            

            $timeout(function() {
                focaBotoneraLateralService.showSalir(false);
                focaBotoneraLateralService.showPausar(false);
                focaBotoneraLateralService.showCancelar(true);
                focaBotoneraLateralService.showGuardar(true, $scope.guardar);
            });
            console.log($routeParams);


            if($scope.nuevo) {
                focaAbmVehiculoService
                    .getTransportistaPorId($routeParams.idTransportista)
                    .then(function(res) {
                        $scope.vehiculo.idTransportista = res.data.COD;
                        $scope.vehiculo.transportista = res.data;
                        $scope.$broadcast('addCabecera', {
                            label: 'Transportista:',
                            valor: res.data.COD + ' - ' + res.data.NOM
                        });
                    });
            }
            $scope.vehiculo = {};
            focaAbmVehiculoService.getVehiculo($routeParams.idVehiculo).then(function(res) {
                if(res.data) {
                    $scope.vehiculo = res.data;
                    $scope.$broadcast('addCabecera', {
                        label: 'Transportista:',
                        valor: res.data.transportista.COD + ' - ' + res.data.transportista.NOM
                    });
                    $scope.$broadcast('addCabecera', {
                        label: 'Vehículo:',
                        valor: res.data.codigo
                    });
                } 
            });
            focaAbmVehiculoService.getCisternadoPorVehiculo($routeParams.idVehiculo)
                .then(function(res) {
                    $scope.cisternas = res.data;
            });
            $scope.next = function(key) {
                if (key === 13) $scope.focused++;
            };
            $scope.cancelar = function() {
                $location.path('/vehiculo');
            };
            $scope.editar = function(id) {
                $location.path('/vehiculo/' + $routeParams.idVehiculo + '/cisterna/' + id);
            };
            $scope.guardar = function() {
                if(!$scope.vehiculo.transportista) {
                    focaModalService.alert('Elija Transportista');
                    return;
                }
                delete $scope.vehiculo.transportista;
                focaAbmVehiculoService.guerdarVehiculo($scope.vehiculo).then(function(res) {
                    if($scope.nuevo) {
                        $location.path('/vehiculo/' + res.data.id + '/' + res.data.idTransportista);
                    } else {
                        $location.path('/vehiculo');
                    }
                });
            };
            $scope.solicitarConfirmacionCisterna = function(cisterna) {
                focaModalService.confirm('¿Está seguro que desea borrar la cisterna ' +
                cisterna.id + ' ' + cisterna.codigo + ' ?').then(
                        function(data) {
                            if(data) {
                                focaAbmVehiculoService.deleteCisterna(cisterna.id);
                                $scope.cisternas.splice($scope.cisternas.indexOf(cisterna), 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) {
                        $scope.vehiculo.idTransportista = transportista.COD;
                        $scope.vehiculo.transportista = transportista;
                    }, function() {

                    }
                );
            };
        }
    ]);