<?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\BaselinkerBundle\EventListener;
use Psr\Container\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use WellCommerce\Bundle\AppBundle\Entity\Shop;
use WellCommerce\Bundle\BaselinkerBundle\Service\Client\BaselinkerClient;
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\Order;
use WellCommerce\Bundle\OrderBundle\Entity\OrderStatus;
use WellCommerce\Bundle\OrderBundle\Entity\OrderStatusHistory;
use WellCommerce\Bundle\OrderBundle\Entity\Shipment;
use WellCommerce\Bundle\OrderBundle\Service\Shipment\Adapter\ShipmentAdapterCollection;
use WellCommerce\Bundle\OrderBundle\Service\Shipment\Adapter\ShipmentAdapterInterface;
use WellCommerce\Component\Form\Event\FormEvent;
/**
* Class SmsApiSubscriber
*
* @author Adam Piotrowski <adam@wellcommerce.org>
*/
class BaselinkerSubscriber extends AbstractServiceSubscriber implements EventSubscriberInterface
{
protected OrderStatusDataSet $dataSet;
protected BaselinkerClient $client;
protected ShipmentAdapterCollection $adapterCollection;
public function __construct(
ContainerInterface $locator,
OrderStatusDataSet $dataSet,
BaselinkerClient $client,
ShipmentAdapterCollection $adapterCollection
) {
parent::__construct($locator);
$this->dataSet = $dataSet;
$this->client = $client;
$this->adapterCollection = $adapterCollection;
}
public static function getSubscribedEvents(): array
{
return [
'admin.shop.pre_form_init' => ['onShopFormAdminInit'],
'admin.order_status.pre_form_init' => ['onOrderStatusFormAdminInit'],
'order_status_history.post_create' => ['onOrderStatusHistoryCreated', 0],
'shipment.post_create' => ['onShipmentCreated', 0],
];
}
public function onShipmentCreated(EntityEvent $event)
{
$shipment = $event->getEntity();
if ($shipment instanceof Shipment) {
$token = $shipment->getOrder()->getShop()->getBaselinkerToken();
if (empty($token)) {
return;
}
$courierCode = $this->getAdapter($shipment)->getBaselinkerCourierCode();
if (!empty($courierCode)) {
$this->client->syncShipment($shipment, $courierCode);
}
}
}
public function onOrderStatusHistoryCreated(EntityEvent $event)
{
$history = $event->getEntity();
if ($history instanceof OrderStatusHistory) {
$order = $history->getOrder();
$status = $history->getOrderStatus();
$token = $order->getShop()->getBaselinkerToken();
if (empty($token)) {
return;
}
if ($order instanceof Order && $status instanceof OrderStatus) {
$this->client->syncOrderStatus($order, $status);
}
}
}
public function onOrderStatusFormAdminInit(FormEvent $event)
{
$resource = $event->getResource();
if ($resource instanceof OrderStatus) {
$form = $event->getForm();
$builder = $event->getFormBuilder();
$shop = $this->getShopStorage()->getCurrentShop();
$token = $shop->getBaselinkerToken();
if (empty($token)) {
return;
}
$statuses = $this->client->getOrderStatuses();
$options = [];
$options[0] = '--- wybierz ---';
foreach ($statuses as $status) {
$options[$status['id']] = $status['name'];
}
$baselinkerData = $form->addChild($builder->getElement('nested_fieldset', [
'name' => 'baselinker_data',
'label' => 'Ustawienia Baselinker',
]));
$baselinkerData->addChild($builder->getElement('select', [
'name' => 'baselinkerId',
'label' => 'Status Baselinker',
'options' => $options,
]));
}
}
public function onShopFormAdminInit(FormEvent $event)
{
$resource = $event->getResource();
if ($resource instanceof Shop) {
$form = $event->getForm();
$builder = $event->getFormBuilder();
$baselinkerData = $form->addChild($builder->getElement('nested_fieldset', [
'name' => 'baselinker_data',
'label' => 'Ustawienia Baselinker',
]));
$baselinkerData->addChild($builder->getElement('text_field', [
'name' => 'baselinkerToken',
'label' => 'Token API',
]));
$orderStatuses = $this->dataSet->getResult('select', [], ['default_option' => '---']);
$baselinkerData->addChild($builder->getElement('select', [
'name' => 'baselinkerOrderStatus',
'label' => 'Status nowych zamówień',
'options' => $orderStatuses,
'transformer' => $builder->getRepositoryTransformer('entity', OrderStatus::class),
]));
$token = $resource->getBaselinkerToken();
if (empty($token)) {
return;
}
$series = $this->client->getInvoiceSeries();
$options = [];
$options[0] = '--- wybierz ---';
foreach ($series as $item) {
$options[$item['id']] = $item['type'] . ' - ' . $item['name'];
}
$baselinkerData->addChild($builder->getElement('select', [
'name' => 'baselinkerInvoiceSeriesId',
'label' => 'Seria dla faktur',
'options' => $options,
]));
}
}
protected function getAdapter(Shipment $shipment): ShipmentAdapterInterface
{
return $this->adapterCollection->get($shipment->getCourier());
}
}