22use Luracast\Restler\RestException;
24require_once DOL_DOCUMENT_ROOT.
'/expensereport/class/expensereport.class.php';
25require_once DOL_DOCUMENT_ROOT.
'/expensereport/class/paymentexpensereport.class.php';
41 public static $FIELDS = array(
50 public static $FIELDSPAYMENT = array(
59 public $expensereport;
64 public static $FIELDSNOTUPDATABLE = array(
65 'fk_statut',
'status',
'statut',
'paid',
66 'fk_user_valid',
'fk_user_approve',
'fk_user_refuse',
'fk_user_cancel',
'fk_user_creat',
'fk_user_modif',
67 'date_valid',
'date_approve',
'date_refuse',
'date_cancel',
'detail_refuse',
'detail_cancel',
68 'total_ht',
'total_tva',
'total_ttc',
'total_localtax1',
'total_localtax2',
96 public function get(
$id)
98 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
99 throw new RestException(403);
102 $result = $this->expensereport->fetch(
$id);
104 throw new RestException(404,
'Expense report not found');
108 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
111 $this->expensereport->fetchObjectLinked();
136 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $user_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
138 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
139 throw new RestException(403);
147 $sql =
"SELECT t.rowid";
148 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expensereport AS t LEFT JOIN ".MAIN_DB_PREFIX.
"expensereport_extrafields AS ef ON (ef.fk_object = t.rowid)";
149 $sql .=
' WHERE t.entity IN ('.getEntity(
'expensereport').
')';
151 $sql .=
" AND t.fk_user_author IN (".$this->db->sanitize($user_ids).
")";
159 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
164 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
166 $sql .= $this->db->order($sortfield, $sortorder);
171 $offset = $limit * $page;
173 $sql .= $this->db->plimit($limit + 1, $offset);
176 $result = $this->db->query($sql);
179 $num = $this->db->num_rows($result);
180 $min = min($num, ($limit <= 0 ? $num : $limit));
183 $obj = $this->db->fetch_object($result);
185 if ($expensereport_static->fetch($obj->rowid)) {
191 throw new RestException(503,
'Error when retrieve Expense Report list : '.$this->db->lasterror());
195 if ($pagination_data) {
196 $totalsResult = $this->db->query($sqlTotals);
197 $total = $this->db->fetch_object($totalsResult)->total;
202 $obj_ret[
'data'] = $tmp;
203 $obj_ret[
'pagination'] = [
204 'total' => (int) $total,
206 'page_count' => ceil((
int) $total / $limit),
226 public function post($request_data =
null)
228 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
229 throw new RestException(403,
"Insuffisant rights");
233 $result = $this->
_validate($request_data);
236 $fk_user_author = (int) ($request_data[
'fk_user_author'] ?? 0);
237 if ($fk_user_author > 0 && $fk_user_author != DolibarrApiAccess::$user->
id
238 && (!
getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS') || !DolibarrApiAccess::$user->hasRight(
'expensereport',
'writeall_advance'))
239 && !in_array($fk_user_author, DolibarrApiAccess::$user->getAllChildIds(1))) {
240 throw new RestException(403,
'User '.$fk_user_author.
' is not in the hierarchy of login '.DolibarrApiAccess::$user->login);
243 foreach (ExpenseReports::$FIELDSNOTUPDATABLE as $field) {
244 unset($request_data[$field]);
247 foreach ($request_data as $field => $value) {
248 if ($field ===
'caller') {
250 $this->expensereport->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
254 $this->expensereport->$field = $this->
_checkValForAPI($field, $value, $this->expensereport);
263 if ($this->expensereport->create(DolibarrApiAccess::$user) < 0) {
264 throw new RestException(500,
"Error creating expensereport", array_merge(array($this->expensereport->error), $this->expensereport->errors));
267 return $this->expensereport->id;
492 public function put(
$id, $request_data =
null)
494 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
495 throw new RestException(403);
498 $result = $this->expensereport->fetch(
$id);
500 throw new RestException(404,
'expensereport not found');
504 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
508 throw new RestException(403,
'Only a draft or refused expense report can be modified');
511 foreach (ExpenseReports::$FIELDSNOTUPDATABLE as $field) {
512 unset($request_data[$field]);
514 foreach ($request_data as $field => $value) {
515 if ($field ==
'id') {
518 if ($field ===
'caller') {
520 $this->expensereport->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
524 if ($field ==
'array_options' && is_array($value)) {
525 foreach ($value as $index => $val) {
526 $this->expensereport->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->expensereport);
531 if (!in_array($field, array(
'fk_statut',
'fk_user_approve'))) {
532 $this->expensereport->$field = $this->
_checkValForAPI($field, $value, $this->expensereport);
536 if ($this->expensereport->update(DolibarrApiAccess::$user) > 0) {
537 return $this->
get(
$id);
539 throw new RestException(500, $this->expensereport->error);
555 public function delete(
$id)
557 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'supprimer')) {
558 throw new RestException(403);
561 $result = $this->expensereport->fetch(
$id);
563 throw new RestException(404,
'Expense Report not found');
567 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
570 if ($this->expensereport->delete(DolibarrApiAccess::$user) <= 0) {
571 throw new RestException(500,
'Error when delete Expense Report : '.$this->expensereport->error);
577 'message' =>
'Expense Report deleted'
603 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'creer')) {
604 throw new RestException(403,
"Insuffisant rights");
606 $result = $this->expensereport->fetch(
$id);
608 throw new RestException(404,
'Expense report not found');
612 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
615 $result = $this->expensereport->setValidate(DolibarrApiAccess::$user, $notrigger);
617 throw new RestException(304,
'Error nothing done. May be object is already validated');
620 throw new RestException(500,
'Error when validating expense report: '.$this->expensereport->error);
623 $this->expensereport->fetchObjectLinked();
650 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'approve')) {
651 throw new RestException(403,
"Insuffisant rights");
653 $result = $this->expensereport->fetch(
$id);
655 throw new RestException(404,
'Expense report not found');
659 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
662 $result = $this->expensereport->setApproved(DolibarrApiAccess::$user, $notrigger);
664 throw new RestException(304,
'Error nothing done. May be object is already approved');
667 throw new RestException(500,
'Error when approving expense report: '.$this->expensereport->error);
670 $this->expensereport->fetchObjectLinked();
696 public function deny(
$id, $details, $notrigger = 0)
698 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'approve')) {
699 throw new RestException(403,
"Insuffisant rights");
701 $result = $this->expensereport->fetch(
$id);
703 throw new RestException(404,
'Expense report not found');
707 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
710 $result = $this->expensereport->setDeny(DolibarrApiAccess::$user, $details, $notrigger);
712 throw new RestException(304,
'Error nothing done. May be object is already denied');
715 throw new RestException(500,
'Error when denying expense report: '.$this->expensereport->error);
718 $this->expensereport->fetchObjectLinked();
741 public function getAllPayments($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0)
745 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
746 throw new RestException(403);
749 $sql =
"SELECT t.rowid FROM " . MAIN_DB_PREFIX .
"payment_expensereport as t, ".MAIN_DB_PREFIX.
"expensereport as e";
750 $sql .=
" WHERE e.rowid = t.fk_expensereport";
751 $sql .=
' AND e.entity IN ('.getEntity(
'expensereport').
')';
753 $sql .= $this->db->order($sortfield, $sortorder);
758 $offset = $limit * $page;
760 $sql .= $this->db->plimit($limit + 1, $offset);
764 $result = $this->db->query($sql);
767 $num = $this->db->num_rows($result);
768 $min = min($num, ($limit <= 0 ? $num : $limit));
769 for ($i = 0; $i < $min; $i++) {
770 $obj = $this->db->fetch_object($result);
772 if ($paymentExpenseReport->fetch($obj->rowid) > 0) {
777 throw new RestException(503,
'Error when retrieving list of paymentexpensereport: ' . $this->db->lasterror());
797 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'lire')) {
798 throw new RestException(403);
802 $result = $paymentExpenseReport->fetch($pid);
804 throw new RestException(404,
'paymentExpenseReport not found');
826 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'to_paid')) {
827 throw new RestException(403);
832 if ($this->expensereport->fetch(
$id) <= 0) {
833 throw new RestException(404,
'Expense report not found');
838 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
840 if (isModEnabled(
"bank") && !((
int) ($request_data[
'accountid'] ?? 0) > 0)) {
841 throw new RestException(400,
"accountid field missing");
845 $paymentExpenseReport->fk_expensereport =
$id;
846 foreach ($request_data as $field => $value) {
847 $paymentExpenseReport->$field = $this->
_checkValForAPI($field, $value, $paymentExpenseReport);
852 throw new RestException(400,
'Expense report must be approved to be paid');
855 foreach ((array) $paymentExpenseReport->amounts as $value) {
856 $totalpayment += (float)
price2num($value,
'MT');
858 $remaintopay = (float)
price2num($this->expensereport->total_ttc - $this->expensereport->getSumPayments(),
'MT');
859 if ((
float)
price2num($totalpayment,
'MT') > $remaintopay) {
860 throw new RestException(400,
'Payment higher than remainder to pay ('.$remaintopay.
')');
866 if ($paymentExpenseReport->create(DolibarrApiAccess::$user) < 0) {
867 $this->db->rollback();
868 throw new RestException(400,
'Payment error : '.$paymentExpenseReport->errorsToString());
870 if (isModEnabled(
"bank")) {
871 $result = $paymentExpenseReport->addPaymentToBank(
872 DolibarrApiAccess::$user,
873 'payment_expensereport',
874 '(ExpenseReportPayment)',
875 (
int) $request_data[
'accountid'],
880 $this->db->rollback();
881 throw new RestException(400,
'Add payment to bank error : '.$paymentExpenseReport->errorsToString());
885 $remaintopay =
price2num($this->expensereport->total_ttc - $this->expensereport->getSumPayments(),
'MT');
886 if ($remaintopay == 0 && $this->expensereport->setPaid($this->expensereport->id, DolibarrApiAccess::$user) < 0) {
887 $this->db->rollback();
888 throw new RestException(400,
'Set paid error : '.$this->expensereport->errorsToString());
893 return $paymentExpenseReport->id;
912 if (!DolibarrApiAccess::$user->hasRight(
'expensereport',
'to_paid')) {
913 throw new RestException(403);
917 $result = $paymentExpenseReport->fetch(
$id);
919 throw new RestException(404,
'payment of expense report not found');
923 $result = $this->expensereport->fetch($paymentExpenseReport->fk_expensereport);
925 throw new RestException(404,
'Expense report not found');
929 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
932 foreach ($request_data as $field => $value) {
933 if ($field ==
'id' || $field ==
'fk_expensereport') {
936 $paymentExpenseReport->$field = $this->
_checkValForAPI($field, $value, $paymentExpenseReport);
941 $oldpayment->fetch($paymentExpenseReport->id);
942 $remaintopay = (float)
price2num($this->expensereport->total_ttc - $this->expensereport->getSumPayments() + (float) $oldpayment->amount,
'MT');
943 if ((
float)
price2num($paymentExpenseReport->amount,
'MT') > $remaintopay) {
944 throw new RestException(400,
'Payment higher than remainder to pay ('.$remaintopay.
')');
947 if ($paymentExpenseReport->update(DolibarrApiAccess::$user) > 0) {
948 return $this->
get(
$id);
950 throw new RestException(500, $paymentExpenseReport->error);
1004 unset(
$object->cond_reglement);
1005 unset(
$object->shipping_method_id);
1008 unset(
$object->barcode_type_code);
1009 unset(
$object->barcode_type_label);
1010 unset(
$object->barcode_type_coder);
1012 unset(
$object->code_paiement);
1014 unset(
$object->fk_c_paiement);
1016 unset(
$object->label_incoterms);
1017 unset(
$object->location_incoterms);
1018 unset(
$object->mode_reglement_id);
1019 unset(
$object->cond_reglement_id);
1025 unset(
$object->cond_reglement_id);
1050 if ($data ===
null) {
1053 $expensereport = array();
1054 foreach (ExpenseReports::$FIELDS as $field) {
1055 if (!isset($data[$field])) {
1056 throw new RestException(400,
"$field field missing");
1058 $expensereport[$field] = $data[$field];
1060 return $expensereport;
1072 if ($data ===
null) {
1075 $expensereport = array();
1076 foreach (ExpenseReports::$FIELDSPAYMENT as $field) {
1077 if (!isset($data[$field])) {
1078 throw new RestException(400,
"$field field missing");
1080 $expensereport[$field] = $data[$field];
1082 return $expensereport;
$id
Support class for third parties, contacts, members, users or resources.
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Class to manage Trips and Expenses.
const STATUS_DRAFT
Draft status.
const STATUS_APPROVED
Classified approved.
const STATUS_REFUSED
Classified refused.
getPayments($pid)
Get an expense report payment.
deny($id, $details, $notrigger=0)
Deny an expense report.
_cleanObjectDatas($object)
Delete paymentExpenseReport.
_validate($data)
Validate fields before create or update object.
validate($id, $notrigger=0)
Validate an expense report.
updatePayment($id, $request_data=null)
Update a payment of an expense report.
approve($id, $notrigger=0)
Approve an expense report.
put($id, $request_data=null)
Get lines of an Expense Report.
addPayment($id, $request_data=null)
Create a payment for an expense report.
getAllPayments($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0)
Get the list of payments of an expense report.
post($request_data=null)
Create an expense report.
_validatepayment($data)
Validate fields before create or update object.
__construct()
Constructor.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $user_ids='', $sqlfilters='', $properties='', $pagination_data=false)
List expense reports.
Class to manage payments of expense report.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.