From f972b15f5fec4b431fb0566a8355bfca34748716 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Tue, 12 Jul 2016 19:22:14 +0200
Subject: [PATCH] FIX #5068

---
 htdocs/product/card.php | 44 +++++++++++++++++++++++++++++++++++++----
 1 file changed, 40 insertions(+), 4 deletions(-)

diff --git a/htdocs/product/card.php b/htdocs/product/card.php
index c269e560a4c..59ccdb79a1f 100644
--- a/htdocs/product/card.php
+++ b/htdocs/product/card.php
@@ -609,8 +609,20 @@ if (empty($reshook))
                     $pu_ttc = price2num($pu_ht * (1 + ($tva_tx / 100)), 'MU');
                 }
             }
-
+            
             if (GETPOST('propalid') > 0) {
+                // Define cost price for margin calculation
+                $buyprice=0;
+                if (($result = $propal->defineBuyPrice($pu_ht, GETPOST('remise_percent'), $object->id)) < 0)
+                {
+                    dol_syslog($langs->trans('FailedToGetCostPrice'));
+                    setEventMessage($langs->trans('FailedToGetCostPrice'), 'errors');
+                }
+                else
+                {
+                    $buyprice = $result;
+                }
+                
                 $result = $propal->addline(
                     $desc,
                     $pu_ht,
@@ -628,7 +640,7 @@ if (empty($reshook))
                     0,
                     0,
                     0,
-                    0,
+                    $buyprice,
                     '',
                     '',
                     '',
@@ -642,6 +654,18 @@ if (empty($reshook))
 
                 setEventMessages($langs->trans("ErrorUnknown") . ": $result", null, 'errors');
             } elseif (GETPOST('commandeid') > 0) {
+                // Define cost price for margin calculation
+                $buyprice=0;
+                if (($result = $commande->defineBuyPrice($pu_ht, GETPOST('remise_percent'), $object->id)) < 0)
+                {
+                    dol_syslog($langs->trans('FailedToGetCostPrice'));
+                    setEventMessage($langs->trans('FailedToGetCostPrice'), 'errors');
+                }
+                else
+                {
+                    $buyprice = $result;
+                }
+                
                 $result = $commande->addline(
                     $desc,
                     $pu_ht,
@@ -662,7 +686,7 @@ if (empty($reshook))
                     0,
                     0,
                     null,
-                    0,
+                    $buyprice,
                     '',
                     0,
                     $object->fk_unit
@@ -673,6 +697,18 @@ if (empty($reshook))
                     exit;
                 }
             } elseif (GETPOST('factureid') > 0) {
+                // Define cost price for margin calculation
+                $buyprice=0;
+                if (($result = $facture->defineBuyPrice($pu_ht, GETPOST('remise_percent'), $object->id)) < 0)
+                {
+                    dol_syslog($langs->trans('FailedToGetCostPrice'));
+                    setEventMessage($langs->trans('FailedToGetCostPrice'), 'errors');
+                }
+                else
+                {
+                    $buyprice = $result;
+                }
+                
                 $result = $facture->addline(
                     $desc,
                     $pu_ht,
@@ -696,7 +732,7 @@ if (empty($reshook))
                     0,
                     0,
                     null,
-                    0,
+                    $buyprice,
                     '',
                     0,
                     100,
-- 
GitLab