NEW Add API for Holidays/Leaves

This commit is contained in:
Laurent Destailleur
2025-10-29 18:58:06 +01:00
parent f32aeac947
commit 4c84650e39
8 changed files with 524 additions and 31 deletions

View File

@@ -213,6 +213,7 @@ class DolibarrApi
unset($object->error);
unset($object->errors);
unset($object->errorhidden);
unset($object->TRIGGER_PREFIX);
unset($object->ref_previous);
unset($object->ref_next);

View File

@@ -55,17 +55,17 @@ class DolibarrApiAccess implements iAuthenticate
public $db;
/**
* @var string[] role required by API method user / external / admin
* @var string[] Role required by API method user / external / admin
*/
public static $requires = array('user', 'external', 'admin');
/**
* @var string user role
* @var string User role
*/
public static $role = 'user';
/**
* @var User $user Loggued user
* @var User Loggued user
*/
public static $user = null;

View File

@@ -77,7 +77,7 @@ class Login
* Login
*
* Request the API token for a couple username / password.
* WARNING: You should NEVER use this API, like you should never use the similar API that uses the POST method. This will expose your password.
* WARNING: You should NEVER use this API, like you should never use the similar API that uses the GET method. This will expose your password.
* To use the APIs, you should instead set an API token to the user you want to allow to use API (This API token called DOLAPIKEY can be found/set on the user page) and use this token as credential for any API call.
* From the API explorer, you can enter directly the "DOLAPIKEY" into the field at the top right of the page to get access to any allowed APIs.
*

View File

@@ -328,11 +328,6 @@ if (!empty($reg[1]) && ($reg[1] != 'explorer' || ($reg[2] != '/swagger.json' &&
$moduleobject = strtolower($moduleobject);
$moduledirforclass = getModuleDirForApiClass($moduleobject);
// this works, but is only necessary because I don't know how to get getModuleDirForApiClass to answer correctly with comm/mailing
if ($moduleobject == 'mailings') {
$moduledirforclass = 'comm/mailing';
}
// Load a dedicated API file
dol_syslog("Load a dedicated API file moduleobject=".$moduleobject." moduledirforclass=".$moduledirforclass);
@@ -358,9 +353,6 @@ if (!empty($reg[1]) && ($reg[1] != 'explorer' || ($reg[2] != '/swagger.json' &&
if ($moduleobject == 'interventions') {
$classfile = 'interventions';
}
if ($moduleobject == 'eventattendees') {
$moduledirforclass = 'eventorganization';
}
$dir_part_file = dol_buildpath('/'.$moduledirforclass.'/class/api_'.$classfile.'.class.php', 0, 2);

View File

@@ -2662,7 +2662,7 @@ function getModuleDirForApiClass($moduleobject)
$moduledirforclass = 'comm/propal';
} elseif ($moduleobject == 'agenda' || $moduleobject == 'agendaevents') {
$moduledirforclass = 'comm/action';
} elseif ($moduleobject == 'mailing') {
} elseif ($moduleobject == 'mailing' || $moduleobject == 'mailings') {
$moduledirforclass = 'comm/mailing';
} elseif (in_array($moduleobject, ['adherent', 'members', 'memberstypes', 'subscriptions'])) {
$moduledirforclass = 'adherents';
@@ -2704,6 +2704,10 @@ function getModuleDirForApiClass($moduleobject)
$moduledirforclass = 'salaries';
} elseif ($moduleobject == 'paymentexpensereports') {
$moduledirforclass = 'expensereport';
} elseif ($moduleobject == 'eventattendees') {
$moduledirforclass = 'eventorganization';
} elseif ($moduleobject == 'holidays') {
$moduledirforclass = 'holiday';
}
return $moduledirforclass;

View File

@@ -49,7 +49,7 @@ class Shipments extends DolibarrApi
*/
public function __construct()
{
global $db, $conf;
global $db;
$this->db = $db;
$this->shipment = new Expedition($this->db);
}

View File

@@ -0,0 +1,504 @@
<?php
/* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
* Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2020-2024 Frédéric France <frederic.france@free.fr>
* Copyright (C) 2025 MDW <mdeweerd@users.noreply.github.com>
* Copyright (C) 2025 William Mead <william@m34d.com>
*
* 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
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use Luracast\Restler\RestException;
require_once DOL_DOCUMENT_ROOT.'/holiday/class/holiday.class.php';
/**
* API class for Leaves
*
* @since 5.0.0 Initial implementation
*
* @access protected
* @class DolibarrApiAccess {@requires user,external}
*/
class Holidays extends DolibarrApi
{
/**
* @var string[] Mandatory fields, checked when create and update object
*/
public static $FIELDS = array(
'fk_user',
'date_debut',
'date_fin',
);
/**
* @var Holiday {@type Holiday}
*/
public $holiday;
/**
* Constructor
*/
public function __construct()
{
global $db;
$this->db = $db;
$this->holiday = new Holiday($this->db);
}
/**
* Get a leave
*
* Return an array with leave information
*
* @since 5.0.0 Initial implementation
*
* @param int $id ID of Leave
* @return Object Object with cleaned properties
*
* @throws RestException
*/
public function get($id)
{
if (!DolibarrApiAccess::$user->hasRight('holiday', 'read')) {
throw new RestException(403);
}
$result = $this->holiday->fetch($id);
if (!$result) {
throw new RestException(404, 'Leave not found');
}
if (!DolibarrApi::_checkAccessToResource('holiday', $this->holiday->id)) {
throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
$this->holiday->fetchObjectLinked();
return $this->_cleanObjectDatas($this->holiday);
}
/**
* List leaves
*
* Get a list of Leaves
*
* @since 5.0.0 Initial implementation
*
* @param string $sortfield Sort field
* @param string $sortorder Sort order
* @param int $limit List limit
* @param int $page Page number
* @param string $user_ids User ids filter field. Example: '1' or '1,2,3' {@pattern /^[0-9,]*$/i}
* @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')"
* @param string $properties Restrict the data returned to these properties. Ignored if empty. Comma separated list of properties names
* @param bool $pagination_data If this parameter is set to true the response will include pagination data. Default value is false. Page starts from 0*
* @return array Array of order objects
*
* @throws RestException
*/
public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $user_ids = '', $sqlfilters = '', $properties = '', $pagination_data = false)
{
// TODO Check on permission holiday->read only if all ID are inside the childids of user
if (!DolibarrApiAccess::$user->hasRight('holiday', 'readall')) {
throw new RestException(403);
}
$obj_ret = array();
// case of external user, $societe param is ignored and replaced by user's socid
//$socid = DolibarrApiAccess::$user->socid ?: $societe;
$sql = "SELECT t.rowid";
$sql .= " FROM ".MAIN_DB_PREFIX."holiday AS t LEFT JOIN ".MAIN_DB_PREFIX."holiday_extrafields AS ef ON (ef.fk_object = t.rowid)"; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields
$sql .= ' WHERE t.entity IN ('.getEntity('holiday').')';
if ($user_ids) {
$sql .= " AND t.fk_user IN (".$this->db->sanitize($user_ids).")";
}
// Add sql filters
if ($sqlfilters) {
$errormessage = '';
$sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
if ($errormessage) {
throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
}
}
//this query will return total orders with the filters given
$sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
$sql .= $this->db->order($sortfield, $sortorder);
if ($limit) {
if ($page < 0) {
$page = 0;
}
$offset = $limit * $page;
$sql .= $this->db->plimit($limit + 1, $offset);
}
$result = $this->db->query($sql);
if ($result) {
$num = $this->db->num_rows($result);
$min = min($num, ($limit <= 0 ? $num : $limit));
$i = 0;
while ($i < $min) {
$obj = $this->db->fetch_object($result);
$holiday_static = new Holiday($this->db);
if ($holiday_static->fetch($obj->rowid)) {
$obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($holiday_static), $properties);
}
$i++;
}
} else {
throw new RestException(503, 'Error when retrieve Leave list : '.$this->db->lasterror());
}
//if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
if ($pagination_data) {
$totalsResult = $this->db->query($sqlTotals);
$total = $this->db->fetch_object($totalsResult)->total;
$tmp = $obj_ret;
$obj_ret = [];
$obj_ret['data'] = $tmp;
$obj_ret['pagination'] = [
'total' => (int) $total,
'page' => $page, //count starts from 0
'page_count' => ceil((int) $total / $limit),
'limit' => $limit
];
}
return $obj_ret;
}
/**
* Create a leave
*
* @since 5.0.0 Initial implementation
*
* @param array $request_data Request data
* @phan-param ?array<string,string> $request_data
* @phpstan-param ?array<string,string> $request_data
* @return int ID of Leave
*
* @throws RestException
*/
public function post($request_data = null)
{
if (!DolibarrApiAccess::$user->hasRight('holiday', 'creer')) {
throw new RestException(403, "Insufficiant rights");
}
// Check mandatory fields
$result = $this->_validate($request_data);
foreach ($request_data as $field => $value) {
if ($field === 'caller') {
// Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
$this->holiday->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
continue;
}
$this->holiday->$field = $this->_checkValForAPI($field, $value, $this->holiday);
}
/*if (isset($request_data["lines"])) {
$lines = array();
foreach ($request_data["lines"] as $line) {
array_push($lines, (object) $line);
}
$this->holiday->lines = $lines;
}*/
if ($this->holiday->create(DolibarrApiAccess::$user) < 0) {
throw new RestException(500, "Error creating holiday", array_merge(array($this->holiday->error), $this->holiday->errors));
}
return $this->holiday->id;
}
/**
* Update expense report general fields
*
* Does not touch lines of the expense report
*
* @since 5.0.0 Initial implementation
*
* @param int $id ID of Expense Report to update
* @param array $request_data Expense report data
* @phan-param ?array<string,string> $request_data
* @phpstan-param ?array<string,string> $request_data
* @return Object Updated object
*
* @throws RestException 401 Not allowed
* @throws RestException 404 Expense report not found
* @throws RestException 500 System error
*/
public function put($id, $request_data = null)
{
if (!DolibarrApiAccess::$user->hasRight('holiday', 'creer')) {
throw new RestException(403);
}
$result = $this->holiday->fetch($id);
if (!$result) {
throw new RestException(404, 'holiday not found');
}
if (!DolibarrApi::_checkAccessToResource('holiday', $this->holiday->id)) {
throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
foreach ($request_data as $field => $value) {
if ($field == 'id') {
continue;
}
if ($field === 'caller') {
// Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
$this->holiday->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
continue;
}
if ($field == 'array_options' && is_array($value)) {
foreach ($value as $index => $val) {
$this->holiday->array_options[$index] = $this->_checkValForAPI($field, $val, $this->holiday);
}
continue;
}
$this->holiday->$field = $this->_checkValForAPI($field, $value, $this->holiday);
}
if ($this->holiday->update(DolibarrApiAccess::$user) > 0) {
return $this->get($id);
} else {
throw new RestException(500, $this->holiday->error);
}
}
/**
* Delete holiday
*
* @since 5.0.0 Initial implementation
*
* @param int $id Expense Report ID
* @return array
* @phan-return array{success:array{code:int,message:string}}
* @phpstan-return array{success:array{code:int,message:string}}
*
* @throws RestException
*/
public function delete($id)
{
if (!DolibarrApiAccess::$user->hasRight('holiday', 'delete')) {
throw new RestException(403);
}
$result = $this->holiday->fetch($id);
if (!$result) {
throw new RestException(404, 'Leave not found');
}
if (!DolibarrApi::_checkAccessToResource('holiday', $this->holiday->id)) {
throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
if (!$this->holiday->delete(DolibarrApiAccess::$user)) {
throw new RestException(500, 'Error when deleting Leave : '.$this->holiday->error);
}
return array(
'success' => array(
'code' => 200,
'message' => 'Leave deleted'
)
);
}
/**
* Validate a holiday
*
* If you get a bad value for param notrigger check, provide this in body
* {
* "notrigger": 0
* }
*
* @since 22.0.0 Initial implementation
*
* @param int $id Expense report ID
* @param int $notrigger 1=Does not execute triggers, 0= execute triggers
*
* @url POST {id}/validate
*
* @return Object
*
* @throws RestException
*/
public function validate($id, $notrigger = 0)
{
if (!DolibarrApiAccess::$user->hasRight('holiday', 'creer')) {
throw new RestException(403, "Insufficiant rights");
}
$result = $this->holiday->fetch($id);
if (!$result) {
throw new RestException(404, 'Leave not found');
}
if (!DolibarrApi::_checkAccessToResource('holiday', $this->holiday->id)) {
throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
$result = $this->holiday->setValidate(DolibarrApiAccess::$user, $notrigger);
if ($result == 0) {
throw new RestException(304, 'Error nothing done. May be object is already validated');
}
if ($result < 0) {
throw new RestException(500, 'Error when validating leave: '.$this->holiday->error);
}
return $this->_cleanObjectDatas($this->holiday);
}
/**
* Approve a leave
*
* If you get a bad value for param notrigger check, provide this in body
* {
* "notrigger": 0
* }
*
* @since 22.0.0 Initial implementation
*
* @param int $id Leave ID
* @param int $notrigger 1=Does not execute triggers, 0= execute triggers
*
* @url POST {id}/approve
*
* @return Object
*
* @throws RestException
*/
public function approve($id, $notrigger = 0)
{
if (!DolibarrApiAccess::$user->hasRight('holiday', 'approve')) {
throw new RestException(403, "Insufficiant rights");
}
$result = $this->holiday->fetch($id);
if (!$result) {
throw new RestException(404, 'Leave not found');
}
if (!DolibarrApi::_checkAccessToResource('holiday', $this->holiday->id)) {
throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
$result = $this->holiday->setApproved(DolibarrApiAccess::$user, $notrigger);
if ($result == 0) {
throw new RestException(304, 'Error nothing done. May be object is already approved');
}
if ($result < 0) {
throw new RestException(500, 'Error when approving expense report: '.$this->holiday->error);
}
return $this->_cleanObjectDatas($this->holiday);
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
/**
* Clean sensible object datas
*
* @param Holiday $object Object to clean
* @return Object Object with cleaned properties
*/
protected function _cleanObjectDatas($object)
{
// phpcs:enable
$object = parent::_cleanObjectDatas($object);
/**
* @var Holiday $object
*/
unset($object->statut);
unset($object->user);
unset($object->thirdparty);
unset($object->cond_reglement);
unset($object->shipping_method_id);
unset($object->barcode_type);
unset($object->barcode_type_code);
unset($object->barcode_type_label);
unset($object->barcode_type_coder);
unset($object->mode_reglement_id);
unset($object->cond_reglement_id);
unset($object->name);
unset($object->lastname);
unset($object->firstname);
unset($object->civility_id);
unset($object->cond_reglement_id);
unset($object->contact);
unset($object->contact_id);
unset($object->state);
unset($object->state_id);
unset($object->state_code);
unset($object->country);
unset($object->country_id);
unset($object->country_code);
unset($object->logs);
unset($object->events);
unset($object->holiday);
unset($object->canvas);
unset($object->lines);
unset($object->totalpaid);
unset($object->totalpaid_multicurrency);
unset($object->note); // We already use note_public and note_pricate
return $object;
}
/**
* Validate fields before create or update object
*
* @param ?array<string,string> $data Array with data to verify
* @return array<string,string>
* @throws RestException
*/
private function _validate($data)
{
if ($data === null) {
$data = array();
}
$expensereport = array();
foreach (Holiday::$FIELDS as $field) {
if (!isset($data[$field])) {
throw new RestException(400, "$field field missing");
}
$expensereport[$field] = $data[$field];
}
return $expensereport;
}
}

View File

@@ -39,6 +39,12 @@ class Holiday extends CommonObject
*/
public $element = 'holiday';
/**
* @var string Prefix to check for any trigger code of any business class to prevent bad value for trigger code.
* @see CommonTrigger::call_trigger()
*/
public $TRIGGER_PREFIX = 'HOLIDAY';
/**
* @var string Name of table without prefix where object is stored
*/
@@ -169,19 +175,6 @@ class Holiday extends CommonObject
public $logs = array();
/**
* @var string
*/
public $optName = '';
/**
* @var string
*/
public $optValue = '';
/**
* @var int
*/
public $optRowid = 0;
/**
* Draft status
*/
@@ -2228,8 +2221,6 @@ class Holiday extends CommonObject
*/
public function addLogCP($fk_user_action, $fk_user_update, $label, $new_solde, $fk_type)
{
global $conf, $langs;
$error = 0;
$prev_solde = price2num((float) $this->getCPforUser($fk_user_update, $fk_type), 5);
@@ -2267,8 +2258,9 @@ class Holiday extends CommonObject
$this->errors[] = "Error ".$this->db->lasterror();
}
$optRowid = 0;
if (!$error) {
$this->optRowid = $this->db->last_insert_id(MAIN_DB_PREFIX."holiday_logs");
$optRowid = $this->db->last_insert_id(MAIN_DB_PREFIX."holiday_logs");
}
// Commit or rollback
@@ -2281,7 +2273,7 @@ class Holiday extends CommonObject
return -1 * $error;
} else {
$this->db->commit();
return $this->optRowid;
return $optRowid;
}
}