<?php
declare(strict_types=0);
/*
* WellCommerce Foundation
*
* This file is part of the WellCommerce package.
*
* (c) Adam Piotrowski <adam@wellcommerce.org>, Adrian Potepa <adrian@wellcommerce.org>
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code.
*/
namespace WellCommerce\Bundle\OrderBundle\EventListener;
use Psr\Container\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use WellCommerce\Bundle\AppBundle\Entity\Client;
use WellCommerce\Bundle\AppBundle\Entity\Shop;
use WellCommerce\Bundle\CoreBundle\DependencyInjection\AbstractServiceSubscriber;
use WellCommerce\Bundle\CoreBundle\Doctrine\Event\EntityEvent;
use WellCommerce\Bundle\OrderBundle\DataSet\Admin\OrderStatusDataSet;
use WellCommerce\Bundle\OrderBundle\Entity\Invoice;
use WellCommerce\Bundle\OrderBundle\Entity\OrderStatus;
use WellCommerce\Bundle\OrderBundle\Entity\OrderStatusHistory;
use WellCommerce\Bundle\OrderBundle\Manager\InvoiceManager;
use WellCommerce\Bundle\OrderBundle\Service\Invoice\Processor\InvoiceProcessorCollection;
use WellCommerce\Bundle\OrderBundle\Service\Invoice\Processor\InvoiceProcessorInterface;
use WellCommerce\Component\Form\Conditions\Equals;
use WellCommerce\Component\Form\Event\FormEvent;
/**
* Class SmsApiSubscriber
*
* @author Adam Piotrowski <adam@wellcommerce.org>
*/
class WFirmaSubscriber extends AbstractServiceSubscriber implements EventSubscriberInterface
{
protected OrderStatusDataSet $dataSet;
protected InvoiceManager $manager;
protected InvoiceProcessorCollection $collection;
public function __construct(
ContainerInterface $locator,
OrderStatusDataSet $dataSet,
InvoiceManager $manager,
InvoiceProcessorCollection $collection
) {
parent::__construct($locator);
$this->dataSet = $dataSet;
$this->manager = $manager;
$this->collection = $collection;
}
public static function getSubscribedEvents()
{
return [
'admin.shop.pre_form_init' => ['onShopFormAdminInit'],
'admin.invoice.pre_form_init' => ['onInvoiceFormAdminInit'],
'admin.client.pre_form_init' => ['onClientFormAdminInit'],
'admin.invoice.post_init' => ['onInvoicePostInit'],
'order_status_history.post_create' => ['onOrderStatusHistoryPostCreate', 0],
];
}
public function onInvoicePostInit(EntityEvent $event)
{
$entity = $event->getEntity();
if ($entity instanceof Invoice) {
$entity->setWFirmaInvoiceType('normal');
}
}
public function onInvoiceFormAdminInit(FormEvent $event)
{
$resource = $event->getResource();
if ($resource instanceof Invoice) {
$form = $event->getForm();
$builder = $event->getFormBuilder();
$wFirmaData = $form->getChildren()->get('required_data');
$wFirmaData->addChild($builder->getElement('select', [
'name' => 'wFirmaInvoiceType',
'label' => 'wfirma.label.type',
'options' => [
'normal' => 'Faktura',
'proforma' => 'Proforma',
],
]));
$schemaBill = $wFirmaData->addChild($builder->getElement('checkbox', [
'name' => 'wFirmaInvoiceSchemaBill',
'label' => 'wfirma.label.schema_bill',
]));
}
}
public function onClientFormAdminInit(FormEvent $event)
{
$resource = $event->getResource();
if ($resource instanceof Client) {
$form = $event->getForm();
$builder = $event->getFormBuilder();
$wFirmaData = $form->addChild($builder->getElement('nested_fieldset', [
'name' => 'wfirma_data',
'label' => 'wfirma.fieldset.settings',
]));
$wFirmaOtherUser = $wFirmaData->addChild($builder->getElement('checkbox', [
'name' => 'wFirmaOtherUser',
'label' => 'wfirma.label.other_user',
'comment' => 'wfirma.comment.other_user',
]));
$wFirmaData->addChild($builder->getElement('text_field', [
'name' => 'wFirmaApiUser',
'label' => 'wfirma.label.user',
'rules' => [
$builder->getRule('required'),
],
'dependencies' => [
$builder->getDependency('show', [
'form' => $form,
'field' => $wFirmaOtherUser,
'condition' => new Equals(1),
]),
],
]));
$wFirmaData->addChild($builder->getElement('password', [
'name' => 'wFirmaApiPass',
'label' => 'wfirma.label.pass',
'rules' => [
$builder->getRule('required'),
],
'dependencies' => [
$builder->getDependency('show', [
'form' => $form,
'field' => $wFirmaOtherUser,
'condition' => new Equals(1),
]),
],
]));
$wFirmaData->addChild($builder->getElement('text_field', [
'name' => 'wFirmaAccessKey',
'label' => 'Access key',
'rules' => [
$builder->getRule('required'),
],
'dependencies' => [
$builder->getDependency('show', [
'form' => $form,
'field' => $wFirmaOtherUser,
'condition' => new Equals(1),
]),
],
]));
$wFirmaData->addChild($builder->getElement('text_field', [
'name' => 'wFirmaSecretKey',
'label' => 'Secret key',
'rules' => [
$builder->getRule('required'),
],
'dependencies' => [
$builder->getDependency('show', [
'form' => $form,
'field' => $wFirmaOtherUser,
'condition' => new Equals(1),
]),
],
]));
$wFirmaData->addChild($builder->getElement('text_field', [
'name' => 'wFirmaAppKey',
'label' => 'App key',
'rules' => [
$builder->getRule('required'),
],
'dependencies' => [
$builder->getDependency('show', [
'form' => $form,
'field' => $wFirmaOtherUser,
'condition' => new Equals(1),
]),
],
]));
}
}
public function onShopFormAdminInit(FormEvent $event)
{
$resource = $event->getResource();
if ($resource instanceof Shop) {
$form = $event->getForm();
$builder = $event->getFormBuilder();
$fieldset = $form->getChildren()->get('invoice_data');
$wFirmaData = $fieldset->addChild($builder->getElement('nested_fieldset', [
'name' => 'wfirma_data',
'label' => 'wfirma.fieldset.settings',
]));
$wFirmaData->addChild($builder->getElement('text_field', [
'name' => 'wFirmaApiUser',
'label' => 'wfirma.label.user',
]));
$wFirmaData->addChild($builder->getElement('password', [
'name' => 'wFirmaApiPass',
'label' => 'wfirma.label.pass',
]));
$wFirmaData->addChild($builder->getElement('password', [
'name' => 'wFirmaAccessKey',
'label' => 'Access key',
]));
$wFirmaData->addChild($builder->getElement('password', [
'name' => 'wFirmaSecretKey',
'label' => 'Secret key',
]));
$wFirmaData->addChild($builder->getElement('password', [
'name' => 'wFirmaAppKey',
'label' => 'App key',
]));
$wFirmaData->addChild($builder->getElement('text_field', [
'name' => 'wFirmaApiSeriesNumberNormal',
'label' => 'wfirma.label.series_number_normal',
]));
$wFirmaData->addChild($builder->getElement('text_field', [
'name' => 'wFirmaApiSeriesNumberProforma',
'label' => 'wfirma.label.series_number_proforma',
]));
$wFirmaData->addChild($builder->getElement('text_field', [
'name' => 'wFirmaWarehouseId',
'label' => 'wfirma.label.warehouse_id',
]));
$wFirmaData->addChild($builder->getElement('checkbox', [
'name' => 'wFirmaApiSyncStock',
'label' => 'wfirma.label.sync_stock',
]));
$wFirmaData->addChild($builder->getElement('checkbox', [
'name' => 'wFirmaApiSyncPrice',
'label' => 'wfirma.label.sync_price',
]));
$wFirmaData->addChild($builder->getElement('checkbox', [
'name' => 'wFirmaApiAutoSend',
'label' => 'wfirma.label.auto_send',
]));
$wFirmaData->addChild($builder->getElement('select', [
'name' => 'wFirmaPriceType',
'label' => 'wfirma.label.price_type',
'options' => [
'netto' => 'netto',
'brutto' => 'brutto',
],
]));
$orderStatuses = $this->dataSet->getResult('select', [], ['default_option' => '---']);
$wFirmaData->addChild($builder->getElement('select', [
'name' => 'wFirmaOrderStatusNormal',
'label' => 'wfirma.label.order_status_normal',
'options' => $orderStatuses,
'transformer' => $builder->getRepositoryTransformer('entity', OrderStatus::class),
]));
$wFirmaData->addChild($builder->getElement('select', [
'name' => 'wFirmaOrderStatusProforma',
'label' => 'wfirma.label.order_status_proforma',
'options' => $orderStatuses,
'transformer' => $builder->getRepositoryTransformer('entity', OrderStatus::class),
]));
$wFirmaData->addChild($builder->getElement('select', [
'name' => 'wFirmaAutoStatusNormal',
'label' => 'wfirma.label.auto_status_normal',
'options' => $orderStatuses,
'transformer' => $builder->getRepositoryTransformer('entity', OrderStatus::class),
]));
}
}
public function onOrderStatusHistoryPostCreate(EntityEvent $event)
{
$history = $event->getEntity();
if ($history instanceof OrderStatusHistory) {
$order = $history->getOrder();
$shop = $order->getShop();
$status = $history->getOrderStatus();
$wFirmaStatus = $shop->getWFirmaAutoStatusNormal();
if ($wFirmaStatus instanceof OrderStatus) {
if ($status->getId() === $wFirmaStatus->getId()) {
if ($order->getInvoices()->count() === 0) {
$invoice = $this->manager->prepareInvoiceForOrder($order);
$invoice->setWFirmaInvoiceType('normal');
$this->manager->createResource($invoice);
}
}
}
}
}
}