2012-08-10 88 views
3

我爲Magento創建了一個附加模塊,它爲報價添加了自定義總字段。 Magento收取附加費,包括稅費。我已經成功地獲得了該模塊,爲報價添加附加費,並且在結帳頁面上的總額是正確的。Magento - 在自定義報價總計字段應用稅

我的問題是,當試圖將稅款應用於附加費用,以便將其納入並顯示在結帳頁面上的稅收字段中時。目前它只包括產品稅和運費。

我已經設法計算了附加稅的稅額,但不能獲得適用於報價的稅額,以便它在稅收領域顯示,但也不認爲我的方法是非常正確的。任何幫助將非常感激。

class ********_Deposits_Model_Quote_Address_Total_Surcharge extends Mage_Sales_Model_Quote_Address_Total_Abstract { 

protected $_config = null; 

public function __construct() 
{ 
    $this->setCode('surcharge_price'); 
    $this->_config  = Mage::getSingleton('tax/config'); 
    $this->_store = Mage::app()->getStore(); 
} 

protected function _calculateTax(Mage_Sales_Model_Quote_Address $address) 
{ 
    $calculator  = Mage::getSingleton('tax/calculation'); 
    $inclTax  = $this->_config->priceIncludesTax($this->_store); 

    $taxRateRequest = $calculator->getRateRequest(
     $address, 
     $address->getQuote()->getBillingAddress(), 
     $address->getQuote()->getCustomerTaxClassId(), 
     $this->_store 
    ); 

    $taxRateRequest->setProductClassId(Mage::getStoreConfig('********/surcharges/tax_class', $this->_store)); 

    $rate = $calculator->getRate($taxRateRequest); 

    $baseTax = $tax = $calculator->calcTaxAmount($address->getBaseSurchargePriceAmount(), $rate, $inclTax, true); 


} 

/** 
* Collect address subtotal 
* 
* @param ********_Surcharges_Model_Quote_Address $address 
* @return ********_Surcharges_Model_Quote_Address_Total_Surcharge 
*/ 
public function collect(Mage_Sales_Model_Quote_Address $address) 
{ 

    parent::collect($address); 

    $this->_setAmount(0)->_setBaseAmount(0); 

    // If Surcharges Is Enabled Then Calculate Away :-) 
    if(Mage::getStoreConfig('********/surcharges/surcharge_enabled')) { 

     $items = $this->_getAddressItems($address); 
     if (!count($items)) { 
      return $this; 
     } 

     // Calculate Total Surcharge For Items In Quote (Base Prices!) 
     $surcharge = 0.0; 
     foreach ($items as $item) { 
     $price = $item->getData('base_surcharge_price', null); 
     if(isset($price)) { 
      $surcharge += $item->getData('base_surcharge_price') * $item->getQty(); 
     } 
     } 

    $this->_setAmount($surcharge); 
    $this->_setBaseAmount($surcharge); 

    $this->_calculateTax($address); 
    } 


    return $this; 
} 

public function fetch(Mage_Sales_Model_Quote_Address $address) 
{ 

    if(Mage::getStoreConfig('********/surcharges/surcharge_enabled')) { 
    $surcharge = $address->getSurchargePriceAmount(); 
    if(isset($surcharge) && $surcharge > 0) { 
     $address->addTotal(array(
      'code' => $this->getCode(), 
      'title' => Mage::getStoreConfig('********/surcharges/surcharge_label'), 
      'value' => $surcharge 
    )); 
    } 
    } 

    return $this; 
} 

public function getLabel() 
{ 
    return Mage::getStoreConfig('********/surcharges/surcharge_label'); 
} 
+0

阿什利嗨你設法得到這個工作? – 2012-11-21 02:04:30

+0

嗨喬納森。我最終通過有效地重新計算地址對象的稅額來做到了這一點。見下面的帖子! – Ashley 2012-11-22 12:13:21

回答

6

我設法解決這個問題,使用下面的代碼。不是最好的解決方案,但花了數小時試圖做到這一點,並沒有得到任何快速。 Asterix的需要被替換。

class *****_Deposits_Model_Quote_Address_Total_Surcharge extends Mage_Sales_Model_Quote_Address_Total_Abstract { 

protected $_taxConfig = null; 

public function __construct() 
{ 
    $this->setCode('surcharge_price'); 
    $this->_taxConfig  = Mage::getSingleton('tax/config'); 
    $this->_store = Mage::app()->getStore(); 
} 

protected function _calculateTax(Mage_Sales_Model_Quote_Address $address) 
{ 
    $calculator  = Mage::getSingleton('tax/calculation'); 
    $calculator->setCustomer($address->getQuote()->getCustomer()); 

    $inclTax  = $this->_taxConfig->priceIncludesTax($this->_store); 

    $taxRateRequest = $calculator->getRateRequest(
     $address, 
     $address->getQuote()->getBillingAddress(), 
     $address->getQuote()->getCustomerTaxClassId(), 
     $this->_store 
    ); 

    $taxRateRequest->setProductClassId(Mage::getStoreConfig('*****/surcharges/tax_class', $this->_store)); 

    $rate = $calculator->getRate($taxRateRequest); 

    if($rate > 0.0) { 
     $baseTax = $calculator->calcTaxAmount($address->getBaseSurchargePriceAmount(), $rate, $inclTax, true); 
     $tax = $address->getQuote()->getStore()->convertPrice($baseTax, false); 

     $address->addTotalAmount('tax', $tax); 
     $address->addBaseTotalAmount('tax', $baseTax); 

     $rates = array(); 
     foreach ($address->getAppliedTaxes() as $rate) { 
     $rate['amount'] = $rate['amount'] + $tax; 
     $rate['base_amount'] = $rate['base_amount'] + $baseTax; 
     $rates[] = $rate; 
     } 

     $address->setAppliedTaxes($rates); 

     if($inclTax) { 
     $address->setGrandTotal($address->getGrandTotal() - $tax); 
     $address->setBaseGrandTotal($address->getBaseGrandTotal() - $baseTax); 
     } 
    } 

} 

/** 
* Collect address subtotal 
* 
* @param *****_Surcharges_Model_Quote_Address $address 
* @return *****_Surcharges_Model_Quote_Address_Total_Surcharge 
*/ 
public function collect(Mage_Sales_Model_Quote_Address $address) 
{ 

    parent::collect($address); 

    // Clear Cached Values As Multiple Addresses Causes Values To Be Added Twice Otherwise! 
    $this->_setAmount(0)->_setBaseAmount(0); 

    // If Surcharges Is Enabled Then Calculate Away :-) 
    if(Mage::getStoreConfig('*****/surcharges/surcharge_enabled')) { 

     $items = $this->_getAddressItems($address); 
     if (!count($items)) { 
      return $this; 
     } 

     // Calculate Total Surcharge For Items In Quote (Base Prices!) 
     $surcharge = 0.0; 
     foreach ($items as $item) { 
     $price = $item->getData('base_surcharge_price', null); 
     if(isset($price)) { 
      $surcharge += $item->getData('base_surcharge_price') * $item->getQty(); 
     } 
     } 

    $this->_setAmount($address->getQuote()->getStore()->convertPrice($surcharge, false)); 
    $this->_setBaseAmount($surcharge); 

     $this->_calculateTax($address); 
    } 


    return $this; 
} 

/** 
* Assign subtotal amount and label to address object 
* 
* @param *****_Surcharges_Model_Quote_Address $address 
* @return *****_Surcharges_Model_Quote_Address_Total_Surcharge 
*/ 
public function fetch(Mage_Sales_Model_Quote_Address $address) 
{ 

    if(Mage::getStoreConfig('*****/surcharges/surcharge_enabled')) { 
    $surcharge = $address->getSurchargePriceAmount(); 
    if(isset($surcharge) && $surcharge > 0) { 
     $address->addTotal(array(
      'code' => $this->getCode(), 
      'title' => Mage::getStoreConfig('*****/surcharges/surcharge_label'), 
      'value' => $surcharge 
    )); 
    } 
    } 

    return $this; 
} 

/** 
* Get Surcharge label 
* 
* @return string 
*/ 
public function getLabel() 
{ 
    return Mage::getStoreConfig('*****/surcharges/surcharge_label'); 
} 

}

+0

它不工作magento 1.7可以任何機構給我1.7解決方案 – 2013-08-29 11:52:46

+0

這在Magento 1.7中正常工作。這是我爲它開發的。這不是完整的解決方案,只是其中的一部分。閱讀完整的問題,你會得到一些關於這個模塊是如何實現的。 – Ashley 2013-09-27 12:48:49

+0

+1在Magento EE 1.13.0.2上爲我工作 – 2013-10-15 13:18:09

相關問題