In Magento we have the problem, that Mage_Sales_Model_Config_Ordered
should sort a bunch of items topological, but unfortunately it doesn't.
I encountered the problem with wrongly sorted totals a few times, but now it was enough. I wrote a blog article about it. Thanks to @Daniel_Sloof, he pointed me to more informations from @VinaiKopp. I then found a question on Stackoverflow from @s3lf.
After reading all this I decided to implement a module which fixes this. Thanks to Dan Mossop a topo sort library for PHP already exists. But it was untested, so I decided to write a few tests for it and then write a Magento module. But we are all lazy and why write tests, if already someone else did. So lets use the library from Marc J. Schmidt.
Unfortunately is the Magento bug in an abstract class, therefore we have to overwrite a core file in app/code/local
Install the library fro Marc J. Schmidt as a composer dependency by running composer require marcj/topsort
Install this repo via modman or copy the code from src/app/code/local/Mage/Sales/Model/Config/Ordered.php to app/code/local/Mage/Sales/Model/Config/Ordered.php
manually.
This module takes a library for topological sorting and fixes the broken algorithm to sort lots of stuff which has before
and after
dependencies, like totals in quote, order and creditmemo.
#License
My work is under BSD 3-clause. But be careful, I only changed the \Mage_Sales_Model_Config_Ordered::_getSortedCollectorCodes
implementation and added the topological sorting to the class. The rest is copied from Magento version 1.9.1