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 $paymentsalary = new PaymentSalary($this->db);
336 $paymentsalary->fk_salary = $id;
337 foreach ($request_data as $field => $value) {
338 $paymentsalary->$field = $this->_checkValForAPI($field, $value, $paymentsalary);
339 }
340
341 if ($paymentsalary->create(DolibarrApiAccess::$user, 1) < 0) {
342 throw new RestException(500, 'Error creating paymentsalary', array_merge(array($paymentsalary->error), $paymentsalary->errors));
343 }
344 if (isModEnabled("bank")) {
345 $paymentsalary->addPaymentToBank(
346 DolibarrApiAccess::$user,
347 'payment_salary',
348 '(SalaryPayment)',
349 (int) $request_data['accountid'],
350 '',
351 ''
352 );
353 }
354 return $paymentsalary->id;
355 }
356
370 public function updatePayment($id, $request_data = null)
371 {
372 if (!DolibarrApiAccess::$user->hasRight('salaries', 'write')) {
373 throw new RestException(403);
374 }
375
376 $paymentsalary = new PaymentSalary($this->db);
377 $result = $paymentsalary->fetch($id);
378 if (!$result) {
379 throw new RestException(404, 'Payment salary not found');
380 }
381
382 foreach ($request_data as $field => $value) {
383 if ($field == 'id') {
384 continue;
385 }
386 $paymentsalary->$field = $this->_checkValForAPI($field, $value, $paymentsalary);
387 }
388
389 if ($paymentsalary->update(DolibarrApiAccess::$user) > 0) {
390 return $this->get($id);
391 } else {
392 throw new RestException(500, $paymentsalary->error);
393 }
394 }
395
404 /*public function delete($id)
405 {
406 if (!DolibarrApiAccess::$user->hasRight('salaries', 'delete')) {
407 throw new RestException(403);
408 }
409 $paymentsalary = new PaymentSalary($this->db);
410 $result = $paymentsalary->fetch($id);
411 if (!$result) {
412 throw new RestException(404, 'paymentsalary not found');
413 }
414
415 if ($paymentsalary->delete(DolibarrApiAccess::$user) < 0) {
416 throw new RestException(500, 'error when deleting paymentsalary');
417 }
418
419 return array(
420 'success' => array(
421 'code' => 200,
422 'message' => 'paymentsalary deleted'
423 )
424 );
425 }*/
426
427
436 private function _validate($data)
437 {
438 if ($data === null) {
439 $data = array();
440 }
441 $salary = array();
442 foreach (Salaries::$FIELDS as $field) {
443 if (!isset($data[$field])) {
444 throw new RestException(400, "$field field missing");
445 }
446 $salary[$field] = $data[$field];
447 }
448 return $salary;
449 }
450
459 private function _validatepayments($data)
460 {
461 if ($data === null) {
462 $data = array();
463 }
464 $paymentsalary = array();
465 $fields = Salaries::$FIELDSPAYMENT;
466 if (isModEnabled("bank")) {
467 array_push($fields, "accountid");
468 }
469 foreach ($fields as $field) {
470 if (!isset($data[$field])) {
471 throw new RestException(400, "$field field missing");
472 }
473 $paymentsalary[$field] = $data[$field];
474 }
475 return $paymentsalary;
476 }
477
478 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
485 protected function _cleanObjectDatas($object)
486 {
487 // phpcs:enable
488 $object = parent::_cleanObjectDatas($object);
489
490 unset($object->rowid);
491
492 return $object;
493 }
494}
$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.