<?php
if(file_exists("../../config/config_dir_raiz.php")) { require_once("../../config/config_dir_raiz.php"); } elseif(file_exists("config/config_dir_raiz.php")) { require_once("config/config_dir_raiz.php"); }
$config = new Config();
require_once($config->diretorio_raiz()."/src/controller/Funcoes_bd.php");
require_once($config->diretorio_raiz()."/src/model/Bi_colunas_layout_imp.php");
require_once($config->diretorio_raiz()."/src/model/bd/Bi_layout_importacaoDAO.php");
class Bi_colunas_layout_impDAO
{
	private $_funcoes_bd;
	private $_banco_atual;
	private $conexao;
	function __construct()
	{
		$this->conexao = Conexao::obterConexao();
		$this->_banco_atual = Conexao::getUtilizarBanco();
		$this->_funcoes_bd = new Funcoes_bd();
	}
	public function salvar($bi_colunas_layout_imp)
	{
		$inputs = array();
		if($bi_colunas_layout_imp->getCodigo() != "")
		{
			array_push($inputs, array('campo'=>':codigo', 'valor'=>$bi_colunas_layout_imp->getCodigo()));
		}
		else
		{
			array_push($inputs, array('campo'=>':codigo', 'valor'=>null));
		}
		if($bi_colunas_layout_imp->getLayout() != "")
		{
			array_push($inputs, array('campo'=>':layout', 'valor'=>$bi_colunas_layout_imp->getLayout()));
		}
		else
		{
			array_push($inputs, array('campo'=>':layout', 'valor'=>null));
		}
		if($bi_colunas_layout_imp->getColuna() != "")
		{
			array_push($inputs, array('campo'=>':coluna', 'valor'=>$bi_colunas_layout_imp->getColuna()));
		}
		else
		{
			array_push($inputs, array('campo'=>':coluna', 'valor'=>null));
		}
		if($bi_colunas_layout_imp->getTipo() != "")
		{
			array_push($inputs, array('campo'=>':tipo', 'valor'=>$bi_colunas_layout_imp->getTipo()));
		}
		else
		{
			array_push($inputs, array('campo'=>':tipo', 'valor'=>null));
		}
		if($bi_colunas_layout_imp->getMascara() != "")
		{
			array_push($inputs, array('campo'=>':mascara', 'valor'=>$bi_colunas_layout_imp->getMascara()));
		}
		else
		{
			array_push($inputs, array('campo'=>':mascara', 'valor'=>null));
		}
		if($bi_colunas_layout_imp->getTamanho() != "")
		{
			array_push($inputs, array('campo'=>':tamanho', 'valor'=>$bi_colunas_layout_imp->getTamanho()));
		}
		else
		{
			array_push($inputs, array('campo'=>':tamanho', 'valor'=>null));
		}
		if($bi_colunas_layout_imp->getQtd_decimais() != "")
		{
			array_push($inputs, array('campo'=>':qtd_decimais', 'valor'=>$bi_colunas_layout_imp->getQtd_decimais()));
		}
		else
		{
			array_push($inputs, array('campo'=>':qtd_decimais', 'valor'=>null));
		}
		try
		{
			$sql = "INSERT INTO bi_colunas_layout_imp (codigo, layout, coluna, tipo, mascara, tamanho, qtd_decimais) VALUES (:codigo, :layout, :coluna, :tipo, :mascara, :tamanho, :qtd_decimais)  ";
			return $this->_funcoes_bd->executarSql($sql,$inputs);
		}
		catch (Exception $e)
		{
			throw new Exception($e->getMessage());
		}
	}
	public function buscarBi_colunas_layout_imp($codigo)
	{
		$inputs = array();
		array_push($inputs, array('campo'=>':codigo', 'valor'=>$codigo));
		$sql = "SELECT * FROM bi_colunas_layout_imp where codigo = :codigo "; 
		$bi_colunas_layout_imp = new Bi_colunas_layout_imp();
		$rows_bi_colunas_layout_imp = $this->_funcoes_bd->sqlArrayInputs($sql, $inputs);
		foreach ($rows_bi_colunas_layout_imp as $row)
		{
			$bi_colunas_layout_imp->setCodigo($row['codigo']);
			if($row['layout'] != "")
			{
				$layoutDAO = new Bi_layout_importacaoDAO();
				$bi_colunas_layout_imp->setLayout($layoutDAO->buscarBi_layout_importacao($row['layout']));
			}
			else
			{
				$bi_colunas_layout_imp->setLayout(new Bi_layout_importacao());
			}
			$bi_colunas_layout_imp->setColuna($row['coluna']);
			$bi_colunas_layout_imp->setTipo($row['tipo']);
			$bi_colunas_layout_imp->setMascara($row['mascara']);
			$bi_colunas_layout_imp->setTamanho($row['tamanho']);
			$bi_colunas_layout_imp->setQtd_decimais($row['qtd_decimais']);
		}
		return $bi_colunas_layout_imp;
	}
	public function buscarFiltro($where)
	{
		$inputs = array();
		$sql = "SELECT * FROM bi_colunas_layout_imp  ".$where;
		$lista_bi_colunas_layout_imp = array();
		$rows_bi_colunas_layout_imp = $this->_funcoes_bd->sql($sql);
		foreach ($rows_bi_colunas_layout_imp as $row)
		{
			$bi_colunas_layout_imp = new Bi_colunas_layout_imp();
			$bi_colunas_layout_imp->setCodigo($row['codigo']);
			if($row['layout'] != "")
			{
				$layoutDAO = new Bi_layout_importacaoDAO();
				$bi_colunas_layout_imp->setLayout($layoutDAO->buscarBi_layout_importacao($row['layout']));
			}
			else
			{
				$bi_colunas_layout_imp->setLayout(new Bi_layout_importacao());
			}
			$bi_colunas_layout_imp->setColuna($row['coluna']);
			$bi_colunas_layout_imp->setTipo($row['tipo']);
			$bi_colunas_layout_imp->setMascara($row['mascara']);
			$bi_colunas_layout_imp->setTamanho($row['tamanho']);
			$bi_colunas_layout_imp->setQtd_decimais($row['qtd_decimais']);
			array_push($lista_bi_colunas_layout_imp, $bi_colunas_layout_imp);
		}
		return $lista_bi_colunas_layout_imp;
	}
	public function atualizar($bi_colunas_layout_imp)
	{
		$inputs = array();
		if($bi_colunas_layout_imp->getCodigo() != "")
		{
			array_push($inputs, array('campo'=>':codigo', 'valor'=>$bi_colunas_layout_imp->getCodigo()));
		}
		else
		{
			array_push($inputs, array('campo'=>':codigo', 'valor'=>null));
		}
		if($bi_colunas_layout_imp->getLayout() != "")
		{
			array_push($inputs, array('campo'=>':layout', 'valor'=>$bi_colunas_layout_imp->getLayout()));
		}
		else
		{
			array_push($inputs, array('campo'=>':layout', 'valor'=>null));
		}
		if($bi_colunas_layout_imp->getColuna() != "")
		{
			array_push($inputs, array('campo'=>':coluna', 'valor'=>$bi_colunas_layout_imp->getColuna()));
		}
		else
		{
			array_push($inputs, array('campo'=>':coluna', 'valor'=>null));
		}
		if($bi_colunas_layout_imp->getTipo() != "")
		{
			array_push($inputs, array('campo'=>':tipo', 'valor'=>$bi_colunas_layout_imp->getTipo()));
		}
		else
		{
			array_push($inputs, array('campo'=>':tipo', 'valor'=>null));
		}
		if($bi_colunas_layout_imp->getMascara() != "")
		{
			array_push($inputs, array('campo'=>':mascara', 'valor'=>$bi_colunas_layout_imp->getMascara()));
		}
		else
		{
			array_push($inputs, array('campo'=>':mascara', 'valor'=>null));
		}
		if($bi_colunas_layout_imp->getTamanho() != "")
		{
			array_push($inputs, array('campo'=>':tamanho', 'valor'=>$bi_colunas_layout_imp->getTamanho()));
		}
		else
		{
			array_push($inputs, array('campo'=>':tamanho', 'valor'=>null));
		}
		if($bi_colunas_layout_imp->getQtd_decimais() != "")
		{
			array_push($inputs, array('campo'=>':qtd_decimais', 'valor'=>$bi_colunas_layout_imp->getQtd_decimais()));
		}
		else
		{
			array_push($inputs, array('campo'=>':qtd_decimais', 'valor'=>null));
		}
		try
		{
			$sql = "UPDATE  bi_colunas_layout_imp  SET codigo = :codigo, layout = :layout, coluna = :coluna, tipo = :tipo, mascara = :mascara, tamanho = :tamanho, qtd_decimais = :qtd_decimais WHERE codigo = :codigo  ";
			return $this->_funcoes_bd->executarSql($sql,$inputs);
		}
		catch (Exception $e)
		{
			throw new Exception($e->getMessage());
		}
	}
	public function excluir($bi_colunas_layout_imp)
	{
		try
		{
			$sql = "DELETE FROM bi_colunas_layout_imp  WHERE codigo =  :codigo ";
			$inputs = array();
			array_push($inputs, array('campo'=>':codigo', 'valor'=>$bi_colunas_layout_imp->getCodigo()));
			return $this->_funcoes_bd->executarSql($sql,$inputs);
		}
		catch (Exception $e)
		{
			throw new Exception($e->getMessage());
		}
	}
	public function listarTodosRegistros()
	{
		$inputs = array();
		$sql = "SELECT * FROM bi_colunas_layout_imp";
		$listaBi_colunas_layout_imp = array();
		$rows_bi_colunas_layout_imp = $this->_funcoes_bd->sql($sql);
		foreach ($rows_bi_colunas_layout_imp as $row)
		{
			$bi_colunas_layout_imp = new Bi_colunas_layout_imp();
			$bi_colunas_layout_imp->setCodigo($row['codigo']);
			if($row['layout'] != "")
			{
				$layoutDAO = new Bi_layout_importacaoDAO();
				$bi_colunas_layout_imp->setLayout($layoutDAO->buscarBi_layout_importacao($row['layout']));
			}
			else
			{
				$bi_colunas_layout_imp->setLayout(new Bi_layout_importacao());
			}
			$bi_colunas_layout_imp->setColuna($row['coluna']);
			$bi_colunas_layout_imp->setTipo($row['tipo']);
			$bi_colunas_layout_imp->setMascara($row['mascara']);
			$bi_colunas_layout_imp->setTamanho($row['tamanho']);
			$bi_colunas_layout_imp->setQtd_decimais($row['qtd_decimais']);
			array_push($listaBi_colunas_layout_imp,$bi_colunas_layout_imp );
		}
		return $listaBi_colunas_layout_imp;
	}
	public function buscarBi_colunas_layout_impPorLayout($layout)
	{
		$inputs = array();
		$sql = "SELECT * FROM bi_colunas_layout_imp where layout = ".$layout."";
		$lista_bi_colunas_layout_imp = array();
		$rows_bi_colunas_layout_imp = $this->_funcoes_bd->sql($sql);
		foreach ($rows_bi_colunas_layout_imp as $row)
		{
			$bi_colunas_layout_imp = new Bi_colunas_layout_imp();
			$bi_colunas_layout_imp->setCodigo($row['codigo']);
			if($row['layout'] != "")
			{
				$layoutDAO = new Bi_layout_importacaoDAO();
				$bi_colunas_layout_imp->setLayout($layoutDAO->buscarBi_layout_importacao($row['layout']));
			}
			else
			{
				$bi_colunas_layout_imp->setLayout(new Bi_layout_importacao());
			}
			$bi_colunas_layout_imp->setColuna($row['coluna']);
			$bi_colunas_layout_imp->setTipo($row['tipo']);
			$bi_colunas_layout_imp->setMascara($row['mascara']);
			$bi_colunas_layout_imp->setTamanho($row['tamanho']);
			$bi_colunas_layout_imp->setQtd_decimais($row['qtd_decimais']);
			array_push($lista_bi_colunas_layout_imp, $bi_colunas_layout_imp);
		}
		return $lista_bi_colunas_layout_imp;
	}
	public function buscarBi_colunas_layout_impPorColuna($coluna)
	{
		$inputs = array();
		$sql = "SELECT * FROM bi_colunas_layout_imp where coluna like '%".$coluna."%'";
		$lista_bi_colunas_layout_imp = array();
		$rows_bi_colunas_layout_imp = $this->_funcoes_bd->sql($sql);
		foreach ($rows_bi_colunas_layout_imp as $row)
		{
			$bi_colunas_layout_imp = new Bi_colunas_layout_imp();
			$bi_colunas_layout_imp->setCodigo($row['codigo']);
			if($row['layout'] != "")
			{
				$layoutDAO = new Bi_layout_importacaoDAO();
				$bi_colunas_layout_imp->setLayout($layoutDAO->buscarBi_layout_importacao($row['layout']));
			}
			else
			{
				$bi_colunas_layout_imp->setLayout(new Bi_layout_importacao());
			}
			$bi_colunas_layout_imp->setColuna($row['coluna']);
			$bi_colunas_layout_imp->setTipo($row['tipo']);
			$bi_colunas_layout_imp->setMascara($row['mascara']);
			$bi_colunas_layout_imp->setTamanho($row['tamanho']);
			$bi_colunas_layout_imp->setQtd_decimais($row['qtd_decimais']);
			array_push($lista_bi_colunas_layout_imp, $bi_colunas_layout_imp);
		}
		return $lista_bi_colunas_layout_imp;
	}
	public function buscarBi_colunas_layout_impPorTipo($tipo)
	{
		$inputs = array();
		$sql = "SELECT * FROM bi_colunas_layout_imp where tipo like '%".$tipo."%'";
		$lista_bi_colunas_layout_imp = array();
		$rows_bi_colunas_layout_imp = $this->_funcoes_bd->sql($sql);
		foreach ($rows_bi_colunas_layout_imp as $row)
		{
			$bi_colunas_layout_imp = new Bi_colunas_layout_imp();
			$bi_colunas_layout_imp->setCodigo($row['codigo']);
			if($row['layout'] != "")
			{
				$layoutDAO = new Bi_layout_importacaoDAO();
				$bi_colunas_layout_imp->setLayout($layoutDAO->buscarBi_layout_importacao($row['layout']));
			}
			else
			{
				$bi_colunas_layout_imp->setLayout(new Bi_layout_importacao());
			}
			$bi_colunas_layout_imp->setColuna($row['coluna']);
			$bi_colunas_layout_imp->setTipo($row['tipo']);
			$bi_colunas_layout_imp->setMascara($row['mascara']);
			$bi_colunas_layout_imp->setTamanho($row['tamanho']);
			$bi_colunas_layout_imp->setQtd_decimais($row['qtd_decimais']);
			array_push($lista_bi_colunas_layout_imp, $bi_colunas_layout_imp);
		}
		return $lista_bi_colunas_layout_imp;
	}
	public function buscarBi_colunas_layout_impPorMascara($mascara)
	{
		$inputs = array();
		$sql = "SELECT * FROM bi_colunas_layout_imp where mascara like '%".$mascara."%'";
		$lista_bi_colunas_layout_imp = array();
		$rows_bi_colunas_layout_imp = $this->_funcoes_bd->sql($sql);
		foreach ($rows_bi_colunas_layout_imp as $row)
		{
			$bi_colunas_layout_imp = new Bi_colunas_layout_imp();
			$bi_colunas_layout_imp->setCodigo($row['codigo']);
			if($row['layout'] != "")
			{
				$layoutDAO = new Bi_layout_importacaoDAO();
				$bi_colunas_layout_imp->setLayout($layoutDAO->buscarBi_layout_importacao($row['layout']));
			}
			else
			{
				$bi_colunas_layout_imp->setLayout(new Bi_layout_importacao());
			}
			$bi_colunas_layout_imp->setColuna($row['coluna']);
			$bi_colunas_layout_imp->setTipo($row['tipo']);
			$bi_colunas_layout_imp->setMascara($row['mascara']);
			$bi_colunas_layout_imp->setTamanho($row['tamanho']);
			$bi_colunas_layout_imp->setQtd_decimais($row['qtd_decimais']);
			array_push($lista_bi_colunas_layout_imp, $bi_colunas_layout_imp);
		}
		return $lista_bi_colunas_layout_imp;
	}
	public function buscarBi_colunas_layout_impPorTamanho($tamanho)
	{
		$inputs = array();
		$sql = "SELECT * FROM bi_colunas_layout_imp where tamanho like '%".$tamanho."%'";
		$lista_bi_colunas_layout_imp = array();
		$rows_bi_colunas_layout_imp = $this->_funcoes_bd->sql($sql);
		foreach ($rows_bi_colunas_layout_imp as $row)
		{
			$bi_colunas_layout_imp = new Bi_colunas_layout_imp();
			$bi_colunas_layout_imp->setCodigo($row['codigo']);
			if($row['layout'] != "")
			{
				$layoutDAO = new Bi_layout_importacaoDAO();
				$bi_colunas_layout_imp->setLayout($layoutDAO->buscarBi_layout_importacao($row['layout']));
			}
			else
			{
				$bi_colunas_layout_imp->setLayout(new Bi_layout_importacao());
			}
			$bi_colunas_layout_imp->setColuna($row['coluna']);
			$bi_colunas_layout_imp->setTipo($row['tipo']);
			$bi_colunas_layout_imp->setMascara($row['mascara']);
			$bi_colunas_layout_imp->setTamanho($row['tamanho']);
			$bi_colunas_layout_imp->setQtd_decimais($row['qtd_decimais']);
			array_push($lista_bi_colunas_layout_imp, $bi_colunas_layout_imp);
		}
		return $lista_bi_colunas_layout_imp;
	}
	public function buscarBi_colunas_layout_impPorQtd_decimais($qtd_decimais)
	{
		$inputs = array();
		$sql = "SELECT * FROM bi_colunas_layout_imp where qtd_decimais like '%".$qtd_decimais."%'";
		$lista_bi_colunas_layout_imp = array();
		$rows_bi_colunas_layout_imp = $this->_funcoes_bd->sql($sql);
		foreach ($rows_bi_colunas_layout_imp as $row)
		{
			$bi_colunas_layout_imp = new Bi_colunas_layout_imp();
			$bi_colunas_layout_imp->setCodigo($row['codigo']);
			if($row['layout'] != "")
			{
				$layoutDAO = new Bi_layout_importacaoDAO();
				$bi_colunas_layout_imp->setLayout($layoutDAO->buscarBi_layout_importacao($row['layout']));
			}
			else
			{
				$bi_colunas_layout_imp->setLayout(new Bi_layout_importacao());
			}
			$bi_colunas_layout_imp->setColuna($row['coluna']);
			$bi_colunas_layout_imp->setTipo($row['tipo']);
			$bi_colunas_layout_imp->setMascara($row['mascara']);
			$bi_colunas_layout_imp->setTamanho($row['tamanho']);
			$bi_colunas_layout_imp->setQtd_decimais($row['qtd_decimais']);
			array_push($lista_bi_colunas_layout_imp, $bi_colunas_layout_imp);
		}
		return $lista_bi_colunas_layout_imp;
	}
}