|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Sylius package. |
| 5 | + * |
| 6 | + * (c) Sylius Sp. z o.o. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace Sylius\InvoicingPlugin\Converter; |
| 15 | + |
| 16 | +use Sylius\Component\Core\Model\OrderInterface; |
| 17 | +use Sylius\Component\Core\Model\OrderItemInterface; |
| 18 | +use Sylius\Component\Core\Model\OrderItemUnitInterface; |
| 19 | +use Sylius\InvoicingPlugin\Entity\LineItemInterface; |
| 20 | +use Sylius\InvoicingPlugin\Factory\LineItemFactoryInterface; |
| 21 | +use Sylius\InvoicingPlugin\Provider\ItemNetPricesProviderInterface; |
| 22 | +use Sylius\InvoicingPlugin\Provider\TaxRatePercentageProviderInterface; |
| 23 | +use Webmozart\Assert\Assert; |
| 24 | + |
| 25 | +final class OrderItemsToLineItemsConverter implements LineItemsConverterInterface |
| 26 | +{ |
| 27 | + public function __construct( |
| 28 | + private readonly TaxRatePercentageProviderInterface $taxRatePercentageProvider, |
| 29 | + private readonly LineItemFactoryInterface $lineItemFactory, |
| 30 | + private readonly ItemNetPricesProviderInterface $unitNetPriceProvider, |
| 31 | + ) { |
| 32 | + } |
| 33 | + |
| 34 | + public function convert(OrderInterface $order): array |
| 35 | + { |
| 36 | + $lineItems = []; |
| 37 | + |
| 38 | + /** @var OrderItemInterface $item */ |
| 39 | + foreach ($order->getItems() as $item) { |
| 40 | + foreach ($this->convertOrderItemToLineItems($item) as $lineItem) { |
| 41 | + $lineItems = $this->addLineItem($lineItem, $lineItems); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + return $lineItems; |
| 46 | + } |
| 47 | + |
| 48 | + private function convertOrderItemToLineItems(OrderItemInterface $item): array |
| 49 | + { |
| 50 | + $lineItems = []; |
| 51 | + $units = $item->getUnits()->getValues(); |
| 52 | + $unitNetPrices = $this->unitNetPriceProvider->getItemNetPrices($item); |
| 53 | + |
| 54 | + /** @var OrderItemUnitInterface $unit */ |
| 55 | + foreach ($units as $index => $unit) { |
| 56 | + $lineItems = $this->addLineItem($this->convertOrderItemUnitToLineItem($unit, (int) $unitNetPrices[$index]), $lineItems); |
| 57 | + } |
| 58 | + |
| 59 | + return $lineItems; |
| 60 | + } |
| 61 | + |
| 62 | + private function convertOrderItemUnitToLineItem(OrderItemUnitInterface $unit, int $unitNetPrice): LineItemInterface |
| 63 | + { |
| 64 | + /** @var OrderItemInterface $item */ |
| 65 | + $item = $unit->getOrderItem(); |
| 66 | + |
| 67 | + $grossValue = $unit->getTotal(); |
| 68 | + $taxAmount = $unit->getTaxTotal(); |
| 69 | + $discountedUnitNetPrice = $grossValue - $taxAmount; |
| 70 | + |
| 71 | + /** @var string|null $productName */ |
| 72 | + $productName = $item->getProductName(); |
| 73 | + Assert::notNull($productName); |
| 74 | + |
| 75 | + $variant = $item->getVariant(); |
| 76 | + |
| 77 | + return $this->lineItemFactory->createWithData( |
| 78 | + $productName, |
| 79 | + 1, |
| 80 | + $unitNetPrice, |
| 81 | + $discountedUnitNetPrice, |
| 82 | + $discountedUnitNetPrice, |
| 83 | + $taxAmount, |
| 84 | + $grossValue, |
| 85 | + $item->getVariantName(), |
| 86 | + $variant !== null ? $variant->getCode() : null, |
| 87 | + $this->taxRatePercentageProvider->provideFromAdjustable($unit), |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @param LineItemInterface[] $lineItems |
| 93 | + * |
| 94 | + * @return LineItemInterface[] |
| 95 | + */ |
| 96 | + private function addLineItem(LineItemInterface $newLineItem, array $lineItems): array |
| 97 | + { |
| 98 | + foreach ($lineItems as $lineItem) { |
| 99 | + if ($lineItem->compare($newLineItem)) { |
| 100 | + $lineItem->merge($newLineItem); |
| 101 | + |
| 102 | + return $lineItems; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + $lineItems[] = $newLineItem; |
| 107 | + |
| 108 | + return $lineItems; |
| 109 | + } |
| 110 | +} |
0 commit comments