|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Copyright (C) 2016 Xebax Christy <[email protected]> |
|
5
|
|
|
* Copyright (C) 2024 MDW <[email protected]> |
|
6
|
|
|
* Copyright (C) 2024 Frédéric France <[email protected]> |
|
7
|
|
|
* Copyright (C) 2024 Rafael San José <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* This program is free software; you can redistribute it and/or modify |
|
10
|
|
|
* it under the terms of the GNU General Public License as published by |
|
11
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
12
|
|
|
* (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU General Public License |
|
20
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
21
|
|
|
*/ |
|
22
|
|
|
|
|
23
|
|
|
namespace Dolibarr\Code\ComptaBank\Api; |
|
24
|
|
|
|
|
25
|
|
|
use Dolibarr\Code\Api\Classes\DolibarrApiAccess; |
|
26
|
|
|
use Dolibarr\Code\Compta\Classes\Account; |
|
27
|
|
|
use Dolibarr\Core\Base\DolibarrApi; |
|
28
|
|
|
use Luracast\Restler\RestException; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* API class for accounts |
|
32
|
|
|
* |
|
33
|
|
|
* @property DoliDB $db |
|
34
|
|
|
* @access protected |
|
35
|
|
|
* @class DolibarrApiAccess {@requires user,external} |
|
36
|
|
|
*/ |
|
37
|
|
|
class BankAccounts extends DolibarrApi |
|
38
|
|
|
{ |
|
39
|
|
|
/** |
|
40
|
|
|
* array $FIELDS Mandatory fields, checked when creating an object |
|
41
|
|
|
*/ |
|
42
|
|
|
public static $FIELDS = array( |
|
43
|
|
|
'ref', |
|
44
|
|
|
'label', |
|
45
|
|
|
'type', |
|
46
|
|
|
'currency_code', |
|
47
|
|
|
'country_id' |
|
48
|
|
|
); |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Constructor |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct() |
|
54
|
|
|
{ |
|
55
|
|
|
global $db; |
|
56
|
|
|
$this->db = $db; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Get the list of accounts. |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $sortfield Sort field |
|
63
|
|
|
* @param string $sortorder Sort order |
|
64
|
|
|
* @param int $limit Limit for list |
|
65
|
|
|
* @param int $page Page number |
|
66
|
|
|
* @param int $category Use this param to filter list by category |
|
67
|
|
|
* @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.import_key:<:'20160101')" |
|
68
|
|
|
* @param string $properties Restrict the data returned to these properties. Ignored if empty. Comma separated list of properties names |
|
69
|
|
|
* @return array List of account objects |
|
70
|
|
|
* |
|
71
|
|
|
* @throws RestException |
|
72
|
|
|
*/ |
|
73
|
|
|
public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $category = 0, $sqlfilters = '', $properties = '') |
|
74
|
|
|
{ |
|
75
|
|
|
$list = array(); |
|
76
|
|
|
|
|
77
|
|
|
if (!DolibarrApiAccess::$user->hasRight('banque', 'lire')) { |
|
78
|
|
|
throw new RestException(403); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$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)"; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields |
|
82
|
|
|
if ($category > 0) { |
|
83
|
|
|
$sql .= ", " . MAIN_DB_PREFIX . "categorie_account as c"; |
|
84
|
|
|
} |
|
85
|
|
|
$sql .= ' WHERE t.entity IN (' . getEntity('bank_account') . ')'; |
|
86
|
|
|
// Select accounts of given category |
|
87
|
|
|
if ($category > 0) { |
|
88
|
|
|
$sql .= " AND c.fk_categorie = " . ((int) $category) . " AND c.fk_account = t.rowid"; |
|
89
|
|
|
} |
|
90
|
|
|
// Add sql filters |
|
91
|
|
|
if ($sqlfilters) { |
|
92
|
|
|
$errormessage = ''; |
|
93
|
|
|
$sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage); |
|
94
|
|
|
if ($errormessage) { |
|
95
|
|
|
throw new RestException(400, 'Error when validating parameter sqlfilters -> ' . $errormessage); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$sql .= $this->db->order($sortfield, $sortorder); |
|
100
|
|
|
if ($limit) { |
|
101
|
|
|
if ($page < 0) { |
|
102
|
|
|
$page = 0; |
|
103
|
|
|
} |
|
104
|
|
|
$offset = $limit * $page; |
|
105
|
|
|
|
|
106
|
|
|
$sql .= $this->db->plimit($limit + 1, $offset); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
dol_syslog("API Rest request"); |
|
110
|
|
|
$result = $this->db->query($sql); |
|
111
|
|
|
|
|
112
|
|
|
if ($result) { |
|
113
|
|
|
$num = $this->db->num_rows($result); |
|
114
|
|
|
$min = min($num, ($limit <= 0 ? $num : $limit)); |
|
115
|
|
|
for ($i = 0; $i < $min; $i++) { |
|
116
|
|
|
$obj = $this->db->fetch_object($result); |
|
117
|
|
|
$account = new Account($this->db); |
|
118
|
|
|
if ($account->fetch($obj->rowid) > 0) { |
|
119
|
|
|
$list[] = $this->_filterObjectProperties($this->_cleanObjectDatas($account), $properties); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
} else { |
|
123
|
|
|
throw new RestException(503, 'Error when retrieving list of accounts: ' . $this->db->lasterror()); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return $list; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Get account by ID. |
|
131
|
|
|
* |
|
132
|
|
|
* @param int $id ID of account |
|
133
|
|
|
* @return array Object with cleaned properties |
|
134
|
|
|
* |
|
135
|
|
|
* @throws RestException |
|
136
|
|
|
*/ |
|
137
|
|
|
public function get($id) |
|
138
|
|
|
{ |
|
139
|
|
|
if (!DolibarrApiAccess::$user->hasRight('banque', 'lire')) { |
|
140
|
|
|
throw new RestException(403); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
$account = new Account($this->db); |
|
144
|
|
|
$result = $account->fetch($id); |
|
145
|
|
|
if (!$result) { |
|
146
|
|
|
throw new RestException(404, 'account not found'); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return $this->_cleanObjectDatas($account); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Create account object |
|
154
|
|
|
* |
|
155
|
|
|
* @param array $request_data Request data |
|
156
|
|
|
* @return int ID of account |
|
157
|
|
|
*/ |
|
158
|
|
|
public function post($request_data = null) |
|
159
|
|
|
{ |
|
160
|
|
|
if (!DolibarrApiAccess::$user->hasRight('banque', 'configurer')) { |
|
161
|
|
|
throw new RestException(403); |
|
162
|
|
|
} |
|
163
|
|
|
// Check mandatory fields |
|
164
|
|
|
$result = $this->_validate($request_data); |
|
165
|
|
|
|
|
166
|
|
|
$account = new Account($this->db); |
|
167
|
|
|
foreach ($request_data as $field => $value) { |
|
168
|
|
|
if ($field === 'caller') { |
|
169
|
|
|
// Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller |
|
170
|
|
|
$account->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09'); |
|
171
|
|
|
continue; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
$account->$field = $this->_checkValForAPI($field, $value, $account); |
|
175
|
|
|
} |
|
176
|
|
|
// Date of the initial balance (required to create an account). |
|
177
|
|
|
$account->date_solde = time(); |
|
178
|
|
|
// courant and type are the same thing but the one used when |
|
179
|
|
|
// creating an account is courant |
|
180
|
|
|
$account->courant = $account->type; // deprecated |
|
|
|
|
|
|
181
|
|
|
|
|
182
|
|
|
if ($account->create(DolibarrApiAccess::$user) < 0) { |
|
183
|
|
|
throw new RestException(500, 'Error creating bank account', array_merge(array($account->error), $account->errors)); |
|
184
|
|
|
} |
|
185
|
|
|
return $account->id; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Create an internal wire transfer between two bank accounts |
|
190
|
|
|
* |
|
191
|
|
|
* @param int $bankaccount_from_id BankAccount ID to use as the source of the internal wire transfer {@from body}{@required true} |
|
192
|
|
|
* @param int $bankaccount_to_id BankAccount ID to use as the destination of the internal wire transfer {@from body}{@required true} |
|
193
|
|
|
* @param string $date Date of the internal wire transfer (UNIX timestamp) {@from body}{@required true}{@type timestamp} |
|
194
|
|
|
* @param string $description Description of the internal wire transfer {@from body}{@required true} |
|
195
|
|
|
* @param float $amount Amount to transfer from the source to the destination BankAccount {@from body}{@required true} |
|
196
|
|
|
* @param float $amount_to Amount to transfer to the destination BankAccount (only when accounts does not share the same currency) {@from body}{@required false} |
|
197
|
|
|
* @param string $cheque_number Cheque numero {@from body}{@required false} |
|
198
|
|
|
* |
|
199
|
|
|
* @url POST /transfer |
|
200
|
|
|
* |
|
201
|
|
|
* @return array |
|
202
|
|
|
* |
|
203
|
|
|
* @status 201 |
|
204
|
|
|
* |
|
205
|
|
|
* @throws RestException 401 Unauthorized: User does not have permission to configure bank accounts |
|
206
|
|
|
* @throws RestException 404 Not Found: Either the source or the destination bankaccount for the provided id does not exist |
|
207
|
|
|
* @throws RestException 422 Unprocessable Entity: Refer to detailed exception message for the cause |
|
208
|
|
|
* @throws RestException 500 Internal Server Error: Error(s) returned by the RDBMS |
|
209
|
|
|
*/ |
|
210
|
|
|
public function transfer($bankaccount_from_id = 0, $bankaccount_to_id = 0, $date = null, $description = "", $amount = 0.0, $amount_to = 0.0, $cheque_number = "") |
|
211
|
|
|
{ |
|
212
|
|
|
if (!DolibarrApiAccess::$user->hasRight('banque', 'configurer')) { |
|
213
|
|
|
throw new RestException(403); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
|
|
217
|
|
|
$accountfrom = new Account($this->db); |
|
218
|
|
|
$resultAccountFrom = $accountfrom->fetch($bankaccount_from_id); |
|
219
|
|
|
|
|
220
|
|
|
if ($resultAccountFrom === 0) { |
|
221
|
|
|
throw new RestException(404, 'The BankAccount for bankaccount_from_id provided does not exist.'); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
$accountto = new Account($this->db); |
|
225
|
|
|
$resultAccountTo = $accountto->fetch($bankaccount_to_id); |
|
226
|
|
|
|
|
227
|
|
|
if ($resultAccountTo === 0) { |
|
228
|
|
|
throw new RestException(404, 'The BankAccount for bankaccount_to_id provided does not exist.'); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
if ($accountto->currency_code == $accountfrom->currency_code) { |
|
232
|
|
|
$amount_to = $amount; |
|
233
|
|
|
} else { |
|
234
|
|
|
if (!$amount_to || empty($amount_to)) { |
|
235
|
|
|
throw new RestException(422, 'You must provide amount_to value since bankaccount_from and bankaccount_to does not share the same currency.'); |
|
236
|
|
|
} |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
if ($amount_to < 0) { |
|
240
|
|
|
throw new RestException(422, 'You must provide a positive value for amount.'); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
if ($accountto->id == $accountfrom->id) { |
|
244
|
|
|
throw new RestException(422, 'bankaccount_from_id and bankaccount_to_id must be different !'); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
$this->db->begin(); |
|
248
|
|
|
|
|
249
|
|
|
$error = 0; |
|
250
|
|
|
$bank_line_id_from = 0; |
|
251
|
|
|
$bank_line_id_to = 0; |
|
252
|
|
|
$result = 0; |
|
253
|
|
|
$user = DolibarrApiAccess::$user; |
|
254
|
|
|
|
|
255
|
|
|
// By default, electronic transfer from bank to bank |
|
256
|
|
|
$typefrom = 'PRE'; |
|
257
|
|
|
$typeto = 'VIR'; |
|
258
|
|
|
|
|
259
|
|
|
if ($accountto->type == Account::TYPE_CASH || $accountfrom->type == Account::TYPE_CASH) { |
|
260
|
|
|
// This is transfer of change |
|
261
|
|
|
$typefrom = 'LIQ'; |
|
262
|
|
|
$typeto = 'LIQ'; |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
// Clean data |
|
266
|
|
|
$description = sanitizeVal($description, 'alphanohtml'); |
|
267
|
|
|
$cheque_number = sanitizeVal($cheque_number, 'alphanohtml'); |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* Creating bank line records |
|
271
|
|
|
*/ |
|
272
|
|
|
|
|
273
|
|
|
if (!$error) { |
|
274
|
|
|
$bank_line_id_from = $accountfrom->addline($date, $typefrom, $description, -1 * (float) price2num($amount), '', '', $user, $cheque_number); |
|
275
|
|
|
} |
|
276
|
|
|
if (!($bank_line_id_from > 0)) { |
|
277
|
|
|
$error++; |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
if (!$error) { |
|
281
|
|
|
$bank_line_id_to = $accountto->addline($date, $typeto, $description, price2num($amount_to), '', '', $user, $cheque_number); |
|
282
|
|
|
} |
|
283
|
|
|
if (!($bank_line_id_to > 0)) { |
|
284
|
|
|
$error++; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* Creating links between bank line record and its source |
|
289
|
|
|
*/ |
|
290
|
|
|
|
|
291
|
|
|
$url = constant('BASE_URL') . '/compta/bank/line.php?rowid='; |
|
292
|
|
|
$label = '(banktransfert)'; |
|
293
|
|
|
$type = 'banktransfert'; |
|
294
|
|
|
|
|
295
|
|
|
if (!$error) { |
|
296
|
|
|
$result = $accountfrom->add_url_line($bank_line_id_from, $bank_line_id_to, $url, $label, $type); |
|
297
|
|
|
} |
|
298
|
|
|
if (!($result > 0)) { |
|
299
|
|
|
$error++; |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
if (!$error) { |
|
303
|
|
|
$result = $accountto->add_url_line($bank_line_id_to, $bank_line_id_from, $url, $label, $type); |
|
304
|
|
|
} |
|
305
|
|
|
if (!($result > 0)) { |
|
306
|
|
|
$error++; |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
if (!$error) { |
|
310
|
|
|
$this->db->commit(); |
|
311
|
|
|
|
|
312
|
|
|
return array( |
|
313
|
|
|
'success' => array( |
|
314
|
|
|
'code' => 201, |
|
315
|
|
|
'message' => 'Internal wire transfer created successfully.', |
|
316
|
|
|
'bank_id_from' => $bank_line_id_from, |
|
317
|
|
|
'bank_id_to' => $bank_line_id_to, |
|
318
|
|
|
) |
|
319
|
|
|
); |
|
320
|
|
|
} else { |
|
321
|
|
|
$this->db->rollback(); |
|
322
|
|
|
throw new RestException(500, $accountfrom->error . ' ' . $accountto->error); |
|
323
|
|
|
} |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
/** |
|
327
|
|
|
* Update account |
|
328
|
|
|
* |
|
329
|
|
|
* @param int $id ID of account |
|
330
|
|
|
* @param array $request_data data |
|
331
|
|
|
* @return array Object with cleaned properties |
|
332
|
|
|
*/ |
|
333
|
|
|
public function put($id, $request_data = null) |
|
334
|
|
|
{ |
|
335
|
|
|
if (!DolibarrApiAccess::$user->hasRight('banque', 'configurer')) { |
|
336
|
|
|
throw new RestException(403); |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
$account = new Account($this->db); |
|
340
|
|
|
$result = $account->fetch($id); |
|
341
|
|
|
if (!$result) { |
|
342
|
|
|
throw new RestException(404, 'account not found'); |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
foreach ($request_data as $field => $value) { |
|
346
|
|
|
if ($field == 'id') { |
|
347
|
|
|
continue; |
|
348
|
|
|
} |
|
349
|
|
|
if ($field === 'caller') { |
|
350
|
|
|
// Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller |
|
351
|
|
|
$account->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09'); |
|
352
|
|
|
continue; |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
|
$account->$field = $this->_checkValForAPI($field, $value, $account); |
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
|
|
if ($account->update(DolibarrApiAccess::$user) > 0) { |
|
359
|
|
|
return $this->get($id); |
|
360
|
|
|
} else { |
|
361
|
|
|
throw new RestException(500, $account->error); |
|
362
|
|
|
} |
|
363
|
|
|
} |
|
364
|
|
|
|
|
365
|
|
|
/** |
|
366
|
|
|
* Delete account |
|
367
|
|
|
* |
|
368
|
|
|
* @param int $id ID of account |
|
369
|
|
|
* @return array |
|
370
|
|
|
*/ |
|
371
|
|
|
public function delete($id) |
|
372
|
|
|
{ |
|
373
|
|
|
if (!DolibarrApiAccess::$user->hasRight('banque', 'configurer')) { |
|
374
|
|
|
throw new RestException(403); |
|
375
|
|
|
} |
|
376
|
|
|
$account = new Account($this->db); |
|
377
|
|
|
$result = $account->fetch($id); |
|
378
|
|
|
if (!$result) { |
|
379
|
|
|
throw new RestException(404, 'account not found'); |
|
380
|
|
|
} |
|
381
|
|
|
|
|
382
|
|
|
if ($account->delete(DolibarrApiAccess::$user) < 0) { |
|
383
|
|
|
throw new RestException(500, 'error when deleting account'); |
|
384
|
|
|
} |
|
385
|
|
|
|
|
386
|
|
|
return array( |
|
387
|
|
|
'success' => array( |
|
388
|
|
|
'code' => 200, |
|
389
|
|
|
'message' => 'account deleted' |
|
390
|
|
|
) |
|
391
|
|
|
); |
|
392
|
|
|
} |
|
393
|
|
|
|
|
394
|
|
|
/** |
|
395
|
|
|
* Validate fields before creating an object |
|
396
|
|
|
* |
|
397
|
|
|
* @param array|null $data Data to validate |
|
398
|
|
|
* @return array |
|
399
|
|
|
* |
|
400
|
|
|
* @throws RestException |
|
401
|
|
|
*/ |
|
402
|
|
|
private function _validate($data) |
|
403
|
|
|
{ |
|
404
|
|
|
$account = array(); |
|
405
|
|
|
foreach (BankAccounts::$FIELDS as $field) { |
|
406
|
|
|
if (!isset($data[$field])) { |
|
407
|
|
|
throw new RestException(400, "$field field missing"); |
|
408
|
|
|
} |
|
409
|
|
|
$account[$field] = $data[$field]; |
|
410
|
|
|
} |
|
411
|
|
|
return $account; |
|
412
|
|
|
} |
|
413
|
|
|
|
|
414
|
|
|
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore |
|
415
|
|
|
/** |
|
416
|
|
|
* Clean sensible object datas |
|
417
|
|
|
* |
|
418
|
|
|
* @param Object $object Object to clean |
|
419
|
|
|
* @return Object Object with cleaned properties |
|
420
|
|
|
*/ |
|
421
|
|
|
protected function _cleanObjectDatas($object) |
|
422
|
|
|
{ |
|
423
|
|
|
// phpcs:enable |
|
424
|
|
|
$object = parent::_cleanObjectDatas($object); |
|
425
|
|
|
|
|
426
|
|
|
unset($object->rowid); |
|
427
|
|
|
|
|
428
|
|
|
return $object; |
|
429
|
|
|
} |
|
430
|
|
|
|
|
431
|
|
|
/** |
|
432
|
|
|
* Get the list of lines of the account. |
|
433
|
|
|
* |
|
434
|
|
|
* @param int $id ID of account |
|
435
|
|
|
* @return array Array of AccountLine objects |
|
436
|
|
|
* |
|
437
|
|
|
* @throws RestException |
|
438
|
|
|
* |
|
439
|
|
|
* @url GET {id}/lines |
|
440
|
|
|
* @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.import_key:<:'20160101')" |
|
441
|
|
|
*/ |
|
442
|
|
|
public function getLines($id, $sqlfilters = '') |
|
443
|
|
|
{ |
|
444
|
|
|
$list = array(); |
|
445
|
|
|
|
|
446
|
|
|
if (!DolibarrApiAccess::$user->hasRight('banque', 'lire')) { |
|
447
|
|
|
throw new RestException(403); |
|
448
|
|
|
} |
|
449
|
|
|
|
|
450
|
|
|
$account = new Account($this->db); |
|
451
|
|
|
$result = $account->fetch($id); |
|
452
|
|
|
if (!$result) { |
|
453
|
|
|
throw new RestException(404, 'account not found'); |
|
454
|
|
|
} |
|
455
|
|
|
|
|
456
|
|
|
$sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . "bank "; |
|
457
|
|
|
$sql .= " WHERE fk_account = " . ((int) $id); |
|
458
|
|
|
|
|
459
|
|
|
// Add sql filters |
|
460
|
|
|
if ($sqlfilters) { |
|
461
|
|
|
$errormessage = ''; |
|
462
|
|
|
$sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage); |
|
463
|
|
|
if ($errormessage) { |
|
464
|
|
|
throw new RestException(400, 'Error when validating parameter sqlfilters -> ' . $errormessage); |
|
465
|
|
|
} |
|
466
|
|
|
} |
|
467
|
|
|
|
|
468
|
|
|
$sql .= " ORDER BY rowid"; |
|
469
|
|
|
|
|
470
|
|
|
$result = $this->db->query($sql); |
|
471
|
|
|
|
|
472
|
|
|
if ($result) { |
|
473
|
|
|
$num = $this->db->num_rows($result); |
|
474
|
|
|
for ($i = 0; $i < $num; $i++) { |
|
475
|
|
|
$obj = $this->db->fetch_object($result); |
|
476
|
|
|
$accountLine = new AccountLine($this->db); |
|
477
|
|
|
if ($accountLine->fetch($obj->rowid) > 0) { |
|
478
|
|
|
$list[] = $this->_cleanObjectDatas($accountLine); |
|
479
|
|
|
} |
|
480
|
|
|
} |
|
481
|
|
|
} else { |
|
482
|
|
|
throw new RestException(503, 'Error when retrieving list of account lines: ' . $this->db->lasterror()); |
|
483
|
|
|
} |
|
484
|
|
|
|
|
485
|
|
|
return $list; |
|
486
|
|
|
} |
|
487
|
|
|
|
|
488
|
|
|
/** |
|
489
|
|
|
* Add a line to an account |
|
490
|
|
|
* |
|
491
|
|
|
* @param int $id ID of account |
|
492
|
|
|
* @param string $date Payment date (timestamp) {@from body} {@type timestamp} |
|
493
|
|
|
* @param string $type Payment mode (TYP,VIR,PRE,LIQ,VAD,CB,CHQ...) {@from body} |
|
494
|
|
|
* @param string $label Label {@from body} |
|
495
|
|
|
* @param float $amount Amount (may be 0) {@from body} |
|
496
|
|
|
* @param int $category Category |
|
497
|
|
|
* @param string $cheque_number Cheque numero {@from body} |
|
498
|
|
|
* @param string $cheque_writer Name of cheque writer {@from body} |
|
499
|
|
|
* @param string $cheque_bank Bank of cheque writer {@from body} |
|
500
|
|
|
* @param string $accountancycode Accountancy code {@from body} |
|
501
|
|
|
* @param string $datev Payment date value (timestamp) {@from body} {@type timestamp} |
|
502
|
|
|
* @param string $num_releve Bank statement numero {@from body} |
|
503
|
|
|
* @return int ID of line |
|
504
|
|
|
* |
|
505
|
|
|
* @url POST {id}/lines |
|
506
|
|
|
*/ |
|
507
|
|
|
public function addLine($id, $date, $type, $label, $amount, $category = 0, $cheque_number = '', $cheque_writer = '', $cheque_bank = '', $accountancycode = '', $datev = null, $num_releve = '') |
|
508
|
|
|
{ |
|
509
|
|
|
if (!DolibarrApiAccess::$user->hasRight('banque', 'modifier')) { |
|
510
|
|
|
throw new RestException(403); |
|
511
|
|
|
} |
|
512
|
|
|
|
|
513
|
|
|
$account = new Account($this->db); |
|
514
|
|
|
$result = $account->fetch($id); |
|
515
|
|
|
if (!$result) { |
|
516
|
|
|
throw new RestException(404, 'account not found'); |
|
517
|
|
|
} |
|
518
|
|
|
|
|
519
|
|
|
$type = sanitizeVal($type); |
|
520
|
|
|
$label = sanitizeVal($label); |
|
521
|
|
|
$cheque_number = sanitizeVal($cheque_number); |
|
522
|
|
|
$cheque_writer = sanitizeVal($cheque_writer); |
|
523
|
|
|
$cheque_bank = sanitizeVal($cheque_bank); |
|
524
|
|
|
$accountancycode = sanitizeVal($accountancycode); |
|
525
|
|
|
$num_releve = sanitizeVal($num_releve); |
|
526
|
|
|
|
|
527
|
|
|
$result = $account->addline( |
|
528
|
|
|
$date, |
|
529
|
|
|
$type, |
|
530
|
|
|
$label, |
|
531
|
|
|
$amount, |
|
532
|
|
|
$cheque_number, |
|
533
|
|
|
$category, |
|
534
|
|
|
DolibarrApiAccess::$user, |
|
535
|
|
|
$cheque_writer, |
|
536
|
|
|
$cheque_bank, |
|
537
|
|
|
$accountancycode, |
|
538
|
|
|
$datev, |
|
539
|
|
|
$num_releve |
|
540
|
|
|
); |
|
541
|
|
|
if ($result < 0) { |
|
542
|
|
|
throw new RestException(503, 'Error when adding line to account: ' . $account->error); |
|
543
|
|
|
} |
|
544
|
|
|
return $result; |
|
545
|
|
|
} |
|
546
|
|
|
|
|
547
|
|
|
/** |
|
548
|
|
|
* Add a link to an account line |
|
549
|
|
|
* |
|
550
|
|
|
* @param int $id ID of account |
|
551
|
|
|
* @param int $line_id ID of account line |
|
552
|
|
|
* @param int $url_id ID to set in the URL {@from body} |
|
553
|
|
|
* @param string $url URL of the link {@from body} |
|
554
|
|
|
* @param string $label Label {@from body} |
|
555
|
|
|
* @param string $type Type of link ('payment', 'company', 'member', ...) {@from body} |
|
556
|
|
|
* @return int ID of link |
|
557
|
|
|
* |
|
558
|
|
|
* @url POST {id}/lines/{line_id}/links |
|
559
|
|
|
*/ |
|
560
|
|
|
public function addLink($id, $line_id, $url_id, $url, $label, $type) |
|
561
|
|
|
{ |
|
562
|
|
|
if (!DolibarrApiAccess::$user->hasRight('banque', 'modifier')) { |
|
563
|
|
|
throw new RestException(403); |
|
564
|
|
|
} |
|
565
|
|
|
|
|
566
|
|
|
$account = new Account($this->db); |
|
567
|
|
|
$result = $account->fetch($id); |
|
568
|
|
|
if (!$result) { |
|
569
|
|
|
throw new RestException(404, 'account not found'); |
|
570
|
|
|
} |
|
571
|
|
|
|
|
572
|
|
|
$accountLine = new AccountLine($this->db); |
|
573
|
|
|
$result = $accountLine->fetch($line_id); |
|
574
|
|
|
if (!$result) { |
|
575
|
|
|
throw new RestException(404, 'account line not found'); |
|
576
|
|
|
} |
|
577
|
|
|
|
|
578
|
|
|
$url = sanitizeVal($url); |
|
579
|
|
|
$label = sanitizeVal($label); |
|
580
|
|
|
$type = sanitizeVal($type); |
|
581
|
|
|
|
|
582
|
|
|
$result = $account->add_url_line($line_id, $url_id, $url, $label, $type); |
|
583
|
|
|
if ($result < 0) { |
|
584
|
|
|
throw new RestException(503, 'Error when adding link to account line: ' . $account->error); |
|
585
|
|
|
} |
|
586
|
|
|
return $result; |
|
587
|
|
|
} |
|
588
|
|
|
|
|
589
|
|
|
/** |
|
590
|
|
|
* Get the list of links for a line of the account. |
|
591
|
|
|
* |
|
592
|
|
|
* @param int $id ID of account |
|
593
|
|
|
* @param int $line_id ID of account line |
|
594
|
|
|
* @return array Array of links |
|
595
|
|
|
* |
|
596
|
|
|
* @throws RestException |
|
597
|
|
|
* |
|
598
|
|
|
* @url GET {id}/lines/{line_id}/links |
|
599
|
|
|
* |
|
600
|
|
|
*/ |
|
601
|
|
|
public function getLinks($id, $line_id) |
|
602
|
|
|
{ |
|
603
|
|
|
$list = array(); |
|
604
|
|
|
|
|
605
|
|
|
if (!DolibarrApiAccess::$user->hasRight('banque', 'lire')) { |
|
606
|
|
|
throw new RestException(403); |
|
607
|
|
|
} |
|
608
|
|
|
|
|
609
|
|
|
$account = new Account($this->db); |
|
610
|
|
|
$result = $account->fetch($id); |
|
611
|
|
|
if (!$result) { |
|
612
|
|
|
throw new RestException(404, 'account not found'); |
|
613
|
|
|
} |
|
614
|
|
|
|
|
615
|
|
|
$links = $account->get_url($line_id); // Get an array('url'=>, 'url_id'=>, 'label'=>, 'type'=> 'fk_bank'=> ) |
|
616
|
|
|
foreach ($links as &$link) { |
|
617
|
|
|
unset($link[0], $link[1], $link[2], $link[3]); // Remove the numeric keys |
|
618
|
|
|
} |
|
619
|
|
|
|
|
620
|
|
|
return $links; |
|
621
|
|
|
} |
|
622
|
|
|
|
|
623
|
|
|
/** |
|
624
|
|
|
* Update an account line |
|
625
|
|
|
* |
|
626
|
|
|
* @param int $id ID of account |
|
627
|
|
|
* @param int $line_id ID of account line |
|
628
|
|
|
* @param string $label Label {@from body} |
|
629
|
|
|
* @return int ID of link |
|
630
|
|
|
* |
|
631
|
|
|
* @url PUT {id}/lines/{line_id} |
|
632
|
|
|
*/ |
|
633
|
|
|
public function updateLine($id, $line_id, $label) |
|
634
|
|
|
{ |
|
635
|
|
|
if (!DolibarrApiAccess::$user->rights->banque->modifier) { |
|
636
|
|
|
throw new RestException(403); |
|
637
|
|
|
} |
|
638
|
|
|
|
|
639
|
|
|
$account = new Account($this->db); |
|
640
|
|
|
$result = $account->fetch($id); |
|
641
|
|
|
if (!$result) { |
|
642
|
|
|
throw new RestException(404, 'account not found'); |
|
643
|
|
|
} |
|
644
|
|
|
|
|
645
|
|
|
$accountLine = new AccountLine($this->db); |
|
646
|
|
|
$result = $accountLine->fetch($line_id); |
|
647
|
|
|
if (!$result) { |
|
648
|
|
|
throw new RestException(404, 'account line not found'); |
|
649
|
|
|
} |
|
650
|
|
|
|
|
651
|
|
|
$accountLine->label = sanitizeVal($label); |
|
652
|
|
|
|
|
653
|
|
|
$result = $accountLine->updateLabel(); |
|
654
|
|
|
if ($result < 0) { |
|
655
|
|
|
throw new RestException(503, 'Error when updating link to account line: ' . $accountLine->error); |
|
656
|
|
|
} |
|
657
|
|
|
return $accountLine->id; |
|
658
|
|
|
} |
|
659
|
|
|
|
|
660
|
|
|
/** |
|
661
|
|
|
* Delete an account line |
|
662
|
|
|
* |
|
663
|
|
|
* @param int $id ID of account |
|
664
|
|
|
* @param int $line_id ID of account line |
|
665
|
|
|
* @return array |
|
666
|
|
|
* |
|
667
|
|
|
* @url DELETE {id}/lines/{line_id} |
|
668
|
|
|
*/ |
|
669
|
|
|
public function deleteLine($id, $line_id) |
|
670
|
|
|
{ |
|
671
|
|
|
if (!DolibarrApiAccess::$user->rights->banque->modifier) { |
|
672
|
|
|
throw new RestException(403); |
|
673
|
|
|
} |
|
674
|
|
|
|
|
675
|
|
|
$account = new Account($this->db); |
|
676
|
|
|
$result = $account->fetch($id); |
|
677
|
|
|
if (!$result) { |
|
678
|
|
|
throw new RestException(404, 'account not found'); |
|
679
|
|
|
} |
|
680
|
|
|
|
|
681
|
|
|
$accountLine = new AccountLine($this->db); |
|
682
|
|
|
$result = $accountLine->fetch($line_id); |
|
683
|
|
|
if (!$result) { |
|
684
|
|
|
throw new RestException(404, 'account line not found'); |
|
685
|
|
|
} |
|
686
|
|
|
|
|
687
|
|
|
if ($accountLine->delete(DolibarrApiAccess::$user) < 0) { |
|
688
|
|
|
throw new RestException(500, 'error when deleting account line'); |
|
689
|
|
|
} |
|
690
|
|
|
|
|
691
|
|
|
return array( |
|
692
|
|
|
'success' => array( |
|
693
|
|
|
'code' => 200, |
|
694
|
|
|
'message' => "account line $line_id deleted" |
|
695
|
|
|
) |
|
696
|
|
|
); |
|
697
|
|
|
} |
|
698
|
|
|
} |
|
699
|
|
|
|