19use Luracast\Restler\RestException;
21require_once DOL_DOCUMENT_ROOT.
'/salaries/class/salary.class.php';
22require_once DOL_DOCUMENT_ROOT.
'/salaries/class/paymentsalary.class.php';
36 static $FIELDS = array(
73 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0)
77 if (!DolibarrApiAccess::$user->hasRight(
'salaries',
'read')) {
78 throw new RestException(403);
81 $sql =
"SELECT rowid FROM " . MAIN_DB_PREFIX .
"salary as t";
84 $sql .= $this->db->order($sortfield, $sortorder);
89 $offset = $limit * $page;
91 $sql .= $this->db->plimit($limit + 1, $offset);
95 $result = $this->db->query($sql);
98 $num = $this->db->num_rows($result);
99 $min = min($num, ($limit <= 0 ? $num : $limit));
100 for ($i = 0; $i < $min; $i++) {
101 $obj = $this->db->fetch_object($result);
102 $salary =
new Salary($this->db);
103 if ($salary->fetch($obj->rowid) > 0) {
108 throw new RestException(503,
'Error when retrieving list of salaries: ' . $this->db->lasterror());
124 if (!DolibarrApiAccess::$user->hasRight(
'salaries',
'read')) {
125 throw new RestException(403);
128 $salary =
new Salary($this->db);
129 $result = $salary->fetch(
$id);
131 throw new RestException(404,
'salary not found');
143 public function post($request_data =
null)
145 if (!DolibarrApiAccess::$user->hasRight(
'salaries',
'write')) {
146 throw new RestException(403);
149 $result = $this->
_validate($request_data);
151 $salary =
new Salary($this->db);
152 foreach ($request_data as $field => $value) {
156 if ($salary->create(DolibarrApiAccess::$user) < 0) {
157 throw new RestException(500,
'Error creating salary', array_merge(array($salary->error), $salary->errors));
169 public function put(
$id, $request_data =
null)
171 if (!DolibarrApiAccess::$user->hasRight(
'salaries',
'write')) {
172 throw new RestException(403);
175 $salary =
new Salary($this->db);
176 $result = $salary->fetch(
$id);
178 throw new RestException(404,
'salary not found');
181 foreach ($request_data as $field => $value) {
182 if ($field ==
'id') {
188 if ($salary->update(DolibarrApiAccess::$user) > 0) {
189 return $this->
get(
$id);
191 throw new RestException(500, $salary->error);
238 public function getAllPayments($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0)
242 if (!DolibarrApiAccess::$user->hasRight(
'salaries',
'read')) {
243 throw new RestException(403);
246 $sql =
"SELECT t.rowid FROM " . MAIN_DB_PREFIX .
"payment_salary as t, ".MAIN_DB_PREFIX.
"salary as s";
247 $sql .=
' WHERE s.rowid = t.fk_salary AND t.entity IN ('.getEntity(
'salary').
')';
249 $sql .= $this->db->order($sortfield, $sortorder);
254 $offset = $limit * $page;
256 $sql .= $this->db->plimit($limit + 1, $offset);
261 $result = $this->db->query($sql);
264 $num = $this->db->num_rows($result);
265 $min = min($num, ($limit <= 0 ? $num : $limit));
266 for ($i = 0; $i < $min; $i++) {
267 $obj = $this->db->fetch_object($result);
269 if ($paymentsalary->fetch($obj->rowid) > 0) {
274 throw new RestException(503,
'Error when retrieving list of paymentsalaries: ' . $this->db->lasterror());
292 if (!DolibarrApiAccess::$user->hasRight(
'salaries',
'read')) {
293 throw new RestException(403);
297 $result = $paymentsalary->fetch($pid);
299 throw new RestException(404,
'paymentsalary not found');
318 if (!DolibarrApiAccess::$user->hasRight(
'salaries',
'write')) {
319 throw new RestException(403);
325 $paymentsalary->fk_salary =
$id;
326 foreach ($request_data as $field => $value) {
327 $paymentsalary->$field = $this->
_checkValForAPI($field, $value, $paymentsalary);
330 if ($paymentsalary->create(DolibarrApiAccess::$user, 1) < 0) {
331 throw new RestException(500,
'Error creating paymentsalary', array_merge(array($paymentsalary->error), $paymentsalary->errors));
333 if (isModEnabled(
"bank")) {
334 $paymentsalary->addPaymentToBank(
335 DolibarrApiAccess::$user,
338 (
int) $request_data[
'accountid'],
343 return $paymentsalary->id;
359 if (!DolibarrApiAccess::$user->hasRight(
'salaries',
'write')) {
360 throw new RestException(403);
364 $result = $paymentsalary->fetch(
$id);
366 throw new RestException(404,
'Payment salary not found');
369 foreach ($request_data as $field => $value) {
370 if ($field ==
'id') {
373 $paymentsalary->$field = $this->
_checkValForAPI($field, $value, $paymentsalary);
376 if ($paymentsalary->update(DolibarrApiAccess::$user) > 0) {
377 return $this->
get(
$id);
379 throw new RestException(500, $paymentsalary->error);
426 foreach (Salaries::$FIELDS as $field) {
427 if (!isset($data[$field])) {
428 throw new RestException(400,
"$field field missing");
430 $salary[$field] = $data[$field];
445 $paymentsalary = array();
447 if (isModEnabled(
"bank")) array_push($fields,
"accountid");
448 foreach ($fields as $field) {
449 if (!isset($data[$field])) {
450 throw new RestException(400,
"$field field missing");
452 $paymentsalary[$field] = $data[$field];
454 return $paymentsalary;
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Class to manage payments of salaries.
put($id, $request_data=null)
Update salary.
__construct()
Constructor.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0)
Get the list of salaries.
updatePayment($id, $request_data=null)
Update paymentsalary.
_cleanObjectDatas($object)
Clean sensible object datas.
_validate($data)
Delete a payment salary.
getPayments($pid)
Get a given payment.
getAllPayments($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0)
Delete salary.
post($request_data=null)
Create salary object.
_validatepayments($data)
Validate fields before creating an object.
addPayment($id, $request_data=null)
Create payment salary on a salary.
static $FIELDSPAYMENT
array $FIELDS Mandatory fields, checked when creating an object
Class to manage salary payments.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.