From 54f424eedfe91d52bfce7ccc2e891741beb48655 Mon Sep 17 00:00:00 2001 From: Regis Houssin Date: Thu, 29 Apr 2010 15:35:52 +0000 Subject: [PATCH] Add a common function to delete the linked contact --- htdocs/compta/facture/class/facture.class.php | 4 +- htdocs/core/class/commonobject.class.php | 38 +++++++++++++++++-- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/htdocs/compta/facture/class/facture.class.php b/htdocs/compta/facture/class/facture.class.php index d2659a8f937..2ae7ef1d3dd 100644 --- a/htdocs/compta/facture/class/facture.class.php +++ b/htdocs/compta/facture/class/facture.class.php @@ -989,7 +989,7 @@ class Facture extends CommonObject { $sql = 'UPDATE '.MAIN_DB_PREFIX.'societe_remise_except'; $sql.= ' SET fk_facture = NULL'; - $sql.= ' WHERE fk_facture in ('.join(',',$list_rowid_det).')'; + $sql.= ' WHERE fk_facture IN ('.join(',',$list_rowid_det).')'; dol_syslog("Facture.class::delete sql=".$sql); if (! $this->db->query($sql)) @@ -1002,7 +1002,7 @@ class Facture extends CommonObject } $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'facturedet WHERE fk_facture = '.$rowid; - if ($this->db->query($sql)) + if ($this->db->query($sql) && $this->delete_linked_contact()) { $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'facture WHERE rowid = '.$rowid; $resql=$this->db->query($sql); diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index d2d8f354663..88f24e9dab5 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -160,6 +160,38 @@ class CommonObject return -1; } } + + /** + * \brief Supprime une ligne de contact + * \return statur >0 si ok, <0 si ko + */ + function delete_linked_contact() + { + $temp = array(); + $typeContact = $this->liste_type_contact(0); + + foreach($typeContact as $key => $value) + { + array_push($temp,$key); + } + $listId = implode(",", $temp); + + $sql = "DELETE FROM ".MAIN_DB_PREFIX."element_contact"; + $sql.= " WHERE element_id =".$this->id; + $sql.= " AND fk_c_type_contact IN (".$listId.")"; + + dol_syslog("CommonObject::delete_linked_contact sql=".$sql); + if ($this->db->query($sql)) + { + return 1; + } + else + { + $this->error=$this->db->lasterror(); + dol_syslog("CommonObject::delete_linked_contact error=".$this->error, LOG_ERR); + return -1; + } + } /** * \brief Get array of all contacts for an object @@ -249,11 +281,11 @@ class CommonObject /** * \brief La liste des valeurs possibles de type de contacts - * \param source internal ou external + * \param source internal, external or all if not defined * \param order Sort order by : code or rowid * \return array La liste des natures */ - function liste_type_contact($source, $order='code') + function liste_type_contact($source='internal', $order='code') { global $langs; @@ -262,7 +294,7 @@ class CommonObject $sql = "SELECT DISTINCT tc.rowid, tc.code, tc.libelle"; $sql.= " FROM ".MAIN_DB_PREFIX."c_type_contact as tc"; $sql.= " WHERE tc.element='".$this->element."'"; - $sql.= " AND tc.source='".$source."'"; + if (!empty($source)) $sql.= " AND tc.source='".$source."'"; $sql.= " ORDER by tc.".$order; $resql=$this->db->query($sql);