custom/plugins/EasyCreditRatenkauf/src/Subscriber/InterestRemover.php line 50

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * (c) NETZKOLLEKTIV GmbH <kontakt@netzkollektiv.com>
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  */
  8. namespace Netzkollektiv\EasyCredit\Subscriber;
  9. use Netzkollektiv\EasyCredit\Cart\Processor;
  10. use Doctrine\DBAL\Connection;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  13. use Shopware\Core\Framework\Uuid\Uuid;
  14. use Netzkollektiv\EasyCredit\Api\Storage;
  15. use Netzkollektiv\EasyCredit\Setting\Service\SettingsServiceInterface;
  16. use Psr\Log\LoggerInterface;
  17. class InterestRemover implements EventSubscriberInterface
  18. {
  19.     private SettingsServiceInterface $settings;
  20.     private $connection;
  21.     private Storage $storage;
  22.     private $logger;
  23.     public function __construct(
  24.         SettingsServiceInterface $settingsService,
  25.         Connection $connection,
  26.         Storage $storage,
  27.         LoggerInterface $logger
  28.     ) {
  29.         $this->settings $settingsService;
  30.         $this->connection $connection;
  31.         $this->storage $storage;
  32.         $this->logger $logger;
  33.     }
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         return [
  37.             CheckoutOrderPlacedEvent::class => 'removeInterest',
  38.         ];
  39.     }
  40.     public function removeInterest(CheckoutOrderPlacedEvent $event): void
  41.     {
  42.         $order $event->getOrder();
  43.         $interestLineItem $order->getLineItems()
  44.             ->filterByType(Processor::LINE_ITEM_TYPE)
  45.             ->first();
  46.         $isEnabled $this->settings
  47.             ->getSettings($event->getSalesChannelId(), false)
  48.             ->getRemoveInterest();
  49.         if (!$isEnabled || !$interestLineItem) {
  50.             return;
  51.         }
  52.         $this->connection->beginTransaction();
  53.         try {
  54.             $this->connection->executeStatement("
  55.                 UPDATE order_transaction ot
  56.                 INNER JOIN order_line_item ol ON ol.order_id = ot.order_id AND ol.type = 'easycredit-interest'
  57.                     Set ot.amount = JSON_REPLACE(ot.amount,
  58.                         '$.unitPrice', ROUND(json_extract(ot.amount, '$.unitPrice') - ol.total_price, 2),
  59.                         '$.totalPrice', ROUND(json_extract(ot.amount, '$.totalPrice') - ol.total_price, 2)
  60.                     )
  61.                 WHERE ot.order_id = ?;
  62.             ", [
  63.                 Uuid::fromHexToBytes($order->getId())
  64.             ]);
  65.             $this->connection->executeStatement("
  66.                 UPDATE `order` o
  67.                 INNER JOIN order_line_item ol ON ol.order_id = o.id AND ol.type = 'easycredit-interest'
  68.                 Set 
  69.                     o.price = JSON_REPLACE(o.price, 
  70.                         '$.netPrice', ROUND(o.amount_net - ol.total_price, 2),
  71.                         '$.totalPrice', ROUND(o.amount_total - ol.total_price, 2), 
  72.                         '$.positionPrice', ROUND(o.position_price - ol.total_price, 2)
  73.                     )
  74.                 WHERE o.id = ?;
  75.             ", [
  76.                 Uuid::fromHexToBytes($order->getId())
  77.             ]);
  78.             $this->connection->executeStatement("
  79.                 DELETE order_line_item FROM order_line_item
  80.                 INNER JOIN `order` o ON order_line_item.order_id = o.id
  81.                 WHERE 
  82.                     o.id = ?
  83.                     AND order_line_item.type = 'easycredit-interest';
  84.             ", [
  85.                 Uuid::fromHexToBytes($order->getId()),
  86.             ]);
  87.             $this->storage->set('interest_amount'null)->persist();
  88.             $this->connection->commit();
  89.         } catch (\Throwable $e) {
  90.             $this->logger->error($e->getMessage());
  91.             $this->connection->rollBack();
  92.         }
  93.     }
  94. }