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 $this->bom->$field = $value;
259 if ($this->bom->update(DolibarrApiAccess::$user) > 0) {
260 return $this->
get($id);
262 throw new RestException(500, $this->bom->error);
309 if (!DolibarrApiAccess::$user->rights->bom->read) {
310 throw new RestException(401);
313 $result = $this->bom->fetch($id);
315 throw new RestException(404,
'BOM not found');
319 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
321 $this->bom->getLinesArray();
323 foreach ($this->bom->lines as $line) {
339 public function postLine($id, $request_data =
null)
341 if (!DolibarrApiAccess::$user->rights->bom->write) {
342 throw new RestException(401);
345 $result = $this->bom->fetch($id);
347 throw new RestException(404,
'BOM not found');
351 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
354 $request_data = (object) $request_data;
356 $updateRes = $this->bom->addLine(
357 $request_data->fk_product,
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
368 if ($updateRes > 0) {
371 throw new RestException(400, $this->bom->error);
386 public function putLine($id, $lineid, $request_data =
null)
388 if (!DolibarrApiAccess::$user->rights->bom->write) {
389 throw new RestException(401);
392 $result = $this->bom->fetch($id);
394 throw new RestException(404,
'BOM not found');
398 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
401 $request_data = (object) $request_data;
403 $updateRes = $this->bom->updateLine(
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
414 if ($updateRes > 0) {
415 $result = $this->
get($id);
416 unset($result->line);
439 if (!DolibarrApiAccess::$user->rights->bom->write) {
440 throw new RestException(401);
443 $result = $this->bom->fetch($id);
445 throw new RestException(404,
'BOM not found');
449 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
453 $lineIdIsFromObject =
false;
454 foreach ($this->bom->lines as $bl) {
455 if ($bl->id == $lineid) {
456 $lineIdIsFromObject =
true;
460 if (!$lineIdIsFromObject) {
461 throw new RestException(500,
'Line to delete (rowid: '.$lineid.
') is not a line of BOM (id: '.$this->bom->id.
')');
464 $updateRes = $this->bom->deleteline(DolibarrApiAccess::$user, $lineid);
465 if ($updateRes > 0) {
466 return $this->
get($id);
468 throw new RestException(405, $this->bom->error);
482 $object = parent::_cleanObjectDatas($object);
484 unset($object->rowid);
485 unset($object->canvas);
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);
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++) {
526 unset($object->lines[$i]->lines);
527 unset($object->lines[$i]->note);
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.");
569 if (strtolower($this->bom->ref) ==
'auto') {
570 if (empty($this->bom->id) && $this->bom->status == 0) {
571 $this->bom->ref =
'';
573 $this->bom->fetch_product();
574 $numref = $this->bom->getNextNumRef($this->bom->product);
575 $this->bom->ref = $numref;