我在结帐时添加了额外费用,但税款计算不正确。
如果我这样做 $this->_calculateTax($address); 在收集功能中,它被加到包括税的总额中,但我的税额仍然是当时的。
if is 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 单页结帐也会给出错误的总计。