|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* Copyright (C) 2015 Jean-François Ferry <[email protected]> |
|
4
|
|
|
* Copyright (C) 2016 Laurent Destailleur <[email protected]> |
|
5
|
|
|
* Copyright (C) 2024 Rafael San José <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* This program is free software; you can redistribute it and/or modify |
|
8
|
|
|
* it under the terms of the GNU General Public License as published by |
|
9
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
10
|
|
|
* (at your option) any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU General Public License |
|
18
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace Dolibarr\Code\Expedition\Api; |
|
22
|
|
|
|
|
23
|
|
|
use Dolibarr\Core\Base\DolibarrApi; |
|
24
|
|
|
use Luracast\Restler\RestException; |
|
25
|
|
|
|
|
26
|
|
|
require_once constant('DOL_DOCUMENT_ROOT') . '/expedition/class/expedition.class.php'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* API class for shipments |
|
30
|
|
|
* |
|
31
|
|
|
* @access protected |
|
32
|
|
|
* @class DolibarrApiAccess {@requires user,external} |
|
33
|
|
|
*/ |
|
34
|
|
|
class Shipments extends DolibarrApi |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* @var array $FIELDS Mandatory fields, checked when create and update object |
|
38
|
|
|
*/ |
|
39
|
|
|
public static $FIELDS = array( |
|
40
|
|
|
'socid', |
|
41
|
|
|
'origin_id', |
|
42
|
|
|
'origin_type', |
|
43
|
|
|
); |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var Expedition $shipment {@type Expedition} |
|
47
|
|
|
*/ |
|
48
|
|
|
public $shipment; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Constructor |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct() |
|
54
|
|
|
{ |
|
55
|
|
|
global $db, $conf; |
|
56
|
|
|
$this->db = $db; |
|
57
|
|
|
$this->shipment = new Expedition($this->db); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Get properties of a shipment object |
|
62
|
|
|
* |
|
63
|
|
|
* Return an array with shipment information |
|
64
|
|
|
* |
|
65
|
|
|
* @param int $id ID of shipment |
|
66
|
|
|
* @return Object Object with cleaned properties |
|
67
|
|
|
* |
|
68
|
|
|
* @throws RestException |
|
69
|
|
|
*/ |
|
70
|
|
|
public function get($id) |
|
71
|
|
|
{ |
|
72
|
|
|
if (!DolibarrApiAccess::$user->hasRight('expedition', 'lire')) { |
|
73
|
|
|
throw new RestException(403); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$result = $this->shipment->fetch($id); |
|
77
|
|
|
if (!$result) { |
|
78
|
|
|
throw new RestException(404, 'Shipment not found'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if (!DolibarrApi::_checkAccessToResource('expedition', $this->shipment->id)) { |
|
82
|
|
|
throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$this->shipment->fetchObjectLinked(); |
|
86
|
|
|
return $this->_cleanObjectDatas($this->shipment); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* List shipments |
|
93
|
|
|
* |
|
94
|
|
|
* Get a list of shipments |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $sortfield Sort field |
|
97
|
|
|
* @param string $sortorder Sort order |
|
98
|
|
|
* @param int $limit Limit for list |
|
99
|
|
|
* @param int $page Page number |
|
100
|
|
|
* @param string $thirdparty_ids Thirdparty ids to filter shipments of (example '1' or '1,2,3') {@pattern /^[0-9,]*$/i} |
|
101
|
|
|
* @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')" |
|
102
|
|
|
* @param string $properties Restrict the data returned to these properties. Ignored if empty. Comma separated list of properties names |
|
103
|
|
|
* @return array Array of shipment objects |
|
104
|
|
|
* |
|
105
|
|
|
* @throws RestException |
|
106
|
|
|
*/ |
|
107
|
|
|
public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '', $properties = '') |
|
108
|
|
|
{ |
|
109
|
|
|
if (!DolibarrApiAccess::$user->hasRight('expedition', 'lire')) { |
|
110
|
|
|
throw new RestException(403); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$obj_ret = array(); |
|
114
|
|
|
|
|
115
|
|
|
// case of external user, $thirdparty_ids param is ignored and replaced by user's socid |
|
116
|
|
|
$socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids; |
|
117
|
|
|
|
|
118
|
|
|
// If the internal user must only see his customers, force searching by him |
|
119
|
|
|
$search_sale = 0; |
|
120
|
|
|
if (!DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socids) { |
|
121
|
|
|
$search_sale = DolibarrApiAccess::$user->id; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
$sql = "SELECT t.rowid"; |
|
125
|
|
|
$sql .= " FROM " . MAIN_DB_PREFIX . "expedition AS t"; |
|
126
|
|
|
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "expedition_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 |
|
127
|
|
|
$sql .= ' WHERE t.entity IN (' . getEntity('expedition') . ')'; |
|
128
|
|
|
if ($socids) { |
|
129
|
|
|
$sql .= " AND t.fk_soc IN (" . $this->db->sanitize($socids) . ")"; |
|
130
|
|
|
} |
|
131
|
|
|
// Search on sale representative |
|
132
|
|
|
if ($search_sale && $search_sale != '-1') { |
|
133
|
|
|
if ($search_sale == -2) { |
|
134
|
|
|
$sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM " . MAIN_DB_PREFIX . "societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)"; |
|
135
|
|
|
} elseif ($search_sale > 0) { |
|
136
|
|
|
$sql .= " AND EXISTS (SELECT sc.fk_soc FROM " . MAIN_DB_PREFIX . "societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc AND sc.fk_user = " . ((int) $search_sale) . ")"; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
// Add sql filters |
|
140
|
|
|
if ($sqlfilters) { |
|
141
|
|
|
$errormessage = ''; |
|
142
|
|
|
$sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage); |
|
143
|
|
|
if ($errormessage) { |
|
144
|
|
|
throw new RestException(400, 'Error when validating parameter sqlfilters -> ' . $errormessage); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
$sql .= $this->db->order($sortfield, $sortorder); |
|
149
|
|
|
if ($limit) { |
|
150
|
|
|
if ($page < 0) { |
|
151
|
|
|
$page = 0; |
|
152
|
|
|
} |
|
153
|
|
|
$offset = $limit * $page; |
|
154
|
|
|
|
|
155
|
|
|
$sql .= $this->db->plimit($limit + 1, $offset); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
dol_syslog("API Rest request"); |
|
159
|
|
|
$result = $this->db->query($sql); |
|
160
|
|
|
|
|
161
|
|
|
if ($result) { |
|
162
|
|
|
$num = $this->db->num_rows($result); |
|
163
|
|
|
$min = min($num, ($limit <= 0 ? $num : $limit)); |
|
164
|
|
|
$i = 0; |
|
165
|
|
|
while ($i < $min) { |
|
166
|
|
|
$obj = $this->db->fetch_object($result); |
|
167
|
|
|
$shipment_static = new Expedition($this->db); |
|
168
|
|
|
if ($shipment_static->fetch($obj->rowid)) { |
|
169
|
|
|
$obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($shipment_static), $properties); |
|
170
|
|
|
} |
|
171
|
|
|
$i++; |
|
172
|
|
|
} |
|
173
|
|
|
} else { |
|
174
|
|
|
throw new RestException(503, 'Error when retrieve commande list : ' . $this->db->lasterror()); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
return $obj_ret; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Create shipment object |
|
182
|
|
|
* |
|
183
|
|
|
* @param array $request_data Request data |
|
184
|
|
|
* @return int ID of shipment created |
|
185
|
|
|
*/ |
|
186
|
|
|
public function post($request_data = null) |
|
187
|
|
|
{ |
|
188
|
|
|
if (!DolibarrApiAccess::$user->hasRight('expedition', 'creer')) { |
|
189
|
|
|
throw new RestException(403, "Insuffisant rights"); |
|
190
|
|
|
} |
|
191
|
|
|
// Check mandatory fields |
|
192
|
|
|
$result = $this->_validate($request_data); |
|
193
|
|
|
|
|
194
|
|
|
foreach ($request_data as $field => $value) { |
|
195
|
|
|
if ($field === 'caller') { |
|
196
|
|
|
// 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 |
|
197
|
|
|
$this->shipment->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09'); |
|
198
|
|
|
continue; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
$this->shipment->$field = $this->_checkValForAPI($field, $value, $this->shipment); |
|
202
|
|
|
} |
|
203
|
|
|
if (isset($request_data["lines"])) { |
|
204
|
|
|
$lines = array(); |
|
205
|
|
|
foreach ($request_data["lines"] as $line) { |
|
206
|
|
|
$shipmentline = new ExpeditionLigne($this->db); |
|
207
|
|
|
|
|
208
|
|
|
$shipmentline->entrepot_id = $line['entrepot_id']; |
|
209
|
|
|
$shipmentline->fk_element = $line['fk_element'] ?? $line['origin_id']; // example: order id. this->origin is 'commande' |
|
210
|
|
|
$shipmentline->origin_line_id = $line['fk_elementdet'] ?? $line['origin_line_id']; // example: order id |
|
211
|
|
|
$shipmentline->fk_elementdet = $line['fk_elementdet'] ?? $line['origin_line_id']; // example: order line id |
|
212
|
|
|
$shipmentline->origin_type = $line['element_type'] ?? $line['origin_type']; // example 'commande' or 'order' |
|
213
|
|
|
$shipmentline->element_type = $line['element_type'] ?? $line['origin_type']; // example 'commande' or 'order' |
|
214
|
|
|
$shipmentline->qty = $line['qty']; |
|
215
|
|
|
$shipmentline->rang = $line['rang']; |
|
216
|
|
|
$shipmentline->array_options = $line['array_options']; |
|
217
|
|
|
$shipmentline->detail_batch = $line['detail_batch']; |
|
218
|
|
|
|
|
219
|
|
|
$lines[] = $shipmentline; |
|
220
|
|
|
} |
|
221
|
|
|
$this->shipment->lines = $lines; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
if ($this->shipment->create(DolibarrApiAccess::$user) < 0) { |
|
225
|
|
|
throw new RestException(500, "Error creating shipment", array_merge(array($this->shipment->error), $this->shipment->errors)); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
return $this->shipment->id; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
// /** |
|
232
|
|
|
// * Get lines of an shipment |
|
233
|
|
|
// * |
|
234
|
|
|
// * @param int $id Id of shipment |
|
235
|
|
|
// * |
|
236
|
|
|
// * @url GET {id}/lines |
|
237
|
|
|
// * |
|
238
|
|
|
// * @return int |
|
239
|
|
|
// */ |
|
240
|
|
|
/* |
|
241
|
|
|
public function getLines($id) |
|
242
|
|
|
{ |
|
243
|
|
|
if(! DolibarrApiAccess::$user->hasRight('expedition', 'lire')) { |
|
244
|
|
|
throw new RestException(403); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
$result = $this->shipment->fetch($id); |
|
248
|
|
|
if( ! $result ) { |
|
249
|
|
|
throw new RestException(404, 'Shipment not found'); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
if( ! DolibarrApi::_checkAccessToResource('expedition',$this->shipment->id)) { |
|
253
|
|
|
throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
|
254
|
|
|
} |
|
255
|
|
|
$this->shipment->getLinesArray(); |
|
256
|
|
|
$result = array(); |
|
257
|
|
|
foreach ($this->shipment->lines as $line) { |
|
258
|
|
|
array_push($result,$this->_cleanObjectDatas($line)); |
|
259
|
|
|
} |
|
260
|
|
|
return $result; |
|
261
|
|
|
} |
|
262
|
|
|
*/ |
|
263
|
|
|
|
|
264
|
|
|
// /** |
|
265
|
|
|
// * Add a line to given shipment |
|
266
|
|
|
// * |
|
267
|
|
|
// * @param int $id Id of shipment to update |
|
268
|
|
|
// * @param array $request_data ShipmentLine data |
|
269
|
|
|
// * |
|
270
|
|
|
// * @url POST {id}/lines |
|
271
|
|
|
// * |
|
272
|
|
|
// * @return int |
|
273
|
|
|
// */ |
|
274
|
|
|
/* |
|
275
|
|
|
public function postLine($id, $request_data = null) |
|
276
|
|
|
{ |
|
277
|
|
|
if(! DolibarrApiAccess::$user->hasRight('expedition', 'creer')) { |
|
278
|
|
|
throw new RestException(403); |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
$result = $this->shipment->fetch($id); |
|
282
|
|
|
if ( ! $result ) { |
|
283
|
|
|
throw new RestException(404, 'Shipment not found'); |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
if( ! DolibarrApi::_checkAccessToResource('expedition',$this->shipment->id)) { |
|
287
|
|
|
throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
$request_data = (object) $request_data; |
|
291
|
|
|
|
|
292
|
|
|
$request_data->desc = sanitizeVal($request_data->desc, 'restricthtml'); |
|
293
|
|
|
$request_data->label = sanitizeVal($request_data->label); |
|
294
|
|
|
|
|
295
|
|
|
$updateRes = $this->shipment->addline( |
|
296
|
|
|
$request_data->desc, |
|
297
|
|
|
$request_data->subprice, |
|
298
|
|
|
$request_data->qty, |
|
299
|
|
|
$request_data->tva_tx, |
|
300
|
|
|
$request_data->localtax1_tx, |
|
301
|
|
|
$request_data->localtax2_tx, |
|
302
|
|
|
$request_data->fk_product, |
|
303
|
|
|
$request_data->remise_percent, |
|
304
|
|
|
$request_data->info_bits, |
|
305
|
|
|
$request_data->fk_remise_except, |
|
306
|
|
|
'HT', |
|
307
|
|
|
0, |
|
308
|
|
|
$request_data->date_start, |
|
309
|
|
|
$request_data->date_end, |
|
310
|
|
|
$request_data->product_type, |
|
311
|
|
|
$request_data->rang, |
|
312
|
|
|
$request_data->special_code, |
|
313
|
|
|
$fk_parent_line, |
|
314
|
|
|
$request_data->fk_fournprice, |
|
315
|
|
|
$request_data->pa_ht, |
|
316
|
|
|
$request_data->label, |
|
317
|
|
|
$request_data->array_options, |
|
318
|
|
|
$request_data->fk_unit, |
|
319
|
|
|
$request_data->origin, |
|
320
|
|
|
$request_data->origin_id, |
|
321
|
|
|
$request_data->multicurrency_subprice |
|
322
|
|
|
); |
|
323
|
|
|
|
|
324
|
|
|
if ($updateRes > 0) { |
|
325
|
|
|
return $updateRes; |
|
326
|
|
|
|
|
327
|
|
|
} |
|
328
|
|
|
return false; |
|
329
|
|
|
}*/ |
|
330
|
|
|
|
|
331
|
|
|
// /** |
|
332
|
|
|
// * Update a line to given shipment |
|
333
|
|
|
// * |
|
334
|
|
|
// * @param int $id Id of shipment to update |
|
335
|
|
|
// * @param int $lineid Id of line to update |
|
336
|
|
|
// * @param array $request_data ShipmentLine data |
|
337
|
|
|
// * |
|
338
|
|
|
// * @url PUT {id}/lines/{lineid} |
|
339
|
|
|
// * |
|
340
|
|
|
// * @return object |
|
341
|
|
|
// */ |
|
342
|
|
|
/* |
|
343
|
|
|
public function putLine($id, $lineid, $request_data = null) |
|
344
|
|
|
{ |
|
345
|
|
|
if (! DolibarrApiAccess::$user->hasRight('expedition', 'creer')) { |
|
346
|
|
|
throw new RestException(403); |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
$result = $this->shipment->fetch($id); |
|
350
|
|
|
if ( ! $result ) { |
|
351
|
|
|
throw new RestException(404, 'Shipment not found'); |
|
352
|
|
|
} |
|
353
|
|
|
|
|
354
|
|
|
if( ! DolibarrApi::_checkAccessToResource('expedition',$this->shipment->id)) { |
|
355
|
|
|
throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
|
|
$request_data = (object) $request_data; |
|
359
|
|
|
|
|
360
|
|
|
$request_data->desc = sanitizeVal($request_data->desc, 'restricthtml'); |
|
361
|
|
|
$request_data->label = sanitizeVal($request_data->label); |
|
362
|
|
|
|
|
363
|
|
|
$updateRes = $this->shipment->updateline( |
|
364
|
|
|
$lineid, |
|
365
|
|
|
$request_data->desc, |
|
366
|
|
|
$request_data->subprice, |
|
367
|
|
|
$request_data->qty, |
|
368
|
|
|
$request_data->remise_percent, |
|
369
|
|
|
$request_data->tva_tx, |
|
370
|
|
|
$request_data->localtax1_tx, |
|
371
|
|
|
$request_data->localtax2_tx, |
|
372
|
|
|
'HT', |
|
373
|
|
|
$request_data->info_bits, |
|
374
|
|
|
$request_data->date_start, |
|
375
|
|
|
$request_data->date_end, |
|
376
|
|
|
$request_data->product_type, |
|
377
|
|
|
$request_data->fk_parent_line, |
|
378
|
|
|
0, |
|
379
|
|
|
$request_data->fk_fournprice, |
|
380
|
|
|
$request_data->pa_ht, |
|
381
|
|
|
$request_data->label, |
|
382
|
|
|
$request_data->special_code, |
|
383
|
|
|
$request_data->array_options, |
|
384
|
|
|
$request_data->fk_unit, |
|
385
|
|
|
$request_data->multicurrency_subprice |
|
386
|
|
|
); |
|
387
|
|
|
|
|
388
|
|
|
if ($updateRes > 0) { |
|
389
|
|
|
$result = $this->get($id); |
|
390
|
|
|
unset($result->line); |
|
391
|
|
|
return $this->_cleanObjectDatas($result); |
|
392
|
|
|
} |
|
393
|
|
|
return false; |
|
394
|
|
|
}*/ |
|
395
|
|
|
|
|
396
|
|
|
/** |
|
397
|
|
|
* Delete a line to given shipment |
|
398
|
|
|
* |
|
399
|
|
|
* |
|
400
|
|
|
* @param int $id Id of shipment to update |
|
401
|
|
|
* @param int $lineid Id of line to delete |
|
402
|
|
|
* |
|
403
|
|
|
* @url DELETE {id}/lines/{lineid} |
|
404
|
|
|
* |
|
405
|
|
|
* @return array |
|
406
|
|
|
* |
|
407
|
|
|
* @throws RestException 401 |
|
408
|
|
|
* @throws RestException 404 |
|
409
|
|
|
*/ |
|
410
|
|
|
public function deleteLine($id, $lineid) |
|
411
|
|
|
{ |
|
412
|
|
|
if (!DolibarrApiAccess::$user->hasRight('expedition', 'creer')) { |
|
413
|
|
|
throw new RestException(403); |
|
414
|
|
|
} |
|
415
|
|
|
|
|
416
|
|
|
$result = $this->shipment->fetch($id); |
|
417
|
|
|
if (!$result) { |
|
418
|
|
|
throw new RestException(404, 'Shipment not found'); |
|
419
|
|
|
} |
|
420
|
|
|
|
|
421
|
|
|
if (!DolibarrApi::_checkAccessToResource('expedition', $this->shipment->id)) { |
|
422
|
|
|
throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login); |
|
423
|
|
|
} |
|
424
|
|
|
|
|
425
|
|
|
// TODO Check the lineid $lineid is a line of object |
|
426
|
|
|
|
|
427
|
|
|
$updateRes = $this->shipment->deleteLine(DolibarrApiAccess::$user, $lineid); |
|
428
|
|
|
if ($updateRes > 0) { |
|
429
|
|
|
return array( |
|
430
|
|
|
'success' => array( |
|
431
|
|
|
'code' => 200, |
|
432
|
|
|
'message' => 'line ' . $lineid . ' deleted' |
|
433
|
|
|
) |
|
434
|
|
|
); |
|
435
|
|
|
} else { |
|
436
|
|
|
throw new RestException(405, $this->shipment->error); |
|
437
|
|
|
} |
|
438
|
|
|
} |
|
439
|
|
|
|
|
440
|
|
|
/** |
|
441
|
|
|
* Update shipment general fields (won't touch lines of shipment) |
|
442
|
|
|
* |
|
443
|
|
|
* @param int $id Id of shipment to update |
|
444
|
|
|
* @param array $request_data Datas |
|
445
|
|
|
* @return Object Updated object |
|
446
|
|
|
*/ |
|
447
|
|
|
public function put($id, $request_data = null) |
|
448
|
|
|
{ |
|
449
|
|
|
if (!DolibarrApiAccess::$user->hasRight('expedition', 'creer')) { |
|
450
|
|
|
throw new RestException(403); |
|
451
|
|
|
} |
|
452
|
|
|
|
|
453
|
|
|
$result = $this->shipment->fetch($id); |
|
454
|
|
|
if (!$result) { |
|
455
|
|
|
throw new RestException(404, 'Shipment not found'); |
|
456
|
|
|
} |
|
457
|
|
|
|
|
458
|
|
|
if (!DolibarrApi::_checkAccessToResource('expedition', $this->shipment->id)) { |
|
459
|
|
|
throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login); |
|
460
|
|
|
} |
|
461
|
|
|
foreach ($request_data as $field => $value) { |
|
462
|
|
|
if ($field == 'id') { |
|
463
|
|
|
continue; |
|
464
|
|
|
} |
|
465
|
|
|
if ($field === 'caller') { |
|
466
|
|
|
// 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 |
|
467
|
|
|
$this->shipment->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09'); |
|
468
|
|
|
continue; |
|
469
|
|
|
} |
|
470
|
|
|
|
|
471
|
|
|
$this->shipment->$field = $this->_checkValForAPI($field, $value, $this->shipment); |
|
472
|
|
|
} |
|
473
|
|
|
|
|
474
|
|
|
if ($this->shipment->update(DolibarrApiAccess::$user) > 0) { |
|
475
|
|
|
return $this->get($id); |
|
476
|
|
|
} else { |
|
477
|
|
|
throw new RestException(500, $this->shipment->error); |
|
478
|
|
|
} |
|
479
|
|
|
} |
|
480
|
|
|
|
|
481
|
|
|
/** |
|
482
|
|
|
* Delete shipment |
|
483
|
|
|
* |
|
484
|
|
|
* @param int $id Shipment ID |
|
485
|
|
|
* |
|
486
|
|
|
* @return array |
|
487
|
|
|
*/ |
|
488
|
|
|
public function delete($id) |
|
489
|
|
|
{ |
|
490
|
|
|
if (!DolibarrApiAccess::$user->hasRight('expedition', 'supprimer')) { |
|
491
|
|
|
throw new RestException(403); |
|
492
|
|
|
} |
|
493
|
|
|
$result = $this->shipment->fetch($id); |
|
494
|
|
|
if (!$result) { |
|
495
|
|
|
throw new RestException(404, 'Shipment not found'); |
|
496
|
|
|
} |
|
497
|
|
|
|
|
498
|
|
|
if (!DolibarrApi::_checkAccessToResource('expedition', $this->shipment->id)) { |
|
499
|
|
|
throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login); |
|
500
|
|
|
} |
|
501
|
|
|
|
|
502
|
|
|
if (!$this->shipment->delete(DolibarrApiAccess::$user)) { |
|
503
|
|
|
throw new RestException(500, 'Error when deleting shipment : ' . $this->shipment->error); |
|
504
|
|
|
} |
|
505
|
|
|
|
|
506
|
|
|
return array( |
|
507
|
|
|
'success' => array( |
|
508
|
|
|
'code' => 200, |
|
509
|
|
|
'message' => 'Shipment deleted' |
|
510
|
|
|
) |
|
511
|
|
|
); |
|
512
|
|
|
} |
|
513
|
|
|
|
|
514
|
|
|
/** |
|
515
|
|
|
* Validate a shipment |
|
516
|
|
|
* |
|
517
|
|
|
* This may record stock movements if module stock is enabled and option to |
|
518
|
|
|
* decrease stock on shipment is on. |
|
519
|
|
|
* |
|
520
|
|
|
* @param int $id Shipment ID |
|
521
|
|
|
* @param int $notrigger 1=Does not execute triggers, 0= execute triggers |
|
522
|
|
|
* |
|
523
|
|
|
* @url POST {id}/validate |
|
524
|
|
|
* |
|
525
|
|
|
* @return object |
|
526
|
|
|
* \todo An error 403 is returned if the request has an empty body. |
|
527
|
|
|
* Error message: "Forbidden: Content type `text/plain` is not supported." |
|
528
|
|
|
* Workaround: send this in the body |
|
529
|
|
|
* { |
|
530
|
|
|
* "notrigger": 0 |
|
531
|
|
|
* } |
|
532
|
|
|
*/ |
|
533
|
|
|
public function validate($id, $notrigger = 0) |
|
534
|
|
|
{ |
|
535
|
|
|
if (!DolibarrApiAccess::$user->hasRight('expedition', 'creer')) { |
|
536
|
|
|
throw new RestException(403); |
|
537
|
|
|
} |
|
538
|
|
|
$result = $this->shipment->fetch($id); |
|
539
|
|
|
if (!$result) { |
|
540
|
|
|
throw new RestException(404, 'Shipment not found'); |
|
541
|
|
|
} |
|
542
|
|
|
|
|
543
|
|
|
if (!DolibarrApi::_checkAccessToResource('expedition', $this->shipment->id)) { |
|
544
|
|
|
throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login); |
|
545
|
|
|
} |
|
546
|
|
|
|
|
547
|
|
|
$result = $this->shipment->valid(DolibarrApiAccess::$user, $notrigger); |
|
548
|
|
|
if ($result == 0) { |
|
549
|
|
|
throw new RestException(304, 'Error nothing done. May be object is already validated'); |
|
550
|
|
|
} |
|
551
|
|
|
if ($result < 0) { |
|
552
|
|
|
throw new RestException(500, 'Error when validating Shipment: ' . $this->shipment->error); |
|
553
|
|
|
} |
|
554
|
|
|
|
|
555
|
|
|
// Reload shipment |
|
556
|
|
|
$result = $this->shipment->fetch($id); |
|
557
|
|
|
|
|
558
|
|
|
$this->shipment->fetchObjectLinked(); |
|
559
|
|
|
return $this->_cleanObjectDatas($this->shipment); |
|
560
|
|
|
} |
|
561
|
|
|
|
|
562
|
|
|
|
|
563
|
|
|
// /** |
|
564
|
|
|
// * Classify the shipment as invoiced |
|
565
|
|
|
// * |
|
566
|
|
|
// * @param int $id Id of the shipment |
|
567
|
|
|
// * |
|
568
|
|
|
// * @url POST {id}/setinvoiced |
|
569
|
|
|
// * |
|
570
|
|
|
// * @return int |
|
571
|
|
|
// * |
|
572
|
|
|
// * @throws RestException 400 |
|
573
|
|
|
// * @throws RestException 401 |
|
574
|
|
|
// * @throws RestException 404 |
|
575
|
|
|
// * @throws RestException 405 |
|
576
|
|
|
// */ |
|
577
|
|
|
/* |
|
578
|
|
|
public function setinvoiced($id) |
|
579
|
|
|
{ |
|
580
|
|
|
|
|
581
|
|
|
if(! DolibarrApiAccess::$user->hasRight('expedition', 'creer')) { |
|
582
|
|
|
throw new RestException(403); |
|
583
|
|
|
} |
|
584
|
|
|
if(empty($id)) { |
|
585
|
|
|
throw new RestException(400, 'Shipment ID is mandatory'); |
|
586
|
|
|
} |
|
587
|
|
|
$result = $this->shipment->fetch($id); |
|
588
|
|
|
if( ! $result ) { |
|
589
|
|
|
throw new RestException(404, 'Shipment not found'); |
|
590
|
|
|
} |
|
591
|
|
|
|
|
592
|
|
|
$result = $this->shipment->classifyBilled(DolibarrApiAccess::$user); |
|
593
|
|
|
if( $result < 0) { |
|
594
|
|
|
throw new RestException(400, $this->shipment->error); |
|
595
|
|
|
} |
|
596
|
|
|
return $result; |
|
597
|
|
|
} |
|
598
|
|
|
*/ |
|
599
|
|
|
|
|
600
|
|
|
|
|
601
|
|
|
// /** |
|
602
|
|
|
// * Create a shipment using an existing order. |
|
603
|
|
|
// * |
|
604
|
|
|
// * @param int $orderid Id of the order |
|
605
|
|
|
// * |
|
606
|
|
|
// * @url POST /createfromorder/{orderid} |
|
607
|
|
|
// * |
|
608
|
|
|
// * @return int |
|
609
|
|
|
// * @throws RestException 400 |
|
610
|
|
|
// * @throws RestException 401 |
|
611
|
|
|
// * @throws RestException 404 |
|
612
|
|
|
// * @throws RestException 405 |
|
613
|
|
|
// */ |
|
614
|
|
|
/* |
|
615
|
|
|
public function createShipmentFromOrder($orderid) |
|
616
|
|
|
{ |
|
617
|
|
|
|
|
618
|
|
|
require_once constant('DOL_DOCUMENT_ROOT') . '/commande/class/commande.class.php'; |
|
619
|
|
|
|
|
620
|
|
|
if(! DolibarrApiAccess::$user->hasRight('expedition', 'lire')) { |
|
621
|
|
|
throw new RestException(403); |
|
622
|
|
|
} |
|
623
|
|
|
if(! DolibarrApiAccess::$user->hasRight('expedition', 'creer')) { |
|
624
|
|
|
throw new RestException(403); |
|
625
|
|
|
} |
|
626
|
|
|
if(empty($proposalid)) { |
|
627
|
|
|
throw new RestException(400, 'Order ID is mandatory'); |
|
628
|
|
|
} |
|
629
|
|
|
|
|
630
|
|
|
$order = new Commande($this->db); |
|
631
|
|
|
$result = $order->fetch($proposalid); |
|
632
|
|
|
if( ! $result ) { |
|
633
|
|
|
throw new RestException(404, 'Order not found'); |
|
634
|
|
|
} |
|
635
|
|
|
|
|
636
|
|
|
$result = $this->shipment->createFromOrder($order, DolibarrApiAccess::$user); |
|
637
|
|
|
if( $result < 0) { |
|
638
|
|
|
throw new RestException(405, $this->shipment->error); |
|
639
|
|
|
} |
|
640
|
|
|
$this->shipment->fetchObjectLinked(); |
|
641
|
|
|
return $this->_cleanObjectDatas($this->shipment); |
|
642
|
|
|
} |
|
643
|
|
|
*/ |
|
644
|
|
|
|
|
645
|
|
|
/** |
|
646
|
|
|
* Close a shipment (Classify it as "Delivered") |
|
647
|
|
|
* |
|
648
|
|
|
* @param int $id Expedition ID |
|
649
|
|
|
* @param int $notrigger Disabled triggers |
|
650
|
|
|
* |
|
651
|
|
|
* @url POST {id}/close |
|
652
|
|
|
* |
|
653
|
|
|
* @return object |
|
654
|
|
|
*/ |
|
655
|
|
|
public function close($id, $notrigger = 0) |
|
656
|
|
|
{ |
|
657
|
|
|
if (!DolibarrApiAccess::$user->hasRight('expedition', 'creer')) { |
|
658
|
|
|
throw new RestException(403); |
|
659
|
|
|
} |
|
660
|
|
|
|
|
661
|
|
|
$result = $this->shipment->fetch($id); |
|
662
|
|
|
if (!$result) { |
|
663
|
|
|
throw new RestException(404, 'Shipment not found'); |
|
664
|
|
|
} |
|
665
|
|
|
|
|
666
|
|
|
if (!DolibarrApi::_checkAccessToResource('expedition', $this->shipment->id)) { |
|
667
|
|
|
throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login); |
|
668
|
|
|
} |
|
669
|
|
|
|
|
670
|
|
|
$result = $this->shipment->setClosed(); |
|
671
|
|
|
if ($result == 0) { |
|
672
|
|
|
throw new RestException(304, 'Error nothing done. May be object is already closed'); |
|
673
|
|
|
} |
|
674
|
|
|
if ($result < 0) { |
|
675
|
|
|
throw new RestException(500, 'Error when closing Order: ' . $this->shipment->error); |
|
676
|
|
|
} |
|
677
|
|
|
|
|
678
|
|
|
// Reload shipment |
|
679
|
|
|
$result = $this->shipment->fetch($id); |
|
680
|
|
|
|
|
681
|
|
|
$this->shipment->fetchObjectLinked(); |
|
682
|
|
|
|
|
683
|
|
|
return $this->_cleanObjectDatas($this->shipment); |
|
684
|
|
|
} |
|
685
|
|
|
|
|
686
|
|
|
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore |
|
687
|
|
|
/** |
|
688
|
|
|
* Clean sensible object datas |
|
689
|
|
|
* |
|
690
|
|
|
* @param Object $object Object to clean |
|
691
|
|
|
* @return Object Object with cleaned properties |
|
692
|
|
|
*/ |
|
693
|
|
|
protected function _cleanObjectDatas($object) |
|
694
|
|
|
{ |
|
695
|
|
|
// phpcs:enable |
|
696
|
|
|
$object = parent::_cleanObjectDatas($object); |
|
697
|
|
|
|
|
698
|
|
|
unset($object->canvas); |
|
699
|
|
|
|
|
700
|
|
|
unset($object->thirdparty); // id already returned |
|
701
|
|
|
|
|
702
|
|
|
unset($object->note); |
|
703
|
|
|
unset($object->address); |
|
704
|
|
|
unset($object->barcode_type); |
|
705
|
|
|
unset($object->barcode_type_code); |
|
706
|
|
|
unset($object->barcode_type_label); |
|
707
|
|
|
unset($object->barcode_type_coder); |
|
708
|
|
|
|
|
709
|
|
|
if (!empty($object->lines) && is_array($object->lines)) { |
|
710
|
|
|
foreach ($object->lines as $line) { |
|
711
|
|
|
if (is_array($line->detail_batch)) { |
|
712
|
|
|
foreach ($line->detail_batch as $keytmp2 => $valtmp2) { |
|
713
|
|
|
unset($line->detail_batch[$keytmp2]->db); |
|
714
|
|
|
} |
|
715
|
|
|
} |
|
716
|
|
|
unset($line->canvas); |
|
717
|
|
|
|
|
718
|
|
|
unset($line->tva_tx); |
|
719
|
|
|
unset($line->vat_src_code); |
|
720
|
|
|
unset($line->total_ht); |
|
721
|
|
|
unset($line->total_ttc); |
|
722
|
|
|
unset($line->total_tva); |
|
723
|
|
|
unset($line->total_localtax1); |
|
724
|
|
|
unset($line->total_localtax2); |
|
725
|
|
|
unset($line->remise_percent); |
|
726
|
|
|
} |
|
727
|
|
|
} |
|
728
|
|
|
|
|
729
|
|
|
return $object; |
|
730
|
|
|
} |
|
731
|
|
|
|
|
732
|
|
|
/** |
|
733
|
|
|
* Validate fields before create or update object |
|
734
|
|
|
* |
|
735
|
|
|
* @param array $data Array with data to verify |
|
736
|
|
|
* @return array |
|
737
|
|
|
* @throws RestException |
|
738
|
|
|
*/ |
|
739
|
|
|
private function _validate($data) |
|
740
|
|
|
{ |
|
741
|
|
|
$shipment = array(); |
|
742
|
|
|
foreach (Shipments::$FIELDS as $field) { |
|
743
|
|
|
if (!isset($data[$field])) { |
|
744
|
|
|
throw new RestException(400, "$field field missing"); |
|
745
|
|
|
} |
|
746
|
|
|
$shipment[$field] = $data[$field]; |
|
747
|
|
|
} |
|
748
|
|
|
return $shipment; |
|
749
|
|
|
} |
|
750
|
|
|
} |
|
751
|
|
|
|