1
Gentlemen, I have the following PHP code:
# Fechamento Chamada
public function fechamentoChamada($data_inicial = null, $data_final = null, $idCliente = null, $idFuncionario = null, $view = null, $tipo_chamada = null){
////// ?view=&tipo_chamada=0&idCliente=&idFuncionario=&data_inicial=2015-06-01&data_final=2015-06-30
if(!empty($tipo_chamada)) $sqlConjunto = "AND c.tipo_chamada = '".$tipo_chamada."'"; else $sqlConjunto = "";
if(!empty($tipo_chamada)) $sqlConjuntoView = "AND tipo_chamada = '".$tipo_chamada."'"; else $sqlConjuntoView = "";
# seleciona somente com a data
if(empty($idFuncionario) and empty($view) and empty($idCliente)){
$sql = "SELECT c.*, cl.*, cl.razaosocial as nomeCliente, count(c.idCliente) as totalOs, sum(c.valor_empresa) as valorEmpresa, ce.idCedente, ce.razaosocial as nomeFaturado, p.idParametro, p.parametro as notaFiscal FROM chamada AS c, cliente as cl, cedente as ce, parametro as p WHERE c.data BETWEEN '".$data_inicial."' AND '".$data_final."' AND cl.idCliente = c.idCliente AND cl.codCedente = ce.idCedente AND cl.nota = p.idParametro {$sqlConjunto} GROUP BY c.idCliente";
}
# seleciona com idFuncionario
if(is_numeric($idFuncionario) && $view=="" && $idCliente==""){
$sql = "SELECT c.*, cl.*, cl.razaosocial as nomeCliente, count(c.idCliente) as totalOs, sum(c.valor_empresa) as valorEmpresa, ce.idCedente, ce.razaosocial as nomeFaturado, p.idParametro, p.parametro as notaFiscal FROM chamada AS c, cliente as cl, cedente as ce, parametro as p WHERE c.data BETWEEN '".$data_inicial."' AND '".$data_final."' AND c.idFuncionario = {$idFuncionario} AND cl.idCliente = c.idCliente AND cl.codCedente = ce.idCedente AND cl.nota = p.idParametro {$sqlConjunto} GROUP BY c.idCliente";
}
# seleciona com idFuncionario & idCliente
if(isset($idFuncionario) && $view=="" && isset($idCliente)){
$sql = "SELECT c.*, cl.*, cl.razaosocial as nomeCliente, count(c.idCliente) as totalOs, sum(c.valor_empresa) as valorEmpresa, ce.idCedente, ce.razaosocial as nomeFaturado, p.idParametro, p.parametro as notaFiscal FROM chamada AS c, cliente as cl, cedente as ce, parametro as p WHERE c.data BETWEEN '".$data_inicial."' AND '".$data_final."' AND c.idFuncionario = {$idFuncionario} AND cl.idCliente = {$idCliente} AND cl.codCedente = ce.idCedente AND cl.nota = p.idParametro {$sqlConjunto} GROUP BY c.idCliente";
}
# seleciona com idCliente, idFuncionario e visualiza todos (relatorio interno)
if(isset($idFuncionario) && $view=="todos" && isset($idCliente)){
$sql = "SELECT c.*, cl.*, cl.razaosocial as nomeCliente, count(c.idCliente) as totalOs, sum(c.valor_empresa) as valorEmpresa, ce.idCedente, ce.razaosocial as nomeFaturado, p.idParametro, p.parametro as notaFiscal FROM chamada AS c, cliente as cl, cedente as ce, parametro as p WHERE c.data BETWEEN '".$data_inicial."' AND '".$data_final."' AND c.idFuncionario = {$idFuncionario} AND cl.idCliente = {$idCliente} AND cl.codCedente = ce.idCedente AND cl.nota = p.idParametro {$sqlConjunto} GROUP BY c.idCliente";
}
# seleciona com idCliente e idFuncionario vazio
if(is_numeric($idCliente) and empty($idFuncionario)){
$sql = "SELECT c.*, cl.*, cl.razaosocial as nomeCliente, count(c.idCliente) as totalOs, sum(c.valor_empresa) as valorEmpresa, ce.idCedente, ce.razaosocial as nomeFaturado, p.idParametro, p.parametro as notaFiscal FROM chamada AS c, cliente as cl, cedente as ce, parametro as p WHERE c.data BETWEEN '".$data_inicial."' AND '".$data_final."' AND cl.idCliente = '".$idCliente."' AND cl.codCedente = ce.idCedente AND cl.nota = p.idParametro GROUP BY c.idCliente";
}
// echo $sql;
$consulta = $this->db->query($sql)->result();
foreach($consulta as &$valor){
if(empty($idFuncionario) and empty($view) and empty($idCliente)){
$sql = "SELECT *, sum(valor_empresa) as valorEmpresa, count(idCliente) as totalOsBase FROM chamada WHERE data BETWEEN '".$data_inicial."' AND '".$data_final."' {$sqlConjuntoView}";
$valor->totalFechamento = $this->db->query($sql)->row();
}
if(is_numeric($idCliente) and empty($idFuncionario)){
$sql = "SELECT * FROM chamada WHERE data BETWEEN '".$data_inicial."' AND '".$data_final."' AND idCliente = '".$idCliente."' {$sqlConjuntoView}";
$valor->totalFechamento = $this->db->query($sql)->result();
$sql_totais = "SELECT *, sum(valor_empresa) as valorEmpresa, count(idCliente) as totalOsBase FROM chamada WHERE data BETWEEN '".$data_inicial."' AND '".$data_final."' AND idCliente = '".$idCliente."' {$sqlConjuntoView} ";
$valor->totalFechamentoTotais = $this->db->query($sql_totais)->row();
}
if(empty($idCliente) and is_numeric($idFuncionario)){
$sql = "SELECT * FROM chamada WHERE data BETWEEN '".$data_inicial."' AND '".$data_final."' AND idFuncionario = '".$idFuncionario."' {$sqlConjuntoView}";
$valor->totalFechamento = $this->db->query($sql)->result();
$sql_totais = "SELECT *, sum(valor_empresa) as valorEmpresa, count(idFuncionario) as totalOsBase FROM chamada WHERE data BETWEEN '".$data_inicial."' AND '".$data_final."' AND idFuncionario = '".$idFuncionario."' {$sqlConjuntoView}";
$valor->totalFechamentoTotais = $this->db->query($sql_totais)->row();
}
if(is_numeric($idCliente) and is_numeric($idFuncionario)){
$sql = "SELECT * FROM chamada WHERE data BETWEEN '".$data_inicial."' AND '".$data_final."' AND idCliente = {$idCliente} AND idFuncionario = '".$idFuncionario."' {$sqlConjuntoView}";
$valor->totalFechamento = $this->db->query($sql)->result();
$sql_totais = "SELECT *, sum(valor_empresa) as valorEmpresa, count(idFuncionario) as totalOsBase FROM chamada WHERE data BETWEEN '".$data_inicial."' AND '".$data_final."' AND idFuncionario = '".$idFuncionario."' AND idCliente = {$idCliente} {$sqlConjuntoView}";
$valor->totalFechamentoTotais = $this->db->query($sql_totais)->row();
}
}
return $consulta;
}
I would like to customize, not to stay this lot of if() Else () and not these various SQL, I know that can be done in a way in SQL to make a query only, however, I do not know... Can someone help me?