NEW: API Add some api endpoints (#25126)

* feat(api): Add updateLine to Bank api + add salaries api

* feat(api): Add "deleteLine" to bank api

* fix(api): Remove unneeded mandatory field "salary"

* feat(api): Add PaymentSalaries and PaymentExpenseReports API

* fix(bankaccount): Fix bad phpdoc return type

* fix(bankaccount): Fix bad phpdoc return type and comments

* add updateLabel function to AccountLine

---------

Co-authored-by: Laurent Destailleur <eldy@destailleur.fr>
This commit is contained in:
notmarrco 2024-02-19 17:03:07 +01:00 committed by GitHub
parent a95cd96278
commit 2c3eff1896
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 894 additions and 18 deletions

View file

@ -2366,6 +2366,32 @@ class AccountLine extends CommonObjectLine
}
/**
* Update bank account record label in database
*
* @return int Return integer <0 if KO, >0 if OK
*/
public function updateLabel()
{
$this->db->begin();
$sql = "UPDATE ".MAIN_DB_PREFIX."bank SET";
$sql .= " label = '".$this->db->escape($this->label)."'";
$sql .= " WHERE rowid = ".((int) $this->rowid);
dol_syslog(get_class($this)."::update_label", LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql) {
$this->db->commit();
return 1;
} else {
$this->db->rollback();
$this->error = $this->db->error();
return -1;
}
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Update conciliation field

View file

@ -18,7 +18,7 @@
use Luracast\Restler\RestException;
require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
require_once DOL_DOCUMENT_ROOT . '/compta/bank/class/account.class.php';
/**
* API class for accounts
@ -73,12 +73,12 @@ class BankAccounts extends DolibarrApi
$sql = "SELECT t.rowid FROM ".MAIN_DB_PREFIX."bank_account AS t LEFT JOIN ".MAIN_DB_PREFIX."bank_account_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
if ($category > 0) {
$sql .= ", ".MAIN_DB_PREFIX."categorie_account as c";
$sql .= ", " . MAIN_DB_PREFIX . "categorie_account as c";
}
$sql .= ' WHERE t.entity IN ('.getEntity('bank_account').')';
$sql .= ' WHERE t.entity IN (' . getEntity('bank_account') . ')';
// Select accounts of given category
if ($category > 0) {
$sql .= " AND c.fk_categorie = ".((int) $category)." AND c.fk_account = t.rowid";
$sql .= " AND c.fk_categorie = " . ((int) $category) . " AND c.fk_account = t.rowid";
}
// Add sql filters
if ($sqlfilters) {
@ -113,7 +113,7 @@ class BankAccounts extends DolibarrApi
}
}
} else {
throw new RestException(503, 'Error when retrieving list of accounts: '.$this->db->lasterror());
throw new RestException(503, 'Error when retrieving list of accounts: ' . $this->db->lasterror());
}
return $list;
@ -205,7 +205,7 @@ class BankAccounts extends DolibarrApi
throw new RestException(403);
}
require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
require_once DOL_DOCUMENT_ROOT . '/compta/bank/class/account.class.php';
$accountfrom = new Account($this->db);
$resultAccountFrom = $accountfrom->fetch($bankaccount_from_id);
@ -281,7 +281,7 @@ class BankAccounts extends DolibarrApi
* Creating links between bank line record and its source
*/
$url = DOL_URL_ROOT.'/compta/bank/line.php?rowid=';
$url = DOL_URL_ROOT . '/compta/bank/line.php?rowid=';
$label = '(banktransfert)';
$type = 'banktransfert';
@ -312,7 +312,7 @@ class BankAccounts extends DolibarrApi
);
} else {
$this->db->rollback();
throw new RestException(500, $accountfrom->error.' '.$accountto->error);
throw new RestException(500, $accountfrom->error . ' ' . $accountto->error);
}
}
@ -446,8 +446,8 @@ class BankAccounts extends DolibarrApi
throw new RestException(404, 'account not found');
}
$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."bank ";
$sql .= " WHERE fk_account = ".((int) $id);
$sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . "bank ";
$sql .= " WHERE fk_account = " . ((int) $id);
// Add sql filters
if ($sqlfilters) {
@ -472,7 +472,7 @@ class BankAccounts extends DolibarrApi
}
}
} else {
throw new RestException(503, 'Error when retrieving list of account lines: '.$this->db->lasterror());
throw new RestException(503, 'Error when retrieving list of account lines: ' . $this->db->lasterror());
}
return $list;
@ -532,7 +532,7 @@ class BankAccounts extends DolibarrApi
$num_releve
);
if ($result < 0) {
throw new RestException(503, 'Error when adding line to account: '.$account->error);
throw new RestException(503, 'Error when adding line to account: ' . $account->error);
}
return $result;
}
@ -574,7 +574,7 @@ class BankAccounts extends DolibarrApi
$result = $account->add_url_line($line_id, $url_id, $url, $label, $type);
if ($result < 0) {
throw new RestException(503, 'Error when adding link to account line: '.$account->error);
throw new RestException(503, 'Error when adding link to account line: ' . $account->error);
}
return $result;
}
@ -604,6 +604,7 @@ class BankAccounts extends DolibarrApi
if (!$result) {
throw new RestException(404, 'account not found');
}
$links = $account->get_url($line_id); // Get an array('url'=>, 'url_id'=>, 'label'=>, 'type'=> 'fk_bank'=> )
foreach ($links as &$link) {
unset($link[0], $link[1], $link[2], $link[3]); // Remove the numeric keys
@ -611,4 +612,80 @@ class BankAccounts extends DolibarrApi
return $links;
}
/**
* Update an account line
*
* @param int $id ID of account
* @param int $line_id ID of account line
* @param string $label Label {@from body}
* @return int ID of link
*
* @url PUT {id}/lines/{line_id}
*/
public function updateLine($id, $line_id, $label)
{
if (!DolibarrApiAccess::$user->rights->banque->modifier) {
throw new RestException(401);
}
$account = new Account($this->db);
$result = $account->fetch($id);
if (!$result) {
throw new RestException(404, 'account not found');
}
$accountLine = new AccountLine($this->db);
$result = $accountLine->fetch($line_id);
if (!$result) {
throw new RestException(404, 'account line not found');
}
$accountLine->label = sanitizeVal($label);
$result = $accountLine->updateLabel();
if ($result < 0) {
throw new RestException(503, 'Error when updating link to account line: ' . $accountLine->error);
}
return $accountLine->id;
}
/**
* Delete an account line
*
* @param int $id ID of account
* @param int $line_id ID of account line
* @return array
*
* @url DELETE {id}/lines/{line_id}
*/
public function deleteLine($id, $line_id)
{
if (!DolibarrApiAccess::$user->rights->banque->modifier) {
throw new RestException(401);
}
$account = new Account($this->db);
$result = $account->fetch($id);
if (!$result) {
throw new RestException(404, 'account not found');
}
$accountLine = new AccountLine($this->db);
$result = $accountLine->fetch($line_id);
if (!$result) {
throw new RestException(404, 'account line not found');
}
if ($accountLine->delete(DolibarrApiAccess::$user) < 0) {
throw new RestException(401, 'error when deleting account line');
}
return array(
'success' => array(
'code' => 200,
'message' => "account line $line_id deleted"
)
);
}
}

View file

@ -1656,9 +1656,9 @@ function numero_semaine($time)
// Definition du numero de semaine: nb de jours entre "premier Jeudi de l'annee" et "Jeudi de la semaine";
$numeroSemaine = (
(
date("z", mktime(12, 0, 0, date("m", $jeudiSemaine), date("d", $jeudiSemaine), date("Y", $jeudiSemaine)))
-
date("z", mktime(12, 0, 0, date("m", $premierJeudiAnnee), date("d", $premierJeudiAnnee), date("Y", $premierJeudiAnnee)))
date("z", mktime(12, 0, 0, date("m", $jeudiSemaine), date("d", $jeudiSemaine), date("Y", $jeudiSemaine)))
-
date("z", mktime(12, 0, 0, date("m", $premierJeudiAnnee), date("d", $premierJeudiAnnee), date("Y", $premierJeudiAnnee)))
) / 7
) + 1;
@ -2675,6 +2675,10 @@ function getModuleDirForApiClass($moduleobject)
$moduledirforclass = 'accountancy';
} elseif (in_array($moduleobject, array('products', 'expensereports', 'users', 'tickets', 'boms', 'receptions', 'partnerships', 'recruitments'))) {
$moduledirforclass = preg_replace('/s$/', '', $moduleobject);
} elseif ($moduleobject == 'paymentsalaries') {
$moduledirforclass = 'salaries';
} elseif ($moduleobject == 'paymentexpensereports') {
$moduledirforclass = 'expensereport';
}
return $moduledirforclass;
@ -2847,8 +2851,8 @@ function phpSyntaxError($code)
if (preg_match("'syntax error, (.+) in .+ on line (\d+)$'s", $code, $code)) {
$code[2] = (int) $code[2];
$code = $code[2] <= $braces
? array($code[1], $code[2])
: array('unexpected $end'.substr($code[1], 14), $braces);
? array($code[1], $code[2])
: array('unexpected $end'.substr($code[1], 14), $braces);
} else {
$code = array('syntax error', 0);
}

View file

@ -0,0 +1,260 @@
<?php
/*
* Copyright (C) 2023 Marc Chenebaux <marc.chenebaux@maj44.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 . '/expensereport/class/paymentexpensereport.class.php';
/**
* API class for paymentexpensereport
*
* @property DoliDB db
* @access protected
* @class DolibarrApiAccess {@requires user,external}
*/
class PaymentExpenseReports extends DolibarrApi
{
/**
* array $FIELDS Mandatory fields, checked when creating an object
*/
static $FIELDS = array(
"fk_expensereport",
"fk_typepayment",
'datepaid',
'amounts',
);
/**
* Constructor
*/
public function __construct()
{
global $db;
$this->db = $db;
}
/**
* Get the list of paymentexpensereport.
*
* @param string $sortfield Sort field
* @param string $sortorder Sort order
* @param int $limit Limit for list
* @param int $page Page number
* @return array List of paymentExpenseReport objects
*
* @throws RestException
*/
public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0)
{
$list = array();
if (!DolibarrApiAccess::$user->rights->banque->lire) {
throw new RestException(401);
}
$sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . "payment_expensereport as t";
//$sql .= ' WHERE t.entity IN ('.getEntity('bank_account').')';
$sql .= $this->db->order($sortfield, $sortorder);
if ($limit) {
if ($page < 0) {
$page = 0;
}
$offset = $limit * $page;
$sql .= $this->db->plimit($limit + 1, $offset);
}
dol_syslog("API Rest request");
$result = $this->db->query($sql);
if ($result) {
$num = $this->db->num_rows($result);
$min = min($num, ($limit <= 0 ? $num : $limit));
for ($i = 0; $i < $min; $i++) {
$obj = $this->db->fetch_object($result);
$paymentExpenseReport = new PaymentExpenseReport($this->db);
if ($paymentExpenseReport->fetch($obj->rowid) > 0) {
$list[] = $this->_cleanObjectDatas($paymentExpenseReport);
}
}
} else {
throw new RestException(503, 'Error when retrieving list of paymentexpensereport: ' . $this->db->lasterror());
}
return $list;
}
/**
* Get paymentExpenseReport by ID.
*
* @param int $id ID of paymentExpenseReport
* @return array PaymentExpenseReport object
*
* @throws RestException
*/
public function get($id)
{
if (!DolibarrApiAccess::$user->rights->banque->lire) {
throw new RestException(401);
}
$paymentExpenseReport = new PaymentExpenseReport($this->db);
$result = $paymentExpenseReport->fetch($id);
if (!$result) {
throw new RestException(404, 'paymentExpenseReport not found');
}
return $this->_cleanObjectDatas($paymentExpenseReport);
}
/**
* Create paymentExpenseReport object
*
* @param array $request_data Request data
* @return int ID of paymentExpenseReport
*/
public function post($request_data = null)
{
if (!DolibarrApiAccess::$user->rights->banque->configurer) {
throw new RestException(401);
}
// Check mandatory fields
$result = $this->_validate($request_data);
$paymentExpenseReport = new PaymentExpenseReport($this->db);
foreach ($request_data as $field => $value) {
$paymentExpenseReport->$field = $this->_checkValForAPI($field, $value, $paymentExpenseReport);
}
if ($paymentExpenseReport->create(DolibarrApiAccess::$user, 1) < 0) {
throw new RestException(500, 'Error creating paymentExpenseReport', array_merge(array($paymentExpenseReport->error), $paymentExpenseReport->errors));
}
if (isModEnabled("banque")) $paymentExpenseReport->addPaymentToBank(
DolibarrApiAccess::$user,
'payment_expensereport',
'(ExpenseReportPayment)',
(int) $request_data['accountid'],
'',
''
);
return $paymentExpenseReport->id;
}
/**
* Update paymentExpenseReport
*
* @param int $id ID of paymentExpenseReport
* @param array $request_data data
* @return int
*/
public function put($id, $request_data = null)
{
/** @todo ->rights->paymentexpensereport ? */
if (!DolibarrApiAccess::$user->rights->banque->creer) {
throw new RestException(401);
}
$paymentExpenseReport = new PaymentExpenseReport($this->db);
$result = $paymentExpenseReport->fetch($id);
if (!$result) {
throw new RestException(404, 'paymentExpenseReport not found');
}
foreach ($request_data as $field => $value) {
if ($field == 'id') {
continue;
}
$paymentExpenseReport->$field = $this->_checkValForAPI($field, $value, $paymentExpenseReport);
}
if ($paymentExpenseReport->update(DolibarrApiAccess::$user) > 0) {
return $this->get($id);
} else {
throw new RestException(500, $paymentExpenseReport->error);
}
}
/**
* Delete paymentExpenseReport
*
* @param int $id ID of paymentExpenseReport
* @return array
*/
/*public function delete($id)
{
if (!DolibarrApiAccess::$user->rights->banque->configurer) {
throw new RestException(401);
}
$paymentExpenseReport = new PaymentExpenseReport($this->db);
$result = $paymentExpenseReport->fetch($id);
if (!$result) {
throw new RestException(404, 'paymentExpenseReport not found');
}
if ($paymentExpenseReport->delete(DolibarrApiAccess::$user) < 0) {
throw new RestException(401, 'error when deleting paymentExpenseReport');
}
return array(
'success' => array(
'code' => 200,
'message' => 'paymentExpenseReport deleted'
)
);
}*/
/**
* Validate fields before creating an object
*
* @param array|null $data Data to validate
* @return array
*
* @throws RestException
*/
private function _validate($data)
{
$paymentExpenseReport = array();
$fields = PaymentExpenseReports::$FIELDS;
if (isModEnabled("banque")) array_push($fields, "accountid");
foreach ($fields as $field) {
if (!isset($data[$field])) {
throw new RestException(400, "$field field missing");
}
$paymentExpenseReport[$field] = $data[$field];
}
return $paymentExpenseReport;
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
/**
* Clean sensible object datas
*
* @param Object $object Object to clean
* @return Object Object with cleaned properties
*/
protected function _cleanObjectDatas($object)
{
// phpcs:enable
$object = parent::_cleanObjectDatas($object);
unset($object->rowid);
return $object;
}
}

View file

@ -0,0 +1,260 @@
<?php
/*
* Copyright (C) 2023 Marc Chenebaux <marc.chenebaux@maj44.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 . '/salaries/class/paymentsalary.class.php';
/**
* API class for paymentsalaries
*
* @property DoliDB db
* @access protected
* @class DolibarrApiAccess {@requires user,external}
*/
class PaymentSalaries extends DolibarrApi
{
/**
* array $FIELDS Mandatory fields, checked when creating an object
*/
static $FIELDS = array(
"paiementtype",
'datepaye',
'chid',
'amounts',
);
/**
* Constructor
*/
public function __construct()
{
global $db;
$this->db = $db;
}
/**
* Get the list of paymentsalaries.
*
* @param string $sortfield Sort field
* @param string $sortorder Sort order
* @param int $limit Limit for list
* @param int $page Page number
* @return array List of paymentsalary objects
*
* @throws RestException
*/
public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0)
{
$list = array();
if (!DolibarrApiAccess::$user->rights->banque->lire) {
throw new RestException(401);
}
$sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . "payment_salary as t";
//$sql .= ' WHERE t.entity IN ('.getEntity('bank_account').')';
$sql .= $this->db->order($sortfield, $sortorder);
if ($limit) {
if ($page < 0) {
$page = 0;
}
$offset = $limit * $page;
$sql .= $this->db->plimit($limit + 1, $offset);
}
dol_syslog("API Rest request");
$result = $this->db->query($sql);
if ($result) {
$num = $this->db->num_rows($result);
$min = min($num, ($limit <= 0 ? $num : $limit));
for ($i = 0; $i < $min; $i++) {
$obj = $this->db->fetch_object($result);
$paymentsalary = new PaymentSalary($this->db);
if ($paymentsalary->fetch($obj->rowid) > 0) {
$list[] = $this->_cleanObjectDatas($paymentsalary);
}
}
} else {
throw new RestException(503, 'Error when retrieving list of paymentsalaries: ' . $this->db->lasterror());
}
return $list;
}
/**
* Get paymentsalary by ID.
*
* @param int $id ID of paymentsalary
* @return array PaymentSalary object
*
* @throws RestException
*/
public function get($id)
{
if (!DolibarrApiAccess::$user->rights->banque->lire) {
throw new RestException(401);
}
$paymentsalary = new PaymentSalary($this->db);
$result = $paymentsalary->fetch($id);
if (!$result) {
throw new RestException(404, 'paymentsalary not found');
}
return $this->_cleanObjectDatas($paymentsalary);
}
/**
* Create paymentsalary object
*
* @param array $request_data Request data
* @return int ID of paymentsalary
*/
public function post($request_data = null)
{
if (!DolibarrApiAccess::$user->rights->banque->configurer) {
throw new RestException(401);
}
// Check mandatory fields
$result = $this->_validate($request_data);
$paymentsalary = new PaymentSalary($this->db);
foreach ($request_data as $field => $value) {
$paymentsalary->$field = $this->_checkValForAPI($field, $value, $paymentsalary);
}
if ($paymentsalary->create(DolibarrApiAccess::$user, 1) < 0) {
throw new RestException(500, 'Error creating paymentsalary', array_merge(array($paymentsalary->error), $paymentsalary->errors));
}
if (isModEnabled("banque")) $paymentsalary->addPaymentToBank(
DolibarrApiAccess::$user,
'payment_salary',
'(SalaryPayment)',
(int) $request_data['accountid'],
'',
''
);
return $paymentsalary->id;
}
/**
* Update paymentsalary
*
* @param int $id ID of paymentsalary
* @param array $request_data data
* @return int
*/
public function put($id, $request_data = null)
{
/** @todo ->rights->paymentsalaries ? */
if (!DolibarrApiAccess::$user->rights->banque->creer) {
throw new RestException(401);
}
$paymentsalary = new PaymentSalary($this->db);
$result = $paymentsalary->fetch($id);
if (!$result) {
throw new RestException(404, 'paymentsalary not found');
}
foreach ($request_data as $field => $value) {
if ($field == 'id') {
continue;
}
$paymentsalary->$field = $this->_checkValForAPI($field, $value, $paymentsalary);
}
if ($paymentsalary->update(DolibarrApiAccess::$user) > 0) {
return $this->get($id);
} else {
throw new RestException(500, $paymentsalary->error);
}
}
/**
* Delete paymentsalary
*
* @param int $id ID of paymentsalary
* @return array
*/
/*public function delete($id)
{
if (!DolibarrApiAccess::$user->rights->banque->configurer) {
throw new RestException(401);
}
$paymentsalary = new PaymentSalary($this->db);
$result = $paymentsalary->fetch($id);
if (!$result) {
throw new RestException(404, 'paymentsalary not found');
}
if ($paymentsalary->delete(DolibarrApiAccess::$user) < 0) {
throw new RestException(401, 'error when deleting paymentsalary');
}
return array(
'success' => array(
'code' => 200,
'message' => 'paymentsalary deleted'
)
);
}*/
/**
* Validate fields before creating an object
*
* @param array|null $data Data to validate
* @return array
*
* @throws RestException
*/
private function _validate($data)
{
$paymentsalary = array();
$fields = PaymentSalaries::$FIELDS;
if (isModEnabled("banque")) array_push($fields, "accountid");
foreach ($fields as $field) {
if (!isset($data[$field])) {
throw new RestException(400, "$field field missing");
}
$paymentsalary[$field] = $data[$field];
}
return $paymentsalary;
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
/**
* Clean sensible object datas
*
* @param Object $object Object to clean
* @return Object Object with cleaned properties
*/
protected function _cleanObjectDatas($object)
{
// phpcs:enable
$object = parent::_cleanObjectDatas($object);
unset($object->rowid);
return $object;
}
}

View file

@ -0,0 +1,249 @@
<?php
/*
* Copyright (C) 2023 Marc Chenebaux <marc.chenebaux@maj44.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 . '/salaries/class/salary.class.php';
/**
* API class for salaries
*
* @property DoliDB db
* @access protected
* @class DolibarrApiAccess {@requires user,external}
*/
class Salaries extends DolibarrApi
{
/**
* array $FIELDS Mandatory fields, checked when creating an object
*/
static $FIELDS = array(
'fk_user',
'label',
'amount',
);
/**
* Constructor
*/
public function __construct()
{
global $db;
$this->db = $db;
}
/**
* Get the list of salaries.
*
* @param string $sortfield Sort field
* @param string $sortorder Sort order
* @param int $limit Limit for list
* @param int $page Page number
* @return array List of salary objects
*
* @throws RestException
*/
public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0)
{
$list = array();
if (!DolibarrApiAccess::$user->rights->banque->lire) {
throw new RestException(401);
}
$sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . "salary as t";
//$sql .= ' WHERE t.entity IN ('.getEntity('bank_account').')';
$sql .= $this->db->order($sortfield, $sortorder);
if ($limit) {
if ($page < 0) {
$page = 0;
}
$offset = $limit * $page;
$sql .= $this->db->plimit($limit + 1, $offset);
}
dol_syslog("API Rest request");
$result = $this->db->query($sql);
if ($result) {
$num = $this->db->num_rows($result);
$min = min($num, ($limit <= 0 ? $num : $limit));
for ($i = 0; $i < $min; $i++) {
$obj = $this->db->fetch_object($result);
$salary = new Salary($this->db);
if ($salary->fetch($obj->rowid) > 0) {
$list[] = $this->_cleanObjectDatas($salary);
}
}
} else {
throw new RestException(503, 'Error when retrieving list of salaries: ' . $this->db->lasterror());
}
return $list;
}
/**
* Get salary by ID.
*
* @param int $id ID of salary
* @return array Salary object
*
* @throws RestException
*/
public function get($id)
{
if (!DolibarrApiAccess::$user->rights->banque->lire) {
throw new RestException(401);
}
$salary = new Salary($this->db);
$result = $salary->fetch($id);
if (!$result) {
throw new RestException(404, 'salary not found');
}
return $this->_cleanObjectDatas($salary);
}
/**
* Create salary object
*
* @param array $request_data Request data
* @return int ID of salary
*/
public function post($request_data = null)
{
if (!DolibarrApiAccess::$user->rights->banque->configurer) {
throw new RestException(401);
}
// Check mandatory fields
$result = $this->_validate($request_data);
$salary = new Salary($this->db);
foreach ($request_data as $field => $value) {
$salary->$field = $this->_checkValForAPI($field, $value, $salary);
}
if ($salary->create(DolibarrApiAccess::$user) < 0) {
throw new RestException(500, 'Error creating salary', array_merge(array($salary->error), $salary->errors));
}
return $salary->id;
}
/**
* Update salary
*
* @param int $id ID of salary
* @param array $request_data data
* @return int
*/
public function put($id, $request_data = null)
{
/** @todo ->rights->salaries ? */
if (!DolibarrApiAccess::$user->rights->banque->creer) {
throw new RestException(401);
}
$salary = new Salary($this->db);
$result = $salary->fetch($id);
if (!$result) {
throw new RestException(404, 'salary not found');
}
foreach ($request_data as $field => $value) {
if ($field == 'id') {
continue;
}
$salary->$field = $this->_checkValForAPI($field, $value, $salary);
}
if ($salary->update(DolibarrApiAccess::$user) > 0) {
return $this->get($id);
} else {
throw new RestException(500, $salary->error);
}
}
/**
* Delete salary
*
* @param int $id ID of salary
* @return array
*/
/*public function delete($id)
{
if (!DolibarrApiAccess::$user->rights->banque->configurer) {
throw new RestException(401);
}
$salary = new Salary($this->db);
$result = $salary->fetch($id);
if (!$result) {
throw new RestException(404, 'salary not found');
}
if ($salary->delete(DolibarrApiAccess::$user) < 0) {
throw new RestException(401, 'error when deleting salary');
}
return array(
'success' => array(
'code' => 200,
'message' => 'salary deleted'
)
);
}*/
/**
* Validate fields before creating an object
*
* @param array|null $data Data to validate
* @return array
*
* @throws RestException
*/
private function _validate($data)
{
$salary = array();
foreach (Salaries::$FIELDS as $field) {
if (!isset($data[$field])) {
throw new RestException(400, "$field field missing");
}
$salary[$field] = $data[$field];
}
return $salary;
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
/**
* Clean sensible object datas
*
* @param Object $object Object to clean
* @return Object Object with cleaned properties
*/
protected function _cleanObjectDatas($object)
{
// phpcs:enable
$object = parent::_cleanObjectDatas($object);
unset($object->rowid);
return $object;
}
}