diff --git a/README.md b/README.md index 70c8988..0b7015a 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/app/code/community/Openstream/RappenRounding/Model/SalesQuoteAddressTotalGrand.php b/app/code/community/Openstream/RappenRounding/Model/SalesQuoteAddressTotalGrand.php index a0e9f4c..d5656ea 100644 --- a/app/code/community/Openstream/RappenRounding/Model/SalesQuoteAddressTotalGrand.php +++ b/app/code/community/Openstream/RappenRounding/Model/SalesQuoteAddressTotalGrand.php @@ -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()); diff --git a/app/code/community/Openstream/RappenRounding/Model/TaxSalesTotalQuoteSubtotal.php b/app/code/community/Openstream/RappenRounding/Model/TaxSalesTotalQuoteSubtotal.php index 01469fa..db9e374 100644 --- a/app/code/community/Openstream/RappenRounding/Model/TaxSalesTotalQuoteSubtotal.php +++ b/app/code/community/Openstream/RappenRounding/Model/TaxSalesTotalQuoteSubtotal.php @@ -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; + } + } diff --git a/app/code/community/Openstream/RappenRounding/etc/config.xml b/app/code/community/Openstream/RappenRounding/etc/config.xml index c1f0f41..fad5d01 100644 --- a/app/code/community/Openstream/RappenRounding/etc/config.xml +++ b/app/code/community/Openstream/RappenRounding/etc/config.xml @@ -2,7 +2,7 @@ - 0.2.2 + 0.3.0 @@ -26,11 +26,25 @@ + + + + Openstream_RappenRounding + + + Openstream_RappenRounding_Helper + + + + * + + + diff --git a/app/code/community/Openstream/RappenRounding/sql/rappenrounding_setup/upgrade-0.2.2-0.3.0.php b/app/code/community/Openstream/RappenRounding/sql/rappenrounding_setup/upgrade-0.2.2-0.3.0.php new file mode 100644 index 0000000..cc14657 --- /dev/null +++ b/app/code/community/Openstream/RappenRounding/sql/rappenrounding_setup/upgrade-0.2.2-0.3.0.php @@ -0,0 +1,24 @@ +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' + ] +); \ No newline at end of file