|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* Copyright (C) 2015 Jean-François Ferry <[email protected]> |
|
4
|
|
|
* Copyright (C) 2019 Maxime Kohlhaas <[email protected]> |
|
5
|
|
|
* Copyright (C) 2020-2024 Frédéric France <[email protected]> |
|
6
|
|
|
* Copyright (C) 2022 Christian Humpel <[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\Bom\Api; |
|
24
|
|
|
|
|
25
|
|
|
use Dolibarr\Code\Api\Classes\DolibarrApiAccess; |
|
26
|
|
|
use Dolibarr\Code\Bom\Classes\BOM; |
|
27
|
|
|
use Dolibarr\Core\Base\DolibarrApi; |
|
28
|
|
|
use Luracast\Restler\RestException; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* \file htdocs/bom/class/api_boms.class.php |
|
32
|
|
|
* \ingroup bom |
|
33
|
|
|
* \brief File for API management of BOM. |
|
34
|
|
|
*/ |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* API class for BOM |
|
38
|
|
|
* |
|
39
|
|
|
* @access protected |
|
40
|
|
|
* @class DolibarrApiAccess {@requires user,external} |
|
41
|
|
|
*/ |
|
42
|
|
|
class Boms extends DolibarrApi |
|
43
|
|
|
{ |
|
44
|
|
|
/** |
|
45
|
|
|
* @var BOM $bom {@type BOM} |
|
46
|
|
|
*/ |
|
47
|
|
|
public $bom; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Constructor |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __construct() |
|
53
|
|
|
{ |
|
54
|
|
|
global $db; |
|
55
|
|
|
|
|
56
|
|
|
$this->db = $db; |
|
57
|
|
|
$this->bom = new BOM($this->db); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Get properties of a bom object |
|
62
|
|
|
* |
|
63
|
|
|
* Return an array with bom information |
|
64
|
|
|
* |
|
65
|
|
|
* @param int $id ID of bom |
|
66
|
|
|
* @return array Object with cleaned properties |
|
67
|
|
|
* |
|
68
|
|
|
* @url GET {id} |
|
69
|
|
|
* |
|
70
|
|
|
* @throws RestException 403 Access denied |
|
71
|
|
|
* @throws RestException 404 BOM not found |
|
72
|
|
|
*/ |
|
73
|
|
|
public function get($id) |
|
74
|
|
|
{ |
|
75
|
|
|
if (!DolibarrApiAccess::$user->hasRight('bom', 'read')) { |
|
76
|
|
|
throw new RestException(403); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$result = $this->bom->fetch($id); |
|
80
|
|
|
if (!$result) { |
|
81
|
|
|
throw new RestException(404, 'BOM not found'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if (!DolibarrApi::_checkAccessToResource('bom', $this->bom->id, 'bom_bom')) { |
|
85
|
|
|
throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $this->_cleanObjectDatas($this->bom); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* List boms |
|
94
|
|
|
* |
|
95
|
|
|
* Get a list of boms |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $sortfield Sort field |
|
98
|
|
|
* @param string $sortorder Sort order |
|
99
|
|
|
* @param int $limit Limit for list |
|
100
|
|
|
* @param int $page Page number |
|
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 order objects |
|
104
|
|
|
* |
|
105
|
|
|
* @throws RestException 400 Bad sqlfilters |
|
106
|
|
|
* @throws RestException 403 Access denied |
|
107
|
|
|
* @throws RestException 503 Error retrieving list of boms |
|
108
|
|
|
*/ |
|
109
|
|
|
public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '') |
|
110
|
|
|
{ |
|
111
|
|
|
if (!DolibarrApiAccess::$user->hasRight('bom', 'read')) { |
|
112
|
|
|
throw new RestException(403); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$obj_ret = array(); |
|
116
|
|
|
$tmpobject = new BOM($this->db); |
|
117
|
|
|
|
|
118
|
|
|
$socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : ''; |
|
119
|
|
|
|
|
120
|
|
|
$restrictonsocid = 0; // Set to 1 if there is a field socid in table of object |
|
121
|
|
|
|
|
122
|
|
|
// If the internal user must only see his customers, force searching by him |
|
123
|
|
|
$search_sale = 0; |
|
124
|
|
|
if ($restrictonsocid && !DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socid) { |
|
125
|
|
|
$search_sale = DolibarrApiAccess::$user->id; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
$sql = "SELECT t.rowid"; |
|
129
|
|
|
$sql .= " FROM " . MAIN_DB_PREFIX . $tmpobject->table_element . " AS t"; |
|
130
|
|
|
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . $tmpobject->table_element . "_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 |
|
131
|
|
|
$sql .= " WHERE 1 = 1"; |
|
132
|
|
|
if ($tmpobject->ismultientitymanaged) { |
|
133
|
|
|
$sql .= ' AND t.entity IN (' . getEntity($tmpobject->element) . ')'; |
|
134
|
|
|
} |
|
135
|
|
|
if ($restrictonsocid && $socid) { |
|
136
|
|
|
$sql .= " AND t.fk_soc = " . ((int) $socid); |
|
137
|
|
|
} |
|
138
|
|
|
// Search on sale representative |
|
139
|
|
|
if ($search_sale && $search_sale != '-1') { |
|
140
|
|
|
if ($search_sale == -2) { |
|
141
|
|
|
$sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM " . MAIN_DB_PREFIX . "societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)"; |
|
142
|
|
|
} elseif ($search_sale > 0) { |
|
143
|
|
|
$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) . ")"; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
if ($sqlfilters) { |
|
147
|
|
|
$errormessage = ''; |
|
148
|
|
|
$sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage); |
|
149
|
|
|
if ($errormessage) { |
|
150
|
|
|
throw new RestException(400, 'Error when validating parameter sqlfilters -> ' . $errormessage); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
$sql .= $this->db->order($sortfield, $sortorder); |
|
155
|
|
|
if ($limit) { |
|
156
|
|
|
if ($page < 0) { |
|
157
|
|
|
$page = 0; |
|
158
|
|
|
} |
|
159
|
|
|
$offset = $limit * $page; |
|
160
|
|
|
|
|
161
|
|
|
$sql .= $this->db->plimit($limit + 1, $offset); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
$result = $this->db->query($sql); |
|
165
|
|
|
if ($result) { |
|
166
|
|
|
$num = $this->db->num_rows($result); |
|
167
|
|
|
$i = 0; |
|
168
|
|
|
while ($i < $num) { |
|
169
|
|
|
$obj = $this->db->fetch_object($result); |
|
170
|
|
|
$bom_static = new BOM($this->db); |
|
171
|
|
|
if ($bom_static->fetch($obj->rowid)) { |
|
172
|
|
|
$obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($bom_static), $properties); |
|
173
|
|
|
} |
|
174
|
|
|
$i++; |
|
175
|
|
|
} |
|
176
|
|
|
} else { |
|
177
|
|
|
throw new RestException(503, 'Error when retrieve bom list'); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
return $obj_ret; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Create bom object |
|
185
|
|
|
* |
|
186
|
|
|
* @param array $request_data Request datas |
|
187
|
|
|
* @return int ID of bom |
|
188
|
|
|
* |
|
189
|
|
|
* @throws RestException 403 Access denied |
|
190
|
|
|
* @throws RestException 500 Error retrieving list of boms |
|
191
|
|
|
*/ |
|
192
|
|
|
public function post($request_data = null) |
|
193
|
|
|
{ |
|
194
|
|
|
if (!DolibarrApiAccess::$user->hasRight('bom', 'write')) { |
|
195
|
|
|
throw new RestException(403); |
|
196
|
|
|
} |
|
197
|
|
|
// Check mandatory fields |
|
198
|
|
|
$result = $this->_validate($request_data); |
|
199
|
|
|
|
|
200
|
|
|
foreach ($request_data as $field => $value) { |
|
201
|
|
|
if ($field === 'caller') { |
|
202
|
|
|
// 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 |
|
203
|
|
|
$this->bom->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09'); |
|
204
|
|
|
continue; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
$this->bom->$field = $this->_checkValForAPI($field, $value, $this->bom); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
$this->checkRefNumbering(); |
|
211
|
|
|
|
|
212
|
|
|
if (!$this->bom->create(DolibarrApiAccess::$user)) { |
|
213
|
|
|
throw new RestException(500, "Error creating BOM", array_merge(array($this->bom->error), $this->bom->errors)); |
|
214
|
|
|
} |
|
215
|
|
|
return $this->bom->id; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Update bom |
|
220
|
|
|
* |
|
221
|
|
|
* @param int $id Id of bom to update |
|
222
|
|
|
* @param array $request_data Datas |
|
223
|
|
|
* @return array Object after update |
|
224
|
|
|
* |
|
225
|
|
|
* @throws RestException 403 Access denied |
|
226
|
|
|
* @throws RestException 404 BOM not found |
|
227
|
|
|
* @throws RestException 500 Error updating bom |
|
228
|
|
|
*/ |
|
229
|
|
|
public function put($id, $request_data = null) |
|
230
|
|
|
{ |
|
231
|
|
|
if (!DolibarrApiAccess::$user->hasRight('bom', 'write')) { |
|
232
|
|
|
throw new RestException(403); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
$result = $this->bom->fetch($id); |
|
236
|
|
|
if (!$result) { |
|
237
|
|
|
throw new RestException(404, 'BOM not found'); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
if (!DolibarrApi::_checkAccessToResource('bom', $this->bom->id, 'bom_bom')) { |
|
241
|
|
|
throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
foreach ($request_data as $field => $value) { |
|
245
|
|
|
if ($field == 'id') { |
|
246
|
|
|
continue; |
|
247
|
|
|
} |
|
248
|
|
|
if ($field === 'caller') { |
|
249
|
|
|
// 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 |
|
250
|
|
|
$this->bom->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09'); |
|
251
|
|
|
continue; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
if ($field == 'array_options' && is_array($value)) { |
|
255
|
|
|
foreach ($value as $index => $val) { |
|
256
|
|
|
$this->bom->array_options[$index] = $this->_checkValForAPI('extrafields', $val, $this->bom); |
|
257
|
|
|
} |
|
258
|
|
|
continue; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
$this->bom->$field = $this->_checkValForAPI($field, $value, $this->bom); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
$this->checkRefNumbering(); |
|
265
|
|
|
|
|
266
|
|
|
if ($this->bom->update(DolibarrApiAccess::$user) > 0) { |
|
267
|
|
|
return $this->get($id); |
|
268
|
|
|
} else { |
|
269
|
|
|
throw new RestException(500, $this->bom->error); |
|
270
|
|
|
} |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* Delete bom |
|
275
|
|
|
* |
|
276
|
|
|
* @param int $id BOM ID |
|
277
|
|
|
* @return array |
|
278
|
|
|
* |
|
279
|
|
|
* @throws RestException 403 Access denied |
|
280
|
|
|
* @throws RestException 404 BOM not found |
|
281
|
|
|
* @throws RestException 500 Error deleting bom |
|
282
|
|
|
*/ |
|
283
|
|
|
public function delete($id) |
|
284
|
|
|
{ |
|
285
|
|
|
if (!DolibarrApiAccess::$user->hasRight('bom', 'delete')) { |
|
286
|
|
|
throw new RestException(403); |
|
287
|
|
|
} |
|
288
|
|
|
$result = $this->bom->fetch($id); |
|
289
|
|
|
if (!$result) { |
|
290
|
|
|
throw new RestException(404, 'BOM not found'); |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
if (!DolibarrApi::_checkAccessToResource('bom', $this->bom->id, 'bom_bom')) { |
|
294
|
|
|
throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login); |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
if (!$this->bom->delete(DolibarrApiAccess::$user)) { |
|
298
|
|
|
throw new RestException(500, 'Error when deleting BOM : ' . $this->bom->error); |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
return array( |
|
302
|
|
|
'success' => array( |
|
303
|
|
|
'code' => 200, |
|
304
|
|
|
'message' => 'BOM deleted' |
|
305
|
|
|
) |
|
306
|
|
|
); |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
/** |
|
310
|
|
|
* Get lines of an BOM |
|
311
|
|
|
* |
|
312
|
|
|
* @param int $id Id of BOM |
|
313
|
|
|
* |
|
314
|
|
|
* @url GET {id}/lines |
|
315
|
|
|
* |
|
316
|
|
|
* @return array |
|
317
|
|
|
* |
|
318
|
|
|
* @throws RestException 403 Access denied |
|
319
|
|
|
* @throws RestException 404 BOM not found |
|
320
|
|
|
*/ |
|
321
|
|
|
public function getLines($id) |
|
322
|
|
|
{ |
|
323
|
|
|
if (!DolibarrApiAccess::$user->hasRight('bom', 'read')) { |
|
324
|
|
|
throw new RestException(403); |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
$result = $this->bom->fetch($id); |
|
328
|
|
|
if (!$result) { |
|
329
|
|
|
throw new RestException(404, 'BOM not found'); |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) { |
|
333
|
|
|
throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login); |
|
334
|
|
|
} |
|
335
|
|
|
$this->bom->getLinesArray(); |
|
336
|
|
|
$result = array(); |
|
337
|
|
|
foreach ($this->bom->lines as $line) { |
|
338
|
|
|
array_push($result, $this->_cleanObjectDatas($line)); |
|
339
|
|
|
} |
|
340
|
|
|
return $result; |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
/** |
|
344
|
|
|
* Add a line to given BOM |
|
345
|
|
|
* |
|
346
|
|
|
* @param int $id Id of BOM to update |
|
347
|
|
|
* @param array $request_data BOMLine data |
|
348
|
|
|
* |
|
349
|
|
|
* @url POST {id}/lines |
|
350
|
|
|
* |
|
351
|
|
|
* @return int |
|
352
|
|
|
* |
|
353
|
|
|
* @throws RestException 403 Access denied |
|
354
|
|
|
* @throws RestException 404 BOM not found |
|
355
|
|
|
* @throws RestException 500 Error adding bom line |
|
356
|
|
|
*/ |
|
357
|
|
|
public function postLine($id, $request_data = null) |
|
358
|
|
|
{ |
|
359
|
|
|
if (!DolibarrApiAccess::$user->hasRight('bom', 'write')) { |
|
360
|
|
|
throw new RestException(403); |
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
|
|
$result = $this->bom->fetch($id); |
|
364
|
|
|
if (!$result) { |
|
365
|
|
|
throw new RestException(404, 'BOM not found'); |
|
366
|
|
|
} |
|
367
|
|
|
|
|
368
|
|
|
if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) { |
|
369
|
|
|
throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login); |
|
370
|
|
|
} |
|
371
|
|
|
|
|
372
|
|
|
$request_data = (object) $request_data; |
|
373
|
|
|
|
|
374
|
|
|
$updateRes = $this->bom->addLine( |
|
375
|
|
|
$request_data->fk_product, |
|
376
|
|
|
$request_data->qty, |
|
377
|
|
|
$request_data->qty_frozen, |
|
378
|
|
|
$request_data->disable_stock_change, |
|
379
|
|
|
$request_data->efficiency, |
|
380
|
|
|
$request_data->position, |
|
381
|
|
|
$request_data->fk_bom_child, |
|
382
|
|
|
$request_data->import_key, |
|
383
|
|
|
$request_data->fk_unit |
|
384
|
|
|
); |
|
385
|
|
|
|
|
386
|
|
|
if ($updateRes > 0) { |
|
387
|
|
|
return $updateRes; |
|
388
|
|
|
} else { |
|
389
|
|
|
throw new RestException(500, $this->bom->error); |
|
390
|
|
|
} |
|
391
|
|
|
} |
|
392
|
|
|
|
|
393
|
|
|
/** |
|
394
|
|
|
* Update a line to given BOM |
|
395
|
|
|
* |
|
396
|
|
|
* @param int $id Id of BOM to update |
|
397
|
|
|
* @param int $lineid Id of line to update |
|
398
|
|
|
* @param array $request_data BOMLine data |
|
399
|
|
|
* |
|
400
|
|
|
* @url PUT {id}/lines/{lineid} |
|
401
|
|
|
* |
|
402
|
|
|
* @return object|bool |
|
403
|
|
|
* |
|
404
|
|
|
* @throws RestException 403 Access denied |
|
405
|
|
|
* @throws RestException 404 BOM not found |
|
406
|
|
|
*/ |
|
407
|
|
|
public function putLine($id, $lineid, $request_data = null) |
|
408
|
|
|
{ |
|
409
|
|
|
if (!DolibarrApiAccess::$user->hasRight('bom', 'write')) { |
|
410
|
|
|
throw new RestException(403); |
|
411
|
|
|
} |
|
412
|
|
|
|
|
413
|
|
|
$result = $this->bom->fetch($id); |
|
414
|
|
|
if (!$result) { |
|
415
|
|
|
throw new RestException(404, 'BOM not found'); |
|
416
|
|
|
} |
|
417
|
|
|
|
|
418
|
|
|
if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) { |
|
419
|
|
|
throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login); |
|
420
|
|
|
} |
|
421
|
|
|
|
|
422
|
|
|
$request_data = (object) $request_data; |
|
423
|
|
|
|
|
424
|
|
|
$updateRes = $this->bom->updateLine( |
|
425
|
|
|
$lineid, |
|
426
|
|
|
$request_data->qty, |
|
427
|
|
|
$request_data->qty_frozen, |
|
428
|
|
|
$request_data->disable_stock_change, |
|
429
|
|
|
$request_data->efficiency, |
|
430
|
|
|
$request_data->position, |
|
431
|
|
|
$request_data->import_key, |
|
432
|
|
|
$request_data->fk_unit |
|
433
|
|
|
); |
|
434
|
|
|
|
|
435
|
|
|
if ($updateRes > 0) { |
|
436
|
|
|
$result = $this->get($id); |
|
437
|
|
|
unset($result->line); |
|
438
|
|
|
return $this->_cleanObjectDatas($result); |
|
439
|
|
|
} |
|
440
|
|
|
return false; |
|
441
|
|
|
} |
|
442
|
|
|
|
|
443
|
|
|
/** |
|
444
|
|
|
* Delete a line to given BOM |
|
445
|
|
|
* |
|
446
|
|
|
* |
|
447
|
|
|
* @param int $id Id of BOM to update |
|
448
|
|
|
* @param int $lineid Id of line to delete |
|
449
|
|
|
* |
|
450
|
|
|
* @url DELETE {id}/lines/{lineid} |
|
451
|
|
|
* |
|
452
|
|
|
* @return array |
|
453
|
|
|
* |
|
454
|
|
|
* @throws RestException 403 Access denied |
|
455
|
|
|
* @throws RestException 404 BOM not found |
|
456
|
|
|
* @throws RestException 500 Error deleting bom line |
|
457
|
|
|
*/ |
|
458
|
|
|
public function deleteLine($id, $lineid) |
|
459
|
|
|
{ |
|
460
|
|
|
if (!DolibarrApiAccess::$user->hasRight('bom', 'write')) { |
|
461
|
|
|
throw new RestException(403); |
|
462
|
|
|
} |
|
463
|
|
|
|
|
464
|
|
|
$result = $this->bom->fetch($id); |
|
465
|
|
|
if (!$result) { |
|
466
|
|
|
throw new RestException(404, 'BOM not found'); |
|
467
|
|
|
} |
|
468
|
|
|
|
|
469
|
|
|
if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) { |
|
470
|
|
|
throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login); |
|
471
|
|
|
} |
|
472
|
|
|
|
|
473
|
|
|
//Check the rowid is a line of current bom object |
|
474
|
|
|
$lineIdIsFromObject = false; |
|
475
|
|
|
foreach ($this->bom->lines as $bl) { |
|
476
|
|
|
if ($bl->id == $lineid) { |
|
477
|
|
|
$lineIdIsFromObject = true; |
|
478
|
|
|
break; |
|
479
|
|
|
} |
|
480
|
|
|
} |
|
481
|
|
|
if (!$lineIdIsFromObject) { |
|
482
|
|
|
throw new RestException(500, 'Line to delete (rowid: ' . $lineid . ') is not a line of BOM (id: ' . $this->bom->id . ')'); |
|
483
|
|
|
} |
|
484
|
|
|
|
|
485
|
|
|
$updateRes = $this->bom->deleteLine(DolibarrApiAccess::$user, $lineid); |
|
486
|
|
|
if ($updateRes > 0) { |
|
487
|
|
|
return array( |
|
488
|
|
|
'success' => array( |
|
489
|
|
|
'code' => 200, |
|
490
|
|
|
'message' => 'line ' . $lineid . ' deleted' |
|
491
|
|
|
) |
|
492
|
|
|
); |
|
493
|
|
|
} else { |
|
494
|
|
|
throw new RestException(500, $this->bom->error); |
|
495
|
|
|
} |
|
496
|
|
|
} |
|
497
|
|
|
|
|
498
|
|
|
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore |
|
499
|
|
|
/** |
|
500
|
|
|
* Clean sensible object datas |
|
501
|
|
|
* |
|
502
|
|
|
* @param Object $object Object to clean |
|
503
|
|
|
* @return Object Object with cleaned properties |
|
504
|
|
|
*/ |
|
505
|
|
|
protected function _cleanObjectDatas($object) |
|
506
|
|
|
{ |
|
507
|
|
|
// phpcs:enable |
|
508
|
|
|
$object = parent::_cleanObjectDatas($object); |
|
509
|
|
|
|
|
510
|
|
|
unset($object->rowid); |
|
511
|
|
|
unset($object->canvas); |
|
512
|
|
|
|
|
513
|
|
|
unset($object->name); |
|
514
|
|
|
unset($object->lastname); |
|
515
|
|
|
unset($object->firstname); |
|
516
|
|
|
unset($object->civility_id); |
|
517
|
|
|
unset($object->statut); |
|
518
|
|
|
unset($object->state); |
|
519
|
|
|
unset($object->state_id); |
|
520
|
|
|
unset($object->state_code); |
|
521
|
|
|
unset($object->region); |
|
522
|
|
|
unset($object->region_code); |
|
523
|
|
|
unset($object->country); |
|
524
|
|
|
unset($object->country_id); |
|
525
|
|
|
unset($object->country_code); |
|
526
|
|
|
unset($object->barcode_type); |
|
527
|
|
|
unset($object->barcode_type_code); |
|
528
|
|
|
unset($object->barcode_type_label); |
|
529
|
|
|
unset($object->barcode_type_coder); |
|
530
|
|
|
unset($object->total_ht); |
|
531
|
|
|
unset($object->total_tva); |
|
532
|
|
|
unset($object->total_localtax1); |
|
533
|
|
|
unset($object->total_localtax2); |
|
534
|
|
|
unset($object->total_ttc); |
|
535
|
|
|
unset($object->fk_account); |
|
536
|
|
|
unset($object->comments); |
|
537
|
|
|
unset($object->note); |
|
538
|
|
|
unset($object->mode_reglement_id); |
|
539
|
|
|
unset($object->cond_reglement_id); |
|
540
|
|
|
unset($object->cond_reglement); |
|
541
|
|
|
unset($object->shipping_method_id); |
|
542
|
|
|
unset($object->fk_incoterms); |
|
543
|
|
|
unset($object->label_incoterms); |
|
544
|
|
|
unset($object->location_incoterms); |
|
545
|
|
|
unset($object->multicurrency_code); |
|
546
|
|
|
unset($object->multicurrency_tx); |
|
547
|
|
|
unset($object->multicurrency_total_ht); |
|
548
|
|
|
unset($object->multicurrency_total_ttc); |
|
549
|
|
|
unset($object->multicurrency_total_tva); |
|
550
|
|
|
unset($object->multicurrency_total_localtax1); |
|
551
|
|
|
unset($object->multicurrency_total_localtax2); |
|
552
|
|
|
|
|
553
|
|
|
|
|
554
|
|
|
// If object has lines, remove $db property |
|
555
|
|
|
if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) { |
|
556
|
|
|
$nboflines = count($object->lines); |
|
557
|
|
|
for ($i = 0; $i < $nboflines; $i++) { |
|
558
|
|
|
$this->_cleanObjectDatas($object->lines[$i]); |
|
559
|
|
|
|
|
560
|
|
|
unset($object->lines[$i]->lines); |
|
561
|
|
|
unset($object->lines[$i]->note); |
|
562
|
|
|
} |
|
563
|
|
|
} |
|
564
|
|
|
|
|
565
|
|
|
return $object; |
|
|
|
|
|
|
566
|
|
|
} |
|
567
|
|
|
|
|
568
|
|
|
/** |
|
569
|
|
|
* Validate fields before create or update object |
|
570
|
|
|
* |
|
571
|
|
|
* @param array $data Array of data to validate |
|
572
|
|
|
* @return array |
|
573
|
|
|
* |
|
574
|
|
|
* @throws RestException |
|
575
|
|
|
*/ |
|
576
|
|
|
private function _validate($data) |
|
577
|
|
|
{ |
|
578
|
|
|
$myobject = array(); |
|
579
|
|
|
foreach ($this->bom->fields as $field => $propfield) { |
|
580
|
|
|
if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) { |
|
581
|
|
|
continue; // Not a mandatory field |
|
582
|
|
|
} |
|
583
|
|
|
if (!isset($data[$field])) { |
|
584
|
|
|
throw new RestException(400, "$field field missing"); |
|
585
|
|
|
} |
|
586
|
|
|
$myobject[$field] = $data[$field]; |
|
587
|
|
|
} |
|
588
|
|
|
return $myobject; |
|
589
|
|
|
} |
|
590
|
|
|
|
|
591
|
|
|
/** |
|
592
|
|
|
* Validate the ref field and get the next Number if it's necessary. |
|
593
|
|
|
* |
|
594
|
|
|
* @return void |
|
595
|
|
|
*/ |
|
596
|
|
|
private function checkRefNumbering() |
|
597
|
|
|
{ |
|
598
|
|
|
$ref = substr($this->bom->ref, 1, 4); |
|
599
|
|
|
if ($this->bom->status > 0 && $ref == 'PROV') { |
|
600
|
|
|
throw new RestException(400, "Wrong naming scheme '(PROV%)' is only allowed on 'DRAFT' status. For automatic increment use 'auto' on the 'ref' field."); |
|
601
|
|
|
} |
|
602
|
|
|
|
|
603
|
|
|
if (strtolower($this->bom->ref) == 'auto') { |
|
604
|
|
|
if (empty($this->bom->id) && $this->bom->status == 0) { |
|
605
|
|
|
$this->bom->ref = ''; // 'ref' will auto incremented with '(PROV' + newID + ')' |
|
606
|
|
|
} else { |
|
607
|
|
|
$this->bom->fetch_product(); |
|
608
|
|
|
$numref = $this->bom->getNextNumRef($this->bom->product); |
|
609
|
|
|
$this->bom->ref = $numref; |
|
610
|
|
|
} |
|
611
|
|
|
} |
|
612
|
|
|
} |
|
613
|
|
|
} |
|
614
|
|
|
|