dolibarr 21.0.0-alpha
project.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2002-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2005-2020 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2005-2010 Regis Houssin <regis.houssin@inodbox.com>
5 * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
6 * Copyright (C) 2014-2017 Marcos García <marcosgdf@gmail.com>
7 * Copyright (C) 2017 Ferran Marcet <fmarcet@2byte.es>
8 * Copyright (C) 2019 Juanjo Menent <jmenent@2byte.es>
9 * Copyright (C) 2022 Charlene Benke <charlene@patas-monkey.com>
10 * Copyright (C) 2023 Gauthier VERDOL <gauthier.verdol@atm-consulting.fr>
11 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
12 * Copyright (C) 2024 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 $oldcopy;
253
257 public $weekWorkLoad;
261 public $weekWorkLoadPerTask; // Used to store workload details of tasks of a projet
262
266 public $monthWorkLoad;
267
271 public $monthWorkLoadPerTask;
272
277 public $datec;
278
282 public $date_c;
283
288 public $datem;
289
293 public $date_m;
294
298 public $ip;
299
303 public $lines;
304
345 // BEGIN MODULEBUILDER PROPERTIES
349 public $fields = array(
350 'rowid' => array('type' => 'integer', 'label' => 'ID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 10),
351 'fk_project' => array('type' => 'integer', 'label' => 'Parent', 'enabled' => 1, 'visible' => 1, 'notnull' => 0, 'position' => 12),
352 'ref' => array('type' => 'varchar(50)', 'label' => 'Ref', 'enabled' => 1, 'visible' => 1, 'showoncombobox' => 1, 'position' => 15, 'searchall' => 1),
353 'title' => array('type' => 'varchar(255)', 'label' => 'ProjectLabel', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'position' => 17, 'showoncombobox' => 2, 'searchall' => 1),
354 'entity' => array('type' => 'integer', 'label' => 'Entity', 'default' => '1', 'enabled' => 1, 'visible' => 3, 'notnull' => 1, 'position' => 19),
355 'fk_soc' => array('type' => 'integer', 'label' => 'Thirdparty', 'enabled' => 1, 'visible' => 0, 'position' => 20),
356 'dateo' => array('type' => 'date', 'label' => 'DateStart', 'enabled' => 1, 'visible' => -1, 'position' => 30),
357 'datee' => array('type' => 'date', 'label' => 'DateEnd', 'enabled' => 1, 'visible' => 1, 'position' => 35),
358 'description' => array('type' => 'text', 'label' => 'Description', 'enabled' => 1, 'visible' => 3, 'position' => 55, 'searchall' => 1),
359 'public' => array('type' => 'integer', 'label' => 'Visibility', 'enabled' => 1, 'visible' => -1, 'position' => 65),
360 'fk_opp_status' => array('type' => 'integer:CLeadStatus:core/class/cleadstatus.class.php', 'label' => 'OpportunityStatusShort', 'enabled' => 'getDolGlobalString("PROJECT_USE_OPPORTUNITIES")', 'visible' => 1, 'position' => 75),
361 'opp_percent' => array('type' => 'double(5,2)', 'label' => 'OpportunityProbabilityShort', 'enabled' => 'getDolGlobalString("PROJECT_USE_OPPORTUNITIES")', 'visible' => 1, 'position' => 80),
362 'note_private' => array('type' => 'html', 'label' => 'NotePrivate', 'enabled' => 1, 'visible' => 0, 'position' => 85, 'searchall' => 1),
363 'note_public' => array('type' => 'html', 'label' => 'NotePublic', 'enabled' => 1, 'visible' => 0, 'position' => 90, 'searchall' => 1),
364 'model_pdf' => array('type' => 'varchar(255)', 'label' => 'ModelPdf', 'enabled' => 1, 'visible' => 0, 'position' => 95),
365 'date_close' => array('type' => 'datetime', 'label' => 'DateClosing', 'enabled' => 1, 'visible' => 0, 'position' => 105),
366 'fk_user_close' => array('type' => 'integer', 'label' => 'UserClosing', 'enabled' => 1, 'visible' => 0, 'position' => 110),
367 'opp_amount' => array('type' => 'double(24,8)', 'label' => 'OpportunityAmountShort', 'enabled' => 1, 'visible' => 'getDolGlobalString("PROJECT_USE_OPPORTUNITIES")', 'position' => 115),
368 'budget_amount' => array('type' => 'double(24,8)', 'label' => 'Budget', 'enabled' => 1, 'visible' => -1, 'position' => 119),
369 'usage_opportunity' => array('type' => 'integer', 'label' => 'UsageOpportunity', 'enabled' => 1, 'visible' => -1, 'position' => 130),
370 'usage_task' => array('type' => 'integer', 'label' => 'UsageTasks', 'enabled' => 1, 'visible' => -1, 'position' => 135),
371 'usage_bill_time' => array('type' => 'integer', 'label' => 'UsageBillTimeShort', 'enabled' => 1, 'visible' => -1, 'position' => 140),
372 'usage_organize_event' => array('type' => 'integer', 'label' => 'UsageOrganizeEvent', 'enabled' => 1, 'visible' => -1, 'position' => 145),
373 // Properties for event organization
374 'date_start_event' => array('type' => 'date', 'label' => 'DateStartEvent', 'enabled' => "isModEnabled('eventorganization')", 'visible' => 1, 'position' => 200),
375 'date_end_event' => array('type' => 'date', 'label' => 'DateEndEvent', 'enabled' => "isModEnabled('eventorganization')", 'visible' => 1, 'position' => 201),
376 'location' => array('type' => 'text', 'label' => 'Location', 'enabled' => 1, 'visible' => 3, 'position' => 55, 'searchall' => 202),
377 'accept_conference_suggestions' => array('type' => 'integer', 'label' => 'AllowUnknownPeopleSuggestConf', 'enabled' => 1, 'visible' => -1, 'position' => 210),
378 'accept_booth_suggestions' => array('type' => 'integer', 'label' => 'AllowUnknownPeopleSuggestBooth', 'enabled' => 1, 'visible' => -1, 'position' => 211),
379 'price_registration' => array('type' => 'double(24,8)', 'label' => 'PriceOfRegistration', 'enabled' => 1, 'visible' => -1, 'position' => 212),
380 'price_booth' => array('type' => 'double(24,8)', 'label' => 'PriceOfBooth', 'enabled' => 1, 'visible' => -1, 'position' => 215),
381 'max_attendees' => array('type' => 'integer', 'label' => 'MaxNbOfAttendees', 'enabled' => 1, 'visible' => -1, 'position' => 215),
382 // Generic
383 'datec' => array('type' => 'datetime', 'label' => 'DateCreationShort', 'enabled' => 1, 'visible' => -2, 'position' => 400),
384 'tms' => array('type' => 'timestamp', 'label' => 'DateModificationShort', 'enabled' => 1, 'visible' => -2, 'notnull' => 1, 'position' => 405),
385 'fk_user_creat' => array('type' => 'integer', 'label' => 'UserCreation', 'enabled' => 1, 'visible' => 0, 'notnull' => 1, 'position' => 410),
386 'fk_user_modif' => array('type' => 'integer', 'label' => 'UserModification', 'enabled' => 1, 'visible' => 0, 'position' => 415),
387 'import_key' => array('type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'visible' => -1, 'position' => 420),
388 'email_msgid' => array('type' => 'varchar(255)', 'label' => 'EmailMsgID', 'enabled' => 1, 'visible' => -1, 'position' => 450, 'help' => 'EmailMsgIDWhenSourceisEmail', 'csslist' => 'tdoverflowmax125'),
389 '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')),
390 );
391 // END MODULEBUILDER PROPERTIES
392
396 const STATUS_DRAFT = 0;
397
402
406 const STATUS_CLOSED = 2;
407
408
414 public function __construct($db)
415 {
416 global $conf;
417
418 $this->db = $db;
419
420 $this->ismultientitymanaged = 1;
421 $this->isextrafieldmanaged = 1;
422
423 $this->labelStatusShort = array(0 => 'Draft', 1 => 'Opened', 2 => 'Closed');
424 $this->labelStatus = array(0 => 'Draft', 1 => 'Opened', 2 => 'Closed');
425
426 global $conf;
427
428 if (!getDolGlobalString('MAIN_SHOW_TECHNICAL_ID')) {
429 $this->fields['rowid']['visible'] = 0;
430 }
431
432 if (!getDolGlobalString('PROJECT_USE_OPPORTUNITIES')) {
433 $this->fields['fk_opp_status']['enabled'] = 0;
434 $this->fields['opp_percent']['enabled'] = 0;
435 $this->fields['opp_amount']['enabled'] = 0;
436 $this->fields['usage_opportunity']['enabled'] = 0;
437 }
438
439 if (getDolGlobalString('PROJECT_HIDE_TASKS')) {
440 $this->fields['usage_bill_time']['visible'] = 0;
441 $this->fields['usage_task']['visible'] = 0;
442 }
443
444 if (empty($conf->eventorganization->enabled)) {
445 $this->fields['usage_organize_event']['visible'] = 0;
446 $this->fields['accept_conference_suggestions']['enabled'] = 0;
447 $this->fields['accept_booth_suggestions']['enabled'] = 0;
448 $this->fields['price_registration']['enabled'] = 0;
449 $this->fields['price_booth']['enabled'] = 0;
450 $this->fields['max_attendees']['enabled'] = 0;
451 }
452 }
453
461 public function create($user, $notrigger = 0)
462 {
463 $error = 0;
464 $ret = 0;
465
466 $now = dol_now();
467
468 // Clean parameters
469 $this->note_private = dol_substr($this->note_private, 0, 65535);
470 $this->note_public = dol_substr($this->note_public, 0, 65535);
471
472 // Check parameters
473 if (!trim($this->ref)) {
474 $this->error = 'ErrorFieldsRequired';
475 dol_syslog(get_class($this)."::create error -1 ref null", LOG_ERR);
476 return -1;
477 }
478 if (getDolGlobalString('PROJECT_THIRDPARTY_REQUIRED') && !($this->socid > 0)) {
479 $this->error = 'ErrorFieldsRequired';
480 dol_syslog(get_class($this)."::create error -1 thirdparty not defined and option PROJECT_THIRDPARTY_REQUIRED is set", LOG_ERR);
481 return -1;
482 }
483
484 // Create project
485 $this->db->begin();
486
487 $sql = "INSERT INTO ".MAIN_DB_PREFIX."projet (";
488 $sql .= "ref";
489 $sql .= ", fk_project";
490 $sql .= ", title";
491 $sql .= ", description";
492 $sql .= ", fk_soc";
493 $sql .= ", fk_user_creat";
494 $sql .= ", fk_statut";
495 $sql .= ", fk_opp_status";
496 $sql .= ", opp_percent";
497 $sql .= ", public";
498 $sql .= ", datec";
499 $sql .= ", dateo";
500 $sql .= ", datee";
501 $sql .= ", opp_amount";
502 $sql .= ", budget_amount";
503 $sql .= ", usage_opportunity";
504 $sql .= ", usage_task";
505 $sql .= ", usage_bill_time";
506 $sql .= ", usage_organize_event";
507 $sql .= ", accept_conference_suggestions";
508 $sql .= ", accept_booth_suggestions";
509 $sql .= ", price_registration";
510 $sql .= ", price_booth";
511 $sql .= ", max_attendees";
512 $sql .= ", date_start_event";
513 $sql .= ", date_end_event";
514 $sql .= ", location";
515 $sql .= ", email_msgid";
516 $sql .= ", note_private";
517 $sql .= ", note_public";
518 $sql .= ", entity";
519 $sql .= ", ip";
520 $sql .= ") VALUES (";
521 $sql .= "'".$this->db->escape($this->ref)."'";
522 $sql .= ", ".($this->fk_project ? ((int) $this->fk_project) : "null");
523 $sql .= ", '".$this->db->escape($this->title)."'";
524 $sql .= ", '".$this->db->escape($this->description)."'";
525 $sql .= ", ".($this->socid > 0 ? $this->socid : "null");
526 $sql .= ", ".((int) $user->id);
527 $sql .= ", ".(is_numeric($this->status) ? ((int) $this->status) : '0');
528 $sql .= ", ".((is_numeric($this->opp_status) && $this->opp_status > 0) ? ((int) $this->opp_status) : 'NULL');
529 $sql .= ", ".(is_numeric($this->opp_percent) ? ((int) $this->opp_percent) : 'NULL');
530 $sql .= ", ".($this->public ? 1 : 0);
531 $sql .= ", '".$this->db->idate($now)."'";
532 $sql .= ", ".($this->date_start != '' ? "'".$this->db->idate($this->date_start)."'" : 'null');
533 $sql .= ", ".($this->date_end != '' ? "'".$this->db->idate($this->date_end)."'" : 'null');
534 $sql .= ", ".(strcmp($this->opp_amount, '') ? price2num($this->opp_amount) : 'null');
535 $sql .= ", ".(strcmp((string) $this->budget_amount, '') ? price2num($this->budget_amount) : 'null');
536 $sql .= ", ".($this->usage_opportunity ? 1 : 0);
537 $sql .= ", ".($this->usage_task ? 1 : 0);
538 $sql .= ", ".($this->usage_bill_time ? 1 : 0);
539 $sql .= ", ".($this->usage_organize_event ? 1 : 0);
540 $sql .= ", ".($this->accept_conference_suggestions ? 1 : 0);
541 $sql .= ", ".($this->accept_booth_suggestions ? 1 : 0);
542 $sql .= ", ".(strcmp((string) $this->price_registration, '') ? price2num($this->price_registration) : 'null');
543 $sql .= ", ".(strcmp((string) $this->price_booth, '') ? price2num($this->price_booth) : 'null');
544 $sql .= ", ".(strcmp((string) $this->max_attendees, '') ? ((int) $this->max_attendees) : 'null');
545 $sql .= ", ".($this->date_start_event != '' ? "'".$this->db->idate($this->date_start_event)."'" : 'null');
546 $sql .= ", ".($this->date_end_event != '' ? "'".$this->db->idate($this->date_end_event)."'" : 'null');
547 $sql .= ", ".($this->location ? "'".$this->db->escape($this->location)."'" : 'null');
548 $sql .= ", ".($this->email_msgid ? "'".$this->db->escape($this->email_msgid)."'" : 'null');
549 $sql .= ", ".($this->note_private ? "'".$this->db->escape($this->note_private)."'" : 'null');
550 $sql .= ", ".($this->note_public ? "'".$this->db->escape($this->note_public)."'" : 'null');
551 $sql .= ", ".setEntity($this);
552 $sql .= ", ".(!isset($this->ip) ? 'NULL' : "'".$this->db->escape($this->ip)."'");
553 $sql .= ")";
554
555 dol_syslog(get_class($this)."::create", LOG_DEBUG);
556 $resql = $this->db->query($sql);
557 if ($resql) {
558 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."projet");
559 $ret = $this->id;
560
561 if (!$notrigger) {
562 // Call trigger
563 $result = $this->call_trigger('PROJECT_CREATE', $user);
564 if ($result < 0) {
565 $error++;
566 }
567 // End call triggers
568 }
569 } else {
570 $this->error = $this->db->lasterror();
571 $error++;
572 }
573
574 // Update extrafield
575 if (!$error) {
576 $result = $this->insertExtraFields();
577 if ($result < 0) {
578 $error++;
579 }
580 }
581
582 if (!$error && (getDolGlobalString('MAIN_DISABLEDRAFTSTATUS') || getDolGlobalString('MAIN_DISABLEDRAFTSTATUS_PROJECT'))) {
583 $res = $this->setValid($user);
584 if ($res < 0) {
585 $error++;
586 }
587 }
588
589 if (!$error) {
590 $this->db->commit();
591 return $ret;
592 } else {
593 $this->db->rollback();
594 return -1;
595 }
596 }
597
605 public function update($user, $notrigger = 0)
606 {
607 global $langs, $conf;
608
609 $error = 0;
610
611 // Clean parameters
612 $this->title = trim($this->title);
613 $this->description = trim($this->description);
614 if ($this->opp_amount < 0) {
615 $this->opp_amount = '';
616 }
617 if ($this->opp_percent < 0) {
618 $this->opp_percent = '';
619 }
620 if ($this->date_end && $this->date_end < $this->date_start) {
621 $this->error = $langs->trans("ErrorDateEndLowerThanDateStart");
622 $this->errors[] = $this->error;
623 $this->db->rollback();
624 dol_syslog(get_class($this)."::update error -3 ".$this->error, LOG_ERR);
625 return -3;
626 }
627
628 $this->entity = ((isset($this->entity) && is_numeric($this->entity)) ? $this->entity : $conf->entity);
629
630 if (dol_strlen(trim($this->ref)) > 0) {
631 $this->db->begin();
632
633 $sql = "UPDATE ".MAIN_DB_PREFIX."projet SET";
634 $sql .= " ref='".$this->db->escape($this->ref)."'";
635 $sql .= ", fk_project=".($this->fk_project ? ((int) $this->fk_project) : "null");
636 $sql .= ", title = '".$this->db->escape($this->title)."'";
637 $sql .= ", description = '".$this->db->escape($this->description)."'";
638 $sql .= ", fk_soc = ".($this->socid > 0 ? $this->socid : "null");
639 $sql .= ", fk_statut = ".((int) $this->status);
640 $sql .= ", fk_opp_status = ".((is_numeric($this->opp_status) && $this->opp_status > 0) ? $this->opp_status : 'null');
641 $sql .= ", opp_percent = ".((is_numeric($this->opp_percent) && $this->opp_percent != '') ? $this->opp_percent : 'null');
642 $sql .= ", public = ".($this->public ? 1 : 0);
643 $sql .= ", datec = ".($this->date_c != '' ? "'".$this->db->idate($this->date_c)."'" : 'null');
644 $sql .= ", dateo = ".($this->date_start != '' ? "'".$this->db->idate($this->date_start)."'" : 'null');
645 $sql .= ", datee = ".($this->date_end != '' ? "'".$this->db->idate($this->date_end)."'" : 'null');
646 $sql .= ", date_close = ".($this->date_close != '' ? "'".$this->db->idate($this->date_close)."'" : 'null');
647 $sql .= ", note_public = ".($this->note_public ? "'".$this->db->escape($this->note_public)."'" : "null");
648 $sql .= ", note_private = ".($this->note_private ? "'".$this->db->escape($this->note_private)."'" : "null");
649 $sql .= ", fk_user_close = ".($this->fk_user_close > 0 ? $this->fk_user_close : "null");
650 $sql .= ", opp_amount = ".(strcmp($this->opp_amount, '') ? price2num($this->opp_amount) : "null");
651 $sql .= ", budget_amount = ".(strcmp($this->budget_amount, '') ? price2num($this->budget_amount) : "null");
652 $sql .= ", fk_user_modif = ".((int) $user->id);
653 $sql .= ", usage_opportunity = ".($this->usage_opportunity ? 1 : 0);
654 $sql .= ", usage_task = ".($this->usage_task ? 1 : 0);
655 $sql .= ", usage_bill_time = ".($this->usage_bill_time ? 1 : 0);
656 $sql .= ", usage_organize_event = ".($this->usage_organize_event ? 1 : 0);
657 $sql .= ", accept_conference_suggestions = ".($this->accept_conference_suggestions ? 1 : 0);
658 $sql .= ", accept_booth_suggestions = ".($this->accept_booth_suggestions ? 1 : 0);
659 $sql .= ", price_registration = ".(isset($this->price_registration) && strcmp($this->price_registration, '') ? price2num($this->price_registration) : "null");
660 $sql .= ", price_booth = ".(isset($this->price_booth) && strcmp((string) $this->price_booth, '') ? price2num($this->price_booth) : "null");
661 $sql .= ", max_attendees = ".(strcmp((string) $this->max_attendees, '') ? (int) $this->max_attendees : "null");
662 $sql .= ", date_start_event = ".($this->date_start_event != '' ? "'".$this->db->idate($this->date_start_event)."'" : 'null');
663 $sql .= ", date_end_event = ".($this->date_end_event != '' ? "'".$this->db->idate($this->date_end_event)."'" : 'null');
664 $sql .= ", location = '".$this->db->escape($this->location)."'";
665 $sql .= ", entity = ".((int) $this->entity);
666 $sql .= " WHERE rowid = ".((int) $this->id);
667
668 dol_syslog(get_class($this)."::update", LOG_DEBUG);
669 $resql = $this->db->query($sql);
670 if ($resql) {
671 // Update extrafield
672 if (!$error) {
673 $result = $this->insertExtraFields();
674 if ($result < 0) {
675 $error++;
676 }
677 }
678
679 if (!$error && !$notrigger) {
680 // Call trigger
681 $result = $this->call_trigger('PROJECT_MODIFY', $user);
682 if ($result < 0) {
683 $error++;
684 }
685 // End call triggers
686 }
687
688 if (!$error && (is_object($this->oldcopy) && $this->oldcopy->ref !== $this->ref)) {
689 // We remove directory
690 if ($conf->project->dir_output) {
691 $olddir = $conf->project->dir_output."/".dol_sanitizeFileName($this->oldcopy->ref);
692 $newdir = $conf->project->dir_output."/".dol_sanitizeFileName($this->ref);
693 if (file_exists($olddir)) {
694 include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
695 $res = @rename($olddir, $newdir);
696 if (!$res) {
697 $langs->load("errors");
698 $this->error = $langs->trans('ErrorFailToRenameDir', $olddir, $newdir);
699 $error++;
700 }
701 }
702 }
703 }
704 if (!$error) {
705 $this->db->commit();
706 $result = 1;
707 } else {
708 $this->db->rollback();
709 $result = -1;
710 }
711 } else {
712 $this->error = $this->db->lasterror();
713 $this->errors[] = $this->error;
714 $this->db->rollback();
715 if ($this->db->lasterrno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
716 $result = -4;
717 } else {
718 $result = -2;
719 }
720 dol_syslog(get_class($this)."::update error ".$result." ".$this->error, LOG_ERR);
721 }
722 } else {
723 dol_syslog(get_class($this)."::update ref null");
724 $result = -1;
725 }
726
727 return $result;
728 }
729
739 public function fetch($id, $ref = '', $ref_ext = '', $email_msgid = '')
740 {
741 if (empty($id) && empty($ref) && empty($ref_ext) && empty($email_msgid)) {
742 dol_syslog(get_class($this)."::fetch Bad parameters", LOG_WARNING);
743 return -1;
744 }
745
746 $sql = "SELECT rowid, entity, fk_project, ref, title, description, public, datec, opp_amount, budget_amount,";
747 $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,";
748 $sql .= " note_private, note_public, model_pdf, usage_opportunity, usage_task, usage_bill_time, usage_organize_event, email_msgid,";
749 $sql .= " accept_conference_suggestions, accept_booth_suggestions, price_registration, price_booth, max_attendees, date_start_event, date_end_event, location, extraparams";
750 $sql .= " FROM ".MAIN_DB_PREFIX."projet";
751 if (!empty($id)) {
752 $sql .= " WHERE rowid = ".((int) $id);
753 } else {
754 $sql .= " WHERE entity IN (".getEntity('project').")";
755 if (!empty($ref)) {
756 $sql .= " AND ref = '".$this->db->escape($ref)."'";
757 } elseif (!empty($ref_ext)) {
758 $sql .= " AND ref_ext = '".$this->db->escape($ref_ext)."'";
759 } else {
760 $sql .= " AND email_msgid = '".$this->db->escape($email_msgid)."'";
761 }
762 }
763
764 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
765 $resql = $this->db->query($sql);
766 if ($resql) {
767 $num_rows = $this->db->num_rows($resql);
768
769 if ($num_rows) {
770 $obj = $this->db->fetch_object($resql);
771
772 $this->id = $obj->rowid;
773 $this->entity = $obj->entity;
774 $this->ref = $obj->ref;
775 $this->fk_project = $obj->fk_project;
776 $this->title = $obj->title;
777 $this->description = $obj->description;
778 $this->date_c = $this->db->jdate($obj->datec);
779 $this->datec = $this->db->jdate($obj->datec); // TODO deprecated
780 $this->date_m = $this->db->jdate($obj->tms);
781 $this->datem = $this->db->jdate($obj->tms); // TODO deprecated
782 $this->date_start = $this->db->jdate($obj->date_start);
783 $this->date_end = $this->db->jdate($obj->date_end);
784 $this->date_close = $this->db->jdate($obj->date_close);
785 $this->note_private = $obj->note_private;
786 $this->note_public = $obj->note_public;
787 $this->socid = $obj->fk_soc;
788 $this->user_author_id = $obj->fk_user_creat;
789 $this->user_modification_id = $obj->fk_user_modif;
790 $this->user_closing_id = $obj->fk_user_close;
791 $this->public = $obj->public;
792 $this->statut = $obj->status; // deprecated
793 $this->status = $obj->status;
794 $this->opp_status = $obj->fk_opp_status;
795 $this->opp_amount = $obj->opp_amount;
796 $this->opp_percent = $obj->opp_percent;
797 $this->budget_amount = $obj->budget_amount;
798 $this->model_pdf = $obj->model_pdf;
799 $this->usage_opportunity = (int) $obj->usage_opportunity;
800 $this->usage_task = (int) $obj->usage_task;
801 $this->usage_bill_time = (int) $obj->usage_bill_time;
802 $this->usage_organize_event = (int) $obj->usage_organize_event;
803 $this->accept_conference_suggestions = (int) $obj->accept_conference_suggestions;
804 $this->accept_booth_suggestions = (int) $obj->accept_booth_suggestions;
805 $this->price_registration = $obj->price_registration;
806 $this->price_booth = $obj->price_booth;
807 $this->max_attendees = $obj->max_attendees;
808 $this->date_start_event = $this->db->jdate($obj->date_start_event);
809 $this->date_end_event = $this->db->jdate($obj->date_end_event);
810 $this->location = $obj->location;
811 $this->email_msgid = $obj->email_msgid;
812 $this->extraparams = !empty($obj->extraparams) ? (array) json_decode($obj->extraparams, true) : array();
813
814 $this->db->free($resql);
815
816 // Retrieve all extrafield
817 // fetch optionals attributes and labels
818 $this->fetch_optionals();
819
820 return 1;
821 }
822
823 $this->db->free($resql);
824
825 return 0;
826 } else {
827 $this->error = $this->db->lasterror();
828 $this->errors[] = $this->db->lasterror();
829 return -1;
830 }
831 }
832
841 public function fetchAndSetSubstitution($id, $key, $fetched = false)
842 {
843 $substitution = '';
844
845 if ($fetched === false) {
846 $res = $this->fetch($id);
847 if ($res > 0) {
848 $fetched = true;
849 }
850 }
851
852 if ($fetched === true) {
853 if ($key == '__PROJECT_ID__') {
854 $substitution = ($this->id > 0 ? $this->id : '');
855 } elseif ($key == '__PROJECT_REF__') {
856 $substitution = $this->ref;
857 } elseif ($key == '__PROJECT_NAME__') {
858 $substitution = $this->title;
859 }
860 }
861
862 return $substitution;
863 }
864
865 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
877 public function get_element_list($type, $tablename, $datefieldname = '', $date_start = null, $date_end = null, $projectkey = 'fk_projet')
878 {
879 // phpcs:enable
880
881 global $hookmanager;
882
883 $elements = array();
884
885 if ($this->id <= 0) {
886 return $elements;
887 }
888
889 $ids = $this->id;
890
891 if ($type == 'agenda') {
892 $sql = "SELECT id as rowid FROM ".MAIN_DB_PREFIX."actioncomm WHERE fk_project IN (".$this->db->sanitize($ids).") AND entity IN (".getEntity('agenda').")";
893 } elseif ($type == 'expensereport') {
894 $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($ids).")";
895 } elseif ($type == 'project_task') {
896 $sql = "SELECT DISTINCT pt.rowid FROM ".MAIN_DB_PREFIX."projet_task as pt WHERE pt.fk_projet IN (".$this->db->sanitize($ids).")";
897 } elseif ($type == 'element_time') { // Case we want to duplicate line foreach user
898 $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($ids).")";
899 } elseif ($type == 'stocktransfer_stocktransfer') {
900 $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($ids).") AND ms.type_mouvement = 1";
901 } elseif ($type == 'loan') {
902 $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($ids).")";
903 } else {
904 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX.$tablename." WHERE ".$projectkey." IN (".$this->db->sanitize($ids).") AND entity IN (".getEntity($type).")";
905 }
906
907 if (isDolTms($date_start) && $type == 'loan') {
908 $sql .= " AND (dateend > '".$this->db->idate($date_start)."' OR dateend IS NULL)";
909 } elseif (isDolTms($date_start) && ($type != 'project_task')) { // For table project_taks, we want the filter on date apply on project_time_spent table
910 if (empty($datefieldname) && !empty($this->table_element_date)) {
911 $datefieldname = $this->table_element_date;
912 }
913 if (empty($datefieldname)) {
914 return 'Error this object has no date field defined';
915 }
916 $sql .= " AND (".$datefieldname." >= '".$this->db->idate($date_start)."' OR ".$datefieldname." IS NULL)";
917 }
918
919 if (isDolTms($date_end) && $type == 'loan') {
920 $sql .= " AND (datestart < '".$this->db->idate($date_end)."' OR datestart IS NULL)";
921 } elseif (isDolTms($date_end) && ($type != 'project_task')) { // For table project_taks, we want the filter on date apply on project_time_spent table
922 if (empty($datefieldname) && !empty($this->table_element_date)) {
923 $datefieldname = $this->table_element_date;
924 }
925 if (empty($datefieldname)) {
926 return 'Error this object has no date field defined';
927 }
928 $sql .= " AND (".$datefieldname." <= '".$this->db->idate($date_end)."' OR ".$datefieldname." IS NULL)";
929 }
930
931 $parameters = array(
932 'sql' => $sql,
933 'type' => $type,
934 'tablename' => $tablename,
935 'datefieldname' => $datefieldname,
936 'dates' => $date_start,
937 'datee' => $date_end,
938 'fk_projet' => $projectkey,
939 'ids' => $ids,
940 );
941 $reshook = $hookmanager->executeHooks('getElementList', $parameters);
942 if ($reshook > 0) {
943 $sql = $hookmanager->resPrint;
944 } else {
945 $sql .= $hookmanager->resPrint;
946 }
947
948 if (!$sql) {
949 return -1;
950 }
951
952 //print $sql;
953 dol_syslog(get_class($this)."::get_element_list", LOG_DEBUG);
954 $result = $this->db->query($sql);
955 if ($result) {
956 $nump = $this->db->num_rows($result);
957 if ($nump) {
958 $i = 0;
959 while ($i < $nump) {
960 $obj = $this->db->fetch_object($result);
961
962 $elements[$i] = $obj->rowid.(empty($obj->fk_user) ? '' : '_'.$obj->fk_user);
963
964 $i++;
965 }
966 $this->db->free($result);
967 }
968
969 /* Return array even if empty*/
970 return $elements;
971 } else {
972 dol_print_error($this->db);
973 }
974 return -1;
975 }
976
984 public function delete($user, $notrigger = 0)
985 {
986 global $langs, $conf;
987 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
988
989 $error = 0;
990
991 $this->db->begin();
992
993 if (!$error) {
994 // Delete linked contacts
995 $res = $this->delete_linked_contact();
996 if ($res < 0) {
997 $this->error = 'ErrorFailToDeleteLinkedContact';
998 //$error++;
999 $this->db->rollback();
1000 return 0;
1001 }
1002 }
1003
1004 // Set fk_projet into elements to null
1005 $listoftables = array(
1006 'propal' => 'fk_projet',
1007 'commande' => 'fk_projet',
1008 'facture' => 'fk_projet',
1009 'supplier_proposal' => 'fk_projet',
1010 'commande_fournisseur' => 'fk_projet',
1011 'facture_fourn' => 'fk_projet',
1012 'expensereport_det' => 'fk_projet',
1013 'contrat' => 'fk_projet',
1014 'fichinter' => 'fk_projet',
1015 'don' => array('field' => 'fk_projet', 'module' => 'don'),
1016 'actioncomm' => 'fk_project',
1017 'mrp_mo' => array('field' => 'fk_project', 'module' => 'mrp'),
1018 'entrepot' => 'fk_project',
1019 );
1020 foreach ($listoftables as $key => $value) {
1021 if (is_array($value)) {
1022 if (!isModEnabled($value['module'])) {
1023 continue;
1024 }
1025 $fieldname = $value['field'];
1026 } else {
1027 $fieldname = $value;
1028 }
1029 $sql = "UPDATE ".MAIN_DB_PREFIX.$key." SET ".$fieldname." = NULL where ".$fieldname." = ".((int) $this->id);
1030
1031 $resql = $this->db->query($sql);
1032 if (!$resql) {
1033 $this->errors[] = $this->db->lasterror();
1034 $error++;
1035 break;
1036 }
1037 }
1038
1039 // Remove linked categories.
1040 if (!$error) {
1041 $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_project";
1042 $sql .= " WHERE fk_project = ".((int) $this->id);
1043
1044 $result = $this->db->query($sql);
1045 if (!$result) {
1046 $error++;
1047 $this->errors[] = $this->db->lasterror();
1048 }
1049 }
1050
1051 // Fetch tasks
1052 $this->getLinesArray($user, 0);
1053
1054 // Delete tasks
1055 $ret = $this->deleteTasks($user);
1056 if ($ret < 0) {
1057 $error++;
1058 }
1059
1060
1061 // Delete all child tables
1062 if (!$error) {
1063 $elements = array('categorie_project'); // elements to delete. TODO Make goodway to delete
1064 foreach ($elements as $table) {
1065 if (!$error) {
1066 $sql = "DELETE FROM ".MAIN_DB_PREFIX.$table;
1067 $sql .= " WHERE fk_project = ".((int) $this->id);
1068
1069 $result = $this->db->query($sql);
1070 if (!$result) {
1071 $error++;
1072 $this->errors[] = $this->db->lasterror();
1073 }
1074 }
1075 }
1076 }
1077
1078 if (!$error) {
1079 $sql = "DELETE FROM ".MAIN_DB_PREFIX."projet_extrafields";
1080 $sql .= " WHERE fk_object = ".((int) $this->id);
1081
1082 $resql = $this->db->query($sql);
1083 if (!$resql) {
1084 $this->errors[] = $this->db->lasterror();
1085 $error++;
1086 }
1087 }
1088
1089 // Delete project
1090 if (!$error) {
1091 $sql = "DELETE FROM ".MAIN_DB_PREFIX."projet";
1092 $sql .= " WHERE rowid=".((int) $this->id);
1093
1094 $resql = $this->db->query($sql);
1095 if (!$resql) {
1096 $this->errors[] = $langs->trans("CantRemoveProject", $langs->transnoentitiesnoconv("ProjectOverview"));
1097 $error++;
1098 }
1099 }
1100
1101
1102
1103 if (empty($error)) {
1104 // We remove directory
1105 $projectref = dol_sanitizeFileName($this->ref);
1106 if ($conf->project->dir_output) {
1107 $dir = $conf->project->dir_output."/".$projectref;
1108 if (file_exists($dir)) {
1109 $res = @dol_delete_dir_recursive($dir);
1110 if (!$res) {
1111 $this->errors[] = 'ErrorFailToDeleteDir';
1112 $error++;
1113 }
1114 }
1115 }
1116
1117 if (!$notrigger) {
1118 // Call trigger
1119 $result = $this->call_trigger('PROJECT_DELETE', $user);
1120
1121 if ($result < 0) {
1122 $error++;
1123 }
1124 // End call triggers
1125 }
1126 }
1127
1128 if (empty($error)) {
1129 $this->db->commit();
1130 return 1;
1131 } else {
1132 foreach ($this->errors as $errmsg) {
1133 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
1134 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
1135 }
1136 dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR);
1137 $this->db->rollback();
1138 return -1;
1139 }
1140 }
1141
1150 public function getElementCount($type, $tablename, $projectkey = 'fk_projet')
1151 {
1152 if ($this->id <= 0) {
1153 return 0;
1154 }
1155
1156 if ($type == 'agenda') {
1157 $sql = "SELECT COUNT(id) as nb FROM ".MAIN_DB_PREFIX."actioncomm WHERE fk_project = ".((int) $this->id)." AND entity IN (".getEntity('agenda').")";
1158 } elseif ($type == 'expensereport') {
1159 $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);
1160 } elseif ($type == 'project_task') {
1161 $sql = "SELECT DISTINCT COUNT(pt.rowid) as nb FROM ".MAIN_DB_PREFIX."projet_task as pt WHERE pt.fk_projet = ".((int) $this->id);
1162 } elseif ($type == 'element_time') { // Case we want to duplicate line foreach user
1163 $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);
1164 } elseif ($type == 'stock_mouvement') {
1165 $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";
1166 } elseif ($type == 'loan') {
1167 $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);
1168 } else {
1169 $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX.$tablename." WHERE ".$projectkey." = ".((int) $this->id)." AND entity IN (".getEntity($type).")";
1170 }
1171
1172 $result = $this->db->query($sql);
1173
1174 if (!$result) {
1175 return 0;
1176 }
1177
1178 $obj = $this->db->fetch_object($result);
1179
1180 $this->db->free($result);
1181
1182 return $obj->nb;
1183 }
1184
1191 public function deleteTasks($user)
1192 {
1193 $countTasks = count($this->lines);
1194 $deleted = false;
1195 if ($countTasks) {
1196 foreach ($this->lines as $task) {
1197 if ($task->hasChildren() <= 0) { // If there is no children (or error to detect them)
1198 $deleted = true;
1199 $ret = $task->delete($user);
1200 if ($ret <= 0) {
1201 $this->errors[] = $this->db->lasterror();
1202 return -1;
1203 }
1204 }
1205 }
1206 }
1207 $this->getLinesArray($user);
1208 if ($deleted && count($this->lines) < $countTasks) {
1209 if (count($this->lines)) {
1210 $this->deleteTasks($user);
1211 }
1212 }
1213
1214 return 1;
1215 }
1216
1224 public function setValid($user, $notrigger = 0)
1225 {
1226 global $langs;
1227
1228 $error = 0;
1229
1230 // Protection
1231 if ($this->status == self::STATUS_VALIDATED) {
1232 dol_syslog(get_class($this)."::validate action abandoned: already validated", LOG_WARNING);
1233 return 0;
1234 }
1235
1236 // Check parameters
1237 if (preg_match('/^'.preg_quote($langs->trans("CopyOf").' ').'/', $this->title)) {
1238 $this->error = $langs->trans("ErrorFieldFormat", $langs->transnoentities("Label")).'. '.$langs->trans('RemoveString', $langs->transnoentitiesnoconv("CopyOf"));
1239 return -1;
1240 }
1241
1242 $this->db->begin();
1243
1244 $sql = "UPDATE ".MAIN_DB_PREFIX."projet";
1245 $sql .= " SET fk_statut = ".self::STATUS_VALIDATED;
1246 $sql .= " WHERE rowid = ".((int) $this->id);
1247 //$sql .= " AND entity = ".((int) $conf->entity); // Disabled, when we use the ID for the where, we must not add any other search condition
1248
1249 dol_syslog(get_class($this)."::setValid", LOG_DEBUG);
1250 $resql = $this->db->query($sql);
1251 if ($resql) {
1252 // Call trigger
1253 if (empty($notrigger)) {
1254 $result = $this->call_trigger('PROJECT_VALIDATE', $user);
1255 if ($result < 0) {
1256 $error++;
1257 }
1258 // End call triggers
1259 }
1260
1261 if (!$error) {
1262 $this->statut = 1;
1263 $this->status = 1;
1264 $this->db->commit();
1265 return 1;
1266 } else {
1267 $this->db->rollback();
1268 $this->error = implode(',', $this->errors);
1269 dol_syslog(get_class($this)."::setValid ".$this->error, LOG_ERR);
1270 return -1;
1271 }
1272 } else {
1273 $this->db->rollback();
1274 $this->error = $this->db->lasterror();
1275 return -1;
1276 }
1277 }
1278
1285 public function setClose($user)
1286 {
1287 $now = dol_now();
1288
1289 $error = 0;
1290
1291 if ($this->status != self::STATUS_CLOSED) {
1292 $this->db->begin();
1293
1294 $sql = "UPDATE ".MAIN_DB_PREFIX."projet";
1295 $sql .= " SET fk_statut = ".self::STATUS_CLOSED.", fk_user_close = ".((int) $user->id).", date_close = '".$this->db->idate($now)."'";
1296 $sql .= " WHERE rowid = ".((int) $this->id);
1297 $sql .= " AND fk_statut = ".self::STATUS_VALIDATED;
1298
1299 if (getDolGlobalString('PROJECT_USE_OPPORTUNITIES')) {
1300 // TODO What to do if fk_opp_status is not code 'WON' or 'LOST'
1301 }
1302
1303 dol_syslog(get_class($this)."::setClose", LOG_DEBUG);
1304 $resql = $this->db->query($sql);
1305 if ($resql) {
1306 // Call trigger
1307 $result = $this->call_trigger('PROJECT_CLOSE', $user);
1308 if ($result < 0) {
1309 $error++;
1310 }
1311 // End call triggers
1312
1313 if (!$error) {
1314 $this->status = 2;
1315 $this->db->commit();
1316 return 1;
1317 } else {
1318 $this->db->rollback();
1319 $this->error = implode(',', $this->errors);
1320 dol_syslog(get_class($this)."::setClose ".$this->error, LOG_ERR);
1321 return -1;
1322 }
1323 } else {
1324 $this->db->rollback();
1325 $this->error = $this->db->lasterror();
1326 return -1;
1327 }
1328 }
1329
1330 return 0;
1331 }
1332
1339 public function getLibStatut($mode = 0)
1340 {
1341 return $this->LibStatut(isset($this->status) ? $this->status : $this->statut, $mode);
1342 }
1343
1344 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1352 public function LibStatut($status, $mode = 0)
1353 {
1354 // phpcs:enable
1355 global $langs;
1356
1357 if (is_null($status)) {
1358 return '';
1359 }
1360
1361 $statustrans = array(
1362 0 => 'status0',
1363 1 => 'status4',
1364 2 => 'status6',
1365 );
1366
1367 $statusClass = 'status0';
1368 if (!empty($statustrans[$status])) {
1369 $statusClass = $statustrans[$status];
1370 }
1371
1372 return dolGetStatus($langs->transnoentitiesnoconv($this->labelStatus[$status]), $langs->transnoentitiesnoconv($this->labelStatusShort[$status]), '', $statusClass, $mode);
1373 }
1374
1381 public function getTooltipContentArray($params)
1382 {
1383 global $conf, $langs;
1384
1385 $langs->load('projects');
1386 $option = $params['option'] ?? '';
1387 $moreinpopup = $params['moreinpopup'] ?? '';
1388
1389 $datas = [];
1390 if ($option != 'nolink') {
1391 $datas['picto'] = img_picto('', $this->picto, 'class="pictofixedwidth"').' <u class="paddingrightonly">'.$langs->trans("Project").'</u>';
1392 }
1393 if (isset($this->status)) {
1394 $datas['picto'] .= ' '.$this->getLibStatut(5);
1395 }
1396 $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
1397 $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
1398 if (isset($this->public)) {
1399 $datas['visibility'] = '<br><b>'.$langs->trans("Visibility").":</b> ";
1400 $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"));
1401 }
1402 if (!empty($this->thirdparty_name)) {
1403 $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
1404 }
1405 if (!empty($this->date_start)) {
1406 $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
1407 }
1408 if (!empty($this->date_end)) {
1409 $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
1410 }
1411 if ($moreinpopup) {
1412 $datas['moreinpopup'] = '<br>'.$moreinpopup;
1413 }
1414
1415 return $datas;
1416 }
1417
1432 public function getNomUrl($withpicto = 0, $option = '', $addlabel = 0, $moreinpopup = '', $sep = ' - ', $notooltip = 0, $save_lastsearch_value = -1, $morecss = '', $save_pageforbacktolist = '')
1433 {
1434 global $conf, $langs, $user, $hookmanager;
1435
1436 if (!empty($conf->dol_no_mouse_hover)) {
1437 $notooltip = 1; // Force disable tooltips
1438 }
1439
1440 $result = '';
1441 if (getDolGlobalString('PROJECT_OPEN_ALWAYS_ON_TAB')) {
1442 $option = getDolGlobalString('PROJECT_OPEN_ALWAYS_ON_TAB');
1443 }
1444 $params = [
1445 'id' => $this->id,
1446 'objecttype' => $this->element,
1447 'moreinpopup' => $moreinpopup,
1448 'option' => $option,
1449 ];
1450 $classfortooltip = 'classfortooltip';
1451 $dataparams = '';
1452 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
1453 $classfortooltip = 'classforajaxtooltip';
1454 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
1455 $label = '';
1456 } else {
1457 $label = implode($this->getTooltipContentArray($params));
1458 }
1459
1460 $url = '';
1461 if ($option != 'nolink') {
1462 if (preg_match('/\.php$/', $option)) {
1463 $url = dol_buildpath($option, 1).'?id='.$this->id;
1464 } elseif ($option == 'task') {
1465 $url = DOL_URL_ROOT.'/projet/tasks.php?id='.$this->id;
1466 } elseif ($option == 'preview') {
1467 $url = DOL_URL_ROOT.'/projet/element.php?id='.$this->id;
1468 } elseif ($option == 'eventorganization') {
1469 $url = DOL_URL_ROOT.'/eventorganization/conferenceorbooth_list.php?projectid='.$this->id;
1470 } else {
1471 $url = DOL_URL_ROOT.'/projet/card.php?id='.$this->id;
1472 }
1473 // Add param to save lastsearch_values or not
1474 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1475 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1476 $add_save_lastsearch_values = 1;
1477 }
1478 if ($add_save_lastsearch_values) {
1479 $url .= '&save_lastsearch_values=1';
1480 }
1481 $add_save_backpagefor = ($save_pageforbacktolist ? 1 : 0);
1482 if ($add_save_backpagefor) {
1483 $url .= "&save_pageforbacktolist=".urlencode($save_pageforbacktolist);
1484 }
1485 }
1486
1487 $linkclose = '';
1488 if (empty($notooltip) && $user->hasRight('projet', 'lire')) {
1489 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1490 $label = $langs->trans("ShowProject");
1491 $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
1492 }
1493 $linkclose .= ($label ? ' title="'.dol_escape_htmltag($label, 1).'"' : ' title="tocomplete"');
1494 $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
1495 } else {
1496 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1497 }
1498
1499 $picto = 'projectpub';
1500 if (!$this->public) {
1501 $picto = 'project';
1502 }
1503
1504 $linkstart = '<a href="'.$url.'"';
1505 $linkstart .= $linkclose.'>';
1506 $linkend = '</a>';
1507
1508 $result .= $linkstart;
1509 if ($withpicto) {
1510 $result .= img_object(($notooltip ? '' : $label), $picto, 'class="pictofixedwidth em088"', 0, 0, $notooltip ? 0 : 1);
1511 }
1512 if ($withpicto != 2) {
1513 $result .= $this->ref;
1514 }
1515 $result .= $linkend;
1516 if ($withpicto != 2) {
1517 $result .= (($addlabel && $this->title) ? '<span class="opacitymedium">'.$sep.dol_trunc($this->title, ($addlabel > 1 ? $addlabel : 0)).'</span>' : '');
1518 }
1519
1520 global $action;
1521 $hookmanager->initHooks(array('projectdao'));
1522 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
1523 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1524 if ($reshook > 0) {
1525 $result = $hookmanager->resPrint;
1526 } else {
1527 $result .= $hookmanager->resPrint;
1528 }
1529
1530 return $result;
1531 }
1532
1540 public function initAsSpecimen()
1541 {
1542 global $user, $langs, $conf;
1543
1544 $now = dol_now();
1545
1546 // Initialise parameters
1547 $this->id = 0;
1548 $this->ref = 'SPECIMEN';
1549 $this->entity = $conf->entity;
1550 $this->specimen = 1;
1551 $this->socid = 1;
1552 $this->date_c = $now;
1553 $this->date_m = $now;
1554 $this->date_start = $now;
1555 $this->date_end = $now + (3600 * 24 * 365);
1556 $this->note_public = 'SPECIMEN';
1557 $this->note_private = 'Private Note';
1558 $this->fk_project = 0;
1559 $this->opp_amount = 20000;
1560 $this->budget_amount = 10000;
1561
1562 $this->usage_opportunity = 1;
1563 $this->usage_task = 1;
1564 $this->usage_bill_time = 1;
1565 $this->usage_organize_event = 1;
1566
1567 /*
1568 $nbp = mt_rand(1, 9);
1569 $xnbp = 0;
1570 while ($xnbp < $nbp)
1571 {
1572 $line = new Task($this->db);
1573 $line->fk_project = 0;
1574 $line->label = $langs->trans("Label") . " " . $xnbp;
1575 $line->description = $langs->trans("Description") . " " . $xnbp;
1576
1577 $this->lines[]=$line;
1578 $xnbp++;
1579 }
1580 */
1581
1582 return 1;
1583 }
1584
1592 public function restrictedProjectArea(User $user, $mode = 'read')
1593 {
1594 // To verify role of users
1595 $userAccess = 0;
1596 if (($mode == 'read' && $user->hasRight('projet', 'all', 'lire')) || ($mode == 'write' && $user->hasRight('projet', 'all', 'creer')) || ($mode == 'delete' && $user->hasRight('projet', 'all', 'supprimer'))) {
1597 $userAccess = 1;
1598 } elseif ($this->public && (($mode == 'read' && $user->hasRight('projet', 'lire')) || ($mode == 'write' && $user->hasRight('projet', 'creer')) || ($mode == 'delete' && $user->hasRight('projet', 'supprimer')))) {
1599 $userAccess = 1;
1600 } else { // No access due to permission to read all projects, so we check if we are a contact of project
1601 foreach (array('internal', 'external') as $source) {
1602 $userRole = $this->liste_contact(4, $source);
1603 $num = count($userRole);
1604
1605 $nblinks = 0;
1606 while ($nblinks < $num) {
1607 if ($source == 'internal' && $user->id == $userRole[$nblinks]['id']) { // $userRole[$nblinks]['id'] is id of user (llx_user) for internal contacts
1608 if ($mode == 'read' && $user->hasRight('projet', 'lire')) {
1609 $userAccess++;
1610 }
1611 if ($mode == 'write' && $user->hasRight('projet', 'creer')) {
1612 $userAccess++;
1613 }
1614 if ($mode == 'delete' && $user->hasRight('projet', 'supprimer')) {
1615 $userAccess++;
1616 }
1617 }
1618 if ($source == 'external' && $user->socid > 0 && $user->socid == $userRole[$nblinks]['socid']) { // $userRole[$nblinks]['id'] is id of contact (llx_socpeople) or external contacts
1619 if ($mode == 'read' && $user->hasRight('projet', 'lire')) {
1620 $userAccess++;
1621 }
1622 if ($mode == 'write' && $user->hasRight('projet', 'creer')) {
1623 $userAccess++;
1624 }
1625 if ($mode == 'delete' && $user->hasRight('projet', 'supprimer')) {
1626 $userAccess++;
1627 }
1628 }
1629 $nblinks++;
1630 }
1631 }
1632 //if (empty($nblinks)) // If nobody has permission, we grant creator
1633 //{
1634 // if ((!empty($this->user_author_id) && $this->user_author_id == $user->id))
1635 // {
1636 // $userAccess = 1;
1637 // }
1638 //}
1639 }
1640
1641 return ($userAccess ? $userAccess : -1);
1642 }
1643
1654 public function getProjectsAuthorizedForUser($user, $mode = 0, $list = 0, $socid = 0, $filter = '')
1655 {
1656 $projects = array();
1657 $temp = array();
1658
1659 $sql = "SELECT ".(($mode == 0 || $mode == 1) ? "DISTINCT " : "")."p.rowid, p.ref";
1660 $sql .= " FROM ".MAIN_DB_PREFIX."projet as p";
1661 if ($mode == 0) {
1662 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."element_contact as ec ON ec.element_id = p.rowid";
1663 } elseif ($mode == 1) {
1664 $sql .= ", ".MAIN_DB_PREFIX."element_contact as ec";
1665 } // elseif ($mode == 2) {
1666 // No filter. Use this if user has permission to see all project
1667 // }
1668 $sql .= " WHERE p.entity IN (".getEntity('project').")";
1669 // Internal users must see project he is contact to even if project linked to a third party he can't see.
1670 //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).")";
1671 if ($socid > 0) {
1672 $sql .= " AND (p.fk_soc IS NULL OR p.fk_soc = 0 OR p.fk_soc = ".((int) $socid).")";
1673 }
1674
1675 // Get id of types of contacts for projects (This list never contains a lot of elements)
1676 $listofprojectcontacttype = array();
1677 $sql2 = "SELECT ctc.rowid, ctc.code FROM ".MAIN_DB_PREFIX."c_type_contact as ctc";
1678 $sql2 .= " WHERE ctc.element = '".$this->db->escape($this->element)."'";
1679 $sql2 .= " AND ctc.source = 'internal'";
1680 $resql = $this->db->query($sql2);
1681 if ($resql) {
1682 while ($obj = $this->db->fetch_object($resql)) {
1683 $listofprojectcontacttype[$obj->rowid] = $obj->code;
1684 }
1685 } else {
1686 dol_print_error($this->db);
1687 }
1688 if (count($listofprojectcontacttype) == 0) {
1689 $listofprojectcontacttype[0] = '0'; // To avoid syntax error if not found
1690 }
1691
1692 if ($mode == 0) {
1693 $sql .= " AND ( p.public = 1";
1694 $sql .= " OR ( ec.fk_c_type_contact IN (".$this->db->sanitize(implode(',', array_keys($listofprojectcontacttype))).")";
1695 $sql .= " AND ec.fk_socpeople = ".((int) $user->id).")";
1696 $sql .= " )";
1697 } elseif ($mode == 1) {
1698 $sql .= " AND ec.element_id = p.rowid";
1699 $sql .= " AND (";
1700 $sql .= " ( ec.fk_c_type_contact IN (".$this->db->sanitize(implode(',', array_keys($listofprojectcontacttype))).")";
1701 $sql .= " AND ec.fk_socpeople = ".((int) $user->id).")";
1702 $sql .= " )";
1703 } // elseif ($mode == 2) {
1704 // No filter. Use this if user has permission to see all project
1705 //}
1706
1707 // Manage filter
1708 $errormessage = '';
1709 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
1710 if ($errormessage) {
1711 $this->errors[] = $errormessage;
1712 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
1713 $sql .= $filter;
1714 }
1715
1716 //print $sql;
1717
1718 $resql = $this->db->query($sql);
1719 if ($resql) {
1720 $num = $this->db->num_rows($resql);
1721 $i = 0;
1722 while ($i < $num) {
1723 $row = $this->db->fetch_row($resql);
1724 $projects[$row[0]] = $row[1];
1725 $temp[] = $row[0];
1726 $i++;
1727 }
1728
1729 $this->db->free($resql);
1730
1731 if ($list) {
1732 if (empty($temp)) {
1733 return '0';
1734 }
1735 $result = implode(',', $temp);
1736 return $result;
1737 }
1738 } else {
1739 dol_print_error($this->db);
1740 }
1741
1742 return $projects;
1743 }
1744
1760 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)
1761 {
1762 global $langs, $conf;
1763
1764 $error = 0;
1765 $clone_project_id = 0; // For static toolcheck
1766
1767 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);
1768
1769 $now = dol_mktime(0, 0, 0, idate('m', dol_now()), idate('d', dol_now()), idate('Y', dol_now()));
1770
1771 $clone_project = new Project($this->db);
1772
1773 $clone_project->context['createfromclone'] = 'createfromclone';
1774
1775 $this->db->begin();
1776
1777 // Load source object
1778 $clone_project->fetch($fromid);
1779 $clone_project->fetch_optionals();
1780 if ($newthirdpartyid > 0) {
1781 $clone_project->socid = $newthirdpartyid;
1782 }
1783 $clone_project->fetch_thirdparty();
1784
1785 $orign_dt_start = $clone_project->date_start;
1786 $orign_project_ref = $clone_project->ref;
1787
1788 $clone_project->id = 0;
1789 if ($move_date) {
1790 $clone_project->date_start = $now;
1791 if (!(empty($clone_project->date_end))) {
1792 $clone_project->date_end += ($now - $orign_dt_start);
1793 }
1794 }
1795
1796 $clone_project->date_c = $now;
1797
1798 if (!$clone_note) {
1799 $clone_project->note_private = '';
1800 $clone_project->note_public = '';
1801 }
1802
1803 //Generate next ref
1804 $defaultref = '';
1805 $obj = getDolGlobalString('PROJECT_ADDON', 'mod_project_simple');
1806 // Search template files
1807 $file = '';
1808 $classname = '';
1809 $filefound = 0;
1810 $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
1811 foreach ($dirmodels as $reldir) {
1812 $file = dol_buildpath($reldir."core/modules/project/".$obj.'.php', 0);
1813 if (file_exists($file)) {
1814 $filefound = 1;
1815 dol_include_once($reldir."core/modules/project/".$obj.'.php');
1816 $modProject = new $obj();
1817 '@phan-var-force ModeleNumRefProjects $modProject';
1818 $defaultref = $modProject->getNextValue(is_object($clone_project->thirdparty) ? $clone_project->thirdparty : null, $clone_project);
1819 break;
1820 }
1821 }
1822 if (is_numeric($defaultref) && $defaultref <= 0) {
1823 $defaultref = '';
1824 }
1825
1826 $clone_project->ref = $defaultref;
1827 $clone_project->title = $langs->trans("CopyOf").' '.$clone_project->title;
1828
1829 // Create clone
1830 $result = $clone_project->create($user, $notrigger);
1831
1832 // Other options
1833 if ($result < 0) {
1834 $this->error .= $clone_project->error;
1835 $error++;
1836 }
1837
1838 if (!$error) {
1839 //Get the new project id
1840 $clone_project_id = $clone_project->id;
1841
1842 //Note Update
1843 if (!$clone_note) {
1844 $clone_project->note_private = '';
1845 $clone_project->note_public = '';
1846 } else {
1847 $this->db->begin();
1848 $res = $clone_project->update_note(dol_html_entity_decode($clone_project->note_public, ENT_QUOTES | ENT_HTML5), '_public');
1849 if ($res < 0) {
1850 $this->error .= $clone_project->error;
1851 $error++;
1852 $this->db->rollback();
1853 } else {
1854 $this->db->commit();
1855 }
1856
1857 $this->db->begin();
1858 $res = $clone_project->update_note(dol_html_entity_decode($clone_project->note_private, ENT_QUOTES | ENT_HTML5), '_private');
1859 if ($res < 0) {
1860 $this->error .= $clone_project->error;
1861 $error++;
1862 $this->db->rollback();
1863 } else {
1864 $this->db->commit();
1865 }
1866 }
1867
1868 //Duplicate contact
1869 if ($clone_contact) {
1870 $origin_project = new Project($this->db);
1871 $origin_project->fetch($fromid);
1872
1873 foreach (array('internal', 'external') as $source) {
1874 $tab = $origin_project->liste_contact(-1, $source);
1875 if (is_array($tab) && count($tab) > 0) {
1876 foreach ($tab as $contacttoadd) {
1877 $clone_project->add_contact($contacttoadd['id'], $contacttoadd['code'], $contacttoadd['source'], $notrigger);
1878 if ($clone_project->error == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
1879 $langs->load("errors");
1880 $this->error .= $langs->trans("ErrorThisContactIsAlreadyDefinedAsThisType");
1881 $error++;
1882 } else {
1883 if ($clone_project->error != '') {
1884 $this->error .= $clone_project->error;
1885 $error++;
1886 }
1887 }
1888 }
1889 } elseif ($tab < 0) {
1890 $this->error .= $origin_project->error;
1891 $error++;
1892 }
1893 }
1894 }
1895
1896 //Duplicate file
1897 if ($clone_project_file) {
1898 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1899
1900 $clone_project_dir = $conf->project->dir_output."/".dol_sanitizeFileName($defaultref);
1901 $ori_project_dir = $conf->project->dir_output."/".dol_sanitizeFileName($orign_project_ref);
1902
1903 if (dol_mkdir($clone_project_dir) >= 0) {
1904 $filearray = dol_dir_list($ori_project_dir, "files", 0, '', '(\.meta|_preview.*\.png)$', '', SORT_ASC, 1);
1905 foreach ($filearray as $key => $file) {
1906 $rescopy = dol_copy($ori_project_dir.'/'.$file['name'], $clone_project_dir.'/'.$file['name'], '0', 1);
1907 if (is_numeric($rescopy) && $rescopy < 0) {
1908 $this->error .= $langs->trans("ErrorFailToCopyFile", $ori_project_dir.'/'.$file['name'], $clone_project_dir.'/'.$file['name']);
1909 $error++;
1910 }
1911 }
1912 } else {
1913 $this->error .= $langs->trans('ErrorInternalErrorDetected').':dol_mkdir';
1914 $error++;
1915 }
1916 }
1917
1918 //Duplicate task
1919 if ($clone_task) {
1920 require_once DOL_DOCUMENT_ROOT.'/projet/class/task.class.php';
1921
1922 $taskstatic = new Task($this->db);
1923
1924 // Security check
1925 $socid = 0;
1926 if ($user->socid > 0) {
1927 $socid = $user->socid;
1928 }
1929
1930 $tasksarray = $taskstatic->getTasksArray(null, null, $fromid, $socid, 0);
1931
1932 $tab_conv_child_parent = array();
1933 $result_clone = 0;
1934
1935 // Loop on each task, to clone it
1936 foreach ($tasksarray as $tasktoclone) {
1937 $result_clone = $taskstatic->createFromClone($user, $tasktoclone->id, $clone_project_id, $tasktoclone->fk_task_parent, $move_date, true, false, $clone_task_file, true, false);
1938 if ($result_clone <= 0) {
1939 $this->error .= $taskstatic->error;
1940 $error++;
1941 } else {
1942 $new_task_id = $result_clone;
1943 $taskstatic->fetch($tasktoclone->id);
1944
1945 //manage new parent clone task id
1946 // if the current task has child we store the original task id and the equivalent clone task id
1947 if (($taskstatic->hasChildren()) && !array_key_exists($tasktoclone->id, $tab_conv_child_parent)) {
1948 $tab_conv_child_parent[$tasktoclone->id] = $new_task_id;
1949 }
1950 }
1951 }
1952
1953 //Parse all clone node to be sure to update new parent
1954 $tasksarray = $taskstatic->getTasksArray(null, null, $clone_project_id, $socid, 0);
1955 foreach ($tasksarray as $task_cloned) {
1956 $taskstatic->fetch($task_cloned->id);
1957 if ($taskstatic->fk_task_parent != 0) {
1958 $taskstatic->fk_task_parent = $tab_conv_child_parent[$taskstatic->fk_task_parent];
1959 }
1960 $res = $taskstatic->update($user, $notrigger);
1961 if ($result_clone <= 0) {
1962 $this->error .= $taskstatic->error;
1963 $error++;
1964 }
1965 }
1966 }
1967 }
1968
1969 unset($clone_project->context['createfromclone']);
1970
1971 if (!$error && $clone_project_id != 0) {
1972 $this->db->commit();
1973 return $clone_project_id;
1974 } else {
1975 $this->db->rollback();
1976 dol_syslog(get_class($this)."::createFromClone nbError: ".$error." error : ".$this->error, LOG_ERR);
1977 return -1;
1978 }
1979 }
1980
1981
1988 public function shiftTaskDate($old_project_dt_start)
1989 {
1990 global $user, $langs, $conf;
1991
1992 $error = 0;
1993 $result = 0;
1994
1995 $taskstatic = new Task($this->db);
1996
1997 // Security check
1998 $socid = 0;
1999 if ($user->socid > 0) {
2000 $socid = $user->socid;
2001 }
2002
2003 $tasksarray = $taskstatic->getTasksArray(null, null, $this->id, $socid, 0);
2004
2005 foreach ($tasksarray as $tasktoshiftdate) {
2006 $to_update = false;
2007 // Fetch only if update of date will be made
2008 if ((!empty($tasktoshiftdate->date_start)) || (!empty($tasktoshiftdate->date_end))) {
2009 //dol_syslog(get_class($this)."::shiftTaskDate to_update", LOG_DEBUG);
2010 $to_update = true;
2011 $task = new Task($this->db);
2012 $result = $task->fetch($tasktoshiftdate->id);
2013 if (!$result) {
2014 $error++;
2015 $this->error .= $task->error;
2016 }
2017 } else {
2018 continue;
2019 }
2020 //print "$this->date_start + $tasktoshiftdate->date_start - $old_project_dt_start";exit;
2021
2022 //Calculate new task start date with difference between old proj start date and origin task start date
2023 if (!empty($tasktoshiftdate->date_start)) {
2024 $task->date_start = $this->date_start + ($tasktoshiftdate->date_start - $old_project_dt_start);
2025 }
2026
2027 //Calculate new task end date with difference between origin proj end date and origin task end date
2028 if (!empty($tasktoshiftdate->date_end)) {
2029 $task->date_end = $this->date_start + ($tasktoshiftdate->date_end - $old_project_dt_start);
2030 }
2031
2032 if ($to_update) {
2033 $result = $task->update($user);
2034 if (!$result) {
2035 $error++;
2036 $this->error .= $task->error;
2037 }
2038 }
2039 }
2040 if ($error != 0) {
2041 return -1;
2042 }
2043 return $result;
2044 }
2045
2046
2047 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2055 public function update_element($tableName, $elementSelectId)
2056 {
2057 // phpcs:enable
2058 $sql = "UPDATE ".MAIN_DB_PREFIX.$tableName;
2059
2060 if ($tableName == "actioncomm") {
2061 $sql .= " SET fk_project=".$this->id;
2062 $sql .= " WHERE id=".((int) $elementSelectId);
2063 } elseif (in_array($tableName, ["entrepot","mrp_mo","stocktransfer_stocktransfer"])) {
2064 $sql .= " SET fk_project=".$this->id;
2065 $sql .= " WHERE rowid=".((int) $elementSelectId);
2066 } else {
2067 $sql .= " SET fk_projet=".$this->id;
2068 $sql .= " WHERE rowid=".((int) $elementSelectId);
2069 }
2070
2071 dol_syslog(get_class($this)."::update_element", LOG_DEBUG);
2072 $resql = $this->db->query($sql);
2073 if (!$resql) {
2074 $this->error = $this->db->lasterror();
2075 return -1;
2076 } else {
2077 return 1;
2078 }
2079 }
2080
2081 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2091 public function remove_element($tableName, $elementSelectId, $projectfield = 'fk_projet')
2092 {
2093 // phpcs:enable
2094 $sql = "UPDATE ".MAIN_DB_PREFIX.$tableName;
2095
2096 if ($tableName == "actioncomm") {
2097 $sql .= " SET fk_project=NULL";
2098 $sql .= " WHERE id=".((int) $elementSelectId);
2099 } else {
2100 $sql .= " SET ".$projectfield."=NULL";
2101 $sql .= " WHERE rowid=".((int) $elementSelectId);
2102 }
2103
2104 dol_syslog(get_class($this)."::remove_element", LOG_DEBUG);
2105 $resql = $this->db->query($sql);
2106 if (!$resql) {
2107 $this->error = $this->db->lasterror();
2108 return -1;
2109 } else {
2110 return 1;
2111 }
2112 }
2113
2124 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
2125 {
2126 global $conf, $langs;
2127
2128 $langs->load("projects");
2129
2130 if (!dol_strlen($modele)) {
2131 $modele = 'baleine';
2132
2133 if ($this->model_pdf) {
2134 $modele = $this->model_pdf;
2135 } elseif (getDolGlobalString('PROJECT_ADDON_PDF')) {
2136 $modele = getDolGlobalString('PROJECT_ADDON_PDF');
2137 }
2138 }
2139
2140 $modelpath = "core/modules/project/doc/";
2141
2142 return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
2143 }
2144
2145
2155 public function loadTimeSpent($datestart, $taskid = 0, $userid = 0)
2156 {
2157 $this->weekWorkLoad = array();
2158 $this->weekWorkLoadPerTask = array();
2159
2160 if (empty($datestart)) {
2161 dol_print_error(null, 'Error datestart parameter is empty');
2162 }
2163
2164 $sql = "SELECT ptt.rowid as taskid, ptt.element_duration, ptt.element_date, ptt.element_datehour, ptt.fk_element";
2165 $sql .= " FROM ".MAIN_DB_PREFIX."element_time AS ptt, ".MAIN_DB_PREFIX."projet_task as pt";
2166 $sql .= " WHERE ptt.fk_element = pt.rowid";
2167 $sql .= " AND ptt.elementtype = 'task'";
2168 $sql .= " AND pt.fk_projet = ".((int) $this->id);
2169 $sql .= " AND (ptt.element_date >= '".$this->db->idate($datestart)."' ";
2170 $sql .= " AND ptt.element_date <= '".$this->db->idate(dol_time_plus_duree($datestart, 1, 'w') - 1)."')";
2171 if ($taskid) {
2172 $sql .= " AND ptt.fk_element=".((int) $taskid);
2173 }
2174 if (is_numeric($userid)) {
2175 $sql .= " AND ptt.fk_user=".((int) $userid);
2176 }
2177
2178 //print $sql;
2179 $resql = $this->db->query($sql);
2180 if ($resql) {
2181 $dayallreadyfound = array();
2182
2183 $num = $this->db->num_rows($resql);
2184 $i = 0;
2185 // Loop on each record found, so each couple (project id, task id)
2186 while ($i < $num) {
2187 $obj = $this->db->fetch_object($resql);
2188 $day = $this->db->jdate($obj->element_date); // task_date is date without hours
2189 if (empty($dayallreadyfound[$day])) {
2190 $this->weekWorkLoad[$day] = $obj->element_duration;
2191 $this->weekWorkLoadPerTask[$day][$obj->fk_element] = $obj->element_duration;
2192 } else {
2193 $this->weekWorkLoad[$day] += $obj->element_duration;
2194 $this->weekWorkLoadPerTask[$day][$obj->fk_element] += $obj->element_duration;
2195 }
2196 $dayallreadyfound[$day] = 1;
2197 $i++;
2198 }
2199 $this->db->free($resql);
2200 return 1;
2201 } else {
2202 $this->error = "Error ".$this->db->lasterror();
2203 dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
2204 return -1;
2205 }
2206 }
2207
2217 public function loadTimeSpentMonth($datestart, $taskid = 0, $userid = 0)
2218 {
2219 $this->monthWorkLoad = array();
2220 $this->monthWorkLoadPerTask = array();
2221
2222 if (empty($datestart)) {
2223 dol_print_error(null, 'Error datestart parameter is empty');
2224 }
2225
2226 $sql = "SELECT ptt.rowid as taskid, ptt.element_duration, ptt.element_date, ptt.element_datehour, ptt.fk_element";
2227 $sql .= " FROM ".MAIN_DB_PREFIX."element_time AS ptt, ".MAIN_DB_PREFIX."projet_task as pt";
2228 $sql .= " WHERE ptt.fk_element = pt.rowid";
2229 $sql .= " AND ptt.elementtype = 'task'";
2230 $sql .= " AND pt.fk_projet = ".((int) $this->id);
2231 $sql .= " AND (ptt.element_date >= '".$this->db->idate($datestart)."' ";
2232 $sql .= " AND ptt.element_date <= '".$this->db->idate(dol_time_plus_duree($datestart, 1, 'm') - 1)."')";
2233 if ($taskid) {
2234 $sql .= " AND ptt.fk_element=".((int) $taskid);
2235 }
2236 if (is_numeric($userid)) {
2237 $sql .= " AND ptt.fk_user=".((int) $userid);
2238 }
2239
2240 //print $sql;
2241 $resql = $this->db->query($sql);
2242 if ($resql) {
2243 $weekalreadyfound = array();
2244
2245 $num = $this->db->num_rows($resql);
2246 $i = 0;
2247 $week_number = ''; // Initialisation for static analysis
2248 // Loop on each record found, so each couple (project id, task id)
2249 while ($i < $num) {
2250 $obj = $this->db->fetch_object($resql);
2251 if (!empty($obj->element_date)) {
2252 $date = explode('-', $obj->element_date);
2253 $week_number = getWeekNumber((int) $date[2], (int) $date[1], (int) $date[0]);
2254 }
2255 if (empty($weekalreadyfound[$week_number])) {
2256 $this->monthWorkLoad[$week_number] = $obj->element_duration;
2257 $this->monthWorkLoadPerTask[$week_number][$obj->fk_element] = $obj->element_duration;
2258 } else {
2259 $this->monthWorkLoad[$week_number] += $obj->element_duration;
2260 $this->monthWorkLoadPerTask[$week_number][$obj->fk_element] += $obj->element_duration;
2261 }
2262 $weekalreadyfound[$week_number] = 1;
2263 $i++;
2264 }
2265 $this->db->free($resql);
2266 return 1;
2267 } else {
2268 $this->error = "Error ".$this->db->lasterror();
2269 dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
2270 return -1;
2271 }
2272 }
2273
2274 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2281 public function load_board($user)
2282 {
2283 // phpcs:enable
2284 global $conf, $langs;
2285
2286 // For external user, no check is done on company because readability is managed by public status of project and assignment.
2287 //$socid=$user->socid;
2288
2289 $response = new WorkboardResponse();
2290 $response->warning_delay = $conf->project->warning_delay / 60 / 60 / 24;
2291 $response->label = $langs->trans("OpenedProjects");
2292 $response->labelShort = $langs->trans("Opened");
2293 $response->url = DOL_URL_ROOT.'/projet/list.php?search_project_user=-1&search_status=1&mainmenu=project';
2294 $response->url_late = DOL_URL_ROOT.'/projet/list.php?search_option=late&mainmenu=project';
2295 $response->img = img_object('', "projectpub");
2296 $response->nbtodo = 0;
2297 $response->nbtodolate = 0;
2298
2299 $sql = "SELECT p.rowid, p.fk_statut as status, p.fk_opp_status, p.datee as datee";
2300 $sql .= " FROM (".MAIN_DB_PREFIX."projet as p";
2301 $sql .= ")";
2302 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s on p.fk_soc = s.rowid";
2303 // For external user, no check is done on company permission because readability is managed by public status of project and assignment.
2304 //if (! $user->rights->societe->client->voir && ! $socid) $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc = s.rowid";
2305 $sql .= " WHERE p.fk_statut = 1";
2306 $sql .= " AND p.entity IN (".getEntity('project').')';
2307
2308
2309 $projectsListId = null;
2310 if (!$user->hasRight("projet", "all", "lire")) {
2311 $response->url = DOL_URL_ROOT.'/projet/list.php?search_status=1&mainmenu=project';
2312 $projectsListId = $this->getProjectsAuthorizedForUser($user, 0, 1);
2313 if (empty($projectsListId)) {
2314 return $response;
2315 }
2316
2317 $sql .= " AND p.rowid IN (".$this->db->sanitize($projectsListId).")";
2318 }
2319
2320 // No need to check company, as filtering of projects must be done by getProjectsAuthorizedForUser
2321 //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).")";
2322 // For external user, no check is done on company permission because readability is managed by public status of project and assignment.
2323 //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))";
2324
2325 //print $sql;
2326 $resql = $this->db->query($sql);
2327 if ($resql) {
2328 $project_static = new Project($this->db);
2329
2330
2331 // This assignment in condition is not a bug. It allows walking the results.
2332 while ($obj = $this->db->fetch_object($resql)) {
2333 $response->nbtodo++;
2334
2335 $project_static->statut = $obj->status;
2336 $project_static->status = $obj->status;
2337 $project_static->opp_status = $obj->fk_opp_status;
2338 $project_static->date_end = $this->db->jdate($obj->datee);
2339
2340 if ($project_static->hasDelay()) {
2341 $response->nbtodolate++;
2342 }
2343 }
2344
2345 return $response;
2346 }
2347
2348 $this->error = $this->db->error();
2349 return -1;
2350 }
2351
2360 public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
2361 {
2362 $tables = array(
2363 'projet'
2364 );
2365
2366 return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
2367 }
2368
2369
2375 public function loadStateBoard()
2376 {
2377 global $user;
2378
2379 $this->nb = array();
2380
2381 $sql = "SELECT count(p.rowid) as nb";
2382 $sql .= " FROM ".MAIN_DB_PREFIX."projet as p";
2383 $sql .= " WHERE";
2384 $sql .= " p.entity IN (".getEntity('project').")";
2385 if (!$user->hasRight('projet', 'all', 'lire')) {
2386 $projectsListId = $this->getProjectsAuthorizedForUser($user, 0, 1);
2387 $sql .= "AND p.rowid IN (".$this->db->sanitize($projectsListId).")";
2388 }
2389
2390 $resql = $this->db->query($sql);
2391 if ($resql) {
2392 while ($obj = $this->db->fetch_object($resql)) {
2393 $this->nb["projects"] = $obj->nb;
2394 }
2395 $this->db->free($resql);
2396 return 1;
2397 } else {
2398 dol_print_error($this->db);
2399 $this->error = $this->db->error();
2400 return -1;
2401 }
2402 }
2403
2404
2410 public function hasDelay()
2411 {
2412 global $conf;
2413
2414 if (!($this->status == self::STATUS_VALIDATED)) {
2415 return false;
2416 }
2417 if (!$this->date_end) {
2418 return false;
2419 }
2420
2421 $now = dol_now();
2422
2423 return ($this->date_end) < ($now - $conf->project->warning_delay);
2424 }
2425
2426
2433 public function info($id)
2434 {
2435 $sql = 'SELECT c.rowid, datec as datec, tms as datem,';
2436 $sql .= ' date_close as datecloture,';
2437 $sql .= ' fk_user_creat as fk_user_author, fk_user_close as fk_user_cloture';
2438 $sql .= ' FROM '.MAIN_DB_PREFIX.'projet as c';
2439 $sql .= ' WHERE c.rowid = '.((int) $id);
2440 $result = $this->db->query($sql);
2441 if ($result) {
2442 if ($this->db->num_rows($result)) {
2443 $obj = $this->db->fetch_object($result);
2444
2445 $this->id = $obj->rowid;
2446
2447 $this->user_creation_id = $obj->fk_user_author;
2448 $this->user_closing_id = $obj->fk_user_cloture;
2449
2450 $this->date_creation = $this->db->jdate($obj->datec);
2451 $this->date_modification = $this->db->jdate($obj->datem);
2452 $this->date_cloture = $this->db->jdate($obj->datecloture);
2453 }
2454
2455 $this->db->free($result);
2456 } else {
2457 dol_print_error($this->db);
2458 }
2459 }
2460
2471 public function setCategories($categories)
2472 {
2473 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
2474 return parent::setCategoriesCommon($categories, Categorie::TYPE_PROJECT);
2475 }
2476
2477
2485 public function getLinesArray($user, $loadRoleMode = 1)
2486 {
2487 require_once DOL_DOCUMENT_ROOT.'/projet/class/task.class.php';
2488 $taskstatic = new Task($this->db);
2489
2490 $this->lines = $taskstatic->getTasksArray(null, $user, $this->id, 0, 0, '', '-1', '', 0, 0, null, 0, array(), 0, $loadRoleMode);
2491 return 1;
2492 }
2493
2512 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 = '')
2513 {
2514 // TODO EMAIL
2515
2516 return 1;
2517 }
2518
2527 public function getKanbanView($option = '', $arraydata = null, $size = '')
2528 {
2529 global $conf, $langs;
2530
2531 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
2532
2533 if (empty($size)) {
2534 if (empty($conf->dol_optimize_smallscreen)) {
2535 $size = 'large';
2536 } else {
2537 $size = 'small';
2538 }
2539 }
2540
2541 $return = '<div class="box-flex-item '.($size == 'small' ? 'box-flex-item-small' : '').' box-flex-grow-zero">';
2542 $return .= '<div class="info-box info-box-sm">';
2543 $return .= '<span class="info-box-icon bg-infobox-action">';
2544 $return .= img_picto('', $this->public ? 'projectpub' : $this->picto);
2545 //$return .= '<i class="fa fa-dol-action"></i>'; // Can be image
2546 $return .= '</span>';
2547 $return .= '<div class="info-box-content">';
2548 $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref);
2549 if ($this->hasDelay()) {
2550 $return .= img_warning($langs->trans('Late'));
2551 }
2552 $return .= '</span>';
2553 if ($selected >= 0) {
2554 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
2555 }
2556 // Date
2557 /*
2558 if (property_exists($this, 'date_start') && $this->date_start) {
2559 $return .= '<br><span class="info-box-label">'.dol_print_date($this->date_start, 'day').'</>';
2560 }
2561 if (property_exists($this, 'date_end') && $this->date_end) {
2562 if ($this->date_start) {
2563 $return .= ' - ';
2564 } else {
2565 $return .= '<br>';
2566 }
2567 $return .= '<span class="info-box-label">'.dol_print_date($this->date_end, 'day').'</span>';
2568 }*/
2569 if (property_exists($this, 'thirdparty') && !is_null($this->thirdparty) && is_object($this->thirdparty) && $this->thirdparty instanceof Societe) {
2570 $return .= '<br><div class="info-box-ref tdoverflowmax125 inline-block valignmiddle">'.$this->thirdparty->getNomUrl(1);
2571 $return .= '</div>';
2572 if (!empty($this->thirdparty->phone)) {
2573 $return .= '<div class="inline-block valignmiddle">';
2574 $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
2575 $return .= '</div>';
2576 }
2577 if (!empty($this->thirdparty->email)) {
2578 $return .= '<div class="inline-block valignmiddle">';
2579 $return .= dol_print_email($this->thirdparty->email, 0, $this->thirdparty->id, 'thirdparty', -1, 1, 2, 'paddingleft paddingright');
2580 $return .= '</div>';
2581 }
2582 }
2583 if (!empty($arraydata['assignedusers'])) {
2584 $return .= '<br>';
2585 if ($this->public) {
2586 $return .= img_picto($langs->trans('Visibility').': '.$langs->trans('SharedProject'), 'world', 'class="paddingrightonly valignmiddle"');
2587 //print $langs->trans('SharedProject');
2588 } else {
2589 $return .= img_picto($langs->trans('Visibility').': '.$langs->trans('PrivateProject'), 'private', 'class="paddingrightonly valignmiddle"');
2590 //print $langs->trans('PrivateProject');
2591 }
2592
2593 $return .= ' <span class="small valignmiddle">'.$arraydata['assignedusers'].'</span>';
2594 }
2595 /*if (property_exists($this, 'user_author_id')) {
2596 $return .= '<br><span class="info-box-label opacitymedium">'.$langs->trans("Author").'</span>';
2597 $return .= '<span> : '.$user->getNomUrl(1).'</span>';
2598 }*/
2599 $return .= '<br><div>'; // start div line status
2600 if ($this->usage_opportunity && $this->opp_status_code) {
2601 //$return .= '<br><span class="info-bo-label opacitymedium">'.$langs->trans("OpportunityStatusShort").'</span>';
2602 //$return .= '<div class="small inline-block">'.dol_trunc($langs->trans("OppStatus".$this->opp_status_code), 5).'</div>';
2603 $return .= '<div class="opacitymedium small marginrightonly inline-block" title="'.dol_escape_htmltag($langs->trans("OppStatus".$this->opp_status_code)).'">'.round($this->opp_percent).'%</div>';
2604 $return .= ' <div class="amount small marginrightonly inline-block">'.price($this->opp_amount).'</div>';
2605 }
2606 if (method_exists($this, 'getLibStatut')) {
2607 $return .= '<div class="info-box-status small inline-block valignmiddle">'.$this->getLibStatut(3).'</div>';
2608 }
2609 $return .= '</div>'; // end div line status
2610
2611 $return .= '</div>';
2612 $return .= '</div>';
2613 $return .= '</div>';
2614
2615 return $return;
2616 }
2617
2623 public function getChildren()
2624 {
2625 $children = [];
2626 $sql = 'SELECT rowid,title';
2627 $sql .= ' FROM '.MAIN_DB_PREFIX.'projet';
2628 $sql .= ' WHERE fk_project = '.((int) $this->id);
2629 $sql .= ' ORDER BY title';
2630 $result = $this->db->query($sql);
2631 if ($result) {
2632 $n = $this->db->num_rows($result);
2633 while ($n) {
2634 $children[] = $this->db->fetch_object($result);
2635 $n--;
2636 }
2637 $this->db->free($result);
2638 } else {
2639 dol_print_error($this->db);
2640 }
2641
2642 return $children;
2643 }
2644
2649 public function createWeeklyReport()
2650 {
2651 global $mysoc, $user;
2652
2653 $now = dol_now();
2654 $nowDate = dol_getdate($now, true);
2655
2656 $errormesg = '';
2657 $errorsMsg = array();
2658
2659 $firstDayOfWeekTS = dol_get_first_day_week($nowDate['mday'], $nowDate['mon'], $nowDate['year']);
2660
2661 $firstDayOfWeekDate = dol_mktime(0, 0, 0, $nowDate['mon'], $firstDayOfWeekTS['first_day'], $nowDate['year']);
2662
2663 $lastWeekStartTS = dol_time_plus_duree($firstDayOfWeekDate, -7, 'd');
2664
2665 $lastWeekEndTS = dol_time_plus_duree($lastWeekStartTS, 6, 'd');
2666
2667 $startDate = dol_print_date($lastWeekStartTS, '%Y-%m-%d 00:00:00');
2668 $endDate = dol_print_date($lastWeekEndTS, '%Y-%m-%d 23:59:59');
2669
2670 $sql = "SELECT
2671 u.rowid AS user_id,
2672 CONCAT(u.firstname, ' ', u.lastname) AS name,
2673 u.email,u.weeklyhours,
2674 SUM(et.element_duration) AS total_seconds
2675 FROM
2676 ".MAIN_DB_PREFIX."element_time AS et
2677 JOIN
2678 ".MAIN_DB_PREFIX."user AS u ON et.fk_user = u.rowid
2679 WHERE
2680 et.element_date BETWEEN '".$this->db->escape($startDate)."' AND '".$this->db->escape($endDate)."'
2681 AND et.elementtype = 'task'
2682 GROUP BY
2683 et.fk_user";
2684
2685 $resql = $this->db->query($sql);
2686 if (!$resql) {
2687 dol_print_error($this->db);
2688 return -1;
2689 } else {
2690 $reportContent = "<span>Weekly time report from $startDate to $endDate </span><br><br>";
2691 $reportContent .= '<table border="1" style="border-collapse: collapse;">';
2692 $reportContent .= '<tr><th>Nom d\'utilisateur</th><th>Temps saisi (heures)</th><th>Temps travaillé par semaine (heures)</th></tr>';
2693
2694 $weekendEnabled = 0;
2695 $to = '';
2696 $nbMailSend = 0;
2697 $error = 0;
2698 $errors_to = '';
2699 while ($obj = $this->db->fetch_object($resql)) {
2700 $to = $obj->email;
2701 $numHolidays = num_public_holiday($lastWeekStartTS, $lastWeekEndTS, $mysoc->country_code, 1);
2702 if (getDolGlobalString('MAIN_NON_WORKING_DAYS_INCLUDE_SATURDAY') && getDolGlobalString('MAIN_NON_WORKING_DAYS_INCLUDE_SUNDAY')) {
2703 $numHolidays -= 2;
2704 $weekendEnabled = 2;
2705 }
2706
2707 $dailyHours = $obj->weeklyhours / (7 - $weekendEnabled);
2708
2709 // Adjust total on seconde
2710 $adjustedSeconds = $obj->total_seconds + ($numHolidays * $dailyHours * 3600);
2711
2712 $totalHours = round($adjustedSeconds / 3600, 2);
2713
2714 $reportContent .= "<tr><td>{$obj->name}</td><td>{$totalHours}</td><td>".round($obj->weeklyhours, 2)."</td></tr>";
2715
2716 $reportContent .= '</table>';
2717
2718 require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
2719
2720 // PREPARE EMAIL
2721 $errormesg = '';
2722
2723 $subject = 'Rapport hebdomadaire des temps travaillés';
2724
2725 $from = getDolGlobalString('MAIN_MAIL_EMAIL_FROM');
2726 if (empty($from)) {
2727 $errormesg = "Failed to get sender into global setup MAIN_MAIL_EMAIL_FROM";
2728 $error++;
2729 }
2730
2731 $mail = new CMailFile($subject, $to, $from, $reportContent, array(), array(), array(), '', '', 0, -1, '', '', '', 'text/html');
2732
2733 if ($mail->sendfile()) {
2734 $nbMailSend++;
2735
2736 // Add a line into event table
2737 require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
2738
2739 // Insert record of emails sent
2740 $actioncomm = new ActionComm($this->db);
2741
2742 $actioncomm->type_code = 'AC_OTH_AUTO'; // Event insert into agenda automatically
2743 $actioncomm->socid = $this->thirdparty->id; // To link to a company
2744 $actioncomm->contact_id = 0;
2745
2746 $actioncomm->code = 'AC_EMAIL';
2747 $actioncomm->label = 'createWeeklyReportOK()';
2748 $actioncomm->fk_project = $this->id;
2749 $actioncomm->datep = dol_now();
2750 $actioncomm->datef = $actioncomm->datep;
2751 $actioncomm->percentage = -1; // Not applicable
2752 $actioncomm->authorid = $user->id; // User saving action
2753 $actioncomm->userownerid = $user->id; // Owner of action
2754 // Fields when action is an email (content should be added into note)
2755 $actioncomm->email_msgid = $mail->msgid;
2756 $actioncomm->email_subject = $subject;
2757 $actioncomm->email_from = $from;
2758 $actioncomm->email_sender = '';
2759 $actioncomm->email_to = $to;
2760
2761 $actioncomm->errors_to = $errors_to;
2762
2763 $actioncomm->elementtype = 'project_task';
2764 $actioncomm->fk_element = (int) $this->element;
2765
2766 $actioncomm->create($user);
2767 } else {
2768 $errormesg = $mail->error.' : '.$to;
2769 $error++;
2770
2771 // Add a line into event table
2772 require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
2773
2774 // Insert record of emails sent
2775 $actioncomm = new ActionComm($this->db);
2776
2777 $actioncomm->type_code = 'AC_OTH_AUTO'; // Event insert into agenda automatically
2778 $actioncomm->socid = $this->thirdparty->id; // To link to a company
2779 $actioncomm->contact_id = 0;
2780
2781 $actioncomm->code = 'AC_EMAIL';
2782 $actioncomm->label = 'createWeeklyReportKO()';
2783 $actioncomm->note_private = $errormesg;
2784 $actioncomm->fk_project = $this->id;
2785 $actioncomm->datep = dol_now();
2786 $actioncomm->datef = $actioncomm->datep;
2787 $actioncomm->authorid = $user->id; // User saving action
2788 $actioncomm->userownerid = $user->id; // Owner of action
2789 // Fields when action is an email (content should be added into note)
2790 $actioncomm->email_msgid = $mail->msgid;
2791 $actioncomm->email_from = $from;
2792 $actioncomm->email_sender = '';
2793 $actioncomm->email_to = $to;
2794
2795 $actioncomm->errors_to = $errors_to;
2796
2797 $actioncomm->elementtype = 'project_task';
2798 $actioncomm->fk_element = (int) $this->element;
2799
2800 $actioncomm->create($user);
2801 }
2802 $this->db->commit();
2803 }
2804 }
2805 if (!empty($errormesg)) {
2806 $errorsMsg[] = $errormesg;
2807 }
2808
2809 if (!$error) {
2810 $this->output .= 'Nb of emails sent : '.$nbMailSend;
2811 dol_syslog(__METHOD__." end - ".$this->output, LOG_INFO);
2812 return 0;
2813 } else {
2814 $this->error = 'Nb of emails sent : '.$nbMailSend.', '.(empty($errorsMsg) ? $error : implode(', ', $errorsMsg));
2815 dol_syslog(__METHOD__." end - ".$this->error, LOG_INFO);
2816 return $error;
2817 }
2818 }
2819}
$object ref
Definition info.php:79
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.
call_trigger($triggerName, $user)
Call trigger based on this instance.
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 picto eventually)
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.
getProjectsAuthorizedForUser($user, $mode=0, $list=0, $socid=0, $filter='')
Return array of projects a user has permission on, is affected to, or all projects.
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.
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:162
dol_get_first_day_week($day, $month, $year, $gm=false)
Return first day of week for a date.
Definition date.lib.php:671
getWeekNumber($day, $month, $year)
Return week number.
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0)
Add a delay to a date.
Definition date.lib.php:125
num_public_holiday($timestampStart, $timestampEnd, $country_code='', $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:765
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0, $indexdatabase=1, $nolog=0)
Remove a directory $dir and its subdirectories (or only files and subdirectories)
dol_copy($srcfile, $destfile, $newmask='0', $overwriteifexists=1, $testvirus=0, $indexdatabase=0)
Copy a file to another file.
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:63
dol_html_entity_decode($a, $b, $c='UTF-8', $keepsomeentities=0)
Replace html_entity_decode functions to manage errors.
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...
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
img_warning($titlealt='default', $moreatt='', $morecss='pictowarning')
Show warning logo.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
dol_print_phone($phone, $countrycode='', $cid=0, $socid=0, $addlink='', $separ="&nbsp;", $withpicto='', $titlealt='', $adddivfloat=0, $morecss='paddingright')
Format phone numbers according to country.
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.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
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, $cid=0, $socid=0, $addlink=0, $max=64, $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_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
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.
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)