<?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\CustomerOutstandingBundle\EventSubscriber;
use App\Services\Configuration;
use Bluue\CustomerOutstandingBundle\Entity\CustomerOutstandingEntity;
use Symfony\Component\Security\Core\Security;
use Bluue\CustomersBundle\Event\CustomerEditEvent;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Bluue\CustomerOutstandingBundle\Repository\CustomerOutstandingRepository;
use Doctrine\ORM\EntityManagerInterface;
class CustomerEditSubscriber implements EventSubscriberInterface
{
/**
* @var TranslatorInterface
*/
private TranslatorInterface $tr;
/**
* @var RequestStack
*/
protected RequestStack $requestStack;
/**
* @var CustomerOutstandingRepository
*/
private CustomerOutstandingRepository $customerOutstandingRepo;
/**
* @var EntityManagerInterface
*/
private EntityManagerInterface $em;
/**
* @var bool
*/
private bool $isGranted;
/**
* @var mixed|null
*/
private $customer_outstanding_bundle__enabled;
/**
* @param RequestStack $requestStack
* @param TranslatorInterface $tr
* @param Security $security
* @param CustomerOutstandingRepository $customerOutstandingRepo
* @param Configuration $configService
* @param EntityManagerInterface $em
*/
public function __construct(
RequestStack $requestStack,
TranslatorInterface $tr,
Security $security,
CustomerOutstandingRepository $customerOutstandingRepo,
Configuration $configService,
EntityManagerInterface $em
) {
$this->requestStack = $requestStack;
$this->tr = $tr;
$this->customerOutstandingRepo = $customerOutstandingRepo;
$this->em = $em;
$this->customer_outstanding_bundle__enabled = $configService->get(
'customer_outstanding_bundle__enabled'
);
$this->isGranted = $security->isGranted('ROLE__CUSTOMER_OUTSTANDING__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 || !$this->customer_outstanding_bundle__enabled || $customer->getParent()) {
return;
}
$builder = $event->getBuilder();
$amount = null;
$customer_outstanding = $this->customerOutstandingRepo->findOneBy(['customer' => $customer]);
if ($customer_outstanding) {
$amount = $customer_outstanding->getAmount();
}
$builder->add(
'customer_outstanding_amount',
NumberType::class,
[
'html5' => true,
'required' => false,
'scale' => 2,
'attr' => [
'step' => 'any',
'min' => 0,
'class' => 'removeUselessDecimals'
],
'label' => $this->tr->trans(
'Customer Outstanding Amount Max',
[],
'CustomerOutstandingBundle'
),
'mapped' => false,
'data' => $amount
]
);
$event->setBuilder($builder);
}
/**
* @param CustomerEditEvent $event
* @return void
*/
public function tabs(CustomerEditEvent $event): void
{
$customer = $event->getCustomer();
if (!$this->isGranted || !$this->customer_outstanding_bundle__enabled || $customer->getParent()) {
return;
}
$tabs = $event->getTabs();
$newTabs = [
[
'template' => '@CustomerOutstanding/CustomersBundle/form_customer_options.html.twig',
'name' => $this->tr->trans('Customer Outstanding', [], 'CustomerOutstandingBundle'),
'icon' => 'fas fa-hand-holding-usd',
'key' => 'customer-outstanding'
]
];
$event->setTabs(array_merge($tabs, $newTabs));
}
/**
* @param CustomerEditEvent $event
* @return void
*/
public function postSubmit(CustomerEditEvent $event): void
{
$customer = $event->getCustomer();
if (!$this->isGranted || !$this->customer_outstanding_bundle__enabled || $customer->getParent()) {
return;
}
$form = $event->getForm();
$customer_outstanding = $this->customerOutstandingRepo->findOneBy(['customer' => $customer]);
if (!$customer_outstanding) {
$customer_outstanding = new CustomerOutstandingEntity();
$customer_outstanding->setCustomer($customer);
}
$amount = $form->get('customer_outstanding_amount')->getData();
if ($amount) {
$amount = (string) $amount;
} else {
$amount = null;
}
$customer_outstanding->setAmount($amount);
$this->em->persist($customer_outstanding);
}
}