<?php
/**
* WBFK OG
* Copyright (c) 2024 WBFK OG
*
* @copyright Copyright (c) 2024 WBFK OG (https://wbfk.at)
* @author WBFK OG <info@wbfk.at>
*/
declare(strict_types=1);
namespace Wbfk\DigitalInvoice\Core\Checkout;
use Shopware\Core\Checkout\Cart\Cart;
use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
use Shopware\Core\Checkout\Cart\Order\OrderConvertedEvent;
use Shopware\Core\Checkout\Cart\SalesChannel\AbstractCartOrderRoute;
use Shopware\Core\Checkout\Cart\SalesChannel\CartOrderRouteResponse;
use Shopware\Core\Framework\Struct\ArrayStruct;
use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Wbfk\DigitalInvoice\Entity\Order\WbfkInvoiceDetailsOrderExtension;
use Wbfk\DigitalInvoice\Entity\Order\WbfkInvoiceDetailsOrderExtensionEntity;
use Wbfk\DigitalInvoice\Service\Utils\Utils;
class CartOrderRouteDecorator extends AbstractCartOrderRoute implements EventSubscriberInterface
{
public function __construct(
private readonly AbstractCartOrderRoute $decorated
) {
}
public static function getSubscribedEvents(): array
{
return [
CartConvertedEvent::class => 'addDigitalInvoiceDataToOrder',
OrderConvertedEvent::class => 'addDigitalInvoiceDataToCart',
];
}
public function getDecorated(): AbstractCartOrderRoute
{
return $this->decorated;
}
public function order(Cart $cart, SalesChannelContext $context, RequestDataBag $data): CartOrderRouteResponse
{
$this->addAdditionalDataToCartSendFromConfirmationPage($cart, $data);
return $this->decorated->order($cart, $context, $data);
}
private function addAdditionalDataToCartSendFromConfirmationPage(Cart $cart, RequestDataBag $data): void
{
$additionalData = new ArrayStruct([
"invoiceType" => Utils::trimNull($data->get("wbfkDigitalInvoiceType")),
'bt10' => Utils::trimNull($data->get("wbfkDigitalInvoiceBt10")),
'bt11' => Utils::trimNull($data->get("wbfkDigitalInvoiceBt11")),
'bt12' => Utils::trimNull($data->get("wbfkDigitalInvoiceBt12")),
'bt13' => Utils::trimNull($data->get("wbfkDigitalInvoiceBt13")),
]);
$cart->addExtension(WbfkInvoiceDetailsOrderExtension::NAME, $additionalData);
}
public function addDigitalInvoiceDataToOrder(CartConvertedEvent $event): void
{
$cart = $event->getCart();
$convertedCart = $event->getConvertedCart();
/** @var ?ArrayStruct $digitalInvoiceData */
$digitalInvoiceData = $cart->getExtension(WbfkInvoiceDetailsOrderExtension::NAME);
if (!$digitalInvoiceData) {
return;
}
$convertedCart['extensions'] = $convertedCart['extensions'] ?? [];
$convertedCart['extensions'][WbfkInvoiceDetailsOrderExtension::NAME] = $digitalInvoiceData->all();
$event->setConvertedCart($convertedCart);
}
public function addDigitalInvoiceDataToCart(OrderConvertedEvent $event): void
{
/** @var WbfkInvoiceDetailsOrderExtensionEntity $digitalInvoiceData */
$digitalInvoiceData = $event->getOrder()->getExtension(WbfkInvoiceDetailsOrderExtension::NAME);
if (!$digitalInvoiceData) {
return;
}
$convertedCart = $event->getConvertedCart();
$convertedCart->addExtension(WbfkInvoiceDetailsOrderExtension::NAME, new ArrayStruct(array_merge(
$digitalInvoiceData->invoiceDataAsArray(),
["id" => $digitalInvoiceData->getId()]
)));
}
}