Magento的 - 应用税自定义报价汇总场(Magento - Apply Tax On Cust

2019-07-30 22:17发布

我已经创建了一个Magento的收费模块增加了一个自定义的总场的报价。 附加费被输入到Magento的含税。 我已经成功地得到了模块添加附加费的报价和总计为结帐页面上是正确的。

试图使之被纳入并在结账页面上的税字段中显示申请税附加费,当我的问题来了。 目前,它仅包含的产品和航运税收。

我已成功地计算附加费的税,使之显示在税收领域,而且不认为我的做法是完全正确要么cant't得到应用到报价的税收。 任何帮助将非常感激。

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');
}

Answer 1:

我设法解决这个这个使用下面的代码。 不是最好的解决办法,但花了几个小时试图做到这一点并没有得到任何地方快。 阿斯特里克斯的需要进行更换。

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');
}

}



文章来源: Magento - Apply Tax On Custom Quote Totals Field