dev-master
9999999-dev http://www.pandora.com.br/Exporta para CSV e PDF resultados rest do APIGILITY
The Requires
by Fabio Ginzel
csv framework rest zf2 apigility pdf pandora
Wallogit.com
2017 © Pedro PelĆ”ez
Exporta para CSV e PDF resultados rest do APIGILITY
Biblioteca para gerar PDF e CSV a partir de uma requisição integrado ao Zend Framework 2 e Apigility, (*1)
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)
'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)
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>
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>
Exporta para CSV e PDF resultados rest do APIGILITY
csv framework rest zf2 apigility pdf pandora