diff --git a/htdocs/emailcollector/class/emailcollector.class.php b/htdocs/emailcollector/class/emailcollector.class.php
index ce91ec68078..0794a80165c 100644
--- a/htdocs/emailcollector/class/emailcollector.class.php
+++ b/htdocs/emailcollector/class/emailcollector.class.php
@@ -2583,14 +2583,15 @@ class EmailCollector extends CommonObject
$operationslog .= '
We try to search existing thirdparty with '.$idtouseforthirdparty.' '.$emailtouseforthirdparty.' '.$nametouseforthirdparty.' '.$namealiastouseforthirdparty;
- $result = $thirdpartystatic->fetch($idtouseforthirdparty, $nametouseforthirdparty, '', '', '', '', '', '', '', '', $emailtouseforthirdparty, $namealiastouseforthirdparty);
- // TODO Replace with fetchBySimilarityRules()
+ // Try to find the thirdparty that match the most the information we have
+ $result = $thirdpartystatic->findNearest($idtouseforthirdparty, $nametouseforthirdparty, '', '', '', '', '', '', '', '', $emailtouseforthirdparty, $namealiastouseforthirdparty);
+
if ($result < 0) {
$errorforactions++;
$this->error = 'Error when getting thirdparty with name '.$nametouseforthirdparty.' (may be 2 record exists with same name ?)';
$this->errors[] = $this->error;
break;
- } elseif ($result == 0) {
+ } elseif ($result == 0) { // No thirdparty found
if ($operation['type'] == 'loadthirdparty') {
dol_syslog("Third party with id=".$idtouseforthirdparty." email=".$emailtouseforthirdparty." name=".$nametouseforthirdparty." name_alias=".$namealiastouseforthirdparty." was not found");
@@ -2654,6 +2655,8 @@ class EmailCollector extends CommonObject
} else {
dol_syslog("One and only one existing third party has been found");
+ $thirdpartystatic->fetch($result);
+
$operationslog .= '
Thirdparty already exists with id = '.dol_escape_htmltag($thirdpartystatic->id);
}
}
diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php
index e9b7b3b7979..a8469e82920 100644
--- a/htdocs/societe/class/societe.class.php
+++ b/htdocs/societe/class/societe.class.php
@@ -2141,6 +2141,45 @@ class Societe extends CommonObject
return $result;
}
+ /**
+ * Search the thirdparty that match the most the provided parameters.
+ * Searching rules try to find the existing third party.
+ *
+ * @param int $rowid Id of third party
+ * @param string $ref Reference of third party, name (Warning, this can return several records)
+ * @param string $ref_ext External reference of third party (Warning, this information is a free field not provided by Dolibarr)
+ * @param string $barcode Barcode of third party to load
+ * @param string $idprof1 Prof id 1 of third party (Warning, this can return several records)
+ * @param string $idprof2 Prof id 2 of third party (Warning, this can return several records)
+ * @param string $idprof3 Prof id 3 of third party (Warning, this can return several records)
+ * @param string $idprof4 Prof id 4 of third party (Warning, this can return several records)
+ * @param string $idprof5 Prof id 5 of third party (Warning, this can return several records)
+ * @param string $idprof6 Prof id 6 of third party (Warning, this can return several records)
+ * @param string $email Email of third party (Warning, this can return several records)
+ * @param string $ref_alias Name_alias of third party (Warning, this can return several records)
+ * @param int $is_client Only client third party
+ * @param int $is_supplier Only supplier third party
+ * @return int >0 if OK, <0 if KO or if two records found, 0 if not found.
+ */
+ public function findNearest($rowid = 0, $ref = '', $ref_ext = '', $barcode = '', $idprof1 = '', $idprof2 = '', $idprof3 = '', $idprof4 = '', $idprof5 = '', $idprof6 = '', $email = '', $ref_alias = '', $is_client = 0, $is_supplier = 0)
+ {
+ // A rowid is known, it is a unique key so we found it
+ if ($rowid) {
+ return $rowid;
+ }
+
+ // We try to find the thirdparty with exact matching on all fields
+ // TODO Replace this with step by step search
+ // Then search on barcode if we have it (+ restriction on is_client and is_supplier)
+ // Then search on profids with a OR (+ restriction on is_client and is_supplier)
+ // Then search on email with a OR (+ restriction on is_client and is_supplier)
+ // Then search ref, ref_ext or alias with a OR (+ restriction on is_client and is_supplier)
+ $tmpthirdparty = new Societe($this->db);
+ $result = $tmpthirdparty->fetch($rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias, $is_client, $is_supplier);
+
+ return $result;
+ }
+
/**
* Delete a third party from database and all its dependencies (contacts, rib...)
*