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 fe1533b
Showing
6 changed files
with
173 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
138 changes: 138 additions & 0 deletions
138
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,138 @@ | ||
<?php | ||
/** | ||
* This file is part of the FIREGENTO project. | ||
* | ||
* FireGento_GermanSetup is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License version 3 as | ||
* published by the Free Software Foundation. | ||
* | ||
* This script is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
* | ||
* PHP version 5 | ||
* | ||
* @category FireGento | ||
* @package FireGento_GermanSetup | ||
* @author FireGento Team <team@firegento.com> | ||
* @copyright 2013 FireGento Team (http://www.firegento.de). All rights served. | ||
* @license http://opensource.org/licenses/gpl-3.0 GNU General Public License, version 3 (GPLv3) | ||
* @version $Id:$ | ||
*/ | ||
|
||
/** | ||
* Tax total model for mixed tax class calculation | ||
* | ||
* @category FireGento | ||
* @package FireGento_GermanSetup | ||
* @author FireGento Team <team@firegento.com> | ||
* @copyright 2012 FireGento Team (http://www.firegento.de). All rights served. | ||
* @license http://opensource.org/licenses/gpl-3.0 GNU General Public License, version 3 (GPLv3) | ||
* @version $Id:$ | ||
*/ | ||
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
fe1533b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you very much :-) With your solution I was able to write my own shipping method with 6% tax and 21% tax for orders with (Dutch) products of different tax classes! Thanks again :-)