Quantcast
Channel: Magento – Ken Nguyen
Viewing all articles
Browse latest Browse all 14

How to Tax working in Magento?

$
0
0

 

  1.  app/code/core/Mage/Tax/Model/Sales/Total/Quote/Subtotal.php

/**

* Calculate item price and row total including/excluding tax based on total price rounding level

*/

protected function _totalBaseCalculation($item, $request)

{

$calc = $this->_calculator;

$request->setProductClassId($item->getProduct()->getTaxClassId());

$rate = $calc->getRate($request);

$qty = $item->getTotalQty()

——

  1. app/code/core/Mage/Tax/Model/Calculation.php

/**

* Get calculation tax rate by specific request

*/

public function getRate($request)

{

if (!$request->getCountryId() || !$request->getCustomerClassId() || !$request->getProductClassId()) {

return 0;

}

$cacheKey = $this->_getRequestCacheKey($request);

 

if (!isset($this->_rateCache[$cacheKey])) {

——-

$key = $request->getStore() ? $request->getStore()->getId() . ‘|’ : ”;

$key .= $request->getProductClassId() . ‘|’ . $request->getCustomerClassId() . ‘|’

. $request->getCountryId() . ‘|’ . $request->getRegionId() . ‘|’ . $request->getPostcode();
$cacheKey =16|4|3|SG|Singapore|967545

 

  1.  app/code/core/Mage/Tax/Model/Resource/Calculation.php

/**

* Get tax rate information: calculation process data and tax rate

*/

3.1.  public function getRateInfo($request)

{

$rates = $this->_getRates($request);

[0] => Array

(

[tax_calculation_rate_id] => 3

[tax_calculation_rule_id] => 2

[customer_tax_class_id] => 3

[product_tax_class_id] => 4

[priority] => 0

[position] => 0

[calculate_subtotal] => 0

[value] => 7.0000

[tax_country_id] => SG

[tax_region_id] => 0

[tax_postcode] => *

[code] => SG

[title] => SG

)

 

return array(

‘process’   => $this->getCalculationProcess($request, $rates),

‘value’     => $this->_calculateRate($rates)

);

}

 

/**

* Returns tax rates for request – either pereforms SELECT from DB, or r

eturns already cached result

*/

3.2. protected function _getRates($request)

{

// Extract params that influence our SELECT statement and use them to create cache key

$storeId = Mage::app()->getStore($request->getStore())->getId();

$customerClassId = $request->getCustomerClassId();

$countryId = $request->getCountryId();

$regionId = $request->getRegionId();

$postcode = $request->getPostcode();

 

// Process productClassId as it can be array or usual value. Form best key for cache.

$productClassId = $request->getProductClassId();

$ids = is_array($productClassId) ? $productClassId : array($productClassId);

foreach ($ids as $key => $val) {

$ids[$key] = (int) $val; // Make it integer for equal cache keys even in case of null/false/0 values

}

$ids = array_unique($ids);

sort($ids);

$productClassKey = implode(‘,’, $ids);

 

// Form cache key and either get data from cache or from DB

$cacheKey = implode(‘|’, array($storeId, $customerClassId, $productClassKey, $countryId, $regionId, $postcode));

 

if (!isset($this->_ratesCache[$cacheKey])) {

// Make SELECT and get data

$select = $this->_getReadAdapter()->select();

$select

->from(array(‘main_table’ => $this->getMainTable()),

array(‘tax_calculation_rate_id’,

‘tax_calculation_rule_id’,

‘customer_tax_class_id’,

‘product_tax_class_id’

)

)

->where(‘customer_tax_class_id = ?’, (int)$customerClassId);

if ($productClassId) {

$select->where(‘product_tax_class_id IN (?)’, $productClassId);

}

$ifnullTitleValue = $this->_getReadAdapter()->getCheckSql(

‘title_table.value IS NULL’,

‘rate.code’,

‘title_table.value’

);

 

  1.  tax_calculation table

select * from tax_calculation;

+--------------------+-------------------------+-------------------------+-----------------------+----------------------+

| tax_calculation_id | tax_calculation_rate_id | tax_calculation_rule_id | customer_tax_class_id | product_tax_class_id |

+--------------------+-------------------------+-------------------------+-----------------------+----------------------+

|                  4 |                       3 |                       2 |                     3 |                    2 |

|                  5 |                       3 |                       2 |                     3 |                    4 |

+--------------------+-------------------------+-------------------------+-----------------------+----------------------+

select * from tax_calculation_rate;

+-------------------------+----------------+---------------+--------------+------+--------+--------------+----------+--------+

| tax_calculation_rate_id | tax_country_id | tax_region_id | tax_postcode | code | rate   | zip_is_range | zip_from | zip_to |

+-------------------------+----------------+---------------+--------------+------+--------+--------------+----------+--------+

|                       3 | SG             |             0 | *            | SG   | 7.0000 |         NULL |     NULL |   NULL |

+-------------------------+----------------+---------------+--------------+------+--------+--------------+----------+--------+

///////////////////////////////////////////////


Viewing all articles
Browse latest Browse all 14

Trending Articles