100 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'')
104 if (!DolibarrApiAccess::$user->rights->bom->read) {
105 throw new RestException(401);
109 $tmpobject =
new BOM($this->db);
111 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
113 $restrictonsocid = 0;
117 if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
118 $search_sale = DolibarrApiAccess::$user->id;
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";
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)";
127 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
128 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
130 $sql .=
" WHERE 1 = 1";
136 if ($tmpobject->ismultientitymanaged) {
137 $sql .=
' AND t.entity IN ('.getEntity($tmpobject->element).
')';
139 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
140 $sql .=
" AND t.fk_soc = sc.fk_soc";
142 if ($restrictonsocid && $socid) {
143 $sql .=
" AND t.fk_soc = ".((int) $socid);
145 if ($restrictonsocid && $search_sale > 0) {
146 $sql .=
" AND t.rowid = sc.fk_soc";
149 if ($restrictonsocid && $search_sale > 0) {
150 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
156 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
160 $sql .= $this->db->order($sortfield, $sortorder);
165 $offset = $limit * $page;
167 $sql .= $this->db->plimit($limit + 1, $offset);
170 $result = $this->db->query($sql);
172 $num = $this->db->num_rows($result);
175 $obj = $this->db->fetch_object($result);
176 $bom_static =
new BOM($this->db);
177 if ($bom_static->fetch($obj->rowid)) {
183 throw new RestException(503,
'Error when retrieve bom list');
195 public function post($request_data =
null)
197 if (!DolibarrApiAccess::$user->rights->bom->write) {
198 throw new RestException(401);
201 $result = $this->
_validate($request_data);
203 foreach ($request_data as $field => $value) {
204 if ($field ===
'caller') {
206 $this->bom->context[
'caller'] = $request_data[
'caller'];
210 $this->bom->$field = $value;
215 if (!$this->bom->create(DolibarrApiAccess::$user)) {
216 throw new RestException(500,
"Error creating BOM", array_merge(array($this->bom->error), $this->bom->errors));
218 return $this->bom->id;
229 public function put($id, $request_data =
null)
231 if (!DolibarrApiAccess::$user->rights->bom->write) {
232 throw new RestException(401);
235 $result = $this->bom->fetch($id);
237 throw new RestException(404,
'BOM not found');
241 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
244 foreach ($request_data as $field => $value) {
245 if ($field ==
'id') {
248 if ($field ===
'caller') {
250 $this->bom->context[
'caller'] = $request_data[
'caller'];
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);
260 $this->bom->$field = $this->
_checkValForAPI($field, $value, $this->bom);
265 if ($this->bom->update(DolibarrApiAccess::$user) > 0) {
266 return $this->
get($id);
268 throw new RestException(500, $this->bom->error);
315 if (!DolibarrApiAccess::$user->rights->bom->read) {
316 throw new RestException(401);
319 $result = $this->bom->fetch($id);
321 throw new RestException(404,
'BOM not found');
325 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
327 $this->bom->getLinesArray();
329 foreach ($this->bom->lines as $line) {
345 public function postLine($id, $request_data =
null)
347 if (!DolibarrApiAccess::$user->rights->bom->write) {
348 throw new RestException(401);
351 $result = $this->bom->fetch($id);
353 throw new RestException(404,
'BOM not found');
357 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
360 $request_data = (object) $request_data;
362 $updateRes = $this->bom->addLine(
363 $request_data->fk_product,
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
374 if ($updateRes > 0) {
377 throw new RestException(400, $this->bom->error);
392 public function putLine($id, $lineid, $request_data =
null)
394 if (!DolibarrApiAccess::$user->rights->bom->write) {
395 throw new RestException(401);
398 $result = $this->bom->fetch($id);
400 throw new RestException(404,
'BOM not found');
404 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
407 $request_data = (object) $request_data;
409 $updateRes = $this->bom->updateLine(
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
420 if ($updateRes > 0) {
421 $result = $this->
get($id);
422 unset($result->line);
445 if (!DolibarrApiAccess::$user->rights->bom->write) {
446 throw new RestException(401);
449 $result = $this->bom->fetch($id);
451 throw new RestException(404,
'BOM not found');
455 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
459 $lineIdIsFromObject =
false;
460 foreach ($this->bom->lines as $bl) {
461 if ($bl->id == $lineid) {
462 $lineIdIsFromObject =
true;
466 if (!$lineIdIsFromObject) {
467 throw new RestException(500,
'Line to delete (rowid: '.$lineid.
') is not a line of BOM (id: '.$this->bom->id.
')');
470 $updateRes = $this->bom->deleteline(DolibarrApiAccess::$user, $lineid);
471 if ($updateRes > 0) {
472 return $this->
get($id);
474 throw new RestException(405, $this->bom->error);
488 $object = parent::_cleanObjectDatas($object);
490 unset($object->rowid);
491 unset($object->canvas);
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);
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++) {
532 unset($object->lines[$i]->lines);
533 unset($object->lines[$i]->note);
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.");
575 if (strtolower($this->bom->ref) ==
'auto') {
576 if (empty($this->bom->id) && $this->bom->status == 0) {
577 $this->bom->ref =
'';
579 $this->bom->fetch_product();
580 $numref = $this->bom->getNextNumRef($this->bom->product);
581 $this->bom->ref = $numref;