html.formprojet.class.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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_closed == 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. * @param User $usertofilter User object to use for filtering
  293. * @return int Nbr of project if OK, <0 if KO
  294. */
  295. 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', $usertofilter=null)
  296. {
  297. global $user,$conf,$langs;
  298. require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  299. if (is_null($usertofilter))
  300. {
  301. $usertofilter = $user;
  302. }
  303. $out='';
  304. $hideunselectables = false;
  305. if (! empty($conf->global->PROJECT_HIDE_UNSELECTABLES)) $hideunselectables = true;
  306. if (empty($projectsListId))
  307. {
  308. if (empty($usertofilter->rights->projet->all->lire))
  309. {
  310. $projectstatic=new Project($this->db);
  311. $projectsListId = $projectstatic->getProjectsAuthorizedForUser($usertofilter,0,1);
  312. }
  313. }
  314. // Search all projects
  315. $sql = 'SELECT t.rowid, t.ref as tref, t.label as tlabel, p.rowid as pid, p.ref, p.title, p.fk_soc, p.fk_statut, p.public,';
  316. $sql.= ' s.nom as name';
  317. $sql.= ' FROM '.MAIN_DB_PREFIX .'projet as p';
  318. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe as s ON s.rowid = p.fk_soc,';
  319. $sql.= ' '.MAIN_DB_PREFIX.'projet_task as t';
  320. $sql.= " WHERE p.entity IN (".getEntity('project').")";
  321. $sql.= " AND t.fk_projet = p.rowid";
  322. if ($projectsListId) $sql.= " AND p.rowid IN (".$projectsListId.")";
  323. if ($socid == 0) $sql.= " AND (p.fk_soc=0 OR p.fk_soc IS NULL)";
  324. if ($socid > 0) $sql.= " AND (p.fk_soc=".$socid." OR p.fk_soc IS NULL)";
  325. $sql.= " ORDER BY p.ref, t.ref ASC";
  326. $resql=$this->db->query($sql);
  327. if ($resql)
  328. {
  329. // Use select2 selector
  330. if (! empty($conf->use_javascript_ajax))
  331. {
  332. include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php';
  333. $comboenhancement = ajax_combobox($htmlname, '', 0, $forcefocus);
  334. $out.=$comboenhancement;
  335. $morecss='minwidth200 maxwidth500';
  336. }
  337. if (empty($option_only)) {
  338. $out.= '<select class="valignmiddle flat'.($morecss?' '.$morecss:'').'"'.($disabled?' disabled="disabled"':'').' id="'.$htmlname.'" name="'.$htmlname.'">';
  339. }
  340. if (! empty($show_empty)) {
  341. $out.= '<option value="0" class="optiongrey">';
  342. if (! is_numeric($show_empty)) $out.=$show_empty;
  343. else $out.='&nbsp;';
  344. $out.= '</option>';
  345. }
  346. $num = $this->db->num_rows($resql);
  347. $i = 0;
  348. if ($num)
  349. {
  350. while ($i < $num)
  351. {
  352. $obj = $this->db->fetch_object($resql);
  353. // 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.
  354. if ($socid > 0 && (empty($obj->fk_soc) || $obj->fk_soc == $socid) && empty($usertofilter->rights->societe->lire))
  355. {
  356. // Do nothing
  357. }
  358. else
  359. {
  360. if ($discard_closed == 1 && $obj->fk_statut == Project::STATUS_CLOSED)
  361. {
  362. $i++;
  363. continue;
  364. }
  365. $labeltoshow = '';
  366. if ($showproject == 'all')
  367. {
  368. $labeltoshow.=dol_trunc($obj->ref,18); // Project ref
  369. //if ($obj->public) $labeltoshow.=' ('.$langs->trans("SharedProject").')';
  370. //else $labeltoshow.=' ('.$langs->trans("Private").')';
  371. $labeltoshow.=' '.dol_trunc($obj->title,$maxlength);
  372. if ($obj->name) $labeltoshow.=' ('.$obj->name.')';
  373. $disabled=0;
  374. if ($obj->fk_statut == Project::STATUS_DRAFT)
  375. {
  376. $disabled=1;
  377. $labeltoshow.=' - '.$langs->trans("Draft");
  378. }
  379. else if ($obj->fk_statut == Project::STATUS_CLOSED)
  380. {
  381. if ($discard_closed == 2) $disabled=1;
  382. $labeltoshow.=' - '.$langs->trans("Closed");
  383. }
  384. else if ($socid > 0 && (! empty($obj->fk_soc) && $obj->fk_soc != $socid))
  385. {
  386. $disabled=1;
  387. $labeltoshow.=' - '.$langs->trans("LinkedToAnotherCompany");
  388. }
  389. $labeltoshow.=' - ';
  390. }
  391. // Label for task
  392. $labeltoshow.=$obj->tref.' '.dol_trunc($obj->tlabel,$maxlength);
  393. if (!empty($selected) && $selected == $obj->rowid)
  394. {
  395. $out.= '<option value="'.$obj->rowid.'" selected';
  396. //if ($disabled) $out.=' disabled'; // with select2, field can't be preselected if disabled
  397. $out.= '>'.$labeltoshow.'</option>';
  398. }
  399. else
  400. {
  401. if ($hideunselectables && $disabled && ($selected != $obj->rowid))
  402. {
  403. $resultat='';
  404. }
  405. else
  406. {
  407. $resultat='<option value="'.$obj->rowid.'"';
  408. if ($disabled) $resultat.=' disabled';
  409. //if ($obj->public) $labeltoshow.=' ('.$langs->trans("Public").')';
  410. //else $labeltoshow.=' ('.$langs->trans("Private").')';
  411. $resultat.='>';
  412. $resultat.=$labeltoshow;
  413. $resultat.='</option>';
  414. }
  415. $out.= $resultat;
  416. }
  417. }
  418. $i++;
  419. }
  420. }
  421. if (empty($option_only)) {
  422. $out.= '</select>';
  423. }
  424. print $out;
  425. $this->db->free($resql);
  426. return $num;
  427. }
  428. else
  429. {
  430. dol_print_error($this->db);
  431. return -1;
  432. }
  433. }
  434. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  435. /**
  436. * Build a HTML select list of element of same thirdparty to suggest to link them to project
  437. *
  438. * @param string $table_element Table of the element to update
  439. * @param string $socid If of thirdparty to use as filter or 'id1,id2,...'
  440. * @param string $morecss More CSS
  441. * @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
  442. * @param string $projectkey Equivalent key to fk_projet for actual table_element
  443. * @return int|string The HTML select list of element or '' if nothing or -1 if KO
  444. */
  445. function select_element($table_element, $socid=0, $morecss='', $limitonstatus=-2,$projectkey="fk_projet")
  446. {
  447. // phpcs:enable
  448. global $conf, $langs;
  449. if ($table_element == 'projet_task') return ''; // Special cas of element we never link to a project (already always done)
  450. $linkedtothirdparty=false;
  451. if (! in_array($table_element, array('don','expensereport_det','expensereport','loan','stock_mouvement','payment_salary','payment_various','chargesociales'))) $linkedtothirdparty=true;
  452. $sqlfilter='';
  453. //print $table_element;
  454. switch ($table_element)
  455. {
  456. case "loan":
  457. $sql = "SELECT t.rowid, t.label as ref";
  458. break;
  459. case "facture":
  460. $sql = "SELECT t.rowid, t.facnumber as ref";
  461. break;
  462. case "facture_fourn":
  463. $sql = "SELECT t.rowid, t.ref, t.ref_supplier";
  464. break;
  465. case "commande_fourn":
  466. case "commande_fournisseur":
  467. $sql = "SELECT t.rowid, t.ref, t.ref_supplier";
  468. break;
  469. case "facture_rec":
  470. $sql = "SELECT t.rowid, t.titre as ref";
  471. break;
  472. case "actioncomm":
  473. $sql = "SELECT t.id as rowid, t.label as ref";
  474. $projectkey="fk_project";
  475. break;
  476. case "expensereport":
  477. return '';
  478. case "expensereport_det":
  479. /*$sql = "SELECT rowid, '' as ref"; // table is llx_expensereport_det
  480. $projectkey="fk_projet";
  481. break;*/
  482. return '';
  483. case "commande":
  484. case "contrat":
  485. case "fichinter":
  486. $sql = "SELECT t.rowid, t.ref";
  487. break;
  488. case 'stock_mouvement':
  489. $sql = 'SELECT t.rowid, t.label as ref';
  490. $projectkey='fk_origin';
  491. break;
  492. case "payment_salary":
  493. $sql = "SELECT t.rowid, t.num_payment as ref"; // TODO In a future fill and use real ref field
  494. break;
  495. case "payment_various":
  496. $sql = "SELECT t.rowid, t.num_payment as ref";
  497. break;
  498. case "chargesociales":
  499. default:
  500. $sql = "SELECT t.rowid, t.ref";
  501. break;
  502. }
  503. if ($linkedtothirdparty) $sql.=", s.nom as name";
  504. $sql.= " FROM ".MAIN_DB_PREFIX.$table_element." as t";
  505. if ($linkedtothirdparty) $sql.=", ".MAIN_DB_PREFIX."societe as s";
  506. $sql.= " WHERE ".$projectkey." is null";
  507. if (! empty($socid) && $linkedtothirdparty)
  508. {
  509. if (is_numeric($socid)) $sql.= " AND t.fk_soc=".$socid;
  510. else $sql.= " AND t.fk_soc IN (".$socid.")";
  511. }
  512. if (! in_array($table_element, array('expensereport_det','stock_mouvement'))) $sql.= ' AND t.entity IN ('.getEntity('project').')';
  513. if ($linkedtothirdparty) $sql.=" AND s.rowid = t.fk_soc";
  514. if ($sqlfilter) $sql.= " AND ".$sqlfilter;
  515. $sql.= " ORDER BY ref DESC";
  516. dol_syslog(get_class($this).'::select_element', LOG_DEBUG);
  517. $resql=$this->db->query($sql);
  518. if ($resql)
  519. {
  520. $num = $this->db->num_rows($resql);
  521. $i = 0;
  522. if ($num > 0)
  523. {
  524. $sellist = '<select class="flat elementselect css'.$table_element.($morecss?' '.$morecss:'').'" name="elementselect">';
  525. $sellist .='<option value="-1"></option>';
  526. while ($i < $num)
  527. {
  528. $obj = $this->db->fetch_object($resql);
  529. $ref=$obj->ref?$obj->ref:$obj->rowid;
  530. if (! empty($obj->ref_supplier)) $ref.=' ('.$obj->ref_supplier.')';
  531. if (! empty($obj->name)) $ref.=' - '.$obj->name;
  532. $sellist .='<option value="'.$obj->rowid.'">'.$ref.'</option>';
  533. $i++;
  534. }
  535. $sellist .='</select>';
  536. }
  537. /*else
  538. {
  539. $sellist = '<select class="flat" name="elementselect">';
  540. $sellist.= '<option value="0" disabled>'.$langs->trans("None").'</option>';
  541. $sellist.= '</select>';
  542. }*/
  543. $this->db->free($resql);
  544. return $sellist;
  545. }
  546. else
  547. {
  548. dol_print_error($this->db);
  549. $this->error=$this->db->lasterror();
  550. $this->errors[]=$this->db->lasterror();
  551. dol_syslog(get_class($this) . "::select_element " . $this->error, LOG_ERR);
  552. return -1;
  553. }
  554. }
  555. /**
  556. * Build a HTML select list of element of same thirdparty to suggest to link them to project
  557. *
  558. * @param string $htmlname HTML name
  559. * @param string $preselected Preselected (int or 'all' or 'none')
  560. * @param int $showempty Add an empty line
  561. * @param int $useshortlabel Use short label
  562. * @param int $showallnone Add choice "All" and "None"
  563. * @param int $showpercent Show default probability for status
  564. * @param string $morecss Add more css
  565. * @return int|string The HTML select list of element or '' if nothing or -1 if KO
  566. */
  567. function selectOpportunityStatus($htmlname, $preselected='-1', $showempty=1, $useshortlabel=0, $showallnone=0, $showpercent=0, $morecss='')
  568. {
  569. global $conf, $langs;
  570. $sql = "SELECT rowid, code, label, percent";
  571. $sql.= " FROM ".MAIN_DB_PREFIX.'c_lead_status';
  572. $sql.= " WHERE active = 1";
  573. $sql.= " ORDER BY position";
  574. $resql=$this->db->query($sql);
  575. if ($resql)
  576. {
  577. $num = $this->db->num_rows($resql);
  578. $i = 0;
  579. if ($num > 0)
  580. {
  581. $sellist = '<select class="flat oppstatus'.($morecss?' '.$morecss:'').'" id="'.$htmlname.'" name="'.$htmlname.'">';
  582. if ($showempty) {
  583. // Without &nbsp, strange move of screen when switching value
  584. $sellist.= '<option value="-1">&nbsp;</option>';
  585. }
  586. if ($showallnone) {
  587. $sellist.= '<option value="all"'.($preselected == 'all'?' selected="selected"':'').'>-- '.$langs->trans("OnlyOpportunitiesShort").' --</option>';
  588. $sellist.= '<option value="openedopp"'.($preselected == 'openedopp'?' selected="selected"':'').'>-- '.$langs->trans("OpenedOpportunitiesShort").' --</option>';
  589. $sellist.= '<option value="notopenedopp"'.($preselected == 'notopenedopp'?' selected="selected"':'').'>-- '.$langs->trans("NotOpenedOpportunitiesShort").' --</option>';
  590. $sellist.= '<option value="none"'.($preselected == 'none'?' selected="selected"':'').'>-- '.$langs->trans("NotAnOpportunityShort").' --</option>';
  591. }
  592. while ($i < $num)
  593. {
  594. $obj = $this->db->fetch_object($resql);
  595. $sellist .='<option value="'.$obj->rowid.'" defaultpercent="'.$obj->percent.'" elemcode="'.$obj->code.'"';
  596. if ($obj->rowid == $preselected) $sellist .= ' selected="selected"';
  597. $sellist .= '>';
  598. if ($useshortlabel)
  599. {
  600. $finallabel = ($langs->transnoentitiesnoconv("OppStatus".$obj->code) != "OppStatus".$obj->code ? $langs->transnoentitiesnoconv("OppStatus".$obj->code) : $obj->label);
  601. }
  602. else
  603. {
  604. $finallabel = ($langs->transnoentitiesnoconv("OppStatus".$obj->code) != "OppStatus".$obj->code ? $langs->transnoentitiesnoconv("OppStatus".$obj->code) : $obj->label);
  605. if ($showpercent) $finallabel.= ' ('.$obj->percent.'%)';
  606. }
  607. $sellist .= $finallabel;
  608. $sellist .='</option>';
  609. $i++;
  610. }
  611. $sellist .='</select>';
  612. }
  613. /*else
  614. {
  615. $sellist = '<select class="flat" name="elementselect">';
  616. $sellist.= '<option value="0" disabled>'.$langs->trans("None").'</option>';
  617. $sellist.= '</select>';
  618. }*/
  619. $this->db->free($resql);
  620. return $sellist;
  621. }
  622. else
  623. {
  624. $this->error=$this->db->lasterror();
  625. $this->errors[]=$this->db->lasterror();
  626. dol_syslog(get_class($this) . "::selectOpportunityStatus " . $this->error, LOG_ERR);
  627. return -1;
  628. }
  629. }
  630. }