forked from firegento/firegento-germansetup
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue firegento#13: Proportional shipping tax calculation (needs test…
…ing!)
- Loading branch information
Alexander Menk
committed
Mar 14, 2013
1 parent
6be1d51
commit 4c1c421
Showing
6 changed files
with
164 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
src/app/code/community/FireGento/GermanSetup/Model/Tax/Sales/Total/Quote/Tax.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
<?php | ||
/** | ||
* netz98 magento module | ||
* | ||
* LICENSE | ||
* | ||
* This source file is subject of netz98. | ||
* You may be not allowed to change the sources | ||
* without authorization of netz98 new media GmbH. | ||
* | ||
* @copyright Copyright (c) 1999-2013 netz98 new media GmbH (http://www.netz98.de) | ||
* @author netz98 new media GmbH <info@netz98.de> | ||
* @category N98 | ||
* @package N98_XXXXXXXX | ||
*/ | ||
|
||
/** | ||
* CLASS DESCRIPTION | ||
* | ||
* @category N98 | ||
* @package N98_XXXXXXXX | ||
*/ | ||
|
||
class FireGento_GermanSetup_Model_Tax_Sales_Total_Quote_Tax extends Mage_Tax_Model_Sales_Total_Quote_Tax { | ||
|
||
|
||
protected function _getQuoteItems() | ||
{ | ||
/* @var $session Mage_Checkout_Model_Session */ | ||
$session = Mage::getSingleton('checkout/session'); | ||
|
||
if ($session->hasQuote()) { | ||
$quoteItems = $session->getQuote()->getAllItems(); | ||
} else { | ||
$quoteItems = array(); | ||
} | ||
return $quoteItems; | ||
} | ||
|
||
/** | ||
* Calculates the portions of a quote with a specifc tax class | ||
* The price incl tax is used for the calculation | ||
* | ||
* @param $quoteItems | ||
* @return array (taxClassId -> percentage in quote) | ||
*/ | ||
protected function _collectTaxClassPortions($quoteItems) | ||
{ | ||
$taxClassIds = array(); | ||
|
||
// Fetch the tax rates from the quote items | ||
$taxClassSums = array(); | ||
$total = 0; | ||
foreach ($quoteItems as $item) { | ||
/** @var $item Mage_Sales_Model_Quote_Item */ | ||
if ($item->getParentItem()) { | ||
continue; | ||
} | ||
// sum up all product values grouped by the tax class id | ||
if (!isset($taxClassSums[$item->getTaxClassId()])) { | ||
$taxClassSums[$item->getTaxClassId()] = 0; | ||
} | ||
$rowSum = $item->getPriceInclTax() * $item->getQty(); | ||
$taxClassSums[$item->getTaxClassId()] += $rowSum; | ||
$total += $rowSum; | ||
} | ||
|
||
$portions = array(); | ||
foreach($taxClassSums as $taxClassId=>$sum) { | ||
$portions[$taxClassId] = $sum/$total; | ||
} | ||
return $portions; | ||
} | ||
|
||
/** | ||
* | ||
* Calculate proportional mixed tax | ||
* | ||
* TODO Works only for "Shipping cost incl Tax" | ||
* | ||
* @param Mage_Sales_Model_Quote_Address $address | ||
* @param Varien_Object $taxRateRequest | ||
* | ||
* @return $this|Mage_Tax_Model_Sales_Total_Quote | ||
*/ | ||
protected function _calculateShippingTax(Mage_Sales_Model_Quote_Address $address, $taxRateRequest) | ||
{ | ||
if (Mage::getStoreConfigFlag(FireGento_GermanSetup_Model_Tax_Config::XML_PATH_SHIPPING_TAX_ON_PRODUCT_TAX) | ||
!= FireGento_GermanSetup_Model_Tax_Config::USE_PROPORTIONALLY_MIXED_TAX) { | ||
return parent::_calculateShippingTax($address, $taxRateRequest); | ||
} | ||
|
||
if (!$address->getIsShippingInclTax()) { | ||
throw Exception('Not implemented'); | ||
} | ||
$portions = $this->_collectTaxClassPortions($this->_getQuoteItems()); | ||
|
||
$totalTaxable = $address->getShippingTaxable(); | ||
$totalBaseTaxable = $address->getBaseShippingTaxable(); | ||
$totalShippingTaxAmount = 0; | ||
$totalBaseShippingTaxAmount = 0; | ||
foreach($portions as $taxClassId=>$portion) { | ||
$address->setShippingTaxable($totalTaxable * $portion); | ||
$address->setBaseShippingTaxable($totalBaseTaxable * $portion); | ||
$this->_config->setSimulateClass($taxClassId); | ||
parent::_calculateShippingTax($address, $taxRateRequest); | ||
$this->_config->setSimulateClass(null); | ||
$totalShippingTaxAmount += $address->getShippingTaxAmount(); | ||
$totalBaseShippingTaxAmount += $address->getBaseShippingTaxAmount(); | ||
} | ||
$address->setShippingTaxAmount($totalShippingTaxAmount); | ||
$address->setBaseShippingTaxAmount($totalBaseShippingTaxAmount); | ||
|
||
// now we have to adjust the actual shipping cost | ||
// we just set it to brutto minus tax | ||
$address->setTotalAmount('shipping', $address->getShippingInclTax() - $totalShippingTaxAmount); | ||
$address->setBaseTotalAmount('shipping', $address->getBaseShippingInclTax() - $totalBaseShippingTaxAmount); | ||
|
||
$address->setShippingTaxable($totalTaxable); | ||
$address->setBaseShippingTaxable($totalBaseTaxable); | ||
|
||
$address->setShippingAmount($address->getTotalAmount('shipping')); | ||
$address->setBaseShippingAmount($address->getBaseTotalAmount('shipping')); | ||
|
||
return $this; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters