dolibarr  16.0.5
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  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 use Luracast\Restler\RestException;
21 
22 require_once DOL_DOCUMENT_ROOT.'/bom/class/bom.class.php';
23 
24 
37 class Boms extends DolibarrApi
38 {
42  public $bom;
43 
47  public function __construct()
48  {
49  global $db, $conf;
50  $this->db = $db;
51  $this->bom = new BOM($this->db);
52  }
53 
65  public function get($id)
66  {
67  if (!DolibarrApiAccess::$user->rights->bom->read) {
68  throw new RestException(401);
69  }
70 
71  $result = $this->bom->fetch($id);
72  if (!$result) {
73  throw new RestException(404, 'BOM not found');
74  }
75 
76  if (!DolibarrApi::_checkAccessToResource('bom', $this->bom->id, 'bom_bom')) {
77  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
78  }
79 
80  return $this->_cleanObjectDatas($this->bom);
81  }
82 
83 
98  public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '')
99  {
100  global $db, $conf;
101 
102  if (!DolibarrApiAccess::$user->rights->bom->read) {
103  throw new RestException(401);
104  }
105 
106  $obj_ret = array();
107  $tmpobject = new BOM($this->db);
108 
109  $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
110 
111  $restrictonsocid = 0; // Set to 1 if there is a field socid in table of object
112 
113  // If the internal user must only see his customers, force searching by him
114  $search_sale = 0;
115  if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
116  $search_sale = DolibarrApiAccess::$user->id;
117  }
118 
119  $sql = "SELECT t.rowid";
120  if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
121  $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)
122  }
123  $sql .= " FROM ".MAIN_DB_PREFIX.$tmpobject->table_element." as t";
124 
125  if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
126  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
127  }
128  $sql .= " WHERE 1 = 1";
129 
130  // Example of use $mode
131  //if ($mode == 1) $sql.= " AND s.client IN (1, 3)";
132  //if ($mode == 2) $sql.= " AND s.client IN (2, 3)";
133 
134  if ($tmpobject->ismultientitymanaged) {
135  $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')';
136  }
137  if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
138  $sql .= " AND t.fk_soc = sc.fk_soc";
139  }
140  if ($restrictonsocid && $socid) {
141  $sql .= " AND t.fk_soc = ".((int) $socid);
142  }
143  if ($restrictonsocid && $search_sale > 0) {
144  $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
145  }
146  // Insert sale filter
147  if ($restrictonsocid && $search_sale > 0) {
148  $sql .= " AND sc.fk_user = ".((int) $search_sale);
149  }
150  if ($sqlfilters) {
151  $errormessage = '';
152  if (!DolibarrApi::_checkFilters($sqlfilters, $errormessage)) {
153  throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
154  }
155  $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
156  $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
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 
282 
283  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
290  protected function _cleanObjectDatas($object)
291  {
292  // phpcs:enable
293  $object = parent::_cleanObjectDatas($object);
294 
295  unset($object->rowid);
296  unset($object->canvas);
297 
298  unset($object->name);
299  unset($object->lastname);
300  unset($object->firstname);
301  unset($object->civility_id);
302  unset($object->statut);
303  unset($object->state);
304  unset($object->state_id);
305  unset($object->state_code);
306  unset($object->region);
307  unset($object->region_code);
308  unset($object->country);
309  unset($object->country_id);
310  unset($object->country_code);
311  unset($object->barcode_type);
312  unset($object->barcode_type_code);
313  unset($object->barcode_type_label);
314  unset($object->barcode_type_coder);
315  unset($object->total_ht);
316  unset($object->total_tva);
317  unset($object->total_localtax1);
318  unset($object->total_localtax2);
319  unset($object->total_ttc);
320  unset($object->fk_account);
321  unset($object->comments);
322  unset($object->note);
323  unset($object->mode_reglement_id);
324  unset($object->cond_reglement_id);
325  unset($object->cond_reglement);
326  unset($object->shipping_method_id);
327  unset($object->fk_incoterms);
328  unset($object->label_incoterms);
329  unset($object->location_incoterms);
330 
331  // If object has lines, remove $db property
332  if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
333  $nboflines = count($object->lines);
334  for ($i = 0; $i < $nboflines; $i++) {
335  $this->_cleanObjectDatas($object->lines[$i]);
336 
337  unset($object->lines[$i]->lines);
338  unset($object->lines[$i]->note);
339  }
340  }
341 
342  return $object;
343  }
344 
353  private function _validate($data)
354  {
355  $myobject = array();
356  foreach ($this->bom->fields as $field => $propfield) {
357  if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) {
358  continue; // Not a mandatory field
359  }
360  if (!isset($data[$field])) {
361  throw new RestException(400, "$field field missing");
362  }
363  $myobject[$field] = $data[$field];
364  }
365  return $myobject;
366  }
367 }
Boms
Definition: api_boms.class.php:37
db
$conf db
API class for accounts.
Definition: inc.php:41
Boms\put
put($id, $request_data=null)
Update bom.
Definition: api_boms.class.php:221
Boms\index
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='')
List boms.
Definition: api_boms.class.php:98
Boms\__construct
__construct()
Constructor.
Definition: api_boms.class.php:47
DolibarrApi\_checkAccessToResource
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
BOM
Class for BOM.
Definition: bom.class.php:34
DolibarrApi
Class for API REST v1.
Definition: api.class.php:30
Boms\post
post($request_data=null)
Create bom object.
Definition: api_boms.class.php:196
DolibarrApi\_checkFilters
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:310
Boms\_cleanObjectDatas
_cleanObjectDatas($object)
Clean sensible object datas.
Definition: api_boms.class.php:290
Boms\_validate
_validate($data)
Validate fields before create or update object.
Definition: api_boms.class.php:353