Commit 557bb863 by Yvon

SEPA file generation for all on going reconversions

parent 6e65ae81
......@@ -66,3 +66,6 @@ templates/themes/custom/
#INTELLIJ
.idea/
#dir containg exported reconversions xml
/reconversions/
......@@ -14,4 +14,5 @@ twig:
tav_env: '%env(TAV_ENV)%'
presta_self_init_and_eval: '%env(PRESTA_SELF_INIT_AND_EVAL)%'
presta_extra_data: '%env(PRESTA_EXTRA_DATA)%'
household_based_allowance: '%env(HOUSEHOLD_BASED_ALLOWANCE)%'
\ No newline at end of file
household_based_allowance: '%env(HOUSEHOLD_BASED_ALLOWANCE)%'
automatisation_reconversion: '%env(AUTOMATISATION_RECONVERSION)%'
\ No newline at end of file
......@@ -96,4 +96,8 @@ class ReconversionAdmin extends FluxAdmin
'Reconverti ?' => 'reconverti',
];
}
public function isReconversionAdmin() {
return true;
}
}
......@@ -2,11 +2,13 @@
namespace App\Controller;
use AndrewSvirin\Ebics\Builders\CustomerCreditTransfer\CustomerCreditTransferBuilder;
use App\Entity\Adherent;
use App\Entity\Don;
use App\Entity\Flux;
use App\Entity\Groupe;
use App\Entity\Prestataire;
use App\Entity\Reconversion;
use App\Entity\User;
use App\Enum\CurrencyEnum;
use App\Flux\FluxInterface;
......@@ -16,6 +18,7 @@ use App\Utils\TAVCotisationUtils;
use FOS\UserBundle\Model\UserManagerInterface;
use FOS\UserBundle\Util\TokenGeneratorInterface;
use Gamez\Symfony\Component\Serializer\Normalizer\UuidNormalizer;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Sonata\Exporter\Handler;
use Sonata\Exporter\Source\DoctrineORMQuerySourceIterator;
use Sonata\Exporter\Writer\CsvWriter;
......@@ -30,6 +33,7 @@ use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
......@@ -75,6 +79,7 @@ class FluxController extends AbstractController
protected $tokenGenerator;
protected $validator;
protected $userManager;
protected $router;
public function __construct(
Security $security,
......@@ -88,7 +93,8 @@ class FluxController extends AbstractController
TokenGeneratorInterface $tokenGenerator,
ValidatorInterface $validator,
CsrfTokenManagerInterface $tokenManager,
UserManagerInterface $userManager
UserManagerInterface $userManager,
RouterInterface $router
) {
$this->security = $security;
$this->em = $em;
......@@ -102,6 +108,7 @@ class FluxController extends AbstractController
$this->tokenManager = $tokenManager;
$this->tavCotisationsUtils = $tavCotisationsUtils;
$this->userManager = $userManager;
$this->router = $router;
}
protected function manageFluxForm(Request $request, Form $form, $template = '@kohinos/flux/transaction.html.twig', $params = [])
......@@ -171,6 +178,96 @@ class FluxController extends AbstractController
}
/**
* @param Request $request
* @Route("/credit-transfer-file", name="credit_transfer_file")
* @IsGranted({"ROLE_TRESORIER", "ROLE_SUPER_ADMIN"})
* @return Response
*/
public function creditTransferFileAction(Request $request)
{
//fetch mlc prestataire and make sure bic, iban are not empty
$mlcPrestataire = $this->em->getRepository(Prestataire::class)->findOneBy(['mlc' => true]);
if(!$mlcPrestataire->getIban() || !$mlcPrestataire->getBic()) {
$this->addFlash(
'sonata_flash_error',
"Opération annulée car l'IBAN ou le BIC du prestataire mlc est vide."
);
return $this->redirect($this->router->generate('index') . 'admin/app/reconversion/list');
}
//SEPA
$builder = new CustomerCreditTransferBuilder();
$customerCreditTransfer = $builder
->createInstance(
$mlcPrestataire->getBic(),
$mlcPrestataire->getIban(),
$mlcPrestataire->getRaison()
);
$reconversions = $this->em->getRepository(Reconversion::class)->findBy(["reconverti" => false]);
if(!$reconversions) {
$this->addFlash(
'sonata_flash_error',
"Aucune reconversion à traiter."
);
return $this->redirect($this->router->generate('index') . 'admin/app/reconversion/list');
}
foreach($reconversions as $r) {
/* @var Reconversion $r */
/* @var Prestataire $presta */
$presta = $r->getExpediteur();
if(!$presta || !$presta->isEnabled()) {
//fail ; do not flush : we don't want to toggle reconverti when file can't be generated
$this->addFlash(
'sonata_flash_error',
"Opération annulée car le prestataire " . $presta->getRaison() . " est désactivé ou introuvable."
);
return $this->redirect($this->router->generate('index') . 'admin/app/reconversion/list');
}
if(!$presta->getIban() || !$presta->getBic()) {
//fail ; do not flush : we don't want to toggle reconverti when file can't be generated
$this->addFlash(
'sonata_flash_error',
"Opération annulée car l'IBAN ou le BIC du prestataire " . $presta->getRaison() . " est vide."
);
return $this->redirect($this->router->generate('index') . 'admin/app/reconversion/list');
}
$customerCreditTransfer
->addTransaction(
$presta->getBic(),
$presta->getIban(),
$presta->getRaison(),
$r->getMontant(),
'EUR',
'Reconversion MonA vers EUR'
);
$r->setReconverti(true);
}
$filename = sprintf(
'credit_transfer_file_%s.%s',
date('Y_m_d_H_i_s', strtotime('now')),
'xml'
);
//projectDir is composer.json
$dir = $this->getParameter('kernel.project_dir') . "/reconversions";
if (!is_dir($dir)) {
mkdir($dir, 0777, true);
}
$path = $dir . "/" . $filename;
$customerCreditTransfer->popInstance()->save($path);
$this->em->flush();
return $this->file($path);
}
/**
* Export all operations for a user role.
*
* @param Request $request Request
......
......@@ -33,6 +33,12 @@
</li>
{% endfor %}
</ul>
{% if tav_env and automatisation_reconversion and admin.isReconversionAdmin is defined and admin.isReconversionAdmin %}
<a href="{{ path('credit_transfer_file') }}" type="button" class="btn btn-default">
<i class="fa fa-share-square-o" aria-hidden="true"></i>
Générer SEPA
</a>
{% endif %}
</div>
</div>
{% if admin.total is defined and admin.total > 0 %}
......
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