22use Luracast\Restler\RestException;
24require_once DOL_DOCUMENT_ROOT .
'/compta/bank/class/account.class.php';
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";
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');
181 $account->courant = $account->type;
183 if ($account->create(DolibarrApiAccess::$user) < 0) {
184 throw new RestException(500,
'Error creating bank account', array_merge(array($account->error), $account->errors));
213 public function transfer($bankaccount_from_id = 0, $bankaccount_to_id = 0, $date =
null, $description =
"", $amount = 0.0, $amount_to = 0.0, $cheque_number =
"")
215 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'configurer')) {
216 throw new RestException(403);
219 require_once DOL_DOCUMENT_ROOT .
'/compta/bank/class/account.class.php';
221 $accountfrom =
new Account($this->db);
222 $resultAccountFrom = $accountfrom->fetch($bankaccount_from_id);
224 if ($resultAccountFrom === 0) {
225 throw new RestException(404,
'The BankAccount for bankaccount_from_id provided does not exist.');
228 $accountto =
new Account($this->db);
229 $resultAccountTo = $accountto->fetch($bankaccount_to_id);
231 if ($resultAccountTo === 0) {
232 throw new RestException(404,
'The BankAccount for bankaccount_to_id provided does not exist.');
235 if ($accountto->currency_code == $accountfrom->currency_code) {
236 $amount_to = $amount;
238 if (!$amount_to || empty($amount_to)) {
239 throw new RestException(422,
'You must provide amount_to value since bankaccount_from and bankaccount_to does not share the same currency.');
243 if ($amount_to < 0) {
244 throw new RestException(422,
'You must provide a positive value for amount.');
247 if ($accountto->id == $accountfrom->id) {
248 throw new RestException(422,
'bankaccount_from_id and bankaccount_to_id must be different !');
254 $bank_line_id_from = 0;
255 $bank_line_id_to = 0;
257 $user = DolibarrApiAccess::$user;
270 $description =
sanitizeVal($description,
'alphanohtml');
271 $cheque_number =
sanitizeVal($cheque_number,
'alphanohtml');
278 $bank_line_id_from = $accountfrom->addline((
int) $date, $typefrom, $description, -1 * (
float)
price2num($amount),
'', 0, $user, $cheque_number);
280 if (!($bank_line_id_from > 0)) {
285 $bank_line_id_to = $accountto->addline((
int) $date, $typeto, $description, (
float)
price2num($amount_to),
'', 0, $user, $cheque_number);
287 if (!($bank_line_id_to > 0)) {
295 $url = DOL_URL_ROOT .
'/compta/bank/line.php?rowid=';
296 $label =
'(banktransfert)';
297 $type =
'banktransfert';
300 $result = $accountfrom->add_url_line($bank_line_id_from, $bank_line_id_to, $url, $label, $type);
302 if (!($result > 0)) {
307 $result = $accountto->add_url_line($bank_line_id_to, $bank_line_id_from, $url, $label, $type);
309 if (!($result > 0)) {
319 'message' =>
'Internal wire transfer created successfully.',
320 'bank_id_from' => $bank_line_id_from,
321 'bank_id_to' => $bank_line_id_to,
325 $this->db->rollback();
326 throw new RestException(500, $accountfrom->error .
' ' . $accountto->error);
339 public function put(
$id, $request_data =
null)
341 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'configurer')) {
342 throw new RestException(403);
345 $account =
new Account($this->db);
346 $result = $account->fetch(
$id);
348 throw new RestException(404,
'account not found');
351 foreach ($request_data as $field => $value) {
352 if ($field ==
'id') {
355 if ($field ===
'caller') {
357 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
361 if ($field ==
'array_options' && is_array($value)) {
362 foreach ($value as $index => $val) {
363 $account->array_options[$index] = $this->
_checkValForAPI($field, $val, $account);
370 if ($account->update(DolibarrApiAccess::$user) > 0) {
371 return $this->
get(
$id);
373 throw new RestException(500, $account->error);
385 public function delete(
$id)
387 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'configurer')) {
388 throw new RestException(403);
390 $account =
new Account($this->db);
391 $result = $account->fetch(
$id);
393 throw new RestException(404,
'account not found');
396 if ($account->delete(DolibarrApiAccess::$user) < 0) {
397 throw new RestException(500,
'error when deleting account');
403 'message' =>
'account deleted'
418 if ($data ===
null) {
423 if (!isset($data[$field])) {
424 throw new RestException(400,
"$field field missing");
426 $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);
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);
493 for ($i = 0; $i < $num; $i++) {
494 $obj = $this->db->fetch_object($result);
496 if ($accountLine->fetch($obj->rowid) > 0) {
501 throw new RestException(503,
'Error when retrieving list of account lines: ' . $this->db->lasterror());
526 public function addLine(
$id, $date, $type, $label, $amount, $category = 0, $cheque_number =
'', $cheque_writer =
'', $cheque_bank =
'', $accountancycode =
'', $datev =
null, $num_releve =
'')
528 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'modifier')) {
529 throw new RestException(403);
532 $account =
new Account($this->db);
533 $result = $account->fetch(
$id);
535 throw new RestException(404,
'account not found');
546 $result = $account->addline(
553 DolibarrApiAccess::$user,
561 throw new RestException(503,
'Error when adding line to account: ' . $account->error);
579 public function addLink(
$id, $line_id, $url_id, $url, $label, $type)
581 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'modifier')) {
582 throw new RestException(403);
585 $account =
new Account($this->db);
586 $result = $account->fetch(
$id);
588 throw new RestException(404,
'account not found');
592 $result = $accountLine->fetch($line_id);
594 throw new RestException(404,
'account line not found');
601 $result = $account->add_url_line($line_id, $url_id, $url, $label, $type);
603 throw new RestException(503,
'Error when adding link to account line: ' . $account->error);
623 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'lire')) {
624 throw new RestException(403);
627 $account =
new Account($this->db);
628 $result = $account->fetch(
$id);
630 throw new RestException(404,
'account not found');
633 $links = $account->get_url($line_id);
634 foreach ($links as &$link) {
635 unset($link[0], $link[1], $link[2], $link[3]);
653 if (!DolibarrApiAccess::$user->rights->banque->modifier) {
654 throw new RestException(403);
657 $account =
new Account($this->db);
658 $result = $account->fetch(
$id);
660 throw new RestException(404,
'account not found');
664 $result = $accountLine->fetch($line_id);
666 throw new RestException(404,
'account line not found');
671 $result = $accountLine->updateLabel();
673 throw new RestException(503,
'Error when updating link to account line: ' . $accountLine->error);
675 return $accountLine->id;
691 if (!DolibarrApiAccess::$user->rights->banque->modifier) {
692 throw new RestException(403);
695 $account =
new Account($this->db);
696 $result = $account->fetch(
$id);
698 throw new RestException(404,
'account not found');
702 $result = $accountLine->fetch($line_id);
704 throw new RestException(404,
'account line not found');
707 if ($accountLine->delete(DolibarrApiAccess::$user) < 0) {
708 throw new RestException(500,
'error when deleting account line');
714 'message' =>
"account line $line_id deleted"
730 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'lire')) {
731 throw new RestException(403);
734 $account =
new Account($this->db);
735 $result = $account->fetch(
$id);
738 throw new RestException(404,
'account not found');
740 $balance = $account->solde(1);
$id
Support class for third parties, contacts, members, users or resources.
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $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 current account balance by ID.
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.
getLines($id, $sqlfilters='')
Get the list of lines of the account.
deleteLine($id, $line_id)
Delete an account line.
static $FIELDS
array $FIELDS Mandatory fields, checked when creating an object
_cleanObjectDatas($object)
Clean sensible object datas.
updateLine($id, $line_id, $label)
Update an account line.
_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 '.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
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.