<?php

/* * *****************************************************************************
 *                               FI109 - INICIO
 * **************************************************************************** */

// Função Principal
function FI109() {



    include_once "funcoes.inc";
    include_once '../auxiliar/strings.inc';
    include_once "../bd/bd_senhas.inc";
    include_once '../auxiliar/datas.inc';
    include '../idiomas/config.inc';

    //Le todos os filtros
    $razao = lecookie("fi109_razao");
    $codcli = lecookie("fi109_codcli");
    $numcli = lecookie("fi109_numcli");
    $dtvenctoini = lecookie("fi109_dtvenctoini");
    $dtvenctofin = lecookie("fi109_dtvenctofin");

    $p_codvnd = GetCodVnd();

    //Monta o Select

    $campos = " SELECT DISTINCT  c.razao, c.numcli, c.codcli "; //essa separao dos campos do select e do resto eh pra reaproveitar o from e o where do select no detalhe

    $sql = "
        FROM CLIENTES C
        JOIN FINAN F ON F.CODCLI = C.CODCLI
        JOIN PARCELAS P ON P.CODFIN = F.CODFIN
        join tipospag t on p.codtipo = t.codtipo
        WHERE ((p.pago = 'N') and (p.dtpagto is null)) and (c.codvnd = $p_codvnd) 
        AND COALESCE(P.CANCELADO,0) = 0";
    

    //trata o filtro de data
    if ($dtvenctoini) {
        $dtvenctoini = ConverteDtUSABR($dtvenctoini);
    } else {
        $dtvenctoini = '01/01/1000';
    }

    if ($dtvenctofin) {
        $dtvenctofin = ConverteDtUSABR($dtvenctofin);
    } else {
        $dtvenctofin = '01/01/5000';
    }

    //passa os filtros do relatorio
    if ($codcli) {
        $filtrosEspecificos .= " and c.codcli = $codcli ";
    }
    // if ($razao) {
    //     $filtrosEspecificos .= " and upper(c.razao) like '%" . strtoupper($razao) . "%' ";
    // }
    // if ($numcli) {
    //     $filtrosEspecificos .= " and c.numcli containing ('$numcli') ";
    // }
    if ($dtvenctoini) {
        $sql .= " and (p.dtvencto >= '$dtvenctoini' ) ";
    }
    if ($dtvenctofin) {
        $sql .= " and (p.dtvencto <= '$dtvenctofin' ) ";
    }



    $selectFinal = $campos . $sql . $filtrosEspecificos . "order by c.razao ";
// var_dump($selectFinal); die();
    //Desenha o Relatrio
    // Traz os campos de fato
    $totalGeral = 0;
    if (cria_matriz_campos_pelo_sql($selectFinal, $itens)) {
        for ($i = 0; $i <= (sizeof($itens) - 1); $i++) {
            echo("    <section class='invoice'>
            <!-- title row -->
            <div class='row'>
              <div class='col-xs-12'>
                <h2 class='page-header'>
                  <i ></i> ". $itens[$i][numcli] . " - " . $itens[$i][razao] ."
                </h2>
              </div>
              <!-- /.col -->
            </div>
            
        
            <!-- Table row -->
            <div class='row'>
              <div class='col-xs-12 table-responsive'>");

            $totalcli = BuscaFI109_det($itens[$i][codcli], $sql);
            $totalGeral += $totalcli;
        echo("
            </div>
            <!-- /.col -->
            
        </div>
        <!-- /.row -->
        
            <div class='pull-right'>
                <b>$r_FI109_total" . number_format($totalcli, 2, ',', '.') . "
            </div>
            
        </section>
        </div>");

            
            
        }
    }
    echo "
        <div class='container'>
        $r_FI109_total_geral <b>" . number_format($totalGeral, 2, ',', '.') ."</b>
        </div>";
}


function getOrcamentosConvertidos($dataIni, $dataFim) {
    
    include_once "funcoes.inc";
    include_once '../auxiliar/strings.inc';
    include_once '../auxiliar/datas.inc';
    include_once "../bd/bd_senhas.inc";



    $select = "
    SELECT
        orc.os as orcamento,
        orc.dataos dataOrcamento,
        os.os osVenda,
        os.dataos dataVenda,
        COALESCE(nf.numnota, '--') as nota,
        l.loja as loja,
        c.razao as cliente,
        ts.tiposaida as tipoSaida,
        v.vendedor as vendedor,
        t.transportadora as transportadora,
        os.total as total
        

    FROM os orc
        JOIN os os ON os.orccodos = orc.codos AND os.tipo = 2
        JOIN lojas l ON l.codloja = orc.codloja
        JOIN clientes c ON c.codcli = orc.codcli
        JOIN tiposaidas ts ON ts.codtiposai = orc.codtiposai
        JOIN vendedores v ON v.codvnd = orc.codvnd
        JOIN transportadora t ON t.codtra = orc.codtra
        LEFT JOIN NOTAFISCAL nf ON nf.codos = os.codos

    WHERE 
        orc.tipo = 1 
        AND orc.dataos >= '" . ConverteDtUSABR($dataIni) . "'
        AND orc.dataos <= '" . ConverteDtUSABR($dataFim) . "'
    ";

    $p_codusu = lecookie("codusu");
    if(isGerente($p_codusu)) {
        $condicaoCodVnd = " and (v.codvnd in (select CODVND from senhas Where codgerente = $p_codusu) ) ";
    } else {
        $p_codvnd = GetCodVnd();
        $condicaoCodVnd = " and (v.codvnd = $p_codvnd) ";
    }

    $select .= $condicaoCodVnd;

    
    $result = qry($con, $select, $a_campos);
    
    if (cria_matriz_campos($result, $a_campos, $r)) {
        if(is_null($r))
            return array();

        return $r;
    }
    return array();

}



function BuscaFI109_det($codcli, $sql) {



    include_once "funcoes.inc";
    include_once '../auxiliar/strings.inc';
    include_once "../bd/bd_senhas.inc";
    include '../idiomas/config.inc';

    //Le  os filtros com excessao do cliente que ele vai receber apenas um
    $dtvenctoini = lecookie("fi109_dtvenctoini");
    $dtvenctofin = lecookie("fi109_dtvenctofin");

    $p_codvnd = GetCodVnd();

    //Monta o Select
    $campos = " SELECT DISTINCT  
        f.numlanc as OS
        , c.numcli
        , c.fone1
        , c.contato1
        , c.codcli
        , f.numlanc
        , (extract(day from p.dtvencto) || '/' || extract(month from p.dtvencto) || '/' || extract(year from p.dtvencto)) as dtvencto
        , p.valor
        , p.valorpg
        , p.codtipo
        , p.numdoc
        , p.numpar
        , f.codaux
        , t.descricao
        , p.obs
        , c.razao
        , p.pago
        , f.nf as numnota ";

    $selectFinal = $campos . $sql . " and (c.codvnd = " . $p_codvnd . ") and  c.codcli = " . $codcli;

    $selectFinal .= " order by c.razao, f.numlanc ";


//echo $selectFinal;
    //Desenha o Relatrio
    //Cabealho
    echo "<table class='table table-striped'>
        <thead>
            <tr class='Titulos'>
                <td>$r_FI109_tipo_pagto</td>
                <td>$r_FI109_data_vencto</td>
                <td>$r_FI109_n_da_venda</td>
                <td>$r_FI109_nota_fiscal</td>
                <td>$r_FI109_valor</td>
                <td>$r_FI109_obs</td>
            </tr>
        </thead>";

    $totalCli = 0;
    if (cria_matriz_campos_pelo_sql($selectFinal, $itens)) {
        for ($i = 0; $i <= (sizeof($itens) - 1); $i++) {

            MostraFI109_det($itens[$i][descricao], $itens[$i][dtvencto], $itens[$i][os], $itens[$i][nf], $itens[$i][valorpg], $itens[$i][obs]);
            $totalCli = $totalCli + $itens[$i][valorpg];
        }
    }



    echo "</table> ";

    return $totalCli;
}

function MostraFI109_det($descricao, $dtvencto, $os, $nf, $valorpg, $obs) {

    echo "<tr>
            <td>$descricao</td>
            <td>$dtvencto</td>
            <td>$os</td>
            <td>$nf</td>
            <td>R$" . number_format($valorpg, 2, ',', '.') . "</td>
            <td>$obs</td>
          </tr>";
}

/* * *****************************************************************************
 *                               FI109 - FIM
 * **************************************************************************** */



/* * *****************************************************************************
 *                               MV142 - INICIO
 * **************************************************************************** */

function MV142() {
    
    include_once '../auxiliar/parametros.inc';


    include_once "funcoes.inc";
    include_once '../auxiliar/strings.inc';
    include_once "../bd/bd_senhas.inc";
    include_once '../auxiliar/datas.inc';
    include '../idiomas/config.inc';


    //Le todos os filtros
    $os = lecookie("mv142_os");
    $dadosAdicionais = lecookie("mv142_dadosAdicionais");
    $codloja = lecookie("mv142_codloja");
    $codcli = lecookie("mv142_codcli");
    $codstatusos = lecookie("mv142_codstatusos");
    $codstatuscli = lecookie("mv142_codstatuscli");
    $tipoData = lecookie("mv142_tipoData"); // dataos | dataosfin
    $statusVenda = lecookie("mv142_statusVenda"); // dataos | dataosfin
    $dtosini = lecookie("mv142_dtosini");
    $dtosfin = lecookie("mv142_dtosfin");


    
    $p_codusu = lecookie("codusu");
    if(isGerente($p_codusu)) {
        $condicaoCodVnd = " and (o.codvnd in (select CODVND from senhas Where codgerente = $p_codusu) ) ";
    } else {
        $p_codvnd = GetCodVnd();
        $condicaoCodVnd = " and ( v.codvnd = $p_codvnd) ";
    }

    //Monta o Select

    $sql = "

        SELECT DISTINCT 
            O.CODOS
            , O.OS
            , ((extract (day from O.DATAOS)) || '/' || (extract (month from O.DATAOS)) || '/' || (extract (year from O.DATAOS)) ) as DATAOS
            , ((extract (day from O.DATAOSFIN)) || '/' || (extract (month from O.DATAOSFIN)) || '/' || (extract (year from O.DATAOSFIN)) ) as DATAOSFIN
            , C.RAZAO
            , cast(substring(c.fone1 from 1 for 14) as varchar(14) ) as FONE1
            , C.NUMCLI
            , V.VENDEDOR as VENDEDOR
            , S.DESCRICAO AS STATUS
            , F.DESCRICAO AS PRAZOPAGTO
            , O.dadosAdicionais 
            , O.TOTAL AS TOTAL
        FROM OS O
            LEFT JOIN OSITENSPRO OI ON OI.CODOS = O.CODOS
            JOIN CLIENTES C ON C.CODCLI = O.CODCLI
            JOIN TIPOSAIDAS T ON T.CODTIPOSAI = O.CODTIPOSAI
            LEFT JOIN VENDEDORES V ON V.CODVND = O.CODVND
            LEFT JOIN STATUS S ON S.CODSTATUS = O.CODSTATUS
            LEFT JOIN FORMAS F ON F.CODFORMA = O.CODFORMA
        where (O.TIPO = 2) AND (T.ISFATURAMENTO = 1) $condicaoCodVnd AND coalesce(O.TOTAL, 0) <> 0 AND coalesce(o.dadosAdicionais, '') like '%$dadosAdicionais%'";


    //trata o filtro de data
    if ($dtosini) {
        $dtosini = ConverteDtUSABR($dtosini);
    } else {
        $dtosini = '01/01/1000';
    }

    if ($dtosfin) {
        $dtosfin = ConverteDtUSABR($dtosfin);
    } else {
        $dtosfin = '01/01/5000';
    }


    //passa os filtros do relatorio
    if ($os) {
        $sql .= " and ( o.os containing ('$os') ) ";
    }
    if ($codloja) {
        $sql .= " and ( o.codloja = $codloja)";
    }
    if ($codcli) {
        $sql .= " and ( c.codcli = $codcli)  ";
    }
    if ($codstatusos) {
        $sql .= " and ( s.codstatus = $codstatusos) ";
    }
    if ($codstatuscli) {
        $sql .= " and ( c.codclistatus = $codstatuscli) ";
    }

    $campoData = ($tipoData == 'dataos') ? 'o.dataos' : 'o.dataosfin';
    if ($dtosini) {
        $sql .= " and ($campoData >= '$dtosini' ) ";
    }
    if ($dtosfin) {
        $sql .= " and ($campoData <= '$dtosfin' ) ";
    }


    if ($statusVenda == 'aberta') {
        $sql .= " AND o.codstatus = $p_codstatus";
    } else if ($statusVenda == 'finalizada') {
        $sql .= " AND o.codstatus = $p_codstatusfin";
    }


    $final = " GROUP BY O.CODOS, O.OS, O.DATAOS, O.DATAOSFIN, C.RAZAO, c.FONE1, C.NUMCLI, 8, 9, 10, 11, 12
    
    order by o.os, c.razao";
    
    $sql .= $final;
    // var_dump($sql);die();

    // echo $sql;
    echo("<p> <h2 class='page-header'>$r_MV142_pedidos_de_venda</h2></p>");

    echo "
    <div class='box-body table-responsive no-padding'>
    <table class='table table-striped'>
            <thead>
            <tr class='titulos'>
                <td>$r_MV142_venda</td>
                <td>$r_MV142_data</td>
                <td>$r_MV142_datafin</td>
                <td>$r_MV142_cliente</td>
                " . ($p_mostra_telefone_rel_mv142==1 ? "<td>$r_MV142_telefone</td>" : "") . "
                <td>$r_MV142_vendedor</td>
                <td>$r_MV142_dados_adic</td>
                <td>$r_MV142_prazo_pagto</td>
                <td>$r_MV142_status</td>
                <td>$r_MV142_total</td>
            </tr>
            </thead>";

    $totalGeral = 0;
    $entregueGeral = 0;
    $saldoGeral = 0;

    //Desenha o Relat�rio
    // Traz os campos de fato
    if (cria_matriz_campos_pelo_sql($sql, $itens)) {
        for ($i = 0; $i <= (sizeof($itens) - 1); $i++) {

            MostraMV142($itens[$i][os], $itens[$i][dataos], $itens[$i][dataosfin], $itens[$i][numcli], $itens[$i][razao], $itens[$i][fone1], $itens[$i][vendedor], $itens[$i][prazopagto],
                    $itens[$i][status], $itens[$i][total], $itens[$i][totalentregue], $itens[$i][totalsaldo], $itens[$i][dadosadicionais]);

            $totalGeral += $itens[$i][total];
            $entregueGeral += $itens[$i][totalentregue];
            $saldoGeral += $itens[$i][totalsaldo];

        }
    }

    echo " 
        <tfoot>
            <tr>
                <td colspan=" . ($p_mostra_telefone_rel_mv142==1 ? "6" : "5") . "></td>
                <td><b>$r_MV142_totais_gerais</b></td>
                <td><b>R$" . number_format($totalGeral, 2, ',', '.') . "</b></td>
            </tr>
        </tfoot>
        </table>
    </div>";
}

function MostraMV142($os, $dataos, $dataosfin, $numcli, $razao, $fone1, $vendedor, $prazopagto, $status, $total, $totalentregue, $totalsaldo, $dadosAdicionais) {
    include '../auxiliar/parametros.inc';
    echo "
        
            <tr>
                <td>$os</td>
                <td>$dataos</td>
                <td>$dataosfin</td>
                <td>$numcli - $razao</td>
                " . ($p_mostra_telefone_rel_mv142==1 ? "<td>$fone1</td>" : "") . "
                <td>$vendedor</td>
                <td>$dadosAdicionais</td>
                <td>$prazopagto</td>
                <td>$status</td>
                <td>R$" . number_format($total, 2, ',', '.') . "</td>
            </tr>";
}

/* * *****************************************************************************
 *                               MV142 - FIM
 * **************************************************************************** */


/* * *****************************************************************************
 *                               MV143 - INICIO
 * **************************************************************************** */

function MV143() {
    
    include_once '../auxiliar/parametros.inc';


    include_once "funcoes.inc";
    include_once '../auxiliar/strings.inc';
    include_once "../bd/bd_senhas.inc";
    include_once '../auxiliar/datas.inc';
    include '../idiomas/config.inc';


    //Le todos os filtros
    $os = lecookie("mv143_os");
    $dadosAdicionais = lecookie("mv143_dadosAdicionais");
    $codloja = lecookie("mv143_codloja");
    $codcli = lecookie("mv143_codcli");
    $codstatusos = lecookie("mv143_codstatusos");
    $codstatuscli = lecookie("mv143_codstatuscli");
    $tipoData = lecookie("mv143_tipoData"); // dataos | dataosfin
    $statusVenda = lecookie("mv143_statusVenda"); // dataos | dataosfin
    $dtosini = lecookie("mv143_dtosini");
    $dtosfin = lecookie("mv143_dtosfin");


    
    $p_codusu = lecookie("codusu");
    if(isGerente($p_codusu)) {
        $condicaoCodVnd = " and (o.codvnd in (select CODVND from senhas Where codgerente = $p_codusu) ) ";
    } else {
        $p_codvnd = GetCodVnd();
        $condicaoCodVnd = " and ( v.codvnd = $p_codvnd) ";
    }

    //Monta o Select

    $sql = "

        SELECT DISTINCT 
            O.CODOS
            , O.OS
            , ((extract (day from O.DATAOS)) || '/' || (extract (month from O.DATAOS)) || '/' || (extract (year from O.DATAOS)) ) as DATAOS
            , ((extract (day from O.DATAOSFIN)) || '/' || (extract (month from O.DATAOSFIN)) || '/' || (extract (year from O.DATAOSFIN)) ) as DATAOSFIN
            , C.RAZAO
            , cast(substring(c.fone1 from 1 for 14) as varchar(14) ) as FONE1
            , C.NUMCLI
            , V.VENDEDOR as VENDEDOR
            , S.DESCRICAO AS STATUS
            , F.DESCRICAO AS PRAZOPAGTO
            , O.dadosAdicionais 
            , O.TOTAL AS TOTAL
        FROM OS O
            LEFT JOIN OSITENSPRO OI ON OI.CODOS = O.CODOS
            JOIN CLIENTES C ON C.CODCLI = O.CODCLI
            JOIN TIPOSAIDAS T ON T.CODTIPOSAI = O.CODTIPOSAI
            LEFT JOIN VENDEDORES V ON V.CODVND = O.CODVND
            LEFT JOIN STATUS S ON S.CODSTATUS = O.CODSTATUS
            LEFT JOIN FORMAS F ON F.CODFORMA = O.CODFORMA
        where (O.TIPO = 4) AND (T.ISFATURAMENTO = 1) $condicaoCodVnd AND coalesce(O.TOTAL, 0) <> 0 AND coalesce(o.dadosAdicionais, '') like '%$dadosAdicionais%'";


    //trata o filtro de data
    if ($dtosini) {
        $dtosini = ConverteDtUSABR($dtosini);
    } else {
        $dtosini = '01/01/1000';
    }

    if ($dtosfin) {
        $dtosfin = ConverteDtUSABR($dtosfin);
    } else {
        $dtosfin = '01/01/5000';
    }


    //passa os filtros do relatorio
    if ($os) {
        $sql .= " and ( o.os containing ('$os') ) ";
    }
    if ($codloja) {
        $sql .= " and ( o.codloja = $codloja)";
    }
    if ($codcli) {
        $sql .= " and ( c.codcli = $codcli)  ";
    }
    if ($codstatusos) {
        $sql .= " and ( s.codstatus = $codstatusos) ";
    }
    if ($codstatuscli) {
        $sql .= " and ( c.codclistatus = $codstatuscli) ";
    }

    $campoData = ($tipoData == 'dataos') ? 'o.dataos' : 'o.dataosfin';
    if ($dtosini) {
        $sql .= " and ($campoData >= '$dtosini' ) ";
    }
    if ($dtosfin) {
        $sql .= " and ($campoData <= '$dtosfin' ) ";
    }


    if ($statusVenda == 'aberta') {
        $sql .= " AND o.codstatus = $p_codstatus";
    } else if ($statusVenda == 'finalizada') {
        $sql .= " AND o.codstatus = $p_codstatusfin";
    }


    $final = " GROUP BY O.CODOS, O.OS, O.DATAOS, O.DATAOSFIN, C.RAZAO, c.FONE1, C.NUMCLI, 8, 9, 10, 11, 12
    
    order by o.os, c.razao";
    
    $sql .= $final;
    // var_dump($sql);die();

    // echo $sql;
    echo("<p> <h2 class='page-header'>$r_MV143_pedidos_de_venda</h2></p>");

    echo "
    <div class='box-body table-responsive no-padding'>
    <table class='table table-striped'>
            <thead>
            <tr class='titulos'>
                <td>$r_MV143_venda</td>
                <td>$r_MV143_data</td>
                <td>$r_MV143_datafin</td>
                <td>$r_MV143_cliente</td>
                " . ($p_mostra_telefone_rel_mv143==1 ? "<td>$r_MV143_telefone</td>" : "") . "
                <td>$r_MV143_vendedor</td>
                <td>$r_MV143_dados_adic</td>
                <td>$r_MV143_prazo_pagto</td>
                <td>$r_MV143_status</td>
                <td>$r_MV143_total</td>
            </tr>
            </thead>";

    $totalGeral = 0;
    $entregueGeral = 0;
    $saldoGeral = 0;

    //Desenha o Relat�rio
    // Traz os campos de fato
    if (cria_matriz_campos_pelo_sql($sql, $itens)) {
        for ($i = 0; $i <= (sizeof($itens) - 1); $i++) {

            MostraMV143($itens[$i][os], $itens[$i][dataos], $itens[$i][dataosfin], $itens[$i][numcli], $itens[$i][razao], $itens[$i][fone1], $itens[$i][vendedor], $itens[$i][prazopagto],
                    $itens[$i][status], $itens[$i][total], $itens[$i][totalentregue], $itens[$i][totalsaldo], $itens[$i][dadosadicionais]);

            $totalGeral += $itens[$i][total];
            $entregueGeral += $itens[$i][totalentregue];
            $saldoGeral += $itens[$i][totalsaldo];

        }
    }

    echo " 
        <tfoot>
            <tr>
                <td colspan=" . ($p_mostra_telefone_rel_mv143==1 ? "6" : "5") . "></td>
                <td><b>$r_MV143_totais_gerais</b></td>
                <td><b>R$" . number_format($totalGeral, 2, ',', '.') . "</b></td>
            </tr>
        </tfoot>
        </table>
    </div>";
}

function MostraMV143($os, $dataos, $dataosfin, $numcli, $razao, $fone1, $vendedor, $prazopagto, $status, $total, $totalentregue, $totalsaldo, $dadosAdicionais) {
    include '../auxiliar/parametros.inc';
    echo "
        
            <tr>
                <td>$os</td>
                <td>$dataos</td>
                <td>$dataosfin</td>
                <td>$numcli - $razao</td>
                " . ($p_mostra_telefone_rel_mv143==1 ? "<td>$fone1</td>" : "") . "
                <td>$vendedor</td>
                <td>$dadosAdicionais</td>
                <td>$prazopagto</td>
                <td>$status</td>
                <td>R$" . number_format($total, 2, ',', '.') . "</td>
            </tr>";
}

/* * *****************************************************************************
 *                               MV143 - FIM
 * **************************************************************************** */

/* * *****************************************************************************
 *                               RelSaldoFlex - INICIO
 * **************************************************************************** */

function RelSaldoFlex() {
    
    include_once '../auxiliar/parametros.inc';


    include_once "funcoes.inc";
    include_once '../auxiliar/strings.inc';
    include_once "../bd/bd_senhas.inc";
    include_once '../auxiliar/datas.inc';
    include '../idiomas/config.inc';


      
    $p_codusu = lecookie("codusu");
  
    //Monta o Select
    $sql = " SELECT codvnd, vendedor, saldo FROM SALDOFLEXVENDEDOR WHERE
            codvnd in ( SELECT distinct CODVND FROM SENHAS WHERE CODUSU = $p_codusu or codgerente = $p_codusu )";

    // var_dump($sql);die();

    // echo $sql;
    echo("<p> <h2 class='page-header'>$r_saldo_flex</h2></p>");

    echo "
    <div class='box-body table-responsive no-padding'>
    <table class='table table-striped'>
            <thead>
            <tr class='titulos'>
                <td>$r_sf_vendedor</td>
                <td>$r_sf_valor</td>
            </tr>
            </thead>";

  
    //Desenha o Relat�rio
    // Traz os campos de fato
    if (cria_matriz_campos_pelo_sql($sql, $itens)) {
        for ($i = 0; $i <= (sizeof($itens) - 1); $i++) {
            MostraRelSaldoFlex($itens[$i][vendedor], $itens[$i][saldo]);
        }
    }

    echo " </table>
    </div>";
}

function MostraRelSaldoFlex($vendedor, $saldo) {
    include '../auxiliar/parametros.inc';
    echo "
        
            <tr>
                <td>$vendedor</td>
                <td>R$ " . number_format($saldo, 2, ',', '.') . "</td>
            </tr>";
}

/* * *****************************************************************************
 *                               RelSaldoFlex - FIM
 * **************************************************************************** */





/*
 *
 *           FUNES
 *            GERAIS
 *          AUXILIARES
 *
 */

function MostraLookup($campo, $cod, $nomecookie, $codcookie, $pathgo) {

    echo "<a href='../relatorios/setafiltrocookie.php?campo=$campo&cod=$cod&nomecookie=$nomecookie&codcookie=$codcookie&pathgo=$pathgo'>$campo</a><br>";
}

function ListaLookup($tabela, $campo, $chave, $valorfiltro, $nomecookie, $codcookie, $pathgo) {
    /*
     * tabela = tabela filtrada no lookup
     * campo = campo que mostra no lookup
     * chave = campo chave pra associar o lookup
     * valorfiltro = valor inserido pelo usuario
     * nomecookie = nome do cookie pra gravar a descricao da op��o filtrada
     * codcookie = nome do cookie pra gravar o codigo da op��o filtrada
     * pathgo = caminho pra onde ser� redirecionada a p�gina depois de gravar os cookies
     *        aqui normalmente deve ser a pagina onde tem os filtros que vai abrir o rel
     */

    include '../auxiliar/parametros.inc';
    include 'conecta.inc.php';


    $l_select = "SELECT $campo as campo, $chave as cod FROM $tabela WHERE (1=1)";

    if ($valorfiltro) {
        $l_select .= " and $campo containing '$valorfiltro' ";
    }

    $l_select .= " order by $campo";

    $a_reg = qry($con, $l_select, $a_campos);


    if (cria_matriz_campos($a_reg, $a_campos, $l_a_sql)) {
        //se tiver resultado do select - quando encontra pelo menos um cliente

        for ($i = 0; $i <= (sizeof($l_a_sql) - 1); $i++) {
            MostraLookup($l_a_sql[$i][campo], $l_a_sql[$i][cod], $nomecookie, $codcookie, $pathgo);
        }
    }
    exit;
}

function LimpaLookup($nomecookie, $codcookie) {
   setcookie($nomecookie, "", time() - 3600, '/');
   setcookie($codcookie, "", time() - 3600, '/');
}

?>
