dolibarr 20.0.2
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 *
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
21use Luracast\Restler\RestException;
22
23require_once DOL_DOCUMENT_ROOT.'/bom/class/bom.class.php';
24
25
38class Boms extends DolibarrApi
39{
43 public $bom;
44
48 public function __construct()
49 {
50 global $db;
51
52 $this->db = $db;
53 $this->bom = new BOM($this->db);
54 }
55
69 public function get($id)
70 {
71 if (!DolibarrApiAccess::$user->hasRight('bom', 'read')) {
72 throw new RestException(403);
73 }
74
75 $result = $this->bom->fetch($id);
76 if (!$result) {
77 throw new RestException(404, 'BOM not found');
78 }
79
80 if (!DolibarrApi::_checkAccessToResource('bom', $this->bom->id, 'bom_bom')) {
81 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
82 }
83
84 return $this->_cleanObjectDatas($this->bom);
85 }
86
87
105 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '')
106 {
107 if (!DolibarrApiAccess::$user->hasRight('bom', 'read')) {
108 throw new RestException(403);
109 }
110
111 $obj_ret = array();
112 $tmpobject = new BOM($this->db);
113
114 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
115
116 $restrictonsocid = 0; // Set to 1 if there is a field socid in table of object
117
118 // If the internal user must only see his customers, force searching by him
119 $search_sale = 0;
120 if ($restrictonsocid && !DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socid) {
121 $search_sale = DolibarrApiAccess::$user->id;
122 }
123
124 $sql = "SELECT t.rowid";
125 $sql .= " FROM ".MAIN_DB_PREFIX.$tmpobject->table_element." AS t";
126 $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
127 $sql .= " WHERE 1 = 1";
128 if ($tmpobject->ismultientitymanaged) {
129 $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')';
130 }
131 if ($restrictonsocid && $socid) {
132 $sql .= " AND t.fk_soc = ".((int) $socid);
133 }
134 // Search on sale representative
135 if ($search_sale && $search_sale != '-1') {
136 if ($search_sale == -2) {
137 $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
138 } elseif ($search_sale > 0) {
139 $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).")";
140 }
141 }
142 if ($sqlfilters) {
143 $errormessage = '';
144 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
145 if ($errormessage) {
146 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
147 }
148 }
149
150 $sql .= $this->db->order($sortfield, $sortorder);
151 if ($limit) {
152 if ($page < 0) {
153 $page = 0;
154 }
155 $offset = $limit * $page;
156
157 $sql .= $this->db->plimit($limit + 1, $offset);
158 }
159
160 $result = $this->db->query($sql);
161 if ($result) {
162 $num = $this->db->num_rows($result);
163 $i = 0;
164 while ($i < $num) {
165 $obj = $this->db->fetch_object($result);
166 $bom_static = new BOM($this->db);
167 if ($bom_static->fetch($obj->rowid)) {
168 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($bom_static), $properties);
169 }
170 $i++;
171 }
172 } else {
173 throw new RestException(503, 'Error when retrieve bom list');
174 }
175
176 return $obj_ret;
177 }
178
188 public function post($request_data = null)
189 {
190 if (!DolibarrApiAccess::$user->hasRight('bom', 'write')) {
191 throw new RestException(403);
192 }
193 // Check mandatory fields
194 $result = $this->_validate($request_data);
195
196 foreach ($request_data as $field => $value) {
197 if ($field === 'caller') {
198 // 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
199 $this->bom->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
200 continue;
201 }
202
203 $this->bom->$field = $this->_checkValForAPI($field, $value, $this->bom);
204 }
205
206 $this->checkRefNumbering();
207
208 if (!$this->bom->create(DolibarrApiAccess::$user)) {
209 throw new RestException(500, "Error creating BOM", array_merge(array($this->bom->error), $this->bom->errors));
210 }
211 return $this->bom->id;
212 }
213
225 public function put($id, $request_data = null)
226 {
227 if (!DolibarrApiAccess::$user->hasRight('bom', 'write')) {
228 throw new RestException(403);
229 }
230
231 $result = $this->bom->fetch($id);
232 if (!$result) {
233 throw new RestException(404, 'BOM not found');
234 }
235
236 if (!DolibarrApi::_checkAccessToResource('bom', $this->bom->id, 'bom_bom')) {
237 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
238 }
239
240 foreach ($request_data as $field => $value) {
241 if ($field == 'id') {
242 continue;
243 }
244 if ($field === 'caller') {
245 // 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
246 $this->bom->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
247 continue;
248 }
249
250 if ($field == 'array_options' && is_array($value)) {
251 foreach ($value as $index => $val) {
252 $this->bom->array_options[$index] = $this->_checkValForAPI('extrafields', $val, $this->bom);
253 }
254 continue;
255 }
256
257 $this->bom->$field = $this->_checkValForAPI($field, $value, $this->bom);
258 }
259
260 $this->checkRefNumbering();
261
262 if ($this->bom->update(DolibarrApiAccess::$user) > 0) {
263 return $this->get($id);
264 } else {
265 throw new RestException(500, $this->bom->error);
266 }
267 }
268
279 public function delete($id)
280 {
281 if (!DolibarrApiAccess::$user->hasRight('bom', 'delete')) {
282 throw new RestException(403);
283 }
284 $result = $this->bom->fetch($id);
285 if (!$result) {
286 throw new RestException(404, 'BOM not found');
287 }
288
289 if (!DolibarrApi::_checkAccessToResource('bom', $this->bom->id, 'bom_bom')) {
290 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
291 }
292
293 if (!$this->bom->delete(DolibarrApiAccess::$user)) {
294 throw new RestException(500, 'Error when deleting BOM : '.$this->bom->error);
295 }
296
297 return array(
298 'success' => array(
299 'code' => 200,
300 'message' => 'BOM deleted'
301 )
302 );
303 }
304
317 public function getLines($id)
318 {
319 if (!DolibarrApiAccess::$user->hasRight('bom', 'read')) {
320 throw new RestException(403);
321 }
322
323 $result = $this->bom->fetch($id);
324 if (!$result) {
325 throw new RestException(404, 'BOM not found');
326 }
327
328 if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) {
329 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
330 }
331 $this->bom->getLinesArray();
332 $result = array();
333 foreach ($this->bom->lines as $line) {
334 array_push($result, $this->_cleanObjectDatas($line));
335 }
336 return $result;
337 }
338
353 public function postLine($id, $request_data = null)
354 {
355 if (!DolibarrApiAccess::$user->hasRight('bom', 'write')) {
356 throw new RestException(403);
357 }
358
359 $result = $this->bom->fetch($id);
360 if (!$result) {
361 throw new RestException(404, 'BOM not found');
362 }
363
364 if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) {
365 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
366 }
367
368 $request_data = (object) $request_data;
369
370 $updateRes = $this->bom->addLine(
371 $request_data->fk_product,
372 $request_data->qty,
373 $request_data->qty_frozen,
374 $request_data->disable_stock_change,
375 $request_data->efficiency,
376 $request_data->position,
377 $request_data->fk_bom_child,
378 $request_data->import_key,
379 $request_data->fk_unit,
380 $request_data->array_options,
381 $request_data->fk_default_workstation
382 );
383
384 if ($updateRes > 0) {
385 return $updateRes;
386 } else {
387 throw new RestException(500, $this->bom->error);
388 }
389 }
390
405 public function putLine($id, $lineid, $request_data = null)
406 {
407 if (!DolibarrApiAccess::$user->hasRight('bom', 'write')) {
408 throw new RestException(403);
409 }
410
411 $result = $this->bom->fetch($id);
412 if (!$result) {
413 throw new RestException(404, 'BOM not found');
414 }
415
416 if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) {
417 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
418 }
419
420 $request_data = (object) $request_data;
421
422 $updateRes = $this->bom->updateLine(
423 $lineid,
424 $request_data->qty,
425 $request_data->qty_frozen,
426 $request_data->disable_stock_change,
427 $request_data->efficiency,
428 $request_data->position,
429 $request_data->import_key,
430 $request_data->fk_unit,
431 $request_data->array_options,
432 $request_data->fk_default_workstation
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
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
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
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
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}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
Class for BOM.
Definition bom.class.php:45
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:30
_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:82
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.