dolibarr 18.0.6
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
99 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '')
100 {
101 global $db, $conf;
102
103 if (!DolibarrApiAccess::$user->rights->bom->read) {
104 throw new RestException(401);
105 }
106
107 $obj_ret = array();
108 $tmpobject = new BOM($this->db);
109
110 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
111
112 $restrictonsocid = 0; // Set to 1 if there is a field socid in table of object
113
114 // If the internal user must only see his customers, force searching by him
115 $search_sale = 0;
116 if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
117 $search_sale = DolibarrApiAccess::$user->id;
118 }
119
120 $sql = "SELECT t.rowid";
121 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
122 $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)
123 }
124 $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
125
126 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
127 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
128 }
129 $sql .= " WHERE 1 = 1";
130
131 // Example of use $mode
132 //if ($mode == 1) $sql.= " AND s.client IN (1, 3)";
133 //if ($mode == 2) $sql.= " AND s.client IN (2, 3)";
134
135 if ($tmpobject->ismultientitymanaged) {
136 $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')';
137 }
138 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
139 $sql .= " AND t.fk_soc = sc.fk_soc";
140 }
141 if ($restrictonsocid && $socid) {
142 $sql .= " AND t.fk_soc = ".((int) $socid);
143 }
144 if ($restrictonsocid && $search_sale > 0) {
145 $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
146 }
147 // Insert sale filter
148 if ($restrictonsocid && $search_sale > 0) {
149 $sql .= " AND sc.fk_user = ".((int) $search_sale);
150 }
151 if ($sqlfilters) {
152 $errormessage = '';
153 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
154 if ($errormessage) {
155 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
156 }
157 }
158
159 $sql .= $this->db->order($sortfield, $sortorder);
160 if ($limit) {
161 if ($page < 0) {
162 $page = 0;
163 }
164 $offset = $limit * $page;
165
166 $sql .= $this->db->plimit($limit + 1, $offset);
167 }
168
169 $result = $this->db->query($sql);
170 if ($result) {
171 $num = $this->db->num_rows($result);
172 $i = 0;
173 while ($i < $num) {
174 $obj = $this->db->fetch_object($result);
175 $bom_static = new BOM($this->db);
176 if ($bom_static->fetch($obj->rowid)) {
177 $obj_ret[] = $this->_cleanObjectDatas($bom_static);
178 }
179 $i++;
180 }
181 } else {
182 throw new RestException(503, 'Error when retrieve bom list');
183 }
184 if (!count($obj_ret)) {
185 throw new RestException(404, 'No bom found');
186 }
187 return $obj_ret;
188 }
189
196 public function post($request_data = null)
197 {
198 if (!DolibarrApiAccess::$user->rights->bom->write) {
199 throw new RestException(401);
200 }
201 // Check mandatory fields
202 $result = $this->_validate($request_data);
203
204 foreach ($request_data as $field => $value) {
205 $this->bom->$field = $value;
206 }
207
208 $this->checkRefNumbering();
209
210 if (!$this->bom->create(DolibarrApiAccess::$user)) {
211 throw new RestException(500, "Error creating BOM", array_merge(array($this->bom->error), $this->bom->errors));
212 }
213 return $this->bom->id;
214 }
215
224 public function put($id, $request_data = null)
225 {
226 if (!DolibarrApiAccess::$user->rights->bom->write) {
227 throw new RestException(401);
228 }
229
230 $result = $this->bom->fetch($id);
231 if (!$result) {
232 throw new RestException(404, 'BOM not found');
233 }
234
235 if (!DolibarrApi::_checkAccessToResource('bom', $this->bom->id, 'bom_bom')) {
236 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
237 }
238
239 foreach ($request_data as $field => $value) {
240 if ($field == 'id') {
241 continue;
242 }
243 if ($field == 'array_options' && is_array($value)) {
244 foreach ($value as $index => $val) {
245 $this->bom->array_options[$index] = $this->_checkValForAPI($field, $val, $this->bom);
246 }
247 continue;
248 }
249 $this->bom->$field = $value;
250 }
251
252 $this->checkRefNumbering();
253
254 if ($this->bom->update(DolibarrApiAccess::$user) > 0) {
255 return $this->get($id);
256 } else {
257 throw new RestException(500, $this->bom->error);
258 }
259 }
260
267 public function delete($id)
268 {
269 if (!DolibarrApiAccess::$user->rights->bom->delete) {
270 throw new RestException(401);
271 }
272 $result = $this->bom->fetch($id);
273 if (!$result) {
274 throw new RestException(404, 'BOM not found');
275 }
276
277 if (!DolibarrApi::_checkAccessToResource('bom', $this->bom->id, 'bom_bom')) {
278 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
279 }
280
281 if (!$this->bom->delete(DolibarrApiAccess::$user)) {
282 throw new RestException(500, 'Error when deleting BOM : '.$this->bom->error);
283 }
284
285 return array(
286 'success' => array(
287 'code' => 200,
288 'message' => 'BOM deleted'
289 )
290 );
291 }
292
302 public function getLines($id)
303 {
304 if (!DolibarrApiAccess::$user->rights->bom->read) {
305 throw new RestException(401);
306 }
307
308 $result = $this->bom->fetch($id);
309 if (!$result) {
310 throw new RestException(404, 'BOM not found');
311 }
312
313 if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) {
314 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
315 }
316 $this->bom->getLinesArray();
317 $result = array();
318 foreach ($this->bom->lines as $line) {
319 array_push($result, $this->_cleanObjectDatas($line));
320 }
321 return $result;
322 }
323
334 public function postLine($id, $request_data = null)
335 {
336 if (!DolibarrApiAccess::$user->rights->bom->write) {
337 throw new RestException(401);
338 }
339
340 $result = $this->bom->fetch($id);
341 if (!$result) {
342 throw new RestException(404, 'BOM not found');
343 }
344
345 if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) {
346 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
347 }
348
349 $request_data = (object) $request_data;
350
351 $updateRes = $this->bom->addLine(
352 $request_data->fk_product,
353 $request_data->qty,
354 $request_data->qty_frozen,
355 $request_data->disable_stock_change,
356 $request_data->efficiency,
357 $request_data->position,
358 $request_data->fk_bom_child,
359 $request_data->import_key,
360 $request_data->fk_unit
361 );
362
363 if ($updateRes > 0) {
364 return $updateRes;
365 } else {
366 throw new RestException(400, $this->bom->error);
367 }
368 }
369
381 public function putLine($id, $lineid, $request_data = null)
382 {
383 if (!DolibarrApiAccess::$user->rights->bom->write) {
384 throw new RestException(401);
385 }
386
387 $result = $this->bom->fetch($id);
388 if (!$result) {
389 throw new RestException(404, 'BOM not found');
390 }
391
392 if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) {
393 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
394 }
395
396 $request_data = (object) $request_data;
397
398 $updateRes = $this->bom->updateLine(
399 $lineid,
400 $request_data->qty,
401 $request_data->qty_frozen,
402 $request_data->disable_stock_change,
403 $request_data->efficiency,
404 $request_data->position,
405 $request_data->import_key,
406 $request_data->fk_unit
407 );
408
409 if ($updateRes > 0) {
410 $result = $this->get($id);
411 unset($result->line);
412 return $this->_cleanObjectDatas($result);
413 }
414 return false;
415 }
416
432 public function deleteLine($id, $lineid)
433 {
434 if (!DolibarrApiAccess::$user->rights->bom->write) {
435 throw new RestException(401);
436 }
437
438 $result = $this->bom->fetch($id);
439 if (!$result) {
440 throw new RestException(404, 'BOM not found');
441 }
442
443 if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) {
444 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
445 }
446
447 //Check the rowid is a line of current bom object
448 $lineIdIsFromObject = false;
449 foreach ($this->bom->lines as $bl) {
450 if ($bl->id == $lineid) {
451 $lineIdIsFromObject = true;
452 break;
453 }
454 }
455 if (!$lineIdIsFromObject) {
456 throw new RestException(500, 'Line to delete (rowid: '.$lineid.') is not a line of BOM (id: '.$this->bom->id.')');
457 }
458
459 $updateRes = $this->bom->deleteline(DolibarrApiAccess::$user, $lineid);
460 if ($updateRes > 0) {
461 return $this->get($id);
462 } else {
463 throw new RestException(405, $this->bom->error);
464 }
465 }
466
467 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
474 protected function _cleanObjectDatas($object)
475 {
476 // phpcs:enable
477 $object = parent::_cleanObjectDatas($object);
478
479 unset($object->rowid);
480 unset($object->canvas);
481
482 unset($object->name);
483 unset($object->lastname);
484 unset($object->firstname);
485 unset($object->civility_id);
486 unset($object->statut);
487 unset($object->state);
488 unset($object->state_id);
489 unset($object->state_code);
490 unset($object->region);
491 unset($object->region_code);
492 unset($object->country);
493 unset($object->country_id);
494 unset($object->country_code);
495 unset($object->barcode_type);
496 unset($object->barcode_type_code);
497 unset($object->barcode_type_label);
498 unset($object->barcode_type_coder);
499 unset($object->total_ht);
500 unset($object->total_tva);
501 unset($object->total_localtax1);
502 unset($object->total_localtax2);
503 unset($object->total_ttc);
504 unset($object->fk_account);
505 unset($object->comments);
506 unset($object->note);
507 unset($object->mode_reglement_id);
508 unset($object->cond_reglement_id);
509 unset($object->cond_reglement);
510 unset($object->shipping_method_id);
511 unset($object->fk_incoterms);
512 unset($object->label_incoterms);
513 unset($object->location_incoterms);
514
515 // If object has lines, remove $db property
516 if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
517 $nboflines = count($object->lines);
518 for ($i = 0; $i < $nboflines; $i++) {
519 $this->_cleanObjectDatas($object->lines[$i]);
520
521 unset($object->lines[$i]->lines);
522 unset($object->lines[$i]->note);
523 }
524 }
525
526 return $object;
527 }
528
537 private function _validate($data)
538 {
539 $myobject = array();
540 foreach ($this->bom->fields as $field => $propfield) {
541 if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) {
542 continue; // Not a mandatory field
543 }
544 if (!isset($data[$field])) {
545 throw new RestException(400, "$field field missing");
546 }
547 $myobject[$field] = $data[$field];
548 }
549 return $myobject;
550 }
551
557 private function checkRefNumbering()
558 {
559 $ref = substr($this->bom->ref, 1, 4);
560 if ($this->bom->status > 0 && $ref == 'PROV') {
561 throw new RestException(400, "Wrong naming scheme '(PROV%)' is only allowed on 'DRAFT' status. For automatic increment use 'auto' on the 'ref' field.");
562 }
563
564 if (strtolower($this->bom->ref) == 'auto') {
565 if (empty($this->bom->id) && $this->bom->status == 0) {
566 $this->bom->ref = ''; // 'ref' will auto incremented with '(PROV' + newID + ')'
567 } else {
568 $this->bom->fetch_product();
569 $numref = $this->bom->getNextNumRef($this->bom->product);
570 $this->bom->ref = $numref;
571 }
572 }
573 }
574}
Class for BOM.
Definition bom.class.php:39
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.
__construct()
Constructor.
deleteLine($id, $lineid)
Delete a line to given BOM.
postLine($id, $request_data=null)
Add a line to given BOM.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='')
List boms.
_validate($data)
Validate fields before create or update object.
Class for API REST v1.
Definition api.class.php:31
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:86
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria