dolibarr  21.0.0-alpha
html.formprojet.class.php
Go to the documentation of this file.
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  * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
6  * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
7  * Copyright (C) 2024 Benjamin Falière <benjamin.faliere@altairis.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <https://www.gnu.org/licenses/>.
21  */
22 
29 require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
30 
34 class FormProjets extends Form
35 {
39  public $db;
40 
44  public $error = '';
45 
46  public $errors = array();
47 
48 
49  public $nboftasks;
50 
51 
57  public function __construct($db)
58  {
59  $this->db = $db;
60  }
61 
62  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
63 
85  public 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 = '', $morefilter = '')
86  {
87  // phpcs:enable
88  global $langs, $conf, $form;
89 
90  $selected_input_value = '';
91  if (is_object($selected)) {
92  $selected_input_value = $selected->ref;
93  $selected = $selected->id;
94  }
95 
96  $out = '';
97 
98  if (!empty($conf->use_javascript_ajax) && getDolGlobalString('PROJECT_USE_SEARCH_TO_SELECT')) {
99  $placeholder = '';
100 
101  if ($selected && empty($selected_input_value)) {
102  require_once DOL_DOCUMENT_ROOT . '/projet/class/project.class.php';
103  $project = new Project($this->db);
104  $project->fetch($selected);
105  $selected_input_value = $project->ref;
106  }
107  $urloption = 'socid=' . ((int) $socid) . '&htmlname=' . urlencode($htmlname) . '&discardclosed=' . ((int) $discard_closed);
108  if ($morefilter == 'usage_organize_event=1') {
109  $urloption .= '&usage_organize_event=1';
110  }
111  $out .= '<input type="text" class="minwidth200' . ($morecss ? ' ' . $morecss : '') . '" name="search_' . $htmlname . '" id="search_' . $htmlname . '" value="' . $selected_input_value . '"' . $placeholder . ' />';
112 
113  $out .= ajax_autocompleter($selected, $htmlname, DOL_URL_ROOT . '/projet/ajax/projects.php', $urloption, $conf->global->PROJECT_USE_SEARCH_TO_SELECT, 0, array());
114  } else {
115  $out .= $this->select_projects_list($socid, $selected, $htmlname, $maxlength, $option_only, $show_empty, abs($discard_closed), $forcefocus, $disabled, 0, $filterkey, 1, $forceaddid, $htmlid, $morecss, $morefilter);
116  }
117  if ($discard_closed > 0) {
118  if (!empty($form)) {
119  $out .= $form->textwithpicto('', $langs->trans("ClosedProjectsAreHidden"));
120  }
121  }
122 
123  if (empty($nooutput)) {
124  print $out;
125  return '';
126  } else {
127  return $out;
128  }
129  }
130 
131  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
132 
154  public function select_projects_list($socid = -1, $selected = 0, $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', $morefilter = '')
155  {
156  // phpcs:enable
157  global $user, $conf, $langs;
158 
159  require_once DOL_DOCUMENT_ROOT . '/projet/class/project.class.php';
160 
161  if (empty($htmlid)) {
162  $htmlid = $htmlname;
163  }
164 
165  $out = '';
166  $outarray = array();
167 
168  $hideunselectables = false;
169  if (getDolGlobalString('PROJECT_HIDE_UNSELECTABLES')) {
170  $hideunselectables = true;
171  }
172  if (getDolGlobalInt('PROJECT_ALWAYS_DISCARD_CLOSED_PROJECTS_IN_SELECT')) {
173  $discard_closed = 1;
174  }
175 
176  $projectsListId = false;
177  if (!$user->hasRight('projet', 'all', 'lire')) {
178  $projectstatic = new Project($this->db);
179  $projectsListId = $projectstatic->getProjectsAuthorizedForUser($user, 0, 1);
180  }
181 
182  // Search all projects
183  $sql = "SELECT p.rowid, p.ref, p.title, p.fk_soc, p.fk_statut, p.public, s.nom as name, s.name_alias";
184  $sql .= " FROM " . $this->db->prefix() . "projet as p LEFT JOIN " . $this->db->prefix() . "societe as s ON s.rowid = p.fk_soc";
185  $sql .= " WHERE p.entity IN (" . getEntity('project') . ")";
186  if ($projectsListId !== false) {
187  $sql .= " AND p.rowid IN (" . $this->db->sanitize($projectsListId) . ")";
188  }
189  if ($socid == 0) {
190  $sql .= " AND (p.fk_soc=0 OR p.fk_soc IS NULL)";
191  }
192  if ($socid > 0) {
193  if (!getDolGlobalString('PROJECT_ALLOW_TO_LINK_FROM_OTHER_COMPANY')) {
194  $sql .= " AND (p.fk_soc=" . ((int) $socid) . " OR p.fk_soc IS NULL)";
195  } elseif (getDolGlobalString('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.
196  $sql .= " AND (p.fk_soc IN (" . $this->db->sanitize(((int) $socid) . ", " . getDolGlobalString('PROJECT_ALLOW_TO_LINK_FROM_OTHER_COMPANY')) . ") OR p.fk_soc IS NULL)";
197  }
198  }
199  if (!empty($filterkey)) {
200  $sql .= natural_search(array('p.title', 'p.ref'), $filterkey);
201  }
202  if ($morefilter) {
203  $sql .= ' AND (' . $this->db->sanitize($morefilter, 0, 1) . ')';
204  }
205  $sql .= " ORDER BY p.ref ASC";
206 
207  $resql = $this->db->query($sql);
208  if ($resql) {
209  if (!empty($conf->use_javascript_ajax)) {
210  $morecss .= ' minwidth100';
211  }
212  if (empty($option_only)) {
213  $out .= '<select class="flat' . ($morecss ? ' ' . $morecss : '') . '"' . ($disabled ? ' disabled="disabled"' : '') . ' id="' . $htmlid . '" name="' . $htmlname . '">';
214  }
215  if (!empty($show_empty)) {
216  if (is_numeric($show_empty)) {
217  $out .= '<option value="0">&nbsp;</option>';
218  } else {
219  $out .= '<option value="-1">'.$show_empty.'</option>';
220  }
221  }
222  $num = $this->db->num_rows($resql);
223  $i = 0;
224  if ($num) {
225  while ($i < $num) {
226  $obj = $this->db->fetch_object($resql);
227  // 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.
228  if ($socid > 0 && (empty($obj->fk_soc) || $obj->fk_soc == $socid) && !$user->hasRight('societe', 'lire')) {
229  // Do nothing
230  } else {
231  if ($discard_closed == 1 && $obj->fk_statut == 2 && $obj->rowid != $selected) { // We discard closed except if selected
232  $i++;
233  continue;
234  }
235 
236  $labeltoshow = dol_trunc($obj->ref, 18);
237  //if ($obj->public) $labeltoshow.=' ('.$langs->trans("SharedProject").')';
238  //else $labeltoshow.=' ('.$langs->trans("Private").')';
239  $labeltoshow .= ', ' . dol_trunc($obj->title, $maxlength);
240  if ($obj->name) {
241  $labeltoshow .= ' - ' . $obj->name;
242  if ($obj->name_alias) {
243  $labeltoshow .= ' (' . $obj->name_alias . ')';
244  }
245  }
246 
247  $disabled = 0;
248  if ($obj->fk_statut == 0) {
249  $disabled = 1;
250  $labeltoshow .= ' - ' . $langs->trans("Draft");
251  } elseif ($obj->fk_statut == 2) {
252  if ($discard_closed == 2) {
253  $disabled = 1;
254  }
255  $labeltoshow .= ' - ' . $langs->trans("Closed");
256  } elseif (!getDolGlobalString('PROJECT_ALLOW_TO_LINK_FROM_OTHER_COMPANY') && $socid > 0 && (!empty($obj->fk_soc) && $obj->fk_soc != $socid)) {
257  $disabled = 1;
258  $labeltoshow .= ' - ' . $langs->trans("LinkedToAnotherCompany");
259  }
260 
261  if (!empty($selected) && $selected == $obj->rowid) {
262  $out .= '<option value="' . $obj->rowid . '" selected';
263  //if ($disabled) $out.=' disabled'; // with select2, field can't be preselected if disabled
264  $out .= '>' . $labeltoshow . '</option>';
265  } else {
266  if ($hideunselectables && $disabled && ($selected != $obj->rowid)) {
267  $resultat = '';
268  } else {
269  $resultat = '<option value="' . $obj->rowid . '"';
270  if ($disabled) {
271  $resultat .= ' disabled';
272  }
273  //if ($obj->public) $labeltoshow.=' ('.$langs->trans("Public").')';
274  //else $labeltoshow.=' ('.$langs->trans("Private").')';
275  $resultat .= '>';
276  $resultat .= $labeltoshow;
277  $resultat .= '</option>';
278  }
279  $out .= $resultat;
280 
281  $outarray[] = array(
282  'key' => (int) $obj->rowid,
283  'value' => $obj->ref,
284  'ref' => $obj->ref,
285  'labelx' => $labeltoshow,
286  'label' => ($disabled ? '<span class="opacitymedium">' . $labeltoshow . '</span>' : $labeltoshow),
287  'disabled' => (bool) $disabled
288  );
289  }
290  }
291  $i++;
292  }
293  }
294 
295  $this->db->free($resql);
296 
297  if (!$mode) {
298  if (empty($option_only)) {
299  $out .= '</select>';
300  }
301 
302  // Use select2 selector
303  if (!empty($conf->use_javascript_ajax)) {
304  include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php';
305  $comboenhancement = ajax_combobox($htmlid, array(), 0, $forcefocus);
306  $out .= $comboenhancement;
307  $morecss .= ' minwidth100';
308  }
309 
310  if (empty($nooutput)) {
311  print $out;
312  return '';
313  } else {
314  return $out;
315  }
316  } else {
317  return $outarray;
318  }
319  } else {
320  dol_print_error($this->db);
321  return -1;
322  }
323  }
324 
345  public function selectTasks($socid = -1, $selected = 0, $htmlname = 'taskid', $maxlength = 24, $option_only = 0, $show_empty = '1', $discard_closed = 0, $forcefocus = 0, $disabled = 0, $morecss = 'maxwidth500', $projectsListId = '', $showmore = 'all', $usertofilter = null, $nooutput = 0)
346  {
347  global $user, $conf, $langs;
348 
349  require_once DOL_DOCUMENT_ROOT . '/projet/class/project.class.php';
350 
351  if (is_null($usertofilter)) {
352  $usertofilter = $user;
353  }
354 
355  $out = '';
356 
357  $hideunselectables = false;
358  if (getDolGlobalString('PROJECT_HIDE_UNSELECTABLES')) {
359  $hideunselectables = true;
360  }
361 
362  if (empty($projectsListId)) {
363  if (!$usertofilter->hasRight('projet', 'all', 'lire')) {
364  $projectstatic = new Project($this->db);
365  $projectsListId = $projectstatic->getProjectsAuthorizedForUser($usertofilter, 0, 1);
366  }
367  }
368 
369  // Search all projects
370  $sql = "SELECT t.rowid, t.ref as tref, t.label as tlabel, t.progress,";
371  $sql .= " p.rowid as pid, p.ref, p.title, p.fk_soc, p.fk_statut, p.public, p.usage_task,";
372  $sql .= " s.nom as name";
373  $sql .= " FROM " . $this->db->prefix() . "projet as p";
374  $sql .= " LEFT JOIN " . $this->db->prefix() . "societe as s ON s.rowid = p.fk_soc,";
375  $sql .= " " . $this->db->prefix() . "projet_task as t";
376  $sql .= " WHERE p.entity IN (" . getEntity('project') . ")";
377  $sql .= " AND t.fk_projet = p.rowid";
378  if ($projectsListId) {
379  $sql .= " AND p.rowid IN (" . $this->db->sanitize($projectsListId) . ")";
380  }
381  if ($socid == 0) {
382  $sql .= " AND (p.fk_soc=0 OR p.fk_soc IS NULL)";
383  }
384  if ($socid > 0) {
385  $sql .= " AND (p.fk_soc=" . ((int) $socid) . " OR p.fk_soc IS NULL)";
386  }
387  $sql .= " ORDER BY p.ref, t.ref ASC";
388 
389  $resql = $this->db->query($sql);
390  if ($resql) {
391  // Use select2 selector
392  if (empty($option_only) && !empty($conf->use_javascript_ajax)) {
393  include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php';
394  $comboenhancement = ajax_combobox($htmlname, '', 0, $forcefocus);
395  $out .= $comboenhancement;
396  $morecss .= ' minwidth150imp';
397  }
398 
399  if (empty($option_only)) {
400  $out .= '<select class="valignmiddle flat' . ($morecss ? ' ' . $morecss : '') . '"' . ($disabled ? ' disabled="disabled"' : '') . ' id="' . $htmlname . '" name="' . $htmlname . '">';
401  }
402  if (!empty($show_empty)) {
403  $out .= '<option value="0" class="optiongrey">';
404  if (!is_numeric($show_empty)) {
405  //if (!empty($conf->use_javascript_ajax)) $out .= '<span class="opacitymedium">aaa';
406  $out .= $show_empty;
407  //if (!empty($conf->use_javascript_ajax)) $out .= '</span>';
408  } else {
409  $out .= '&nbsp;';
410  }
411  $out .= '</option>';
412  }
413 
414  $num = $this->db->num_rows($resql);
415  $i = 0;
416  if ($num) {
417  while ($i < $num) {
418  $obj = $this->db->fetch_object($resql);
419  // 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.
420  if ($socid > 0 && (empty($obj->fk_soc) || $obj->fk_soc == $socid) && !$usertofilter->hasRight('societe', 'lire')) {
421  // Do nothing
422  } else {
423  if ($discard_closed == 1 && $obj->fk_statut == Project::STATUS_CLOSED) {
424  $i++;
425  continue;
426  }
427 
428  $labeltoshow = '';
429  $labeltoshowhtml = '';
430 
431  $disabled = 0;
432  if ($obj->fk_statut == Project::STATUS_DRAFT) {
433  $disabled = 1;
434  } elseif ($obj->fk_statut == Project::STATUS_CLOSED) {
435  if ($discard_closed == 2) {
436  $disabled = 1;
437  }
438  } elseif ($socid > 0 && (!empty($obj->fk_soc) && $obj->fk_soc != $socid)) {
439  $disabled = 1;
440  }
441 
442  if (preg_match('/all/', $showmore)) {
443  $labeltoshow .= dol_trunc($obj->ref, 18); // Project ref
444  //if ($obj->public) $labeltoshow.=' ('.$langs->trans("SharedProject").')';
445  //else $labeltoshow.=' ('.$langs->trans("Private").')';
446  $labeltoshow .= ' ' . dol_trunc($obj->title, $maxlength);
447  $labeltoshowhtml = $labeltoshow;
448 
449  if ($obj->name) {
450  $labeltoshow .= ' (' . $obj->name . ')';
451  $labeltoshowhtml .= ' <span class="opacitymedium">(' . $obj->name . ')</span>';
452  }
453 
454  $disabled = 0;
455  if ($obj->fk_statut == Project::STATUS_DRAFT) {
456  $disabled = 1;
457  $labeltoshow .= ' - ' . $langs->trans("Draft");
458  $labeltoshowhtml .= ' - <span class="opacitymedium">' . $langs->trans("Draft") . '</span>';
459  } elseif ($obj->fk_statut == Project::STATUS_CLOSED) {
460  if ($discard_closed == 2) {
461  $disabled = 1;
462  }
463  $labeltoshow .= ' - ' . $langs->trans("Closed");
464  $labeltoshowhtml .= ' - <span class="opacitymedium">' . $langs->trans("Closed") . '</span>';
465  } elseif ($socid > 0 && (!empty($obj->fk_soc) && $obj->fk_soc != $socid)) {
466  $disabled = 1;
467  $labeltoshow .= ' - ' . $langs->trans("LinkedToAnotherCompany");
468  $labeltoshowhtml .= ' - <span class="opacitymedium">' . $langs->trans("LinkedToAnotherCompany") . '</span>';
469  }
470  $labeltoshow .= ' - ';
471  $labeltoshowhtml .= ' - ';
472  }
473 
474  // Label for task
475  $labeltoshow .= $obj->tref . ' ' . dol_trunc($obj->tlabel, $maxlength);
476  $labeltoshowhtml .= $obj->tref . ' - ' . dol_trunc($obj->tlabel, $maxlength);
477  if ($obj->usage_task && preg_match('/progress/', $showmore)) {
478  $labeltoshow .= ' <span class="opacitymedium">(' . $obj->progress . '%)</span>';
479  $labeltoshowhtml .= ' <span class="opacitymedium">(' . $obj->progress . '%)</span>';
480  }
481 
482  if (!empty($selected) && $selected == $obj->rowid) {
483  $out .= '<option value="' . $obj->rowid . '" selected';
484  $out .= ' data-html="' . dol_escape_htmltag($labeltoshowhtml) . '"';
485  //if ($disabled) $out.=' disabled'; // with select2, field can't be preselected if disabled
486  $out .= '>' . $labeltoshow . '</option>';
487  } else {
488  if ($hideunselectables && $disabled && ($selected != $obj->rowid)) {
489  $resultat = '';
490  } else {
491  $resultat = '<option value="' . $obj->rowid . '"';
492  if ($disabled) {
493  $resultat .= ' disabled';
494  }
495  //if ($obj->public) $labeltoshow.=' ('.$langs->trans("Public").')';
496  //else $labeltoshow.=' ('.$langs->trans("Private").')';
497  $resultat .= ' data-html="' . dol_escape_htmltag($labeltoshowhtml) . '"';
498  $resultat .= '>';
499  $resultat .= $labeltoshow;
500  $resultat .= '</option>';
501  }
502  $out .= $resultat;
503  }
504  }
505  $i++;
506  }
507  }
508  if (empty($option_only)) {
509  $out .= '</select>';
510  }
511 
512  $this->nboftasks = $num;
513  $this->db->free($resql);
514 
515  // Output or return
516  if (empty($nooutput)) {
517  print $out;
518  } else {
519  return $out;
520  }
521 
522  return $num;
523  } else {
524  dol_print_error($this->db);
525  return -1;
526  }
527  }
528 
529 
530  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
531 
543  public function select_element($table_element, $socid = 0, $morecss = '', $limitonstatus = -2, $projectkey = "fk_projet", $placeholder = '')
544  {
545  // phpcs:enable
546  global $conf, $langs;
547 
548  if ($table_element == 'projet_task') {
549  return ''; // Special cas of element we never link to a project (already always done)
550  }
551 
552  $linkedtothirdparty = false;
553  if (!in_array(
554  $table_element,
555  array(
556  'don',
557  'expensereport_det',
558  'expensereport', 'loan',
559  'stock_mouvement',
560  'payment_salary',
561  'payment_various',
562  'salary',
563  'chargesociales',
564  'entrepot')
565  )) {
566  $linkedtothirdparty = true;
567  }
568 
569  $sqlfilter = '';
570 
571  //print $table_element;
572  switch ($table_element) {
573  case "loan":
574  $sql = "SELECT t.rowid, t.label as ref";
575  break;
576  case "facture":
577  $sql = "SELECT t.rowid, t.ref as ref";
578  break;
579  case "facture_fourn":
580  $sql = "SELECT t.rowid, t.ref, t.ref_supplier";
581  break;
582  case "commande_fourn":
583  case "commande_fournisseur":
584  $sql = "SELECT t.rowid, t.ref, t.ref_supplier";
585  break;
586  case "facture_rec":
587  $sql = "SELECT t.rowid, t.titre as ref";
588  break;
589  case "actioncomm":
590  $sql = "SELECT t.id as rowid, t.label as ref";
591  $projectkey = "fk_project";
592  break;
593  case "expensereport":
594  return '';
595  case "expensereport_det":
596  /*$sql = "SELECT rowid, '' as ref"; // table is llx_expensereport_det
597  $projectkey="fk_projet";
598  break;*/
599  return '';
600  case "commande":
601  case "contrat":
602  case "fichinter":
603  $sql = "SELECT t.rowid, t.ref";
604  break;
605  case 'stock_mouvement':
606  $sql = "SELECT t.rowid, t.label as ref";
607  $projectkey = 'fk_origin';
608  break;
609  case "payment_salary":
610  $sql = "SELECT t.rowid, t.num_payment as ref"; // TODO In a future fill and use real ref field
611  break;
612  case "payment_various":
613  $sql = "SELECT t.rowid, t.num_payment as ref";
614  break;
615  case "chargesociales":
616  default:
617  $sql = "SELECT t.rowid, t.ref";
618  break;
619  }
620  if ($linkedtothirdparty) {
621  $sql .= ", s.nom as name";
622  }
623  $sql .= " FROM " . $this->db->prefix() . $table_element . " as t";
624  if ($linkedtothirdparty) {
625  $sql .= ", " . $this->db->prefix() . "societe as s";
626  }
627  $sql .= " WHERE " . $projectkey . " is null";
628  if (!empty($socid) && $linkedtothirdparty) {
629  if (is_numeric($socid)) {
630  $sql .= " AND t.fk_soc = " . ((int) $socid);
631  } else {
632  $sql .= " AND t.fk_soc IN (" . $this->db->sanitize($socid) . ")";
633  }
634  }
635  if (!in_array($table_element, array('expensereport_det', 'stock_mouvement'))) {
636  $sql .= ' AND t.entity IN (' . getEntity('project') . ')';
637  }
638  if ($linkedtothirdparty) {
639  $sql .= " AND s.rowid = t.fk_soc";
640  }
641  if ($sqlfilter) {
642  $sql .= " AND " . $sqlfilter;
643  }
644  $sql .= " ORDER BY ref DESC";
645 
646  dol_syslog(get_class($this) . '::select_element', LOG_DEBUG);
647  $resql = $this->db->query($sql);
648  if ($resql) {
649  $num = $this->db->num_rows($resql);
650  $i = 0;
651  $sellist = '';
652 
653  if ($num > 0) {
654  $sellist = '<select class="flat elementselect css' . $table_element . ($morecss ? ' ' . $morecss : '') . '" name="elementselect">';
655  $sellist .= '<option value="-1"' . ($placeholder ? ' class="optiongrey"' : '') . '>' . $placeholder . '</option>';
656  while ($i < $num) {
657  $obj = $this->db->fetch_object($resql);
658  $ref = $obj->ref ? $obj->ref : $obj->rowid;
659  if (!empty($obj->ref_supplier)) {
660  $ref .= ' (' . $obj->ref_supplier . ')';
661  }
662  if (!empty($obj->name)) {
663  $ref .= ' - ' . $obj->name;
664  }
665  $sellist .= '<option value="' . $obj->rowid . '">' . $ref . '</option>';
666  $i++;
667  }
668  $sellist .= '</select>';
669  }
670  /*else
671  {
672  $sellist = '<select class="flat" name="elementselect">';
673  $sellist.= '<option value="0" disabled>'.$langs->trans("None").'</option>';
674  $sellist.= '</select>';
675  }*/
676  $this->db->free($resql);
677 
678  return $sellist;
679  } else {
680  dol_print_error($this->db);
681  $this->error = $this->db->lasterror();
682  $this->errors[] = $this->db->lasterror();
683  dol_syslog(get_class($this) . "::select_element " . $this->error, LOG_ERR);
684  return -1;
685  }
686  }
687 
688 
703  public function selectOpportunityStatus($htmlname, $preselected = '-1', $showempty = 1, $useshortlabel = 0, $showallnone = 0, $showpercent = 0, $morecss = '', $noadmininfo = 0, $addcombojs = 0)
704  {
705  global $conf, $langs, $user;
706 
707  $sql = "SELECT rowid, code, label, percent";
708  $sql .= " FROM " . $this->db->prefix() . 'c_lead_status';
709  $sql .= " WHERE active = 1";
710  $sql .= " ORDER BY position";
711 
712  $resql = $this->db->query($sql);
713  if ($resql) {
714  $num = $this->db->num_rows($resql);
715  $i = 0;
716  $sellist = '';
717  if ($num > 0) {
718  $sellist = '<select class="flat oppstatus' . ($morecss ? ' ' . $morecss : '') . '" id="' . $htmlname . '" name="' . $htmlname . '">';
719  if ($showempty) {
720  // Without &nbsp, strange move of screen when switching value
721  $sellist .= '<option value="-1">&nbsp;</option>';
722  }
723  if ($showallnone) {
724  $sellist .= '<option value="all"' . ($preselected == 'all' ? ' selected="selected"' : '') . '>-- ' . $langs->trans("OnlyOpportunitiesShort") . '</option>';
725  $sellist .= '<option value="openedopp"' . ($preselected == 'openedopp' ? ' selected="selected"' : '') . '>-- ' . $langs->trans("OpenedOpportunitiesShort") . '</option>';
726  $sellist .= '<option value="notopenedopp"' . ($preselected == 'notopenedopp' ? ' selected="selected"' : '') . '>-- ' . $langs->trans("NotOpenedOpportunitiesShort") . '</option>';
727  $sellist .= '<option value="none"' . ($preselected == 'none' ? ' selected="selected"' : '') . '>-- ' . $langs->trans("NotAnOpportunityShort") . '</option>';
728  }
729  while ($i < $num) {
730  $obj = $this->db->fetch_object($resql);
731 
732  $sellist .= '<option value="' . $obj->rowid . '" defaultpercent="' . $obj->percent . '" elemcode="' . $obj->code . '"';
733  if ($obj->rowid == $preselected) {
734  $sellist .= ' selected="selected"';
735  }
736  $sellist .= '>';
737  if ($useshortlabel) {
738  $finallabel = ($langs->transnoentitiesnoconv("OppStatus" . $obj->code) != "OppStatus" . $obj->code ? $langs->transnoentitiesnoconv("OppStatus" . $obj->code) : $obj->label);
739  } else {
740  $finallabel = ($langs->transnoentitiesnoconv("OppStatus" . $obj->code) != "OppStatus" . $obj->code ? $langs->transnoentitiesnoconv("OppStatus" . $obj->code) : $obj->label);
741  if ($showpercent) {
742  $finallabel .= ' (' . $obj->percent . '%)';
743  }
744  }
745  $sellist .= $finallabel;
746  $sellist .= '</option>';
747  $i++;
748  }
749  $sellist .= '</select>';
750 
751  if ($user->admin && !$noadmininfo) {
752  $sellist .= info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
753  }
754 
755  if ($addcombojs) {
756  $sellist .= ajax_combobox($htmlname);
757  }
758  }
759  /*else
760  {
761  $sellist = '<select class="flat" name="elementselect">';
762  $sellist.= '<option value="0" disabled>'.$langs->trans("None").'</option>';
763  $sellist.= '</select>';
764  }*/
765  $this->db->free($resql);
766 
767  return $sellist;
768  } else {
769  $this->error = $this->db->lasterror();
770  $this->errors[] = $this->db->lasterror();
771  dol_syslog(get_class($this) . "::selectOpportunityStatus " . $this->error, LOG_ERR);
772  return -1;
773  }
774  }
775 
784  public function selectProjectsStatus($selected = '', $short = 0, $hmlname = 'order_status')
785  {
786  $options = array();
787 
788  // 7 is same label than 6. 8 does not exists (billed is another field)
789  $statustohow = array(
790  '0' => '0',
791  '1' => '1',
792  '2' => '2',
793  );
794 
795  $tmpproject = new Project($this->db);
796 
797  foreach ($statustohow as $key => $value) {
798  $tmpproject->statut = $key;
799  $options[$value] = $tmpproject->getLibStatut($short);
800  }
801 
802  if (is_array($selected)) {
803  $selectedarray = $selected;
804  } elseif ($selected == 99) {
805  $selectedarray = array(0,1);
806  } else {
807  $selectedarray = explode(',', $selected);
808  }
809 
810  print Form::multiselectarray($hmlname, $options, $selectedarray, 0, 0, 'minwidth100');
811  }
812 
825  public function selectInvoiceAndLine($selectedInvoiceId = 0, $selectedLineId = 0, $htmlNameInvoice = 'invoiceid', $htmlNameInvoiceLine = 'invoicelineid', $morecss = 'maxwidth500', $filters = array(), $lineOnly = 0)
826  {
827  global $user, $conf, $langs;
828 
829  require_once DOL_DOCUMENT_ROOT . '/projet/class/project.class.php';
830 
831  $out = '';
832  if (empty($lineOnly)) {
833  // Search Invoice
834  $sql = "SELECT f.rowid, f.ref as fref,";
835  $sql .= ' s.nom as name';
836  $sql .= ' FROM ' . $this->db->prefix() . 'projet as p';
837  $sql .= ' INNER JOIN ' . $this->db->prefix() . 'societe as s ON s.rowid = p.fk_soc';
838  $sql .= ' INNER JOIN ' . $this->db->prefix() . 'facture as f ON f.fk_projet = p.rowid';
839  $sql .= " WHERE p.entity IN (" . getEntity('project') . ")";
840  if (!empty($filters)) {
841  foreach ($filters as $key => $value) {
842  if ($key == 'p.rowid') {
843  $sql .= " AND p.rowid=" . (int) $value;
844  }
845  if ($key == 'f.rowid') {
846  $sql .= " AND f.rowid=" . (int) $value;
847  }
848  }
849  }
850  $sql .= " ORDER BY p.ref, f.ref ASC";
851 
852  $resql = $this->db->query($sql);
853  if ($resql) {
854  // Use select2 selector
855  if (!empty($conf->use_javascript_ajax)) {
856  include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php';
857  $comboenhancement = ajax_combobox($htmlNameInvoice, array(array('method' => 'getLines', 'url' => dol_buildpath('/core/ajax/ajaxinvoiceline.php', 1), 'htmlname' => $htmlNameInvoiceLine)), 0, 0);
858  $out .= $comboenhancement;
859  $morecss = 'minwidth200imp maxwidth500';
860  }
861 
862  $out .= '<select class="valignmiddle flat' . ($morecss ? ' ' . $morecss : '') . '" id="' . $htmlNameInvoice . '" name="' . $htmlNameInvoice . '">';
863  $num = $this->db->num_rows($resql);
864  if ($num) {
865  while ($obj = $this->db->fetch_object($resql)) {
866  $labeltoshow = $obj->fref; // Invoice ref
867  if ($obj->name) {
868  $labeltoshow .= ' - ' . $obj->name;
869  }
870 
871  $out .= '<option value="' . $obj->rowid . '" ';
872  if (!empty($selectedInvoiceId) && $selectedInvoiceId == $obj->rowid) {
873  $out .= ' selected ';
874  }
875  $out .= '>' . $labeltoshow . '</option>';
876  }
877  }
878  $out .= '</select>';
879  } else {
880  dol_print_error($this->db->lasterror);
881  return '';
882  }
883  }
884 
885  // Search Invoice Line
886  $sql = "SELECT fd.rowid, fd.label, fd.description";
887  $sql .= ' FROM ' . $this->db->prefix() . 'projet as p';
888  $sql .= ' INNER JOIN ' . $this->db->prefix() . 'societe as s ON s.rowid = p.fk_soc';
889  $sql .= ' INNER JOIN ' . $this->db->prefix() . 'facture as f ON f.fk_projet = p.rowid';
890  $sql .= ' INNER JOIN ' . $this->db->prefix() . 'facturedet as fd ON fd.fk_facture = f.rowid';
891  $sql .= " WHERE p.entity IN (" . getEntity('project') . ")";
892  if (!empty($filters)) {
893  foreach ($filters as $key => $value) {
894  if ($key == 'p.rowid') {
895  $sql .= " AND p.rowid=" . (int) $value;
896  }
897  }
898  }
899  if (!empty($selectedInvoiceId)) {
900  $sql .= " AND f.rowid=" . (int) $selectedInvoiceId;
901  }
902  $sql .= " ORDER BY p.ref, f.ref ASC";
903  $resql = $this->db->query($sql);
904  if ($resql) {
905  // Use select2 selector
906  if (empty($lineOnly)) {
907  if (!empty($conf->use_javascript_ajax)) {
908  include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php';
909  $comboenhancement = ajax_combobox($htmlNameInvoiceLine, '', 0, 0);
910  $out .= $comboenhancement;
911  $morecss = 'minwidth200imp maxwidth500';
912  }
913 
914  $out .= '<select class="valignmiddle flat' . ($morecss ? ' ' . $morecss : '') . '" id="' . $htmlNameInvoiceLine . '" name="' . $htmlNameInvoiceLine . '">';
915  }
916  $num = $this->db->num_rows($resql);
917  if ($num) {
918  while ($obj = $this->db->fetch_object($resql)) {
919  $labeltoshow .= $obj->description; // Invoice ref
920 
921  $out .= '<option value="' . $obj->rowid . '" ';
922  if (!empty($selectedLineId) && $selectedLineId == $obj->rowid) {
923  $out .= ' selected ';
924  }
925  $out .= '>' . $labeltoshow . '</option>';
926  }
927  }
928  if (empty($lineOnly)) {
929  $out .= '</select>';
930  }
931  } else {
932  dol_print_error($this->db->lasterror);
933  return '';
934  }
935 
936  return $out;
937  }
938 
951  public function formOpportunityStatus($page, $selected = '', $percent_value = 0, $htmlname_status = 'none', $htmlname_percent = 'none', $filter = '', $nooutput = 0)
952  {
953  // phpcs:enable
954  global $conf, $langs;
955 
956  $out = '';
957  if ($htmlname_status != "none" && $htmlname_percent != 'none') {
958  $out .= '<form method="post" action="' . $page . '">';
959  $out .= '<input type="hidden" name="action" value="set_opp_status">';
960  $out .= '<input type="hidden" name="token" value="' . newToken() . '">';
961  $out .= $this-> selectOpportunityStatus($htmlname_status, $selected, 1, 0, 0, 0, 'minwidth150 inline-block valignmiddle', 1, 1);
962  $out .= ' / <span title="'.$langs->trans("OpportunityProbability").'"> ';
963  $out .= '<input class="width50 right" type="text" id="'.$htmlname_percent.'" name="'.$htmlname_percent.'" title="'.dol_escape_htmltag($langs->trans("OpportunityProbability")).'" value="'.$percent_value.'"> %';
964  $out .= '</span>';
965  $out .= '<input type="submit" class="button smallpaddingimp valignmiddle" value="' . $langs->trans("Modify") . '">';
966  $out .= '</form>';
967  } else {
968  if ($selected > 0) {
969  $code = dol_getIdFromCode($this->db, $selected, 'c_lead_status', 'rowid', 'code');
970  $out .= $langs->trans("OppStatus".$code);
971 
972  // Opportunity percent
973  $out .= ' / <span title="'.$langs->trans("OpportunityProbability").'"> ';
974  $out .= price($percent_value, 0, $langs, 1, 0).' %';
975  $out .= '</span>';
976  } else {
977  $out .= "&nbsp;";
978  }
979  }
980 
981  if ($nooutput) {
982  return $out;
983  } else {
984  print $out;
985  }
986  }
987 }
ajax_autocompleter($selected, $htmlname, $url, $urloption='', $minLength=2, $autoselect=0, $ajaxoptions=array(), $moreparams='')
Generic function that return javascript to add to transform a common input text or select field into ...
Definition: ajax.lib.php:48
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete='resolve', $idforemptyvalue='-1', $morecss='')
Convert a html select field into an ajax combobox.
Definition: ajax.lib.php:456
Class to manage generation of HTML components Only common components must be here.
static multiselectarray($htmlname, $array, $selected=array(), $key_in_label=0, $value_as_key=0, $morecss='', $translate=0, $width=0, $moreattrib='', $elemtype='', $placeholder='', $addjscombo=-1)
Show a multiselect form from an array.
Class to manage building of HTML components.
selectTasks($socid=-1, $selected=0, $htmlname='taskid', $maxlength=24, $option_only=0, $show_empty='1', $discard_closed=0, $forcefocus=0, $disabled=0, $morecss='maxwidth500', $projectsListId='', $showmore='all', $usertofilter=null, $nooutput=0)
Output a combo list with tasks qualified for a third party.
select_element($table_element, $socid=0, $morecss='', $limitonstatus=-2, $projectkey="fk_projet", $placeholder='')
Build a HTML select list of element of same thirdparty to suggest to link them to project.
selectProjectsStatus($selected='', $short=0, $hmlname='order_status')
Return combo list of different statuses of orders.
select_projects_list($socid=-1, $selected=0, $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', $morefilter='')
Returns an array with projects qualified for a third party.
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='', $morefilter='')
Output a combo list with projects qualified for a third party / user.
selectInvoiceAndLine($selectedInvoiceId=0, $selectedLineId=0, $htmlNameInvoice='invoiceid', $htmlNameInvoiceLine='invoicelineid', $morecss='maxwidth500', $filters=array(), $lineOnly=0)
Output a combo list with invoices and lines qualified for a project.
selectOpportunityStatus($htmlname, $preselected='-1', $showempty=1, $useshortlabel=0, $showallnone=0, $showpercent=0, $morecss='', $noadmininfo=0, $addcombojs=0)
Build a HTML select list of element of same thirdparty to suggest to link them to project.
formOpportunityStatus($page, $selected='', $percent_value=0, $htmlname_status='none', $htmlname_percent='none', $filter='', $nooutput=0)
Output html select to select opportunity status.
__construct($db)
Constructor.
Class to manage projects.
const STATUS_CLOSED
Closed status.
const STATUS_DRAFT
Draft status.
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:751
natural_search($fields, $value, $mode=0, $nofirstand=0)
Generate natural SQL search string for a criteria (this criteria can be tested on one or several fiel...
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0, $filters='')
Return an id or code from a code or id.
newToken()
Return the value of token currently saved into session with name 'newtoken'.
info_admin($text, $infoonimgalt=0, $nodiv=0, $admin='1', $morecss='hideonsmartphone', $textfordropdown='')
Show information in HTML for admin users or standard users.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...