dolibarr  21.0.0-alpha
api_salaries.class.php
1 <?php
2 /*
3  * Copyright (C) 2023 Marc Chenebaux <marc.chenebaux@maj44.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
19 use Luracast\Restler\RestException;
20 
21 require_once DOL_DOCUMENT_ROOT.'/salaries/class/salary.class.php';
22 require_once DOL_DOCUMENT_ROOT.'/salaries/class/paymentsalary.class.php';
23 
24 
31 class Salaries extends DolibarrApi
32 {
36  static $FIELDS = array(
37  'fk_user',
38  'label',
39  'amount',
40  );
41 
45  static $FIELDSPAYMENT = array(
46  "paiementtype",
47  'datepaye',
48  'chid',
49  'amounts',
50  );
51 
52 
56  public function __construct()
57  {
58  global $db;
59  $this->db = $db;
60  }
61 
73  public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0)
74  {
75  $list = array();
76 
77  if (!DolibarrApiAccess::$user->hasRight('salaries', 'read')) {
78  throw new RestException(403);
79  }
80 
81  $sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . "salary as t";
82  //$sql .= ' WHERE t.entity IN ('.getEntity('bank_account').')';
83 
84  $sql .= $this->db->order($sortfield, $sortorder);
85  if ($limit) {
86  if ($page < 0) {
87  $page = 0;
88  }
89  $offset = $limit * $page;
90 
91  $sql .= $this->db->plimit($limit + 1, $offset);
92  }
93 
94  dol_syslog("API Rest request");
95  $result = $this->db->query($sql);
96 
97  if ($result) {
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) {
104  $list[] = $this->_cleanObjectDatas($salary);
105  }
106  }
107  } else {
108  throw new RestException(503, 'Error when retrieving list of salaries: ' . $this->db->lasterror());
109  }
110 
111  return $list;
112  }
113 
122  public function get($id)
123  {
124  if (!DolibarrApiAccess::$user->hasRight('salaries', 'read')) {
125  throw new RestException(403);
126  }
127 
128  $salary = new Salary($this->db);
129  $result = $salary->fetch($id);
130  if (!$result) {
131  throw new RestException(404, 'salary not found');
132  }
133 
134  return $this->_cleanObjectDatas($salary);
135  }
136 
143  public function post($request_data = null)
144  {
145  if (!DolibarrApiAccess::$user->hasRight('salaries', 'write')) {
146  throw new RestException(403);
147  }
148  // Check mandatory fields
149  $result = $this->_validate($request_data);
150 
151  $salary = new Salary($this->db);
152  foreach ($request_data as $field => $value) {
153  $salary->$field = $this->_checkValForAPI($field, $value, $salary);
154  }
155 
156  if ($salary->create(DolibarrApiAccess::$user) < 0) {
157  throw new RestException(500, 'Error creating salary', array_merge(array($salary->error), $salary->errors));
158  }
159  return $salary->id;
160  }
161 
169  public function put($id, $request_data = null)
170  {
171  if (!DolibarrApiAccess::$user->hasRight('salaries', 'write')) {
172  throw new RestException(403);
173  }
174 
175  $salary = new Salary($this->db);
176  $result = $salary->fetch($id);
177  if (!$result) {
178  throw new RestException(404, 'salary not found');
179  }
180 
181  foreach ($request_data as $field => $value) {
182  if ($field == 'id') {
183  continue;
184  }
185  $salary->$field = $this->_checkValForAPI($field, $value, $salary);
186  }
187 
188  if ($salary->update(DolibarrApiAccess::$user) > 0) {
189  return $this->get($id);
190  } else {
191  throw new RestException(500, $salary->error);
192  }
193  }
194 
201  /*public function delete($id)
202  {
203  if (!DolibarrApiAccess::$user->hasRight('salaries', 'delete')) {
204  throw new RestException(403);
205  }
206  $salary = new Salary($this->db);
207  $result = $salary->fetch($id);
208  if (!$result) {
209  throw new RestException(404, 'salary not found');
210  }
211 
212  if ($salary->delete(DolibarrApiAccess::$user) < 0) {
213  throw new RestException(500, 'error when deleting salary');
214  }
215 
216  return array(
217  'success' => array(
218  'code' => 200,
219  'message' => 'salary deleted'
220  )
221  );
222  }*/
223 
224 
238  public function getAllPayments($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0)
239  {
240  $list = array();
241 
242  if (!DolibarrApiAccess::$user->hasRight('salaries', 'read')) {
243  throw new RestException(403);
244  }
245 
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').')';
248 
249  $sql .= $this->db->order($sortfield, $sortorder);
250  if ($limit) {
251  if ($page < 0) {
252  $page = 0;
253  }
254  $offset = $limit * $page;
255 
256  $sql .= $this->db->plimit($limit + 1, $offset);
257  }
258 
259  dol_syslog("API Rest request");
260 
261  $result = $this->db->query($sql);
262 
263  if ($result) {
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);
268  $paymentsalary = new PaymentSalary($this->db);
269  if ($paymentsalary->fetch($obj->rowid) > 0) {
270  $list[] = $this->_cleanObjectDatas($paymentsalary);
271  }
272  }
273  } else {
274  throw new RestException(503, 'Error when retrieving list of paymentsalaries: ' . $this->db->lasterror());
275  }
276 
277  return $list;
278  }
279 
290  public function getPayments($pid)
291  {
292  if (!DolibarrApiAccess::$user->hasRight('salaries', 'read')) {
293  throw new RestException(403);
294  }
295 
296  $paymentsalary = new PaymentSalary($this->db);
297  $result = $paymentsalary->fetch($pid);
298  if (!$result) {
299  throw new RestException(404, 'paymentsalary not found');
300  }
301 
302  return $this->_cleanObjectDatas($paymentsalary);
303  }
304 
316  public function addPayment($id, $request_data = null)
317  {
318  if (!DolibarrApiAccess::$user->hasRight('salaries', 'write')) {
319  throw new RestException(403);
320  }
321  // Check mandatory fields
322  $result = $this->_validatepayments($request_data);
323 
324  $paymentsalary = new PaymentSalary($this->db);
325  $paymentsalary->fk_salary = $id;
326  foreach ($request_data as $field => $value) {
327  $paymentsalary->$field = $this->_checkValForAPI($field, $value, $paymentsalary);
328  }
329 
330  if ($paymentsalary->create(DolibarrApiAccess::$user, 1) < 0) {
331  throw new RestException(500, 'Error creating paymentsalary', array_merge(array($paymentsalary->error), $paymentsalary->errors));
332  }
333  if (isModEnabled("bank")) {
334  $paymentsalary->addPaymentToBank(
335  DolibarrApiAccess::$user,
336  'payment_salary',
337  '(SalaryPayment)',
338  (int) $request_data['accountid'],
339  '',
340  ''
341  );
342  }
343  return $paymentsalary->id;
344  }
345 
357  public function updatePayment($id, $request_data = null)
358  {
359  if (!DolibarrApiAccess::$user->hasRight('salaries', 'write')) {
360  throw new RestException(403);
361  }
362 
363  $paymentsalary = new PaymentSalary($this->db);
364  $result = $paymentsalary->fetch($id);
365  if (!$result) {
366  throw new RestException(404, 'Payment salary not found');
367  }
368 
369  foreach ($request_data as $field => $value) {
370  if ($field == 'id') {
371  continue;
372  }
373  $paymentsalary->$field = $this->_checkValForAPI($field, $value, $paymentsalary);
374  }
375 
376  if ($paymentsalary->update(DolibarrApiAccess::$user) > 0) {
377  return $this->get($id);
378  } else {
379  throw new RestException(500, $paymentsalary->error);
380  }
381  }
382 
391  /*public function delete($id)
392  {
393  if (!DolibarrApiAccess::$user->hasRight('salaries', 'delete')) {
394  throw new RestException(403);
395  }
396  $paymentsalary = new PaymentSalary($this->db);
397  $result = $paymentsalary->fetch($id);
398  if (!$result) {
399  throw new RestException(404, 'paymentsalary not found');
400  }
401 
402  if ($paymentsalary->delete(DolibarrApiAccess::$user) < 0) {
403  throw new RestException(500, 'error when deleting paymentsalary');
404  }
405 
406  return array(
407  'success' => array(
408  'code' => 200,
409  'message' => 'paymentsalary deleted'
410  )
411  );
412  }*/
413 
414 
423  private function _validate($data)
424  {
425  $salary = array();
426  foreach (Salaries::$FIELDS as $field) {
427  if (!isset($data[$field])) {
428  throw new RestException(400, "$field field missing");
429  }
430  $salary[$field] = $data[$field];
431  }
432  return $salary;
433  }
434 
443  private function _validatepayments($data)
444  {
445  $paymentsalary = array();
446  $fields = Salaries::$FIELDSPAYMENT;
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");
451  }
452  $paymentsalary[$field] = $data[$field];
453  }
454  return $paymentsalary;
455  }
456 
457  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
464  protected function _cleanObjectDatas($object)
465  {
466  // phpcs:enable
467  $object = parent::_cleanObjectDatas($object);
468 
469  unset($object->rowid);
470 
471  return $object;
472  }
473 }
if($user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition: card.php:58
Class for API REST v1.
Definition: api.class.php:30
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Definition: api.class.php:82
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.
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:751
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.