19 use Luracast\Restler\RestException;
21 require_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
65 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $category = 0, $sqlfilters =
'')
69 if (!DolibarrApiAccess::$user->rights->banque->lire) {
70 throw new RestException(401);
73 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"bank_account as t";
75 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_account as c";
77 $sql .=
' WHERE t.entity IN ('.getEntity(
'bank_account').
')';
80 $sql .=
" AND c.fk_categorie = ".((int) $category).
" AND c.fk_account = t.rowid";
86 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
88 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
89 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
92 $sql .= $this->
db->order($sortfield, $sortorder);
97 $offset = $limit * $page;
99 $sql .= $this->
db->plimit($limit + 1, $offset);
103 $result = $this->
db->query($sql);
106 $num = $this->
db->num_rows($result);
107 $min = min($num, ($limit <= 0 ? $num : $limit));
108 for ($i = 0; $i < $min; $i++) {
109 $obj = $this->
db->fetch_object($result);
111 if ($account->fetch($obj->rowid) > 0) {
116 throw new RestException(503,
'Error when retrieving list of accounts: '.$this->
db->lasterror());
130 public function get($id)
132 if (!DolibarrApiAccess::$user->rights->banque->lire) {
133 throw new RestException(401);
137 $result = $account->fetch($id);
139 throw new RestException(404,
'account not found');
151 public function post($request_data =
null)
153 if (!DolibarrApiAccess::$user->rights->banque->configurer) {
154 throw new RestException(401);
157 $result = $this->
_validate($request_data);
160 foreach ($request_data as $field => $value) {
164 $account->date_solde = time();
167 $account->courant = $account->type;
169 if ($account->create(DolibarrApiAccess::$user) < 0) {
170 throw new RestException(500,
'Error creating bank account', array_merge(array($account->error), $account->errors));
196 public function transfer($bankaccount_from_id = 0, $bankaccount_to_id = 0, $date =
null, $description =
"", $amount = 0.0, $amount_to = 0.0)
198 if (!DolibarrApiAccess::$user->rights->banque->configurer) {
199 throw new RestException(401);
202 require_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
205 $resultAccountFrom = $accountfrom->fetch($bankaccount_from_id);
207 if ($resultAccountFrom === 0) {
208 throw new RestException(404,
'The BankAccount for bankaccount_from_id provided does not exist.');
212 $resultAccountTo = $accountto->fetch($bankaccount_to_id);
214 if ($resultAccountTo === 0) {
215 throw new RestException(404,
'The BankAccount for bankaccount_to_id provided does not exist.');
218 if ($accountto->currency_code == $accountfrom->currency_code) {
219 $amount_to = $amount;
221 if (!$amount_to || empty($amount_to)) {
222 throw new RestException(422,
'You must provide amount_to value since bankaccount_from and bankaccount_to does not share the same currency.');
226 if ($amount_to < 0) {
227 throw new RestException(422,
'You must provide a positive value for amount.');
230 if ($accountto->id == $accountfrom->id) {
231 throw new RestException(422,
'bankaccount_from_id and bankaccount_to_id must be different !');
237 $bank_line_id_from = 0;
238 $bank_line_id_to = 0;
240 $user = DolibarrApiAccess::$user;
253 $description =
sanitizeVal($description,
'alphanohtml');
261 $bank_line_id_from = $accountfrom->addline($date, $typefrom, $description, -1 *
price2num($amount),
'',
'', $user);
263 if (!($bank_line_id_from > 0)) {
268 $bank_line_id_to = $accountto->addline($date, $typeto, $description,
price2num($amount_to),
'',
'', $user);
270 if (!($bank_line_id_to > 0)) {
278 $url = DOL_URL_ROOT.
'/compta/bank/line.php?rowid=';
279 $label =
'(banktransfert)';
280 $type =
'banktransfert';
283 $result = $accountfrom->add_url_line($bank_line_id_from, $bank_line_id_to, $url, $label, $type);
285 if (!($result > 0)) {
290 $result = $accountto->add_url_line($bank_line_id_to, $bank_line_id_from, $url, $label, $type);
292 if (!($result > 0)) {
302 'message' =>
'Internal wire transfer created successfully.',
303 'bank_id_from' => $bank_line_id_from,
304 'bank_id_to' => $bank_line_id_to,
308 $this->
db->rollback();
309 throw new RestException(500, $accountfrom->error.
' '.$accountto->error);
320 public function put($id, $request_data =
null)
322 if (!DolibarrApiAccess::$user->rights->banque->configurer) {
323 throw new RestException(401);
327 $result = $account->fetch($id);
329 throw new RestException(404,
'account not found');
332 foreach ($request_data as $field => $value) {
333 if ($field ==
'id') {
339 if ($account->update(DolibarrApiAccess::$user) > 0) {
340 return $this->
get($id);
342 throw new RestException(500, $account->error);
352 public function delete($id)
354 if (!DolibarrApiAccess::$user->rights->banque->configurer) {
355 throw new RestException(401);
358 $result = $account->fetch($id);
360 throw new RestException(404,
'account not found');
363 if ($account->delete(DolibarrApiAccess::$user) < 0) {
364 throw new RestException(401,
'error when deleting account');
370 'message' =>
'account deleted'
387 if (!isset($data[$field])) {
388 throw new RestException(400,
"$field field missing");
390 $account[$field] = $data[$field];
405 $object = parent::_cleanObjectDatas($object);
407 unset($object->rowid);
427 if (!DolibarrApiAccess::$user->rights->banque->lire) {
428 throw new RestException(401);
432 $result = $account->fetch($id);
434 throw new RestException(404,
'account not found');
437 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"bank ";
438 $sql .=
" WHERE fk_account = ".((int) $id);
444 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
446 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
447 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
450 $sql .=
" ORDER BY rowid";
452 $result = $this->
db->query($sql);
455 $num = $this->
db->num_rows($result);
456 for ($i = 0; $i < $num; $i++) {
457 $obj = $this->
db->fetch_object($result);
459 if ($accountLine->fetch($obj->rowid) > 0) {
464 throw new RestException(503,
'Error when retrieving list of account lines: '.$this->
db->lasterror());
489 public function addLine($id, $date, $type, $label, $amount, $category = 0, $cheque_number =
'', $cheque_writer =
'', $cheque_bank =
'', $accountancycode =
'', $datev =
null, $num_releve =
'')
491 if (!DolibarrApiAccess::$user->rights->banque->modifier) {
492 throw new RestException(401);
496 $result = $account->fetch($id);
498 throw new RestException(404,
'account not found');
509 $result = $account->addline(
516 DolibarrApiAccess::$user,
524 throw new RestException(503,
'Error when adding line to account: '.$account->error);
542 public function addLink($id, $line_id, $url_id, $url, $label, $type)
544 if (!DolibarrApiAccess::$user->rights->banque->modifier) {
545 throw new RestException(401);
549 $result = $account->fetch($id);
551 throw new RestException(404,
'account not found');
555 $result = $accountLine->fetch($line_id);
557 throw new RestException(404,
'account line not found');
564 $result = $account->add_url_line($line_id, $url_id, $url, $label, $type);
566 throw new RestException(503,
'Error when adding link to account line: '.$account->error);
Class to manage bank accounts.
const TYPE_CASH
Cash account.
Class to manage bank transaction lines.
post($request_data=null)
Create account object.
addLink($id, $line_id, $url_id, $url, $label, $type)
Add a link to an account line.
put($id, $request_data=null)
Update account.
_validate($data)
Validate fields before creating an object.
__construct()
Constructor.
addLine($id, $date, $type, $label, $amount, $category=0, $cheque_number='', $cheque_writer='', $cheque_bank='', $accountancycode='', $datev=null, $num_releve='')
Add a line to an account.
transfer($bankaccount_from_id=0, $bankaccount_to_id=0, $date=null, $description="", $amount=0.0, $amount_to=0.0)
Create an internal wire transfer between two bank accounts.
getLines($id, $sqlfilters='')
Get the list of lines of the account.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $category=0, $sqlfilters='')
Get the list of accounts.
static $FIELDS
array $FIELDS Mandatory fields, checked when creating an object
_cleanObjectDatas($object)
Clean sensible object datas.
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
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.
$conf db
API class for accounts.