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'];
348 if ($field ==
'array_options' && is_array($value)) {
349 foreach ($value as $index => $val) {
350 $account->array_options[$index] = $this->
_checkValForAPI($field, $val, $account);
357 if ($account->update(DolibarrApiAccess::$user) > 0) {
358 return $this->
get($id);
360 throw new RestException(500, $account->error);
370 public function delete($id)
372 if (!DolibarrApiAccess::$user->rights->banque->configurer) {
373 throw new RestException(401);
375 $account =
new Account($this->db);
376 $result = $account->fetch($id);
378 throw new RestException(404,
'account not found');
381 if ($account->delete(DolibarrApiAccess::$user) < 0) {
382 throw new RestException(401,
'error when deleting account');
388 'message' =>
'account deleted'
405 if (!isset($data[$field])) {
406 throw new RestException(400,
"$field field missing");
408 $account[$field] = $data[$field];
423 $object = parent::_cleanObjectDatas($object);
425 unset($object->rowid);
445 if (!DolibarrApiAccess::$user->rights->banque->lire) {
446 throw new RestException(401);
449 $account =
new Account($this->db);
450 $result = $account->fetch($id);
452 throw new RestException(404,
'account not found');
455 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"bank ";
456 $sql .=
" WHERE fk_account = ".((int) $id);
463 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
467 $sql .=
" ORDER BY rowid";
469 $result = $this->db->query($sql);
472 $num = $this->db->num_rows($result);
473 for ($i = 0; $i < $num; $i++) {
474 $obj = $this->db->fetch_object($result);
476 if ($accountLine->fetch($obj->rowid) > 0) {
481 throw new RestException(503,
'Error when retrieving list of account lines: '.$this->db->lasterror());
506 public function addLine($id, $date, $type, $label, $amount, $category = 0, $cheque_number =
'', $cheque_writer =
'', $cheque_bank =
'', $accountancycode =
'', $datev =
null, $num_releve =
'')
508 if (!DolibarrApiAccess::$user->rights->banque->modifier) {
509 throw new RestException(401);
512 $account =
new Account($this->db);
513 $result = $account->fetch($id);
515 throw new RestException(404,
'account not found');
526 $result = $account->addline(
533 DolibarrApiAccess::$user,
541 throw new RestException(503,
'Error when adding line to account: '.$account->error);
559 public function addLink($id, $line_id, $url_id, $url, $label, $type)
561 if (!DolibarrApiAccess::$user->rights->banque->modifier) {
562 throw new RestException(401);
565 $account =
new Account($this->db);
566 $result = $account->fetch($id);
568 throw new RestException(404,
'account not found');
572 $result = $accountLine->fetch($line_id);
574 throw new RestException(404,
'account line not found');
581 $result = $account->add_url_line($line_id, $url_id, $url, $label, $type);
583 throw new RestException(503,
'Error when adding link to account line: '.$account->error);
604 if (!DolibarrApiAccess::$user->rights->banque->lire) {
605 throw new RestException(401);
608 $account =
new Account($this->db);
609 $result = $account->fetch($id);
611 throw new RestException(404,
'account not found');
613 $links = $account->get_url($line_id);
614 foreach ($links as &$link) {
615 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.