21use Luracast\Restler\RestException;
23require_once DOL_DOCUMENT_ROOT .
'/compta/bank/class/account.class.php';
68 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $category = 0, $sqlfilters =
'', $properties =
'')
72 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'lire')) {
73 throw new RestException(403);
76 $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)";
78 $sql .=
", " . MAIN_DB_PREFIX .
"categorie_account as c";
80 $sql .=
' WHERE t.entity IN (' .
getEntity(
'bank_account') .
')';
83 $sql .=
" AND c.fk_categorie = " . ((int) $category) .
" AND c.fk_account = t.rowid";
90 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
94 $sql .= $this->db->order($sortfield, $sortorder);
99 $offset = $limit * $page;
101 $sql .= $this->db->plimit($limit + 1, $offset);
105 $result = $this->db->query($sql);
108 $num = $this->db->num_rows($result);
109 $min = min($num, ($limit <= 0 ? $num : $limit));
110 for ($i = 0; $i < $min; $i++) {
111 $obj = $this->db->fetch_object($result);
112 $account =
new Account($this->db);
113 if ($account->fetch($obj->rowid) > 0) {
118 throw new RestException(503,
'Error when retrieving list of accounts: ' . $this->db->lasterror());
132 public function get($id)
134 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'lire')) {
135 throw new RestException(403);
138 $account =
new Account($this->db);
139 $result = $account->fetch($id);
141 throw new RestException(404,
'account not found');
153 public function post($request_data =
null)
155 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'configurer')) {
156 throw new RestException(403);
159 $result = $this->
_validate($request_data);
161 $account =
new Account($this->db);
162 foreach ($request_data as $field => $value) {
163 if ($field ===
'caller') {
165 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
172 $account->date_solde = time();
175 $account->courant = $account->type;
177 if ($account->create(DolibarrApiAccess::$user) < 0) {
178 throw new RestException(500,
'Error creating bank account', array_merge(array($account->error), $account->errors));
205 public function transfer($bankaccount_from_id = 0, $bankaccount_to_id = 0, $date =
null, $description =
"", $amount = 0.0, $amount_to = 0.0, $cheque_number =
"")
207 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'configurer')) {
208 throw new RestException(403);
211 require_once DOL_DOCUMENT_ROOT .
'/compta/bank/class/account.class.php';
213 $accountfrom =
new Account($this->db);
214 $resultAccountFrom = $accountfrom->fetch($bankaccount_from_id);
216 if ($resultAccountFrom === 0) {
217 throw new RestException(404,
'The BankAccount for bankaccount_from_id provided does not exist.');
220 $accountto =
new Account($this->db);
221 $resultAccountTo = $accountto->fetch($bankaccount_to_id);
223 if ($resultAccountTo === 0) {
224 throw new RestException(404,
'The BankAccount for bankaccount_to_id provided does not exist.');
227 if ($accountto->currency_code == $accountfrom->currency_code) {
228 $amount_to = $amount;
230 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.');
235 if ($amount_to < 0) {
236 throw new RestException(422,
'You must provide a positive value for amount.');
239 if ($accountto->id == $accountfrom->id) {
240 throw new RestException(422,
'bankaccount_from_id and bankaccount_to_id must be different !');
246 $bank_line_id_from = 0;
247 $bank_line_id_to = 0;
249 $user = DolibarrApiAccess::$user;
262 $description =
sanitizeVal($description,
'alphanohtml');
263 $cheque_number =
sanitizeVal($cheque_number,
'alphanohtml');
270 $bank_line_id_from = $accountfrom->addline($date, $typefrom, $description, -1 * (
float)
price2num($amount),
'',
'', $user, $cheque_number);
272 if (!($bank_line_id_from > 0)) {
277 $bank_line_id_to = $accountto->addline($date, $typeto, $description,
price2num($amount_to),
'',
'', $user, $cheque_number);
279 if (!($bank_line_id_to > 0)) {
287 $url = DOL_URL_ROOT .
'/compta/bank/line.php?rowid=';
288 $label =
'(banktransfert)';
289 $type =
'banktransfert';
292 $result = $accountfrom->add_url_line($bank_line_id_from, $bank_line_id_to, $url, $label, $type);
294 if (!($result > 0)) {
299 $result = $accountto->add_url_line($bank_line_id_to, $bank_line_id_from, $url, $label, $type);
301 if (!($result > 0)) {
311 'message' =>
'Internal wire transfer created successfully.',
312 'bank_id_from' => $bank_line_id_from,
313 'bank_id_to' => $bank_line_id_to,
317 $this->db->rollback();
318 throw new RestException(500, $accountfrom->error .
' ' . $accountto->error);
329 public function put($id, $request_data =
null)
331 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'configurer')) {
332 throw new RestException(403);
335 $account =
new Account($this->db);
336 $result = $account->fetch($id);
338 throw new RestException(404,
'account not found');
341 foreach ($request_data as $field => $value) {
342 if ($field ==
'id') {
345 if ($field ===
'caller') {
347 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
354 if ($account->update(DolibarrApiAccess::$user) > 0) {
355 return $this->
get($id);
357 throw new RestException(500, $account->error);
367 public function delete($id)
369 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'configurer')) {
370 throw new RestException(403);
372 $account =
new Account($this->db);
373 $result = $account->fetch($id);
375 throw new RestException(404,
'account not found');
378 if ($account->delete(DolibarrApiAccess::$user) < 0) {
379 throw new RestException(500,
'error when deleting account');
385 'message' =>
'account deleted'
402 if (!isset($data[$field])) {
403 throw new RestException(400,
"$field field missing");
405 $account[$field] = $data[$field];
442 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'lire')) {
443 throw new RestException(403);
446 $account =
new Account($this->db);
447 $result = $account->fetch($id);
449 throw new RestException(404,
'account not found');
452 $sql =
"SELECT rowid FROM " . MAIN_DB_PREFIX .
"bank ";
453 $sql .=
" WHERE fk_account = " . ((int) $id);
460 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
464 $sql .=
" ORDER BY rowid";
466 $result = $this->db->query($sql);
469 $num = $this->db->num_rows($result);
470 for ($i = 0; $i < $num; $i++) {
471 $obj = $this->db->fetch_object($result);
473 if ($accountLine->fetch($obj->rowid) > 0) {
478 throw new RestException(503,
'Error when retrieving list of account lines: ' . $this->db->lasterror());
503 public function addLine($id, $date, $type, $label, $amount, $category = 0, $cheque_number =
'', $cheque_writer =
'', $cheque_bank =
'', $accountancycode =
'', $datev =
null, $num_releve =
'')
505 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'modifier')) {
506 throw new RestException(403);
509 $account =
new Account($this->db);
510 $result = $account->fetch($id);
512 throw new RestException(404,
'account not found');
523 $result = $account->addline(
530 DolibarrApiAccess::$user,
538 throw new RestException(503,
'Error when adding line to account: ' . $account->error);
556 public function addLink($id, $line_id, $url_id, $url, $label, $type)
558 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'modifier')) {
559 throw new RestException(403);
562 $account =
new Account($this->db);
563 $result = $account->fetch($id);
565 throw new RestException(404,
'account not found');
569 $result = $accountLine->fetch($line_id);
571 throw new RestException(404,
'account line not found');
578 $result = $account->add_url_line($line_id, $url_id, $url, $label, $type);
580 throw new RestException(503,
'Error when adding link to account line: ' . $account->error);
601 if (!DolibarrApiAccess::$user->hasRight(
'banque',
'lire')) {
602 throw new RestException(403);
605 $account =
new Account($this->db);
606 $result = $account->fetch($id);
608 throw new RestException(404,
'account not found');
611 $links = $account->get_url($line_id);
612 foreach ($links as &$link) {
613 unset($link[0], $link[1], $link[2], $link[3]);
631 if (!DolibarrApiAccess::$user->rights->banque->modifier) {
632 throw new RestException(403);
635 $account =
new Account($this->db);
636 $result = $account->fetch($id);
638 throw new RestException(404,
'account not found');
642 $result = $accountLine->fetch($line_id);
644 throw new RestException(404,
'account line not found');
649 $result = $accountLine->updateLabel();
651 throw new RestException(503,
'Error when updating link to account line: ' . $accountLine->error);
653 return $accountLine->id;
667 if (!DolibarrApiAccess::$user->rights->banque->modifier) {
668 throw new RestException(403);
671 $account =
new Account($this->db);
672 $result = $account->fetch($id);
674 throw new RestException(404,
'account not found');
678 $result = $accountLine->fetch($line_id);
680 throw new RestException(404,
'account line not found');
683 if ($accountLine->delete(DolibarrApiAccess::$user) < 0) {
684 throw new RestException(500,
'error when deleting account line');
690 'message' =>
"account line $line_id deleted"
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
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.
transfer($bankaccount_from_id=0, $bankaccount_to_id=0, $date=null, $description="", $amount=0.0, $amount_to=0.0, $cheque_number="")
Create an internal wire transfer between two bank accounts.
__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.
getLines($id, $sqlfilters='')
Get the list of lines of the account.
deleteLine($id, $line_id)
Delete an account line.
static $FIELDS
array $FIELDS Mandatory fields, checked when creating an object
_cleanObjectDatas($object)
Clean sensible object datas.
updateLine($id, $line_id, $label)
Update an account line.
_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.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.