diff --git a/htdocs/accountancy/admin/productaccount.php b/htdocs/accountancy/admin/productaccount.php
index e29653a3e40..58ffa9bd150 100644
--- a/htdocs/accountancy/admin/productaccount.php
+++ b/htdocs/accountancy/admin/productaccount.php
@@ -186,18 +186,35 @@ if ($action == 'update') {
$msg .= '
'.$langs->trans("ErrorDB").' : '.$langs->trans("Product").' '.$productid.' '.$langs->trans("NotVentilatedinAccount").' : id='.$accounting_account_id.'
'.$sql.'
';
$ko++;
} else {
- $db->begin();
-
+ $sql = '';
if (!empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
- $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_perentity (fk_product, entity, '".$db->escape($accountancy_field_name)."')";
- $sql .= " VALUES (".((int) $productid).", ".((int) $conf->entity).", '".$db->escape($accounting->account_number)."')";
- $sql .= " ON DUPLICATE KEY UPDATE ".$accountancy_field_name." = '".$db->escape($accounting->account_number)."'";
+ $sql_exists = "SELECT rowid FROM " . MAIN_DB_PREFIX . "product_perentity";
+ $sql_exists .= " WHERE fk_product = " . ((int) $productid) . " AND entity = " . ((int) $conf->entity);
+ $resql_exists = $db->query($sql_exists);
+ if (!$resql_exists) {
+ $msg .= ''.$langs->trans("ErrorDB").' : '.$langs->trans("Product").' '.$productid.' '.$langs->trans("NotVentilatedinAccount").' : id='.$accounting_account_id.'
'.$resql_exists.'
';
+ $ko++;
+ } else {
+ $nb_exists = $db->num_rows($resql_exists);
+ if ($nb_exists <= 0) {
+ // insert
+ $sql = "INSERT INTO " . MAIN_DB_PREFIX . "product_perentity (fk_product, entity, '" . $db->escape($accountancy_field_name) . "')";
+ $sql .= " VALUES (" . ((int) $productid) . ", " . ((int) $conf->entity) . ", '" . $db->escape($accounting->account_number) . "')";
+ } else {
+ $obj_exists = $db->fetch_object($resql_exists);
+ // update
+ $sql = "UPDATE " . MAIN_DB_PREFIX . "product_perentity";
+ $sql .= " SET " . $accountancy_field_name . " = '" . $db->escape($accounting->account_number) . "'";
+ $sql .= " WHERE rowid = " . ((int) $obj_exists->rowid);
+ }
+ }
} else {
$sql = " UPDATE ".MAIN_DB_PREFIX."product";
$sql .= " SET ".$accountancy_field_name." = '".$db->escape($accounting->account_number)."'";
$sql .= " WHERE rowid = ".((int) $productid);
}
+ $db->begin();
dol_syslog("/accountancy/admin/productaccount.php sql=".$sql, LOG_DEBUG);
if ($db->query($sql)) {
$ok++;
diff --git a/htdocs/fourn/class/fournisseur.product.class.php b/htdocs/fourn/class/fournisseur.product.class.php
index 3eaed8d27b8..230c794b038 100644
--- a/htdocs/fourn/class/fournisseur.product.class.php
+++ b/htdocs/fourn/class/fournisseur.product.class.php
@@ -846,7 +846,7 @@ class ProductFournisseur extends Product
$this->fourn_qty = $record["quantity"];
$this->fourn_remise_percent = $record["remise_percent"];
$this->fourn_remise = $record["remise"];
- $this->fourn_unitprice = $record["unitprice"];
+ $this->fourn_unitprice = $fourn_unitprice;
$this->fourn_charges = $record["charges"]; // deprecated
$this->fourn_tva_tx = $record["tva_tx"];
$this->fourn_id = $record["fourn_id"];