dolibarr  7.0.0-beta
api_bankaccounts.class.php
1 <?php
2 /*
3  * Copyright (C) 2016 Xebax Christy <xebax@wanadoo.fr>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 use Luracast\Restler\RestException;
20 
21 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
22 
30 {
31 
35  static $FIELDS = array(
36  'ref',
37  'label',
38  'type',
39  'currency_code',
40  'country_id'
41  );
42 
46  function __construct()
47  {
48  global $db;
49  $this->db = $db;
50  }
51 
64  function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '')
65  {
66  $list = array();
67 
68  if(! DolibarrApiAccess::$user->rights->banque->lire) {
69  throw new RestException(401);
70  }
71 
72  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."bank_account as t";
73  $sql.= ' WHERE t.entity IN ('.getEntity('bank_account').')';
74  // Add sql filters
75  if ($sqlfilters)
76  {
77  if (! DolibarrApi::_checkFilters($sqlfilters))
78  {
79  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
80  }
81  $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
82  $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
83  }
84 
85  $sql.= $this->db->order($sortfield, $sortorder);
86  if ($limit) {
87  if ($page < 0)
88  {
89  $page = 0;
90  }
91  $offset = $limit * $page;
92 
93  $sql.= $this->db->plimit($limit + 1, $offset);
94  }
95 
96  dol_syslog("API Rest request");
97  $result = $this->db->query($sql);
98 
99  if ($result) {
100  $num = $this->db->num_rows($result);
101  $min = min($num, ($limit <= 0 ? $num : $limit));
102  for ($i = 0; $i < $min; $i++) {
103  $obj = $this->db->fetch_object($result);
104  $account = new Account($this->db);
105  if ($account->fetch($obj->rowid) > 0) {
106  $list[] = $this->_cleanObjectDatas($account);
107  }
108  }
109  } else {
110  throw new RestException(503, 'Error when retrieving list of accounts: ' . $this->db->lasterror());
111  }
112 
113  return $list;
114  }
115 
124  function get($id)
125  {
126  if (! DolibarrApiAccess::$user->rights->banque->lire) {
127  throw new RestException(401);
128  }
129 
130  $account = new Account($this->db);
131  $result = $account->fetch($id);
132  if (! $result) {
133  throw new RestException(404, 'account not found');
134  }
135 
136  return $this->_cleanObjectDatas($account);
137  }
138 
145  function post($request_data = null)
146  {
147  if (! DolibarrApiAccess::$user->rights->banque->configurer) {
148  throw new RestException(401);
149  }
150  // Check mandatory fields
151  $result = $this->_validate($request_data);
152 
153  $account = new Account($this->db);
154  foreach ($request_data as $field => $value) {
155  $account->$field = $value;
156  }
157  // Date of the initial balance (required to create an account).
158  $account->date_solde = time();
159  // courant and type are the same thing but the one used when
160  // creating an account is courant
161  $account->courant = $account->type;
162 
163  if ($account->create(DolibarrApiAccess::$user) < 0) {
164  throw new RestException(500, 'Error creating bank account', array_merge(array($account->error), $account->errors));
165  }
166  return $account->id;
167  }
168 
176  function put($id, $request_data = null)
177  {
178  if (! DolibarrApiAccess::$user->rights->banque->configurer) {
179  throw new RestException(401);
180  }
181 
182  $account = new Account($this->db);
183  $result = $account->fetch($id);
184  if (! $result) {
185  throw new RestException(404, 'account not found');
186  }
187 
188  foreach ($request_data as $field => $value) {
189  if ($field == 'id') continue;
190  $account->$field = $value;
191  }
192 
193  if ($account->update(DolibarrApiAccess::$user) > 0)
194  {
195  return $this->get($id);
196  }
197  else
198  {
199  throw new RestException(500, $account->error);
200  }
201  }
202 
209  function delete($id)
210  {
211  if (! DolibarrApiAccess::$user->rights->banque->configurer) {
212  throw new RestException(401);
213  }
214  $account = new Account($this->db);
215  $result = $account->fetch($id);
216  if (! $result) {
217  throw new RestException(404, 'account not found');
218  }
219 
220  if ($account->delete(DolibarrApiAccess::$user) < 0) {
221  throw new RestException(401, 'error when deleting account');
222  }
223 
224  return array(
225  'success' => array(
226  'code' => 200,
227  'message' => 'account deleted'
228  )
229  );
230  }
231 
240  function _validate($data)
241  {
242  $account = array();
243  foreach (BankAccounts::$FIELDS as $field) {
244  if (! isset($data[$field]))
245  throw new RestException(400, "$field field missing");
246  $account[$field] = $data[$field];
247  }
248  return $account;
249  }
250 
257  function _cleanObjectDatas($object)
258  {
259  $object = parent::_cleanObjectDatas($object);
260 
261  unset($object->rowid);
262 
263  return $object;
264  }
265 
276  function getLines($id)
277  {
278  $list = array();
279 
280  if (! DolibarrApiAccess::$user->rights->banque->lire) {
281  throw new RestException(401);
282  }
283 
284  $account = new Account($this->db);
285  $result = $account->fetch($id);
286  if (! $result) {
287  throw new RestException(404, 'account not found');
288  }
289 
290  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."bank ";
291  $sql .= " WHERE fk_account = ".$id;
292  $sql .= " ORDER BY rowid";
293 
294  $result = $this->db->query($sql);
295 
296  if ($result) {
297  $num = $this->db->num_rows($result);
298  for ($i = 0; $i < $num; $i++) {
299  $obj = $this->db->fetch_object($result);
300  $accountLine = new AccountLine($this->db);
301  if ($accountLine->fetch($obj->rowid) > 0) {
302  $list[] = $this->_cleanObjectDatas($accountLine);
303  }
304  }
305  } else {
306  throw new RestException(503, 'Error when retrieving list of account lines: ' . $accountLine->error);
307  }
308 
309  return $list;
310  }
311 
328  function addLine($id, $date, $type, $label, $amount, $category=0, $cheque_number='', $cheque_writer='', $cheque_bank='')
329  {
330  if (! DolibarrApiAccess::$user->rights->banque->modifier) {
331  throw new RestException(401);
332  }
333 
334  $account = new Account($this->db);
335  $result = $account->fetch($id);
336  if (! $result) {
337  throw new RestException(404, 'account not found');
338  }
339 
340  $result = $account->addline($date, $type, $label, $amount, $cheque_number, $category,
341  DolibarrApiAccess::$user, $cheque_writer, $cheque_bank);
342  if ($result < 0) {
343  throw new RestException(503, 'Error when adding line to account: ' . $account->error);
344  }
345  return $result;
346  }
347 
361  function addLink($id, $line_id, $url_id, $url, $label, $type)
362  {
363  if (! DolibarrApiAccess::$user->rights->banque->modifier) {
364  throw new RestException(401);
365  }
366 
367  $account = new Account($this->db);
368  $result = $account->fetch($id);
369  if (! $result) {
370  throw new RestException(404, 'account not found');
371  }
372 
373  $accountLine = new AccountLine($this->db);
374  $result = $accountLine->fetch($line_id);
375  if (! $result) {
376  throw new RestException(404, 'account line not found');
377  }
378 
379  $result = $account->add_url_line($line_id, $url_id, $url, $label, $type);
380  if ($result < 0) {
381  throw new RestException(503, 'Error when adding link to account line: ' . $account->error);
382  }
383  return $result;
384  }
385 }
post($request_data=null)
Create account object.
getLines($id)
Get the list of lines of the account.
addLink($id, $line_id, $url_id, $url, $label, $type)
Add a link to an account line.
_validate($data)
Validate fields before creating an object.
addLine($id, $date, $type, $label, $amount, $category=0, $cheque_number='', $cheque_writer='', $cheque_bank='')
Add a line to an account.
put($id, $request_data=null)
Update account.
__construct()
Constructor.
Class to manage bank transaction lines.
Class to manage bank accounts.
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:246
Class for API REST v1.
Definition: api.class.php:29
static $FIELDS
array $FIELDS Mandatory fields, checked when creating an object
index($sortfield="t.rowid", $sortorder= 'ASC', $limit=100, $page=0, $sqlfilters= '')
Get the list of accounts.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
_cleanObjectDatas($object)
Clean sensible object datas.