dolibarr 24.0.0-beta
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-2025 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' => 'ID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 10),
346 'fk_project' => array('type' => 'integer', 'label' => 'Parent', 'enabled' => 1, 'visible' => -1, 'notnull' => 0, 'position' => 12),
347 'ref' => array('type' => 'varchar(50)', 'label' => 'Ref', 'enabled' => 1, 'visible' => 1, 'showoncombobox' => 1, 'position' => 15, 'searchall' => 1),
348 'title' => array('type' => 'varchar(255)', 'label' => 'ProjectLabel', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'position' => 17, 'showoncombobox' => 2, 'searchall' => 1, 'csslist' => 'tdoverflowmax250'),
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),
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 ? $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 $this->error = $langs->trans("ErrorDateEndLowerThanDateStart");
617 $this->errors[] = $this->error;
618 $this->db->rollback();
619 dol_syslog(get_class($this)."::update error -3 ".$this->error, LOG_ERR);
620 return -3;
621 }
622
623 $this->entity = ((isset($this->entity) && is_numeric($this->entity)) ? $this->entity : $conf->entity);
624
625 if (dol_strlen(trim($this->ref)) > 0) {
626 $this->db->begin();
627
628 $sql = "UPDATE ".MAIN_DB_PREFIX."projet SET";
629 $sql .= " ref='".$this->db->escape($this->ref)."'";
630 $sql .= ", fk_project=".($this->fk_project ? ((int) $this->fk_project) : "null");
631 $sql .= ", title = '".$this->db->escape($this->title)."'";
632 $sql .= ", description = '".$this->db->escape($this->description)."'";
633 $sql .= ", fk_soc = ".($this->socid > 0 ? $this->socid : "null");
634 $sql .= ", fk_statut = ".((int) $this->status);
635 $sql .= ", fk_opp_status = ".((is_numeric($this->opp_status) && $this->opp_status > 0) ? $this->opp_status : 'null');
636 $sql .= ", opp_percent = ".((is_numeric($this->opp_percent) && $this->opp_percent != '') ? $this->opp_percent : 'null');
637 $sql .= ", public = ".($this->public ? 1 : 0);
638 $sql .= ", datec = ".($this->date_c != '' ? "'".$this->db->idate($this->date_c)."'" : 'null');
639 $sql .= ", dateo = ".($this->date_start != '' ? "'".$this->db->idate($this->date_start)."'" : 'null');
640 $sql .= ", datee = ".($this->date_end != '' ? "'".$this->db->idate($this->date_end)."'" : 'null');
641 $sql .= ", date_close = ".($this->date_close != '' ? "'".$this->db->idate($this->date_close)."'" : 'null');
642 $sql .= ", note_public = ".($this->note_public ? "'".$this->db->escape($this->note_public)."'" : "null");
643 $sql .= ", note_private = ".($this->note_private ? "'".$this->db->escape($this->note_private)."'" : "null");
644 $sql .= ", fk_user_close = ".($this->fk_user_close > 0 ? $this->fk_user_close : "null");
645 $sql .= ", opp_amount = ".(strcmp($this->opp_amount, '') ? price2num($this->opp_amount) : "null");
646 $sql .= ", budget_amount = ".(strcmp($this->budget_amount, '') ? price2num($this->budget_amount) : "null");
647 $sql .= ", fk_user_modif = ".((int) $user->id);
648 $sql .= ", usage_opportunity = ".($this->usage_opportunity ? 1 : 0);
649 $sql .= ", usage_task = ".($this->usage_task ? 1 : 0);
650 $sql .= ", usage_bill_time = ".($this->usage_bill_time ? 1 : 0);
651 $sql .= ", usage_organize_event = ".($this->usage_organize_event ? 1 : 0);
652 $sql .= ", accept_conference_suggestions = ".($this->accept_conference_suggestions ? 1 : 0);
653 $sql .= ", accept_booth_suggestions = ".($this->accept_booth_suggestions ? 1 : 0);
654 $sql .= ", price_registration = ".(isset($this->price_registration) && strcmp($this->price_registration, '') ? price2num($this->price_registration) : "null");
655 $sql .= ", price_booth = ".(isset($this->price_booth) && strcmp((string) $this->price_booth, '') ? price2num($this->price_booth) : "null");
656 $sql .= ", max_attendees = ".(strcmp((string) $this->max_attendees, '') ? (int) $this->max_attendees : "null");
657 $sql .= ", date_start_event = ".($this->date_start_event != '' ? "'".$this->db->idate($this->date_start_event)."'" : 'null');
658 $sql .= ", date_end_event = ".($this->date_end_event != '' ? "'".$this->db->idate($this->date_end_event)."'" : 'null');
659 $sql .= ", location = '".$this->db->escape($this->location)."'";
660 $sql .= ", entity = ".((int) $this->entity);
661 $sql .= " WHERE rowid = ".((int) $this->id);
662
663 dol_syslog(get_class($this)."::update", LOG_DEBUG);
664 $resql = $this->db->query($sql);
665 if ($resql) {
666 // Update extrafield
667 if (!$error) {
668 $result = $this->insertExtraFields();
669 if ($result < 0) {
670 $error++;
671 }
672 }
673
674 if (!$error && !$notrigger) {
675 // Call trigger
676 $result = $this->call_trigger('PROJECT_MODIFY', $user);
677 if ($result < 0) {
678 $error++;
679 }
680 // End call triggers
681 }
682
683 if (!$error && (is_object($this->oldcopy) && $this->oldcopy->ref !== $this->ref)) {
684 // We remove directory
685 if ($conf->project->dir_output) {
686 $olddir = $conf->project->dir_output."/".dol_sanitizeFileName($this->oldcopy->ref);
687 $newdir = $conf->project->dir_output."/".dol_sanitizeFileName($this->ref);
688 if (file_exists($olddir)) {
689 include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
690 $res = @rename($olddir, $newdir);
691 if (!$res) {
692 $langs->load("errors");
693 $this->error = $langs->trans('ErrorFailToRenameDir', $olddir, $newdir);
694 $error++;
695 }
696 }
697 }
698 }
699 if (!$error) {
700 $this->db->commit();
701 $result = 1;
702 } else {
703 $this->db->rollback();
704 $result = -1;
705 }
706 } else {
707 $this->error = $this->db->lasterror();
708 $this->errors[] = $this->error;
709 $this->db->rollback();
710 if ($this->db->lasterrno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
711 $result = -4;
712 } else {
713 $result = -2;
714 }
715 dol_syslog(get_class($this)."::update error ".$result." ".$this->error, LOG_ERR);
716 }
717 } else {
718 dol_syslog(get_class($this)."::update ref null");
719 $result = -1;
720 }
721
722 return $result;
723 }
724
734 public function fetch($id, $ref = '', $ref_ext = '', $email_msgid = '')
735 {
736 if (empty($id) && empty($ref) && empty($ref_ext) && empty($email_msgid)) {
737 dol_syslog(get_class($this)."::fetch Bad parameters", LOG_WARNING);
738 return -1;
739 }
740
741 $sql = "SELECT rowid, entity, fk_project, ref, title, description, public, datec, opp_amount, budget_amount,";
742 $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,";
743 $sql .= " note_private, note_public, model_pdf, usage_opportunity, usage_task, usage_bill_time, usage_organize_event, email_msgid,";
744 $sql .= " accept_conference_suggestions, accept_booth_suggestions, price_registration, price_booth, max_attendees, date_start_event, date_end_event, location, extraparams";
745 $sql .= " FROM ".MAIN_DB_PREFIX."projet";
746 if (!empty($id)) {
747 $sql .= " WHERE rowid = ".((int) $id);
748 } else {
749 $sql .= " WHERE entity IN (".getEntity('project').")";
750 if (!empty($ref)) {
751 $sql .= " AND ref = '".$this->db->escape($ref)."'";
752 } elseif (!empty($ref_ext)) {
753 $sql .= " AND ref_ext = '".$this->db->escape($ref_ext)."'";
754 } else {
755 $sql .= " AND email_msgid = '".$this->db->escape($email_msgid)."'";
756 }
757 }
758
759 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
760 $resql = $this->db->query($sql);
761 if ($resql) {
762 $num_rows = $this->db->num_rows($resql);
763
764 if ($num_rows) {
765 $obj = $this->db->fetch_object($resql);
766
767 $this->id = $obj->rowid;
768 $this->entity = $obj->entity;
769 $this->ref = $obj->ref;
770 $this->fk_project = $obj->fk_project;
771 $this->title = $obj->title;
772 $this->description = $obj->description;
773 $this->date_c = $this->db->jdate($obj->datec);
774 $this->datec = $this->db->jdate($obj->datec); // TODO deprecated
775 $this->date_m = $this->db->jdate($obj->tms);
776 $this->datem = $this->db->jdate($obj->tms); // TODO deprecated
777 $this->date_start = $this->db->jdate($obj->date_start);
778 $this->date_end = $this->db->jdate($obj->date_end);
779 $this->date_close = $this->db->jdate($obj->date_close);
780 $this->note_private = $obj->note_private;
781 $this->note_public = $obj->note_public;
782 $this->socid = $obj->fk_soc;
783 $this->user_author_id = $obj->fk_user_creat;
784 $this->user_modification_id = $obj->fk_user_modif;
785 $this->user_closing_id = $obj->fk_user_close;
786 $this->public = $obj->public;
787 $this->statut = $obj->status; // deprecated
788 $this->status = $obj->status;
789 $this->opp_status = $obj->fk_opp_status;
790 $this->opp_amount = $obj->opp_amount;
791 $this->opp_percent = $obj->opp_percent;
792 $this->budget_amount = $obj->budget_amount;
793 $this->model_pdf = $obj->model_pdf;
794 $this->usage_opportunity = (int) $obj->usage_opportunity;
795 $this->usage_task = (int) $obj->usage_task;
796 $this->usage_bill_time = (int) $obj->usage_bill_time;
797 $this->usage_organize_event = (int) $obj->usage_organize_event;
798 $this->accept_conference_suggestions = (int) $obj->accept_conference_suggestions;
799 $this->accept_booth_suggestions = (int) $obj->accept_booth_suggestions;
800 $this->price_registration = $obj->price_registration;
801 $this->price_booth = $obj->price_booth;
802 $this->max_attendees = $obj->max_attendees;
803 $this->date_start_event = $this->db->jdate($obj->date_start_event);
804 $this->date_end_event = $this->db->jdate($obj->date_end_event);
805 $this->location = $obj->location;
806 $this->email_msgid = $obj->email_msgid;
807 $this->extraparams = !empty($obj->extraparams) ? (array) json_decode($obj->extraparams, true) : array();
808
809 $this->db->free($resql);
810
811 // Retrieve all extrafield
812 // fetch optionals attributes and labels
813 $this->fetch_optionals();
814
815 return 1;
816 }
817
818 $this->db->free($resql);
819
820 return 0;
821 } else {
822 $this->error = $this->db->lasterror();
823 $this->errors[] = $this->db->lasterror();
824 return -1;
825 }
826 }
827
836 public function fetchAndSetSubstitution($id, $key, $fetched = false)
837 {
838 $substitution = '';
839
840 if ($fetched === false) {
841 $res = $this->fetch($id);
842 if ($res > 0) {
843 $fetched = true;
844 }
845 }
846
847 if ($fetched === true) {
848 if ($key == '__PROJECT_ID__') {
849 $substitution = ($this->id > 0 ? $this->id : '');
850 } elseif ($key == '__PROJECT_REF__') {
851 $substitution = $this->ref;
852 } elseif ($key == '__PROJECT_NAME__') {
853 $substitution = $this->title;
854 }
855 }
856
857 return (string) $substitution;
858 }
859
860 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
872 public function get_element_list($type, $tablename, $datefieldname = '', $date_start = null, $date_end = null, $projectkey = 'fk_projet')
873 {
874 // phpcs:enable
875
876 global $hookmanager;
877
878 $elements = array();
879
880 if ($this->id <= 0) {
881 return $elements;
882 }
883
884 $ids = $this->id;
885
886 if ($type == 'agenda') {
887 $sql = "SELECT id as rowid FROM ".MAIN_DB_PREFIX."actioncomm WHERE fk_project IN (".$this->db->sanitize((string) $ids).") AND entity IN (".getEntity('agenda').")";
888 } elseif ($type == 'expensereport') {
889 $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).")";
890 } elseif ($type == 'project_task') {
891 $sql = "SELECT DISTINCT pt.rowid FROM ".MAIN_DB_PREFIX."projet_task as pt WHERE pt.fk_projet IN (".$this->db->sanitize((string) $ids).")";
892 } elseif ($type == 'element_time') { // Case we want to duplicate line foreach user
893 $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).")";
894 } elseif ($type == 'stocktransfer_stocktransfer') {
895 $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";
896 } elseif ($type == 'loan') {
897 $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).")";
898 } else {
899 $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).")";
900 }
901
902 if (isDolTms($date_start) && $type == 'loan') {
903 $sql .= " AND (dateend > '".$this->db->idate((int) $date_start)."' OR dateend IS NULL)";
904 } elseif (isDolTms($date_start) && ($type != 'project_task')) { // For table project_taks, we want the filter on date apply on project_time_spent table
905 if (empty($datefieldname) && !empty($this->table_element_date)) {
906 $datefieldname = $this->table_element_date;
907 }
908 if (empty($datefieldname)) {
909 return 'Error this object has no date field defined';
910 }
911 $sql .= " AND (".$datefieldname." >= '".$this->db->idate((int) $date_start)."' OR ".$datefieldname." IS NULL)";
912 }
913
914 if (isDolTms($date_end) && $type == 'loan') {
915 $sql .= " AND (datestart < '".$this->db->idate((int) $date_end)."' OR datestart IS NULL)";
916 } elseif (isDolTms($date_end) && ($type != 'project_task')) { // For table project_taks, we want the filter on date apply on project_time_spent table
917 if (empty($datefieldname) && !empty($this->table_element_date)) {
918 $datefieldname = $this->table_element_date;
919 }
920 if (empty($datefieldname)) {
921 return 'Error this object has no date field defined';
922 }
923 $sql .= " AND (".$datefieldname." <= '".$this->db->idate((int) $date_end)."' OR ".$datefieldname." IS NULL)";
924 }
925
926 $parameters = array(
927 'sql' => $sql,
928 'type' => $type,
929 'tablename' => $tablename,
930 'datefieldname' => $datefieldname,
931 'dates' => $date_start,
932 'datee' => $date_end,
933 'fk_projet' => $projectkey,
934 'ids' => $ids,
935 );
936 $reshook = $hookmanager->executeHooks('getElementList', $parameters);
937 if ($reshook > 0) {
938 $sql = $hookmanager->resPrint;
939 } else {
940 $sql .= $hookmanager->resPrint;
941 }
942
943 if (!$sql) {
944 return -1;
945 }
946
947 //print $sql;
948 dol_syslog(get_class($this)."::get_element_list", LOG_DEBUG);
949 $result = $this->db->query($sql);
950 if ($result) {
951 $nump = $this->db->num_rows($result);
952 if ($nump) {
953 $i = 0;
954 while ($i < $nump) {
955 $obj = $this->db->fetch_object($result);
956
957 $elements[$i] = $obj->rowid.(empty($obj->fk_user) ? '' : '_'.$obj->fk_user);
958
959 $i++;
960 }
961 $this->db->free($result);
962 }
963
964 /* Return array even if empty*/
965 return $elements;
966 } else {
967 dol_print_error($this->db);
968 }
969 return -1;
970 }
971
979 public function delete($user, $notrigger = 0)
980 {
981 global $langs, $conf;
982 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
983
984 $error = 0;
985
986 $this->db->begin();
987
988 if (!$error) {
989 // Delete linked contacts
990 $res = $this->delete_linked_contact();
991 if ($res < 0) {
992 $this->error = 'ErrorFailToDeleteLinkedContact';
993 //$error++;
994 $this->db->rollback();
995 return 0;
996 }
997 }
998
999 // Set fk_projet into elements to null
1000 $listoftables = array(
1001 'propal' => 'fk_projet',
1002 'commande' => 'fk_projet',
1003 'facture' => 'fk_projet',
1004 'supplier_proposal' => 'fk_projet',
1005 'commande_fournisseur' => 'fk_projet',
1006 'facture_fourn' => 'fk_projet',
1007 'expensereport_det' => 'fk_projet',
1008 'contrat' => 'fk_projet',
1009 'fichinter' => 'fk_projet',
1010 'don' => array('field' => 'fk_projet', 'module' => 'don'),
1011 'actioncomm' => 'fk_project',
1012 'mrp_mo' => array('field' => 'fk_project', 'module' => 'mrp'),
1013 'entrepot' => 'fk_project',
1014 );
1015 foreach ($listoftables as $key => $value) {
1016 if (is_array($value)) {
1017 if (!isModEnabled($value['module'])) {
1018 continue;
1019 }
1020 $fieldname = $value['field'];
1021 } else {
1022 $fieldname = $value;
1023 }
1024 $sql = "UPDATE ".MAIN_DB_PREFIX.$this->db->sanitize($key)." SET ".$this->db->sanitize($fieldname)." = NULL WHERE ".$this->db->sanitize($fieldname)." = ".((int) $this->id);
1025
1026 $resql = $this->db->query($sql);
1027 if (!$resql) {
1028 $this->errors[] = $this->db->lasterror();
1029 $error++;
1030 break;
1031 }
1032 }
1033
1034 // Remove linked categories.
1035 if (!$error) {
1036 $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_project";
1037 $sql .= " WHERE fk_project = ".((int) $this->id);
1038
1039 $result = $this->db->query($sql);
1040 if (!$result) {
1041 $error++;
1042 $this->errors[] = $this->db->lasterror();
1043 }
1044 }
1045
1046 // Fetch tasks
1047 $this->getLinesArray($user, 0);
1048
1049 // Delete tasks
1050 $ret = $this->deleteTasks($user);
1051 if ($ret < 0) {
1052 $error++;
1053 }
1054
1055
1056 // Delete all child tables
1057 if (!$error) {
1058 $elements = array('categorie_project'); // elements to delete. TODO Make goodway to delete
1059 foreach ($elements as $table) {
1060 if (!$error) {
1061 $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->db->sanitize($table);
1062 $sql .= " WHERE fk_project = ".((int) $this->id);
1063
1064 $result = $this->db->query($sql);
1065 if (!$result) {
1066 $error++;
1067 $this->errors[] = $this->db->lasterror();
1068 }
1069 }
1070 }
1071 }
1072
1073 if (!$error) {
1074 $sql = "DELETE FROM ".MAIN_DB_PREFIX."projet_extrafields";
1075 $sql .= " WHERE fk_object = ".((int) $this->id);
1076
1077 $resql = $this->db->query($sql);
1078 if (!$resql) {
1079 $this->errors[] = $this->db->lasterror();
1080 $error++;
1081 }
1082 }
1083
1084 // Delete project
1085 if (!$error) {
1086 $sql = "DELETE FROM ".MAIN_DB_PREFIX."projet";
1087 $sql .= " WHERE rowid=".((int) $this->id);
1088
1089 $resql = $this->db->query($sql);
1090 if (!$resql) {
1091 $this->errors[] = $langs->trans("CantRemoveProject", $langs->transnoentitiesnoconv("ProjectOverview"));
1092 $error++;
1093 }
1094 }
1095
1096
1097
1098 if (empty($error)) {
1099 // We remove directory
1100 $projectref = dol_sanitizeFileName($this->ref);
1101 if ($conf->project->dir_output) {
1102 $dir = $conf->project->dir_output."/".$projectref;
1103 if (file_exists($dir)) {
1104 $res = @dol_delete_dir_recursive($dir);
1105 if (!$res) {
1106 $this->errors[] = 'ErrorFailToDeleteDir';
1107 $error++;
1108 }
1109 }
1110 }
1111
1112 if (!$notrigger) {
1113 // Call trigger
1114 $result = $this->call_trigger('PROJECT_DELETE', $user);
1115
1116 if ($result < 0) {
1117 $error++;
1118 }
1119 // End call triggers
1120 }
1121 }
1122
1123 if (empty($error)) {
1124 $this->db->commit();
1125 return 1;
1126 } else {
1127 foreach ($this->errors as $errmsg) {
1128 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
1129 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
1130 }
1131 dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR);
1132 $this->db->rollback();
1133 return -1;
1134 }
1135 }
1136
1145 public function getElementCount($type, $tablename, $projectkey = 'fk_projet')
1146 {
1147 if ($this->id <= 0) {
1148 return 0;
1149 }
1150
1151 if ($type == 'agenda') {
1152 $sql = "SELECT COUNT(id) as nb FROM ".MAIN_DB_PREFIX."actioncomm WHERE fk_project = ".((int) $this->id)." AND entity IN (".getEntity('agenda').")";
1153 } elseif ($type == 'expensereport') {
1154 $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);
1155 } elseif ($type == 'project_task') {
1156 $sql = "SELECT DISTINCT COUNT(pt.rowid) as nb FROM ".MAIN_DB_PREFIX."projet_task as pt WHERE pt.fk_projet = ".((int) $this->id);
1157 } elseif ($type == 'element_time') { // Case we want to duplicate line foreach user
1158 $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);
1159 } elseif ($type == 'stock_mouvement') {
1160 $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";
1161 } elseif ($type == 'loan') {
1162 $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);
1163 } else {
1164 $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).")";
1165 }
1166
1167 $result = $this->db->query($sql);
1168
1169 if (!$result) {
1170 return 0;
1171 }
1172
1173 $obj = $this->db->fetch_object($result);
1174
1175 $this->db->free($result);
1176
1177 return $obj->nb;
1178 }
1179
1186 public function deleteTasks($user)
1187 {
1188 $countTasks = count($this->lines);
1189 $deleted = false;
1190 if ($countTasks) {
1191 foreach ($this->lines as $task) {
1192 if ($task->hasChildren() <= 0) { // If there is no children (or error to detect them)
1193 $deleted = true;
1194 $ret = $task->delete($user);
1195 if ($ret <= 0) {
1196 $this->errors[] = $this->db->lasterror();
1197 return -1;
1198 }
1199 }
1200 }
1201 }
1202 $this->getLinesArray($user);
1203 if ($deleted && count($this->lines) < $countTasks) {
1204 if (count($this->lines)) {
1205 $this->deleteTasks($user);
1206 }
1207 }
1208
1209 return 1;
1210 }
1211
1219 public function setValid($user, $notrigger = 0)
1220 {
1221 global $langs;
1222
1223 $error = 0;
1224
1225 // Protection
1226 if ($this->status == self::STATUS_VALIDATED) {
1227 dol_syslog(get_class($this)."::validate action abandoned: already validated", LOG_WARNING);
1228 return 0;
1229 }
1230
1231 // Check parameters
1232 if (preg_match('/^'.preg_quote($langs->trans("CopyOf").' ', '/').'/', $this->title)) {
1233 $this->error = $langs->trans("ErrorFieldFormat", $langs->transnoentities("Label")).'. '.$langs->trans('RemoveString', $langs->transnoentitiesnoconv("CopyOf"));
1234 return -1;
1235 }
1236
1237 $this->db->begin();
1238
1239 $sql = "UPDATE ".MAIN_DB_PREFIX."projet";
1240 $sql .= " SET fk_statut = ".self::STATUS_VALIDATED;
1241 $sql .= " WHERE rowid = ".((int) $this->id);
1242 //$sql .= " AND entity = ".((int) $conf->entity); // Disabled, when we use the ID for the where, we must not add any other search condition
1243
1244 dol_syslog(get_class($this)."::setValid", LOG_DEBUG);
1245 $resql = $this->db->query($sql);
1246 if ($resql) {
1247 // Call trigger
1248 if (empty($notrigger)) {
1249 $result = $this->call_trigger('PROJECT_VALIDATE', $user);
1250 if ($result < 0) {
1251 $error++;
1252 }
1253 // End call triggers
1254 }
1255
1256 if (!$error) {
1257 $this->statut = 1;
1258 $this->status = 1;
1259 $this->db->commit();
1260 return 1;
1261 } else {
1262 $this->db->rollback();
1263 $this->error = implode(',', $this->errors);
1264 dol_syslog(get_class($this)."::setValid ".$this->error, LOG_ERR);
1265 return -1;
1266 }
1267 } else {
1268 $this->db->rollback();
1269 $this->error = $this->db->lasterror();
1270 return -1;
1271 }
1272 }
1273
1280 public function setClose($user)
1281 {
1282 $now = dol_now();
1283
1284 $error = 0;
1285
1286 if ($this->status != self::STATUS_CLOSED) {
1287 $this->db->begin();
1288
1289 $sql = "UPDATE ".MAIN_DB_PREFIX."projet";
1290 $sql .= " SET fk_statut = ".self::STATUS_CLOSED.", fk_user_close = ".((int) $user->id).", date_close = '".$this->db->idate($now)."'";
1291 $sql .= " WHERE rowid = ".((int) $this->id);
1292 $sql .= " AND fk_statut = ".self::STATUS_VALIDATED;
1293
1294 if (getDolGlobalString('PROJECT_USE_OPPORTUNITIES')) {
1295 // TODO What to do if fk_opp_status is not code 'WON' or 'LOST'
1296 }
1297
1298 dol_syslog(get_class($this)."::setClose", LOG_DEBUG);
1299 $resql = $this->db->query($sql);
1300 if ($resql) {
1301 // Call trigger
1302 $result = $this->call_trigger('PROJECT_CLOSE', $user);
1303 if ($result < 0) {
1304 $error++;
1305 }
1306 // End call triggers
1307
1308 if (!$error) {
1309 $this->status = 2;
1310 $this->db->commit();
1311 return 1;
1312 } else {
1313 $this->db->rollback();
1314 $this->error = implode(',', $this->errors);
1315 dol_syslog(get_class($this)."::setClose ".$this->error, LOG_ERR);
1316 return -1;
1317 }
1318 } else {
1319 $this->db->rollback();
1320 $this->error = $this->db->lasterror();
1321 return -1;
1322 }
1323 }
1324
1325 return 0;
1326 }
1327
1334 public function getLibStatut($mode = 0)
1335 {
1336 return $this->LibStatut(isset($this->status) ? $this->status : $this->statut, $mode);
1337 }
1338
1339 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1347 public function LibStatut($status, $mode = 0)
1348 {
1349 // phpcs:enable
1350 global $langs;
1351
1352 if (is_null($status)) {
1353 return '';
1354 }
1355
1356 $statustrans = array(
1357 0 => 'status0',
1358 1 => 'status4',
1359 2 => 'status6',
1360 );
1361
1362 $statusClass = 'status0';
1363 if (!empty($statustrans[$status])) {
1364 $statusClass = $statustrans[$status];
1365 }
1366
1367 return dolGetStatus($langs->transnoentitiesnoconv($this->labelStatus[$status]), $langs->transnoentitiesnoconv($this->labelStatusShort[$status]), '', $statusClass, $mode);
1368 }
1369
1376 public function getTooltipContentArray($params)
1377 {
1378 global $conf, $langs;
1379
1380 $langs->load('projects');
1381 $option = $params['option'] ?? '';
1382 $moreinpopup = $params['moreinpopup'] ?? '';
1383
1384 $datas = [];
1385 if ($option != 'nolink') {
1386 $datas['picto'] = img_picto('', $this->picto, 'class="pictofixedwidth"').' <u class="paddingrightonly">'.$langs->trans("Project").'</u>';
1387 }
1388 if (isset($this->status)) {
1389 $datas['picto'] .= ' '.$this->getLibStatut(5);
1390 }
1391 $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
1392 $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
1393 if (isset($this->public)) {
1394 $datas['visibility'] = '<br><b>'.$langs->trans("Visibility").":</b> ";
1395 $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"));
1396 }
1397 if (!empty($this->thirdparty_name)) {
1398 $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
1399 }
1400 if (!empty($this->date_start)) {
1401 $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
1402 }
1403 if (!empty($this->date_end)) {
1404 $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
1405 }
1406 if ($moreinpopup) {
1407 $datas['moreinpopup'] = '<br>'.$moreinpopup;
1408 }
1409
1410 return $datas;
1411 }
1412
1427 public function getNomUrl($withpicto = 0, $option = '', $addlabel = 0, $moreinpopup = '', $sep = ' - ', $notooltip = 0, $save_lastsearch_value = -1, $morecss = '', $save_pageforbacktolist = '')
1428 {
1429 global $conf, $langs, $user, $hookmanager;
1430
1431 if (!empty($conf->dol_no_mouse_hover)) {
1432 $notooltip = 1; // Force disable tooltips
1433 }
1434
1435 $result = '';
1436 if (getDolGlobalString('PROJECT_OPEN_ALWAYS_ON_TAB')) {
1437 $option = getDolGlobalString('PROJECT_OPEN_ALWAYS_ON_TAB');
1438 }
1439 $params = [
1440 'id' => $this->id,
1441 'objecttype' => $this->element,
1442 'moreinpopup' => $moreinpopup,
1443 'option' => $option,
1444 ];
1445 $classfortooltip = 'classfortooltip';
1446 $dataparams = '';
1447 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
1448 $classfortooltip = 'classforajaxtooltip';
1449 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
1450 $label = '';
1451 } else {
1452 $label = implode($this->getTooltipContentArray($params));
1453 }
1454
1455 $url = '';
1456 if ($option != 'nolink') {
1457 if (preg_match('/\.php$/', $option)) {
1458 $url = dol_buildpath($option, 1).'?id='.$this->id;
1459 } elseif ($option == 'task') {
1460 $url = DOL_URL_ROOT.'/projet/tasks.php?id='.$this->id;
1461 } elseif ($option == 'preview') {
1462 $url = DOL_URL_ROOT.'/projet/element.php?id='.$this->id;
1463 } elseif ($option == 'eventorganization') {
1464 $url = DOL_URL_ROOT.'/eventorganization/conferenceorbooth_list.php?projectid='.$this->id;
1465 } elseif ($option == 'mailing') {
1466 $url = DOL_URL_ROOT.'/comm/mailing/list.php?projectid='.$this->id;
1467 } else {
1468 $url = DOL_URL_ROOT.'/projet/card.php?id='.$this->id;
1469 }
1470 // Add param to save lastsearch_values or not
1471 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1472 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1473 $add_save_lastsearch_values = 1;
1474 }
1475 if ($add_save_lastsearch_values) {
1476 $url .= '&save_lastsearch_values=1';
1477 }
1478 $add_save_backpagefor = ($save_pageforbacktolist ? 1 : 0);
1479 if ($add_save_backpagefor) {
1480 $url .= "&save_pageforbacktolist=".urlencode($save_pageforbacktolist);
1481 }
1482 }
1483
1484 $linkclose = '';
1485 if (empty($notooltip) && $user->hasRight('projet', 'lire')) {
1486 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1487 $label = $langs->trans("ShowProject");
1488 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label).'"';
1489 }
1490 $linkclose .= ($label ? ' title="'.dolPrintHTMLForAttribute($label).'"' : ' title="tocomplete"');
1491 $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
1492 } else {
1493 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1494 }
1495
1496 $picto = 'projectpub';
1497 if (!$this->public) {
1498 $picto = 'project';
1499 }
1500
1501 $linkstart = '<a href="'.$url.'"';
1502 $linkstart .= $linkclose.'>';
1503 $linkend = '</a>';
1504
1505 $result .= $linkstart;
1506 if ($withpicto) {
1507 $result .= img_object(($notooltip ? '' : $label), $picto, 'class="pictofixedwidth em088"', 0, 0, $notooltip ? 0 : 1);
1508 }
1509 if ($withpicto != 2) {
1510 if ($addlabel >= 0) {
1511 $result .= $this->ref;
1512 } else {
1513 $result .= $this->title;
1514 }
1515 }
1516 $result .= $linkend;
1517 if ($withpicto != 2) {
1518 $result .= (($addlabel > 0 && $this->title) ? '<span class="opacitymedium">'.$sep.dol_trunc($this->title, ($addlabel > 1 ? $addlabel : 0)).'</span>' : '');
1519 }
1520
1521 global $action;
1522 $hookmanager->initHooks(array('projectdao'));
1523 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
1524 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1525 if ($reshook > 0) {
1526 $result = $hookmanager->resPrint;
1527 } else {
1528 $result .= $hookmanager->resPrint;
1529 }
1530
1531 return $result;
1532 }
1533
1541 public function initAsSpecimen()
1542 {
1543 global $user, $langs, $conf;
1544
1545 $now = dol_now();
1546
1547 // Initialise parameters
1548 $this->id = 0;
1549 $this->ref = 'SPECIMEN';
1550 $this->entity = $conf->entity;
1551 $this->specimen = 1;
1552 $this->socid = 1;
1553 $this->date_c = $now;
1554 $this->date_m = $now;
1555 $this->date_start = $now;
1556 $this->date_end = $now + (3600 * 24 * 365);
1557 $this->note_public = 'SPECIMEN';
1558 $this->note_private = 'Private Note';
1559 $this->fk_project = 0;
1560 $this->opp_amount = 20000;
1561 $this->budget_amount = 10000;
1562
1563 $this->usage_opportunity = 1;
1564 $this->usage_task = 1;
1565 $this->usage_bill_time = 1;
1566 $this->usage_organize_event = 1;
1567
1568 /*
1569 $nbp = mt_rand(1, 9);
1570 $xnbp = 0;
1571 while ($xnbp < $nbp)
1572 {
1573 $line = new Task($this->db);
1574 $line->fk_project = 0;
1575 $line->label = $langs->trans("Label") . " " . $xnbp;
1576 $line->description = $langs->trans("Description") . " " . $xnbp;
1577
1578 $this->lines[]=$line;
1579 $xnbp++;
1580 }
1581 */
1582
1583 return 1;
1584 }
1585
1593 public function restrictedProjectArea(User $user, $mode = 'read')
1594 {
1595 // To verify role of users
1596 $userAccess = 0;
1597 if (($mode == 'read' && $user->hasRight('projet', 'all', 'lire')) || ($mode == 'write' && $user->hasRight('projet', 'all', 'creer')) || ($mode == 'delete' && $user->hasRight('projet', 'all', 'supprimer'))) {
1598 $userAccess = 1;
1599 } elseif ($this->public && (($mode == 'read' && $user->hasRight('projet', 'lire')) || ($mode == 'write' && $user->hasRight('projet', 'creer')) || ($mode == 'delete' && $user->hasRight('projet', 'supprimer')))) {
1600 $userAccess = 1;
1601 } else { // No access due to permission to read all projects, so we check if we are a contact of project
1602 foreach (array('internal', 'external') as $source) {
1603 $userRole = $this->liste_contact(4, $source);
1604 $num = count($userRole);
1605
1606 $nblinks = 0;
1607 while ($nblinks < $num) {
1608 if ($source == 'internal' && $user->id == $userRole[$nblinks]['id']) { // $userRole[$nblinks]['id'] is id of user (llx_user) for internal contacts
1609 if ($mode == 'read' && $user->hasRight('projet', 'lire')) {
1610 $userAccess++;
1611 }
1612 if ($mode == 'write' && $user->hasRight('projet', 'creer')) {
1613 $userAccess++;
1614 }
1615 if ($mode == 'delete' && $user->hasRight('projet', 'supprimer')) {
1616 $userAccess++;
1617 }
1618 }
1619 if ($source == 'external' && $user->socid > 0 && $user->socid == $userRole[$nblinks]['socid']) { // $userRole[$nblinks]['id'] is id of contact (llx_socpeople) or external contacts
1620 if ($mode == 'read' && $user->hasRight('projet', 'lire')) {
1621 $userAccess++;
1622 }
1623 if ($mode == 'write' && $user->hasRight('projet', 'creer')) {
1624 $userAccess++;
1625 }
1626 if ($mode == 'delete' && $user->hasRight('projet', 'supprimer')) {
1627 $userAccess++;
1628 }
1629 }
1630 $nblinks++;
1631 }
1632 }
1633 //if (empty($nblinks)) // If nobody has permission, we grant creator
1634 //{
1635 // if ((!empty($this->user_author_id) && $this->user_author_id == $user->id))
1636 // {
1637 // $userAccess = 1;
1638 // }
1639 //}
1640 }
1641
1642 return ($userAccess ? $userAccess : -1);
1643 }
1644
1655 public function getProjectsAuthorizedForUser($fuser, $mode = 0, $list = 0, $socid = 0, $filter = '')
1656 {
1657 $projects = array();
1658 $temp = array();
1659
1660 // Get id of types of contacts for projects (This list never contains a lot of elements)
1661 $listofprojectcontacttype = array();
1662 $sql2 = "SELECT ctc.rowid, ctc.code FROM ".MAIN_DB_PREFIX."c_type_contact as ctc";
1663 $sql2 .= " WHERE ctc.element = '".$this->db->escape($this->element)."'";
1664 $sql2 .= " AND ctc.source = 'internal'";
1665 $resql = $this->db->query($sql2);
1666 if ($resql) {
1667 while ($obj = $this->db->fetch_object($resql)) {
1668 $listofprojectcontacttype[$obj->rowid] = $obj->code;
1669 }
1670 } else {
1671 dol_print_error($this->db);
1672 }
1673 if (count($listofprojectcontacttype) == 0) {
1674 $listofprojectcontacttype[0] = '0'; // To avoid syntax error if not found
1675 }
1676
1677
1678 $sql = "SELECT p.rowid, p.ref";
1679 $sql .= " FROM ".MAIN_DB_PREFIX."projet as p";
1680 $sql .= " WHERE p.entity IN (".getEntity('project').")";
1681 // Internal users must see project he is contact to even if project is linked to a third party he can't see.
1682 if ($socid > 0) {
1683 $sql .= " AND (p.fk_soc IS NULL OR p.fk_soc = 0 OR p.fk_soc = ".((int) $socid).")";
1684 }
1685
1686 if ($mode == 0) {
1687 $sql .= " AND (p.public = 1";
1688 $sql .= " OR EXISTS (SELECT ec.rowid FROM ".MAIN_DB_PREFIX."element_contact as ec";
1689 $sql .= " WHERE ec.element_id = p.rowid AND ec.fk_c_type_contact IN (".$this->db->sanitize(implode(',', array_keys($listofprojectcontacttype))).")";
1690 $sql .= " AND ec.fk_socpeople = ".((int) $fuser->id).")";
1691 $sql .= ")";
1692 } elseif ($mode == 1) {
1693 $sql .= " AND EXISTS (SELECT ec.rowid FROM ".MAIN_DB_PREFIX."element_contact as ec";
1694 $sql .= " WHERE ec.element_id = p.rowid AND ec.fk_c_type_contact IN (".$this->db->sanitize(implode(',', array_keys($listofprojectcontacttype))).")";
1695 $sql .= " AND ec.fk_socpeople = ".((int) $fuser->id).")";
1696 } // elseif ($mode == 2) {
1697 // No filter. Use this if fuser has permission to see all project
1698 //}
1699
1700 // Manage filter
1701 $errormessage = '';
1702 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
1703 if ($errormessage) {
1704 $this->errors[] = $errormessage;
1705 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
1706 $sql .= $filter;
1707 }
1708
1709 $resql = $this->db->query($sql);
1710 if ($resql) {
1711 $num = $this->db->num_rows($resql);
1712 $i = 0;
1713 while ($i < $num) {
1714 $row = $this->db->fetch_row($resql);
1715 $projects[$row[0]] = $row[1];
1716 $temp[] = $row[0];
1717 $i++;
1718 }
1719
1720 $this->db->free($resql);
1721
1722 if ($list) {
1723 if (empty($temp)) {
1724 return '0';
1725 }
1726 $result = implode(',', $temp);
1727 return $result;
1728 }
1729 } else {
1730 dol_print_error($this->db);
1731 }
1732
1733 return $projects;
1734 }
1735
1751 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)
1752 {
1753 global $langs, $conf;
1754
1755 $error = 0;
1756 $clone_project_id = 0; // For static toolcheck
1757
1758 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);
1759
1760 $now = dol_mktime(0, 0, 0, idate('m', dol_now()), idate('d', dol_now()), idate('Y', dol_now()));
1761
1762 $clone_project = new Project($this->db);
1763
1764 $clone_project->context['createfromclone'] = 'createfromclone';
1765
1766 $this->db->begin();
1767
1768 // Load source object
1769 $clone_project->fetch($fromid);
1770 $clone_project->fetch_optionals();
1771 $clone_project->socid = ($newthirdpartyid > 0 ? $newthirdpartyid : 0);
1772 $clone_project->fetch_thirdparty();
1773
1774 $orign_dt_start = $clone_project->date_start;
1775 $orign_project_ref = $clone_project->ref;
1776
1777 $clone_project->id = 0;
1778 if ($move_date) {
1779 $clone_project->date_start = $now;
1780 if (!empty($clone_project->date_end)) {
1781 if (!empty($orign_dt_start)) {
1782 $clone_project->date_end += ($now - (int) $orign_dt_start);
1783 } elseif (!empty($clone_project->date_c)) {
1784 $clone_project->date_end += ($now - (int) $clone_project->date_c);
1785 }
1786 }
1787 }
1788
1789 $clone_project->date_c = $now;
1790
1791 if (!$clone_note) {
1792 $clone_project->note_private = '';
1793 $clone_project->note_public = '';
1794 }
1795
1796 //Generate next ref
1797 $defaultref = '';
1798 $obj = getDolGlobalString('PROJECT_ADDON', 'mod_project_simple');
1799 // Search template files
1800 $file = '';
1801 $classname = '';
1802 $filefound = 0;
1803 $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
1804 foreach ($dirmodels as $reldir) {
1805 $file = dol_buildpath($reldir."core/modules/project/".$obj.'.php', 0);
1806 if (file_exists($file)) {
1807 $filefound = 1;
1808 dol_include_once($reldir."core/modules/project/".$obj.'.php');
1809 $modProject = new $obj();
1810 '@phan-var-force ModeleNumRefProjects $modProject';
1811 $defaultref = $modProject->getNextValue(is_object($clone_project->thirdparty) ? $clone_project->thirdparty : null, $clone_project);
1812 break;
1813 }
1814 }
1815 if (is_numeric($defaultref) && $defaultref <= 0) {
1816 $defaultref = '';
1817 }
1818
1819 $clone_project->ref = $defaultref;
1820 $clone_project->title = $langs->trans("CopyOf").' '.$clone_project->title;
1821
1822 // Create clone
1823 $result = $clone_project->create($user, $notrigger);
1824
1825 // Other options
1826 if ($result < 0) {
1827 $this->error .= $clone_project->error;
1828 $error++;
1829 }
1830
1831 if (!$error) {
1832 //Get the new project id
1833 $clone_project_id = $clone_project->id;
1834
1835 //Note Update
1836 if (!$clone_note) {
1837 $clone_project->note_private = '';
1838 $clone_project->note_public = '';
1839 } else {
1840 $this->db->begin();
1841 $res = $clone_project->update_note(dol_html_entity_decode($clone_project->note_public, ENT_QUOTES | ENT_HTML5), '_public');
1842 if ($res < 0) {
1843 $this->error .= $clone_project->error;
1844 $error++;
1845 $this->db->rollback();
1846 } else {
1847 $this->db->commit();
1848 }
1849
1850 $this->db->begin();
1851 $res = $clone_project->update_note(dol_html_entity_decode($clone_project->note_private, ENT_QUOTES | ENT_HTML5), '_private');
1852 if ($res < 0) {
1853 $this->error .= $clone_project->error;
1854 $error++;
1855 $this->db->rollback();
1856 } else {
1857 $this->db->commit();
1858 }
1859 }
1860
1861 //Duplicate contact
1862 if ($clone_contact) {
1863 $origin_project = new Project($this->db);
1864 $origin_project->fetch($fromid);
1865
1866 foreach (array('internal', 'external') as $source) {
1867 $tab = $origin_project->liste_contact(-1, $source);
1868 if (is_array($tab) && count($tab) > 0) {
1869 foreach ($tab as $contacttoadd) {
1870 $clone_project->add_contact($contacttoadd['id'], $contacttoadd['code'], $contacttoadd['source'], $notrigger);
1871 if ($clone_project->error == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
1872 $langs->load("errors");
1873 $this->error .= $langs->trans("ErrorThisContactIsAlreadyDefinedAsThisType");
1874 $error++;
1875 } else {
1876 if ($clone_project->error != '') {
1877 $this->error .= $clone_project->error;
1878 $error++;
1879 }
1880 }
1881 }
1882 } elseif ($tab < 0) {
1883 $this->error .= $origin_project->error;
1884 $error++;
1885 }
1886 }
1887 }
1888
1889 //Duplicate file
1890 if ($clone_project_file) {
1891 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1892
1893 $clone_project_dir = $conf->project->dir_output."/".dol_sanitizeFileName($defaultref);
1894 $ori_project_dir = $conf->project->dir_output."/".dol_sanitizeFileName($orign_project_ref);
1895
1896 if (dol_mkdir($clone_project_dir) >= 0) {
1897 $filearray = dol_dir_list($ori_project_dir, "files", 0, '', '(\.meta|_preview.*\.png)$', '', SORT_ASC, 1);
1898 foreach ($filearray as $key => $file) {
1899 $rescopy = dol_copy($ori_project_dir.'/'.$file['name'], $clone_project_dir.'/'.$file['name'], '0', 1);
1900 if (is_numeric($rescopy) && $rescopy < 0) {
1901 $this->error .= $langs->trans("ErrorFailToCopyFile", $ori_project_dir.'/'.$file['name'], $clone_project_dir.'/'.$file['name']);
1902 $error++;
1903 }
1904 }
1905 } else {
1906 $this->error .= $langs->trans('ErrorInternalErrorDetected').':dol_mkdir';
1907 $error++;
1908 }
1909 }
1910
1911 //Duplicate task
1912 if ($clone_task) {
1913 require_once DOL_DOCUMENT_ROOT.'/projet/class/task.class.php';
1914
1915 $taskstatic = new Task($this->db);
1916
1917 // Security check
1918 $socid = 0;
1919 if ($user->socid > 0) {
1920 $socid = $user->socid;
1921 }
1922
1923 $tasksarray = $taskstatic->getTasksArray(null, null, $fromid, $socid, 0);
1924
1925 $tab_conv_child_parent = array();
1926 $result_clone = 0;
1927
1928 // Loop on each task, to clone it
1929 foreach ($tasksarray as $tasktoclone) {
1930 $result_clone = $taskstatic->createFromClone($user, $tasktoclone->id, $clone_project_id, $tasktoclone->fk_task_parent, $move_date, true, false, $clone_task_file, true, false);
1931 if ($result_clone <= 0) {
1932 $this->error .= $taskstatic->error;
1933 $error++;
1934 } else {
1935 $new_task_id = $result_clone;
1936 $taskstatic->fetch($tasktoclone->id);
1937
1938 //manage new parent clone task id
1939 // if the current task has child we store the original task id and the equivalent clone task id
1940 if (($taskstatic->hasChildren()) && !array_key_exists($tasktoclone->id, $tab_conv_child_parent)) {
1941 $tab_conv_child_parent[$tasktoclone->id] = $new_task_id;
1942 }
1943 }
1944 }
1945
1946 //Parse all clone node to be sure to update new parent
1947 $tasksarray = $taskstatic->getTasksArray(null, null, $clone_project_id, $socid, 0);
1948 foreach ($tasksarray as $task_cloned) {
1949 $taskstatic->fetch($task_cloned->id);
1950 if ($taskstatic->fk_task_parent != 0) {
1951 $taskstatic->fk_task_parent = $tab_conv_child_parent[$taskstatic->fk_task_parent];
1952 }
1953 $res = $taskstatic->update($user, $notrigger);
1954 if ($result_clone <= 0) {
1955 $this->error .= $taskstatic->error;
1956 $error++;
1957 }
1958 }
1959 }
1960 }
1961
1962 unset($clone_project->context['createfromclone']);
1963
1964 if (!$error && $clone_project_id != 0) {
1965 $this->db->commit();
1966 return $clone_project_id;
1967 } else {
1968 $this->db->rollback();
1969 dol_syslog(get_class($this)."::createFromClone nbError: ".$error." error : ".$this->error, LOG_ERR);
1970 return -1;
1971 }
1972 }
1973
1974
1981 public function shiftTaskDate($old_project_dt_start)
1982 {
1983 global $user;
1984
1985 $error = 0;
1986 $result = 0;
1987
1988 $taskstatic = new Task($this->db);
1989
1990 // Security check
1991 $socid = 0;
1992 if ($user->socid > 0) {
1993 $socid = $user->socid;
1994 }
1995
1996 $tasksarray = $taskstatic->getTasksArray(null, null, $this->id, $socid, 0);
1997
1998 foreach ($tasksarray as $tasktoshiftdate) {
1999 $to_update = false;
2000 // Fetch only if update of date will be made
2001 if ((!empty($tasktoshiftdate->date_start)) || (!empty($tasktoshiftdate->date_end))) {
2002 //dol_syslog(get_class($this)."::shiftTaskDate to_update", LOG_DEBUG);
2003 $to_update = true;
2004 $task = new Task($this->db);
2005 $result = $task->fetch($tasktoshiftdate->id);
2006 if (!$result) {
2007 $error++;
2008 $this->error .= $task->error;
2009 }
2010 } else {
2011 continue;
2012 }
2013 //print "$this->date_start + $tasktoshiftdate->date_start - $old_project_dt_start";exit;
2014
2015 //Calculate new task start date with difference between old proj start date and origin task start date
2016 if (!empty($tasktoshiftdate->date_start)) {
2017 $task->date_start = $this->date_start + ($tasktoshiftdate->date_start - $old_project_dt_start);
2018 }
2019
2020 //Calculate new task end date with difference between origin proj end date and origin task end date
2021 if (!empty($tasktoshiftdate->date_end)) {
2022 $task->date_end = $this->date_start + ($tasktoshiftdate->date_end - $old_project_dt_start);
2023 }
2024
2025 if ($to_update) {
2026 $result = $task->update($user);
2027 if (!$result) {
2028 $error++;
2029 $this->error .= $task->error;
2030 }
2031 }
2032 }
2033 if ($error != 0) {
2034 return -1;
2035 }
2036 return $result;
2037 }
2038
2039
2040 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2048 public function update_element($tableName, $elementSelectId)
2049 {
2050 // phpcs:enable
2051 $sql = "UPDATE ".MAIN_DB_PREFIX.$this->db->sanitize($tableName);
2052
2053 if ($tableName == "actioncomm") {
2054 $sql .= " SET fk_project = ".((int) $this->id);
2055 $sql .= " WHERE id = ".((int) $elementSelectId);
2056 } elseif (in_array($tableName, ["entrepot", "mrp_mo", "stocktransfer_stocktransfer"])) {
2057 $sql .= " SET fk_project = ".((int) $this->id);
2058 $sql .= " WHERE rowid = ".((int) $elementSelectId);
2059 } else {
2060 $sql .= " SET fk_projet = ".((int) $this->id);
2061 $sql .= " WHERE rowid = ".((int) $elementSelectId);
2062 }
2063
2064 dol_syslog(get_class($this)."::update_element", LOG_DEBUG);
2065
2066 $resql = $this->db->query($sql);
2067 if (!$resql) {
2068 $this->error = $this->db->lasterror();
2069 return -1;
2070 } else {
2071 return 1;
2072 }
2073 }
2074
2075 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2084 public function remove_element($tableName, $elementSelectId, $projectfield = 'fk_projet')
2085 {
2086 // phpcs:enable
2087 $sql = "UPDATE ".MAIN_DB_PREFIX.$this->db->sanitize($tableName);
2088
2089 if ($tableName == "actioncomm") {
2090 $sql .= " SET fk_project = NULL";
2091 $sql .= " WHERE id = ".((int) $elementSelectId);
2092 } else {
2093 $sql .= " SET ".$this->db->sanitize($projectfield)." = NULL";
2094 $sql .= " WHERE rowid = ".((int) $elementSelectId);
2095 }
2096
2097 dol_syslog(get_class($this)."::remove_element", LOG_DEBUG);
2098
2099 $resql = $this->db->query($sql);
2100 if (!$resql) {
2101 $this->error = $this->db->lasterror();
2102 return -1;
2103 } else {
2104 return 1;
2105 }
2106 }
2107
2118 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
2119 {
2120 global $conf, $langs;
2121
2122 $langs->load("projects");
2123
2124 if (!dol_strlen($modele)) {
2125 $modele = 'baleine';
2126
2127 if ($this->model_pdf) {
2128 $modele = $this->model_pdf;
2129 } elseif (getDolGlobalString('PROJECT_ADDON_PDF')) {
2130 $modele = getDolGlobalString('PROJECT_ADDON_PDF');
2131 }
2132 }
2133
2134 $modelpath = "core/modules/project/doc/";
2135
2136 return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
2137 }
2138
2139
2149 public function loadTimeSpent($datestart, $taskid = 0, $userid = 0)
2150 {
2151 $this->weekWorkLoad = array();
2152 $this->weekWorkLoadPerTask = array();
2153
2154 if (empty($datestart)) {
2155 dol_print_error(null, 'Error datestart parameter is empty');
2156 }
2157
2158 $sql = "SELECT ptt.rowid as taskid, ptt.element_duration, ptt.element_date, ptt.element_datehour, ptt.fk_element";
2159 $sql .= " FROM ".MAIN_DB_PREFIX."element_time AS ptt, ".MAIN_DB_PREFIX."projet_task as pt";
2160 $sql .= " WHERE ptt.fk_element = pt.rowid";
2161 $sql .= " AND ptt.elementtype = 'task'";
2162 $sql .= " AND pt.fk_projet = ".((int) $this->id);
2163 $sql .= " AND (ptt.element_date >= '".$this->db->idate($datestart)."' ";
2164 $sql .= " AND ptt.element_date <= '".$this->db->idate(dol_time_plus_duree($datestart, 1, 'w') - 1)."')";
2165 if ($taskid) {
2166 $sql .= " AND ptt.fk_element=".((int) $taskid);
2167 }
2168 if (is_numeric($userid)) {
2169 $sql .= " AND ptt.fk_user=".((int) $userid);
2170 }
2171
2172 //print $sql;
2173 $resql = $this->db->query($sql);
2174 if ($resql) {
2175 $dayallreadyfound = array();
2176
2177 $num = $this->db->num_rows($resql);
2178 $i = 0;
2179 // Loop on each record found, so each couple (project id, task id)
2180 while ($i < $num) {
2181 $obj = $this->db->fetch_object($resql);
2182 $day = $this->db->jdate($obj->element_date); // task_date is date without hours
2183
2184 if (empty($dayallreadyfound[$day])) {
2185 $this->weekWorkLoad[$day] = (int) $obj->element_duration; // Float in db used as int
2186 $this->weekWorkLoadPerTask[$day][$obj->fk_element] = (int) $obj->element_duration;
2187 } else {
2188 $this->weekWorkLoad[$day] += (int) $obj->element_duration; // Float in db used as int
2189 $this->weekWorkLoadPerTask[$day][$obj->fk_element] += (int) $obj->element_duration;
2190 }
2191 $dayallreadyfound[$day] = 1;
2192 $i++;
2193 }
2194 $this->db->free($resql);
2195 return $num;
2196 } else {
2197 $this->error = "Error ".$this->db->lasterror();
2198 dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
2199 return -1;
2200 }
2201 }
2202
2212 public function loadTimeSpentMonth($datestart, $taskid = 0, $userid = 0)
2213 {
2214 $this->monthWorkLoad = array();
2215 $this->monthWorkLoadPerTask = array();
2216
2217 if (empty($datestart)) {
2218 dol_print_error(null, 'Error datestart parameter is empty');
2219 }
2220
2221 $sql = "SELECT ptt.rowid as taskid, ptt.element_duration, ptt.element_date, ptt.element_datehour, ptt.fk_element";
2222 $sql .= " FROM ".MAIN_DB_PREFIX."element_time AS ptt, ".MAIN_DB_PREFIX."projet_task as pt";
2223 $sql .= " WHERE ptt.fk_element = pt.rowid";
2224 $sql .= " AND ptt.elementtype = 'task'";
2225 $sql .= " AND pt.fk_projet = ".((int) $this->id);
2226 $sql .= " AND (ptt.element_date >= '".$this->db->idate($datestart)."' ";
2227 $sql .= " AND ptt.element_date <= '".$this->db->idate(dol_time_plus_duree($datestart, 1, 'm') - 1)."')";
2228 if ($taskid) {
2229 $sql .= " AND ptt.fk_element=".((int) $taskid);
2230 }
2231 if (is_numeric($userid)) {
2232 $sql .= " AND ptt.fk_user=".((int) $userid);
2233 }
2234
2235 //print $sql;
2236 $resql = $this->db->query($sql);
2237 if ($resql) {
2238 $weekalreadyfound = array();
2239
2240 $num = $this->db->num_rows($resql);
2241 $i = 0;
2242 $week_number = ''; // Initialisation for static analysis
2243 // Loop on each record found, so each couple (project id, task id)
2244 while ($i < $num) {
2245 $obj = $this->db->fetch_object($resql);
2246 if (!empty($obj->element_date)) {
2247 $date = explode('-', $obj->element_date);
2248 $week_number = getWeekNumber((int) $date[2], (int) $date[1], (int) $date[0]);
2249 }
2250 if (empty($weekalreadyfound[$week_number])) {
2251 $this->monthWorkLoad[$week_number] = (int) $obj->element_duration;
2252 $this->monthWorkLoadPerTask[$week_number][$obj->fk_element] = (int) $obj->element_duration;
2253 } else {
2254 $this->monthWorkLoad[$week_number] += (int) $obj->element_duration;
2255 if (!isset($this->monthWorkLoadPerTask[$week_number][$obj->fk_element])) {
2256 $this->monthWorkLoadPerTask[$week_number][$obj->fk_element] = 0;
2257 }
2258 $this->monthWorkLoadPerTask[$week_number][$obj->fk_element] += (int) $obj->element_duration;
2259 }
2260 $weekalreadyfound[$week_number] = 1;
2261 $i++;
2262 }
2263 $this->db->free($resql);
2264 return 1;
2265 } else {
2266 $this->error = "Error ".$this->db->lasterror();
2267 dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
2268 return -1;
2269 }
2270 }
2271
2272 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2279 public function load_board($user)
2280 {
2281 // phpcs:enable
2282 global $conf, $langs;
2283
2284 // For external user, no check is done on company because readability is managed by public status of project and assignment.
2285 //$socid=$user->socid;
2286
2287 $response = new WorkboardResponse();
2288 $response->warning_delay = $conf->project->warning_delay / 60 / 60 / 24;
2289 $response->label = $langs->trans("OpenedProjects");
2290 $response->labelShort = $langs->trans("Opened");
2291 $response->url = DOL_URL_ROOT.'/projet/list.php?search_project_user=-1&search_status=1&mainmenu=project';
2292 $response->url_late = DOL_URL_ROOT.'/projet/list.php?search_option=late&mainmenu=project';
2293 $response->img = img_object('', "projectpub");
2294 $response->nbtodo = 0;
2295 $response->nbtodolate = 0;
2296
2297 $sql = "SELECT p.rowid, p.fk_statut as status, p.fk_opp_status, p.datee as datee";
2298 $sql .= " FROM (".MAIN_DB_PREFIX."projet as p";
2299 $sql .= ")";
2300 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s on p.fk_soc = s.rowid";
2301 // For external user, no check is done on company permission because readability is managed by public status of project and assignment.
2302 //if (! $user->rights->societe->client->voir && ! $socid) $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc = s.rowid";
2303 $sql .= " WHERE p.fk_statut = 1";
2304 $sql .= " AND p.entity IN (".getEntity('project').')';
2305
2306
2307 $projectsListId = null;
2308 if (!$user->hasRight("projet", "all", "lire")) {
2309 $response->url = DOL_URL_ROOT.'/projet/list.php?search_status=1&mainmenu=project';
2310 $projectsListId = $this->getProjectsAuthorizedForUser($user, 0, 1);
2311 if (empty($projectsListId)) {
2312 return $response;
2313 }
2314
2315 $sql .= " AND p.rowid IN (".$this->db->sanitize($projectsListId).")";
2316 }
2317
2318 // No need to check company, as filtering of projects must be done by getProjectsAuthorizedForUser
2319 //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).")";
2320 // For external user, no check is done on company permission because readability is managed by public status of project and assignment.
2321 //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))";
2322
2323 //print $sql;
2324 $resql = $this->db->query($sql);
2325 if ($resql) {
2326 $project_static = new Project($this->db);
2327
2328
2329 // This assignment in condition is not a bug. It allows walking the results.
2330 while ($obj = $this->db->fetch_object($resql)) {
2331 $response->nbtodo++;
2332
2333 $project_static->statut = $obj->status;
2334 $project_static->status = $obj->status;
2335 $project_static->opp_status = $obj->fk_opp_status;
2336 $project_static->date_end = $this->db->jdate($obj->datee);
2337
2338 if ($project_static->hasDelay()) {
2339 $response->nbtodolate++;
2340 }
2341 }
2342
2343 return $response;
2344 }
2345
2346 $this->error = $this->db->error();
2347 return -1;
2348 }
2349
2358 public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
2359 {
2360 $tables = array(
2361 'projet'
2362 );
2363
2364 return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
2365 }
2366
2367
2373 public function loadStateBoard()
2374 {
2375 global $user;
2376
2377 $this->nb = array();
2378
2379 $sql = "SELECT count(p.rowid) as nb";
2380 $sql .= " FROM ".MAIN_DB_PREFIX."projet as p";
2381 $sql .= " WHERE";
2382 $sql .= " p.entity IN (".getEntity('project').")";
2383 if (!$user->hasRight('projet', 'all', 'lire')) {
2384 $projectsListId = $this->getProjectsAuthorizedForUser($user, 0, 1);
2385 $sql .= "AND p.rowid IN (".$this->db->sanitize($projectsListId).")";
2386 }
2387
2388 $resql = $this->db->query($sql);
2389 if ($resql) {
2390 while ($obj = $this->db->fetch_object($resql)) {
2391 $this->nb["projects"] = $obj->nb;
2392 }
2393 $this->db->free($resql);
2394 return 1;
2395 } else {
2396 dol_print_error($this->db);
2397 $this->error = $this->db->error();
2398 return -1;
2399 }
2400 }
2401
2402
2408 public function hasDelay()
2409 {
2410 global $conf;
2411
2412 if (!($this->status == self::STATUS_VALIDATED)) {
2413 return false;
2414 }
2415 if (!$this->date_end) {
2416 return false;
2417 }
2418
2419 $now = dol_now();
2420
2421 return ($this->date_end) < ($now - $conf->project->warning_delay);
2422 }
2423
2424
2431 public function info($id)
2432 {
2433 $sql = 'SELECT c.rowid, datec as datec, tms as datem,';
2434 $sql .= ' date_close as datecloture,';
2435 $sql .= ' fk_user_creat as fk_user_author, fk_user_close as fk_user_cloture';
2436 $sql .= ' FROM '.MAIN_DB_PREFIX.'projet as c';
2437 $sql .= ' WHERE c.rowid = '.((int) $id);
2438 $result = $this->db->query($sql);
2439 if ($result) {
2440 if ($this->db->num_rows($result)) {
2441 $obj = $this->db->fetch_object($result);
2442
2443 $this->id = $obj->rowid;
2444
2445 $this->user_creation_id = $obj->fk_user_author;
2446 $this->user_closing_id = $obj->fk_user_cloture;
2447
2448 $this->date_creation = $this->db->jdate($obj->datec);
2449 $this->date_modification = $this->db->jdate($obj->datem);
2450 $this->date_cloture = $this->db->jdate($obj->datecloture);
2451 }
2452
2453 $this->db->free($result);
2454 } else {
2455 dol_print_error($this->db);
2456 }
2457 }
2458
2469 public function setCategories($categories)
2470 {
2471 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
2472 return parent::setCategoriesCommon($categories, Categorie::TYPE_PROJECT);
2473 }
2474
2475
2483 public function getLinesArray($user, $loadRoleMode = 1)
2484 {
2485 require_once DOL_DOCUMENT_ROOT.'/projet/class/task.class.php';
2486 $taskstatic = new Task($this->db);
2487
2488 $this->lines = $taskstatic->getTasksArray(null, $user, $this->id, 0, 0, '', '-1', '', 0, 0, null, 0, array(), 0, $loadRoleMode);
2489 return 1;
2490 }
2491
2510 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 = '')
2511 {
2512 // TODO EMAIL
2513
2514 return 1;
2515 }
2516
2525 public function getKanbanView($option = '', $arraydata = null, $size = '')
2526 {
2527 global $conf, $langs;
2528
2529 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
2530
2531 if (empty($size)) {
2532 if (empty($conf->dol_optimize_smallscreen)) {
2533 $size = 'large';
2534 } else {
2535 $size = 'small';
2536 }
2537 }
2538
2539 $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.'"' : '"').'>';
2540 $return .= '<div class="info-box info-box-sm">';
2541 $return .= '<span class="info-box-icon bg-infobox-action">';
2542 $return .= img_picto('', $this->public ? 'projectpub' : $this->picto);
2543 //$return .= '<i class="fa fa-dol-action"></i>'; // Can be image
2544 $return .= '</span>';
2545 $return .= '<div class="info-box-content">';
2546 $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref);
2547 if ($this->hasDelay()) {
2548 $return .= img_warning($langs->trans('Late'));
2549 }
2550 $return .= '</span>';
2551 if ($selected >= 0) {
2552 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
2553 }
2554 // Date
2555 /*
2556 if (property_exists($this, 'date_start') && $this->date_start) {
2557 $return .= '<br><span class="info-box-label">'.dol_print_date($this->date_start, 'day').'</>';
2558 }
2559 if (property_exists($this, 'date_end') && $this->date_end) {
2560 if ($this->date_start) {
2561 $return .= ' - ';
2562 } else {
2563 $return .= '<br>';
2564 }
2565 $return .= '<span class="info-box-label">'.dol_print_date($this->date_end, 'day').'</span>';
2566 }*/
2567 if (property_exists($this, 'thirdparty') && !is_null($this->thirdparty) && is_object($this->thirdparty) && $this->thirdparty instanceof Societe && $this->thirdparty->id > 0) {
2568 $return .= '<br><div class="info-box-ref tdoverflowmax125 inline-block valignmiddle">'.$this->thirdparty->getNomUrl(1);
2569 $return .= '</div>';
2570 if (!empty($this->thirdparty->phone)) {
2571 $return .= '<div class="inline-block valignmiddle">';
2572 $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
2573 $return .= '</div>';
2574 }
2575 if (!empty($this->thirdparty->email)) {
2576 $return .= '<div class="inline-block valignmiddle">';
2577 $return .= dol_print_email($this->thirdparty->email, 0, $this->thirdparty->id, 'thirdparty', -1, 1, 2, 'paddingleft paddingright');
2578 $return .= '</div>';
2579 }
2580 }
2581 if (!empty($arraydata['assignedusers'])) {
2582 $return .= '<br>';
2583 if ($this->public) {
2584 $return .= img_picto($langs->trans('Visibility').': '.$langs->trans('SharedProject'), 'world', 'class="paddingrightonly valignmiddle"');
2585 //print $langs->trans('SharedProject');
2586 } else {
2587 $return .= img_picto($langs->trans('Visibility').': '.$langs->trans('PrivateProject'), 'private', 'class="paddingrightonly valignmiddle"');
2588 //print $langs->trans('PrivateProject');
2589 }
2590
2591 $return .= ' <span class="small valignmiddle">'.$arraydata['assignedusers'].'</span>';
2592 }
2593 /*if (property_exists($this, 'user_author_id')) {
2594 $return .= '<br><span class="info-box-label opacitymedium">'.$langs->trans("Author").'</span>';
2595 $return .= '<span> : '.$user->getNomUrl(1).'</span>';
2596 }*/
2597 $return .= '<br><div>'; // start div line status
2598 if ($this->usage_opportunity && $this->opp_status_code) {
2599 //$return .= '<br><span class="info-bo-label opacitymedium">'.$langs->trans("OpportunityStatusShort").'</span>';
2600 //$return .= '<div class="small inline-block">'.dol_trunc($langs->trans("OppStatus".$this->opp_status_code), 5).'</div>';
2601 $return .= '<div class="opacitymedium small marginrightonly inline-block" title="'.dol_escape_htmltag($langs->trans("OppStatus".$this->opp_status_code)).'">'.round($this->opp_percent).'%</div>';
2602 $return .= ' <div class="amount small marginrightonly inline-block">'.price($this->opp_amount).'</div>';
2603 }
2604 if (method_exists($this, 'getLibStatut')) {
2605 $return .= '<div class="info-box-status small inline-block valignmiddle">'.$this->getLibStatut(3).'</div>';
2606 }
2607 $return .= '</div>'; // end div line status
2608
2609 $return .= '</div>';
2610 $return .= '</div>';
2611 $return .= '</div>';
2612
2613 return $return;
2614 }
2615
2621 public function getChildren()
2622 {
2623 $children = [];
2624 $sql = 'SELECT rowid,title';
2625 $sql .= ' FROM '.MAIN_DB_PREFIX.'projet';
2626 $sql .= ' WHERE fk_project = '.((int) $this->id);
2627 $sql .= ' ORDER BY title';
2628 $result = $this->db->query($sql);
2629 if ($result) {
2630 $n = $this->db->num_rows($result);
2631 while ($n) {
2632 $children[] = $this->db->fetch_object($result);
2633 $n--;
2634 }
2635 $this->db->free($result);
2636 } else {
2637 dol_print_error($this->db);
2638 }
2639
2640 return $children;
2641 }
2642
2648 public function createWeeklyReport()
2649 {
2650 global $mysoc, $user, $langs;
2651
2652 $now = dol_now();
2653 $nowDate = dol_getdate($now, true);
2654
2655 $errormesg = '';
2656 $errorsMsg = array();
2657
2658 $firstDayOfWeekTS = dol_get_first_day_week($nowDate['mday'], $nowDate['mon'], $nowDate['year']);
2659
2660 $firstDayOfWeekDate = dol_mktime(0, 0, 0, $nowDate['mon'], $firstDayOfWeekTS['first_day'], $nowDate['year']);
2661
2662 $lastWeekStartTS = dol_time_plus_duree($firstDayOfWeekDate, -7, 'd');
2663
2664 $lastWeekEndTS = dol_time_plus_duree($lastWeekStartTS, 6, 'd');
2665
2666 $startDate = dol_print_date($lastWeekStartTS, '%Y-%m-%d 00:00:00');
2667 $endDate = dol_print_date($lastWeekEndTS, '%Y-%m-%d 23:59:59');
2668
2669 $sql = "SELECT
2670 u.rowid AS user_id,
2671 CONCAT(u.firstname, ' ', u.lastname) AS name,
2672 u.email,u.weeklyhours,
2673 SUM(et.element_duration) AS total_seconds
2674 FROM
2675 ".MAIN_DB_PREFIX."element_time AS et
2676 JOIN
2677 ".MAIN_DB_PREFIX."user AS u ON et.fk_user = u.rowid
2678 WHERE
2679 et.element_date BETWEEN '".$this->db->escape($startDate)."' AND '".$this->db->escape($endDate)."'
2680 AND et.elementtype = 'task'
2681 GROUP BY
2682 et.fk_user";
2683
2684 $resql = $this->db->query($sql);
2685 if (!$resql) {
2686 dol_print_error($this->db);
2687 return -1;
2688 } else {
2689 $reportContent = "<span>Weekly time report from $startDate to $endDate </span><br><br>";
2690 $reportContent .= '<table border="1" style="border-collapse: collapse;">';
2691 $reportContent .= '<tr><th>'.$langs->trans("User").'</th><th>Temps saisi (heures)</th><th>Temps travaillé par semaine (heures)</th></tr>';
2692
2693 $to = '';
2694 $nbMailSend = 0;
2695 $error = 0;
2696 $errors_to = '';
2697 while ($obj = $this->db->fetch_object($resql)) {
2698 $to = $obj->email;
2699
2700 $weekendEnabled = 0;
2701 $numHolidays = num_public_holiday($lastWeekStartTS, $lastWeekEndTS, $mysoc->country_code, 1);
2702 if (getDolGlobalString('MAIN_NON_WORKING_DAYS_INCLUDE_MONDAY')) {
2703 $numHolidays -= 1;
2704 $weekendEnabled += 1;
2705 }
2706 if (getDolGlobalString('MAIN_NON_WORKING_DAYS_INCLUDE_TUESDAY')) {
2707 $numHolidays -= 1;
2708 $weekendEnabled += 1;
2709 }
2710 if (getDolGlobalString('MAIN_NON_WORKING_DAYS_INCLUDE_SATURDAY')) {
2711 $numHolidays -= 1;
2712 $weekendEnabled += 1;
2713 }
2714 if (getDolGlobalString('MAIN_NON_WORKING_DAYS_INCLUDE_SUNDAY')) {
2715 $numHolidays -= 1;
2716 $weekendEnabled += 1;
2717 }
2718
2719 $dailyHours = $obj->weeklyhours / (7 - $weekendEnabled);
2720
2721 // Adjust total on seconde
2722 $adjustedSeconds = $obj->total_seconds + ($numHolidays * $dailyHours * 3600);
2723
2724 $totalHours = round($adjustedSeconds / 3600, 2);
2725
2726 $reportContent .= "<tr><td>{$obj->name}</td><td>{$totalHours}</td><td>".round($obj->weeklyhours, 2)."</td></tr>";
2727
2728 $reportContent .= '</table>';
2729
2730 require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
2731
2732 // PREPARE EMAIL
2733 $errormesg = '';
2734
2735 $subject = 'Rapport hebdomadaire des temps travaillés';
2736
2737 $from = getDolGlobalString('MAIN_MAIL_EMAIL_FROM');
2738 if (empty($from)) {
2739 $errormesg = "Failed to get sender into global setup MAIN_MAIL_EMAIL_FROM";
2740 $error++;
2741 }
2742
2743 $mail = new CMailFile($subject, $to, $from, $reportContent, array(), array(), array(), '', '', 0, -1, '', '', '', 'text/html');
2744
2745 if ($mail->sendfile()) {
2746 $nbMailSend++;
2747
2748 // Add a line into event table
2749 require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
2750
2751 // Insert record of emails sent
2752 $actioncomm = new ActionComm($this->db);
2753
2754 $actioncomm->type_code = 'AC_OTH_AUTO'; // Event insert into agenda automatically
2755 $actioncomm->socid = $this->thirdparty->id; // To link to a company
2756 $actioncomm->contact_id = 0;
2757
2758 $actioncomm->code = 'AC_EMAIL';
2759 $actioncomm->label = 'createWeeklyReportOK()';
2760 $actioncomm->fk_project = $this->id;
2761 $actioncomm->datep = dol_now();
2762 $actioncomm->datef = $actioncomm->datep;
2763 $actioncomm->percentage = -1; // Not applicable
2764 $actioncomm->authorid = $user->id; // User saving action
2765 $actioncomm->userownerid = $user->id; // Owner of action
2766 // Fields when action is an email (content should be added into note)
2767 $actioncomm->email_msgid = $mail->msgid;
2768 $actioncomm->email_subject = $subject;
2769 $actioncomm->email_from = $from;
2770 $actioncomm->email_sender = '';
2771 $actioncomm->email_to = $to;
2772
2773 $actioncomm->errors_to = $errors_to;
2774
2775 $actioncomm->elementtype = 'project_task';
2776 $actioncomm->fk_element = (int) $this->element;
2777
2778 $actioncomm->create($user);
2779 } else {
2780 $errormesg = $mail->error.' : '.$to;
2781 $error++;
2782
2783 // Add a line into event table
2784 require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
2785
2786 // Insert record of emails sent
2787 $actioncomm = new ActionComm($this->db);
2788
2789 $actioncomm->type_code = 'AC_OTH_AUTO'; // Event insert into agenda automatically
2790 $actioncomm->socid = $this->thirdparty->id; // To link to a company
2791 $actioncomm->contact_id = 0;
2792
2793 $actioncomm->code = 'AC_EMAIL';
2794 $actioncomm->label = 'createWeeklyReportKO()';
2795 $actioncomm->note_private = $errormesg;
2796 $actioncomm->fk_project = $this->id;
2797 $actioncomm->datep = dol_now();
2798 $actioncomm->datef = $actioncomm->datep;
2799 $actioncomm->authorid = $user->id; // User saving action
2800 $actioncomm->userownerid = $user->id; // Owner of action
2801 // Fields when action is an email (content should be added into note)
2802 $actioncomm->email_msgid = $mail->msgid;
2803 $actioncomm->email_from = $from;
2804 $actioncomm->email_sender = '';
2805 $actioncomm->email_to = $to;
2806
2807 $actioncomm->errors_to = $errors_to;
2808
2809 $actioncomm->elementtype = 'project_task';
2810 $actioncomm->fk_element = (int) $this->element;
2811
2812 $actioncomm->create($user);
2813 }
2814 $this->db->commit();
2815 }
2816 }
2817 if (!empty($errormesg)) {
2818 $errorsMsg[] = $errormesg;
2819 }
2820
2821 if (!$error) {
2822 $this->output .= 'Nb of emails sent : '.$nbMailSend;
2823 dol_syslog(__METHOD__." end - ".$this->output, LOG_INFO);
2824 return 0;
2825 } else {
2826 $this->error = 'Nb of emails sent : '.$nbMailSend.', '.(empty($errorsMsg) ? $error : implode(', ', $errorsMsg));
2827 dol_syslog(__METHOD__." end - ".$this->error, LOG_INFO);
2828 return $error;
2829 }
2830 }
2831}
$object ref
Definition info.php:90
Class to manage agenda events (actions)
Class to send emails (with attachments or not) Usage: $mailfile = new CMailFile($subject,...
Parent class of all other business classes (invoices, contracts, proposals, orders,...
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.
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.
getNomUrl($withpicto=0, $option='', $addlabel=0, $moreinpopup='', $sep=' - ', $notooltip=0, $save_lastsearch_value=-1, $morecss='', $save_pageforbacktolist='')
Return clickable name (with optional picto)
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.
loadTimeSpentMonth($datestart, $taskid=0, $userid=0)
Load time spent into this->weekWorkLoad and this->weekWorkLoadPerTask for all day of a week of projec...
__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:168
global $mysoc
dol_get_first_day_week($day, $month, $year, $gm=false)
Return first day of week for a date.
Definition date.lib.php:679
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:771
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0)
Add a delay to a date.
Definition date.lib.php:126
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.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
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:64
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_phone($phone, $countrycode='', $contactid=0, $socid=0, $addlink='', $separ="&nbsp;", $withpicto='', $titlealt='', $adddivfloat=0, $morecss='paddingright')
Format phone numbers according to country.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
img_warning($titlealt='default', $moreatt='', $morecss='pictowarning')
Show warning logo.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
isDolTms($timestamp)
isDolTms check if a timestamp is valid.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
Show a picto called object_picto (generic function)
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0, $allowdash=0)
Clean a string to use it as a file name.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
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.
dol_print_email($email, $contactid=0, $socid=0, $addlink=0, $max=0, $showinvalid=1, $withpicto=0, $morecss='paddingrightonly')
Show EMail link formatted for HTML output.
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).
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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)