From 2566170c68a769aef767f15510ece3b633ca0e48 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Tue, 13 Mar 2018 17:47:43 +0100 Subject: [PATCH] Use code of societeaccount --- htdocs/core/lib/company.lib.php | 18 +++++++--- htdocs/societe/class/societeaccount.class.php | 34 +++++++++++++++++++ htdocs/stripe/class/stripe.class.php | 25 +++----------- 3 files changed, 52 insertions(+), 25 deletions(-) diff --git a/htdocs/core/lib/company.lib.php b/htdocs/core/lib/company.lib.php index ea9a46bc54e..3a295f7a941 100644 --- a/htdocs/core/lib/company.lib.php +++ b/htdocs/core/lib/company.lib.php @@ -178,6 +178,8 @@ function societe_prepare_head(Societe $object) // Bank accounts if (empty($conf->global->SOCIETE_DISABLE_BANKACCOUNT)) { + $nbBankAccount=0; + $foundonexternalonlinesystem=0; $langs->load("banks"); $title = $langs->trans("BankAccounts"); @@ -185,11 +187,16 @@ function societe_prepare_head(Societe $object) { $langs->load("stripe"); $title = $langs->trans("BankAccountsAndGateways"); + + $servicestatus = 0; + if (! empty($conf->global->STRIPE_LIVE) && ! GETPOST('forcesandbox','alpha')) $servicestatus = 1; + + include_once DOL_DOCUMENT_ROOT.'/societe/class/societeaccount.class.php'; + $societeaccount = new SocieteAccount($db); + $stripecu = $societeaccount->getCustomerAccount($object->id, 'stripe', $servicestatus); // Get thirdparty cu_... + if ($stripecu) $foundonexternalonlinesystem++; } - $nbBankAccount=0; - $head[$h][0] = DOL_URL_ROOT .'/societe/paymentmodes.php?socid='.$object->id; - $head[$h][1] = $title; $sql = "SELECT COUNT(n.rowid) as nb"; $sql.= " FROM ".MAIN_DB_PREFIX."societe_rib as n"; $sql.= " WHERE fk_soc = ".$object->id; @@ -211,7 +218,10 @@ function societe_prepare_head(Societe $object) //if (! empty($conf->stripe->enabled) && $nbBankAccount > 0) $nbBankAccount = '...'; // No way to know exact number - if ($nbBankAccount > 0) $head[$h][1].= ' '.$nbBankAccount.''; + $head[$h][0] = DOL_URL_ROOT .'/societe/paymentmodes.php?socid='.$object->id; + $head[$h][1] = $title; + if ($foundonexternalonlinesystem) $head[$h][1].= ' ...'; + elseif ($nbBankAccount > 0) $head[$h][1].= ' '.$nbBankAccount.''; $head[$h][2] = 'rib'; $h++; } diff --git a/htdocs/societe/class/societeaccount.class.php b/htdocs/societe/class/societeaccount.class.php index 05ce15a8c94..08b3617e540 100644 --- a/htdocs/societe/class/societeaccount.class.php +++ b/htdocs/societe/class/societeaccount.class.php @@ -246,6 +246,40 @@ class SocieteAccount extends CommonObject return count($this->lines)?1:0; } + /** + * Try to find the external customer id of a thirdparty for an another site/system. + * + * @param int $id Id of third party + * @param string $site Site (example: 'stripe', '...') + * @param int $status Status (0=test, 1=live) + * @return string Stripe customer ref 'cu_xxxxxxxxxxxxx' or '' + */ + public function getCustomerAccount($id, $site, $status=0) + { + global $conf; + + $sql = "SELECT sa.key_account as key_account, sa.entity"; + $sql.= " FROM " . MAIN_DB_PREFIX . "societe_account as sa"; + $sql.= " WHERE sa.fk_soc = " . $id; + $sql.= " AND sa.entity IN (".getEntity('societe').")"; + $sql.= " AND sa.site = '".$this->db->escape($site)."' AND sa.status = ".((int) $status); + + dol_syslog(get_class($this) . "::getCustomerAccount Try to find the system customer id of thirdparty id=".$id." (exemple: cu_.... for stripe)", LOG_DEBUG); + $result = $this->db->query($sql); + if ($result) { + if ($this->db->num_rows($result)) { + $obj = $this->db->fetch_object($result); + $key = $obj->key_account; + } else { + $key = ''; + } + } else { + $key = ''; + } + + return $key; + } + /** * Update object into database * diff --git a/htdocs/stripe/class/stripe.class.php b/htdocs/stripe/class/stripe.class.php index dfecc56a172..1053f3236c3 100644 --- a/htdocs/stripe/class/stripe.class.php +++ b/htdocs/stripe/class/stripe.class.php @@ -94,32 +94,15 @@ class Stripe extends CommonObject * * @param int $id Id of third party * @param int $status Status - * @return string Stripe customer ref 'cu_xxxxxxxxxxxxx' + * @return string Stripe customer ref 'cu_xxxxxxxxxxxxx' or '' */ public function getStripeCustomerAccount($id, $status=0) { global $conf; - $sql = "SELECT sa.key_account as key_account, sa.entity"; - $sql.= " FROM " . MAIN_DB_PREFIX . "societe_account as sa"; - $sql.= " WHERE sa.fk_soc = " . $id; - $sql.= " AND sa.entity IN (".getEntity('societe').")"; - $sql.= " AND sa.site = 'stripe' AND sa.status = ".((int) $status); - - dol_syslog(get_class($this) . "::getStripeCustomerAccount Try to find the cu_.... of thirdparty id=".$id, LOG_DEBUG); - $result = $this->db->query($sql); - if ($result) { - if ($this->db->num_rows($result)) { - $obj = $this->db->fetch_object($result); - $key = $obj->key_account; - } else { - $key = NULL; - } - } else { - $key = NULL; - } - - return $key; + include_once DOL_DOCUMENT_ROOT.'/societe/class/societeaccount.class.php'; + $societeaccount = new SocieteAccount($this->db); + return $societeaccount->getCustomerAccount($object->id, 'stripe', $status); // Get thirdparty cu_... }