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(
78 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'')
82 if (!DolibarrApiAccess::$user->hasRight(
'salaries',
'read')
83 && !DolibarrApiAccess::$user->hasRight(
'salaries',
'readchild')
84 && !DolibarrApiAccess::$user->hasRight(
'salaries',
'readall')) {
85 throw new RestException(403);
88 $sql =
"SELECT rowid FROM " . MAIN_DB_PREFIX .
"salary as t";
89 $sql .=
' WHERE t.entity IN ('.getEntity(
'user').
')';
90 if (!DolibarrApiAccess::$user->hasRight(
'salaries',
'readall')) {
91 if (!DolibarrApiAccess::$user->hasRight(
'salaries',
'readchild')) {
92 $sql .=
' AND t.fk_user = '.((int) DolibarrApiAccess::$user->
id);
94 $childids = DolibarrApiAccess::$user->getAllChildIds(1);
95 $sql .=
' AND t.fk_user IN ('.$this->db->sanitize(implode(
',', $childids)).
')';
104 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
108 $sql .= $this->db->order($sortfield, $sortorder);
113 $offset = $limit * $page;
115 $sql .= $this->db->plimit($limit + 1, $offset);
119 $result = $this->db->query($sql);
122 $num = $this->db->num_rows($result);
123 $min = min($num, ($limit <= 0 ? $num : $limit));
124 for ($i = 0; $i < $min; $i++) {
125 $obj = $this->db->fetch_object($result);
126 $salary =
new Salary($this->db);
127 if ($salary->fetch($obj->rowid) > 0) {
132 throw new RestException(503,
'Error when retrieving list of salaries: ' . $this->db->lasterror());
148 if (!DolibarrApiAccess::$user->hasRight(
'salaries',
'read')
149 && !DolibarrApiAccess::$user->hasRight(
'salaries',
'readchild')
150 && !DolibarrApiAccess::$user->hasRight(
'salaries',
'readall')) {
151 throw new RestException(403);
154 if (!DolibarrApiAccess::$user->hasRight(
'salaries',
'readall')) {
155 if (!DolibarrApiAccess::$user->hasRight(
'salaries',
'readchild')) {
156 if (
$id != DolibarrApiAccess::$user->
id) {
157 throw new RestException(404,
'salary not found');
160 $childids = DolibarrApiAccess::$user->getAllChildIds(1);
161 if (!in_array(
$id, $childids)) {
162 throw new RestException(404,
'salary not found');
167 $salary =
new Salary($this->db);
168 $result = $salary->fetch(
$id);
170 throw new RestException(404,
'salary not found');
184 public function post($request_data =
null)
186 if (!DolibarrApiAccess::$user->hasRight(
'salaries',
'write')) {
187 throw new RestException(403);
190 $result = $this->
_validate($request_data);
192 $salary =
new Salary($this->db);
193 foreach ($request_data as $field => $value) {
197 if ($salary->create(DolibarrApiAccess::$user) < 0) {
198 throw new RestException(500,
'Error creating salary', array_merge(array($salary->error), $salary->errors));
212 public function put(
$id, $request_data =
null)
214 if (!DolibarrApiAccess::$user->hasRight(
'salaries',
'write')) {
215 throw new RestException(403);
218 $salary =
new Salary($this->db);
219 $result = $salary->fetch(
$id);
221 throw new RestException(404,
'salary not found');
224 foreach ($request_data as $field => $value) {
225 if ($field ==
'id') {
231 if ($salary->update(DolibarrApiAccess::$user) > 0) {
232 return $this->
get(
$id);
234 throw new RestException(500, $salary->error);
283 public function getAllPayments($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0)
287 if (!DolibarrApiAccess::$user->hasRight(
'salaries',
'read')
288 && !DolibarrApiAccess::$user->hasRight(
'salaries',
'readchild')
289 && !DolibarrApiAccess::$user->hasRight(
'salaries',
'readall')) {
290 throw new RestException(403);
293 $sql =
"SELECT t.rowid FROM " . MAIN_DB_PREFIX .
"payment_salary as t, ".MAIN_DB_PREFIX.
"salary as s";
294 $sql .=
' WHERE s.rowid = t.fk_salary AND t.entity IN ('.getEntity(
'salary').
')';
295 if (!DolibarrApiAccess::$user->hasRight(
'salaries',
'readall')) {
296 if (!DolibarrApiAccess::$user->hasRight(
'salaries',
'readchild')) {
297 $sql .=
' AND s.fk_user = '.((int) DolibarrApiAccess::$user->
id).
')';
299 $childids = DolibarrApiAccess::$user->getAllChildIds(1);
300 $sql .=
' AND s.fk_user IN ('.$this->db->sanitize(implode(
',', $childids)).
')';
304 $sql .= $this->db->order($sortfield, $sortorder);
309 $offset = $limit * $page;
311 $sql .= $this->db->plimit($limit + 1, $offset);
316 $result = $this->db->query($sql);
319 $num = $this->db->num_rows($result);
320 $min = min($num, ($limit <= 0 ? $num : $limit));
321 for ($i = 0; $i < $min; $i++) {
322 $obj = $this->db->fetch_object($result);
324 if ($paymentsalary->fetch($obj->rowid) > 0) {
329 throw new RestException(503,
'Error when retrieving list of paymentsalaries: ' . $this->db->lasterror());
351 if (!DolibarrApiAccess::$user->hasRight(
'salaries',
'readall')) {
352 throw new RestException(403);
356 $result = $paymentsalary->fetch($pid);
358 throw new RestException(404,
'paymentsalary not found');
379 if (!DolibarrApiAccess::$user->hasRight(
'salaries',
'write')) {
380 throw new RestException(403);
386 $paymentsalary->fk_salary =
$id;
387 foreach ($request_data as $field => $value) {
388 $paymentsalary->$field = $this->
_checkValForAPI($field, $value, $paymentsalary);
391 if ($paymentsalary->create(DolibarrApiAccess::$user, 1) < 0) {
392 throw new RestException(500,
'Error creating paymentsalary', array_merge(array($paymentsalary->error), $paymentsalary->errors));
395 $paymentsalary->addPaymentToBank(
396 DolibarrApiAccess::$user,
399 (
int) $request_data[
'accountid'],
404 return $paymentsalary->id;
422 if (!DolibarrApiAccess::$user->hasRight(
'salaries',
'write')) {
423 throw new RestException(403);
427 $result = $paymentsalary->fetch(
$id);
429 throw new RestException(404,
'Payment salary not found');
432 foreach ($request_data as $field => $value) {
433 if ($field ==
'id') {
436 $paymentsalary->$field = $this->
_checkValForAPI($field, $value, $paymentsalary);
439 if ($paymentsalary->update(DolibarrApiAccess::$user) > 0) {
440 return $this->
get(
$id);
442 throw new RestException(500, $paymentsalary->error);
488 if ($data ===
null) {
492 foreach (Salaries::$FIELDS as $field) {
493 if (!isset($data[$field])) {
494 throw new RestException(400,
"$field field missing");
496 $salary[$field] = $data[$field];
511 if ($data ===
null) {
514 $paymentsalary = array();
515 $fields = Salaries::$FIELDSPAYMENT;
517 array_push($fields,
"accountid");
519 foreach ($fields as $field) {
520 if (!isset($data[$field])) {
521 throw new RestException(400,
"$field field missing");
523 $paymentsalary[$field] = $data[$field];
525 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.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='')
Get the list of salaries.
put($id, $request_data=null)
Update salary.
__construct()
Constructor.
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.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.