Fix add an order to have same behavior in using stripe customer account

event if data are corrupted with duplicate record.
This commit is contained in:
Laurent Destailleur 2024-11-25 14:46:22 +01:00
parent 92aaf4e618
commit 63ac936652
4 changed files with 14 additions and 7 deletions

View File

@ -5294,7 +5294,7 @@ class Societe extends CommonObject
$dbs->query('DELETE FROM '.MAIN_DB_PREFIX.'societe_commerciaux WHERE rowid = '.((int) $obj->rowid));
}
// llx_societe_extrafields table must not be here because we don't care about the old thirdparty extrafields that are managed directly into mergeCompany.
// The table llx_societe_extrafields must NOT be in this list because we don't care about the old thirdparty extrafields that are managed directly into mergeCompany.
// Do not include llx_societe because it will be replaced later.
$tables = array(
'societe_account',
@ -5305,6 +5305,8 @@ class Societe extends CommonObject
'societe_rib'
);
// TODO When we merge societe_account, we may get 2 lines for the stripe account. Must fix this.
return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
}

View File

@ -359,7 +359,7 @@ class SocieteAccount extends CommonObject
$sql .= " AND sa.site = '".$this->db->escape($site)."' AND sa.status = ".((int) $status);
$sql .= " AND sa.key_account IS NOT NULL AND sa.key_account <> ''";
$sql .= " AND (sa.site_account = '' OR sa.site_account IS NULL OR sa.site_account = '".$this->db->escape($site_account)."')";
$sql .= " ORDER BY sa.site_account DESC"; // To get the entry with a site_account defined in priority
$sql .= " ORDER BY sa.site_account DESC, sa.rowid DESC"; // To get the entry with a site_account defined in priority
dol_syslog(get_class($this)."::getCustomerAccount Try to find the first system customer id for ".$site." of thirdparty id=".$id." (example: cus_.... for stripe)", LOG_DEBUG);
$result = $this->db->query($sql);
@ -396,6 +396,7 @@ class SocieteAccount extends CommonObject
$sql .= " AND sa.entity IN (".getEntity('societe').")";
$sql .= " AND sa.site = '".$this->db->escape($site)."' AND sa.status = ".((int) $status);
$sql .= " AND sa.fk_soc > 0";
$sql .= " ORDER BY sa.site_account DESC, sa.rowid DESC"; // To get the entry with a site_account defined in priority
dol_syslog(get_class($this)."::getCustomerAccount Try to find the first thirdparty id for ".$site." for external id=".$id, LOG_DEBUG);
$result = $this->db->query($sql);

View File

@ -666,16 +666,18 @@ if (empty($reshook)) {
$db->begin();
if (empty($newcu)) {
$sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_account WHERE site = 'stripe' AND (site_account IS NULL or site_account = '' or site_account = '".$db->escape($tmpsite_account)."') AND fk_soc = ".$object->id." AND status = ".((int) $tmpservicestatus)." AND entity = ".$conf->entity;
$sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_account";
$sql .= " WHERE site = 'stripe' AND (site_account IS NULL or site_account = '' or site_account = '".$db->escape($tmpsite_account)."') AND fk_soc = ".((int) $object->id)." AND status = ".((int) $tmpservicestatus)." AND entity = ".$conf->entity;
} else {
$sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX."societe_account";
$sql .= " WHERE site = 'stripe' AND (site_account IS NULL or site_account = '' or site_account = '".$db->escape($tmpsite_account)."') AND fk_soc = ".((int) $object->id)." AND status = ".((int) $tmpservicestatus)." AND entity = ".$conf->entity; // Keep = here for entity. Only 1 record must be modified !
$sql .= " WHERE site = 'stripe' AND (site_account IS NULL or site_account = '' or site_account = '".$db->escape($tmpsite_account)."') AND fk_soc = ".((int) $object->id)." AND status = ".((int) $tmpservicestatus)." AND entity = ".$conf->entity; // Keep the = here for entity. Only 1 record must be modified !
}
$resql = $db->query($sql);
$num = $db->num_rows($resql); // Note: $num is always 0 on an update and delete, it is defined for select only.
if (!empty($newcu)) {
if (empty($num)) {
if (!empty($newcu)) { // If we did a select
if (empty($num)) { // and found nothing
$societeaccount = new SocieteAccount($db);
$societeaccount->fk_soc = $object->id;
$societeaccount->login = '';
@ -733,7 +735,7 @@ if (empty($reshook)) {
$db->begin();
if (empty($newsup)) {
$sql = "DELETE FROM ".MAIN_DB_PREFIX."oauth_token WHERE fk_soc = ".$object->id." AND service = '".$db->escape($tmpservice)."' AND entity = ".$conf->entity;
$sql = "DELETE FROM ".MAIN_DB_PREFIX."oauth_token WHERE fk_soc = ".((int) $object->id)." AND service = '".$db->escape($tmpservice)."' AND entity = ".((int) $conf->entity);
// TODO Add site and site_account on oauth_token table
//$sql = "DELETE FROM ".MAIN_DB_PREFIX."oauth_token WHERE site = 'stripe' AND (site_account IS NULL or site_account = '".$db->escape($site_account)."') AND fk_soc = ".((int) $object->id)." AND service = '".$db->escape($service)."' AND entity = ".$conf->entity;
} else {

View File

@ -198,6 +198,7 @@ class Stripe extends CommonObject
$sql .= " AND sa.site = 'stripe' AND sa.status = ".((int) $status);
$sql .= " AND (sa.site_account IS NULL OR sa.site_account = '' OR sa.site_account = '".$this->db->escape($stripearrayofkeysbyenv[$status]['publishable_key'])."')";
$sql .= " AND sa.key_account IS NOT NULL AND sa.key_account <> ''";
$sql .= " ORDER BY sa.site_account DESC, sa.rowid DESC"; // To get the entry with a site_account defined in priority
dol_syslog(get_class($this)."::customerStripe search stripe customer id for thirdparty id=".$object->id, LOG_DEBUG);
$resql = $this->db->query($sql);
@ -1190,6 +1191,7 @@ class Stripe extends CommonObject
$sql .= " WHERE sa.key_account = '".$this->db->escape($customer)."'";
//$sql.= " AND sa.entity IN (".getEntity('societe').")";
$sql .= " AND sa.site = 'stripe' AND sa.status = ".((int) $status);
$sql .= " ORDER BY sa.site_account DESC, sa.rowid DESC"; // To get the entry with a site_account defined in priority
dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
$result = $this->db->query($sql);