Skip to content

Commit

Permalink
Add teller as public order function
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeholder committed Dec 11, 2024
1 parent 5e68d3f commit 27e8bf6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
- Added support for `to`, `bcc`, and `cc` email fields to support environment variables. ([#3738](https://github.com/craftcms/commerce/issues/3738))
- Added an `originalCart` value to the `commerce/update-cart` failed ajax response. ([#430](https://github.com/craftcms/commerce/issues/430))
- Added a new "Payment Gateway" order condition rule. ([#3722](https://github.com/craftcms/commerce/discussions/3722))
- Added `craft\commerce\elements\Order::getTeller()`.
20 changes: 10 additions & 10 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 @@ -2546,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 @@ -2594,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 @@ -2604,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 @@ -2718,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 @@ -2756,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 @@ -2794,7 +2794,7 @@ public function getTotalAuthorized(): float
}
}

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

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

/**
Expand All @@ -715,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 => $this->order->getTeller()->add($this->getSubtotal(), $this->getDiscount() , $this->getShippingCost()),

Check failure on line 718 in src/models/LineItem.php

View workflow job for this annotation

GitHub Actions / ci / Code Quality / PHPStan / PHPStan

Parameter #3 ...$others of method Money\Teller::add() expects array, float given.
default => $this->order->getTeller()->add($this->getSubtotal() , $this->getDiscount()), // TaxRateRecord::TAXABLE_PRICE is default
};
}

Expand Down

0 comments on commit 27e8bf6

Please sign in to comment.