vendor/bluue/customer-outstanding-bundle/src/EventSubscriber/CustomerEditSubscriber.php line 143

Open in your IDE?
  1. <?php
  2. /**
  3.  * @author Thomas HERISSON (contact@scaledev.fr)
  4.  * @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
  5.  * @license commercial
  6.  */
  7. declare(strict_types=1);
  8. namespace Bluue\CustomerOutstandingBundle\EventSubscriber;
  9. use App\Services\Configuration;
  10. use Bluue\CustomerOutstandingBundle\Entity\CustomerOutstandingEntity;
  11. use Symfony\Component\Security\Core\Security;
  12. use Bluue\CustomersBundle\Event\CustomerEditEvent;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Contracts\Translation\TranslatorInterface;
  15. use Symfony\Component\Form\Extension\Core\Type\NumberType;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Bluue\CustomerOutstandingBundle\Repository\CustomerOutstandingRepository;
  18. use Doctrine\ORM\EntityManagerInterface;
  19. class CustomerEditSubscriber implements EventSubscriberInterface
  20. {
  21.     /**
  22.      * @var TranslatorInterface
  23.      */
  24.     private TranslatorInterface $tr;
  25.     /**
  26.      * @var RequestStack
  27.      */
  28.     protected RequestStack $requestStack;
  29.     /**
  30.      * @var CustomerOutstandingRepository
  31.      */
  32.     private CustomerOutstandingRepository $customerOutstandingRepo;
  33.     /**
  34.      * @var EntityManagerInterface
  35.      */
  36.     private EntityManagerInterface $em;
  37.     /**
  38.      * @var bool
  39.      */
  40.     private bool $isGranted;
  41.     /**
  42.      * @var mixed|null
  43.      */
  44.     private $customer_outstanding_bundle__enabled;
  45.     /**
  46.      * @param RequestStack $requestStack
  47.      * @param TranslatorInterface $tr
  48.      * @param Security $security
  49.      * @param CustomerOutstandingRepository $customerOutstandingRepo
  50.      * @param Configuration $configService
  51.      * @param EntityManagerInterface $em
  52.      */
  53.     public function __construct(
  54.         RequestStack $requestStack,
  55.         TranslatorInterface $tr,
  56.         Security $security,
  57.         CustomerOutstandingRepository $customerOutstandingRepo,
  58.         Configuration $configService,
  59.         EntityManagerInterface $em
  60.     ) {
  61.         $this->requestStack $requestStack;
  62.         $this->tr $tr;
  63.         $this->customerOutstandingRepo $customerOutstandingRepo;
  64.         $this->em $em;
  65.         $this->customer_outstanding_bundle__enabled $configService->get(
  66.             'customer_outstanding_bundle__enabled'
  67.         );
  68.         $this->isGranted $security->isGranted('ROLE__CUSTOMER_OUTSTANDING__READ_ONLY');
  69.     }
  70.     /**
  71.      * @return string[]
  72.      */
  73.     public static function getSubscribedEvents(): array
  74.     {
  75.         return [
  76.             CustomerEditEvent::PRE_SET_DATA => 'addFields',
  77.             CustomerEditEvent::TABS => 'tabs',
  78.             CustomerEditEvent::POST_SUBMIT => 'postSubmit'
  79.         ];
  80.     }
  81.     /**
  82.      * @param CustomerEditEvent $event
  83.      * @return void
  84.      */
  85.     public function addFields(CustomerEditEvent $event): void
  86.     {
  87.         $customer $event->getCustomer();
  88.         if (!$this->isGranted || !$this->customer_outstanding_bundle__enabled || $customer->getParent()) {
  89.             return;
  90.         }
  91.         $builder $event->getBuilder();
  92.         $amount null;
  93.         $customer_outstanding $this->customerOutstandingRepo->findOneBy(['customer' => $customer]);
  94.         if ($customer_outstanding) {
  95.             $amount $customer_outstanding->getAmount();
  96.         }
  97.         $builder->add(
  98.             'customer_outstanding_amount',
  99.             NumberType::class,
  100.             [
  101.                 'html5' => true,
  102.                 'required' => false,
  103.                 'scale' => 2,
  104.                 'attr' => [
  105.                     'step' => 'any',
  106.                     'min' => 0,
  107.                     'class' => 'removeUselessDecimals'
  108.                 ],
  109.                 'label' => $this->tr->trans(
  110.                     'Customer Outstanding Amount Max',
  111.                     [],
  112.                     'CustomerOutstandingBundle'
  113.                 ),
  114.                 'mapped' => false,
  115.                 'data' => $amount
  116.             ]
  117.         );
  118.         $event->setBuilder($builder);
  119.     }
  120.     /**
  121.      * @param CustomerEditEvent $event
  122.      * @return void
  123.      */
  124.     public function tabs(CustomerEditEvent $event): void
  125.     {
  126.         $customer $event->getCustomer();
  127.         if (!$this->isGranted || !$this->customer_outstanding_bundle__enabled || $customer->getParent()) {
  128.             return;
  129.         }
  130.         $tabs $event->getTabs();
  131.         $newTabs = [
  132.             [
  133.                 'template' => '@CustomerOutstanding/CustomersBundle/form_customer_options.html.twig',
  134.                 'name' => $this->tr->trans('Customer Outstanding', [], 'CustomerOutstandingBundle'),
  135.                 'icon' => 'fas fa-hand-holding-usd',
  136.                 'key' => 'customer-outstanding'
  137.             ]
  138.         ];
  139.         $event->setTabs(array_merge($tabs$newTabs));
  140.     }
  141.     /**
  142.      * @param CustomerEditEvent $event
  143.      * @return void
  144.      */
  145.     public function postSubmit(CustomerEditEvent $event): void
  146.     {
  147.         $customer $event->getCustomer();
  148.         if (!$this->isGranted || !$this->customer_outstanding_bundle__enabled || $customer->getParent()) {
  149.             return;
  150.         }
  151.         $form $event->getForm();
  152.         $customer_outstanding $this->customerOutstandingRepo->findOneBy(['customer' => $customer]);
  153.         if (!$customer_outstanding) {
  154.             $customer_outstanding = new CustomerOutstandingEntity();
  155.             $customer_outstanding->setCustomer($customer);
  156.         }
  157.         $amount $form->get('customer_outstanding_amount')->getData();
  158.         if ($amount) {
  159.             $amount = (string) $amount;
  160.         } else {
  161.             $amount null;
  162.         }
  163.         $customer_outstanding->setAmount($amount);
  164.         $this->em->persist($customer_outstanding);
  165.     }
  166. }