2005-08-21 14:39:11 +02:00
< ? php
2014-10-12 01:04:34 +02:00
/* Copyright ( C ) 2008 - 2014 Laurent Destailleur < eldy @ users . sourceforge . net >
2018-10-27 14:43:12 +02:00
* Copyright ( C ) 2010 - 2012 Regis Houssin < regis . houssin @ inodbox . com >
Created a method generateDocument for several classes
Which are: Commande, Contrat, Livraison, Facture, Projet, Propal, Task, Expedition, CommandeFournisseur, FactureFournisseur and therefore deprecated the following methods supplier_order_pdf_create, supplier_invoice_pdf_create, delivery_order_pdf_create, task_pdf_create, propale_pdf_create, project_pdf_create, facture_pdf_create, expedition_pdf_create, commande_pdf_create
2014-09-16 12:30:37 +02:00
* Copyright ( C ) 2014 Marcos García < marcosgdf @ gmail . com >
2024-02-25 22:14:55 +01:00
* Copyright ( C ) 2018 - 2024 Frédéric France < frederic . france @ free . fr >
2020-03-24 18:49:16 +01:00
* Copyright ( C ) 2020 Juanjo Menent < jmenent @ 2 byte . es >
2022-01-20 17:06:03 +01:00
* Copyright ( C ) 2022 Charlene Benke < charlene @ patas - monkey . com >
2023-03-08 11:00:58 +01:00
* Copyright ( C ) 2023 Gauthier VERDOL < gauthier . verdol @ atm - consulting . fr >
2024-03-11 22:21:08 +01:00
* Copyright ( C ) 2024 MDW < mdeweerd @ users . noreply . github . com >
2005-08-21 14:39:11 +02:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2005-08-21 14:39:11 +02:00
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2005-08-21 14:39:11 +02:00
*/
/**
2010-04-30 07:48:16 +02:00
* \file htdocs / projet / class / task . class . php
2009-07-28 15:37:28 +02:00
* \ingroup project
2009-11-24 07:51:12 +01:00
* \brief This file is a CRUD class file for Task ( Create / Read / Update / Delete )
2009-07-28 15:37:28 +02:00
*/
2005-08-21 14:39:11 +02:00
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php' ;
2022-03-01 16:38:06 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobjectline.class.php' ;
2020-04-20 14:21:12 +02:00
require_once DOL_DOCUMENT_ROOT . '/projet/class/project.class.php' ;
2023-03-08 11:00:58 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/class/timespent.class.php' ;
2010-04-17 16:49:27 +02:00
2008-09-10 13:14:45 +02:00
2005-08-21 14:39:11 +02:00
/**
2012-09-15 15:33:00 +02:00
* Class to manage tasks
2009-07-28 15:37:28 +02:00
*/
2022-01-20 17:06:03 +01:00
class Task extends CommonObjectLine
2008-09-10 13:14:45 +02:00
{
2018-08-23 18:52:47 +02:00
/**
* @ var string ID to identify managed object
*/
2019-11-13 18:32:11 +01:00
public $element = 'project_task' ;
2018-08-28 08:33:02 +02:00
2018-08-22 19:56:24 +02:00
/**
2022-01-26 14:17:32 +01:00
* @ var string Name of table without prefix where object is stored
2018-08-22 19:56:24 +02:00
*/
2019-11-13 18:32:11 +01:00
public $table_element = 'projet_task' ;
2018-08-28 08:33:02 +02:00
2018-09-01 15:27:55 +02:00
/**
2020-12-05 23:53:55 +01:00
* @ var string Field with ID of parent key if this field has a parent
2018-09-01 15:27:55 +02:00
*/
2023-03-08 11:00:58 +01:00
public $fk_element = 'fk_element' ;
2018-09-01 15:27:55 +02:00
2019-10-30 10:08:36 +01:00
/**
2020-11-28 01:14:45 +01:00
* @ var string String with name of icon for myobject .
2019-10-30 10:08:36 +01:00
*/
2020-11-28 01:14:45 +01:00
public $picto = 'projecttask' ;
2019-06-22 20:17:40 +02:00
/**
2024-03-11 22:21:08 +01:00
* @ var array < string , array < string >> List of child tables . To test if we can delete object .
2019-06-22 20:17:40 +02:00
*/
2022-03-26 08:32:20 +01:00
protected $childtables = array (
2023-03-08 11:00:58 +01:00
'element_time' => array ( 'name' => 'Task' , 'parent' => 'projet_task' , 'parentkey' => 'fk_element' , 'parenttypefield' => 'elementtype' , 'parenttypevalue' => 'task' )
2022-03-26 08:32:20 +01:00
);
2017-10-07 13:09:31 +02:00
2018-10-12 09:34:54 +02:00
/**
2020-09-07 10:18:17 +02:00
* @ var int ID parent task
*/
public $fk_task_parent = 0 ;
2018-08-28 08:33:02 +02:00
2020-09-07 10:18:17 +02:00
/**
* @ var string Label of task
*/
public $label ;
2018-08-28 08:33:02 +02:00
2018-08-31 19:26:08 +02:00
/**
* @ var string description
*/
public $description ;
2019-11-13 18:32:11 +01:00
public $duration_effective ; // total of time spent on this task
2018-10-04 09:15:15 +02:00
public $planned_workload ;
public $date_c ;
public $progress ;
2018-10-12 09:34:54 +02:00
2024-01-14 12:10:09 +01:00
/**
* @ deprecated Use date_start instead
*/
public $dateo ;
public $date_start ;
2022-06-15 14:09:00 +02:00
/**
2022-06-15 14:10:15 +02:00
* @ deprecated Use date_end instead
2022-06-15 14:09:00 +02:00
*/
public $datee ;
2018-10-12 09:34:54 +02:00
2024-01-14 12:10:09 +01:00
public $date_end ;
2018-10-12 09:34:54 +02:00
/**
2020-09-07 10:18:17 +02:00
* @ var int ID
2024-02-16 13:23:23 +01:00
* @ deprecated use status instead
2020-09-07 10:18:17 +02:00
*/
2018-10-04 09:15:15 +02:00
public $fk_statut ;
2018-10-12 09:34:54 +02:00
2024-02-16 13:23:23 +01:00
/**
* @ var int ID
*/
public $status ;
2018-10-04 09:15:15 +02:00
public $priority ;
2018-10-12 09:34:54 +02:00
/**
2020-09-07 10:18:17 +02:00
* @ var int ID
*/
2018-10-04 09:15:15 +02:00
public $fk_user_creat ;
2018-10-12 09:34:54 +02:00
/**
2020-09-07 10:18:17 +02:00
* @ var int ID
*/
2018-10-04 09:15:15 +02:00
public $fk_user_valid ;
2018-10-12 09:34:54 +02:00
2018-10-04 09:15:15 +02:00
public $rang ;
public $timespent_min_date ;
public $timespent_max_date ;
public $timespent_total_duration ;
public $timespent_total_amount ;
public $timespent_nblinesnull ;
public $timespent_nblines ;
2016-11-19 16:08:27 +01:00
// For detail of lines of timespent record, there is the property ->lines in common
2017-06-07 10:55:39 +02:00
2016-11-19 16:08:27 +01:00
// Var used to call method addTimeSpent(). Bad practice.
2018-10-04 09:15:15 +02:00
public $timespent_id ;
public $timespent_duration ;
public $timespent_old_duration ;
public $timespent_date ;
2019-11-13 18:32:11 +01:00
public $timespent_datehour ; // More accurate start date (same than timespent_date but includes hours, minutes and seconds)
public $timespent_withhour ; // 1 = we entered also start hours for timesheet line
2018-10-04 09:15:15 +02:00
public $timespent_fk_user ;
2021-07-12 23:01:34 +02:00
public $timespent_thm ;
2018-10-04 09:15:15 +02:00
public $timespent_note ;
2022-01-07 12:17:39 +01:00
public $timespent_fk_product ;
2023-03-03 12:07:46 +01:00
public $timespent_invoiceid ;
public $timespent_invoicelineid ;
2018-10-04 09:15:15 +02:00
public $comments = array ();
2017-10-07 13:09:31 +02:00
2023-06-27 21:46:52 +02:00
/**
* @ var array
*/
2023-11-24 10:10:24 +01:00
public $labelStatus ;
2023-06-27 21:46:52 +02:00
/**
* @ var array
*/
2023-11-24 10:10:24 +01:00
public $labelStatusShort ;
2023-06-27 21:46:52 +02:00
2023-06-29 16:30:34 +02:00
// Properties calculated from sum of llx_element_time linked to task
public $tobill ;
public $billed ;
2023-06-29 15:54:09 +02:00
2024-01-12 17:55:52 +01:00
// Properties to store project information
2023-06-29 15:54:09 +02:00
public $projectref ;
public $projectstatus ;
public $projectlabel ;
public $opp_amount ;
public $opp_percent ;
public $fk_opp_status ;
public $usage_bill_time ;
public $public ;
2023-06-29 16:30:34 +02:00
public $array_options_project ;
2023-06-29 15:54:09 +02:00
// Properties to store thirdparty of project information
public $socid ;
public $thirdparty_id ;
public $thirdparty_name ;
public $thirdparty_email ;
2023-11-16 23:19:33 +01:00
// store parent ref and position
public $task_parent_ref ;
public $task_parent_position ;
2023-06-29 15:54:09 +02:00
/**
* @ var float budget_amount
*/
public $budget_amount ;
2021-10-15 10:24:56 +02:00
/**
* @ var float project_budget_amount
*/
public $project_budget_amount ;
2024-02-16 13:23:23 +01:00
/**
* Draft status
*/
const STATUS_DRAFT = 0 ;
/**
2024-02-16 13:32:40 +01:00
* Validated status ( To do ) . Note : We also have the field progress to know the progression from 0 to 100 %.
2024-02-16 13:23:23 +01:00
*/
2024-02-16 13:32:40 +01:00
const STATUS_VALIDATED = 1 ;
2024-02-16 13:23:23 +01:00
/**
* Finished status
*/
2024-02-16 13:32:40 +01:00
const STATUS_CLOSED = 3 ;
2024-02-16 13:23:23 +01:00
/**
* Transferred status
*/
const STATUS_TRANSFERRED = 4 ;
/**
* status canceled
*/
const STATUS_CANCELED = 9 ;
2017-10-07 13:09:31 +02:00
/**
* Constructor
*
* @ param DoliDB $db Database handler
*/
2019-02-24 23:32:09 +01:00
public function __construct ( $db )
2017-10-07 13:09:31 +02:00
{
$this -> db = $db ;
}
/**
* Create into database
*
* @ param User $user User that create
* @ param int $notrigger 0 = launch triggers after , 1 = disable triggers
2023-12-01 19:51:32 +01:00
* @ return int Return integer < 0 if KO , Id of created object if OK
2017-10-07 13:09:31 +02:00
*/
2019-02-24 23:32:09 +01:00
public function create ( $user , $notrigger = 0 )
2017-10-07 13:09:31 +02:00
{
global $conf , $langs ;
2020-08-19 11:24:32 +02:00
//For the date
$now = dol_now ();
2019-11-13 18:32:11 +01:00
$error = 0 ;
2017-10-07 13:09:31 +02:00
// Clean parameters
$this -> label = trim ( $this -> label );
$this -> description = trim ( $this -> description );
2022-01-07 11:29:43 +01:00
if ( ! empty ( $this -> date_start ) && ! empty ( $this -> date_end ) && $this -> date_start > $this -> date_end ) {
$this -> errors [] = $langs -> trans ( 'StartDateCannotBeAfterEndDate' );
return - 1 ;
}
2017-10-07 13:09:31 +02:00
// Insert request
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " projet_task ( " ;
2020-03-24 18:49:16 +01:00
$sql .= " entity " ;
$sql .= " , fk_projet " ;
2019-11-13 18:32:11 +01:00
$sql .= " , ref " ;
$sql .= " , fk_task_parent " ;
$sql .= " , label " ;
$sql .= " , description " ;
$sql .= " , datec " ;
$sql .= " , fk_user_creat " ;
$sql .= " , dateo " ;
$sql .= " , datee " ;
$sql .= " , planned_workload " ;
$sql .= " , progress " ;
2021-10-15 10:24:56 +02:00
$sql .= " , budget_amount " ;
2019-11-13 18:32:11 +01:00
$sql .= " ) VALUES ( " ;
2023-03-24 16:18:14 +01:00
$sql .= ( ! empty ( $this -> entity ) ? ( int ) $this -> entity : ( int ) $conf -> entity );
2021-09-03 21:25:17 +02:00
$sql .= " , " . (( int ) $this -> fk_project );
2019-11-13 18:32:11 +01:00
$sql .= " , " . ( ! empty ( $this -> ref ) ? " ' " . $this -> db -> escape ( $this -> ref ) . " ' " : 'null' );
2021-09-03 21:25:17 +02:00
$sql .= " , " . (( int ) $this -> fk_task_parent );
2019-11-13 18:32:11 +01:00
$sql .= " , ' " . $this -> db -> escape ( $this -> label ) . " ' " ;
$sql .= " , ' " . $this -> db -> escape ( $this -> description ) . " ' " ;
2020-08-19 11:24:32 +02:00
$sql .= " , ' " . $this -> db -> idate ( $now ) . " ' " ;
2021-09-03 21:25:17 +02:00
$sql .= " , " . (( int ) $user -> id );
2021-09-24 02:10:42 +02:00
$sql .= " , " . ( $this -> date_start ? " ' " . $this -> db -> idate ( $this -> date_start ) . " ' " : 'null' );
$sql .= " , " . ( $this -> date_end ? " ' " . $this -> db -> idate ( $this -> date_end ) . " ' " : 'null' );
2021-09-03 21:25:17 +02:00
$sql .= " , " . (( $this -> planned_workload != '' && $this -> planned_workload >= 0 ) ? (( int ) $this -> planned_workload ) : 'null' );
$sql .= " , " . (( $this -> progress != '' && $this -> progress >= 0 ) ? (( int ) $this -> progress ) : 'null' );
2021-10-15 10:24:56 +02:00
$sql .= " , " . (( $this -> budget_amount != '' && $this -> budget_amount >= 0 ) ? (( int ) $this -> budget_amount ) : 'null' );
2019-11-13 18:32:11 +01:00
$sql .= " ) " ;
2017-10-07 13:09:31 +02:00
$this -> db -> begin ();
dol_syslog ( get_class ( $this ) . " ::create " , LOG_DEBUG );
2019-11-13 18:32:11 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-26 18:49:22 +01:00
if ( ! $resql ) {
2023-12-04 13:49:31 +01:00
$error ++ ;
$this -> errors [] = " Error " . $this -> db -> lasterror ();
2021-02-26 18:49:22 +01:00
}
2017-10-07 13:09:31 +02:00
2021-02-26 18:49:22 +01:00
if ( ! $error ) {
2017-10-07 13:09:31 +02:00
$this -> id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . " projet_task " );
2021-02-26 18:49:22 +01:00
if ( ! $notrigger ) {
2017-10-07 13:09:31 +02:00
// Call trigger
2019-11-13 18:32:11 +01:00
$result = $this -> call_trigger ( 'TASK_CREATE' , $user );
2021-02-26 18:49:22 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2017-10-07 13:09:31 +02:00
// End call triggers
}
}
// Update extrafield
2021-02-26 18:49:22 +01:00
if ( ! $error ) {
if ( ! $error ) {
2019-11-13 18:32:11 +01:00
$result = $this -> insertExtraFields ();
2021-02-26 18:49:22 +01:00
if ( $result < 0 ) {
2017-10-07 13:09:31 +02:00
$error ++ ;
}
}
}
// Commit or rollback
2021-02-26 18:49:22 +01:00
if ( $error ) {
foreach ( $this -> errors as $errmsg ) {
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::create " . $errmsg , LOG_ERR );
2019-11-13 18:32:11 +01:00
$this -> error .= ( $this -> error ? ', ' . $errmsg : $errmsg );
2017-10-07 13:09:31 +02:00
}
$this -> db -> rollback ();
2019-11-13 18:32:11 +01:00
return - 1 * $error ;
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
$this -> db -> commit ();
return $this -> id ;
}
}
/**
* Load object in memory from database
*
2018-01-04 13:52:37 +01:00
* @ param int $id Id object
2024-01-21 19:54:42 +01:00
* @ param string $ref ref object
2018-01-04 13:52:37 +01:00
* @ param int $loadparentdata Also load parent data
2023-12-01 19:51:32 +01:00
* @ return int Return integer < 0 if KO , 0 if not found , > 0 if OK
2017-10-07 13:09:31 +02:00
*/
2019-02-24 23:32:09 +01:00
public function fetch ( $id , $ref = '' , $loadparentdata = 0 )
2017-10-07 13:09:31 +02:00
{
$sql = " SELECT " ;
2019-11-13 18:32:11 +01:00
$sql .= " t.rowid, " ;
$sql .= " t.ref, " ;
2023-03-24 15:58:53 +01:00
$sql .= " t.entity, " ;
2019-11-13 18:32:11 +01:00
$sql .= " t.fk_projet as fk_project, " ;
$sql .= " t.fk_task_parent, " ;
$sql .= " t.label, " ;
$sql .= " t.description, " ;
$sql .= " t.duration_effective, " ;
$sql .= " t.planned_workload, " ;
$sql .= " t.datec, " ;
2024-01-14 12:10:09 +01:00
$sql .= " t.dateo as date_start, " ;
$sql .= " t.datee as date_end, " ;
2019-11-13 18:32:11 +01:00
$sql .= " t.fk_user_creat, " ;
$sql .= " t.fk_user_valid, " ;
2024-02-16 18:18:17 +01:00
$sql .= " t.fk_statut as status, " ;
2019-11-13 18:32:11 +01:00
$sql .= " t.progress, " ;
2021-10-15 10:24:56 +02:00
$sql .= " t.budget_amount, " ;
2019-11-13 18:32:11 +01:00
$sql .= " t.priority, " ;
$sql .= " t.note_private, " ;
$sql .= " t.note_public, " ;
$sql .= " t.rang " ;
2021-02-26 18:49:22 +01:00
if ( ! empty ( $loadparentdata )) {
2019-11-13 18:32:11 +01:00
$sql .= " , t2.ref as task_parent_ref " ;
$sql .= " , t2.rang as task_parent_position " ;
}
$sql .= " FROM " . MAIN_DB_PREFIX . " projet_task as t " ;
2021-02-26 18:49:22 +01:00
if ( ! empty ( $loadparentdata )) {
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " projet_task as t2 ON t.fk_task_parent = t2.rowid " ;
}
2019-11-13 18:32:11 +01:00
$sql .= " WHERE " ;
2017-10-07 13:09:31 +02:00
if ( ! empty ( $ref )) {
2021-05-21 18:53:09 +02:00
$sql .= " entity IN ( " . getEntity ( 'project' ) . " ) " ;
$sql .= " AND t.ref = ' " . $this -> db -> escape ( $ref ) . " ' " ;
2019-11-13 18:32:11 +01:00
} else {
2021-03-14 11:48:39 +01:00
$sql .= " t.rowid = " . (( int ) $id );
2017-10-07 13:09:31 +02:00
}
dol_syslog ( get_class ( $this ) . " ::fetch " , LOG_DEBUG );
2019-11-13 18:32:11 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-26 18:49:22 +01:00
if ( $resql ) {
2017-10-07 13:09:31 +02:00
$num_rows = $this -> db -> num_rows ( $resql );
2021-02-26 18:49:22 +01:00
if ( $num_rows ) {
2017-10-07 13:09:31 +02:00
$obj = $this -> db -> fetch_object ( $resql );
2019-11-13 18:32:11 +01:00
$this -> id = $obj -> rowid ;
$this -> ref = $obj -> ref ;
2023-03-24 15:58:53 +01:00
$this -> entity = $obj -> entity ;
2019-11-13 18:32:11 +01:00
$this -> fk_project = $obj -> fk_project ;
$this -> fk_task_parent = $obj -> fk_task_parent ;
$this -> label = $obj -> label ;
$this -> description = $obj -> description ;
$this -> duration_effective = $obj -> duration_effective ;
$this -> planned_workload = $obj -> planned_workload ;
$this -> date_c = $this -> db -> jdate ( $obj -> datec );
2024-01-14 12:10:09 +01:00
$this -> date_start = $this -> db -> jdate ( $obj -> date_start );
$this -> date_end = $this -> db -> jdate ( $obj -> date_end );
2017-10-07 13:09:31 +02:00
$this -> fk_user_creat = $obj -> fk_user_creat ;
$this -> fk_user_valid = $obj -> fk_user_valid ;
2024-02-16 13:23:23 +01:00
$this -> fk_statut = $obj -> status ;
$this -> status = $obj -> status ;
2017-10-07 13:09:31 +02:00
$this -> progress = $obj -> progress ;
2021-10-15 10:24:56 +02:00
$this -> budget_amount = $obj -> budget_amount ;
2017-10-07 13:09:31 +02:00
$this -> priority = $obj -> priority ;
2019-11-13 18:32:11 +01:00
$this -> note_private = $obj -> note_private ;
$this -> note_public = $obj -> note_public ;
$this -> rang = $obj -> rang ;
2017-06-07 16:44:04 +02:00
2021-02-26 18:49:22 +01:00
if ( ! empty ( $loadparentdata )) {
2018-01-04 13:52:37 +01:00
$this -> task_parent_ref = $obj -> task_parent_ref ;
$this -> task_parent_position = $obj -> task_parent_position ;
}
2020-10-23 20:08:35 +02:00
// Retrieve all extrafield
2018-02-20 19:38:18 +01:00
$this -> fetch_optionals ();
2017-10-07 13:09:31 +02:00
}
$this -> db -> free ( $resql );
2021-02-26 18:49:22 +01:00
if ( $num_rows ) {
2017-10-07 13:09:31 +02:00
return 1 ;
2019-11-13 18:32:11 +01:00
} else {
2017-10-07 13:09:31 +02:00
return 0 ;
}
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 18:32:11 +01:00
$this -> error = " Error " . $this -> db -> lasterror ();
2017-10-07 13:09:31 +02:00
return - 1 ;
}
}
/**
* Update database
*
* @ param User $user User that modify
* @ param int $notrigger 0 = launch triggers after , 1 = disable triggers
2023-12-06 15:46:39 +01:00
* @ return int Return integer <= 0 if KO , > 0 if OK
2017-10-07 13:09:31 +02:00
*/
2019-02-24 23:32:09 +01:00
public function update ( $user = null , $notrigger = 0 )
2017-10-07 13:09:31 +02:00
{
global $conf , $langs ;
2019-11-13 18:32:11 +01:00
$error = 0 ;
2017-10-07 13:09:31 +02:00
// Clean parameters
2021-02-26 18:49:22 +01:00
if ( isset ( $this -> fk_project )) {
2024-03-02 12:40:59 +01:00
$this -> fk_project = ( int ) $this -> fk_project ;
2021-02-26 18:49:22 +01:00
}
if ( isset ( $this -> ref )) {
$this -> ref = trim ( $this -> ref );
}
if ( isset ( $this -> fk_task_parent )) {
$this -> fk_task_parent = ( int ) $this -> fk_task_parent ;
}
if ( isset ( $this -> label )) {
$this -> label = trim ( $this -> label );
}
if ( isset ( $this -> description )) {
$this -> description = trim ( $this -> description );
}
if ( isset ( $this -> duration_effective )) {
$this -> duration_effective = trim ( $this -> duration_effective );
}
if ( isset ( $this -> planned_workload )) {
$this -> planned_workload = trim ( $this -> planned_workload );
}
2021-10-15 10:24:56 +02:00
if ( isset ( $this -> budget_amount )) {
2024-03-02 12:40:59 +01:00
$this -> budget_amount = ( float ) $this -> budget_amount ;
2021-10-15 10:24:56 +02:00
}
2017-10-07 13:09:31 +02:00
2022-01-07 11:29:43 +01:00
if ( ! empty ( $this -> date_start ) && ! empty ( $this -> date_end ) && $this -> date_start > $this -> date_end ) {
$this -> errors [] = $langs -> trans ( 'StartDateCannotBeAfterEndDate' );
return - 1 ;
}
2017-10-07 13:09:31 +02:00
// Check parameters
// Put here code to add control on parameters values
// Update request
$sql = " UPDATE " . MAIN_DB_PREFIX . " projet_task SET " ;
2019-11-13 18:32:11 +01:00
$sql .= " fk_projet= " . ( isset ( $this -> fk_project ) ? $this -> fk_project : " null " ) . " , " ;
$sql .= " ref= " . ( isset ( $this -> ref ) ? " ' " . $this -> db -> escape ( $this -> ref ) . " ' " : " ' " . $this -> db -> escape ( $this -> id ) . " ' " ) . " , " ;
$sql .= " fk_task_parent= " . ( isset ( $this -> fk_task_parent ) ? $this -> fk_task_parent : " null " ) . " , " ;
$sql .= " label= " . ( isset ( $this -> label ) ? " ' " . $this -> db -> escape ( $this -> label ) . " ' " : " null " ) . " , " ;
$sql .= " description= " . ( isset ( $this -> description ) ? " ' " . $this -> db -> escape ( $this -> description ) . " ' " : " null " ) . " , " ;
$sql .= " duration_effective= " . ( isset ( $this -> duration_effective ) ? $this -> duration_effective : " null " ) . " , " ;
$sql .= " planned_workload= " . (( isset ( $this -> planned_workload ) && $this -> planned_workload != '' ) ? $this -> planned_workload : " null " ) . " , " ;
$sql .= " dateo= " . ( $this -> date_start != '' ? " ' " . $this -> db -> idate ( $this -> date_start ) . " ' " : 'null' ) . " , " ;
$sql .= " datee= " . ( $this -> date_end != '' ? " ' " . $this -> db -> idate ( $this -> date_end ) . " ' " : 'null' ) . " , " ;
$sql .= " progress= " . (( $this -> progress != '' && $this -> progress >= 0 ) ? $this -> progress : 'null' ) . " , " ;
2021-10-15 10:24:56 +02:00
$sql .= " budget_amount= " . (( $this -> budget_amount != '' && $this -> budget_amount >= 0 ) ? $this -> budget_amount : 'null' ) . " , " ;
2019-11-13 18:32:11 +01:00
$sql .= " rang= " . (( ! empty ( $this -> rang )) ? $this -> rang : " 0 " );
2021-03-14 12:20:23 +01:00
$sql .= " WHERE rowid= " . (( int ) $this -> id );
2017-10-07 13:09:31 +02:00
$this -> db -> begin ();
dol_syslog ( get_class ( $this ) . " ::update " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
2021-02-26 18:49:22 +01:00
if ( ! $resql ) {
2023-12-04 13:49:31 +01:00
$error ++ ;
$this -> errors [] = " Error " . $this -> db -> lasterror ();
2021-02-26 18:49:22 +01:00
}
2017-10-07 13:09:31 +02:00
2018-04-10 12:03:01 +02:00
// Update extrafield
2019-11-13 18:32:11 +01:00
if ( ! $error ) {
2021-02-16 14:11:21 +01:00
$result = $this -> insertExtraFields ();
2021-02-26 18:49:22 +01:00
if ( $result < 0 ) {
2021-02-16 14:11:21 +01:00
$error ++ ;
}
}
2023-11-27 11:56:32 +01:00
if ( ! $error && getDolGlobalString ( 'PROJECT_CLASSIFY_CLOSED_WHEN_ALL_TASKS_DONE' )) {
2021-02-16 14:11:21 +01:00
// Close the parent project if it is open (validated) and its tasks are 100% completed
$project = new Project ( $this -> db );
2021-04-06 16:59:00 +02:00
if ( $project -> fetch ( $this -> fk_project ) > 0 ) {
2021-04-06 17:03:07 +02:00
if ( $project -> statut == Project :: STATUS_VALIDATED ) {
2021-04-06 16:59:00 +02:00
$project -> getLinesArray ( null ); // this method does not return <= 0 if fails
$projectCompleted = array_reduce (
$project -> lines ,
2024-03-14 21:59:15 +01:00
/**
* @ param bool $allTasksCompleted
* @ param Task $task
* @ return bool
*/
static function ( $allTasksCompleted , $task ) {
2021-04-06 16:59:00 +02:00
return $allTasksCompleted && $task -> progress >= 100 ;
},
2023-12-04 13:49:31 +01:00
1
2021-04-06 16:59:00 +02:00
);
if ( $projectCompleted ) {
if ( $project -> setClose ( $user ) <= 0 ) {
$error ++ ;
}
2021-02-16 14:11:21 +01:00
}
2018-04-10 12:03:01 +02:00
}
2021-02-16 14:11:21 +01:00
} else {
$error ++ ;
}
if ( $error ) {
$this -> errors [] = $project -> error ;
2018-04-10 12:03:01 +02:00
}
}
2021-02-26 18:49:22 +01:00
if ( ! $error ) {
if ( ! $notrigger ) {
2017-10-07 13:09:31 +02:00
// Call trigger
2019-11-13 18:32:11 +01:00
$result = $this -> call_trigger ( 'TASK_MODIFY' , $user );
2021-02-26 18:49:22 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2017-10-07 13:09:31 +02:00
// End call triggers
}
}
2021-02-26 18:49:22 +01:00
if ( ! $error && ( is_object ( $this -> oldcopy ) && $this -> oldcopy -> ref !== $this -> ref )) {
2017-10-07 13:09:31 +02:00
// We remove directory
2022-06-14 17:53:17 +02:00
if ( $conf -> project -> dir_output ) {
2017-10-07 13:09:31 +02:00
$project = new Project ( $this -> db );
$project -> fetch ( $this -> fk_project );
2022-06-14 17:53:17 +02:00
$olddir = $conf -> project -> dir_output . '/' . dol_sanitizeFileName ( $project -> ref ) . '/' . dol_sanitizeFileName ( $this -> oldcopy -> ref );
$newdir = $conf -> project -> dir_output . '/' . dol_sanitizeFileName ( $project -> ref ) . '/' . dol_sanitizeFileName ( $this -> ref );
2021-02-26 18:49:22 +01:00
if ( file_exists ( $olddir )) {
2019-11-13 18:32:11 +01:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
2022-06-01 15:17:19 +02:00
$res = dol_move_dir ( $olddir , $newdir );
2021-02-26 18:49:22 +01:00
if ( ! $res ) {
2017-10-07 13:09:31 +02:00
$langs -> load ( " errors " );
2019-11-13 18:32:11 +01:00
$this -> error = $langs -> trans ( 'ErrorFailToRenameDir' , $olddir , $newdir );
2017-10-07 13:09:31 +02:00
$error ++ ;
}
}
}
}
// Commit or rollback
2021-02-26 18:49:22 +01:00
if ( $error ) {
foreach ( $this -> errors as $errmsg ) {
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::update " . $errmsg , LOG_ERR );
2019-11-13 18:32:11 +01:00
$this -> error .= ( $this -> error ? ', ' . $errmsg : $errmsg );
2017-10-07 13:09:31 +02:00
}
$this -> db -> rollback ();
2019-11-13 18:32:11 +01:00
return - 1 * $error ;
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
$this -> db -> commit ();
return 1 ;
}
}
/**
* Delete task from database
*
* @ param User $user User that delete
* @ param int $notrigger 0 = launch triggers after , 1 = disable triggers
2023-12-01 19:51:32 +01:00
* @ return int Return integer < 0 if KO , > 0 if OK
2017-10-07 13:09:31 +02:00
*/
2019-02-24 23:32:09 +01:00
public function delete ( $user , $notrigger = 0 )
2017-10-07 13:09:31 +02:00
{
2024-02-20 01:17:00 +01:00
global $conf ;
2019-11-13 18:32:11 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
2017-10-07 13:09:31 +02:00
2019-11-13 18:32:11 +01:00
$error = 0 ;
2017-10-07 13:09:31 +02:00
$this -> db -> begin ();
2021-02-26 18:49:22 +01:00
if ( $this -> hasChildren () > 0 ) {
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::delete Can't delete record as it has some sub tasks " , LOG_WARNING );
2019-11-13 18:32:11 +01:00
$this -> error = 'ErrorRecordHasSubTasks' ;
2017-10-07 13:09:31 +02:00
$this -> db -> rollback ();
return 0 ;
}
$objectisused = $this -> isObjectUsed ( $this -> id );
2021-02-26 18:49:22 +01:00
if ( ! empty ( $objectisused )) {
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::delete Can't delete record as it has some child " , LOG_WARNING );
2019-11-13 18:32:11 +01:00
$this -> error = 'ErrorRecordHasChildren' ;
2017-10-07 13:09:31 +02:00
$this -> db -> rollback ();
return 0 ;
}
2021-02-26 18:49:22 +01:00
if ( ! $error ) {
2017-10-07 13:09:31 +02:00
// Delete linked contacts
$res = $this -> delete_linked_contact ();
2021-02-26 18:49:22 +01:00
if ( $res < 0 ) {
2019-11-13 18:32:11 +01:00
$this -> error = 'ErrorFailToDeleteLinkedContact' ;
2017-10-07 13:09:31 +02:00
//$error++;
$this -> db -> rollback ();
return 0 ;
}
}
2021-02-26 18:49:22 +01:00
if ( ! $error ) {
2023-03-08 11:00:58 +01:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " element_time " ;
$sql .= " WHERE fk_element = " . (( int ) $this -> id ) . " AND elementtype = 'task' " ;
2017-10-07 13:09:31 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-26 18:49:22 +01:00
if ( ! $resql ) {
2023-12-04 13:49:31 +01:00
$error ++ ;
$this -> errors [] = " Error " . $this -> db -> lasterror ();
2021-02-26 18:49:22 +01:00
}
2017-10-07 13:09:31 +02:00
}
2021-02-26 18:49:22 +01:00
if ( ! $error ) {
2017-10-07 13:09:31 +02:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " projet_task_extrafields " ;
2021-08-27 18:18:50 +02:00
$sql .= " WHERE fk_object = " . (( int ) $this -> id );
2017-10-07 13:09:31 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-26 18:49:22 +01:00
if ( ! $resql ) {
2023-12-04 13:49:31 +01:00
$error ++ ;
$this -> errors [] = " Error " . $this -> db -> lasterror ();
2021-02-26 18:49:22 +01:00
}
2017-10-07 13:09:31 +02:00
}
2021-02-26 18:49:22 +01:00
if ( ! $error ) {
2017-10-07 13:09:31 +02:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " projet_task " ;
2021-03-14 12:20:23 +01:00
$sql .= " WHERE rowid= " . (( int ) $this -> id );
2017-10-07 13:09:31 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-26 18:49:22 +01:00
if ( ! $resql ) {
2023-12-04 13:49:31 +01:00
$error ++ ;
$this -> errors [] = " Error " . $this -> db -> lasterror ();
2021-02-26 18:49:22 +01:00
}
2017-10-07 13:09:31 +02:00
}
2021-02-26 18:49:22 +01:00
if ( ! $error ) {
if ( ! $notrigger ) {
2017-10-07 13:09:31 +02:00
// Call trigger
2019-11-13 18:32:11 +01:00
$result = $this -> call_trigger ( 'TASK_DELETE' , $user );
2021-02-26 18:49:22 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2017-10-07 13:09:31 +02:00
// End call triggers
}
}
// Commit or rollback
2021-02-26 18:49:22 +01:00
if ( $error ) {
foreach ( $this -> errors as $errmsg ) {
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::delete " . $errmsg , LOG_ERR );
2019-11-13 18:32:11 +01:00
$this -> error .= ( $this -> error ? ', ' . $errmsg : $errmsg );
2017-10-07 13:09:31 +02:00
}
$this -> db -> rollback ();
2019-11-13 18:32:11 +01:00
return - 1 * $error ;
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
//Delete associated link file
2022-06-14 17:53:17 +02:00
if ( $conf -> project -> dir_output ) {
2019-11-13 18:32:11 +01:00
$projectstatic = new Project ( $this -> db );
2017-10-07 13:09:31 +02:00
$projectstatic -> fetch ( $this -> fk_project );
2022-06-14 17:53:17 +02:00
$dir = $conf -> project -> dir_output . " / " . dol_sanitizeFileName ( $projectstatic -> ref ) . '/' . dol_sanitizeFileName ( $this -> id );
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::delete dir= " . $dir , LOG_DEBUG );
2021-02-26 18:49:22 +01:00
if ( file_exists ( $dir )) {
2019-11-13 18:32:11 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
2017-10-07 13:09:31 +02:00
$res = @ dol_delete_dir_recursive ( $dir );
2021-02-26 18:49:22 +01:00
if ( ! $res ) {
2017-10-07 13:09:31 +02:00
$this -> error = 'ErrorFailToDeleteDir' ;
$this -> db -> rollback ();
return 0 ;
}
}
}
$this -> db -> commit ();
return 1 ;
}
}
/**
* Return nb of children
*
2023-12-06 15:46:39 +01:00
* @ return int Return integer < 0 if KO , 0 if no children , > 0 if OK
2017-10-07 13:09:31 +02:00
*/
2019-02-24 23:32:09 +01:00
public function hasChildren ()
2017-10-07 13:09:31 +02:00
{
2019-11-13 18:32:11 +01:00
$error = 0 ;
$ret = 0 ;
2017-10-07 13:09:31 +02:00
$sql = " SELECT COUNT(*) as nb " ;
2019-11-13 18:32:11 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " projet_task " ;
2021-08-27 18:18:50 +02:00
$sql .= " WHERE fk_task_parent = " . (( int ) $this -> id );
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::hasChildren " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
2021-02-26 18:49:22 +01:00
if ( ! $resql ) {
2023-12-04 13:49:31 +01:00
$error ++ ;
$this -> errors [] = " Error " . $this -> db -> lasterror ();
2021-02-26 18:49:22 +01:00
} else {
2019-11-13 18:32:11 +01:00
$obj = $this -> db -> fetch_object ( $resql );
2021-02-26 18:49:22 +01:00
if ( $obj ) {
$ret = $obj -> nb ;
}
2017-10-07 13:09:31 +02:00
$this -> db -> free ( $resql );
}
2021-02-26 18:49:22 +01:00
if ( ! $error ) {
2017-10-07 13:09:31 +02:00
return $ret ;
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
return - 1 ;
}
}
/**
* Return nb of time spent
*
2023-12-06 15:46:39 +01:00
* @ return int Return integer < 0 if KO , 0 if no children , > 0 if OK
2017-10-07 13:09:31 +02:00
*/
2019-02-24 23:32:09 +01:00
public function hasTimeSpent ()
2017-10-07 13:09:31 +02:00
{
2019-11-13 18:32:11 +01:00
$error = 0 ;
$ret = 0 ;
2017-10-07 13:09:31 +02:00
$sql = " SELECT COUNT(*) as nb " ;
2023-03-08 11:00:58 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " element_time " ;
$sql .= " WHERE fk_element = " . (( int ) $this -> id );
$sql .= " AND elementtype = 'task' " ;
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::hasTimeSpent " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
2021-02-26 18:49:22 +01:00
if ( ! $resql ) {
2023-12-04 13:49:31 +01:00
$error ++ ;
$this -> errors [] = " Error " . $this -> db -> lasterror ();
2021-02-26 18:49:22 +01:00
} else {
2019-11-13 18:32:11 +01:00
$obj = $this -> db -> fetch_object ( $resql );
2021-02-26 18:49:22 +01:00
if ( $obj ) {
$ret = $obj -> nb ;
}
2017-10-07 13:09:31 +02:00
$this -> db -> free ( $resql );
}
2021-02-26 18:49:22 +01:00
if ( ! $error ) {
2017-10-07 13:09:31 +02:00
return $ret ;
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
return - 1 ;
}
}
2023-02-07 14:07:27 +01:00
/**
* getTooltipContentArray
*
* @ param array $params ex option , infologin
* @ since v18
* @ return array
*/
public function getTooltipContentArray ( $params )
{
global $langs ;
$langs -> load ( 'projects' );
$datas = [];
$datas [ 'picto' ] = img_picto ( '' , $this -> picto ) . ' <u>' . $langs -> trans ( " Task " ) . '</u>' ;
if ( ! empty ( $this -> ref )) {
$datas [ 'ref' ] = '<br><b>' . $langs -> trans ( 'Ref' ) . ':</b> ' . $this -> ref ;
}
if ( ! empty ( $this -> label )) {
$datas [ 'label' ] = '<br><b>' . $langs -> trans ( 'LabelTask' ) . ':</b> ' . $this -> label ;
}
if ( $this -> date_start || $this -> date_end ) {
$datas [ 'range' ] = " <br> " . get_date_range ( $this -> date_start , $this -> date_end , '' , $langs , 0 );
}
return $datas ;
}
2017-10-07 13:09:31 +02:00
/**
* Return clicable name ( with picto eventually )
*
* @ param int $withpicto 0 = No picto , 1 = Include picto into link , 2 = Only picto
* @ param string $option 'withproject' or ''
* @ param string $mode Mode 'task' , 'time' , 'contact' , 'note' , document ' define page to link to .
* @ param int $addlabel 0 = Default , 1 = Add label into string , > 1 = Add first chars into string
* @ param string $sep Separator between ref and label if option addlabel is set
* @ param int $notooltip 1 = Disable tooltip
* @ param int $save_lastsearch_value - 1 = Auto , 0 = No save of lastsearch_values when clicking , 1 = Save lastsearch_values whenclicking
* @ return string Chaine avec URL
*/
2019-02-24 23:32:09 +01:00
public function getNomUrl ( $withpicto = 0 , $option = '' , $mode = 'task' , $addlabel = 0 , $sep = ' - ' , $notooltip = 0 , $save_lastsearch_value = - 1 )
2017-10-07 13:09:31 +02:00
{
2023-11-17 14:27:13 +01:00
global $action , $conf , $hookmanager , $langs ;
2017-10-07 13:09:31 +02:00
2021-02-26 18:49:22 +01:00
if ( ! empty ( $conf -> dol_no_mouse_hover )) {
$notooltip = 1 ; // Force disable tooltips
}
2017-10-07 13:09:31 +02:00
2019-11-13 18:32:11 +01:00
$result = '' ;
2023-02-07 14:07:27 +01:00
$params = [
'id' => $this -> id ,
'objecttype' => $this -> element ,
];
$classfortooltip = 'classfortooltip' ;
$dataparams = '' ;
if ( getDolGlobalInt ( 'MAIN_ENABLE_AJAX_TOOLTIP' )) {
$classfortooltip = 'classforajaxtooltip' ;
2023-04-03 19:51:40 +02:00
$dataparams = ' data-params="' . dol_escape_htmltag ( json_encode ( $params )) . '"' ;
$label = '' ;
} else {
$label = implode ( $this -> getTooltipContentArray ( $params ));
2023-02-07 14:07:27 +01:00
}
2017-10-07 13:09:31 +02:00
2019-11-13 18:32:11 +01:00
$url = DOL_URL_ROOT . '/projet/tasks/' . $mode . '.php?id=' . $this -> id . ( $option == 'withproject' ? '&withproject=1' : '' );
2017-10-07 13:09:31 +02:00
// Add param to save lastsearch_values or not
2019-11-13 18:32:11 +01:00
$add_save_lastsearch_values = ( $save_lastsearch_value == 1 ? 1 : 0 );
2023-09-10 17:41:22 +02:00
if ( $save_lastsearch_value == - 1 && isset ( $_SERVER [ " PHP_SELF " ]) && preg_match ( '/list\.php/' , $_SERVER [ " PHP_SELF " ])) {
2021-02-26 18:49:22 +01:00
$add_save_lastsearch_values = 1 ;
}
if ( $add_save_lastsearch_values ) {
$url .= '&save_lastsearch_values=1' ;
}
2017-10-07 13:09:31 +02:00
$linkclose = '' ;
2021-02-26 18:49:22 +01:00
if ( empty ( $notooltip )) {
2023-11-27 11:56:32 +01:00
if ( getDolGlobalString ( 'MAIN_OPTIMIZEFORTEXTBROWSER' )) {
2019-11-13 18:32:11 +01:00
$label = $langs -> trans ( " ShowTask " );
$linkclose .= ' alt="' . dol_escape_htmltag ( $label , 1 ) . '"' ;
2017-10-07 13:09:31 +02:00
}
2023-12-04 13:49:31 +01:00
$linkclose .= ( $label ? ' title="' . dol_escape_htmltag ( $label , 1 ) . '"' : ' title="tocomplete"' );
2023-04-03 19:51:40 +02:00
$linkclose .= $dataparams . ' class="' . $classfortooltip . ' nowraponall"' ;
2020-11-21 23:36:20 +01:00
} else {
$linkclose .= ' class="nowraponall"' ;
2017-10-07 13:09:31 +02:00
}
$linkstart = '<a href="' . $url . '"' ;
2019-11-13 18:32:11 +01:00
$linkstart .= $linkclose . '>' ;
$linkend = '</a>' ;
2017-10-07 13:09:31 +02:00
2019-11-13 18:32:11 +01:00
$picto = 'projecttask' ;
2017-10-07 13:09:31 +02:00
2017-11-02 15:03:09 +01:00
$result .= $linkstart ;
2021-02-26 18:49:22 +01:00
if ( $withpicto ) {
2023-04-10 01:39:47 +02:00
$result .= img_object (( $notooltip ? '' : $label ), $picto , 'class="paddingright"' , 0 , 0 , $notooltip ? 0 : 1 );
2021-02-26 18:49:22 +01:00
}
if ( $withpicto != 2 ) {
$result .= $this -> ref ;
}
2017-11-02 15:03:09 +01:00
$result .= $linkend ;
2021-02-26 18:49:22 +01:00
if ( $withpicto != 2 ) {
$result .= (( $addlabel && $this -> label ) ? $sep . dol_trunc ( $this -> label , ( $addlabel > 1 ? $addlabel : 0 )) : '' );
}
2017-11-02 15:03:09 +01:00
2024-03-11 22:21:08 +01:00
$parameters = array ( 'id' => $this -> id , 'getnomurl' => & $result );
2023-11-17 14:27:13 +01:00
$reshook = $hookmanager -> executeHooks ( 'getNomUrl' , $parameters , $this , $action ); // Note that $action and $object may have been modified by some hooks
if ( $reshook > 0 ) {
$result = $hookmanager -> resPrint ;
} else {
$result .= $hookmanager -> resPrint ;
}
2017-10-07 13:09:31 +02:00
return $result ;
}
/**
* Initialise an instance with random values .
* Used to build previews or test instances .
* id must be 0 if object instance is a specimen .
*
2024-03-02 16:38:35 +01:00
* @ return int
2017-10-07 13:09:31 +02:00
*/
2019-02-24 23:32:09 +01:00
public function initAsSpecimen ()
2017-10-07 13:09:31 +02:00
{
2019-11-13 18:32:11 +01:00
$this -> id = 0 ;
2024-02-25 22:14:55 +01:00
$this -> fk_project = 0 ;
2019-11-13 18:32:11 +01:00
$this -> ref = 'TK01' ;
2024-02-25 22:14:55 +01:00
$this -> fk_task_parent = 0 ;
2019-11-13 18:32:11 +01:00
$this -> label = 'Specimen task TK01' ;
$this -> duration_effective = '' ;
2024-02-25 22:14:55 +01:00
$this -> fk_user_creat = 1 ;
2019-11-13 18:32:11 +01:00
$this -> progress = '25' ;
2024-02-25 22:14:55 +01:00
$this -> status = 0 ;
2019-11-13 18:32:11 +01:00
$this -> note = 'This is a specimen task not' ;
2024-03-02 16:38:35 +01:00
return 1 ;
2017-10-07 13:09:31 +02:00
}
/**
* Return list of tasks for all projects or for one particular project
* Sort order is on project , then on position of task , and last on start date of first level task
*
2023-06-29 15:54:09 +02:00
* @ param User $usert Object user to limit tasks affected to a particular user
* @ param User $userp Object user to limit projects of a particular user and public projects
* @ param int $projectid Project id
* @ param int $socid Third party id
* @ param int $mode 0 = Return list of tasks and their projects , 1 = Return projects and tasks if exists
* @ param string $filteronproj Filter on project ref or label
* @ param string $filteronprojstatus Filter on project status ( '-1' = no filter , '0,1' = Draft + Validated only )
* @ param string $morewherefilter Add more filter into where SQL request ( must start with ' AND ...' )
2024-03-15 14:13:38 +01:00
* @ param int $filteronprojuser Filter on user that is a contact of project
* @ param int $filterontaskuser Filter on user assigned to task
* @ param ? Extrafields $extrafields Show additional column from project or task
2023-06-29 15:54:09 +02:00
* @ param int $includebilltime Calculate also the time to bill and billed
* @ param array $search_array_options Array of search filters . Not Used yet .
2023-06-29 16:30:34 +02:00
* @ param int $loadextras Fetch all Extrafields on each project and task
2023-06-29 15:54:09 +02:00
* @ param int $loadRoleMode 1 = will test Roles on task ; 0 used in delete project action
* @ param string $sortfield Sort field
* @ param string $sortorder Sort order
* @ return array | string Array of tasks
2017-10-07 13:09:31 +02:00
*/
2024-03-15 14:13:38 +01:00
public function getTasksArray ( $usert = null , $userp = null , $projectid = 0 , $socid = 0 , $mode = 0 , $filteronproj = '' , $filteronprojstatus = '-1' , $morewherefilter = '' , $filteronprojuser = 0 , $filterontaskuser = 0 , $extrafields = null , $includebilltime = 0 , $search_array_options = array (), $loadextras = 0 , $loadRoleMode = 1 , $sortfield = '' , $sortorder = '' )
2017-10-07 13:09:31 +02:00
{
2024-03-15 14:13:38 +01:00
global $hookmanager ;
2017-10-07 13:09:31 +02:00
$tasks = array ();
//print $usert.'-'.$userp.'-'.$projectid.'-'.$socid.'-'.$mode.'<br>';
// List of tasks (does not care about permissions. Filtering will be done later)
$sql = " SELECT " ;
2021-02-26 18:49:22 +01:00
if ( $filteronprojuser > 0 || $filterontaskuser > 0 ) {
$sql .= " DISTINCT " ; // We may get several time the same record if user has several roles on same project/task
}
2019-11-13 18:32:11 +01:00
$sql .= " p.rowid as projectid, p.ref, p.title as plabel, p.public, p.fk_statut as projectstatus, p.usage_bill_time, " ;
$sql .= " t.rowid as taskid, t.ref as taskref, t.label, t.description, t.fk_task_parent, t.duration_effective, t.progress, t.fk_statut as status, " ;
$sql .= " t.dateo as date_start, t.datee as date_end, t.planned_workload, t.rang, " ;
2020-03-24 19:50:01 +01:00
$sql .= " t.description, " ;
2021-10-15 10:24:56 +02:00
$sql .= " t.budget_amount, " ;
2019-11-13 18:32:11 +01:00
$sql .= " s.rowid as thirdparty_id, s.nom as thirdparty_name, s.email as thirdparty_email, " ;
2021-10-15 10:24:56 +02:00
$sql .= " p.fk_opp_status, p.opp_amount, p.opp_percent, p.budget_amount as project_budget_amount " ;
2023-06-29 16:30:34 +02:00
if ( $loadextras ) { // TODO Replace this with a fetch_optionnal() on the project after the fetch_object of line.
if ( ! empty ( $extrafields -> attributes [ 'projet' ][ 'label' ])) {
foreach ( $extrafields -> attributes [ 'projet' ][ 'label' ] as $key => $val ) {
$sql .= ( $extrafields -> attributes [ 'projet' ][ 'type' ][ $key ] != 'separate' ? " ,efp. " . $key . " as options_ " . $key : '' );
}
2021-02-26 18:49:22 +01:00
}
2023-11-17 16:39:49 +01:00
if ( ! empty ( $extrafields -> attributes [ 'projet_task' ][ 'label' ])) {
foreach ( $extrafields -> attributes [ 'projet_task' ][ 'label' ] as $key => $val ) {
$sql .= ( $extrafields -> attributes [ 'projet_task' ][ 'type' ][ $key ] != 'separate' ? " ,efpt. " . $key . " as options_ " . $key : '' );
}
}
2019-10-01 13:47:12 +02:00
}
2021-02-26 18:49:22 +01:00
if ( $includebilltime ) {
2023-03-08 11:00:58 +01:00
$sql .= " , SUM(tt.element_duration * " . $this -> db -> ifsql ( " invoice_id IS NULL " , " 1 " , " 0 " ) . " ) as tobill, SUM(tt.element_duration * " . $this -> db -> ifsql ( " invoice_id IS NULL " , " 0 " , " 1 " ) . " ) as billed " ;
2019-03-29 14:34:55 +01:00
}
2019-03-31 19:12:27 +02:00
2019-11-13 18:32:11 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " projet as p " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " societe as s ON p.fk_soc = s.rowid " ;
2023-06-29 16:30:34 +02:00
if ( $loadextras ) {
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " projet_extrafields as efp ON (p.rowid = efp.fk_object) " ;
}
2019-03-21 14:21:29 +01:00
2021-02-26 18:49:22 +01:00
if ( $mode == 0 ) {
if ( $filteronprojuser > 0 ) {
2019-11-13 18:32:11 +01:00
$sql .= " , " . MAIN_DB_PREFIX . " element_contact as ec " ;
$sql .= " , " . MAIN_DB_PREFIX . " c_type_contact as ctc " ;
2017-10-07 13:09:31 +02:00
}
2019-11-13 18:32:11 +01:00
$sql .= " , " . MAIN_DB_PREFIX . " projet_task as t " ;
2023-11-17 16:39:49 +01:00
if ( $loadextras ) {
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " projet_task_extrafields as efpt ON (t.rowid = efpt.fk_object) " ;
}
2021-02-26 18:49:22 +01:00
if ( $includebilltime ) {
2023-03-08 11:00:58 +01:00
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " element_time as tt ON (tt.fk_element = t.rowid AND tt.elementtype='task') " ;
2019-03-29 14:34:55 +01:00
}
2021-02-26 18:49:22 +01:00
if ( $filterontaskuser > 0 ) {
2019-11-13 18:32:11 +01:00
$sql .= " , " . MAIN_DB_PREFIX . " element_contact as ec2 " ;
$sql .= " , " . MAIN_DB_PREFIX . " c_type_contact as ctc2 " ;
2017-10-07 13:09:31 +02:00
}
2020-09-07 10:18:17 +02:00
$sql .= " WHERE p.entity IN ( " . getEntity ( 'project' ) . " ) " ;
2019-11-13 18:32:11 +01:00
$sql .= " AND t.fk_projet = p.rowid " ;
2021-02-26 18:49:22 +01:00
} elseif ( $mode == 1 ) {
if ( $filteronprojuser > 0 ) {
2019-11-13 18:32:11 +01:00
$sql .= " , " . MAIN_DB_PREFIX . " element_contact as ec " ;
$sql .= " , " . MAIN_DB_PREFIX . " c_type_contact as ctc " ;
2017-10-07 13:09:31 +02:00
}
2021-02-26 18:49:22 +01:00
if ( $filterontaskuser > 0 ) {
2019-11-13 18:32:11 +01:00
$sql .= " , " . MAIN_DB_PREFIX . " projet_task as t " ;
2021-02-26 18:49:22 +01:00
if ( $includebilltime ) {
2023-03-08 11:00:58 +01:00
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " element_time as tt ON (tt.fk_element = t.rowid AND tt.elementtype='task') " ;
2019-03-29 14:34:55 +01:00
}
2019-11-13 18:32:11 +01:00
$sql .= " , " . MAIN_DB_PREFIX . " element_contact as ec2 " ;
$sql .= " , " . MAIN_DB_PREFIX . " c_type_contact as ctc2 " ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 18:32:11 +01:00
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " projet_task as t on t.fk_projet = p.rowid " ;
2021-02-26 18:49:22 +01:00
if ( $includebilltime ) {
2023-03-08 11:00:58 +01:00
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " element_time as tt ON (tt.fk_element = t.rowid AND tt.elementtype = 'task') " ;
2019-03-29 14:34:55 +01:00
}
2017-10-07 13:09:31 +02:00
}
2020-09-07 10:18:17 +02:00
$sql .= " WHERE p.entity IN ( " . getEntity ( 'project' ) . " ) " ;
2021-02-26 18:49:22 +01:00
} else {
return 'BadValueForParameterMode' ;
}
2017-10-07 13:09:31 +02:00
2021-02-26 18:49:22 +01:00
if ( $filteronprojuser > 0 ) {
2019-11-13 18:32:11 +01:00
$sql .= " AND p.rowid = ec.element_id " ;
$sql .= " AND ctc.rowid = ec.fk_c_type_contact " ;
$sql .= " AND ctc.element = 'project' " ;
2021-06-09 15:36:47 +02:00
$sql .= " AND ec.fk_socpeople = " . (( int ) $filteronprojuser );
2019-11-13 18:32:11 +01:00
$sql .= " AND ec.statut = 4 " ;
$sql .= " AND ctc.source = 'internal' " ;
2017-10-07 13:09:31 +02:00
}
2021-02-26 18:49:22 +01:00
if ( $filterontaskuser > 0 ) {
2019-11-13 18:32:11 +01:00
$sql .= " AND t.fk_projet = p.rowid " ;
$sql .= " AND p.rowid = ec2.element_id " ;
$sql .= " AND ctc2.rowid = ec2.fk_c_type_contact " ;
$sql .= " AND ctc2.element = 'project_task' " ;
2021-06-09 15:36:47 +02:00
$sql .= " AND ec2.fk_socpeople = " . (( int ) $filterontaskuser );
2019-11-13 18:32:11 +01:00
$sql .= " AND ec2.statut = 4 " ;
$sql .= " AND ctc2.source = 'internal' " ;
}
2021-02-26 18:49:22 +01:00
if ( $socid ) {
2021-06-09 15:36:47 +02:00
$sql .= " AND p.fk_soc = " . (( int ) $socid );
2021-02-26 18:49:22 +01:00
}
if ( $projectid ) {
2021-03-22 12:47:23 +01:00
$sql .= " AND p.rowid IN ( " . $this -> db -> sanitize ( $projectid ) . " ) " ;
2021-02-26 18:49:22 +01:00
}
if ( $filteronproj ) {
$sql .= natural_search ( array ( " p.ref " , " p.title " ), $filteronproj );
}
2024-03-15 14:13:38 +01:00
if ( $filteronprojstatus && ( int ) $filteronprojstatus != '-1' ) {
2021-03-22 12:47:23 +01:00
$sql .= " AND p.fk_statut IN ( " . $this -> db -> sanitize ( $filteronprojstatus ) . " ) " ;
2021-02-26 18:49:22 +01:00
}
if ( $morewherefilter ) {
$sql .= $morewherefilter ;
}
2024-03-16 23:22:12 +01:00
2020-03-24 19:50:01 +01:00
// Add where from extra fields
2020-04-10 10:59:32 +02:00
$extrafieldsobjectkey = 'projet_task' ;
$extrafieldsobjectprefix = 'efpt.' ;
2024-03-16 23:22:12 +01:00
global $db , $conf ; // needed for extrafields_list_search_sql.tpl
2020-03-24 19:50:01 +01:00
include DOL_DOCUMENT_ROOT . '/core/tpl/extrafields_list_search_sql.tpl.php' ;
2024-03-16 23:22:12 +01:00
2020-03-24 19:50:01 +01:00
// Add where from hooks
$parameters = array ();
$reshook = $hookmanager -> executeHooks ( 'printFieldListWhere' , $parameters ); // Note that $action and $object may have been modified by hook
$sql .= $hookmanager -> resPrint ;
2021-02-26 18:49:22 +01:00
if ( $includebilltime ) {
2020-09-07 10:18:17 +02:00
$sql .= " GROUP BY p.rowid, p.ref, p.title, p.public, p.fk_statut, p.usage_bill_time, " ;
$sql .= " t.datec, t.dateo, t.datee, t.tms, " ;
2024-02-19 16:38:40 +01:00
$sql .= " t.rowid, t.ref, t.label, t.description, t.fk_task_parent, t.duration_effective, t.progress, t.fk_statut, " ;
2020-09-07 10:18:17 +02:00
$sql .= " t.dateo, t.datee, t.planned_workload, t.rang, " ;
$sql .= " t.description, " ;
2021-10-15 10:24:56 +02:00
$sql .= " t.budget_amount, " ;
2020-09-07 10:18:17 +02:00
$sql .= " s.rowid, s.nom, s.email, " ;
2019-11-13 18:32:11 +01:00
$sql .= " p.fk_opp_status, p.opp_amount, p.opp_percent, p.budget_amount " ;
2023-06-29 16:30:34 +02:00
if ( $loadextras ) {
if ( ! empty ( $extrafields -> attributes [ 'projet' ][ 'label' ])) {
foreach ( $extrafields -> attributes [ 'projet' ][ 'label' ] as $key => $val ) {
$sql .= ( $extrafields -> attributes [ 'projet' ][ 'type' ][ $key ] != 'separate' ? " ,efp. " . $key : '' );
}
2021-02-26 18:49:22 +01:00
}
2023-11-17 16:39:49 +01:00
if ( ! empty ( $extrafields -> attributes [ 'projet_task' ][ 'label' ])) {
foreach ( $extrafields -> attributes [ 'projet_task' ][ 'label' ] as $key => $val ) {
$sql .= ( $extrafields -> attributes [ 'projet_task' ][ 'type' ][ $key ] != 'separate' ? " ,efpt. " . $key : '' );
}
}
2019-10-20 11:59:21 +02:00
}
2019-03-29 14:34:55 +01:00
}
2023-02-23 08:33:16 +01:00
if ( $sortfield && $sortorder ) {
2023-02-23 08:30:31 +01:00
$sql .= $this -> db -> order ( $sortfield , $sortorder );
} else {
$sql .= " ORDER BY p.ref, t.rang, t.dateo " ;
}
2017-10-07 13:09:31 +02:00
//print $sql;exit;
dol_syslog ( get_class ( $this ) . " ::getTasksArray " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
2021-02-26 18:49:22 +01:00
if ( $resql ) {
2017-10-07 13:09:31 +02:00
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
// Loop on each record found, so each couple (project id, task id)
2021-02-26 18:49:22 +01:00
while ( $i < $num ) {
2019-11-13 18:32:11 +01:00
$error = 0 ;
2017-10-07 13:09:31 +02:00
$obj = $this -> db -> fetch_object ( $resql );
2022-10-18 11:36:17 +02:00
if ( $loadRoleMode ) {
if (( ! $obj -> public ) && ( is_object ( $userp ))) { // If not public project and we ask a filter on project owned by a user
2022-12-28 11:41:59 +01:00
if ( ! $this -> getUserRolesForProjectsOrTasks ( $userp , null , $obj -> projectid , 0 )) {
2022-10-18 11:36:17 +02:00
$error ++ ;
}
2017-10-07 13:09:31 +02:00
}
2022-10-18 11:36:17 +02:00
if ( is_object ( $usert )) { // If we ask a filter on a user affected to a task
2022-12-28 11:41:59 +01:00
if ( ! $this -> getUserRolesForProjectsOrTasks ( null , $usert , $obj -> projectid , $obj -> taskid )) {
2022-10-18 11:36:17 +02:00
$error ++ ;
}
2017-10-07 13:09:31 +02:00
}
}
2021-02-26 18:49:22 +01:00
if ( ! $error ) {
2017-10-07 13:09:31 +02:00
$tasks [ $i ] = new Task ( $this -> db );
2019-11-13 18:32:11 +01:00
$tasks [ $i ] -> id = $obj -> taskid ;
$tasks [ $i ] -> ref = $obj -> taskref ;
2023-06-29 15:54:09 +02:00
$tasks [ $i ] -> fk_project = $obj -> projectid ;
2023-06-29 16:30:34 +02:00
// Data from project
2023-06-29 15:54:09 +02:00
$tasks [ $i ] -> projectref = $obj -> ref ;
2019-11-13 18:32:11 +01:00
$tasks [ $i ] -> projectlabel = $obj -> plabel ;
$tasks [ $i ] -> projectstatus = $obj -> projectstatus ;
2020-11-28 00:59:07 +01:00
$tasks [ $i ] -> fk_opp_status = $obj -> fk_opp_status ;
$tasks [ $i ] -> opp_amount = $obj -> opp_amount ;
$tasks [ $i ] -> opp_percent = $obj -> opp_percent ;
$tasks [ $i ] -> budget_amount = $obj -> budget_amount ;
2021-10-15 10:24:56 +02:00
$tasks [ $i ] -> project_budget_amount = $obj -> project_budget_amount ;
2019-11-13 18:32:11 +01:00
$tasks [ $i ] -> usage_bill_time = $obj -> usage_bill_time ;
2020-11-28 00:59:07 +01:00
2019-11-13 18:32:11 +01:00
$tasks [ $i ] -> label = $obj -> label ;
$tasks [ $i ] -> description = $obj -> description ;
$tasks [ $i ] -> fk_task_parent = $obj -> fk_task_parent ;
2023-06-29 15:54:09 +02:00
$tasks [ $i ] -> duration_effective = $obj -> duration_effective ;
2019-11-13 18:32:11 +01:00
$tasks [ $i ] -> planned_workload = $obj -> planned_workload ;
2019-03-29 14:34:55 +01:00
2020-11-28 00:59:07 +01:00
if ( $includebilltime ) {
2023-06-29 16:30:34 +02:00
// Data summed from element_time linked to task
2020-12-01 02:41:19 +01:00
$tasks [ $i ] -> tobill = $obj -> tobill ;
2020-11-28 00:59:07 +01:00
$tasks [ $i ] -> billed = $obj -> billed ;
}
2019-03-29 14:34:55 +01:00
2017-10-07 13:09:31 +02:00
$tasks [ $i ] -> progress = $obj -> progress ;
2024-02-16 13:23:23 +01:00
$tasks [ $i ] -> fk_statut = $obj -> status ;
$tasks [ $i ] -> status = $obj -> status ;
2019-11-13 18:32:11 +01:00
$tasks [ $i ] -> public = $obj -> public ;
$tasks [ $i ] -> date_start = $this -> db -> jdate ( $obj -> date_start );
2017-10-07 13:09:31 +02:00
$tasks [ $i ] -> date_end = $this -> db -> jdate ( $obj -> date_end );
$tasks [ $i ] -> rang = $obj -> rang ;
2019-11-13 18:32:11 +01:00
$tasks [ $i ] -> socid = $obj -> thirdparty_id ; // For backward compatibility
$tasks [ $i ] -> thirdparty_id = $obj -> thirdparty_id ;
2017-10-07 13:09:31 +02:00
$tasks [ $i ] -> thirdparty_name = $obj -> thirdparty_name ;
2019-11-13 18:32:11 +01:00
$tasks [ $i ] -> thirdparty_email = $obj -> thirdparty_email ;
2019-03-21 14:21:29 +01:00
2023-06-29 16:30:34 +02:00
if ( $loadextras ) {
if ( ! empty ( $extrafields -> attributes [ 'projet' ][ 'label' ])) {
foreach ( $extrafields -> attributes [ 'projet' ][ 'label' ] as $key => $val ) {
if ( $extrafields -> attributes [ 'projet' ][ 'type' ][ $key ] != 'separate' ) {
$tmpvar = 'options_' . $key ;
$tasks [ $i ] -> array_options_project [ 'options_' . $key ] = $obj -> $tmpvar ;
}
2021-02-26 18:49:22 +01:00
}
2020-09-07 10:18:17 +02:00
}
}
2022-01-25 09:17:30 +01:00
if ( $loadextras ) {
$tasks [ $i ] -> fetch_optionals ();
}
2017-10-07 13:09:31 +02:00
}
$i ++ ;
}
$this -> db -> free ( $resql );
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
dol_print_error ( $this -> db );
}
return $tasks ;
}
/**
* Return list of roles for a user for each projects or each tasks ( or a particular project or a particular task ) .
*
2022-12-28 11:41:59 +01:00
* @ param User | null $userp Return roles on project for this internal user . If set , usert and taskid must not be defined .
* @ param User | null $usert Return roles on task for this internal user . If set userp must NOT be defined . - 1 means no filter .
2024-03-09 14:48:06 +01:00
* @ param string $projectid Project id list separated with , to filter on project
2022-12-28 11:41:59 +01:00
* @ param int $taskid Task id to filter on a task
* @ param integer $filteronprojstatus Filter on project status if userp is set . Not used if userp not defined .
2023-01-04 11:36:46 +01:00
* @ return array | int Array ( projectid => 'list of roles for project' or taskid => 'list of roles for task' )
2017-10-07 13:09:31 +02:00
*/
2019-02-24 23:32:09 +01:00
public function getUserRolesForProjectsOrTasks ( $userp , $usert , $projectid = '' , $taskid = 0 , $filteronprojstatus = - 1 )
2017-10-07 13:09:31 +02:00
{
$arrayroles = array ();
2024-03-16 23:38:50 +01:00
dol_syslog ( get_class ( $this ) . " ::getUserRolesForProjectsOrTasks userp= " . json_encode ( is_object ( $userp )) . " usert= " . json_encode ( is_object ( $usert )) . " projectid= " . $projectid . " taskid= " . $taskid );
2017-10-07 13:09:31 +02:00
// We want role of user for a projet or role of user for a task. Both are not possible.
2021-02-26 18:49:22 +01:00
if ( empty ( $userp ) && empty ( $usert )) {
2019-11-13 18:32:11 +01:00
$this -> error = " CallWithWrongParameters " ;
2017-10-07 13:09:31 +02:00
return - 1 ;
}
2021-02-26 18:49:22 +01:00
if ( ! empty ( $userp ) && ! empty ( $usert )) {
2019-11-13 18:32:11 +01:00
$this -> error = " CallWithWrongParameters " ;
2017-10-07 13:09:31 +02:00
return - 1 ;
}
2024-01-12 17:55:52 +01:00
/* Liste des taches et role sur les projects ou taches */
2024-02-20 00:41:31 +01:00
$sql = " SELECT " ;
if ( $userp ) {
$sql .= " p.rowid as pid, " ;
} else {
$sql .= " pt.rowid as pid, " ;
}
$sql .= " ec.element_id, ctc.code, ctc.source " ;
2021-02-26 18:49:22 +01:00
if ( $userp ) {
2024-02-20 00:41:31 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " projet as p " ;
2021-02-26 18:49:22 +01:00
}
if ( $usert && $filteronprojstatus > - 1 ) {
$sql .= " FROM " . MAIN_DB_PREFIX . " projet as p, " . MAIN_DB_PREFIX . " projet_task as pt " ;
}
if ( $usert && $filteronprojstatus <= - 1 ) {
$sql .= " FROM " . MAIN_DB_PREFIX . " projet_task as pt " ;
}
2019-11-13 18:32:11 +01:00
$sql .= " , " . MAIN_DB_PREFIX . " element_contact as ec " ;
$sql .= " , " . MAIN_DB_PREFIX . " c_type_contact as ctc " ;
2024-02-20 00:41:31 +01:00
if ( $userp ) {
$sql .= " WHERE p.rowid = ec.element_id " ;
} else {
$sql .= " WHERE pt.rowid = ec.element_id " ;
}
2021-02-26 18:49:22 +01:00
if ( $userp && $filteronprojstatus > - 1 ) {
2024-02-20 01:17:00 +01:00
$sql .= " AND p.fk_statut = " . (( int ) $filteronprojstatus );
2021-02-26 18:49:22 +01:00
}
if ( $usert && $filteronprojstatus > - 1 ) {
2021-08-23 18:56:46 +02:00
$sql .= " AND pt.fk_projet = p.rowid AND p.fk_statut = " . (( int ) $filteronprojstatus );
2021-02-26 18:49:22 +01:00
}
if ( $userp ) {
$sql .= " AND ctc.element = 'project' " ;
}
if ( $usert ) {
$sql .= " AND ctc.element = 'project_task' " ;
}
2019-11-13 18:32:11 +01:00
$sql .= " AND ctc.rowid = ec.fk_c_type_contact " ;
2021-02-26 18:49:22 +01:00
if ( $userp ) {
2021-08-23 18:56:46 +02:00
$sql .= " AND ec.fk_socpeople = " . (( int ) $userp -> id );
2021-02-26 18:49:22 +01:00
}
if ( $usert ) {
2021-08-23 18:56:46 +02:00
$sql .= " AND ec.fk_socpeople = " . (( int ) $usert -> id );
2021-02-26 18:49:22 +01:00
}
2019-11-13 18:32:11 +01:00
$sql .= " AND ec.statut = 4 " ;
$sql .= " AND ctc.source = 'internal' " ;
2021-02-26 18:49:22 +01:00
if ( $projectid ) {
if ( $userp ) {
2024-02-20 00:41:31 +01:00
$sql .= " AND p.rowid IN ( " . $this -> db -> sanitize ( $projectid ) . " ) " ;
2021-02-26 18:49:22 +01:00
}
if ( $usert ) {
2021-03-22 12:47:23 +01:00
$sql .= " AND pt.fk_projet IN ( " . $this -> db -> sanitize ( $projectid ) . " ) " ;
2021-02-26 18:49:22 +01:00
}
2017-10-07 13:09:31 +02:00
}
2021-02-26 18:49:22 +01:00
if ( $taskid ) {
if ( $userp ) {
$sql .= " ERROR SHOULD NOT HAPPENS " ;
}
if ( $usert ) {
2021-03-22 12:47:23 +01:00
$sql .= " AND pt.rowid = " . (( int ) $taskid );
2021-02-26 18:49:22 +01:00
}
2017-10-07 13:09:31 +02:00
}
//print $sql;
dol_syslog ( get_class ( $this ) . " ::getUserRolesForProjectsOrTasks execute request " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
2021-02-26 18:49:22 +01:00
if ( $resql ) {
2017-10-07 13:09:31 +02:00
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
2021-02-26 18:49:22 +01:00
while ( $i < $num ) {
2017-10-07 13:09:31 +02:00
$obj = $this -> db -> fetch_object ( $resql );
2021-02-26 18:49:22 +01:00
if ( empty ( $arrayroles [ $obj -> pid ])) {
$arrayroles [ $obj -> pid ] = $obj -> code ;
} else {
$arrayroles [ $obj -> pid ] .= ',' . $obj -> code ;
}
2017-10-07 13:09:31 +02:00
$i ++ ;
}
$this -> db -> free ( $resql );
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
dol_print_error ( $this -> db );
}
return $arrayroles ;
}
2017-06-07 10:55:39 +02:00
2017-01-18 10:57:18 +01:00
/**
2017-10-07 13:09:31 +02:00
* Return list of id of contacts of task
*
* @ param string $source Source
* @ return array Array of id of contacts
*/
2019-02-24 23:32:09 +01:00
public function getListContactId ( $source = 'internal' )
2017-10-07 13:09:31 +02:00
{
$contactAlreadySelected = array ();
2019-01-27 11:55:16 +01:00
$tab = $this -> liste_contact ( - 1 , $source );
2017-10-07 13:09:31 +02:00
//var_dump($tab);
2019-11-13 18:32:11 +01:00
$num = count ( $tab );
2017-10-07 13:09:31 +02:00
$i = 0 ;
2021-02-26 18:49:22 +01:00
while ( $i < $num ) {
if ( $source == 'thirdparty' ) {
$contactAlreadySelected [ $i ] = $tab [ $i ][ 'socid' ];
} else {
$contactAlreadySelected [ $i ] = $tab [ $i ][ 'id' ];
}
2017-10-07 13:09:31 +02:00
$i ++ ;
}
return $contactAlreadySelected ;
}
/**
* Add time spent
*
* @ param User $user User object
* @ param int $notrigger 0 = launch triggers after , 1 = disable triggers
2023-12-06 15:46:39 +01:00
* @ return int Return integer <= 0 if KO , > 0 if OK
2017-10-07 13:09:31 +02:00
*/
2019-02-24 23:32:09 +01:00
public function addTimeSpent ( $user , $notrigger = 0 )
2017-10-07 13:09:31 +02:00
{
2019-11-13 18:32:11 +01:00
global $conf , $langs ;
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::addTimeSpent " , LOG_DEBUG );
$ret = 0 ;
2022-05-11 09:30:39 +02:00
$now = dol_now ();
2017-10-07 13:09:31 +02:00
// Check parameters
2021-02-26 18:49:22 +01:00
if ( ! is_object ( $user )) {
2024-01-20 09:22:38 +01:00
dol_print_error ( null , " Method addTimeSpent was called with wrong parameter user " );
2017-10-07 13:09:31 +02:00
return - 1 ;
}
// Clean parameters
2021-02-26 18:49:22 +01:00
if ( isset ( $this -> timespent_note )) {
$this -> timespent_note = trim ( $this -> timespent_note );
}
2023-05-12 15:00:48 +02:00
if ( empty ( $this -> timespent_datehour ) || ( $this -> timespent_date != $this -> timespent_datehour )) {
2021-02-26 18:49:22 +01:00
$this -> timespent_datehour = $this -> timespent_date ;
}
2014-10-29 18:01:51 +01:00
2023-11-27 11:56:32 +01:00
if ( getDolGlobalString ( 'PROJECT_TIMESHEET_PREVENT_AFTER_MONTHS' )) {
2021-06-09 11:29:22 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php' ;
$restrictBefore = dol_time_plus_duree ( dol_now (), - $conf -> global -> PROJECT_TIMESHEET_PREVENT_AFTER_MONTHS , 'm' );
if ( $this -> timespent_date < $restrictBefore ) {
2024-01-05 04:18:53 +01:00
$this -> error = $langs -> trans ( 'TimeRecordingRestrictedToNMonthsBack' , getDolGlobalString ( 'PROJECT_TIMESHEET_PREVENT_AFTER_MONTHS' ));
2021-06-09 11:29:22 +02:00
$this -> errors [] = $this -> error ;
return - 1 ;
}
}
2021-06-08 18:13:18 +02:00
2017-10-07 13:09:31 +02:00
$this -> db -> begin ();
2023-03-08 11:00:58 +01:00
$timespent = new TimeSpent ( $this -> db );
$timespent -> fk_element = $this -> id ;
$timespent -> elementtype = 'task' ;
$timespent -> element_date = $this -> timespent_date ;
$timespent -> element_datehour = $this -> timespent_datehour ;
$timespent -> element_date_withhour = $this -> timespent_withhour ;
$timespent -> element_duration = $this -> timespent_duration ;
$timespent -> fk_user = $this -> timespent_fk_user ;
$timespent -> fk_product = $this -> timespent_fk_product ;
$timespent -> note = $this -> timespent_note ;
$timespent -> datec = $this -> db -> idate ( $now );
2023-08-15 13:51:17 +02:00
$result = $timespent -> create ( $user );
if ( $result > 0 ) {
$ret = $result ;
$this -> timespent_id = $result ;
2017-06-07 10:55:39 +02:00
2021-02-26 18:49:22 +01:00
if ( ! $notrigger ) {
2017-10-07 13:09:31 +02:00
// Call trigger
2019-11-13 18:32:11 +01:00
$result = $this -> call_trigger ( 'TASK_TIMESPENT_CREATE' , $user );
2021-02-26 18:49:22 +01:00
if ( $result < 0 ) {
$ret = - 1 ;
}
2017-10-07 13:09:31 +02:00
// End call triggers
}
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 18:32:11 +01:00
$this -> error = $this -> db -> lasterror ();
2017-10-07 13:09:31 +02:00
$ret = - 1 ;
}
2021-02-26 18:49:22 +01:00
if ( $ret > 0 ) {
2017-10-07 13:09:31 +02:00
// Recalculate amount of time spent for task and update denormalized field
$sql = " UPDATE " . MAIN_DB_PREFIX . " projet_task " ;
2023-03-08 11:00:58 +01:00
$sql .= " SET duration_effective = (SELECT SUM(element_duration) FROM " . MAIN_DB_PREFIX . " element_time as ptt where ptt.elementtype = 'task' AND ptt.fk_element = " . (( int ) $this -> id ) . " ) " ;
2021-02-26 18:49:22 +01:00
if ( isset ( $this -> progress )) {
2021-03-30 03:37:54 +02:00
$sql .= " , progress = " . (( float ) $this -> progress ); // Do not overwrite value if not provided
2021-02-26 18:49:22 +01:00
}
2021-03-30 03:37:54 +02:00
$sql .= " WHERE rowid = " . (( int ) $this -> id );
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::addTimeSpent " , LOG_DEBUG );
2021-02-26 18:49:22 +01:00
if ( ! $this -> db -> query ( $sql )) {
2019-11-13 18:32:11 +01:00
$this -> error = $this -> db -> lasterror ();
2017-10-07 13:09:31 +02:00
$ret = - 2 ;
}
2021-07-12 23:01:34 +02:00
// Update hourly rate of this time spent entry
2023-03-08 11:08:08 +01:00
$resql_thm_user = $this -> db -> query ( " SELECT thm FROM " . MAIN_DB_PREFIX . " user WHERE rowid = " . (( int ) $timespent -> fk_user ));
2023-03-08 11:00:58 +01:00
if ( ! empty ( $resql_thm_user )) {
$obj_thm_user = $this -> db -> fetch_object ( $resql_thm_user );
$timespent -> thm = $obj_thm_user -> thm ;
}
$res_update = $timespent -> update ( $user );
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::addTimeSpent " , LOG_DEBUG );
2023-03-08 11:00:58 +01:00
if ( $res_update <= 0 ) {
2019-11-13 18:32:11 +01:00
$this -> error = $this -> db -> lasterror ();
2017-10-07 13:09:31 +02:00
$ret = - 2 ;
}
}
2021-02-26 18:49:22 +01:00
if ( $ret > 0 ) {
2017-10-07 13:09:31 +02:00
$this -> db -> commit ();
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
$this -> db -> rollback ();
}
return $ret ;
}
2022-01-28 22:59:32 +01:00
/**
* Fetch records of time spent of this task
*
* @ param string $morewherefilter Add more filter into where SQL request ( must start with ' AND ...' )
2023-12-06 15:46:39 +01:00
* @ return int Return integer < 0 if KO , array of time spent if OK
2022-01-28 22:59:32 +01:00
*/
public function fetchTimeSpentOnTask ( $morewherefilter = '' )
{
2022-01-28 23:03:08 +01:00
$arrayres = array ();
$sql = " SELECT " ;
$sql .= " s.rowid as socid, " ;
$sql .= " s.nom as thirdparty_name, " ;
$sql .= " s.email as thirdparty_email, " ;
$sql .= " ptt.rowid, " ;
2023-05-16 22:14:06 +02:00
$sql .= " ptt.ref_ext, " ;
2023-03-08 11:00:58 +01:00
$sql .= " ptt.fk_element as fk_task, " ;
$sql .= " ptt.element_date as task_date, " ;
$sql .= " ptt.element_datehour as task_datehour, " ;
$sql .= " ptt.element_date_withhour as task_date_withhour, " ;
$sql .= " ptt.element_duration as task_duration, " ;
2022-01-28 23:03:08 +01:00
$sql .= " ptt.fk_user, " ;
$sql .= " ptt.note, " ;
$sql .= " ptt.thm, " ;
$sql .= " pt.rowid as task_id, " ;
$sql .= " pt.ref as task_ref, " ;
$sql .= " pt.label as task_label, " ;
$sql .= " p.rowid as project_id, " ;
$sql .= " p.ref as project_ref, " ;
$sql .= " p.title as project_label, " ;
$sql .= " p.public as public " ;
2023-03-08 11:00:58 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " element_time as ptt, " . MAIN_DB_PREFIX . " projet_task as pt, " . MAIN_DB_PREFIX . " projet as p " ;
2022-01-28 23:03:08 +01:00
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " societe as s ON p.fk_soc = s.rowid " ;
2023-03-08 11:00:58 +01:00
$sql .= " WHERE ptt.fk_element = pt.rowid AND pt.fk_projet = p.rowid " ;
$sql .= " AND ptt.elementtype = 'task' " ;
2022-01-28 23:03:08 +01:00
$sql .= " AND pt.rowid = " . (( int ) $this -> id );
$sql .= " AND pt.entity IN ( " . getEntity ( 'project' ) . " ) " ;
if ( $morewherefilter ) {
$sql .= $morewherefilter ;
}
dol_syslog ( get_class ( $this ) . " ::fetchAllTimeSpent " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
if ( $resql ) {
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
while ( $i < $num ) {
$obj = $this -> db -> fetch_object ( $resql );
$newobj = new stdClass ();
$newobj -> socid = $obj -> socid ;
$newobj -> thirdparty_name = $obj -> thirdparty_name ;
$newobj -> thirdparty_email = $obj -> thirdparty_email ;
$newobj -> fk_project = $obj -> project_id ;
$newobj -> project_ref = $obj -> project_ref ;
$newobj -> project_label = $obj -> project_label ;
$newobj -> public = $obj -> project_public ;
$newobj -> fk_task = $obj -> task_id ;
$newobj -> task_ref = $obj -> task_ref ;
$newobj -> task_label = $obj -> task_label ;
$newobj -> timespent_line_id = $obj -> rowid ;
2023-05-16 22:14:06 +02:00
$newobj -> timespent_line_ref_ext = $obj -> ref_ext ;
2022-01-28 23:03:08 +01:00
$newobj -> timespent_line_date = $this -> db -> jdate ( $obj -> task_date );
$newobj -> timespent_line_datehour = $this -> db -> jdate ( $obj -> task_datehour );
$newobj -> timespent_line_withhour = $obj -> task_date_withhour ;
$newobj -> timespent_line_duration = $obj -> task_duration ;
$newobj -> timespent_line_fk_user = $obj -> fk_user ;
$newobj -> timespent_line_thm = $obj -> thm ; // hourly rate
$newobj -> timespent_line_note = $obj -> note ;
$arrayres [] = $newobj ;
$i ++ ;
}
$this -> db -> free ( $resql );
$this -> lines = $arrayres ;
return 1 ;
} else {
dol_print_error ( $this -> db );
$this -> error = " Error " . $this -> db -> lasterror ();
return - 1 ;
}
2022-01-28 22:59:32 +01:00
}
2022-01-28 23:03:08 +01:00
2017-10-07 13:09:31 +02:00
/**
* Calculate total of time spent for task
*
2017-11-22 21:19:40 +01:00
* @ param User | int $userobj Filter on user . null or 0 = No filter
* @ param string $morewherefilter Add more filter into where SQL request ( must start with ' AND ...' )
2023-01-04 11:36:46 +01:00
* @ return array | int Array of info for task array ( 'min_date' , 'max_date' , 'total_duration' , 'total_amount' , 'nblines' , 'nblinesnull' )
2017-10-07 13:09:31 +02:00
*/
2019-02-24 23:32:09 +01:00
public function getSummaryOfTimeSpent ( $userobj = null , $morewherefilter = '' )
2017-10-07 13:09:31 +02:00
{
2021-02-26 18:49:22 +01:00
if ( is_object ( $userobj )) {
$userid = $userobj -> id ;
} else {
$userid = $userobj ; // old method
}
2017-11-22 21:19:40 +01:00
2019-11-13 18:32:11 +01:00
$id = $this -> id ;
2021-02-26 18:49:22 +01:00
if ( empty ( $id ) && empty ( $userid )) {
2017-11-22 21:19:40 +01:00
dol_syslog ( " getSummaryOfTimeSpent called on a not loaded task without user param defined " , LOG_ERR );
2017-10-07 13:09:31 +02:00
return - 1 ;
}
2019-11-13 18:32:11 +01:00
$result = array ();
2017-10-07 13:09:31 +02:00
$sql = " SELECT " ;
2023-03-08 11:00:58 +01:00
$sql .= " MIN(t.element_datehour) as min_date, " ;
$sql .= " MAX(t.element_datehour) as max_date, " ;
$sql .= " SUM(t.element_duration) as total_duration, " ;
$sql .= " SUM(t.element_duration / 3600 * " . $this -> db -> ifsql ( " t.thm IS NULL " , 0 , " t.thm " ) . " ) as total_amount, " ;
2019-11-13 18:32:11 +01:00
$sql .= " COUNT(t.rowid) as nblines, " ;
$sql .= " SUM( " . $this -> db -> ifsql ( " t.thm IS NULL " , 1 , 0 ) . " ) as nblinesnull " ;
2023-03-08 11:00:58 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " element_time as t " ;
$sql .= " WHERE t.elementtype='task' " ;
2021-02-26 18:49:22 +01:00
if ( $morewherefilter ) {
$sql .= $morewherefilter ;
}
if ( $id > 0 ) {
2023-03-08 11:00:58 +01:00
$sql .= " AND t.fk_element = " . (( int ) $id );
2021-02-26 18:49:22 +01:00
}
if ( $userid > 0 ) {
2021-03-22 13:31:06 +01:00
$sql .= " AND t.fk_user = " . (( int ) $userid );
2021-02-26 18:49:22 +01:00
}
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::getSummaryOfTimeSpent " , LOG_DEBUG );
2019-11-13 18:32:11 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-26 18:49:22 +01:00
if ( $resql ) {
2017-10-07 13:09:31 +02:00
$obj = $this -> db -> fetch_object ( $resql );
2019-11-13 18:32:11 +01:00
$result [ 'min_date' ] = $obj -> min_date ; // deprecated. use the ->timespent_xxx instead
$result [ 'max_date' ] = $obj -> max_date ; // deprecated. use the ->timespent_xxx instead
$result [ 'total_duration' ] = $obj -> total_duration ; // deprecated. use the ->timespent_xxx instead
2017-10-07 13:09:31 +02:00
2019-11-13 18:32:11 +01:00
$this -> timespent_min_date = $this -> db -> jdate ( $obj -> min_date );
$this -> timespent_max_date = $this -> db -> jdate ( $obj -> max_date );
$this -> timespent_total_duration = $obj -> total_duration ;
$this -> timespent_total_amount = $obj -> total_amount ;
$this -> timespent_nblinesnull = ( $obj -> nblinesnull ? $obj -> nblinesnull : 0 );
$this -> timespent_nblines = ( $obj -> nblines ? $obj -> nblines : 0 );
2017-10-07 13:09:31 +02:00
$this -> db -> free ( $resql );
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
dol_print_error ( $this -> db );
}
return $result ;
}
/**
* Calculate quantity and value of time consumed using the thm ( hourly amount value of work for user entering time )
*
2024-03-09 14:48:06 +01:00
* @ param User | string $fuser Filter on a dedicated user
2017-10-07 13:09:31 +02:00
* @ param string $dates Start date ( ex 00 : 00 : 00 )
* @ param string $datee End date ( ex 23 : 59 : 59 )
* @ return array Array of info for task array ( 'amount' , 'nbseconds' , 'nblinesnull' )
*/
2019-02-24 23:32:09 +01:00
public function getSumOfAmount ( $fuser = '' , $dates = '' , $datee = '' )
2017-10-07 13:09:31 +02:00
{
2023-11-17 02:12:25 +01:00
$id = $this -> id ;
2017-10-07 13:09:31 +02:00
2019-11-13 18:32:11 +01:00
$result = array ();
2017-10-07 13:09:31 +02:00
$sql = " SELECT " ;
2023-03-08 11:00:58 +01:00
$sql .= " SUM(t.element_duration) as nbseconds, " ;
$sql .= " SUM(t.element_duration / 3600 * " . $this -> db -> ifsql ( " t.thm IS NULL " , 0 , " t.thm " ) . " ) as amount, SUM( " . $this -> db -> ifsql ( " t.thm IS NULL " , 1 , 0 ) . " ) as nblinesnull " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " element_time as t " ;
$sql .= " WHERE t.elementtype='task' AND t.fk_element = " . (( int ) $id );
2021-02-26 18:49:22 +01:00
if ( is_object ( $fuser ) && $fuser -> id > 0 ) {
2021-03-30 19:12:07 +02:00
$sql .= " AND fk_user = " . (( int ) $fuser -> id );
2017-10-07 13:09:31 +02:00
}
2021-02-26 18:49:22 +01:00
if ( $dates > 0 ) {
2023-03-08 11:00:58 +01:00
$datefieldname = " element_datehour " ;
2019-11-13 18:32:11 +01:00
$sql .= " AND ( " . $datefieldname . " >= ' " . $this -> db -> idate ( $dates ) . " ' OR " . $datefieldname . " IS NULL) " ;
2015-06-03 21:01:50 +02:00
}
2021-02-26 18:49:22 +01:00
if ( $datee > 0 ) {
2023-03-08 11:00:58 +01:00
$datefieldname = " element_datehour " ;
2019-11-13 18:32:11 +01:00
$sql .= " AND ( " . $datefieldname . " <= ' " . $this -> db -> idate ( $datee ) . " ' OR " . $datefieldname . " IS NULL) " ;
2015-06-03 21:01:50 +02:00
}
2015-06-23 10:13:08 +02:00
//print $sql;
2015-07-04 18:20:17 +02:00
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::getSumOfAmount " , LOG_DEBUG );
2019-11-13 18:32:11 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-26 18:49:22 +01:00
if ( $resql ) {
2017-10-07 13:09:31 +02:00
$obj = $this -> db -> fetch_object ( $resql );
$result [ 'amount' ] = $obj -> amount ;
$result [ 'nbseconds' ] = $obj -> nbseconds ;
$result [ 'nblinesnull' ] = $obj -> nblinesnull ;
$this -> db -> free ( $resql );
return $result ;
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
dol_print_error ( $this -> db );
return $result ;
}
}
/**
2021-05-18 13:36:07 +02:00
* Load properties of timespent of a task from the time spent ID .
2017-10-07 13:09:31 +02:00
*
2021-05-18 13:36:07 +02:00
* @ param int $id Id in time spent table
2023-12-01 19:51:32 +01:00
* @ return int Return integer < 0 if KO , > 0 if OK
2017-10-07 13:09:31 +02:00
*/
2019-02-24 23:32:09 +01:00
public function fetchTimeSpent ( $id )
2017-10-07 13:09:31 +02:00
{
2023-03-08 11:00:58 +01:00
$timespent = new TimeSpent ( $this -> db );
$timespent -> fetch ( $id );
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::fetchTimeSpent " , LOG_DEBUG );
2023-03-08 11:00:58 +01:00
if ( $timespent -> id > 0 ) {
$this -> timespent_id = $timespent -> id ;
$this -> id = $timespent -> fk_element ;
2023-09-23 15:57:23 +02:00
$this -> timespent_date = $timespent -> element_date ;
$this -> timespent_datehour = $timespent -> element_datehour ;
2023-03-08 11:00:58 +01:00
$this -> timespent_withhour = $timespent -> element_date_withhour ;
$this -> timespent_duration = $timespent -> element_duration ;
$this -> timespent_fk_user = $timespent -> fk_user ;
$this -> timespent_fk_product = $timespent -> fk_product ;
$this -> timespent_thm = $timespent -> thm ; // hourly rate
$this -> timespent_note = $timespent -> note ;
2017-10-07 13:09:31 +02:00
return 1 ;
}
2023-03-08 11:00:58 +01:00
return 0 ;
2017-10-07 13:09:31 +02:00
}
2017-11-22 21:19:40 +01:00
/**
* Load all records of time spent
*
2023-01-04 11:36:46 +01:00
* @ param User $userobj User object
* @ param string $morewherefilter Add more filter into where SQL request ( must start with ' AND ...' )
2023-12-06 15:46:39 +01:00
* @ return array | int Return integer < 0 if KO , array of time spent if OK
2017-11-22 21:19:40 +01:00
*/
2019-02-24 23:32:09 +01:00
public function fetchAllTimeSpent ( User $userobj , $morewherefilter = '' )
2017-11-22 21:19:40 +01:00
{
2019-11-13 18:32:11 +01:00
$arrayres = array ();
2017-11-22 21:19:40 +01:00
$sql = " SELECT " ;
2019-11-13 18:32:11 +01:00
$sql .= " s.rowid as socid, " ;
$sql .= " s.nom as thirdparty_name, " ;
$sql .= " s.email as thirdparty_email, " ;
$sql .= " ptt.rowid, " ;
2023-03-08 11:00:58 +01:00
$sql .= " ptt.fk_element as fk_task, " ;
$sql .= " ptt.element_date as task_date, " ;
$sql .= " ptt.element_datehour as task_datehour, " ;
$sql .= " ptt.element_date_withhour as task_date_withhour, " ;
$sql .= " ptt.element_duration as task_duration, " ;
2019-11-13 18:32:11 +01:00
$sql .= " ptt.fk_user, " ;
$sql .= " ptt.note, " ;
2021-07-12 23:01:34 +02:00
$sql .= " ptt.thm, " ;
2019-11-13 18:32:11 +01:00
$sql .= " pt.rowid as task_id, " ;
$sql .= " pt.ref as task_ref, " ;
$sql .= " pt.label as task_label, " ;
$sql .= " p.rowid as project_id, " ;
$sql .= " p.ref as project_ref, " ;
$sql .= " p.title as project_label, " ;
$sql .= " p.public as public " ;
2023-03-08 11:00:58 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " element_time as ptt, " . MAIN_DB_PREFIX . " projet_task as pt, " . MAIN_DB_PREFIX . " projet as p " ;
2019-11-13 18:32:11 +01:00
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " societe as s ON p.fk_soc = s.rowid " ;
2023-03-08 11:00:58 +01:00
$sql .= " WHERE ptt.fk_element = pt.rowid AND pt.fk_projet = p.rowid " ;
$sql .= " AND ptt.elementtype = 'task' " ;
2021-07-12 23:01:34 +02:00
$sql .= " AND ptt.fk_user = " . (( int ) $userobj -> id );
2019-11-13 18:32:11 +01:00
$sql .= " AND pt.entity IN ( " . getEntity ( 'project' ) . " ) " ;
2021-02-26 18:49:22 +01:00
if ( $morewherefilter ) {
$sql .= $morewherefilter ;
}
2017-11-22 21:19:40 +01:00
dol_syslog ( get_class ( $this ) . " ::fetchAllTimeSpent " , LOG_DEBUG );
2019-11-13 18:32:11 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-26 18:49:22 +01:00
if ( $resql ) {
2017-11-22 21:19:40 +01:00
$num = $this -> db -> num_rows ( $resql );
2019-11-13 18:32:11 +01:00
$i = 0 ;
2021-02-26 18:49:22 +01:00
while ( $i < $num ) {
2017-11-22 21:19:40 +01:00
$obj = $this -> db -> fetch_object ( $resql );
$newobj = new stdClass ();
$newobj -> socid = $obj -> socid ;
$newobj -> thirdparty_name = $obj -> thirdparty_name ;
$newobj -> thirdparty_email = $obj -> thirdparty_email ;
$newobj -> fk_project = $obj -> project_id ;
$newobj -> project_ref = $obj -> project_ref ;
2019-11-13 18:32:11 +01:00
$newobj -> project_label = $obj -> project_label ;
2017-11-22 21:19:40 +01:00
$newobj -> public = $obj -> project_public ;
$newobj -> fk_task = $obj -> task_id ;
2019-11-13 18:32:11 +01:00
$newobj -> task_ref = $obj -> task_ref ;
$newobj -> task_label = $obj -> task_label ;
2017-11-22 21:19:40 +01:00
2019-11-13 18:32:11 +01:00
$newobj -> timespent_id = $obj -> rowid ;
$newobj -> timespent_date = $this -> db -> jdate ( $obj -> task_date );
2017-11-22 21:19:40 +01:00
$newobj -> timespent_datehour = $this -> db -> jdate ( $obj -> task_datehour );
$newobj -> timespent_withhour = $obj -> task_date_withhour ;
$newobj -> timespent_duration = $obj -> task_duration ;
2019-11-13 18:32:11 +01:00
$newobj -> timespent_fk_user = $obj -> fk_user ;
2021-07-12 23:01:34 +02:00
$newobj -> timespent_thm = $obj -> thm ; // hourly rate
2019-11-13 18:32:11 +01:00
$newobj -> timespent_note = $obj -> note ;
2017-11-22 21:19:40 +01:00
$arrayres [] = $newobj ;
$i ++ ;
}
$this -> db -> free ( $resql );
2020-05-21 15:05:19 +02:00
} else {
2017-11-22 21:19:40 +01:00
dol_print_error ( $this -> db );
2019-11-13 18:32:11 +01:00
$this -> error = " Error " . $this -> db -> lasterror ();
2017-11-22 21:19:40 +01:00
return - 1 ;
}
return $arrayres ;
}
2017-10-07 13:09:31 +02:00
/**
* Update time spent
*
* @ param User $user User id
* @ param int $notrigger 0 = launch triggers after , 1 = disable triggers
2023-12-01 19:51:32 +01:00
* @ return int Return integer < 0 if KO , > 0 if OK
2017-10-07 13:09:31 +02:00
*/
2019-02-24 23:32:09 +01:00
public function updateTimeSpent ( $user , $notrigger = 0 )
2017-10-07 13:09:31 +02:00
{
2019-11-13 18:32:11 +01:00
global $conf , $langs ;
2017-10-07 13:09:31 +02:00
$ret = 0 ;
2019-01-04 15:44:09 +01:00
// Check parameters
2021-02-26 18:49:22 +01:00
if ( $this -> timespent_date == '' ) {
2020-09-07 10:18:17 +02:00
$this -> error = $langs -> trans ( " ErrorFieldRequired " , $langs -> transnoentities ( " Date " ));
return - 1 ;
2019-01-04 15:44:09 +01:00
}
2021-02-26 18:49:22 +01:00
if ( ! ( $this -> timespent_fk_user > 0 )) {
2020-09-07 10:18:17 +02:00
$this -> error = $langs -> trans ( " ErrorFieldRequired " , $langs -> transnoentities ( " User " ));
return - 1 ;
2019-01-04 15:44:09 +01:00
}
2017-10-07 13:09:31 +02:00
// Clean parameters
2021-02-26 18:49:22 +01:00
if ( empty ( $this -> timespent_datehour )) {
$this -> timespent_datehour = $this -> timespent_date ;
}
if ( isset ( $this -> timespent_note )) {
$this -> timespent_note = trim ( $this -> timespent_note );
}
2017-10-07 13:09:31 +02:00
2023-11-27 11:56:32 +01:00
if ( getDolGlobalString ( 'PROJECT_TIMESHEET_PREVENT_AFTER_MONTHS' )) {
2021-06-09 11:29:22 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php' ;
$restrictBefore = dol_time_plus_duree ( dol_now (), - $conf -> global -> PROJECT_TIMESHEET_PREVENT_AFTER_MONTHS , 'm' );
2021-06-09 11:08:38 +02:00
2021-06-09 11:29:22 +02:00
if ( $this -> timespent_date < $restrictBefore ) {
2024-01-05 04:18:53 +01:00
$this -> error = $langs -> trans ( 'TimeRecordingRestrictedToNMonthsBack' , getDolGlobalString ( 'PROJECT_TIMESHEET_PREVENT_AFTER_MONTHS' ));
2021-06-09 11:29:22 +02:00
$this -> errors [] = $this -> error ;
return - 1 ;
}
}
2021-06-09 11:08:38 +02:00
2017-10-07 13:09:31 +02:00
$this -> db -> begin ();
2023-03-08 11:00:58 +01:00
$timespent = new TimeSpent ( $this -> db );
$timespent -> fetch ( $this -> timespent_id );
$timespent -> element_date = $this -> timespent_date ;
$timespent -> element_datehour = $this -> timespent_datehour ;
$timespent -> element_date_withhour = $this -> timespent_withhour ;
$timespent -> element_duration = $this -> timespent_duration ;
$timespent -> fk_user = $this -> timespent_fk_user ;
$timespent -> fk_product = $this -> timespent_fk_product ;
$timespent -> note = $this -> timespent_note ;
2023-03-10 19:42:22 +01:00
$timespent -> invoice_id = $this -> timespent_invoiceid ;
$timespent -> invoice_line_id = $this -> timespent_invoicelineid ;
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::updateTimeSpent " , LOG_DEBUG );
2023-03-08 11:00:58 +01:00
if ( $timespent -> update ( $user ) > 0 ) {
2021-02-26 18:49:22 +01:00
if ( ! $notrigger ) {
2017-10-07 13:09:31 +02:00
// Call trigger
2019-11-13 18:32:11 +01:00
$result = $this -> call_trigger ( 'TASK_TIMESPENT_MODIFY' , $user );
2021-02-26 18:49:22 +01:00
if ( $result < 0 ) {
2017-10-07 13:09:31 +02:00
$this -> db -> rollback ();
$ret = - 1 ;
2021-02-26 18:49:22 +01:00
} else {
$ret = 1 ;
}
2017-10-07 13:09:31 +02:00
// End call triggers
2021-02-26 18:49:22 +01:00
} else {
$ret = 1 ;
}
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 18:32:11 +01:00
$this -> error = $this -> db -> lasterror ();
2017-10-07 13:09:31 +02:00
$this -> db -> rollback ();
$ret = - 1 ;
}
2023-11-27 11:56:32 +01:00
if ( $ret == 1 && (( $this -> timespent_old_duration != $this -> timespent_duration ) || getDolGlobalString ( 'TIMESPENT_ALWAYS_UPDATE_THM' ))) {
2022-08-02 11:00:15 +02:00
if ( $this -> timespent_old_duration != $this -> timespent_duration ) {
2022-08-02 10:49:34 +02:00
// Recalculate amount of time spent for task and update denormalized field
$sql = " UPDATE " . MAIN_DB_PREFIX . " projet_task " ;
2023-03-08 11:00:58 +01:00
$sql .= " SET duration_effective = (SELECT SUM(element_duration) FROM " . MAIN_DB_PREFIX . " element_time as ptt where ptt.elementtype = 'task' AND ptt.fk_element = " . (( int ) $this -> id ) . " ) " ;
2022-08-02 10:49:34 +02:00
if ( isset ( $this -> progress )) {
2022-08-02 11:00:15 +02:00
$sql .= " , progress = " . (( float ) $this -> progress ); // Do not overwrite value if not provided
2022-08-02 10:49:34 +02:00
}
2022-08-02 11:00:15 +02:00
$sql .= " WHERE rowid = " . (( int ) $this -> id );
2017-10-07 13:09:31 +02:00
2022-08-02 10:49:34 +02:00
dol_syslog ( get_class ( $this ) . " ::updateTimeSpent " , LOG_DEBUG );
if ( ! $this -> db -> query ( $sql )) {
$this -> error = $this -> db -> lasterror ();
$this -> db -> rollback ();
$ret = - 2 ;
}
2017-10-07 13:09:31 +02:00
}
2021-07-12 23:01:34 +02:00
2024-01-12 17:55:52 +01:00
// Update hourly rate of this time spent entry, but only if it was not set initially
2023-03-08 11:00:58 +01:00
$res_update = 1 ;
2023-11-27 11:56:32 +01:00
if ( empty ( $timespent -> thm ) || getDolGlobalString ( 'TIMESPENT_ALWAYS_UPDATE_THM' )) {
2023-03-08 11:08:08 +01:00
$resql_thm_user = $this -> db -> query ( " SELECT thm FROM " . MAIN_DB_PREFIX . " user WHERE rowid = " . (( int ) $timespent -> fk_user ));
2023-03-08 11:00:58 +01:00
if ( ! empty ( $resql_thm_user )) {
$obj_thm_user = $this -> db -> fetch_object ( $resql_thm_user );
$timespent -> thm = $obj_thm_user -> thm ;
}
$res_update = $timespent -> update ( $user );
2022-08-02 10:49:34 +02:00
}
2021-07-12 23:01:34 +02:00
2023-03-08 11:00:58 +01:00
dol_syslog ( get_class ( $this ) . " ::updateTimeSpent " , LOG_DEBUG );
if ( $res_update <= 0 ) {
2021-07-12 23:01:34 +02:00
$this -> error = $this -> db -> lasterror ();
$ret = - 2 ;
}
2017-10-07 13:09:31 +02:00
}
2021-02-26 18:49:22 +01:00
if ( $ret >= 0 ) {
$this -> db -> commit ();
}
2017-10-07 13:09:31 +02:00
return $ret ;
}
/**
* Delete time spent
2012-04-27 11:03:01 +02:00
*
2017-10-07 13:09:31 +02:00
* @ param User $user User that delete
* @ param int $notrigger 0 = launch triggers after , 1 = disable triggers
2023-12-01 19:51:32 +01:00
* @ return int Return integer < 0 if KO , > 0 if OK
2012-04-27 11:03:01 +02:00
*/
2019-02-24 23:32:09 +01:00
public function delTimeSpent ( $user , $notrigger = 0 )
2017-10-07 13:09:31 +02:00
{
global $conf , $langs ;
2019-11-13 18:32:11 +01:00
$error = 0 ;
2017-10-07 13:09:31 +02:00
2023-11-27 11:56:32 +01:00
if ( getDolGlobalString ( 'PROJECT_TIMESHEET_PREVENT_AFTER_MONTHS' )) {
2021-06-09 11:29:22 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php' ;
$restrictBefore = dol_time_plus_duree ( dol_now (), - $conf -> global -> PROJECT_TIMESHEET_PREVENT_AFTER_MONTHS , 'm' );
2021-06-09 11:08:38 +02:00
2021-06-09 11:29:22 +02:00
if ( $this -> timespent_date < $restrictBefore ) {
2024-01-05 04:18:53 +01:00
$this -> error = $langs -> trans ( 'TimeRecordingRestrictedToNMonthsBack' , getDolGlobalString ( 'PROJECT_TIMESHEET_PREVENT_AFTER_MONTHS' ));
2021-06-09 11:29:22 +02:00
$this -> errors [] = $this -> error ;
return - 1 ;
}
}
2021-06-09 11:08:38 +02:00
2017-10-07 13:09:31 +02:00
$this -> db -> begin ();
2022-03-29 10:26:38 +02:00
if ( ! $notrigger ) {
// Call trigger
$result = $this -> call_trigger ( 'TASK_TIMESPENT_DELETE' , $user );
if ( $result < 0 ) {
$error ++ ;
}
// End call triggers
2021-02-26 18:49:22 +01:00
}
2017-10-07 13:09:31 +02:00
2021-02-26 18:49:22 +01:00
if ( ! $error ) {
2023-03-08 11:00:58 +01:00
$timespent = new TimeSpent ( $this -> db );
$timespent -> fetch ( $this -> timespent_id );
$res_del = $timespent -> delete ( $user );
if ( $res_del < 0 ) {
2023-12-04 13:49:31 +01:00
$error ++ ;
$this -> errors [] = " Error " . $this -> db -> lasterror ();
2017-10-07 13:09:31 +02:00
}
}
2021-02-26 18:49:22 +01:00
if ( ! $error ) {
2017-10-07 13:09:31 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " projet_task " ;
2019-11-13 18:32:11 +01:00
$sql .= " SET duration_effective = duration_effective - " . $this -> db -> escape ( $this -> timespent_duration ? $this -> timespent_duration : 0 );
2021-08-27 16:33:03 +02:00
$sql .= " WHERE rowid = " . (( int ) $this -> id );
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::delTimeSpent " , LOG_DEBUG );
2021-02-26 18:49:22 +01:00
if ( $this -> db -> query ( $sql )) {
2017-10-07 13:09:31 +02:00
$result = 0 ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 18:32:11 +01:00
$this -> error = $this -> db -> lasterror ();
2017-10-07 13:09:31 +02:00
$result = - 2 ;
}
}
// Commit or rollback
2021-02-26 18:49:22 +01:00
if ( $error ) {
foreach ( $this -> errors as $errmsg ) {
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::delTimeSpent " . $errmsg , LOG_ERR );
2019-11-13 18:32:11 +01:00
$this -> error .= ( $this -> error ? ', ' . $errmsg : $errmsg );
2017-10-07 13:09:31 +02:00
}
$this -> db -> rollback ();
2019-11-13 18:32:11 +01:00
return - 1 * $error ;
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
$this -> db -> commit ();
return 1 ;
}
}
2020-09-07 10:18:17 +02:00
/** Load an object from its id and create a new one in database
*
2019-04-25 12:11:32 +02:00
* @ param User $user User making the clone
2019-02-24 23:32:09 +01:00
* @ param int $fromid Id of object to clone
* @ param int $project_id Id of project to attach clone task
* @ param int $parent_task_id Id of task to attach clone task
* @ param bool $clone_change_dt recalculate date of task regarding new project start date
* @ param bool $clone_affectation clone affectation of project
* @ param bool $clone_time clone time of project
* @ param bool $clone_file clone file of project
* @ param bool $clone_note clone note of project
* @ param bool $clone_prog clone progress of project
* @ return int New id of clone
2020-09-07 10:18:17 +02:00
*/
2019-04-25 12:11:32 +02:00
public function createFromClone ( User $user , $fromid , $project_id , $parent_task_id , $clone_change_dt = false , $clone_affectation = false , $clone_time = false , $clone_file = false , $clone_note = false , $clone_prog = false )
2012-04-27 11:03:01 +02:00
{
2019-11-13 18:32:11 +01:00
global $langs , $conf ;
2012-04-27 11:03:01 +02:00
2019-11-13 18:32:11 +01:00
$error = 0 ;
2012-05-06 00:15:03 +02:00
2014-12-05 21:24:16 +01:00
//Use 00:00 of today if time is use on task.
2019-11-13 18:32:11 +01:00
$now = dol_mktime ( 0 , 0 , 0 , dol_print_date ( dol_now (), '%m' ), dol_print_date ( dol_now (), '%d' ), dol_print_date ( dol_now (), '%Y' ));
2012-05-06 00:15:03 +02:00
2012-05-07 17:04:35 +02:00
$datec = $now ;
2012-04-27 11:03:01 +02:00
2019-11-13 18:32:11 +01:00
$clone_task = new Task ( $this -> db );
$origin_task = new Task ( $this -> db );
2012-04-27 11:03:01 +02:00
2019-11-13 18:32:11 +01:00
$clone_task -> context [ 'createfromclone' ] = 'createfromclone' ;
2015-02-26 13:03:17 +01:00
2012-04-27 11:03:01 +02:00
$this -> db -> begin ();
// Load source object
$clone_task -> fetch ( $fromid );
2016-10-16 18:27:39 +02:00
$clone_task -> fetch_optionals ();
//var_dump($clone_task->array_options);exit;
2017-06-07 10:55:39 +02:00
2013-07-18 10:24:45 +02:00
$origin_task -> fetch ( $fromid );
2014-03-11 09:43:26 +01:00
2019-11-13 18:32:11 +01:00
$defaultref = '' ;
2023-11-27 11:56:32 +01:00
$obj = ! getDolGlobalString ( 'PROJECT_TASK_ADDON' ) ? 'mod_task_simple' : $conf -> global -> PROJECT_TASK_ADDON ;
if ( getDolGlobalString ( 'PROJECT_TASK_ADDON' ) && is_readable ( DOL_DOCUMENT_ROOT . " /core/modules/project/task/ " . getDolGlobalString ( 'PROJECT_TASK_ADDON' ) . " .php " )) {
2023-10-15 18:39:13 +02:00
require_once DOL_DOCUMENT_ROOT . " /core/modules/project/task/ " . getDolGlobalString ( 'PROJECT_TASK_ADDON' ) . '.php' ;
2023-12-04 13:49:31 +01:00
$modTask = new $obj ();
2019-01-27 11:55:16 +01:00
$defaultref = $modTask -> getNextValue ( 0 , $clone_task );
2013-07-18 10:24:45 +02:00
}
2012-05-06 00:15:03 +02:00
2012-04-27 11:03:01 +02:00
$ori_project_id = $clone_task -> fk_project ;
2012-05-06 00:15:03 +02:00
2012-04-27 11:03:01 +02:00
$clone_task -> id = 0 ;
2013-07-18 10:24:45 +02:00
$clone_task -> ref = $defaultref ;
2019-11-13 18:32:11 +01:00
$clone_task -> fk_project = $project_id ;
$clone_task -> fk_task_parent = $parent_task_id ;
$clone_task -> date_c = $datec ;
$clone_task -> planned_workload = $origin_task -> planned_workload ;
$clone_task -> rang = $origin_task -> rang ;
2012-05-06 00:15:03 +02:00
2017-10-07 13:09:31 +02:00
//Manage Task Date
2021-02-26 18:49:22 +01:00
if ( $clone_change_dt ) {
2019-11-13 18:32:11 +01:00
$projectstatic = new Project ( $this -> db );
2017-10-07 13:09:31 +02:00
$projectstatic -> fetch ( $ori_project_id );
2012-05-06 00:15:03 +02:00
2024-01-12 17:55:52 +01:00
//Origin project start date
2017-10-07 13:09:31 +02:00
$orign_project_dt_start = $projectstatic -> date_start ;
2012-05-06 00:15:03 +02:00
2024-01-12 17:55:52 +01:00
//Calculate new task start date with difference between origin proj start date and origin task start date
2021-02-26 18:49:22 +01:00
if ( ! empty ( $clone_task -> date_start )) {
2019-11-13 18:32:11 +01:00
$clone_task -> date_start = $now + $clone_task -> date_start - $orign_project_dt_start ;
2017-10-07 13:09:31 +02:00
}
2012-05-06 00:15:03 +02:00
2024-01-12 17:55:52 +01:00
//Calculate new task end date with difference between origin proj end date and origin task end date
2021-02-26 18:49:22 +01:00
if ( ! empty ( $clone_task -> date_end )) {
2019-11-13 18:32:11 +01:00
$clone_task -> date_end = $now + $clone_task -> date_end - $orign_project_dt_start ;
2017-10-07 13:09:31 +02:00
}
}
2012-05-06 00:15:03 +02:00
2021-02-26 18:49:22 +01:00
if ( ! $clone_prog ) {
2023-12-04 13:49:31 +01:00
$clone_task -> progress = 0 ;
2017-10-07 13:09:31 +02:00
}
2012-04-27 11:03:01 +02:00
// Create clone
2019-11-13 18:32:11 +01:00
$result = $clone_task -> create ( $user );
2012-04-27 11:03:01 +02:00
// Other options
2021-02-26 18:49:22 +01:00
if ( $result < 0 ) {
2019-11-13 18:32:11 +01:00
$this -> error = $clone_task -> error ;
2012-04-27 11:03:01 +02:00
$error ++ ;
}
// End
2024-03-20 14:56:14 +01:00
if ( $error ) {
$clone_task_id = 0 ; // For static tool check
} else {
2019-11-13 18:32:11 +01:00
$clone_task_id = $clone_task -> id ;
2013-10-19 17:51:21 +02:00
$clone_task_ref = $clone_task -> ref ;
2012-05-06 00:15:03 +02:00
2021-02-26 18:49:22 +01:00
//Note Update
if ( ! $clone_note ) {
2019-11-13 18:32:11 +01:00
$clone_task -> note_private = '' ;
$clone_task -> note_public = '' ;
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
$this -> db -> begin ();
2020-10-31 14:32:18 +01:00
$res = $clone_task -> update_note ( dol_html_entity_decode ( $clone_task -> note_public , ENT_QUOTES | ENT_HTML5 ), '_public' );
2021-02-26 18:49:22 +01:00
if ( $res < 0 ) {
2019-11-13 18:32:11 +01:00
$this -> error .= $clone_task -> error ;
2012-04-27 11:03:01 +02:00
$error ++ ;
$this -> db -> rollback ();
2020-05-21 15:05:19 +02:00
} else {
2012-04-27 11:03:01 +02:00
$this -> db -> commit ();
}
2012-05-06 00:15:03 +02:00
2012-04-27 11:03:01 +02:00
$this -> db -> begin ();
2020-10-31 14:32:18 +01:00
$res = $clone_task -> update_note ( dol_html_entity_decode ( $clone_task -> note_private , ENT_QUOTES | ENT_HTML5 ), '_private' );
2021-02-26 18:49:22 +01:00
if ( $res < 0 ) {
2019-11-13 18:32:11 +01:00
$this -> error .= $clone_task -> error ;
2012-04-27 11:03:01 +02:00
$error ++ ;
$this -> db -> rollback ();
2020-05-21 15:05:19 +02:00
} else {
2012-04-27 11:03:01 +02:00
$this -> db -> commit ();
}
2017-10-07 13:09:31 +02:00
}
2012-05-06 00:15:03 +02:00
2012-04-27 11:03:01 +02:00
//Duplicate file
2021-02-26 18:49:22 +01:00
if ( $clone_file ) {
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
2012-05-06 00:15:03 +02:00
2020-10-23 20:08:35 +02:00
//retrieve project origin ref to know folder to copy
2019-11-13 18:32:11 +01:00
$projectstatic = new Project ( $this -> db );
2017-10-07 13:09:31 +02:00
$projectstatic -> fetch ( $ori_project_id );
2019-11-13 18:32:11 +01:00
$ori_project_ref = $projectstatic -> ref ;
2017-10-07 13:09:31 +02:00
2021-02-26 18:49:22 +01:00
if ( $ori_project_id != $project_id ) {
2017-10-07 13:09:31 +02:00
$projectstatic -> fetch ( $project_id );
2019-11-13 18:32:11 +01:00
$clone_project_ref = $projectstatic -> ref ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 18:32:11 +01:00
$clone_project_ref = $ori_project_ref ;
2017-10-07 13:09:31 +02:00
}
2012-05-06 00:15:03 +02:00
2022-06-14 17:53:17 +02:00
$clone_task_dir = $conf -> project -> dir_output . " / " . dol_sanitizeFileName ( $clone_project_ref ) . " / " . dol_sanitizeFileName ( $clone_task_ref );
$ori_task_dir = $conf -> project -> dir_output . " / " . dol_sanitizeFileName ( $ori_project_ref ) . " / " . dol_sanitizeFileName ( $fromid );
2012-05-06 00:15:03 +02:00
2019-11-13 18:32:11 +01:00
$filearray = dol_dir_list ( $ori_task_dir , " files " , 0 , '' , '(\.meta|_preview.*\.png)$' , '' , SORT_ASC , 1 );
2021-02-26 18:49:22 +01:00
foreach ( $filearray as $key => $file ) {
if ( ! file_exists ( $clone_task_dir )) {
if ( dol_mkdir ( $clone_task_dir ) < 0 ) {
2019-11-13 18:32:11 +01:00
$this -> error .= $langs -> trans ( 'ErrorInternalErrorDetected' ) . ':dol_mkdir' ;
2012-05-06 00:15:03 +02:00
$error ++ ;
2012-04-27 11:03:01 +02:00
}
}
2012-05-06 00:15:03 +02:00
2019-11-13 18:32:11 +01:00
$rescopy = dol_copy ( $ori_task_dir . '/' . $file [ 'name' ], $clone_task_dir . '/' . $file [ 'name' ], 0 , 1 );
2021-02-26 18:49:22 +01:00
if ( is_numeric ( $rescopy ) && $rescopy < 0 ) {
2019-11-13 18:32:11 +01:00
$this -> error .= $langs -> trans ( " ErrorFailToCopyFile " , $ori_task_dir . '/' . $file [ 'name' ], $clone_task_dir . '/' . $file [ 'name' ]);
2012-04-27 11:03:01 +02:00
$error ++ ;
}
2012-05-06 00:15:03 +02:00
}
2012-04-27 11:03:01 +02:00
}
2012-05-06 00:15:03 +02:00
2012-04-27 11:03:01 +02:00
// clone affectation
2021-02-26 18:49:22 +01:00
if ( $clone_affectation ) {
2012-04-27 11:03:01 +02:00
$origin_task = new Task ( $this -> db );
$origin_task -> fetch ( $fromid );
2012-05-06 00:15:03 +02:00
2021-02-26 18:49:22 +01:00
foreach ( array ( 'internal' , 'external' ) as $source ) {
2019-01-27 11:55:16 +01:00
$tab = $origin_task -> liste_contact ( - 1 , $source );
2019-11-13 18:32:11 +01:00
$num = count ( $tab );
2012-04-27 11:03:01 +02:00
$i = 0 ;
2021-02-26 18:49:22 +01:00
while ( $i < $num ) {
2012-04-27 11:03:01 +02:00
$clone_task -> add_contact ( $tab [ $i ][ 'id' ], $tab [ $i ][ 'code' ], $tab [ $i ][ 'source' ]);
2021-02-26 18:49:22 +01:00
if ( $clone_task -> error == 'DB_ERROR_RECORD_ALREADY_EXISTS' ) {
2012-04-27 11:03:01 +02:00
$langs -> load ( " errors " );
2019-11-13 18:32:11 +01:00
$this -> error .= $langs -> trans ( " ErrorThisContactIsAlreadyDefinedAsThisType " );
2012-04-27 11:03:01 +02:00
$error ++ ;
2020-05-21 15:05:19 +02:00
} else {
2021-02-26 18:49:22 +01:00
if ( $clone_task -> error != '' ) {
2019-11-13 18:32:11 +01:00
$this -> error .= $clone_task -> error ;
2012-04-27 11:03:01 +02:00
$error ++ ;
}
}
$i ++ ;
}
}
}
2012-05-06 00:15:03 +02:00
2021-02-26 18:49:22 +01:00
if ( $clone_time ) {
2012-04-27 11:03:01 +02:00
//TODO clone time of affectation
}
2015-02-26 13:03:17 +01:00
}
2012-05-06 00:15:03 +02:00
2015-02-26 13:03:17 +01:00
unset ( $clone_task -> context [ 'createfromclone' ]);
2021-02-26 18:49:22 +01:00
if ( ! $error ) {
2015-02-26 13:03:17 +01:00
$this -> db -> commit ();
return $clone_task_id ;
2020-05-21 15:05:19 +02:00
} else {
2012-04-27 11:03:01 +02:00
$this -> db -> rollback ();
2019-11-13 18:32:11 +01:00
dol_syslog ( get_class ( $this ) . " ::createFromClone nbError: " . $error . " error : " . $this -> error , LOG_ERR );
2012-04-27 11:03:01 +02:00
return - 1 ;
}
}
2008-09-10 13:14:45 +02:00
2014-07-06 15:06:34 +02:00
2014-07-08 01:15:59 +02:00
/**
* Return status label of object
*
2015-04-07 02:45:30 +02:00
* @ param integer $mode 0 = long label , 1 = short label , 2 = Picto + short label , 3 = Picto , 4 = Picto + long label , 5 = Short label + Picto
2014-07-08 01:15:59 +02:00
* @ return string Label
2014-07-06 15:06:34 +02:00
*/
2019-02-24 23:32:09 +01:00
public function getLibStatut ( $mode = 0 )
2014-07-06 15:06:34 +02:00
{
2024-02-16 13:23:23 +01:00
return $this -> LibStatut ( $this -> status , $mode );
2014-07-06 15:06:34 +02:00
}
2020-09-07 10:18:17 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2014-07-06 15:06:34 +02:00
/**
2019-02-24 23:32:09 +01:00
* Return status label for an object
2014-07-08 01:15:59 +02:00
*
2019-11-01 23:58:14 +01:00
* @ param int $status Id status
2019-02-24 23:32:09 +01:00
* @ param integer $mode 0 = long label , 1 = short label , 2 = Picto + short label , 3 = Picto , 4 = Picto + long label , 5 = Short label + Picto
* @ return string Label
2014-07-06 15:06:34 +02:00
*/
2019-11-01 23:58:14 +01:00
public function LibStatut ( $status , $mode = 0 )
2014-07-06 15:06:34 +02:00
{
2020-09-07 10:18:17 +02:00
// phpcs:enable
2019-11-06 00:42:29 +01:00
global $langs ;
2014-07-06 15:06:34 +02:00
// list of Statut of the task
2023-11-24 10:10:24 +01:00
$this -> labelStatus [ 0 ] = 'Draft' ;
$this -> labelStatus [ 1 ] = 'ToDo' ;
$this -> labelStatus [ 2 ] = 'Running' ;
$this -> labelStatus [ 3 ] = 'Finish' ;
$this -> labelStatus [ 4 ] = 'Transfered' ;
$this -> labelStatusShort [ 0 ] = 'Draft' ;
$this -> labelStatusShort [ 1 ] = 'ToDo' ;
$this -> labelStatusShort [ 2 ] = 'Running' ;
$this -> labelStatusShort [ 3 ] = 'Completed' ;
$this -> labelStatusShort [ 4 ] = 'Transfered' ;
2014-07-08 01:15:59 +02:00
2021-02-26 18:49:22 +01:00
if ( $mode == 0 ) {
2023-11-24 10:10:24 +01:00
return $langs -> trans ( $this -> labelStatus [ $status ]);
2021-02-26 18:49:22 +01:00
} elseif ( $mode == 1 ) {
2023-11-24 10:10:24 +01:00
return $langs -> trans ( $this -> labelStatusShort [ $status ]);
2021-02-26 18:49:22 +01:00
} elseif ( $mode == 2 ) {
if ( $status == 0 ) {
2023-11-24 10:10:24 +01:00
return img_picto ( $langs -> trans ( $this -> labelStatusShort [ $status ]), 'statut0' ) . ' ' . $langs -> trans ( $this -> labelStatusShort [ $status ]);
2021-02-26 18:49:22 +01:00
} elseif ( $status == 1 ) {
2023-11-24 10:10:24 +01:00
return img_picto ( $langs -> trans ( $this -> labelStatusShort [ $status ]), 'statut1' ) . ' ' . $langs -> trans ( $this -> labelStatusShort [ $status ]);
2021-02-26 18:49:22 +01:00
} elseif ( $status == 2 ) {
2023-11-24 10:10:24 +01:00
return img_picto ( $langs -> trans ( $this -> labelStatusShort [ $status ]), 'statut3' ) . ' ' . $langs -> trans ( $this -> labelStatusShort [ $status ]);
2021-02-26 18:49:22 +01:00
} elseif ( $status == 3 ) {
2023-11-24 10:10:24 +01:00
return img_picto ( $langs -> trans ( $this -> labelStatusShort [ $status ]), 'statut6' ) . ' ' . $langs -> trans ( $this -> labelStatusShort [ $status ]);
2021-02-26 18:49:22 +01:00
} elseif ( $status == 4 ) {
2023-11-24 10:10:24 +01:00
return img_picto ( $langs -> trans ( $this -> labelStatusShort [ $status ]), 'statut6' ) . ' ' . $langs -> trans ( $this -> labelStatusShort [ $status ]);
2021-02-26 18:49:22 +01:00
} elseif ( $status == 5 ) {
2023-11-24 10:10:24 +01:00
return img_picto ( $langs -> trans ( $this -> labelStatusShort [ $status ]), 'statut5' ) . ' ' . $langs -> trans ( $this -> labelStatusShort [ $status ]);
2021-02-26 18:49:22 +01:00
}
} elseif ( $mode == 3 ) {
if ( $status == 0 ) {
2023-11-24 10:10:24 +01:00
return img_picto ( $langs -> trans ( $this -> labelStatusShort [ $status ]), 'statut0' );
2021-02-26 18:49:22 +01:00
} elseif ( $status == 1 ) {
2023-11-24 10:10:24 +01:00
return img_picto ( $langs -> trans ( $this -> labelStatusShort [ $status ]), 'statut1' );
2021-02-26 18:49:22 +01:00
} elseif ( $status == 2 ) {
2023-11-24 10:10:24 +01:00
return img_picto ( $langs -> trans ( $this -> labelStatusShort [ $status ]), 'statut3' );
2021-02-26 18:49:22 +01:00
} elseif ( $status == 3 ) {
2023-11-24 10:10:24 +01:00
return img_picto ( $langs -> trans ( $this -> labelStatusShort [ $status ]), 'statut6' );
2021-02-26 18:49:22 +01:00
} elseif ( $status == 4 ) {
2023-11-24 10:10:24 +01:00
return img_picto ( $langs -> trans ( $this -> labelStatusShort [ $status ]), 'statut6' );
2021-02-26 18:49:22 +01:00
} elseif ( $status == 5 ) {
2023-11-24 10:10:24 +01:00
return img_picto ( $langs -> trans ( $this -> labelStatusShort [ $status ]), 'statut5' );
2021-02-26 18:49:22 +01:00
}
} elseif ( $mode == 4 ) {
if ( $status == 0 ) {
2023-11-24 10:10:24 +01:00
return img_picto ( $langs -> trans ( $this -> labelStatusShort [ $status ]), 'statut0' ) . ' ' . $langs -> trans ( $this -> labelStatus [ $status ]);
2021-02-26 18:49:22 +01:00
} elseif ( $status == 1 ) {
2023-11-24 10:10:24 +01:00
return img_picto ( $langs -> trans ( $this -> labelStatusShort [ $status ]), 'statut1' ) . ' ' . $langs -> trans ( $this -> labelStatus [ $status ]);
2021-02-26 18:49:22 +01:00
} elseif ( $status == 2 ) {
2023-11-24 10:10:24 +01:00
return img_picto ( $langs -> trans ( $this -> labelStatusShort [ $status ]), 'statut3' ) . ' ' . $langs -> trans ( $this -> labelStatus [ $status ]);
2021-02-26 18:49:22 +01:00
} elseif ( $status == 3 ) {
2023-11-24 10:10:24 +01:00
return img_picto ( $langs -> trans ( $this -> labelStatusShort [ $status ]), 'statut6' ) . ' ' . $langs -> trans ( $this -> labelStatus [ $status ]);
2021-02-26 18:49:22 +01:00
} elseif ( $status == 4 ) {
2023-11-24 10:10:24 +01:00
return img_picto ( $langs -> trans ( $this -> labelStatusShort [ $status ]), 'statut6' ) . ' ' . $langs -> trans ( $this -> labelStatus [ $status ]);
2021-02-26 18:49:22 +01:00
} elseif ( $status == 5 ) {
2023-11-24 10:10:24 +01:00
return img_picto ( $langs -> trans ( $this -> labelStatusShort [ $status ]), 'statut5' ) . ' ' . $langs -> trans ( $this -> labelStatus [ $status ]);
2021-02-26 18:49:22 +01:00
}
} elseif ( $mode == 5 ) {
2023-11-24 10:10:24 +01:00
/* if ( $status == 0 ) return $langs -> trans ( $this -> labelStatusShort [ $status ]) . ' ' . img_picto ( $langs -> trans ( $this -> labelStatusShort [ $status ]), 'statut0' );
elseif ( $status == 1 ) return $langs -> trans ( $this -> labelStatusShort [ $status ]) . ' ' . img_picto ( $langs -> trans ( $this -> labelStatusShort [ $status ]), 'statut1' );
elseif ( $status == 2 ) return $langs -> trans ( $this -> labelStatusShort [ $status ]) . ' ' . img_picto ( $langs -> trans ( $this -> labelStatusShort [ $status ]), 'statut3' );
elseif ( $status == 3 ) return $langs -> trans ( $this -> labelStatusShort [ $status ]) . ' ' . img_picto ( $langs -> trans ( $this -> labelStatusShort [ $status ]), 'statut6' );
elseif ( $status == 4 ) return $langs -> trans ( $this -> labelStatusShort [ $status ]) . ' ' . img_picto ( $langs -> trans ( $this -> labelStatusShort [ $status ]), 'statut6' );
elseif ( $status == 5 ) return $langs -> trans ( $this -> labelStatusShort [ $status ]) . ' ' . img_picto ( $langs -> trans ( $this -> labelStatusShort [ $status ]), 'statut5' );
2018-03-06 21:51:43 +01:00
*/
2018-09-05 18:49:06 +02:00
//else return $this->progress.' %';
2018-03-06 21:51:43 +01:00
return ' ' ;
2021-02-26 18:49:22 +01:00
} elseif ( $mode == 6 ) {
2023-11-24 10:10:24 +01:00
/* if ( $status == 0 ) return $langs -> trans ( $this -> labelStatus [ $status ]) . ' ' . img_picto ( $langs -> trans ( $this -> labelStatusShort [ $status ]), 'statut0' );
elseif ( $status == 1 ) return $langs -> trans ( $this -> labelStatus [ $status ]) . ' ' . img_picto ( $langs -> trans ( $this -> labelStatusShort [ $status ]), 'statut1' );
elseif ( $status == 2 ) return $langs -> trans ( $this -> labelStatus [ $status ]) . ' ' . img_picto ( $langs -> trans ( $this -> labelStatusShort [ $status ]), 'statut3' );
elseif ( $status == 3 ) return $langs -> trans ( $this -> labelStatus [ $status ]) . ' ' . img_picto ( $langs -> trans ( $this -> labelStatusShort [ $status ]), 'statut6' );
elseif ( $status == 4 ) return $langs -> trans ( $this -> labelStatus [ $status ]) . ' ' . img_picto ( $langs -> trans ( $this -> labelStatusShort [ $status ]), 'statut6' );
elseif ( $status == 5 ) return $langs -> trans ( $this -> labelStatus [ $status ]) . ' ' . img_picto ( $langs -> trans ( $this -> labelStatusShort [ $status ]), 'statut5' );
2018-03-06 21:51:43 +01:00
*/
2018-09-05 18:49:06 +02:00
//else return $this->progress.' %';
2017-10-07 13:09:31 +02:00
return ' ' ;
2017-04-05 14:48:24 +02:00
}
2023-07-24 19:42:31 +02:00
return " " ;
2014-07-06 15:06:34 +02:00
}
Created a method generateDocument for several classes
Which are: Commande, Contrat, Livraison, Facture, Projet, Propal, Task, Expedition, CommandeFournisseur, FactureFournisseur and therefore deprecated the following methods supplier_order_pdf_create, supplier_invoice_pdf_create, delivery_order_pdf_create, task_pdf_create, propale_pdf_create, project_pdf_create, facture_pdf_create, expedition_pdf_create, commande_pdf_create
2014-09-16 12:30:37 +02:00
/**
* Create an intervention document on disk using template defined into PROJECT_TASK_ADDON_PDF
*
2024-01-12 17:55:52 +01:00
* @ param string $modele force le modele a utiliser ( '' par default )
* @ param Translate $outputlangs object lang a utiliser pour traduction
Created a method generateDocument for several classes
Which are: Commande, Contrat, Livraison, Facture, Projet, Propal, Task, Expedition, CommandeFournisseur, FactureFournisseur and therefore deprecated the following methods supplier_order_pdf_create, supplier_invoice_pdf_create, delivery_order_pdf_create, task_pdf_create, propale_pdf_create, project_pdf_create, facture_pdf_create, expedition_pdf_create, commande_pdf_create
2014-09-16 12:30:37 +02:00
* @ param int $hidedetails Hide details of lines
* @ param int $hidedesc Hide description
* @ param int $hideref Hide ref
* @ return int 0 if KO , 1 if OK
*/
2019-01-27 15:20:16 +01:00
public function generateDocument ( $modele , $outputlangs , $hidedetails = 0 , $hidedesc = 0 , $hideref = 0 )
Created a method generateDocument for several classes
Which are: Commande, Contrat, Livraison, Facture, Projet, Propal, Task, Expedition, CommandeFournisseur, FactureFournisseur and therefore deprecated the following methods supplier_order_pdf_create, supplier_invoice_pdf_create, delivery_order_pdf_create, task_pdf_create, propale_pdf_create, project_pdf_create, facture_pdf_create, expedition_pdf_create, commande_pdf_create
2014-09-16 12:30:37 +02:00
{
2020-12-13 13:34:21 +01:00
global $conf ;
Created a method generateDocument for several classes
Which are: Commande, Contrat, Livraison, Facture, Projet, Propal, Task, Expedition, CommandeFournisseur, FactureFournisseur and therefore deprecated the following methods supplier_order_pdf_create, supplier_invoice_pdf_create, delivery_order_pdf_create, task_pdf_create, propale_pdf_create, project_pdf_create, facture_pdf_create, expedition_pdf_create, commande_pdf_create
2014-09-16 12:30:37 +02:00
2020-12-13 13:34:21 +01:00
$outputlangs -> load ( " projects " );
Created a method generateDocument for several classes
Which are: Commande, Contrat, Livraison, Facture, Projet, Propal, Task, Expedition, CommandeFournisseur, FactureFournisseur and therefore deprecated the following methods supplier_order_pdf_create, supplier_invoice_pdf_create, delivery_order_pdf_create, task_pdf_create, propale_pdf_create, project_pdf_create, facture_pdf_create, expedition_pdf_create, commande_pdf_create
2014-09-16 12:30:37 +02:00
2019-11-13 18:32:11 +01:00
if ( ! dol_strlen ( $modele )) {
2017-01-16 21:16:05 +01:00
$modele = 'nodefault' ;
2020-12-13 13:34:21 +01:00
if ( ! empty ( $this -> model_pdf )) {
$modele = $this -> model_pdf ;
2023-11-27 11:56:32 +01:00
} elseif ( getDolGlobalString ( 'PROJECT_TASK_ADDON_PDF' )) {
2024-01-05 04:18:53 +01:00
$modele = getDolGlobalString ( 'PROJECT_TASK_ADDON_PDF' );
Created a method generateDocument for several classes
Which are: Commande, Contrat, Livraison, Facture, Projet, Propal, Task, Expedition, CommandeFournisseur, FactureFournisseur and therefore deprecated the following methods supplier_order_pdf_create, supplier_invoice_pdf_create, delivery_order_pdf_create, task_pdf_create, propale_pdf_create, project_pdf_create, facture_pdf_create, expedition_pdf_create, commande_pdf_create
2014-09-16 12:30:37 +02:00
}
}
2014-09-29 11:08:50 +02:00
$modelpath = " core/modules/project/task/doc/ " ;
Created a method generateDocument for several classes
Which are: Commande, Contrat, Livraison, Facture, Projet, Propal, Task, Expedition, CommandeFournisseur, FactureFournisseur and therefore deprecated the following methods supplier_order_pdf_create, supplier_invoice_pdf_create, delivery_order_pdf_create, task_pdf_create, propale_pdf_create, project_pdf_create, facture_pdf_create, expedition_pdf_create, commande_pdf_create
2014-09-16 12:30:37 +02:00
2014-09-21 18:16:14 +02:00
return $this -> commonGenerateDocument ( $modelpath , $modele , $outputlangs , $hidedetails , $hidedesc , $hideref );
Created a method generateDocument for several classes
Which are: Commande, Contrat, Livraison, Facture, Projet, Propal, Task, Expedition, CommandeFournisseur, FactureFournisseur and therefore deprecated the following methods supplier_order_pdf_create, supplier_invoice_pdf_create, delivery_order_pdf_create, task_pdf_create, propale_pdf_create, project_pdf_create, facture_pdf_create, expedition_pdf_create, commande_pdf_create
2014-09-16 12:30:37 +02:00
}
2017-10-12 13:03:45 +02:00
2020-09-07 10:18:17 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2016-01-18 15:56:51 +01:00
/**
* Load indicators for dashboard ( this -> nbtodo and this -> nbtodolate )
*
2024-01-12 17:55:52 +01:00
* @ param User $user Object user
2023-12-06 15:46:39 +01:00
* @ return WorkboardResponse | int Return integer < 0 if KO , WorkboardResponse if OK
2016-01-18 15:56:51 +01:00
*/
2019-02-24 23:32:09 +01:00
public function load_board ( $user )
2016-01-18 15:56:51 +01:00
{
2020-09-07 10:18:17 +02:00
// phpcs:enable
2017-09-26 12:32:58 +02:00
global $conf , $langs ;
2017-10-12 13:03:45 +02:00
2024-01-12 17:55:52 +01:00
// For external user, no check is done on company because readability is managed by public status of project and assignment.
2021-05-25 19:54:09 +02:00
//$socid = $user->socid;
$socid = 0 ;
2018-06-21 18:47:02 +02:00
2017-09-26 12:32:58 +02:00
$projectstatic = new Project ( $this -> db );
2019-01-27 11:55:16 +01:00
$projectsListId = $projectstatic -> getProjectsAuthorizedForUser ( $user , 0 , 1 , $socid );
2018-06-21 18:47:02 +02:00
2017-09-26 12:32:58 +02:00
// List of tasks (does not care about permissions. Filtering will be done later)
$sql = " SELECT p.rowid as projectid, p.fk_statut as projectstatus, " ;
2019-11-13 18:32:11 +01:00
$sql .= " t.rowid as taskid, t.progress as progress, t.fk_statut as status, " ;
2024-01-14 12:10:09 +01:00
$sql .= " t.dateo as date_start, t.datee as date_end " ;
2019-11-13 18:32:11 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " projet as p " ;
2022-11-15 22:12:53 +01:00
//$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s on p.fk_soc = s.rowid";
2018-05-05 14:34:54 +02:00
//if (! $user->rights->societe->client->voir && ! $socid) $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc = s.rowid";
2019-11-13 18:32:11 +01:00
$sql .= " , " . MAIN_DB_PREFIX . " projet_task as t " ;
$sql .= " WHERE p.entity IN ( " . getEntity ( 'project' , 0 ) . ')' ;
$sql .= " AND p.fk_statut = 1 " ;
$sql .= " AND t.fk_projet = p.rowid " ;
2019-11-21 08:29:47 +01:00
$sql .= " AND (t.progress IS NULL OR t.progress < 100) " ; // tasks to do
2023-10-15 18:39:13 +02:00
if ( ! $user -> hasRight ( 'projet' , 'all' , 'lire' )) {
2021-03-22 11:30:18 +01:00
$sql .= " AND p.rowid IN ( " . $this -> db -> sanitize ( $projectsListId ) . " ) " ;
2021-02-26 18:49:22 +01:00
}
2017-09-26 12:32:58 +02:00
// No need to check company, as filtering of projects must be done by getProjectsAuthorizedForUser
2021-05-25 19:54:09 +02:00
//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).")";
2018-05-05 14:34:54 +02:00
// No need to check company, as filtering of projects must be done by getProjectsAuthorizedForUser
2021-08-23 18:56:46 +02:00
// 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))";
2018-06-21 18:47:02 +02:00
2017-09-26 12:32:58 +02:00
//print $sql;
2019-11-13 18:32:11 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-26 18:49:22 +01:00
if ( $resql ) {
2017-09-26 12:32:58 +02:00
$task_static = new Task ( $this -> db );
2017-10-12 13:03:45 +02:00
2017-09-26 12:32:58 +02:00
$response = new WorkboardResponse ();
2022-06-14 17:53:17 +02:00
$response -> warning_delay = $conf -> project -> task -> warning_delay / 60 / 60 / 24 ;
2017-09-26 12:32:58 +02:00
$response -> label = $langs -> trans ( " OpenedTasks " );
2022-12-09 14:27:43 +01:00
if ( $user -> hasRight ( " projet " , " all " , " lire " )) {
2021-02-26 18:49:22 +01:00
$response -> url = DOL_URL_ROOT . '/projet/tasks/list.php?mainmenu=project' ;
} else {
$response -> url = DOL_URL_ROOT . '/projet/tasks/list.php?mode=mine&mainmenu=project' ;
}
2019-01-27 11:55:16 +01:00
$response -> img = img_object ( '' , " task " );
2017-10-12 13:03:45 +02:00
2017-09-26 12:32:58 +02:00
// This assignment in condition is not a bug. It allows walking the results.
2021-02-26 18:49:22 +01:00
while ( $obj = $this -> db -> fetch_object ( $resql )) {
2017-09-26 12:32:58 +02:00
$response -> nbtodo ++ ;
2017-10-12 13:03:45 +02:00
2017-09-26 12:32:58 +02:00
$task_static -> projectstatus = $obj -> projectstatus ;
$task_static -> progress = $obj -> progress ;
$task_static -> fk_statut = $obj -> status ;
2024-02-16 13:23:23 +01:00
$task_static -> status = $obj -> status ;
2024-01-14 12:10:09 +01:00
$task_static -> date_start = $this -> db -> jdate ( $obj -> date_start );
$task_static -> date_end = $this -> db -> jdate ( $obj -> date_end );
2017-10-12 13:03:45 +02:00
2017-09-26 12:32:58 +02:00
if ( $task_static -> hasDelay ()) {
$response -> nbtodolate ++ ;
}
}
2017-10-12 13:03:45 +02:00
2017-09-26 12:32:58 +02:00
return $response ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 18:32:11 +01:00
$this -> error = $this -> db -> error ();
2017-09-26 12:32:58 +02:00
return - 1 ;
}
}
2017-10-12 13:03:45 +02:00
2017-09-26 12:32:58 +02:00
/**
2024-01-12 17:55:52 +01:00
* Load indicators this -> nb for state board
2017-09-26 12:32:58 +02:00
*
2023-12-06 15:46:39 +01:00
* @ return int Return integer < 0 if ko , > 0 if ok
2017-09-26 12:32:58 +02:00
*/
2024-01-18 18:55:53 +01:00
public function loadStateBoard ()
2017-09-26 12:32:58 +02:00
{
global $user ;
2017-10-12 13:03:45 +02:00
2023-12-04 13:49:31 +01:00
$mine = 0 ;
$socid = $user -> socid ;
2017-10-12 13:03:45 +02:00
2017-09-26 12:32:58 +02:00
$projectstatic = new Project ( $this -> db );
2019-01-27 11:55:16 +01:00
$projectsListId = $projectstatic -> getProjectsAuthorizedForUser ( $user , $mine , 1 , $socid );
2017-10-12 13:03:45 +02:00
2017-09-26 12:32:58 +02:00
// List of tasks (does not care about permissions. Filtering will be done later)
$sql = " SELECT count(p.rowid) as nb " ;
2019-11-13 18:32:11 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " projet as p " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " societe as s on p.fk_soc = s.rowid " ;
2024-01-09 13:08:22 +01:00
if ( ! $user -> hasRight ( 'societe' , 'client' , 'voir' )) {
2021-02-26 18:49:22 +01:00
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " societe_commerciaux as sc ON sc.fk_soc = s.rowid " ;
}
2019-11-13 18:32:11 +01:00
$sql .= " , " . MAIN_DB_PREFIX . " projet_task as t " ;
$sql .= " WHERE p.entity IN ( " . getEntity ( 'project' , 0 ) . ')' ;
$sql .= " AND t.fk_projet = p.rowid " ; // tasks to do
2023-10-15 18:39:13 +02:00
if ( $mine || ! $user -> hasRight ( 'projet' , 'all' , 'lire' )) {
2021-03-22 11:30:18 +01:00
$sql .= " AND p.rowid IN ( " . $this -> db -> sanitize ( $projectsListId ) . " ) " ;
2021-02-26 18:49:22 +01:00
}
2017-09-26 12:32:58 +02:00
// No need to check company, as filtering of projects must be done by getProjectsAuthorizedForUser
2021-08-23 17:41:11 +02:00
//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).")";
2021-02-26 18:49:22 +01:00
if ( $socid ) {
2021-06-09 15:36:47 +02:00
$sql .= " AND (p.fk_soc IS NULL OR p.fk_soc = 0 OR p.fk_soc = " . (( int ) $socid ) . " ) " ;
2021-02-26 18:49:22 +01:00
}
2024-01-09 13:08:22 +01:00
if ( ! $user -> hasRight ( 'societe' , 'client' , 'voir' )) {
2021-08-23 17:41:11 +02:00
$sql .= " AND ((s.rowid = sc.fk_soc AND sc.fk_user = " . (( int ) $user -> id ) . " ) OR (s.rowid IS NULL)) " ;
2021-02-26 18:49:22 +01:00
}
2017-10-12 13:03:45 +02:00
2019-11-13 18:32:11 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-26 18:49:22 +01:00
if ( $resql ) {
2017-09-26 12:32:58 +02:00
// This assignment in condition is not a bug. It allows walking the results.
2021-02-26 18:49:22 +01:00
while ( $obj = $this -> db -> fetch_object ( $resql )) {
2019-11-13 18:32:11 +01:00
$this -> nb [ " tasks " ] = $obj -> nb ;
2017-09-26 12:32:58 +02:00
}
$this -> db -> free ( $resql );
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2017-09-26 12:32:58 +02:00
dol_print_error ( $this -> db );
2019-11-13 18:32:11 +01:00
$this -> error = $this -> db -> error ();
2017-09-26 12:32:58 +02:00
return - 1 ;
}
2016-01-18 15:56:51 +01:00
}
2017-06-07 10:55:39 +02:00
2016-01-18 15:56:51 +01:00
/**
2016-06-15 13:37:50 +02:00
* Is the task delayed ?
2016-01-18 15:56:51 +01:00
*
* @ return bool
*/
public function hasDelay ()
{
2017-10-07 13:09:31 +02:00
global $conf ;
2017-06-07 10:55:39 +02:00
2019-11-13 18:32:11 +01:00
if ( ! ( $this -> progress >= 0 && $this -> progress < 100 )) {
2017-10-07 13:09:31 +02:00
return false ;
}
2016-01-18 15:56:51 +01:00
2017-10-07 13:09:31 +02:00
$now = dol_now ();
2016-01-18 15:56:51 +01:00
2021-07-05 13:44:05 +02:00
$datetouse = ( $this -> date_end > 0 ) ? $this -> date_end : (( isset ( $this -> datee ) && $this -> datee > 0 ) ? $this -> datee : 0 );
2016-06-15 13:37:50 +02:00
2022-06-14 17:53:17 +02:00
return ( $datetouse > 0 && ( $datetouse < ( $now - $conf -> project -> task -> warning_delay )));
2017-06-07 10:55:39 +02:00
}
2023-01-09 16:56:55 +01:00
/**
* Return clicable link of object ( with eventually picto )
*
* @ param string $option Where point the link ( 0 => main card , 1 , 2 => shipment , 'nolink' => No link )
2023-01-18 22:27:52 +01:00
* @ param array $arraydata Array of data
* @ return string HTML Code for Kanban thumb .
2023-01-09 16:56:55 +01:00
*/
2023-01-18 22:27:52 +01:00
public function getKanbanView ( $option = '' , $arraydata = null )
2023-01-09 16:56:55 +01:00
{
2023-03-07 22:04:40 +01:00
$selected = ( empty ( $arraydata [ 'selected' ]) ? 0 : $arraydata [ 'selected' ]);
2023-01-09 16:56:55 +01:00
$return = '<div class="box-flex-item box-flex-grow-zero">' ;
2023-04-07 23:51:41 +02:00
$return .= '<div class="info-box info-box-sm info-box-kanban">' ;
2023-01-09 16:56:55 +01:00
$return .= '<span class="info-box-icon bg-infobox-action">' ;
$return .= img_picto ( '' , $this -> picto );
//$return .= '<i class="fa fa-dol-action"></i>'; // Can be image
$return .= '</span>' ;
$return .= '<div class="info-box-content">' ;
2023-05-11 11:03:26 +02:00
$return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">' . ( method_exists ( $this , 'getNomUrl' ) ? $this -> getNomUrl ( 1 ) : $this -> ref ) . '</span>' ;
2023-10-17 15:43:56 +02:00
if ( $selected >= 0 ) {
$return .= '<input id="cb' . $this -> id . '" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="' . $this -> id . '"' . ( $selected ? ' checked="checked"' : '' ) . '>' ;
}
2023-04-07 23:51:41 +02:00
if ( ! empty ( $arraydata [ 'projectlink' ])) {
//$tmpproject = $arraydata['project'];
//$return .= '<br><span class="info-box-status ">'.$tmpproject->getNomProject().'</span>';
$return .= '<br><span class="info-box-status ">' . $arraydata [ 'projectlink' ] . '</span>' ;
2023-01-09 16:56:55 +01:00
}
if ( property_exists ( $this , 'budget_amount' )) {
2023-04-07 23:51:41 +02:00
//$return .= '<br><span class="info-box-label amount">'.$langs->trans("Budget").' : '.price($this->budget_amount, 0, $langs, 1, 0, 0, $conf->currency).'</span>';
2023-01-09 16:56:55 +01:00
}
if ( property_exists ( $this , 'duration_effective' )) {
2023-04-21 14:26:39 +02:00
$return .= '<br><br><div class="info-box-label progressinkanban">' . getTaskProgressView ( $this , false , true ) . '</div>' ;
2023-01-09 16:56:55 +01:00
}
$return .= '</div>' ;
$return .= '</div>' ;
$return .= '</div>' ;
return $return ;
}
2018-10-17 20:14:01 +02:00
}