diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php index b8bd2fe5d90..23c731f2b59 100644 --- a/htdocs/societe/class/societe.class.php +++ b/htdocs/societe/class/societe.class.php @@ -5279,4 +5279,91 @@ class Societe extends CommonObject $return .= ''; return $return; } + + /** + * Get array of all contacts for a society (stored in societe_contacts instead of element_contacts for all other objects) + * + * @param int socid id of societe + * @param int $list 0:Return array contains all properties, 1:Return array contains just id + * @param string $code Filter on this code of contact type ('SHIPPING', 'BILLING', ...) + * @param string $element Filter on this element of default contact type ('facture', 'propal', 'commande' ...) + * @return array|int Array of contacts, -1 if error + * + */ + function getContacts($list = 0, $code = '', $element = '') + { + // phpcs:enable + global $langs; + + $tab = array(); + + $sql = "SELECT sc.rowid, sc.fk_socpeople as id, sc.fk_c_type_contact"; // This field contains id of llx_socpeople or id of llx_user + + $sql .= ", t.fk_soc as socid, t.statut as statuscontact"; + + $sql .= ", t.civility as civility, t.lastname as lastname, t.firstname, t.email"; + $sql .= ", tc.source, tc.element, tc.code, tc.libelle"; + $sql .= " FROM ".$this->db->prefix()."c_type_contact tc"; + $sql .= ", ".$this->db->prefix()."societe_contacts sc"; + + $sql .= " LEFT JOIN ".$this->db->prefix()."socpeople t on sc.fk_socpeople = t.rowid"; + + $sql .= " WHERE sc.fk_soc = ".((int) $this->id); + $sql .= " AND sc.fk_c_type_contact = tc.rowid"; + if (!empty($element)) { + $sql .= " AND tc.element = '".$this->db->escape($element)."'"; + } + if ($code) { + $sql .= " AND tc.code = '".$this->db->escape($code)."'"; + } + $sql .= " AND sc.entity = ".getEntity($this->element); + $sql .= " AND tc.source = 'external'"; + $sql .= " AND tc.active=1"; + + $sql .= " ORDER BY t.lastname ASC"; + + dol_syslog(get_class($this)."::getContacts", LOG_DEBUG); + $resql = $this->db->query($sql); + if ($resql) { + $num = $this->db->num_rows($resql); + $i = 0; + while ($i < $num) { + $obj = $this->db->fetch_object($resql); + + if (!$list) { + $transkey = "TypeContact_".$obj->element."_".$obj->source."_".$obj->code; + $libelle_type = ($langs->trans($transkey) != $transkey ? $langs->trans($transkey) : $obj->libelle); + $tab[$i] = array( + 'source' => $obj->source, + 'socid' => $obj->socid, + 'id' => $obj->id, + 'nom' => $obj->lastname, // For backward compatibility + 'civility' => $obj->civility, + 'lastname' => $obj->lastname, + 'firstname' => $obj->firstname, + 'email'=>$obj->email, + 'login'=> (empty($obj->login) ? '' : $obj->login), + 'photo' => (empty($obj->photo) ? '' : $obj->photo), + 'statuscontact' => $obj->statuscontact, + 'rowid' => $obj->rowid, + 'code' => $obj->code, + 'element' => $obj->element, + 'libelle' => $libelle_type, + 'status' => $obj->statuslink, + 'fk_c_type_contact' => $obj->fk_c_type_contact + ); + } else { + $tab[$i] = $obj->id; + } + + $i++; + } + + return $tab; + } else { + $this->error = $this->db->lasterror(); + dol_print_error($this->db); + return -1; + } + } }