diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php
index a3787797c25..52ff50b6791 100644
--- a/htdocs/core/class/html.form.class.php
+++ b/htdocs/core/class/html.form.class.php
@@ -2999,11 +2999,6 @@ class Form
$sql .= ' AND e.statut IN (' . $this->db->sanitize($this->db->escape(implode(',', $warehouseStatusArray))) . ')'; // Return line if product is inside the selected stock. If not, an empty line will be returned so we will count 0.
}
- // include search in supplier ref
- if (getDolGlobalString('MAIN_SEARCH_PRODUCT_BY_FOURN_REF')) {
- $sql .= " LEFT JOIN " . $this->db->prefix() . "product_fournisseur_price as pfp ON p.rowid = pfp.fk_product";
- }
-
//Price by customer
if (getDolGlobalString('PRODUIT_CUSTOMER_PRICES') && !empty($socid)) {
$sql .= " LEFT JOIN " . $this->db->prefix() . "product_customer_price as pcp ON pcp.fk_soc=" . ((int) $socid) . " AND pcp.fk_product=p.rowid";
@@ -3064,6 +3059,8 @@ class Form
$sql .= $hookmanager->resPrint;
// Add criteria on ref/label
if ($filterkey != '') {
+ $sqlSupplierSearch= '';
+
$sql .= ' AND (';
$prefix = !getDolGlobalString('PRODUCT_DONOTSEARCH_ANYWHERE') ? '%' : ''; // Can use index if PRODUCT_DONOTSEARCH_ANYWHERE is on
// For natural search
@@ -3089,8 +3086,11 @@ class Form
$sql .= " OR pl.description LIKE '" . $this->db->escape($prefix . $crit) . "%'";
}
}
+
+ // include search in supplier ref
if (getDolGlobalString('MAIN_SEARCH_PRODUCT_BY_FOURN_REF')) {
- $sql .= " OR pfp.ref_fourn LIKE '" . $this->db->escape($prefix . $crit) . "%'";
+ $sqlSupplierSearch .= !empty($sqlSupplierSearch) ? ' OR ':'';
+ $sqlSupplierSearch .= " pfp.ref_fourn LIKE '" . $this->db->escape($prefix . $crit) . "%'";
}
$sql .= ")";
$i++;
@@ -3101,6 +3101,12 @@ class Form
if (isModEnabled('barcode')) {
$sql .= " OR p.barcode LIKE '" . $this->db->escape($prefix . $filterkey) . "%'";
}
+
+ // include search in supplier ref
+ if (getDolGlobalString('MAIN_SEARCH_PRODUCT_BY_FOURN_REF')) {
+ $sql .= ' OR EXISTS (SELECT pfp.fk_product FROM ' . $this->db->prefix() . 'product_fournisseur_price as pfp WHERE p.rowid = pfp.fk_product AND ('.$sqlSupplierSearch.') )';
+ }
+
$sql .= ')';
}
if (count($warehouseStatusArray)) {