vendor/bluue/accounting-bundle/src/EventSubscriber/CustomersBundle/CustomerEditSubscriber.php line 128

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\AccountingBundle\EventSubscriber\CustomersBundle;
  9. use Bluue\AccountingBundle\Entity\CustomerAccount;
  10. use Bluue\AccountingBundle\Form\Type\AccountChoiceType;
  11. use Symfony\Component\Form\Extension\Core\Type\TextType;
  12. use Symfony\Component\Security\Core\Security;
  13. use Bluue\CustomersBundle\Event\CustomerEditEvent;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Bluue\AccountingBundle\Repository\CustomerAccountRepository;
  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 CustomerAccountRepository
  31.      */
  32.     private CustomerAccountRepository $customerAccountRepo;
  33.     /**
  34.      * @var EntityManagerInterface
  35.      */
  36.     private EntityManagerInterface $em;
  37.     /**
  38.      * @var bool
  39.      */
  40.     private bool $isGranted;
  41.     /**
  42.      * @param RequestStack $requestStack
  43.      * @param TranslatorInterface $tr
  44.      * @param Security $security
  45.      * @param CustomerAccountRepository $customerAccountRepo
  46.      * @param EntityManagerInterface $em
  47.      */
  48.     public function __construct(
  49.         RequestStack $requestStack,
  50.         TranslatorInterface $tr,
  51.         Security $security,
  52.         CustomerAccountRepository $customerAccountRepo,
  53.         EntityManagerInterface $em
  54.     ) {
  55.         $this->requestStack $requestStack;
  56.         $this->tr $tr;
  57.         $this->customerAccountRepo $customerAccountRepo;
  58.         $this->em $em;
  59.         $this->isGranted $security->isGranted('ROLE__ACCOUNTING__READ_ONLY');
  60.     }
  61.     /**
  62.      * @return string[]
  63.      */
  64.     public static function getSubscribedEvents(): array
  65.     {
  66.         return [
  67.             CustomerEditEvent::PRE_SET_DATA => 'addFields',
  68.             CustomerEditEvent::TABS => 'tabs',
  69.             CustomerEditEvent::POST_SUBMIT => 'postSubmit'
  70.         ];
  71.     }
  72.     /**
  73.      * @param CustomerEditEvent $event
  74.      * @return void
  75.      */
  76.     public function addFields(CustomerEditEvent $event): void
  77.     {
  78.         $customer $event->getCustomer();
  79.         if (!$this->isGranted || $customer->isContact()) {
  80.             return;
  81.         }
  82.         $builder $event->getBuilder();
  83.         $accountNumber $auxiliaryNumber null;
  84.         $customerAccount $this->customerAccountRepo->findOneBy(['customer' => $customer]);
  85.         if ($customerAccount) {
  86.             $accountNumber $customerAccount->getNumber();
  87.             $auxiliaryNumber $customerAccount->getAuxiliaryNumber();
  88.         }
  89.         $builder
  90.             ->add('customer_account_number'AccountChoiceType::class, [
  91.                 'label' => $this->tr->trans('Sub-account', [], 'AccountingBundle'),
  92.                 'mapped' => false,
  93.                 'data' => $accountNumber
  94.             ])
  95.             ->add('customer_auxiliary_number'TextType::class, [
  96.                 'required' => false,
  97.                 'label' => $this->tr->trans('Auxiliary account', [], 'AccountingBundle'),
  98.                 'mapped' => false,
  99.                 'data' => $auxiliaryNumber,
  100.                 'attr' => [
  101.                     'maxlength' => 32
  102.                 ]
  103.             ]);
  104.         $event->setBuilder($builder);
  105.     }
  106.     /**
  107.      * @param CustomerEditEvent $event
  108.      * @return void
  109.      */
  110.     public function tabs(CustomerEditEvent $event): void
  111.     {
  112.         $customer $event->getCustomer();
  113.         if (!$this->isGranted || $customer->isContact()) {
  114.             return;
  115.         }
  116.         $tabs $event->getTabs();
  117.         $newTabs = [
  118.             [
  119.                 'template' => '@Accounting/CustomersBundle/_form_account.html.twig',
  120.                 'name' => $this->tr->trans('Accounting', [], 'AccountingBundle'),
  121.                 'icon' => 'fas fa-calculator',
  122.                 'key' => 'customer-accounting'
  123.             ]
  124.         ];
  125.         $event->setTabs(array_merge($tabs$newTabs));
  126.     }
  127.     /**
  128.      * @param CustomerEditEvent $event
  129.      * @return void
  130.      */
  131.     public function postSubmit(CustomerEditEvent $event): void
  132.     {
  133.         $customer $event->getCustomer();
  134.         if (!$this->isGranted || $customer->isContact()) {
  135.             return;
  136.         }
  137.         $form $event->getForm();
  138.         $customerAccount $this->customerAccountRepo->findOneBy(['customer' => $customer]);
  139.         if (!$customerAccount) {
  140.             $customerAccount = new CustomerAccount();
  141.             $customerAccount->setCustomer($customer);
  142.         }
  143.         $accountNumber $form->get('customer_account_number')->getData();
  144.         if ($accountNumber) {
  145.             $accountNumber = (string) $accountNumber;
  146.         } else {
  147.             $accountNumber null;
  148.         }
  149.         $customerAccount->setNumber($accountNumber);
  150.         $auxiliaryNumber $form->get('customer_auxiliary_number')->getData();
  151.         if ($auxiliaryNumber) {
  152.             $auxiliaryNumber = (string) $auxiliaryNumber;
  153.         } else {
  154.             $auxiliaryNumber null;
  155.         }
  156.         $customerAccount->setAuxiliaryNumber($auxiliaryNumber);
  157.         $this->em->persist($customerAccount);
  158.     }
  159. }