我已经在结帐单上加了一笔额外费用,但是税款计算不正确。
如果我执行$ this-> _ calculateTax($ address);税额未正确加起来;在收集功能中,它被加到包括税在内的总额中,但是我的税额仍然是那时。
如果是var_dump,则在$ address-> setAppliedTaxes($ previouslyAppliedTaxes)行之后设置已套用的税款;我确实看到了正确的金额。它两次转储,第一次我看到的只是我的额外费用的正确税额,第二次看到总税额的正确额度。但是在前端,它显示的是税款,而没有我的额外费用。
关于这可能是什么线索?
class Company_Customer_Model_Quote_Address_Total_PrintPrepCosts extends Mage_Sales_Model_Quote_Address_Total_Abstract
{
public function __construct() {
$this->setCode('printPrepCosts');
$this->_store = Mage::app()->getStore();
return $this;
}
public function collect(Mage_Sales_Model_Quote_Address $address)
{
parent::collect($address);
$address->setPrintPrepcosts(0);
$address->setTaxAmount(0);
$address->setBaseTaxAmount(0);
if(count($address->getAllItems()) == 0)
{
return $this;
}
$pricePrint = $this->calcTotalPrintPrepCosts();
$address->setPrintPrepcosts($pricePrint);
$address->setBasePrintPrepcosts($pricePrint);
$address->setBaseGrandTotal($address->getBaseGrandTotal() + $address->getPrintPrepcosts());
$address->setGrandTotal($address->getGrandTotal() + $address->getPrintPrepcosts());
$this->_calculateTax($address);
return $this;
}
protected function _calculateTax(Mage_Sales_Model_Quote_Address $address)
{
$calculator = Mage::getSingleton('tax/calculation');
$inclTax = Mage::getStoreConfig('tax/calculation/printing_prep_includes_tax', $this->_store);
$taxRateRequest = $calculator->getRateRequest(
$address,
$address->getQuote()->getBillingAddress(),
$address->getQuote()->getCustomerTaxClassId(),
$this->_store
);
// TODO undef prop _store
$taxRateRequest->setProductClassId(Mage::getStoreConfig('tax/classes/printing_prep_tax_class', $this->_store));
$rate = $calculator->getRate($taxRateRequest);
$baseTax = $tax = $calculator->calcTaxAmount($address->getPrepPrintcosts(), $rate, $inclTax, true);
$address->addTotalAmount('tax', max(0, $tax));
$address->addBaseTotalAmount('tax', max(0, $baseTax));
$this->_saveAppliedTaxes($address,
$calculator->getAppliedRates($taxRateRequest),
$tax,
$baseTax,
$rate
);
// later on added - which fixes the total, lose tax amount still off
$address->setTaxAmount($tax);
$address->setBaseTaxAmount($baseTax);
if($inclTax)
{
$address->setBaseGrandTotal($address->getBaseGrandTotal() - $baseTax);
$address->setGrandTotal($address->getGrandTotal() - $tax);
}
}
protected function _saveAppliedTaxes(Mage_Sales_Model_Quote_Address $address, $applied, $amount, $baseAmount, $rate)
{
$previouslyAppliedTaxes = $address->getAppliedTaxes();
$process = count($previouslyAppliedTaxes);
foreach ($applied as $row) {
if (!isset($previouslyAppliedTaxes[$row['id']])) {
$row['process'] = $process;
$row['amount'] = 0;
$row['base_amount'] = 0;
$previouslyAppliedTaxes[$row['id']] = $row;
}
if (!is_null($row['percent'])) {
$row['percent'] = $row['percent'] ? $row['percent'] : 1;
$rate = $rate ? $rate : 1;
$appliedAmount = $amount/$rate*$row['percent'];
$baseAppliedAmount = $baseAmount/$rate*$row['percent'];
} else {
$appliedAmount = 0;
$baseAppliedAmount = 0;
foreach ($row['rates'] as $rate) {
$appliedAmount += $rate['amount'];
$baseAppliedAmount += $rate['base_amount'];
}
}
if ($appliedAmount || $previouslyAppliedTaxes[$row['id']]['amount']) {
$previouslyAppliedTaxes[$row['id']]['amount'] += $appliedAmount;
$previouslyAppliedTaxes[$row['id']]['base_amount'] += $baseAppliedAmount;
} else {
unset($previouslyAppliedTaxes[$row['id']]);
}
}
$address->setAppliedTaxes($previouslyAppliedTaxes);
}
public function fetch(Mage_Sales_Model_Quote_Address $address)
{
$address->addTotal(array(
'code' => $this->getCode(),
'title' => "Prep Print costs",
'value' => $address->getPrintPrepcosts(),
));
return $this;
}
编辑1
XML包含以下内容:
<sales>
<quote>
<totals>
<printPrepCosts>
<class>Company_Customer_Model_Quote_Address_Total_PrintPrepCosts</class>
<after>subtotal</after>
<before>tax</before>
</printPrepCosts>
</totals>
</quote>
</sales>
编辑2
我在calcTax函数中添加了以下几行,这确实解决了总计问题,但TAX的金额仍然关闭。
$address->setTaxAmount($tax);
$address->setBaseTaxAmount($baseTax);
输出-示例
Subtotal € 67,50
printPrepCosts € 40,00
Shipping € 50,00
TAX € 22,33
Total € 187,43
编辑3
不好意思,只有GoMage一页结帐可以提供正确的总计,而购物车没有。常规的Magento单页结帐也会给出错误的总计。
最佳答案
我找到了一种解决方法,它不是最好的解决方案,但是它确实有效:)
在我的地址中,我将税款设置为一个称为tax的额外变量,并在观察者中使用它来操纵税款。
class Company_Client_Model_Observer
{
public function setCorrectTax ($observer)
{
$quote = $observer->getQuote();
foreach ($quote->getAllAddresses() as $address) {
$printPrepCosts = $address->getPrintPrepcosts();
if(!empty($printPrepCosts)) {
$address->setTaxAmount($address->tax);
}
}
}
}
和XML
<events>
<sales_quote_collect_totals_after>
<observers>
<client>
<type>singleton</type>
<class>client/observer</class>
<method>setCorrectTax</method>
</client>
</observers>
</sales_quote_collect_totals_after>
</events>
关于php - Magento:在结帐时需支付额外费用的税款错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11799089/