In Magento there is a functionality where you can define the order of total calculation by specifing before and after which totals a total should be run.
I added a c
EDIT: This answer is wrong. See the discussion in the comments.
As Vinai noted, the problem is that the order function returns 0 even if the parameters are not equal. I modified the function to fall back on the string order of the keys as follows:
protected function _compareTotals($a, $b)
{
$aCode = $a['_code'];
$bCode = $b['_code'];
if (in_array($aCode, $b['after']) || in_array($bCode, $a['before'])) {
$res = -1;
} elseif (in_array($bCode, $a['after']) || in_array($aCode, $b['before'])) {
$res = 1;
} else {
$res = strcmp($aCode, $bCode); // was $res = 0 before
}
return $res;
}
Thanks for persisting @Alex, here is a better answer with a better explanation :) My first answer was wrong.
PHP implements the quicksort for all array sorting functions (reference zend_qsort.c).
If two records in the array are identical, their place will be swapped.
The problem is the giftwrap total record, which, according to _compareTotals()
, is larger then subtotal and nominal but equal to all other totals.
Depending on the original order of the $confArray
input array and on the placement of the pivot element it is legal to swap giftwrap with e.g. discount, because both are equal, even though discount is larger then shipping.
This might make the problem clearer from the sorting algorithms point of view:
There are several possible solutions, even though the original problem is the choice of quicksort to build a directed acyclic dependency graph
Interestingly there are not many PHP packages floating around. There is an orphaned PEAR package Structures_Graph. Using that would probably be the quick solution, but it would mean transforming the $confArray
into a Structures_Graph
structure (so maybe not that quick).
Wikipedia does a good job of explaining the problem, so rolling your own solution might be a fun challenge. The German Wikipedia topological sorting page breaks down the problem into logical steps and also has a great example algorithm in PERL.
I decided to go with Plan B, overloading the getSortedCollectors... its straight forward and gives me absolut control, if course if I would introduce new modules I would have to check if I need to add them here
<?php
class YourModule_Sales_Model_Total_Quote_Collector extends Mage_Sales_Model_Quote_Address_Total_Collector {
protected function _getSortedCollectorCodes() {
return array(
'nominal',
'subtotal',
'msrp',
'freeshipping',
'tax_subtotal',
'weee',
'shipping',
'tax_shipping',
'floorfee',
'bottlediscount',
'discount',
'tax',
'grand_total',
);
}
}
Well was stucked at this for years!!!+
Now i know why some projects of the past were so difficult to adjust regarding wee and tax combinations a nightmare i could say, never got to understand why, yesterday i found why, later i found this article, a real shame.. but most of the time i need to know the response to be capable of search the question..
And the obvius solution at least for linux heads without fear, is the code below, basically i make use of the ancient linux command tsort that especifically does a topolocical order in the way we need here..
For the entomological and archeologist souls among us some pointers http://www.gnu.org/software/coreutils/manual/html_node/tsort-invocation.html,I am using authentic 80's technology... yummmm
/**
* Aggregate before/after information from all items and sort totals based on this data
*
* @return array
*/
protected function _getSortedCollectorCodes() {
if (Mage::app()->useCache('config')) {
$cachedData = Mage::app()->loadCache($this->_collectorsCacheKey);
if ($cachedData) {
return unserialize($cachedData);
}
}
$configArray = $this->_modelsConfig;
// invoke simple sorting if the first element contains the "sort_order" key
reset($configArray);
$element = current($configArray);
if (isset($element['sort_order'])) {
uasort($configArray, array($this, '_compareSortOrder'));
$sortedCollectors = array_keys($configArray);
} else {
foreach ($configArray as $code => $data) {
foreach ($data['before'] as $beforeCode) {
if (!isset($configArray[$beforeCode])) {
continue;
}
$configArray[$code]['before'] = array_merge(
$configArray[$code]['before'],
$configArray[$beforeCode]['before']);
$configArray[$code]['before'] = array_unique(
$configArray[$code]['before']);
$configArray[$beforeCode]['after'] = array_merge(
$configArray[$beforeCode]['after'], array($code),
$data['after']);
$configArray[$beforeCode]['after'] = array_unique(
$configArray[$beforeCode]['after']);
}
foreach ($data['after'] as $afterCode) {
if (!isset($configArray[$afterCode])) {
continue;
}
$configArray[$code]['after'] = array_merge(
$configArray[$code]['after'],
$configArray[$afterCode]['after']);
$configArray[$code]['after'] = array_unique(
$configArray[$code]['after']);
$configArray[$afterCode]['before'] = array_merge(
$configArray[$afterCode]['before'], array($code),
$data['before']);
$configArray[$afterCode]['before'] = array_unique(
$configArray[$afterCode]['before']);
}
}
//uasort($configArray, array($this, '_compareTotals'));
$res = "";
foreach ($configArray as $code => $data) {
foreach ($data['before'] as $beforeCode) {
if (!isset($configArray[$beforeCode])) {
continue;
}
$res = $res . "$code $beforeCode\n";
}
foreach ($data['after'] as $afterCode) {
if (!isset($configArray[$afterCode])) {
continue;
}
$res = $res . "$afterCode $code\n";
}
}
file_put_contents(Mage::getBaseDir('tmp')."/graph.txt", $res);
$sortedCollectors=explode("\n",shell_exec('tsort '.Mage::getBaseDir('tmp')."/graph.txt"),-1);
}
if (Mage::app()->useCache('config')) {
Mage::app()
->saveCache(serialize($sortedCollectors),
$this->_collectorsCacheKey,
array(Mage_Core_Model_Config::CACHE_TAG));
}
return $sortedCollectors;
}
I've posted the complete funcion for the sake of completeness, and of course is working like a charm for me at least...
So finally, here is my patch for this issue.
It implements topological sorting as suggested by Vinai.
app/code/core/Mage/Sales/Model/Config/Ordered.php
to app/code/local/Mage/Sales/Model/Config/Ordered.php
total-sorting.patch
and call patch -p0 app/code/local/Mage/Sales/Model/Config/Ordered.php
In case of upgrades make sure to re-apply these steps.
The patch is tested to work with Magento 1.7.0.2
--- app/code/core/Mage/Sales/Model/Config/Ordered.php 2012-08-14 14:19:50.306504947 +0200 +++ app/code/local/Mage/Sales/Model/Config/Ordered.php 2012-08-15 10:00:47.027003404 +0200 @@ -121,6 +121,78 @@ return $totalConfig; } +// [PATCHED CODE BEGIN] + + /** + * Topological sort + * + * Copyright: http://www.calcatraz.com/blog/php-topological-sort-function-384 + * And fix see comment on http://stackoverflow.com/questions/11953021/topological-sorting-in-php + * + * @param $nodeids Node Ids + * @param $edges Array of Edges. Each edge is specified as an array with two elements: The source and destination node of the edge + * @return array|null + */ + function topological_sort($nodeids, $edges) { + $L = $S = $nodes = array(); + foreach($nodeids as $id) { + $nodes[$id] = array('in'=>array(), 'out'=>array()); + foreach($edges as $e) { + if ($id==$e[0]) { $nodes[$id]['out'][]=$e[1]; } + if ($id==$e[1]) { $nodes[$id]['in'][]=$e[0]; } + } + } + foreach ($nodes as $id=>$n) { if (empty($n['in'])) $S[]=$id; } + while ($id = array_shift($S)) { + if (!in_array($id, $L)) { + $L[] = $id; + foreach($nodes[$id]['out'] as $m) { + $nodes[$m]['in'] = array_diff($nodes[$m]['in'], array($id)); + if (empty($nodes[$m]['in'])) { $S[] = $m; } + } + $nodes[$id]['out'] = array(); + } + } + foreach($nodes as $n) { + if (!empty($n['in']) or !empty($n['out'])) { + return null; // not sortable as graph is cyclic + } + } + return $L; + } + + /** + * Sort config array + * + * public to be easily accessable by test + * + * @param $configArray + * @return array + */ + public function _topSortConfigArray($configArray) + { + $nodes = array_keys($configArray); + $edges = array(); + + foreach ($configArray as $code => $data) { + $_code = $data['_code']; + if (!isset($configArray[$_code])) continue; + foreach ($data['before'] as $beforeCode) { + if (!isset($configArray[$beforeCode])) continue; + $edges[] = array($_code, $beforeCode); + } + + foreach ($data['after'] as $afterCode) { + if (!isset($configArray[$afterCode])) continue; + $edges[] = array($afterCode, $_code); + } + } + return $this->topological_sort($nodes, $edges); + } + +// [PATCHED CODE END] + + /** * Aggregate before/after information from all items and sort totals based on this data * @@ -138,38 +210,16 @@ // invoke simple sorting if the first element contains the "sort_order" key reset($configArray); $element = current($configArray); + // [PATCHED CODE BEGIN] if (isset($element['sort_order'])) { uasort($configArray, array($this, '_compareSortOrder')); + $sortedCollectors = array_keys($configArray); + } else { - foreach ($configArray as $code => $data) { - foreach ($data['before'] as $beforeCode) { - if (!isset($configArray[$beforeCode])) { - continue; - } - $configArray[$code]['before'] = array_unique(array_merge( - $configArray[$code]['before'], $configArray[$beforeCode]['before'] - )); - $configArray[$beforeCode]['after'] = array_merge( - $configArray[$beforeCode]['after'], array($code), $data['after'] - ); - $configArray[$beforeCode]['after'] = array_unique($configArray[$beforeCode]['after']); - } - foreach ($data['after'] as $afterCode) { - if (!isset($configArray[$afterCode])) { - continue; - } - $configArray[$code]['after'] = array_unique(array_merge( - $configArray[$code]['after'], $configArray[$afterCode]['after'] - )); - $configArray[$afterCode]['before'] = array_merge( - $configArray[$afterCode]['before'], array($code), $data['before'] - ); - $configArray[$afterCode]['before'] = array_unique($configArray[$afterCode]['before']); - } - } - uasort($configArray, array($this, '_compareTotals')); + $sortedCollectors = $this->_topSortConfigArray($configArray); } - $sortedCollectors = array_keys($configArray); + // [PATCHED CODE END] + if (Mage::app()->useCache('config')) { Mage::app()->saveCache(serialize($sortedCollectors), $this->_collectorsCacheKey, array( Mage_Core_Model_Config::CACHE_TAG @@ -196,27 +246,6 @@ } /** - * Callback that uses after/before for comparison - * - * @param array $a - * @param array $b - * @return int - */ - protected function _compareTotals($a, $b) - { - $aCode = $a['_code']; - $bCode = $b['_code']; - if (in_array($aCode, $b['after']) || in_array($bCode, $a['before'])) { - $res = -1; - } elseif (in_array($bCode, $a['after']) || in_array($aCode, $b['before'])) { - $res = 1; - } else { - $res = 0; - } - return $res; - } - - /** * Callback that uses sort_order for comparison * * @param array $a
EDIT: There is also another suggested change (for Magento 2): https://github.com/magento/magento2/pull/49
The discussion above clearly indicate the problem. Usual sort doesn't work on the set of data without order function to be defied between any two element of the set. If only some of relational is defined like a "partial dependency" then topological sort must be used to obey declared "before" and "after" statement. In my test these declaration was broken in the resulted set depending of that and there I add additional modules. The scare part, it was not only affecting additional module but could change entire sorting result in unpredictable way. So, I implement standard topological sort which does solve the problem:
/**
* The source data of the nodes and their dependencies, they are not required to be symmetrical node cold list other
* node in 'after' but not present in its 'before' list:
* @param $configArray
* $configArray = [
* <nodeCode> => ["_code"=> <nodeCode>, "after"=> [<dependsOnNodeCode>, ...], "before"=> [<dependedByCode>, ...] ],
* ...
* ]
* The procedure updates adjacency list , to have every edge to be listed in the both nodes (in one 'after' and other 'before')
*/
function normalizeDependencies(&$configArray) {
//For each node in the source data
foreach ($configArray as $code => $data) {
//Look up all nodes listed 'before' and update their 'after' for consistency
foreach ($data['before'] as $beforeCode) {
if (!isset($configArray[$beforeCode])) {
continue;
}
$configArray[$beforeCode]['after'] = array_unique(array_merge(
$configArray[$beforeCode]['after'], array($code)
));
}
//Look up all nodes listed 'after' and update their 'before' for consistency
foreach ($data['after'] as $afterCode) {
if (!isset($configArray[$afterCode])) {
continue;
}
$configArray[$afterCode]['before'] = array_unique(array_merge(
$configArray[$afterCode]['before'], array($code)
));
}
}
}
/**
* http://en.wikipedia.org/wiki/Topological_sorting
* Implements Kahn (1962) algorithms
*/
function topoSort(&$array) {
normalizeDependencies($array);
$result = array(); // Empty list that will contain the sorted elements
$front = array(); // Set of all nodeCodes with no incoming edges
//Push all items with no predecessors in S;
foreach ($array as $code => $data) {
if ( empty ($data['after']) ) {
$front[] = $code;
}
}
// While 'front' is not empty
while (! empty ($front)) {
//Deque 'frontier' from 'front'
$frontierCode = array_shift($front);
//Push it in 'result'
$result[$frontierCode]= $array[$frontierCode];
// Remove all outgoing edges from 'frontier';
while (! empty ($array[$frontierCode]['before'])) {
$afterCode = array_shift($array[$frontierCode]['before']);
// remove corresponding edge e from the graph
$array[$afterCode]['after'] = array_remove($array[$afterCode]['after'], $frontierCode);
//* if, no more decencies put node into processing queue:
// if m has no other incoming edges then
if ( empty ($array[$afterCode]['after']) ) {
// insert m into 'front'
array_push($front, $afterCode);
}
}
}
if(count($result) != count($array)){
saveGraph($array, 'mage-dependencies.dot');
throw new Exception("Acyclic dependencies in data, see graphviz diagram: mage-dependencies.dot for details.");
}
return $result;
}
/**
* Could not find better way to remove value from array
*
* @param $array
* @param $value
* @return array
*/
protected function array_remove($array, $value){
$cp = array();
foreach($array as $b) {
if($b != $value){
$cp[]=$b;
}
}
return $cp;
}
/**
* Saves graph in the graphviz format for visualisation:
* >dot -Tpng /tmp/dotfile.dot > viz-graph.png
*/
function saveGraph($configArray, $fileName){
$fp = fopen($fileName,'w');
fwrite($fp,"digraph TotalOrder\n");
fwrite($fp,"{\n");
foreach($configArray as $code=>$data) {
fwrite($fp,"$code;\n");
foreach($data['before'] as $beforeCode) {
fwrite($fp,"$beforeCode -> $code;\n");
}
foreach($data['after'] as $afterCode) {
fwrite($fp,"$code -> $afterCode;\n");
}
}
fwrite($fp,"}\n");
fclose($fp);
}
The question, how hard would be to get it (or other topo sort) into Magento release/hot fix?