diff --git a/htdocs/adherents/card.php b/htdocs/adherents/card.php index fe3fc57d7b5..9b28cc6473e 100644 --- a/htdocs/adherents/card.php +++ b/htdocs/adherents/card.php @@ -7,6 +7,7 @@ * Copyright (C) 2012-2020 Philippe Grand * Copyright (C) 2015-2018 Alexandre Spangaro * Copyright (C) 2018-2020 Frédéric France + * Copyright (C) 2021 Waël Almoman * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -554,7 +555,6 @@ if (empty($reshook)) { $db->commit(); $rowid = $object->id; $id = $object->id; - $action = ''; } else { $db->rollback(); @@ -563,12 +563,41 @@ if (empty($reshook)) { } else { setEventMessages($object->error, $object->errors, 'errors'); } - - $action = 'create'; } - } else { - $action = 'create'; + // Auto-create thirdparty on member creation + if (!empty($conf->global->ADHERENT_DEFAULT_CREATE_THIRDPARTY)) { + if ($result > 0) { + // User creation + $company = new Societe($db); + + $companyalias = ''; + $fullname = $object->getFullName($langs); + + if ($object->morphy == 'mor') { + $companyname = $object->company; + if (!empty($fullname)) { + $companyalias = $fullname; + } + } else { + $companyname = $fullname; + if (!empty($object->company)) { + $companyalias = $object->company; + } + } + + $result = $company->create_from_member($object, $companyname, $companyalias); + + if ($result < 0) { + $langs->load("errors"); + setEventMessages($langs->trans($company->error), null, 'errors'); + setEventMessages($company->error, $company->errors, 'errors'); + } + } else { + setEventMessages($object->error, $object->errors, 'errors'); + } + } } + $action = ($result < 0 || !$error) ? '' : 'create'; } if ($user->rights->adherent->supprimer && $action == 'confirm_delete' && $confirm == 'yes') { diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php index b2387314b94..954ebe94673 100644 --- a/htdocs/societe/class/societe.class.php +++ b/htdocs/societe/class/societe.class.php @@ -868,6 +868,7 @@ class Societe extends CommonObject $sql .= ", name_alias"; $sql .= ", entity"; $sql .= ", datec"; + $sql .= ", fk_typent"; $sql .= ", fk_user_creat"; $sql .= ", canvas"; $sql .= ", status"; @@ -882,6 +883,7 @@ class Societe extends CommonObject $sql .= ", accountancy_code_sell"; $sql .= ") VALUES ('".$this->db->escape($this->name)."', '".$this->db->escape($this->name_alias)."', ".$this->db->escape($this->entity).", '".$this->db->idate($now)."'"; $sql .= ", ".(!empty($user->id) ? ((int) $user->id) : "null"); + $sql .= ", ".(!empty($this->typent_id) ? ((int) $this->typent_id) : "null"); $sql .= ", ".(!empty($this->canvas) ? "'".$this->db->escape($this->canvas)."'" : "null"); $sql .= ", ".$this->status; $sql .= ", ".(!empty($this->ref_ext) ? "'".$this->db->escape($this->ref_ext)."'" : "null"); @@ -958,7 +960,7 @@ class Societe extends CommonObject * @param array $tags Array of tag to affect to contact * @return int <0 if KO, >0 if OK */ - public function create_individual(User $user, $no_email, $tags = array()) + public function create_individual(User $user, $no_email = 0, $tags = array()) { global $conf; @@ -3793,11 +3795,14 @@ class Societe extends CommonObject $this->client = 1; // A member is a customer by default $this->code_client = ($customercode ? $customercode : -1); $this->code_fournisseur = -1; + $this->typent_code = ($member->morphy == 'phy' ? 'TE_PRIVATE' : 0); + $this->typent_id = $this->typent_code ? dol_getIdFromCode($this->db, $this->typent_code, 'c_typent', 'id', 'code') : 0; $this->db->begin(); // Cree et positionne $this->id $result = $this->create($user); + if ($result >= 0) { // Auto-create contact on thirdparty creation if (!empty($conf->global->THIRDPARTY_DEFAULT_CREATE_CONTACT)) { @@ -3808,6 +3813,7 @@ class Societe extends CommonObject dol_syslog("We ask to create a contact/address too", LOG_DEBUG); $result = $this->create_individual($user); + if ($result < 0) { setEventMessages($this->error, $this->errors, 'errors'); $this->db->rollback();