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 to do currency calculations #3834

Draft
wants to merge 2 commits into
base: 5.3
Choose a base branch
from
Draft
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
28 changes: 19 additions & 9 deletions src/adjusters/Tax.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use DvK\Vat\Validator;
use Exception;
use Illuminate\Support\Collection;
use Money\Teller;
use yii\base\InvalidConfigException;
use function in_array;

Expand Down Expand Up @@ -146,12 +147,12 @@
$amount = -$this->_getTaxAmount($orderTaxableAmount, $taxRate->rate, $taxRate->include);

if ($taxRate->taxable === TaxRateRecord::TAXABLE_ORDER_TOTAL_PRICE) {
$this->_costRemovedForOrderTotalPrice += $amount;
$this->_costRemovedForOrderTotalPrice = $this->_getTeller()->add($this->_costRemovedForOrderTotalPrice, $amount);
} elseif ($taxRate->taxable === TaxRateRecord::TAXABLE_ORDER_TOTAL_SHIPPING) {
$this->_costRemovedForOrderShipping += $amount;
$this->_costRemovedForOrderShipping = $this->_getTeller()->add($this->_costRemovedForOrderShipping, $amount);
}

Check failure on line 153 in src/adjusters/Tax.php

View workflow job for this annotation

GitHub Actions / ci / Code Quality / PHPStan / PHPStan

Property craft\commerce\adjusters\Tax::$_costRemovedForOrderTotalPrice (float) does not accept string.

$adjustment = $this->_createAdjustment($taxRate);

Check failure on line 155 in src/adjusters/Tax.php

View workflow job for this annotation

GitHub Actions / ci / Code Quality / PHPStan / PHPStan

Property craft\commerce\adjusters\Tax::$_costRemovedForOrderShipping (float) does not accept string.
// We need to display the adjustment that removed the included tax
$adjustment->name = Craft::t('site', $taxRate->name) . ' ' . Craft::t('commerce', 'Removed');
$adjustment->amount = $amount;
Expand Down Expand Up @@ -298,17 +299,16 @@
*/
private function _getTaxAmount($taxableAmount, $rate, $included): float
{
$teller = $this->_getTeller();
if (!$included) {
$incTax = $taxableAmount * (1 + $rate);
$incTax = Currency::round($incTax);
$tax = $incTax - $taxableAmount;
$incTax = $teller->multiply($taxableAmount, (1 + $rate));
$tax = $teller->subtract($incTax, $taxableAmount);
} else {
$exTax = $taxableAmount / (1 + $rate);
$exTax = Currency::round($exTax);
$tax = $taxableAmount - $exTax;
$exTax = $teller->divide($taxableAmount, (1 + $rate));
$tax = $teller->subtract($taxableAmount, $exTax);
}

return $tax;
return (float)$tax;
}

/**
Expand Down Expand Up @@ -430,4 +430,14 @@

return $address;
}

/**
* @return Teller
* @throws InvalidConfigException
* @since 5.3.0
*/
private function _getTeller(): Teller
{
return Plugin::getInstance()->getCurrencies()->getTeller($this->_order->currency);
}
}
4 changes: 3 additions & 1 deletion src/services/ShippingCategories.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ public function getAllShippingCategoriesAsList(?int $storeId = null): array
*/
public function getShippingCategoryById(int $shippingCategoryId, ?int $storeId = null): ?ShippingCategory
{
return $this->getAllShippingCategories($storeId)->firstWhere('id', $shippingCategoryId);
$shippingCategories = $this->getAllShippingCategories($storeId);
$first = $shippingCategories->firstWhere('id', $shippingCategoryId);
return $first;
}

/**
Expand Down
Loading