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') {
338 if ($account->update(DolibarrApiAccess::$user) > 0) {
339 return $this->
get($id);
341 throw new RestException(500, $account->error);
351 public function delete($id)
353 if (!DolibarrApiAccess::$user->rights->banque->configurer) {
354 throw new RestException(401);
356 $account =
new Account($this->db);
357 $result = $account->fetch($id);
359 throw new RestException(404,
'account not found');
362 if ($account->delete(DolibarrApiAccess::$user) < 0) {
363 throw new RestException(401,
'error when deleting account');
369 'message' =>
'account deleted'
386 if (!isset($data[$field])) {
387 throw new RestException(400,
"$field field missing");
389 $account[$field] = $data[$field];
404 $object = parent::_cleanObjectDatas($object);
406 unset($object->rowid);
426 if (!DolibarrApiAccess::$user->rights->banque->lire) {
427 throw new RestException(401);
430 $account =
new Account($this->db);
431 $result = $account->fetch($id);
433 throw new RestException(404,
'account not found');
436 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"bank ";
437 $sql .=
" WHERE fk_account = ".((int) $id);
444 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
448 $sql .=
" ORDER BY rowid";
450 $result = $this->db->query($sql);
453 $num = $this->db->num_rows($result);
454 for ($i = 0; $i < $num; $i++) {
455 $obj = $this->db->fetch_object($result);
457 if ($accountLine->fetch($obj->rowid) > 0) {
462 throw new RestException(503,
'Error when retrieving list of account lines: '.$this->db->lasterror());
487 public function addLine($id, $date, $type, $label, $amount, $category = 0, $cheque_number =
'', $cheque_writer =
'', $cheque_bank =
'', $accountancycode =
'', $datev =
null, $num_releve =
'')
489 if (!DolibarrApiAccess::$user->rights->banque->modifier) {
490 throw new RestException(401);
493 $account =
new Account($this->db);
494 $result = $account->fetch($id);
496 throw new RestException(404,
'account not found');
507 $result = $account->addline(
514 DolibarrApiAccess::$user,
522 throw new RestException(503,
'Error when adding line to account: '.$account->error);
540 public function addLink($id, $line_id, $url_id, $url, $label, $type)
542 if (!DolibarrApiAccess::$user->rights->banque->modifier) {
543 throw new RestException(401);
546 $account =
new Account($this->db);
547 $result = $account->fetch($id);
549 throw new RestException(404,
'account not found');
553 $result = $accountLine->fetch($line_id);
555 throw new RestException(404,
'account line not found');
562 $result = $account->add_url_line($line_id, $url_id, $url, $label, $type);
564 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.