2012-09-05 03:26:57 +02:00
< ? php
2016-03-03 08:41:39 +01:00
/* Copyright ( C ) 2011 Dimitri Mouillard < dmouillard @ teclib . com >
* Copyright ( C ) 2012 - 2014 Laurent Destailleur < eldy @ users . sourceforge . net >
2018-10-27 14:43:12 +02:00
* Copyright ( C ) 2012 - 2016 Regis Houssin < regis . houssin @ inodbox . com >
2016-03-03 08:41:39 +01:00
* Copyright ( C ) 2013 Florian Henry < florian . henry @ open - concept . pro >
2016-05-14 00:47:46 +02:00
* Copyright ( C ) 2016 Juanjo Menent < jmenent @ 2 byte . es >
2021-03-14 20:51:02 +01:00
* Copyright ( C ) 2018 - 2021 Frédéric France < frederic . france @ netlogic . fr >
2012-11-14 11:29:22 +01: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
2012-11-14 11:29:22 +01: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 .
*
2012-09-05 03:57:26 +02:00
* 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 />.
2012-11-14 11:29:22 +01:00
*/
2012-09-05 03:26:57 +02:00
/**
* \file holiday . class . php
* \ingroup holiday
* \brief Class file of the module paid holiday .
*/
2019-11-14 12:09:15 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php' ;
2012-09-05 03:26:57 +02:00
/**
2012-09-05 12:59:33 +02:00
* Class of the module paid holiday . Developed by Teclib ( http :// www . teclib . com / )
2012-09-05 03:26:57 +02:00
*/
2012-09-05 12:59:33 +02:00
class Holiday extends CommonObject
2012-09-05 03:26:57 +02:00
{
2018-08-23 18:35:45 +02:00
/**
* @ var string ID to identify managed object
*/
2019-11-14 12:09:15 +01:00
public $element = 'holiday' ;
2018-09-02 14:01:37 +02:00
2018-08-22 18:48:53 +02:00
/**
* @ var string Name of table without prefix where object is stored
*/
2019-11-14 12:09:15 +01:00
public $table_element = 'holiday' ;
2018-09-02 14:01:37 +02:00
2018-10-06 12:14:47 +02:00
/**
* 0 = No test on entity , 1 = Test with field entity , 2 = Test with link by societe
* @ var int
*/
public $ismultientitymanaged = 0 ;
2018-10-06 12:01:00 +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-10-06 12:01:00 +02:00
*/
2018-10-04 09:24:57 +02:00
public $fk_element = 'fk_holiday' ;
2018-10-06 12:01:00 +02:00
2019-10-30 09:35:45 +01:00
/**
* @ var string String with name of icon for myobject . Must be the part after the 'object_' into object_myobject . png
*/
2017-10-07 13:09:31 +02:00
public $picto = 'holiday' ;
2017-06-10 22:57:25 +02:00
2015-04-23 23:21:06 +02:00
/**
* @ deprecated
2018-10-25 18:50:53 +02:00
* @ see $id
2015-04-23 23:21:06 +02:00
*/
2018-10-04 09:24:57 +02:00
public $rowid ;
2017-10-07 13:09:31 +02:00
2018-08-31 18:06:17 +02:00
/**
* @ var int User ID
*/
public $fk_user ;
2019-11-14 12:09:15 +01:00
public $date_create = '' ;
2018-08-31 19:26:08 +02:00
/**
* @ var string description
*/
public $description ;
2019-11-14 12:09:15 +01:00
public $date_debut = '' ; // Date start in PHP server TZ
public $date_fin = '' ; // Date end in PHP server TZ
public $date_debut_gmt = '' ; // Date start in GMT
public $date_fin_gmt = '' ; // Date end in GMT
public $halfday = '' ; // 0:Full days, 2:Start afternoon end morning, -1:Start afternoon end afternoon, 1:Start morning end morning
public $statut = '' ; // 1=draft, 2=validated, 3=approved
2018-10-11 09:22:03 +02:00
/**
2022-08-28 23:32:36 +02:00
* @ var int ID of user that must approve . Real user for approval is fk_user_valid ( old version ) or fk_user_approve ( new versions )
2020-10-31 14:32:18 +01:00
*/
2018-10-04 09:24:57 +02:00
public $fk_validator ;
2018-10-11 09:22:03 +02:00
2021-08-18 00:12:25 +02:00
/**
2022-08-28 23:32:36 +02:00
* @ var int Date of validation or approval . TODO : Use date_valid instead for validation .
2021-08-18 00:12:25 +02:00
*/
2019-11-14 12:09:15 +01:00
public $date_valid = '' ;
2018-10-11 09:22:03 +02:00
/**
2022-08-28 23:32:36 +02:00
* @ var int ID of user that has validated or approved ( empty if not approved )
2020-10-31 14:32:18 +01:00
*/
2018-10-04 09:24:57 +02:00
public $fk_user_valid ;
2018-10-11 09:22:03 +02:00
2022-06-08 20:07:16 +02:00
/**
* @ var int Date approbation
*/
public $date_approbation ;
2021-08-18 00:12:25 +02:00
/**
* @ var int Date for refuse
*/
2019-11-14 12:09:15 +01:00
public $date_refuse = '' ;
2018-10-11 09:22:03 +02:00
/**
2021-08-18 00:12:25 +02:00
* @ var int ID for refuse
2020-10-31 14:32:18 +01:00
*/
2018-10-04 09:24:57 +02:00
public $fk_user_refuse ;
2018-10-11 09:22:03 +02:00
2021-08-18 00:12:25 +02:00
/**
* @ var int Date for cancelation
*/
2019-11-14 12:09:15 +01:00
public $date_cancel = '' ;
2018-10-11 09:22:03 +02:00
/**
2021-08-18 00:12:25 +02:00
* @ var int ID for cancelation
2020-10-31 14:32:18 +01:00
*/
2018-10-04 09:24:57 +02:00
public $fk_user_cancel ;
2018-10-11 09:22:03 +02:00
2021-08-18 00:12:25 +02:00
2019-11-14 12:09:15 +01:00
public $detail_refuse = '' ;
2018-10-11 09:22:03 +02:00
/**
2020-10-31 14:32:18 +01:00
* @ var int ID
*/
2018-10-04 09:24:57 +02:00
public $fk_type ;
public $holiday = array ();
public $events = array ();
public $logs = array ();
public $optName = '' ;
public $optValue = '' ;
public $optRowid = '' ;
2017-10-07 13:09:31 +02:00
2018-04-26 18:25:57 +02:00
/**
* Draft status
*/
const STATUS_DRAFT = 1 ;
/**
* Validated status
*/
const STATUS_VALIDATED = 2 ;
/**
* Approved
*/
const STATUS_APPROVED = 3 ;
/**
* Canceled
*/
const STATUS_CANCELED = 4 ;
/**
* Refused
*/
const STATUS_REFUSED = 5 ;
2017-10-07 13:09:31 +02:00
/**
* Constructor
*
* @ param DoliDB $db Database handler
*/
2019-02-25 20:35:59 +01:00
public function __construct ( $db )
2017-10-07 13:09:31 +02:00
{
$this -> db = $db ;
}
2018-10-25 18:50:53 +02:00
/**
* Returns the reference to the following non used Order depending on the active numbering module
* defined into HOLIDAY_ADDON
*
* @ param Societe $objsoc third party object
* @ return string Holiday free reference
*/
2019-02-25 20:35:59 +01:00
public function getNextNumRef ( $objsoc )
2018-10-25 18:50:53 +02:00
{
global $langs , $conf ;
$langs -> load ( " order " );
2021-02-26 17:59:31 +01:00
if ( empty ( $conf -> global -> HOLIDAY_ADDON )) {
2018-10-25 18:50:53 +02:00
$conf -> global -> HOLIDAY_ADDON = 'mod_holiday_madonna' ;
}
2021-02-26 17:59:31 +01:00
if ( ! empty ( $conf -> global -> HOLIDAY_ADDON )) {
2019-11-14 12:09:15 +01:00
$mybool = false ;
2018-10-25 18:50:53 +02:00
$file = $conf -> global -> HOLIDAY_ADDON . " .php " ;
$classname = $conf -> global -> HOLIDAY_ADDON ;
// Include file with class
2019-11-14 12:09:15 +01:00
$dirmodels = array_merge ( array ( '/' ), ( array ) $conf -> modules_parts [ 'models' ]);
2021-02-26 17:59:31 +01:00
foreach ( $dirmodels as $reldir ) {
2018-10-25 18:50:53 +02:00
$dir = dol_buildpath ( $reldir . " core/modules/holiday/ " );
// Load file with numbering class (if found)
2019-11-14 12:09:15 +01:00
$mybool |= @ include_once $dir . $file ;
2018-10-25 18:50:53 +02:00
}
2021-02-26 17:59:31 +01:00
if ( $mybool === false ) {
2019-01-27 11:55:16 +01:00
dol_print_error ( '' , " Failed to include file " . $file );
2018-10-25 18:50:53 +02:00
return '' ;
}
$obj = new $classname ();
2019-01-27 11:55:16 +01:00
$numref = $obj -> getNextValue ( $objsoc , $this );
2018-10-25 18:50:53 +02:00
2021-02-26 17:59:31 +01:00
if ( $numref != " " ) {
2018-10-25 18:50:53 +02:00
return $numref ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-14 12:09:15 +01:00
$this -> error = $obj -> error ;
2018-10-25 18:50:53 +02:00
//dol_print_error($this->db,get_class($this)."::getNextNumRef ".$obj->error);
return " " ;
}
2020-05-21 15:05:19 +02:00
} else {
2018-10-25 18:50:53 +02:00
print $langs -> trans ( " Error " ) . " " . $langs -> trans ( " Error_HOLIDAY_ADDON_NotDefined " );
return " " ;
}
}
2017-10-07 13:09:31 +02:00
/**
* Update balance of vacations and check table of users for holidays is complete . If not complete .
*
* @ return int < 0 if KO , > 0 if OK
*/
2019-02-25 20:35:59 +01:00
public function updateBalance ()
2017-10-07 13:09:31 +02:00
{
$this -> db -> begin ();
// Update sold of vocations
$result = $this -> updateSoldeCP ();
// Check nb of users into table llx_holiday_users and update with empty lines
//if ($result > 0) $result = $this->verifNbUsers($this->countActiveUsersWithoutCP(), $this->getConfCP('nbUser'));
2021-02-26 17:59:31 +01:00
if ( $result >= 0 ) {
2017-10-07 13:09:31 +02:00
$this -> db -> commit ();
2021-06-10 21:53:06 +02:00
return 0 ; // for cronjob use (0 is OK, any other value is an error code)
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
$this -> db -> rollback ();
return - 1 ;
}
}
/**
* Créer un congés payés dans la base de données
*
* @ param User $user User that create
* @ param int $notrigger 0 = launch triggers after , 1 = disable triggers
* @ return int < 0 if KO , Id of created object if OK
*/
2019-02-25 20:35:59 +01:00
public function create ( $user , $notrigger = 0 )
2017-10-07 13:09:31 +02:00
{
global $conf ;
2019-11-14 12:09:15 +01:00
$error = 0 ;
2017-10-07 13:09:31 +02:00
2019-11-14 12:09:15 +01:00
$now = dol_now ();
2017-10-07 13:09:31 +02:00
// Check parameters
2021-02-26 17:59:31 +01:00
if ( empty ( $this -> fk_user ) || ! is_numeric ( $this -> fk_user ) || $this -> fk_user < 0 ) {
$this -> error = " ErrorBadParameterFkUser " ; return - 1 ;
}
if ( empty ( $this -> fk_validator ) || ! is_numeric ( $this -> fk_validator ) || $this -> fk_validator < 0 ) {
$this -> error = " ErrorBadParameterFkValidator " ; return - 1 ;
}
if ( empty ( $this -> fk_type ) || ! is_numeric ( $this -> fk_type ) || $this -> fk_type < 0 ) {
$this -> error = " ErrorBadParameterFkType " ; return - 1 ;
}
2017-10-07 13:09:31 +02:00
// Insert request
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " holiday( " ;
2019-11-26 12:39:31 +01:00
$sql .= " ref, " ;
2019-11-14 12:09:15 +01:00
$sql .= " fk_user, " ;
$sql .= " date_create, " ;
$sql .= " description, " ;
$sql .= " date_debut, " ;
$sql .= " date_fin, " ;
$sql .= " halfday, " ;
$sql .= " statut, " ;
$sql .= " fk_validator, " ;
$sql .= " fk_type, " ;
$sql .= " fk_user_create, " ;
$sql .= " entity " ;
$sql .= " ) VALUES ( " ;
2019-11-26 12:39:31 +01:00
$sql .= " '(PROV)', " ;
2021-06-14 14:24:18 +02:00
$sql .= " " . (( int ) $this -> fk_user ) . " , " ;
2019-11-14 12:09:15 +01:00
$sql .= " ' " . $this -> db -> idate ( $now ) . " ', " ;
$sql .= " ' " . $this -> db -> escape ( $this -> description ) . " ', " ;
$sql .= " ' " . $this -> db -> idate ( $this -> date_debut ) . " ', " ;
$sql .= " ' " . $this -> db -> idate ( $this -> date_fin ) . " ', " ;
2021-06-14 13:51:09 +02:00
$sql .= " " . (( int ) $this -> halfday ) . " , " ;
2019-11-14 12:09:15 +01:00
$sql .= " '1', " ;
2021-06-14 13:51:09 +02:00
$sql .= " " . (( int ) $this -> fk_validator ) . " , " ;
$sql .= " " . (( int ) $this -> fk_type ) . " , " ;
$sql .= " " . (( int ) $user -> id ) . " , " ;
$sql .= " " . (( int ) $conf -> entity );
2019-11-14 12:09:15 +01:00
$sql .= " ) " ;
2017-10-07 13:09:31 +02:00
$this -> db -> begin ();
dol_syslog ( get_class ( $this ) . " ::create " , LOG_DEBUG );
2019-11-14 12:09:15 +01:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) {
$error ++ ; $this -> errors [] = " Error " . $this -> db -> lasterror ();
2017-10-07 13:09:31 +02:00
}
2021-02-26 17:59:31 +01:00
if ( ! $error ) {
2017-10-07 13:09:31 +02:00
$this -> id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . " holiday " );
2021-02-26 17:59:31 +01:00
if ( $this -> id ) {
2019-11-24 18:25:55 +01:00
// update ref
$initialref = '(PROV' . $this -> id . ')' ;
2021-02-26 17:59:31 +01:00
if ( ! empty ( $this -> ref )) {
$initialref = $this -> ref ;
}
2019-11-24 18:25:55 +01:00
2021-03-14 12:20:23 +01:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . " holiday SET ref=' " . $this -> db -> escape ( $initialref ) . " ' WHERE rowid= " . (( int ) $this -> id );
2021-02-26 17:59:31 +01:00
if ( $this -> db -> query ( $sql )) {
2019-11-24 18:25:55 +01:00
$this -> ref = $initialref ;
2021-02-26 17:59:31 +01:00
if ( ! $error ) {
2019-11-24 18:25:55 +01:00
$result = $this -> insertExtraFields ();
2021-02-26 17:59:31 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2019-11-24 18:25:55 +01:00
}
2021-02-26 17:59:31 +01:00
if ( ! $error && ! $notrigger ) {
2019-11-24 18:25:55 +01:00
// Call trigger
$result = $this -> call_trigger ( 'HOLIDAY_CREATE' , $user );
2021-02-26 17:59:31 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2019-11-24 18:25:55 +01:00
// End call triggers
}
}
2017-10-07 13:09:31 +02:00
}
}
// Commit or rollback
2021-02-26 17:59:31 +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-14 12:09:15 +01:00
$this -> error .= ( $this -> error ? ', ' . $errmsg : $errmsg );
2017-10-07 13:09:31 +02:00
}
$this -> db -> rollback ();
2019-11-14 12:09:15 +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
*
* @ param int $id Id object
2018-10-25 18:50:53 +02:00
* @ param string $ref Ref object
2019-11-24 21:30:00 +01:00
* @ return int < 0 if KO , 0 if not found , > 0 if OK
2017-10-07 13:09:31 +02:00
*/
2019-02-25 20:35:59 +01:00
public function fetch ( $id , $ref = '' )
2017-10-07 13:09:31 +02:00
{
global $langs ;
$sql = " SELECT " ;
2019-11-14 12:09:15 +01:00
$sql .= " cp.rowid, " ;
$sql .= " cp.ref, " ;
$sql .= " cp.fk_user, " ;
$sql .= " cp.date_create, " ;
$sql .= " cp.description, " ;
$sql .= " cp.date_debut, " ;
$sql .= " cp.date_fin, " ;
$sql .= " cp.halfday, " ;
$sql .= " cp.statut, " ;
$sql .= " cp.fk_validator, " ;
$sql .= " cp.date_valid, " ;
$sql .= " cp.fk_user_valid, " ;
$sql .= " cp.date_refuse, " ;
$sql .= " cp.fk_user_refuse, " ;
$sql .= " cp.date_cancel, " ;
$sql .= " cp.fk_user_cancel, " ;
$sql .= " cp.detail_refuse, " ;
$sql .= " cp.note_private, " ;
$sql .= " cp.note_public, " ;
$sql .= " cp.fk_user_create, " ;
$sql .= " cp.fk_type, " ;
$sql .= " cp.entity " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " holiday as cp " ;
2021-02-26 17:59:31 +01:00
if ( $id > 0 ) {
2021-03-14 11:48:39 +01:00
$sql .= " WHERE cp.rowid = " . (( int ) $id );
2021-02-26 17:59:31 +01:00
} else {
$sql .= " WHERE cp.ref = ' " . $this -> db -> escape ( $ref ) . " ' " ;
}
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::fetch " , LOG_DEBUG );
2019-11-14 12:09:15 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-26 17:59:31 +01:00
if ( $resql ) {
if ( $this -> db -> num_rows ( $resql )) {
2017-10-07 13:09:31 +02:00
$obj = $this -> db -> fetch_object ( $resql );
$this -> id = $obj -> rowid ;
2019-11-14 12:09:15 +01:00
$this -> ref = ( $obj -> ref ? $obj -> ref : $obj -> rowid );
2017-10-07 13:09:31 +02:00
$this -> fk_user = $obj -> fk_user ;
$this -> date_create = $this -> db -> jdate ( $obj -> date_create );
$this -> description = $obj -> description ;
$this -> date_debut = $this -> db -> jdate ( $obj -> date_debut );
$this -> date_fin = $this -> db -> jdate ( $obj -> date_fin );
2019-01-27 11:55:16 +01:00
$this -> date_debut_gmt = $this -> db -> jdate ( $obj -> date_debut , 1 );
$this -> date_fin_gmt = $this -> db -> jdate ( $obj -> date_fin , 1 );
2017-10-07 13:09:31 +02:00
$this -> halfday = $obj -> halfday ;
$this -> statut = $obj -> statut ;
$this -> fk_validator = $obj -> fk_validator ;
$this -> date_valid = $this -> db -> jdate ( $obj -> date_valid );
$this -> fk_user_valid = $obj -> fk_user_valid ;
$this -> date_refuse = $this -> db -> jdate ( $obj -> date_refuse );
$this -> fk_user_refuse = $obj -> fk_user_refuse ;
$this -> date_cancel = $this -> db -> jdate ( $obj -> date_cancel );
$this -> fk_user_cancel = $obj -> fk_user_cancel ;
$this -> detail_refuse = $obj -> detail_refuse ;
$this -> note_private = $obj -> note_private ;
$this -> note_public = $obj -> note_public ;
$this -> fk_user_create = $obj -> fk_user_create ;
$this -> fk_type = $obj -> fk_type ;
$this -> entity = $obj -> entity ;
2019-11-24 21:30:00 +01:00
$this -> fetch_optionals ();
$result = 1 ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-24 21:30:00 +01:00
$result = 0 ;
2017-10-07 13:09:31 +02:00
}
$this -> db -> free ( $resql );
2019-11-24 21:30:00 +01:00
return $result ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-14 12:09:15 +01:00
$this -> error = " Error " . $this -> db -> lasterror ();
2017-10-07 13:09:31 +02:00
return - 1 ;
}
}
/**
2018-03-21 19:17:42 +01:00
* List holidays for a particular user or list of users
2017-10-07 13:09:31 +02:00
*
2018-03-21 19:17:42 +01:00
* @ param int | string $user_id ID of user to list , or comma separated list of IDs of users to list
* @ param string $order Sort order
* @ param string $filter SQL Filter
* @ return int - 1 if KO , 1 if OK , 2 if no result
2017-10-07 13:09:31 +02:00
*/
2019-02-25 20:35:59 +01:00
public function fetchByUser ( $user_id , $order = '' , $filter = '' )
2017-10-07 13:09:31 +02:00
{
global $langs , $conf ;
$sql = " SELECT " ;
2019-11-14 12:09:15 +01:00
$sql .= " cp.rowid, " ;
$sql .= " cp.ref, " ;
$sql .= " cp.fk_user, " ;
$sql .= " cp.fk_type, " ;
$sql .= " cp.date_create, " ;
$sql .= " cp.description, " ;
$sql .= " cp.date_debut, " ;
$sql .= " cp.date_fin, " ;
$sql .= " cp.halfday, " ;
$sql .= " cp.statut, " ;
$sql .= " cp.fk_validator, " ;
$sql .= " cp.date_valid, " ;
$sql .= " cp.fk_user_valid, " ;
$sql .= " cp.date_refuse, " ;
$sql .= " cp.fk_user_refuse, " ;
$sql .= " cp.date_cancel, " ;
$sql .= " cp.fk_user_cancel, " ;
$sql .= " cp.detail_refuse, " ;
$sql .= " uu.lastname as user_lastname, " ;
$sql .= " uu.firstname as user_firstname, " ;
$sql .= " uu.login as user_login, " ;
$sql .= " uu.statut as user_statut, " ;
$sql .= " uu.photo as user_photo, " ;
$sql .= " ua.lastname as validator_lastname, " ;
$sql .= " ua.firstname as validator_firstname, " ;
$sql .= " ua.login as validator_login, " ;
$sql .= " ua.statut as validator_statut, " ;
$sql .= " ua.photo as validator_photo " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " holiday as cp, " . MAIN_DB_PREFIX . " user as uu, " . MAIN_DB_PREFIX . " user as ua " ;
$sql .= " WHERE cp.entity IN ( " . getEntity ( 'holiday' ) . " ) " ;
$sql .= " AND cp.fk_user = uu.rowid AND cp.fk_validator = ua.rowid " ; // Hack pour la recherche sur le tableau
2021-03-22 12:47:23 +01:00
$sql .= " AND cp.fk_user IN ( " . $this -> db -> sanitize ( $user_id ) . " ) " ;
2017-10-07 13:09:31 +02:00
2018-07-07 11:03:22 +02:00
// Selection filter
2019-11-14 12:09:15 +01:00
if ( ! empty ( $filter )) {
$sql .= $filter ;
2017-10-07 13:09:31 +02:00
}
2018-07-07 11:03:22 +02:00
// Order of display of the result
2019-11-14 12:09:15 +01:00
if ( ! empty ( $order )) {
$sql .= $order ;
2017-10-07 13:09:31 +02:00
}
dol_syslog ( get_class ( $this ) . " ::fetchByUser " , LOG_DEBUG );
2019-11-14 12:09:15 +01:00
$resql = $this -> db -> query ( $sql );
2017-10-07 13:09:31 +02:00
2018-07-07 11:03:22 +02:00
// If no SQL error
2017-10-07 13:09:31 +02:00
if ( $resql ) {
$i = 0 ;
$tab_result = $this -> holiday ;
$num = $this -> db -> num_rows ( $resql );
2018-07-07 11:03:22 +02:00
// If no registration
2019-11-14 12:09:15 +01:00
if ( ! $num ) {
2017-10-07 13:09:31 +02:00
return 2 ;
}
2018-07-07 11:03:22 +02:00
// List the records and add them to the table
2019-11-14 12:09:15 +01:00
while ( $i < $num ) {
2017-10-07 13:09:31 +02:00
$obj = $this -> db -> fetch_object ( $resql );
$tab_result [ $i ][ 'rowid' ] = $obj -> rowid ;
2019-11-14 12:09:15 +01:00
$tab_result [ $i ][ 'ref' ] = ( $obj -> ref ? $obj -> ref : $obj -> rowid );
2018-10-25 18:50:53 +02:00
2017-10-07 13:09:31 +02:00
$tab_result [ $i ][ 'fk_user' ] = $obj -> fk_user ;
$tab_result [ $i ][ 'fk_type' ] = $obj -> fk_type ;
$tab_result [ $i ][ 'date_create' ] = $this -> db -> jdate ( $obj -> date_create );
$tab_result [ $i ][ 'description' ] = $obj -> description ;
$tab_result [ $i ][ 'date_debut' ] = $this -> db -> jdate ( $obj -> date_debut );
$tab_result [ $i ][ 'date_fin' ] = $this -> db -> jdate ( $obj -> date_fin );
2019-01-27 11:55:16 +01:00
$tab_result [ $i ][ 'date_debut_gmt' ] = $this -> db -> jdate ( $obj -> date_debut , 1 );
$tab_result [ $i ][ 'date_fin_gmt' ] = $this -> db -> jdate ( $obj -> date_fin , 1 );
2017-10-07 13:09:31 +02:00
$tab_result [ $i ][ 'halfday' ] = $obj -> halfday ;
$tab_result [ $i ][ 'statut' ] = $obj -> statut ;
$tab_result [ $i ][ 'fk_validator' ] = $obj -> fk_validator ;
$tab_result [ $i ][ 'date_valid' ] = $this -> db -> jdate ( $obj -> date_valid );
$tab_result [ $i ][ 'fk_user_valid' ] = $obj -> fk_user_valid ;
$tab_result [ $i ][ 'date_refuse' ] = $this -> db -> jdate ( $obj -> date_refuse );
$tab_result [ $i ][ 'fk_user_refuse' ] = $obj -> fk_user_refuse ;
$tab_result [ $i ][ 'date_cancel' ] = $this -> db -> jdate ( $obj -> date_cancel );
$tab_result [ $i ][ 'fk_user_cancel' ] = $obj -> fk_user_cancel ;
$tab_result [ $i ][ 'detail_refuse' ] = $obj -> detail_refuse ;
$tab_result [ $i ][ 'user_firstname' ] = $obj -> user_firstname ;
$tab_result [ $i ][ 'user_lastname' ] = $obj -> user_lastname ;
$tab_result [ $i ][ 'user_login' ] = $obj -> user_login ;
$tab_result [ $i ][ 'user_statut' ] = $obj -> user_statut ;
$tab_result [ $i ][ 'user_photo' ] = $obj -> user_photo ;
$tab_result [ $i ][ 'validator_firstname' ] = $obj -> validator_firstname ;
$tab_result [ $i ][ 'validator_lastname' ] = $obj -> validator_lastname ;
$tab_result [ $i ][ 'validator_login' ] = $obj -> validator_login ;
$tab_result [ $i ][ 'validator_statut' ] = $obj -> validator_statut ;
$tab_result [ $i ][ 'validator_photo' ] = $obj -> validator_photo ;
$i ++ ;
}
2018-07-07 11:03:22 +02:00
// Returns 1 with the filled array
2017-10-07 13:09:31 +02:00
$this -> holiday = $tab_result ;
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2018-08-13 17:26:32 +02:00
// SQL Error
2019-11-14 12:09:15 +01:00
$this -> error = " Error " . $this -> db -> lasterror ();
2017-10-07 13:09:31 +02:00
return - 1 ;
2015-06-27 11:45:23 +02:00
}
2017-10-07 13:09:31 +02:00
}
/**
* List all holidays of all users
*
* @ param string $order Sort order
* @ param string $filter SQL Filter
* @ return int - 1 if KO , 1 if OK , 2 if no result
*/
2019-02-25 20:35:59 +01:00
public function fetchAll ( $order , $filter )
2017-10-07 13:09:31 +02:00
{
global $langs ;
$sql = " SELECT " ;
2019-11-14 12:09:15 +01:00
$sql .= " cp.rowid, " ;
$sql .= " cp.ref, " ;
$sql .= " cp.fk_user, " ;
$sql .= " cp.fk_type, " ;
$sql .= " cp.date_create, " ;
$sql .= " cp.tms as date_update, " ;
$sql .= " cp.description, " ;
$sql .= " cp.date_debut, " ;
$sql .= " cp.date_fin, " ;
$sql .= " cp.halfday, " ;
$sql .= " cp.statut, " ;
$sql .= " cp.fk_validator, " ;
$sql .= " cp.date_valid, " ;
$sql .= " cp.fk_user_valid, " ;
$sql .= " cp.date_refuse, " ;
$sql .= " cp.fk_user_refuse, " ;
$sql .= " cp.date_cancel, " ;
$sql .= " cp.fk_user_cancel, " ;
$sql .= " cp.detail_refuse, " ;
$sql .= " uu.lastname as user_lastname, " ;
$sql .= " uu.firstname as user_firstname, " ;
$sql .= " uu.login as user_login, " ;
$sql .= " uu.statut as user_statut, " ;
$sql .= " uu.photo as user_photo, " ;
$sql .= " ua.lastname as validator_lastname, " ;
$sql .= " ua.firstname as validator_firstname, " ;
$sql .= " ua.login as validator_login, " ;
$sql .= " ua.statut as validator_statut, " ;
$sql .= " ua.photo as validator_photo " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " holiday as cp, " . MAIN_DB_PREFIX . " user as uu, " . MAIN_DB_PREFIX . " user as ua " ;
$sql .= " WHERE cp.entity IN ( " . getEntity ( 'holiday' ) . " ) " ;
$sql .= " AND cp.fk_user = uu.rowid AND cp.fk_validator = ua.rowid " ; // Hack pour la recherche sur le tableau
2017-10-07 13:09:31 +02:00
2018-07-07 11:03:22 +02:00
// Selection filtering
2019-11-14 12:09:15 +01:00
if ( ! empty ( $filter )) {
$sql .= $filter ;
2017-10-07 13:09:31 +02:00
}
2018-07-07 11:03:22 +02:00
// order of display
2019-11-14 12:09:15 +01:00
if ( ! empty ( $order )) {
$sql .= $order ;
2017-10-07 13:09:31 +02:00
}
dol_syslog ( get_class ( $this ) . " ::fetchAll " , LOG_DEBUG );
2019-11-14 12:09:15 +01:00
$resql = $this -> db -> query ( $sql );
2017-10-07 13:09:31 +02:00
2018-07-07 11:03:22 +02:00
// If no SQL error
2017-10-07 13:09:31 +02:00
if ( $resql ) {
$i = 0 ;
$tab_result = $this -> holiday ;
$num = $this -> db -> num_rows ( $resql );
2018-07-07 11:03:22 +02:00
// If no registration
2019-11-14 12:09:15 +01:00
if ( ! $num ) {
2017-10-07 13:09:31 +02:00
return 2 ;
}
2018-07-07 11:03:22 +02:00
// List the records and add them to the table
2019-11-14 12:09:15 +01:00
while ( $i < $num ) {
2017-10-07 13:09:31 +02:00
$obj = $this -> db -> fetch_object ( $resql );
$tab_result [ $i ][ 'rowid' ] = $obj -> rowid ;
2019-11-14 12:09:15 +01:00
$tab_result [ $i ][ 'ref' ] = ( $obj -> ref ? $obj -> ref : $obj -> rowid );
2017-10-07 13:09:31 +02:00
$tab_result [ $i ][ 'fk_user' ] = $obj -> fk_user ;
$tab_result [ $i ][ 'fk_type' ] = $obj -> fk_type ;
$tab_result [ $i ][ 'date_create' ] = $this -> db -> jdate ( $obj -> date_create );
2019-09-24 12:01:18 +02:00
$tab_result [ $i ][ 'date_update' ] = $this -> db -> jdate ( $obj -> date_update );
2017-10-07 13:09:31 +02:00
$tab_result [ $i ][ 'description' ] = $obj -> description ;
$tab_result [ $i ][ 'date_debut' ] = $this -> db -> jdate ( $obj -> date_debut );
$tab_result [ $i ][ 'date_fin' ] = $this -> db -> jdate ( $obj -> date_fin );
2019-01-27 11:55:16 +01:00
$tab_result [ $i ][ 'date_debut_gmt' ] = $this -> db -> jdate ( $obj -> date_debut , 1 );
$tab_result [ $i ][ 'date_fin_gmt' ] = $this -> db -> jdate ( $obj -> date_fin , 1 );
2017-10-07 13:09:31 +02:00
$tab_result [ $i ][ 'halfday' ] = $obj -> halfday ;
$tab_result [ $i ][ 'statut' ] = $obj -> statut ;
$tab_result [ $i ][ 'fk_validator' ] = $obj -> fk_validator ;
$tab_result [ $i ][ 'date_valid' ] = $this -> db -> jdate ( $obj -> date_valid );
$tab_result [ $i ][ 'fk_user_valid' ] = $obj -> fk_user_valid ;
$tab_result [ $i ][ 'date_refuse' ] = $obj -> date_refuse ;
$tab_result [ $i ][ 'fk_user_refuse' ] = $obj -> fk_user_refuse ;
$tab_result [ $i ][ 'date_cancel' ] = $obj -> date_cancel ;
$tab_result [ $i ][ 'fk_user_cancel' ] = $obj -> fk_user_cancel ;
$tab_result [ $i ][ 'detail_refuse' ] = $obj -> detail_refuse ;
$tab_result [ $i ][ 'user_firstname' ] = $obj -> user_firstname ;
$tab_result [ $i ][ 'user_lastname' ] = $obj -> user_lastname ;
$tab_result [ $i ][ 'user_login' ] = $obj -> user_login ;
$tab_result [ $i ][ 'user_statut' ] = $obj -> user_statut ;
$tab_result [ $i ][ 'user_photo' ] = $obj -> user_photo ;
$tab_result [ $i ][ 'validator_firstname' ] = $obj -> validator_firstname ;
$tab_result [ $i ][ 'validator_lastname' ] = $obj -> validator_lastname ;
$tab_result [ $i ][ 'validator_login' ] = $obj -> validator_login ;
$tab_result [ $i ][ 'validator_statut' ] = $obj -> validator_statut ;
$tab_result [ $i ][ 'validator_photo' ] = $obj -> validator_photo ;
$i ++ ;
}
2018-07-07 11:03:22 +02:00
// Returns 1 and adds the array to the variable
2017-10-07 13:09:31 +02:00
$this -> holiday = $tab_result ;
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2018-08-13 17:26:32 +02:00
// SQL Error
2019-11-14 12:09:15 +01:00
$this -> error = " Error " . $this -> db -> lasterror ();
2017-10-07 13:09:31 +02:00
return - 1 ;
}
}
2018-10-25 12:45:13 +02:00
/**
* Validate leave request
*
* @ param User $user User that validate
* @ param int $notrigger 0 = launch triggers after , 1 = disable triggers
* @ return int < 0 if KO , > 0 if OK
*/
2019-02-25 20:35:59 +01:00
public function validate ( $user = null , $notrigger = 0 )
2018-10-25 12:45:13 +02:00
{
global $conf , $langs ;
2021-10-22 15:27:16 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
2019-11-14 12:09:15 +01:00
$error = 0 ;
2018-10-25 12:45:13 +02:00
2022-05-09 15:57:10 +02:00
$checkBalance = getDictionaryValue ( 'c_holiday_types' , 'block_if_negative' , $this -> fk_type );
2021-06-22 16:27:43 +02:00
if ( $checkBalance > 0 ) {
$balance = $this -> getCPforUser ( $this -> fk_user , $this -> fk_type );
if ( $balance < 0 ) {
$this -> error = 'LeaveRequestCreationBlockedBecauseBalanceIsNegative' ;
return - 1 ;
}
}
2018-10-25 18:50:53 +02:00
// Define new ref
2021-02-26 17:59:31 +01:00
if ( ! $error && ( preg_match ( '/^[\(]?PROV/i' , $this -> ref ) || empty ( $this -> ref ) || $this -> ref == $this -> id )) {
2018-10-25 18:50:53 +02:00
$num = $this -> getNextNumRef ( null );
2020-05-21 15:05:19 +02:00
} else {
2018-10-25 18:50:53 +02:00
$num = $this -> ref ;
2018-10-25 12:45:13 +02:00
}
2020-03-03 11:49:17 +01:00
$this -> newref = dol_sanitizeFileName ( $num );
2018-10-25 18:50:53 +02:00
// Update status
$sql = " UPDATE " . MAIN_DB_PREFIX . " holiday SET " ;
2019-11-14 12:09:15 +01:00
if ( ! empty ( $this -> statut ) && is_numeric ( $this -> statut )) {
2021-08-18 00:10:48 +02:00
$sql .= " statut = " . (( int ) $this -> statut ) . " , " ;
2018-10-25 12:45:13 +02:00
} else {
$error ++ ;
}
2019-11-14 12:09:15 +01:00
$sql .= " ref = ' " . $this -> db -> escape ( $num ) . " ' " ;
2021-08-27 18:18:50 +02:00
$sql .= " WHERE rowid = " . (( int ) $this -> id );
2018-10-25 12:45:13 +02:00
$this -> db -> begin ();
dol_syslog ( get_class ( $this ) . " ::validate " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
2019-11-14 12:09:15 +01:00
if ( ! $resql ) {
$error ++ ; $this -> errors [] = " Error " . $this -> db -> lasterror ();
2018-10-25 12:45:13 +02:00
}
2021-02-26 17:59:31 +01:00
if ( ! $error ) {
if ( ! $notrigger ) {
2018-10-25 12:45:13 +02:00
// Call trigger
2019-11-14 12:09:15 +01:00
$result = $this -> call_trigger ( 'HOLIDAY_VALIDATE' , $user );
2021-02-26 17:59:31 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2018-10-25 12:45:13 +02:00
// End call triggers
}
}
2021-10-22 15:27:16 +02:00
if ( ! $error ) {
$this -> oldref = $this -> ref ;
// Rename directory if dir was a temporary ref
if ( preg_match ( '/^[\(]?PROV/i' , $this -> ref )) {
// Now we rename also files into index
$sql = 'UPDATE ' . MAIN_DB_PREFIX . " ecm_files set filename = CONCAT(' " . $this -> db -> escape ( $this -> newref ) . " ', SUBSTR(filename, " . ( strlen ( $this -> ref ) + 1 ) . " )), filepath = 'holiday/ " . $this -> db -> escape ( $this -> newref ) . " ' " ;
2021-10-22 15:35:30 +02:00
$sql .= " WHERE filename LIKE ' " . $this -> db -> escape ( $this -> ref ) . " %' AND filepath = 'holiday/ " . $this -> db -> escape ( $this -> ref ) . " ' and entity = " . (( int ) $conf -> entity );
2021-10-22 15:27:16 +02:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) {
$error ++ ;
$this -> error = $this -> db -> lasterror ();
}
// We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments
$oldref = dol_sanitizeFileName ( $this -> ref );
$newref = dol_sanitizeFileName ( $num );
$dirsource = $conf -> holiday -> multidir_output [ $this -> entity ] . '/' . $oldref ;
$dirdest = $conf -> holiday -> multidir_output [ $this -> entity ] . '/' . $newref ;
if ( ! $error && file_exists ( $dirsource )) {
dol_syslog ( get_class ( $this ) . " ::validate rename dir " . $dirsource . " into " . $dirdest );
if ( @ rename ( $dirsource , $dirdest )) {
dol_syslog ( " Rename ok " );
// Rename docs starting with $oldref with $newref
$listoffiles = dol_dir_list ( $dirdest , 'files' , 1 , '^' . preg_quote ( $oldref , '/' ));
foreach ( $listoffiles as $fileentry ) {
$dirsource = $fileentry [ 'name' ];
$dirdest = preg_replace ( '/^' . preg_quote ( $oldref , '/' ) . '/' , $newref , $dirsource );
$dirsource = $fileentry [ 'path' ] . '/' . $dirsource ;
$dirdest = $fileentry [ 'path' ] . '/' . $dirdest ;
@ rename ( $dirsource , $dirdest );
}
}
}
}
}
2018-10-25 12:45:13 +02:00
// Commit or rollback
2021-02-26 17:59:31 +01:00
if ( $error ) {
foreach ( $this -> errors as $errmsg ) {
2018-10-25 12:45:13 +02:00
dol_syslog ( get_class ( $this ) . " ::validate " . $errmsg , LOG_ERR );
2019-11-14 12:09:15 +01:00
$this -> error .= ( $this -> error ? ', ' . $errmsg : $errmsg );
2018-10-25 12:45:13 +02:00
}
$this -> db -> rollback ();
2019-11-14 12:09:15 +01:00
return - 1 * $error ;
2020-05-21 15:05:19 +02:00
} else {
2018-10-25 12:45:13 +02:00
$this -> db -> commit ();
return 1 ;
}
}
/**
* Approve leave request
*
* @ param User $user User that approve
* @ param int $notrigger 0 = launch triggers after , 1 = disable triggers
* @ return int < 0 if KO , > 0 if OK
*/
2019-02-25 20:35:59 +01:00
public function approve ( $user = null , $notrigger = 0 )
2018-10-25 12:45:13 +02:00
{
global $conf , $langs ;
2019-11-14 12:09:15 +01:00
$error = 0 ;
2018-10-25 12:45:13 +02:00
2022-05-09 15:57:10 +02:00
$checkBalance = getDictionaryValue ( 'c_holiday_types' , 'block_if_negative' , $this -> fk_type );
2021-06-22 16:27:43 +02:00
if ( $checkBalance > 0 ) {
$balance = $this -> getCPforUser ( $this -> fk_user , $this -> fk_type );
if ( $balance < 0 ) {
$this -> error = 'LeaveRequestCreationBlockedBecauseBalanceIsNegative' ;
return - 1 ;
}
}
2018-10-25 12:45:13 +02:00
// Update request
$sql = " UPDATE " . MAIN_DB_PREFIX . " holiday SET " ;
2019-11-14 12:09:15 +01:00
$sql .= " description= ' " . $this -> db -> escape ( $this -> description ) . " ', " ;
2018-10-25 12:45:13 +02:00
2019-11-14 12:09:15 +01:00
if ( ! empty ( $this -> date_debut )) {
$sql .= " date_debut = ' " . $this -> db -> idate ( $this -> date_debut ) . " ', " ;
2018-10-25 12:45:13 +02:00
} else {
$error ++ ;
}
2019-11-14 12:09:15 +01:00
if ( ! empty ( $this -> date_fin )) {
$sql .= " date_fin = ' " . $this -> db -> idate ( $this -> date_fin ) . " ', " ;
2018-10-25 12:45:13 +02:00
} else {
$error ++ ;
}
2021-08-18 00:10:48 +02:00
$sql .= " halfday = " . (( int ) $this -> halfday ) . " , " ;
2019-11-14 12:09:15 +01:00
if ( ! empty ( $this -> statut ) && is_numeric ( $this -> statut )) {
2021-08-18 00:10:48 +02:00
$sql .= " statut = " . (( int ) $this -> statut ) . " , " ;
2018-10-25 12:45:13 +02:00
} else {
$error ++ ;
}
2019-11-14 12:09:15 +01:00
if ( ! empty ( $this -> fk_validator )) {
$sql .= " fk_validator = ' " . $this -> db -> escape ( $this -> fk_validator ) . " ', " ;
2018-10-25 12:45:13 +02:00
} else {
$error ++ ;
}
2019-11-14 12:09:15 +01:00
if ( ! empty ( $this -> date_valid )) {
$sql .= " date_valid = ' " . $this -> db -> idate ( $this -> date_valid ) . " ', " ;
2018-10-25 12:45:13 +02:00
} else {
2019-11-14 12:09:15 +01:00
$sql .= " date_valid = NULL, " ;
2018-10-25 12:45:13 +02:00
}
2019-11-14 12:09:15 +01:00
if ( ! empty ( $this -> fk_user_valid )) {
$sql .= " fk_user_valid = ' " . $this -> db -> escape ( $this -> fk_user_valid ) . " ', " ;
2018-10-25 12:45:13 +02:00
} else {
2019-11-14 12:09:15 +01:00
$sql .= " fk_user_valid = NULL, " ;
2018-10-25 12:45:13 +02:00
}
2019-11-14 12:09:15 +01:00
if ( ! empty ( $this -> date_refuse )) {
$sql .= " date_refuse = ' " . $this -> db -> idate ( $this -> date_refuse ) . " ', " ;
2018-10-25 12:45:13 +02:00
} else {
2019-11-14 12:09:15 +01:00
$sql .= " date_refuse = NULL, " ;
2018-10-25 12:45:13 +02:00
}
2019-11-14 12:09:15 +01:00
if ( ! empty ( $this -> fk_user_refuse )) {
$sql .= " fk_user_refuse = ' " . $this -> db -> escape ( $this -> fk_user_refuse ) . " ', " ;
2018-10-25 12:45:13 +02:00
} else {
2019-11-14 12:09:15 +01:00
$sql .= " fk_user_refuse = NULL, " ;
2018-10-25 12:45:13 +02:00
}
2019-11-14 12:09:15 +01:00
if ( ! empty ( $this -> date_cancel )) {
$sql .= " date_cancel = ' " . $this -> db -> idate ( $this -> date_cancel ) . " ', " ;
2018-10-25 12:45:13 +02:00
} else {
2019-11-14 12:09:15 +01:00
$sql .= " date_cancel = NULL, " ;
2018-10-25 12:45:13 +02:00
}
2019-11-14 12:09:15 +01:00
if ( ! empty ( $this -> fk_user_cancel )) {
$sql .= " fk_user_cancel = ' " . $this -> db -> escape ( $this -> fk_user_cancel ) . " ', " ;
2018-10-25 12:45:13 +02:00
} else {
2019-11-14 12:09:15 +01:00
$sql .= " fk_user_cancel = NULL, " ;
2018-10-25 12:45:13 +02:00
}
2019-11-14 12:09:15 +01:00
if ( ! empty ( $this -> detail_refuse )) {
$sql .= " detail_refuse = ' " . $this -> db -> escape ( $this -> detail_refuse ) . " ' " ;
2018-10-25 12:45:13 +02:00
} else {
2019-11-14 12:09:15 +01:00
$sql .= " detail_refuse = NULL " ;
2018-10-25 12:45:13 +02:00
}
2021-08-27 18:18:50 +02:00
$sql .= " WHERE rowid = " . (( int ) $this -> id );
2018-10-25 12:45:13 +02:00
$this -> db -> begin ();
dol_syslog ( get_class ( $this ) . " ::approve " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
2019-11-14 12:09:15 +01:00
if ( ! $resql ) {
$error ++ ; $this -> errors [] = " Error " . $this -> db -> lasterror ();
2018-10-25 12:45:13 +02:00
}
2021-02-26 17:59:31 +01:00
if ( ! $error ) {
if ( ! $notrigger ) {
2018-10-25 12:45:13 +02:00
// Call trigger
2019-11-14 12:09:15 +01:00
$result = $this -> call_trigger ( 'HOLIDAY_APPROVE' , $user );
2021-02-26 17:59:31 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2018-10-25 12:45:13 +02:00
// End call triggers
}
}
// Commit or rollback
2021-02-26 17:59:31 +01:00
if ( $error ) {
foreach ( $this -> errors as $errmsg ) {
2018-10-25 12:45:13 +02:00
dol_syslog ( get_class ( $this ) . " ::approve " . $errmsg , LOG_ERR );
2019-11-14 12:09:15 +01:00
$this -> error .= ( $this -> error ? ', ' . $errmsg : $errmsg );
2018-10-25 12:45:13 +02:00
}
$this -> db -> rollback ();
2019-11-14 12:09:15 +01:00
return - 1 * $error ;
2020-05-21 15:05:19 +02:00
} else {
2018-10-25 12:45:13 +02:00
$this -> db -> commit ();
return 1 ;
}
}
2017-10-07 13:09:31 +02:00
/**
* Update database
*
* @ param User $user User that modify
* @ param int $notrigger 0 = launch triggers after , 1 = disable triggers
* @ return int < 0 if KO , > 0 if OK
*/
2019-02-25 20:35:59 +01:00
public function update ( $user = null , $notrigger = 0 )
2017-10-07 13:09:31 +02:00
{
global $conf , $langs ;
2019-11-14 12:09:15 +01:00
$error = 0 ;
2017-10-07 13:09:31 +02:00
2022-05-09 15:57:10 +02:00
$checkBalance = getDictionaryValue ( 'c_holiday_types' , 'block_if_negative' , $this -> fk_type );
2021-06-22 16:27:43 +02:00
2021-09-30 17:48:20 +02:00
if ( $checkBalance > 0 && $this -> statut != self :: STATUS_DRAFT ) {
2021-06-22 16:27:43 +02:00
$balance = $this -> getCPforUser ( $this -> fk_user , $this -> fk_type );
if ( $balance < 0 ) {
$this -> error = 'LeaveRequestCreationBlockedBecauseBalanceIsNegative' ;
return - 1 ;
}
}
2017-10-07 13:09:31 +02:00
// Update request
$sql = " UPDATE " . MAIN_DB_PREFIX . " holiday SET " ;
2019-11-14 12:09:15 +01:00
$sql .= " description= ' " . $this -> db -> escape ( $this -> description ) . " ', " ;
2017-10-07 13:09:31 +02:00
2019-11-14 12:09:15 +01:00
if ( ! empty ( $this -> date_debut )) {
$sql .= " date_debut = ' " . $this -> db -> idate ( $this -> date_debut ) . " ', " ;
2017-10-07 13:09:31 +02:00
} else {
$error ++ ;
}
2019-11-14 12:09:15 +01:00
if ( ! empty ( $this -> date_fin )) {
$sql .= " date_fin = ' " . $this -> db -> idate ( $this -> date_fin ) . " ', " ;
2017-10-07 13:09:31 +02:00
} else {
$error ++ ;
}
2019-11-14 12:09:15 +01:00
$sql .= " halfday = " . $this -> halfday . " , " ;
if ( ! empty ( $this -> statut ) && is_numeric ( $this -> statut )) {
$sql .= " statut = " . $this -> statut . " , " ;
2017-10-07 13:09:31 +02:00
} else {
$error ++ ;
}
2019-11-14 12:09:15 +01:00
if ( ! empty ( $this -> fk_validator )) {
$sql .= " fk_validator = ' " . $this -> db -> escape ( $this -> fk_validator ) . " ', " ;
2017-10-07 13:09:31 +02:00
} else {
$error ++ ;
}
2019-11-14 12:09:15 +01:00
if ( ! empty ( $this -> date_valid )) {
$sql .= " date_valid = ' " . $this -> db -> idate ( $this -> date_valid ) . " ', " ;
2017-10-07 13:09:31 +02:00
} else {
2019-11-14 12:09:15 +01:00
$sql .= " date_valid = NULL, " ;
2017-10-07 13:09:31 +02:00
}
2019-11-14 12:09:15 +01:00
if ( ! empty ( $this -> fk_user_valid )) {
$sql .= " fk_user_valid = ' " . $this -> db -> escape ( $this -> fk_user_valid ) . " ', " ;
2017-10-07 13:09:31 +02:00
} else {
2019-11-14 12:09:15 +01:00
$sql .= " fk_user_valid = NULL, " ;
2017-10-07 13:09:31 +02:00
}
2019-11-14 12:09:15 +01:00
if ( ! empty ( $this -> date_refuse )) {
$sql .= " date_refuse = ' " . $this -> db -> idate ( $this -> date_refuse ) . " ', " ;
2017-10-07 13:09:31 +02:00
} else {
2019-11-14 12:09:15 +01:00
$sql .= " date_refuse = NULL, " ;
2017-10-07 13:09:31 +02:00
}
2019-11-14 12:09:15 +01:00
if ( ! empty ( $this -> fk_user_refuse )) {
$sql .= " fk_user_refuse = ' " . $this -> db -> escape ( $this -> fk_user_refuse ) . " ', " ;
2017-10-07 13:09:31 +02:00
} else {
2019-11-14 12:09:15 +01:00
$sql .= " fk_user_refuse = NULL, " ;
2017-10-07 13:09:31 +02:00
}
2019-11-14 12:09:15 +01:00
if ( ! empty ( $this -> date_cancel )) {
$sql .= " date_cancel = ' " . $this -> db -> idate ( $this -> date_cancel ) . " ', " ;
2017-10-07 13:09:31 +02:00
} else {
2019-11-14 12:09:15 +01:00
$sql .= " date_cancel = NULL, " ;
2017-10-07 13:09:31 +02:00
}
2019-11-14 12:09:15 +01:00
if ( ! empty ( $this -> fk_user_cancel )) {
$sql .= " fk_user_cancel = ' " . $this -> db -> escape ( $this -> fk_user_cancel ) . " ', " ;
2017-10-07 13:09:31 +02:00
} else {
2019-11-14 12:09:15 +01:00
$sql .= " fk_user_cancel = NULL, " ;
2017-10-07 13:09:31 +02:00
}
2019-11-14 12:09:15 +01:00
if ( ! empty ( $this -> detail_refuse )) {
$sql .= " detail_refuse = ' " . $this -> db -> escape ( $this -> detail_refuse ) . " ' " ;
2017-10-07 13:09:31 +02:00
} else {
2019-11-14 12:09:15 +01:00
$sql .= " detail_refuse = NULL " ;
2017-10-07 13:09:31 +02:00
}
2021-08-27 18:18:50 +02: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 );
2019-11-14 12:09:15 +01:00
if ( ! $resql ) {
$error ++ ; $this -> errors [] = " Error " . $this -> db -> lasterror ();
2017-10-07 13:09:31 +02:00
}
2021-02-26 17:59:31 +01:00
if ( ! $error ) {
if ( ! $notrigger ) {
2017-10-07 13:09:31 +02:00
// Call trigger
2019-11-14 12:09:15 +01:00
$result = $this -> call_trigger ( 'HOLIDAY_MODIFY' , $user );
2021-02-26 17:59:31 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2017-10-07 13:09:31 +02:00
// End call triggers
}
}
// Commit or rollback
2021-02-26 17:59:31 +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-14 12:09:15 +01:00
$this -> error .= ( $this -> error ? ', ' . $errmsg : $errmsg );
2017-10-07 13:09:31 +02:00
}
$this -> db -> rollback ();
2019-11-14 12:09:15 +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 object in database
*
* @ param User $user User that delete
* @ param int $notrigger 0 = launch triggers after , 1 = disable triggers
* @ return int < 0 if KO , > 0 if OK
*/
2019-02-25 20:35:59 +01:00
public function delete ( $user , $notrigger = 0 )
2017-10-07 13:09:31 +02:00
{
global $conf , $langs ;
2019-11-14 12:09:15 +01:00
$error = 0 ;
2017-10-07 13:09:31 +02:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " holiday " ;
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 ) . " ::delete " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
2019-11-14 12:09:15 +01:00
if ( ! $resql ) {
$error ++ ; $this -> errors [] = " Error " . $this -> db -> lasterror ();
2017-10-07 13:09:31 +02:00
}
2021-02-26 17:59:31 +01:00
if ( ! $error ) {
if ( ! $notrigger ) {
2017-10-07 13:09:31 +02:00
// Call trigger
2019-11-14 12:09:15 +01:00
$result = $this -> call_trigger ( 'HOLIDAY_DELETE' , $user );
2021-02-26 17:59:31 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2017-10-07 13:09:31 +02:00
// End call triggers
}
}
// Commit or rollback
2021-02-26 17:59:31 +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-14 12:09:15 +01:00
$this -> error .= ( $this -> error ? ', ' . $errmsg : $errmsg );
2017-10-07 13:09:31 +02:00
}
$this -> db -> rollback ();
2019-11-14 12:09:15 +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 ;
}
}
/**
* Check if a user is on holiday ( partially or completely ) into a period .
* This function can be used to avoid to have 2 leave requests on same period for example .
* Warning : It consumes a lot of memory because it load in -> holiday all holiday of a dedicated user at each call .
*
2019-03-01 18:28:51 +01:00
* @ param int $fk_user Id user
2019-03-15 18:09:44 +01:00
* @ param integer $dateStart Start date of period to check
* @ param integer $dateEnd End date of period to check
2019-03-01 18:28:51 +01:00
* @ param int $halfday Tag to define how start and end the period to check :
* 0 : Full days , 2 : Start afternoon end morning , - 1 : Start afternoon end afternoon , 1 : Start morning end morning
2017-12-02 01:13:32 +01:00
* @ return boolean False = New range overlap an existing holiday , True = no overlapping ( is never on holiday during checked period ) .
2019-03-15 18:09:44 +01:00
* @ see verifDateHolidayForTimestamp ()
2017-10-07 13:09:31 +02:00
*/
2019-02-25 20:35:59 +01:00
public function verifDateHolidayCP ( $fk_user , $dateStart , $dateEnd , $halfday = 0 )
2017-10-07 13:09:31 +02:00
{
2019-01-27 11:55:16 +01:00
$this -> fetchByUser ( $fk_user , '' , '' );
2017-10-07 13:09:31 +02:00
2021-02-26 17:59:31 +01:00
foreach ( $this -> holiday as $infos_CP ) {
if ( $infos_CP [ 'statut' ] == 4 ) {
continue ; // ignore not validated holidays
}
if ( $infos_CP [ 'statut' ] == 5 ) {
continue ; // ignore not validated holidays
}
2022-05-17 14:55:38 +02:00
//var_dump("--");
//var_dump("old: ".dol_print_date($infos_CP['date_debut'],'dayhour').' '.dol_print_date($infos_CP['date_fin'],'dayhour').' '.$infos_CP['halfday']);
//var_dump("new: ".dol_print_date($dateStart,'dayhour').' '.dol_print_date($dateEnd,'dayhour').' '.$halfday);
2017-10-07 13:09:31 +02:00
2021-02-26 17:59:31 +01:00
if ( $halfday == 0 ) {
if ( $dateStart >= $infos_CP [ 'date_debut' ] && $dateStart <= $infos_CP [ 'date_fin' ]) {
2017-12-02 01:13:32 +01:00
return false ;
}
2021-02-26 17:59:31 +01:00
if ( $dateEnd <= $infos_CP [ 'date_fin' ] && $dateEnd >= $infos_CP [ 'date_debut' ]) {
2017-10-07 13:09:31 +02:00
return false ;
}
2021-02-26 17:59:31 +01:00
} elseif ( $halfday == - 1 ) {
2017-12-02 01:13:32 +01:00
// new start afternoon, new end afternoon
2021-02-26 17:59:31 +01:00
if ( $dateStart >= $infos_CP [ 'date_debut' ] && $dateStart <= $infos_CP [ 'date_fin' ]) {
if ( $dateStart < $infos_CP [ 'date_fin' ] || in_array ( $infos_CP [ 'halfday' ], array ( 0 , - 1 ))) {
return false ;
}
2017-12-02 01:13:32 +01:00
}
2021-02-26 17:59:31 +01:00
if ( $dateEnd <= $infos_CP [ 'date_fin' ] && $dateEnd >= $infos_CP [ 'date_debut' ]) {
if ( $dateStart < $dateEnd ) {
return false ;
}
if ( $dateEnd < $infos_CP [ 'date_fin' ] || in_array ( $infos_CP [ 'halfday' ], array ( 0 , - 1 ))) {
return false ;
}
2017-10-07 13:09:31 +02:00
}
2021-02-26 17:59:31 +01:00
} elseif ( $halfday == 1 ) {
2017-12-02 01:13:32 +01:00
// new start morning, new end morning
2021-02-26 17:59:31 +01:00
if ( $dateStart >= $infos_CP [ 'date_debut' ] && $dateStart <= $infos_CP [ 'date_fin' ]) {
if ( $dateStart < $dateEnd ) {
return false ;
}
if ( $dateStart > $infos_CP [ 'date_debut' ] || in_array ( $infos_CP [ 'halfday' ], array ( 0 , 1 ))) {
return false ;
}
2017-12-02 01:13:32 +01:00
}
2021-02-26 17:59:31 +01:00
if ( $dateEnd <= $infos_CP [ 'date_fin' ] && $dateEnd >= $infos_CP [ 'date_debut' ]) {
if ( $dateEnd > $infos_CP [ 'date_debut' ] || in_array ( $infos_CP [ 'halfday' ], array ( 0 , 1 ))) {
return false ;
}
2017-10-07 13:09:31 +02:00
}
2021-02-26 17:59:31 +01:00
} elseif ( $halfday == 2 ) {
2017-12-02 01:13:32 +01:00
// new start afternoon, new end morning
2021-02-26 17:59:31 +01:00
if ( $dateStart >= $infos_CP [ 'date_debut' ] && $dateStart <= $infos_CP [ 'date_fin' ]) {
if ( $dateStart < $infos_CP [ 'date_fin' ] || in_array ( $infos_CP [ 'halfday' ], array ( 0 , - 1 ))) {
return false ;
}
2017-12-02 01:13:32 +01:00
}
2021-02-26 17:59:31 +01:00
if ( $dateEnd <= $infos_CP [ 'date_fin' ] && $dateEnd >= $infos_CP [ 'date_debut' ]) {
if ( $dateEnd > $infos_CP [ 'date_debut' ] || in_array ( $infos_CP [ 'halfday' ], array ( 0 , 1 ))) {
return false ;
}
2017-10-07 13:09:31 +02:00
}
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
dol_print_error ( '' , 'Bad value of parameter halfday when calling function verifDateHolidayCP' );
}
}
return true ;
}
/**
2020-11-05 19:09:58 +01:00
* Check that a user is not on holiday for a particular timestamp . Can check approved leave requests and not into public holidays of company .
2017-10-07 13:09:31 +02:00
*
* @ param int $fk_user Id user
2019-03-15 18:09:44 +01:00
* @ param integer $timestamp Time stamp date for a day ( YYYY - MM - DD ) without hours ( = 12 : 00 AM in english and not 12 : 00 PM that is 12 : 00 )
2018-04-26 17:44:36 +02:00
* @ param string $status Filter on holiday status . '-1' = no filter .
2017-10-07 13:09:31 +02:00
* @ return array array ( 'morning' => , 'afternoon' => ), Boolean is true if user is available for day timestamp .
2019-03-15 18:09:44 +01:00
* @ see verifDateHolidayCP ()
2017-10-07 13:09:31 +02:00
*/
2019-02-25 20:35:59 +01:00
public function verifDateHolidayForTimestamp ( $fk_user , $timestamp , $status = '-1' )
2017-10-07 13:09:31 +02:00
{
global $langs , $conf ;
2019-11-14 12:09:15 +01:00
$isavailablemorning = true ;
$isavailableafternoon = true ;
2017-10-07 13:09:31 +02:00
2020-11-05 19:09:58 +01:00
// Check into leave requests
2018-04-26 17:44:36 +02:00
$sql = " SELECT cp.rowid, cp.date_debut as date_start, cp.date_fin as date_end, cp.halfday, cp.statut " ;
2019-11-14 12:09:15 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " holiday as cp " ;
$sql .= " WHERE cp.entity IN ( " . getEntity ( 'holiday' ) . " ) " ;
$sql .= " AND cp.fk_user = " . ( int ) $fk_user ;
$sql .= " AND cp.date_debut <= ' " . $this -> db -> idate ( $timestamp ) . " ' AND cp.date_fin >= ' " . $this -> db -> idate ( $timestamp ) . " ' " ;
2021-02-26 17:59:31 +01:00
if ( $status != '-1' ) {
2021-03-22 11:30:18 +01:00
$sql .= " AND cp.statut IN ( " . $this -> db -> sanitize ( $status ) . " ) " ;
2021-02-26 17:59:31 +01:00
}
2017-09-19 16:40:55 +02:00
2017-10-07 13:09:31 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-26 17:59:31 +01:00
if ( $resql ) {
2019-11-14 12:09:15 +01:00
$num_rows = $this -> db -> num_rows ( $resql ); // Note, we can have 2 records if on is morning and the other one is afternoon
2021-02-26 17:59:31 +01:00
if ( $num_rows > 0 ) {
2019-11-14 12:09:15 +01:00
$arrayofrecord = array ();
$i = 0 ;
2021-02-26 17:59:31 +01:00
while ( $i < $num_rows ) {
2017-10-07 13:09:31 +02:00
$obj = $this -> db -> fetch_object ( $resql );
// Note: $obj->halfday is 0:Full days, 2:Sart afternoon end morning, -1:Start afternoon, 1:End morning
2019-11-14 12:09:15 +01:00
$arrayofrecord [ $obj -> rowid ] = array ( 'date_start' => $this -> db -> jdate ( $obj -> date_start ), 'date_end' => $this -> db -> jdate ( $obj -> date_end ), 'halfday' => $obj -> halfday );
2017-10-07 13:09:31 +02:00
$i ++ ;
}
// We found a record, user is on holiday by default, so is not available is true.
$isavailablemorning = true ;
2021-02-26 17:59:31 +01:00
foreach ( $arrayofrecord as $record ) {
if ( $timestamp == $record [ 'date_start' ] && $record [ 'halfday' ] == 2 ) {
continue ;
}
if ( $timestamp == $record [ 'date_start' ] && $record [ 'halfday' ] == - 1 ) {
continue ;
}
2017-10-07 13:09:31 +02:00
$isavailablemorning = false ;
break ;
}
$isavailableafternoon = true ;
2021-02-26 17:59:31 +01:00
foreach ( $arrayofrecord as $record ) {
if ( $timestamp == $record [ 'date_end' ] && $record [ 'halfday' ] == 2 ) {
continue ;
}
if ( $timestamp == $record [ 'date_end' ] && $record [ 'halfday' ] == 1 ) {
continue ;
}
2017-10-07 13:09:31 +02:00
$isavailableafternoon = false ;
break ;
}
}
2021-02-26 17:59:31 +01:00
} else {
dol_print_error ( $this -> db );
}
2017-09-19 16:40:55 +02:00
2020-11-05 19:09:58 +01:00
$result = array ( 'morning' => $isavailablemorning , 'afternoon' => $isavailableafternoon );
2021-02-26 17:59:31 +01:00
if ( ! $isavailablemorning ) {
$result [ 'morning_reason' ] = 'leave_request' ;
}
if ( ! $isavailableafternoon ) {
$result [ 'afternoon_reason' ] = 'leave_request' ;
}
2020-11-05 19:09:58 +01:00
return $result ;
2017-10-07 13:09:31 +02:00
}
/**
* Return clicable name ( with picto eventually )
*
* @ param int $withpicto 0 = _No picto , 1 = Includes the picto in the linkn , 2 = Picto only
* @ param int $save_lastsearch_value - 1 = Auto , 0 = No save of lastsearch_values when clicking , 1 = Save lastsearch_values whenclicking
2021-03-14 20:51:02 +01:00
* @ param int $notooltip 1 = Disable tooltip
2017-10-07 13:09:31 +02:00
* @ return string String with URL
*/
2021-03-14 20:51:02 +01:00
public function getNomUrl ( $withpicto = 0 , $save_lastsearch_value = - 1 , $notooltip = 0 )
2017-10-07 13:09:31 +02:00
{
2022-02-15 18:14:44 +01:00
global $langs , $hookmanager ;
2017-10-07 13:09:31 +02:00
2019-11-14 12:09:15 +01:00
$result = '' ;
2017-11-02 15:03:09 +01:00
2020-11-04 18:05:48 +01:00
$label = img_picto ( '' , $this -> picto ) . ' <u class="paddingrightonly">' . $langs -> trans ( " Holiday " ) . '</u>' ;
2020-09-11 13:14:14 +02:00
if ( isset ( $this -> statut )) {
2020-11-04 18:05:48 +01:00
$label .= ' ' . $this -> getLibStatut ( 5 );
2020-09-11 13:14:14 +02:00
}
2020-11-04 18:05:48 +01:00
$label .= '<br><b>' . $langs -> trans ( 'Ref' ) . ':</b> ' . $this -> ref ;
2017-10-07 13:09:31 +02:00
$url = DOL_URL_ROOT . '/holiday/card.php?id=' . $this -> id ;
//if ($option != 'nolink')
//{
2018-03-21 19:17:42 +01:00
// Add param to save lastsearch_values or not
2019-11-14 12:09:15 +01:00
$add_save_lastsearch_values = ( $save_lastsearch_value == 1 ? 1 : 0 );
2021-02-26 17:59:31 +01:00
if ( $save_lastsearch_value == - 1 && preg_match ( '/list\.php/' , $_SERVER [ " PHP_SELF " ])) {
$add_save_lastsearch_values = 1 ;
}
if ( $add_save_lastsearch_values ) {
$url .= '&save_lastsearch_values=1' ;
}
2017-10-07 13:09:31 +02:00
//}
$linkstart = '<a href="' . $url . '" title="' . dol_escape_htmltag ( $label , 1 ) . '" class="classfortooltip">' ;
2019-11-14 12:09:15 +01:00
$linkend = '</a>' ;
2017-10-07 13:09:31 +02:00
2017-11-02 15:03:09 +01:00
$result .= $linkstart ;
2021-02-26 17:59:31 +01:00
if ( $withpicto ) {
$result .= img_object (( $notooltip ? '' : $label ), $this -> picto , ( $notooltip ? (( $withpicto != 2 ) ? 'class="paddingright"' : '' ) : 'class="' . (( $withpicto != 2 ) ? 'paddingright ' : '' ) . 'classfortooltip"' ), 0 , 0 , $notooltip ? 0 : 1 );
}
if ( $withpicto != 2 ) {
$result .= $this -> ref ;
}
2017-11-02 15:03:09 +01:00
$result .= $linkend ;
2022-02-15 18:14:44 +01:00
global $action ;
$hookmanager -> initHooks ( array ( $this -> element . 'dao' ));
2021-11-22 19:57:26 +01:00
$parameters = array ( 'id' => $this -> id , 'getnomurl' => & $result );
2022-02-15 18:14:44 +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 ;
}
/**
* Returns the label status
*
* @ param int $mode 0 = long label , 1 = short label , 2 = Picto + short label , 3 = Picto , 4 = Picto + long label , 5 = Short label + Picto
* @ return string Label
*/
2019-02-25 20:35:59 +01:00
public function getLibStatut ( $mode = 0 )
2017-10-07 13:09:31 +02:00
{
return $this -> LibStatut ( $this -> statut , $mode , $this -> date_debut );
}
2013-02-01 10:13:42 +01:00
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2012-12-18 13:12:11 +01:00
/**
2019-11-01 12:11:29 +01:00
* Returns the label of a status
2012-12-18 13:12:11 +01:00
*
2019-11-01 12:11:29 +01:00
* @ param int $status Id status
2012-12-18 13:12:11 +01:00
* @ param int $mode 0 = long label , 1 = short label , 2 = Picto + short label , 3 = Picto , 4 = Picto + long label , 5 = Short label + Picto
2019-03-15 18:09:44 +01:00
* @ param integer $startdate Date holiday should start
2012-12-18 13:12:11 +01:00
* @ return string Label
*/
2019-11-01 12:11:29 +01:00
public function LibStatut ( $status , $mode = 0 , $startdate = '' )
2012-12-18 13:12:11 +01:00
{
2019-11-01 12:11:29 +01:00
// phpcs:enable
2021-07-14 14:32:05 +02:00
global $langs ;
2021-02-26 17:59:31 +01:00
if ( empty ( $this -> labelStatus ) || empty ( $this -> labelStatusShort )) {
2019-11-01 12:11:29 +01:00
global $langs ;
//$langs->load("mymodule");
2021-10-16 19:37:57 +02:00
$this -> labelStatus [ self :: STATUS_DRAFT ] = $langs -> transnoentitiesnoconv ( 'DraftCP' );
$this -> labelStatus [ self :: STATUS_VALIDATED ] = $langs -> transnoentitiesnoconv ( 'ToReviewCP' );
$this -> labelStatus [ self :: STATUS_APPROVED ] = $langs -> transnoentitiesnoconv ( 'ApprovedCP' );
$this -> labelStatus [ self :: STATUS_CANCELED ] = $langs -> transnoentitiesnoconv ( 'CancelCP' );
$this -> labelStatus [ self :: STATUS_REFUSED ] = $langs -> transnoentitiesnoconv ( 'RefuseCP' );
$this -> labelStatusShort [ self :: STATUS_DRAFT ] = $langs -> transnoentitiesnoconv ( 'DraftCP' );
$this -> labelStatusShort [ self :: STATUS_VALIDATED ] = $langs -> transnoentitiesnoconv ( 'ToReviewCP' );
$this -> labelStatusShort [ self :: STATUS_APPROVED ] = $langs -> transnoentitiesnoconv ( 'ApprovedCP' );
$this -> labelStatusShort [ self :: STATUS_CANCELED ] = $langs -> transnoentitiesnoconv ( 'CancelCP' );
$this -> labelStatusShort [ self :: STATUS_REFUSED ] = $langs -> transnoentitiesnoconv ( 'RefuseCP' );
2019-11-01 12:11:29 +01:00
}
2021-07-14 14:32:05 +02:00
$params = array ();
2019-11-01 12:11:29 +01:00
$statusType = 'status6' ;
2021-07-14 14:32:05 +02:00
if ( ! empty ( $startdate ) && $startdate >= dol_now ()) { // If not yet passed, we use a green "in live" color
2021-02-26 17:59:31 +01:00
$statusType = 'status4' ;
2021-07-14 14:32:05 +02:00
$params = array ( 'tooltip' => $this -> labelStatus [ $status ] . ' - ' . $langs -> trans ( " Forthcoming " ));
2021-02-26 17:59:31 +01:00
}
if ( $status == self :: STATUS_DRAFT ) {
$statusType = 'status0' ;
}
if ( $status == self :: STATUS_VALIDATED ) {
$statusType = 'status1' ;
}
if ( $status == self :: STATUS_CANCELED ) {
$statusType = 'status5' ;
}
if ( $status == self :: STATUS_REFUSED ) {
$statusType = 'status5' ;
}
2019-11-01 12:11:29 +01:00
2021-07-14 14:32:05 +02:00
return dolGetStatus ( $this -> labelStatus [ $status ], $this -> labelStatusShort [ $status ], '' , $statusType , $mode , '' , $params );
2017-10-07 13:09:31 +02:00
}
/**
* Affiche un select HTML des statuts de congés payés
*
2018-03-19 16:11:42 +01:00
* @ param int $selected Id of preselected status
2018-03-20 10:10:21 +01:00
* @ param string $htmlname Name of HTML select field
2021-07-23 00:02:14 +02:00
* @ param string $morecss More CSS on select component
2018-03-19 16:11:42 +01:00
* @ return string Show select of status
2017-10-07 13:09:31 +02:00
*/
2021-07-23 00:02:14 +02:00
public function selectStatutCP ( $selected = '' , $htmlname = 'select_statut' , $morecss = 'minwidth125' )
2020-10-31 14:32:18 +01:00
{
2017-10-07 13:09:31 +02:00
global $langs ;
// Liste des statuts
2019-11-14 12:09:15 +01:00
$name = array ( 'DraftCP' , 'ToReviewCP' , 'ApprovedCP' , 'CancelCP' , 'RefuseCP' );
$nb = count ( $name ) + 1 ;
2017-10-07 13:09:31 +02:00
// Select HTML
2021-07-23 00:02:14 +02:00
$out = '<select name="' . $htmlname . '" id="' . $htmlname . '" class="flat' . ( $morecss ? ' ' . $morecss : '' ) . '">' . " \n " ;
2020-09-11 13:14:14 +02:00
$out .= '<option value="-1"> </option>' . " \n " ;
2017-10-07 13:09:31 +02:00
// Boucle des statuts
2019-11-14 12:09:15 +01:00
for ( $i = 1 ; $i < $nb ; $i ++ ) {
if ( $i == $selected ) {
2020-09-11 13:14:14 +02:00
$out .= '<option value="' . $i . '" selected>' . $langs -> trans ( $name [ $i - 1 ]) . '</option>' . " \n " ;
2020-05-21 15:05:19 +02:00
} else {
2020-09-11 13:14:14 +02:00
$out .= '<option value="' . $i . '">' . $langs -> trans ( $name [ $i - 1 ]) . '</option>' . " \n " ;
2017-10-07 13:09:31 +02:00
}
}
2020-09-11 13:14:14 +02:00
$out .= '</select>' . " \n " ;
$out .= ajax_combobox ( $htmlname );
print $out ;
2017-10-07 13:09:31 +02:00
}
/**
* Met à jour une option du module Holiday Payés
*
* @ param string $name name du paramètre de configuration
* @ param string $value vrai si mise à jour OK sinon faux
* @ return boolean ok or ko
*/
2020-10-31 14:32:18 +01:00
public function updateConfCP ( $name , $value )
{
2017-10-07 13:09:31 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " holiday_config SET " ;
2020-09-20 02:30:53 +02:00
$sql .= " value = ' " . $this -> db -> escape ( $value ) . " ' " ;
$sql .= " WHERE name = ' " . $this -> db -> escape ( $name ) . " ' " ;
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . '::updateConfCP name=' . $name . '' , LOG_DEBUG );
$result = $this -> db -> query ( $sql );
2019-11-14 12:09:15 +01:00
if ( $result ) {
2017-10-07 13:09:31 +02:00
return true ;
}
return false ;
}
/**
2022-07-26 10:12:40 +02:00
* Return value of a conf parameter for leave module
2017-10-07 13:09:31 +02:00
* TODO Move this into llx_const table
*
* @ param string $name Name of parameter
* @ param string $createifnotfound 'stringvalue' = Create entry with string value if not found . For example 'YYYYMMDDHHMMSS' .
* @ return string Value of parameter . Example : 'YYYYMMDDHHMMSS' or < 0 if error
*/
2019-02-25 20:35:59 +01:00
public function getConfCP ( $name , $createifnotfound = '' )
2017-10-07 13:09:31 +02:00
{
$sql = " SELECT value " ;
2019-11-14 12:09:15 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " holiday_config " ;
$sql .= " WHERE name = ' " . $this -> db -> escape ( $name ) . " ' " ;
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . '::getConfCP name=' . $name . ' createifnotfound=' . $createifnotfound , LOG_DEBUG );
$result = $this -> db -> query ( $sql );
2019-11-14 12:09:15 +01:00
if ( $result ) {
2017-10-07 13:09:31 +02:00
$obj = $this -> db -> fetch_object ( $result );
// Return value
2021-02-26 17:59:31 +01:00
if ( empty ( $obj )) {
if ( $createifnotfound ) {
2017-10-07 13:09:31 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " holiday_config(name, value) " ;
2019-11-14 12:09:15 +01:00
$sql .= " VALUES(' " . $this -> db -> escape ( $name ) . " ', ' " . $this -> db -> escape ( $createifnotfound ) . " ') " ;
2017-10-07 13:09:31 +02:00
$result = $this -> db -> query ( $sql );
2021-02-26 17:59:31 +01:00
if ( $result ) {
2017-10-07 13:09:31 +02:00
return $createifnotfound ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-14 12:09:15 +01:00
$this -> error = $this -> db -> lasterror ();
2017-10-07 13:09:31 +02:00
return - 2 ;
}
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
return '' ;
}
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
return $obj -> value ;
}
} else {
// Erreur SQL
2019-11-14 12:09:15 +01:00
$this -> error = $this -> db -> lasterror ();
2017-10-07 13:09:31 +02:00
return - 1 ;
}
}
/**
* Met à jour le timestamp de la dernière mise à jour du solde des CP
*
* @ param int $userID Id of user
2021-06-15 16:29:42 +02:00
* @ param float $nbHoliday Nb of days
2017-10-07 13:09:31 +02:00
* @ param int $fk_type Type of vacation
* @ return int 0 = Nothing done , 1 = OK , - 1 = KO
*/
2019-02-25 20:35:59 +01:00
public function updateSoldeCP ( $userID = '' , $nbHoliday = '' , $fk_type = '' )
2017-10-07 13:09:31 +02:00
{
global $user , $langs ;
$error = 0 ;
2021-02-26 17:59:31 +01:00
if ( empty ( $userID ) && empty ( $nbHoliday ) && empty ( $fk_type )) {
2017-10-07 13:09:31 +02:00
$langs -> load ( " holiday " );
// Si mise à jour pour tout le monde en début de mois
2019-11-14 12:09:15 +01:00
$now = dol_now ();
2013-02-01 10:13:42 +01:00
2019-01-27 11:55:16 +01:00
$month = date ( 'm' , $now );
2017-10-07 13:09:31 +02:00
$newdateforlastupdate = dol_print_date ( $now , '%Y%m%d%H%M%S' );
2017-06-10 22:57:25 +02:00
2017-10-07 13:09:31 +02:00
// Get month of last update
$lastUpdate = $this -> getConfCP ( 'lastUpdate' , $newdateforlastupdate );
$monthLastUpdate = $lastUpdate [ 4 ] . $lastUpdate [ 5 ];
2016-12-12 12:33:43 +01:00
//print 'month: '.$month.' lastUpdate:'.$lastUpdate.' monthLastUpdate:'.$monthLastUpdate;exit;
2012-09-05 03:26:57 +02:00
2021-12-03 01:50:17 +01:00
// If month date is not same than the one of last update (the one we saved in database), then we update the timestamp and balance of each open user.
2021-02-26 17:59:31 +01:00
if ( $month != $monthLastUpdate ) {
2017-10-07 13:09:31 +02:00
$this -> db -> begin ();
2015-06-21 03:19:15 +02:00
2021-12-03 01:50:17 +01:00
$users = $this -> fetchUsers ( false , false , ' AND u.statut > 0' );
2017-10-07 13:09:31 +02:00
$nbUser = count ( $users );
2015-06-27 13:01:44 +02:00
2017-10-07 13:09:31 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " holiday_config SET " ;
2019-11-14 12:09:15 +01:00
$sql .= " value = ' " . $this -> db -> escape ( $newdateforlastupdate ) . " ' " ;
$sql .= " WHERE name = 'lastUpdate' " ;
2017-10-07 13:09:31 +02:00
$result = $this -> db -> query ( $sql );
2012-09-05 03:26:57 +02:00
2019-11-14 12:09:15 +01:00
$typeleaves = $this -> getTypes ( 1 , 1 );
2015-06-21 03:19:15 +02:00
2019-07-27 19:19:13 +02:00
// Update each user counter
foreach ( $users as $userCounter ) {
2021-10-05 16:18:56 +02:00
$nbDaysToAdd = ( isset ( $typeleaves [ $userCounter [ 'type' ]][ 'newbymonth' ]) ? $typeleaves [ $userCounter [ 'type' ]][ 'newbymonth' ] : 0 );
2021-02-26 17:59:31 +01:00
if ( empty ( $nbDaysToAdd )) {
continue ;
}
2015-06-21 03:19:15 +02:00
2019-07-27 19:19:13 +02:00
dol_syslog ( " We update leave type id " . $userCounter [ 'type' ] . " for user id " . $userCounter [ 'rowid' ], LOG_DEBUG );
2015-06-21 03:19:15 +02:00
2019-07-27 19:19:13 +02:00
$nowHoliday = $userCounter [ 'nb_holiday' ];
$newSolde = $nowHoliday + $nbDaysToAdd ;
2015-06-21 03:19:15 +02:00
2019-07-27 19:19:13 +02:00
// We add a log for each user
$this -> addLogCP ( $user -> id , $userCounter [ 'rowid' ], $langs -> trans ( 'HolidaysMonthlyUpdate' ), $newSolde , $userCounter [ 'type' ]);
2015-06-21 03:19:15 +02:00
2019-07-27 19:19:13 +02:00
$result = $this -> updateSoldeCP ( $userCounter [ 'rowid' ], $newSolde , $userCounter [ 'type' ], $langs -> trans ( 'HolidaysMonthlyUpdate' ));
2015-06-21 03:19:15 +02:00
2021-02-26 17:59:31 +01:00
if ( $result < 0 ) {
2019-07-27 19:19:13 +02:00
$error ++ ;
break ;
2015-06-27 13:01:44 +02:00
}
2017-10-07 13:09:31 +02:00
}
2015-06-21 03:19:15 +02:00
2021-02-26 17:59:31 +01:00
if ( ! $error ) {
2017-10-07 13:09:31 +02:00
$this -> db -> commit ();
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2018-03-21 19:17:42 +01:00
$this -> db -> rollback ();
2017-10-07 13:09:31 +02:00
return - 1 ;
2018-03-21 19:17:42 +01:00
}
2017-10-07 13:09:31 +02:00
}
return 0 ;
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
// Mise à jour pour un utilisateur
2019-01-27 11:55:16 +01:00
$nbHoliday = price2num ( $nbHoliday , 5 );
2017-10-07 13:09:31 +02:00
$sql = " SELECT nb_holiday FROM " . MAIN_DB_PREFIX . " holiday_users " ;
2019-11-14 12:09:15 +01:00
$sql .= " WHERE fk_user = " . ( int ) $userID . " AND fk_type = " . ( int ) $fk_type ;
2017-10-07 13:09:31 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-26 17:59:31 +01:00
if ( $resql ) {
2017-10-07 13:09:31 +02:00
$num = $this -> db -> num_rows ( $resql );
2021-02-26 17:59:31 +01:00
if ( $num > 0 ) {
2017-10-07 13:09:31 +02:00
// Update for user
$sql = " UPDATE " . MAIN_DB_PREFIX . " holiday_users SET " ;
2021-06-15 16:29:42 +02:00
$sql .= " nb_holiday = " . (( float ) $nbHoliday );
2019-11-14 12:09:15 +01:00
$sql .= " WHERE fk_user = " . ( int ) $userID . " AND fk_type = " . ( int ) $fk_type ;
2017-10-07 13:09:31 +02:00
$result = $this -> db -> query ( $sql );
2021-02-26 17:59:31 +01:00
if ( ! $result ) {
2017-10-07 13:09:31 +02:00
$error ++ ;
2019-11-14 12:09:15 +01:00
$this -> errors [] = $this -> db -> lasterror ();
2017-10-07 13:09:31 +02:00
}
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
// Insert for user
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " holiday_users(nb_holiday, fk_user, fk_type) VALUES ( " ;
2021-06-15 16:29:42 +02:00
$sql .= (( float ) $nbHoliday );
2019-11-14 12:09:15 +01:00
$sql .= " , " . ( int ) $userID . " , " . ( int ) $fk_type . " ) " ;
2017-10-07 13:09:31 +02:00
$result = $this -> db -> query ( $sql );
2021-02-26 17:59:31 +01:00
if ( ! $result ) {
2017-10-07 13:09:31 +02:00
$error ++ ;
2019-11-14 12:09:15 +01:00
$this -> errors [] = $this -> db -> lasterror ();
2017-10-07 13:09:31 +02:00
}
}
2020-05-21 15:05:19 +02:00
} else {
2019-11-14 12:09:15 +01:00
$this -> errors [] = $this -> db -> lasterror ();
2017-10-07 13:09:31 +02:00
$error ++ ;
}
2021-02-26 17:59:31 +01:00
if ( ! $error ) {
2017-10-07 13:09:31 +02:00
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
return - 1 ;
}
}
}
/**
* Retourne un checked si vrai
*
* @ param string $name name du paramètre de configuration
* @ return string retourne checked si > 0
*/
2020-10-31 14:32:18 +01:00
public function getCheckOption ( $name )
{
2017-10-07 13:09:31 +02:00
$sql = " SELECT value " ;
2019-11-14 12:09:15 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " holiday_config " ;
2020-09-20 02:30:53 +02:00
$sql .= " WHERE name = ' " . $this -> db -> escape ( $name ) . " ' " ;
2017-10-07 13:09:31 +02:00
$result = $this -> db -> query ( $sql );
2019-11-14 12:09:15 +01:00
if ( $result ) {
2017-10-07 13:09:31 +02:00
$obj = $this -> db -> fetch_object ( $result );
// Si la valeur est 1 on retourne checked
2019-11-14 12:09:15 +01:00
if ( $obj -> value ) {
2017-10-07 13:09:31 +02:00
return 'checked' ;
2015-06-21 03:19:15 +02:00
}
2017-10-07 13:09:31 +02:00
}
}
2014-02-12 18:47:16 +01:00
2017-10-07 13:09:31 +02:00
/**
* Créer les entrées pour chaque utilisateur au moment de la configuration
*
* @ param boolean $single Single
* @ param int $userid Id user
* @ return void
*/
2019-02-25 20:35:59 +01:00
public function createCPusers ( $single = false , $userid = '' )
2017-10-07 13:09:31 +02:00
{
2020-09-20 02:30:53 +02:00
// do we have to add balance for all users ?
2021-02-26 17:59:31 +01:00
if ( ! $single ) {
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . '::createCPusers' );
2019-01-27 11:55:16 +01:00
$arrayofusers = $this -> fetchUsers ( false , true );
2017-10-07 13:09:31 +02:00
2021-02-26 17:59:31 +01:00
foreach ( $arrayofusers as $users ) {
2017-10-07 13:09:31 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " holiday_users " ;
2019-11-14 12:09:15 +01:00
$sql .= " (fk_user, nb_holiday) " ;
2020-09-20 02:30:53 +02:00
$sql .= " VALUES ( " . (( int ) $users [ 'rowid' ]) . " ', '0') " ;
2017-10-07 13:09:31 +02:00
2019-11-14 12:09:15 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-26 17:59:31 +01:00
if ( ! $resql ) {
dol_print_error ( $this -> db );
}
2017-10-07 13:09:31 +02:00
}
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " holiday_users " ;
2019-11-14 12:09:15 +01:00
$sql .= " (fk_user, nb_holiday) " ;
2020-09-20 02:30:53 +02:00
$sql .= " VALUES ( " . (( int ) $userid ) . " ', '0') " ;
2017-10-07 13:09:31 +02:00
2019-11-14 12:09:15 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-26 17:59:31 +01:00
if ( ! $resql ) {
dol_print_error ( $this -> db );
}
2017-10-07 13:09:31 +02:00
}
}
/**
* Supprime un utilisateur du module Congés Payés
*
* @ param int $user_id ID de l ' utilisateur à supprimer
* @ return boolean Vrai si pas d ' erreur , faut si Erreur
*/
2020-10-31 14:32:18 +01:00
public function deleteCPuser ( $user_id )
{
2017-10-07 13:09:31 +02:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " holiday_users " ;
2020-09-20 02:30:53 +02:00
$sql .= " WHERE fk_user = " . (( int ) $user_id );
2017-10-07 13:09:31 +02:00
$this -> db -> query ( $sql );
}
/**
2021-09-09 03:21:41 +02:00
* Return balance of holiday for one user
2017-10-07 13:09:31 +02:00
*
* @ param int $user_id ID de l ' utilisateur
* @ param int $fk_type Filter on type
* @ return float Retourne le solde de congés payés de l ' utilisateur
*/
2019-02-25 20:35:59 +01:00
public function getCPforUser ( $user_id , $fk_type = 0 )
2017-10-07 13:09:31 +02:00
{
$sql = " SELECT nb_holiday " ;
2019-11-14 12:09:15 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " holiday_users " ;
$sql .= " WHERE fk_user = " . ( int ) $user_id ;
2021-02-26 17:59:31 +01:00
if ( $fk_type > 0 ) {
$sql .= " AND fk_type = " . ( int ) $fk_type ;
}
2017-10-07 13:09:31 +02:00
2019-07-30 20:37:07 +02:00
dol_syslog ( get_class ( $this ) . '::getCPforUser user_id=' . $user_id . ' type_id=' . $fk_type , LOG_DEBUG );
2017-10-07 13:09:31 +02:00
$result = $this -> db -> query ( $sql );
2021-02-26 17:59:31 +01:00
if ( $result ) {
2017-10-07 13:09:31 +02:00
$obj = $this -> db -> fetch_object ( $result );
//return number_format($obj->nb_holiday,2);
2021-02-26 17:59:31 +01:00
if ( $obj ) {
return $obj -> nb_holiday ;
} else {
return null ;
}
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
return null ;
}
}
/**
* Get list of Users or list of vacation balance .
*
* @ param boolean $stringlist If true return a string list of id . If false , return an array with detail .
* @ param boolean $type If true , read Dolibarr user list , if false , return vacation balance list .
2021-12-03 01:50:17 +01:00
* @ param string $filters Filters . Warning : This must not contains data from user input .
2017-10-07 13:09:31 +02:00
* @ return array | string | int Return an array
*/
2019-02-25 20:35:59 +01:00
public function fetchUsers ( $stringlist = true , $type = true , $filters = '' )
2017-10-07 13:09:31 +02:00
{
global $conf ;
2020-11-02 11:46:28 +01:00
dol_syslog ( get_class ( $this ) . " ::fetchUsers " , LOG_DEBUG );
2017-10-07 13:09:31 +02:00
2021-02-26 17:59:31 +01:00
if ( $stringlist ) {
if ( $type ) {
2020-11-02 11:46:28 +01:00
// If user of Dolibarr
$sql = " SELECT " ;
if ( ! empty ( $conf -> multicompany -> enabled ) && ! empty ( $conf -> global -> MULTICOMPANY_TRANSVERSE_MODE )) {
$sql .= " DISTINCT " ;
}
$sql .= " u.rowid " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " user as u " ;
2017-10-07 13:09:31 +02:00
2021-02-26 17:59:31 +01:00
if ( ! empty ( $conf -> multicompany -> enabled ) && ! empty ( $conf -> global -> MULTICOMPANY_TRANSVERSE_MODE )) {
2020-11-02 11:46:28 +01:00
$sql .= " , " . MAIN_DB_PREFIX . " usergroup_user as ug " ;
$sql .= " WHERE ((ug.fk_user = u.rowid " ;
$sql .= " AND ug.entity IN ( " . getEntity ( 'usergroup' ) . " )) " ;
$sql .= " OR u.entity = 0) " ; // Show always superadmin
} else {
$sql .= " WHERE u.entity IN ( " . getEntity ( 'user' ) . " ) " ;
2017-10-07 13:09:31 +02:00
}
2020-11-02 11:46:28 +01:00
$sql .= " AND u.statut > 0 " ;
2021-08-04 14:48:10 +02:00
$sql .= " AND u.employee = 1 " ; // We only want employee users for holidays
2021-02-26 17:59:31 +01:00
if ( $filters ) {
$sql .= $filters ;
}
2017-10-07 13:09:31 +02:00
2020-11-02 11:46:28 +01:00
$resql = $this -> db -> query ( $sql );
2017-10-07 13:09:31 +02:00
2020-11-02 11:46:28 +01:00
// Si pas d'erreur SQL
if ( $resql ) {
$i = 0 ;
$num = $this -> db -> num_rows ( $resql );
$stringlist = '' ;
2017-10-07 13:09:31 +02:00
2020-11-02 11:46:28 +01:00
// Boucles du listage des utilisateurs
2021-02-26 17:59:31 +01:00
while ( $i < $num ) {
2020-11-02 11:46:28 +01:00
$obj = $this -> db -> fetch_object ( $resql );
2017-10-07 13:09:31 +02:00
2020-11-02 11:46:28 +01:00
if ( $i == 0 ) {
$stringlist .= $obj -> rowid ;
} else {
$stringlist .= ', ' . $obj -> rowid ;
}
2020-11-01 21:28:06 +01:00
2020-11-02 11:46:28 +01:00
$i ++ ;
}
// Retoune le tableau des utilisateurs
return $stringlist ;
} else {
// Erreur SQL
$this -> error = " Error " . $this -> db -> lasterror ();
return - 1 ;
2017-10-07 13:09:31 +02:00
}
2020-11-01 21:28:06 +01:00
} else {
2020-11-02 11:46:28 +01:00
// We want only list of vacation balance for user ids
$sql = " SELECT DISTINCT cpu.fk_user " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " holiday_users as cpu, " . MAIN_DB_PREFIX . " user as u " ;
$sql .= " WHERE cpu.fk_user = u.rowid " ;
2021-02-26 17:59:31 +01:00
if ( $filters ) {
$sql .= $filters ;
}
2020-11-01 21:28:06 +01:00
2020-11-02 11:46:28 +01:00
$resql = $this -> db -> query ( $sql );
2017-10-07 13:09:31 +02:00
2020-11-02 11:46:28 +01:00
// Si pas d'erreur SQL
if ( $resql ) {
$i = 0 ;
$num = $this -> db -> num_rows ( $resql );
$stringlist = '' ;
2017-10-07 13:09:31 +02:00
2020-11-02 11:46:28 +01:00
// Boucles du listage des utilisateurs
2021-02-26 17:59:31 +01:00
while ( $i < $num ) {
2020-11-02 11:46:28 +01:00
$obj = $this -> db -> fetch_object ( $resql );
2020-11-01 21:28:06 +01:00
2020-11-02 11:46:28 +01:00
if ( $i == 0 ) {
$stringlist .= $obj -> fk_user ;
} else {
$stringlist .= ', ' . $obj -> fk_user ;
}
2020-11-01 21:28:06 +01:00
2020-11-02 11:46:28 +01:00
$i ++ ;
}
// Retoune le tableau des utilisateurs
return $stringlist ;
} else {
// Erreur SQL
$this -> error = " Error " . $this -> db -> lasterror ();
return - 1 ;
2019-10-27 09:51:15 +01:00
}
2020-11-01 21:28:06 +01:00
}
} else {
2020-11-02 11:46:28 +01:00
// Si faux donc return array
// List for Dolibarr users
2021-02-26 17:59:31 +01:00
if ( $type ) {
2021-12-03 01:50:17 +01:00
// If we need users of Dolibarr
2020-11-02 11:46:28 +01:00
$sql = " SELECT " ;
if ( ! empty ( $conf -> multicompany -> enabled ) && ! empty ( $conf -> global -> MULTICOMPANY_TRANSVERSE_MODE )) {
$sql .= " DISTINCT " ;
}
$sql .= " u.rowid, u.lastname, u.firstname, u.gender, u.photo, u.employee, u.statut, u.fk_user " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " user as u " ;
2017-10-07 13:09:31 +02:00
2021-02-26 17:59:31 +01:00
if ( ! empty ( $conf -> multicompany -> enabled ) && ! empty ( $conf -> global -> MULTICOMPANY_TRANSVERSE_MODE )) {
2020-11-02 11:46:28 +01:00
$sql .= " , " . MAIN_DB_PREFIX . " usergroup_user as ug " ;
$sql .= " WHERE ((ug.fk_user = u.rowid " ;
$sql .= " AND ug.entity IN ( " . getEntity ( 'usergroup' ) . " )) " ;
$sql .= " OR u.entity = 0) " ; // Show always superadmin
} else {
$sql .= " WHERE u.entity IN ( " . getEntity ( 'user' ) . " ) " ;
}
2017-10-07 13:09:31 +02:00
2020-11-02 11:46:28 +01:00
$sql .= " AND u.statut > 0 " ;
2021-08-04 14:48:10 +02:00
$sql .= " AND u.employee = 1 " ; // We only want employee users for holidays
2021-02-26 17:59:31 +01:00
if ( $filters ) {
$sql .= $filters ;
}
2020-11-01 21:28:06 +01:00
2020-11-02 11:46:28 +01:00
$resql = $this -> db -> query ( $sql );
2020-11-01 21:28:06 +01:00
2020-11-02 11:46:28 +01:00
// Si pas d'erreur SQL
2021-02-26 17:59:31 +01:00
if ( $resql ) {
2020-11-02 11:46:28 +01:00
$i = 0 ;
$tab_result = $this -> holiday ;
$num = $this -> db -> num_rows ( $resql );
// Boucles du listage des utilisateurs
while ( $i < $num ) {
$obj = $this -> db -> fetch_object ( $resql );
$tab_result [ $i ][ 'rowid' ] = $obj -> rowid ; // rowid of user
$tab_result [ $i ][ 'name' ] = $obj -> lastname ; // deprecated
$tab_result [ $i ][ 'lastname' ] = $obj -> lastname ;
$tab_result [ $i ][ 'firstname' ] = $obj -> firstname ;
$tab_result [ $i ][ 'gender' ] = $obj -> gender ;
$tab_result [ $i ][ 'status' ] = $obj -> statut ;
$tab_result [ $i ][ 'employee' ] = $obj -> employee ;
$tab_result [ $i ][ 'photo' ] = $obj -> photo ;
$tab_result [ $i ][ 'fk_user' ] = $obj -> fk_user ; // rowid of manager
//$tab_result[$i]['type'] = $obj->type;
//$tab_result[$i]['nb_holiday'] = $obj->nb_holiday;
$i ++ ;
}
// Retoune le tableau des utilisateurs
return $tab_result ;
} else {
// Erreur SQL
$this -> errors [] = " Error " . $this -> db -> lasterror ();
return - 1 ;
2017-10-07 13:09:31 +02:00
}
2020-11-01 21:28:06 +01:00
} else {
2020-11-02 11:46:28 +01:00
// List of vacation balance users
$sql = " SELECT cpu.fk_type, cpu.nb_holiday, u.rowid, u.lastname, u.firstname, u.gender, u.photo, u.employee, u.statut, u.fk_user " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " holiday_users as cpu, " . MAIN_DB_PREFIX . " user as u " ;
$sql .= " WHERE cpu.fk_user = u.rowid " ;
2021-02-26 17:59:31 +01:00
if ( $filters ) {
$sql .= $filters ;
}
2020-11-02 11:46:28 +01:00
$resql = $this -> db -> query ( $sql );
// Si pas d'erreur SQL
2021-02-26 17:59:31 +01:00
if ( $resql ) {
2020-11-02 11:46:28 +01:00
$i = 0 ;
$tab_result = $this -> holiday ;
$num = $this -> db -> num_rows ( $resql );
// Boucles du listage des utilisateurs
2021-02-26 17:59:31 +01:00
while ( $i < $num ) {
2020-11-02 11:46:28 +01:00
$obj = $this -> db -> fetch_object ( $resql );
$tab_result [ $i ][ 'rowid' ] = $obj -> rowid ; // rowid of user
$tab_result [ $i ][ 'name' ] = $obj -> lastname ; // deprecated
$tab_result [ $i ][ 'lastname' ] = $obj -> lastname ;
$tab_result [ $i ][ 'firstname' ] = $obj -> firstname ;
$tab_result [ $i ][ 'gender' ] = $obj -> gender ;
$tab_result [ $i ][ 'status' ] = $obj -> statut ;
$tab_result [ $i ][ 'employee' ] = $obj -> employee ;
$tab_result [ $i ][ 'photo' ] = $obj -> photo ;
$tab_result [ $i ][ 'fk_user' ] = $obj -> fk_user ; // rowid of manager
$tab_result [ $i ][ 'type' ] = $obj -> fk_type ;
$tab_result [ $i ][ 'nb_holiday' ] = $obj -> nb_holiday ;
$i ++ ;
}
// Retoune le tableau des utilisateurs
return $tab_result ;
} else {
// Erreur SQL
$this -> error = " Error " . $this -> db -> lasterror ();
return - 1 ;
}
2017-10-07 13:09:31 +02:00
}
}
}
2018-05-10 18:56:21 +02:00
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2018-05-10 18:56:21 +02:00
/**
* Return list of people with permission to validate leave requests .
* Search for permission " approve leave requests "
*
2020-11-02 11:46:28 +01:00
* @ return array Array of user ids
2018-05-10 18:56:21 +02:00
*/
2019-02-25 20:35:59 +01:00
public function fetch_users_approver_holiday ()
2018-05-10 18:56:21 +02:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2019-11-14 12:09:15 +01:00
$users_validator = array ();
2018-05-10 18:56:21 +02:00
$sql = " SELECT DISTINCT ur.fk_user " ;
2019-11-14 12:09:15 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " user_rights as ur, " . MAIN_DB_PREFIX . " rights_def as rd " ;
$sql .= " WHERE ur.fk_id = rd.id and rd.module = 'holiday' AND rd.perms = 'approve' " ; // Permission 'Approve';
$sql .= " UNION " ;
$sql .= " SELECT DISTINCT ugu.fk_user " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " usergroup_user as ugu, " . MAIN_DB_PREFIX . " usergroup_rights as ur, " . MAIN_DB_PREFIX . " rights_def as rd " ;
$sql .= " WHERE ugu.fk_usergroup = ur.fk_usergroup AND ur.fk_id = rd.id and rd.module = 'holiday' AND rd.perms = 'approve' " ; // Permission 'Approve';
2018-05-10 18:56:21 +02:00
//print $sql;
dol_syslog ( get_class ( $this ) . " ::fetch_users_approver_holiday sql= " . $sql );
$result = $this -> db -> query ( $sql );
2021-02-26 17:59:31 +01:00
if ( $result ) {
2019-06-29 16:29:32 +02:00
$num_rows = $this -> db -> num_rows ( $result ); $i = 0 ;
2021-02-26 17:59:31 +01:00
while ( $i < $num_rows ) {
2018-05-10 18:56:21 +02:00
$objp = $this -> db -> fetch_object ( $result );
2019-01-27 11:55:16 +01:00
array_push ( $users_validator , $objp -> fk_user );
2018-05-10 18:56:21 +02:00
$i ++ ;
}
return $users_validator ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-14 12:09:15 +01:00
$this -> error = $this -> db -> lasterror ();
2018-05-10 18:56:21 +02:00
dol_syslog ( get_class ( $this ) . " ::fetch_users_approver_holiday Error " . $this -> error , LOG_ERR );
return - 1 ;
}
}
2017-10-07 13:09:31 +02:00
/**
* Compte le nombre d ' utilisateur actifs dans Dolibarr
*
* @ return int retourne le nombre d ' utilisateur
*/
2019-02-25 20:35:59 +01:00
public function countActiveUsers ()
2017-10-07 13:09:31 +02:00
{
$sql = " SELECT count(u.rowid) as compteur " ;
2019-11-14 12:09:15 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " user as u " ;
$sql .= " WHERE u.statut > 0 " ;
2014-02-10 09:39:26 +01:00
2017-10-07 13:09:31 +02:00
$result = $this -> db -> query ( $sql );
$objet = $this -> db -> fetch_object ( $result );
2014-02-10 09:39:26 +01:00
2017-10-07 13:09:31 +02:00
return $objet -> compteur ;
}
/**
* Compte le nombre d ' utilisateur actifs dans Dolibarr sans CP
*
* @ return int retourne le nombre d ' utilisateur
*/
2020-10-31 14:32:18 +01:00
public function countActiveUsersWithoutCP ()
{
2014-02-10 09:39:26 +01:00
2017-10-07 13:09:31 +02:00
$sql = " SELECT count(u.rowid) as compteur " ;
2019-11-14 12:09:15 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " user as u LEFT OUTER JOIN " . MAIN_DB_PREFIX . " holiday_users hu ON (hu.fk_user=u.rowid) " ;
$sql .= " WHERE u.statut > 0 AND hu.fk_user IS NULL " ;
2012-09-05 03:26:57 +02:00
2017-10-07 13:09:31 +02:00
$result = $this -> db -> query ( $sql );
$objet = $this -> db -> fetch_object ( $result );
return $objet -> compteur ;
}
/**
* Compare le nombre d ' utilisateur actif de Dolibarr à celui des utilisateurs des congés payés
*
* @ param int $userDolibarrWithoutCP Number of active users in Dolibarr without holidays
* @ param int $userCP Number of active users into table of holidays
* @ return int < 0 if KO , > 0 if OK
*/
2019-02-25 20:35:59 +01:00
public function verifNbUsers ( $userDolibarrWithoutCP , $userCP )
2017-10-07 13:09:31 +02:00
{
2021-02-26 17:59:31 +01:00
if ( empty ( $userCP )) {
$userCP = 0 ;
}
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . '::verifNbUsers userDolibarr=' . $userDolibarrWithoutCP . ' userCP=' . $userCP );
return 1 ;
}
/**
* addLogCP
*
* @ param int $fk_user_action Id user creation
* @ param int $fk_user_update Id user update
2021-09-09 19:47:15 +02:00
* @ param string $label Label ( Example : 'Leave' , 'Manual update' , 'Leave request cancelation' ... )
2017-10-07 13:09:31 +02:00
* @ param int $new_solde New value
* @ param int $fk_type Type of vacation
* @ return int Id of record added , 0 if nothing done , < 0 if KO
*/
2019-02-25 20:35:59 +01:00
public function addLogCP ( $fk_user_action , $fk_user_update , $label , $new_solde , $fk_type )
2017-10-07 13:09:31 +02:00
{
global $conf , $langs ;
2019-11-14 12:09:15 +01:00
$error = 0 ;
2017-10-07 13:09:31 +02:00
$prev_solde = price2num ( $this -> getCPforUser ( $fk_user_update , $fk_type ), 5 );
$new_solde = price2num ( $new_solde , 5 );
2015-06-21 03:19:15 +02:00
//print "$prev_solde == $new_solde";
2012-09-05 03:26:57 +02:00
2021-02-26 17:59:31 +01:00
if ( $prev_solde == $new_solde ) {
return 0 ;
}
2015-06-21 03:19:15 +02:00
$this -> db -> begin ();
2012-09-05 03:26:57 +02:00
2015-06-21 03:19:15 +02:00
// Insert request
2017-10-07 13:09:31 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " holiday_logs ( " ;
2019-11-14 12:09:15 +01:00
$sql .= " date_action, " ;
$sql .= " fk_user_action, " ;
$sql .= " fk_user_update, " ;
$sql .= " type_action, " ;
$sql .= " prev_solde, " ;
$sql .= " new_solde, " ;
$sql .= " fk_type " ;
$sql .= " ) VALUES ( " ;
$sql .= " ' " . $this -> db -> idate ( dol_now ()) . " ', " ;
2021-06-14 13:51:09 +02:00
$sql .= " " . (( int ) $fk_user_action ) . " , " ;
$sql .= " " . (( int ) $fk_user_update ) . " , " ;
2019-11-14 12:09:15 +01:00
$sql .= " ' " . $this -> db -> escape ( $label ) . " ', " ;
2021-06-15 16:29:42 +02:00
$sql .= " " . (( float ) $prev_solde ) . " , " ;
$sql .= " " . (( float ) $new_solde ) . " , " ;
2021-06-14 13:51:09 +02:00
$sql .= " " . (( int ) $fk_type );
2019-11-14 12:09:15 +01:00
$sql .= " ) " ;
$resql = $this -> db -> query ( $sql );
2021-02-26 17:59:31 +01:00
if ( ! $resql ) {
2020-11-02 11:46:28 +01:00
$error ++ ; $this -> errors [] = " Error " . $this -> db -> lasterror ();
2018-03-21 19:17:42 +01:00
}
2021-02-26 17:59:31 +01:00
if ( ! $error ) {
2018-03-21 19:17:42 +01:00
$this -> optRowid = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . " holiday_logs " );
}
// Commit or rollback
2021-02-26 17:59:31 +01:00
if ( $error ) {
foreach ( $this -> errors as $errmsg ) {
2018-03-21 19:17:42 +01:00
dol_syslog ( get_class ( $this ) . " ::addLogCP " . $errmsg , LOG_ERR );
2019-11-14 12:09:15 +01:00
$this -> error .= ( $this -> error ? ', ' . $errmsg : $errmsg );
2018-03-21 19:17:42 +01:00
}
$this -> db -> rollback ();
2019-11-14 12:09:15 +01:00
return - 1 * $error ;
2020-05-21 15:05:19 +02:00
} else {
2018-03-21 19:17:42 +01:00
$this -> db -> commit ();
2017-10-07 13:09:31 +02:00
return $this -> optRowid ;
2018-03-21 19:17:42 +01:00
}
2017-10-07 13:09:31 +02:00
}
/**
* Liste le log des congés payés
*
* @ param string $order Filtrage par ordre
* @ param string $filter Filtre de séléction
* @ return int - 1 si erreur , 1 si OK et 2 si pas de résultat
*/
2019-02-25 20:35:59 +01:00
public function fetchLog ( $order , $filter )
2017-10-07 13:09:31 +02:00
{
$sql = " SELECT " ;
2019-11-14 12:09:15 +01:00
$sql .= " cpl.rowid, " ;
$sql .= " cpl.date_action, " ;
$sql .= " cpl.fk_user_action, " ;
$sql .= " cpl.fk_user_update, " ;
$sql .= " cpl.type_action, " ;
$sql .= " cpl.prev_solde, " ;
$sql .= " cpl.new_solde, " ;
$sql .= " cpl.fk_type " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " holiday_logs as cpl " ;
$sql .= " WHERE cpl.rowid > 0 " ; // To avoid error with other search and criteria
2012-09-05 03:26:57 +02:00
2017-10-07 13:09:31 +02:00
// Filtrage de séléction
2019-11-14 12:09:15 +01:00
if ( ! empty ( $filter )) {
$sql .= " " . $filter ;
2017-10-07 13:09:31 +02:00
}
2012-09-05 03:26:57 +02:00
2017-10-07 13:09:31 +02:00
// Ordre d'affichage
2019-11-14 12:09:15 +01:00
if ( ! empty ( $order )) {
$sql .= " " . $order ;
2017-10-07 13:09:31 +02:00
}
2012-09-05 03:26:57 +02:00
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::fetchLog " , LOG_DEBUG );
2019-11-14 12:09:15 +01:00
$resql = $this -> db -> query ( $sql );
2012-09-05 03:26:57 +02:00
2017-10-07 13:09:31 +02:00
// Si pas d'erreur SQL
2018-03-21 19:17:42 +01:00
if ( $resql ) {
$i = 0 ;
$tab_result = $this -> logs ;
$num = $this -> db -> num_rows ( $resql );
2017-10-07 13:09:31 +02:00
2018-03-21 19:17:42 +01:00
// Si pas d'enregistrement
2019-11-14 12:09:15 +01:00
if ( ! $num ) {
2017-10-07 13:09:31 +02:00
return 2 ;
2018-03-21 19:17:42 +01:00
}
2020-11-02 11:46:28 +01:00
// On liste les résultats et on les ajoutent dans le tableau
2019-11-14 12:09:15 +01:00
while ( $i < $num ) {
2018-03-21 19:17:42 +01:00
$obj = $this -> db -> fetch_object ( $resql );
$tab_result [ $i ][ 'rowid' ] = $obj -> rowid ;
$tab_result [ $i ][ 'date_action' ] = $obj -> date_action ;
$tab_result [ $i ][ 'fk_user_action' ] = $obj -> fk_user_action ;
$tab_result [ $i ][ 'fk_user_update' ] = $obj -> fk_user_update ;
$tab_result [ $i ][ 'type_action' ] = $obj -> type_action ;
$tab_result [ $i ][ 'prev_solde' ] = $obj -> prev_solde ;
$tab_result [ $i ][ 'new_solde' ] = $obj -> new_solde ;
$tab_result [ $i ][ 'fk_type' ] = $obj -> fk_type ;
$i ++ ;
}
// Retourne 1 et ajoute le tableau à la variable
$this -> logs = $tab_result ;
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2018-03-21 19:17:42 +01:00
// Erreur SQL
2019-11-14 12:09:15 +01:00
$this -> error = " Error " . $this -> db -> lasterror ();
2018-03-21 19:17:42 +01:00
return - 1 ;
}
2017-10-07 13:09:31 +02:00
}
/**
* Return array with list of types
*
* @ param int $active Status of type . - 1 = Both
* @ param int $affect Filter on affect ( a request will change sold or not ) . - 1 = Both
* @ return array Return array with list of types
*/
2019-02-25 20:35:59 +01:00
public function getTypes ( $active = - 1 , $affect = - 1 )
2017-10-07 13:09:31 +02:00
{
global $mysoc ;
2021-09-30 18:30:22 +02:00
$sql = " SELECT rowid, code, label, affect, delay, newbymonth " ;
2019-11-14 12:09:15 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " c_holiday_types " ;
2021-03-30 19:12:07 +02:00
$sql .= " WHERE (fk_country IS NULL OR fk_country = " . (( int ) $mysoc -> country_id ) . ')' ;
2021-02-26 17:59:31 +01:00
if ( $active >= 0 ) {
$sql .= " AND active = " . (( int ) $active );
}
if ( $affect >= 0 ) {
$sql .= " AND affect = " . (( int ) $affect );
}
2022-05-19 12:34:05 +02:00
$sql .= " ORDER BY sortorder " ;
2017-10-07 13:09:31 +02:00
$result = $this -> db -> query ( $sql );
2021-02-26 17:59:31 +01:00
if ( $result ) {
2017-10-07 13:09:31 +02:00
$num = $this -> db -> num_rows ( $result );
2021-02-26 17:59:31 +01:00
if ( $num ) {
while ( $obj = $this -> db -> fetch_object ( $result )) {
2021-10-05 16:18:56 +02:00
$types [ $obj -> rowid ] = array ( 'rowid' => $obj -> rowid , 'code' => $obj -> code , 'label' => $obj -> label , 'affect' => $obj -> affect , 'delay' => $obj -> delay , 'newbymonth' => $obj -> newbymonth );
2017-10-07 13:09:31 +02:00
}
return $types ;
}
2021-02-26 17:59:31 +01:00
} else {
dol_print_error ( $this -> db );
}
2017-10-07 13:09:31 +02:00
return array ();
}
2021-02-04 12:15:31 +01:00
/**
* Load information on object
*
* @ param int $id Id of object
* @ return void
*/
public function info ( $id )
{
global $conf ;
2021-08-18 00:12:25 +02:00
$sql = " SELECT f.rowid, f.statut as status, " ;
2021-02-04 12:15:31 +01:00
$sql .= " f.date_create as datec, " ;
$sql .= " f.tms as date_modification, " ;
$sql .= " f.date_valid as datev, " ;
2022-06-08 20:07:16 +02:00
$sql .= " f.date_approve as datea, " ;
2021-02-04 12:15:31 +01:00
$sql .= " f.date_refuse as dater, " ;
$sql .= " f.fk_user_create as fk_user_creation, " ;
$sql .= " f.fk_user_modif as fk_user_modification, " ;
2021-08-18 00:12:25 +02:00
$sql .= " f.fk_user_valid as fk_user_approve_done, " ;
$sql .= " f.fk_validator as fk_user_approve_expected, " ;
2021-02-04 12:15:31 +01:00
$sql .= " f.fk_user_refuse as fk_user_refuse " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " holiday as f " ;
2021-03-14 11:48:39 +01:00
$sql .= " WHERE f.rowid = " . (( int ) $id );
2021-02-04 12:15:31 +01:00
$sql .= " AND f.entity = " . $conf -> entity ;
$resql = $this -> db -> query ( $sql );
2021-02-26 17:59:31 +01:00
if ( $resql ) {
if ( $this -> db -> num_rows ( $resql )) {
2021-02-04 12:15:31 +01:00
$obj = $this -> db -> fetch_object ( $resql );
$this -> id = $obj -> rowid ;
$this -> date_creation = $this -> db -> jdate ( $obj -> datec );
$this -> date_modification = $this -> db -> jdate ( $obj -> date_modification );
$this -> date_validation = $this -> db -> jdate ( $obj -> datev );
$this -> date_approbation = $this -> db -> jdate ( $obj -> datea );
2022-06-08 20:07:16 +02:00
if ( ! empty ( $obj -> fk_user_creation )) {
2021-02-04 12:15:31 +01:00
$cuser = new User ( $this -> db );
$cuser -> fetch ( $obj -> fk_user_creation );
$this -> user_creation = $cuser ;
}
2022-06-08 20:07:16 +02:00
if ( ! empty ( $obj -> fk_user_approve_done )) {
2021-02-04 12:15:31 +01:00
$vuser = new User ( $this -> db );
2022-06-08 20:07:16 +02:00
$vuser -> fetch ( $obj -> fk_user_approve_done );
2021-02-04 12:15:31 +01:00
$this -> user_validation = $vuser ;
}
2022-06-08 20:07:16 +02:00
if ( ! empty ( $obj -> fk_user_modification )) {
2021-02-04 12:15:31 +01:00
$muser = new User ( $this -> db );
$muser -> fetch ( $obj -> fk_user_modification );
$this -> user_modification = $muser ;
}
2021-08-18 00:12:25 +02:00
if ( $obj -> status == Holiday :: STATUS_APPROVED || $obj -> status == Holiday :: STATUS_CANCELED ) {
if ( $obj -> fk_user_approve_done ) {
$auser = new User ( $this -> db );
$auser -> fetch ( $obj -> fk_user_approve_done );
$this -> user_approve = $auser ;
}
} else {
2022-06-08 20:07:16 +02:00
if ( ! empty ( $obj -> fk_user_approve_expected )) {
2021-08-18 00:12:25 +02:00
$auser = new User ( $this -> db );
$auser -> fetch ( $obj -> fk_user_approve_expected );
$this -> user_approve = $auser ;
}
2021-02-04 12:15:31 +01:00
}
}
$this -> db -> free ( $resql );
} else {
dol_print_error ( $this -> db );
}
}
2017-10-07 13:09:31 +02:00
/**
* Initialise an instance with random values .
* Used to build previews or test instances .
* id must be 0 if object instance is a specimen .
*
* @ return void
*/
2019-02-25 20:35:59 +01:00
public function initAsSpecimen ()
2017-10-07 13:09:31 +02:00
{
2019-11-14 12:09:15 +01:00
global $user , $langs ;
2017-10-07 13:09:31 +02:00
// Initialise parameters
2019-11-14 12:09:15 +01:00
$this -> id = 0 ;
$this -> specimen = 1 ;
2021-08-18 00:10:48 +02:00
$this -> fk_user = $user -> id ;
2019-11-14 12:09:15 +01:00
$this -> description = 'SPECIMEN description' ;
$this -> date_debut = dol_now ();
$this -> date_fin = dol_now () + ( 24 * 3600 );
$this -> date_valid = dol_now ();
2021-08-18 00:10:48 +02:00
$this -> fk_validator = $user -> id ;
2019-11-14 12:09:15 +01:00
$this -> halfday = 0 ;
$this -> fk_type = 1 ;
$this -> statut = Holiday :: STATUS_VALIDATED ;
2017-10-07 13:09:31 +02:00
}
2019-05-18 16:40:14 +02:00
2019-05-19 01:45:33 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2019-05-18 20:11:51 +02:00
/**
2020-10-31 14:32:18 +01:00
* Load this -> nb for dashboard
*
* @ return int < 0 if KO , > 0 if OK
*/
public function load_state_board ()
{
// phpcs:enable
2020-05-22 00:33:30 +02:00
global $user ;
2020-10-31 14:32:18 +01:00
$this -> nb = array ();
2019-05-18 16:40:14 +02:00
2020-10-31 14:32:18 +01:00
$sql = " SELECT count(h.rowid) as nb " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " holiday as h " ;
$sql .= " WHERE h.statut > 1 " ;
$sql .= " AND h.entity IN ( " . getEntity ( 'holiday' ) . " ) " ;
2021-02-26 17:59:31 +01:00
if ( empty ( $user -> rights -> expensereport -> readall )) {
2020-05-22 00:33:30 +02:00
$userchildids = $user -> getAllChildIds ( 1 );
2021-03-22 11:30:18 +01:00
$sql .= " AND (h.fk_user IN ( " . $this -> db -> sanitize ( join ( ',' , $userchildids )) . " ) " ;
$sql .= " OR h.fk_validator IN ( " . $this -> db -> sanitize ( join ( ',' , $userchildids )) . " )) " ;
2020-05-22 00:33:30 +02:00
}
2019-05-18 16:40:14 +02:00
2020-10-31 14:32:18 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql ) {
while ( $obj = $this -> db -> fetch_object ( $resql )) {
$this -> nb [ " holidays " ] = $obj -> nb ;
}
$this -> db -> free ( $resql );
return 1 ;
} else {
dol_print_error ( $this -> db );
$this -> error = $this -> db -> error ();
return - 1 ;
}
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Load indicators for dashboard ( this -> nbtodo and this -> nbtodolate )
*
* @ param User $user Objet user
* @ return WorkboardResponse | int < 0 if KO , WorkboardResponse if OK
*/
public function load_board ( $user )
{
// phpcs:enable
global $conf , $langs ;
2021-02-26 17:59:31 +01:00
if ( $user -> socid ) {
return - 1 ; // protection pour eviter appel par utilisateur externe
}
2020-10-31 14:32:18 +01:00
$now = dol_now ();
$sql = " SELECT h.rowid, h.date_debut " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " holiday as h " ;
$sql .= " WHERE h.statut = 2 " ;
$sql .= " AND h.entity IN ( " . getEntity ( 'holiday' ) . " ) " ;
2021-02-26 17:59:31 +01:00
if ( empty ( $user -> rights -> expensereport -> read_all )) {
2020-05-22 00:33:30 +02:00
$userchildids = $user -> getAllChildIds ( 1 );
2021-03-22 11:30:18 +01:00
$sql .= " AND (h.fk_user IN ( " . $this -> db -> sanitize ( join ( ',' , $userchildids )) . " ) " ;
$sql .= " OR h.fk_validator IN ( " . $this -> db -> sanitize ( join ( ',' , $userchildids )) . " )) " ;
2020-05-22 00:33:30 +02:00
}
2019-05-18 16:40:14 +02:00
2020-10-31 14:32:18 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-26 17:59:31 +01:00
if ( $resql ) {
2020-10-31 14:32:18 +01:00
$langs -> load ( " members " );
$response = new WorkboardResponse ();
$response -> warning_delay = $conf -> holiday -> approve -> warning_delay / 60 / 60 / 24 ;
$response -> label = $langs -> trans ( " HolidaysToApprove " );
$response -> labelShort = $langs -> trans ( " ToApprove " );
2021-07-02 11:35:26 +02:00
$response -> url = DOL_URL_ROOT . '/holiday/list.php?search_status=2&mainmenu=hrm&leftmenu=holiday' ;
2020-10-31 14:32:18 +01:00
$response -> img = img_object ( '' , " holiday " );
2021-02-26 17:59:31 +01:00
while ( $obj = $this -> db -> fetch_object ( $resql )) {
2020-10-31 14:32:18 +01:00
$response -> nbtodo ++ ;
if ( $this -> db -> jdate ( $obj -> date_debut ) < ( $now - $conf -> holiday -> approve -> warning_delay )) {
$response -> nbtodolate ++ ;
}
}
return $response ;
} else {
dol_print_error ( $this -> db );
$this -> error = $this -> db -> error ();
return - 1 ;
}
}
2012-09-05 03:26:57 +02:00
}