19 use Luracast\Restler\RestException;
21 require_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 =
'')
70 if (!DolibarrApiAccess::$user->rights->banque->lire) {
71 throw new RestException(401);
74 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"bank_account as t";
76 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_account as c";
78 $sql .=
' WHERE t.entity IN ('.getEntity(
'bank_account').
')';
81 $sql .=
" AND c.fk_categorie = ".$this->db->escape($category).
" AND c.fk_account = t.rowid ";
88 throw new RestException(503,
'Error when validating parameter sqlfilters '.$sqlfilters);
90 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
91 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
94 $sql .= $this->
db->order($sortfield, $sortorder);
100 $offset = $limit * $page;
102 $sql .= $this->
db->plimit($limit + 1, $offset);
106 $result = $this->
db->query($sql);
109 $num = $this->
db->num_rows($result);
110 $min = min($num, ($limit <= 0 ? $num : $limit));
111 for ($i = 0; $i < $min; $i++) {
112 $obj = $this->
db->fetch_object($result);
114 if ($account->fetch($obj->rowid) > 0) {
119 throw new RestException(503,
'Error when retrieving list of accounts: '.$this->
db->lasterror());
133 public function get($id)
135 if (!DolibarrApiAccess::$user->rights->banque->lire) {
136 throw new RestException(401);
140 $result = $account->fetch($id);
142 throw new RestException(404,
'account not found');
154 public function post($request_data = null)
156 if (!DolibarrApiAccess::$user->rights->banque->configurer) {
157 throw new RestException(401);
160 $result = $this->
_validate($request_data);
163 foreach ($request_data as $field => $value) {
164 $account->$field = $value;
167 $account->date_solde = time();
170 $account->courant = $account->type;
172 if ($account->create(DolibarrApiAccess::$user) < 0) {
173 throw new RestException(500,
'Error creating bank account', array_merge(array($account->error), $account->errors));
199 public function transfer($bankaccount_from_id = 0, $bankaccount_to_id = 0, $date = null, $description =
"", $amount = 0.0, $amount_to = 0.0)
201 if (!DolibarrApiAccess::$user->rights->banque->configurer) {
202 throw new RestException(401);
205 if ($bankaccount_from_id === $bankaccount_to_id) {
206 throw new RestException(422,
'bankaccount_from_id and bankaccount_to_id must be different !');
209 require_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
212 $resultAccountFrom = $accountfrom->fetch($bankaccount_from_id);
214 if ($resultAccountFrom === 0) {
215 throw new RestException(404,
'The BankAccount for bankaccount_from_id provided does not exist.');
219 $resultAccountTo = $accountto->fetch($bankaccount_to_id);
221 if ($resultAccountTo === 0) {
222 throw new RestException(404,
'The BankAccount for bankaccount_to_id provided does not exist.');
225 if ($accountto->currency_code == $accountfrom->currency_code)
227 $amount_to = $amount;
229 if (!$amount_to || empty($amount_to))
231 throw new RestException(422,
'You must provide amount_to value since bankaccount_from and bankaccount_to does not share the same currency.');
238 $bank_line_id_from = 0;
239 $bank_line_id_to = 0;
241 $user = DolibarrApiAccess::$user;
259 $bank_line_id_from = $accountfrom->addline($date, $typefrom, $description, -1 *
price2num($amount),
'',
'', $user);
261 if (!($bank_line_id_from > 0)) {
266 $bank_line_id_to = $accountto->addline($date, $typeto, $description,
price2num($amount_to),
'',
'', $user);
268 if (!($bank_line_id_to > 0)) {
276 $url = DOL_URL_ROOT.
'/compta/bank/line.php?rowid=';
277 $label =
'(banktransfert)';
278 $type =
'banktransfert';
281 $result = $accountfrom->add_url_line($bank_line_id_from, $bank_line_id_to, $url, $label, $type);
283 if (!($result > 0)) {
288 $result = $accountto->add_url_line($bank_line_id_to, $bank_line_id_from, $url, $label, $type);
290 if (!($result > 0)) {
301 'message' =>
'Internal wire transfer created successfully.'
305 $this->
db->rollback();
306 throw new RestException(500, $accountfrom->error.
' '.$accountto->error);
317 public function put($id, $request_data = null)
319 if (!DolibarrApiAccess::$user->rights->banque->configurer) {
320 throw new RestException(401);
324 $result = $account->fetch($id);
326 throw new RestException(404,
'account not found');
329 foreach ($request_data as $field => $value) {
330 if ($field ==
'id')
continue;
331 $account->$field = $value;
334 if ($account->update(DolibarrApiAccess::$user) > 0)
336 return $this->
get($id);
338 throw new RestException(500, $account->error);
348 public function delete($id)
350 if (!DolibarrApiAccess::$user->rights->banque->configurer) {
351 throw new RestException(401);
354 $result = $account->fetch($id);
356 throw new RestException(404,
'account not found');
359 if ($account->delete(DolibarrApiAccess::$user) < 0) {
360 throw new RestException(401,
'error when deleting account');
366 'message' =>
'account deleted'
383 if (!isset($data[$field]))
384 throw new RestException(400,
"$field field missing");
385 $account[$field] = $data[$field];
400 $object = parent::_cleanObjectDatas($object);
402 unset($object->rowid);
422 if (!DolibarrApiAccess::$user->rights->banque->lire) {
423 throw new RestException(401);
427 $result = $account->fetch($id);
429 throw new RestException(404,
'account not found');
432 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"bank ";
433 $sql .=
" WHERE fk_account = ".$id;
440 throw new RestException(503,
'Error when validating parameter sqlfilters '.$sqlfilters);
442 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
443 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
446 $sql .=
" ORDER BY rowid";
448 $result = $this->
db->query($sql);
451 $num = $this->
db->num_rows($result);
452 for ($i = 0; $i < $num; $i++) {
453 $obj = $this->
db->fetch_object($result);
455 if ($accountLine->fetch($obj->rowid) > 0) {
460 throw new RestException(503,
'Error when retrieving list of account lines: '.$accountLine->error);
482 public function addLine($id, $date, $type, $label, $amount, $category = 0, $cheque_number =
'', $cheque_writer =
'', $cheque_bank =
'')
484 if (!DolibarrApiAccess::$user->rights->banque->modifier) {
485 throw new RestException(401);
489 $result = $account->fetch($id);
491 throw new RestException(404,
'account not found');
494 $result = $account->addline(
501 DolibarrApiAccess::$user,
502 $cheque_writer, $cheque_bank
505 throw new RestException(503,
'Error when adding line to account: '.$account->error);
523 public function addLink($id, $line_id, $url_id, $url, $label, $type)
525 if (!DolibarrApiAccess::$user->rights->banque->modifier) {
526 throw new RestException(401);
530 $result = $account->fetch($id);
532 throw new RestException(404,
'account not found');
536 $result = $accountLine->fetch($line_id);
538 throw new RestException(404,
'account line not found');
541 $result = $account->add_url_line($line_id, $url_id, $url, $label, $type);
543 throw new RestException(503,
'Error when adding link to account line: '.$account->error);
addLine($id, $date, $type, $label, $amount, $category=0, $cheque_number= '', $cheque_writer= '', $cheque_bank= '')
Add a line to an account.
post($request_data=null)
Create account object.
addLink($id, $line_id, $url_id, $url, $label, $type)
Add a link to an account line.
_validate($data)
Validate fields before creating an object.
put($id, $request_data=null)
Update account.
__construct()
Constructor.
Class to manage bank transaction lines.
$conf db
API class for accounts.
Class to manage bank accounts.
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
static $FIELDS
array $FIELDS Mandatory fields, checked when creating an object
getLines($id, $sqlfilters= '')
Get the list of lines of the account.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is '...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
const TYPE_CASH
Cash account.
index($sortfield="t.rowid", $sortorder= 'ASC', $limit=100, $page=0, $category=0, $sqlfilters= '')
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.
_cleanObjectDatas($object)
Clean sensible object datas.