html.formcompany.class.php 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. <?php
  2. /* Copyright (C) 2008-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2008-2012 Regis Houssin <regis.houssin@inodbox.com>
  4. * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
  5. * Copyright (C) 2017 Rui Strecht <rui.strecht@aliartalentos.com>
  6. * Copyright (C) 2020 Open-Dsi <support@open-dsi.fr>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * \file htdocs/core/class/html.formcompany.class.php
  23. * \ingroup core
  24. * \brief File of class to build HTML component for third parties management
  25. */
  26. /**
  27. * Class to build HTML component for third parties management
  28. * Only common components are here.
  29. */
  30. require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
  31. /**
  32. * Class of forms component to manage companies
  33. */
  34. class FormCompany extends Form
  35. {
  36. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  37. /**
  38. * Return list of labels (translated) of third parties type
  39. *
  40. * @param int $mode 0=Return id+label, 1=Return code+label
  41. * @param string $filter Add a SQL filter to select
  42. * @return array Array of types
  43. */
  44. public function typent_array($mode = 0, $filter = '')
  45. {
  46. // phpcs:enable
  47. global $langs, $mysoc;
  48. $effs = array();
  49. $sql = "SELECT id, code, libelle";
  50. $sql .= " FROM ".MAIN_DB_PREFIX."c_typent";
  51. $sql .= " WHERE active = 1 AND (fk_country IS NULL OR fk_country = ".(empty($mysoc->country_id) ? '0' : $mysoc->country_id).")";
  52. if ($filter) $sql .= " ".$filter;
  53. $sql .= " ORDER by position, id";
  54. dol_syslog(get_class($this).'::typent_array', LOG_DEBUG);
  55. $resql = $this->db->query($sql);
  56. if ($resql)
  57. {
  58. $num = $this->db->num_rows($resql);
  59. $i = 0;
  60. while ($i < $num)
  61. {
  62. $objp = $this->db->fetch_object($resql);
  63. if (!$mode) $key = $objp->id;
  64. else $key = $objp->code;
  65. if ($langs->trans($objp->code) != $objp->code) $effs[$key] = $langs->trans($objp->code);
  66. else $effs[$key] = $objp->libelle;
  67. if ($effs[$key] == '-') $effs[$key] = '';
  68. $i++;
  69. }
  70. $this->db->free($resql);
  71. }
  72. return $effs;
  73. }
  74. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  75. /**
  76. * Renvoie la liste des types d'effectifs possibles (pas de traduction car nombre)
  77. *
  78. * @param int $mode 0=renvoi id+libelle, 1=renvoi code+libelle
  79. * @param string $filter Add a SQL filter to select
  80. * @return array Array of types d'effectifs
  81. */
  82. public function effectif_array($mode = 0, $filter = '')
  83. {
  84. // phpcs:enable
  85. $effs = array();
  86. $sql = "SELECT id, code, libelle";
  87. $sql .= " FROM ".MAIN_DB_PREFIX."c_effectif";
  88. $sql .= " WHERE active = 1";
  89. if ($filter) $sql .= " ".$filter;
  90. $sql .= " ORDER BY id ASC";
  91. dol_syslog(get_class($this).'::effectif_array', LOG_DEBUG);
  92. $resql = $this->db->query($sql);
  93. if ($resql)
  94. {
  95. $num = $this->db->num_rows($resql);
  96. $i = 0;
  97. while ($i < $num)
  98. {
  99. $objp = $this->db->fetch_object($resql);
  100. if (!$mode) $key = $objp->id;
  101. else $key = $objp->code;
  102. $effs[$key] = $objp->libelle != '-' ? $objp->libelle : '';
  103. $i++;
  104. }
  105. $this->db->free($resql);
  106. }
  107. return $effs;
  108. }
  109. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  110. /**
  111. * Affiche formulaire de selection des modes de reglement
  112. *
  113. * @param int $page Page
  114. * @param int $selected Id or code preselected
  115. * @param string $htmlname Nom du formulaire select
  116. * @param int $empty Add empty value in list
  117. * @return void
  118. */
  119. public function form_prospect_level($page, $selected = '', $htmlname = 'prospect_level_id', $empty = 0)
  120. {
  121. // phpcs:enable
  122. global $user, $langs;
  123. print '<form method="post" action="'.$page.'">';
  124. print '<input type="hidden" name="action" value="setprospectlevel">';
  125. print '<input type="hidden" name="token" value="'.newToken().'">';
  126. dol_syslog(get_class($this).'::form_prospect_level', LOG_DEBUG);
  127. $sql = "SELECT code, label";
  128. $sql .= " FROM ".MAIN_DB_PREFIX."c_prospectlevel";
  129. $sql .= " WHERE active > 0";
  130. $sql .= " ORDER BY sortorder";
  131. $resql = $this->db->query($sql);
  132. if ($resql)
  133. {
  134. $options = array();
  135. if ($empty) {
  136. $options[''] = '';
  137. }
  138. while ($obj = $this->db->fetch_object($resql)) {
  139. $level = $langs->trans($obj->code);
  140. if ($level == $obj->code) {
  141. $level = $langs->trans($obj->label);
  142. }
  143. $options[$obj->code] = $level;
  144. }
  145. print Form::selectarray($htmlname, $options, $selected);
  146. } else dol_print_error($this->db);
  147. if (!empty($htmlname) && $user->admin) print ' '.info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
  148. print '<input type="submit" class="button valignmiddle" value="'.$langs->trans("Modify").'">';
  149. print '</form>';
  150. }
  151. /**
  152. * Affiche formulaire de selection des niveau de prospection pour les contacts
  153. *
  154. * @param int $page Page
  155. * @param int $selected Id or code preselected
  156. * @param string $htmlname Nom du formulaire select
  157. * @param int $empty Add empty value in list
  158. * @return void
  159. */
  160. public function formProspectContactLevel($page, $selected = '', $htmlname = 'prospect_contact_level_id', $empty = 0)
  161. {
  162. global $user, $langs;
  163. print '<form method="post" action="' . $page . '">';
  164. print '<input type="hidden" name="action" value="setprospectcontactlevel">';
  165. print '<input type="hidden" name="token" value="' . newToken() . '">';
  166. dol_syslog(__METHOD__, LOG_DEBUG);
  167. $sql = "SELECT code, label";
  168. $sql .= " FROM " . MAIN_DB_PREFIX . "c_prospectcontactlevel";
  169. $sql .= " WHERE active > 0";
  170. $sql .= " ORDER BY sortorder";
  171. $resql = $this->db->query($sql);
  172. if ($resql)
  173. {
  174. $options = array();
  175. if ($empty)
  176. {
  177. $options[''] = '';
  178. }
  179. while ($obj = $this->db->fetch_object($resql))
  180. {
  181. $level = $langs->trans($obj->code);
  182. if ($level == $obj->code)
  183. {
  184. $level = $langs->trans($obj->label);
  185. }
  186. $options[$obj->code] = $level;
  187. }
  188. print Form::selectarray($htmlname, $options, $selected);
  189. }
  190. else dol_print_error($this->db);
  191. if (!empty($htmlname) && $user->admin) print ' ' . info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
  192. print '<input type="submit" class="button valignmiddle" value="' . $langs->trans("Modify") . '">';
  193. print '</form>';
  194. }
  195. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  196. /**
  197. * Returns the drop-down list of departments/provinces/cantons for all countries or for a given country.
  198. * In the case of an all-country list, the display breaks on the country.
  199. * The key of the list is the code (there can be several entries for a given code but in this case, the country field differs).
  200. * Thus the links with the departments are done on a department independently of its name.
  201. *
  202. * @param string $selected Code state preselected
  203. * @param int $country_codeid 0=list for all countries, otherwise country code or country rowid to show
  204. * @param string $htmlname Id of department
  205. * @return void
  206. */
  207. public function select_departement($selected = '', $country_codeid = 0, $htmlname = 'state_id')
  208. {
  209. // phpcs:enable
  210. print $this->select_state($selected, $country_codeid, $htmlname);
  211. }
  212. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  213. /**
  214. * Returns the drop-down list of departments/provinces/cantons for all countries or for a given country.
  215. * In the case of an all-country list, the display breaks on the country.
  216. * The key of the list is the code (there can be several entries for a given code but in this case, the country field differs).
  217. * Thus the links with the departments are done on a department independently of its name.
  218. *
  219. * @param int $selected Code state preselected (mus be state id)
  220. * @param integer $country_codeid Country code or id: 0=list for all countries, otherwise country code or country rowid to show
  221. * @param string $htmlname Id of department. If '', we want only the string with <option>
  222. * @return string String with HTML select
  223. * @see select_country()
  224. */
  225. public function select_state($selected = 0, $country_codeid = 0, $htmlname = 'state_id')
  226. {
  227. // phpcs:enable
  228. global $conf, $langs, $user;
  229. dol_syslog(get_class($this)."::select_departement selected=".$selected.", country_codeid=".$country_codeid, LOG_DEBUG);
  230. $langs->load("dict");
  231. $out = '';
  232. // Serch departements/cantons/province active d'une region et pays actif
  233. $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";
  234. $sql .= " ".MAIN_DB_PREFIX."c_departements as d, ".MAIN_DB_PREFIX."c_regions as r,".MAIN_DB_PREFIX."c_country as c";
  235. $sql .= " WHERE d.fk_region=r.code_region and r.fk_pays=c.rowid";
  236. $sql .= " AND d.active = 1 AND r.active = 1 AND c.active = 1";
  237. if ($country_codeid && is_numeric($country_codeid)) $sql .= " AND c.rowid = '".$this->db->escape($country_codeid)."'";
  238. if ($country_codeid && !is_numeric($country_codeid)) $sql .= " AND c.code = '".$this->db->escape($country_codeid)."'";
  239. $sql .= " ORDER BY c.code, d.code_departement";
  240. $result = $this->db->query($sql);
  241. if ($result)
  242. {
  243. if (!empty($htmlname)) $out .= '<select id="'.$htmlname.'" class="flat maxwidth200onsmartphone minwidth300" name="'.$htmlname.'">';
  244. if ($country_codeid) $out .= '<option value="0">&nbsp;</option>';
  245. $num = $this->db->num_rows($result);
  246. $i = 0;
  247. dol_syslog(get_class($this)."::select_departement num=".$num, LOG_DEBUG);
  248. if ($num)
  249. {
  250. $country = '';
  251. while ($i < $num)
  252. {
  253. $obj = $this->db->fetch_object($result);
  254. if ($obj->code == '0') // Le code peut etre une chaine
  255. {
  256. $out .= '<option value="0">&nbsp;</option>';
  257. } else {
  258. if (!$country || $country != $obj->country)
  259. {
  260. // Affiche la rupture si on est en mode liste multipays
  261. if (!$country_codeid && $obj->country_code)
  262. {
  263. $out .= '<option value="-1" disabled>----- '.$obj->country." -----</option>\n";
  264. $country = $obj->country;
  265. }
  266. }
  267. if (!empty($selected) && $selected == $obj->rowid)
  268. {
  269. $out .= '<option value="'.$obj->rowid.'" selected>';
  270. } else {
  271. $out .= '<option value="'.$obj->rowid.'">';
  272. }
  273. // Si traduction existe, on l'utilise, sinon on prend le libelle par defaut
  274. if (!empty($conf->global->MAIN_SHOW_STATE_CODE) &&
  275. ($conf->global->MAIN_SHOW_STATE_CODE == 1 || $conf->global->MAIN_SHOW_STATE_CODE == 2 || $conf->global->MAIN_SHOW_STATE_CODE === 'all')) {
  276. if (!empty($conf->global->MAIN_SHOW_REGION_IN_STATE_SELECT) && $conf->global->MAIN_SHOW_REGION_IN_STATE_SELECT == 1) {
  277. $out .= $obj->region_name.' - '.$obj->code.' - '.($langs->trans($obj->code) != $obj->code ? $langs->trans($obj->code) : ($obj->name != '-' ? $obj->name : ''));
  278. } else {
  279. $out .= $obj->code.' - '.($langs->trans($obj->code) != $obj->code ? $langs->trans($obj->code) : ($obj->name != '-' ? $obj->name : ''));
  280. }
  281. } else {
  282. if (!empty($conf->global->MAIN_SHOW_REGION_IN_STATE_SELECT) && $conf->global->MAIN_SHOW_REGION_IN_STATE_SELECT == 1) {
  283. $out .= $obj->region_name.' - '.($langs->trans($obj->code) != $obj->code ? $langs->trans($obj->code) : ($obj->name != '-' ? $obj->name : ''));
  284. } else {
  285. $out .= ($langs->trans($obj->code) != $obj->code ? $langs->trans($obj->code) : ($obj->name != '-' ? $obj->name : ''));
  286. }
  287. }
  288. $out .= '</option>';
  289. }
  290. $i++;
  291. }
  292. }
  293. if (!empty($htmlname)) $out .= '</select>';
  294. if (!empty($htmlname) && $user->admin) $out .= ' '.info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
  295. } else {
  296. dol_print_error($this->db);
  297. }
  298. // Make select dynamic
  299. if (!empty($htmlname))
  300. {
  301. include_once DOL_DOCUMENT_ROOT.'/core/lib/ajax.lib.php';
  302. $out .= ajax_combobox($htmlname);
  303. }
  304. return $out;
  305. }
  306. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  307. /**
  308. * Retourne la liste deroulante des regions actives dont le pays est actif
  309. * La cle de la liste est le code (il peut y avoir plusieurs entree pour
  310. * un code donnee mais dans ce cas, le champ pays et lang differe).
  311. * Ainsi les liens avec les regions se font sur une region independemment de son name.
  312. *
  313. * @param string $selected Preselected value
  314. * @param string $htmlname Name of HTML select field
  315. * @return void
  316. */
  317. public function select_region($selected = '', $htmlname = 'region_id')
  318. {
  319. // phpcs:enable
  320. global $conf, $langs;
  321. $langs->load("dict");
  322. $sql = "SELECT r.rowid, r.code_region as code, r.nom as label, r.active, c.code as country_code, c.label as country";
  323. $sql .= " FROM ".MAIN_DB_PREFIX."c_regions as r, ".MAIN_DB_PREFIX."c_country as c";
  324. $sql .= " WHERE r.fk_pays=c.rowid AND r.active = 1 and c.active = 1";
  325. $sql .= " ORDER BY c.code, c.label ASC";
  326. dol_syslog(get_class($this)."::select_region", LOG_DEBUG);
  327. $resql = $this->db->query($sql);
  328. if ($resql)
  329. {
  330. print '<select class="flat" name="'.$htmlname.'">';
  331. $num = $this->db->num_rows($resql);
  332. $i = 0;
  333. if ($num)
  334. {
  335. $country = '';
  336. while ($i < $num)
  337. {
  338. $obj = $this->db->fetch_object($resql);
  339. if ($obj->code == 0) {
  340. print '<option value="0">&nbsp;</option>';
  341. } else {
  342. if ($country == '' || $country != $obj->country)
  343. {
  344. // Show break
  345. $key = $langs->trans("Country".strtoupper($obj->country_code));
  346. $valuetoshow = ($key != "Country".strtoupper($obj->country_code)) ? $obj->country_code." - ".$key : $obj->country;
  347. print '<option value="-1" disabled>----- '.$valuetoshow." -----</option>\n";
  348. $country = $obj->country;
  349. }
  350. if ($selected > 0 && $selected == $obj->code)
  351. {
  352. print '<option value="'.$obj->code.'" selected>'.$obj->label.'</option>';
  353. } else {
  354. print '<option value="'.$obj->code.'">'.$obj->label.'</option>';
  355. }
  356. }
  357. $i++;
  358. }
  359. }
  360. print '</select>';
  361. } else {
  362. dol_print_error($this->db);
  363. }
  364. }
  365. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  366. /**
  367. * Return combo list with people title
  368. *
  369. * @param string $selected Civility/Title code preselected
  370. * @param string $htmlname Name of HTML select combo field
  371. * @param string $morecss Add more css on SELECT element
  372. * @param int $addjscombo Add js combo
  373. * @return string String with HTML select
  374. */
  375. public function select_civility($selected = '', $htmlname = 'civility_id', $morecss = 'maxwidth150', $addjscombo = 0)
  376. {
  377. // phpcs:enable
  378. global $conf, $langs, $user;
  379. $langs->load("dict");
  380. $out = '';
  381. $sql = "SELECT rowid, code, label, active FROM ".MAIN_DB_PREFIX."c_civility";
  382. $sql .= " WHERE active = 1";
  383. dol_syslog("Form::select_civility", LOG_DEBUG);
  384. $resql = $this->db->query($sql);
  385. if ($resql)
  386. {
  387. $out .= '<select class="flat'.($morecss ? ' '.$morecss : '').'" name="'.$htmlname.'" id="'.$htmlname.'">';
  388. $out .= '<option value="">&nbsp;</option>';
  389. $num = $this->db->num_rows($resql);
  390. $i = 0;
  391. if ($num)
  392. {
  393. while ($i < $num)
  394. {
  395. $obj = $this->db->fetch_object($resql);
  396. if ($selected == $obj->code)
  397. {
  398. $out .= '<option value="'.$obj->code.'" selected>';
  399. } else {
  400. $out .= '<option value="'.$obj->code.'">';
  401. }
  402. // If translation exists, we use it, otherwise, we use tha had coded label
  403. $out .= ($langs->trans("Civility".$obj->code) != "Civility".$obj->code ? $langs->trans("Civility".$obj->code) : ($obj->label != '-' ? $obj->label : ''));
  404. $out .= '</option>';
  405. $i++;
  406. }
  407. }
  408. $out .= '</select>';
  409. if ($user->admin) $out .= info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
  410. if ($addjscombo) {
  411. // Enhance with select2
  412. include_once DOL_DOCUMENT_ROOT.'/core/lib/ajax.lib.php';
  413. $out .= ajax_combobox($htmlname);
  414. }
  415. } else {
  416. dol_print_error($this->db);
  417. }
  418. return $out;
  419. }
  420. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  421. /**
  422. * Retourne la liste deroulante des formes juridiques tous pays confondus ou pour un pays donne.
  423. * Dans le cas d'une liste tous pays confondu, on affiche une rupture sur le pays.
  424. *
  425. * @param string $selected Code forme juridique a pre-selectionne
  426. * @param mixed $country_codeid 0=liste tous pays confondus, sinon code du pays a afficher
  427. * @param string $filter Add a SQL filter on list
  428. * @return void
  429. * @deprecated Use print xxx->select_juridicalstatus instead
  430. * @see select_juridicalstatus()
  431. */
  432. public function select_forme_juridique($selected = '', $country_codeid = 0, $filter = '')
  433. {
  434. // phpcs:enable
  435. print $this->select_juridicalstatus($selected, $country_codeid, $filter);
  436. }
  437. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  438. /**
  439. * Retourne la liste deroulante des formes juridiques tous pays confondus ou pour un pays donne.
  440. * Dans le cas d'une liste tous pays confondu, on affiche une rupture sur le pays
  441. *
  442. * @param string $selected Preselected code of juridical type
  443. * @param int $country_codeid 0=list for all countries, otherwise list only country requested
  444. * @param string $filter Add a SQL filter on list
  445. * @param string $htmlname HTML name of select
  446. * @param string $morecss More CSS
  447. * @return string String with HTML select
  448. */
  449. public function select_juridicalstatus($selected = '', $country_codeid = 0, $filter = '', $htmlname = 'forme_juridique_code', $morecss = '')
  450. {
  451. // phpcs:enable
  452. global $conf, $langs, $user;
  453. $langs->load("dict");
  454. $out = '';
  455. // On recherche les formes juridiques actives des pays actifs
  456. $sql = "SELECT f.rowid, f.code as code , f.libelle as label, f.active, c.label as country, c.code as country_code";
  457. $sql .= " FROM ".MAIN_DB_PREFIX."c_forme_juridique as f, ".MAIN_DB_PREFIX."c_country as c";
  458. $sql .= " WHERE f.fk_pays=c.rowid";
  459. $sql .= " AND f.active = 1 AND c.active = 1";
  460. if ($country_codeid) $sql .= " AND c.code = '".$country_codeid."'";
  461. if ($filter) $sql .= " ".$filter;
  462. $sql .= " ORDER BY c.code";
  463. dol_syslog(get_class($this)."::select_juridicalstatus", LOG_DEBUG);
  464. $resql = $this->db->query($sql);
  465. if ($resql)
  466. {
  467. $out .= '<div id="particulier2" class="visible">';
  468. $out .= '<select class="flat minwidth200'.($morecss ? ' '.$morecss : '').'" name="'.$htmlname.'" id="'.$htmlname.'">';
  469. if ($country_codeid) $out .= '<option value="0">&nbsp;</option>'; // When country_codeid is set, we force to add an empty line because it does not appears from select. When not set, we already get the empty line from select.
  470. $num = $this->db->num_rows($resql);
  471. if ($num)
  472. {
  473. $i = 0;
  474. $country = ''; $arraydata = array();
  475. while ($i < $num)
  476. {
  477. $obj = $this->db->fetch_object($resql);
  478. if ($obj->code) // We exclude empty line, we will add it later
  479. {
  480. $labelcountry = (($langs->trans("Country".$obj->country_code) != "Country".$obj->country_code) ? $langs->trans("Country".$obj->country_code) : $obj->country);
  481. $labeljs = (($langs->trans("JuridicalStatus".$obj->code) != "JuridicalStatus".$obj->code) ? $langs->trans("JuridicalStatus".$obj->code) : ($obj->label != '-' ? $obj->label : '')); // $obj->label is already in output charset (converted by database driver)
  482. $arraydata[$obj->code] = array('code'=>$obj->code, 'label'=>$labeljs, 'label_sort'=>$labelcountry.'_'.$labeljs, 'country_code'=>$obj->country_code, 'country'=>$labelcountry);
  483. }
  484. $i++;
  485. }
  486. $arraydata = dol_sort_array($arraydata, 'label_sort', 'ASC');
  487. if (empty($country_codeid)) // Introduce empty value (if $country_codeid not empty, empty value was already added)
  488. {
  489. $arraydata[0] = array('code'=>0, 'label'=>'', 'label_sort'=>'_', 'country_code'=>'', 'country'=>'');
  490. }
  491. foreach ($arraydata as $key => $val)
  492. {
  493. if (!$country || $country != $val['country'])
  494. {
  495. // Show break when we are in multi country mode
  496. if (empty($country_codeid) && $val['country_code'])
  497. {
  498. $out .= '<option value="0" disabled class="selectoptiondisabledwhite">----- '.$val['country']." -----</option>\n";
  499. $country = $val['country'];
  500. }
  501. }
  502. if ($selected > 0 && $selected == $val['code'])
  503. {
  504. $out .= '<option value="'.$val['code'].'" selected>';
  505. } else {
  506. $out .= '<option value="'.$val['code'].'">';
  507. }
  508. // If translation exists, we use it, otherwise we use default label in database
  509. $out .= $val['label'];
  510. $out .= '</option>';
  511. }
  512. }
  513. $out .= '</select>';
  514. if ($user->admin) $out .= ' '.info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
  515. // Make select dynamic
  516. include_once DOL_DOCUMENT_ROOT.'/core/lib/ajax.lib.php';
  517. $out .= ajax_combobox($htmlname);
  518. $out .= '</div>';
  519. } else {
  520. dol_print_error($this->db);
  521. }
  522. return $out;
  523. }
  524. /**
  525. * Output list of third parties.
  526. *
  527. * @param object $object Object we try to find contacts
  528. * @param string $var_id Name of id field
  529. * @param string $selected Pre-selected third party
  530. * @param string $htmlname Name of HTML form
  531. * @param array $limitto Disable answers that are not id in this array list
  532. * @param int $forceid This is to force another object id than object->id
  533. * @param string $moreparam String with more param to add into url when noajax search is used.
  534. * @param string $morecss More CSS on select component
  535. * @return int The selected third party ID
  536. */
  537. public function selectCompaniesForNewContact($object, $var_id, $selected = '', $htmlname = 'newcompany', $limitto = '', $forceid = 0, $moreparam = '', $morecss = '')
  538. {
  539. global $conf, $langs;
  540. if (!empty($conf->use_javascript_ajax) && !empty($conf->global->COMPANY_USE_SEARCH_TO_SELECT))
  541. {
  542. // Use Ajax search
  543. $minLength = (is_numeric($conf->global->COMPANY_USE_SEARCH_TO_SELECT) ? $conf->global->COMPANY_USE_SEARCH_TO_SELECT : 2);
  544. $socid = 0; $name = '';
  545. if ($selected > 0)
  546. {
  547. $tmpthirdparty = new Societe($this->db);
  548. $result = $tmpthirdparty->fetch($selected);
  549. if ($result > 0)
  550. {
  551. $socid = $selected;
  552. $name = $tmpthirdparty->name;
  553. }
  554. }
  555. $events = array();
  556. // Add an entry 'method' to say 'yes, we must execute url with param action = method';
  557. // Add an entry 'url' to say which url to execute
  558. // Add an entry htmlname to say which element we must change once url is called
  559. // Add entry params => array('cssid' => 'attr') to say to remov or add attribute attr if answer of url return 0 or >0 lines
  560. // To refresh contacts list on thirdparty list change
  561. $events[] = array('method' => 'getContacts', 'url' => dol_buildpath('/core/ajax/contacts.php', 1), 'htmlname' => 'contactid', 'params' => array('add-customer-contact' => 'disabled'));
  562. if (count($events)) // If there is some ajax events to run once selection is done, we add code here to run events
  563. {
  564. print '<script type="text/javascript">
  565. jQuery(document).ready(function() {
  566. $("#search_'.$htmlname.'").change(function() {
  567. var obj = '.json_encode($events).';
  568. $.each(obj, function(key,values) {
  569. if (values.method.length) {
  570. runJsCodeForEvent'.$htmlname.'(values);
  571. }
  572. });
  573. $(this).trigger("blur");
  574. });
  575. // Function used to execute events when search_htmlname change
  576. function runJsCodeForEvent'.$htmlname.'(obj) {
  577. var id = $("#'.$htmlname.'").val();
  578. var method = obj.method;
  579. var url = obj.url;
  580. var htmlname = obj.htmlname;
  581. var showempty = obj.showempty;
  582. console.log("Run runJsCodeForEvent-'.$htmlname.' from selectCompaniesForNewContact id="+id+" method="+method+" showempty="+showempty+" url="+url+" htmlname="+htmlname);
  583. $.getJSON(url,
  584. {
  585. action: method,
  586. id: id,
  587. htmlname: htmlname
  588. },
  589. function(response) {
  590. if (response != null)
  591. {
  592. console.log("Change select#"+htmlname+" with content "+response.value)
  593. $.each(obj.params, function(key,action) {
  594. if (key.length) {
  595. var num = response.num;
  596. if (num > 0) {
  597. $("#" + key).removeAttr(action);
  598. } else {
  599. $("#" + key).attr(action, action);
  600. }
  601. }
  602. });
  603. $("select#" + htmlname).html(response.value);
  604. }
  605. }
  606. );
  607. };
  608. });
  609. </script>';
  610. }
  611. print "\n".'<!-- Input text for third party with Ajax.Autocompleter (selectCompaniesForNewContact) -->'."\n";
  612. print '<input type="text" size="30" id="search_'.$htmlname.'" name="search_'.$htmlname.'" value="'.$name.'" />';
  613. print ajax_autocompleter(($socid ? $socid : -1), $htmlname, DOL_URL_ROOT.'/societe/ajaxcompanies.php', '', $minLength, 0);
  614. return $socid;
  615. } else {
  616. // Search to list thirdparties
  617. $sql = "SELECT s.rowid, s.nom as name FROM";
  618. $sql .= " ".MAIN_DB_PREFIX."societe as s";
  619. $sql .= " WHERE s.entity IN (".getEntity('societe').")";
  620. // For ajax search we limit here. For combo list, we limit later
  621. if (is_array($limitto) && count($limitto))
  622. {
  623. $sql .= " AND s.rowid IN (".join(',', $limitto).")";
  624. }
  625. $sql .= " ORDER BY s.nom ASC";
  626. $resql = $this->db->query($sql);
  627. if ($resql)
  628. {
  629. print '<select class="flat'.($morecss ? ' '.$morecss : '').'" id="'.$htmlname.'" name="'.$htmlname.'"';
  630. if ($conf->use_javascript_ajax)
  631. {
  632. $javaScript = "window.location='".$_SERVER['PHP_SELF']."?".$var_id."=".($forceid > 0 ? $forceid : $object->id).$moreparam."&".$htmlname."=' + form.".$htmlname.".options[form.".$htmlname.".selectedIndex].value;";
  633. print ' onChange="'.$javaScript.'"';
  634. }
  635. print '>';
  636. $num = $this->db->num_rows($resql);
  637. $i = 0;
  638. if ($num)
  639. {
  640. while ($i < $num)
  641. {
  642. $obj = $this->db->fetch_object($resql);
  643. if ($i == 0) $firstCompany = $obj->rowid;
  644. $disabled = 0;
  645. if (is_array($limitto) && count($limitto) && !in_array($obj->rowid, $limitto)) $disabled = 1;
  646. if ($selected > 0 && $selected == $obj->rowid)
  647. {
  648. print '<option value="'.$obj->rowid.'"';
  649. if ($disabled) print ' disabled';
  650. print ' selected>'.dol_trunc($obj->name, 24).'</option>';
  651. $firstCompany = $obj->rowid;
  652. } else {
  653. print '<option value="'.$obj->rowid.'"';
  654. if ($disabled) print ' disabled';
  655. print '>'.dol_trunc($obj->name, 24).'</option>';
  656. }
  657. $i++;
  658. }
  659. }
  660. print "</select>\n";
  661. print ajax_combobox($htmlname);
  662. return $firstCompany;
  663. } else {
  664. dol_print_error($this->db);
  665. return 0;
  666. }
  667. }
  668. }
  669. /**
  670. * Return a select list with types of contacts
  671. *
  672. * @param object $object Object to use to find type of contact
  673. * @param string $selected Default selected value
  674. * @param string $htmlname HTML select name
  675. * @param string $source Source ('internal' or 'external')
  676. * @param string $sortorder Sort criteria ('position', 'code', ...)
  677. * @param int $showempty 1=Add en empty line
  678. * @param string $morecss Add more css to select component
  679. * @return void
  680. */
  681. public function selectTypeContact($object, $selected, $htmlname = 'type', $source = 'internal', $sortorder = 'position', $showempty = 0, $morecss = '')
  682. {
  683. global $user, $langs;
  684. if (is_object($object) && method_exists($object, 'liste_type_contact'))
  685. {
  686. $lesTypes = $object->liste_type_contact($source, $sortorder, 0, 1);
  687. print '<select class="flat valignmiddle'.($morecss ? ' '.$morecss : '').'" name="'.$htmlname.'" id="'.$htmlname.'">';
  688. if ($showempty) print '<option value="0"></option>';
  689. foreach ($lesTypes as $key=>$value)
  690. {
  691. print '<option value="'.$key.'"';
  692. if ($key == $selected) print ' selected';
  693. print '>'.$value.'</option>';
  694. }
  695. print "</select>";
  696. if ($user->admin) print ' '.info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
  697. print ajax_combobox($htmlname);
  698. print "\n";
  699. }
  700. }
  701. /**
  702. * showContactRoles on view and edit mode
  703. *
  704. * @param string $htmlname Html component name and id
  705. * @param Contact $contact Contact Obejct
  706. * @param string $rendermode view, edit
  707. * @param array $selected $key=>$val $val is selected Roles for input mode
  708. * @return string String with contacts roles
  709. */
  710. public function showRoles($htmlname, Contact $contact, $rendermode = 'view', $selected = array())
  711. {
  712. if ($rendermode === 'view') {
  713. $toprint = array();
  714. foreach ($contact->roles as $key => $val) {
  715. $toprint[] = '<li class="select2-search-choice-dolibarr noborderoncategories" style="background: #aaa;">'.$val['label'].'</li>';
  716. }
  717. return '<div class="select2-container-multi-dolibarr" style="width: 90%;" id="'.$htmlname.'"><ul class="select2-choices-dolibarr">'.implode(' ', $toprint).'</ul></div>';
  718. }
  719. if ($rendermode === 'edit')
  720. {
  721. $contactType = $contact->listeTypeContacts('external', '', 1, '', '', 'agenda'); // We exclude agenda as there is no contact on such element
  722. if (count($selected) > 0) {
  723. $newselected = array();
  724. foreach ($selected as $key=>$val) {
  725. if (is_array($val) && array_key_exists('id', $val) && in_array($val['id'], array_keys($contactType))) {
  726. $newselected[] = $val['id'];
  727. } else {
  728. break;
  729. }
  730. }
  731. if (count($newselected) > 0) $selected = $newselected;
  732. }
  733. return $this->multiselectarray($htmlname, $contactType, $selected);
  734. }
  735. return 'ErrorBadValueForParameterRenderMode'; // Should not happened
  736. }
  737. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  738. /**
  739. * Return a select list with zip codes and their town
  740. *
  741. * @param string $selected Preselected value
  742. * @param string $htmlname HTML select name
  743. * @param string $fields Fields
  744. * @param int $fieldsize Field size
  745. * @param int $disableautocomplete 1 To disable ajax autocomplete features (browser autocomplete may still occurs)
  746. * @param string $moreattrib Add more attribute on HTML input field
  747. * @param string $morecss More css
  748. * @return string
  749. */
  750. public function select_ziptown($selected = '', $htmlname = 'zipcode', $fields = '', $fieldsize = 0, $disableautocomplete = 0, $moreattrib = '', $morecss = '')
  751. {
  752. // phpcs:enable
  753. global $conf;
  754. $out = '';
  755. $size = '';
  756. if (!empty($fieldsize)) $size = 'size="'.$fieldsize.'"';
  757. if ($conf->use_javascript_ajax && empty($disableautocomplete))
  758. {
  759. $out .= ajax_multiautocompleter($htmlname, $fields, DOL_URL_ROOT.'/core/ajax/ziptown.php')."\n";
  760. $moreattrib .= ' autocomplete="off"';
  761. }
  762. $out .= '<input id="'.$htmlname.'" class="maxwidthonsmartphone'.($morecss ? ' '.$morecss : '').'" type="text"'.($moreattrib ? ' '.$moreattrib : '').' name="'.$htmlname.'" '.$size.' value="'.$selected.'">'."\n";
  763. return $out;
  764. }
  765. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  766. /**
  767. * Return HTML string to use as input of professional id into a HTML page (siren, siret, etc...)
  768. *
  769. * @param int $idprof 1,2,3,4 (Example: 1=siren,2=siret,3=naf,4=rcs/rm)
  770. * @param string $htmlname Name of HTML select
  771. * @param string $preselected Default value to show
  772. * @param string $country_code FR, IT, ...
  773. * @param string $morecss More css
  774. * @return string HTML string with prof id
  775. */
  776. public function get_input_id_prof($idprof, $htmlname, $preselected, $country_code, $morecss = 'maxwidth100onsmartphone quatrevingtpercent')
  777. {
  778. // phpcs:enable
  779. global $conf, $langs, $hookmanager;
  780. $formlength = 0;
  781. if (empty($conf->global->MAIN_DISABLEPROFIDRULES)) {
  782. if ($country_code == 'FR')
  783. {
  784. if (isset($idprof)) {
  785. if ($idprof == 1) $formlength = 9;
  786. elseif ($idprof == 2) $formlength = 14;
  787. elseif ($idprof == 3) $formlength = 5; // 4 chiffres et 1 lettre depuis janvier
  788. elseif ($idprof == 4) $formlength = 32; // No maximum as we need to include a town name in this id
  789. }
  790. } elseif ($country_code == 'ES')
  791. {
  792. if ($idprof == 1) $formlength = 9; //CIF/NIF/NIE 9 digits
  793. if ($idprof == 2) $formlength = 12; //NASS 12 digits without /
  794. if ($idprof == 3) $formlength = 5; //CNAE 5 digits
  795. if ($idprof == 4) $formlength = 32; //depend of college
  796. }
  797. }
  798. $selected = $preselected;
  799. if (!$selected && isset($idprof)) {
  800. if ($idprof == 1 && !empty($this->idprof1)) $selected = $this->idprof1;
  801. elseif ($idprof == 2 && !empty($this->idprof2)) $selected = $this->idprof2;
  802. elseif ($idprof == 3 && !empty($this->idprof3)) $selected = $this->idprof3;
  803. elseif ($idprof == 4 && !empty($this->idprof4)) $selected = $this->idprof4;
  804. }
  805. $maxlength = $formlength;
  806. if (empty($formlength)) { $formlength = 24; $maxlength = 128; }
  807. $out = '';
  808. // Execute hook getInputIdProf to complete or replace $out
  809. $parameters = array('formlength'=>$formlength, 'selected'=>$preselected, 'idprof'=>$idprof, 'htmlname'=>$htmlname, 'country_code'=>$country_code);
  810. $reshook = $hookmanager->executeHooks('getInputIdProf', $parameters);
  811. if (empty($reshook))
  812. {
  813. $out .= '<input type="text" '.($morecss ? 'class="'.$morecss.'" ' : '').'name="'.$htmlname.'" id="'.$htmlname.'" maxlength="'.$maxlength.'" value="'.$selected.'">';
  814. }
  815. $out .= $hookmanager->resPrint;
  816. return $out;
  817. }
  818. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  819. /**
  820. * Return a HTML select with localtax values for thirdparties
  821. *
  822. * @param int $local LocalTax
  823. * @param int $selected Preselected value
  824. * @param string $htmlname HTML select name
  825. * @return void
  826. */
  827. public function select_localtax($local, $selected, $htmlname)
  828. {
  829. // phpcs:enable
  830. $tax = get_localtax_by_third($local);
  831. $num = $this->db->num_rows($tax);
  832. $i = 0;
  833. if ($num)
  834. {
  835. $valors = explode(":", $tax);
  836. if (count($valors) > 1)
  837. {
  838. //montar select
  839. print '<select class="flat" name="'.$htmlname.'" id="'.$htmlname.'">';
  840. while ($i <= (count($valors)) - 1)
  841. {
  842. if ($selected == $valors[$i])
  843. {
  844. print '<option value="'.$valors[$i].'" selected>';
  845. } else {
  846. print '<option value="'.$valors[$i].'">';
  847. }
  848. print $valors[$i];
  849. print '</option>';
  850. $i++;
  851. }
  852. print'</select>';
  853. }
  854. }
  855. }
  856. /**
  857. * Return a HTML select for thirdparty type
  858. *
  859. * @param int $selected selected value
  860. * @param string $htmlname HTML select name
  861. * @param string $htmlidname HTML select id
  862. * @param string $typeinput HTML output
  863. * @param string $morecss More css
  864. * @return string HTML string
  865. */
  866. public function selectProspectCustomerType($selected, $htmlname = 'client', $htmlidname = 'customerprospect', $typeinput = 'form', $morecss = '')
  867. {
  868. global $conf, $langs;
  869. $out = '<select class="flat '.$morecss.'" name="'.$htmlname.'" id="'.$htmlidname.'">';
  870. if ($typeinput == 'form') {
  871. if ($selected == '' || $selected == '-1') $out .= '<option value="-1">&nbsp;</option>';
  872. if (empty($conf->global->SOCIETE_DISABLE_PROSPECTS)) {
  873. $out .= '<option value="2"'.($selected == 2 ? ' selected' : '').'>'.$langs->trans('Prospect').'</option>';
  874. }
  875. if (empty($conf->global->SOCIETE_DISABLE_PROSPECTS) && empty($conf->global->SOCIETE_DISABLE_CUSTOMERS) && empty($conf->global->SOCIETE_DISABLE_PROSPECTSCUSTOMERS)) {
  876. $out .= '<option value="3"'.($selected == 3 ? ' selected' : '').'>'.$langs->trans('ProspectCustomer').'</option>';
  877. }
  878. if (empty($conf->global->SOCIETE_DISABLE_CUSTOMERS)) {
  879. $out .= '<option value="1"'.($selected == 1 ? ' selected' : '').'>'.$langs->trans('Customer').'</option>';
  880. }
  881. $out .= '<option value="0"'.((string) $selected == '0' ? ' selected' : '').'>'.$langs->trans('NorProspectNorCustomer').'</option>';
  882. } elseif ($typeinput == 'list') {
  883. $out .= '<option value="-1"'.(($selected == '' || $selected == '-1') ? ' selected' : '').'>&nbsp;</option>';
  884. if (empty($conf->global->SOCIETE_DISABLE_PROSPECTS)) {
  885. $out .= '<option value="2,3"'.($selected == '2,3' ? ' selected' : '').'>'.$langs->trans('Prospect').'</option>';
  886. }
  887. if (empty($conf->global->SOCIETE_DISABLE_CUSTOMERS)) {
  888. $out .= '<option value="1,3"'.($selected == '1,3' ? ' selected' : '').'>'.$langs->trans('Customer').'</option>';
  889. }
  890. $out .= '<option value="4"'.($selected == '4' ? ' selected' : '').'>'.$langs->trans('Supplier').'</option>';
  891. $out .= '<option value="0"'.($selected == '0' ? ' selected' : '').'>'.$langs->trans('Other').'</option>';
  892. } elseif ($typeinput == 'admin') {
  893. if (empty($conf->global->SOCIETE_DISABLE_PROSPECTS) && empty($conf->global->SOCIETE_DISABLE_CUSTOMERS) && empty($conf->global->SOCIETE_DISABLE_PROSPECTSCUSTOMERS)) {
  894. $out .= '<option value="3"'.($selected == 3 ? ' selected' : '').'>'.$langs->trans('ProspectCustomer').'</option>';
  895. }
  896. if (empty($conf->global->SOCIETE_DISABLE_CUSTOMERS)) {
  897. $out .= '<option value="1"'.($selected == 1 ? ' selected' : '').'>'.$langs->trans('Customer').'</option>';
  898. }
  899. }
  900. $out .= '</select>';
  901. return $out;
  902. }
  903. }