22use Luracast\Restler\RestException;
24require_once DOL_DOCUMENT_ROOT .
'/compta/bank/class/account.class.php';
38 public static $FIELDS = array(
71 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $category = 0, $sqlfilters =
'', $properties =
'')
75 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'lire')) {
76 throw new RestException(403);
79 $sql =
"SELECT t.rowid FROM ".MAIN_DB_PREFIX.
"bank_account AS t LEFT JOIN ".MAIN_DB_PREFIX.
"bank_account_extrafields AS ef ON (ef.fk_object = t.rowid)";
81 $sql .=
", " . MAIN_DB_PREFIX .
"categorie_account as c";
83 $sql .=
' WHERE t.entity IN (' .
getEntity(
'bank_account') .
')';
86 $sql .=
" AND c.fk_categorie = " . ((int) $category) .
" AND c.fk_account = t.rowid";
91 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
93 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
97 $sql .= $this->db->order($sortfield, $sortorder);
102 $offset = $limit * $page;
104 $sql .= $this->db->plimit($limit + 1, $offset);
108 $result = $this->db->query($sql);
111 $num = $this->db->num_rows($result);
112 $min = min($num, ($limit <= 0 ? $num : $limit));
113 for ($i = 0; $i < $min; $i++) {
114 $obj = $this->db->fetch_object($result);
115 $account =
new Account($this->db);
116 if ($account->fetch($obj->rowid) > 0) {
117 $account->balance = $account->solde(1);
122 throw new RestException(503,
'Error when retrieving list of accounts: ' . $this->db->lasterror());
138 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'lire')) {
139 throw new RestException(403);
142 $account =
new Account($this->db);
143 $result = $account->fetch(
$id);
145 throw new RestException(404,
'account not found');
159 public function post($request_data =
null)
161 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'configurer')) {
162 throw new RestException(403);
167 $account =
new Account($this->db);
169 $account->date_solde = time();
170 foreach ($request_data as $field => $value) {
171 if ($field ===
'caller') {
173 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
180 if ($account->create(DolibarrApiAccess::$user) < 0) {
181 throw new RestException(500,
'Error creating bank account', array_merge(array($account->error), $account->errors));
210 public function transfer($bankaccount_from_id = 0, $bankaccount_to_id = 0, $date =
null, $description =
"", $amount = 0.0, $amount_to = 0.0, $cheque_number =
"")
212 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'configurer')) {
213 throw new RestException(403);
216 require_once DOL_DOCUMENT_ROOT .
'/compta/bank/class/account.class.php';
218 $accountfrom =
new Account($this->db);
219 $resultAccountFrom = $accountfrom->fetch($bankaccount_from_id);
221 if ($resultAccountFrom === 0) {
222 throw new RestException(404,
'The BankAccount for bankaccount_from_id provided does not exist.');
225 $accountto =
new Account($this->db);
226 $resultAccountTo = $accountto->fetch($bankaccount_to_id);
228 if ($resultAccountTo === 0) {
229 throw new RestException(404,
'The BankAccount for bankaccount_to_id provided does not exist.');
232 if ($accountto->currency_code == $accountfrom->currency_code) {
233 $amount_to = $amount;
235 if (!$amount_to || empty($amount_to)) {
236 throw new RestException(422,
'You must provide amount_to value since bankaccount_from and bankaccount_to does not share the same currency.');
240 if ($amount_to < 0) {
241 throw new RestException(422,
'You must provide a positive value for amount.');
244 if ($accountto->id == $accountfrom->id) {
245 throw new RestException(422,
'bankaccount_from_id and bankaccount_to_id must be different !');
251 $bank_line_id_from = 0;
252 $bank_line_id_to = 0;
254 $user = DolibarrApiAccess::$user;
267 $description =
sanitizeVal($description,
'alphanohtml');
268 $cheque_number =
sanitizeVal($cheque_number,
'alphanohtml');
275 $bank_line_id_from = $accountfrom->addline((
int) $date, $typefrom, $description, -1 * (
float)
price2num($amount),
'', 0, $user, $cheque_number);
277 if (!($bank_line_id_from > 0)) {
282 $bank_line_id_to = $accountto->addline((
int) $date, $typeto, $description, (
float)
price2num($amount_to),
'', 0, $user, $cheque_number);
284 if (!($bank_line_id_to > 0)) {
292 $url = DOL_URL_ROOT .
'/compta/bank/line.php?rowid=';
293 $label =
'(banktransfert)';
294 $type =
'banktransfert';
297 $result = $accountfrom->add_url_line($bank_line_id_from, $bank_line_id_to, $url, $label, $type);
299 if (!($result > 0)) {
304 $result = $accountto->add_url_line($bank_line_id_to, $bank_line_id_from, $url, $label, $type);
306 if (!($result > 0)) {
316 'message' =>
'Internal wire transfer created successfully.',
317 'bank_id_from' => $bank_line_id_from,
318 'bank_id_to' => $bank_line_id_to,
322 $this->db->rollback();
323 throw new RestException(500, $accountfrom->error .
' ' . $accountto->error);
336 public function put(
$id, $request_data =
null)
338 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'configurer')) {
339 throw new RestException(403);
342 $account =
new Account($this->db);
343 $result = $account->fetch(
$id);
345 throw new RestException(404,
'account not found');
348 foreach ($request_data as $field => $value) {
349 if ($field ==
'id') {
352 if ($field ===
'caller') {
354 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
358 if ($field ==
'array_options' && is_array($value)) {
359 foreach ($value as $index => $val) {
367 if ($account->update(DolibarrApiAccess::$user) > 0) {
368 return $this->
get(
$id);
370 throw new RestException(500, $account->error);
382 public function delete(
$id)
384 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'configurer')) {
385 throw new RestException(403);
387 $account =
new Account($this->db);
388 $result = $account->fetch(
$id);
390 throw new RestException(404,
'account not found');
393 if ($account->delete(DolibarrApiAccess::$user) < 0) {
394 throw new RestException(500,
'error when deleting account');
400 'message' =>
'account deleted'
415 if ($data ===
null) {
419 foreach (BankAccounts::$FIELDS as $field) {
420 if (!isset($data[$field])) {
421 throw new RestException(400,
"$field field missing");
423 $account[$field] = $data[$field];
465 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'lire')) {
466 throw new RestException(403);
469 $account =
new Account($this->db);
470 $result = $account->fetch(
$id);
472 throw new RestException(404,
'account not found');
475 $sql =
"SELECT rowid FROM " . MAIN_DB_PREFIX .
"bank ";
476 $sql .=
" WHERE fk_account = " . ((int)
$id);
481 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
483 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
487 $sql .=
" ORDER BY rowid";
489 $result = $this->db->query($sql);
492 $num = $this->db->num_rows($result);
494 for ($i = 0; $i < $num; $i++) {
495 $obj = $this->db->fetch_object($result);
497 if ($accountLine->fetch($obj->rowid) > 0) {
502 throw new RestException(503,
'Error when retrieving list of account lines: ' . $this->db->lasterror());
527 public function addLine(
$id, $date, $type, $label, $amount, $category = 0, $cheque_number =
'', $cheque_writer =
'', $cheque_bank =
'', $accountancycode =
'', $datev =
null, $num_releve =
'')
529 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'modifier')) {
530 throw new RestException(403);
533 $account =
new Account($this->db);
534 $result = $account->fetch(
$id);
536 throw new RestException(404,
'account not found');
547 $result = $account->addline(
554 DolibarrApiAccess::$user,
562 throw new RestException(503,
'Error when adding line to account: ' . $account->error);
580 public function addLink(
$id, $line_id, $url_id, $url, $label, $type)
582 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'modifier')) {
583 throw new RestException(403);
586 $account =
new Account($this->db);
587 $result = $account->fetch(
$id);
589 throw new RestException(404,
'account not found');
593 $result = $accountLine->fetch($line_id);
595 throw new RestException(404,
'account line not found');
598 if ($accountLine->fk_account !=
$id) {
599 throw new RestException(400,
'Line does not belong to this account');
606 $result = $account->add_url_line($line_id, $url_id, $url, $label, $type);
608 throw new RestException(503,
'Error when adding link to account line: ' . $account->error);
628 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'lire')) {
629 throw new RestException(403);
632 $account =
new Account($this->db);
633 $result = $account->fetch(
$id);
635 throw new RestException(404,
'account not found');
638 $links = $account->get_url($line_id);
639 foreach ($links as &$link) {
640 unset($link[0], $link[1], $link[2], $link[3]);
657 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'lire')) {
658 throw new RestException(403);
662 $result = $accountLine->fetch($line_id);
664 throw new RestException(404,
'account Line not found');
682 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'modifier')) {
683 throw new RestException(403);
686 $account =
new Account($this->db);
687 $result = $account->fetch(
$id);
689 throw new RestException(404,
'account not found');
693 $result = $accountLine->fetch($line_id);
695 throw new RestException(404,
'account line not found');
698 if ($accountLine->fk_account !=
$id) {
699 throw new RestException(400,
'Line does not belong to this account');
704 $result = $accountLine->updateLabel();
706 throw new RestException(503,
'Error when updating link to account line: ' . $accountLine->error);
708 return $accountLine->id;
724 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'modifier')) {
725 throw new RestException(403);
728 $account =
new Account($this->db);
729 $result = $account->fetch(
$id);
731 throw new RestException(404,
'account not found');
735 $result = $accountLine->fetch($line_id);
737 throw new RestException(404,
'account line not found');
740 if ($accountLine->fk_account !=
$id) {
741 throw new RestException(400,
'Line does not belong to this account');
744 if ($accountLine->delete(DolibarrApiAccess::$user) < 0) {
745 throw new RestException(500,
'error when deleting account line');
751 'message' =>
"account line $line_id deleted"
767 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'lire')) {
768 throw new RestException(403);
771 $account =
new Account($this->db);
772 $result = $account->fetch(
$id);
775 throw new RestException(404,
'account not found');
778 $balance = $account->solde(1);
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
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.
getLinks($id, $line_id)
Get the list of links for a line of the account.
transfer($bankaccount_from_id=0, $bankaccount_to_id=0, $date=null, $description="", $amount=0.0, $amount_to=0.0, $cheque_number="")
Create an internal wire transfer between two bank accounts.
__construct()
Constructor.
getBalance($id)
Get the current account balance.
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.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $category=0, $sqlfilters='', $properties='')
Get the list of accounts.
getDetailAccountLine($line_id)
Get the detail of a given line of the bank account.
getLines($id, $sqlfilters='')
Get the list of lines of the account.
deleteLine($id, $line_id)
Delete an account line.
_cleanObjectDatas($object)
Clean sensible object datas @phpstan-template T.
updateLine($id, $line_id, $label)
Update an account line.
_checkValExtrafieldsForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
_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.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.