1
|
|
|
<?php |
2
|
|
|
/* Copyright (C) 2015 Jean-François Ferry <[email protected]> |
3
|
|
|
* Copyright (C) 2016 Laurent Destailleur <[email protected]> |
4
|
|
|
* Copyright (C) 2018 Frédéric France <[email protected]> |
5
|
|
|
* |
6
|
|
|
* This program is free software; you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU General Public License as published by |
8
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* This program is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU General Public License for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU General Public License |
17
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
use Luracast\Restler\RestException; |
21
|
|
|
|
22
|
|
|
require_once DOL_DOCUMENT_ROOT.'/contrat/class/contrat.class.php'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* API class for contracts |
26
|
|
|
* |
27
|
|
|
* @access protected |
28
|
|
|
* @class DolibarrApiAccess {@requires user,external} |
29
|
|
|
*/ |
30
|
|
|
class Contracts extends DolibarrApi |
31
|
|
|
{ |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array $FIELDS Mandatory fields, checked when create and update object |
35
|
|
|
*/ |
36
|
|
|
static $FIELDS = array( |
37
|
|
|
'socid', |
38
|
|
|
'date_contrat', |
39
|
|
|
'commercial_signature_id', |
40
|
|
|
'commercial_suivi_id' |
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var Contrat $contract {@type Contrat} |
45
|
|
|
*/ |
46
|
|
|
public $contract; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Constructor |
50
|
|
|
*/ |
51
|
|
|
function __construct() |
52
|
|
|
{ |
53
|
|
|
global $db, $conf; |
54
|
|
|
$this->db = $db; |
55
|
|
|
$this->contract = new Contrat($this->db); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get properties of a contract object |
60
|
|
|
* |
61
|
|
|
* Return an array with contract informations |
62
|
|
|
* |
63
|
|
|
* @param int $id ID of contract |
64
|
|
|
* @return array|mixed data without useless information |
65
|
|
|
* |
66
|
|
|
* @throws RestException |
67
|
|
|
*/ |
68
|
|
|
function get($id) |
69
|
|
|
{ |
70
|
|
|
if(! DolibarrApiAccess::$user->rights->contrat->lire) { |
71
|
|
|
throw new RestException(401); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$result = $this->contract->fetch($id); |
75
|
|
|
if( ! $result ) { |
76
|
|
|
throw new RestException(404, 'Contract not found'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if( ! DolibarrApi::_checkAccessToResource('contrat',$this->contract->id)) { |
80
|
|
|
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$this->contract->fetchObjectLinked(); |
84
|
|
|
return $this->_cleanObjectDatas($this->contract); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* List contracts |
91
|
|
|
* |
92
|
|
|
* Get a list of contracts |
93
|
|
|
* |
94
|
|
|
* @param string $sortfield Sort field |
95
|
|
|
* @param string $sortorder Sort order |
96
|
|
|
* @param int $limit Limit for list |
97
|
|
|
* @param int $page Page number |
98
|
|
|
* @param string $thirdparty_ids Thirdparty ids to filter contracts of. {@example '1' or '1,2,3'} {@pattern /^[0-9,]*$/i} |
99
|
|
|
* @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')" |
100
|
|
|
* @return array Array of contract objects |
101
|
|
|
* |
102
|
|
|
* @throws RestException |
103
|
|
|
*/ |
104
|
|
|
function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '') |
105
|
|
|
{ |
106
|
|
|
global $db, $conf; |
107
|
|
|
|
108
|
|
|
$obj_ret = array(); |
109
|
|
|
|
110
|
|
|
// case of external user, $thirdparty_ids param is ignored and replaced by user's socid |
111
|
|
|
$socids = DolibarrApiAccess::$user->societe_id ? DolibarrApiAccess::$user->societe_id : $thirdparty_ids; |
112
|
|
|
|
113
|
|
|
// If the internal user must only see his customers, force searching by him |
114
|
|
|
$search_sale = 0; |
115
|
|
|
if (! DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) $search_sale = DolibarrApiAccess::$user->id; |
116
|
|
|
|
117
|
|
|
$sql = "SELECT t.rowid"; |
118
|
|
|
if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects) |
119
|
|
|
$sql.= " FROM ".MAIN_DB_PREFIX."contrat as t"; |
120
|
|
|
|
121
|
|
|
if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale |
122
|
|
|
|
123
|
|
|
$sql.= ' WHERE t.entity IN ('.getEntity('contrat').')'; |
124
|
|
|
if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql.= " AND t.fk_soc = sc.fk_soc"; |
125
|
|
|
if ($socids) $sql.= " AND t.fk_soc IN (".$socids.")"; |
126
|
|
|
if ($search_sale > 0) $sql.= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale |
127
|
|
|
// Insert sale filter |
128
|
|
|
if ($search_sale > 0) |
129
|
|
|
{ |
130
|
|
|
$sql .= " AND sc.fk_user = ".$search_sale; |
131
|
|
|
} |
132
|
|
|
// Add sql filters |
133
|
|
|
if ($sqlfilters) |
134
|
|
|
{ |
135
|
|
|
if (! DolibarrApi::_checkFilters($sqlfilters)) |
136
|
|
|
{ |
137
|
|
|
throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters); |
138
|
|
|
} |
139
|
|
|
$regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)'; |
140
|
|
|
$sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")"; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$sql.= $db->order($sortfield, $sortorder); |
144
|
|
|
if ($limit) { |
145
|
|
|
if ($page < 0) |
146
|
|
|
{ |
147
|
|
|
$page = 0; |
148
|
|
|
} |
149
|
|
|
$offset = $limit * $page; |
150
|
|
|
|
151
|
|
|
$sql.= $db->plimit($limit + 1, $offset); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
dol_syslog("API Rest request"); |
155
|
|
|
$result = $db->query($sql); |
156
|
|
|
|
157
|
|
|
if ($result) |
158
|
|
|
{ |
159
|
|
|
$num = $db->num_rows($result); |
160
|
|
|
$min = min($num, ($limit <= 0 ? $num : $limit)); |
161
|
|
|
$i=0; |
162
|
|
|
while ($i < $min) |
163
|
|
|
{ |
164
|
|
|
$obj = $db->fetch_object($result); |
165
|
|
|
$contrat_static = new Contrat($db); |
166
|
|
|
if($contrat_static->fetch($obj->rowid)) { |
167
|
|
|
$obj_ret[] = $this->_cleanObjectDatas($contrat_static); |
168
|
|
|
} |
169
|
|
|
$i++; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
else { |
173
|
|
|
throw new RestException(503, 'Error when retrieve contrat list : '.$db->lasterror()); |
174
|
|
|
} |
175
|
|
|
if( ! count($obj_ret)) { |
176
|
|
|
throw new RestException(404, 'No contract found'); |
177
|
|
|
} |
178
|
|
|
return $obj_ret; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Create contract object |
183
|
|
|
* |
184
|
|
|
* @param array $request_data Request data |
185
|
|
|
* @return int ID of contrat |
186
|
|
|
*/ |
187
|
|
|
function post($request_data = null) |
188
|
|
|
{ |
189
|
|
|
if(! DolibarrApiAccess::$user->rights->contrat->creer) { |
190
|
|
|
throw new RestException(401, "Insuffisant rights"); |
191
|
|
|
} |
192
|
|
|
// Check mandatory fields |
193
|
|
|
$result = $this->_validate($request_data); |
194
|
|
|
|
195
|
|
|
foreach($request_data as $field => $value) { |
196
|
|
|
$this->contract->$field = $value; |
197
|
|
|
} |
198
|
|
|
/*if (isset($request_data["lines"])) { |
199
|
|
|
$lines = array(); |
200
|
|
|
foreach ($request_data["lines"] as $line) { |
201
|
|
|
array_push($lines, (object) $line); |
202
|
|
|
} |
203
|
|
|
$this->contract->lines = $lines; |
204
|
|
|
}*/ |
205
|
|
|
if ($this->contract->create(DolibarrApiAccess::$user) < 0) { |
206
|
|
|
throw new RestException(500, "Error creating contract", array_merge(array($this->contract->error), $this->contract->errors)); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
return $this->contract->id; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Get lines of a contract |
214
|
|
|
* |
215
|
|
|
* @param int $id Id of contract |
216
|
|
|
* |
217
|
|
|
* @url GET {id}/lines |
218
|
|
|
* |
219
|
|
|
* @return array |
220
|
|
|
*/ |
221
|
|
|
function getLines($id) |
222
|
|
|
{ |
223
|
|
|
if(! DolibarrApiAccess::$user->rights->contrat->lire) { |
224
|
|
|
throw new RestException(401); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
$result = $this->contract->fetch($id); |
228
|
|
|
if( ! $result ) { |
229
|
|
|
throw new RestException(404, 'Contract not found'); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
if( ! DolibarrApi::_checkAccessToResource('contrat',$this->contract->id)) { |
233
|
|
|
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
234
|
|
|
} |
235
|
|
|
$this->contract->getLinesArray(); |
236
|
|
|
$result = array(); |
237
|
|
|
foreach ($this->contract->lines as $line) { |
238
|
|
|
array_push($result, $this->_cleanObjectDatas($line)); |
239
|
|
|
} |
240
|
|
|
return $result; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Add a line to given contract |
245
|
|
|
* |
246
|
|
|
* @param int $id Id of contrat to update |
247
|
|
|
* @param array $request_data Contractline data |
248
|
|
|
* |
249
|
|
|
* @url POST {id}/lines |
250
|
|
|
* |
251
|
|
|
* @return int|bool |
252
|
|
|
*/ |
253
|
|
|
function postLine($id, $request_data = null) |
254
|
|
|
{ |
255
|
|
|
if(! DolibarrApiAccess::$user->rights->contrat->creer) { |
256
|
|
|
throw new RestException(401); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
$result = $this->contract->fetch($id); |
260
|
|
|
if( ! $result ) { |
261
|
|
|
throw new RestException(404, 'Contract not found'); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
if( ! DolibarrApi::_checkAccessToResource('contrat',$this->contract->id)) { |
265
|
|
|
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
266
|
|
|
} |
267
|
|
|
$request_data = (object) $request_data; |
268
|
|
|
$updateRes = $this->contract->addline( |
269
|
|
|
$request_data->desc, |
270
|
|
|
$request_data->subprice, |
271
|
|
|
$request_data->qty, |
272
|
|
|
$request_data->tva_tx, |
273
|
|
|
$request_data->localtax1_tx, |
274
|
|
|
$request_data->localtax2_tx, |
275
|
|
|
$request_data->fk_product, |
276
|
|
|
$request_data->remise_percent, |
277
|
|
|
$request_data->date_start, // date_start = date planned start, date ouverture = date_start_real |
278
|
|
|
$request_data->date_end, // date_end = date planned end, date_cloture = date_end_real |
279
|
|
|
$request_data->HT, |
280
|
|
|
$request_data->subprice_excl_tax, |
281
|
|
|
$request_data->info_bits, |
282
|
|
|
$request_data->fk_fournprice, |
283
|
|
|
$request_data->pa_ht, |
284
|
|
|
$request_data->array_options, |
285
|
|
|
$request_data->fk_unit, |
286
|
|
|
$request_data->rang |
287
|
|
|
); |
288
|
|
|
|
289
|
|
|
if ($updateRes > 0) { |
290
|
|
|
return $updateRes; |
291
|
|
|
} |
292
|
|
|
return false; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Update a line to given contract |
297
|
|
|
* |
298
|
|
|
* @param int $id Id of contrat to update |
299
|
|
|
* @param int $lineid Id of line to update |
300
|
|
|
* @param array $request_data Contractline data |
301
|
|
|
* |
302
|
|
|
* @url PUT {id}/lines/{lineid} |
303
|
|
|
* |
304
|
|
|
* @return array|bool |
305
|
|
|
*/ |
306
|
|
|
function putLine($id, $lineid, $request_data = null) |
307
|
|
|
{ |
308
|
|
|
if(! DolibarrApiAccess::$user->rights->contrat->creer) { |
309
|
|
|
throw new RestException(401); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
$result = $this->contract->fetch($id); |
313
|
|
|
if( ! $result ) { |
314
|
|
|
throw new RestException(404, 'Contrat not found'); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
if( ! DolibarrApi::_checkAccessToResource('contrat',$this->contract->id)) { |
318
|
|
|
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
$request_data = (object) $request_data; |
322
|
|
|
|
323
|
|
|
$updateRes = $this->contract->updateline( |
324
|
|
|
$lineid, |
325
|
|
|
$request_data->desc, |
326
|
|
|
$request_data->subprice, |
327
|
|
|
$request_data->qty, |
328
|
|
|
$request_data->remise_percent, |
329
|
|
|
$request_data->date_ouveture_prevue, |
330
|
|
|
$request_data->date_fin_validite, |
331
|
|
|
$request_data->tva_tx, |
332
|
|
|
$request_data->localtax1_tx, |
333
|
|
|
$request_data->localtax2_tx, |
334
|
|
|
$request_data->date_ouverture, |
335
|
|
|
$request_data->date_cloture, |
336
|
|
|
'HT', |
337
|
|
|
$request_data->info_bits, |
338
|
|
|
$request_data->fk_fourn_price, |
339
|
|
|
$request_data->pa_ht, |
340
|
|
|
$request_data->array_options, |
341
|
|
|
$request_data->fk_unit |
342
|
|
|
); |
343
|
|
|
|
344
|
|
|
if ($updateRes > 0) { |
345
|
|
|
$result = $this->get($id); |
346
|
|
|
unset($result->line); |
347
|
|
|
return $this->_cleanObjectDatas($result); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
return false; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Activate a service line of a given contract |
355
|
|
|
* |
356
|
|
|
* @param int $id Id of contract to activate |
357
|
|
|
* @param int $lineid Id of line to activate |
358
|
|
|
* @param string $datestart {@from body} Date start {@type timestamp} |
359
|
|
|
* @param string $dateend {@from body} Date end {@type timestamp} |
360
|
|
|
* @param string $comment {@from body} Comment |
361
|
|
|
* |
362
|
|
|
* @url PUT {id}/lines/{lineid}/activate |
363
|
|
|
* |
364
|
|
|
* @return array|bool |
365
|
|
|
*/ |
366
|
|
|
function activateLine($id, $lineid, $datestart, $dateend = null, $comment = null) |
367
|
|
|
{ |
368
|
|
|
if(! DolibarrApiAccess::$user->rights->contrat->creer) { |
369
|
|
|
throw new RestException(401); |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
$result = $this->contract->fetch($id); |
373
|
|
|
if (! $result) { |
374
|
|
|
throw new RestException(404, 'Contrat not found'); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
if( ! DolibarrApi::_checkAccessToResource('contrat',$this->contract->id)) { |
378
|
|
|
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
$updateRes = $this->contract->active_line(DolibarrApiAccess::$user, $lineid, $datestart, $dateend, $comment); |
382
|
|
|
|
383
|
|
|
if ($updateRes > 0) { |
384
|
|
|
$result = $this->get($id); |
385
|
|
|
unset($result->line); |
386
|
|
|
return $this->_cleanObjectDatas($result); |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
return false; |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
/** |
393
|
|
|
* Unactivate a service line of a given contract |
394
|
|
|
* |
395
|
|
|
* @param int $id Id of contract to activate |
396
|
|
|
* @param int $lineid Id of line to activate |
397
|
|
|
* @param string $datestart {@from body} Date start {@type timestamp} |
398
|
|
|
* @param string $comment {@from body} Comment |
399
|
|
|
* |
400
|
|
|
* @url PUT {id}/lines/{lineid}/unactivate |
401
|
|
|
* |
402
|
|
|
* @return array|bool |
403
|
|
|
*/ |
404
|
|
|
function unactivateLine($id, $lineid, $datestart, $comment = null) |
405
|
|
|
{ |
406
|
|
|
if (! DolibarrApiAccess::$user->rights->contrat->creer) { |
407
|
|
|
throw new RestException(401); |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
$result = $this->contract->fetch($id); |
411
|
|
|
if (! $result) { |
412
|
|
|
throw new RestException(404, 'Contrat not found'); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
if( ! DolibarrApi::_checkAccessToResource('contrat',$this->contract->id)) { |
416
|
|
|
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
$request_data = (object) $request_data; |
|
|
|
|
420
|
|
|
|
421
|
|
|
$updateRes = $this->contract->close_line(DolibarrApiAccess::$user, $lineid, $datestart, $comment); |
422
|
|
|
|
423
|
|
|
if ($updateRes > 0) { |
424
|
|
|
$result = $this->get($id); |
425
|
|
|
unset($result->line); |
426
|
|
|
return $this->_cleanObjectDatas($result); |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
return false; |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
/** |
433
|
|
|
* Delete a line to given contract |
434
|
|
|
* |
435
|
|
|
* |
436
|
|
|
* @param int $id Id of contract to update |
437
|
|
|
* @param int $lineid Id of line to delete |
438
|
|
|
* |
439
|
|
|
* @url DELETE {id}/lines/{lineid} |
440
|
|
|
* |
441
|
|
|
* @return int |
442
|
|
|
* @throws 401 |
443
|
|
|
* @throws 404 |
444
|
|
|
*/ |
445
|
|
|
function deleteLine($id, $lineid) |
446
|
|
|
{ |
447
|
|
|
if (! DolibarrApiAccess::$user->rights->contrat->creer) { |
448
|
|
|
throw new RestException(401); |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
$result = $this->contract->fetch($id); |
452
|
|
|
if (! $result) { |
453
|
|
|
throw new RestException(404, 'Contrat not found'); |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
if (! DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) { |
457
|
|
|
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
// TODO Check the lineid $lineid is a line of object |
461
|
|
|
|
462
|
|
|
$updateRes = $this->contract->deleteline($lineid, DolibarrApiAccess::$user); |
463
|
|
|
if ($updateRes > 0) { |
464
|
|
|
return $this->get($id); |
465
|
|
|
} |
466
|
|
|
else |
467
|
|
|
{ |
468
|
|
|
throw new RestException(405, $this->contract->error); |
469
|
|
|
} |
470
|
|
|
} |
471
|
|
|
|
472
|
|
|
/** |
473
|
|
|
* Update contract general fields (won't touch lines of contract) |
474
|
|
|
* |
475
|
|
|
* @param int $id Id of contract to update |
476
|
|
|
* @param array $request_data Datas |
477
|
|
|
* |
478
|
|
|
* @return int |
479
|
|
|
*/ |
480
|
|
|
function put($id, $request_data = null) |
481
|
|
|
{ |
482
|
|
|
if (! DolibarrApiAccess::$user->rights->contrat->creer) { |
483
|
|
|
throw new RestException(401); |
484
|
|
|
} |
485
|
|
|
|
486
|
|
|
$result = $this->contract->fetch($id); |
487
|
|
|
if (! $result) { |
488
|
|
|
throw new RestException(404, 'Contrat not found'); |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
if (! DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) { |
492
|
|
|
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
493
|
|
|
} |
494
|
|
|
foreach($request_data as $field => $value) { |
495
|
|
|
if ($field == 'id') continue; |
496
|
|
|
$this->contract->$field = $value; |
497
|
|
|
} |
498
|
|
|
|
499
|
|
|
if ($this->contract->update(DolibarrApiAccess::$user) > 0) |
500
|
|
|
{ |
501
|
|
|
return $this->get($id); |
502
|
|
|
} |
503
|
|
|
else |
504
|
|
|
{ |
505
|
|
|
throw new RestException(500, $this->contract->error); |
506
|
|
|
} |
507
|
|
|
} |
508
|
|
|
|
509
|
|
|
/** |
510
|
|
|
* Delete contract |
511
|
|
|
* |
512
|
|
|
* @param int $id Contract ID |
513
|
|
|
* |
514
|
|
|
* @return array |
515
|
|
|
*/ |
516
|
|
|
function delete($id) |
517
|
|
|
{ |
518
|
|
|
if (! DolibarrApiAccess::$user->rights->contrat->supprimer) { |
519
|
|
|
throw new RestException(401); |
520
|
|
|
} |
521
|
|
|
$result = $this->contract->fetch($id); |
522
|
|
|
if (! $result) { |
523
|
|
|
throw new RestException(404, 'Contract not found'); |
524
|
|
|
} |
525
|
|
|
|
526
|
|
|
if (! DolibarrApi::_checkAccessToResource('contrat',$this->contract->id)) { |
527
|
|
|
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
528
|
|
|
} |
529
|
|
|
|
530
|
|
|
if (! $this->contract->delete(DolibarrApiAccess::$user)) { |
531
|
|
|
throw new RestException(500, 'Error when delete contract : '.$this->contract->error); |
532
|
|
|
} |
533
|
|
|
|
534
|
|
|
return array( |
535
|
|
|
'success' => array( |
536
|
|
|
'code' => 200, |
537
|
|
|
'message' => 'Contract deleted' |
538
|
|
|
) |
539
|
|
|
); |
540
|
|
|
} |
541
|
|
|
|
542
|
|
|
/** |
543
|
|
|
* Validate a contract |
544
|
|
|
* |
545
|
|
|
* @param int $id Contract ID |
546
|
|
|
* @param int $notrigger 1=Does not execute triggers, 0= execute triggers |
547
|
|
|
* |
548
|
|
|
* @url POST {id}/validate |
549
|
|
|
* |
550
|
|
|
* @return array |
551
|
|
|
* FIXME An error 403 is returned if the request has an empty body. |
552
|
|
|
* Error message: "Forbidden: Content type `text/plain` is not supported." |
553
|
|
|
* Workaround: send this in the body |
554
|
|
|
* { |
555
|
|
|
* "notrigger": 0 |
556
|
|
|
* } |
557
|
|
|
*/ |
558
|
|
|
function validate($id, $notrigger=0) |
559
|
|
|
{ |
560
|
|
|
if (! DolibarrApiAccess::$user->rights->contrat->creer) { |
561
|
|
|
throw new RestException(401); |
562
|
|
|
} |
563
|
|
|
$result = $this->contract->fetch($id); |
564
|
|
|
if (! $result) { |
565
|
|
|
throw new RestException(404, 'Contract not found'); |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
if (! DolibarrApi::_checkAccessToResource('contrat',$this->contract->id)) { |
569
|
|
|
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
570
|
|
|
} |
571
|
|
|
|
572
|
|
|
$result = $this->contract->validate(DolibarrApiAccess::$user, '', $notrigger); |
573
|
|
|
if ($result == 0) { |
574
|
|
|
throw new RestException(304, 'Error nothing done. May be object is already validated'); |
575
|
|
|
} |
576
|
|
|
if ($result < 0) { |
577
|
|
|
throw new RestException(500, 'Error when validating Contract: '.$this->contract->error); |
578
|
|
|
} |
579
|
|
|
|
580
|
|
|
return array( |
581
|
|
|
'success' => array( |
582
|
|
|
'code' => 200, |
583
|
|
|
'message' => 'Contract validated (Ref='.$this->contract->ref.')' |
584
|
|
|
) |
585
|
|
|
); |
586
|
|
|
} |
587
|
|
|
|
588
|
|
|
/** |
589
|
|
|
* Close all services of a contract |
590
|
|
|
* |
591
|
|
|
* @param int $id Contract ID |
592
|
|
|
* @param int $notrigger 1=Does not execute triggers, 0= execute triggers |
593
|
|
|
* |
594
|
|
|
* @url POST {id}/close |
595
|
|
|
* |
596
|
|
|
* @return array |
597
|
|
|
* FIXME An error 403 is returned if the request has an empty body. |
598
|
|
|
* Error message: "Forbidden: Content type `text/plain` is not supported." |
599
|
|
|
* Workaround: send this in the body |
600
|
|
|
* { |
601
|
|
|
* "notrigger": 0 |
602
|
|
|
* } |
603
|
|
|
*/ |
604
|
|
|
function close($id, $notrigger=0) |
605
|
|
|
{ |
606
|
|
|
if (! DolibarrApiAccess::$user->rights->contrat->creer) { |
607
|
|
|
throw new RestException(401); |
608
|
|
|
} |
609
|
|
|
$result = $this->contract->fetch($id); |
610
|
|
|
if (! $result) { |
611
|
|
|
throw new RestException(404, 'Contract not found'); |
612
|
|
|
} |
613
|
|
|
|
614
|
|
|
if (! DolibarrApi::_checkAccessToResource('contrat',$this->contract->id)) { |
615
|
|
|
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
616
|
|
|
} |
617
|
|
|
|
618
|
|
|
$result = $this->contract->closeAll(DolibarrApiAccess::$user, $notrigger); |
619
|
|
|
if ($result == 0) { |
620
|
|
|
throw new RestException(304, 'Error nothing done. May be object is already close'); |
621
|
|
|
} |
622
|
|
|
if ($result < 0) { |
623
|
|
|
throw new RestException(500, 'Error when closing Contract: '.$this->contract->error); |
624
|
|
|
} |
625
|
|
|
|
626
|
|
|
return array( |
627
|
|
|
'success' => array( |
628
|
|
|
'code' => 200, |
629
|
|
|
'message' => 'Contract closed (Ref='.$this->contract->ref.'). All services were closed.' |
630
|
|
|
) |
631
|
|
|
); |
632
|
|
|
} |
633
|
|
|
|
634
|
|
|
|
635
|
|
|
|
636
|
|
|
/** |
637
|
|
|
* Clean sensible object datas |
638
|
|
|
* |
639
|
|
|
* @param object $object Object to clean |
640
|
|
|
* @return array Array of cleaned object properties |
641
|
|
|
*/ |
642
|
|
|
function _cleanObjectDatas($object) |
643
|
|
|
{ |
644
|
|
|
|
645
|
|
|
$object = parent::_cleanObjectDatas($object); |
646
|
|
|
|
647
|
|
|
unset($object->address); |
648
|
|
|
|
649
|
|
|
unset($object->date_ouverture_prevue); |
650
|
|
|
unset($object->date_ouverture); |
651
|
|
|
unset($object->date_fin_validite); |
652
|
|
|
unset($object->date_cloture); |
653
|
|
|
unset($object->date_debut_prevue); |
654
|
|
|
unset($object->date_debut_reel); |
655
|
|
|
unset($object->date_fin_prevue); |
656
|
|
|
unset($object->date_fin_reel); |
657
|
|
|
unset($object->civility_id); |
658
|
|
|
|
659
|
|
|
return $object; |
660
|
|
|
} |
661
|
|
|
|
662
|
|
|
/** |
663
|
|
|
* Validate fields before create or update object |
664
|
|
|
* |
665
|
|
|
* @param array $data Array with data to verify |
666
|
|
|
* @return array |
667
|
|
|
* @throws RestException |
668
|
|
|
*/ |
669
|
|
|
function _validate($data) |
670
|
|
|
{ |
671
|
|
|
$contrat = array(); |
672
|
|
|
foreach (Contracts::$FIELDS as $field) { |
673
|
|
|
if (!isset($data[$field])) |
674
|
|
|
throw new RestException(400, "$field field missing"); |
675
|
|
|
$contrat[$field] = $data[$field]; |
676
|
|
|
} |
677
|
|
|
return $contrat; |
678
|
|
|
} |
679
|
|
|
} |
680
|
|
|
|