2017 © Pedro PelĆ”ez
 

library exporta-rest

Exporta para CSV e PDF resultados rest do APIGILITY

image

pandora-una/exporta-rest

Exporta para CSV e PDF resultados rest do APIGILITY

  • Friday, July 1, 2016
  • by fabioginzel
  • Repository
  • 8 Watchers
  • 2 Stars
  • 1,099 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 1 Versions
  • 9 % Grown

The README.md

ExportaRest

Biblioteca para gerar PDF e CSV a partir de uma requisição integrado ao Zend Framework 2 e Apigility, (*1)

Instalação

A forma recomendada de instalação é por composer:, (*2)

    {
        "require": {
            "pandora-una/exporta-rest" : "dev-master"
        }    
    }

Adicionar no application.config.php o módulo "ExportaRest", (*3)

Configuração

        'exporta_rest' => array(
            'csv_template_dir_name' => 'relatorio-csv',
            'pdf_template_dir_name' => 'relatorio-pdf'
        ),

Para cada controller que for retornar um pdf ou um csv configurar:, (*4)

    'zf-content-negotiation' => array(
        'controllers' => array(
            'Api\NomeDoController\Controller' => 'HalJsonExportacao'
        ),

Criar no módulo do controller os templates:, (*5)

  • Api/view/api/relatorio-csv/nomeDaColecao.phtml
  • Api/view/api/relatorio-pdf/nomeDaColecao.phtml
  • Api/view/api/relatorio-csv/nomeDaEntidade-entity.phtml
  • Api/view/api/relatorio-pdf/nomeDaEntidade-entity.phtml

Dentro de cada template usar a variÔvel payload para pegar os dados da coleção. Ex:, (*6)

CSV, (*7)

Cód;Data;Nome;CPF;Dt. Nascimento;Valor;Forma de Pagto;Status;Pagamento;Pago;Dt. Crédito;Valor Crédito;Status Transação;Recibo de doação
payload->getCollection() as $item):?>
getId().';';
$linha .= $this->dateFormat($item->getDataHora(),IntlDateFormatter::MEDIUM).';';
$linha .= $item->getPessoa()->getNome().';';
$linha .= $this->cpfFormat($item->getPessoa()->getCpf()).';';
$linha .= $this->dateFormat($item->getPessoa()->getDtNascimento(),IntlDateFormatter::MEDIUM) .';';
$linha .= $this->currencyFormat($item->getValor(),'BRL').';';
$linha .= $item->getFormaPagamentoDescritivo().';';
$linha .= $item->getStatusDescritivo().';';
$linha .= $this->dateFormat($item->getDtPagamento(),IntlDateFormatter::MEDIUM).';';
$linha .= $this->currencyFormat($item->getValorPago(),'BRL').';';
$linha .= $this->dateFormat($item->getDtCredito(),IntlDateFormatter::MEDIUM).';';
$linha .= $this->currencyFormat($item->getValorCredito(),'BRL').';';
$linha .= ($item->getStatTran() ? $item->getStatTran()->getDescricao() : '').';';
$linha .= ($item->getRecibo() ? $item->getRecibo()->getNumeroReciboFormatado() : '').';';
echo $linha."\n";

endforeach;?>

PDF, (*8)

<html>
<body>
    <table>
        <thead>
            <tr>
                <td style="width: 30px">Cód</td>
                <td style="width: 40px">Data</td>
                <td style="width: 160px">Nome</td>
                <td style="width: 70px">CPF</td>
                <td style="width: 40px">Nasc</td>               
                <td style="width: 50px">Valor</td>
                <td style="width: 80px">Forma de Pagto</td>
                <td style="width: 80px">Status</td>
                <td style="width: 70px">Pagamento</td>
                <td style="width: 50px">Pago</td>
                <td style="width: 140px">Status Transação</td>
                <td style="width: 130px">Recibo de doação</td>
            </tr>
        </thead>
        <tbody> 
<?php foreach ($this->payload->getCollection() as $item):?>
            <tr>
                <td class="centro"><?= $item->getId()?></td>
                <td class="centro"><?= $this->dateFormat($item->getDataHora(),IntlDateFormatter::SHORT)?></td>
                <td><?= $item->getPessoa()->getNome()?></td>
                <td class="centro"><?= $this->cpfFormat($item->getPessoa()->getCpf())?></td>
                <td class="centro"><?= $this->dateFormat($item->getPessoa()->getDtNascimento(),IntlDateFormatter::SHORT)?></td>
                <td class="centro"><?= $this->currencyFormat($item->getValor(),'BRL') ?></td>
                <td class="centro"><?= $item->getFormaPagamentoDescritivo()?></td>
                <td class="centro"><?= $item->getStatusDescritivo()?></td>
                <td class="centro"><?= $this->dateFormat($item->getDtPagamento(),IntlDateFormatter::SHORT)?></td>
                <td class="centro"><?= $this->currencyFormat($item->getValorPago(),'BRL') ?></td>
                <td class="centro"><?=($item->getStatTran() ? $item->getStatTran()->getDescricao() : '')?></td>
                <td class="centro"><?= ($item->getRecibo() ? $item->getRecibo()->getNumeroReciboFormatado() : '')?>
            </tr>
<?php endforeach;?>
        </tbody>
    </table>    
</body>
</html>

PDF/CSV para entidades

Caso queira imprimir os dados de uma entidade (não collection), basta fazer a requisição rest normalmente, mas o nome do template deve possuir a terminação "-entity", ex: nomeDaEntidade-entity. No template para acessar os dados da entidade deve-se chamar $this->payload->entity->getPropriedade(): Ex:, (*9)

CSV, (*10)

<?php $item = $this->payload->entity; ?>
Cód;Data;Nome;CPF;Dt. Nascimento;Valor;Forma de Pagto;Status;Pagamento;Pago;Dt. Crédito;Valor Crédito;Status Transação;Recibo de doação
<?php
$linha = $item->getId().';';
$linha .= $this->dateFormat($item->getDataHora(),IntlDateFormatter::MEDIUM).';';
$linha .= $item->getPessoa()->getNome().';';
$linha .= $this->cpfFormat($item->getPessoa()->getCpf()).';';
$linha .= $this->dateFormat($item->getPessoa()->getDtNascimento(),IntlDateFormatter::MEDIUM) .';';
$linha .= $this->currencyFormat($item->getValor(),'BRL').';';
$linha .= $item->getFormaPagamentoDescritivo().';';
$linha .= $item->getStatusDescritivo().';';
$linha .= $this->dateFormat($item->getDtPagamento(),IntlDateFormatter::MEDIUM).';';
$linha .= $this->currencyFormat($item->getValorPago(),'BRL').';';
$linha .= $this->dateFormat($item->getDtCredito(),IntlDateFormatter::MEDIUM).';';
$linha .= $this->currencyFormat($item->getValorCredito(),'BRL').';';
$linha .= ($item->getStatTran() ? $item->getStatTran()->getDescricao() : '').';';
$linha .= ($item->getRecibo() ? $item->getRecibo()->getNumeroReciboFormatado() : '').';';
echo $linha."\n";
?>

PDF, (*11)

<?php $item = $this->payload->entity; ?>
<html>
<body>
    <table>
        <thead>
            <tr>
                <td style="width: 30px">Cód</td>
                <td style="width: 40px">Data</td>
                <td style="width: 160px">Nome</td>
                <td style="width: 70px">CPF</td>
                <td style="width: 40px">Nasc</td>               
                <td style="width: 50px">Valor</td>
                <td style="width: 80px">Forma de Pagto</td>
                <td style="width: 80px">Status</td>
                <td style="width: 70px">Pagamento</td>
                <td style="width: 50px">Pago</td>
                <td style="width: 140px">Status Transação</td>
                <td style="width: 130px">Recibo de doação</td>
            </tr>
        </thead>
        <tbody> 
            <tr>
                <td class="centro"><?= $item->getId()?></td>
                <td class="centro"><?= $this->dateFormat($item->getDataHora(),IntlDateFormatter::SHORT)?></td>
                <td><?= $item->getPessoa()->getNome()?></td>
                <td class="centro"><?= $this->cpfFormat($item->getPessoa()->getCpf())?></td>
                <td class="centro"><?= $this->dateFormat($item->getPessoa()->getDtNascimento(),IntlDateFormatter::SHORT)?></td>
                <td class="centro"><?= $this->currencyFormat($item->getValor(),'BRL') ?></td>
                <td class="centro"><?= $item->getFormaPagamentoDescritivo()?></td>
                <td class="centro"><?= $item->getStatusDescritivo()?></td>
                <td class="centro"><?= $this->dateFormat($item->getDtPagamento(),IntlDateFormatter::SHORT)?></td>
                <td class="centro"><?= $this->currencyFormat($item->getValorPago(),'BRL') ?></td>
                <td class="centro"><?=($item->getStatTran() ? $item->getStatTran()->getDescricao() : '')?></td>
                <td class="centro"><?= ($item->getRecibo() ? $item->getRecibo()->getNumeroReciboFormatado() : '')?>
            </tr>
        </tbody>
    </table>    
</body>
</html>

Uso

  • Para CSV fazer requisição com o header Accept: text/csv e o parametro get filename=nomeDoArquivo
  • Para PDF fazer requisição com o header Accept: application/pdf e o parametro get filename=nomeDoArquivo e paper-orientation=landscape ou portrait

The Versions

01/07 2016

dev-master

9999999-dev http://www.pandora.com.br/

Exporta para CSV e PDF resultados rest do APIGILITY

  Sources   Download

The Requires

 

csv framework rest zf2 apigility pdf pandora