dolibarr  17.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 
21 use Luracast\Restler\RestException;
22 
23 require_once DOL_DOCUMENT_ROOT.'/bom/class/bom.class.php';
24 
25 
38 class 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";
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  if (!$this->bom->create(DolibarrApiAccess::$user)) {
208  throw new RestException(500, "Error creating BOM", array_merge(array($this->bom->error), $this->bom->errors));
209  }
210  return $this->bom->id;
211  }
212 
221  public function put($id, $request_data = null)
222  {
223  if (!DolibarrApiAccess::$user->rights->bom->write) {
224  throw new RestException(401);
225  }
226 
227  $result = $this->bom->fetch($id);
228  if (!$result) {
229  throw new RestException(404, 'BOM not found');
230  }
231 
232  if (!DolibarrApi::_checkAccessToResource('bom', $this->bom->id, 'bom_bom')) {
233  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
234  }
235 
236  foreach ($request_data as $field => $value) {
237  if ($field == 'id') {
238  continue;
239  }
240  $this->bom->$field = $value;
241  }
242 
243  if ($this->bom->update(DolibarrApiAccess::$user) > 0) {
244  return $this->get($id);
245  } else {
246  throw new RestException(500, $this->bom->error);
247  }
248  }
249 
256  public function delete($id)
257  {
258  if (!DolibarrApiAccess::$user->rights->bom->delete) {
259  throw new RestException(401);
260  }
261  $result = $this->bom->fetch($id);
262  if (!$result) {
263  throw new RestException(404, 'BOM not found');
264  }
265 
266  if (!DolibarrApi::_checkAccessToResource('bom', $this->bom->id, 'bom_bom')) {
267  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
268  }
269 
270  if (!$this->bom->delete(DolibarrApiAccess::$user)) {
271  throw new RestException(500, 'Error when deleting BOM : '.$this->bom->error);
272  }
273 
274  return array(
275  'success' => array(
276  'code' => 200,
277  'message' => 'BOM deleted'
278  )
279  );
280  }
281 
291  public function getLines($id)
292  {
293  if (!DolibarrApiAccess::$user->rights->bom->read) {
294  throw new RestException(401);
295  }
296 
297  $result = $this->bom->fetch($id);
298  if (!$result) {
299  throw new RestException(404, 'BOM not found');
300  }
301 
302  if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) {
303  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
304  }
305  $this->bom->getLinesArray();
306  $result = array();
307  foreach ($this->bom->lines as $line) {
308  array_push($result, $this->_cleanObjectDatas($line));
309  }
310  return $result;
311  }
312 
323  public function postLine($id, $request_data = null)
324  {
325  if (!DolibarrApiAccess::$user->rights->bom->write) {
326  throw new RestException(401);
327  }
328 
329  $result = $this->bom->fetch($id);
330  if (!$result) {
331  throw new RestException(404, 'BOM not found');
332  }
333 
334  if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) {
335  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
336  }
337 
338  $request_data = (object) $request_data;
339 
340  $updateRes = $this->bom->addLine(
341  $request_data->fk_product,
342  $request_data->qty,
343  $request_data->qty_frozen,
344  $request_data->disable_stock_change,
345  $request_data->efficiency,
346  $request_data->position,
347  $request_data->fk_bom_child,
348  $request_data->import_key
349  );
350 
351  if ($updateRes > 0) {
352  return $updateRes;
353  } else {
354  throw new RestException(400, $this->bom->error);
355  }
356  }
357 
369  public function putLine($id, $lineid, $request_data = null)
370  {
371  if (!DolibarrApiAccess::$user->rights->bom->write) {
372  throw new RestException(401);
373  }
374 
375  $result = $this->bom->fetch($id);
376  if (!$result) {
377  throw new RestException(404, 'BOM not found');
378  }
379 
380  if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) {
381  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
382  }
383 
384  $request_data = (object) $request_data;
385 
386  $updateRes = $this->bom->updateLine(
387  $lineid,
388  $request_data->qty,
389  $request_data->qty_frozen,
390  $request_data->disable_stock_change,
391  $request_data->efficiency,
392  $request_data->position,
393  $request_data->import_key
394  );
395 
396  if ($updateRes > 0) {
397  $result = $this->get($id);
398  unset($result->line);
399  return $this->_cleanObjectDatas($result);
400  }
401  return false;
402  }
403 
419  public function deleteLine($id, $lineid)
420  {
421  if (!DolibarrApiAccess::$user->rights->bom->write) {
422  throw new RestException(401);
423  }
424 
425  $result = $this->bom->fetch($id);
426  if (!$result) {
427  throw new RestException(404, 'BOM not found');
428  }
429 
430  if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) {
431  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
432  }
433 
434  //Check the rowid is a line of current bom object
435  $lineIdIsFromObject = false;
436  foreach ($this->bom->lines as $bl) {
437  if ($bl->id == $lineid) {
438  $lineIdIsFromObject = true;
439  break;
440  }
441  }
442  if (!$lineIdIsFromObject) {
443  throw new RestException(500, 'Line to delete (rowid: '.$lineid.') is not a line of BOM (id: '.$this->bom->id.')');
444  }
445 
446  $updateRes = $this->bom->deleteline(DolibarrApiAccess::$user, $lineid);
447  if ($updateRes > 0) {
448  return $this->get($id);
449  } else {
450  throw new RestException(405, $this->bom->error);
451  }
452  }
453 
454  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
461  protected function _cleanObjectDatas($object)
462  {
463  // phpcs:enable
464  $object = parent::_cleanObjectDatas($object);
465 
466  unset($object->rowid);
467  unset($object->canvas);
468 
469  unset($object->name);
470  unset($object->lastname);
471  unset($object->firstname);
472  unset($object->civility_id);
473  unset($object->statut);
474  unset($object->state);
475  unset($object->state_id);
476  unset($object->state_code);
477  unset($object->region);
478  unset($object->region_code);
479  unset($object->country);
480  unset($object->country_id);
481  unset($object->country_code);
482  unset($object->barcode_type);
483  unset($object->barcode_type_code);
484  unset($object->barcode_type_label);
485  unset($object->barcode_type_coder);
486  unset($object->total_ht);
487  unset($object->total_tva);
488  unset($object->total_localtax1);
489  unset($object->total_localtax2);
490  unset($object->total_ttc);
491  unset($object->fk_account);
492  unset($object->comments);
493  unset($object->note);
494  unset($object->mode_reglement_id);
495  unset($object->cond_reglement_id);
496  unset($object->cond_reglement);
497  unset($object->shipping_method_id);
498  unset($object->fk_incoterms);
499  unset($object->label_incoterms);
500  unset($object->location_incoterms);
501 
502  // If object has lines, remove $db property
503  if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
504  $nboflines = count($object->lines);
505  for ($i = 0; $i < $nboflines; $i++) {
506  $this->_cleanObjectDatas($object->lines[$i]);
507 
508  unset($object->lines[$i]->lines);
509  unset($object->lines[$i]->note);
510  }
511  }
512 
513  return $object;
514  }
515 
524  private function _validate($data)
525  {
526  $myobject = array();
527  foreach ($this->bom->fields as $field => $propfield) {
528  if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) {
529  continue; // Not a mandatory field
530  }
531  if (!isset($data[$field])) {
532  throw new RestException(400, "$field field missing");
533  }
534  $myobject[$field] = $data[$field];
535  }
536  return $myobject;
537  }
538 }
Class for BOM.
Definition: bom.class.php:37
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.
Definition: api.class.php:283
forgeSQLFromUniversalSearchCriteria($filter, &$error='')
forgeSQLFromUniversalSearchCriteria
$conf db
API class for accounts.
Definition: inc.php:41