99 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'')
103 if (!DolibarrApiAccess::$user->rights->bom->read) {
104 throw new RestException(401);
108 $tmpobject =
new BOM($this->db);
110 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
112 $restrictonsocid = 0;
116 if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
117 $search_sale = DolibarrApiAccess::$user->id;
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";
124 $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)";
126 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
127 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
129 $sql .=
" WHERE 1 = 1";
135 if ($tmpobject->ismultientitymanaged) {
136 $sql .=
' AND t.entity IN ('.getEntity($tmpobject->element).
')';
138 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
139 $sql .=
" AND t.fk_soc = sc.fk_soc";
141 if ($restrictonsocid && $socid) {
142 $sql .=
" AND t.fk_soc = ".((int) $socid);
144 if ($restrictonsocid && $search_sale > 0) {
145 $sql .=
" AND t.rowid = sc.fk_soc";
148 if ($restrictonsocid && $search_sale > 0) {
149 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
155 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
159 $sql .= $this->db->order($sortfield, $sortorder);
164 $offset = $limit * $page;
166 $sql .= $this->db->plimit($limit + 1, $offset);
169 $result = $this->db->query($sql);
171 $num = $this->db->num_rows($result);
174 $obj = $this->db->fetch_object($result);
175 $bom_static =
new BOM($this->db);
176 if ($bom_static->fetch($obj->rowid)) {
182 throw new RestException(503,
'Error when retrieve bom list');
184 if (!count($obj_ret)) {
185 throw new RestException(404,
'No bom found');
196 public function post($request_data =
null)
198 if (!DolibarrApiAccess::$user->rights->bom->write) {
199 throw new RestException(401);
202 $result = $this->
_validate($request_data);
204 foreach ($request_data as $field => $value) {
205 $this->bom->$field = $value;
210 if (!$this->bom->create(DolibarrApiAccess::$user)) {
211 throw new RestException(500,
"Error creating BOM", array_merge(array($this->bom->error), $this->bom->errors));
213 return $this->bom->id;
224 public function put($id, $request_data =
null)
226 if (!DolibarrApiAccess::$user->rights->bom->write) {
227 throw new RestException(401);
230 $result = $this->bom->fetch($id);
232 throw new RestException(404,
'BOM not found');
236 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
239 foreach ($request_data as $field => $value) {
240 if ($field ==
'id') {
243 $this->bom->$field = $value;
248 if ($this->bom->update(DolibarrApiAccess::$user) > 0) {
249 return $this->
get($id);
251 throw new RestException(500, $this->bom->error);
298 if (!DolibarrApiAccess::$user->rights->bom->read) {
299 throw new RestException(401);
302 $result = $this->bom->fetch($id);
304 throw new RestException(404,
'BOM not found');
308 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
310 $this->bom->getLinesArray();
312 foreach ($this->bom->lines as $line) {
328 public function postLine($id, $request_data =
null)
330 if (!DolibarrApiAccess::$user->rights->bom->write) {
331 throw new RestException(401);
334 $result = $this->bom->fetch($id);
336 throw new RestException(404,
'BOM not found');
340 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
343 $request_data = (object) $request_data;
345 $updateRes = $this->bom->addLine(
346 $request_data->fk_product,
348 $request_data->qty_frozen,
349 $request_data->disable_stock_change,
350 $request_data->efficiency,
351 $request_data->position,
352 $request_data->fk_bom_child,
353 $request_data->import_key,
354 $request_data->fk_unit
357 if ($updateRes > 0) {
360 throw new RestException(400, $this->bom->error);
375 public function putLine($id, $lineid, $request_data =
null)
377 if (!DolibarrApiAccess::$user->rights->bom->write) {
378 throw new RestException(401);
381 $result = $this->bom->fetch($id);
383 throw new RestException(404,
'BOM not found');
387 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
390 $request_data = (object) $request_data;
392 $updateRes = $this->bom->updateLine(
395 $request_data->qty_frozen,
396 $request_data->disable_stock_change,
397 $request_data->efficiency,
398 $request_data->position,
399 $request_data->import_key,
400 $request_data->fk_unit
403 if ($updateRes > 0) {
404 $result = $this->
get($id);
405 unset($result->line);
428 if (!DolibarrApiAccess::$user->rights->bom->write) {
429 throw new RestException(401);
432 $result = $this->bom->fetch($id);
434 throw new RestException(404,
'BOM not found');
438 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
442 $lineIdIsFromObject =
false;
443 foreach ($this->bom->lines as $bl) {
444 if ($bl->id == $lineid) {
445 $lineIdIsFromObject =
true;
449 if (!$lineIdIsFromObject) {
450 throw new RestException(500,
'Line to delete (rowid: '.$lineid.
') is not a line of BOM (id: '.$this->bom->id.
')');
453 $updateRes = $this->bom->deleteline(DolibarrApiAccess::$user, $lineid);
454 if ($updateRes > 0) {
455 return $this->
get($id);
457 throw new RestException(405, $this->bom->error);
471 $object = parent::_cleanObjectDatas($object);
473 unset($object->rowid);
474 unset($object->canvas);
476 unset($object->name);
477 unset($object->lastname);
478 unset($object->firstname);
479 unset($object->civility_id);
480 unset($object->statut);
481 unset($object->state);
482 unset($object->state_id);
483 unset($object->state_code);
484 unset($object->region);
485 unset($object->region_code);
486 unset($object->country);
487 unset($object->country_id);
488 unset($object->country_code);
489 unset($object->barcode_type);
490 unset($object->barcode_type_code);
491 unset($object->barcode_type_label);
492 unset($object->barcode_type_coder);
493 unset($object->total_ht);
494 unset($object->total_tva);
495 unset($object->total_localtax1);
496 unset($object->total_localtax2);
497 unset($object->total_ttc);
498 unset($object->fk_account);
499 unset($object->comments);
500 unset($object->note);
501 unset($object->mode_reglement_id);
502 unset($object->cond_reglement_id);
503 unset($object->cond_reglement);
504 unset($object->shipping_method_id);
505 unset($object->fk_incoterms);
506 unset($object->label_incoterms);
507 unset($object->location_incoterms);
510 if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
511 $nboflines = count($object->lines);
512 for ($i = 0; $i < $nboflines; $i++) {
515 unset($object->lines[$i]->lines);
516 unset($object->lines[$i]->note);
553 $ref = substr($this->bom->ref, 1, 4);
554 if ($this->bom->status > 0 && $ref ==
'PROV') {
555 throw new RestException(400,
"Wrong naming scheme '(PROV%)' is only allowed on 'DRAFT' status. For automatic increment use 'auto' on the 'ref' field.");
558 if (strtolower($this->bom->ref) ==
'auto') {
559 if (empty($this->bom->id) && $this->bom->status == 0) {
560 $this->bom->ref =
'';
562 $this->bom->fetch_product();
563 $numref = $this->bom->getNextNumRef($this->bom->product);
564 $this->bom->ref = $numref;