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
96 public $dateo;
97
101 public $date_start;
102
108 public $datee;
109
113 public $date_end;
114
118 public $date_start_event;
119
123 public $date_end_event;
124
128 public $location;
129
133 public $date_close;
134
135 public $socid; // To store id of thirdparty
136
140 public $thirdparty_name; // To store name of thirdparty (defined only in some cases)
141
143
147 public $fk_user_close;
148
149 public $public;
150
154 public $budget_amount;
155
159 public $usage_opportunity;
160
164 public $usage_task;
165
169 public $usage_bill_time; // Is the time spent on project must be invoiced or not
170
174 public $usage_organize_event;
175
179 public $accept_conference_suggestions;
180
184 public $accept_booth_suggestions;
185
189 public $price_registration;
190
194 public $price_booth;
195
199 public $max_attendees;
200
206 public $statut; // 0=draft, 1=opened, 2=closed
207
211 public $opp_status; // opportunity status, into table llx_c_lead_status
212
216 public $opp_status_code;
217
221 public $fk_opp_status; // opportunity status, into table llx_c_lead_status
222
226 public $opp_amount; // opportunity amount
227
231 public $opp_percent; // opportunity probability
232
236 public $opp_weighted_amount; // opportunity weighted amount
237
241 public $email_msgid;
242
243 public $oldcopy;
244
245 public $weekWorkLoad; // Used to store workload details of a projet
246 public $weekWorkLoadPerTask; // Used to store workload details of tasks of a projet
247
251 public $monthWorkLoad;
252
256 public $monthWorkLoadPerTask;
257
263 public $datec;
264
268 public $date_c;
269
275 public $datem;
276
280 public $date_m;
281
285 public $ip;
286
290 public $lines;
291
332 // BEGIN MODULEBUILDER PROPERTIES
336 public $fields = array(
337 'rowid' => array('type' => 'integer', 'label' => 'ID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 10),
338 'fk_project' => array('type' => 'integer', 'label' => 'Parent', 'enabled' => 1, 'visible' => 1, 'notnull' => 0, 'position' => 12),
339 'ref' => array('type' => 'varchar(50)', 'label' => 'Ref', 'enabled' => 1, 'visible' => 1, 'showoncombobox' => 1, 'position' => 15, 'searchall' => 1),
340 'title' => array('type' => 'varchar(255)', 'label' => 'ProjectLabel', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'position' => 17, 'showoncombobox' => 2, 'searchall' => 1),
341 'entity' => array('type' => 'integer', 'label' => 'Entity', 'default' => '1', 'enabled' => 1, 'visible' => 3, 'notnull' => 1, 'position' => 19),
342 'fk_soc' => array('type' => 'integer', 'label' => 'Thirdparty', 'enabled' => 1, 'visible' => 0, 'position' => 20),
343 'dateo' => array('type' => 'date', 'label' => 'DateStart', 'enabled' => 1, 'visible' => -1, 'position' => 30),
344 'datee' => array('type' => 'date', 'label' => 'DateEnd', 'enabled' => 1, 'visible' => 1, 'position' => 35),
345 'description' => array('type' => 'text', 'label' => 'Description', 'enabled' => 1, 'visible' => 3, 'position' => 55, 'searchall' => 1),
346 'public' => array('type' => 'integer', 'label' => 'Visibility', 'enabled' => 1, 'visible' => -1, 'position' => 65),
347 'fk_opp_status' => array('type' => 'integer:CLeadStatus:core/class/cleadstatus.class.php', 'label' => 'OpportunityStatusShort', 'enabled' => 'getDolGlobalString("PROJECT_USE_OPPORTUNITIES")', 'visible' => 1, 'position' => 75),
348 'opp_percent' => array('type' => 'double(5,2)', 'label' => 'OpportunityProbabilityShort', 'enabled' => 'getDolGlobalString("PROJECT_USE_OPPORTUNITIES")', 'visible' => 1, 'position' => 80),
349 'note_private' => array('type' => 'html', 'label' => 'NotePrivate', 'enabled' => 1, 'visible' => 0, 'position' => 85, 'searchall' => 1),
350 'note_public' => array('type' => 'html', 'label' => 'NotePublic', 'enabled' => 1, 'visible' => 0, 'position' => 90, 'searchall' => 1),
351 'model_pdf' => array('type' => 'varchar(255)', 'label' => 'ModelPdf', 'enabled' => 1, 'visible' => 0, 'position' => 95),
352 'date_close' => array('type' => 'datetime', 'label' => 'DateClosing', 'enabled' => 1, 'visible' => 0, 'position' => 105),
353 'fk_user_close' => array('type' => 'integer', 'label' => 'UserClosing', 'enabled' => 1, 'visible' => 0, 'position' => 110),
354 'opp_amount' => array('type' => 'double(24,8)', 'label' => 'OpportunityAmountShort', 'enabled' => 1, 'visible' => 'getDolGlobalString("PROJECT_USE_OPPORTUNITIES")', 'position' => 115),
355 'budget_amount' => array('type' => 'double(24,8)', 'label' => 'Budget', 'enabled' => 1, 'visible' => -1, 'position' => 119),
356 'usage_opportunity' => array('type' => 'integer', 'label' => 'UsageOpportunity', 'enabled' => 1, 'visible' => -1, 'position' => 130),
357 'usage_task' => array('type' => 'integer', 'label' => 'UsageTasks', 'enabled' => 1, 'visible' => -1, 'position' => 135),
358 'usage_bill_time' => array('type' => 'integer', 'label' => 'UsageBillTimeShort', 'enabled' => 1, 'visible' => -1, 'position' => 140),
359 'usage_organize_event' => array('type' => 'integer', 'label' => 'UsageOrganizeEvent', 'enabled' => 1, 'visible' => -1, 'position' => 145),
360 // Properties for event organization
361 'date_start_event' => array('type' => 'date', 'label' => 'DateStartEvent', 'enabled' => "isModEnabled('eventorganization')", 'visible' => 1, 'position' => 200),
362 'date_end_event' => array('type' => 'date', 'label' => 'DateEndEvent', 'enabled' => "isModEnabled('eventorganization')", 'visible' => 1, 'position' => 201),
363 'location' => array('type' => 'text', 'label' => 'Location', 'enabled' => 1, 'visible' => 3, 'position' => 55, 'searchall' => 202),
364 'accept_conference_suggestions' => array('type' => 'integer', 'label' => 'AllowUnknownPeopleSuggestConf', 'enabled' => 1, 'visible' => -1, 'position' => 210),
365 'accept_booth_suggestions' => array('type' => 'integer', 'label' => 'AllowUnknownPeopleSuggestBooth', 'enabled' => 1, 'visible' => -1, 'position' => 211),
366 'price_registration' => array('type' => 'double(24,8)', 'label' => 'PriceOfRegistration', 'enabled' => 1, 'visible' => -1, 'position' => 212),
367 'price_booth' => array('type' => 'double(24,8)', 'label' => 'PriceOfBooth', 'enabled' => 1, 'visible' => -1, 'position' => 215),
368 'max_attendees' => array('type' => 'integer', 'label' => 'MaxNbOfAttendees', 'enabled' => 1, 'visible' => -1, 'position' => 215),
369 // Generic
370 'datec' => array('type' => 'datetime', 'label' => 'DateCreationShort', 'enabled' => 1, 'visible' => -2, 'position' => 400),
371 'tms' => array('type' => 'timestamp', 'label' => 'DateModificationShort', 'enabled' => 1, 'visible' => -2, 'notnull' => 1, 'position' => 405),
372 'fk_user_creat' => array('type' => 'integer', 'label' => 'UserCreation', 'enabled' => 1, 'visible' => 0, 'notnull' => 1, 'position' => 410),
373 'fk_user_modif' => array('type' => 'integer', 'label' => 'UserModification', 'enabled' => 1, 'visible' => 0, 'position' => 415),
374 'import_key' => array('type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'visible' => -1, 'position' => 420),
375 'email_msgid' => array('type' => 'varchar(255)', 'label' => 'EmailMsgID', 'enabled' => 1, 'visible' => -1, 'position' => 450, 'help' => 'EmailMsgIDWhenSourceisEmail', 'csslist' => 'tdoverflowmax125'),
376 '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')),
377 );
378 // END MODULEBUILDER PROPERTIES
379
383 const STATUS_DRAFT = 0;
384
389
393 const STATUS_CLOSED = 2;
394
395
401 public function __construct($db)
402 {
403 global $conf;
404
405 $this->db = $db;
406
407 $this->ismultientitymanaged = 1;
408 $this->isextrafieldmanaged = 1;
409
410 $this->labelStatusShort = array(0 => 'Draft', 1 => 'Opened', 2 => 'Closed');
411 $this->labelStatus = array(0 => 'Draft', 1 => 'Opened', 2 => 'Closed');
412
413 global $conf;
414
415 if (!getDolGlobalString('MAIN_SHOW_TECHNICAL_ID')) {
416 $this->fields['rowid']['visible'] = 0;
417 }
418
419 if (!getDolGlobalString('PROJECT_USE_OPPORTUNITIES')) {
420 $this->fields['fk_opp_status']['enabled'] = 0;
421 $this->fields['opp_percent']['enabled'] = 0;
422 $this->fields['opp_amount']['enabled'] = 0;
423 $this->fields['usage_opportunity']['enabled'] = 0;
424 }
425
426 if (getDolGlobalString('PROJECT_HIDE_TASKS')) {
427 $this->fields['usage_bill_time']['visible'] = 0;
428 $this->fields['usage_task']['visible'] = 0;
429 }
430
431 if (empty($conf->eventorganization->enabled)) {
432 $this->fields['usage_organize_event']['visible'] = 0;
433 $this->fields['accept_conference_suggestions']['enabled'] = 0;
434 $this->fields['accept_booth_suggestions']['enabled'] = 0;
435 $this->fields['price_registration']['enabled'] = 0;
436 $this->fields['price_booth']['enabled'] = 0;
437 $this->fields['max_attendees']['enabled'] = 0;
438 }
439 }
440
448 public function create($user, $notrigger = 0)
449 {
450 $error = 0;
451 $ret = 0;
452
453 $now = dol_now();
454
455 // Clean parameters
456 $this->note_private = dol_substr($this->note_private, 0, 65535);
457 $this->note_public = dol_substr($this->note_public, 0, 65535);
458
459 // Check parameters
460 if (!trim($this->ref)) {
461 $this->error = 'ErrorFieldsRequired';
462 dol_syslog(get_class($this)."::create error -1 ref null", LOG_ERR);
463 return -1;
464 }
465 if (getDolGlobalString('PROJECT_THIRDPARTY_REQUIRED') && !($this->socid > 0)) {
466 $this->error = 'ErrorFieldsRequired';
467 dol_syslog(get_class($this)."::create error -1 thirdparty not defined and option PROJECT_THIRDPARTY_REQUIRED is set", LOG_ERR);
468 return -1;
469 }
470
471 // Create project
472 $this->db->begin();
473
474 $sql = "INSERT INTO ".MAIN_DB_PREFIX."projet (";
475 $sql .= "ref";
476 $sql .= ", fk_project";
477 $sql .= ", title";
478 $sql .= ", description";
479 $sql .= ", fk_soc";
480 $sql .= ", fk_user_creat";
481 $sql .= ", fk_statut";
482 $sql .= ", fk_opp_status";
483 $sql .= ", opp_percent";
484 $sql .= ", public";
485 $sql .= ", datec";
486 $sql .= ", dateo";
487 $sql .= ", datee";
488 $sql .= ", opp_amount";
489 $sql .= ", budget_amount";
490 $sql .= ", usage_opportunity";
491 $sql .= ", usage_task";
492 $sql .= ", usage_bill_time";
493 $sql .= ", usage_organize_event";
494 $sql .= ", accept_conference_suggestions";
495 $sql .= ", accept_booth_suggestions";
496 $sql .= ", price_registration";
497 $sql .= ", price_booth";
498 $sql .= ", max_attendees";
499 $sql .= ", date_start_event";
500 $sql .= ", date_end_event";
501 $sql .= ", location";
502 $sql .= ", email_msgid";
503 $sql .= ", note_private";
504 $sql .= ", note_public";
505 $sql .= ", entity";
506 $sql .= ", ip";
507 $sql .= ") VALUES (";
508 $sql .= "'".$this->db->escape($this->ref)."'";
509 $sql .= ", ".($this->fk_project ? ((int) $this->fk_project) : "null");
510 $sql .= ", '".$this->db->escape($this->title)."'";
511 $sql .= ", '".$this->db->escape($this->description)."'";
512 $sql .= ", ".($this->socid > 0 ? $this->socid : "null");
513 $sql .= ", ".((int) $user->id);
514 $sql .= ", ".(is_numeric($this->status) ? ((int) $this->status) : '0');
515 $sql .= ", ".((is_numeric($this->opp_status) && $this->opp_status > 0) ? ((int) $this->opp_status) : 'NULL');
516 $sql .= ", ".(is_numeric($this->opp_percent) ? ((int) $this->opp_percent) : 'NULL');
517 $sql .= ", ".($this->public ? 1 : 0);
518 $sql .= ", '".$this->db->idate($now)."'";
519 $sql .= ", ".($this->date_start != '' ? "'".$this->db->idate($this->date_start)."'" : 'null');
520 $sql .= ", ".($this->date_end != '' ? "'".$this->db->idate($this->date_end)."'" : 'null');
521 $sql .= ", ".(strcmp($this->opp_amount, '') ? price2num($this->opp_amount) : 'null');
522 $sql .= ", ".(strcmp((string) $this->budget_amount, '') ? price2num($this->budget_amount) : 'null');
523 $sql .= ", ".($this->usage_opportunity ? 1 : 0);
524 $sql .= ", ".($this->usage_task ? 1 : 0);
525 $sql .= ", ".($this->usage_bill_time ? 1 : 0);
526 $sql .= ", ".($this->usage_organize_event ? 1 : 0);
527 $sql .= ", ".($this->accept_conference_suggestions ? 1 : 0);
528 $sql .= ", ".($this->accept_booth_suggestions ? 1 : 0);
529 $sql .= ", ".(strcmp((string) $this->price_registration, '') ? price2num($this->price_registration) : 'null');
530 $sql .= ", ".(strcmp((string) $this->price_booth, '') ? price2num($this->price_booth) : 'null');
531 $sql .= ", ".(strcmp((string) $this->max_attendees, '') ? ((int) $this->max_attendees) : 'null');
532 $sql .= ", ".($this->date_start_event != '' ? "'".$this->db->idate($this->date_start_event)."'" : 'null');
533 $sql .= ", ".($this->date_end_event != '' ? "'".$this->db->idate($this->date_end_event)."'" : 'null');
534 $sql .= ", ".($this->location ? "'".$this->db->escape($this->location)."'" : 'null');
535 $sql .= ", ".($this->email_msgid ? "'".$this->db->escape($this->email_msgid)."'" : 'null');
536 $sql .= ", ".($this->note_private ? "'".$this->db->escape($this->note_private)."'" : 'null');
537 $sql .= ", ".($this->note_public ? "'".$this->db->escape($this->note_public)."'" : 'null');
538 $sql .= ", ".setEntity($this);
539 $sql .= ", ".(!isset($this->ip) ? 'NULL' : "'".$this->db->escape($this->ip)."'");
540 $sql .= ")";
541
542 dol_syslog(get_class($this)."::create", LOG_DEBUG);
543 $resql = $this->db->query($sql);
544 if ($resql) {
545 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."projet");
546 $ret = $this->id;
547
548 if (!$notrigger) {
549 // Call trigger
550 $result = $this->call_trigger('PROJECT_CREATE', $user);
551 if ($result < 0) {
552 $error++;
553 }
554 // End call triggers
555 }
556 } else {
557 $this->error = $this->db->lasterror();
558 $error++;
559 }
560
561 // Update extrafield
562 if (!$error) {
563 $result = $this->insertExtraFields();
564 if ($result < 0) {
565 $error++;
566 }
567 }
568
569 if (!$error && (getDolGlobalString('MAIN_DISABLEDRAFTSTATUS') || getDolGlobalString('MAIN_DISABLEDRAFTSTATUS_PROJECT'))) {
570 $res = $this->setValid($user);
571 if ($res < 0) {
572 $error++;
573 }
574 }
575
576 if (!$error) {
577 $this->db->commit();
578 return $ret;
579 } else {
580 $this->db->rollback();
581 return -1;
582 }
583 }
584
592 public function update($user, $notrigger = 0)
593 {
594 global $langs, $conf;
595
596 $error = 0;
597
598 // Clean parameters
599 $this->title = trim($this->title);
600 $this->description = trim($this->description);
601 if ($this->opp_amount < 0) {
602 $this->opp_amount = '';
603 }
604 if ($this->opp_percent < 0) {
605 $this->opp_percent = '';
606 }
607 if ($this->date_end && $this->date_end < $this->date_start) {
608 $this->error = $langs->trans("ErrorDateEndLowerThanDateStart");
609 $this->errors[] = $this->error;
610 $this->db->rollback();
611 dol_syslog(get_class($this)."::update error -3 ".$this->error, LOG_ERR);
612 return -3;
613 }
614
615 $this->entity = ((isset($this->entity) && is_numeric($this->entity)) ? $this->entity : $conf->entity);
616
617 if (dol_strlen(trim($this->ref)) > 0) {
618 $this->db->begin();
619
620 $sql = "UPDATE ".MAIN_DB_PREFIX."projet SET";
621 $sql .= " ref='".$this->db->escape($this->ref)."'";
622 $sql .= ", fk_project=".($this->fk_project ? ((int) $this->fk_project) : "null");
623 $sql .= ", title = '".$this->db->escape($this->title)."'";
624 $sql .= ", description = '".$this->db->escape($this->description)."'";
625 $sql .= ", fk_soc = ".($this->socid > 0 ? $this->socid : "null");
626 $sql .= ", fk_statut = ".((int) $this->status);
627 $sql .= ", fk_opp_status = ".((is_numeric($this->opp_status) && $this->opp_status > 0) ? $this->opp_status : 'null');
628 $sql .= ", opp_percent = ".((is_numeric($this->opp_percent) && $this->opp_percent != '') ? $this->opp_percent : 'null');
629 $sql .= ", public = ".($this->public ? 1 : 0);
630 $sql .= ", datec = ".($this->date_c != '' ? "'".$this->db->idate($this->date_c)."'" : 'null');
631 $sql .= ", dateo = ".($this->date_start != '' ? "'".$this->db->idate($this->date_start)."'" : 'null');
632 $sql .= ", datee = ".($this->date_end != '' ? "'".$this->db->idate($this->date_end)."'" : 'null');
633 $sql .= ", date_close = ".($this->date_close != '' ? "'".$this->db->idate($this->date_close)."'" : 'null');
634 $sql .= ", note_public = ".($this->note_public ? "'".$this->db->escape($this->note_public)."'" : "null");
635 $sql .= ", note_private = ".($this->note_private ? "'".$this->db->escape($this->note_private)."'" : "null");
636 $sql .= ", fk_user_close = ".($this->fk_user_close > 0 ? $this->fk_user_close : "null");
637 $sql .= ", opp_amount = ".(strcmp($this->opp_amount, '') ? price2num($this->opp_amount) : "null");
638 $sql .= ", budget_amount = ".(strcmp($this->budget_amount, '') ? price2num($this->budget_amount) : "null");
639 $sql .= ", fk_user_modif = ".$user->id;
640 $sql .= ", usage_opportunity = ".($this->usage_opportunity ? 1 : 0);
641 $sql .= ", usage_task = ".($this->usage_task ? 1 : 0);
642 $sql .= ", usage_bill_time = ".($this->usage_bill_time ? 1 : 0);
643 $sql .= ", usage_organize_event = ".($this->usage_organize_event ? 1 : 0);
644 $sql .= ", accept_conference_suggestions = ".($this->accept_conference_suggestions ? 1 : 0);
645 $sql .= ", accept_booth_suggestions = ".($this->accept_booth_suggestions ? 1 : 0);
646 $sql .= ", price_registration = ".(isset($this->price_registration) && strcmp($this->price_registration, '') ? price2num($this->price_registration) : "null");
647 $sql .= ", price_booth = ".(isset($this->price_booth) && strcmp((string) $this->price_booth, '') ? price2num($this->price_booth) : "null");
648 $sql .= ", max_attendees = ".(strcmp((string) $this->max_attendees, '') ? (int) $this->max_attendees : "null");
649 $sql .= ", date_start_event = ".($this->date_start_event != '' ? "'".$this->db->idate($this->date_start_event)."'" : 'null');
650 $sql .= ", date_end_event = ".($this->date_end_event != '' ? "'".$this->db->idate($this->date_end_event)."'" : 'null');
651 $sql .= ", location = '".$this->db->escape($this->location)."'";
652 $sql .= ", entity = ".((int) $this->entity);
653 $sql .= " WHERE rowid = ".((int) $this->id);
654
655 dol_syslog(get_class($this)."::update", LOG_DEBUG);
656 $resql = $this->db->query($sql);
657 if ($resql) {
658 // Update extrafield
659 if (!$error) {
660 $result = $this->insertExtraFields();
661 if ($result < 0) {
662 $error++;
663 }
664 }
665
666 if (!$error && !$notrigger) {
667 // Call trigger
668 $result = $this->call_trigger('PROJECT_MODIFY', $user);
669 if ($result < 0) {
670 $error++;
671 }
672 // End call triggers
673 }
674
675 if (!$error && (is_object($this->oldcopy) && $this->oldcopy->ref !== $this->ref)) {
676 // We remove directory
677 if ($conf->project->dir_output) {
678 $olddir = $conf->project->dir_output."/".dol_sanitizeFileName($this->oldcopy->ref);
679 $newdir = $conf->project->dir_output."/".dol_sanitizeFileName($this->ref);
680 if (file_exists($olddir)) {
681 include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
682 $res = @rename($olddir, $newdir);
683 if (!$res) {
684 $langs->load("errors");
685 $this->error = $langs->trans('ErrorFailToRenameDir', $olddir, $newdir);
686 $error++;
687 }
688 }
689 }
690 }
691 if (!$error) {
692 $this->db->commit();
693 $result = 1;
694 } else {
695 $this->db->rollback();
696 $result = -1;
697 }
698 } else {
699 $this->error = $this->db->lasterror();
700 $this->errors[] = $this->error;
701 $this->db->rollback();
702 if ($this->db->lasterrno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
703 $result = -4;
704 } else {
705 $result = -2;
706 }
707 dol_syslog(get_class($this)."::update error ".$result." ".$this->error, LOG_ERR);
708 }
709 } else {
710 dol_syslog(get_class($this)."::update ref null");
711 $result = -1;
712 }
713
714 return $result;
715 }
716
726 public function fetch($id, $ref = '', $ref_ext = '', $email_msgid = '')
727 {
728 if (empty($id) && empty($ref) && empty($ref_ext) && empty($email_msgid)) {
729 dol_syslog(get_class($this)."::fetch Bad parameters", LOG_WARNING);
730 return -1;
731 }
732
733 $sql = "SELECT rowid, entity, fk_project, ref, title, description, public, datec, opp_amount, budget_amount,";
734 $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,";
735 $sql .= " note_private, note_public, model_pdf, usage_opportunity, usage_task, usage_bill_time, usage_organize_event, email_msgid,";
736 $sql .= " accept_conference_suggestions, accept_booth_suggestions, price_registration, price_booth, max_attendees, date_start_event, date_end_event, location, extraparams";
737 $sql .= " FROM ".MAIN_DB_PREFIX."projet";
738 if (!empty($id)) {
739 $sql .= " WHERE rowid = ".((int) $id);
740 } else {
741 $sql .= " WHERE entity IN (".getEntity('project').")";
742 if (!empty($ref)) {
743 $sql .= " AND ref = '".$this->db->escape($ref)."'";
744 } elseif (!empty($ref_ext)) {
745 $sql .= " AND ref_ext = '".$this->db->escape($ref_ext)."'";
746 } else {
747 $sql .= " AND email_msgid = '".$this->db->escape($email_msgid)."'";
748 }
749 }
750
751 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
752 $resql = $this->db->query($sql);
753 if ($resql) {
754 $num_rows = $this->db->num_rows($resql);
755
756 if ($num_rows) {
757 $obj = $this->db->fetch_object($resql);
758
759 $this->id = $obj->rowid;
760 $this->entity = $obj->entity;
761 $this->ref = $obj->ref;
762 $this->fk_project = $obj->fk_project;
763 $this->title = $obj->title;
764 $this->description = $obj->description;
765 $this->date_c = $this->db->jdate($obj->datec);
766 $this->datec = $this->db->jdate($obj->datec); // TODO deprecated
767 $this->date_m = $this->db->jdate($obj->tms);
768 $this->datem = $this->db->jdate($obj->tms); // TODO deprecated
769 $this->date_start = $this->db->jdate($obj->date_start);
770 $this->date_end = $this->db->jdate($obj->date_end);
771 $this->date_close = $this->db->jdate($obj->date_close);
772 $this->note_private = $obj->note_private;
773 $this->note_public = $obj->note_public;
774 $this->socid = $obj->fk_soc;
775 $this->user_author_id = $obj->fk_user_creat;
776 $this->user_modification_id = $obj->fk_user_modif;
777 $this->user_closing_id = $obj->fk_user_close;
778 $this->public = $obj->public;
779 $this->statut = $obj->status; // deprecated
780 $this->status = $obj->status;
781 $this->opp_status = $obj->fk_opp_status;
782 $this->opp_amount = $obj->opp_amount;
783 $this->opp_percent = $obj->opp_percent;
784 $this->budget_amount = $obj->budget_amount;
785 $this->model_pdf = $obj->model_pdf;
786 $this->usage_opportunity = (int) $obj->usage_opportunity;
787 $this->usage_task = (int) $obj->usage_task;
788 $this->usage_bill_time = (int) $obj->usage_bill_time;
789 $this->usage_organize_event = (int) $obj->usage_organize_event;
790 $this->accept_conference_suggestions = (int) $obj->accept_conference_suggestions;
791 $this->accept_booth_suggestions = (int) $obj->accept_booth_suggestions;
792 $this->price_registration = $obj->price_registration;
793 $this->price_booth = $obj->price_booth;
794 $this->max_attendees = $obj->max_attendees;
795 $this->date_start_event = $this->db->jdate($obj->date_start_event);
796 $this->date_end_event = $this->db->jdate($obj->date_end_event);
797 $this->location = $obj->location;
798 $this->email_msgid = $obj->email_msgid;
799 $this->extraparams = !empty($obj->extraparams) ? (array) json_decode($obj->extraparams, true) : array();
800
801 $this->db->free($resql);
802
803 // Retrieve all extrafield
804 // fetch optionals attributes and labels
805 $this->fetch_optionals();
806
807 return 1;
808 }
809
810 $this->db->free($resql);
811
812 return 0;
813 } else {
814 $this->error = $this->db->lasterror();
815 $this->errors[] = $this->db->lasterror();
816 return -1;
817 }
818 }
819
828 public function fetchAndSetSubstitution($id, $key, $fetched = false)
829 {
830 $substitution = '';
831
832 if ($fetched === false) {
833 $res = $this->fetch($id);
834 if ($res > 0) {
835 $fetched = true;
836 }
837 }
838
839 if ($fetched === true) {
840 if ($key == '__PROJECT_ID__') {
841 $substitution = ($this->id > 0 ? $this->id : '');
842 } elseif ($key == '__PROJECT_REF__') {
843 $substitution = $this->ref;
844 } elseif ($key == '__PROJECT_NAME__') {
845 $substitution = $this->title;
846 }
847 }
848
849 return $substitution;
850 }
851
852 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
864 public function get_element_list($type, $tablename, $datefieldname = '', $date_start = null, $date_end = null, $projectkey = 'fk_projet')
865 {
866 // phpcs:enable
867
868 global $hookmanager;
869
870 $elements = array();
871
872 if ($this->id <= 0) {
873 return $elements;
874 }
875
876 $ids = $this->id;
877
878 if ($type == 'agenda') {
879 $sql = "SELECT id as rowid FROM ".MAIN_DB_PREFIX."actioncomm WHERE fk_project IN (".$this->db->sanitize($ids).") AND entity IN (".getEntity('agenda').")";
880 } elseif ($type == 'expensereport') {
881 $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).")";
882 } elseif ($type == 'project_task') {
883 $sql = "SELECT DISTINCT pt.rowid FROM ".MAIN_DB_PREFIX."projet_task as pt WHERE pt.fk_projet IN (".$this->db->sanitize($ids).")";
884 } elseif ($type == 'element_time') { // Case we want to duplicate line foreach user
885 $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).")";
886 } elseif ($type == 'stocktransfer_stocktransfer') {
887 $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";
888 } elseif ($type == 'loan') {
889 $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).")";
890 } else {
891 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX.$tablename." WHERE ".$projectkey." IN (".$this->db->sanitize($ids).") AND entity IN (".getEntity($type).")";
892 }
893
894 if (isDolTms($date_start) && $type == 'loan') {
895 $sql .= " AND (dateend > '".$this->db->idate($date_start)."' OR dateend IS NULL)";
896 } elseif (isDolTms($date_start) && ($type != 'project_task')) { // For table project_taks, we want the filter on date apply on project_time_spent table
897 if (empty($datefieldname) && !empty($this->table_element_date)) {
898 $datefieldname = $this->table_element_date;
899 }
900 if (empty($datefieldname)) {
901 return 'Error this object has no date field defined';
902 }
903 $sql .= " AND (".$datefieldname." >= '".$this->db->idate($date_start)."' OR ".$datefieldname." IS NULL)";
904 }
905
906 if (isDolTms($date_end) && $type == 'loan') {
907 $sql .= " AND (datestart < '".$this->db->idate($date_end)."' OR datestart IS NULL)";
908 } elseif (isDolTms($date_end) && ($type != 'project_task')) { // For table project_taks, we want the filter on date apply on project_time_spent table
909 if (empty($datefieldname) && !empty($this->table_element_date)) {
910 $datefieldname = $this->table_element_date;
911 }
912 if (empty($datefieldname)) {
913 return 'Error this object has no date field defined';
914 }
915 $sql .= " AND (".$datefieldname." <= '".$this->db->idate($date_end)."' OR ".$datefieldname." IS NULL)";
916 }
917
918 $parameters = array(
919 'sql' => $sql,
920 'type' => $type,
921 'tablename' => $tablename,
922 'datefieldname' => $datefieldname,
923 'dates' => $date_start,
924 'datee' => $date_end,
925 'fk_projet' => $projectkey,
926 'ids' => $ids,
927 );
928 $reshook = $hookmanager->executeHooks('getElementList', $parameters);
929 if ($reshook > 0) {
930 $sql = $hookmanager->resPrint;
931 } else {
932 $sql .= $hookmanager->resPrint;
933 }
934
935 if (!$sql) {
936 return -1;
937 }
938
939 //print $sql;
940 dol_syslog(get_class($this)."::get_element_list", LOG_DEBUG);
941 $result = $this->db->query($sql);
942 if ($result) {
943 $nump = $this->db->num_rows($result);
944 if ($nump) {
945 $i = 0;
946 while ($i < $nump) {
947 $obj = $this->db->fetch_object($result);
948
949 $elements[$i] = $obj->rowid.(empty($obj->fk_user) ? '' : '_'.$obj->fk_user);
950
951 $i++;
952 }
953 $this->db->free($result);
954 }
955
956 /* Return array even if empty*/
957 return $elements;
958 } else {
959 dol_print_error($this->db);
960 }
961 return -1;
962 }
963
971 public function delete($user, $notrigger = 0)
972 {
973 global $langs, $conf;
974 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
975
976 $error = 0;
977
978 $this->db->begin();
979
980 if (!$error) {
981 // Delete linked contacts
982 $res = $this->delete_linked_contact();
983 if ($res < 0) {
984 $this->error = 'ErrorFailToDeleteLinkedContact';
985 //$error++;
986 $this->db->rollback();
987 return 0;
988 }
989 }
990
991 // Set fk_projet into elements to null
992 $listoftables = array(
993 'propal' => 'fk_projet',
994 'commande' => 'fk_projet',
995 'facture' => 'fk_projet',
996 'supplier_proposal' => 'fk_projet',
997 'commande_fournisseur' => 'fk_projet',
998 'facture_fourn' => 'fk_projet',
999 'expensereport_det' => 'fk_projet',
1000 'contrat' => 'fk_projet',
1001 'fichinter' => 'fk_projet',
1002 'don' => array('field' => 'fk_projet', 'module' => 'don'),
1003 'actioncomm' => 'fk_project',
1004 'mrp_mo' => 'fk_project',
1005 'entrepot' => 'fk_project',
1006 );
1007 foreach ($listoftables as $key => $value) {
1008 if (is_array($value)) {
1009 if (!isModEnabled($value['module'])) {
1010 continue;
1011 }
1012 $fieldname = $value['field'];
1013 } else {
1014 $fieldname = $value;
1015 }
1016 $sql = "UPDATE ".MAIN_DB_PREFIX.$key." SET ".$fieldname." = NULL where ".$fieldname." = ".((int) $this->id);
1017
1018 $resql = $this->db->query($sql);
1019 if (!$resql) {
1020 $this->errors[] = $this->db->lasterror();
1021 $error++;
1022 break;
1023 }
1024 }
1025
1026 // Remove linked categories.
1027 if (!$error) {
1028 $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_project";
1029 $sql .= " WHERE fk_project = ".((int) $this->id);
1030
1031 $result = $this->db->query($sql);
1032 if (!$result) {
1033 $error++;
1034 $this->errors[] = $this->db->lasterror();
1035 }
1036 }
1037
1038 // Fetch tasks
1039 $this->getLinesArray($user, 0);
1040
1041 // Delete tasks
1042 $ret = $this->deleteTasks($user);
1043 if ($ret < 0) {
1044 $error++;
1045 }
1046
1047
1048 // Delete all child tables
1049 if (!$error) {
1050 $elements = array('categorie_project'); // elements to delete. TODO Make goodway to delete
1051 foreach ($elements as $table) {
1052 if (!$error) {
1053 $sql = "DELETE FROM ".MAIN_DB_PREFIX.$table;
1054 $sql .= " WHERE fk_project = ".((int) $this->id);
1055
1056 $result = $this->db->query($sql);
1057 if (!$result) {
1058 $error++;
1059 $this->errors[] = $this->db->lasterror();
1060 }
1061 }
1062 }
1063 }
1064
1065 if (!$error) {
1066 $sql = "DELETE FROM ".MAIN_DB_PREFIX."projet_extrafields";
1067 $sql .= " WHERE fk_object = ".((int) $this->id);
1068
1069 $resql = $this->db->query($sql);
1070 if (!$resql) {
1071 $this->errors[] = $this->db->lasterror();
1072 $error++;
1073 }
1074 }
1075
1076 // Delete project
1077 if (!$error) {
1078 $sql = "DELETE FROM ".MAIN_DB_PREFIX."projet";
1079 $sql .= " WHERE rowid=".((int) $this->id);
1080
1081 $resql = $this->db->query($sql);
1082 if (!$resql) {
1083 $this->errors[] = $langs->trans("CantRemoveProject", $langs->transnoentitiesnoconv("ProjectOverview"));
1084 $error++;
1085 }
1086 }
1087
1088
1089
1090 if (empty($error)) {
1091 // We remove directory
1092 $projectref = dol_sanitizeFileName($this->ref);
1093 if ($conf->project->dir_output) {
1094 $dir = $conf->project->dir_output."/".$projectref;
1095 if (file_exists($dir)) {
1096 $res = @dol_delete_dir_recursive($dir);
1097 if (!$res) {
1098 $this->errors[] = 'ErrorFailToDeleteDir';
1099 $error++;
1100 }
1101 }
1102 }
1103
1104 if (!$notrigger) {
1105 // Call trigger
1106 $result = $this->call_trigger('PROJECT_DELETE', $user);
1107
1108 if ($result < 0) {
1109 $error++;
1110 }
1111 // End call triggers
1112 }
1113 }
1114
1115 if (empty($error)) {
1116 $this->db->commit();
1117 return 1;
1118 } else {
1119 foreach ($this->errors as $errmsg) {
1120 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
1121 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
1122 }
1123 dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR);
1124 $this->db->rollback();
1125 return -1;
1126 }
1127 }
1128
1137 public function getElementCount($type, $tablename, $projectkey = 'fk_projet')
1138 {
1139 if ($this->id <= 0) {
1140 return 0;
1141 }
1142
1143 if ($type == 'agenda') {
1144 $sql = "SELECT COUNT(id) as nb FROM ".MAIN_DB_PREFIX."actioncomm WHERE fk_project = ".((int) $this->id)." AND entity IN (".getEntity('agenda').")";
1145 } elseif ($type == 'expensereport') {
1146 $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);
1147 } elseif ($type == 'project_task') {
1148 $sql = "SELECT DISTINCT COUNT(pt.rowid) as nb FROM ".MAIN_DB_PREFIX."projet_task as pt WHERE pt.fk_projet = ".((int) $this->id);
1149 } elseif ($type == 'element_time') { // Case we want to duplicate line foreach user
1150 $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);
1151 } elseif ($type == 'stock_mouvement') {
1152 $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";
1153 } elseif ($type == 'loan') {
1154 $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);
1155 } else {
1156 $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX.$tablename." WHERE ".$projectkey." = ".((int) $this->id)." AND entity IN (".getEntity($type).")";
1157 }
1158
1159 $result = $this->db->query($sql);
1160
1161 if (!$result) {
1162 return 0;
1163 }
1164
1165 $obj = $this->db->fetch_object($result);
1166
1167 $this->db->free($result);
1168
1169 return $obj->nb;
1170 }
1171
1178 public function deleteTasks($user)
1179 {
1180 $countTasks = count($this->lines);
1181 $deleted = false;
1182 if ($countTasks) {
1183 foreach ($this->lines as $task) {
1184 if ($task->hasChildren() <= 0) { // If there is no children (or error to detect them)
1185 $deleted = true;
1186 $ret = $task->delete($user);
1187 if ($ret <= 0) {
1188 $this->errors[] = $this->db->lasterror();
1189 return -1;
1190 }
1191 }
1192 }
1193 }
1194 $this->getLinesArray($user);
1195 if ($deleted && count($this->lines) < $countTasks) {
1196 if (count($this->lines)) {
1197 $this->deleteTasks($user);
1198 }
1199 }
1200
1201 return 1;
1202 }
1203
1211 public function setValid($user, $notrigger = 0)
1212 {
1213 global $langs;
1214
1215 $error = 0;
1216
1217 // Protection
1218 if ($this->status == self::STATUS_VALIDATED) {
1219 dol_syslog(get_class($this)."::validate action abandoned: already validated", LOG_WARNING);
1220 return 0;
1221 }
1222
1223 // Check parameters
1224 if (preg_match('/^'.preg_quote($langs->trans("CopyOf").' ').'/', $this->title)) {
1225 $this->error = $langs->trans("ErrorFieldFormat", $langs->transnoentities("Label")).'. '.$langs->trans('RemoveString', $langs->transnoentitiesnoconv("CopyOf"));
1226 return -1;
1227 }
1228
1229 $this->db->begin();
1230
1231 $sql = "UPDATE ".MAIN_DB_PREFIX."projet";
1232 $sql .= " SET fk_statut = ".self::STATUS_VALIDATED;
1233 $sql .= " WHERE rowid = ".((int) $this->id);
1234 //$sql .= " AND entity = ".((int) $conf->entity); // Disabled, when we use the ID for the where, we must not add any other search condition
1235
1236 dol_syslog(get_class($this)."::setValid", LOG_DEBUG);
1237 $resql = $this->db->query($sql);
1238 if ($resql) {
1239 // Call trigger
1240 if (empty($notrigger)) {
1241 $result = $this->call_trigger('PROJECT_VALIDATE', $user);
1242 if ($result < 0) {
1243 $error++;
1244 }
1245 // End call triggers
1246 }
1247
1248 if (!$error) {
1249 $this->statut = 1;
1250 $this->status = 1;
1251 $this->db->commit();
1252 return 1;
1253 } else {
1254 $this->db->rollback();
1255 $this->error = implode(',', $this->errors);
1256 dol_syslog(get_class($this)."::setValid ".$this->error, LOG_ERR);
1257 return -1;
1258 }
1259 } else {
1260 $this->db->rollback();
1261 $this->error = $this->db->lasterror();
1262 return -1;
1263 }
1264 }
1265
1272 public function setClose($user)
1273 {
1274 $now = dol_now();
1275
1276 $error = 0;
1277
1278 if ($this->status != self::STATUS_CLOSED) {
1279 $this->db->begin();
1280
1281 $sql = "UPDATE ".MAIN_DB_PREFIX."projet";
1282 $sql .= " SET fk_statut = ".self::STATUS_CLOSED.", fk_user_close = ".((int) $user->id).", date_close = '".$this->db->idate($now)."'";
1283 $sql .= " WHERE rowid = ".((int) $this->id);
1284 $sql .= " AND fk_statut = ".self::STATUS_VALIDATED;
1285
1286 if (getDolGlobalString('PROJECT_USE_OPPORTUNITIES')) {
1287 // TODO What to do if fk_opp_status is not code 'WON' or 'LOST'
1288 }
1289
1290 dol_syslog(get_class($this)."::setClose", LOG_DEBUG);
1291 $resql = $this->db->query($sql);
1292 if ($resql) {
1293 // Call trigger
1294 $result = $this->call_trigger('PROJECT_CLOSE', $user);
1295 if ($result < 0) {
1296 $error++;
1297 }
1298 // End call triggers
1299
1300 if (!$error) {
1301 $this->status = 2;
1302 $this->db->commit();
1303 return 1;
1304 } else {
1305 $this->db->rollback();
1306 $this->error = implode(',', $this->errors);
1307 dol_syslog(get_class($this)."::setClose ".$this->error, LOG_ERR);
1308 return -1;
1309 }
1310 } else {
1311 $this->db->rollback();
1312 $this->error = $this->db->lasterror();
1313 return -1;
1314 }
1315 }
1316
1317 return 0;
1318 }
1319
1326 public function getLibStatut($mode = 0)
1327 {
1328 return $this->LibStatut(isset($this->status) ? $this->status : $this->statut, $mode);
1329 }
1330
1331 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1339 public function LibStatut($status, $mode = 0)
1340 {
1341 // phpcs:enable
1342 global $langs;
1343
1344 if (is_null($status)) {
1345 return '';
1346 }
1347
1348 $statustrans = array(
1349 0 => 'status0',
1350 1 => 'status4',
1351 2 => 'status6',
1352 );
1353
1354 $statusClass = 'status0';
1355 if (!empty($statustrans[$status])) {
1356 $statusClass = $statustrans[$status];
1357 }
1358
1359 return dolGetStatus($langs->transnoentitiesnoconv($this->labelStatus[$status]), $langs->transnoentitiesnoconv($this->labelStatusShort[$status]), '', $statusClass, $mode);
1360 }
1361
1369 public function getTooltipContentArray($params)
1370 {
1371 global $conf, $langs;
1372
1373 $langs->load('projects');
1374 $option = $params['option'] ?? '';
1375 $moreinpopup = $params['moreinpopup'] ?? '';
1376
1377 $datas = [];
1378 if ($option != 'nolink') {
1379 $datas['picto'] = img_picto('', $this->picto, 'class="pictofixedwidth"').' <u class="paddingrightonly">'.$langs->trans("Project").'</u>';
1380 }
1381 if (isset($this->status)) {
1382 $datas['picto'] .= ' '.$this->getLibStatut(5);
1383 }
1384 $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
1385 $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
1386 if (isset($this->public)) {
1387 $datas['visibility'] = '<br><b>'.$langs->trans("Visibility").":</b> ";
1388 $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"));
1389 }
1390 if (!empty($this->thirdparty_name)) {
1391 $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
1392 }
1393 if (!empty($this->date_start)) {
1394 $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
1395 }
1396 if (!empty($this->date_end)) {
1397 $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
1398 }
1399 if ($moreinpopup) {
1400 $datas['moreinpopup'] = '<br>'.$moreinpopup;
1401 }
1402
1403 return $datas;
1404 }
1405
1420 public function getNomUrl($withpicto = 0, $option = '', $addlabel = 0, $moreinpopup = '', $sep = ' - ', $notooltip = 0, $save_lastsearch_value = -1, $morecss = '', $save_pageforbacktolist = '')
1421 {
1422 global $conf, $langs, $user, $hookmanager;
1423
1424 if (!empty($conf->dol_no_mouse_hover)) {
1425 $notooltip = 1; // Force disable tooltips
1426 }
1427
1428 $result = '';
1429 if (getDolGlobalString('PROJECT_OPEN_ALWAYS_ON_TAB')) {
1430 $option = getDolGlobalString('PROJECT_OPEN_ALWAYS_ON_TAB');
1431 }
1432 $params = [
1433 'id' => $this->id,
1434 'objecttype' => $this->element,
1435 'moreinpopup' => $moreinpopup,
1436 'option' => $option,
1437 ];
1438 $classfortooltip = 'classfortooltip';
1439 $dataparams = '';
1440 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
1441 $classfortooltip = 'classforajaxtooltip';
1442 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
1443 $label = '';
1444 } else {
1445 $label = implode($this->getTooltipContentArray($params));
1446 }
1447
1448 $url = '';
1449 if ($option != 'nolink') {
1450 if (preg_match('/\.php$/', $option)) {
1451 $url = dol_buildpath($option, 1).'?id='.$this->id;
1452 } elseif ($option == 'task') {
1453 $url = DOL_URL_ROOT.'/projet/tasks.php?id='.$this->id;
1454 } elseif ($option == 'preview') {
1455 $url = DOL_URL_ROOT.'/projet/element.php?id='.$this->id;
1456 } elseif ($option == 'eventorganization') {
1457 $url = DOL_URL_ROOT.'/eventorganization/conferenceorbooth_list.php?projectid='.$this->id;
1458 } else {
1459 $url = DOL_URL_ROOT.'/projet/card.php?id='.$this->id;
1460 }
1461 // Add param to save lastsearch_values or not
1462 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1463 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1464 $add_save_lastsearch_values = 1;
1465 }
1466 if ($add_save_lastsearch_values) {
1467 $url .= '&save_lastsearch_values=1';
1468 }
1469 $add_save_backpagefor = ($save_pageforbacktolist ? 1 : 0);
1470 if ($add_save_backpagefor) {
1471 $url .= "&save_pageforbacktolist=".urlencode($save_pageforbacktolist);
1472 }
1473 }
1474
1475 $linkclose = '';
1476 if (empty($notooltip) && $user->hasRight('projet', 'lire')) {
1477 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1478 $label = $langs->trans("ShowProject");
1479 $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
1480 }
1481 $linkclose .= ($label ? ' title="'.dol_escape_htmltag($label, 1).'"' : ' title="tocomplete"');
1482 $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
1483 } else {
1484 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1485 }
1486
1487 $picto = 'projectpub';
1488 if (!$this->public) {
1489 $picto = 'project';
1490 }
1491
1492 $linkstart = '<a href="'.$url.'"';
1493 $linkstart .= $linkclose.'>';
1494 $linkend = '</a>';
1495
1496 $result .= $linkstart;
1497 if ($withpicto) {
1498 $result .= img_object(($notooltip ? '' : $label), $picto, 'class="pictofixedwidth em088"', 0, 0, $notooltip ? 0 : 1);
1499 }
1500 if ($withpicto != 2) {
1501 $result .= $this->ref;
1502 }
1503 $result .= $linkend;
1504 if ($withpicto != 2) {
1505 $result .= (($addlabel && $this->title) ? '<span class="opacitymedium">'.$sep.dol_trunc($this->title, ($addlabel > 1 ? $addlabel : 0)).'</span>' : '');
1506 }
1507
1508 global $action;
1509 $hookmanager->initHooks(array('projectdao'));
1510 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
1511 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1512 if ($reshook > 0) {
1513 $result = $hookmanager->resPrint;
1514 } else {
1515 $result .= $hookmanager->resPrint;
1516 }
1517
1518 return $result;
1519 }
1520
1528 public function initAsSpecimen()
1529 {
1530 global $user, $langs, $conf;
1531
1532 $now = dol_now();
1533
1534 // Initialise parameters
1535 $this->id = 0;
1536 $this->ref = 'SPECIMEN';
1537 $this->entity = $conf->entity;
1538 $this->specimen = 1;
1539 $this->socid = 1;
1540 $this->date_c = $now;
1541 $this->date_m = $now;
1542 $this->date_start = $now;
1543 $this->date_end = $now + (3600 * 24 * 365);
1544 $this->note_public = 'SPECIMEN';
1545 $this->note_private = 'Private Note';
1546 $this->fk_project = 0;
1547 $this->opp_amount = 20000;
1548 $this->budget_amount = 10000;
1549
1550 $this->usage_opportunity = 1;
1551 $this->usage_task = 1;
1552 $this->usage_bill_time = 1;
1553 $this->usage_organize_event = 1;
1554
1555 /*
1556 $nbp = mt_rand(1, 9);
1557 $xnbp = 0;
1558 while ($xnbp < $nbp)
1559 {
1560 $line = new Task($this->db);
1561 $line->fk_project = 0;
1562 $line->label = $langs->trans("Label") . " " . $xnbp;
1563 $line->description = $langs->trans("Description") . " " . $xnbp;
1564
1565 $this->lines[]=$line;
1566 $xnbp++;
1567 }
1568 */
1569
1570 return 1;
1571 }
1572
1580 public function restrictedProjectArea(User $user, $mode = 'read')
1581 {
1582 // To verify role of users
1583 $userAccess = 0;
1584 if (($mode == 'read' && $user->hasRight('projet', 'all', 'lire')) || ($mode == 'write' && $user->hasRight('projet', 'all', 'creer')) || ($mode == 'delete' && $user->hasRight('projet', 'all', 'supprimer'))) {
1585 $userAccess = 1;
1586 } elseif ($this->public && (($mode == 'read' && $user->hasRight('projet', 'lire')) || ($mode == 'write' && $user->hasRight('projet', 'creer')) || ($mode == 'delete' && $user->hasRight('projet', 'supprimer')))) {
1587 $userAccess = 1;
1588 } else { // No access due to permission to read all projects, so we check if we are a contact of project
1589 foreach (array('internal', 'external') as $source) {
1590 $userRole = $this->liste_contact(4, $source);
1591 $num = count($userRole);
1592
1593 $nblinks = 0;
1594 while ($nblinks < $num) {
1595 if ($source == 'internal' && $user->id == $userRole[$nblinks]['id']) { // $userRole[$nblinks]['id'] is id of user (llx_user) for internal contacts
1596 if ($mode == 'read' && $user->hasRight('projet', 'lire')) {
1597 $userAccess++;
1598 }
1599 if ($mode == 'write' && $user->hasRight('projet', 'creer')) {
1600 $userAccess++;
1601 }
1602 if ($mode == 'delete' && $user->hasRight('projet', 'supprimer')) {
1603 $userAccess++;
1604 }
1605 }
1606 if ($source == 'external' && $user->socid > 0 && $user->socid == $userRole[$nblinks]['socid']) { // $userRole[$nblinks]['id'] is id of contact (llx_socpeople) or external contacts
1607 if ($mode == 'read' && $user->hasRight('projet', 'lire')) {
1608 $userAccess++;
1609 }
1610 if ($mode == 'write' && $user->hasRight('projet', 'creer')) {
1611 $userAccess++;
1612 }
1613 if ($mode == 'delete' && $user->hasRight('projet', 'supprimer')) {
1614 $userAccess++;
1615 }
1616 }
1617 $nblinks++;
1618 }
1619 }
1620 //if (empty($nblinks)) // If nobody has permission, we grant creator
1621 //{
1622 // if ((!empty($this->user_author_id) && $this->user_author_id == $user->id))
1623 // {
1624 // $userAccess = 1;
1625 // }
1626 //}
1627 }
1628
1629 return ($userAccess ? $userAccess : -1);
1630 }
1631
1642 public function getProjectsAuthorizedForUser($user, $mode = 0, $list = 0, $socid = 0, $filter = '')
1643 {
1644 $projects = array();
1645 $temp = array();
1646
1647 $sql = "SELECT ".(($mode == 0 || $mode == 1) ? "DISTINCT " : "")."p.rowid, p.ref";
1648 $sql .= " FROM ".MAIN_DB_PREFIX."projet as p";
1649 if ($mode == 0) {
1650 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."element_contact as ec ON ec.element_id = p.rowid";
1651 } elseif ($mode == 1) {
1652 $sql .= ", ".MAIN_DB_PREFIX."element_contact as ec";
1653 } elseif ($mode == 2) {
1654 // No filter. Use this if user has permission to see all project
1655 }
1656 $sql .= " WHERE p.entity IN (".getEntity('project').")";
1657 // Internal users must see project he is contact to even if project linked to a third party he can't see.
1658 //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).")";
1659 if ($socid > 0) {
1660 $sql .= " AND (p.fk_soc IS NULL OR p.fk_soc = 0 OR p.fk_soc = ".((int) $socid).")";
1661 }
1662
1663 // Get id of types of contacts for projects (This list never contains a lot of elements)
1664 $listofprojectcontacttype = array();
1665 $sql2 = "SELECT ctc.rowid, ctc.code FROM ".MAIN_DB_PREFIX."c_type_contact as ctc";
1666 $sql2 .= " WHERE ctc.element = '".$this->db->escape($this->element)."'";
1667 $sql2 .= " AND ctc.source = 'internal'";
1668 $resql = $this->db->query($sql2);
1669 if ($resql) {
1670 while ($obj = $this->db->fetch_object($resql)) {
1671 $listofprojectcontacttype[$obj->rowid] = $obj->code;
1672 }
1673 } else {
1674 dol_print_error($this->db);
1675 }
1676 if (count($listofprojectcontacttype) == 0) {
1677 $listofprojectcontacttype[0] = '0'; // To avoid syntax error if not found
1678 }
1679
1680 if ($mode == 0) {
1681 $sql .= " AND ( p.public = 1";
1682 $sql .= " OR ( ec.fk_c_type_contact IN (".$this->db->sanitize(implode(',', array_keys($listofprojectcontacttype))).")";
1683 $sql .= " AND ec.fk_socpeople = ".((int) $user->id).")";
1684 $sql .= " )";
1685 } elseif ($mode == 1) {
1686 $sql .= " AND ec.element_id = p.rowid";
1687 $sql .= " AND (";
1688 $sql .= " ( ec.fk_c_type_contact IN (".$this->db->sanitize(implode(',', array_keys($listofprojectcontacttype))).")";
1689 $sql .= " AND ec.fk_socpeople = ".((int) $user->id).")";
1690 $sql .= " )";
1691 } elseif ($mode == 2) {
1692 // No filter. Use this if user has permission to see all project
1693 }
1694
1695 $sql .= $filter;
1696 //print $sql;
1697
1698 $resql = $this->db->query($sql);
1699 if ($resql) {
1700 $num = $this->db->num_rows($resql);
1701 $i = 0;
1702 while ($i < $num) {
1703 $row = $this->db->fetch_row($resql);
1704 $projects[$row[0]] = $row[1];
1705 $temp[] = $row[0];
1706 $i++;
1707 }
1708
1709 $this->db->free($resql);
1710
1711 if ($list) {
1712 if (empty($temp)) {
1713 return '0';
1714 }
1715 $result = implode(',', $temp);
1716 return $result;
1717 }
1718 } else {
1719 dol_print_error($this->db);
1720 }
1721
1722 return $projects;
1723 }
1724
1740 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)
1741 {
1742 global $langs, $conf;
1743
1744 $error = 0;
1745 $clone_project_id = 0; // For static toolcheck
1746
1747 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);
1748
1749 $now = dol_mktime(0, 0, 0, idate('m', dol_now()), idate('d', dol_now()), idate('Y', dol_now()));
1750
1751 $clone_project = new Project($this->db);
1752
1753 $clone_project->context['createfromclone'] = 'createfromclone';
1754
1755 $this->db->begin();
1756
1757 // Load source object
1758 $clone_project->fetch($fromid);
1759 $clone_project->fetch_optionals();
1760 if ($newthirdpartyid > 0) {
1761 $clone_project->socid = $newthirdpartyid;
1762 }
1763 $clone_project->fetch_thirdparty();
1764
1765 $orign_dt_start = $clone_project->date_start;
1766 $orign_project_ref = $clone_project->ref;
1767
1768 $clone_project->id = 0;
1769 if ($move_date) {
1770 $clone_project->date_start = $now;
1771 if (!(empty($clone_project->date_end))) {
1772 $clone_project->date_end += ($now - $orign_dt_start);
1773 }
1774 }
1775
1776 $clone_project->date_c = $now;
1777
1778 if (!$clone_note) {
1779 $clone_project->note_private = '';
1780 $clone_project->note_public = '';
1781 }
1782
1783 //Generate next ref
1784 $defaultref = '';
1785 $obj = getDolGlobalString('PROJECT_ADDON', 'mod_project_simple');
1786 // Search template files
1787 $file = '';
1788 $classname = '';
1789 $filefound = 0;
1790 $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
1791 foreach ($dirmodels as $reldir) {
1792 $file = dol_buildpath($reldir."core/modules/project/".$obj.'.php', 0);
1793 if (file_exists($file)) {
1794 $filefound = 1;
1795 dol_include_once($reldir."core/modules/project/".$obj.'.php');
1796 $modProject = new $obj();
1797 $defaultref = $modProject->getNextValue(is_object($clone_project->thirdparty) ? $clone_project->thirdparty : null, $clone_project);
1798 break;
1799 }
1800 }
1801 if (is_numeric($defaultref) && $defaultref <= 0) {
1802 $defaultref = '';
1803 }
1804
1805 $clone_project->ref = $defaultref;
1806 $clone_project->title = $langs->trans("CopyOf").' '.$clone_project->title;
1807
1808 // Create clone
1809 $result = $clone_project->create($user, $notrigger);
1810
1811 // Other options
1812 if ($result < 0) {
1813 $this->error .= $clone_project->error;
1814 $error++;
1815 }
1816
1817 if (!$error) {
1818 //Get the new project id
1819 $clone_project_id = $clone_project->id;
1820
1821 //Note Update
1822 if (!$clone_note) {
1823 $clone_project->note_private = '';
1824 $clone_project->note_public = '';
1825 } else {
1826 $this->db->begin();
1827 $res = $clone_project->update_note(dol_html_entity_decode($clone_project->note_public, ENT_QUOTES | ENT_HTML5), '_public');
1828 if ($res < 0) {
1829 $this->error .= $clone_project->error;
1830 $error++;
1831 $this->db->rollback();
1832 } else {
1833 $this->db->commit();
1834 }
1835
1836 $this->db->begin();
1837 $res = $clone_project->update_note(dol_html_entity_decode($clone_project->note_private, ENT_QUOTES | ENT_HTML5), '_private');
1838 if ($res < 0) {
1839 $this->error .= $clone_project->error;
1840 $error++;
1841 $this->db->rollback();
1842 } else {
1843 $this->db->commit();
1844 }
1845 }
1846
1847 //Duplicate contact
1848 if ($clone_contact) {
1849 $origin_project = new Project($this->db);
1850 $origin_project->fetch($fromid);
1851
1852 foreach (array('internal', 'external') as $source) {
1853 $tab = $origin_project->liste_contact(-1, $source);
1854 if (is_array($tab) && count($tab) > 0) {
1855 foreach ($tab as $contacttoadd) {
1856 $clone_project->add_contact($contacttoadd['id'], $contacttoadd['code'], $contacttoadd['source'], $notrigger);
1857 if ($clone_project->error == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
1858 $langs->load("errors");
1859 $this->error .= $langs->trans("ErrorThisContactIsAlreadyDefinedAsThisType");
1860 $error++;
1861 } else {
1862 if ($clone_project->error != '') {
1863 $this->error .= $clone_project->error;
1864 $error++;
1865 }
1866 }
1867 }
1868 } elseif ($tab < 0) {
1869 $this->error .= $origin_project->error;
1870 $error++;
1871 }
1872 }
1873 }
1874
1875 //Duplicate file
1876 if ($clone_project_file) {
1877 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1878
1879 $clone_project_dir = $conf->project->dir_output."/".dol_sanitizeFileName($defaultref);
1880 $ori_project_dir = $conf->project->dir_output."/".dol_sanitizeFileName($orign_project_ref);
1881
1882 if (dol_mkdir($clone_project_dir) >= 0) {
1883 $filearray = dol_dir_list($ori_project_dir, "files", 0, '', '(\.meta|_preview.*\.png)$', '', SORT_ASC, 1);
1884 foreach ($filearray as $key => $file) {
1885 $rescopy = dol_copy($ori_project_dir.'/'.$file['name'], $clone_project_dir.'/'.$file['name'], 0, 1);
1886 if (is_numeric($rescopy) && $rescopy < 0) {
1887 $this->error .= $langs->trans("ErrorFailToCopyFile", $ori_project_dir.'/'.$file['name'], $clone_project_dir.'/'.$file['name']);
1888 $error++;
1889 }
1890 }
1891 } else {
1892 $this->error .= $langs->trans('ErrorInternalErrorDetected').':dol_mkdir';
1893 $error++;
1894 }
1895 }
1896
1897 //Duplicate task
1898 if ($clone_task) {
1899 require_once DOL_DOCUMENT_ROOT.'/projet/class/task.class.php';
1900
1901 $taskstatic = new Task($this->db);
1902
1903 // Security check
1904 $socid = 0;
1905 if ($user->socid > 0) {
1906 $socid = $user->socid;
1907 }
1908
1909 $tasksarray = $taskstatic->getTasksArray(0, 0, $fromid, $socid, 0);
1910
1911 $tab_conv_child_parent = array();
1912
1913 // Loop on each task, to clone it
1914 foreach ($tasksarray as $tasktoclone) {
1915 $result_clone = $taskstatic->createFromClone($user, $tasktoclone->id, $clone_project_id, $tasktoclone->fk_task_parent, $move_date, true, false, $clone_task_file, true, false);
1916 if ($result_clone <= 0) {
1917 $this->error .= $taskstatic->error;
1918 $error++;
1919 } else {
1920 $new_task_id = $result_clone;
1921 $taskstatic->fetch($tasktoclone->id);
1922
1923 //manage new parent clone task id
1924 // if the current task has child we store the original task id and the equivalent clone task id
1925 if (($taskstatic->hasChildren()) && !array_key_exists($tasktoclone->id, $tab_conv_child_parent)) {
1926 $tab_conv_child_parent[$tasktoclone->id] = $new_task_id;
1927 }
1928 }
1929 }
1930
1931 //Parse all clone node to be sure to update new parent
1932 $tasksarray = $taskstatic->getTasksArray(0, 0, $clone_project_id, $socid, 0);
1933 foreach ($tasksarray as $task_cloned) {
1934 $taskstatic->fetch($task_cloned->id);
1935 if ($taskstatic->fk_task_parent != 0) {
1936 $taskstatic->fk_task_parent = $tab_conv_child_parent[$taskstatic->fk_task_parent];
1937 }
1938 $res = $taskstatic->update($user, $notrigger);
1939 if ($result_clone <= 0) {
1940 $this->error .= $taskstatic->error;
1941 $error++;
1942 }
1943 }
1944 }
1945 }
1946
1947 unset($clone_project->context['createfromclone']);
1948
1949 if (!$error && $clone_project_id != 0) {
1950 $this->db->commit();
1951 return $clone_project_id;
1952 } else {
1953 $this->db->rollback();
1954 dol_syslog(get_class($this)."::createFromClone nbError: ".$error." error : ".$this->error, LOG_ERR);
1955 return -1;
1956 }
1957 }
1958
1959
1966 public function shiftTaskDate($old_project_dt_start)
1967 {
1968 global $user, $langs, $conf;
1969
1970 $error = 0;
1971 $result = 0;
1972
1973 $taskstatic = new Task($this->db);
1974
1975 // Security check
1976 $socid = 0;
1977 if ($user->socid > 0) {
1978 $socid = $user->socid;
1979 }
1980
1981 $tasksarray = $taskstatic->getTasksArray(0, 0, $this->id, $socid, 0);
1982
1983 foreach ($tasksarray as $tasktoshiftdate) {
1984 $to_update = false;
1985 // Fetch only if update of date will be made
1986 if ((!empty($tasktoshiftdate->date_start)) || (!empty($tasktoshiftdate->date_end))) {
1987 //dol_syslog(get_class($this)."::shiftTaskDate to_update", LOG_DEBUG);
1988 $to_update = true;
1989 $task = new Task($this->db);
1990 $result = $task->fetch($tasktoshiftdate->id);
1991 if (!$result) {
1992 $error++;
1993 $this->error .= $task->error;
1994 }
1995 }
1996 //print "$this->date_start + $tasktoshiftdate->date_start - $old_project_dt_start";exit;
1997
1998 //Calculate new task start date with difference between old proj start date and origin task start date
1999 if (!empty($tasktoshiftdate->date_start)) {
2000 $task->date_start = $this->date_start + ($tasktoshiftdate->date_start - $old_project_dt_start);
2001 }
2002
2003 //Calculate new task end date with difference between origin proj end date and origin task end date
2004 if (!empty($tasktoshiftdate->date_end)) {
2005 $task->date_end = $this->date_start + ($tasktoshiftdate->date_end - $old_project_dt_start);
2006 }
2007
2008 if ($to_update) {
2009 $result = $task->update($user);
2010 if (!$result) {
2011 $error++;
2012 $this->error .= $task->error;
2013 }
2014 }
2015 }
2016 if ($error != 0) {
2017 return -1;
2018 }
2019 return $result;
2020 }
2021
2022
2023 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2031 public function update_element($tableName, $elementSelectId)
2032 {
2033 // phpcs:enable
2034 $sql = "UPDATE ".MAIN_DB_PREFIX.$tableName;
2035
2036 if ($tableName == "actioncomm") {
2037 $sql .= " SET fk_project=".$this->id;
2038 $sql .= " WHERE id=".((int) $elementSelectId);
2039 } elseif (in_array($tableName, ["entrepot","mrp_mo","stocktransfer_stocktransfer"])) {
2040 $sql .= " SET fk_project=".$this->id;
2041 $sql .= " WHERE rowid=".((int) $elementSelectId);
2042 } else {
2043 $sql .= " SET fk_projet=".$this->id;
2044 $sql .= " WHERE rowid=".((int) $elementSelectId);
2045 }
2046
2047 dol_syslog(get_class($this)."::update_element", LOG_DEBUG);
2048 $resql = $this->db->query($sql);
2049 if (!$resql) {
2050 $this->error = $this->db->lasterror();
2051 return -1;
2052 } else {
2053 return 1;
2054 }
2055 }
2056
2057 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2067 public function remove_element($tableName, $elementSelectId, $projectfield = 'fk_projet')
2068 {
2069 // phpcs:enable
2070 $sql = "UPDATE ".MAIN_DB_PREFIX.$tableName;
2071
2072 if ($tableName == "actioncomm") {
2073 $sql .= " SET fk_project=NULL";
2074 $sql .= " WHERE id=".((int) $elementSelectId);
2075 } else {
2076 $sql .= " SET ".$projectfield."=NULL";
2077 $sql .= " WHERE rowid=".((int) $elementSelectId);
2078 }
2079
2080 dol_syslog(get_class($this)."::remove_element", LOG_DEBUG);
2081 $resql = $this->db->query($sql);
2082 if (!$resql) {
2083 $this->error = $this->db->lasterror();
2084 return -1;
2085 } else {
2086 return 1;
2087 }
2088 }
2089
2100 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
2101 {
2102 global $conf, $langs;
2103
2104 $langs->load("projects");
2105
2106 if (!dol_strlen($modele)) {
2107 $modele = 'baleine';
2108
2109 if ($this->model_pdf) {
2110 $modele = $this->model_pdf;
2111 } elseif (getDolGlobalString('PROJECT_ADDON_PDF')) {
2112 $modele = getDolGlobalString('PROJECT_ADDON_PDF');
2113 }
2114 }
2115
2116 $modelpath = "core/modules/project/doc/";
2117
2118 return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
2119 }
2120
2121
2131 public function loadTimeSpent($datestart, $taskid = 0, $userid = 0)
2132 {
2133 $this->weekWorkLoad = array();
2134 $this->weekWorkLoadPerTask = array();
2135
2136 if (empty($datestart)) {
2137 dol_print_error(null, 'Error datestart parameter is empty');
2138 }
2139
2140 $sql = "SELECT ptt.rowid as taskid, ptt.element_duration, ptt.element_date, ptt.element_datehour, ptt.fk_element";
2141 $sql .= " FROM ".MAIN_DB_PREFIX."element_time AS ptt, ".MAIN_DB_PREFIX."projet_task as pt";
2142 $sql .= " WHERE ptt.fk_element = pt.rowid";
2143 $sql .= " AND ptt.elementtype = 'task'";
2144 $sql .= " AND pt.fk_projet = ".((int) $this->id);
2145 $sql .= " AND (ptt.element_date >= '".$this->db->idate($datestart)."' ";
2146 $sql .= " AND ptt.element_date <= '".$this->db->idate(dol_time_plus_duree($datestart, 1, 'w') - 1)."')";
2147 if ($taskid) {
2148 $sql .= " AND ptt.fk_element=".((int) $taskid);
2149 }
2150 if (is_numeric($userid)) {
2151 $sql .= " AND ptt.fk_user=".((int) $userid);
2152 }
2153
2154 //print $sql;
2155 $resql = $this->db->query($sql);
2156 if ($resql) {
2157 $daylareadyfound = array();
2158
2159 $num = $this->db->num_rows($resql);
2160 $i = 0;
2161 // Loop on each record found, so each couple (project id, task id)
2162 while ($i < $num) {
2163 $obj = $this->db->fetch_object($resql);
2164 $day = $this->db->jdate($obj->element_date); // task_date is date without hours
2165 if (empty($daylareadyfound[$day])) {
2166 $this->weekWorkLoad[$day] = $obj->element_duration;
2167 $this->weekWorkLoadPerTask[$day][$obj->fk_element] = $obj->element_duration;
2168 } else {
2169 $this->weekWorkLoad[$day] += $obj->element_duration;
2170 $this->weekWorkLoadPerTask[$day][$obj->fk_element] += $obj->element_duration;
2171 }
2172 $daylareadyfound[$day] = 1;
2173 $i++;
2174 }
2175 $this->db->free($resql);
2176 return 1;
2177 } else {
2178 $this->error = "Error ".$this->db->lasterror();
2179 dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
2180 return -1;
2181 }
2182 }
2183
2193 public function loadTimeSpentMonth($datestart, $taskid = 0, $userid = 0)
2194 {
2195 $this->monthWorkLoad = array();
2196 $this->monthWorkLoadPerTask = array();
2197
2198 if (empty($datestart)) {
2199 dol_print_error(null, 'Error datestart parameter is empty');
2200 }
2201
2202 $sql = "SELECT ptt.rowid as taskid, ptt.element_duration, ptt.element_date, ptt.element_datehour, ptt.fk_element";
2203 $sql .= " FROM ".MAIN_DB_PREFIX."element_time AS ptt, ".MAIN_DB_PREFIX."projet_task as pt";
2204 $sql .= " WHERE ptt.fk_element = pt.rowid";
2205 $sql .= " AND ptt.elementtype = 'task'";
2206 $sql .= " AND pt.fk_projet = ".((int) $this->id);
2207 $sql .= " AND (ptt.element_date >= '".$this->db->idate($datestart)."' ";
2208 $sql .= " AND ptt.element_date <= '".$this->db->idate(dol_time_plus_duree($datestart, 1, 'm') - 1)."')";
2209 if ($taskid) {
2210 $sql .= " AND ptt.fk_element=".((int) $taskid);
2211 }
2212 if (is_numeric($userid)) {
2213 $sql .= " AND ptt.fk_user=".((int) $userid);
2214 }
2215
2216 //print $sql;
2217 $resql = $this->db->query($sql);
2218 if ($resql) {
2219 $weekalreadyfound = array();
2220
2221 $num = $this->db->num_rows($resql);
2222 $i = 0;
2223 // Loop on each record found, so each couple (project id, task id)
2224 while ($i < $num) {
2225 $obj = $this->db->fetch_object($resql);
2226 if (!empty($obj->element_date)) {
2227 $date = explode('-', $obj->element_date);
2228 $week_number = getWeekNumber((int) $date[2], (int) $date[1], (int) $date[0]);
2229 }
2230 '@phan-var-force int $week_number'; // Needed because phan considers it might be null
2231 if (empty($weekalreadyfound[$week_number])) {
2232 $this->monthWorkLoad[$week_number] = $obj->element_duration;
2233 $this->monthWorkLoadPerTask[$week_number][$obj->fk_element] = $obj->element_duration;
2234 } else {
2235 $this->monthWorkLoad[$week_number] += $obj->element_duration;
2236 $this->monthWorkLoadPerTask[$week_number][$obj->fk_element] += $obj->element_duration;
2237 }
2238 $weekalreadyfound[$week_number] = 1;
2239 $i++;
2240 }
2241 $this->db->free($resql);
2242 return 1;
2243 } else {
2244 $this->error = "Error ".$this->db->lasterror();
2245 dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
2246 return -1;
2247 }
2248 }
2249
2250 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2257 public function load_board($user)
2258 {
2259 // phpcs:enable
2260 global $conf, $langs;
2261
2262 // For external user, no check is done on company because readability is managed by public status of project and assignment.
2263 //$socid=$user->socid;
2264
2265 $response = new WorkboardResponse();
2266 $response->warning_delay = $conf->project->warning_delay / 60 / 60 / 24;
2267 $response->label = $langs->trans("OpenedProjects");
2268 $response->labelShort = $langs->trans("Opened");
2269 $response->url = DOL_URL_ROOT.'/projet/list.php?search_project_user=-1&search_status=1&mainmenu=project';
2270 $response->url_late = DOL_URL_ROOT.'/projet/list.php?search_option=late&mainmenu=project';
2271 $response->img = img_object('', "projectpub");
2272 $response->nbtodo = 0;
2273 $response->nbtodolate = 0;
2274
2275 $sql = "SELECT p.rowid, p.fk_statut as status, p.fk_opp_status, p.datee as datee";
2276 $sql .= " FROM (".MAIN_DB_PREFIX."projet as p";
2277 $sql .= ")";
2278 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s on p.fk_soc = s.rowid";
2279 // For external user, no check is done on company permission because readability is managed by public status of project and assignment.
2280 //if (! $user->rights->societe->client->voir && ! $socid) $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc = s.rowid";
2281 $sql .= " WHERE p.fk_statut = 1";
2282 $sql .= " AND p.entity IN (".getEntity('project').')';
2283
2284
2285 $projectsListId = null;
2286 if (!$user->hasRight("projet", "all", "lire")) {
2287 $response->url = DOL_URL_ROOT.'/projet/list.php?search_status=1&mainmenu=project';
2288 $projectsListId = $this->getProjectsAuthorizedForUser($user, 0, 1);
2289 if (empty($projectsListId)) {
2290 return $response;
2291 }
2292
2293 $sql .= " AND p.rowid IN (".$this->db->sanitize($projectsListId).")";
2294 }
2295
2296 // No need to check company, as filtering of projects must be done by getProjectsAuthorizedForUser
2297 //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).")";
2298 // For external user, no check is done on company permission because readability is managed by public status of project and assignment.
2299 //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))";
2300
2301 //print $sql;
2302 $resql = $this->db->query($sql);
2303 if ($resql) {
2304 $project_static = new Project($this->db);
2305
2306
2307 // This assignment in condition is not a bug. It allows walking the results.
2308 while ($obj = $this->db->fetch_object($resql)) {
2309 $response->nbtodo++;
2310
2311 $project_static->statut = $obj->status;
2312 $project_static->status = $obj->status;
2313 $project_static->opp_status = $obj->fk_opp_status;
2314 $project_static->date_end = $this->db->jdate($obj->datee);
2315
2316 if ($project_static->hasDelay()) {
2317 $response->nbtodolate++;
2318 }
2319 }
2320
2321 return $response;
2322 }
2323
2324 $this->error = $this->db->error();
2325 return -1;
2326 }
2327
2336 public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
2337 {
2338 $tables = array(
2339 'projet'
2340 );
2341
2342 return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
2343 }
2344
2345
2351 public function loadStateBoard()
2352 {
2353 global $user;
2354
2355 $this->nb = array();
2356
2357 $sql = "SELECT count(p.rowid) as nb";
2358 $sql .= " FROM ".MAIN_DB_PREFIX."projet as p";
2359 $sql .= " WHERE";
2360 $sql .= " p.entity IN (".getEntity('project').")";
2361 if (!$user->hasRight('projet', 'all', 'lire')) {
2362 $projectsListId = $this->getProjectsAuthorizedForUser($user, 0, 1);
2363 $sql .= "AND p.rowid IN (".$this->db->sanitize($projectsListId).")";
2364 }
2365
2366 $resql = $this->db->query($sql);
2367 if ($resql) {
2368 while ($obj = $this->db->fetch_object($resql)) {
2369 $this->nb["projects"] = $obj->nb;
2370 }
2371 $this->db->free($resql);
2372 return 1;
2373 } else {
2374 dol_print_error($this->db);
2375 $this->error = $this->db->error();
2376 return -1;
2377 }
2378 }
2379
2380
2386 public function hasDelay()
2387 {
2388 global $conf;
2389
2390 if (!($this->status == self::STATUS_VALIDATED)) {
2391 return false;
2392 }
2393 if (!$this->date_end) {
2394 return false;
2395 }
2396
2397 $now = dol_now();
2398
2399 return ($this->date_end) < ($now - $conf->project->warning_delay);
2400 }
2401
2402
2409 public function info($id)
2410 {
2411 $sql = 'SELECT c.rowid, datec as datec, tms as datem,';
2412 $sql .= ' date_close as datecloture,';
2413 $sql .= ' fk_user_creat as fk_user_author, fk_user_close as fk_user_cloture';
2414 $sql .= ' FROM '.MAIN_DB_PREFIX.'projet as c';
2415 $sql .= ' WHERE c.rowid = '.((int) $id);
2416 $result = $this->db->query($sql);
2417 if ($result) {
2418 if ($this->db->num_rows($result)) {
2419 $obj = $this->db->fetch_object($result);
2420
2421 $this->id = $obj->rowid;
2422
2423 $this->user_creation_id = $obj->fk_user_author;
2424 $this->user_closing_id = $obj->fk_user_cloture;
2425
2426 $this->date_creation = $this->db->jdate($obj->datec);
2427 $this->date_modification = $this->db->jdate($obj->datem);
2428 $this->date_cloture = $this->db->jdate($obj->datecloture);
2429 }
2430
2431 $this->db->free($result);
2432 } else {
2433 dol_print_error($this->db);
2434 }
2435 }
2436
2447 public function setCategories($categories)
2448 {
2449 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
2450 return parent::setCategoriesCommon($categories, Categorie::TYPE_PROJECT);
2451 }
2452
2453
2461 public function getLinesArray($user, $loadRoleMode = 1)
2462 {
2463 require_once DOL_DOCUMENT_ROOT.'/projet/class/task.class.php';
2464 $taskstatic = new Task($this->db);
2465
2466 $this->lines = $taskstatic->getTasksArray(0, $user, $this->id, 0, 0, '', '-1', '', 0, 0, array(), 0, array(), 0, $loadRoleMode);
2467 return 1;
2468 }
2469
2488 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 = '')
2489 {
2490 // TODO EMAIL
2491
2492 return 1;
2493 }
2494
2503 public function getKanbanView($option = '', $arraydata = null, $size = '')
2504 {
2505 global $conf, $langs;
2506
2507 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
2508
2509 if (empty($size)) {
2510 if (empty($conf->dol_optimize_smallscreen)) {
2511 $size = 'large';
2512 } else {
2513 $size = 'small';
2514 }
2515 }
2516
2517 $return = '<div class="box-flex-item '.($size == 'small' ? 'box-flex-item-small' : '').' box-flex-grow-zero">';
2518 $return .= '<div class="info-box info-box-sm">';
2519 $return .= '<span class="info-box-icon bg-infobox-action">';
2520 $return .= img_picto('', $this->public ? 'projectpub' : $this->picto);
2521 //$return .= '<i class="fa fa-dol-action"></i>'; // Can be image
2522 $return .= '</span>';
2523 $return .= '<div class="info-box-content">';
2524 $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref);
2525 if ($this->hasDelay()) {
2526 $return .= img_warning($langs->trans('Late'));
2527 }
2528 $return .= '</span>';
2529 if ($selected >= 0) {
2530 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
2531 }
2532 // Date
2533 /*
2534 if (property_exists($this, 'date_start') && $this->date_start) {
2535 $return .= '<br><span class="info-box-label">'.dol_print_date($this->date_start, 'day').'</>';
2536 }
2537 if (property_exists($this, 'date_end') && $this->date_end) {
2538 if ($this->date_start) {
2539 $return .= ' - ';
2540 } else {
2541 $return .= '<br>';
2542 }
2543 $return .= '<span class="info-box-label">'.dol_print_date($this->date_end, 'day').'</span>';
2544 }*/
2545 if (property_exists($this, 'thirdparty') && !is_null($this->thirdparty) && is_object($this->thirdparty) && $this->thirdparty instanceof Societe) {
2546 $return .= '<br><div class="info-box-ref tdoverflowmax125 inline-block valignmiddle">'.$this->thirdparty->getNomUrl(1);
2547 $return .= '</div>';
2548 if (!empty($this->thirdparty->phone)) {
2549 $return .= '<div class="inline-block valignmiddle">';
2550 $return .= dol_print_phone($this->thirdparty->phone, $this->thirdparty->country_code, 0, $this->thirdparty->id, 'tel', 'hidenum', 'phone', $this->thirdparty->phone, 0, 'paddingleft paddingright');
2551 $return .= '</div>';
2552 }
2553 if (!empty($this->thirdparty->email)) {
2554 $return .= '<div class="inline-block valignmiddle">';
2555 $return .= dol_print_email($this->thirdparty->email, 0, $this->thirdparty->id, 'thirdparty', -1, 1, 2, 'paddingleft paddingright');
2556 $return .= '</div>';
2557 }
2558 }
2559 if (!empty($arraydata['assignedusers'])) {
2560 $return .= '<br>';
2561 if ($this->public) {
2562 $return .= img_picto($langs->trans('Visibility').': '.$langs->trans('SharedProject'), 'world', 'class="paddingrightonly valignmiddle"');
2563 //print $langs->trans('SharedProject');
2564 } else {
2565 $return .= img_picto($langs->trans('Visibility').': '.$langs->trans('PrivateProject'), 'private', 'class="paddingrightonly valignmiddle"');
2566 //print $langs->trans('PrivateProject');
2567 }
2568
2569 $return .= ' <span class="small valignmiddle">'.$arraydata['assignedusers'].'</span>';
2570 }
2571 /*if (property_exists($this, 'user_author_id')) {
2572 $return .= '<br><span class="info-box-label opacitymedium">'.$langs->trans("Author").'</span>';
2573 $return .= '<span> : '.$user->getNomUrl(1).'</span>';
2574 }*/
2575 $return .= '<br><div>'; // start div line status
2576 if ($this->usage_opportunity && $this->opp_status_code) {
2577 //$return .= '<br><span class="info-bo-label opacitymedium">'.$langs->trans("OpportunityStatusShort").'</span>';
2578 //$return .= '<div class="small inline-block">'.dol_trunc($langs->trans("OppStatus".$this->opp_status_code), 5).'</div>';
2579 $return .= '<div class="opacitymedium small marginrightonly inline-block" title="'.dol_escape_htmltag($langs->trans("OppStatus".$this->opp_status_code)).'">'.round($this->opp_percent).'%</div>';
2580 $return .= ' <div class="amount small marginrightonly inline-block">'.price($this->opp_amount).'</div>';
2581 }
2582 if (method_exists($this, 'getLibStatut')) {
2583 $return .= '<div class="info-box-status small inline-block valignmiddle">'.$this->getLibStatut(3).'</div>';
2584 }
2585 $return .= '</div>'; // end div line status
2586
2587 $return .= '</div>';
2588 $return .= '</div>';
2589 $return .= '</div>';
2590
2591 return $return;
2592 }
2593
2599 public function getChildren()
2600 {
2601 $children = [];
2602 $sql = 'SELECT rowid,title';
2603 $sql .= ' FROM '.MAIN_DB_PREFIX.'projet';
2604 $sql .= ' WHERE fk_project = '.((int) $this->id);
2605 $sql .= ' ORDER BY title';
2606 $result = $this->db->query($sql);
2607 if ($result) {
2608 $n = $this->db->num_rows($result);
2609 while ($n) {
2610 $children[] = $this->db->fetch_object($result);
2611 $n--;
2612 }
2613 $this->db->free($result);
2614 } else {
2615 dol_print_error($this->db);
2616 }
2617
2618 return $children;
2619 }
2620
2625 public function createWeeklyReport()
2626 {
2627 global $mysoc, $user;
2628
2629 $now = dol_now();
2630 $nowDate = dol_getdate($now, true);
2631
2632 $errormesg = '';
2633 $errorsMsg = array();
2634
2635 $firstDayOfWeekTS = dol_get_first_day_week($nowDate['mday'], $nowDate['mon'], $nowDate['year']);
2636
2637 $firstDayOfWeekDate = dol_mktime(0, 0, 0, $nowDate['mon'], $firstDayOfWeekTS['first_day'], $nowDate['year']);
2638
2639 $lastWeekStartTS = dol_time_plus_duree($firstDayOfWeekDate, -7, 'd');
2640
2641 $lastWeekEndTS = dol_time_plus_duree($lastWeekStartTS, 6, 'd');
2642
2643 $startDate = dol_print_date($lastWeekStartTS, '%Y-%m-%d 00:00:00');
2644 $endDate = dol_print_date($lastWeekEndTS, '%Y-%m-%d 23:59:59');
2645
2646 $sql = "SELECT
2647 u.rowid AS user_id,
2648 CONCAT(u.firstname, ' ', u.lastname) AS name,
2649 u.email,u.weeklyhours,
2650 SUM(et.element_duration) AS total_seconds
2651 FROM
2652 ".MAIN_DB_PREFIX."element_time AS et
2653 JOIN
2654 ".MAIN_DB_PREFIX."user AS u ON et.fk_user = u.rowid
2655 WHERE
2656 et.element_date BETWEEN '".$this->db->escape($startDate)."' AND '".$this->db->escape($endDate)."'
2657 AND et.elementtype = 'task'
2658 GROUP BY
2659 et.fk_user";
2660
2661 $resql = $this->db->query($sql);
2662 if (!$resql) {
2663 dol_print_error($this->db);
2664 return -1;
2665 } else {
2666 $reportContent = "<span>Weekly time report from $startDate to $endDate </span><br><br>";
2667 $reportContent .= '<table border="1" style="border-collapse: collapse;">';
2668 $reportContent .= '<tr><th>Nom d\'utilisateur</th><th>Temps saisi (heures)</th><th>Temps travaillé par semaine (heures)</th></tr>';
2669
2670 $weekendEnabled = 0;
2671 $to = '';
2672 $nbMailSend = 0;
2673 $error = 0;
2674 $errors_to = '';
2675 while ($obj = $this->db->fetch_object($resql)) {
2676 $to = $obj->email;
2677 $numHolidays = num_public_holiday($lastWeekStartTS, $lastWeekEndTS, $mysoc->country_code, 1);
2678 if (getDolGlobalString('MAIN_NON_WORKING_DAYS_INCLUDE_SATURDAY') && getDolGlobalString('MAIN_NON_WORKING_DAYS_INCLUDE_SUNDAY')) {
2679 $numHolidays -= 2;
2680 $weekendEnabled = 2;
2681 }
2682
2683 $dailyHours = $obj->weeklyhours / (7 - $weekendEnabled);
2684
2685 // Adjust total on seconde
2686 $adjustedSeconds = $obj->total_seconds + ($numHolidays * $dailyHours * 3600);
2687
2688 $totalHours = round($adjustedSeconds / 3600, 2);
2689
2690 $reportContent .= "<tr><td>{$obj->name}</td><td>{$totalHours}</td><td>".round($obj->weeklyhours, 2)."</td></tr>";
2691
2692 $reportContent .= '</table>';
2693
2694 require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
2695
2696 // PREPARE EMAIL
2697 $errormesg = '';
2698
2699 $subject = 'Rapport hebdomadaire des temps travaillés';
2700
2701 $from = getDolGlobalString('MAIN_MAIL_EMAIL_FROM');
2702 if (empty($from)) {
2703 $errormesg = "Failed to get sender into global setup MAIN_MAIL_EMAIL_FROM";
2704 $error++;
2705 }
2706
2707 $mail = new CMailFile($subject, $to, $from, $reportContent, array(), array(), array(), '', '', 0, -1, '', '', 0, 'text/html');
2708
2709 if ($mail->sendfile()) {
2710 $nbMailSend++;
2711
2712 // Add a line into event table
2713 require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
2714
2715 // Insert record of emails sent
2716 $actioncomm = new ActionComm($this->db);
2717
2718 $actioncomm->type_code = 'AC_OTH_AUTO'; // Event insert into agenda automatically
2719 $actioncomm->socid = $this->thirdparty->id; // To link to a company
2720 $actioncomm->contact_id = 0;
2721
2722 $actioncomm->code = 'AC_EMAIL';
2723 $actioncomm->label = 'createWeeklyReportOK()';
2724 $actioncomm->fk_project = $this->id;
2725 $actioncomm->datep = dol_now();
2726 $actioncomm->datef = $actioncomm->datep;
2727 $actioncomm->percentage = -1; // Not applicable
2728 $actioncomm->authorid = $user->id; // User saving action
2729 $actioncomm->userownerid = $user->id; // Owner of action
2730 // Fields when action is an email (content should be added into note)
2731 $actioncomm->email_msgid = $mail->msgid;
2732 $actioncomm->email_subject = $subject;
2733 $actioncomm->email_from = $from;
2734 $actioncomm->email_sender = '';
2735 $actioncomm->email_to = $to;
2736
2737 $actioncomm->errors_to = $errors_to;
2738
2739 $actioncomm->elementtype = 'project_task';
2740 $actioncomm->fk_element = (int) $this->element;
2741
2742 $actioncomm->create($user);
2743 } else {
2744 $errormesg = $mail->error.' : '.$to;
2745 $error++;
2746
2747 // Add a line into event table
2748 require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
2749
2750 // Insert record of emails sent
2751 $actioncomm = new ActionComm($this->db);
2752
2753 $actioncomm->type_code = 'AC_OTH_AUTO'; // Event insert into agenda automatically
2754 $actioncomm->socid = $this->thirdparty->id; // To link to a company
2755 $actioncomm->contact_id = 0;
2756
2757 $actioncomm->code = 'AC_EMAIL';
2758 $actioncomm->label = 'createWeeklyReportKO()';
2759 $actioncomm->note_private = $errormesg;
2760 $actioncomm->fk_project = $this->id;
2761 $actioncomm->datep = dol_now();
2762 $actioncomm->datef = $actioncomm->datep;
2763 $actioncomm->authorid = $user->id; // User saving action
2764 $actioncomm->userownerid = $user->id; // Owner of action
2765 // Fields when action is an email (content should be added into note)
2766 $actioncomm->email_msgid = $mail->msgid;
2767 $actioncomm->email_from = $from;
2768 $actioncomm->email_sender = '';
2769 $actioncomm->email_to = $to;
2770
2771 $actioncomm->errors_to = $errors_to;
2772
2773 $actioncomm->elementtype = 'project_task';
2774 $actioncomm->fk_element = (int) $this->element;
2775
2776 $actioncomm->create($user);
2777 }
2778 $this->db->commit();
2779 }
2780 }
2781 if (!empty($errormesg)) {
2782 $errorsMsg[] = $errormesg;
2783 }
2784
2785 if (!$error) {
2786 $this->output .= 'Nb of emails sent : '.$nbMailSend;
2787 dol_syslog(__METHOD__." end - ".$this->output, LOG_INFO);
2788 return 0;
2789 } else {
2790 $this->error = 'Nb of emails sent : '.$nbMailSend.', '.(empty($errorsMsg) ? $error : implode(', ', $errorsMsg));
2791 dol_syslog(__METHOD__." end - ".$this->error, LOG_INFO);
2792 return $error;
2793 }
2794 }
2795}
print $langs trans("AuditedSecurityEvents").'</strong >< span class="opacitymedium"></span >< br > status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition security.php:634
$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.
$user_author_id
Id of project creator. Not defined if shared project.
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.
$public
Tell if this is a public or private project.
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.
dol_get_first_day_week($day, $month, $year, $gm=false)
Return first day of week for a date.
Definition date.lib.php:670
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:124
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:764
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.
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)