* Copyright (C) 2008-2012 Regis Houssin * Copyright (C) 2014 Juanjo Menent * Copyright (C) 2017 Rui Strecht * * 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 * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ /** * \file htdocs/core/class/html.formcompany.class.php * \ingroup core * \brief File of class to build HTML component for third parties management */ /** * Class to build HTML component for third parties management * Only common components are here. */ class FormCompany { var $db; var $error; /** * Constructor * * @param DoliDB $db Database handler */ function __construct($db) { $this->db = $db; return 1; } /** * Return list of labels (translated) of third parties type * * @param int $mode 0=Return id+label, 1=Return code+label * @param string $filter Add a SQL filter to select * @return array Array of types */ function typent_array($mode=0, $filter='') { global $langs,$mysoc; $effs = array(); $sql = "SELECT id, code, libelle"; $sql.= " FROM ".MAIN_DB_PREFIX."c_typent"; $sql.= " WHERE active = 1 AND (fk_country IS NULL OR fk_country = ".(empty($mysoc->country_id)?'0':$mysoc->country_id).")"; if ($filter) $sql.=" ".$filter; $sql.= " ORDER by position, id"; dol_syslog(get_class($this).'::typent_array', LOG_DEBUG); $resql=$this->db->query($sql); if ($resql) { $num = $this->db->num_rows($resql); $i = 0; while ($i < $num) { $objp = $this->db->fetch_object($resql); if (! $mode) $key=$objp->id; else $key=$objp->code; if ($langs->trans($objp->code) != $objp->code) $effs[$key] = $langs->trans($objp->code); else $effs[$key] = $objp->libelle; if ($effs[$key]=='-') $effs[$key]=''; $i++; } $this->db->free($resql); } return $effs; } /** * Renvoie la liste des types d'effectifs possibles (pas de traduction car nombre) * * @param int $mode 0=renvoi id+libelle, 1=renvoi code+libelle * @param string $filter Add a SQL filter to select * @return array Array of types d'effectifs */ function effectif_array($mode=0, $filter='') { $effs = array(); $sql = "SELECT id, code, libelle"; $sql .= " FROM ".MAIN_DB_PREFIX."c_effectif"; $sql.= " WHERE active = 1"; if ($filter) $sql.=" ".$filter; $sql .= " ORDER BY id ASC"; dol_syslog(get_class($this).'::effectif_array', LOG_DEBUG); $resql=$this->db->query($sql); if ($resql) { $num = $this->db->num_rows($resql); $i = 0; while ($i < $num) { $objp = $this->db->fetch_object($resql); if (! $mode) $key=$objp->id; else $key=$objp->code; $effs[$key] = $objp->libelle!='-'?$objp->libelle:''; $i++; } $this->db->free($resql); } return $effs; } /** * Affiche formulaire de selection des modes de reglement * * @param int $page Page * @param int $selected Id or code preselected * @param string $htmlname Nom du formulaire select * @param int $empty Add empty value in list * @return void */ function form_prospect_level($page, $selected='', $htmlname='prospect_level_id', $empty=0) { global $user, $langs; print '
'; print ''; print ''; dol_syslog(get_class($this).'::form_prospect_level',LOG_DEBUG); $sql = "SELECT code, label"; $sql.= " FROM ".MAIN_DB_PREFIX."c_prospectlevel"; $sql.= " WHERE active > 0"; $sql.= " ORDER BY sortorder"; $resql = $this->db->query($sql); if ($resql) { $options = array(); if ($empty) { $options[''] = ''; } while ($obj = $this->db->fetch_object($resql)) { $level = $langs->trans($obj->code); if ($level == $obj->code) { $level = $langs->trans($obj->label); } $options[$obj->code] = $level; } print Form::selectarray($htmlname, $options, $selected); } else dol_print_error($this->db); if (! empty($htmlname) && $user->admin) print ' '.info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"),1); print ''; print '
'; } /** * Retourne la liste deroulante des departements/province/cantons tout pays confondu ou pour un pays donne. * Dans le cas d'une liste tout pays confondus, l'affichage fait une rupture sur le pays. * La cle de la liste est le code (il peut y avoir plusieurs entree pour * un code donnee mais dans ce cas, le champ pays differe). * Ainsi les liens avec les departements se font sur un departement independemment de son nom. * * @param string $selected Code state preselected * @param int $country_codeid 0=list for all countries, otherwise country code or country rowid to show * @param string $htmlname Id of department * @return void */ function select_departement($selected='',$country_codeid=0, $htmlname='state_id') { print $this->select_state($selected,$country_codeid, $htmlname); } /** * Retourne la liste deroulante des departements/province/cantons tout pays confondu ou pour un pays donne. * Dans le cas d'une liste tout pays confondus, l'affichage fait une rupture sur le pays. * La cle de la liste est le code (il peut y avoir plusieurs entree pour * un code donnee mais dans ce cas, le champ pays differe). * Ainsi les liens avec les departements se font sur un departement independemment de son nom. * * @param string $selected Code state preselected (mus be state id) * @param integer $country_codeid Country code or id: 0=list for all countries, otherwise country code or country rowid to show * @param string $htmlname Id of department * @return string String with HTML select * @see select_country */ function select_state($selected='',$country_codeid=0, $htmlname='state_id') { global $conf,$langs,$user; dol_syslog(get_class($this)."::select_departement selected=".$selected.", country_codeid=".$country_codeid,LOG_DEBUG); $langs->load("dict"); $out=''; // On recherche les departements/cantons/province active d'une region et pays actif $sql = "SELECT d.rowid, d.code_departement as code, d.nom as name, d.active, c.label as country, c.code as country_code, r.nom as region_name FROM"; $sql .= " ".MAIN_DB_PREFIX ."c_departements as d, ".MAIN_DB_PREFIX."c_regions as r,".MAIN_DB_PREFIX."c_country as c"; $sql .= " WHERE d.fk_region=r.code_region and r.fk_pays=c.rowid"; $sql .= " AND d.active = 1 AND r.active = 1 AND c.active = 1"; if ($country_codeid && is_numeric($country_codeid)) $sql .= " AND c.rowid = '".$country_codeid."'"; if ($country_codeid && ! is_numeric($country_codeid)) $sql .= " AND c.code = '".$country_codeid."'"; $sql .= " ORDER BY c.code, d.code_departement"; dol_syslog(get_class($this)."::select_departement", LOG_DEBUG); $result=$this->db->query($sql); if ($result) { if (!empty($htmlname)) $out.= ''; if (! empty($htmlname) && $user->admin) $out.= ' '.info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"),1); } else { dol_print_error($this->db); } // Make select dynamic include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php'; $out .= ajax_combobox($htmlname); return $out; } /** * Retourne la liste deroulante des regions actives dont le pays est actif * La cle de la liste est le code (il peut y avoir plusieurs entree pour * un code donnee mais dans ce cas, le champ pays et lang differe). * Ainsi les liens avec les regions se font sur une region independemment de son name. * * @param string $selected Preselected value * @param string $htmlname Name of HTML select field * @return void */ function select_region($selected='',$htmlname='region_id') { global $conf,$langs; $langs->load("dict"); $sql = "SELECT r.rowid, r.code_region as code, r.nom as label, r.active, c.code as country_code, c.label as country"; $sql.= " FROM ".MAIN_DB_PREFIX."c_regions as r, ".MAIN_DB_PREFIX."c_country as c"; $sql.= " WHERE r.fk_pays=c.rowid AND r.active = 1 and c.active = 1"; $sql.= " ORDER BY c.code, c.label ASC"; dol_syslog(get_class($this)."::select_region", LOG_DEBUG); $resql=$this->db->query($sql); if ($resql) { print ''; } else { dol_print_error($this->db); } } /** * Return combo list with people title * * @param string $selected Title preselected * @param string $htmlname Name of HTML select combo field * @param string $morecss Add more css on SELECT element * @return string String with HTML select */ function select_civility($selected='',$htmlname='civility_id',$morecss='maxwidth100') { global $conf,$langs,$user; $langs->load("dict"); $out=''; $sql = "SELECT rowid, code, label, active FROM ".MAIN_DB_PREFIX."c_civility"; $sql.= " WHERE active = 1"; dol_syslog("Form::select_civility", LOG_DEBUG); $resql=$this->db->query($sql); if ($resql) { $out.= ''; if ($user->admin) $out.= info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"),1); } else { dol_print_error($this->db); } return $out; } /** * Retourne la liste deroulante des formes juridiques tous pays confondus ou pour un pays donne. * Dans le cas d'une liste tous pays confondu, on affiche une rupture sur le pays. * * @param string $selected Code forme juridique a pre-selectionne * @param mixed $country_codeid 0=liste tous pays confondus, sinon code du pays a afficher * @param string $filter Add a SQL filter on list * @return void * @deprecated Use print xxx->select_juridicalstatus instead * @see select_juridicalstatus() */ function select_forme_juridique($selected='', $country_codeid=0, $filter='') { print $this->select_juridicalstatus($selected, $country_codeid, $filter); } /** * Retourne la liste deroulante des formes juridiques tous pays confondus ou pour un pays donne. * Dans le cas d'une liste tous pays confondu, on affiche une rupture sur le pays * * @param string $selected Preselected code of juridical type * @param int $country_codeid 0=list for all countries, otherwise list only country requested * @param string $filter Add a SQL filter on list * @param string $htmlname HTML name of select * @return string String with HTML select */ function select_juridicalstatus($selected='', $country_codeid=0, $filter='', $htmlname='forme_juridique_code') { global $conf,$langs,$user; $langs->load("dict"); $out=''; // On recherche les formes juridiques actives des pays actifs $sql = "SELECT f.rowid, f.code as code , f.libelle as label, f.active, c.label as country, c.code as country_code"; $sql .= " FROM ".MAIN_DB_PREFIX."c_forme_juridique as f, ".MAIN_DB_PREFIX."c_country as c"; $sql .= " WHERE f.fk_pays=c.rowid"; $sql .= " AND f.active = 1 AND c.active = 1"; if ($country_codeid) $sql .= " AND c.code = '".$country_codeid."'"; if ($filter) $sql .= " ".$filter; $sql .= " ORDER BY c.code"; dol_syslog(get_class($this)."::select_juridicalstatus", LOG_DEBUG); $resql=$this->db->query($sql); if ($resql) { $out.= '
'; $out.= ''; if ($user->admin) $out.= ' '.info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"),1); // Make select dynamic include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php'; $out .= ajax_combobox($htmlname); $out.= '
'; } else { dol_print_error($this->db); } return $out; } /** * Output list of third parties. * * @param object $object Object we try to find contacts * @param string $var_id Name of id field * @param string $selected Pre-selected third party * @param string $htmlname Name of HTML form * @param array $limitto Disable answers that are not id in this array list * @param int $forceid This is to force another object id than object->id * @param string $moreparam String with more param to add into url when noajax search is used. * @return int The selected third party ID */ function selectCompaniesForNewContact($object, $var_id, $selected='', $htmlname='newcompany', $limitto='', $forceid=0, $moreparam='') { global $conf, $langs; if (! empty($conf->use_javascript_ajax) && ! empty($conf->global->COMPANY_USE_SEARCH_TO_SELECT)) { // Use Ajax search $minLength = (is_numeric($conf->global->COMPANY_USE_SEARCH_TO_SELECT)?$conf->global->COMPANY_USE_SEARCH_TO_SELECT:2); $socid=0; $name=''; if ($selected > 0) { $tmpthirdparty=new Societe($this->db); $result = $tmpthirdparty->fetch($selected); if ($result > 0) { $socid = $selected; $name = $tmpthirdparty->name; } } $events=array(); // Add an entry 'method' to say 'yes, we must execute url with param action = method'; // Add an entry 'url' to say which url to execute // Add an entry htmlname to say which element we must change once url is called // Add entry params => array('cssid' => 'attr') to say to remov or add attribute attr if answer of url return 0 or >0 lines // To refresh contacts list on thirdparty list change $events[]=array('method' => 'getContacts', 'url' => dol_buildpath('/core/ajax/contacts.php',1), 'htmlname' => 'contactid', 'params' => array('add-customer-contact' => 'disabled')); if (count($events)) // If there is some ajax events to run once selection is done, we add code here to run events { print ''; } print "\n".''."\n"; print ''; print ajax_autocompleter(($socid?$socid:-1), $htmlname, DOL_URL_ROOT.'/societe/ajaxcompanies.php', '', $minLength, 0); return $socid; } else { // Search to list thirdparties $sql = "SELECT s.rowid, s.nom as name FROM"; $sql.= " ".MAIN_DB_PREFIX."societe as s"; $sql.= " WHERE s.entity IN (".getEntity('societe').")"; // For ajax search we limit here. For combo list, we limit later if (is_array($limitto) && count($limitto)) { $sql.= " AND s.rowid IN (".join(',',$limitto).")"; } $sql.= " ORDER BY s.nom ASC"; $resql = $this->db->query($sql); if ($resql) { print '\n"; return $firstCompany; } else { dol_print_error($this->db); print 'Error sql'; } } } /** * Return a select list with types of contacts * * @param object $object Object to use to find type of contact * @param string $selected Default selected value * @param string $htmlname HTML select name * @param string $source Source ('internal' or 'external') * @param string $sortorder Sort criteria ('position', 'code', ...) * @param int $showempty 1=Add en empty line * @param string $morecss Add more css to select component * @return void */ function selectTypeContact($object, $selected, $htmlname = 'type', $source='internal', $sortorder='position', $showempty=0, $morecss='') { global $user, $langs; if (is_object($object) && method_exists($object, 'liste_type_contact')) { $lesTypes = $object->liste_type_contact($source, $sortorder, 0, 1); print '"; if ($user->admin) print ' '.info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"),1); print "\n"; } } /** * Return a select list with zip codes and their town * * @param string $selected Preselected value * @param string $htmlname HTML select name * @param string $fields Fields * @param int $fieldsize Field size * @param int $disableautocomplete 1 To disable ajax autocomplete features (browser autocomplete may still occurs) * @param string $moreattrib Add more attribute on HTML input field * @param string $morecss More css * @return string */ function select_ziptown($selected='', $htmlname='zipcode', $fields='', $fieldsize=0, $disableautocomplete=0, $moreattrib='',$morecss='') { global $conf; $out=''; $size=''; if (!empty($fieldsize)) $size='size="'.$fieldsize.'"'; if ($conf->use_javascript_ajax && empty($disableautocomplete)) { $out.= ajax_multiautocompleter($htmlname,$fields,DOL_URL_ROOT.'/core/ajax/ziptown.php')."\n"; $moreattrib.=' autocomplete="off"'; } $out.= ''."\n"; return $out; } /** * Return HTML string to use as input of professional id into a HTML page (siren, siret, etc...) * * @param int $idprof 1,2,3,4 (Example: 1=siren,2=siret,3=naf,4=rcs/rm) * @param string $htmlname Name of HTML select * @param string $preselected Default value to show * @param string $country_code FR, IT, ... * @param string $morecss More css * @return string HTML string with prof id */ function get_input_id_prof($idprof,$htmlname,$preselected,$country_code,$morecss='maxwidth100onsmartphone quatrevingtpercent') { global $conf,$langs; $formlength=0; if (empty($conf->global->MAIN_DISABLEPROFIDRULES)) { if ($country_code == 'FR') { if (isset($idprof)) { if ($idprof==1) $formlength=9; else if ($idprof==2) $formlength=14; else if ($idprof==3) $formlength=5; // 4 chiffres et 1 lettre depuis janvier else if ($idprof==4) $formlength=32; // No maximum as we need to include a town name in this id } } else if ($country_code == 'ES') { if ($idprof==1) $formlength=9; //CIF/NIF/NIE 9 digits if ($idprof==2) $formlength=12; //NASS 12 digits without / if ($idprof==3) $formlength=5; //CNAE 5 digits if ($idprof==4) $formlength=32; //depend of college } } $selected=$preselected; if (! $selected && isset($idprof)) { if ($idprof==1 && ! empty($this->idprof1)) $selected=$this->idprof1; else if ($idprof==2 && ! empty($this->idprof2)) $selected=$this->idprof2; else if ($idprof==3 && ! empty($this->idprof3)) $selected=$this->idprof3; else if ($idprof==4 && ! empty($this->idprof4)) $selected=$this->idprof4; } $maxlength=$formlength; if (empty($formlength)) { $formlength=24; $maxlength=128; } $out = ''; return $out; } /** * Return a HTML select with localtax values for thirdparties * * @param int $local LocalTax * @param int $selected Preselected value * @param string $htmlname HTML select name * @return void */ function select_localtax($local, $selected, $htmlname) { $tax=get_localtax_by_third($local); $num = $this->db->num_rows($tax); $i = 0; if ($num) { $valors=explode(":", $tax); if (count($valors) > 1) { //montar select print ''; } } } }