dolibarr 22.0.5
api_boms.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
3 * Copyright (C) 2019 Maxime Kohlhaas <maxime@atm-consulting.fr>
4 * Copyright (C) 2020-2024 Frédéric France <frederic.france@free.fr>
5 * Copyright (C) 2022 Christian Humpel <christian.humpel@live.com>
6 * Copyright (C) 2025 MDW <mdeweerd@users.noreply.github.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22use Luracast\Restler\RestException;
23
24require_once DOL_DOCUMENT_ROOT.'/bom/class/bom.class.php';
25
26
39class Boms extends DolibarrApi
40{
44 public $bom;
45
49 public function __construct()
50 {
51 global $db;
52
53 $this->db = $db;
54 $this->bom = new BOM($this->db);
55 }
56
72 public function get($id)
73 {
74 if (!DolibarrApiAccess::$user->hasRight('bom', 'read')) {
75 throw new RestException(403);
76 }
77
78 $result = $this->bom->fetch($id);
79 if (!$result) {
80 throw new RestException(404, 'BOM not found');
81 }
82
83 if (!DolibarrApi::_checkAccessToResource('bom', $this->bom->id, 'bom_bom')) {
84 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
85 }
86
87 return $this->_cleanObjectDatas($this->bom);
88 }
89
90
110 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '')
111 {
112 if (!DolibarrApiAccess::$user->hasRight('bom', 'read')) {
113 throw new RestException(403);
114 }
115
116 $obj_ret = array();
117 $tmpobject = new BOM($this->db);
118
119 $socid = DolibarrApiAccess::$user->socid ?: '';
120
121 $restrictonsocid = 0; // Set to 1 if there is a field socid in table of object
122
123 // If the internal user must only see his customers, force searching by him
124 $search_sale = 0;
125 if ($restrictonsocid && !DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socid) {
126 $search_sale = DolibarrApiAccess::$user->id;
127 }
128
129 $sql = "SELECT t.rowid";
130 $sql .= " FROM ".MAIN_DB_PREFIX.$tmpobject->table_element." AS t";
131 $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
132 $sql .= " WHERE 1 = 1";
133 if ($tmpobject->ismultientitymanaged) {
134 $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')';
135 }
136 if ($restrictonsocid && $socid) {
137 $sql .= " AND t.fk_soc = ".((int) $socid);
138 }
139 // Search on sale representative
140 if ($search_sale && $search_sale != '-1') {
141 if ($search_sale == -2) {
142 $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
143 } elseif ($search_sale > 0) {
144 $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).")";
145 }
146 }
147 if ($sqlfilters) {
148 $errormessage = '';
149 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
150 if ($errormessage) {
151 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
152 }
153 }
154
155 $sql .= $this->db->order($sortfield, $sortorder);
156 if ($limit) {
157 if ($page < 0) {
158 $page = 0;
159 }
160 $offset = $limit * $page;
161
162 $sql .= $this->db->plimit($limit + 1, $offset);
163 }
164
165 $result = $this->db->query($sql);
166 if ($result) {
167 $num = $this->db->num_rows($result);
168 $i = 0;
169 while ($i < $num) {
170 $obj = $this->db->fetch_object($result);
171 $bom_static = new BOM($this->db);
172 if ($bom_static->fetch($obj->rowid)) {
173 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($bom_static), $properties);
174 }
175 $i++;
176 }
177 } else {
178 throw new RestException(503, 'Error when retrieve bom list');
179 }
180
181 return $obj_ret;
182 }
183
195 public function post($request_data = null)
196 {
197 if (!DolibarrApiAccess::$user->hasRight('bom', 'write')) {
198 throw new RestException(403);
199 }
200 // Check mandatory fields
201 $result = $this->_validate($request_data);
202
203 foreach ($request_data as $field => $value) {
204 if ($field === 'caller') {
205 // 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
206 $this->bom->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
207 continue;
208 }
209
210 $this->bom->$field = $this->_checkValForAPI($field, $value, $this->bom);
211 }
212
213 $this->checkRefNumbering();
214
215 if (!$this->bom->create(DolibarrApiAccess::$user)) {
216 throw new RestException(500, "Error creating BOM", array_merge(array($this->bom->error), $this->bom->errors));
217 }
218 return $this->bom->id;
219 }
220
236 public function put($id, $request_data = null)
237 {
238 if (!DolibarrApiAccess::$user->hasRight('bom', 'write')) {
239 throw new RestException(403);
240 }
241
242 $result = $this->bom->fetch($id);
243 if (!$result) {
244 throw new RestException(404, 'BOM not found');
245 }
246
247 if (!DolibarrApi::_checkAccessToResource('bom', $this->bom->id, 'bom_bom')) {
248 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
249 }
250
251 foreach ($request_data as $field => $value) {
252 if ($field == 'id') {
253 continue;
254 }
255 if ($field === 'caller') {
256 // 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
257 $this->bom->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
258 continue;
259 }
260
261 if ($field == 'array_options' && is_array($value)) {
262 foreach ($value as $index => $val) {
263 $this->bom->array_options[$index] = $this->_checkValForAPI($field, $val, $this->bom);
264 }
265 continue;
266 }
267 $this->bom->$field = $this->_checkValForAPI($field, $value, $this->bom);
268 }
269
270 $this->checkRefNumbering();
271
272 if ($this->bom->update(DolibarrApiAccess::$user) > 0) {
273 return $this->get($id);
274 } else {
275 throw new RestException(500, $this->bom->error);
276 }
277 }
278
291 public function delete($id)
292 {
293 if (!DolibarrApiAccess::$user->hasRight('bom', 'delete')) {
294 throw new RestException(403);
295 }
296 $result = $this->bom->fetch($id);
297 if (!$result) {
298 throw new RestException(404, 'BOM not found');
299 }
300
301 if (!DolibarrApi::_checkAccessToResource('bom', $this->bom->id, 'bom_bom')) {
302 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
303 }
304
305 if (!$this->bom->delete(DolibarrApiAccess::$user)) {
306 throw new RestException(500, 'Error when deleting BOM : '.$this->bom->error);
307 }
308
309 return array(
310 'success' => array(
311 'code' => 200,
312 'message' => 'BOM deleted'
313 )
314 );
315 }
316
331 public function getLines($id)
332 {
333 if (!DolibarrApiAccess::$user->hasRight('bom', 'read')) {
334 throw new RestException(403);
335 }
336
337 $result = $this->bom->fetch($id);
338 if (!$result) {
339 throw new RestException(404, 'BOM not found');
340 }
341
342 if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) {
343 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
344 }
345 $this->bom->getLinesArray();
346 $result = array();
347 foreach ($this->bom->lines as $line) {
348 array_push($result, $this->_cleanObjectDatas($line));
349 }
350 return $result;
351 }
352
369 public function postLine($id, $request_data = null)
370 {
371 if (!DolibarrApiAccess::$user->hasRight('bom', 'write')) {
372 throw new RestException(403);
373 }
374
375 $result = $this->bom->fetch($id);
376 if (!$result) {
377 throw new RestException(404, 'BOM not found');
378 }
379
380 if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) {
381 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
382 }
383
384 $request_data = (object) $request_data;
385
386 $updateRes = $this->bom->addLine(
387 $request_data->fk_product,
388 $request_data->qty,
389 $request_data->qty_frozen,
390 $request_data->disable_stock_change,
391 $request_data->efficiency,
392 $request_data->position,
393 $request_data->fk_bom_child,
394 $request_data->import_key,
395 $request_data->fk_unit,
396 $request_data->array_options,
397 $request_data->fk_default_workstation
398 );
399
400 if ($updateRes > 0) {
401 return $updateRes;
402 } else {
403 throw new RestException(500, $this->bom->error);
404 }
405 }
406
423 public function putLine($id, $lineid, $request_data = null)
424 {
425 if (!DolibarrApiAccess::$user->hasRight('bom', 'write')) {
426 throw new RestException(403);
427 }
428
429 $result = $this->bom->fetch($id);
430 if (!$result) {
431 throw new RestException(404, 'BOM not found');
432 }
433
434 if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) {
435 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
436 }
437
438 $request_data = (object) $request_data;
439
440 $updateRes = $this->bom->updateLine(
441 $lineid,
442 $request_data->qty,
443 $request_data->qty_frozen,
444 $request_data->disable_stock_change,
445 $request_data->efficiency,
446 $request_data->position,
447 $request_data->import_key,
448 $request_data->fk_unit,
449 $request_data->array_options,
450 $request_data->fk_default_workstation
451 );
452
453 if ($updateRes > 0) {
454 $result = $this->get($id);
455 unset($result->line);
456 return $this->_cleanObjectDatas($result);
457 }
458 return false;
459 }
460
478 public function deleteLine($id, $lineid)
479 {
480 if (!DolibarrApiAccess::$user->hasRight('bom', 'write')) {
481 throw new RestException(403);
482 }
483
484 $result = $this->bom->fetch($id);
485 if (!$result) {
486 throw new RestException(404, 'BOM not found');
487 }
488
489 if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) {
490 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
491 }
492
493 //Check the rowid is a line of current bom object
494 $lineIdIsFromObject = false;
495 foreach ($this->bom->lines as $bl) {
496 if ($bl->id == $lineid) {
497 $lineIdIsFromObject = true;
498 break;
499 }
500 }
501 if (!$lineIdIsFromObject) {
502 throw new RestException(500, 'Line to delete (rowid: '.$lineid.') is not a line of BOM (id: '.$this->bom->id.')');
503 }
504
505 $updateRes = $this->bom->deleteLine(DolibarrApiAccess::$user, $lineid);
506 if ($updateRes > 0) {
507 return array(
508 'success' => array(
509 'code' => 200,
510 'message' => 'line ' .$lineid. ' deleted'
511 )
512 );
513 } else {
514 throw new RestException(500, $this->bom->error);
515 }
516 }
517
518 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
525 protected function _cleanObjectDatas($object)
526 {
527 // phpcs:enable
528 $object = parent::_cleanObjectDatas($object);
529
530 unset($object->rowid);
531 unset($object->canvas);
532
533 unset($object->name);
534 unset($object->lastname);
535 unset($object->firstname);
536 unset($object->civility_id);
537 unset($object->statut);
538 unset($object->state);
539 unset($object->state_id);
540 unset($object->state_code);
541 unset($object->region);
542 unset($object->region_code);
543 unset($object->country);
544 unset($object->country_id);
545 unset($object->country_code);
546 unset($object->barcode_type);
547 unset($object->barcode_type_code);
548 unset($object->barcode_type_label);
549 unset($object->barcode_type_coder);
550 unset($object->total_ht);
551 unset($object->total_tva);
552 unset($object->total_localtax1);
553 unset($object->total_localtax2);
554 unset($object->total_ttc);
555 unset($object->fk_account);
556 unset($object->comments);
557 unset($object->note);
558 unset($object->mode_reglement_id);
559 unset($object->cond_reglement_id);
560 unset($object->cond_reglement);
561 unset($object->shipping_method_id);
562 unset($object->fk_incoterms);
563 unset($object->label_incoterms);
564 unset($object->location_incoterms);
565 unset($object->multicurrency_code);
566 unset($object->multicurrency_tx);
567 unset($object->multicurrency_total_ht);
568 unset($object->multicurrency_total_ttc);
569 unset($object->multicurrency_total_tva);
570 unset($object->multicurrency_total_localtax1);
571 unset($object->multicurrency_total_localtax2);
572
573
574 // If object has lines, remove $db property
575 if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
576 $nboflines = count($object->lines);
577 for ($i = 0; $i < $nboflines; $i++) {
578 $this->_cleanObjectDatas($object->lines[$i]);
579
580 unset($object->lines[$i]->lines);
581 unset($object->lines[$i]->note);
582 }
583 }
584
585 return $object;
586 }
587
596 private function _validate($data)
597 {
598 if ($data === null) {
599 $data = array();
600 }
601 $myobject = array();
602 foreach ($this->bom->fields as $field => $propfield) {
603 if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || empty($propfield['notnull']) || $propfield['notnull'] != 1) {
604 continue; // Not a mandatory field
605 }
606 if (!isset($data[$field])) {
607 throw new RestException(400, "$field field missing");
608 }
609 $myobject[$field] = $data[$field];
610 }
611 return $myobject;
612 }
613
619 private function checkRefNumbering()
620 {
621 $ref = substr($this->bom->ref, 1, 4);
622 if ($this->bom->status > 0 && $ref == 'PROV') {
623 throw new RestException(400, "Wrong naming scheme '(PROV%)' is only allowed on 'DRAFT' status. For automatic increment use 'auto' on the 'ref' field.");
624 }
625
626 if (strtolower($this->bom->ref) == 'auto') {
627 if (empty($this->bom->id) && $this->bom->status == 0) {
628 $this->bom->ref = ''; // 'ref' will auto incremented with '(PROV' + newID + ')'
629 } else {
630 $this->bom->fetch_product();
631 $numref = $this->bom->getNextNumRef($this->bom->product);
632 $this->bom->ref = $numref;
633 }
634 }
635 }
636}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:48
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:67
Class for BOM.
Definition bom.class.php:42
checkRefNumbering()
Validate the ref field and get the next Number if it's necessary.
put($id, $request_data=null)
Update bom.
_cleanObjectDatas($object)
Clean sensible object datas.
putLine($id, $lineid, $request_data=null)
Update a line to given BOM.
post($request_data=null)
Create bom object.
getLines($id)
Get lines of an BOM.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='')
List boms.
__construct()
Constructor.
deleteLine($id, $lineid)
Delete a line to given BOM.
postLine($id, $request_data=null)
Add a line to given BOM.
_validate($data)
Validate fields before create or update object.
Class for API REST v1.
Definition api.class.php:33
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Definition api.class.php:98
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.