dolibarr 19.0.3
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 $this->bom->$field = $value;
255 }
256
257 $this->checkRefNumbering();
258
259 if ($this->bom->update(DolibarrApiAccess::$user) > 0) {
260 return $this->get($id);
261 } else {
262 throw new RestException(500, $this->bom->error);
263 }
264 }
265
272 public function delete($id)
273 {
274 if (!DolibarrApiAccess::$user->rights->bom->delete) {
275 throw new RestException(401);
276 }
277 $result = $this->bom->fetch($id);
278 if (!$result) {
279 throw new RestException(404, 'BOM not found');
280 }
281
282 if (!DolibarrApi::_checkAccessToResource('bom', $this->bom->id, 'bom_bom')) {
283 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
284 }
285
286 if (!$this->bom->delete(DolibarrApiAccess::$user)) {
287 throw new RestException(500, 'Error when deleting BOM : '.$this->bom->error);
288 }
289
290 return array(
291 'success' => array(
292 'code' => 200,
293 'message' => 'BOM deleted'
294 )
295 );
296 }
297
307 public function getLines($id)
308 {
309 if (!DolibarrApiAccess::$user->rights->bom->read) {
310 throw new RestException(401);
311 }
312
313 $result = $this->bom->fetch($id);
314 if (!$result) {
315 throw new RestException(404, 'BOM not found');
316 }
317
318 if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) {
319 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
320 }
321 $this->bom->getLinesArray();
322 $result = array();
323 foreach ($this->bom->lines as $line) {
324 array_push($result, $this->_cleanObjectDatas($line));
325 }
326 return $result;
327 }
328
339 public function postLine($id, $request_data = null)
340 {
341 if (!DolibarrApiAccess::$user->rights->bom->write) {
342 throw new RestException(401);
343 }
344
345 $result = $this->bom->fetch($id);
346 if (!$result) {
347 throw new RestException(404, 'BOM not found');
348 }
349
350 if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) {
351 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
352 }
353
354 $request_data = (object) $request_data;
355
356 $updateRes = $this->bom->addLine(
357 $request_data->fk_product,
358 $request_data->qty,
359 $request_data->qty_frozen,
360 $request_data->disable_stock_change,
361 $request_data->efficiency,
362 $request_data->position,
363 $request_data->fk_bom_child,
364 $request_data->import_key,
365 $request_data->fk_unit
366 );
367
368 if ($updateRes > 0) {
369 return $updateRes;
370 } else {
371 throw new RestException(400, $this->bom->error);
372 }
373 }
374
386 public function putLine($id, $lineid, $request_data = null)
387 {
388 if (!DolibarrApiAccess::$user->rights->bom->write) {
389 throw new RestException(401);
390 }
391
392 $result = $this->bom->fetch($id);
393 if (!$result) {
394 throw new RestException(404, 'BOM not found');
395 }
396
397 if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) {
398 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
399 }
400
401 $request_data = (object) $request_data;
402
403 $updateRes = $this->bom->updateLine(
404 $lineid,
405 $request_data->qty,
406 $request_data->qty_frozen,
407 $request_data->disable_stock_change,
408 $request_data->efficiency,
409 $request_data->position,
410 $request_data->import_key,
411 $request_data->fk_unit
412 );
413
414 if ($updateRes > 0) {
415 $result = $this->get($id);
416 unset($result->line);
417 return $this->_cleanObjectDatas($result);
418 }
419 return false;
420 }
421
437 public function deleteLine($id, $lineid)
438 {
439 if (!DolibarrApiAccess::$user->rights->bom->write) {
440 throw new RestException(401);
441 }
442
443 $result = $this->bom->fetch($id);
444 if (!$result) {
445 throw new RestException(404, 'BOM not found');
446 }
447
448 if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) {
449 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
450 }
451
452 //Check the rowid is a line of current bom object
453 $lineIdIsFromObject = false;
454 foreach ($this->bom->lines as $bl) {
455 if ($bl->id == $lineid) {
456 $lineIdIsFromObject = true;
457 break;
458 }
459 }
460 if (!$lineIdIsFromObject) {
461 throw new RestException(500, 'Line to delete (rowid: '.$lineid.') is not a line of BOM (id: '.$this->bom->id.')');
462 }
463
464 $updateRes = $this->bom->deleteline(DolibarrApiAccess::$user, $lineid);
465 if ($updateRes > 0) {
466 return $this->get($id);
467 } else {
468 throw new RestException(405, $this->bom->error);
469 }
470 }
471
472 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
479 protected function _cleanObjectDatas($object)
480 {
481 // phpcs:enable
482 $object = parent::_cleanObjectDatas($object);
483
484 unset($object->rowid);
485 unset($object->canvas);
486
487 unset($object->name);
488 unset($object->lastname);
489 unset($object->firstname);
490 unset($object->civility_id);
491 unset($object->statut);
492 unset($object->state);
493 unset($object->state_id);
494 unset($object->state_code);
495 unset($object->region);
496 unset($object->region_code);
497 unset($object->country);
498 unset($object->country_id);
499 unset($object->country_code);
500 unset($object->barcode_type);
501 unset($object->barcode_type_code);
502 unset($object->barcode_type_label);
503 unset($object->barcode_type_coder);
504 unset($object->total_ht);
505 unset($object->total_tva);
506 unset($object->total_localtax1);
507 unset($object->total_localtax2);
508 unset($object->total_ttc);
509 unset($object->fk_account);
510 unset($object->comments);
511 unset($object->note);
512 unset($object->mode_reglement_id);
513 unset($object->cond_reglement_id);
514 unset($object->cond_reglement);
515 unset($object->shipping_method_id);
516 unset($object->fk_incoterms);
517 unset($object->label_incoterms);
518 unset($object->location_incoterms);
519
520 // If object has lines, remove $db property
521 if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
522 $nboflines = count($object->lines);
523 for ($i = 0; $i < $nboflines; $i++) {
524 $this->_cleanObjectDatas($object->lines[$i]);
525
526 unset($object->lines[$i]->lines);
527 unset($object->lines[$i]->note);
528 }
529 }
530
531 return $object;
532 }
533
542 private function _validate($data)
543 {
544 $myobject = array();
545 foreach ($this->bom->fields as $field => $propfield) {
546 if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) {
547 continue; // Not a mandatory field
548 }
549 if (!isset($data[$field])) {
550 throw new RestException(400, "$field field missing");
551 }
552 $myobject[$field] = $data[$field];
553 }
554 return $myobject;
555 }
556
562 private function checkRefNumbering()
563 {
564 $ref = substr($this->bom->ref, 1, 4);
565 if ($this->bom->status > 0 && $ref == 'PROV') {
566 throw new RestException(400, "Wrong naming scheme '(PROV%)' is only allowed on 'DRAFT' status. For automatic increment use 'auto' on the 'ref' field.");
567 }
568
569 if (strtolower($this->bom->ref) == 'auto') {
570 if (empty($this->bom->id) && $this->bom->status == 0) {
571 $this->bom->ref = ''; // 'ref' will auto incremented with '(PROV' + newID + ')'
572 } else {
573 $this->bom->fetch_product();
574 $numref = $this->bom->getNextNumRef($this->bom->product);
575 $this->bom->ref = $numref;
576 }
577 }
578 }
579}
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.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria