diff --git a/ChangeLog b/ChangeLog index 3bbd3412f22..8253767a5e5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -58,6 +58,7 @@ For users: - New: [ task #923 ] Localtax support for ODT templates. - New: [ task #90 ] Barcode search. - New: Can send an email from thirdparty card. +- Fix: [bug #1022] correct margin calculation for credit notes For translators: - Qual: Normalized sort order of all languages files with english reference files. diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 6d89e18c77d..aef2289ab2e 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -3129,53 +3129,80 @@ abstract class CommonObject // calcul des marges if (isset($line->fk_remise_except) && isset($conf->global->MARGIN_METHODE_FOR_DISCOUNT)) { // remise + $pa = $line->qty * $line->pa_ht; + $pv = $line->qty * $line->subprice * (1 - $line->remise_percent / 100); if ($conf->global->MARGIN_METHODE_FOR_DISCOUNT == '1') { // remise globale considérée comme produit - $marginInfos['pa_products'] += $line->pa_ht;// ($line->pa_ht != 0)?$line->pa_ht:$line->subprice * (1 - $line->remise_percent / 100); - $marginInfos['pv_products'] += $line->subprice * (1 - $line->remise_percent / 100); - $marginInfos['pa_total'] += $line->pa_ht;// ($line->pa_ht != 0)?$line->pa_ht:$line->subprice * (1 - $line->remise_percent / 100); - $marginInfos['pv_total'] += $line->subprice * (1 - $line->remise_percent / 100); + $marginInfos['pa_products'] += $pa; + $marginInfos['pv_products'] += $pv; + $marginInfos['pa_total'] += $pa; + $marginInfos['pv_total'] += $pv; + // if credit note, margin = -1 * (abs(selling_price) - buying_price) + if ($pv < 0) + $marginInfos['margin_on_products'] += -1 * (abs($pv) - $pa); + else + $marginInfos['margin_on_products'] += $pv - $pa; } elseif ($conf->global->MARGIN_METHODE_FOR_DISCOUNT == '2') { // remise globale considérée comme service - $marginInfos['pa_services'] += $line->pa_ht;// ($line->pa_ht != 0)?$line->pa_ht:$line->subprice * (1 - $line->remise_percent / 100); - $marginInfos['pv_services'] += $line->subprice * (1 - ($line->remise_percent / 100)); - $marginInfos['pa_total'] += $line->pa_ht;// ($line->pa_ht != 0)?$line->pa_ht:$line->subprice * (1 - $line->remise_percent / 100); - $marginInfos['pv_total'] += $line->subprice * (1 - $line->remise_percent / 100); + $marginInfos['pa_services'] += $pa; + $marginInfos['pv_services'] += $pv; + $marginInfos['pa_total'] += $pa; + $marginInfos['pv_total'] += $pv; + // if credit note, margin = -1 * (abs(selling_price) - buying_price) + if ($pv < 0) + $marginInfos['margin_on_services'] += -1 * (abs($pv) - $pa); + else + $marginInfos['margin_on_services'] += $pv - $pa; } elseif ($conf->global->MARGIN_METHODE_FOR_DISCOUNT == '3') { // remise globale prise en compte uniqt sur total - $marginInfos['pa_total'] += $line->pa_ht;// ($line->pa_ht != 0)?$line->pa_ht:$line->subprice * (1 - $line->remise_percent / 100); - $marginInfos['pv_total'] += $line->subprice * (1 - ($line->remise_percent / 100)); + $marginInfos['pa_total'] += $pa; + $marginInfos['pv_total'] += $pv; } } else { $type=$line->product_type?$line->product_type:$line->fk_product_type; if ($type == 0) { // product - $marginInfos['pa_products'] += $line->qty * $line->pa_ht; - $marginInfos['pv_products'] += $line->qty * $line->subprice * (1 - $line->remise_percent / 100); - $marginInfos['pa_total'] += $line->qty * $line->pa_ht; - $marginInfos['pv_total'] += $line->qty * $line->subprice * (1 - $line->remise_percent / 100); + $pa = $line->qty * $line->pa_ht; + $pv = $line->qty * $line->subprice * (1 - $line->remise_percent / 100); + $marginInfos['pa_products'] += $pa; + $marginInfos['pv_products'] += $pv; + $marginInfos['pa_total'] += $pa; + $marginInfos['pv_total'] += $pv; + // if credit note, margin = -1 * (abs(selling_price) - buying_price) + if ($pv < 0) + $marginInfos['margin_on_products'] += -1 * (abs($pv) - $pa); + else + $marginInfos['margin_on_products'] += $pv - $pa; } elseif ($type == 1) { // service - $marginInfos['pa_services'] += $line->qty * $line->pa_ht; - $marginInfos['pv_services'] += $line->qty * $line->subprice * (1 - ($line->remise_percent / 100)); - $marginInfos['pa_total'] += $line->qty * $line->pa_ht; - $marginInfos['pv_total'] += $line->qty * $line->subprice * (1 - $line->remise_percent / 100); + $pa = $line->qty * $line->pa_ht; + $pv = $line->qty * $line->subprice * (1 - $line->remise_percent / 100); + $marginInfos['pa_services'] += $pa; + $marginInfos['pv_services'] += $pv; + $marginInfos['pa_total'] += $pa; + $marginInfos['pv_total'] += $pv; + // if credit note, margin = -1 * (abs(selling_price) - buying_price) + if ($pv < 0) + $marginInfos['margin_on_services'] += -1 * (abs($pv) - $pa); + else + $marginInfos['margin_on_services'] += $pv - $pa; } } } - - $marginInfos['margin_on_products'] = $marginInfos['pv_products'] - $marginInfos['pa_products']; if ($marginInfos['pa_products'] > 0) $marginInfos['margin_rate_products'] = 100 * round($marginInfos['margin_on_products'] / $marginInfos['pa_products'],5); if ($marginInfos['pv_products'] > 0) $marginInfos['mark_rate_products'] = 100 * round($marginInfos['margin_on_products'] / $marginInfos['pv_products'],5); - $marginInfos['margin_on_services'] = $marginInfos['pv_services'] - $marginInfos['pa_services']; if ($marginInfos['pa_services'] > 0) $marginInfos['margin_rate_services'] = 100 * round($marginInfos['margin_on_services'] / $marginInfos['pa_services'],5); if ($marginInfos['pv_services'] > 0) $marginInfos['mark_rate_services'] = 100 * round($marginInfos['margin_on_services'] / $marginInfos['pv_services'],5); - $marginInfos['total_margin'] = $marginInfos['pv_total'] - $marginInfos['pa_total']; + // if credit note, margin = -1 * (abs(selling_price) - buying_price) + if ($marginInfos['pv_total'] < 0) + $marginInfos['total_margin'] = -1 * (abs($marginInfos['pv_total']) - $marginInfos['pa_total']); + else + $marginInfos['total_margin'] = $marginInfos['pv_total'] - $marginInfos['pa_total']; if ($marginInfos['pa_total'] > 0) $marginInfos['total_margin_rate'] = 100 * round($marginInfos['total_margin'] / $marginInfos['pa_total'],5); if ($marginInfos['pv_total'] > 0) diff --git a/htdocs/core/tpl/objectline_edit.tpl.php b/htdocs/core/tpl/objectline_edit.tpl.php index ac05f71b901..c77e24e8bbd 100644 --- a/htdocs/core/tpl/objectline_edit.tpl.php +++ b/htdocs/core/tpl/objectline_edit.tpl.php @@ -141,13 +141,21 @@ if (! empty($conf->global->MAIN_VIEW_LINE_NUMBER)) { if (! empty($conf->global->DISPLAY_MARGIN_RATES)) { $margin_rate = (isset($_POST["marginRate"])?$_POST["marginRate"]:(($line->pa_ht == 0)?'':price($line->marge_tx))); - echo '