19use Luracast\Restler\RestException;
21require_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
66 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $category = 0, $sqlfilters =
'', $properties =
'')
70 if (!DolibarrApiAccess::$user->rights->banque->lire) {
71 throw new RestException(401);
74 $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)";
76 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_account as c";
78 $sql .=
' WHERE t.entity IN ('.getEntity(
'bank_account').
')';
81 $sql .=
" AND c.fk_categorie = ".((int) $category).
" AND c.fk_account = t.rowid";
88 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
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);
110 $account =
new Account($this->db);
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);
136 $account =
new Account($this->db);
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);
159 $account =
new Account($this->db);
160 foreach ($request_data as $field => $value) {
161 if ($field ===
'caller') {
163 $account->context[
'caller'] = $request_data[
'caller'];
170 $account->date_solde = time();
173 $account->courant = $account->type;
175 if ($account->create(DolibarrApiAccess::$user) < 0) {
176 throw new RestException(500,
'Error creating bank account', array_merge(array($account->error), $account->errors));
202 public function transfer($bankaccount_from_id = 0, $bankaccount_to_id = 0, $date =
null, $description =
"", $amount = 0.0, $amount_to = 0.0)
204 if (!DolibarrApiAccess::$user->rights->banque->configurer) {
205 throw new RestException(401);
208 require_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
210 $accountfrom =
new Account($this->db);
211 $resultAccountFrom = $accountfrom->fetch($bankaccount_from_id);
213 if ($resultAccountFrom === 0) {
214 throw new RestException(404,
'The BankAccount for bankaccount_from_id provided does not exist.');
217 $accountto =
new Account($this->db);
218 $resultAccountTo = $accountto->fetch($bankaccount_to_id);
220 if ($resultAccountTo === 0) {
221 throw new RestException(404,
'The BankAccount for bankaccount_to_id provided does not exist.');
224 if ($accountto->currency_code == $accountfrom->currency_code) {
225 $amount_to = $amount;
227 if (!$amount_to || empty($amount_to)) {
228 throw new RestException(422,
'You must provide amount_to value since bankaccount_from and bankaccount_to does not share the same currency.');
232 if ($amount_to < 0) {
233 throw new RestException(422,
'You must provide a positive value for amount.');
236 if ($accountto->id == $accountfrom->id) {
237 throw new RestException(422,
'bankaccount_from_id and bankaccount_to_id must be different !');
243 $bank_line_id_from = 0;
244 $bank_line_id_to = 0;
246 $user = DolibarrApiAccess::$user;
259 $description =
sanitizeVal($description,
'alphanohtml');
267 $bank_line_id_from = $accountfrom->addline($date, $typefrom, $description, -1 *
price2num($amount),
'',
'', $user);
269 if (!($bank_line_id_from > 0)) {
274 $bank_line_id_to = $accountto->addline($date, $typeto, $description,
price2num($amount_to),
'',
'', $user);
276 if (!($bank_line_id_to > 0)) {
284 $url = DOL_URL_ROOT.
'/compta/bank/line.php?rowid=';
285 $label =
'(banktransfert)';
286 $type =
'banktransfert';
289 $result = $accountfrom->add_url_line($bank_line_id_from, $bank_line_id_to, $url, $label, $type);
291 if (!($result > 0)) {
296 $result = $accountto->add_url_line($bank_line_id_to, $bank_line_id_from, $url, $label, $type);
298 if (!($result > 0)) {
308 'message' =>
'Internal wire transfer created successfully.',
309 'bank_id_from' => $bank_line_id_from,
310 'bank_id_to' => $bank_line_id_to,
314 $this->db->rollback();
315 throw new RestException(500, $accountfrom->error.
' '.$accountto->error);
326 public function put($id, $request_data =
null)
328 if (!DolibarrApiAccess::$user->rights->banque->configurer) {
329 throw new RestException(401);
332 $account =
new Account($this->db);
333 $result = $account->fetch($id);
335 throw new RestException(404,
'account not found');
338 foreach ($request_data as $field => $value) {
339 if ($field ==
'id') {
342 if ($field ===
'caller') {
344 $account->context[
'caller'] = $request_data[
'caller'];
351 if ($account->update(DolibarrApiAccess::$user) > 0) {
352 return $this->
get($id);
354 throw new RestException(500, $account->error);
364 public function delete($id)
366 if (!DolibarrApiAccess::$user->rights->banque->configurer) {
367 throw new RestException(401);
369 $account =
new Account($this->db);
370 $result = $account->fetch($id);
372 throw new RestException(404,
'account not found');
375 if ($account->delete(DolibarrApiAccess::$user) < 0) {
376 throw new RestException(401,
'error when deleting account');
382 'message' =>
'account deleted'
399 if (!isset($data[$field])) {
400 throw new RestException(400,
"$field field missing");
402 $account[$field] = $data[$field];
417 $object = parent::_cleanObjectDatas($object);
419 unset($object->rowid);
439 if (!DolibarrApiAccess::$user->rights->banque->lire) {
440 throw new RestException(401);
443 $account =
new Account($this->db);
444 $result = $account->fetch($id);
446 throw new RestException(404,
'account not found');
449 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"bank ";
450 $sql .=
" WHERE fk_account = ".((int) $id);
457 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
461 $sql .=
" ORDER BY rowid";
463 $result = $this->db->query($sql);
466 $num = $this->db->num_rows($result);
467 for ($i = 0; $i < $num; $i++) {
468 $obj = $this->db->fetch_object($result);
470 if ($accountLine->fetch($obj->rowid) > 0) {
475 throw new RestException(503,
'Error when retrieving list of account lines: '.$this->db->lasterror());
500 public function addLine($id, $date, $type, $label, $amount, $category = 0, $cheque_number =
'', $cheque_writer =
'', $cheque_bank =
'', $accountancycode =
'', $datev =
null, $num_releve =
'')
502 if (!DolibarrApiAccess::$user->rights->banque->modifier) {
503 throw new RestException(401);
506 $account =
new Account($this->db);
507 $result = $account->fetch($id);
509 throw new RestException(404,
'account not found');
520 $result = $account->addline(
527 DolibarrApiAccess::$user,
535 throw new RestException(503,
'Error when adding line to account: '.$account->error);
553 public function addLink($id, $line_id, $url_id, $url, $label, $type)
555 if (!DolibarrApiAccess::$user->rights->banque->modifier) {
556 throw new RestException(401);
559 $account =
new Account($this->db);
560 $result = $account->fetch($id);
562 throw new RestException(404,
'account not found');
566 $result = $accountLine->fetch($line_id);
568 throw new RestException(404,
'account line not found');
575 $result = $account->add_url_line($line_id, $url_id, $url, $label, $type);
577 throw new RestException(503,
'Error when adding link to account line: '.$account->error);
598 if (!DolibarrApiAccess::$user->rights->banque->lire) {
599 throw new RestException(401);
602 $account =
new Account($this->db);
603 $result = $account->fetch($id);
605 throw new RestException(404,
'account not found');
607 $links = $account->get_url($line_id);
608 foreach ($links as &$link) {
609 unset($link[0], $link[1], $link[2], $link[3]);
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.
__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.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $category=0, $sqlfilters='', $properties='')
Get the list of accounts.
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.
static $FIELDS
array $FIELDS Mandatory fields, checked when creating an object
_cleanObjectDatas($object)
Clean sensible object datas.
_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.