Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use teller for orders #3810

Merged
merged 9 commits into from
Dec 11, 2024
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
3 changes: 3 additions & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
- Added an `originalCart` value to the `commerce/update-cart` failed ajax response. ([#430](https://github.com/craftcms/commerce/issues/430))

### Extensibility

- Added `craft\commerce\elements\Order::getTeller()`.
- Added `craft\commerce\elements\conditions\variants\ProductConditionRule`.
- Added `craft\commerce\base\InventoryItemTrait`.
- Added `craft\commerce\base\InventoryLocationTrait`.
- Added `craft\commerce\elements\conditions\orders\CouponCodeConditionRule`.
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- Fixed a PHP error that could occur when calculating order totals. ([#3802](https://github.com/craftcms/commerce/issues/3802))
- Fixed a bug where a product’s default price was showing incorrectly on the Products index page. ([#3807](https://github.com/craftcms/commerce/issues/3807))
- Fixed a bug where inline-editable Matrix fields weren’t saving content on product variants. ([#3805](https://github.com/craftcms/commerce/issues/3805))
- Fixed a bug where order errors weren't showing on the Edit Order page.
Expand Down
34 changes: 18 additions & 16 deletions src/elements/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -1542,9 +1542,9 @@ public function extraFields(): array
/**
* @return Teller
* @throws InvalidConfigException
* @since 5.0.0
* @since 5.3.0
*/
private function _getTeller(): Teller
public function getTeller(): Teller
{
return Plugin::getInstance()->getCurrencies()->getTeller($this->currency);
}
Expand Down Expand Up @@ -1700,7 +1700,8 @@ public function updateOrderPaidInformation(): void
$justPaid = $paidInFull && $this->datePaid == null;
$justAuthorized = $authorizedInFull && $this->dateAuthorized == null;

$canComplete = ($this->getTotalAuthorized() + $this->getTotalPaid()) > 0;
$completeTotal = $this->getTeller()->add($this->getTotalAuthorized(), $this->getTotalPaid());
$canComplete = $this->getTeller()->greaterThan($completeTotal, 0);

// If it is no longer paid in full, set datePaid to null
if (!$paidInFull) {
Expand Down Expand Up @@ -2545,7 +2546,7 @@ public function getPaymentAmount(): float

// Only convert if we have differing currencies
if ($this->currency !== $this->getPaymentCurrency()) {
$teller = $this->_getTeller();
$teller = $this->getTeller();
$tellerTo = Plugin::getInstance()->getCurrencies()->getTeller($this->getPaymentCurrency());
$outstandingBalanceAmount = $teller->convertToMoney($this->getOutstandingBalance());
$outstandingBalanceInPaymentCurrency = Plugin::getInstance()->getPaymentCurrencies()->convertAmount($outstandingBalanceAmount, $this->getPaymentCurrency(), $this->getStore()->id);
Expand Down Expand Up @@ -2593,8 +2594,8 @@ public function isPaymentAmountPartial(): bool
public function getPaidStatus(): string
{
if ($this->getIsPaid() &&
$this->_getTeller()->greaterThan($this->getTotalPrice(), 0) &&
$this->_getTeller()->greaterThan($this->getTotalPaid(), $this->getTotalPrice())
$this->getTeller()->greaterThan($this->getTotalPrice(), 0) &&
$this->getTeller()->greaterThan($this->getTotalPaid(), $this->getTotalPrice())
) {
return self::PAID_STATUS_OVERPAID;
}
Expand All @@ -2603,7 +2604,7 @@ public function getPaidStatus(): string
return self::PAID_STATUS_PAID;
}

if ($this->_getTeller()->greaterThan($this->getTotalPaid(), 0)) {
if ($this->getTeller()->greaterThan($this->getTotalPaid(), 0)) {
return self::PAID_STATUS_PARTIAL;
}

Expand Down Expand Up @@ -2662,26 +2663,27 @@ public function getPaidStatusHtml(): string
*/
public function getTotal(): float
{
return Currency::round($this->getItemSubtotal() + $this->getAdjustmentsTotal());
return (float)$this->getTeller()->add($this->getItemSubtotal(), $this->getAdjustmentsTotal());
}

/**
* Get the total price of the order, whose minimum value is enforced by the configured {@link Store::getMinimumTotalPriceStrategy() strategy set for minimum total price}.
*/
public function getTotalPrice(): float
{
$total = $this->getItemSubtotal() + $this->getAdjustmentsTotal(); // Don't get the pre-rounded total.
$total = (float)$this->getTeller()->add($this->getItemSubtotal(), $this->getAdjustmentsTotal());
// Don't get the pre-rounded total.
$strategy = $this->getStore()->getMinimumTotalPriceStrategy();

if ($strategy === Store::MINIMUM_TOTAL_PRICE_STRATEGY_ZERO) {
return Currency::round(max(0, $total));
return (float)$this->getTeller()->max(0, $total);
}

if ($strategy === Store::MINIMUM_TOTAL_PRICE_STRATEGY_SHIPPING) {
return Currency::round(max($this->getTotalShippingCost(), $total));
return (float)$this->getTeller()->max($this->getTotalShippingCost(), $total);
}

return Currency::round($total);
return $total;
}

public function getItemTotal(): float
Expand Down Expand Up @@ -2716,15 +2718,15 @@ public function hasShippableItems(): bool
*/
public function getOutstandingBalance(): float
{
return (float)$this->_getTeller()->subtract($this->getTotalPrice(), $this->getTotalPaid());
return (float)$this->getTeller()->subtract($this->getTotalPrice(), $this->getTotalPaid());
}

/**
* @return bool Whether the order has an outstanding balance.
*/
public function hasOutstandingBalance(): bool
{
return $this->_getTeller()->greaterThan($this->getOutstandingBalance(), 0);
return $this->getTeller()->greaterThan($this->getOutstandingBalance(), 0);
}

/**
Expand Down Expand Up @@ -2754,7 +2756,7 @@ public function getTotalPaid(): float
&& $transaction->type == TransactionRecord::TYPE_REFUND;
})->sum('amount');

return (float)$this->_getTeller()->subtract($paid, $refunded);
return (float)$this->getTeller()->subtract($paid, $refunded);
}

/**
Expand Down Expand Up @@ -2792,7 +2794,7 @@ public function getTotalAuthorized(): float
}
}

return (float)$this->_getTeller()->subtract($authorized, $captured);
return (float)$this->getTeller()->subtract($authorized, $captured);
}

/**
Expand Down
7 changes: 3 additions & 4 deletions src/models/LineItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ public function getSubtotal(): float
*/
public function getTotal(): float
{
return $this->getSubtotal() + $this->getAdjustmentsTotal();
return (float)$this->order->getTeller()->add($this->getSubtotal(), $this->getAdjustmentsTotal());
}

/**
Expand All @@ -714,10 +714,9 @@ public function getTotal(): float
public function getTaxableSubtotal(string $taxable): float
{
return match ($taxable) {
TaxRateRecord::TAXABLE_PRICE => $this->getSubtotal() + $this->getDiscount(),
TaxRateRecord::TAXABLE_SHIPPING => $this->getShippingCost(),
TaxRateRecord::TAXABLE_PRICE_SHIPPING => $this->getSubtotal() + $this->getDiscount() + $this->getShippingCost(),
default => $this->getSubtotal() + $this->getDiscount(),
TaxRateRecord::TAXABLE_PRICE_SHIPPING => (float)$this->order->getTeller()->sum($this->getSubtotal(), $this->getDiscount() , $this->getShippingCost()),
default => (float)$this->order->getTeller()->add($this->getSubtotal() , $this->getDiscount()), // TaxRateRecord::TAXABLE_PRICE is default
};
}

Expand Down
Loading