dolibarr 21.0.0-alpha
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 );
381
382 if ($updateRes > 0) {
383 return $updateRes;
384 } else {
385 throw new RestException(500, $this->bom->error);
386 }
387 }
388
403 public function putLine($id, $lineid, $request_data = null)
404 {
405 if (!DolibarrApiAccess::$user->hasRight('bom', 'write')) {
406 throw new RestException(403);
407 }
408
409 $result = $this->bom->fetch($id);
410 if (!$result) {
411 throw new RestException(404, 'BOM not found');
412 }
413
414 if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) {
415 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
416 }
417
418 $request_data = (object) $request_data;
419
420 $updateRes = $this->bom->updateLine(
421 $lineid,
422 $request_data->qty,
423 $request_data->qty_frozen,
424 $request_data->disable_stock_change,
425 $request_data->efficiency,
426 $request_data->position,
427 $request_data->import_key,
428 $request_data->fk_unit
429 );
430
431 if ($updateRes > 0) {
432 $result = $this->get($id);
433 unset($result->line);
434 return $this->_cleanObjectDatas($result);
435 }
436 return false;
437 }
438
454 public function deleteLine($id, $lineid)
455 {
456 if (!DolibarrApiAccess::$user->hasRight('bom', 'write')) {
457 throw new RestException(403);
458 }
459
460 $result = $this->bom->fetch($id);
461 if (!$result) {
462 throw new RestException(404, 'BOM not found');
463 }
464
465 if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) {
466 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
467 }
468
469 //Check the rowid is a line of current bom object
470 $lineIdIsFromObject = false;
471 foreach ($this->bom->lines as $bl) {
472 if ($bl->id == $lineid) {
473 $lineIdIsFromObject = true;
474 break;
475 }
476 }
477 if (!$lineIdIsFromObject) {
478 throw new RestException(500, 'Line to delete (rowid: '.$lineid.') is not a line of BOM (id: '.$this->bom->id.')');
479 }
480
481 $updateRes = $this->bom->deleteLine(DolibarrApiAccess::$user, $lineid);
482 if ($updateRes > 0) {
483 return array(
484 'success' => array(
485 'code' => 200,
486 'message' => 'line ' .$lineid. ' deleted'
487 )
488 );
489 } else {
490 throw new RestException(500, $this->bom->error);
491 }
492 }
493
494 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
501 protected function _cleanObjectDatas($object)
502 {
503 // phpcs:enable
504 $object = parent::_cleanObjectDatas($object);
505
506 unset($object->rowid);
507 unset($object->canvas);
508
509 unset($object->name);
510 unset($object->lastname);
511 unset($object->firstname);
512 unset($object->civility_id);
513 unset($object->statut);
514 unset($object->state);
515 unset($object->state_id);
516 unset($object->state_code);
517 unset($object->region);
518 unset($object->region_code);
519 unset($object->country);
520 unset($object->country_id);
521 unset($object->country_code);
522 unset($object->barcode_type);
523 unset($object->barcode_type_code);
524 unset($object->barcode_type_label);
525 unset($object->barcode_type_coder);
526 unset($object->total_ht);
527 unset($object->total_tva);
528 unset($object->total_localtax1);
529 unset($object->total_localtax2);
530 unset($object->total_ttc);
531 unset($object->fk_account);
532 unset($object->comments);
533 unset($object->note);
534 unset($object->mode_reglement_id);
535 unset($object->cond_reglement_id);
536 unset($object->cond_reglement);
537 unset($object->shipping_method_id);
538 unset($object->fk_incoterms);
539 unset($object->label_incoterms);
540 unset($object->location_incoterms);
541 unset($object->multicurrency_code);
542 unset($object->multicurrency_tx);
543 unset($object->multicurrency_total_ht);
544 unset($object->multicurrency_total_ttc);
545 unset($object->multicurrency_total_tva);
546 unset($object->multicurrency_total_localtax1);
547 unset($object->multicurrency_total_localtax2);
548
549
550 // If object has lines, remove $db property
551 if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
552 $nboflines = count($object->lines);
553 for ($i = 0; $i < $nboflines; $i++) {
554 $this->_cleanObjectDatas($object->lines[$i]);
555
556 unset($object->lines[$i]->lines);
557 unset($object->lines[$i]->note);
558 }
559 }
560
561 return $object;
562 }
563
572 private function _validate($data)
573 {
574 $myobject = array();
575 foreach ($this->bom->fields as $field => $propfield) {
576 if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) {
577 continue; // Not a mandatory field
578 }
579 if (!isset($data[$field])) {
580 throw new RestException(400, "$field field missing");
581 }
582 $myobject[$field] = $data[$field];
583 }
584 return $myobject;
585 }
586
592 private function checkRefNumbering()
593 {
594 $ref = substr($this->bom->ref, 1, 4);
595 if ($this->bom->status > 0 && $ref == 'PROV') {
596 throw new RestException(400, "Wrong naming scheme '(PROV%)' is only allowed on 'DRAFT' status. For automatic increment use 'auto' on the 'ref' field.");
597 }
598
599 if (strtolower($this->bom->ref) == 'auto') {
600 if (empty($this->bom->id) && $this->bom->status == 0) {
601 $this->bom->ref = ''; // 'ref' will auto incremented with '(PROV' + newID + ')'
602 } else {
603 $this->bom->fetch_product();
604 $numref = $this->bom->getNextNumRef($this->bom->product);
605 $this->bom->ref = $numref;
606 }
607 }
608 }
609}
$id
Definition account.php:39
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.