custom/plugins/WbfkOffer/src/Subscriber/AddOfferDetailsToOrder.php line 42

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Wbfk\Offer\Subscriber;
  4. use Shopware\Core\Checkout\Order\OrderEntity;
  5. use Shopware\Core\Checkout\Order\OrderEvents;
  6. use Shopware\Core\Defaults;
  7. use Shopware\Core\Framework\Context;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  13. use Shopware\Core\Framework\Uuid\Uuid;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Wbfk\Offer\Core\Entity\Offer\OfferEntity;
  16. use Wbfk\Offer\Core\Offer\Storefront\OfferService;
  17. use WbfkExtensions\Core\Checkout\WbfkOrderExtension\WbfkOrderExtensionEntity;
  18. class AddOfferDetailsToOrder implements EventSubscriberInterface
  19. {
  20.     public function __construct(
  21.         private readonly OfferService     $offerService,
  22.         private readonly EntityRepository $orderRepository,
  23.         private readonly EntityRepository $offerRepository,
  24.         private readonly EntityRepository $wbfkOrderExtensionRepository,
  25.         private readonly EntityRepository $kzphLastchangeRepository
  26.     )
  27.     {
  28.     }
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             OrderEvents::ORDER_WRITTEN_EVENT => 'addOfferDetailsToOrder',
  33.         ];
  34.     }
  35.     public function addOfferDetailsToOrder(EntityWrittenEvent $event): void
  36.     {
  37.         foreach ($event->getWriteResults() as $result) {
  38.             if (!in_array($result->getOperation(), [EntityWriteResult::OPERATION_INSERTEntityWriteResult::OPERATION_UPDATE])) {
  39.                 continue;
  40.             }
  41.             $orderId $result->getPayload()['id'] ?? null;
  42.             if (!$orderId) {
  43.                 continue;
  44.             }
  45.             // Fetch offer
  46.             $criteria = new Criteria();
  47.             $criteria->addFilter(new EqualsFilter('orderId'$orderId));
  48.             $criteria->setLimit(1);
  49.             /** @var OfferEntity $offer */
  50.             $offer $this->offerRepository->search($criteria$event->getContext())->first();
  51.             if (!$offer) return;
  52.             // Fetch order
  53.             $criteria = new Criteria();
  54.             $criteria->addFilter(new EqualsFilter('id'$orderId));
  55.             $criteria->addAssociation('lineItems');
  56.             $criteria->addAssociation('wbfkOrderExtension');
  57.             $criteria->setLimit(1);
  58.             /** @var ?OrderEntity $order */
  59.             $order $this->orderRepository->search($criteria$event->getContext())->first();
  60.             if (!$order) return;
  61.             $updatedOrderData = [];
  62.             $updatedOrderCustomFields $order->getCustomFields() ?? [];
  63.             if ($result->getOperation() === EntityWriteResult::OPERATION_INSERT $event->getContext()->getVersionId() === Defaults::LIVE_VERSION) {
  64.                 // Set order shipping prime cost
  65.                 if (!!$offer->getShippingPrimeCost()) {
  66.                     $updatedOrderCustomFields['wbfk_shipping_prime_cost'] = $offer->getShippingPrimeCost();
  67.                     $updatedOrderData['customFields'] = $updatedOrderCustomFields;
  68.                 }
  69.                 // Set XInvoice routing id
  70.                 if (!!$offer->getXInvoice()) {
  71.                     $updatedOrderCustomFields['wbfk_invoice_x_invoice_routing_id'] = $offer->getXInvoice();
  72.                     $updatedOrderData['customFields'] = $updatedOrderCustomFields;
  73.                 }
  74.                 // Set project / order reference
  75.                 if (!!$offer->getProjectNumber()) {
  76.                     $updatedOrderCustomFields['wbfk_order_reference'] = $offer->getProjectNumber();
  77.                     $updatedOrderData['customFields'] = $updatedOrderCustomFields;
  78.                 }
  79.                 // Set order payment target
  80.                 if ($offer->getPaymentTarget()) {
  81.                     $updatedOrderCustomFields['wbfk_invoice_payment_target'] = $offer->getPaymentTarget();
  82.                     $updatedOrderData['customFields'] = $updatedOrderCustomFields;
  83.                 }
  84.                 // Set Hide Billing Address Contact Name
  85.                 if (!!$offer->getCustomFields() && isset($offer->getCustomFields()['wbfk_hide_billing_address_name'])) {
  86.                     $updatedOrderCustomFields['wbfk_hide_billing_address_name'] = $offer->getCustomFields()['wbfk_hide_billing_address_name'];
  87.                     $updatedOrderData['customFields'] = $updatedOrderCustomFields;
  88.                 }
  89.                 // Set Hide Shipping Address Contact Name
  90.                 if (!!$offer->getCustomFields() && isset($offer->getCustomFields()['wbfk_hide_shipping_address_name'])) {
  91.                     $updatedOrderCustomFields['wbfk_hide_shipping_address_name'] = $offer->getCustomFields()['wbfk_hide_shipping_address_name'];
  92.                     $updatedOrderData['customFields'] = $updatedOrderCustomFields;
  93.                 }
  94.                 // Set order creator
  95.                 if (isset($_REQUEST['creatorUsername']) && $creatorUsername $_REQUEST['creatorUsername']) {
  96.                     $this->createKzphLastchangeOrderCreationRecord($orderId$creatorUsername$event->getContext());
  97.                 }
  98.                 // Set order assignee
  99.                 /** @var WbfkOrderExtensionEntity $wbfkOrderExtension */
  100.                 if ($offer->getAssigneeId() && $wbfkOrderExtension $order->getExtension('wbfkOrderExtension')) {
  101.                     $this->updateWbfkOrderExtension($wbfkOrderExtension->getId(), ['assigneeId' => $offer->getAssigneeId()], $event->getContext());
  102.                 }
  103.             }
  104.             if (!empty($updatedOrderData) && json_encode($updatedOrderData) !== json_encode(['customFields' => $order->getCustomFields()])) {
  105.                 $this->updateOrder($orderId$updatedOrderData$event->getContext());
  106.             }
  107.         }
  108.     }
  109.     /**
  110.      * @param string $orderId
  111.      * @param array $params
  112.      * @param Context $context
  113.      */
  114.     private function updateOrder(string $orderId, array $paramsContext $context): void
  115.     {
  116.         $rowData = ['id' => $orderId];
  117.         foreach ($params as $paramKey => $paramValue) {
  118.             $rowData[$paramKey] = $paramValue;
  119.         }
  120.         $this->orderRepository->update([$rowData], $context);
  121.     }
  122.     /**
  123.      * @param string $wbfkOrderExtensionId
  124.      * @param array $params
  125.      * @param Context $context
  126.      */
  127.     private function updateWbfkOrderExtension(string $wbfkOrderExtensionId, array $paramsContext $context): void
  128.     {
  129.         $rowData = ['id' => $wbfkOrderExtensionId];
  130.         foreach ($params as $paramKey => $paramValue) {
  131.             $rowData[$paramKey] = $paramValue;
  132.         }
  133.         $this->wbfkOrderExtensionRepository->update([$rowData], $context);
  134.     }
  135.     /**
  136.      * @param string $wbfkOrderExtensionId
  137.      * @param array $params
  138.      * @param Context $context
  139.      */
  140.     private function createKzphLastchangeOrderCreationRecord(string $orderIdstring $creatorUsernameContext $context): void
  141.     {
  142.         $this->kzphLastchangeRepository->create(
  143.             [[
  144.                 'id' => Uuid::randomHex(),
  145.                 'entityId' => $orderId,
  146.                 'entityType' => 'order',
  147.                 'type' => 'created',
  148.                 'username' => $creatorUsername
  149.             ]],
  150.             $context);
  151.     }
  152. }