dolibarr 22.0.5
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 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20use Luracast\Restler\RestException;
21
22require_once DOL_DOCUMENT_ROOT.'/salaries/class/salary.class.php';
23require_once DOL_DOCUMENT_ROOT.'/salaries/class/paymentsalary.class.php';
24
25
32class Salaries extends DolibarrApi
33{
37 public static $FIELDS = array(
38 'fk_user',
39 'label',
40 'amount',
41 );
42
46 public static $FIELDSPAYMENT = array(
47 "paiementtype",
48 'datepaye',
49 'chid',
50 'amounts',
51 );
52
53
57 public function __construct()
58 {
59 global $db;
60 $this->db = $db;
61 }
62
76 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0)
77 {
78 $list = array();
79
80 if (!DolibarrApiAccess::$user->hasRight('salaries', 'read')) {
81 throw new RestException(403);
82 }
83
84 $sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . "salary as t";
85 //$sql .= ' WHERE t.entity IN ('.getEntity('bank_account').')';
86
87 $sql .= $this->db->order($sortfield, $sortorder);
88 if ($limit) {
89 if ($page < 0) {
90 $page = 0;
91 }
92 $offset = $limit * $page;
93
94 $sql .= $this->db->plimit($limit + 1, $offset);
95 }
96
97 dol_syslog("API Rest request");
98 $result = $this->db->query($sql);
99
100 if ($result) {
101 $num = $this->db->num_rows($result);
102 $min = min($num, ($limit <= 0 ? $num : $limit));
103 for ($i = 0; $i < $min; $i++) {
104 $obj = $this->db->fetch_object($result);
105 $salary = new Salary($this->db);
106 if ($salary->fetch($obj->rowid) > 0) {
107 $list[] = $this->_cleanObjectDatas($salary);
108 }
109 }
110 } else {
111 throw new RestException(503, 'Error when retrieving list of salaries: ' . $this->db->lasterror());
112 }
113
114 return $list;
115 }
116
125 public function get($id)
126 {
127 if (!DolibarrApiAccess::$user->hasRight('salaries', 'read')) {
128 throw new RestException(403);
129 }
130
131 $salary = new Salary($this->db);
132 $result = $salary->fetch($id);
133 if (!$result) {
134 throw new RestException(404, 'salary not found');
135 }
136
137 return $this->_cleanObjectDatas($salary);
138 }
139
148 public function post($request_data = null)
149 {
150 if (!DolibarrApiAccess::$user->hasRight('salaries', 'write')) {
151 throw new RestException(403);
152 }
153 // Check mandatory fields
154 $result = $this->_validate($request_data);
155
156 $salary = new Salary($this->db);
157 foreach ($request_data as $field => $value) {
158 $salary->$field = $this->_checkValForAPI($field, $value, $salary);
159 }
160
161 if ($salary->create(DolibarrApiAccess::$user) < 0) {
162 throw new RestException(500, 'Error creating salary', array_merge(array($salary->error), $salary->errors));
163 }
164 return $salary->id;
165 }
166
176 public function put($id, $request_data = null)
177 {
178 if (!DolibarrApiAccess::$user->hasRight('salaries', 'write')) {
179 throw new RestException(403);
180 }
181
182 $salary = new Salary($this->db);
183 $result = $salary->fetch($id);
184 if (!$result) {
185 throw new RestException(404, 'salary not found');
186 }
187
188 foreach ($request_data as $field => $value) {
189 if ($field == 'id') {
190 continue;
191 }
192 $salary->$field = $this->_checkValForAPI($field, $value, $salary);
193 }
194
195 if ($salary->update(DolibarrApiAccess::$user) > 0) {
196 return $this->get($id);
197 } else {
198 throw new RestException(500, $salary->error);
199 }
200 }
201
208 /*public function delete($id)
209 {
210 if (!DolibarrApiAccess::$user->hasRight('salaries', 'delete')) {
211 throw new RestException(403);
212 }
213 $salary = new Salary($this->db);
214 $result = $salary->fetch($id);
215 if (!$result) {
216 throw new RestException(404, 'salary not found');
217 }
218
219 if ($salary->delete(DolibarrApiAccess::$user) < 0) {
220 throw new RestException(500, 'error when deleting salary');
221 }
222
223 return array(
224 'success' => array(
225 'code' => 200,
226 'message' => 'salary deleted'
227 )
228 );
229 }*/
230
231
247 public function getAllPayments($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0)
248 {
249 $list = array();
250
251 if (!DolibarrApiAccess::$user->hasRight('salaries', 'read')) {
252 throw new RestException(403);
253 }
254
255 $sql = "SELECT t.rowid FROM " . MAIN_DB_PREFIX . "payment_salary as t, ".MAIN_DB_PREFIX."salary as s";
256 $sql .= ' WHERE s.rowid = t.fk_salary AND t.entity IN ('.getEntity('salary').')';
257
258 $sql .= $this->db->order($sortfield, $sortorder);
259 if ($limit) {
260 if ($page < 0) {
261 $page = 0;
262 }
263 $offset = $limit * $page;
264
265 $sql .= $this->db->plimit($limit + 1, $offset);
266 }
267
268 dol_syslog("API Rest request");
269
270 $result = $this->db->query($sql);
271
272 if ($result) {
273 $num = $this->db->num_rows($result);
274 $min = min($num, ($limit <= 0 ? $num : $limit));
275 for ($i = 0; $i < $min; $i++) {
276 $obj = $this->db->fetch_object($result);
277 $paymentsalary = new PaymentSalary($this->db);
278 if ($paymentsalary->fetch($obj->rowid) > 0) {
279 $list[] = $this->_cleanObjectDatas($paymentsalary);
280 }
281 }
282 } else {
283 throw new RestException(503, 'Error when retrieving list of paymentsalaries: ' . $this->db->lasterror());
284 }
285
286 return $list;
287 }
288
299 public function getPayments($pid)
300 {
301 if (!DolibarrApiAccess::$user->hasRight('salaries', 'read')) {
302 throw new RestException(403);
303 }
304
305 $paymentsalary = new PaymentSalary($this->db);
306 $result = $paymentsalary->fetch($pid);
307 if (!$result) {
308 throw new RestException(404, 'paymentsalary not found');
309 }
310
311 return $this->_cleanObjectDatas($paymentsalary);
312 }
313
327 public function addPayment($id, $request_data = null)
328 {
329 if (!DolibarrApiAccess::$user->hasRight('salaries', 'write')) {
330 throw new RestException(403);
331 }
332 // Check mandatory fields
333 $result = $this->_validatepayments($request_data);
334
335 $salary = new Salary($this->db);
336 if ($salary->fetch($id) <= 0 || empty($salary->id)) {
337 throw new RestException(404, 'Salary not found');
338 }
339 // create() pays the salaries that are the keys of amounts, not {id}
340 if (!is_array($request_data['amounts']) || array_diff(array_keys($request_data['amounts']), array($salary->id))) {
341 throw new RestException(400, 'amounts must be keyed by the salary id '.$salary->id);
342 }
343
344 $paymentsalary = new PaymentSalary($this->db);
345 $paymentsalary->fk_salary = $id;
346 foreach ($request_data as $field => $value) {
347 $paymentsalary->$field = $this->_checkValForAPI($field, $value, $paymentsalary);
348 }
349 // create() and addPaymentToBank() read fk_typepayment, the mandatory field is paiementtype
350 if (empty($paymentsalary->fk_typepayment)) {
351 $paymentsalary->fk_typepayment = (int) $request_data['paiementtype'];
352 }
353
354 // Payment and bank line are written together, or not at all
355 $this->db->begin();
356
357 if ($paymentsalary->create(DolibarrApiAccess::$user, 1) < 0) {
358 $this->db->rollback();
359 throw new RestException(400, 'Payment error : '.$paymentsalary->errorsToString());
360 }
361 if (isModEnabled("bank")) {
362 $result = $paymentsalary->addPaymentToBank(
363 DolibarrApiAccess::$user,
364 'payment_salary',
365 '(SalaryPayment)',
366 (int) $request_data['accountid'],
367 '',
368 ''
369 );
370 if ($result <= 0) {
371 $this->db->rollback();
372 throw new RestException(400, 'Add payment to bank error : '.$paymentsalary->errorsToString());
373 }
374 }
375
376 $this->db->commit();
377
378 return $paymentsalary->id;
379 }
380
394 public function updatePayment($id, $request_data = null)
395 {
396 if (!DolibarrApiAccess::$user->hasRight('salaries', 'write')) {
397 throw new RestException(403);
398 }
399
400 $paymentsalary = new PaymentSalary($this->db);
401 $result = $paymentsalary->fetch($id);
402 if (!$result) {
403 throw new RestException(404, 'Payment salary not found');
404 }
405
406 foreach ($request_data as $field => $value) {
407 if ($field == 'id') {
408 continue;
409 }
410 $paymentsalary->$field = $this->_checkValForAPI($field, $value, $paymentsalary);
411 }
412
413 if ($paymentsalary->update(DolibarrApiAccess::$user) > 0) {
414 return $this->get($id);
415 } else {
416 throw new RestException(500, $paymentsalary->error);
417 }
418 }
419
428 /*public function delete($id)
429 {
430 if (!DolibarrApiAccess::$user->hasRight('salaries', 'delete')) {
431 throw new RestException(403);
432 }
433 $paymentsalary = new PaymentSalary($this->db);
434 $result = $paymentsalary->fetch($id);
435 if (!$result) {
436 throw new RestException(404, 'paymentsalary not found');
437 }
438
439 if ($paymentsalary->delete(DolibarrApiAccess::$user) < 0) {
440 throw new RestException(500, 'error when deleting paymentsalary');
441 }
442
443 return array(
444 'success' => array(
445 'code' => 200,
446 'message' => 'paymentsalary deleted'
447 )
448 );
449 }*/
450
451
460 private function _validate($data)
461 {
462 if ($data === null) {
463 $data = array();
464 }
465 $salary = array();
466 foreach (Salaries::$FIELDS as $field) {
467 if (!isset($data[$field])) {
468 throw new RestException(400, "$field field missing");
469 }
470 $salary[$field] = $data[$field];
471 }
472 return $salary;
473 }
474
483 private function _validatepayments($data)
484 {
485 if ($data === null) {
486 $data = array();
487 }
488 $paymentsalary = array();
489 $fields = Salaries::$FIELDSPAYMENT;
490 if (isModEnabled("bank")) {
491 array_push($fields, "accountid");
492 }
493 foreach ($fields as $field) {
494 if (!isset($data[$field])) {
495 throw new RestException(400, "$field field missing");
496 }
497 $paymentsalary[$field] = $data[$field];
498 }
499 return $paymentsalary;
500 }
501
502 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
509 protected function _cleanObjectDatas($object)
510 {
511 // phpcs:enable
512 $object = parent::_cleanObjectDatas($object);
513
514 unset($object->rowid);
515
516 return $object;
517 }
518}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:48
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:67
Class for API REST v1.
Definition api.class.php:33
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Definition api.class.php:98
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.
Class to manage salary payments.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.