src/WellCommerce/Bundle/OrderBundle/EventListener/WFirmaSubscriber.php line 101

Open in your IDE?
  1. <?php
  2. declare(strict_types=0);
  3. /*
  4.  * WellCommerce Foundation
  5.  *
  6.  * This file is part of the WellCommerce package.
  7.  *
  8.  * (c) Adam Piotrowski <adam@wellcommerce.org>, Adrian Potepa <adrian@wellcommerce.org>
  9.  *
  10.  * For the full copyright and license information,
  11.  * please view the LICENSE file that was distributed with this source code.
  12.  */
  13. namespace WellCommerce\Bundle\OrderBundle\EventListener;
  14. use Psr\Container\ContainerInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use WellCommerce\Bundle\AppBundle\Entity\Client;
  17. use WellCommerce\Bundle\AppBundle\Entity\Shop;
  18. use WellCommerce\Bundle\CoreBundle\DependencyInjection\AbstractServiceSubscriber;
  19. use WellCommerce\Bundle\CoreBundle\Doctrine\Event\EntityEvent;
  20. use WellCommerce\Bundle\OrderBundle\DataSet\Admin\OrderStatusDataSet;
  21. use WellCommerce\Bundle\OrderBundle\Entity\Invoice;
  22. use WellCommerce\Bundle\OrderBundle\Entity\OrderStatus;
  23. use WellCommerce\Bundle\OrderBundle\Entity\OrderStatusHistory;
  24. use WellCommerce\Bundle\OrderBundle\Manager\InvoiceManager;
  25. use WellCommerce\Bundle\OrderBundle\Service\Invoice\Processor\InvoiceProcessorCollection;
  26. use WellCommerce\Bundle\OrderBundle\Service\Invoice\Processor\InvoiceProcessorInterface;
  27. use WellCommerce\Component\Form\Conditions\Equals;
  28. use WellCommerce\Component\Form\Event\FormEvent;
  29. /**
  30.  * Class SmsApiSubscriber
  31.  *
  32.  * @author  Adam Piotrowski <adam@wellcommerce.org>
  33.  */
  34. class WFirmaSubscriber extends AbstractServiceSubscriber implements EventSubscriberInterface
  35. {
  36.     protected OrderStatusDataSet         $dataSet;
  37.     protected InvoiceManager             $manager;
  38.     protected InvoiceProcessorCollection $collection;
  39.     public function __construct(
  40.         ContainerInterface $locator,
  41.         OrderStatusDataSet $dataSet,
  42.         InvoiceManager $manager,
  43.         InvoiceProcessorCollection $collection
  44.     ) {
  45.         parent::__construct($locator);
  46.         $this->dataSet    $dataSet;
  47.         $this->manager    $manager;
  48.         $this->collection $collection;
  49.     }
  50.     public static function getSubscribedEvents()
  51.     {
  52.         return [
  53.             'admin.shop.pre_form_init'         => ['onShopFormAdminInit'],
  54.             'admin.invoice.pre_form_init'      => ['onInvoiceFormAdminInit'],
  55.             'admin.client.pre_form_init'       => ['onClientFormAdminInit'],
  56.             'admin.invoice.post_init'          => ['onInvoicePostInit'],
  57.             'order_status_history.post_create' => ['onOrderStatusHistoryPostCreate'0],
  58.         ];
  59.     }
  60.     public function onInvoicePostInit(EntityEvent $event)
  61.     {
  62.         $entity $event->getEntity();
  63.         if ($entity instanceof Invoice) {
  64.             $entity->setWFirmaInvoiceType('normal');
  65.         }
  66.     }
  67.     public function onInvoiceFormAdminInit(FormEvent $event)
  68.     {
  69.         $resource $event->getResource();
  70.         if ($resource instanceof Invoice) {
  71.             $form    $event->getForm();
  72.             $builder $event->getFormBuilder();
  73.             $wFirmaData $form->getChildren()->get('required_data');
  74.             $wFirmaData->addChild($builder->getElement('select', [
  75.                 'name'    => 'wFirmaInvoiceType',
  76.                 'label'   => 'wfirma.label.type',
  77.                 'options' => [
  78.                     'normal'   => 'Faktura',
  79.                     'proforma' => 'Proforma',
  80.                 ],
  81.             ]));
  82.             $schemaBill $wFirmaData->addChild($builder->getElement('checkbox', [
  83.                 'name'  => 'wFirmaInvoiceSchemaBill',
  84.                 'label' => 'wfirma.label.schema_bill',
  85.             ]));
  86.         }
  87.     }
  88.     public function onClientFormAdminInit(FormEvent $event)
  89.     {
  90.         $resource $event->getResource();
  91.         if ($resource instanceof Client) {
  92.             $form    $event->getForm();
  93.             $builder $event->getFormBuilder();
  94.             $wFirmaData $form->addChild($builder->getElement('nested_fieldset', [
  95.                 'name'  => 'wfirma_data',
  96.                 'label' => 'wfirma.fieldset.settings',
  97.             ]));
  98.             $wFirmaOtherUser $wFirmaData->addChild($builder->getElement('checkbox', [
  99.                 'name'    => 'wFirmaOtherUser',
  100.                 'label'   => 'wfirma.label.other_user',
  101.                 'comment' => 'wfirma.comment.other_user',
  102.             ]));
  103.             $wFirmaData->addChild($builder->getElement('text_field', [
  104.                 'name'         => 'wFirmaApiUser',
  105.                 'label'        => 'wfirma.label.user',
  106.                 'rules'        => [
  107.                     $builder->getRule('required'),
  108.                 ],
  109.                 'dependencies' => [
  110.                     $builder->getDependency('show', [
  111.                         'form'      => $form,
  112.                         'field'     => $wFirmaOtherUser,
  113.                         'condition' => new Equals(1),
  114.                     ]),
  115.                 ],
  116.             ]));
  117.             $wFirmaData->addChild($builder->getElement('password', [
  118.                 'name'         => 'wFirmaApiPass',
  119.                 'label'        => 'wfirma.label.pass',
  120.                 'rules'        => [
  121.                     $builder->getRule('required'),
  122.                 ],
  123.                 'dependencies' => [
  124.                     $builder->getDependency('show', [
  125.                         'form'      => $form,
  126.                         'field'     => $wFirmaOtherUser,
  127.                         'condition' => new Equals(1),
  128.                     ]),
  129.                 ],
  130.             ]));
  131.             $wFirmaData->addChild($builder->getElement('text_field', [
  132.                 'name'         => 'wFirmaAccessKey',
  133.                 'label'        => 'Access key',
  134.                 'rules'        => [
  135.                     $builder->getRule('required'),
  136.                 ],
  137.                 'dependencies' => [
  138.                     $builder->getDependency('show', [
  139.                         'form'      => $form,
  140.                         'field'     => $wFirmaOtherUser,
  141.                         'condition' => new Equals(1),
  142.                     ]),
  143.                 ],
  144.             ]));
  145.             $wFirmaData->addChild($builder->getElement('text_field', [
  146.                 'name'         => 'wFirmaSecretKey',
  147.                 'label'        => 'Secret key',
  148.                 'rules'        => [
  149.                     $builder->getRule('required'),
  150.                 ],
  151.                 'dependencies' => [
  152.                     $builder->getDependency('show', [
  153.                         'form'      => $form,
  154.                         'field'     => $wFirmaOtherUser,
  155.                         'condition' => new Equals(1),
  156.                     ]),
  157.                 ],
  158.             ]));
  159.             $wFirmaData->addChild($builder->getElement('text_field', [
  160.                 'name'         => 'wFirmaAppKey',
  161.                 'label'        => 'App key',
  162.                 'rules'        => [
  163.                     $builder->getRule('required'),
  164.                 ],
  165.                 'dependencies' => [
  166.                     $builder->getDependency('show', [
  167.                         'form'      => $form,
  168.                         'field'     => $wFirmaOtherUser,
  169.                         'condition' => new Equals(1),
  170.                     ]),
  171.                 ],
  172.             ]));
  173.         }
  174.     }
  175.     public function onShopFormAdminInit(FormEvent $event)
  176.     {
  177.         $resource $event->getResource();
  178.         if ($resource instanceof Shop) {
  179.             $form    $event->getForm();
  180.             $builder $event->getFormBuilder();
  181.             $fieldset $form->getChildren()->get('invoice_data');
  182.             $wFirmaData $fieldset->addChild($builder->getElement('nested_fieldset', [
  183.                 'name'  => 'wfirma_data',
  184.                 'label' => 'wfirma.fieldset.settings',
  185.             ]));
  186.             $wFirmaData->addChild($builder->getElement('text_field', [
  187.                 'name'  => 'wFirmaApiUser',
  188.                 'label' => 'wfirma.label.user',
  189.             ]));
  190.             $wFirmaData->addChild($builder->getElement('password', [
  191.                 'name'  => 'wFirmaApiPass',
  192.                 'label' => 'wfirma.label.pass',
  193.             ]));
  194.             $wFirmaData->addChild($builder->getElement('password', [
  195.                 'name'  => 'wFirmaAccessKey',
  196.                 'label' => 'Access key',
  197.             ]));
  198.             $wFirmaData->addChild($builder->getElement('password', [
  199.                 'name'  => 'wFirmaSecretKey',
  200.                 'label' => 'Secret key',
  201.             ]));
  202.             $wFirmaData->addChild($builder->getElement('password', [
  203.                 'name'  => 'wFirmaAppKey',
  204.                 'label' => 'App key',
  205.             ]));
  206.             $wFirmaData->addChild($builder->getElement('text_field', [
  207.                 'name'  => 'wFirmaApiSeriesNumberNormal',
  208.                 'label' => 'wfirma.label.series_number_normal',
  209.             ]));
  210.             $wFirmaData->addChild($builder->getElement('text_field', [
  211.                 'name'  => 'wFirmaApiSeriesNumberProforma',
  212.                 'label' => 'wfirma.label.series_number_proforma',
  213.             ]));
  214.             $wFirmaData->addChild($builder->getElement('text_field', [
  215.                 'name'  => 'wFirmaWarehouseId',
  216.                 'label' => 'wfirma.label.warehouse_id',
  217.             ]));
  218.             $wFirmaData->addChild($builder->getElement('checkbox', [
  219.                 'name'  => 'wFirmaApiSyncStock',
  220.                 'label' => 'wfirma.label.sync_stock',
  221.             ]));
  222.             $wFirmaData->addChild($builder->getElement('checkbox', [
  223.                 'name'  => 'wFirmaApiSyncPrice',
  224.                 'label' => 'wfirma.label.sync_price',
  225.             ]));
  226.             $wFirmaData->addChild($builder->getElement('checkbox', [
  227.                 'name'  => 'wFirmaApiAutoSend',
  228.                 'label' => 'wfirma.label.auto_send',
  229.             ]));
  230.             $wFirmaData->addChild($builder->getElement('select', [
  231.                 'name'    => 'wFirmaPriceType',
  232.                 'label'   => 'wfirma.label.price_type',
  233.                 'options' => [
  234.                     'netto'  => 'netto',
  235.                     'brutto' => 'brutto',
  236.                 ],
  237.             ]));
  238.             $orderStatuses $this->dataSet->getResult('select', [], ['default_option' => '---']);
  239.             $wFirmaData->addChild($builder->getElement('select', [
  240.                 'name'        => 'wFirmaOrderStatusNormal',
  241.                 'label'       => 'wfirma.label.order_status_normal',
  242.                 'options'     => $orderStatuses,
  243.                 'transformer' => $builder->getRepositoryTransformer('entity'OrderStatus::class),
  244.             ]));
  245.             $wFirmaData->addChild($builder->getElement('select', [
  246.                 'name'        => 'wFirmaOrderStatusProforma',
  247.                 'label'       => 'wfirma.label.order_status_proforma',
  248.                 'options'     => $orderStatuses,
  249.                 'transformer' => $builder->getRepositoryTransformer('entity'OrderStatus::class),
  250.             ]));
  251.             $wFirmaData->addChild($builder->getElement('select', [
  252.                 'name'        => 'wFirmaAutoStatusNormal',
  253.                 'label'       => 'wfirma.label.auto_status_normal',
  254.                 'options'     => $orderStatuses,
  255.                 'transformer' => $builder->getRepositoryTransformer('entity'OrderStatus::class),
  256.             ]));
  257.         }
  258.     }
  259.     public function onOrderStatusHistoryPostCreate(EntityEvent $event)
  260.     {
  261.         $history $event->getEntity();
  262.         if ($history instanceof OrderStatusHistory) {
  263.             $order        $history->getOrder();
  264.             $shop         $order->getShop();
  265.             $status       $history->getOrderStatus();
  266.             $wFirmaStatus $shop->getWFirmaAutoStatusNormal();
  267.             if ($wFirmaStatus instanceof OrderStatus) {
  268.                 if ($status->getId() === $wFirmaStatus->getId()) {
  269.                     if ($order->getInvoices()->count() === 0) {
  270.                         $invoice $this->manager->prepareInvoiceForOrder($order);
  271.                         $invoice->setWFirmaInvoiceType('normal');
  272.                         $this->manager->createResource($invoice);
  273.                     }
  274.                 }
  275.             }
  276.         }
  277.     }
  278. }