dolibarr 25.0.0-alpha
project.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2002-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2005-2020 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2005-2010 Regis Houssin <regis.houssin@inodbox.com>
5 * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
6 * Copyright (C) 2014-2017 Marcos García <marcosgdf@gmail.com>
7 * Copyright (C) 2017 Ferran Marcet <fmarcet@2byte.es>
8 * Copyright (C) 2019 Juanjo Menent <jmenent@2byte.es>
9 * Copyright (C) 2022 Charlene Benke <charlene@patas-monkey.com>
10 * Copyright (C) 2023 Gauthier VERDOL <gauthier.verdol@atm-consulting.fr>
11 * Copyright (C) 2024-2026 Frédéric France <frederic.france@free.fr>
12 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
13 * Copyright (C) 2024 William Mead <william.mead@manchenumerique.fr>
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 3 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program. If not, see <https://www.gnu.org/licenses/>.
27 */
28
34require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
35
39class Project extends CommonObject
40{
44 public $element = 'project';
45
49 public $table_element = 'projet';
50
54 public $table_element_line = 'projet_task';
55
59 public $table_element_date;
60
64 public $fk_element = 'fk_projet';
65
69 public $picto = 'project';
70
74 protected $table_ref_field = 'ref';
75
79 public $fk_project;
80
84 public $description;
85
89 public $title;
90
95 public $dateo;
96
100 public $date_start;
101
106 public $datee;
107
111 public $date_end;
112
116 public $date_start_event;
117
121 public $date_end_event;
122
126 public $location;
127
131 public $date_close;
132
136 public $socid; // To store id of thirdparty
137
141 public $thirdparty_name; // To store name of thirdparty (defined only in some cases)
142
146 public $user_author_id;
147
151 public $fk_user_close;
152
156 public $public;
157
161 public $budget_amount;
162
166 public $usage_opportunity;
167
171 public $usage_task;
172
176 public $usage_bill_time; // Is the time spent on project must be invoiced or not
177
181 public $usage_organize_event;
182
186 public $accept_conference_suggestions;
187
191 public $accept_booth_suggestions;
192
196 public $price_registration;
197
201 public $price_booth;
202
206 public $max_attendees;
207
212 public $statut; // 0=draft, 1=opened, 2=closed
213
217 public $opp_status; // opportunity status, into table llx_c_lead_status
218
222 public $opp_status_code;
223
227 public $fk_opp_status; // opportunity status, into table llx_c_lead_status
228
232 public $opp_amount; // opportunity amount
233
237 public $opp_percent; // opportunity probability
238
242 public $opp_weighted_amount; // opportunity weighted amount
243
247 public $email_msgid;
248
252 public $weekWorkLoad;
256 public $weekWorkLoadPerTask; // Used to store workload details of tasks of a projet
257
261 public $monthWorkLoad;
262
266 public $monthWorkLoadPerTask;
267
272 public $datec;
273
277 public $date_c;
278
283 public $datem;
284
288 public $date_m;
289
293 public $ip;
294
298 public $lines;
299
340 // BEGIN MODULEBUILDER PROPERTIES
344 public $fields = array(
345 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 10),
346 'ref' => array('type' => 'varchar(50)', 'label' => 'Ref', 'enabled' => 1, 'visible' => 1, 'showoncombobox' => 1, 'position' => 15, 'searchall' => 1),
347 'title' => array('type' => 'varchar(255)', 'label' => 'ProjectLabel', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'position' => 17, 'showoncombobox' => 2, 'searchall' => 1, 'csslist' => 'tdoverflowmax250'),
348 'fk_project' => array('type' => 'integer', 'label' => 'Parent', 'enabled' => 1, 'visible' => -1, 'notnull' => 0, 'position' => 18),
349 'entity' => array('type' => 'integer', 'label' => 'Entity', 'default' => '1', 'enabled' => 1, 'visible' => 3, 'notnull' => 1, 'position' => 19),
350 'fk_soc' => array('type' => 'integer:Societe:societe/class/societe.class.php', 'label' => 'ThirdParty', 'enabled' => 1, 'visible' => 0, 'position' => 20),
351 'dateo' => array('type' => 'date', 'label' => 'DateStart', 'enabled' => 1, 'visible' => -1, 'position' => 30),
352 'datee' => array('type' => 'date', 'label' => 'DateEnd', 'enabled' => 1, 'visible' => 1, 'position' => 35),
353 'description' => array('type' => 'text', 'label' => 'Description', 'enabled' => 1, 'visible' => 3, 'position' => 55, 'searchall' => 1),
354 'public' => array('type' => 'integer', 'label' => 'Visibility', 'enabled' => 1, 'visible' => -1, 'position' => 65),
355 'fk_opp_status' => array('type' => 'integer:CLeadStatus:core/class/cleadstatus.class.php', 'label' => 'OpportunityStatusShort', 'enabled' => 'getDolGlobalString("PROJECT_USE_OPPORTUNITIES")', 'visible' => 1, 'position' => 75, 'alwayseditable' => 1, 'uieditable' => 1),
356 'opp_percent' => array('type' => 'double(5,2)', 'label' => 'OpportunityProbabilityShort', 'enabled' => 'getDolGlobalString("PROJECT_USE_OPPORTUNITIES")', 'visible' => 1, 'position' => 80),
357 'note_private' => array('type' => 'html', 'label' => 'NotePrivate', 'enabled' => 1, 'visible' => 0, 'position' => 85, 'searchall' => 1),
358 'note_public' => array('type' => 'html', 'label' => 'NotePublic', 'enabled' => 1, 'visible' => 0, 'position' => 90, 'searchall' => 1),
359 'model_pdf' => array('type' => 'varchar(255)', 'label' => 'ModelPdf', 'enabled' => 1, 'visible' => 0, 'position' => 95),
360 'date_close' => array('type' => 'datetime', 'label' => 'DateClosing', 'enabled' => 1, 'visible' => 0, 'position' => 105),
361 'fk_user_close' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserClosing', 'enabled' => 1, 'visible' => 0, 'position' => 110),
362 'opp_amount' => array('type' => 'double(24,8)', 'label' => 'OpportunityAmountShort', 'enabled' => 1, 'visible' => 'getDolGlobalString("PROJECT_USE_OPPORTUNITIES")', 'position' => 115),
363 'budget_amount' => array('type' => 'double(24,8)', 'label' => 'Budget', 'enabled' => 1, 'visible' => -1, 'position' => 119),
364 'usage_opportunity' => array('type' => 'integer', 'label' => 'UsageOpportunity', 'enabled' => 1, 'visible' => -1, 'position' => 130),
365 'usage_task' => array('type' => 'integer', 'label' => 'UsageTasks', 'enabled' => 1, 'visible' => -1, 'position' => 135),
366 'usage_bill_time' => array('type' => 'integer', 'label' => 'UsageBillTimeShort', 'enabled' => 1, 'visible' => -1, 'position' => 140),
367 'usage_organize_event' => array('type' => 'integer', 'label' => 'UsageOrganizeEvent', 'enabled' => 1, 'visible' => -1, 'position' => 145),
368 // Properties for event organization
369 'date_start_event' => array('type' => 'date', 'label' => 'DateStartEvent', 'enabled' => "isModEnabled('eventorganization')", 'visible' => 1, 'position' => 200),
370 'date_end_event' => array('type' => 'date', 'label' => 'DateEndEvent', 'enabled' => "isModEnabled('eventorganization')", 'visible' => 1, 'position' => 201),
371 'location' => array('type' => 'text', 'label' => 'Location', 'enabled' => 1, 'visible' => 3, 'position' => 202, 'searchall' => 1),
372 'accept_conference_suggestions' => array('type' => 'integer', 'label' => 'AllowUnknownPeopleSuggestConf', 'enabled' => 1, 'visible' => -1, 'position' => 210),
373 'accept_booth_suggestions' => array('type' => 'integer', 'label' => 'AllowUnknownPeopleSuggestBooth', 'enabled' => 1, 'visible' => -1, 'position' => 211),
374 'price_registration' => array('type' => 'double(24,8)', 'label' => 'PriceOfRegistration', 'enabled' => 1, 'visible' => -1, 'position' => 212),
375 'price_booth' => array('type' => 'double(24,8)', 'label' => 'PriceOfBooth', 'enabled' => 1, 'visible' => -1, 'position' => 215),
376 'max_attendees' => array('type' => 'integer', 'label' => 'MaxNbOfAttendees', 'enabled' => 1, 'visible' => -1, 'position' => 215),
377 // Generic
378 'datec' => array('type' => 'datetime', 'label' => 'DateCreationShort', 'enabled' => 1, 'visible' => -2, 'position' => 400),
379 'tms' => array('type' => 'timestamp', 'label' => 'DateModificationShort', 'enabled' => 1, 'visible' => -2, 'notnull' => 1, 'position' => 405),
380 'fk_user_creat' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserCreation', 'enabled' => 1, 'visible' => 0, 'notnull' => 1, 'position' => 410),
381 'fk_user_modif' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserModification', 'enabled' => 1, 'visible' => 0, 'position' => 415),
382 'import_key' => array('type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'visible' => -1, 'position' => 420),
383 'email_msgid' => array('type' => 'varchar(255)', 'label' => 'EmailMsgID', 'enabled' => 1, 'visible' => -1, 'position' => 450, 'help' => 'EmailMsgIDWhenSourceisEmail', 'csslist' => 'tdoverflowmax125'),
384 'fk_statut' => array('type' => 'smallint(6)', 'label' => 'Status', 'alias' => 'status', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'position' => 500, 'arrayofkeyval' => array(0 => 'Draft', 1 => 'Validated', 2 => 'Closed')),
385 );
386 // END MODULEBUILDER PROPERTIES
387
391 const STATUS_DRAFT = 0;
392
397
401 const STATUS_CLOSED = 2;
402
403
409 public function __construct($db)
410 {
411 global $conf;
412
413 $this->db = $db;
414
415 $this->ismultientitymanaged = 1;
416 $this->isextrafieldmanaged = 1;
417
418 $this->labelStatusShort = array(0 => 'Draft', 1 => 'Opened', 2 => 'Closed');
419 $this->labelStatus = array(0 => 'Draft', 1 => 'Opened', 2 => 'Closed');
420
421 global $conf;
422
423 if (!getDolGlobalString('MAIN_SHOW_TECHNICAL_ID')) {
424 $this->fields['rowid']['visible'] = 0;
425 }
426
427 if (!getDolGlobalString('PROJECT_USE_OPPORTUNITIES')) {
428 $this->fields['fk_opp_status']['enabled'] = 0;
429 $this->fields['opp_percent']['enabled'] = 0;
430 $this->fields['opp_amount']['enabled'] = 0;
431 $this->fields['usage_opportunity']['enabled'] = 0;
432 }
433
434 if (getDolGlobalString('PROJECT_HIDE_TASKS')) {
435 $this->fields['usage_bill_time']['visible'] = 0;
436 $this->fields['usage_task']['visible'] = 0;
437 }
438
439 if (empty($conf->eventorganization->enabled)) {
440 $this->fields['usage_organize_event']['visible'] = 0;
441 $this->fields['accept_conference_suggestions']['enabled'] = 0;
442 $this->fields['accept_booth_suggestions']['enabled'] = 0;
443 $this->fields['price_registration']['enabled'] = 0;
444 $this->fields['price_booth']['enabled'] = 0;
445 $this->fields['max_attendees']['enabled'] = 0;
446 }
447 }
448
456 public function create($user, $notrigger = 0)
457 {
458 $error = 0;
459 $ret = 0;
460
461 $now = dol_now();
462
463 // Clean parameters
464 $this->note_private = dol_substr($this->note_private, 0, 65535);
465 $this->note_public = dol_substr($this->note_public, 0, 65535);
466
467 // Check parameters
468 if (!trim($this->ref)) {
469 $this->error = 'ErrorFieldsRequired';
470 dol_syslog(get_class($this)."::create error -1 ref null", LOG_ERR);
471 return -1;
472 }
473 if (getDolGlobalString('PROJECT_THIRDPARTY_REQUIRED') && !($this->socid > 0)) {
474 $this->error = 'ErrorFieldsRequired';
475 dol_syslog(get_class($this)."::create error -1 thirdparty not defined and option PROJECT_THIRDPARTY_REQUIRED is set", LOG_ERR);
476 return -1;
477 }
478
479 // Create project
480 $this->db->begin();
481
482 $sql = "INSERT INTO ".MAIN_DB_PREFIX."projet (";
483 $sql .= "ref";
484 $sql .= ", fk_project";
485 $sql .= ", title";
486 $sql .= ", description";
487 $sql .= ", fk_soc";
488 $sql .= ", fk_user_creat";
489 $sql .= ", fk_statut";
490 $sql .= ", fk_opp_status";
491 $sql .= ", opp_percent";
492 $sql .= ", public";
493 $sql .= ", datec";
494 $sql .= ", dateo";
495 $sql .= ", datee";
496 $sql .= ", opp_amount";
497 $sql .= ", budget_amount";
498 $sql .= ", usage_opportunity";
499 $sql .= ", usage_task";
500 $sql .= ", usage_bill_time";
501 $sql .= ", usage_organize_event";
502 $sql .= ", accept_conference_suggestions";
503 $sql .= ", accept_booth_suggestions";
504 $sql .= ", price_registration";
505 $sql .= ", price_booth";
506 $sql .= ", max_attendees";
507 $sql .= ", date_start_event";
508 $sql .= ", date_end_event";
509 $sql .= ", location";
510 $sql .= ", email_msgid";
511 $sql .= ", note_private";
512 $sql .= ", note_public";
513 $sql .= ", entity";
514 $sql .= ", ip";
515 $sql .= ") VALUES (";
516 $sql .= "'".$this->db->escape($this->ref)."'";
517 $sql .= ", ".($this->fk_project ? ((int) $this->fk_project) : "null");
518 $sql .= ", '".$this->db->escape($this->title)."'";
519 $sql .= ", '".$this->db->escape($this->description)."'";
520 $sql .= ", ".($this->socid > 0 ? ((int) $this->socid) : "null");
521 $sql .= ", ".((int) $user->id);
522 $sql .= ", ".(is_numeric($this->status) ? ((int) $this->status) : '0');
523 $sql .= ", ".((is_numeric($this->opp_status) && $this->opp_status > 0) ? ((int) $this->opp_status) : 'NULL');
524 $sql .= ", ".(is_numeric($this->opp_percent) ? ((int) $this->opp_percent) : 'NULL');
525 $sql .= ", ".($this->public ? 1 : 0);
526 $sql .= ", '".$this->db->idate($now)."'";
527 $sql .= ", ".($this->date_start != '' ? "'".$this->db->idate($this->date_start)."'" : 'null');
528 $sql .= ", ".($this->date_end != '' ? "'".$this->db->idate($this->date_end)."'" : 'null');
529 $sql .= ", ".(strcmp($this->opp_amount, '') ? price2num($this->opp_amount) : 'null');
530 $sql .= ", ".(strcmp((string) $this->budget_amount, '') ? price2num($this->budget_amount) : 'null');
531 $sql .= ", ".($this->usage_opportunity ? 1 : 0);
532 $sql .= ", ".($this->usage_task ? 1 : 0);
533 $sql .= ", ".($this->usage_bill_time ? 1 : 0);
534 $sql .= ", ".($this->usage_organize_event ? 1 : 0);
535 $sql .= ", ".($this->accept_conference_suggestions ? 1 : 0);
536 $sql .= ", ".($this->accept_booth_suggestions ? 1 : 0);
537 $sql .= ", ".(strcmp((string) $this->price_registration, '') ? price2num($this->price_registration) : 'null');
538 $sql .= ", ".(strcmp((string) $this->price_booth, '') ? price2num($this->price_booth) : 'null');
539 $sql .= ", ".(strcmp((string) $this->max_attendees, '') ? ((int) $this->max_attendees) : 'null');
540 $sql .= ", ".($this->date_start_event != '' ? "'".$this->db->idate($this->date_start_event)."'" : 'null');
541 $sql .= ", ".($this->date_end_event != '' ? "'".$this->db->idate($this->date_end_event)."'" : 'null');
542 $sql .= ", ".($this->location ? "'".$this->db->escape($this->location)."'" : 'null');
543 $sql .= ", ".($this->email_msgid ? "'".$this->db->escape($this->email_msgid)."'" : 'null');
544 $sql .= ", ".($this->note_private ? "'".$this->db->escape($this->note_private)."'" : 'null');
545 $sql .= ", ".($this->note_public ? "'".$this->db->escape($this->note_public)."'" : 'null');
546 $sql .= ", ".setEntity($this);
547 $sql .= ", ".(!isset($this->ip) ? 'NULL' : "'".$this->db->escape($this->ip)."'");
548 $sql .= ")";
549
550 dol_syslog(get_class($this)."::create", LOG_DEBUG);
551 $resql = $this->db->query($sql);
552 if ($resql) {
553 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."projet");
554 $ret = $this->id;
555
556 if (!$notrigger) {
557 // Call trigger
558 $result = $this->call_trigger('PROJECT_CREATE', $user);
559 if ($result < 0) {
560 $error++;
561 }
562 // End call triggers
563 }
564 } else {
565 $this->error = $this->db->lasterror();
566 $error++;
567 }
568
569 // Update extrafield
570 if (!$error) {
571 $result = $this->insertExtraFields();
572 if ($result < 0) {
573 $error++;
574 }
575 }
576
577 if (!$error && (getDolGlobalString('MAIN_DISABLEDRAFTSTATUS') || getDolGlobalString('MAIN_DISABLEDRAFTSTATUS_PROJECT'))) {
578 $res = $this->setValid($user);
579 if ($res < 0) {
580 $error++;
581 }
582 }
583
584 if (!$error) {
585 $this->db->commit();
586 return $ret;
587 } else {
588 $this->db->rollback();
589 return -1;
590 }
591 }
592
600 public function update($user, $notrigger = 0)
601 {
602 global $langs, $conf;
603
604 $error = 0;
605
606 // Clean parameters
607 $this->title = trim($this->title);
608 $this->description = trim($this->description);
609 if ($this->opp_amount < 0) {
610 $this->opp_amount = '';
611 }
612 if ($this->opp_percent < 0) {
613 $this->opp_percent = '';
614 }
615 if ($this->date_end && $this->date_end < $this->date_start) {
616 $langs->load("errors");
617 $this->error = $langs->trans("ErrorStartDateGreaterEnd");
618 $this->errors[] = $this->error;
619 $this->db->rollback();
620 dol_syslog(get_class($this)."::update error -3 ".$this->error, LOG_ERR);
621 return -3;
622 }
623
624 $this->entity = ((isset($this->entity) && is_numeric($this->entity)) ? $this->entity : $conf->entity);
625
626 if (dol_strlen(trim($this->ref)) > 0) {
627 $this->db->begin();
628
629 $sql = "UPDATE ".MAIN_DB_PREFIX."projet SET";
630 $sql .= " ref='".$this->db->escape($this->ref)."'";
631 $sql .= ", fk_project=".($this->fk_project ? ((int) $this->fk_project) : "null");
632 $sql .= ", title = '".$this->db->escape($this->title)."'";
633 $sql .= ", description = '".$this->db->escape($this->description)."'";
634 $sql .= ", fk_soc = ".($this->socid > 0 ? ((int) $this->socid) : "null");
635 $sql .= ", fk_statut = ".((int) $this->status);
636 $sql .= ", fk_opp_status = ".((is_numeric($this->opp_status) && $this->opp_status > 0) ? ((int) $this->opp_status) : 'null');
637 $sql .= ", opp_percent = ".((is_numeric($this->opp_percent) && $this->opp_percent != '') ? ((float) $this->opp_percent) : 'null');
638 $sql .= ", public = ".($this->public ? 1 : 0);
639 $sql .= ", datec = ".($this->date_c != '' ? "'".$this->db->idate($this->date_c)."'" : 'null');
640 $sql .= ", dateo = ".($this->date_start != '' ? "'".$this->db->idate($this->date_start)."'" : 'null');
641 $sql .= ", datee = ".($this->date_end != '' ? "'".$this->db->idate($this->date_end)."'" : 'null');
642 $sql .= ", date_close = ".($this->date_close != '' ? "'".$this->db->idate($this->date_close)."'" : 'null');
643 $sql .= ", note_public = ".($this->note_public ? "'".$this->db->escape($this->note_public)."'" : "null");
644 $sql .= ", note_private = ".($this->note_private ? "'".$this->db->escape($this->note_private)."'" : "null");
645 $sql .= ", fk_user_close = ".($this->fk_user_close > 0 ? ((int) $this->fk_user_close) : "null");
646 $sql .= ", opp_amount = ".(strcmp($this->opp_amount, '') ? price2num($this->opp_amount) : "null");
647 $sql .= ", budget_amount = ".(strcmp($this->budget_amount, '') ? price2num($this->budget_amount) : "null");
648 $sql .= ", fk_user_modif = ".((int) $user->id);
649 $sql .= ", usage_opportunity = ".($this->usage_opportunity ? 1 : 0);
650 $sql .= ", usage_task = ".($this->usage_task ? 1 : 0);
651 $sql .= ", usage_bill_time = ".($this->usage_bill_time ? 1 : 0);
652 $sql .= ", usage_organize_event = ".($this->usage_organize_event ? 1 : 0);
653 $sql .= ", accept_conference_suggestions = ".($this->accept_conference_suggestions ? 1 : 0);
654 $sql .= ", accept_booth_suggestions = ".($this->accept_booth_suggestions ? 1 : 0);
655 $sql .= ", price_registration = ".(isset($this->price_registration) && strcmp($this->price_registration, '') ? price2num($this->price_registration) : "null");
656 $sql .= ", price_booth = ".(isset($this->price_booth) && strcmp((string) $this->price_booth, '') ? price2num($this->price_booth) : "null");
657 $sql .= ", max_attendees = ".(strcmp((string) $this->max_attendees, '') ? (int) $this->max_attendees : "null");
658 $sql .= ", date_start_event = ".($this->date_start_event != '' ? "'".$this->db->idate($this->date_start_event)."'" : 'null');
659 $sql .= ", date_end_event = ".($this->date_end_event != '' ? "'".$this->db->idate($this->date_end_event)."'" : 'null');
660 $sql .= ", location = '".$this->db->escape($this->location)."'";
661 $sql .= ", entity = ".((int) $this->entity);
662 $sql .= " WHERE rowid = ".((int) $this->id);
663
664 dol_syslog(get_class($this)."::update", LOG_DEBUG);
665 $resql = $this->db->query($sql);
666 if ($resql) {
667 // Update extrafield
668 if (!$error) {
669 $result = $this->insertExtraFields();
670 if ($result < 0) {
671 $error++;
672 }
673 }
674
675 if (!$error && !$notrigger) {
676 // Call trigger
677 $result = $this->call_trigger('PROJECT_MODIFY', $user);
678 if ($result < 0) {
679 $error++;
680 }
681 // End call triggers
682 }
683
684 if (!$error && (is_object($this->oldcopy) && $this->oldcopy->ref !== $this->ref)) {
685 // We remove directory
686 if ($conf->project->dir_output) {
687 $olddir = $conf->project->dir_output."/".dol_sanitizeFileName($this->oldcopy->ref);
688 $newdir = $conf->project->dir_output."/".dol_sanitizeFileName($this->ref);
689 if (file_exists($olddir)) {
690 include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
691 $res = @rename($olddir, $newdir);
692 if (!$res) {
693 $langs->load("errors");
694 $this->error = $langs->trans('ErrorFailToRenameDir', $olddir, $newdir);
695 $error++;
696 }
697 }
698 }
699 }
700 if (!$error) {
701 $this->db->commit();
702 $result = 1;
703 } else {
704 $this->db->rollback();
705 $result = -1;
706 }
707 } else {
708 $this->error = $this->db->lasterror();
709 $this->errors[] = $this->error;
710 $this->db->rollback();
711 if ($this->db->lasterrno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
712 $result = -4;
713 } else {
714 $result = -2;
715 }
716 dol_syslog(get_class($this)."::update error ".$result." ".$this->error, LOG_ERR);
717 }
718 } else {
719 dol_syslog(get_class($this)."::update ref null");
720 $result = -1;
721 }
722
723 return $result;
724 }
725
735 public function fetch($id, $ref = '', $ref_ext = '', $email_msgid = '')
736 {
737 if (empty($id) && empty($ref) && empty($ref_ext) && empty($email_msgid)) {
738 dol_syslog(get_class($this)."::fetch Bad parameters", LOG_WARNING);
739 return -1;
740 }
741
742 $sql = "SELECT rowid, entity, fk_project, ref, title, description, public, datec, opp_amount, budget_amount,";
743 $sql .= " tms, dateo as date_start, datee as date_end, date_close, fk_soc, fk_user_creat, fk_user_modif, fk_user_close, fk_statut as status, fk_opp_status, opp_percent,";
744 $sql .= " note_private, note_public, model_pdf, usage_opportunity, usage_task, usage_bill_time, usage_organize_event, email_msgid,";
745 $sql .= " accept_conference_suggestions, accept_booth_suggestions, price_registration, price_booth, max_attendees, date_start_event, date_end_event, location, extraparams";
746 $sql .= " FROM ".MAIN_DB_PREFIX."projet";
747 if (!empty($id)) {
748 $sql .= " WHERE rowid = ".((int) $id);
749 } else {
750 $sql .= " WHERE entity IN (".getEntity('project').")";
751 if (!empty($ref)) {
752 $sql .= " AND ref = '".$this->db->escape($ref)."'";
753 } elseif (!empty($ref_ext)) {
754 $sql .= " AND ref_ext = '".$this->db->escape($ref_ext)."'";
755 } else {
756 $sql .= " AND email_msgid = '".$this->db->escape($email_msgid)."'";
757 }
758 }
759
760 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
761 $resql = $this->db->query($sql);
762 if ($resql) {
763 $num_rows = $this->db->num_rows($resql);
764
765 if ($num_rows) {
766 $obj = $this->db->fetch_object($resql);
767
768 $this->id = $obj->rowid;
769 $this->entity = $obj->entity;
770 $this->ref = $obj->ref;
771 $this->fk_project = $obj->fk_project;
772 $this->title = $obj->title;
773 $this->description = $obj->description;
774 $this->date_c = $this->db->jdate($obj->datec);
775 $this->datec = $this->db->jdate($obj->datec); // TODO deprecated
776 $this->date_m = $this->db->jdate($obj->tms);
777 $this->datem = $this->db->jdate($obj->tms); // TODO deprecated
778 $this->date_start = $this->db->jdate($obj->date_start);
779 $this->date_end = $this->db->jdate($obj->date_end);
780 $this->date_close = $this->db->jdate($obj->date_close);
781 $this->note_private = $obj->note_private;
782 $this->note_public = $obj->note_public;
783 $this->socid = $obj->fk_soc;
784 $this->user_author_id = $obj->fk_user_creat;
785 $this->user_modification_id = $obj->fk_user_modif;
786 $this->user_closing_id = $obj->fk_user_close;
787 $this->public = $obj->public;
788 $this->statut = $obj->status; // deprecated
789 $this->status = $obj->status;
790 $this->opp_status = $obj->fk_opp_status;
791 $this->opp_amount = $obj->opp_amount;
792 $this->opp_percent = $obj->opp_percent;
793 $this->budget_amount = $obj->budget_amount;
794 $this->model_pdf = $obj->model_pdf;
795 $this->usage_opportunity = (int) $obj->usage_opportunity;
796 $this->usage_task = (int) $obj->usage_task;
797 $this->usage_bill_time = (int) $obj->usage_bill_time;
798 $this->usage_organize_event = (int) $obj->usage_organize_event;
799 $this->accept_conference_suggestions = (int) $obj->accept_conference_suggestions;
800 $this->accept_booth_suggestions = (int) $obj->accept_booth_suggestions;
801 $this->price_registration = $obj->price_registration;
802 $this->price_booth = $obj->price_booth;
803 $this->max_attendees = $obj->max_attendees;
804 $this->date_start_event = $this->db->jdate($obj->date_start_event);
805 $this->date_end_event = $this->db->jdate($obj->date_end_event);
806 $this->location = $obj->location;
807 $this->email_msgid = $obj->email_msgid;
808 $this->extraparams = !empty($obj->extraparams) ? (array) json_decode($obj->extraparams, true) : array();
809
810 $this->db->free($resql);
811
812 // Retrieve all extrafield
813 // fetch optionals attributes and labels
814 $this->fetch_optionals();
815
816 return 1;
817 }
818
819 $this->db->free($resql);
820
821 return 0;
822 } else {
823 $this->error = $this->db->lasterror();
824 $this->errors[] = $this->db->lasterror();
825 return -1;
826 }
827 }
828
837 public function fetchAndSetSubstitution($id, $key, $fetched = false)
838 {
839 $substitution = '';
840
841 if ($fetched === false) {
842 $res = $this->fetch($id);
843 if ($res > 0) {
844 $fetched = true;
845 }
846 }
847
848 if ($fetched === true) {
849 if ($key == '__PROJECT_ID__') {
850 $substitution = ($this->id > 0 ? $this->id : '');
851 } elseif ($key == '__PROJECT_REF__') {
852 $substitution = $this->ref;
853 } elseif ($key == '__PROJECT_NAME__') {
854 $substitution = $this->title;
855 }
856 }
857
858 return (string) $substitution;
859 }
860
861 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
873 public function get_element_list($type, $tablename, $datefieldname = '', $date_start = null, $date_end = null, $projectkey = 'fk_projet')
874 {
875 // phpcs:enable
876
877 global $hookmanager;
878
879 $elements = array();
880
881 if ($this->id <= 0) {
882 return $elements;
883 }
884
885 $ids = $this->id;
886
887 if ($type == 'agenda') {
888 $sql = "SELECT id as rowid FROM ".MAIN_DB_PREFIX."actioncomm WHERE fk_project IN (".$this->db->sanitize((string) $ids).") AND entity IN (".getEntity('agenda').")";
889 } elseif ($type == 'expensereport') {
890 $sql = "SELECT ed.rowid FROM ".MAIN_DB_PREFIX."expensereport as e, ".MAIN_DB_PREFIX."expensereport_det as ed WHERE e.rowid = ed.fk_expensereport AND e.entity IN (".getEntity('expensereport').") AND ed.fk_projet IN (".$this->db->sanitize((string) $ids).")";
891 } elseif ($type == 'project_task') {
892 $sql = "SELECT DISTINCT pt.rowid FROM ".MAIN_DB_PREFIX."projet_task as pt WHERE pt.fk_projet IN (".$this->db->sanitize((string) $ids).")";
893 } elseif ($type == 'element_time') { // Case we want to duplicate line foreach user
894 $sql = "SELECT DISTINCT pt.rowid, ptt.fk_user FROM ".MAIN_DB_PREFIX."projet_task as pt, ".MAIN_DB_PREFIX."element_time as ptt WHERE pt.rowid = ptt.fk_element AND ptt.elementtype = 'task' AND pt.fk_projet IN (".$this->db->sanitize((string) $ids).")";
895 } elseif ($type == 'stocktransfer_stocktransfer') {
896 $sql = "SELECT ms.rowid, ms.fk_user_author as fk_user FROM ".MAIN_DB_PREFIX."stocktransfer_stocktransfer as ms, ".MAIN_DB_PREFIX."entrepot as e WHERE e.rowid = ms.fk_entrepot AND e.entity IN (".getEntity('stock').") AND ms.origintype = 'project' AND ms.fk_origin IN (".$this->db->sanitize((string) $ids).") AND ms.type_mouvement = 1";
897 } elseif ($type == 'stock_mouvement') {
898 // Stock movements have no entity field (the warehouse has one), and the project is stored into fk_projet
899 $sql = "SELECT ms.rowid FROM ".MAIN_DB_PREFIX."stock_mouvement as ms, ".MAIN_DB_PREFIX."entrepot as e WHERE e.rowid = ms.fk_entrepot AND e.entity IN (".getEntity('stock').") AND ms.fk_projet IN (".$this->db->sanitize((string) $ids).")";
900 } elseif ($type == 'loan') {
901 $sql = "SELECT l.rowid, l.fk_user_author as fk_user FROM ".MAIN_DB_PREFIX."loan as l WHERE l.entity IN (".getEntity('loan').") AND l.fk_projet IN (".$this->db->sanitize((string) $ids).")";
902 } else {
903 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX.$this->db->sanitize($tablename)." WHERE ".$this->db->sanitize($projectkey)." IN (".$this->db->sanitize((string) $ids).") AND entity IN (".getEntity($type).")";
904 }
905
906 if (isDolTms($date_start) && $type == 'loan') {
907 $sql .= " AND (dateend > '".$this->db->idate((int) $date_start)."' OR dateend IS NULL)";
908 } elseif (isDolTms($date_start) && ($type != 'project_task')) { // For table project_taks, we want the filter on date apply on project_time_spent table
909 if (empty($datefieldname) && !empty($this->table_element_date)) {
910 $datefieldname = $this->table_element_date;
911 }
912 if (empty($datefieldname)) {
913 return 'Error this object has no date field defined';
914 }
915 $sql .= " AND (".$this->db->sanitize($datefieldname)." >= '".$this->db->idate((int) $date_start)."' OR ".$this->db->sanitize($datefieldname)." IS NULL)";
916 }
917
918 if (isDolTms($date_end) && $type == 'loan') {
919 $sql .= " AND (datestart < '".$this->db->idate((int) $date_end)."' OR datestart IS NULL)";
920 } elseif (isDolTms($date_end) && ($type != 'project_task')) { // For table project_taks, we want the filter on date apply on project_time_spent table
921 if (empty($datefieldname) && !empty($this->table_element_date)) {
922 $datefieldname = $this->table_element_date;
923 }
924 if (empty($datefieldname)) {
925 return 'Error this object has no date field defined';
926 }
927 $sql .= " AND (".$this->db->sanitize($datefieldname)." <= '".$this->db->idate((int) $date_end)."' OR ".$this->db->sanitize($datefieldname)." IS NULL)";
928 }
929
930 $parameters = array(
931 'sql' => $sql,
932 'type' => $type,
933 'tablename' => $tablename,
934 'datefieldname' => $datefieldname,
935 'dates' => $date_start,
936 'datee' => $date_end,
937 'fk_projet' => $projectkey,
938 'ids' => $ids,
939 );
940 $reshook = $hookmanager->executeHooks('getElementList', $parameters);
941 if ($reshook > 0) {
942 $sql = $hookmanager->resPrint;
943 } else {
944 $sql .= $hookmanager->resPrint;
945 }
946
947 if (!$sql) {
948 return -1;
949 }
950
951 //print $sql;
952 dol_syslog(get_class($this)."::get_element_list", LOG_DEBUG);
953 $result = $this->db->query($sql);
954 if ($result) {
955 $nump = $this->db->num_rows($result);
956 if ($nump) {
957 $i = 0;
958 while ($i < $nump) {
959 $obj = $this->db->fetch_object($result);
960
961 $elements[$i] = $obj->rowid.(empty($obj->fk_user) ? '' : '_'.$obj->fk_user);
962
963 $i++;
964 }
965 $this->db->free($result);
966 }
967
968 /* Return array even if empty*/
969 return $elements;
970 } else {
971 dol_print_error($this->db);
972 }
973 return -1;
974 }
975
983 public function delete($user, $notrigger = 0)
984 {
985 global $langs, $conf;
986 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
987
988 $error = 0;
989
990 $this->db->begin();
991
992 if (!$error) {
993 // Delete linked contacts
994 $res = $this->delete_linked_contact();
995 if ($res < 0) {
996 $this->error = 'ErrorFailToDeleteLinkedContact';
997 //$error++;
998 $this->db->rollback();
999 return 0;
1000 }
1001 }
1002
1003 // Set fk_projet into elements to null
1004 $listoftables = array(
1005 'propal' => 'fk_projet',
1006 'commande' => 'fk_projet',
1007 'facture' => 'fk_projet',
1008 'supplier_proposal' => 'fk_projet',
1009 'commande_fournisseur' => 'fk_projet',
1010 'facture_fourn' => 'fk_projet',
1011 'expensereport_det' => 'fk_projet',
1012 'contrat' => 'fk_projet',
1013 'fichinter' => 'fk_projet',
1014 'don' => array('field' => 'fk_projet', 'module' => 'don'),
1015 'actioncomm' => 'fk_project',
1016 'mrp_mo' => array('field' => 'fk_project', 'module' => 'mrp'),
1017 'entrepot' => 'fk_project',
1018 );
1019 foreach ($listoftables as $key => $value) {
1020 if (is_array($value)) {
1021 if (!isModEnabled($value['module'])) {
1022 continue;
1023 }
1024 $fieldname = $value['field'];
1025 } else {
1026 $fieldname = $value;
1027 }
1028 $sql = "UPDATE ".MAIN_DB_PREFIX.$this->db->sanitize($key)." SET ".$this->db->sanitize($fieldname)." = NULL WHERE ".$this->db->sanitize($fieldname)." = ".((int) $this->id);
1029
1030 $resql = $this->db->query($sql);
1031 if (!$resql) {
1032 $this->errors[] = $this->db->lasterror();
1033 $error++;
1034 break;
1035 }
1036 }
1037
1038 // Remove linked categories.
1039 if (!$error) {
1040 $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_project";
1041 $sql .= " WHERE fk_project = ".((int) $this->id);
1042
1043 $result = $this->db->query($sql);
1044 if (!$result) {
1045 $error++;
1046 $this->errors[] = $this->db->lasterror();
1047 }
1048 }
1049
1050 // Fetch tasks
1051 $this->getLinesArray($user, 0);
1052
1053 // Delete tasks
1054 $ret = $this->deleteTasks($user);
1055 if ($ret < 0) {
1056 $error++;
1057 }
1058
1059
1060 // Delete all child tables
1061 if (!$error) {
1062 $elements = array('categorie_project'); // elements to delete. TODO Make goodway to delete
1063 foreach ($elements as $table) {
1064 if (!$error) {
1065 $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->db->sanitize($table);
1066 $sql .= " WHERE fk_project = ".((int) $this->id);
1067
1068 $result = $this->db->query($sql);
1069 if (!$result) {
1070 $error++;
1071 $this->errors[] = $this->db->lasterror();
1072 }
1073 }
1074 }
1075 }
1076
1077 if (!$error) {
1078 $sql = "DELETE FROM ".MAIN_DB_PREFIX."projet_extrafields";
1079 $sql .= " WHERE fk_object = ".((int) $this->id);
1080
1081 $resql = $this->db->query($sql);
1082 if (!$resql) {
1083 $this->errors[] = $this->db->lasterror();
1084 $error++;
1085 }
1086 }
1087
1088 // Delete project
1089 if (!$error) {
1090 $sql = "DELETE FROM ".MAIN_DB_PREFIX."projet";
1091 $sql .= " WHERE rowid=".((int) $this->id);
1092
1093 $resql = $this->db->query($sql);
1094 if (!$resql) {
1095 $this->errors[] = $langs->trans("CantRemoveProject", $langs->transnoentitiesnoconv("ProjectOverview"));
1096 $error++;
1097 }
1098 }
1099
1100
1101
1102 if (empty($error)) {
1103 // We remove directory
1104 $projectref = dol_sanitizeFileName($this->ref);
1105 if ($conf->project->dir_output && !empty($projectref)) {
1106 $dir = $conf->project->dir_output."/".$projectref;
1107 if (file_exists($dir)) {
1108 $res = @dol_delete_dir_recursive($dir);
1109 if (!$res) {
1110 $this->errors[] = 'ErrorFailToDeleteDir';
1111 $error++;
1112 }
1113 }
1114 }
1115
1116 if (!$notrigger) {
1117 // Call trigger
1118 $result = $this->call_trigger('PROJECT_DELETE', $user);
1119
1120 if ($result < 0) {
1121 $error++;
1122 }
1123 // End call triggers
1124 }
1125 }
1126
1127 if (empty($error)) {
1128 $this->db->commit();
1129 return 1;
1130 } else {
1131 foreach ($this->errors as $errmsg) {
1132 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
1133 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
1134 }
1135 dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR);
1136 $this->db->rollback();
1137 return -1;
1138 }
1139 }
1140
1149 public function getElementCount($type, $tablename, $projectkey = 'fk_projet')
1150 {
1151 if ($this->id <= 0) {
1152 return 0;
1153 }
1154
1155 if ($type == 'agenda') {
1156 $sql = "SELECT COUNT(id) as nb FROM ".MAIN_DB_PREFIX."actioncomm WHERE fk_project = ".((int) $this->id)." AND entity IN (".getEntity('agenda').")";
1157 } elseif ($type == 'expensereport') {
1158 $sql = "SELECT COUNT(ed.rowid) as nb FROM ".MAIN_DB_PREFIX."expensereport as e, ".MAIN_DB_PREFIX."expensereport_det as ed WHERE e.rowid = ed.fk_expensereport AND e.entity IN (".getEntity('expensereport').") AND ed.fk_projet = ".((int) $this->id);
1159 } elseif ($type == 'project_task') {
1160 $sql = "SELECT DISTINCT COUNT(pt.rowid) as nb FROM ".MAIN_DB_PREFIX."projet_task as pt WHERE pt.fk_projet = ".((int) $this->id);
1161 } elseif ($type == 'element_time') { // Case we want to duplicate line foreach user
1162 $sql = "SELECT DISTINCT COUNT(pt.rowid) as nb FROM ".MAIN_DB_PREFIX."projet_task as pt, ".MAIN_DB_PREFIX."element_time as ptt WHERE pt.rowid = ptt.fk_element AND ptt.elementtype = 'task' AND pt.fk_projet = ".((int) $this->id);
1163 } elseif ($type == 'stock_mouvement') {
1164 $sql = "SELECT COUNT(ms.rowid) as nb FROM ".MAIN_DB_PREFIX."stock_mouvement as ms, ".MAIN_DB_PREFIX."entrepot as e WHERE e.rowid = ms.fk_entrepot AND e.entity IN (".getEntity('stock').") AND ms.origintype = 'project' AND ms.fk_origin = ".((int) $this->id)." AND ms.type_mouvement = 1";
1165 } elseif ($type == 'loan') {
1166 $sql = "SELECT COUNT(l.rowid) as nb FROM ".MAIN_DB_PREFIX."loan as l WHERE l.entity IN (".getEntity('loan').") AND l.fk_projet = ".((int) $this->id);
1167 } else {
1168 $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX.$this->db->sanitize($tablename)." WHERE ".$this->db->sanitize($projectkey)." = ".((int) $this->id)." AND entity IN (".getEntity($type).")";
1169 }
1170
1171 $result = $this->db->query($sql);
1172
1173 if (!$result) {
1174 return 0;
1175 }
1176
1177 $obj = $this->db->fetch_object($result);
1178
1179 $this->db->free($result);
1180
1181 return $obj->nb;
1182 }
1183
1190 public function deleteTasks($user)
1191 {
1192 $countTasks = count($this->lines);
1193 $deleted = false;
1194 if ($countTasks) {
1195 foreach ($this->lines as $task) {
1196 if ($task->hasChildren() <= 0) { // If there is no children (or error to detect them)
1197 $deleted = true;
1198 $ret = $task->delete($user);
1199 if ($ret <= 0) {
1200 $this->errors[] = $this->db->lasterror();
1201 return -1;
1202 }
1203 }
1204 }
1205 }
1206 $this->getLinesArray($user);
1207 if ($deleted && count($this->lines) < $countTasks) {
1208 if (count($this->lines)) {
1209 $this->deleteTasks($user);
1210 }
1211 }
1212
1213 return 1;
1214 }
1215
1223 public function setValid($user, $notrigger = 0)
1224 {
1225 global $langs;
1226
1227 $error = 0;
1228
1229 // Protection
1230 if ($this->status == self::STATUS_VALIDATED) {
1231 dol_syslog(get_class($this)."::validate action abandoned: already validated", LOG_WARNING);
1232 return 0;
1233 }
1234
1235 // Check parameters
1236 if (preg_match('/^'.preg_quote($langs->trans("CopyOf").' ', '/').'/', $this->title)) {
1237 $this->error = $langs->trans("ErrorFieldFormat", $langs->transnoentities("Label")).'. '.$langs->trans('RemoveString', $langs->transnoentitiesnoconv("CopyOf"));
1238 return -1;
1239 }
1240
1241 $this->db->begin();
1242
1243 $sql = "UPDATE ".MAIN_DB_PREFIX."projet";
1244 $sql .= " SET fk_statut = ".self::STATUS_VALIDATED;
1245 $sql .= " WHERE rowid = ".((int) $this->id);
1246 //$sql .= " AND entity = ".((int) $conf->entity); // Disabled, when we use the ID for the where, we must not add any other search condition
1247
1248 dol_syslog(get_class($this)."::setValid", LOG_DEBUG);
1249 $resql = $this->db->query($sql);
1250 if ($resql) {
1251 // Call trigger
1252 if (empty($notrigger)) {
1253 $result = $this->call_trigger('PROJECT_VALIDATE', $user);
1254 if ($result < 0) {
1255 $error++;
1256 }
1257 // End call triggers
1258 }
1259
1260 if (!$error) {
1261 $this->statut = 1;
1262 $this->status = 1;
1263 $this->db->commit();
1264 return 1;
1265 } else {
1266 $this->db->rollback();
1267 $this->error = implode(',', $this->errors);
1268 dol_syslog(get_class($this)."::setValid ".$this->error, LOG_ERR);
1269 return -1;
1270 }
1271 } else {
1272 $this->db->rollback();
1273 $this->error = $this->db->lasterror();
1274 return -1;
1275 }
1276 }
1277
1284 public function setClose($user)
1285 {
1286 $now = dol_now();
1287
1288 $error = 0;
1289
1290 if ($this->status != self::STATUS_CLOSED) {
1291 $this->db->begin();
1292
1293 $sql = "UPDATE ".MAIN_DB_PREFIX."projet";
1294 $sql .= " SET fk_statut = ".self::STATUS_CLOSED.", fk_user_close = ".((int) $user->id).", date_close = '".$this->db->idate($now)."'";
1295 $sql .= " WHERE rowid = ".((int) $this->id);
1296 $sql .= " AND fk_statut = ".self::STATUS_VALIDATED;
1297
1298 if (getDolGlobalString('PROJECT_USE_OPPORTUNITIES')) {
1299 // TODO What to do if fk_opp_status is not code 'WON' or 'LOST'
1300 }
1301
1302 dol_syslog(get_class($this)."::setClose", LOG_DEBUG);
1303 $resql = $this->db->query($sql);
1304 if ($resql) {
1305 // Call trigger
1306 $result = $this->call_trigger('PROJECT_CLOSE', $user);
1307 if ($result < 0) {
1308 $error++;
1309 }
1310 // End call triggers
1311
1312 if (!$error) {
1313 $this->status = 2;
1314 $this->db->commit();
1315 return 1;
1316 } else {
1317 $this->db->rollback();
1318 $this->error = implode(',', $this->errors);
1319 dol_syslog(get_class($this)."::setClose ".$this->error, LOG_ERR);
1320 return -1;
1321 }
1322 } else {
1323 $this->db->rollback();
1324 $this->error = $this->db->lasterror();
1325 return -1;
1326 }
1327 }
1328
1329 return 0;
1330 }
1331
1338 public function getLibStatut($mode = 0)
1339 {
1340 return $this->LibStatut(isset($this->status) ? $this->status : $this->statut, $mode);
1341 }
1342
1343 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1351 public function LibStatut($status, $mode = 0)
1352 {
1353 // phpcs:enable
1354 global $langs;
1355
1356 if (is_null($status)) {
1357 return '';
1358 }
1359
1360 $statustrans = array(
1361 0 => 'status0',
1362 1 => 'status4',
1363 2 => 'status6',
1364 );
1365
1366 $statusClass = 'status0';
1367 if (!empty($statustrans[$status])) {
1368 $statusClass = $statustrans[$status];
1369 }
1370
1371 return dolGetStatus($langs->transnoentitiesnoconv($this->labelStatus[$status]), $langs->transnoentitiesnoconv($this->labelStatusShort[$status]), '', $statusClass, $mode);
1372 }
1373
1380 public function getTooltipContentArray($params)
1381 {
1382 global $conf, $langs;
1383
1384 $langs->load('projects');
1385 $option = $params['option'] ?? '';
1386 $moreinpopup = $params['moreinpopup'] ?? '';
1387
1388 $datas = [];
1389 if ($option != 'nolink') {
1390 $datas['picto'] = img_picto('', $this->picto, 'class="pictofixedwidth"').' <u class="paddingrightonly">'.$langs->trans("Project").'</u>';
1391 }
1392 if (isset($this->status)) {
1393 $datas['picto'] .= ' '.$this->getLibStatut(5);
1394 }
1395 $datas['ref'] = (isset($datas['picto']) ? '<br>' : '').'<b>'.$langs->trans('Ref').': </b>'.$this->ref; // The space must be after the : to not being explode when showing the title in img_picto
1396 $datas['label'] = '<br><b>'.$langs->trans('Label').': </b>'.$this->title; // The space must be after the : to not being explode when showing the title in img_picto
1397 if (isset($this->public)) {
1398 $datas['visibility'] = '<br><b>'.$langs->trans("Visibility").":</b> ";
1399 $datas['visibility'] .= ($this->public ? img_picto($langs->trans('SharedProject'), 'world', 'class="pictofixedwidth"').$langs->trans("SharedProject") : img_picto($langs->trans('PrivateProject'), 'private', 'class="pictofixedwidth"').$langs->trans("PrivateProject"));
1400 }
1401 if (!empty($this->thirdparty_name)) {
1402 $datas['thirdparty'] = '<br><b>'.$langs->trans('ThirdParty').': </b>'.$this->thirdparty_name; // The space must be after the : to not being explode when showing the title in img_picto
1403 }
1404 if (!empty($this->date_start)) {
1405 $datas['datestart'] = '<br><b>'.$langs->trans('DateStart').': </b>'.dol_print_date($this->date_start, 'day'); // The space must be after the : to not being explode when showing the title in img_picto
1406 }
1407 if (!empty($this->date_end)) {
1408 $datas['dateend'] = '<br><b>'.$langs->trans('DateEnd').': </b>'.dol_print_date($this->date_end, 'day'); // The space must be after the : to not being explode when showing the title in img_picto
1409 }
1410 if ($moreinpopup) {
1411 $datas['moreinpopup'] = '<br>'.$moreinpopup;
1412 }
1413
1414 return $datas;
1415 }
1416
1424 protected function getFieldPropertyName($field)
1425 {
1426 if ($field == 'fk_opp_status') {
1427 return 'opp_status';
1428 }
1429
1430 return parent::getFieldPropertyName($field);
1431 }
1432
1440 public function ajaxOutputField($field)
1441 {
1442 global $langs;
1443
1444 if ($field == 'fk_opp_status') {
1445 if (empty($this->opp_status)) {
1446 return '';
1447 }
1448 $langs->load('projects');
1449 $code = dol_getIdFromCode($this->db, (int) $this->opp_status, 'c_lead_status', 'rowid', 'code');
1450
1451 return dol_escape_htmltag($langs->trans('OppStatus'.$code));
1452 }
1453
1454 return parent::ajaxOutputField($field);
1455 }
1456
1465 public function hasUserPermission(User $user, $mode = 'write')
1466 {
1467 if ($mode == 'read') {
1468 return $user->hasRight('projet', 'lire') || $user->hasRight('projet', 'all', 'lire');
1469 }
1470 if ($mode == 'create' || $mode == 'write') {
1471 return $user->hasRight('projet', 'creer') || $user->hasRight('projet', 'all', 'creer');
1472 }
1473 if ($mode == 'delete') {
1474 return $user->hasRight('projet', 'supprimer') || $user->hasRight('projet', 'all', 'supprimer');
1475 }
1476
1477 return false;
1478 }
1479
1495 public function getNomUrl($withpicto = 0, $option = '', $addlabel = 0, $moreinpopup = '', $sep = ' - ', $notooltip = 0, $save_lastsearch_value = -1, $morecss = '', $save_pageforbacktolist = '', $addlinktonotes = 0)
1496 {
1497 global $conf, $langs, $user, $hookmanager;
1498
1499 if (!empty($conf->dol_no_mouse_hover)) {
1500 $notooltip = 1; // Force disable tooltips
1501 }
1502
1503 $result = '';
1504 if (getDolGlobalString('PROJECT_OPEN_ALWAYS_ON_TAB')) {
1505 $option = getDolGlobalString('PROJECT_OPEN_ALWAYS_ON_TAB');
1506 }
1507 $params = [
1508 'id' => $this->id,
1509 'objecttype' => $this->element,
1510 'moreinpopup' => $moreinpopup,
1511 'option' => $option,
1512 ];
1513 $classfortooltip = 'classfortooltip';
1514 $dataparams = '';
1515 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
1516 $classfortooltip = 'classforajaxtooltip';
1517 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
1518 $label = '';
1519 } else {
1520 $label = implode($this->getTooltipContentArray($params));
1521 }
1522
1523 $url = '';
1524 if ($option != 'nolink') {
1525 $query = ['id' => $this->id];
1526 if (preg_match('/\.php$/', $option)) {
1527 $baseurl = dol_buildpath($option, 1);
1528 } elseif ($option == 'task') {
1529 $baseurl = DOL_URL_ROOT.'/projet/tasks.php';
1530 } elseif ($option == 'preview') {
1531 $baseurl = DOL_URL_ROOT.'/projet/element.php';
1532 } elseif ($option == 'eventorganization') {
1533 $baseurl = DOL_URL_ROOT.'/eventorganization/conferenceorbooth_list.php';
1534 $query = ['projectid' => $this->id];
1535 } elseif ($option == 'mailing') {
1536 $baseurl = DOL_URL_ROOT.'/comm/mailing/list.php';
1537 $query = ['projectid' => $this->id];
1538 } else {
1539 $baseurl = DOL_URL_ROOT.'/projet/card.php';
1540 }
1541 // Add param to save lastsearch_values or not
1542 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1543 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1544 $add_save_lastsearch_values = 1;
1545 }
1546 if ($add_save_lastsearch_values) {
1547 $query['save_lastsearch_values'] = 1;
1548 }
1549 if ($save_pageforbacktolist) {
1550 $query['save_pageforbacktolist'] = $save_pageforbacktolist;
1551 }
1552 $url = dolBuildUrl($baseurl, $query);
1553 }
1554
1555 $linkclose = '';
1556 if (empty($notooltip) && $user->hasRight('projet', 'lire')) {
1557 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1558 $label = $langs->trans("ShowProject");
1559 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label).'"';
1560 }
1561 $linkclose .= ($label ? ' title="'.dolPrintHTMLForAttribute($label).'"' : ' title="tocomplete"');
1562 $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
1563 } else {
1564 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1565 }
1566
1567 $picto = 'projectpub';
1568 if (!$this->public) {
1569 $picto = 'project';
1570 }
1571
1572 $linkstart = '<a href="'.$url.'"';
1573 $linkstart .= $linkclose.'>';
1574 $linkend = '</a>';
1575
1576 $result .= $linkstart;
1577 if ($withpicto) {
1578 $result .= img_object(($notooltip ? '' : $label), $picto, 'class="pictofixedwidth em088"', 0, 0, $notooltip ? 0 : 1);
1579 }
1580 if ($withpicto != 2) {
1581 if ($addlabel >= 0) {
1582 $result .= $this->ref;
1583 } else {
1584 $result .= $this->title;
1585 }
1586 }
1587 $result .= $linkend;
1588 if ($withpicto != 2) {
1589 $result .= (($addlabel > 0 && $this->title) ? '<span class="opacitymedium">'.$sep.dol_trunc($this->title, ($addlabel > 1 ? $addlabel : 0)).'</span>' : '');
1590 }
1591
1592 if ($addlinktonotes) {
1593 $txttoshow = ($user->socid > 0 ? $this->note_public : $this->note_private);
1594 if ($txttoshow) {
1595 $notetoshow = $langs->trans("ViewPrivateNote").':<br>'.dol_string_nohtmltag($txttoshow, 1);
1596 $result .= ' <span class="note inline-block">';
1597 $result .= '<a href="'.DOL_URL_ROOT.'/projet/note.php?id='.$this->id.'" class="classfortooltip" title="'.dol_escape_htmltag($notetoshow).'">';
1598 $result .= img_picto('', 'note');
1599 $result .= '</a>';
1600 $result .= '</span>';
1601 }
1602 }
1603
1604 global $action;
1605 $hookmanager->initHooks(array('projectdao'));
1606 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
1607 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1608 if ($reshook > 0) {
1609 $result = $hookmanager->resPrint;
1610 } else {
1611 $result .= $hookmanager->resPrint;
1612 }
1613
1614 return $result;
1615 }
1616
1624 public function initAsSpecimen()
1625 {
1626 global $user, $langs, $conf;
1627
1628 $now = dol_now();
1629
1630 // Initialise parameters
1631 $this->id = 0;
1632 $this->ref = 'SPECIMEN';
1633 $this->entity = $conf->entity;
1634 $this->specimen = 1;
1635 $this->socid = 1;
1636 $this->date_c = $now;
1637 $this->date_m = $now;
1638 $this->date_start = $now;
1639 $this->date_end = $now + (3600 * 24 * 365);
1640 $this->note_public = 'SPECIMEN';
1641 $this->note_private = 'Private Note';
1642 $this->fk_project = 0;
1643 $this->opp_amount = 20000;
1644 $this->budget_amount = 10000;
1645
1646 $this->usage_opportunity = 1;
1647 $this->usage_task = 1;
1648 $this->usage_bill_time = 1;
1649 $this->usage_organize_event = 1;
1650
1651 /*
1652 $nbp = mt_rand(1, 9);
1653 $xnbp = 0;
1654 while ($xnbp < $nbp)
1655 {
1656 $line = new Task($this->db);
1657 $line->fk_project = 0;
1658 $line->label = $langs->trans("Label") . " " . $xnbp;
1659 $line->description = $langs->trans("Description") . " " . $xnbp;
1660
1661 $this->lines[]=$line;
1662 $xnbp++;
1663 }
1664 */
1665
1666 return 1;
1667 }
1668
1676 public function restrictedProjectArea(User $user, $mode = 'read')
1677 {
1678 // To verify role of users
1679 $userAccess = 0;
1680 if (($mode == 'read' && $user->hasRight('projet', 'all', 'lire')) || ($mode == 'write' && $user->hasRight('projet', 'all', 'creer')) || ($mode == 'delete' && $user->hasRight('projet', 'all', 'supprimer'))) {
1681 $userAccess = 1;
1682 } elseif ($this->public && (($mode == 'read' && $user->hasRight('projet', 'lire')) || ($mode == 'write' && $user->hasRight('projet', 'creer')) || ($mode == 'delete' && $user->hasRight('projet', 'supprimer')))) {
1683 $userAccess = 1;
1684 } else { // No access due to permission to read all projects, so we check if we are a contact of project
1685 foreach (array('internal', 'external') as $source) {
1686 $userRole = $this->liste_contact(4, $source);
1687 $num = count($userRole);
1688
1689 $nblinks = 0;
1690 while ($nblinks < $num) {
1691 if ($source == 'internal' && $user->id == $userRole[$nblinks]['id']) { // $userRole[$nblinks]['id'] is id of user (llx_user) for internal contacts
1692 if ($mode == 'read' && $user->hasRight('projet', 'lire')) {
1693 $userAccess++;
1694 }
1695 if ($mode == 'write' && $user->hasRight('projet', 'creer')) {
1696 $userAccess++;
1697 }
1698 if ($mode == 'delete' && $user->hasRight('projet', 'supprimer')) {
1699 $userAccess++;
1700 }
1701 }
1702 if ($source == 'external' && $user->socid > 0 && $user->socid == $userRole[$nblinks]['socid']) { // $userRole[$nblinks]['id'] is id of contact (llx_socpeople) or external contacts
1703 if ($mode == 'read' && $user->hasRight('projet', 'lire')) {
1704 $userAccess++;
1705 }
1706 if ($mode == 'write' && $user->hasRight('projet', 'creer')) {
1707 $userAccess++;
1708 }
1709 if ($mode == 'delete' && $user->hasRight('projet', 'supprimer')) {
1710 $userAccess++;
1711 }
1712 }
1713 $nblinks++;
1714 }
1715 }
1716 //if (empty($nblinks)) // If nobody has permission, we grant creator
1717 //{
1718 // if ((!empty($this->user_author_id) && $this->user_author_id == $user->id))
1719 // {
1720 // $userAccess = 1;
1721 // }
1722 //}
1723 }
1724
1725 return ($userAccess ? $userAccess : -1);
1726 }
1727
1738 public function getProjectsAuthorizedForUser($fuser, $mode = 0, $list = 0, $socid = 0, $filter = '')
1739 {
1740 $projects = array();
1741 $temp = array();
1742
1743 // Get id of types of contacts for projects (This list never contains a lot of elements)
1744 $listofprojectcontacttype = array();
1745 $sql2 = "SELECT ctc.rowid, ctc.code FROM ".MAIN_DB_PREFIX."c_type_contact as ctc";
1746 $sql2 .= " WHERE ctc.element = '".$this->db->escape($this->element)."'";
1747 $sql2 .= " AND ctc.source = 'internal'";
1748 $resql = $this->db->query($sql2);
1749 if ($resql) {
1750 while ($obj = $this->db->fetch_object($resql)) {
1751 $listofprojectcontacttype[$obj->rowid] = $obj->code;
1752 }
1753 } else {
1754 dol_print_error($this->db);
1755 }
1756 if (count($listofprojectcontacttype) == 0) {
1757 $listofprojectcontacttype[0] = '0'; // To avoid syntax error if not found
1758 }
1759
1760
1761 $sql = "SELECT p.rowid, p.ref";
1762 $sql .= " FROM ".MAIN_DB_PREFIX."projet as p";
1763 $sql .= " WHERE p.entity IN (".getEntity('project').")";
1764 // Internal users must see project he is contact to even if project is linked to a third party he can't see.
1765 if ($socid > 0) {
1766 $sql .= " AND (p.fk_soc IS NULL OR p.fk_soc = 0 OR p.fk_soc = ".((int) $socid).")";
1767 }
1768
1769 if ($mode == 0) {
1770 $sql .= " AND (p.public = 1";
1771 $sql .= " OR EXISTS (SELECT ec.rowid FROM ".MAIN_DB_PREFIX."element_contact as ec";
1772 $sql .= " WHERE ec.element_id = p.rowid AND ec.fk_c_type_contact IN (".$this->db->sanitize(implode(',', array_keys($listofprojectcontacttype))).")";
1773 $sql .= " AND ec.fk_socpeople = ".((int) $fuser->id).")";
1774 $sql .= ")";
1775 } elseif ($mode == 1) {
1776 $sql .= " AND EXISTS (SELECT ec.rowid FROM ".MAIN_DB_PREFIX."element_contact as ec";
1777 $sql .= " WHERE ec.element_id = p.rowid AND ec.fk_c_type_contact IN (".$this->db->sanitize(implode(',', array_keys($listofprojectcontacttype))).")";
1778 $sql .= " AND ec.fk_socpeople = ".((int) $fuser->id).")";
1779 } // elseif ($mode == 2) {
1780 // No filter. Use this if fuser has permission to see all project
1781 //}
1782
1783 // Manage filter
1784 $errormessage = '';
1785 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
1786 if ($errormessage) {
1787 $this->errors[] = $errormessage;
1788 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
1789 $sql .= $filter; // @phan-suppress-current-line SqlInjection
1790 }
1791
1792 $resql = $this->db->query($sql);
1793 if ($resql) {
1794 $num = $this->db->num_rows($resql);
1795 $i = 0;
1796 while ($i < $num) {
1797 $row = $this->db->fetch_row($resql);
1798 $projects[$row[0]] = $row[1];
1799 $temp[] = $row[0];
1800 $i++;
1801 }
1802
1803 $this->db->free($resql);
1804
1805 if ($list) {
1806 if (empty($temp)) {
1807 return '0';
1808 }
1809 $result = implode(',', $temp);
1810 return $result;
1811 }
1812 } else {
1813 dol_print_error($this->db);
1814 }
1815
1816 return $projects;
1817 }
1818
1834 public function createFromClone(User $user, $fromid, $clone_contact = false, $clone_task = true, $clone_project_file = false, $clone_task_file = false, $clone_note = true, $move_date = true, $notrigger = 0, $newthirdpartyid = 0)
1835 {
1836 global $langs, $conf;
1837
1838 $error = 0;
1839 $clone_project_id = 0; // For static toolcheck
1840
1841 dol_syslog("createFromClone clone_contact=".json_encode($clone_contact)." clone_task=".json_encode($clone_task)." clone_project_file=".json_encode($clone_project_file)." clone_note=".json_encode($clone_note)." move_date=".json_encode($move_date), LOG_DEBUG);
1842
1843 $now = dol_mktime(0, 0, 0, idate('m', dol_now()), idate('d', dol_now()), idate('Y', dol_now()));
1844
1845 $clone_project = new Project($this->db);
1846
1847 $clone_project->context['createfromclone'] = 'createfromclone';
1848
1849 $this->db->begin();
1850
1851 // Load source object
1852 $clone_project->fetch($fromid);
1853 $clone_project->fetch_optionals();
1854 $clone_project->socid = ($newthirdpartyid > 0 ? $newthirdpartyid : 0);
1855 $clone_project->fetch_thirdparty();
1856
1857 $orign_dt_start = $clone_project->date_start;
1858 $orign_project_ref = $clone_project->ref;
1859
1860 $clone_project->id = 0;
1861 if ($move_date) {
1862 $clone_project->date_start = $now;
1863 if (!empty($clone_project->date_end)) {
1864 if (!empty($orign_dt_start)) {
1865 $clone_project->date_end += ($now - (int) $orign_dt_start);
1866 } elseif (!empty($clone_project->date_c)) {
1867 $clone_project->date_end += ($now - (int) $clone_project->date_c);
1868 }
1869 }
1870 }
1871
1872 $clone_project->date_c = $now;
1873
1874 if (!$clone_note) {
1875 $clone_project->note_private = '';
1876 $clone_project->note_public = '';
1877 }
1878
1879 //Generate next ref
1880 $defaultref = '';
1881 $obj = getDolGlobalString('PROJECT_ADDON', 'mod_project_simple');
1882 // Search template files
1883 $file = '';
1884 $classname = '';
1885 $filefound = 0;
1886 $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
1887 foreach ($dirmodels as $reldir) {
1888 $file = dol_buildpath($reldir."core/modules/project/".$obj.'.php', 0);
1889 if (file_exists($file)) {
1890 $filefound = 1;
1891 dol_include_once($reldir."core/modules/project/".$obj.'.php');
1892 $modProject = new $obj();
1893 '@phan-var-force ModeleNumRefProjects $modProject';
1894 $defaultref = $modProject->getNextValue(is_object($clone_project->thirdparty) ? $clone_project->thirdparty : null, $clone_project);
1895 break;
1896 }
1897 }
1898 if (is_numeric($defaultref) && $defaultref <= 0) {
1899 $defaultref = '';
1900 }
1901
1902 $clone_project->ref = $defaultref;
1903 $clone_project->title = $langs->trans("CopyOf").' '.$clone_project->title;
1904
1905 // Create clone
1906 $result = $clone_project->create($user, $notrigger);
1907
1908 // Other options
1909 if ($result < 0) {
1910 $this->error .= $clone_project->error;
1911 $error++;
1912 }
1913
1914 if (!$error) {
1915 //Get the new project id
1916 $clone_project_id = $clone_project->id;
1917
1918 //Note Update
1919 if (!$clone_note) {
1920 $clone_project->note_private = '';
1921 $clone_project->note_public = '';
1922 } else {
1923 $this->db->begin();
1924 $res = $clone_project->update_note(dol_html_entity_decode($clone_project->note_public, ENT_QUOTES | ENT_HTML5), '_public');
1925 if ($res < 0) {
1926 $this->error .= $clone_project->error;
1927 $error++;
1928 $this->db->rollback();
1929 } else {
1930 $this->db->commit();
1931 }
1932
1933 $this->db->begin();
1934 $res = $clone_project->update_note(dol_html_entity_decode($clone_project->note_private, ENT_QUOTES | ENT_HTML5), '_private');
1935 if ($res < 0) {
1936 $this->error .= $clone_project->error;
1937 $error++;
1938 $this->db->rollback();
1939 } else {
1940 $this->db->commit();
1941 }
1942 }
1943
1944 //Duplicate contact
1945 if ($clone_contact) {
1946 $origin_project = new Project($this->db);
1947 $origin_project->fetch($fromid);
1948
1949 foreach (array('internal', 'external') as $source) {
1950 $tab = $origin_project->liste_contact(-1, $source);
1951 if (is_array($tab) && count($tab) > 0) {
1952 foreach ($tab as $contacttoadd) {
1953 $clone_project->add_contact($contacttoadd['id'], $contacttoadd['code'], $contacttoadd['source'], $notrigger);
1954 if ($clone_project->error == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
1955 $langs->load("errors");
1956 $this->error .= $langs->trans("ErrorThisContactIsAlreadyDefinedAsThisType");
1957 $error++;
1958 } else {
1959 if ($clone_project->error != '') {
1960 $this->error .= $clone_project->error;
1961 $error++;
1962 }
1963 }
1964 }
1965 } elseif ($tab < 0) {
1966 $this->error .= $origin_project->error;
1967 $error++;
1968 }
1969 }
1970 }
1971
1972 //Duplicate file
1973 if ($clone_project_file) {
1974 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1975
1976 $clone_project_dir = $conf->project->dir_output."/".dol_sanitizeFileName($defaultref);
1977 $ori_project_dir = $conf->project->dir_output."/".dol_sanitizeFileName($orign_project_ref);
1978
1979 if (dol_mkdir($clone_project_dir) >= 0) {
1980 $filearray = dol_dir_list($ori_project_dir, "files", 0, '', '(\.meta|_preview.*\.png)$', '', SORT_ASC, 1);
1981 foreach ($filearray as $key => $file) {
1982 $rescopy = dol_copy($ori_project_dir.'/'.$file['name'], $clone_project_dir.'/'.$file['name'], '0', 1);
1983 if (is_numeric($rescopy) && $rescopy < 0) {
1984 $this->error .= $langs->trans("ErrorFailToCopyFile", $ori_project_dir.'/'.$file['name'], $clone_project_dir.'/'.$file['name']);
1985 $error++;
1986 }
1987 }
1988 } else {
1989 $this->error .= $langs->trans('ErrorInternalErrorDetected').':dol_mkdir';
1990 $error++;
1991 }
1992 }
1993
1994 //Duplicate task
1995 if ($clone_task) {
1996 require_once DOL_DOCUMENT_ROOT.'/projet/class/task.class.php';
1997
1998 $taskstatic = new Task($this->db);
1999
2000 // Security check
2001 $socid = 0;
2002 if ($user->socid > 0) {
2003 $socid = $user->socid;
2004 }
2005
2006 $tasksarray = $taskstatic->getTasksArray(null, null, $fromid, $socid, 0);
2007
2008 $tab_conv_child_parent = array();
2009 $result_clone = 0;
2010
2011 // Loop on each task, to clone it
2012 foreach ($tasksarray as $tasktoclone) {
2013 $result_clone = $taskstatic->createFromClone($user, $tasktoclone->id, $clone_project_id, $tasktoclone->fk_task_parent, $move_date, true, false, $clone_task_file, true, false);
2014 if ($result_clone <= 0) {
2015 $this->error .= $taskstatic->error;
2016 $error++;
2017 } else {
2018 $new_task_id = $result_clone;
2019 $taskstatic->fetch($tasktoclone->id);
2020
2021 //manage new parent clone task id
2022 // if the current task has child we store the original task id and the equivalent clone task id
2023 if (($taskstatic->hasChildren()) && !array_key_exists($tasktoclone->id, $tab_conv_child_parent)) {
2024 $tab_conv_child_parent[$tasktoclone->id] = $new_task_id;
2025 }
2026 }
2027 }
2028
2029 //Parse all clone node to be sure to update new parent
2030 $tasksarray = $taskstatic->getTasksArray(null, null, $clone_project_id, $socid, 0);
2031 foreach ($tasksarray as $task_cloned) {
2032 $taskstatic->fetch($task_cloned->id);
2033 if ($taskstatic->fk_task_parent != 0) {
2034 $taskstatic->fk_task_parent = $tab_conv_child_parent[$taskstatic->fk_task_parent];
2035 }
2036 $res = $taskstatic->update($user, $notrigger);
2037 if ($result_clone <= 0) {
2038 $this->error .= $taskstatic->error;
2039 $error++;
2040 }
2041 }
2042 }
2043 }
2044
2045 unset($clone_project->context['createfromclone']);
2046
2047 if (!$error && $clone_project_id != 0) {
2048 $this->db->commit();
2049 return $clone_project_id;
2050 } else {
2051 $this->db->rollback();
2052 dol_syslog(get_class($this)."::createFromClone nbError: ".$error." error : ".$this->error, LOG_ERR);
2053 return -1;
2054 }
2055 }
2056
2057
2064 public function shiftTaskDate($old_project_dt_start)
2065 {
2066 global $user;
2067
2068 $error = 0;
2069 $result = 0;
2070
2071 $taskstatic = new Task($this->db);
2072
2073 // Security check
2074 $socid = 0;
2075 if ($user->socid > 0) {
2076 $socid = $user->socid;
2077 }
2078
2079 $tasksarray = $taskstatic->getTasksArray(null, null, $this->id, $socid, 0);
2080
2081 foreach ($tasksarray as $tasktoshiftdate) {
2082 $to_update = false;
2083 // Fetch only if update of date will be made
2084 if ((!empty($tasktoshiftdate->date_start)) || (!empty($tasktoshiftdate->date_end))) {
2085 //dol_syslog(get_class($this)."::shiftTaskDate to_update", LOG_DEBUG);
2086 $to_update = true;
2087 $task = new Task($this->db);
2088 $result = $task->fetch($tasktoshiftdate->id);
2089 if (!$result) {
2090 $error++;
2091 $this->error .= $task->error;
2092 }
2093 } else {
2094 continue;
2095 }
2096 //print "$this->date_start + $tasktoshiftdate->date_start - $old_project_dt_start";exit;
2097
2098 //Calculate new task start date with difference between old proj start date and origin task start date
2099 if (!empty($tasktoshiftdate->date_start)) {
2100 $task->date_start = $this->date_start + ($tasktoshiftdate->date_start - $old_project_dt_start);
2101 }
2102
2103 //Calculate new task end date with difference between origin proj end date and origin task end date
2104 if (!empty($tasktoshiftdate->date_end)) {
2105 $task->date_end = $this->date_start + ($tasktoshiftdate->date_end - $old_project_dt_start);
2106 }
2107
2108 if ($to_update) {
2109 $result = $task->update($user);
2110 if (!$result) {
2111 $error++;
2112 $this->error .= $task->error;
2113 }
2114 }
2115 }
2116 if ($error != 0) {
2117 return -1;
2118 }
2119 return $result;
2120 }
2121
2122
2123 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2131 public function update_element($tableName, $elementSelectId)
2132 {
2133 // phpcs:enable
2134 $sql = "UPDATE ".MAIN_DB_PREFIX.$this->db->sanitize($tableName);
2135
2136 if ($tableName == "actioncomm") {
2137 $sql .= " SET fk_project = ".((int) $this->id);
2138 $sql .= " WHERE id = ".((int) $elementSelectId);
2139 } elseif (in_array($tableName, ["entrepot", "mrp_mo", "stocktransfer_stocktransfer"])) {
2140 $sql .= " SET fk_project = ".((int) $this->id);
2141 $sql .= " WHERE rowid = ".((int) $elementSelectId);
2142 } else {
2143 $sql .= " SET fk_projet = ".((int) $this->id);
2144 $sql .= " WHERE rowid = ".((int) $elementSelectId);
2145 }
2146
2147 dol_syslog(get_class($this)."::update_element", LOG_DEBUG);
2148
2149 $resql = $this->db->query($sql);
2150 if (!$resql) {
2151 $this->error = $this->db->lasterror();
2152 return -1;
2153 } else {
2154 return 1;
2155 }
2156 }
2157
2158 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2167 public function remove_element($tableName, $elementSelectId, $projectfield = 'fk_projet')
2168 {
2169 // phpcs:enable
2170 $sql = "UPDATE ".MAIN_DB_PREFIX.$this->db->sanitize($tableName);
2171
2172 if ($tableName == "actioncomm") {
2173 $sql .= " SET fk_project = NULL";
2174 $sql .= " WHERE id = ".((int) $elementSelectId);
2175 $sql .= " AND fk_project = ".((int) $this->id);
2176 } else {
2177 $sql .= " SET ".$this->db->sanitize($projectfield)." = NULL";
2178 $sql .= " WHERE rowid = ".((int) $elementSelectId);
2179 $sql .= " AND ".$this->db->sanitize($projectfield)." = ".((int) $this->id);
2180 }
2181
2182 dol_syslog(get_class($this)."::remove_element", LOG_DEBUG);
2183
2184 $resql = $this->db->query($sql);
2185 if (!$resql) {
2186 $this->error = $this->db->lasterror();
2187 return -1;
2188 } else {
2189 return 1;
2190 }
2191 }
2192
2203 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
2204 {
2205 global $conf, $langs;
2206
2207 $langs->load("projects");
2208
2209 if (!dol_strlen($modele)) {
2210 $modele = 'baleine';
2211
2212 if ($this->model_pdf) {
2213 $modele = $this->model_pdf;
2214 } elseif (getDolGlobalString('PROJECT_ADDON_PDF')) {
2215 $modele = getDolGlobalString('PROJECT_ADDON_PDF');
2216 }
2217 }
2218
2219 $modelpath = "core/modules/project/doc/";
2220
2221 return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
2222 }
2223
2224
2234 public function loadTimeSpent($datestart, $taskid = 0, $userid = 0)
2235 {
2236 $this->weekWorkLoad = array();
2237 $this->weekWorkLoadPerTask = array();
2238
2239 if (empty($datestart)) {
2240 dol_print_error(null, 'Error datestart parameter is empty');
2241 }
2242
2243 $sql = "SELECT ptt.rowid as taskid, ptt.element_duration, ptt.element_date, ptt.element_datehour, ptt.fk_element";
2244 $sql .= " FROM ".MAIN_DB_PREFIX."element_time AS ptt, ".MAIN_DB_PREFIX."projet_task as pt";
2245 $sql .= " WHERE ptt.fk_element = pt.rowid";
2246 $sql .= " AND ptt.elementtype = 'task'";
2247 $sql .= " AND pt.fk_projet = ".((int) $this->id);
2248 $sql .= " AND (ptt.element_date >= '".$this->db->idate($datestart)."' ";
2249 $sql .= " AND ptt.element_date <= '".$this->db->idate(dol_time_plus_duree($datestart, 1, 'w') - 1)."')";
2250 if ($taskid) {
2251 $sql .= " AND ptt.fk_element=".((int) $taskid);
2252 }
2253 if (is_numeric($userid)) {
2254 $sql .= " AND ptt.fk_user=".((int) $userid);
2255 }
2256
2257 //print $sql;
2258 $resql = $this->db->query($sql);
2259 if ($resql) {
2260 $dayallreadyfound = array();
2261
2262 $num = $this->db->num_rows($resql);
2263 $i = 0;
2264 // Loop on each record found, so each couple (project id, task id)
2265 while ($i < $num) {
2266 $obj = $this->db->fetch_object($resql);
2267 $day = $this->db->jdate($obj->element_date); // task_date is date without hours
2268
2269 if (empty($dayallreadyfound[$day])) {
2270 $this->weekWorkLoad[$day] = (int) $obj->element_duration; // Float in db used as int
2271 $this->weekWorkLoadPerTask[$day][$obj->fk_element] = (int) $obj->element_duration;
2272 } else {
2273 $this->weekWorkLoad[$day] += (int) $obj->element_duration; // Float in db used as int
2274 $this->weekWorkLoadPerTask[$day][$obj->fk_element] += (int) $obj->element_duration;
2275 }
2276 $dayallreadyfound[$day] = 1;
2277 $i++;
2278 }
2279 $this->db->free($resql);
2280 return $num;
2281 } else {
2282 $this->error = "Error ".$this->db->lasterror();
2283 dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
2284 return -1;
2285 }
2286 }
2287
2297 public function loadTimeSpentMonth($datestart, $taskid = 0, $userid = 0)
2298 {
2299 $this->monthWorkLoad = array();
2300 $this->monthWorkLoadPerTask = array();
2301
2302 if (empty($datestart)) {
2303 dol_print_error(null, 'Error datestart parameter is empty');
2304 }
2305
2306 $sql = "SELECT ptt.rowid as taskid, ptt.element_duration, ptt.element_date, ptt.element_datehour, ptt.fk_element";
2307 $sql .= " FROM ".MAIN_DB_PREFIX."element_time AS ptt, ".MAIN_DB_PREFIX."projet_task as pt";
2308 $sql .= " WHERE ptt.fk_element = pt.rowid";
2309 $sql .= " AND ptt.elementtype = 'task'";
2310 $sql .= " AND pt.fk_projet = ".((int) $this->id);
2311 $sql .= " AND (ptt.element_date >= '".$this->db->idate($datestart)."' ";
2312 $sql .= " AND ptt.element_date <= '".$this->db->idate(dol_time_plus_duree($datestart, 1, 'm') - 1)."')";
2313 if ($taskid) {
2314 $sql .= " AND ptt.fk_element=".((int) $taskid);
2315 }
2316 if (is_numeric($userid)) {
2317 $sql .= " AND ptt.fk_user=".((int) $userid);
2318 }
2319
2320 //print $sql;
2321 $resql = $this->db->query($sql);
2322 if ($resql) {
2323 $weekalreadyfound = array();
2324
2325 $num = $this->db->num_rows($resql);
2326 $i = 0;
2327 $week_number = ''; // Initialisation for static analysis
2328 // Loop on each record found, so each couple (project id, task id)
2329 while ($i < $num) {
2330 $obj = $this->db->fetch_object($resql);
2331 if (!empty($obj->element_date)) {
2332 $date = explode('-', $obj->element_date);
2333 $week_number = getWeekNumber((int) $date[2], (int) $date[1], (int) $date[0]);
2334 }
2335 if (empty($weekalreadyfound[$week_number])) {
2336 $this->monthWorkLoad[$week_number] = (int) $obj->element_duration;
2337 $this->monthWorkLoadPerTask[$week_number][$obj->fk_element] = (int) $obj->element_duration;
2338 } else {
2339 $this->monthWorkLoad[$week_number] += (int) $obj->element_duration;
2340 if (!isset($this->monthWorkLoadPerTask[$week_number][$obj->fk_element])) {
2341 $this->monthWorkLoadPerTask[$week_number][$obj->fk_element] = 0;
2342 }
2343 $this->monthWorkLoadPerTask[$week_number][$obj->fk_element] += (int) $obj->element_duration;
2344 }
2345 $weekalreadyfound[$week_number] = 1;
2346 $i++;
2347 }
2348 $this->db->free($resql);
2349 return 1;
2350 } else {
2351 $this->error = "Error ".$this->db->lasterror();
2352 dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
2353 return -1;
2354 }
2355 }
2356
2357 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2364 public function load_board($user)
2365 {
2366 // phpcs:enable
2367 global $conf, $langs;
2368
2369 // For external user, no check is done on company because readability is managed by public status of project and assignment.
2370 //$socid=$user->socid;
2371
2372 $response = new WorkboardResponse();
2373 $response->warning_delay = $conf->project->warning_delay / 60 / 60 / 24;
2374 $response->label = $langs->trans("OpenedProjects");
2375 $response->labelShort = $langs->trans("Opened");
2376 $response->url = DOL_URL_ROOT.'/projet/list.php?search_project_user=-1&search_status=1&mainmenu=project';
2377 $response->url_late = DOL_URL_ROOT.'/projet/list.php?search_option=late&mainmenu=project';
2378 $response->img = img_object('', "projectpub");
2379 $response->nbtodo = 0;
2380 $response->nbtodolate = 0;
2381
2382 // The count and the number of late projects are computed by the database instead of reading every project. An open project
2383 // is late when it has an end date and that date is before now minus the warning delay (the rule of hasDelay()).
2384 $sql = "SELECT COUNT(p.rowid) as nb,";
2385 $sql .= " SUM(CASE WHEN p.datee IS NOT NULL AND p.datee < '".$this->db->idate(dol_now() - $conf->project->warning_delay)."' THEN 1 ELSE 0 END) as nblate";
2386 $sql .= " FROM (".MAIN_DB_PREFIX."projet as p";
2387 $sql .= ")";
2388 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s on p.fk_soc = s.rowid";
2389 // For external user, no check is done on company permission because readability is managed by public status of project and assignment.
2390 //if (! $user->rights->societe->client->voir && ! $socid) $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc = s.rowid";
2391 $sql .= " WHERE p.fk_statut = 1";
2392 $sql .= " AND p.entity IN (".getEntity('project').')';
2393
2394
2395 $projectsListId = null;
2396 if (!$user->hasRight("projet", "all", "lire")) {
2397 $response->url = DOL_URL_ROOT.'/projet/list.php?search_status=1&mainmenu=project';
2398 $projectsListId = $this->getProjectsAuthorizedForUser($user, 0, 1);
2399 if (empty($projectsListId)) {
2400 return $response;
2401 }
2402
2403 $sql .= " AND p.rowid IN (".$this->db->sanitize($projectsListId).")";
2404 }
2405
2406 // No need to check company, as filtering of projects must be done by getProjectsAuthorizedForUser
2407 //if ($socid || ! $user->rights->societe->client->voir) $sql.= " AND (p.fk_soc IS NULL OR p.fk_soc = 0 OR p.fk_soc = ".((int) $socid).")";
2408 // For external user, no check is done on company permission because readability is managed by public status of project and assignment.
2409 //if (! $user->rights->societe->client->voir && ! $socid) $sql.= " AND ((s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id).") OR (s.rowid IS NULL))";
2410
2411 //print $sql;
2412 $resql = $this->db->query($sql);
2413 if ($resql) {
2414 $obj = $this->db->fetch_object($resql);
2415 if ($obj) {
2416 $response->nbtodo = (int) $obj->nb;
2417 $response->nbtodolate = (int) $obj->nblate;
2418 }
2419
2420 return $response;
2421 }
2422
2423 $this->error = $this->db->error();
2424 return -1;
2425 }
2426
2441 public function getViewFilterSQL(string $view, string $alias = 'p'): string
2442 {
2443 // $tablealiastouse holds a literal of this method, never the caller input.
2444 if ($alias == 'p') {
2445 $tablealiastouse = 'p';
2446 } elseif ($alias == 't') {
2447 $tablealiastouse = 't';
2448 } else {
2449 return '';
2450 }
2451
2452 $sanitizedwonlost = $tablealiastouse.".fk_opp_status IN (SELECT rowid FROM ".$this->db->prefix()."c_lead_status WHERE code IN ('WON', 'LOST'))";
2453
2454 if ($view == 'openedopp') {
2455 return "(".$tablealiastouse.".usage_opportunity = 1 AND (".$tablealiastouse.".fk_opp_status IS NULL OR NOT ".$sanitizedwonlost."))";
2456 } elseif ($view == 'notopenedopp') {
2457 return "(".$tablealiastouse.".usage_opportunity IS NULL OR ".$tablealiastouse.".usage_opportunity <> 1 OR ".$sanitizedwonlost.")";
2458 }
2459
2460 return '';
2461 }
2462
2471 public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
2472 {
2473 $tables = array(
2474 'projet'
2475 );
2476
2477 return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
2478 }
2479
2480
2486 public function loadStateBoard()
2487 {
2488 global $user;
2489
2490 $this->nb = array();
2491
2492 $sql = "SELECT count(p.rowid) as nb";
2493 $sql .= " FROM ".MAIN_DB_PREFIX."projet as p";
2494 $sql .= " WHERE";
2495 $sql .= " p.entity IN (".getEntity('project').")";
2496 if (!$user->hasRight('projet', 'all', 'lire')) {
2497 $projectsListId = $this->getProjectsAuthorizedForUser($user, 0, 1);
2498 $sql .= "AND p.rowid IN (".$this->db->sanitize($projectsListId).")";
2499 }
2500
2501 $resql = $this->db->query($sql);
2502 if ($resql) {
2503 while ($obj = $this->db->fetch_object($resql)) {
2504 $this->nb["projects"] = $obj->nb;
2505 }
2506 $this->db->free($resql);
2507 return 1;
2508 } else {
2509 dol_print_error($this->db);
2510 $this->error = $this->db->error();
2511 return -1;
2512 }
2513 }
2514
2515
2521 public function hasDelay()
2522 {
2523 global $conf;
2524
2525 if (!($this->status == self::STATUS_VALIDATED)) {
2526 return false;
2527 }
2528 if (!$this->date_end) {
2529 return false;
2530 }
2531
2532 $now = dol_now();
2533
2534 return ($this->date_end) < ($now - $conf->project->warning_delay);
2535 }
2536
2537
2544 public function info($id)
2545 {
2546 $sql = 'SELECT c.rowid, datec as datec, tms as datem,';
2547 $sql .= ' date_close as datecloture,';
2548 $sql .= ' fk_user_creat as fk_user_author, fk_user_close as fk_user_cloture';
2549 $sql .= ' FROM '.MAIN_DB_PREFIX.'projet as c';
2550 $sql .= ' WHERE c.rowid = '.((int) $id);
2551 $result = $this->db->query($sql);
2552 if ($result) {
2553 if ($this->db->num_rows($result)) {
2554 $obj = $this->db->fetch_object($result);
2555
2556 $this->id = $obj->rowid;
2557
2558 $this->user_creation_id = $obj->fk_user_author;
2559 $this->user_closing_id = $obj->fk_user_cloture;
2560
2561 $this->date_creation = $this->db->jdate($obj->datec);
2562 $this->date_modification = $this->db->jdate($obj->datem);
2563 $this->date_cloture = $this->db->jdate($obj->datecloture);
2564 }
2565
2566 $this->db->free($result);
2567 } else {
2568 dol_print_error($this->db);
2569 }
2570 }
2571
2582 public function setCategories($categories)
2583 {
2584 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
2585 return parent::setCategoriesCommon($categories, Categorie::TYPE_PROJECT);
2586 }
2587
2588
2596 public function getLinesArray($user, $loadRoleMode = 1)
2597 {
2598 require_once DOL_DOCUMENT_ROOT.'/projet/class/task.class.php';
2599 $taskstatic = new Task($this->db);
2600
2601 $this->lines = $taskstatic->getTasksArray(null, $user, $this->id, 0, 0, '', '-1', '', 0, 0, null, 0, array(), 0, $loadRoleMode);
2602 return 1;
2603 }
2604
2623 public function sendEmail($text, $subject, $filename_list = array(), $mimetype_list = array(), $mimefilename_list = array(), $addr_cc = "", $addr_bcc = "", $deliveryreceipt = 0, $msgishtml = -1, $errors_to = '', $moreinheader = '')
2624 {
2625 // TODO EMAIL
2626
2627 return 1;
2628 }
2629
2638 public function getKanbanView($option = '', $arraydata = null, $size = '')
2639 {
2640 global $conf, $langs;
2641
2642 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
2643
2644 if (empty($size)) {
2645 if (empty($conf->dol_optimize_smallscreen)) {
2646 $size = 'large';
2647 } else {
2648 $size = 'small';
2649 }
2650 }
2651
2652 $return = '<div class="box-flex-item '.($size == 'small' ? 'box-flex-item-small' : '').' box-flex-grow-zero '.($arraydata['mode'] == 'kanbangroupby' ? 'kanban-draggable" data-itemid="'.$this->id.'" data-element="'.$this->element.'" data-tableelement="'.$this->table_element.'"' : '"').'>';
2653 $return .= '<div class="info-box info-box-sm">';
2654 $return .= '<span class="info-box-icon bg-infobox-action">';
2655 $return .= img_picto('', $this->public ? 'projectpub' : $this->picto);
2656 //$return .= '<i class="fa fa-dol-action"></i>'; // Can be image
2657 $return .= '</span>';
2658 $return .= '<div class="info-box-content">';
2659 $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref);
2660 if ($this->hasDelay()) {
2661 $return .= img_warning($langs->trans('Late'));
2662 }
2663 $return .= '</span>';
2664 if ($selected >= 0) {
2665 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
2666 }
2667 // Date
2668 /*
2669 if (property_exists($this, 'date_start') && $this->date_start) {
2670 $return .= '<br><span class="info-box-label">'.dol_print_date($this->date_start, 'day').'</>';
2671 }
2672 if (property_exists($this, 'date_end') && $this->date_end) {
2673 if ($this->date_start) {
2674 $return .= ' - ';
2675 } else {
2676 $return .= '<br>';
2677 }
2678 $return .= '<span class="info-box-label">'.dol_print_date($this->date_end, 'day').'</span>';
2679 }*/
2680 if (property_exists($this, 'thirdparty') && !is_null($this->thirdparty) && is_object($this->thirdparty) && $this->thirdparty instanceof Societe && $this->thirdparty->id > 0) {
2681 $return .= '<br><div class="info-box-ref tdoverflowmax125 inline-block valignmiddle">'.$this->thirdparty->getNomUrl(1);
2682 $return .= '</div>';
2683 if (!empty($this->thirdparty->phone)) {
2684 $return .= '<div class="inline-block valignmiddle">';
2685 $return .= dol_print_phone($this->thirdparty->phone, $this->thirdparty->country_code, 0, $this->thirdparty->id, 'tel', 'hidenum', 'phone', $this->thirdparty->phone, 0, 'paddingleft paddingright'); // @phan-suppress-current-line PhanPluginSuspiciousParamPosition
2686 $return .= '</div>';
2687 }
2688 if (!empty($this->thirdparty->email)) {
2689 $return .= '<div class="inline-block valignmiddle">';
2690 $return .= dol_print_email($this->thirdparty->email, 0, $this->thirdparty->id, 'thirdparty', -1, 1, 2, 'paddingleft paddingright');
2691 $return .= '</div>';
2692 }
2693 }
2694 if (!empty($arraydata['assignedusers'])) {
2695 $return .= '<br>';
2696 if ($this->public) {
2697 $return .= img_picto($langs->trans('Visibility').': '.$langs->trans('SharedProject'), 'world', 'class="paddingrightonly valignmiddle"');
2698 //print $langs->trans('SharedProject');
2699 } else {
2700 $return .= img_picto($langs->trans('Visibility').': '.$langs->trans('PrivateProject'), 'private', 'class="paddingrightonly valignmiddle"');
2701 //print $langs->trans('PrivateProject');
2702 }
2703
2704 $return .= ' <span class="small valignmiddle">'.$arraydata['assignedusers'].'</span>';
2705 }
2706 /*if (property_exists($this, 'user_author_id')) {
2707 $return .= '<br><span class="info-box-label opacitymedium">'.$langs->trans("Author").'</span>';
2708 $return .= '<span> : '.$user->getNomUrl(1).'</span>';
2709 }*/
2710 $return .= '<br><div>'; // start div line status
2711 if ($this->usage_opportunity && $this->opp_status_code) {
2712 //$return .= '<br><span class="info-bo-label opacitymedium">'.$langs->trans("OpportunityStatusShort").'</span>';
2713 //$return .= '<div class="small inline-block">'.dol_trunc($langs->trans("OppStatus".$this->opp_status_code), 5).'</div>';
2714 $return .= '<div class="opacitymedium small marginrightonly inline-block" title="'.dol_escape_htmltag($langs->trans("OppStatus".$this->opp_status_code)).'">'.round($this->opp_percent).'%</div>';
2715 $return .= ' <div class="amount small marginrightonly inline-block">'.price($this->opp_amount).'</div>';
2716 }
2717 if (method_exists($this, 'getLibStatut')) {
2718 $return .= '<div class="info-box-status small inline-block valignmiddle">'.$this->getLibStatut(3).'</div>';
2719 }
2720 $return .= '</div>'; // end div line status
2721
2722 $return .= '</div>';
2723 $return .= '</div>';
2724 $return .= '</div>';
2725
2726 return $return;
2727 }
2728
2734 public function getChildren()
2735 {
2736 $children = [];
2737 $sql = 'SELECT rowid,title';
2738 $sql .= ' FROM '.MAIN_DB_PREFIX.'projet';
2739 $sql .= ' WHERE fk_project = '.((int) $this->id);
2740 $sql .= ' ORDER BY title';
2741 $result = $this->db->query($sql);
2742 if ($result) {
2743 $n = $this->db->num_rows($result);
2744 while ($n) {
2745 $children[] = $this->db->fetch_object($result);
2746 $n--;
2747 }
2748 $this->db->free($result);
2749 } else {
2750 dol_print_error($this->db);
2751 }
2752
2753 return $children;
2754 }
2755
2761 public function createWeeklyReport()
2762 {
2763 global $mysoc, $user, $langs;
2764
2765 $now = dol_now();
2766 $nowDate = dol_getdate($now, true);
2767
2768 $errormesg = '';
2769 $errorsMsg = array();
2770
2771 $firstDayOfWeekTS = dol_get_first_day_week($nowDate['mday'], $nowDate['mon'], $nowDate['year']);
2772
2773 $firstDayOfWeekDate = dol_mktime(0, 0, 0, $nowDate['mon'], $firstDayOfWeekTS['first_day'], $nowDate['year']);
2774
2775 $lastWeekStartTS = dol_time_plus_duree($firstDayOfWeekDate, -7, 'd');
2776
2777 $lastWeekEndTS = dol_time_plus_duree($lastWeekStartTS, 6, 'd');
2778
2779 $startDate = dol_print_date($lastWeekStartTS, '%Y-%m-%d 00:00:00');
2780 $endDate = dol_print_date($lastWeekEndTS, '%Y-%m-%d 23:59:59');
2781
2782 $sql = "SELECT
2783 u.rowid AS user_id,
2784 CONCAT(u.firstname, ' ', u.lastname) AS name,
2785 u.email,u.weeklyhours,
2786 SUM(et.element_duration) AS total_seconds
2787 FROM
2788 ".MAIN_DB_PREFIX."element_time AS et
2789 JOIN
2790 ".MAIN_DB_PREFIX."user AS u ON et.fk_user = u.rowid
2791 WHERE
2792 et.element_date BETWEEN '".$this->db->escape($startDate)."' AND '".$this->db->escape($endDate)."'
2793 AND et.elementtype = 'task'
2794 GROUP BY
2795 et.fk_user";
2796
2797 $resql = $this->db->query($sql);
2798 if (!$resql) {
2799 dol_print_error($this->db);
2800 return -1;
2801 } else {
2802 $reportContent = "<span>Weekly time report from $startDate to $endDate </span><br><br>";
2803 $reportContent .= '<table border="1" style="border-collapse: collapse;">';
2804 $reportContent .= '<tr><th>'.$langs->trans("User").'</th><th>Temps saisi (heures)</th><th>Temps travaillé par semaine (heures)</th></tr>';
2805
2806 $to = '';
2807 $nbMailSend = 0;
2808 $error = 0;
2809 $errors_to = '';
2810 while ($obj = $this->db->fetch_object($resql)) {
2811 $to = $obj->email;
2812
2813 $weekendEnabled = 0;
2814 $numHolidays = num_public_holiday($lastWeekStartTS, $lastWeekEndTS, $mysoc->country_code, 1);
2815 if (getDolGlobalString('MAIN_NON_WORKING_DAYS_INCLUDE_MONDAY')) {
2816 $numHolidays -= 1;
2817 $weekendEnabled += 1;
2818 }
2819 if (getDolGlobalString('MAIN_NON_WORKING_DAYS_INCLUDE_TUESDAY')) {
2820 $numHolidays -= 1;
2821 $weekendEnabled += 1;
2822 }
2823 if (getDolGlobalString('MAIN_NON_WORKING_DAYS_INCLUDE_SATURDAY')) {
2824 $numHolidays -= 1;
2825 $weekendEnabled += 1;
2826 }
2827 if (getDolGlobalString('MAIN_NON_WORKING_DAYS_INCLUDE_SUNDAY')) {
2828 $numHolidays -= 1;
2829 $weekendEnabled += 1;
2830 }
2831
2832 $dailyHours = $obj->weeklyhours / (7 - $weekendEnabled);
2833
2834 // Adjust total on seconde
2835 $adjustedSeconds = $obj->total_seconds + ($numHolidays * $dailyHours * 3600);
2836
2837 $totalHours = round($adjustedSeconds / 3600, 2);
2838
2839 $reportContent .= "<tr><td>{$obj->name}</td><td>{$totalHours}</td><td>".round($obj->weeklyhours, 2)."</td></tr>";
2840
2841 $reportContent .= '</table>';
2842
2843 require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
2844
2845 // PREPARE EMAIL
2846 $errormesg = '';
2847
2848 $subject = 'Rapport hebdomadaire des temps travaillés';
2849
2850 $from = getDolGlobalString('MAIN_MAIL_EMAIL_FROM');
2851 if (empty($from)) {
2852 $errormesg = "Failed to get sender into global setup MAIN_MAIL_EMAIL_FROM";
2853 $error++;
2854 }
2855
2856 $mail = new CMailFile($subject, $to, $from, $reportContent, array(), array(), array(), '', '', 0, -1, '', '', '', 'text/html');
2857
2858 if ($mail->sendfile()) {
2859 $nbMailSend++;
2860
2861 // Add a line into event table
2862 require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
2863
2864 // Insert record of emails sent
2865 $actioncomm = new ActionComm($this->db);
2866
2867 $actioncomm->type_code = 'AC_OTH_AUTO'; // Event insert into agenda automatically
2868 $actioncomm->socid = $this->thirdparty->id; // To link to a company
2869 $actioncomm->contact_id = 0;
2870
2871 $actioncomm->code = 'AC_EMAIL';
2872 $actioncomm->label = 'createWeeklyReportOK()';
2873 $actioncomm->fk_project = $this->id;
2874 $actioncomm->datep = dol_now();
2875 $actioncomm->datef = $actioncomm->datep;
2876 $actioncomm->percentage = -1; // Not applicable
2877 $actioncomm->authorid = $user->id; // User saving action
2878 $actioncomm->userownerid = $user->id; // Owner of action
2879 // Fields when action is an email (content should be added into note)
2880 $actioncomm->email_msgid = $mail->msgid;
2881 $actioncomm->email_subject = $subject;
2882 $actioncomm->email_from = $from;
2883 $actioncomm->email_sender = '';
2884 $actioncomm->email_to = $to;
2885
2886 $actioncomm->errors_to = $errors_to;
2887
2888 $actioncomm->elementtype = 'project_task';
2889 $actioncomm->fk_element = (int) $this->element;
2890
2891 $actioncomm->create($user);
2892 } else {
2893 $errormesg = $mail->error.' : '.$to;
2894 $error++;
2895
2896 // Add a line into event table
2897 require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
2898
2899 // Insert record of emails sent
2900 $actioncomm = new ActionComm($this->db);
2901
2902 $actioncomm->type_code = 'AC_OTH_AUTO'; // Event insert into agenda automatically
2903 $actioncomm->socid = $this->thirdparty->id; // To link to a company
2904 $actioncomm->contact_id = 0;
2905
2906 $actioncomm->code = 'AC_EMAIL';
2907 $actioncomm->label = 'createWeeklyReportKO()';
2908 $actioncomm->note_private = $errormesg;
2909 $actioncomm->fk_project = $this->id;
2910 $actioncomm->datep = dol_now();
2911 $actioncomm->datef = $actioncomm->datep;
2912 $actioncomm->authorid = $user->id; // User saving action
2913 $actioncomm->userownerid = $user->id; // Owner of action
2914 // Fields when action is an email (content should be added into note)
2915 $actioncomm->email_msgid = $mail->msgid;
2916 $actioncomm->email_from = $from;
2917 $actioncomm->email_sender = '';
2918 $actioncomm->email_to = $to;
2919
2920 $actioncomm->errors_to = $errors_to;
2921
2922 $actioncomm->elementtype = 'project_task';
2923 $actioncomm->fk_element = (int) $this->element;
2924
2925 $actioncomm->create($user);
2926 }
2927 $this->db->commit();
2928 }
2929 }
2930 if (!empty($errormesg)) {
2931 $errorsMsg[] = $errormesg;
2932 }
2933
2934 if (!$error) {
2935 $this->output .= 'Nb of emails sent : '.$nbMailSend;
2936 dol_syslog(__METHOD__." end - ".$this->output, LOG_INFO);
2937 return 0;
2938 } else {
2939 $this->error = 'Nb of emails sent : '.$nbMailSend.', '.(empty($errorsMsg) ? $error : implode(', ', $errorsMsg));
2940 dol_syslog(__METHOD__." end - ".$this->error, LOG_INFO);
2941 return $error;
2942 }
2943 }
2944}
$object ref
Definition info.php:90
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
Class to manage agenda events (actions)
Class to send emails (with attachments or not) Usage: $mailfile = new CMailFile($subject,...
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this->array_options This method is in most cases call...
commonGenerateDocument($modelspath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams=null)
Common function for all objects extending CommonObject for generating documents.
liste_contact($statusoflink=-1, $source='external', $list=0, $code='', $status=-1, $arrayoftcids=array())
Get array of all contacts for an object.
static commonReplaceThirdparty(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
delete_linked_contact($source='', $code='')
Delete all links between an object $this and all its contacts in llx_element_contact.
Class to manage Dolibarr database access.
Class to manage projects.
getFieldPropertyName($field)
Return the name of the property of the object holding the value of a field of $fields.
LibStatut($status, $mode=0)
Renvoi status label for a status.
getLibStatut($mode=0)
Return status label of object.
getElementCount($type, $tablename, $projectkey='fk_projet')
Return the count of a type of linked elements of this project.
fetchAndSetSubstitution($id, $key, $fetched=false)
Fetch object and substitute key.
getChildren()
Return array of sub-projects of the current project.
setValid($user, $notrigger=0)
Validate a project.
createWeeklyReport()
Method for calculating weekly hours worked and generating a report.
static replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
const STATUS_VALIDATED
Open/Validated status.
create($user, $notrigger=0)
Create a project into database.
const STATUS_CLOSED
Closed status.
const STATUS_DRAFT
Draft status.
getNomUrl($withpicto=0, $option='', $addlabel=0, $moreinpopup='', $sep=' - ', $notooltip=0, $save_lastsearch_value=-1, $morecss='', $save_pageforbacktolist='', $addlinktonotes=0)
Return clickable name (with optional picto)
loadTimeSpentMonth($datestart, $taskid=0, $userid=0)
Load time spent into this->weekWorkLoad and this->weekWorkLoadPerTask for all day of a week of projec...
getViewFilterSQL(string $view, string $alias='p')
Build the SQL WHERE fragment that tells apart an open opportunity from a record that is not one.
ajaxOutputField($field)
Return the output html of a field for the user interface (core/ajax/objectfield.php).
hasUserPermission(User $user, $mode='write')
Check if user has a permission on projects, on its own projects or on all projects.
__construct($db)
Constructor.
fetch($id, $ref='', $ref_ext='', $email_msgid='')
Get object from database.
shiftTaskDate($old_project_dt_start)
Shift project task date from current date to delta.
hasDelay()
Is the project delayed?
remove_element($tableName, $elementSelectId, $projectfield='fk_projet')
Associate element to a project.
setCategories($categories)
Sets object to supplied categories.
update_element($tableName, $elementSelectId)
Associate element to a project.
createFromClone(User $user, $fromid, $clone_contact=false, $clone_task=true, $clone_project_file=false, $clone_task_file=false, $clone_note=true, $move_date=true, $notrigger=0, $newthirdpartyid=0)
Load an object from its id and create a new one in database.
loadStateBoard()
Load indicators this->nb for the state board.
load_board($user)
Load indicators for dashboard (this->nbtodo and this->nbtodolate)
getLinesArray($user, $loadRoleMode=1)
Create an array of tasks of current project.
get_element_list($type, $tablename, $datefieldname='', $date_start=null, $date_end=null, $projectkey='fk_projet')
Return list of elements for type, linked to a project.
getKanbanView($option='', $arraydata=null, $size='')
Return clickable link of object (with eventually picto)
info($id)
Charge les information d'ordre info dans l'objet commande.
getProjectsAuthorizedForUser($fuser, $mode=0, $list=0, $socid=0, $filter='')
Return array of projects a user has permission on, is affected to, or all projects.
getTooltipContentArray($params)
getTooltipContentArray
sendEmail($text, $subject, $filename_list=array(), $mimetype_list=array(), $mimefilename_list=array(), $addr_cc="", $addr_bcc="", $deliveryreceipt=0, $msgishtml=-1, $errors_to='', $moreinheader='')
Function sending an email to the current project with the text supplied in parameter.
initAsSpecimen()
Initialise an instance with random values.
setClose($user)
Close a project.
loadTimeSpent($datestart, $taskid=0, $userid=0)
Load time spent into this->weekWorkLoad and this->weekWorkLoadPerTask for all day of a week of projec...
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0)
Create an intervention document on disk using template defined into PROJECT_ADDON_PDF.
restrictedProjectArea(User $user, $mode='read')
Check if user has permission on current project.
deleteTasks($user)
Delete tasks with no children first, then task with children recursively.
update($user, $notrigger=0)
Update a project.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage tasks.
Class to manage Dolibarr users.
print $langs trans("Ref").' m titre as m m statut as status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition index.php:169
global $mysoc
dol_get_first_day_week($day, $month, $year, $gm=false)
Return first day of week for a date.
Definition date.lib.php:701
getWeekNumber($day, $month, $year)
Return week number.
num_public_holiday($timestampStart, $timestampEnd, $countryCodeOrId='', $lastday=0, $includesaturday=-1, $includesunday=-1, $includefriday=-1, $includemonday=-1)
Return the number of non working days including Friday, Saturday and Sunday (or not) between 2 dates ...
Definition date.lib.php:793
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0, $tz='')
Add a delay to a date.
Definition date.lib.php:127
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_copy($srcfile, $destfile, $newmask='0', $overwriteifexists=1, $testvirus=0, $indexdatabase=0)
Copy a file to another file.
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0, $indexdatabase=1, $nolog=0, $level=0)
Remove a directory $dir and its subdirectories (or only files and subdirectories)
dol_dir_list($utf8_path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
Definition files.lib.php:66
dol_html_entity_decode($a, $b, $c='UTF-8', $keepsomeentities=0)
Replace html_entity_decode functions to manage errors.
dol_now($mode='gmt')
Return date for now.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed information (by default a local PHP server timestamp) Rep...
dol_print_email($email, $contactid=0, $socid=0, $addlink=0, $max=0, $showinvalid=2, $withpicto=0, $morecss='paddingrightonly')
Show EMail link formatted for HTML output.
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0, $filters='', $useCache=true)
Return an id or code from a code or id.
price2num($amount, $rounding='', $option=0, $currencycode='')
Function that return a number with universal decimal format (decimal separator is '.
dol_print_phone($phone, $countrycode='', $contactid=0, $socid=0, $addlink='', $separ="&nbsp;", $withpicto='', $titlealt='', $adddivfloat=0, $morecss='paddingright')
Format phone numbers according to country.
dol_strlen($string, $stringencoding='UTF-8', $binarycount=0)
Make a strlen call.
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
dolBuildUrl($url, $params=[], $addtoken=false, $anchor='')
Return path of url.
isDolTms($timestamp)
isDolTms check if a timestamp is valid.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0, $allowdash=0)
Clean a string to use it as a file name.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
dol_substr($string, $start, $length=null, $stringencoding='', $trunconbytes=0)
Make a substring.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
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_getdate($timestamp, $fast=false, $forcetimezone='')
Return an array with locale date info.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Definition html.lib.php:183