<?php
/**
* @author Quentin CHATELAIN (contact@scaledev.fr)
* @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
* @license commercial
*/
declare(strict_types=1);
namespace Bluue\GocardlessBundle\EventSubscriber\SalesBundle;
use Bluue\GocardlessBundle\Repository\CustomerMappingRepository;
use Bluue\GocardlessBundle\Repository\DirectDebitRepository;
use Bluue\SalesBundle\Event\DocumentTabsEvent;
use Bluue\SalesBundle\Repository\InvoiceRepository;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class DocumentTabsEventSubscriber implements EventSubscriberInterface
{
/**
* @var TranslatorInterface
*/
private TranslatorInterface $tr;
/**
* @var RequestStack
*/
private RequestStack $requestStack;
/**
* @var CustomerMappingRepository
*/
private CustomerMappingRepository $customerMappingRepo;
/**
* @var InvoiceRepository
*/
private InvoiceRepository $invoiceRepo;
/**
* @var DirectDebitRepository
*/
private DirectDebitRepository $directDebitRepo;
/**
* @var Security
*/
private Security $security;
/**
* @param TranslatorInterface $tr
* @param RequestStack $requestStack
* @param CustomerMappingRepository $customerMappingRepo
* @param InvoiceRepository $invoiceRepo
* @param DirectDebitRepository $directDebitRepo
* @param Security $security
*/
public function __construct(
TranslatorInterface $tr,
RequestStack $requestStack,
CustomerMappingRepository $customerMappingRepo,
InvoiceRepository $invoiceRepo,
DirectDebitRepository $directDebitRepo,
Security $security
) {
$this->tr = $tr;
$this->requestStack = $requestStack;
$this->customerMappingRepo = $customerMappingRepo;
$this->invoiceRepo = $invoiceRepo;
$this->directDebitRepo = $directDebitRepo;
$this->security = $security;
}
/**
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [
DocumentTabsEvent::INVOICE_TABS => 'invoiceTabs'
];
}
/**
* @param DocumentTabsEvent $event
* @return void
*/
public function invoiceTabs(DocumentTabsEvent $event)
{
if (!$this->security->isGranted('ROLE__GOCARDLESS__READ_ONLY')) {
return;
}
$invoiceId = $this->requestStack->getMainRequest()->attributes->get('id');
$invoice = $this->invoiceRepo->find($invoiceId);
$customerMapping = $this->customerMappingRepo->findOneBy([
'customer' => $invoice->getCustomer()
]);
if (!$customerMapping) {
return;
}
$directDebits = $this->directDebitRepo->count([
'invoice' => $invoice
]);
$badge = '';
if ($directDebits) {
$badge = '<span class="badge badge-pill badge-info ml-1">' . $directDebits . '</span>';
}
$event->addTab([
'key' => 'gocardless_bundle__direct_debits',
'name' => $this->tr->trans('Direct debits', [], 'GocardlessBundle') . $badge,
'icon' => 'far fa-credit-card',
'idRequired' => true,
'path' => 'gocardless_bundle__invoice_direct_debits',
'layout' => 'view'
]);
}
}