html.formprojet.class.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. <?php
  2. /* Copyright (c) 2013 Florian Henry <florian.henry@open-concept.pro>
  3. * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
  4. * Copyright (C) 2018 Charlene Benke <charlie@patas-monkey.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/core/class/html.formprojet.class.php
  21. * \ingroup core
  22. * \brief Class file for html component project
  23. */
  24. /**
  25. * Class to manage building of HTML components
  26. */
  27. class FormProjets
  28. {
  29. /**
  30. * @var DoliDB Database handler.
  31. */
  32. public $db;
  33. /**
  34. * @var string Error code (or message)
  35. */
  36. public $error='';
  37. /**
  38. * Constructor
  39. *
  40. * @param DoliDB $db Database handler
  41. */
  42. function __construct($db)
  43. {
  44. $this->db = $db;
  45. }
  46. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  47. /**
  48. * Output a combo list with projects qualified for a third party / user
  49. *
  50. * @param int $socid Id third party (-1=all, 0=only projects not linked to a third party, id=projects not linked or linked to third party id)
  51. * @param string $selected Id project preselected ('' or id of project)
  52. * @param string $htmlname Name of HTML field
  53. * @param int $maxlength Maximum length of label
  54. * @param int $option_only Return only html options lines without the select tag
  55. * @param int $show_empty Add an empty line
  56. * @param int $discard_closed Discard closed projects (0=Keep,1=hide completely,2=Disable)
  57. * @param int $forcefocus Force focus on field (works with javascript only)
  58. * @param int $disabled Disabled
  59. * @param int $mode 0 for HTML mode and 1 for JSON mode
  60. * @param string $filterkey Key to filter
  61. * @param int $nooutput No print output. Return it only.
  62. * @param int $forceaddid Force to add project id in list, event if not qualified
  63. * @param string $morecss More css
  64. * @param int $htmlid Html id to use instead of htmlname
  65. * @return string Return html content
  66. */
  67. function select_projects($socid=-1, $selected='', $htmlname='projectid', $maxlength=16, $option_only=0, $show_empty=1, $discard_closed=0, $forcefocus=0, $disabled=0, $mode = 0, $filterkey = '', $nooutput=0, $forceaddid=0, $morecss='', $htmlid='')
  68. {
  69. // phpcs:enable
  70. global $langs,$conf,$form;
  71. $out='';
  72. if (! empty($conf->use_javascript_ajax) && ! empty($conf->global->PROJECT_USE_SEARCH_TO_SELECT))
  73. {
  74. $placeholder='';
  75. if ($selected && empty($selected_input_value))
  76. {
  77. require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  78. $project = new Project($this->db);
  79. $project->fetch($selected);
  80. $selected_input_value=$project->ref;
  81. }
  82. $urloption='socid='.$socid.'&htmlname='.$htmlname.'&discardclosed='.$discard_closed;
  83. $out.=ajax_autocompleter($selected, $htmlname, DOL_URL_ROOT.'/projet/ajax/projects.php', $urloption, $conf->global->PROJECT_USE_SEARCH_TO_SELECT, 0, array(
  84. // 'update' => array(
  85. // 'projectid' => 'id'
  86. // )
  87. ));
  88. $out.='<input type="text" class="minwidth200'.($morecss?' '.$morecss:'').'" name="search_'.$htmlname.'" id="search_'.$htmlname.'" value="'.$selected_input_value.'"'.$placeholder.' />';
  89. }
  90. else
  91. {
  92. $out.=$this->select_projects_list($socid, $selected, $htmlname, $maxlength, $option_only, $show_empty, $discard_closed, $forcefocus, $disabled, 0, $filterkey, 1, $forceaddid, $htmlid, $morecss);
  93. }
  94. if ($discard_closed)
  95. {
  96. if (class_exists('Form'))
  97. {
  98. if (empty($form)) $form=new Form($this->db);
  99. $out.=$form->textwithpicto('', $langs->trans("ClosedProjectsAreHidden"));
  100. }
  101. }
  102. if (empty($nooutput))
  103. {
  104. print $out;
  105. return '';
  106. }
  107. else return $out;
  108. }
  109. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  110. /**
  111. * Returns an array with projects qualified for a third party
  112. *
  113. * @param int $socid Id third party (-1=all, 0=only projects not linked to a third party, id=projects not linked or linked to third party id)
  114. * @param int $selected Id project preselected
  115. * @param string $htmlname Nom de la zone html
  116. * @param int $maxlength Maximum length of label
  117. * @param int $option_only Return only html options lines without the select tag
  118. * @param int $show_empty Add an empty line
  119. * @param int $discard_closed Discard closed projects (0=Keep,1=hide completely,2=Disable)
  120. * @param int $forcefocus Force focus on field (works with javascript only)
  121. * @param int $disabled Disabled
  122. * @param int $mode 0 for HTML mode and 1 for array return (to be used by json_encode for example)
  123. * @param string $filterkey Key to filter
  124. * @param int $nooutput No print output. Return it only.
  125. * @param int $forceaddid Force to add project id in list, event if not qualified
  126. * @param int $htmlid Html id to use instead of htmlname
  127. * @param string $morecss More CSS
  128. * @return int Nb of project if OK, <0 if KO
  129. */
  130. function select_projects_list($socid=-1, $selected='', $htmlname='projectid', $maxlength=24, $option_only=0, $show_empty=1, $discard_closed=0, $forcefocus=0, $disabled=0, $mode=0, $filterkey = '', $nooutput=0, $forceaddid=0, $htmlid='', $morecss='maxwidth500')
  131. {
  132. // phpcs:enable
  133. global $user,$conf,$langs;
  134. require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  135. if (empty($htmlid)) $htmlid = $htmlname;
  136. $out='';
  137. $outarray=array();
  138. $hideunselectables = false;
  139. if (! empty($conf->global->CONTRACT_HIDE_UNSELECTABLES)) $hideunselectables = true;
  140. $projectsListId = false;
  141. if (empty($user->rights->projet->all->lire))
  142. {
  143. $projectstatic=new Project($this->db);
  144. $projectsListId = $projectstatic->getProjectsAuthorizedForUser($user,0,1);
  145. }
  146. // Search all projects
  147. $sql = 'SELECT p.rowid, p.ref, p.title, p.fk_soc, p.fk_statut, p.public, s.nom as name, s.name_alias';
  148. $sql.= ' FROM '.MAIN_DB_PREFIX .'projet as p LEFT JOIN '.MAIN_DB_PREFIX .'societe as s ON s.rowid = p.fk_soc';
  149. $sql.= " WHERE p.entity IN (".getEntity('project').")";
  150. if ($projectsListId !== false) $sql.= " AND p.rowid IN (".$projectsListId.")";
  151. if ($socid == 0) $sql.= " AND (p.fk_soc=0 OR p.fk_soc IS NULL)";
  152. if ($socid > 0)
  153. {
  154. if (empty($conf->global->PROJECT_ALLOW_TO_LINK_FROM_OTHER_COMPANY)) $sql.= " AND (p.fk_soc=".$socid." OR p.fk_soc IS NULL)";
  155. else if ($conf->global->PROJECT_ALLOW_TO_LINK_FROM_OTHER_COMPANY != 'all') // PROJECT_ALLOW_TO_LINK_FROM_OTHER_COMPANY is 'all' or a list of ids separated by coma.
  156. {
  157. $sql.= " AND (p.fk_soc IN (".$socid.", ".$conf->global->PROJECT_ALLOW_TO_LINK_FROM_OTHER_COMPANY.") OR p.fk_soc IS NULL)";
  158. }
  159. }
  160. if (!empty($filterkey)) $sql .= natural_search(array('p.title', 'p.ref'), $filterkey);
  161. $sql.= " ORDER BY p.ref ASC";
  162. $resql=$this->db->query($sql);
  163. if ($resql)
  164. {
  165. // Use select2 selector
  166. if (! empty($conf->use_javascript_ajax))
  167. {
  168. include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php';
  169. $comboenhancement = ajax_combobox($htmlid, array(), 0, $forcefocus);
  170. $out.=$comboenhancement;
  171. $morecss.=' minwidth100';
  172. }
  173. if (empty($option_only)) {
  174. $out.= '<select class="flat'.($morecss?' '.$morecss:'').'"'.($disabled?' disabled="disabled"':'').' id="'.$htmlid.'" name="'.$htmlname.'">';
  175. }
  176. if (!empty($show_empty)) {
  177. $out.= '<option value="0">&nbsp;</option>';
  178. }
  179. $num = $this->db->num_rows($resql);
  180. $i = 0;
  181. if ($num)
  182. {
  183. while ($i < $num)
  184. {
  185. $obj = $this->db->fetch_object($resql);
  186. // If we ask to filter on a company and user has no permission to see all companies and project is linked to another company, we hide project.
  187. if ($socid > 0 && (empty($obj->fk_soc) || $obj->fk_soc == $socid) && ! $user->rights->societe->lire)
  188. {
  189. // Do nothing
  190. }
  191. else
  192. {
  193. if ($discard_closed == 1 && $obj->fk_statut == 2 && $obj->rowid != $selected) // We discard closed except if selected
  194. {
  195. $i++;
  196. continue;
  197. }
  198. $labeltoshow=dol_trunc($obj->ref,18);
  199. //if ($obj->public) $labeltoshow.=' ('.$langs->trans("SharedProject").')';
  200. //else $labeltoshow.=' ('.$langs->trans("Private").')';
  201. $labeltoshow.=', '.dol_trunc($obj->title, $maxlength);
  202. if ($obj->name)
  203. {
  204. $labeltoshow.=' - '.$obj->name;
  205. if ($obj->name_alias) $labeltoshow.=' ('.$obj->name_alias.')';
  206. }
  207. $disabled=0;
  208. if ($obj->fk_statut == 0)
  209. {
  210. $disabled=1;
  211. $labeltoshow.=' - '.$langs->trans("Draft");
  212. }
  213. else if ($obj->fk_statut == 2)
  214. {
  215. if ($discard_close == 2) $disabled=1;
  216. $labeltoshow.=' - '.$langs->trans("Closed");
  217. }
  218. else if ( empty($conf->global->PROJECT_ALLOW_TO_LINK_FROM_OTHER_COMPANY) && $socid > 0 && (! empty($obj->fk_soc) && $obj->fk_soc != $socid))
  219. {
  220. $disabled=1;
  221. $labeltoshow.=' - '.$langs->trans("LinkedToAnotherCompany");
  222. }
  223. if (!empty($selected) && $selected == $obj->rowid)
  224. {
  225. $out.= '<option value="'.$obj->rowid.'" selected';
  226. //if ($disabled) $out.=' disabled'; // with select2, field can't be preselected if disabled
  227. $out.= '>'.$labeltoshow.'</option>';
  228. }
  229. else
  230. {
  231. if ($hideunselectables && $disabled && ($selected != $obj->rowid))
  232. {
  233. $resultat='';
  234. }
  235. else
  236. {
  237. $resultat='<option value="'.$obj->rowid.'"';
  238. if ($disabled) $resultat.=' disabled';
  239. //if ($obj->public) $labeltoshow.=' ('.$langs->trans("Public").')';
  240. //else $labeltoshow.=' ('.$langs->trans("Private").')';
  241. $resultat.='>';
  242. $resultat.=$labeltoshow;
  243. $resultat.='</option>';
  244. }
  245. $out.= $resultat;
  246. $outarray[] = array(
  247. 'key' => (int) $obj->rowid,
  248. 'value' => $obj->ref,
  249. 'ref' => $obj->ref,
  250. 'label' => $labeltoshow,
  251. 'disabled' => (bool) $disabled
  252. );
  253. }
  254. }
  255. $i++;
  256. }
  257. }
  258. $this->db->free($resql);
  259. if (!$mode) {
  260. if (empty($option_only)) $out.= '</select>';
  261. if (empty($nooutput))
  262. {
  263. print $out;
  264. return '';
  265. }
  266. else return $out;
  267. } else {
  268. return $outarray;
  269. }
  270. }
  271. else
  272. {
  273. dol_print_error($this->db);
  274. return -1;
  275. }
  276. }
  277. /**
  278. * Output a combo list with tasks qualified for a third party
  279. *
  280. * @param int $socid Id third party (-1=all, 0=only projects not linked to a third party, id=projects not linked or linked to third party id)
  281. * @param int $selected Id task preselected
  282. * @param string $htmlname Name of HTML select
  283. * @param int $maxlength Maximum length of label
  284. * @param int $option_only Return only html options lines without the select tag
  285. * @param string $show_empty Add an empty line ('1' or string to show for empty line)
  286. * @param int $discard_closed Discard closed projects (0=Keep,1=hide completely,2=Disable)
  287. * @param int $forcefocus Force focus on field (works with javascript only)
  288. * @param int $disabled Disabled
  289. * @param string $morecss More css added to the select component
  290. * @param string $projectsListId ''=Automatic filter on project allowed. List of id=Filter on project ids.
  291. * @param string $showproject 'all' = Show project info, ''=Hide project info
  292. * @return int Nbr of project if OK, <0 if KO
  293. */
  294. function selectTasks($socid=-1, $selected='', $htmlname='taskid', $maxlength=24, $option_only=0, $show_empty='1', $discard_closed=0, $forcefocus=0, $disabled=0, $morecss='maxwidth500', $projectsListId='', $showproject='all')
  295. {
  296. global $user,$conf,$langs;
  297. require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  298. $out='';
  299. $hideunselectables = false;
  300. if (! empty($conf->global->CONTRACT_HIDE_UNSELECTABLES)) $hideunselectables = true;
  301. if (empty($projectsListId))
  302. {
  303. if (empty($user->rights->projet->all->lire))
  304. {
  305. $projectstatic=new Project($this->db);
  306. $projectsListId = $projectstatic->getProjectsAuthorizedForUser($user,0,1);
  307. }
  308. }
  309. // Search all projects
  310. $sql = 'SELECT t.rowid, t.ref as tref, t.label as tlabel, p.ref, p.title, p.fk_soc, p.fk_statut, p.public,';
  311. $sql.= ' s.nom as name';
  312. $sql.= ' FROM '.MAIN_DB_PREFIX .'projet as p';
  313. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe as s ON s.rowid = p.fk_soc';
  314. $sql.= ', '.MAIN_DB_PREFIX.'projet_task as t';
  315. $sql.= " WHERE p.entity IN (".getEntity('project').")";
  316. $sql.= " AND t.fk_projet = p.rowid";
  317. if ($projectsListId) $sql.= " AND p.rowid IN (".$projectsListId.")";
  318. if ($socid == 0) $sql.= " AND (p.fk_soc=0 OR p.fk_soc IS NULL)";
  319. if ($socid > 0) $sql.= " AND (p.fk_soc=".$socid." OR p.fk_soc IS NULL)";
  320. $sql.= " ORDER BY p.ref, t.ref ASC";
  321. $resql=$this->db->query($sql);
  322. if ($resql)
  323. {
  324. // Use select2 selector
  325. if (! empty($conf->use_javascript_ajax))
  326. {
  327. include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php';
  328. $comboenhancement = ajax_combobox($htmlname, '', 0, $forcefocus);
  329. $out.=$comboenhancement;
  330. $morecss='minwidth200 maxwidth500';
  331. }
  332. if (empty($option_only)) {
  333. $out.= '<select class="valignmiddle flat'.($morecss?' '.$morecss:'').'"'.($disabled?' disabled="disabled"':'').' id="'.$htmlname.'" name="'.$htmlname.'">';
  334. }
  335. if (! empty($show_empty)) {
  336. $out.= '<option value="0" class="optiongrey">';
  337. if (! is_numeric($show_empty)) $out.=$show_empty;
  338. else $out.='&nbsp;';
  339. $out.= '</option>';
  340. }
  341. $num = $this->db->num_rows($resql);
  342. $i = 0;
  343. if ($num)
  344. {
  345. while ($i < $num)
  346. {
  347. $obj = $this->db->fetch_object($resql);
  348. // If we ask to filter on a company and user has no permission to see all companies and project is linked to another company, we hide project.
  349. if ($socid > 0 && (empty($obj->fk_soc) || $obj->fk_soc == $socid) && empty($user->rights->societe->lire))
  350. {
  351. // Do nothing
  352. }
  353. else
  354. {
  355. if ($discard_closed == 1 && $obj->fk_statut == 2)
  356. {
  357. $i++;
  358. continue;
  359. }
  360. $labeltoshow = '';
  361. if ($showproject == 'all')
  362. {
  363. $labeltoshow.=dol_trunc($obj->ref,18); // Project ref
  364. //if ($obj->public) $labeltoshow.=' ('.$langs->trans("SharedProject").')';
  365. //else $labeltoshow.=' ('.$langs->trans("Private").')';
  366. $labeltoshow.=' '.dol_trunc($obj->title,$maxlength);
  367. if ($obj->name) $labeltoshow.=' ('.$obj->name.')';
  368. $disabled=0;
  369. if ($obj->fk_statut == 0)
  370. {
  371. $disabled=1;
  372. $labeltoshow.=' - '.$langs->trans("Draft");
  373. }
  374. else if ($obj->fk_statut == 2)
  375. {
  376. if ($discard_closed == 2) $disabled=1;
  377. $labeltoshow.=' - '.$langs->trans("Closed");
  378. }
  379. else if ($socid > 0 && (! empty($obj->fk_soc) && $obj->fk_soc != $socid))
  380. {
  381. $disabled=1;
  382. $labeltoshow.=' - '.$langs->trans("LinkedToAnotherCompany");
  383. }
  384. $labeltoshow.=' - ';
  385. }
  386. // Label for task
  387. $labeltoshow.=$obj->tref.' '.dol_trunc($obj->tlabel,$maxlength);
  388. if (!empty($selected) && $selected == $obj->rowid)
  389. {
  390. $out.= '<option value="'.$obj->rowid.'" selected';
  391. //if ($disabled) $out.=' disabled'; // with select2, field can't be preselected if disabled
  392. $out.= '>'.$labeltoshow.'</option>';
  393. }
  394. else
  395. {
  396. if ($hideunselectables && $disabled && ($selected != $obj->rowid))
  397. {
  398. $resultat='';
  399. }
  400. else
  401. {
  402. $resultat='<option value="'.$obj->rowid.'"';
  403. if ($disabled) $resultat.=' disabled';
  404. //if ($obj->public) $labeltoshow.=' ('.$langs->trans("Public").')';
  405. //else $labeltoshow.=' ('.$langs->trans("Private").')';
  406. $resultat.='>';
  407. $resultat.=$labeltoshow;
  408. $resultat.='</option>';
  409. }
  410. $out.= $resultat;
  411. }
  412. }
  413. $i++;
  414. }
  415. }
  416. if (empty($option_only)) {
  417. $out.= '</select>';
  418. }
  419. print $out;
  420. $this->db->free($resql);
  421. return $num;
  422. }
  423. else
  424. {
  425. dol_print_error($this->db);
  426. return -1;
  427. }
  428. }
  429. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  430. /**
  431. * Build a HTML select list of element of same thirdparty to suggest to link them to project
  432. *
  433. * @param string $table_element Table of the element to update
  434. * @param string $socid If of thirdparty to use as filter or 'id1,id2,...'
  435. * @param string $morecss More CSS
  436. * @param int $limitonstatus Add filters to limit length of list to opened status (for example to avoid ERR_RESPONSE_HEADERS_TOO_BIG on project/element.php page). TODO To implement
  437. * @param string $projectkey Equivalent key to fk_projet for actual table_element
  438. * @return int|string The HTML select list of element or '' if nothing or -1 if KO
  439. */
  440. function select_element($table_element, $socid=0, $morecss='', $limitonstatus=-2,$projectkey="fk_projet")
  441. {
  442. // phpcs:enable
  443. global $conf, $langs;
  444. if ($table_element == 'projet_task') return ''; // Special cas of element we never link to a project (already always done)
  445. $linkedtothirdparty=false;
  446. if (! in_array($table_element, array('don','expensereport_det','expensereport','loan','stock_mouvement','chargesociales'))) $linkedtothirdparty=true;
  447. $sqlfilter='';
  448. //print $table_element;
  449. switch ($table_element)
  450. {
  451. case "loan":
  452. $sql = "SELECT t.rowid, t.label as ref";
  453. break;
  454. case "facture":
  455. $sql = "SELECT t.rowid, t.facnumber as ref";
  456. break;
  457. case "facture_fourn":
  458. $sql = "SELECT t.rowid, t.ref, t.ref_supplier";
  459. break;
  460. case "commande_fourn":
  461. case "commande_fournisseur":
  462. $sql = "SELECT t.rowid, t.ref, t.ref_supplier";
  463. break;
  464. case "facture_rec":
  465. $sql = "SELECT t.rowid, t.titre as ref";
  466. break;
  467. case "actioncomm":
  468. $sql = "SELECT t.id as rowid, t.label as ref";
  469. $projectkey="fk_project";
  470. break;
  471. case "expensereport":
  472. return '';
  473. case "expensereport_det":
  474. /*$sql = "SELECT rowid, '' as ref"; // table is llx_expensereport_det
  475. $projectkey="fk_projet";
  476. break;*/
  477. return '';
  478. case "commande":
  479. case "contrat":
  480. case "fichinter":
  481. $sql = "SELECT t.rowid, t.ref";
  482. break;
  483. case 'stock_mouvement':
  484. $sql = 'SELECT t.rowid, t.label as ref';
  485. $projectkey='fk_origin';
  486. break;
  487. case "payment_various":
  488. $sql = "SELECT t.rowid, t.num_payment as ref";
  489. break;
  490. case "chargesociales":
  491. default:
  492. $sql = "SELECT t.rowid, t.ref";
  493. break;
  494. }
  495. if ($linkedtothirdparty) $sql.=", s.nom as name";
  496. $sql.= " FROM ".MAIN_DB_PREFIX.$table_element." as t";
  497. if ($linkedtothirdparty) $sql.=", ".MAIN_DB_PREFIX."societe as s";
  498. $sql.= " WHERE ".$projectkey." is null";
  499. if (! empty($socid) && $linkedtothirdparty)
  500. {
  501. if (is_numeric($socid)) $sql.= " AND t.fk_soc=".$socid;
  502. else $sql.= " AND t.fk_soc IN (".$socid.")";
  503. }
  504. if (! in_array($table_element, array('expensereport_det','stock_mouvement'))) $sql.= ' AND t.entity IN ('.getEntity('project').')';
  505. if ($linkedtothirdparty) $sql.=" AND s.rowid = t.fk_soc";
  506. if ($sqlfilter) $sql.= " AND ".$sqlfilter;
  507. $sql.= " ORDER BY ref DESC";
  508. dol_syslog(get_class($this).'::select_element', LOG_DEBUG);
  509. $resql=$this->db->query($sql);
  510. if ($resql)
  511. {
  512. $num = $this->db->num_rows($resql);
  513. $i = 0;
  514. if ($num > 0)
  515. {
  516. $sellist = '<select class="flat elementselect css'.$table_element.($morecss?' '.$morecss:'').'" name="elementselect">';
  517. $sellist .='<option value="-1"></option>';
  518. while ($i < $num)
  519. {
  520. $obj = $this->db->fetch_object($resql);
  521. $ref=$obj->ref?$obj->ref:$obj->rowid;
  522. if (! empty($obj->ref_supplier)) $ref.=' ('.$obj->ref_supplier.')';
  523. if (! empty($obj->name)) $ref.=' - '.$obj->name;
  524. $sellist .='<option value="'.$obj->rowid.'">'.$ref.'</option>';
  525. $i++;
  526. }
  527. $sellist .='</select>';
  528. }
  529. /*else
  530. {
  531. $sellist = '<select class="flat" name="elementselect">';
  532. $sellist.= '<option value="0" disabled>'.$langs->trans("None").'</option>';
  533. $sellist.= '</select>';
  534. }*/
  535. $this->db->free($resql);
  536. return $sellist;
  537. }
  538. else
  539. {
  540. dol_print_error($this->db);
  541. $this->error=$this->db->lasterror();
  542. $this->errors[]=$this->db->lasterror();
  543. dol_syslog(get_class($this) . "::select_element " . $this->error, LOG_ERR);
  544. return -1;
  545. }
  546. }
  547. /**
  548. * Build a HTML select list of element of same thirdparty to suggest to link them to project
  549. *
  550. * @param string $htmlname HTML name
  551. * @param string $preselected Preselected (int or 'all' or 'none')
  552. * @param int $showempty Add an empty line
  553. * @param int $useshortlabel Use short label
  554. * @param int $showallnone Add choice "All" and "None"
  555. * @param int $showpercent Show default probability for status
  556. * @param string $morecss Add more css
  557. * @return int|string The HTML select list of element or '' if nothing or -1 if KO
  558. */
  559. function selectOpportunityStatus($htmlname, $preselected='-1', $showempty=1, $useshortlabel=0, $showallnone=0, $showpercent=0, $morecss='')
  560. {
  561. global $conf, $langs;
  562. $sql = "SELECT rowid, code, label, percent";
  563. $sql.= " FROM ".MAIN_DB_PREFIX.'c_lead_status';
  564. $sql.= " WHERE active = 1";
  565. $sql.= " ORDER BY position";
  566. $resql=$this->db->query($sql);
  567. if ($resql)
  568. {
  569. $num = $this->db->num_rows($resql);
  570. $i = 0;
  571. if ($num > 0)
  572. {
  573. $sellist = '<select class="flat oppstatus'.($morecss?' '.$morecss:'').'" id="'.$htmlname.'" name="'.$htmlname.'">';
  574. if ($showempty) $sellist.= '<option value="-1">&nbsp;</option>'; // Without &nbsp, strange move of screen when switching value
  575. if ($showallnone) $sellist.= '<option value="all"'.($preselected == 'all'?' selected="selected"':'').'>--'.$langs->trans("OnlyOpportunitiesShort").'--</option>';
  576. if ($showallnone) $sellist.= '<option value="openedopp"'.($preselected == 'openedopp'?' selected="selected"':'').'>--'.$langs->trans("OpenedOpportunitiesShort").'--</option>';
  577. if ($showallnone) $sellist.= '<option value="none"'.($preselected == 'none'?' selected="selected"':'').'>--'.$langs->trans("NotAnOpportunityShort").'--</option>';
  578. while ($i < $num)
  579. {
  580. $obj = $this->db->fetch_object($resql);
  581. $sellist .='<option value="'.$obj->rowid.'" defaultpercent="'.$obj->percent.'" elemcode="'.$obj->code.'"';
  582. if ($obj->rowid == $preselected) $sellist .= ' selected="selected"';
  583. $sellist .= '>';
  584. if ($useshortlabel)
  585. {
  586. $finallabel = ($langs->transnoentitiesnoconv("OppStatusShort".$obj->code) != "OppStatusShort".$obj->code ? $langs->transnoentitiesnoconv("OppStatusShort".$obj->code) : $obj->label);
  587. }
  588. else
  589. {
  590. $finallabel = ($langs->transnoentitiesnoconv("OppStatus".$obj->code) != "OppStatus".$obj->code ? $langs->transnoentitiesnoconv("OppStatus".$obj->code) : $obj->label);
  591. if ($showpercent) $finallabel.= ' ('.$obj->percent.'%)';
  592. }
  593. $sellist .= $finallabel;
  594. $sellist .='</option>';
  595. $i++;
  596. }
  597. $sellist .='</select>';
  598. }
  599. /*else
  600. {
  601. $sellist = '<select class="flat" name="elementselect">';
  602. $sellist.= '<option value="0" disabled>'.$langs->trans("None").'</option>';
  603. $sellist.= '</select>';
  604. }*/
  605. $this->db->free($resql);
  606. return $sellist;
  607. }
  608. else
  609. {
  610. $this->error=$this->db->lasterror();
  611. $this->errors[]=$this->db->lasterror();
  612. dol_syslog(get_class($this) . "::selectOpportunityStatus " . $this->error, LOG_ERR);
  613. return -1;
  614. }
  615. }
  616. }