22use Luracast\Restler\RestException;
24require_once DOL_DOCUMENT_ROOT .
'/compta/bank/class/account.class.php';
25require_once DOL_DOCUMENT_ROOT .
'/compta/bank/class/bankcateg.class.php';
26require_once DOL_DOCUMENT_ROOT .
'/categories/class/categorie.class.php';
40 public static $FIELDS = array(
73 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $category = 0, $sqlfilters =
'', $properties =
'')
77 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'lire')) {
78 throw new RestException(403);
81 $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)";
83 $sql .=
", " . MAIN_DB_PREFIX .
"categorie_account as c";
85 $sql .=
' WHERE t.entity IN (' .
getEntity(
'bank_account') .
')';
88 $sql .=
" AND c.fk_categorie = " . ((int) $category) .
" AND c.fk_account = t.rowid";
95 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
99 $sql .= $this->db->order($sortfield, $sortorder);
104 $offset = $limit * $page;
106 $sql .= $this->db->plimit($limit + 1, $offset);
110 $result = $this->db->query($sql);
113 $num = $this->db->num_rows($result);
114 $min = min($num, ($limit <= 0 ? $num : $limit));
115 for ($i = 0; $i < $min; $i++) {
116 $obj = $this->db->fetch_object($result);
117 $account =
new Account($this->db);
118 if ($account->fetch($obj->rowid) > 0) {
119 $account->balance = $account->solde(1);
124 throw new RestException(503,
'Error when retrieving list of accounts: ' . $this->db->lasterror());
140 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'lire')) {
141 throw new RestException(403);
144 $account =
new Account($this->db);
145 $result = $account->fetch(
$id);
147 throw new RestException(404,
'account not found');
161 public function post($request_data =
null)
163 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'configurer')) {
164 throw new RestException(403);
169 $account =
new Account($this->db);
171 $account->date_solde =
dol_now();
172 foreach ($request_data as $field => $value) {
173 if ($field ===
'caller') {
175 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
182 if ($account->create(DolibarrApiAccess::$user) < 0) {
183 throw new RestException(500,
'Error creating bank account', array_merge(array($account->error), $account->errors));
212 public function transfer($bankaccount_from_id = 0, $bankaccount_to_id = 0, $date =
null, $description =
"", $amount = 0.0, $amount_to = 0.0, $cheque_number =
"")
214 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'configurer')) {
215 throw new RestException(403);
218 require_once DOL_DOCUMENT_ROOT .
'/compta/bank/class/account.class.php';
220 $accountfrom =
new Account($this->db);
221 $resultAccountFrom = $accountfrom->fetch($bankaccount_from_id);
223 if ($resultAccountFrom === 0) {
224 throw new RestException(404,
'The BankAccount for bankaccount_from_id provided does not exist.');
227 $accountto =
new Account($this->db);
228 $resultAccountTo = $accountto->fetch($bankaccount_to_id);
230 if ($resultAccountTo === 0) {
231 throw new RestException(404,
'The BankAccount for bankaccount_to_id provided does not exist.');
234 if ($accountto->currency_code == $accountfrom->currency_code) {
235 $amount_to = $amount;
237 if (!$amount_to || empty($amount_to)) {
238 throw new RestException(422,
'You must provide amount_to value since bankaccount_from and bankaccount_to does not share the same currency.');
242 if ($amount_to < 0) {
243 throw new RestException(422,
'You must provide a positive value for amount.');
246 if ($accountto->id == $accountfrom->id) {
247 throw new RestException(422,
'bankaccount_from_id and bankaccount_to_id must be different !');
253 $bank_line_id_from = 0;
254 $bank_line_id_to = 0;
256 $user = DolibarrApiAccess::$user;
269 $description =
sanitizeVal($description,
'alphanohtml');
270 $cheque_number =
sanitizeVal($cheque_number,
'alphanohtml');
277 $bank_line_id_from = $accountfrom->addline((
int) $date, $typefrom, $description, -1 * (
float)
price2num($amount),
'', 0, $user, $cheque_number);
279 if (!($bank_line_id_from > 0)) {
284 $bank_line_id_to = $accountto->addline((
int) $date, $typeto, $description, (
float)
price2num($amount_to),
'', 0, $user, $cheque_number);
286 if (!($bank_line_id_to > 0)) {
294 $url = DOL_URL_ROOT .
'/compta/bank/line.php?rowid=';
295 $label =
'(banktransfert)';
296 $type =
'banktransfert';
299 $result = $accountfrom->add_url_line($bank_line_id_from, $bank_line_id_to, $url, $label, $type);
301 if (!($result > 0)) {
306 $result = $accountto->add_url_line($bank_line_id_to, $bank_line_id_from, $url, $label, $type);
308 if (!($result > 0)) {
318 'message' =>
'Internal wire transfer created successfully.',
319 'bank_id_from' => $bank_line_id_from,
320 'bank_id_to' => $bank_line_id_to,
324 $this->db->rollback();
325 throw new RestException(500, $accountfrom->errorsToString() .
' ' . $accountto->errorsToString());
338 public function put(
$id, $request_data =
null)
340 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'configurer')) {
341 throw new RestException(403);
344 $account =
new Account($this->db);
345 $result = $account->fetch(
$id);
347 throw new RestException(404,
'account not found');
350 foreach ($request_data as $field => $value) {
351 if ($field ==
'id') {
354 if ($field ===
'caller') {
356 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
360 if ($field ==
'array_options' && is_array($value)) {
361 foreach ($value as $index => $val) {
369 if ($account->update(DolibarrApiAccess::$user) > 0) {
370 return $this->
get(
$id);
372 throw new RestException(500, $account->errorsToString());
384 public function delete(
$id)
386 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'configurer')) {
387 throw new RestException(403);
389 $account =
new Account($this->db);
390 $result = $account->fetch(
$id);
392 throw new RestException(404,
'account not found');
395 if ($account->delete(DolibarrApiAccess::$user) < 0) {
396 throw new RestException(500,
'error when deleting account');
402 'message' =>
'account deleted'
417 if ($data ===
null) {
421 foreach (BankAccounts::$FIELDS as $field) {
422 if (!isset($data[$field])) {
423 throw new RestException(400,
"$field field missing");
425 $account[$field] = $data[$field];
467 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'lire')) {
468 throw new RestException(403);
471 $account =
new Account($this->db);
472 $result = $account->fetch(
$id);
474 throw new RestException(404,
'account not found');
477 $sql =
"SELECT rowid FROM " . MAIN_DB_PREFIX .
"bank ";
478 $sql .=
" WHERE fk_account = " . ((int)
$id);
485 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
489 $sql .=
" ORDER BY rowid";
491 $result = $this->db->query($sql);
494 $num = $this->db->num_rows($result);
496 for ($i = 0; $i < $num; $i++) {
497 $obj = $this->db->fetch_object($result);
499 if ($accountLine->fetch($obj->rowid) > 0) {
504 throw new RestException(503,
'Error when retrieving list of account lines: ' . $this->db->lasterror());
529 public function addLine(
$id, $date, $type, $label, $amount, $category = 0, $cheque_number =
'', $cheque_writer =
'', $cheque_bank =
'', $accountancycode =
'', $datev =
null, $num_releve =
'')
531 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'modifier')) {
532 throw new RestException(403);
535 $account =
new Account($this->db);
536 $result = $account->fetch(
$id);
538 throw new RestException(404,
'account not found');
549 $result = $account->addline(
556 DolibarrApiAccess::$user,
564 throw new RestException(503,
'Error when adding line to account: ' . $account->errorsToString());
582 public function addLink(
$id, $line_id, $url_id, $url, $label, $type)
584 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'modifier')) {
585 throw new RestException(403);
588 $account =
new Account($this->db);
589 $result = $account->fetch(
$id);
591 throw new RestException(404,
'account not found');
595 $result = $accountLine->fetch($line_id);
597 throw new RestException(404,
'account line not found');
600 if ($accountLine->fk_account !=
$id) {
601 throw new RestException(400,
'Line does not belong to this account');
608 $result = $account->add_url_line($line_id, $url_id, $url, $label, $type);
610 throw new RestException(503,
'Error when adding link to account line: ' . $account->errorsToString());
630 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'lire')) {
631 throw new RestException(403);
634 $account =
new Account($this->db);
635 $result = $account->fetch(
$id);
637 throw new RestException(404,
'account not found');
640 $links = $account->get_url($line_id);
641 foreach ($links as &$link) {
642 unset($link[0], $link[1], $link[2], $link[3]);
659 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'lire')) {
660 throw new RestException(403);
664 $result = $accountLine->fetch($line_id);
666 throw new RestException(404,
'account Line not found');
684 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'modifier')) {
685 throw new RestException(403);
688 $account =
new Account($this->db);
689 $result = $account->fetch(
$id);
691 throw new RestException(404,
'account not found');
695 $result = $accountLine->fetch($line_id);
697 throw new RestException(404,
'account line not found');
700 if ($accountLine->fk_account !=
$id) {
701 throw new RestException(400,
'Line does not belong to this account');
706 $result = $accountLine->updateLabel();
708 throw new RestException(503,
'Error when updating link to account line: ' . $accountLine->errorsToString());
710 return $accountLine->id;
726 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'modifier')) {
727 throw new RestException(403);
730 $account =
new Account($this->db);
731 $result = $account->fetch(
$id);
733 throw new RestException(404,
'account not found');
737 $result = $accountLine->fetch($line_id);
739 throw new RestException(404,
'account line not found');
742 if ($accountLine->fk_account !=
$id) {
743 throw new RestException(400,
'Line does not belong to this account');
746 if ($accountLine->delete(DolibarrApiAccess::$user) < 0) {
747 throw new RestException(500,
'error when deleting account line');
753 'message' =>
"account line $line_id deleted"
784 public function reconcile(
$id, $num_releve, $lines, $conciliated = 1, $catid = 0)
788 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'lire') || !DolibarrApiAccess::$user->hasRight(
'banque',
'consolidate')) {
789 throw new RestException(403);
792 $account =
new Account($this->db);
793 $result = $account->fetch(
$id);
795 throw new RestException(404,
'account not found');
798 throw new RestException(403,
'Access to this bank account not allowed for login '.DolibarrApiAccess::$user->login);
800 if ($account->canBeConciliated() <= 0) {
801 throw new RestException(400,
'Account can not be reconciled (reconciliation disabled, cash account or closed account)');
805 if ($num_releve ===
'' ||
dol_strlen($num_releve) > 50) {
806 throw new RestException(400,
'num_releve field missing or longer than 50 characters');
809 $langs->load(
"errors");
810 throw new RestException(400, $langs->transnoentitiesnoconv(
"ErrorBankStatementNameMustFollowRegex",
getDolGlobalString(
'BANK_STATEMENT_REGEX_RULE')));
812 if (!is_array($lines) || empty($lines)) {
813 throw new RestException(400,
'lines field missing or empty');
815 $conciliated = empty($conciliated) ? 0 : 1;
816 $catid = (int) $catid;
819 if ($bankcateg->fetch($catid) <= 0 || empty($bankcateg->id)) {
820 throw new RestException(400,
'Bank category '.$catid.
' not found');
828 foreach (array_unique(array_map(
'intval', $lines)) as $line_id) {
830 if ($line_id <= 0 || $accountLine->fetch($line_id) <= 0) {
831 throw new RestException(404,
'account line '.$line_id.
' not found');
833 if ($accountLine->fk_account !=
$id) {
834 throw new RestException(400,
'Line '.$line_id.
' does not belong to this account');
836 if ($accountLine->rappro && (
string) $accountLine->num_releve !== $num_releve) {
837 throw new RestException(409,
'Line '.$line_id.
' is already reconciled with statement '.$accountLine->num_releve.
', cannot reconcile it with '.$num_releve);
839 $lineids[] = $line_id;
840 if (!$accountLine->rappro) {
844 $linecats = $categorie->containing($line_id, Categorie::TYPE_BANK_LINE,
'id');
845 if (!is_array($linecats)) {
846 throw new RestException(503,
'Error when reading categories of account line '.$line_id);
848 $linecat = in_array($catid, $linecats) ? 0 : $catid;
850 $toupdate[] = array($accountLine, $linecat);
855 foreach ($toupdate as list($accountLine, $linecat)) {
856 $accountLine->num_releve = $num_releve;
857 if ($accountLine->update_conciliation(DolibarrApiAccess::$user, $linecat, $conciliated) < 0) {
858 $errors = array_merge(array($accountLine->error, $this->db->lasterror()), $accountLine->errors);
859 $this->db->rollback();
860 throw new RestException(503,
'Error when reconciling account line '.$accountLine->id, $errors);
863 if (!$this->db->commit()) {
864 throw new RestException(503,
'Error when reconciling account lines: '.$this->db->lasterror());
868 'num_releve' => $num_releve,
869 'conciliated' => $conciliated,
885 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'lire')) {
886 throw new RestException(403);
889 $account =
new Account($this->db);
890 $result = $account->fetch(
$id);
893 throw new RestException(404,
'account not found');
896 $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.
reconcile($id, $num_releve, $lines, $conciliated=1, $catid=0)
Reconcile account lines with a bank statement.
_cleanObjectDatas($object)
Clean sensible object datas @phpstan-template T.
updateLine($id, $line_id, $label)
Update an account line.
Class to manage bank categories.
Class to manage categories.
_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.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $parenttableforentity='')
Check access by user to a given resource.
dol_now($mode='gmt')
Return date for now.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
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.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.