From 80d95c0c6d9880a07029cc19c34a436fb8e8977b Mon Sep 17 00:00:00 2001 From: Eriksen Costa Paixão Date: Fri, 24 Apr 2009 23:16:32 +0000 Subject: [PATCH] r28@erik: eriksen | 2009-04-24 19:57:24 -0300 #17 by Eriksen: Corrigido bug que não listava os clientes de uma bibliteca enquanto não houvesse uma entrada na tabela de suspensão. --- ieducar/intranet/include/clsBanco.inc.php | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------------------------------------------- ieducar/intranet/include/pmieducar/clsPmieducarCliente.inc.php | 508 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 2 files changed, 352 insertions(+), 312 deletions(-) diff --git a/ieducar/intranet/include/clsBanco.inc.php b/ieducar/intranet/include/clsBanco.inc.php index 9f44d2f..ce0f602 100755 --- a/ieducar/intranet/include/clsBanco.inc.php +++ b/ieducar/intranet/include/clsBanco.inc.php @@ -1,71 +1,91 @@ strBanco = $strDataBase; - } - } +if (!class_exists('clsBancoSql_')) { + require_once 'include/clsBancoPgSql.inc.php'; } -?> + +class clsBanco extends clsBancoSQL_ { + + public $strHost = "localhost"; // Nome ou endereço IP do servidor do banco de dados + public $strBanco = "ieducardb"; // Nome do banco de dados + public $strUsuario = "ieducaruser"; // Usuário devidamente autorizado a acessar o banco + public $strSenha = "ieducar"; // Senha do usuário do banco + + public $bLink_ID = 0; // Identificador da conexão + public $bConsulta_ID = 0; // Identificador do resultado da consulta + public $arrayStrRegistro = array(); // Tupla resultante de uma consulta + public $iLinha = 0; // Ponteiro interno para a tupla atual da consulta + + public $bErro_no = 0; // Se ocorreu erro na consulta, retorna FALSE + public $strErro = ""; // Frase de descrição do erro retornado + public $bDepurar = FALSE; // Ativa ou desativa funções de depuração + + public $bAuto_Limpa = FALSE; // '1' para limpar o resultado assim que chegar ao último registro + + public $strStringSQL = ""; + + var $strType = ""; + var $arrayStrFields = array(); + var $arrayStrFrom = array(); + var $arrayStrWhere = array(); + var $arrayStrOrderBy = array(); + var $arrayStrGroupBy = array(); + var $iLimitInicio; + var $iLimitQtd; + var $arrayStrArquivo = ""; + + + + /** + * Construtor (PHP 4). + */ + public function clsBanco($strDataBase = FALSE) {} + + + + /** + * Retorna a quantidade de registros de uma tabela baseado no objeto que a + * abstrai. Este deve ter um atributo público Object->_tabela. + * + * @param object Objeto que abstrai a tabela + * @param string Nome da coluna para cálculo COUNT() + * @return int Quantidade de registros da tabela + */ + public function doCountFromObj($obj, $column = '*') { + if ($obj->_tabela == NULL) { + return FALSE; + } + + $sql = sprintf('SELECT COUNT(%s) FROM %s', $column, $obj->_tabela); + $this->Consulta($sql); + + return (int)$this->UnicoCampo($sql); + } + +} \ No newline at end of file diff --git a/ieducar/intranet/include/pmieducar/clsPmieducarCliente.inc.php b/ieducar/intranet/include/pmieducar/clsPmieducarCliente.inc.php index ec62854..4aa8eb5 100644 --- a/ieducar/intranet/include/pmieducar/clsPmieducarCliente.inc.php +++ b/ieducar/intranet/include/pmieducar/clsPmieducarCliente.inc.php @@ -1,35 +1,32 @@ _schema = "pmieducar."; $this->_tabela = "{$this->_schema}cliente"; @@ -479,228 +479,249 @@ class clsPmieducarCliente return false; } - /** - * Retorna uma lista filtrados de acordo com os parametros - * - * @return array - */ - function listaCompleta( $int_cod_cliente = null, $int_ref_usuario_exc = null, $int_ref_usuario_cad = null, $int_ref_idpes = null, $int_login = null, $str_senha = null, $date_data_cadastro_ini = null, $date_data_cadastro_fim = null, $date_data_exclusao_ini = null, $date_data_exclusao_fim = null, $int_ativo = 1, $str_nm_cliente = null, $str_suspenso = null, $int_cod_cliente_tipo = null, $int_cod_escola = null, $int_cod_biblioteca = null, $int_cod_instituicao = null ) - { - $tab_adicional = ''; - $condicao = ''; - $camp_adicional = ''; - - // Se suspenso não for nulo, seleciona clientes suspensos - if (!is_null($str_suspenso)) - { - $camp_adicional .= ", pmieducar.cliente_suspensao cs "; - $condicao .= " AND c.cod_cliente = cs.ref_cod_cliente"; - } - else - { - $camp_adicional .= ", pmieducar.cliente_suspensao cs "; - $condicao .= " AND c.cod_cliente <> cs.ref_cod_cliente"; - } - - $sql1 = "SELECT c.cod_cliente, - c.ref_idpes, - c.ref_usuario_cad, - c.login, - p.nome, - ct.nm_tipo, - ct.cod_cliente_tipo, - b.nm_biblioteca, - b.cod_biblioteca, - e.cod_escola as cod_escola, - i.cod_instituicao, - (SELECT 'S'::text - FROM pmieducar.cliente_suspensao cs - WHERE cs.ref_cod_cliente = c.cod_cliente - AND cs.data_liberacao IS NULL) AS id_suspensao - FROM pmieducar.cliente c, - pmieducar.cliente_tipo_cliente ctc, - pmieducar.cliente_tipo ct, - pmieducar.biblioteca b, - pmieducar.escola e, - pmieducar.instituicao i, - cadastro.pessoa p{$camp_adicional} - WHERE c.cod_cliente = ctc.ref_cod_cliente - AND ct.cod_cliente_tipo = ctc.ref_cod_cliente_tipo - AND b.cod_biblioteca = ct.ref_cod_biblioteca - AND e.cod_escola = b.ref_cod_escola - AND i.cod_instituicao = b.ref_cod_instituicao - AND e.ref_cod_instituicao = i.cod_instituicao{$condicao} - AND p.idpes = c.ref_idpes - AND c.ativo = '{$int_ativo}' - AND ctc.ativo = '{$int_ativo}'"; - - $sql2 = "SELECT c.cod_cliente, - c.ref_idpes, - c.ref_usuario_cad, - c.login, - p.nome, - ct.nm_tipo, - ct.cod_cliente_tipo, - b.nm_biblioteca, - b.cod_biblioteca, - null as cod_escola, - i.cod_instituicao, - (SELECT 'S'::text - FROM pmieducar.cliente_suspensao cs - WHERE cs.ref_cod_cliente = c.cod_cliente - AND cs.data_liberacao IS NULL) AS id_suspensao - FROM pmieducar.cliente c, - pmieducar.cliente_tipo_cliente ctc, - pmieducar.cliente_tipo ct, - pmieducar.biblioteca b, - pmieducar.instituicao i, - cadastro.pessoa p{$camp_adicional} - WHERE c.cod_cliente = ctc.ref_cod_cliente - AND ct.cod_cliente_tipo = ctc.ref_cod_cliente_tipo - AND b.cod_biblioteca = ct.ref_cod_biblioteca - AND i.cod_instituicao = b.ref_cod_instituicao - AND b.ref_cod_escola IS NULL - AND ct.ref_cod_biblioteca = b.cod_biblioteca{$condicao} - AND p.idpes = c.ref_idpes - AND c.ativo = '{$int_ativo}' - AND ctc.ativo = '{$int_ativo}'"; - $filtros = ""; - $whereAnd = " AND "; - if( is_numeric( $int_cod_cliente ) ) - { - $filtros .= "{$whereAnd} c.cod_cliente = '{$int_cod_cliente}'"; - $whereAnd = " AND "; - } - if( is_numeric( $int_ref_usuario_exc ) ) - { - $filtros .= "{$whereAnd} c.ref_usuario_exc = '{$int_ref_usuario_exc}'"; - $whereAnd = " AND "; - } - if( is_numeric( $int_ref_usuario_cad ) ) - { - $filtros .= "{$whereAnd} c.ref_usuario_cad = '{$int_ref_usuario_cad}'"; - $whereAnd = " AND "; - } - if( is_numeric( $int_ref_idpes ) ) - { - $filtros .= "{$whereAnd} c.ref_idpes = '{$int_ref_idpes}'"; - $whereAnd = " AND "; - } - if( is_numeric( $int_login ) ) - { - $filtros .= "{$whereAnd} c.login = '{$int_login}'"; - $whereAnd = " AND "; - } - if( is_string( $str_senha ) ) - { - $filtros .= "{$whereAnd} c.senha = '{$str_senha}'"; - $whereAnd = " AND "; - } - if( is_string( $date_data_cadastro_ini ) ) - { - $filtros .= "{$whereAnd} c.data_cadastro >= '{$date_data_cadastro_ini}'"; - $whereAnd = " AND "; - } - if( is_string( $date_data_cadastro_fim ) ) - { - $filtros .= "{$whereAnd} c.data_cadastro <= '{$date_data_cadastro_fim}'"; - $whereAnd = " AND "; - } - if( is_string( $date_data_exclusao_ini ) ) - { - $filtros .= "{$whereAnd} c.data_exclusao >= '{$date_data_exclusao_ini}'"; - $whereAnd = " AND "; - } - if( is_string( $date_data_exclusao_fim ) ) - { - $filtros .= "{$whereAnd} c.data_exclusao <= '{$date_data_exclusao_fim}'"; - $whereAnd = " AND "; - } - if( is_null( $int_ativo ) || $int_ativo ) - { - $filtros .= "{$whereAnd} c.ativo = '1'"; - $whereAnd = " AND "; - } - else - { - $filtros .= "{$whereAnd} c.ativo = '0'"; - $whereAnd = " AND "; - } - if( is_string( $str_nm_cliente ) ) - { - $filtros .= "{$whereAnd} p.nome LIKE '%{$str_nm_cliente}%'"; - $whereAnd = " AND "; - } - if ( is_numeric( $int_cod_cliente_tipo ) ) - { - $filtros .= "{$whereAnd} ct.cod_cliente_tipo = '{$int_cod_cliente_tipo}'"; - $whereAnd = " AND "; - } - if ( is_array( $int_cod_biblioteca ) ) - { - $array_biblioteca = implode(", ", $int_cod_biblioteca); - $filtros .= "{$whereAnd} b.cod_biblioteca IN ({$array_biblioteca})"; - $whereAnd = " AND "; - } - if ( is_numeric( $int_cod_biblioteca ) ) - { - $filtros .= "{$whereAnd} b.cod_biblioteca = '{$int_cod_biblioteca}'"; - $whereAnd = " AND "; - } - if ( is_numeric( $int_cod_escola ) ) - { - $filtros .= "{$whereAnd} e.cod_escola = '{$int_cod_escola}'"; - $whereAnd = " AND "; - } - if ( is_numeric( $int_cod_instituicao ) ) - { - $filtros .= "{$whereAnd} i.cod_instituicao = '{$int_cod_instituicao}'"; - $whereAnd = " AND "; - } - if( is_string( $str_suspenso ) ) { - $filtros .= "{$whereAnd} cs.data_liberacao IS NULL"; - $whereAnd = " AND "; - } - - $db = new clsBanco(); - $resultado = array(); - - $sql1 .= $filtros; /*. $this->getOrderby() . $this->getLimite();*/ - $sql2 .= $filtros; - - if(is_numeric($int_cod_escola)) - { - $this->_total = $db->CampoUnico( "SELECT count(0) FROM ( {$sql1} ) AS SUBQUERY" ); - } - else - { - $this->_total = $db->CampoUnico( "SELECT count(0) FROM ( ".$sql1." UNION ".$sql2." ) AS SUBQUERY" ); - } - - $sql2 .= $this->getOrderby() . $this->getLimite(); - - if ( is_numeric( $int_cod_escola ) ) - $sql = $sql1; - else - $sql = $sql1." UNION ".$sql2; - - $db->Consulta( $sql ); + /** + * Retorna uma lista filtrada de acordo com os argumentos. + * + * @return Array + */ + public function listaCompleta($int_cod_cliente = NULL, $int_ref_usuario_exc = NULL, + $int_ref_usuario_cad = NULL, $int_ref_idpes = NULL, $int_login = NULL, $str_senha = NULL, + $date_data_cadastro_ini = NULL, $date_data_cadastro_fim = NULL, $date_data_exclusao_ini = NULL, + $date_data_exclusao_fim = NULL, $int_ativo = 1, $str_nm_cliente = NULL, $str_suspenso = NULL, + $int_cod_cliente_tipo = NULL, $int_cod_escola = NULL, $int_cod_biblioteca = NULL, + $int_cod_instituicao = NULL) { + + $tab_adicional = ''; + $condicao = ''; + $camp_adicional = ''; + + // Executa filtragem por clientes suspensos somente quando houver registros na tabela + $clienteSuspenso = new clsPmieducarClienteSuspensao(); + $db = new clsBanco(); + + // Se suspenso não for nulo e existirem cliente suspensos, seleciona-os + // quando ainda estiverem no prazo de suspensão + if (!is_null($str_suspenso) && $db->doCountFromObj($clienteSuspenso) > 0) { + $camp_adicional .= ', pmieducar.cliente_suspensao cs '; + $condicao .= ' AND c.cod_cliente = cs.ref_cod_cliente AND + (cs.data_suspensao < current_date - cs.dias)'; + } + else { + $camp_adicional .= ', pmieducar.cliente_suspensao cs '; + $condicao .= ' AND (c.cod_cliente <> cs.ref_cod_cliente OR + (cs.data_suspensao > current_date - cs.dias))'; + } + + $sql1 = " + SELECT + c.cod_cliente, + c.ref_idpes, + c.ref_usuario_cad, + c.login, + p.nome, + ct.nm_tipo, + ct.cod_cliente_tipo, + b.nm_biblioteca, + b.cod_biblioteca, + e.cod_escola as cod_escola, + i.cod_instituicao, + (SELECT 'S'::text + FROM pmieducar.cliente_suspensao cs + WHERE cs.ref_cod_cliente = c.cod_cliente + AND cs.data_liberacao IS NULL) AS id_suspensao + FROM + pmieducar.cliente c, + pmieducar.cliente_tipo_cliente ctc, + pmieducar.cliente_tipo ct, + pmieducar.biblioteca b, + pmieducar.escola e, + pmieducar.instituicao i, + cadastro.pessoa p{$camp_adicional} + WHERE + c.cod_cliente = ctc.ref_cod_cliente + AND ct.cod_cliente_tipo = ctc.ref_cod_cliente_tipo + AND b.cod_biblioteca = ct.ref_cod_biblioteca + AND e.cod_escola = b.ref_cod_escola + AND i.cod_instituicao = b.ref_cod_instituicao + AND e.ref_cod_instituicao = i.cod_instituicao{$condicao} + AND p.idpes = c.ref_idpes + AND c.ativo = '{$int_ativo}' + AND ctc.ativo = '{$int_ativo}'"; + + $sql2 = " + SELECT + c.cod_cliente, + c.ref_idpes, + c.ref_usuario_cad, + c.login, + p.nome, + ct.nm_tipo, + ct.cod_cliente_tipo, + b.nm_biblioteca, + b.cod_biblioteca, + null as cod_escola, + i.cod_instituicao, + (SELECT 'S'::text + FROM pmieducar.cliente_suspensao cs + WHERE cs.ref_cod_cliente = c.cod_cliente + AND cs.data_liberacao IS NULL) AS id_suspensao + FROM + pmieducar.cliente c, + pmieducar.cliente_tipo_cliente ctc, + pmieducar.cliente_tipo ct, + pmieducar.biblioteca b, + pmieducar.instituicao i, + cadastro.pessoa p{$camp_adicional} + WHERE c.cod_cliente = ctc.ref_cod_cliente + AND ct.cod_cliente_tipo = ctc.ref_cod_cliente_tipo + AND b.cod_biblioteca = ct.ref_cod_biblioteca + AND i.cod_instituicao = b.ref_cod_instituicao + AND b.ref_cod_escola IS NULL + AND ct.ref_cod_biblioteca = b.cod_biblioteca{$condicao} + AND p.idpes = c.ref_idpes + AND c.ativo = '{$int_ativo}' + AND ctc.ativo = '{$int_ativo}'"; + + $filtros = ''; + $whereAnd = ' AND '; + + if (is_numeric($int_cod_cliente)) { + $filtros .= "{$whereAnd} c.cod_cliente = '{$int_cod_cliente}'"; + $whereAnd = " AND "; + } + + if (is_numeric($int_ref_usuario_exc)) { + $filtros .= "{$whereAnd} c.ref_usuario_exc = '{$int_ref_usuario_exc}'"; + $whereAnd = " AND "; + } + + if (is_numeric($int_ref_usuario_cad)) { + $filtros .= "{$whereAnd} c.ref_usuario_cad = '{$int_ref_usuario_cad}'"; + $whereAnd = " AND "; + } + + if (is_numeric($int_ref_idpes)) { + $filtros .= "{$whereAnd} c.ref_idpes = '{$int_ref_idpes}'"; + $whereAnd = " AND "; + } + + if (is_numeric($int_login)) { + $filtros .= "{$whereAnd} c.login = '{$int_login}'"; + $whereAnd = " AND "; + } + + if (is_string($str_senha)) { + $filtros .= "{$whereAnd} c.senha = '{$str_senha}'"; + $whereAnd = " AND "; + } + + if (is_string($date_data_cadastro_ini)) { + $filtros .= "{$whereAnd} c.data_cadastro >= '{$date_data_cadastro_ini}'"; + $whereAnd = " AND "; + } + + if (is_string($date_data_cadastro_fim)) { + $filtros .= "{$whereAnd} c.data_cadastro <= '{$date_data_cadastro_fim}'"; + $whereAnd = " AND "; + } + + if (is_string($date_data_exclusao_ini)) { + $filtros .= "{$whereAnd} c.data_exclusao >= '{$date_data_exclusao_ini}'"; + $whereAnd = " AND "; + } + + if (is_string($date_data_exclusao_fim)) { + $filtros .= "{$whereAnd} c.data_exclusao <= '{$date_data_exclusao_fim}'"; + $whereAnd = " AND "; + } + + if (is_null($int_ativo) || $int_ativo) { + $filtros .= "{$whereAnd} c.ativo = '1'"; + $whereAnd = " AND "; + } + else { + $filtros .= "{$whereAnd} c.ativo = '0'"; + $whereAnd = " AND "; + } + + if (is_string($str_nm_cliente)) { + $filtros .= "{$whereAnd} p.nome LIKE '%{$str_nm_cliente}%'"; + $whereAnd = " AND "; + } + + if (is_numeric($int_cod_cliente_tipo)) { + $filtros .= "{$whereAnd} ct.cod_cliente_tipo = '{$int_cod_cliente_tipo}'"; + $whereAnd = " AND "; + } + + if (is_array($int_cod_biblioteca)) { + $array_biblioteca = implode(", ", $int_cod_biblioteca); + $filtros .= "{$whereAnd} b.cod_biblioteca IN ({$array_biblioteca})"; + $whereAnd = " AND "; + } + + if (is_numeric($int_cod_biblioteca)) { + $filtros .= "{$whereAnd} b.cod_biblioteca = '{$int_cod_biblioteca}'"; + $whereAnd = " AND "; + } + + if (is_numeric($int_cod_escola)) { + $filtros .= "{$whereAnd} e.cod_escola = '{$int_cod_escola}'"; + $whereAnd = " AND "; + } + + if (is_numeric($int_cod_instituicao)) { + $filtros .= "{$whereAnd} i.cod_instituicao = '{$int_cod_instituicao}'"; + $whereAnd = " AND "; + } + + if (is_string($str_suspenso)) { + $filtros .= "{$whereAnd} cs.data_liberacao IS NULL"; + $whereAnd = " AND "; + } + + $db = new clsBanco(); + $resultado = array(); + + $sql1 .= $filtros; + $sql2 .= $filtros; + + if (is_numeric($int_cod_escola)) { + $this->_total = $db->CampoUnico("SELECT count(0) FROM ({$sql1}) AS SUBQUERY"); + } + else { + $this->_total = $db->CampoUnico("SELECT count(0) FROM (" . $sql1 . " UNION " . $sql2 . ") AS SUBQUERY"); + } + + // Aplica ordenação e limite + $sql2 .= $this->getOrderby() . $this->getLimite(); + + if (is_numeric($int_cod_escola)) { + $sql = $sql1; + } + else { + $sql = $sql1 . ' UNION ' . $sql2; + } + + $db->Consulta($sql); + + while ($db->ProximoRegistro()) { + $tupla = $db->Tupla(); + + $tupla["_total"] = $this->_total; + $resultado[] = $tupla; + } + + if (count($resultado) > 0) { + return $resultado; + } + + return FALSE; + } - while ( $db->ProximoRegistro() ) - { - $tupla = $db->Tupla(); - $tupla["_total"] = $this->_total; - $resultado[] = $tupla; - } - if( count( $resultado ) ) - { - return $resultado; - } - return false; - } /** * Retorna uma lista filtrados de acordo com os parametros @@ -991,5 +1012,4 @@ class clsPmieducarCliente return false; } -} -?> +} \ No newline at end of file -- libgit2 0.21.2