From 0bfb77439768923f6eee459c357f5cc6702f2d44 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sun, 16 Sep 2007 03:55:35 +0000 Subject: [PATCH] Fix: La suppression d'un contact n'effacait pas les liens sur element_contact --- htdocs/comm/propal.php | 2 +- htdocs/commonobject.class.php | 13 +++-- htdocs/contact.class.php | 102 +++++++++++++++++++++++++--------- htdocs/contact/fiche.php | 4 +- 4 files changed, 85 insertions(+), 36 deletions(-) diff --git a/htdocs/comm/propal.php b/htdocs/comm/propal.php index 051dec55ad3..e99d3ba4a49 100644 --- a/htdocs/comm/propal.php +++ b/htdocs/comm/propal.php @@ -795,7 +795,7 @@ if ($_GET['propalid'] > 0) { if ($propal->statut > 0) { - print $langs->trans("CompanyHasAbsoluteDiscount",$absolute_discount,$langs->trans("Currency".$conf->monnaie)); + print $langs->trans("CompanyHasAbsoluteDiscount",price($absolute_discount),$langs->trans("Currency".$conf->monnaie)); } else { diff --git a/htdocs/commonobject.class.php b/htdocs/commonobject.class.php index 72d3745c61a..220579456b3 100644 --- a/htdocs/commonobject.class.php +++ b/htdocs/commonobject.class.php @@ -170,17 +170,16 @@ class CommonObject if ($source == 'external') $sql.=" t.fk_soc as socid,"; $sql.=" t.name as nom,"; $sql.= "tc.source, tc.element, tc.code, tc.libelle"; - $sql.= " FROM ".MAIN_DB_PREFIX."element_contact ec,"; - if ($source == 'internal') $sql.=" ".MAIN_DB_PREFIX."user t,"; - if ($source == 'external') $sql.=" ".MAIN_DB_PREFIX."socpeople t,"; - $sql.= " ".MAIN_DB_PREFIX."c_type_contact tc"; + $sql.= " FROM (".MAIN_DB_PREFIX."element_contact ec,"; + $sql.= " ".MAIN_DB_PREFIX."c_type_contact tc)"; + if ($source == 'internal') $sql.=" LEFT JOIN ".MAIN_DB_PREFIX."user t on ec.fk_socpeople = t.rowid"; + if ($source == 'external') $sql.=" LEFT JOIN ".MAIN_DB_PREFIX."socpeople t on ec.fk_socpeople = t.rowid"; $sql.= " WHERE ec.element_id =".$this->id; $sql.= " AND ec.fk_c_type_contact=tc.rowid"; $sql.= " AND tc.element='".$this->element."'"; if ($source == 'internal') $sql.= " AND tc.source = 'internal'"; if ($source == 'external') $sql.= " AND tc.source = 'external'"; $sql.= " AND tc.active=1"; - $sql.= " AND ec.fk_socpeople = t.rowid"; if ($statut >= 0) $sql.= " AND ec.statut = '".$statut."'"; $sql.=" ORDER BY t.name ASC"; @@ -286,6 +285,8 @@ class CommonObject * Exemple: contact client de facturation ('external', 'BILLING') * Exemple: contact client de livraison ('external', 'SHIPPING') * Exemple: contact interne suivi paiement ('internal', 'SALESREPFOLL') + * \param source 'external' or 'internal' + * \param source 'BILLING', 'SHIPPING', 'SALESREPFOLL', ... * \return array Liste des id contacts */ function getIdContact($source,$code) @@ -299,8 +300,8 @@ class CommonObject $sql.= " AND ec.fk_c_type_contact=tc.rowid"; $sql.= " AND tc.element = '".$this->element."'"; $sql.= " AND tc.source = '".$source."'"; - $sql.= " AND tc.active = 1"; $sql.= " AND tc.code = '".$code."'"; + $sql.= " AND tc.active = 1"; dolibarr_syslog("CommonObject::getIdContact sql=".$sql); $resql=$this->db->query($sql); diff --git a/htdocs/contact.class.php b/htdocs/contact.class.php index 4b826402121..319a3b02652 100644 --- a/htdocs/contact.class.php +++ b/htdocs/contact.class.php @@ -534,45 +534,93 @@ class Contact /* * \brief Efface le contact de la base - * \param id id du contact a effacer * \return int <0 si ko, >0 si ok */ - function delete($id) + function delete($notrigger=0) { global $conf, $langs; + + $error=0; + + $this->old_name = $obj->name; + $this->old_firstname = $obj->firstname; - $sql = "SELECT c.name, c.firstname FROM ".MAIN_DB_PREFIX."socpeople as c"; - $sql .= " WHERE c.rowid = ". $id; - $resql=$this->db->query($sql); - if ($resql) + $this->db->begin(); + + if (! $error) { - if ($this->db->num_rows($resql)) + // Get all rowid of element_contact linked to a type that is link to llx_socpeople + $sql = "SELECT ec.rowid"; + $sql.= " FROM ".MAIN_DB_PREFIX."element_contact ec,"; + $sql.= " ".MAIN_DB_PREFIX."c_type_contact tc"; + $sql.= " WHERE ec.fk_socpeople=".$this->id; + $sql.= " AND ec.fk_c_type_contact=tc.rowid"; + $sql.= " AND tc.source='external'"; + dolibarr_syslog("Contact::delete sql=".$sql); + $resql = $this->db->query($sql); + if ($resql) { - $obj = $this->db->fetch_object($resql); - - $this->old_name = $obj->name; - $this->old_firstname = $obj->firstname; + $num=$this->db->num_rows($resql); + + $i=0; + while ($i < $num && ! $error) + { + $obj = $this->db->fetch_object($resql); + + $sqldel = "DELETE FROM ".MAIN_DB_PREFIX."element_contact"; + $sqldel.=" WHERE rowid = ".$obj->rowid; + dolibarr_syslog("Contact::delete sql=".$sqldel); + $result = $this->db->query($sqldel); + if (! $result) + { + $error++; + $this->error=$this->db->error().' sql='.$sqldel; + } + + $i++; + } + } + else + { + $error++; + $this->error=$this->db->error().' sql='.$sql; } } - - $sql = "DELETE FROM ".MAIN_DB_PREFIX."socpeople"; - $sql .= " WHERE rowid=".$id; - - $result = $this->db->query($sql); - if (! $result) + + if (! $error) { - $this->error=$this->db->error().' sql='.$sql; + $sql = "DELETE FROM ".MAIN_DB_PREFIX."socpeople"; + $sql .= " WHERE rowid=".$this->id; + dolibarr_syslog("Contact::delete sql=".$sql); + $result = $this->db->query($sql); + if (! $result) + { + $error++; + $this->error=$this->db->error().' sql='.$sql; + } + } + + if (! $error && ! $notrigger) + { + // Appel des triggers + include_once(DOL_DOCUMENT_ROOT . "/interfaces.class.php"); + $interface=new Interfaces($this->db); + $result=$interface->run_triggers('CONTACT_DELETE',$this,$user,$langs,$conf); + if ($result < 0) $error++; + // Fin appel triggers + } + + if (! $error) + { + + $this->db->commit(); + return 1; + } + else + { + $this->db->rollback(); return -1; } - - // Appel des triggers - include_once(DOL_DOCUMENT_ROOT . "/interfaces.class.php"); - $interface=new Interfaces($this->db); - $result=$interface->run_triggers('CONTACT_DELETE',$this,$user,$langs,$conf); - if ($result < 0) $error++; - // Fin appel triggers - - return 1; } diff --git a/htdocs/contact/fiche.php b/htdocs/contact/fiche.php index be680685386..1e09f133669 100644 --- a/htdocs/contact/fiche.php +++ b/htdocs/contact/fiche.php @@ -153,12 +153,12 @@ if ($user->rights->societe->contact->supprimer) if ($_POST["action"] == 'confirm_delete' AND $_POST["confirm"] == 'yes') { $contact = new Contact($db); - $contact->fetch($_GET["id"]); + $result=$contact->fetch($_GET["id"]); $contact->old_name = $_POST["old_name"]; $contact->old_firstname = $_POST["old_firstname"]; - $result = $contact->delete($_GET["id"]); + $result = $contact->delete(); Header("Location: index.php"); exit;