<?php
/**
* @author Thomas HERISSON (contact@scaledev.fr)
* @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
* @license commercial
*/
declare(strict_types=1);
namespace Bluue\AccountingBundle\EventSubscriber\CustomersBundle;
use Bluue\AccountingBundle\Entity\CustomerAccount;
use Bluue\AccountingBundle\Form\Type\AccountChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Security\Core\Security;
use Bluue\CustomersBundle\Event\CustomerEditEvent;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Bluue\AccountingBundle\Repository\CustomerAccountRepository;
use Doctrine\ORM\EntityManagerInterface;
class CustomerEditSubscriber implements EventSubscriberInterface
{
/**
* @var TranslatorInterface
*/
private TranslatorInterface $tr;
/**
* @var RequestStack
*/
protected RequestStack $requestStack;
/**
* @var CustomerAccountRepository
*/
private CustomerAccountRepository $customerAccountRepo;
/**
* @var EntityManagerInterface
*/
private EntityManagerInterface $em;
/**
* @var bool
*/
private bool $isGranted;
/**
* @param RequestStack $requestStack
* @param TranslatorInterface $tr
* @param Security $security
* @param CustomerAccountRepository $customerAccountRepo
* @param EntityManagerInterface $em
*/
public function __construct(
RequestStack $requestStack,
TranslatorInterface $tr,
Security $security,
CustomerAccountRepository $customerAccountRepo,
EntityManagerInterface $em
) {
$this->requestStack = $requestStack;
$this->tr = $tr;
$this->customerAccountRepo = $customerAccountRepo;
$this->em = $em;
$this->isGranted = $security->isGranted('ROLE__ACCOUNTING__READ_ONLY');
}
/**
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [
CustomerEditEvent::PRE_SET_DATA => 'addFields',
CustomerEditEvent::TABS => 'tabs',
CustomerEditEvent::POST_SUBMIT => 'postSubmit'
];
}
/**
* @param CustomerEditEvent $event
* @return void
*/
public function addFields(CustomerEditEvent $event): void
{
$customer = $event->getCustomer();
if (!$this->isGranted || $customer->isContact()) {
return;
}
$builder = $event->getBuilder();
$accountNumber = $auxiliaryNumber = null;
$customerAccount = $this->customerAccountRepo->findOneBy(['customer' => $customer]);
if ($customerAccount) {
$accountNumber = $customerAccount->getNumber();
$auxiliaryNumber = $customerAccount->getAuxiliaryNumber();
}
$builder
->add('customer_account_number', AccountChoiceType::class, [
'label' => $this->tr->trans('Sub-account', [], 'AccountingBundle'),
'mapped' => false,
'data' => $accountNumber
])
->add('customer_auxiliary_number', TextType::class, [
'required' => false,
'label' => $this->tr->trans('Auxiliary account', [], 'AccountingBundle'),
'mapped' => false,
'data' => $auxiliaryNumber,
'attr' => [
'maxlength' => 32
]
]);
$event->setBuilder($builder);
}
/**
* @param CustomerEditEvent $event
* @return void
*/
public function tabs(CustomerEditEvent $event): void
{
$customer = $event->getCustomer();
if (!$this->isGranted || $customer->isContact()) {
return;
}
$tabs = $event->getTabs();
$newTabs = [
[
'template' => '@Accounting/CustomersBundle/_form_account.html.twig',
'name' => $this->tr->trans('Accounting', [], 'AccountingBundle'),
'icon' => 'fas fa-calculator',
'key' => 'customer-accounting'
]
];
$event->setTabs(array_merge($tabs, $newTabs));
}
/**
* @param CustomerEditEvent $event
* @return void
*/
public function postSubmit(CustomerEditEvent $event): void
{
$customer = $event->getCustomer();
if (!$this->isGranted || $customer->isContact()) {
return;
}
$form = $event->getForm();
$customerAccount = $this->customerAccountRepo->findOneBy(['customer' => $customer]);
if (!$customerAccount) {
$customerAccount = new CustomerAccount();
$customerAccount->setCustomer($customer);
}
$accountNumber = $form->get('customer_account_number')->getData();
if ($accountNumber) {
$accountNumber = (string) $accountNumber;
} else {
$accountNumber = null;
}
$customerAccount->setNumber($accountNumber);
$auxiliaryNumber = $form->get('customer_auxiliary_number')->getData();
if ($auxiliaryNumber) {
$auxiliaryNumber = (string) $auxiliaryNumber;
} else {
$auxiliaryNumber = null;
}
$customerAccount->setAuxiliaryNumber($auxiliaryNumber);
$this->em->persist($customerAccount);
}
}