dolibarr 19.0.4
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 Frédéric France <frederic.france@netlogic.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, $conf;
51 $this->db = $db;
52 $this->bom = new BOM($this->db);
53 }
54
66 public function get($id)
67 {
68 if (!DolibarrApiAccess::$user->rights->bom->read) {
69 throw new RestException(401);
70 }
71
72 $result = $this->bom->fetch($id);
73 if (!$result) {
74 throw new RestException(404, 'BOM not found');
75 }
76
77 if (!DolibarrApi::_checkAccessToResource('bom', $this->bom->id, 'bom_bom')) {
78 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
79 }
80
81 return $this->_cleanObjectDatas($this->bom);
82 }
83
84
100 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '')
101 {
102 global $db, $conf;
103
104 if (!DolibarrApiAccess::$user->rights->bom->read) {
105 throw new RestException(401);
106 }
107
108 $obj_ret = array();
109 $tmpobject = new BOM($this->db);
110
111 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
112
113 $restrictonsocid = 0; // Set to 1 if there is a field socid in table of object
114
115 // If the internal user must only see his customers, force searching by him
116 $search_sale = 0;
117 if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
118 $search_sale = DolibarrApiAccess::$user->id;
119 }
120
121 $sql = "SELECT t.rowid";
122 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
123 $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects)
124 }
125 $sql .= " FROM ".MAIN_DB_PREFIX.$tmpobject->table_element." AS t 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
126
127 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
128 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
129 }
130 $sql .= " WHERE 1 = 1";
131
132 // Example of use $mode
133 //if ($mode == 1) $sql.= " AND s.client IN (1, 3)";
134 //if ($mode == 2) $sql.= " AND s.client IN (2, 3)";
135
136 if ($tmpobject->ismultientitymanaged) {
137 $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')';
138 }
139 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
140 $sql .= " AND t.fk_soc = sc.fk_soc";
141 }
142 if ($restrictonsocid && $socid) {
143 $sql .= " AND t.fk_soc = ".((int) $socid);
144 }
145 if ($restrictonsocid && $search_sale > 0) {
146 $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
147 }
148 // Insert sale filter
149 if ($restrictonsocid && $search_sale > 0) {
150 $sql .= " AND sc.fk_user = ".((int) $search_sale);
151 }
152 if ($sqlfilters) {
153 $errormessage = '';
154 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
155 if ($errormessage) {
156 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
157 }
158 }
159
160 $sql .= $this->db->order($sortfield, $sortorder);
161 if ($limit) {
162 if ($page < 0) {
163 $page = 0;
164 }
165 $offset = $limit * $page;
166
167 $sql .= $this->db->plimit($limit + 1, $offset);
168 }
169
170 $result = $this->db->query($sql);
171 if ($result) {
172 $num = $this->db->num_rows($result);
173 $i = 0;
174 while ($i < $num) {
175 $obj = $this->db->fetch_object($result);
176 $bom_static = new BOM($this->db);
177 if ($bom_static->fetch($obj->rowid)) {
178 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($bom_static), $properties);
179 }
180 $i++;
181 }
182 } else {
183 throw new RestException(503, 'Error when retrieve bom list');
184 }
185
186 return $obj_ret;
187 }
188
195 public function post($request_data = null)
196 {
197 if (!DolibarrApiAccess::$user->rights->bom->write) {
198 throw new RestException(401);
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 whith the caller
206 $this->bom->context['caller'] = $request_data['caller'];
207 continue;
208 }
209
210 $this->bom->$field = $value;
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
229 public function put($id, $request_data = null)
230 {
231 if (!DolibarrApiAccess::$user->rights->bom->write) {
232 throw new RestException(401);
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(401, '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 whith the caller
250 $this->bom->context['caller'] = $request_data['caller'];
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($field, $val, $this->bom);
257 }
258 continue;
259 }
260 $this->bom->$field = $this->_checkValForAPI($field, $value, $this->bom);
261 }
262
263 $this->checkRefNumbering();
264
265 if ($this->bom->update(DolibarrApiAccess::$user) > 0) {
266 return $this->get($id);
267 } else {
268 throw new RestException(500, $this->bom->error);
269 }
270 }
271
278 public function delete($id)
279 {
280 if (!DolibarrApiAccess::$user->rights->bom->delete) {
281 throw new RestException(401);
282 }
283 $result = $this->bom->fetch($id);
284 if (!$result) {
285 throw new RestException(404, 'BOM not found');
286 }
287
288 if (!DolibarrApi::_checkAccessToResource('bom', $this->bom->id, 'bom_bom')) {
289 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
290 }
291
292 if (!$this->bom->delete(DolibarrApiAccess::$user)) {
293 throw new RestException(500, 'Error when deleting BOM : '.$this->bom->error);
294 }
295
296 return array(
297 'success' => array(
298 'code' => 200,
299 'message' => 'BOM deleted'
300 )
301 );
302 }
303
313 public function getLines($id)
314 {
315 if (!DolibarrApiAccess::$user->rights->bom->read) {
316 throw new RestException(401);
317 }
318
319 $result = $this->bom->fetch($id);
320 if (!$result) {
321 throw new RestException(404, 'BOM not found');
322 }
323
324 if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) {
325 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
326 }
327 $this->bom->getLinesArray();
328 $result = array();
329 foreach ($this->bom->lines as $line) {
330 array_push($result, $this->_cleanObjectDatas($line));
331 }
332 return $result;
333 }
334
345 public function postLine($id, $request_data = null)
346 {
347 if (!DolibarrApiAccess::$user->rights->bom->write) {
348 throw new RestException(401);
349 }
350
351 $result = $this->bom->fetch($id);
352 if (!$result) {
353 throw new RestException(404, 'BOM not found');
354 }
355
356 if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) {
357 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
358 }
359
360 $request_data = (object) $request_data;
361
362 $updateRes = $this->bom->addLine(
363 $request_data->fk_product,
364 $request_data->qty,
365 $request_data->qty_frozen,
366 $request_data->disable_stock_change,
367 $request_data->efficiency,
368 $request_data->position,
369 $request_data->fk_bom_child,
370 $request_data->import_key,
371 $request_data->fk_unit
372 );
373
374 if ($updateRes > 0) {
375 return $updateRes;
376 } else {
377 throw new RestException(400, $this->bom->error);
378 }
379 }
380
392 public function putLine($id, $lineid, $request_data = null)
393 {
394 if (!DolibarrApiAccess::$user->rights->bom->write) {
395 throw new RestException(401);
396 }
397
398 $result = $this->bom->fetch($id);
399 if (!$result) {
400 throw new RestException(404, 'BOM not found');
401 }
402
403 if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) {
404 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
405 }
406
407 $request_data = (object) $request_data;
408
409 $updateRes = $this->bom->updateLine(
410 $lineid,
411 $request_data->qty,
412 $request_data->qty_frozen,
413 $request_data->disable_stock_change,
414 $request_data->efficiency,
415 $request_data->position,
416 $request_data->import_key,
417 $request_data->fk_unit
418 );
419
420 if ($updateRes > 0) {
421 $result = $this->get($id);
422 unset($result->line);
423 return $this->_cleanObjectDatas($result);
424 }
425 return false;
426 }
427
443 public function deleteLine($id, $lineid)
444 {
445 if (!DolibarrApiAccess::$user->rights->bom->write) {
446 throw new RestException(401);
447 }
448
449 $result = $this->bom->fetch($id);
450 if (!$result) {
451 throw new RestException(404, 'BOM not found');
452 }
453
454 if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) {
455 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
456 }
457
458 //Check the rowid is a line of current bom object
459 $lineIdIsFromObject = false;
460 foreach ($this->bom->lines as $bl) {
461 if ($bl->id == $lineid) {
462 $lineIdIsFromObject = true;
463 break;
464 }
465 }
466 if (!$lineIdIsFromObject) {
467 throw new RestException(500, 'Line to delete (rowid: '.$lineid.') is not a line of BOM (id: '.$this->bom->id.')');
468 }
469
470 $updateRes = $this->bom->deleteline(DolibarrApiAccess::$user, $lineid);
471 if ($updateRes > 0) {
472 return $this->get($id);
473 } else {
474 throw new RestException(405, $this->bom->error);
475 }
476 }
477
478 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
485 protected function _cleanObjectDatas($object)
486 {
487 // phpcs:enable
488 $object = parent::_cleanObjectDatas($object);
489
490 unset($object->rowid);
491 unset($object->canvas);
492
493 unset($object->name);
494 unset($object->lastname);
495 unset($object->firstname);
496 unset($object->civility_id);
497 unset($object->statut);
498 unset($object->state);
499 unset($object->state_id);
500 unset($object->state_code);
501 unset($object->region);
502 unset($object->region_code);
503 unset($object->country);
504 unset($object->country_id);
505 unset($object->country_code);
506 unset($object->barcode_type);
507 unset($object->barcode_type_code);
508 unset($object->barcode_type_label);
509 unset($object->barcode_type_coder);
510 unset($object->total_ht);
511 unset($object->total_tva);
512 unset($object->total_localtax1);
513 unset($object->total_localtax2);
514 unset($object->total_ttc);
515 unset($object->fk_account);
516 unset($object->comments);
517 unset($object->note);
518 unset($object->mode_reglement_id);
519 unset($object->cond_reglement_id);
520 unset($object->cond_reglement);
521 unset($object->shipping_method_id);
522 unset($object->fk_incoterms);
523 unset($object->label_incoterms);
524 unset($object->location_incoterms);
525
526 // If object has lines, remove $db property
527 if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
528 $nboflines = count($object->lines);
529 for ($i = 0; $i < $nboflines; $i++) {
530 $this->_cleanObjectDatas($object->lines[$i]);
531
532 unset($object->lines[$i]->lines);
533 unset($object->lines[$i]->note);
534 }
535 }
536
537 return $object;
538 }
539
548 private function _validate($data)
549 {
550 $myobject = array();
551 foreach ($this->bom->fields as $field => $propfield) {
552 if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) {
553 continue; // Not a mandatory field
554 }
555 if (!isset($data[$field])) {
556 throw new RestException(400, "$field field missing");
557 }
558 $myobject[$field] = $data[$field];
559 }
560 return $myobject;
561 }
562
568 private function checkRefNumbering()
569 {
570 $ref = substr($this->bom->ref, 1, 4);
571 if ($this->bom->status > 0 && $ref == 'PROV') {
572 throw new RestException(400, "Wrong naming scheme '(PROV%)' is only allowed on 'DRAFT' status. For automatic increment use 'auto' on the 'ref' field.");
573 }
574
575 if (strtolower($this->bom->ref) == 'auto') {
576 if (empty($this->bom->id) && $this->bom->status == 0) {
577 $this->bom->ref = ''; // 'ref' will auto incremented with '(PROV' + newID + ')'
578 } else {
579 $this->bom->fetch_product();
580 $numref = $this->bom->getNextNumRef($this->bom->product);
581 $this->bom->ref = $numref;
582 }
583 }
584 }
585}
Class for BOM.
Definition bom.class.php:43
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:31
_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:85
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria