Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ Magento 5-Rappen-Rundung for Swiss Magento Merchants

After installing the extension, please log out of your Magento backend and re-login, the go to

System > Configuration > General > Currency Setup > 5 Rappen Rundung
_System > Configuration > General > Currency Setup > 5 Rappen Rundung_

in order to enable the extension and set the rounding to either "All prices and totals" (including invoices) or "Totals only".

The difference between the resulting (rounded) and the initial (unrounded) amount is saved to the column _rounding_difference_ in both quote and order tables.

Credits:

Thanks to Andreas von Studnitz (http://www.avs-webentwicklung.de) for providing the code basis and Michael Baer (http://www.codeag.ch) for sponsoring the extension.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,23 @@ public function collect(Mage_Sales_Model_Quote_Address $address)
$grandTotal = $address->getGrandTotal();
$baseGrandTotal = $address->getBaseGrandTotal();

// at some circumstances the difference between rounded and unrounded totals can be caught on this place
$roundingDifference = 0.0;
/** @var Mage_Sales_Model_Quote_Item $item */
foreach ($address->getQuote()->getAllVisibleItems() as $item) {
$roundingDifference += $item->getRowTotalInclTax() - $item->getRowTotal() * (1 + $item->getTaxPercent()/100);
}

if (abs(round($roundingDifference, 2)) > 0) {
$address->getQuote()->setRoundingDifference($roundingDifference);
}


/* These totals if rounded are mostly wrong because they are excl. tax and tax is not rounded
* in the corresponding total. So it's actually a @fixme
* Rounding the total amount normally just corrects grand total
* It also makes no sense to calculate rounding difference here until tax total is fixed
*/
$totals = array_sum($address->getAllTotalAmounts());
$baseTotals = array_sum($address->getAllBaseTotalAmounts());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,45 @@ public function collect(Mage_Sales_Model_Quote_Address $address)
{
parent::collect($address);

$roundingDifference = $this->caculateItemRoundingDifference($address->getQuote());

/** @var $helper Openstream_RappenRounding_Helper_Data */
$helper = Mage::helper('rappenrounding');

if($helper->getScope() == 'totals' || $helper->getScope() == 'all') {
// no need to calculate difference for base currency since we only need the end result
$roundingDifference += $address->getSubtotalInclTax() - $helper->_roundBase5($address->getSubtotalInclTax());

$address->setSubtotalInclTax($helper->_roundBase5($address->getSubtotalInclTax()));
$address->setBaseSubtotalInclTax($helper->_roundBase5($address->getBaseSubtotalInclTax()));
$address->setTotalAmount('subtotal', $helper->_roundBase5($address->getTotalAmount('subtotal')));
$address->setBaseTotalAmount('subtotal', $helper->_roundBase5($address->getBaseTotalAmount('subtotal')));
}

$address->getQuote()->setRoundingDifference($roundingDifference);

return $this;
}

/**
* Calculate difference between unrounded and rounded row totals
*
* @param Mage_Sales_Model_Quote $quote
* @return float
*/
protected function caculateItemRoundingDifference($quote)
{
$difference = 0.0;

/** @var Mage_Sales_Model_Quote_Item $item */
foreach ($quote->getAllVisibleItems() as $item) {
$difference += $item->getRowTotalInclTax() - $item->getPriceInclTax() * $item->getQty();
}

$quote->setRoundingDifference($difference);

return $difference;
}


}
16 changes: 15 additions & 1 deletion app/code/community/Openstream/RappenRounding/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<config>
<modules>
<Openstream_RappenRounding>
<version>0.2.2</version>
<version>0.3.0</version>
</Openstream_RappenRounding>
</modules>
<global>
Expand All @@ -26,11 +26,25 @@
</rewrite>
</sales>
</models>
<resources>
<rappenrounding_setup>
<setup>
<module>Openstream_RappenRounding</module>
</setup>
</rappenrounding_setup>
</resources>
<helpers>
<rappenrounding>
<class>Openstream_RappenRounding_Helper</class>
</rappenrounding>
</helpers>
<fieldsets>
<sales_convert_quote>
<rounding_difference>
<to_order>*</to_order>
</rounding_difference>
</sales_convert_quote>
</fieldsets>
</global>
<adminhtml>
<translate>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/* @var $installer Mage_Core_Model_Resource_Setup */
$installer = $this;
$installer->startSetup();

$installer->getConnection()->addColumn(
$installer->getTable('sales/quote'),
'rounding_difference',
[
'type' => Varien_Db_Ddl_Table::TYPE_DECIMAL,
'length' => '12,4',
'comment' => 'Difference between rounded and unrounded grand total'
]
);

$installer->getConnection()->addColumn(
$installer->getTable('sales/order'),
'rounding_difference',
[
'type' => Varien_Db_Ddl_Table::TYPE_DECIMAL,
'length' => '12,4',
'comment' => 'Difference between rounded and unrounded grand total'
]
);