19use Luracast\Restler\RestException;
21require_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
65 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $category = 0, $sqlfilters =
'')
69 if (!DolibarrApiAccess::$user->rights->banque->lire) {
70 throw new RestException(401);
73 $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)";
75 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_account as c";
77 $sql .=
' WHERE t.entity IN ('.getEntity(
'bank_account').
')';
80 $sql .=
" AND c.fk_categorie = ".((int) $category).
" AND c.fk_account = t.rowid";
87 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
91 $sql .= $this->db->order($sortfield, $sortorder);
96 $offset = $limit * $page;
98 $sql .= $this->db->plimit($limit + 1, $offset);
102 $result = $this->db->query($sql);
105 $num = $this->db->num_rows($result);
106 $min = min($num, ($limit <= 0 ? $num : $limit));
107 for ($i = 0; $i < $min; $i++) {
108 $obj = $this->db->fetch_object($result);
109 $account =
new Account($this->db);
110 if ($account->fetch($obj->rowid) > 0) {
115 throw new RestException(503,
'Error when retrieving list of accounts: '.$this->db->lasterror());
129 public function get($id)
131 if (!DolibarrApiAccess::$user->rights->banque->lire) {
132 throw new RestException(401);
135 $account =
new Account($this->db);
136 $result = $account->fetch($id);
138 throw new RestException(404,
'account not found');
150 public function post($request_data =
null)
152 if (!DolibarrApiAccess::$user->rights->banque->configurer) {
153 throw new RestException(401);
156 $result = $this->
_validate($request_data);
158 $account =
new Account($this->db);
159 foreach ($request_data as $field => $value) {
163 $account->date_solde = time();
166 $account->courant = $account->type;
168 if ($account->create(DolibarrApiAccess::$user) < 0) {
169 throw new RestException(500,
'Error creating bank account', array_merge(array($account->error), $account->errors));
195 public function transfer($bankaccount_from_id = 0, $bankaccount_to_id = 0, $date =
null, $description =
"", $amount = 0.0, $amount_to = 0.0)
197 if (!DolibarrApiAccess::$user->rights->banque->configurer) {
198 throw new RestException(401);
201 require_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
203 $accountfrom =
new Account($this->db);
204 $resultAccountFrom = $accountfrom->fetch($bankaccount_from_id);
206 if ($resultAccountFrom === 0) {
207 throw new RestException(404,
'The BankAccount for bankaccount_from_id provided does not exist.');
210 $accountto =
new Account($this->db);
211 $resultAccountTo = $accountto->fetch($bankaccount_to_id);
213 if ($resultAccountTo === 0) {
214 throw new RestException(404,
'The BankAccount for bankaccount_to_id provided does not exist.');
217 if ($accountto->currency_code == $accountfrom->currency_code) {
218 $amount_to = $amount;
220 if (!$amount_to || empty($amount_to)) {
221 throw new RestException(422,
'You must provide amount_to value since bankaccount_from and bankaccount_to does not share the same currency.');
225 if ($amount_to < 0) {
226 throw new RestException(422,
'You must provide a positive value for amount.');
229 if ($accountto->id == $accountfrom->id) {
230 throw new RestException(422,
'bankaccount_from_id and bankaccount_to_id must be different !');
236 $bank_line_id_from = 0;
237 $bank_line_id_to = 0;
239 $user = DolibarrApiAccess::$user;
252 $description =
sanitizeVal($description,
'alphanohtml');
260 $bank_line_id_from = $accountfrom->addline($date, $typefrom, $description, -1 *
price2num($amount),
'',
'', $user);
262 if (!($bank_line_id_from > 0)) {
267 $bank_line_id_to = $accountto->addline($date, $typeto, $description,
price2num($amount_to),
'',
'', $user);
269 if (!($bank_line_id_to > 0)) {
277 $url = DOL_URL_ROOT.
'/compta/bank/line.php?rowid=';
278 $label =
'(banktransfert)';
279 $type =
'banktransfert';
282 $result = $accountfrom->add_url_line($bank_line_id_from, $bank_line_id_to, $url, $label, $type);
284 if (!($result > 0)) {
289 $result = $accountto->add_url_line($bank_line_id_to, $bank_line_id_from, $url, $label, $type);
291 if (!($result > 0)) {
301 'message' =>
'Internal wire transfer created successfully.',
302 'bank_id_from' => $bank_line_id_from,
303 'bank_id_to' => $bank_line_id_to,
307 $this->db->rollback();
308 throw new RestException(500, $accountfrom->error.
' '.$accountto->error);
319 public function put($id, $request_data =
null)
321 if (!DolibarrApiAccess::$user->rights->banque->configurer) {
322 throw new RestException(401);
325 $account =
new Account($this->db);
326 $result = $account->fetch($id);
328 throw new RestException(404,
'account not found');
331 foreach ($request_data as $field => $value) {
332 if ($field ==
'id') {
335 if ($field ==
'array_options' && is_array($value)) {
336 foreach ($value as $index => $val) {
337 $account->array_options[$index] = $this->
_checkValForAPI($field, $val, $account);
344 if ($account->update(DolibarrApiAccess::$user) > 0) {
345 return $this->
get($id);
347 throw new RestException(500, $account->error);
357 public function delete($id)
359 if (!DolibarrApiAccess::$user->rights->banque->configurer) {
360 throw new RestException(401);
362 $account =
new Account($this->db);
363 $result = $account->fetch($id);
365 throw new RestException(404,
'account not found');
368 if ($account->delete(DolibarrApiAccess::$user) < 0) {
369 throw new RestException(401,
'error when deleting account');
375 'message' =>
'account deleted'
392 if (!isset($data[$field])) {
393 throw new RestException(400,
"$field field missing");
395 $account[$field] = $data[$field];
410 $object = parent::_cleanObjectDatas($object);
412 unset($object->rowid);
432 if (!DolibarrApiAccess::$user->rights->banque->lire) {
433 throw new RestException(401);
436 $account =
new Account($this->db);
437 $result = $account->fetch($id);
439 throw new RestException(404,
'account not found');
442 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"bank ";
443 $sql .=
" WHERE fk_account = ".((int) $id);
450 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
454 $sql .=
" ORDER BY rowid";
456 $result = $this->db->query($sql);
459 $num = $this->db->num_rows($result);
460 for ($i = 0; $i < $num; $i++) {
461 $obj = $this->db->fetch_object($result);
463 if ($accountLine->fetch($obj->rowid) > 0) {
468 throw new RestException(503,
'Error when retrieving list of account lines: '.$this->db->lasterror());
493 public function addLine($id, $date, $type, $label, $amount, $category = 0, $cheque_number =
'', $cheque_writer =
'', $cheque_bank =
'', $accountancycode =
'', $datev =
null, $num_releve =
'')
495 if (!DolibarrApiAccess::$user->rights->banque->modifier) {
496 throw new RestException(401);
499 $account =
new Account($this->db);
500 $result = $account->fetch($id);
502 throw new RestException(404,
'account not found');
513 $result = $account->addline(
520 DolibarrApiAccess::$user,
528 throw new RestException(503,
'Error when adding line to account: '.$account->error);
546 public function addLink($id, $line_id, $url_id, $url, $label, $type)
548 if (!DolibarrApiAccess::$user->rights->banque->modifier) {
549 throw new RestException(401);
552 $account =
new Account($this->db);
553 $result = $account->fetch($id);
555 throw new RestException(404,
'account not found');
559 $result = $accountLine->fetch($line_id);
561 throw new RestException(404,
'account line not found');
568 $result = $account->add_url_line($line_id, $url_id, $url, $label, $type);
570 throw new RestException(503,
'Error when adding link to account line: '.$account->error);
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.
__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.
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.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $category=0, $sqlfilters='')
Get the list of accounts.
static $FIELDS
array $FIELDS Mandatory fields, checked when creating an object
_cleanObjectDatas($object)
Clean sensible object datas.
_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.