Commit 956fb4ad by Damien Moulard

Fix export des operations

parent 27396758
......@@ -11,6 +11,7 @@ use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Security;
......@@ -18,6 +19,7 @@ use Symfony\Component\Translation\TranslatorInterface;
use Payum\Core\Payum;
use Payum\Core\Request\GetHumanStatus;
use Payum\Core\Request\Notify;
use App\Entity\Flux;
use App\Entity\Payment;
use App\Entity\Siege;
use App\Entity\User;
......@@ -27,7 +29,12 @@ use App\Entity\AchatMonnaieAdherent;
use App\Entity\AchatMonnaiePrestataire;
use App\Entity\CotisationAdherent;
use App\Entity\CotisationPrestataire;
use Sonata\Exporter\Handler;
use Sonata\Exporter\Source\DoctrineORMQuerySourceIterator;
use Sonata\Exporter\Writer\CsvWriter;
use Sonata\Exporter\Writer\JsonWriter;
use Sonata\Exporter\Writer\XmlWriter;
use Sonata\Exporter\Writer\XlsWriter;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
/**
......@@ -101,6 +108,92 @@ class FluxController extends AbstractController
]);
}
/**
* Export all transferts / transactions for a user role
*
* @param Request $request Request
* @param String $format Format of export ('json', 'xml', 'csv', 'xls')
* @Route("/flux/export/{format}/", name="exportUserFlux", defaults={"format": "csv"})
*/
public function exportFluxAction(Request $request, $format = 'csv')
{
//Prepare query depending on user role
$query = null;
$user = $this->security->getUser();
if ($this->session->get('_prestagere') != null && $this->security->getUser()->isGranted('ROLE_PRESTATAIRE')) {
$query = $this->em->getRepository(Flux::class)->getQueryByPrestataire($this->session->get('_prestagere'));
} elseif ($user->getAdherent() != null && $this->security->getUser()->isGranted('ROLE_ADHERENT')) {
$query = $this->em->getRepository(Flux::class)->getQueryByAdherent($user->getAdherent());
} elseif ($this->session->get('_comptoirgere') != null && $this->security->getUser()->isGranted('ROLE_COMPTOIR')) {
$query = $this->em->getRepository(Flux::class)->getQueryByComptoir($this->session->get('_comptoirgere'));
} elseif ($this->session->get('_groupegere') != null && $this->security->getUser()->isGranted('ROLE_GESTION_GROUPE')) {
$query = $this->em->getRepository(Flux::class)->getQueryByGroupe($this->session->get('_groupegere'));
}
if ($query != null) {
// Prepare the data source
$fields = ['expediteur', 'destinataire', 'type', 'parenttype', 'montant', 'moyen', 'operateur'];
$source = new DoctrineORMQuerySourceIterator($query, $fields);
$filename = sprintf(
'export_flux_%s.%s',
date('Y_m_d_H_i_s', strtotime('now')),
$format
);
return $this->getResponse(
$format,
$filename,
$source
);
} else {
$this->addFlash(
'error',
$this->translator->trans('Export impossible.')
);
return $this->redirectToRoute('index');
}
}
private function getResponse($format, $filename, $source)
{
switch ($format) {
case 'xls':
$writer = new XlsWriter('php://output');
$contentType = 'application/vnd.ms-excel';
break;
case 'xml':
$writer = new XmlWriter('php://output');
$contentType = 'text/xml';
break;
case 'json':
$writer = new JsonWriter('php://output');
$contentType = 'application/json';
break;
case 'csv':
$writer = new CsvWriter('php://output', ',', '"', '\\', true, true);
$contentType = 'text/csv';
break;
default:
throw new \RuntimeException('Invalid format');
}
$callback = static function () use ($source, $writer) {
$handler = Handler::create($source, $writer);
$handler->export();
};
return new StreamedResponse($callback, 200, [
'Content-Type' => $contentType,
'Content-Disposition' => sprintf('attachment; filename="%s"', $filename),
]);
}
/*
* Crée une instance de Payment et redirige vers la page de paiement
*/
......
......@@ -2,7 +2,6 @@
namespace App\Controller;
use App\Entity\Flux;
use App\Entity\Prestataire;
use App\Entity\Rubrique;
use App\Entity\TransactionPrestataireAdherent;
......@@ -17,9 +16,6 @@ use App\Form\Type\TransfertPrestataireSiegeFormType;
use App\Form\Type\AchatMonnaiePrestataireFormType;
use Doctrine\ORM\EntityManagerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Sonata\Exporter\Handler;
use Sonata\Exporter\Source\DoctrineORMQuerySourceIterator;
use Sonata\Exporter\Writer\CsvWriter;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
......@@ -101,7 +97,7 @@ class UserPrestataireController extends FluxController
return $this->manageFluxForm(
$request,
$form,
$this->session->get('_prestagere')->getCompte(),
$this->session->get('_prestagere')->getEcompte(),
$this->translator->trans('Transaction bien effectuée !'),
$this->translator->trans('Transaction à un ').$type
);
......@@ -121,7 +117,7 @@ class UserPrestataireController extends FluxController
return $this->manageFluxForm(
$request,
$form,
$this->session->get('_prestagere')->getCompte(),
$this->session->get('_prestagere')->getEcompte(),
$this->translator->trans('Reconversion envoyée, elle sera validée lorsque le virement sera effectué !'),
$this->translator->trans('Reconversion de monnaie au siège')
);
......@@ -154,69 +150,4 @@ class UserPrestataireController extends FluxController
]);
}
/**
* Export all transferts / transactions for prestataire
*
* @param Request $request Request
* @param String $format Format of export ('json', 'xml', 'csv', 'xls')
* @Route("/prestataire/export/{format}/", name="exportPrestaFlux", defaults={"format": "csv"})
* @IsGranted("ROLE_PRESTATAIRE")
*/
public function exportFluxAction(Request $request, $format = 'csv')
{
// Prepare the data source
$query = $this->em->getRepository(Flux::class)->getQueryByPrestataire($this->session->get('_prestagere'));
$fields = ['expediteur', 'destinataire', 'type', 'parenttype', 'montant', 'moyen', 'operateur'];
$source = new DoctrineORMQuerySourceIterator($query, $fields);
$filename = sprintf(
'export_flux_%s.%s',
date('Y_m_d_H_i_s', strtotime('now')),
$format
);
return $this->getResponse(
$format,
$filename,
$source
);
}
private function getResponse($format, $filename, SourceIteratorInterface $source)
{
switch ($format) {
case 'xls':
$writer = new XlsWriter('php://output');
$contentType = 'application/vnd.ms-excel';
break;
case 'xml':
$writer = new XmlWriter('php://output');
$contentType = 'text/xml';
break;
case 'json':
$writer = new JsonWriter('php://output');
$contentType = 'application/json';
break;
case 'csv':
$writer = new CsvWriter('php://output', ',', '"', '\\', true, true);
$contentType = 'text/csv';
break;
default:
throw new \RuntimeException('Invalid format');
}
$callback = static function () use ($source, $writer) {
$handler = Handler::create($source, $writer);
$handler->export();
};
return new StreamedResponse($callback, 200, [
'Content-Type' => $contentType,
'Content-Disposition' => sprintf('attachment; filename="%s"', $filename),
]);
}
}
......@@ -45,19 +45,19 @@
<span class="caret"></span>
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
<a class="dropdown-item" href="{{ path('exportPrestaFlux', {'format' : 'json'}) }}">
<a class="dropdown-item" href="{{ path('exportUserFlux', {'format' : 'json'}) }}">
<i class="fa fa-arrow-circle-o-down" aria-hidden="true"></i>
JSON
</a>
<a class="dropdown-item" href="{{ path('exportPrestaFlux', {'format' : 'xml'}) }}">
<a class="dropdown-item" href="{{ path('exportUserFlux', {'format' : 'xml'}) }}">
<i class="fa fa-arrow-circle-o-down" aria-hidden="true"></i>
XML
</a>
<a class="dropdown-item" href="{{ path('exportPrestaFlux', {'format' : 'csv'}) }}">
<a class="dropdown-item" href="{{ path('exportUserFlux', {'format' : 'csv'}) }}">
<i class="fa fa-arrow-circle-o-down" aria-hidden="true"></i>
CSV
</a>
<a class="dropdown-item" href="{{ path('exportPrestaFlux', {'format' : 'xls'}) }}">
<a class="dropdown-item" href="{{ path('exportUserFlux', {'format' : 'xls'}) }}">
<i class="fa fa-arrow-circle-o-down" aria-hidden="true"></i>
XLS
</a>
......
......@@ -865,6 +865,18 @@
<source>Cotiser</source>
<target>Cotiser</target>
</trans-unit>
<trans-unit id="STMXKMO" resname="Achat de monnaie locale bien effectué !">
<source>Achat de monnaie locale bien effectué !</source>
<target>Achat de monnaie locale bien effectué !</target>
</trans-unit>
<trans-unit id="dfPWdav" resname="La transaction a été annulée.">
<source>La transaction a été annulée.</source>
<target>La transaction a été annulée.</target>
</trans-unit>
<trans-unit id="NXrqKKn" resname="Export impossible.">
<source>Export impossible.</source>
<target>Export impossible.</target>
</trans-unit>
</body>
</file>
</xliff>
......@@ -881,6 +881,18 @@
<source>Cotiser</source>
<target>Cotiser</target>
</trans-unit>
<trans-unit id="STMXKMO" resname="Achat de monnaie locale bien effectué !">
<source>Achat de monnaie locale bien effectué !</source>
<target>Achat de monnaie locale bien effectué !</target>
</trans-unit>
<trans-unit id="dfPWdav" resname="La transaction a été annulée.">
<source>La transaction a été annulée.</source>
<target>La transaction a été annulée.</target>
</trans-unit>
<trans-unit id="NXrqKKn" resname="Export impossible.">
<source>Export impossible.</source>
<target>Export impossible.</target>
</trans-unit>
</body>
</file>
</xliff>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment