<?php
require 'Conexao.php';
require 'dadosBoleto.php';
require_once 'tcpdf_barcodes_1d.php';
define("BASE_URL", "http://172.21.55.39/boleto");

########################################################### TITULO ##########################################################################
$titulo = isset($_GET['titulo']) ? $_GET['titulo'] : null;
if (empty($titulo)){
    echo "TITULO NAO INFORMADO";
    exit;
}
########################################################### TITULO ##########################################################################

$Conexao = new Conexao();
$database = $Conexao->getConnection();

$boletoPf   = new dadosBoleto($database);
$topo       = $boletoPf->topoBoleto($titulo);
$corpo      = $boletoPf->corpoBoleto($titulo);
$codigo     = $boletoPf->codbarrasBoleto($titulo);
 
$barcodeobj = new TCPDFBarcode($codigo[0]['DIGITAVEL'], 'C39');

########################################################### CÓDIGO DE BARRAS ########################################################################     

$html=' 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pt" lang="pt-br">
<meta http-equiv="content-type" content="text/html; charset=utf-8" />

<html>
	<head>

        <title>UNIMED SETE LAGOAS</title>
        <link href="css/css.css" rel="stylesheet" />

    </head>

    <body>
        <div id="printableArea" >
            <table>
                <tr>
                    <td> 
                        <table id="table-form2">
                            <tbody>
                                <tr>';
                                foreach($topo as $item):  endforeach;
                                    $html.='
                                    <td style="width:100px;border: none;" rowspan="3">
                                        <img src="img/unimed.png" alt="UNIMED SETE LAGOAS" width="120px" height="50px"><br>
                                        <img src="img/ans.png" alt="UNIMED SETE LAGOAS" width="100px" height="30px">
                                    </td>
                                    <td style="width:390px;border: none;" colspan="4" rowspan="3 ">
                                        <h1><b>UNIMED SETE LAGOAS COOPERATIVA DE TRABALHO MÉDICO</b></h1>
                                        <h2>RUA CORONEL RANDOLFO SIMÕES 281</h2>
                                        <h2>BOA VISTA - SETE LAGOAS - MG - CEP: 35700102</h2>
                                        <h2>Tel.: 31-2106-1900</h2>
                                        <h2>CNPJ: 24.014.235/0001-09 Insc. Municipal: 03.21118-5 </h2>
                                    </td>
                                    <td height="8">Contrato: <br>'.$item['CONTRATO'].'</td>
                                    <td> Competência: '.$item["COMPETENCIA"].'</td>
                                    <td colspan="2">Emissão: <br>'.date("d/m/Y", strtotime($item["EMISSAO"])).'</td>
                                </tr>
                                <tr>
                                    <td> Vencimento:<br>'.date("d/m/Y", strtotime($item["VENCIMENTO"])).'</td>
                                    <td> Nº Fatura: '.$item["FATURA"].'</td>
                                    <td> Nº Título: '.$item["TITULO"].'</td>
                                    <td> Página: <br>1</td>
                                </tr>
                                <tr>
                                    <td colspan="2">Agência/Código Cedente: '.$item["AGENC_CEDENTE"].'</td>
                                    <td>Carteira: '.$item["CARTEIRA"].'</td>
                                    <td>Nosso Número: '.$item["NOSSO_NUMERO"].'</td>
                                </tr>
                            </tbody>
                        </table>

                        <table id="table-form3">
                            <tbody>
                                <tr>
                                    <td width="100px">Sacado:</td>
                                    <td width="400px" colspan="2" >'.$item['SACADO'].'</td>
                                    <td>Telefone:</td>
                                    <td colspan="2">'.$item['TELEFONE'].'</td>
                                </tr>
                                <tr>
                                    <td width="10px">Endereço:</td>
                                    <td colspan="2">'.$item['ENDERECO'].'</td>
                                    <td>Bairro: </td>
                                    <td colspan="2">'.$item['BAIRRO'].'</td>
                                </tr>
                                <tr>
                                    <td width="10px">Município:</td>
                                    <td colspan="2">'.$item['CIDADE'].'</td>
                                    <td>Estado:</td>
                                    <td colspan="2">'.$item['ESTADO'].'</td>
                                </tr>
                                <tr>
                                    <td width="10px">CNPJ:</td>
                                    <td colspan="2">'.$item['CNPJ'].'</td>
                                    <td>Inscr.Estadual:</td>
                                    <td colspan="2">'.$item['JINSC_ESTADUAL'].'</td>
                                </tr>
                                <tr>
                                    <td>Lotação:</td>
                                    <td colspan="2">'.$item['LOTACAO'].'</td>
                                    <td></td>
                                    <td colspan="2" style=";text-align: right;">RECIBO DO SACADO </td>
                                </tr>
                            </tbody>
                        </table> 

                        <table id="table-form4">
                            <tbody>
                            <tr>
                                <td colspan="3">DESCRIÇÃO</td>
                                <td>VLR. UNITÁRIO</td>
                                <td>VLR. TOTAL</td>
                            </tr>
                            <tr>
                                <td  colspan="3" style="height:200px;border-bottom-color:white;">';
                                foreach($corpo as $item): 
                                    $html.='<table id="table-form6">
                                            <tr><td style="font-size:11px;width: 250px;"><br>&nbsp; '.$item['QTD'].' - '.$item['DESCICAO_ITEM'].'</td></tr>
                                    </table>';
                                endforeach; 
                                    $html.='<table id="table-form6">
                                        <tr><td style="font-size:11px;"><strong><br>&nbsp; TOTAL</strong></td></tr>
                                    </table>
                                </td>
                                <td style="border-bottom-color:white;">';
                                foreach($corpo as $item): 
                                    $html.='<table id="table-form6">
                                        <tr><td style="font-size:11px;"><br>&nbsp; R$ '.number_format($item['VALOR_UNIT'],2,',','.').'</td></tr>
                                    </table>';
                                endforeach;
                                    $html.='<td style="border-bottom-color:white;">';
                                foreach($corpo as $item): 
                                    $html.='<table id="table-form6">
                                        <tr><td style="font-size:11px;"><br>&nbsp;  R$ '.number_format($item['VALOR_TOTAL'],2,',','.').'</td></tr>
                                    </table>';
                                    endforeach;
                                foreach($corpo as $item): 
                                    $html.='<table id="table-form6">
                                        <tr><td style="font-size:11px;width: 50px;"><strong><br>&nbsp; R$ '.number_format($item['VALOR_TOTAL'],2,',','.').'</strong></td></tr>
                                    </table>';
                                endforeach;
                                $html.='</td>
                                </tr>
                                <tr>
                                    <td colspan="3" style="border-top-color:white;border-bottom-color:white;font-size:10px;">
                                        &nbsp;&nbsp;&nbsp;Valor aproximado dos tributos: PIS 0,65% COFINS 4% ISSQN 3%, com base na Lei 12.741/2012 e no Decreto 8.264/2014.
                                        <br>
                                        &nbsp;&nbsp;&nbsp;Não reter CSLL conforme IN SRF nº 459, art. 5º de 18.10.2004.
                                    </td>
                                    <td style="border-bottom-color:white;"></td>
                                    <td style="border-bottom-color:white;"></td>
                                </tr>
                                <tr>
                                    <td style="border-bottom-color:white;border-top-color:white;border-right-color:white;"><strong>&nbsp;&nbsp;Tributo</strong></td>
                                    <td style="border-bottom-color:white;border-top-color:white;border-right-color:white;"><strong>Base de Cálculo</strong></td>
                                    <td style="border-bottom-color:white;border-top-color:white;"><strong>Percentual</strong></td>
                                    <td style="border-bottom-color:white;border-top-color:white;"><br></td>
                                    <td style="border-bottom-color:white;border-top-color:white;"><br></td>
                                </tr>
                                <tr>
                                    <td style="border-top-color:white;" colspan="3" ></td>
                                    <td style="border-top-color:white;">&nbsp;</td>
                                    <td style="border-top-color:white;">&nbsp;</td>
                                </tr>
                                <tr>
                                    <td colspan="3" style="border-top-color:black; border-top:1px solid black;"><strong>&nbsp;&nbsp;VALOR TOTAL</strong></td>
                                    <td style="border-top-color:white;">&nbsp;</td>
                                    <td><strong>&nbsp;&nbsp;'; 
                                    foreach($corpo as $item): 
                                        $html.=' R$ '.number_format($item['VALOR_TOTAL'],2,',','.'); 
                                    endforeach; $html.='<br></strong></td>
                                </tr>
                            </tbody>
                        </table>

                        <br><hr size="1" style="border:0.5px dashed black;">

                        <table id="table-form5">
                            <tbody>
                                <tr>
                                    <td style="width: 20px;border-bottom:2px solid black;border-right:2px solid black;">BANCO SANTANDER</td>
                                    <td style="width: 5px;border-bottom:2px solid black;border-right:2px solid black;"> 033-7</td>
                                    <td colspan="6" style="width:200px;border-right-color:white;border-bottom:2px solid black;font-size:19px;">'; 
                                    foreach($topo as $item): 
                                        $html.=' '.$item['DIGITAVEL']; 
                                    endforeach; 
                                    $html.='</h6></td>
                                </tr>
                                <tr>
                                    <td colspan="7" >Local de Pagamento<br>&nbsp;&nbsp;&nbsp;Pagável em qualquer banco até o vencimento</td>
                                    <td width="70px">Vencimento:<br>&nbsp;&nbsp;&nbsp;'; 
                                    foreach($topo as $item): 
                                        $html.=' '.date("d/m/Y", strtotime($item['VENCIMENTO'])); 
                                    endforeach; 
                                    $html.='</td>
                                </tr>
                                <tr>
                                    <td colspan="7" >Beneficiário<br>&nbsp;&nbsp;&nbsp;UNIMED SETE LAGOAS COOPERATIVA DE TRABALHO MÉDICO CNPJ: 24.014.235/0001-09</td>
                                    <td>Agência/Código Beneficiário<br>&nbsp;&nbsp;&nbsp;'; 
                                    foreach($topo as $item): 
                                        $html.=' '.$item['AGENC_CEDENTE']; 
                                    endforeach; 
                                    $html.='</td>
                                </tr>
                            </tbody>
                        </table>

                   
                        <table id="table-form5">
                            <tbody>
                                <tr>
                                    <td width="150px">Data do Documento <br>&nbsp;&nbsp;'; foreach($topo as $item): $html.=' '.date("d/m/Y", strtotime($item['EMISSAO'])); endforeach; $html.='</td>
                                    <td width="150px" colspan="2">Nº do Documento <br>&nbsp;&nbsp;'; foreach($topo as $item): $html.=' '.$item['FATURA']; endforeach; $html.='</td>
                                    <td>Espécie Doc. <br>&nbsp;&nbsp;DM</td>
                                    <td>Aceite <br>&nbsp;&nbsp;&nbsp;N</td>
                                    <td width="150px">Data do Processamento <br>&nbsp;&nbsp;'; foreach($topo as $item):$html.=' '.date("d/m/Y", strtotime($item['EMISSAO'])); endforeach; $html.='</td>
                                    <td width="163.5px">Nosso Número <br>&nbsp;&nbsp;'; foreach($topo as $item): $html.=' '.$item['NOSSO_NUMERO']; endforeach; $html.='</td>
                                </tr>
                                <tr>
                                    <td>Uso do Banco <br>&nbsp;&nbsp;&nbsp;CNAB 400</td>
                                    <td>Carteira<br>&nbsp;&nbsp;&nbsp;'; foreach($topo as $item): $html.=' '.$item['CARTEIRA']; endforeach; $html.='</td>
                                    <td>Espécie <br>&nbsp;&nbsp;&nbsp;R$</td>
                                    <td  colspan="2">Quantidade<br>&nbsp;&nbsp;&nbsp;</td>
                                    <td>Valor<br>&nbsp;&nbsp;&nbsp;</td>
                                    <td>(=)Valor do documento<br>&nbsp;&nbsp;&nbsp;';
                                        foreach($corpo as $item): $html.=' <strong>'.number_format($item['VALOR_TOTAL'],2,',','.').'</br>'; endforeach; $html.='</strong></td>
                                </tr>
                            </tbody>
                        </table>

                        <table id="table-form5">
                            <tbody>
                                <tr>
                                    <td width="611.8px" rowspan="5">
                                        Instruções:
                                        <br>&nbsp;&nbsp;&nbsp;&nbsp;APÓS O VENCIMENTO COBRAR JUROS DE 0,03% AO DIA + MULTA DE 2%.
                                        <br>&nbsp;&nbsp;&nbsp;&nbsp;ATÉ O VENCIMENTO PAGÁVEL EM QUALQUER AGÊNCIA BANCÁRIA OU CASA LOTÉRICA.
                                        <br>&nbsp;&nbsp;&nbsp;&nbsp;APÓS O VENCIMENTO DIRIGIR-SE A AGÊNCIA DO SANTANDER.
                                        <br>&nbsp;&nbsp;&nbsp;&nbsp;2ª VIA DESTE, DISPONÍVEL NO SITE: WWW.SANTANDER.COM.BR
                                        <br>&nbsp;&nbsp;&nbsp;&nbsp;Competência
                                    </td>
                                    <td >(-)Desconto/Abatimento</td>
                                    
                                </tr>
                                <tr>
                                    <td >(-)Outras Deduções</td>
                                </tr>
                                <tr>
                                    <td >(+)Mora/Multa</td>
                                </tr>
                                <tr>
                                    <td >(+)Outros Acréscimos</td>
                                </tr>
                                <tr>
                                    <td >(=)Valor Cobrado</td>
                                </tr>
                            </tbody>
                        </table>

                        <table id="table-form5">
                            <tbody>
                                <tr>
                                    <td rowspan="3" style="border-bottom:2px solid black;border-right:0px solid black;"><br>Pagador:</td>
                                    <td style="border-right:0px solid black;border-bottom:0px solid black;"><br>'; foreach($topo as $item): $html.=' '.$item['CONTRATO']."  ".$item['SACADO']; endforeach; $html.='</td>
                                    <td style="border-right:0px solid black;border-bottom:0px solid black;"><br>CNPJ:</td>
                                </tr>
                                <tr>
                                    <td style="border-right:0px solid black;border-bottom:0px solid black;">'; foreach($topo as $item): $html.=' '.$item['ENDERECO']."      -       ".$item['BAIRRO']; endforeach; $html.='</td>
                                    <td style="border-right:0px solid black;border-bottom:0px solid black;"></td>
                                </tr>
                                <tr>
                                    <td style="border-bottom:2px solid black;border-right:0px solid black;">'; foreach($topo as $item): $html.=' '.$item['CEP']."  ".$item['CIDADE']; endforeach; $html.='</td>
                                    <td style="border-bottom:2px solid black;border-right:0px solid black;font-weight: bold;">(145.'.$item['CONTRATO'].')'; $html.='</td>
                                </tr>
                            </tbody>
                        </table>
                        <br>
                        <table id="table-form6">
                            <tbody>
                                <tr>
                                    <td rowspan="2"><div class="barras">'.$barcodeobj->getBarcodeHTML(0.7, 50, 'black').'</div> </td>
                                    <td width="150px">Autenticação Mecânica</td>
                                </tr>
                                <tr>
                                    <td >Ficha de Compensação</td>
                                </tr>
                            </tbody>
                        </table>
                    </td>
                </tr>
            </table>
        </div>
    </body>
</html>';
                            
        //echo $html;
        require_once 'vendor/autoload.php';

        use Dompdf\Dompdf;
        use Dompdf\Options;
        
        $options = new Options();
        $options->set('isRemoteEnabled', TRUE);

        /* Cria a instância */
        $dompdf = new Dompdf($options);

        /* Carrega seu HTML */
        $dompdf->load_html($html,'UTF-8');

        $dompdf->setPaper('A4', 'portrait');

        /* Renderiza */
        $dompdf->render();
        
        ob_end_clean();

        /* Exibe */
        $dompdf->stream(
            "saida.pdf", /* Nome do arquivo de saída */
            array(
                "Attachment" => false /* Para download, altere para true */
            )
        );

        // /* Salva o arquivo no servidor */
        // file_put_contents($titulo.'.pdf', $dompdf->output());

        // /* Codifica o arquivo em formato base64 */
        // $encode = chunk_split(base64_encode(file_get_contents(BASE_URL.'/'.$titulo.'.pdf')));

        // /* Mostra o arquivo em formato base64 */
        // echo $encode;

        // /* Exclui o arquivo PDF */
        // unlink($titulo.'.pdf');

?>