Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 0 additions & 42 deletions Build/phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2874,48 +2874,6 @@ parameters:
count: 1
path: ../Classes/EventListener/Order/Create/OrderNumber.php

-
message: '#^Cannot call method getCode\(\) on mixed\.$#'
identifier: method.nonObject
count: 2
path: ../Classes/EventListener/Order/Create/PersistOrder/Coupons.php

-
message: '#^Cannot call method getGross\(\) on mixed\.$#'
identifier: method.nonObject
count: 1
path: ../Classes/EventListener/Order/Create/PersistOrder/Coupons.php

-
message: '#^Cannot call method getNet\(\) on mixed\.$#'
identifier: method.nonObject
count: 1
path: ../Classes/EventListener/Order/Create/PersistOrder/Coupons.php

-
message: '#^Cannot call method getTax\(\) on mixed\.$#'
identifier: method.nonObject
count: 1
path: ../Classes/EventListener/Order/Create/PersistOrder/Coupons.php

-
message: '#^Cannot call method getTaxClass\(\) on mixed\.$#'
identifier: method.nonObject
count: 1
path: ../Classes/EventListener/Order/Create/PersistOrder/Coupons.php

-
message: '#^Cannot call method getTitle\(\) on mixed\.$#'
identifier: method.nonObject
count: 1
path: ../Classes/EventListener/Order/Create/PersistOrder/Coupons.php

-
message: '#^Cannot call method isUseable\(\) on mixed\.$#'
identifier: method.nonObject
count: 1
path: ../Classes/EventListener/Order/Create/PersistOrder/Coupons.php

-
message: '#^Cannot access offset ''currency'' on mixed\.$#'
identifier: offsetAccess.nonOffsetAccessible
Expand Down
98 changes: 94 additions & 4 deletions Classes/Domain/Model/Order/Discount.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* LICENSE file that was distributed with this source code.
*/

use Extcode\Cart\Domain\Model\Cart\TaxClass;
use Extcode\Cart\Domain\Model\Cart\TaxClass as CartTaxClass;
use TYPO3\CMS\Extbase\Attribute\ORM\Lazy;
use TYPO3\CMS\Extbase\Attribute\Validate;
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
Expand All @@ -22,6 +22,30 @@ class Discount extends AbstractEntity
#[Lazy]
protected Item|LazyLoadingProxy $item;

protected ?TaxClass $taxClass = null;

/**
* Class of the cart coupon, e.g. \Extcode\Cart\Domain\Model\Cart\CartCouponPercentage.
*/
protected string $couponType = '';

/**
* JSON encoded taxes of the discount per tax class of the order (uid => tax).
* An empty string means that the taxes per tax class are unknown (orders
* which were not migrated by the upgrade wizard).
*
* @var string JSON, the tag keeps Extbase from taking the array type of getTaxes() for the property
*/
protected string $taxes = '';

/**
* The discount is not changed after the order was persisted, so there are
* no setters.
*
* @param null|CartTaxClass|TaxClass $taxClass the tax class of the order; passing the tax class of the cart is deprecated
* @param string $couponType class of the cart coupon
* @param null|array<int, float> $taxes uid of the tax class of the order => tax, null if unknown
*/
public function __construct(
#[Validate(validator: 'NotEmpty')]
protected string $title,
Expand All @@ -31,11 +55,28 @@ public function __construct(
protected float $gross,
#[Validate(validator: 'NotEmpty')]
protected float $net,
null|CartTaxClass|TaxClass $taxClass,
#[Validate(validator: 'NotEmpty')]
protected TaxClass $taxClass,
#[Validate(validator: 'NotEmpty')]
protected float $tax
protected float $tax,
string $couponType = '',
?array $taxes = null
) {
// TODO: 13.0.0 Accept only the tax class of the order, see Deprecation #896.
if ($taxClass instanceof CartTaxClass) {
trigger_error(
'Passing a tax class of the cart (' . CartTaxClass::class . ') to ' . self::class . ' is deprecated'
. ' since cart 12.8 and will stop working in cart 13.0. Pass the tax class of the order ('
. TaxClass::class . ') instead.',
E_USER_DEPRECATED
);
$taxClass = null;
}

$this->taxClass = $taxClass;
$this->couponType = $couponType;
if ($taxes !== null) {
$this->taxes = json_encode((object) $taxes, JSON_THROW_ON_ERROR | JSON_PRESERVE_ZERO_FRACTION);
}
}

public function getItem(): ?Item
Expand Down Expand Up @@ -67,13 +108,62 @@ public function getNet(): float
return $this->net;
}

/**
* Returns the tax class of the order the coupon was configured with. The
* tax class is not stored for orders created before cart 12.8.
*/
public function getTaxClass(): ?TaxClass
{
return $this->taxClass;
}

/**
* Returns the tax of the coupon in its own tax class. It is 0.0 for
* percentage coupons, their tax is split over the tax classes of the
* products, see getTaxes().
*/
public function getTax(): float
{
return $this->tax;
}

public function getCouponType(): string
{
return $this->couponType;
}

/**
* Returns the taxes of the discount per tax class of the order.
*
* @return array<int, float> uid of the tax class of the order => tax
*/
public function getTaxes(): array
{
if ($this->taxes === '') {
return [];
}

$taxes = json_decode($this->taxes, true);
if (!is_array($taxes)) {
return [];
}

$result = [];
foreach ($taxes as $taxClassUid => $tax) {
if (is_numeric($taxClassUid) && is_numeric($tax)) {
$result[(int) $taxClassUid] = (float) $tax;
}
}

return $result;
}

/**
* Returns false for discounts of orders created before cart 12.8 which
* were not migrated by the upgrade wizard "extcodeCartOrderDiscountTaxes".
*/
public function hasTaxes(): bool
{
return $this->taxes !== '';
}
}
47 changes: 44 additions & 3 deletions Classes/EventListener/Order/Create/PersistOrder/Coupons.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
* LICENSE file that was distributed with this source code.
*/

use Extcode\Cart\Domain\Model\Cart\CartCouponInterface;
use Extcode\Cart\Domain\Model\Coupon;
use Extcode\Cart\Domain\Model\Order\Discount;
use Extcode\Cart\Domain\Model\Order\TaxClass;
use Extcode\Cart\Domain\Repository\CouponRepository;
use Extcode\Cart\Domain\Repository\Order\DiscountRepository;
use Extcode\Cart\Domain\Repository\Order\ItemRepository;
Expand All @@ -35,21 +37,27 @@ public function __invoke(PersistOrderEvent $event): void
$cart = $event->getCart();
$orderItem = $event->getOrderItem();
$storagePid = $event->getStoragePid();
$taxClasses = array_filter(
$event->getTaxClasses(),
static fn (mixed $taxClass): bool => $taxClass instanceof TaxClass
);

if (!$cart->getCoupons()) {
return;
}

foreach ($cart->getCoupons() as $cartCoupon) {
if ($cartCoupon->isUseable()) {
if ($cartCoupon instanceof CartCouponInterface && $cartCoupon->isUseable()) {
$orderDiscount = GeneralUtility::makeInstance(
Discount::class,
$cartCoupon->getTitle(),
$cartCoupon->getCode(),
$cartCoupon->getGross(),
$cartCoupon->getNet(),
$cartCoupon->getTaxClass(),
$cartCoupon->getTax()
$taxClasses[$cartCoupon->getTaxClass()->getId()] ?? null,
$cartCoupon->getTax(),
$cartCoupon::class,
self::getTaxesPerOrderTaxClass($cartCoupon, $taxClasses)
);
$orderDiscount->setPid($storagePid);

Expand All @@ -69,4 +77,37 @@ public function __invoke(PersistOrderEvent $event): void

$this->persistenceManager->persistAll();
}

/**
* Returns the taxes of the coupon per tax class of the order. They are
* calculated like in Cart::getCouponTaxes(), so the taxes of all discounts
* of an order add up to the coupon taxes of the cart.
*
* @param array<TaxClass> $taxClasses id of the tax class of the cart => tax class of the order
*
* @return array<int, float> uid of the tax class of the order => tax
*/
private static function getTaxesPerOrderTaxClass(CartCouponInterface $cartCoupon, array $taxClasses): array
{
$cartTaxes = [
$cartCoupon->getTaxClass()->getId() => $cartCoupon->getTax(),
];
$couponTaxes = method_exists($cartCoupon, 'getTaxes') ? $cartCoupon->getTaxes() : [];
foreach (is_array($couponTaxes) ? $couponTaxes : [] as $taxClassId => $tax) {
if (is_int($taxClassId) && is_numeric($tax)) {
$cartTaxes[$taxClassId] = ($cartTaxes[$taxClassId] ?? 0.0) + (float) $tax;
}
}

$taxes = [];
foreach ($cartTaxes as $taxClassId => $tax) {
$taxClassUid = ($taxClasses[$taxClassId] ?? null)?->getUid();
if ($taxClassUid !== null) {
// rounded like the amounts in the decimal(17,5) columns
$taxes[$taxClassUid] = round($tax, 5);
}
}

return $taxes;
}
}
Loading
Loading