diff --git a/htdocs/admin/company.php b/htdocs/admin/company.php index 162801797a2ba..44101ce9300c5 100644 --- a/htdocs/admin/company.php +++ b/htdocs/admin/company.php @@ -199,6 +199,22 @@ /*}*/ } + + // TODO Must be in trigger + if( isModEnabled('multicompany')) { + $linked_soc = GETPOST('linked_soc', 'int'); + if($linked_soc) { + $sql = 'SELECT fk_entity FROM '.MAIN_DB_PREFIX.'entity_thirdparty WHERE fk_entity ='. $conf->entity; + $resql = $db->query($sql); + if(!$resql || $db->num_rows($resql) == 0) { + $sql = 'INSERT INTO ' . MAIN_DB_PREFIX . 'entity_thirdparty (entity, fk_entity, fk_thirdparty) VALUES ( 0, ' . $conf->entity . ', ' . $linked_soc . ')'; + } else { + $sql = 'UPDATE ' . MAIN_DB_PREFIX . 'entity_thirdparty SET fk_soc = ' . $linked_soc . ' WHERE fk_entity = ' . $conf->entity; + } + $resql = $db->query($sql); + } + } + dolibarr_set_const($db, "MAIN_INFO_SOCIETE_MANAGERS", GETPOST("MAIN_INFO_SOCIETE_MANAGERS", 'alphanohtml'), 'chaine', 0, '', $conf->entity); dolibarr_set_const($db, "MAIN_INFO_GDPR", GETPOST("MAIN_INFO_GDPR", 'alphanohtml'), 'chaine', 0, '', $conf->entity); dolibarr_set_const($db, "MAIN_INFO_CAPITAL", GETPOST("capital", 'alphanohtml'), 'chaine', 0, '', $conf->entity); @@ -605,7 +621,41 @@ print ''; print ''; -print ''; +// Society/Third party +// TODO We need to add a check to see thirdparty association is allowed. --> Some const checks to add +// !dolibarr_get_const($this->db, 'MULTICOMPANY_LINK_THIRDPARTY_ENTITY', +if (isModEnabled('multicompany')) { + // TODO Trad not loaded + print ''; + + // Get current society associated to the company (if any) + // TODO Code to be improved + $linked_soc = 0; + $sql = 'SELECT fk_soc FROM '.MAIN_DB_PREFIX.'entity_thirdparty WHERE fk_entity = ' . $entity; + $resql = $db->query($sql); + if ($resql) { + $obj = $db->fetch_object($resql); + $linked_soc = $obj->fk_soc; + } + + // TODO Get all thirdparty list from the database + print $form->select_company($linked_soc, 'linked_soc', '', $langs->trans("ThirdParty"), 0, 0, array(), 0, 'minwidth100', '', '', 1, array(), false, array(), 0, true); + print ''; + print ''; + print info_admin($langs->trans("DANGERZONE"), 1); + print ''; + print '' ."\n"; +} + +$parameters=array(); +$reshook=$hookmanager->executeHooks('hookantho',$parameters,$object,$action); // See description below + + + + print ''; + + +print('Module context: '.join(',', $object->contextarray)); print $form->buttonsSaveCancel("Save", '', array(), false, 'reposition'); diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php index 762416d2ad8fd..a3cb47da12b28 100644 --- a/htdocs/core/class/html.form.class.php +++ b/htdocs/core/class/html.form.class.php @@ -1319,7 +1319,7 @@ public function select_type_fees($selected = '', $htmlname = 'type', $showempty * @param int $showcode Show code * @return string HTML string with select box for thirdparty. */ - public function select_company($selected = '', $htmlname = 'socid', $filter = '', $showempty = '', $showtype = 0, $forcecombo = 0, $events = array(), $limit = 0, $morecss = 'minwidth100', $moreparam = '', $selected_input_value = '', $hidelabel = 1, $ajaxoptions = array(), $multiple = false, $excludeids = array(), $showcode = 0) + public function select_company($selected = '', $htmlname = 'socid', $filter = '', $showempty = '', $showtype = 0, $forcecombo = 0, $events = array(), $limit = 0, $morecss = 'minwidth100', $moreparam = '', $selected_input_value = '', $hidelabel = 1, $ajaxoptions = array(), $multiple = false, $excludeids = array(), $showcode = 0, $searchInAllEntities = false) { // phpcs:enable global $conf, $user, $langs; @@ -1365,7 +1365,7 @@ public function select_company($selected = '', $htmlname = 'socid', $filter = '' $out .= ajax_autocompleter($selected, $htmlname, DOL_URL_ROOT . '/societe/ajax/company.php', $urloption, $conf->global->COMPANY_USE_SEARCH_TO_SELECT, 0, $ajaxoptions); } else { // Immediate load of all database - $out .= $this->select_thirdparty_list($selected, $htmlname, $filter, $showempty, $showtype, $forcecombo, $events, '', 0, $limit, $morecss, $moreparam, $multiple, $excludeids, $showcode); + $out .= $this->select_thirdparty_list($selected, $htmlname, $filter, $showempty, $showtype, $forcecombo, $events, '', 0, $limit, $morecss, $moreparam, $multiple, $excludeids, $showcode, $searchInAllEntities); } return $out; @@ -1396,7 +1396,7 @@ public function select_company($selected = '', $htmlname = 'socid', $filter = '' * @param int $showcode Show code in list * @return array|string HTML string with */ - public function select_thirdparty_list($selected = '', $htmlname = 'socid', $filter = '', $showempty = '', $showtype = 0, $forcecombo = 0, $events = array(), $filterkey = '', $outputmode = 0, $limit = 0, $morecss = 'minwidth100', $moreparam = '', $multiple = false, $excludeids = array(), $showcode = 0) + public function select_thirdparty_list($selected = '', $htmlname = 'socid', $filter = '', $showempty = '', $showtype = 0, $forcecombo = 0, $events = array(), $filterkey = '', $outputmode = 0, $limit = 0, $morecss = 'minwidth100', $moreparam = '', $multiple = false, $excludeids = array(), $showcode = 0, $searchInAllEntities = false) { // phpcs:enable global $conf, $user, $langs; @@ -1446,6 +1446,12 @@ public function select_thirdparty_list($selected = '', $htmlname = 'socid', $fil $sql .= ", s.address, s.zip, s.town"; $sql .= ", dictp.code as country_code"; } + + if ($searchInAllEntities && isModEnabled('multicompany') && $conf->entity == 1 && $user->admin) { + $sql .= ", e.label as entity_label"; + } + + $sql .= " FROM " . $this->db->prefix() . "societe as s"; if (getDolGlobalString('COMPANY_SHOW_ADDRESS_SELECTLIST')) { $sql .= " LEFT JOIN " . $this->db->prefix() . "c_country as dictp ON dictp.rowid = s.fk_pays"; @@ -1453,7 +1459,16 @@ public function select_thirdparty_list($selected = '', $htmlname = 'socid', $fil if (!$user->hasRight('societe', 'client', 'voir') && !$user->socid) { $sql .= ", " . $this->db->prefix() . "societe_commerciaux as sc"; } - $sql .= " WHERE s.entity IN (" . getEntity('societe') . ")"; + + // If multicompany is enabled and user is admin and in master entity, search societe in all entities + // TODO must be improved, like in select_user function + if ($searchInAllEntities && isModEnabled('multicompany') && $conf->entity == 1 && $user->admin) { + $sql .= " JOIN " . $this->db->prefix() . "entity as e ON s.entity = e.rowid"; + $sql .= " WHERE s.entity IS NOT NULL"; + } else { + $sql .= " WHERE s.entity IN (" . getEntity('societe') . ")"; + } + if (!empty($user->socid)) { $sql .= " AND s.rowid = " . ((int) $user->socid); } @@ -1559,6 +1574,11 @@ public function select_thirdparty_list($selected = '', $htmlname = 'socid', $fil $label .= ' - '.$obj->tva_intra; } + // Add entity label when soc are searched in all entities + if ($searchInAllEntities && isModEnabled('multicompany') && $conf->entity == 1 && $user->admin) { + $label .= ' (' . $obj->entity_label . ')'; + } + $labelhtml = $label; if ($showtype) {