21use Luracast\Restler\RestException;
23require_once DOL_DOCUMENT_ROOT.
'/salaries/class/salary.class.php';
24require_once DOL_DOCUMENT_ROOT.
'/salaries/class/paymentsalary.class.php';
38 public static $FIELDS = array(
47 public static $FIELDSPAYMENT = array(
77 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0)
81 if (!DolibarrApiAccess::$user->hasRight(
'salaries',
'read')
82 && !DolibarrApiAccess::$user->hasRight(
'salaries',
'readchild')
83 && !DolibarrApiAccess::$user->hasRight(
'salaries',
'readall')) {
84 throw new RestException(403);
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).
')';
93 $childids = DolibarrApiAccess::$user->getAllChildIds(1);
94 $sql .=
' AND t.fk_user IN ('.$this->db->sanitize(implode(
',', $childids)).
')';
98 $sql .= $this->db->order($sortfield, $sortorder);
103 $offset = $limit * $page;
105 $sql .= $this->db->plimit($limit + 1, $offset);
109 $result = $this->db->query($sql);
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) {
122 throw new RestException(503,
'Error when retrieving list of salaries: ' . $this->db->lasterror());
138 if (!DolibarrApiAccess::$user->hasRight(
'salaries',
'read')
139 && !DolibarrApiAccess::$user->hasRight(
'salaries',
'readchild')
140 && !DolibarrApiAccess::$user->hasRight(
'salaries',
'readall')) {
141 throw new RestException(403);
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');
150 $childids = DolibarrApiAccess::$user->getAllChildIds(1);
151 if (!in_array(
$id, $childids)) {
152 throw new RestException(404,
'salary not found');
157 $salary =
new Salary($this->db);
158 $result = $salary->fetch(
$id);
160 throw new RestException(404,
'salary not found');
174 public function post($request_data =
null)
176 if (!DolibarrApiAccess::$user->hasRight(
'salaries',
'write')) {
177 throw new RestException(403);
180 $result = $this->
_validate($request_data);
182 $salary =
new Salary($this->db);
183 foreach ($request_data as $field => $value) {
187 if ($salary->create(DolibarrApiAccess::$user) < 0) {
188 throw new RestException(500,
'Error creating salary', array_merge(array($salary->error), $salary->errors));
202 public function put(
$id, $request_data =
null)
204 if (!DolibarrApiAccess::$user->hasRight(
'salaries',
'write')) {
205 throw new RestException(403);
208 $salary =
new Salary($this->db);
209 $result = $salary->fetch(
$id);
211 throw new RestException(404,
'salary not found');
214 foreach ($request_data as $field => $value) {
215 if ($field ==
'id') {
221 if ($salary->update(DolibarrApiAccess::$user) > 0) {
222 return $this->
get(
$id);
224 throw new RestException(500, $salary->error);
273 public function getAllPayments($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0)
277 if (!DolibarrApiAccess::$user->hasRight(
'salaries',
'read')
278 && !DolibarrApiAccess::$user->hasRight(
'salaries',
'readchild')
279 && !DolibarrApiAccess::$user->hasRight(
'salaries',
'readall')) {
280 throw new RestException(403);
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).
')';
289 $childids = DolibarrApiAccess::$user->getAllChildIds(1);
290 $sql .=
' AND s.fk_user IN ('.$this->db->sanitize(implode(
',', $childids)).
')';
294 $sql .= $this->db->order($sortfield, $sortorder);
299 $offset = $limit * $page;
301 $sql .= $this->db->plimit($limit + 1, $offset);
306 $result = $this->db->query($sql);
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);
314 if ($paymentsalary->fetch($obj->rowid) > 0) {
319 throw new RestException(503,
'Error when retrieving list of paymentsalaries: ' . $this->db->lasterror());
341 if (!DolibarrApiAccess::$user->hasRight(
'salaries',
'readall')) {
342 throw new RestException(403);
346 $result = $paymentsalary->fetch($pid);
348 throw new RestException(404,
'paymentsalary not found');
369 if (!DolibarrApiAccess::$user->hasRight(
'salaries',
'write')) {
370 throw new RestException(403);
376 $paymentsalary->fk_salary =
$id;
377 foreach ($request_data as $field => $value) {
378 $paymentsalary->$field = $this->
_checkValForAPI($field, $value, $paymentsalary);
381 if ($paymentsalary->create(DolibarrApiAccess::$user, 1) < 0) {
382 throw new RestException(500,
'Error creating paymentsalary', array_merge(array($paymentsalary->error), $paymentsalary->errors));
385 $paymentsalary->addPaymentToBank(
386 DolibarrApiAccess::$user,
389 (
int) $request_data[
'accountid'],
394 return $paymentsalary->id;
412 if (!DolibarrApiAccess::$user->hasRight(
'salaries',
'write')) {
413 throw new RestException(403);
417 $result = $paymentsalary->fetch(
$id);
419 throw new RestException(404,
'Payment salary not found');
422 foreach ($request_data as $field => $value) {
423 if ($field ==
'id') {
426 $paymentsalary->$field = $this->
_checkValForAPI($field, $value, $paymentsalary);
429 if ($paymentsalary->update(DolibarrApiAccess::$user) > 0) {
430 return $this->
get(
$id);
432 throw new RestException(500, $paymentsalary->error);
478 if ($data ===
null) {
482 foreach (Salaries::$FIELDS as $field) {
483 if (!isset($data[$field])) {
484 throw new RestException(400,
"$field field missing");
486 $salary[$field] = $data[$field];
501 if ($data ===
null) {
504 $paymentsalary = array();
505 $fields = Salaries::$FIELDSPAYMENT;
507 array_push($fields,
"accountid");
509 foreach ($fields as $field) {
510 if (!isset($data[$field])) {
511 throw new RestException(400,
"$field field missing");
513 $paymentsalary[$field] = $data[$field];
515 return $paymentsalary;
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $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 @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.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.