diff --git a/htdocs/expensereport/class/api_expensereports.class.php b/htdocs/expensereport/class/api_expensereports.class.php index 3a8ae5f764d..1fc30629aea 100644 --- a/htdocs/expensereport/class/api_expensereports.class.php +++ b/htdocs/expensereport/class/api_expensereports.class.php @@ -546,6 +546,46 @@ class ExpenseReports extends DolibarrApi ); } + /** + * Set an expense report to draft + * + * @since 23.0.0 Initial implementation + * + * @param int $id Expense report ID + * + * @url POST {id}/settodraft + * + * @return Object + * + * @throws RestException 304 + * @throws RestException 403 + * @throws RestException 404 + */ + public function setToDraft($id) + { + if (!DolibarrApiAccess::$user->hasRight('expensereport', 'creer')) { + throw new RestException(403, "Insufficiant rights"); + } + $result = $this->expensereport->fetch($id); + if (!$result) { + throw new RestException(404, 'Expense report not found'); + } + + if (!DolibarrApi::_checkAccessToResource('expensereport', $this->expensereport->id)) { + throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + $result = $this->expensereport->setStatut(0); + if ($result == 0) { + throw new RestException(304, 'Error nothing done. May be object is already draft'); + } + if ($result < 0) { + throw new RestException(500, 'Error when setting to draft expense report: '.$this->expensereport->error); + } + + return $this->_cleanObjectDatas($this->expensereport); + } + /** * Validate an expense report *