dolibarr 24.0.0-beta
api_salaries.class.php
1<?php
2/*
3 * Copyright (C) 2023 Marc Chenebaux <marc.chenebaux@maj44.com>
4 * Copyright (C) 2025 MDW <mdeweerd@users.noreply.github.com>
5 * Copyright (C) 2025 Frédéric France <frederic.france@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21use Luracast\Restler\RestException;
22
23require_once DOL_DOCUMENT_ROOT.'/salaries/class/salary.class.php';
24require_once DOL_DOCUMENT_ROOT.'/salaries/class/paymentsalary.class.php';
25
26
33class Salaries extends DolibarrApi
34{
38 public static $FIELDS = array(
39 'fk_user',
40 'label',
41 'amount',
42 );
43
47 public static $FIELDSPAYMENT = array(
48 "paiementtype",
49 'datepaye',
50 'chid',
51 'amounts',
52 );
53
54
58 public function __construct()
59 {
60 global $db;
61 $this->db = $db;
62 }
63
77 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0)
78 {
79 $list = array();
80
81 if (!DolibarrApiAccess::$user->hasRight('salaries', 'read')
82 && !DolibarrApiAccess::$user->hasRight('salaries', 'readchild')
83 && !DolibarrApiAccess::$user->hasRight('salaries', 'readall')) {
84 throw new RestException(403);
85 }
86
87 $sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . "salary as t";
88 $sql .= ' WHERE t.entity IN ('.getEntity('user').')';
89 if (!DolibarrApiAccess::$user->hasRight('salaries', 'readall')) {
90 if (!DolibarrApiAccess::$user->hasRight('salaries', 'readchild')) {
91 $sql .= ' AND t.fk_user = '.((int) DolibarrApiAccess::$user->id).')';
92 } else {
93 $childids = DolibarrApiAccess::$user->getAllChildIds(1);
94 $sql .= ' AND t.fk_user IN ('.$this->db->sanitize(implode(',', $childids)).')';
95 }
96 }
97
98 $sql .= $this->db->order($sortfield, $sortorder);
99 if ($limit) {
100 if ($page < 0) {
101 $page = 0;
102 }
103 $offset = $limit * $page;
104
105 $sql .= $this->db->plimit($limit + 1, $offset);
106 }
107
108 dol_syslog("API Rest request");
109 $result = $this->db->query($sql);
110
111 if ($result) {
112 $num = $this->db->num_rows($result);
113 $min = min($num, ($limit <= 0 ? $num : $limit));
114 for ($i = 0; $i < $min; $i++) {
115 $obj = $this->db->fetch_object($result);
116 $salary = new Salary($this->db);
117 if ($salary->fetch($obj->rowid) > 0) {
118 $list[] = $this->_cleanObjectDatas($salary);
119 }
120 }
121 } else {
122 throw new RestException(503, 'Error when retrieving list of salaries: ' . $this->db->lasterror());
123 }
124
125 return $list;
126 }
127
136 public function get($id)
137 {
138 if (!DolibarrApiAccess::$user->hasRight('salaries', 'read')
139 && !DolibarrApiAccess::$user->hasRight('salaries', 'readchild')
140 && !DolibarrApiAccess::$user->hasRight('salaries', 'readall')) {
141 throw new RestException(403);
142 }
143
144 if (!DolibarrApiAccess::$user->hasRight('salaries', 'readall')) {
145 if (!DolibarrApiAccess::$user->hasRight('salaries', 'readchild')) {
146 if ($id != DolibarrApiAccess::$user->id) {
147 throw new RestException(404, 'salary not found');
148 }
149 } else {
150 $childids = DolibarrApiAccess::$user->getAllChildIds(1);
151 if (!in_array($id, $childids)) {
152 throw new RestException(404, 'salary not found');
153 }
154 }
155 }
156
157 $salary = new Salary($this->db);
158 $result = $salary->fetch($id);
159 if (!$result) {
160 throw new RestException(404, 'salary not found');
161 }
162
163 return $this->_cleanObjectDatas($salary);
164 }
165
174 public function post($request_data = null)
175 {
176 if (!DolibarrApiAccess::$user->hasRight('salaries', 'write')) {
177 throw new RestException(403);
178 }
179 // Check mandatory fields
180 $result = $this->_validate($request_data);
181
182 $salary = new Salary($this->db);
183 foreach ($request_data as $field => $value) {
184 $salary->$field = $this->_checkValForAPI($field, $value, $salary);
185 }
186
187 if ($salary->create(DolibarrApiAccess::$user) < 0) {
188 throw new RestException(500, 'Error creating salary', array_merge(array($salary->error), $salary->errors));
189 }
190 return $salary->id;
191 }
192
202 public function put($id, $request_data = null)
203 {
204 if (!DolibarrApiAccess::$user->hasRight('salaries', 'write')) {
205 throw new RestException(403);
206 }
207
208 $salary = new Salary($this->db);
209 $result = $salary->fetch($id);
210 if (!$result) {
211 throw new RestException(404, 'salary not found');
212 }
213
214 foreach ($request_data as $field => $value) {
215 if ($field == 'id') {
216 continue;
217 }
218 $salary->$field = $this->_checkValForAPI($field, $value, $salary);
219 }
220
221 if ($salary->update(DolibarrApiAccess::$user) > 0) {
222 return $this->get($id);
223 } else {
224 throw new RestException(500, $salary->error);
225 }
226 }
227
234 /*public function delete($id)
235 {
236 if (!DolibarrApiAccess::$user->hasRight('salaries', 'delete')) {
237 throw new RestException(403);
238 }
239 $salary = new Salary($this->db);
240 $result = $salary->fetch($id);
241 if (!$result) {
242 throw new RestException(404, 'salary not found');
243 }
244
245 if ($salary->delete(DolibarrApiAccess::$user) < 0) {
246 throw new RestException(500, 'error when deleting salary');
247 }
248
249 return array(
250 'success' => array(
251 'code' => 200,
252 'message' => 'salary deleted'
253 )
254 );
255 }*/
256
257
273 public function getAllPayments($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0)
274 {
275 $list = array();
276
277 if (!DolibarrApiAccess::$user->hasRight('salaries', 'read')
278 && !DolibarrApiAccess::$user->hasRight('salaries', 'readchild')
279 && !DolibarrApiAccess::$user->hasRight('salaries', 'readall')) {
280 throw new RestException(403);
281 }
282
283 $sql = "SELECT t.rowid FROM " . MAIN_DB_PREFIX . "payment_salary as t, ".MAIN_DB_PREFIX."salary as s";
284 $sql .= ' WHERE s.rowid = t.fk_salary AND t.entity IN ('.getEntity('salary').')';
285 if (!DolibarrApiAccess::$user->hasRight('salaries', 'readall')) {
286 if (!DolibarrApiAccess::$user->hasRight('salaries', 'readchild')) {
287 $sql .= ' AND s.fk_user = '.((int) DolibarrApiAccess::$user->id).')';
288 } else {
289 $childids = DolibarrApiAccess::$user->getAllChildIds(1);
290 $sql .= ' AND s.fk_user IN ('.$this->db->sanitize(implode(',', $childids)).')';
291 }
292 }
293
294 $sql .= $this->db->order($sortfield, $sortorder);
295 if ($limit) {
296 if ($page < 0) {
297 $page = 0;
298 }
299 $offset = $limit * $page;
300
301 $sql .= $this->db->plimit($limit + 1, $offset);
302 }
303
304 dol_syslog("API Rest request");
305
306 $result = $this->db->query($sql);
307
308 if ($result) {
309 $num = $this->db->num_rows($result);
310 $min = min($num, ($limit <= 0 ? $num : $limit));
311 for ($i = 0; $i < $min; $i++) {
312 $obj = $this->db->fetch_object($result);
313 $paymentsalary = new PaymentSalary($this->db);
314 if ($paymentsalary->fetch($obj->rowid) > 0) {
315 $list[] = $this->_cleanObjectDatas($paymentsalary);
316 }
317 }
318 } else {
319 throw new RestException(503, 'Error when retrieving list of paymentsalaries: ' . $this->db->lasterror());
320 }
321
322 return $list;
323 }
324
335 public function getPayments($pid)
336 {
337 // A payment of salary can be done on different salaires of didderent user, so only users with permission
338 // to read all area allowed.
339 // TODO To support read or readchild case, the get must be done with a SQL that include the paid user with
340 // a where on current user and childids of current user.
341 if (!DolibarrApiAccess::$user->hasRight('salaries', 'readall')) {
342 throw new RestException(403);
343 }
344
345 $paymentsalary = new PaymentSalary($this->db);
346 $result = $paymentsalary->fetch($pid);
347 if (!$result) {
348 throw new RestException(404, 'paymentsalary not found');
349 }
350
351 return $this->_cleanObjectDatas($paymentsalary);
352 }
353
367 public function addPayment($id, $request_data = null)
368 {
369 if (!DolibarrApiAccess::$user->hasRight('salaries', 'write')) {
370 throw new RestException(403);
371 }
372 // Check mandatory fields
373 $result = $this->_validatepayments($request_data);
374
375 $paymentsalary = new PaymentSalary($this->db);
376 $paymentsalary->fk_salary = $id;
377 foreach ($request_data as $field => $value) {
378 $paymentsalary->$field = $this->_checkValForAPI($field, $value, $paymentsalary);
379 }
380
381 if ($paymentsalary->create(DolibarrApiAccess::$user, 1) < 0) {
382 throw new RestException(500, 'Error creating paymentsalary', array_merge(array($paymentsalary->error), $paymentsalary->errors));
383 }
384 if (isModEnabled("bank")) {
385 $paymentsalary->addPaymentToBank(
386 DolibarrApiAccess::$user,
387 'payment_salary',
388 '(SalaryPayment)',
389 (int) $request_data['accountid'],
390 '',
391 ''
392 );
393 }
394 return $paymentsalary->id;
395 }
396
410 public function updatePayment($id, $request_data = null)
411 {
412 if (!DolibarrApiAccess::$user->hasRight('salaries', 'write')) {
413 throw new RestException(403);
414 }
415
416 $paymentsalary = new PaymentSalary($this->db);
417 $result = $paymentsalary->fetch($id);
418 if (!$result) {
419 throw new RestException(404, 'Payment salary not found');
420 }
421
422 foreach ($request_data as $field => $value) {
423 if ($field == 'id') {
424 continue;
425 }
426 $paymentsalary->$field = $this->_checkValForAPI($field, $value, $paymentsalary);
427 }
428
429 if ($paymentsalary->update(DolibarrApiAccess::$user) > 0) {
430 return $this->get($id);
431 } else {
432 throw new RestException(500, $paymentsalary->error);
433 }
434 }
435
444 /*public function delete($id)
445 {
446 if (!DolibarrApiAccess::$user->hasRight('salaries', 'delete')) {
447 throw new RestException(403);
448 }
449 $paymentsalary = new PaymentSalary($this->db);
450 $result = $paymentsalary->fetch($id);
451 if (!$result) {
452 throw new RestException(404, 'paymentsalary not found');
453 }
454
455 if ($paymentsalary->delete(DolibarrApiAccess::$user) < 0) {
456 throw new RestException(500, 'error when deleting paymentsalary');
457 }
458
459 return array(
460 'success' => array(
461 'code' => 200,
462 'message' => 'paymentsalary deleted'
463 )
464 );
465 }*/
466
467
476 private function _validate($data)
477 {
478 if ($data === null) {
479 $data = array();
480 }
481 $salary = array();
482 foreach (Salaries::$FIELDS as $field) {
483 if (!isset($data[$field])) {
484 throw new RestException(400, "$field field missing");
485 }
486 $salary[$field] = $data[$field];
487 }
488 return $salary;
489 }
490
499 private function _validatepayments($data)
500 {
501 if ($data === null) {
502 $data = array();
503 }
504 $paymentsalary = array();
505 $fields = Salaries::$FIELDSPAYMENT;
506 if (isModEnabled("bank")) {
507 array_push($fields, "accountid");
508 }
509 foreach ($fields as $field) {
510 if (!isset($data[$field])) {
511 throw new RestException(400, "$field field missing");
512 }
513 $paymentsalary[$field] = $data[$field];
514 }
515 return $paymentsalary;
516 }
517
518 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
528 protected function _cleanObjectDatas($object)
529 {
530 // phpcs:enable
531 $object = parent::_cleanObjectDatas($object);
532
533 unset($object->rowid);
534
535 return $object;
536 }
537}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class for API REST v1.
Definition api.class.php:35
_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 @phpstan-template T.
_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.
Class to manage salary payments.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.