Merge pull request #16990 from daraelmin/daraelmin-adh-create-soc

Close #15830 Add setup option ADHERENT_DEFAULT_CREATE_THIRDPARTY
This commit is contained in:
Laurent Destailleur 2021-03-30 00:55:17 +02:00 committed by GitHub
commit de5df84207
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 6 deletions

View File

@ -7,6 +7,7 @@
* Copyright (C) 2012-2020 Philippe Grand <philippe.grand@atoo-net.com>
* Copyright (C) 2015-2018 Alexandre Spangaro <aspangaro@open-dsi.fr>
* Copyright (C) 2018-2020 Frédéric France <frederic.france@netlogic.fr>
* Copyright (C) 2021 Waël Almoman <info@almoman.com>
*
* 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') {

View File

@ -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();