<?php

    mb_internal_encoding("UTF-8");

    require_once "protected/extensions/tcpdf/tcpdf/tcpdf.php";

    class MYPDF extends TCPDF {
        public function Header() {
            $this->SetFont('helvetica', '', 8);

            $header = '<table width="100%" border="0" cellspacing="0" cellpadding="0">';
                $header .= '<tr>';
                    $header .= '<td><img src="'.FaHtml::getImgUrl('logo-contrato.jpg', false, false).'" width="99" height="24" /></td>';
                    $header .= '<td align="right">';
                        $header .= 'Emiss&atilde;o: ' . date('d/m/Y');
                        $header .= "<br>";
                        $header .= 'Hora: ' . date('H:i:s');
                    $header .= '</td>';
                $header .= '</tr>';
            $header .= '</table>';

            $this->MultiCell(0, 15, $header, 0, 'J', false, 0, '', '', true, 0, true, true, 0, 'M', false);
        }

        public function Footer() {
            $this->SetY(-15);
            $this->SetFont('helvetica', 'I', 8);
            $this->Cell(0, 10, 'Página '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
        }
    }

    $this->layout = 'pdf';
    $pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'ISO 8859-1', false);

    $pdf->SetAuthor("Windel Sistemas");
    $pdf->SetTitle('Relatório de Comissão');
    $pdf->AliasNbPages();
    $pdf->setFont('Helvetica', '', 8);
    $pdf->SetAutoPageBreak(TRUE, 0);

    $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
    $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

    $pdf->setMargins(4, 15, 4, 15);

    $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
    $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

    $pdf->AddPage();

    ob_start();

    function lineHeader(&$cont) {
        echo "<tr>";
            echo "<th width=\"40\"><b>T&iacute;tulo</b></th>";
            echo "<th width=\"30\"><b>S&eacute;rie</b></th>";
            echo "<th width=\"230\"><b>Cliente</b></th>";
            echo "<th width=\"50\"><b>Emiss&atilde;o</b></th>";
            echo "<th width=\"50\"><b>Vencimento</b></th>";
            echo "<th width=\"50\"><b>Pagamento</b></th>";
            echo "<th width=\"40\"><b>Mensal</b></th>";
            echo "<th width=\"40\"><b>Juros</b></th>";
            echo "<th width=\"40\"><b>Total</b></th>";
        echo "</tr>";
        $cont++;
    }

    $MAXIMO_LINHAS = 27;

    if(empty($search['TIT_IDREVENDA'])){
        $unidade = "Todas";
    } else {
        $unidade = $data[0]->Cliente->getRevendaResponsavel2()->REV_FANTASIA;
    }

    $serie = $search['TIT_SERIE'];
    $pgini = $search['TIT_DTPAGAMENTO'];
    $pgfim = $search['TIT_DTPAGAMENTO_FINAL'];

    echo "<h1> Relat&oacute;rio de Comiss&otilde;es</h1>";
    echo "<h2> Filtros </h2>";

    echo "<div>";
        echo "<div style='float: left; text-align: left;'>
                   <strong>Unidade: </strong><span>{$unidade}</span>
              </div>";
        echo "<div style='float: left; text-align: right;'>
                   <strong>Pagamento Inicial: </strong><span>{$pgini}</span>
                   <strong> - Final: </strong><span>{$pgfim}</span> |
                   <strong>S&eacute;rie: </strong><span>{$serie}</span>
              </div>";
    echo "</div>";

    echo "<table border=\"1\" cellpadding=\"2\">";
        $cont = 2;
        lineHeader($cont);

        $totalDocumento = 0.00;
        $quebrarPagina = false;

        foreach ($data as $key => $documento) {

            if ($quebrarPagina) {
                $cont = 0;
                $quebrarPagina = false;

                echo '</table>';
                echo '<br pagebreak="true"\>';
                echo "<table border=\"1\" cellpadding=\"2\">";
                lineHeader($cont);
            }

            echo "<tr>";

                echo "<td width=\"40\">";
                    echo $documento['TIT_TITULO'];
                echo "</td>";

                echo "<td width=\"30\">";
                    echo $documento['TIT_SERIE'];
                echo "</td>";

                echo "<td width=\"230\">";
                    echo GxHtml::valueEx($documento->Cliente);
                echo "</td>";

                echo "<td width=\"50\">";
                    echo Util::formatDataBr($documento->TIT_EMISSAO);
                echo "</td>";

                echo "<td width=\"50\">";
                    echo Util::formatDataBr($documento->TIT_VENCIMENTO);
                echo "</td>";

                echo "<td width=\"50\">";
                    echo Util::formatDataBr($documento->TIT_DTPAGAMENTO);
                echo "</td>";

                echo "<td width=\"40\">";
                    echo Util::verMoeda($documento->TIT_VALOR);
                echo "</td>";
                echo "<td width=\"40\">";
                    echo Util::verMoeda($documento->TIT_VALORPAGO);
                echo "</td>";
                echo "<td width=\"40\">";
                    echo Util::verMoeda($documento->TIT_VALOR_TOTAL);
                echo "</td>";

            echo "</tr>";

            if ($cont == $MAXIMO_LINHAS)
                $quebrarPagina = true;

            $cont++;
        }
        echo "<tr><td colspan='8'></td></tr>";
        echo "<tr>";
            echo "<td colspan=\"2\"></td>";
            echo "<th colspan=\"6\"><b>Total</b></th>";

            echo "<td>";
                echo Util::verMoeda($total);
            echo "</td>";
        echo "</tr>";

        echo "<tr>";
            echo "<td colspan=\"2\"></td>";
            echo "<th colspan=\"6\"><b>Comiss&atilde;o</b></th>";
            echo "<td>";
                echo Util::verMoeda($comissao);
            echo "</td>";
        echo "</tr>";

    echo "</table>";

    $HTML = ob_get_contents();
    ob_end_clean();

    $pdf->writeHTML(utf8_encode($HTML), true, 0, true, true);
    $pdf->Output($generatePath.'.pdf', "FI");

?>