A lot of people seem to have had this problem over the years, but I did not find an answer anywhere. I have tried to find a solution, or at least a workaround. So I share this here to maybe start a new discussion.
There where (at least) two problems with the order editor:
1) For some reason (which I did not find out) it mixed up the order of the different totals.
2) Tax was not calculated on discounts and extras.
Here my attempt at a solution. I'm using AJAX, so I only modified edit_orders_ajax.php but it should work the same for edit_orders.php, I guess.
1) To define a sort order for components:
At the beginning of edit_orders_ajax.php ADD:
$sort_order = array('ot_subtotal' => '1',
'ot_shipping' => '3',
'ot_modul_spesen' => '4',
'ot_custom' => '5',
'ot_total' => '7',
'ot_tax' => '8');
Or, use whatever sort order you have defined in your order totals modules. However, ot_tax should be placed after discounts and custom totals to ensure that tax is calculated properly.
REPLACE all
'sort_order' => $j);
for components that show up in the $order_totals array, WITH:
'sort_order' => ((array_key_exists($order_totals[$i]['code'], $sort_order)) ? $sort_order[$order_totals[$i]['code']] : '5'));
for other components, WITH:
'sort_order' => ((array_key_exists($ot_class, $sort_order)) ? $sort_order[$ot_class] : '5'));
2) The tax problem:
AFTER:
if ($ot_title != '') { //7
ADD:
//This changes the value of ot_tax
if ($order_totals[$i]['code'] == 'ot_tax') {
$order_totals[$i]['value'] += $tax_to_add;
$order_totals[$i]['text'] = $currencies->format($order_totals[$i]['value'], true, $order->info['currency'], $order->info['currency_value']);
}
//This calculates the tax on additional components
if (($order_totals[$i]['code'] != 'ot_total') && ($order_totals[$i]['code'] != 'ot_subtotal') && ($order_totals[$i]['code'] != 'ot_tax') && ($order_totals[$i]['code'] != 'ot_shipping')) {
$tax_to_add += $order_totals[$i]['value'] * $tax / (100 + $tax);
}
AFTER:
} elseif ( (tep_not_null($ot_value)) && (tep_not_null($ot_title)) ) {
ADD:
//This calculates tax on ot_custom
$tax_to_add += $ot_value * $tax / (100 + $tax);
Hope it works for others as well...