dolibarr 24.0.0-beta
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-2026 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
29require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
30
34class FormProjets extends Form
35{
39 public $db;
40
44 public $error = '';
45
46 public $errors = array();
47
48
52 public $nboftasks;
53
54
60 public function __construct($db)
61 {
62 $this->db = $db;
63 }
64
65 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
66
88 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 = '')
89 {
90 // phpcs:enable
91 global $langs, $conf, $form;
92
93 $selected_input_value = '';
94 if (is_object($selected)) {
95 $selected_input_value = $selected->ref;
96 $selected = $selected->id;
97 }
98
99 $out = '';
100
101 if (!empty($conf->use_javascript_ajax) && getDolGlobalString('PROJECT_USE_SEARCH_TO_SELECT')) {
102 $placeholder = '';
103
104 if ($selected && empty($selected_input_value)) {
105 require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
106 $project = new Project($this->db);
107 $project->fetch((int) $selected);
108 $selected_input_value = $project->ref;
109 }
110 $urloption = 'socid=' . ((int) $socid) . '&htmlname=' . urlencode($htmlname) . '&discardclosed=' . ((int) $discard_closed);
111 if ($morefilter == 'usage_organize_event=1') {
112 $urloption .= '&usage_organize_event=1';
113 }
114 $out .= '<input type="text" class="minwidth200' . ($morecss ? ' ' . $morecss : '') . '" name="search_' . $htmlname . '" id="search_' . $htmlname . '" value="' . $selected_input_value . '"' . $placeholder . ' />';
115
116 $out .= ajax_autocompleter($selected, $htmlname, DOL_URL_ROOT . '/projet/ajax/projects.php', $urloption, getDolGlobalInt('PROJECT_USE_SEARCH_TO_SELECT'), 0, array());
117 } else {
118 $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);
119 }
120 if ($discard_closed > 0) {
121 if (!empty($form)) {
122 $out .= $form->textwithpicto('', $langs->trans("ClosedProjectsAreHidden"));
123 }
124 }
125
126 if (empty($nooutput)) {
127 print $out;
128 return '';
129 } else {
130 return $out;
131 }
132 }
133
134 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
135
157 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 = '')
158 {
159 // phpcs:enable
160 global $user, $conf, $langs;
161
162 require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
163
164 if (empty($htmlid)) {
165 $htmlid = $htmlname;
166 }
167
168 $out = '';
169 $outarray = array();
170
171 $hideunselectables = false;
172 if (getDolGlobalString('PROJECT_HIDE_UNSELECTABLES')) {
173 $hideunselectables = true;
174 }
175 if (getDolGlobalInt('PROJECT_ALWAYS_DISCARD_CLOSED_PROJECTS_IN_SELECT')) {
176 $discard_closed = 1;
177 }
178
179 $projectsListId = false;
180 if (!$user->hasRight('projet', 'all', 'lire')) {
181 $projectstatic = new Project($this->db);
182 $projectsListId = $projectstatic->getProjectsAuthorizedForUser($user, 0, 1);
183 }
184
185 // Search all projects
186 $sql = "SELECT p.rowid, p.ref, p.title, p.fk_soc, p.fk_statut, p.public, s.nom as name, s.name_alias";
187 $sql .= " FROM " . $this->db->prefix() . "projet as p LEFT JOIN " . $this->db->prefix() . "societe as s ON s.rowid = p.fk_soc";
188 $sql .= " WHERE p.entity IN (" . getEntity('project') . ")";
189 if ($projectsListId !== false) {
190 $sql .= " AND p.rowid IN (" . $this->db->sanitize($projectsListId) . ")";
191 }
192 if ($socid == 0) {
193 $sql .= " AND (p.fk_soc=0 OR p.fk_soc IS NULL)";
194 }
195 if ($socid > 0) {
196 if (!getDolGlobalString('PROJECT_ALLOW_TO_LINK_FROM_OTHER_COMPANY')) {
197 $sql .= " AND (p.fk_soc=" . ((int) $socid) . " OR p.fk_soc IS NULL)";
198 } 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.
199 $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)";
200 }
201 }
202 if (!empty($filterkey)) {
203 $sql .= natural_search(array('p.title', 'p.ref'), $filterkey);
204 }
205 if ($morefilter) {
206 $sql .= ' AND (' . $this->db->sanitize($morefilter, 0, 1) . ')';
207 }
208 $sql .= " ORDER BY p.ref ASC";
209
210 $resql = $this->db->query($sql);
211 if ($resql) {
212 if (!empty($conf->use_javascript_ajax)) {
213 $morecss .= ' minwidth100';
214 }
215 if (empty($option_only)) {
216 $out .= '<select class="flat' . ($morecss ? ' ' . $morecss : '') . '"' . ($disabled ? ' disabled="disabled"' : '') . ' id="' . $htmlid . '" name="' . $htmlname . '">';
217 }
218 if (!empty($show_empty)) {
219 if (is_numeric($show_empty)) {
220 $out .= '<option value="0">&nbsp;</option>';
221 } else {
222 $out .= '<option value="-1">'.$show_empty.'</option>';
223 }
224 }
225 $num = $this->db->num_rows($resql);
226 $i = 0;
227 if ($num) {
228 while ($i < $num) {
229 $obj = $this->db->fetch_object($resql);
230 // 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.
231 if ($socid > 0 && (empty($obj->fk_soc) || $obj->fk_soc == $socid) && !$user->hasRight('societe', 'lire')) {
232 // Do nothing
233 } else {
234 if ($discard_closed == 1 && $obj->fk_statut == 2 && $obj->rowid != $selected) { // We discard closed except if selected
235 $i++;
236 continue;
237 }
238
239 $labeltoshow = dol_trunc($obj->ref, 18);
240 //if ($obj->public) $labeltoshow.=' ('.$langs->trans("SharedProject").')';
241 //else $labeltoshow.=' ('.$langs->trans("Private").')';
242 $labeltoshow .= ', ' . dol_trunc($obj->title, $maxlength);
243 if ($obj->name) {
244 $labeltoshow .= ' - ' . $obj->name;
245 if ($obj->name_alias) {
246 $labeltoshow .= ' (' . $obj->name_alias . ')';
247 }
248 }
249
250 $disabled = 0;
251 if ($obj->fk_statut == 0) {
252 $disabled = 1;
253 $labeltoshow .= ' - ' . $langs->trans("Draft");
254 } elseif ($obj->fk_statut == 2) {
255 if ($discard_closed == 2) {
256 $disabled = 1;
257 }
258 $labeltoshow .= ' - ' . $langs->trans("Closed");
259 } elseif (!getDolGlobalString('PROJECT_ALLOW_TO_LINK_FROM_OTHER_COMPANY') && $socid > 0 && (!empty($obj->fk_soc) && $obj->fk_soc != $socid)) {
260 $disabled = 1;
261 $labeltoshow .= ' - ' . $langs->trans("LinkedToAnotherCompany");
262 }
263
264 if (!empty($selected) && $selected == $obj->rowid) {
265 $out .= '<option value="' . $obj->rowid . '" selected';
266 //if ($disabled) $out.=' disabled'; // with select2, field can't be preselected if disabled
267 $out .= '>' . $labeltoshow . '</option>';
268 } else {
269 if ($hideunselectables && $disabled && ($selected != $obj->rowid)) {
270 $resultat = '';
271 } else {
272 $resultat = '<option value="' . $obj->rowid . '"';
273 if ($disabled) {
274 $resultat .= ' disabled';
275 }
276 //if ($obj->public) $labeltoshow.=' ('.$langs->trans("Public").')';
277 //else $labeltoshow.=' ('.$langs->trans("Private").')';
278 $resultat .= '>';
279 $resultat .= $labeltoshow;
280 $resultat .= '</option>';
281 }
282 $out .= $resultat;
283
284 $outarray[] = array(
285 'key' => (int) $obj->rowid,
286 'value' => $obj->ref,
287 'ref' => $obj->ref,
288 'labelx' => $labeltoshow,
289 'label' => ($disabled ? '<span class="opacitymedium">' . $labeltoshow . '</span>' : $labeltoshow),
290 'disabled' => (bool) $disabled
291 );
292 }
293 }
294 $i++;
295 }
296 }
297
298 $this->db->free($resql);
299
300 if (!$mode) {
301 if (empty($option_only)) {
302 $out .= '</select>';
303 }
304
305 // Use select2 selector
306 if (!empty($conf->use_javascript_ajax)) {
307 include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php';
308 $comboenhancement = ajax_combobox($htmlid, array(), 0, $forcefocus);
309 $out .= $comboenhancement;
310 $morecss .= ' minwidth100';
311 }
312
313 if (empty($nooutput)) {
314 print $out;
315 return '';
316 } else {
317 return $out;
318 }
319 } else {
320 return $outarray;
321 }
322 } else {
323 dol_print_error($this->db);
324 return -1;
325 }
326 }
327
348 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)
349 {
350 global $user, $conf, $langs;
351
352 require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
353
354 if (is_null($usertofilter)) {
355 $usertofilter = $user;
356 }
357
358 $out = '';
359
360 $hideunselectables = false;
361 if (getDolGlobalString('PROJECT_HIDE_UNSELECTABLES')) {
362 $hideunselectables = true;
363 }
364
365 if (empty($projectsListId)) {
366 if (!$usertofilter->hasRight('projet', 'all', 'lire')) {
367 $projectstatic = new Project($this->db);
368 $projectsListId = $projectstatic->getProjectsAuthorizedForUser($usertofilter, 0, 1);
369 }
370 }
371
372 // Search all projects
373 $sql = "SELECT t.rowid, t.ref as tref, t.label as tlabel, t.progress,";
374 $sql .= " p.rowid as pid, p.ref, p.title, p.fk_soc, p.fk_statut, p.public, p.usage_task,";
375 $sql .= " s.nom as name";
376 $sql .= " FROM " . $this->db->prefix() . "projet as p";
377 $sql .= " LEFT JOIN " . $this->db->prefix() . "societe as s ON s.rowid = p.fk_soc,";
378 $sql .= " " . $this->db->prefix() . "projet_task as t";
379 $sql .= " WHERE p.entity IN (" . getEntity('project') . ")";
380 $sql .= " AND t.fk_projet = p.rowid";
381 if ($projectsListId) {
382 $sql .= " AND p.rowid IN (" . $this->db->sanitize($projectsListId) . ")";
383 }
384 if ($socid == 0) {
385 $sql .= " AND (p.fk_soc=0 OR p.fk_soc IS NULL)";
386 }
387 if ($socid > 0) {
388 $sql .= " AND (p.fk_soc=" . ((int) $socid) . " OR p.fk_soc IS NULL)";
389 }
390 $sql .= " ORDER BY p.ref, t.ref ASC";
391
392 $resql = $this->db->query($sql);
393 if ($resql) {
394 // Use select2 selector
395 if (empty($option_only) && !empty($conf->use_javascript_ajax)) {
396 include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php';
397 $comboenhancement = ajax_combobox($htmlname, [], 0, $forcefocus);
398 $out .= $comboenhancement;
399 $morecss .= ' minwidth150imp';
400 }
401
402 if (empty($option_only)) {
403 $out .= '<select class="valignmiddle flat' . ($morecss ? ' ' . $morecss : '') . '"' . ($disabled ? ' disabled="disabled"' : '') . ' id="' . $htmlname . '" name="' . $htmlname . '">'."\n";
404 }
405 if (!empty($show_empty)) {
406 $out .= '<option value="0" class="optiongrey">';
407 if (!is_numeric($show_empty)) {
408 //if (!empty($conf->use_javascript_ajax)) $out .= '<span class="opacitymedium">';
409 $out .= $show_empty;
410 //if (!empty($conf->use_javascript_ajax)) $out .= '</span>';
411 } else {
412 $out .= '&nbsp;';
413 }
414 $out .= '</option>'."\n";
415 }
416
417 $num = $this->db->num_rows($resql);
418 $i = 0;
419 if ($num) {
420 while ($i < $num) {
421 $obj = $this->db->fetch_object($resql);
422 // 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.
423 if ($socid > 0 && (empty($obj->fk_soc) || $obj->fk_soc == $socid) && !$usertofilter->hasRight('societe', 'lire')) {
424 // Do nothing
425 } else {
426 if ($discard_closed == 1 && $obj->fk_statut == Project::STATUS_CLOSED) {
427 $i++;
428 continue;
429 }
430
431 $labeltoshow = '';
432 $labeltoshowhtml = '';
433
434 $disabled = 0;
435 if ($obj->fk_statut == Project::STATUS_DRAFT) {
436 $disabled = 1;
437 } elseif ($obj->fk_statut == Project::STATUS_CLOSED) {
438 if ($discard_closed == 2) {
439 $disabled = 1;
440 }
441 } elseif ($socid > 0 && (!empty($obj->fk_soc) && $obj->fk_soc != $socid)) {
442 $disabled = 1;
443 }
444
445 if (preg_match('/all/', $showmore)) {
446 $labeltoshow .= dol_trunc($obj->ref, 18); // Project ref
447 //if ($obj->public) $labeltoshow.=' ('.$langs->trans("SharedProject").')';
448 //else $labeltoshow.=' ('.$langs->trans("Private").')';
449 $labeltoshow .= ' ' . dol_trunc($obj->title, $maxlength);
450 $labeltoshowhtml = $labeltoshow;
451
452 if ($obj->name) {
453 $labeltoshow .= ' (' . $obj->name . ')';
454 $labeltoshowhtml .= ' <span class="opacitymedium">(' . $obj->name . ')</span>';
455 }
456
457 $disabled = 0;
458 if ($obj->fk_statut == Project::STATUS_DRAFT) {
459 $disabled = 1;
460 $labeltoshow .= ' - ' . $langs->trans("Draft");
461 $labeltoshowhtml .= ' - <span class="opacitymedium">' . $langs->trans("Draft") . '</span>';
462 } elseif ($obj->fk_statut == Project::STATUS_CLOSED) {
463 if ($discard_closed == 2) {
464 $disabled = 1;
465 }
466 $labeltoshow .= ' - ' . $langs->trans("Closed");
467 $labeltoshowhtml .= ' - <span class="opacitymedium">' . $langs->trans("Closed") . '</span>';
468 } elseif ($socid > 0 && (!empty($obj->fk_soc) && $obj->fk_soc != $socid)) {
469 $disabled = 1;
470 $labeltoshow .= ' - ' . $langs->trans("LinkedToAnotherCompany");
471 $labeltoshowhtml .= ' - <span class="opacitymedium">' . $langs->trans("LinkedToAnotherCompany") . '</span>';
472 }
473 $labeltoshow .= ' - ';
474 $labeltoshowhtml .= ' - ';
475 }
476
477 // Label for task
478 $labeltoshow .= $obj->tref . ' ' . dol_trunc($obj->tlabel, $maxlength);
479 $labeltoshowhtml .= $obj->tref . ' - ' . dol_trunc($obj->tlabel, $maxlength);
480 if ($obj->usage_task && preg_match('/progress/', $showmore)) {
481 $labeltoshow .= ' <span class="opacitymedium">(' . $obj->progress . '%)</span>';
482 $labeltoshowhtml .= ' <span class="opacitymedium">(' . $obj->progress . '%)</span>';
483 }
484
485 if (!empty($selected) && $selected == $obj->rowid) {
486 $out .= '<option value="' . $obj->rowid . '" selected';
487 $out .= ' data-html="' . dol_escape_htmltag($labeltoshowhtml) . '"';
488 //if ($disabled) $out.=' disabled'; // with select2, field can't be preselected if disabled
489 $out .= '>' . $labeltoshow . '</option>'."\n";
490 } else {
491 if ($hideunselectables && $disabled && ($selected != $obj->rowid)) {
492 $resultat = '';
493 } else {
494 $resultat = '<option value="' . $obj->rowid . '"';
495 if ($disabled) {
496 $resultat .= ' disabled';
497 }
498 //if ($obj->public) $labeltoshow.=' ('.$langs->trans("Public").')';
499 //else $labeltoshow.=' ('.$langs->trans("Private").')';
500 $resultat .= ' data-html="' . dol_escape_htmltag($labeltoshowhtml) . '"';
501 $resultat .= '>';
502 $resultat .= $labeltoshow;
503 $resultat .= '</option>'."\n";
504 }
505 $out .= $resultat;
506 }
507 }
508 $i++;
509 }
510 }
511 if (empty($option_only)) {
512 $out .= '</select>'."\n";
513 }
514
515 $this->nboftasks = $num;
516 $this->db->free($resql);
517
518 // Output or return
519 if (empty($nooutput)) {
520 print $out;
521 } else {
522 return $out;
523 }
524
525 return $num;
526 } else {
527 dol_print_error($this->db);
528 return -1;
529 }
530 }
531
532
533 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
534
546 public function select_element($table_element, $socid = 0, $morecss = '', $limitonstatus = -2, $projectkey = "fk_projet", $placeholder = '')
547 {
548 // phpcs:enable
549 if ($table_element == 'projet_task') {
550 return ''; // Special case of element we never link to a project (already always done)
551 }
552
553 $linkedtothirdparty = false;
554 if (!in_array(
555 $table_element,
556 array(
557 'don',
558 'expensereport_det',
559 'expensereport', 'loan',
560 'stock_mouvement',
561 'payment_salary',
562 'payment_various',
563 'salary',
564 'chargesociales',
565 'entrepot')
566 )) {
567 $linkedtothirdparty = true;
568 }
569
570 //print $table_element;
571 switch ($table_element) {
572 case "loan":
573 $sql = "SELECT t.rowid, t.label as ref";
574 break;
575 case "facture":
576 $sql = "SELECT t.rowid, t.ref as ref";
577 break;
578 case "facture_fourn":
579 $sql = "SELECT t.rowid, t.ref, t.ref_supplier";
580 break;
581 case "commande_fourn":
582 case "commande_fournisseur":
583 $sql = "SELECT t.rowid, t.ref, t.ref_supplier";
584 break;
585 case "facture_rec":
586 $sql = "SELECT t.rowid, t.titre as ref";
587 break;
588 case "actioncomm":
589 $sql = "SELECT t.id as rowid, t.label as ref";
590 $projectkey = "fk_project";
591 break;
592 case "expensereport":
593 return '';
594 case "expensereport_det":
595 /*$sql = "SELECT rowid, '' as ref"; // table is llx_expensereport_det
596 $projectkey="fk_projet";
597 break;*/
598 return '';
599 case "commande":
600 case "contrat":
601 case "fichinter":
602 $sql = "SELECT t.rowid, t.ref";
603 break;
604 case 'stock_mouvement':
605 $sql = "SELECT t.rowid, t.label as ref";
606 $projectkey = 'fk_origin';
607 break;
608 case "payment_salary":
609 $sql = "SELECT t.rowid, t.num_payment as ref"; // TODO In a future fill and use real ref field
610 break;
611 case "payment_various":
612 $sql = "SELECT t.rowid, t.num_payment as ref";
613 break;
614 case "chargesociales":
615 default:
616 $sql = "SELECT t.rowid, t.ref";
617 break;
618 }
619 if ($linkedtothirdparty) {
620 $sql .= ", s.nom as name";
621 }
622 $sql .= " FROM " . $this->db->prefix() . $this->db->sanitize($table_element) . " as t";
623 if ($linkedtothirdparty) {
624 $sql .= ", " . $this->db->prefix() . "societe as s";
625 }
626 $sql .= " WHERE " . $this->db->sanitize($projectkey) . " is null";
627 if (!empty($socid) && $linkedtothirdparty) {
628 if (is_numeric($socid)) {
629 $sql .= " AND t.fk_soc = " . ((int) $socid);
630 } else {
631 $sql .= " AND t.fk_soc IN (" . $this->db->sanitize($socid) . ")";
632 }
633 }
634 if (!in_array($table_element, array('expensereport_det', 'stock_mouvement'))) {
635 $sql .= ' AND t.entity IN (' . getEntity('project') . ')';
636 }
637 if ($linkedtothirdparty) {
638 $sql .= " AND s.rowid = t.fk_soc";
639 }
640 $sql .= " ORDER BY ref DESC";
641
642 dol_syslog(get_class($this) . '::select_element', LOG_DEBUG);
643 $resql = $this->db->query($sql);
644 if ($resql) {
645 $num = $this->db->num_rows($resql);
646 $i = 0;
647 $sellist = '';
648
649 if ($num > 0) {
650 $sellist = '<select class="flat elementselect css' . $table_element . ($morecss ? ' ' . $morecss : '') . '" name="elementselect">';
651 $sellist .= '<option value="-1"' . ($placeholder ? ' class="optiongrey"' : '') . '>' . $placeholder . '</option>';
652 while ($i < $num) {
653 $obj = $this->db->fetch_object($resql);
654 $ref = $obj->ref ? $obj->ref : $obj->rowid;
655 if (!empty($obj->ref_supplier)) {
656 $ref .= ' (' . $obj->ref_supplier . ')';
657 }
658 if (!empty($obj->name)) {
659 $ref .= ' - ' . $obj->name;
660 }
661 $sellist .= '<option value="' . $obj->rowid . '">' . $ref . '</option>';
662 $i++;
663 }
664 $sellist .= '</select>';
665 }
666 /*else
667 {
668 $sellist = '<select class="flat" name="elementselect">';
669 $sellist.= '<option value="0" disabled>'.$langs->trans("None").'</option>';
670 $sellist.= '</select>';
671 }*/
672 $this->db->free($resql);
673
674 return $sellist;
675 } else {
676 dol_print_error($this->db);
677 $this->error = $this->db->lasterror();
678 $this->errors[] = $this->db->lasterror();
679 dol_syslog(get_class($this) . "::select_element " . $this->error, LOG_ERR);
680 return -1;
681 }
682 }
683
684
699 public function selectOpportunityStatus($htmlname, $preselected = -1, $showempty = 1, $useshortlabel = 0, $showallnone = 0, $showpercent = 0, $morecss = '', $noadmininfo = 0, $addcombojs = 0)
700 {
701 global $langs, $user;
702
703 $sql = "SELECT rowid, code, label, percent";
704 $sql .= " FROM " . $this->db->prefix() . 'c_lead_status';
705 $sql .= " WHERE active = 1";
706 $sql .= " ORDER BY position";
707
708 $resql = $this->db->query($sql);
709 if ($resql) {
710 $num = $this->db->num_rows($resql);
711 $i = 0;
712 $sellist = '';
713 if ($num > 0) {
714 $sellist = '<select class="flat oppstatus' . ($morecss ? ' ' . $morecss : '') . '" id="' . $htmlname . '" name="' . $htmlname . '">';
715 if ($showempty) {
716 // Without &nbsp, strange move of screen when switching value
717 $sellist .= '<option value="-1">&nbsp;</option>';
718 }
719 if ($showallnone) {
720 $sellist .= '<option value="all"' . ($preselected == 'all' ? ' selected="selected"' : '') . '>-- ' . $langs->trans("OnlyOpportunitiesShort") . '</option>';
721 $sellist .= '<option value="openedopp"' . ($preselected == 'openedopp' ? ' selected="selected"' : '') . '>-- ' . $langs->trans("OpenedOpportunitiesShort") . '</option>';
722 $sellist .= '<option value="notopenedopp"' . ($preselected == 'notopenedopp' ? ' selected="selected"' : '') . '>-- ' . $langs->trans("NotOpenedOpportunitiesShort") . '</option>';
723 $sellist .= '<option value="none"' . ($preselected == 'none' ? ' selected="selected"' : '') . '>-- ' . $langs->trans("NotAnOpportunityShort") . '</option>';
724 }
725 $separatoradded = 0;
726 while ($i < $num) {
727 $obj = $this->db->fetch_object($resql);
728
729 if (($obj->code == 'WON' || $obj->code == 'LOST') && !$separatoradded) {
730 $separatoradded = 1;
731 $sellist .= '<option value="" disabled>--------------------</option>';
732 }
733 $sellist .= '<option value="' . $obj->rowid . '" defaultpercent="' . $obj->percent . '" data-elemcode="' . $obj->code . '"';
734 if ($obj->rowid == $preselected) {
735 $sellist .= ' selected="selected"';
736 }
737 $sellist .= '>';
738 if ($useshortlabel) {
739 $finallabel = ($langs->transnoentitiesnoconv("OppStatus" . $obj->code) != "OppStatus" . $obj->code ? $langs->transnoentitiesnoconv("OppStatus" . $obj->code) : $obj->label);
740 } else {
741 $finallabel = ($langs->transnoentitiesnoconv("OppStatus" . $obj->code) != "OppStatus" . $obj->code ? $langs->transnoentitiesnoconv("OppStatus" . $obj->code) : $obj->label);
742 if ($showpercent) {
743 $finallabel .= ' (' . $obj->percent . '%)';
744 }
745 }
746 $sellist .= $finallabel;
747 $sellist .= '</option>';
748 $i++;
749 }
750 $sellist .= '</select>';
751
752 if ($user->admin && !$noadmininfo) {
753 $sellist .= info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
754 }
755
756 if ($addcombojs) {
757 $sellist .= ajax_combobox($htmlname);
758 }
759 }
760
761 $this->db->free($resql);
762
763 return $sellist;
764 } else {
765 $this->error = $this->db->lasterror();
766 $this->errors[] = $this->db->lasterror();
767 dol_syslog(get_class($this) . "::selectOpportunityStatus " . $this->error, LOG_ERR);
768 return -1;
769 }
770 }
771
780 public function selectProjectsStatus($selected = '', $short = 0, $htmlname = 'order_status')
781 {
782 $options = array();
783
784 // 7 is same label than 6. 8 does not exists (billed is another field)
785 $statustohow = array(
786 '0' => '0',
787 '1' => '1',
788 '2' => '2',
789 );
790
791 require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
792 $tmpproject = new Project($this->db);
793
794 foreach ($statustohow as $key => $value) {
795 $tmpproject->statut = $key; // deprecated
796 $tmpproject->status = $key;
797 $options[$value] = $tmpproject->getLibStatut($short);
798 }
799
800 if (is_array($selected)) {
801 $selectedarray = $selected;
802 } elseif ($selected == 99) {
803 $selectedarray = array(0,1);
804 } else {
805 $selectedarray = explode(',', $selected);
806 }
807
808 print Form::multiselectarray($htmlname, $options, $selectedarray, 0, 0, 'minwidth100');
809 }
810
823 public function selectInvoiceAndLine($selectedInvoiceId = 0, $selectedLineId = 0, $htmlNameInvoice = 'invoiceid', $htmlNameInvoiceLine = 'invoicelineid', $morecss = 'maxwidth500', $filters = array(), $lineOnly = 0)
824 {
825 global $user, $conf, $langs;
826
827 require_once DOL_DOCUMENT_ROOT . '/projet/class/project.class.php';
828
829 $out = '';
830 $labeltoshow = '';
831
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, $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
917 $num = $this->db->num_rows($resql);
918 if ($num) {
919 while ($obj = $this->db->fetch_object($resql)) {
920 $labeltoshow .= $obj->description; // Invoice ref
921
922 $out .= '<option value="' . $obj->rowid . '" ';
923 if (!empty($selectedLineId) && $selectedLineId == $obj->rowid) {
924 $out .= ' selected ';
925 }
926 $out .= '>' . $labeltoshow . '</option>';
927 }
928 }
929 if (empty($lineOnly)) {
930 $out .= '</select>';
931 }
932 } else {
933 dol_print_error($this->db, $this->db->lasterror);
934 return '';
935 }
936
937 return $out;
938 }
939
952 public function formOpportunityStatus($page, $selected = '', $percent_value = 0, $htmlname_status = 'none', $htmlname_percent = 'none', $filter = '', $nooutput = 0)
953 {
954 // phpcs:enable
955 global $conf, $langs;
956
957 $out = '';
958 if ($htmlname_status != "none" && $htmlname_percent != 'none') {
959 $out .= '<form method="post" action="' . $page . '">';
960 $out .= '<input type="hidden" name="action" value="set_opp_status">';
961 $out .= '<input type="hidden" name="token" value="' . newToken() . '">';
962 $out .= $this-> selectOpportunityStatus($htmlname_status, $selected, 1, 0, 0, 0, 'minwidth150 inline-block valignmiddle', 1, 1);
963 $out .= ' / <span title="'.$langs->trans("OpportunityProbability").'"> ';
964 $out .= '<input class="width50 right" type="text" id="'.$htmlname_percent.'" name="'.$htmlname_percent.'" title="'.dol_escape_htmltag($langs->trans("OpportunityProbability")).'" value="'.$percent_value.'"> %';
965 $out .= '</span>';
966 $out .= '<input type="submit" class="button smallpaddingimp valignmiddle" value="' . $langs->trans("Modify") . '">';
967 $out .= '</form>';
968 } else {
969 if ($selected > 0) {
970 $code = dol_getIdFromCode($this->db, $selected, 'c_lead_status', 'rowid', 'code');
971 $out .= $langs->trans("OppStatus".$code);
972
973 // Opportunity percent
974 $out .= ' / <span title="'.$langs->trans("OpportunityProbability").'"> ';
975 $out .= price($percent_value, 0, $langs, 1, 0).' %';
976 $out .= '</span>';
977 } else {
978 $out .= "&nbsp;";
979 }
980 }
981
982 if ($nooutput) {
983 return $out;
984 } else {
985 print $out;
986 }
987 }
988}
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:50
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:476
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='', $nu='', $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, $htmlname='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.
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.
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.
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('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0, $filters='', $useCache=true)
Return an id or code from a code or id.
info_admin($text, $infoonimgalt=0, $nodiv=0, $admin='1', $morecss='hideonsmartphone', $textfordropdown='', $picto='', $textonpictotooltip='')
Show information in HTML for admin users or standard users.
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.
natural_search($fields, $value, $mode=0, $nofirstand=0, $sqltoadd='')
Generate natural SQL search string for a criteria (this criteria can be tested on one or several fiel...
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
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 a 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...