init
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\CustomerVat\Plugin\Tax;
|
||||
|
||||
use Esun\CustomerVat\Model\TaxContextResolver;
|
||||
use Esun\CustomerVat\Setup\Patch\Data\ConfigureTaxRules;
|
||||
use Magento\Framework\App\ResourceConnection;
|
||||
use Magento\Tax\Api\Data\QuoteDetailsInterface;
|
||||
use Magento\Tax\Model\TaxCalculation;
|
||||
|
||||
class TaxCalculationPlugin
|
||||
{
|
||||
private ?int $exemptClassId = null;
|
||||
|
||||
public function __construct(
|
||||
private readonly TaxContextResolver $contextResolver,
|
||||
private readonly ResourceConnection $resourceConnection
|
||||
) {
|
||||
}
|
||||
|
||||
public function beforeCalculateTax(
|
||||
TaxCalculation $subject,
|
||||
QuoteDetailsInterface $quoteDetails,
|
||||
$storeId = null,
|
||||
$round = true
|
||||
): array {
|
||||
if ($storeId === null) {
|
||||
return [$quoteDetails, $storeId, $round];
|
||||
}
|
||||
$storeId = (int)$storeId;
|
||||
$shippingCountryId = (string)($quoteDetails->getShippingAddress()?->getCountryId() ?? '');
|
||||
$context = $this->contextResolver->resolve(
|
||||
$storeId,
|
||||
(int)$quoteDetails->getCustomerId(),
|
||||
$shippingCountryId
|
||||
);
|
||||
if (!$context->exempt || $context->nominalRate <= 0) {
|
||||
return [$quoteDetails, $storeId, $round];
|
||||
}
|
||||
|
||||
$taxClassKey = $quoteDetails->getCustomerTaxClassKey();
|
||||
if ($taxClassKey) {
|
||||
$taxClassKey->setValue($this->getExemptClassId());
|
||||
$quoteDetails->setCustomerTaxClassKey($taxClassKey);
|
||||
}
|
||||
$divisor = 1 + ($context->nominalRate / 100);
|
||||
foreach ($quoteDetails->getItems() ?? [] as $item) {
|
||||
$item->setUnitPrice($this->divide((float)$item->getUnitPrice(), $divisor));
|
||||
$item->setDiscountAmount($this->divide((float)$item->getDiscountAmount(), $divisor));
|
||||
$item->setIsTaxIncluded(false);
|
||||
$extensionAttributes = $item->getExtensionAttributes();
|
||||
if ($extensionAttributes && $extensionAttributes->getPriceForTaxCalculation() !== null) {
|
||||
$extensionAttributes->setPriceForTaxCalculation(
|
||||
$this->divide((float)$extensionAttributes->getPriceForTaxCalculation(), $divisor)
|
||||
);
|
||||
$item->setExtensionAttributes($extensionAttributes);
|
||||
}
|
||||
}
|
||||
return [$quoteDetails, $storeId, $round];
|
||||
}
|
||||
|
||||
private function getExemptClassId(): int
|
||||
{
|
||||
if ($this->exemptClassId !== null) {
|
||||
return $this->exemptClassId;
|
||||
}
|
||||
$connection = $this->resourceConnection->getConnection();
|
||||
$this->exemptClassId = (int)$connection->fetchOne(
|
||||
$connection->select()->from($this->resourceConnection->getTableName('tax_class'), 'class_id')
|
||||
->where('class_name = ?', ConfigureTaxRules::EXEMPT_TAX_CLASS)
|
||||
->where('class_type = ?', 'CUSTOMER')
|
||||
);
|
||||
return $this->exemptClassId;
|
||||
}
|
||||
|
||||
private function divide(float $amount, float $divisor): float
|
||||
{
|
||||
return round($amount / $divisor, 8);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user