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 if ($field ==
'array_options' && is_array($value)) {
244 foreach ($value as $index => $val) {
245 $this->bom->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->bom);
249 $this->bom->$field = $value;
254 if ($this->bom->update(DolibarrApiAccess::$user) > 0) {
255 return $this->
get($id);
257 throw new RestException(500, $this->bom->error);
304 if (!DolibarrApiAccess::$user->rights->bom->read) {
305 throw new RestException(401);
308 $result = $this->bom->fetch($id);
310 throw new RestException(404,
'BOM not found');
314 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
316 $this->bom->getLinesArray();
318 foreach ($this->bom->lines as $line) {
334 public function postLine($id, $request_data =
null)
336 if (!DolibarrApiAccess::$user->rights->bom->write) {
337 throw new RestException(401);
340 $result = $this->bom->fetch($id);
342 throw new RestException(404,
'BOM not found');
346 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
349 $request_data = (object) $request_data;
351 $updateRes = $this->bom->addLine(
352 $request_data->fk_product,
354 $request_data->qty_frozen,
355 $request_data->disable_stock_change,
356 $request_data->efficiency,
357 $request_data->position,
358 $request_data->fk_bom_child,
359 $request_data->import_key,
360 $request_data->fk_unit
363 if ($updateRes > 0) {
366 throw new RestException(400, $this->bom->error);
381 public function putLine($id, $lineid, $request_data =
null)
383 if (!DolibarrApiAccess::$user->rights->bom->write) {
384 throw new RestException(401);
387 $result = $this->bom->fetch($id);
389 throw new RestException(404,
'BOM not found');
393 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
396 $request_data = (object) $request_data;
398 $updateRes = $this->bom->updateLine(
401 $request_data->qty_frozen,
402 $request_data->disable_stock_change,
403 $request_data->efficiency,
404 $request_data->position,
405 $request_data->import_key,
406 $request_data->fk_unit
409 if ($updateRes > 0) {
410 $result = $this->
get($id);
411 unset($result->line);
434 if (!DolibarrApiAccess::$user->rights->bom->write) {
435 throw new RestException(401);
438 $result = $this->bom->fetch($id);
440 throw new RestException(404,
'BOM not found');
444 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
448 $lineIdIsFromObject =
false;
449 foreach ($this->bom->lines as $bl) {
450 if ($bl->id == $lineid) {
451 $lineIdIsFromObject =
true;
455 if (!$lineIdIsFromObject) {
456 throw new RestException(500,
'Line to delete (rowid: '.$lineid.
') is not a line of BOM (id: '.$this->bom->id.
')');
459 $updateRes = $this->bom->deleteline(DolibarrApiAccess::$user, $lineid);
460 if ($updateRes > 0) {
461 return $this->
get($id);
463 throw new RestException(405, $this->bom->error);
477 $object = parent::_cleanObjectDatas($object);
479 unset($object->rowid);
480 unset($object->canvas);
482 unset($object->name);
483 unset($object->lastname);
484 unset($object->firstname);
485 unset($object->civility_id);
486 unset($object->statut);
487 unset($object->state);
488 unset($object->state_id);
489 unset($object->state_code);
490 unset($object->region);
491 unset($object->region_code);
492 unset($object->country);
493 unset($object->country_id);
494 unset($object->country_code);
495 unset($object->barcode_type);
496 unset($object->barcode_type_code);
497 unset($object->barcode_type_label);
498 unset($object->barcode_type_coder);
499 unset($object->total_ht);
500 unset($object->total_tva);
501 unset($object->total_localtax1);
502 unset($object->total_localtax2);
503 unset($object->total_ttc);
504 unset($object->fk_account);
505 unset($object->comments);
506 unset($object->note);
507 unset($object->mode_reglement_id);
508 unset($object->cond_reglement_id);
509 unset($object->cond_reglement);
510 unset($object->shipping_method_id);
511 unset($object->fk_incoterms);
512 unset($object->label_incoterms);
513 unset($object->location_incoterms);
516 if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
517 $nboflines = count($object->lines);
518 for ($i = 0; $i < $nboflines; $i++) {
521 unset($object->lines[$i]->lines);
522 unset($object->lines[$i]->note);
559 $ref = substr($this->bom->ref, 1, 4);
560 if ($this->bom->status > 0 && $ref ==
'PROV') {
561 throw new RestException(400,
"Wrong naming scheme '(PROV%)' is only allowed on 'DRAFT' status. For automatic increment use 'auto' on the 'ref' field.");
564 if (strtolower($this->bom->ref) ==
'auto') {
565 if (empty($this->bom->id) && $this->bom->status == 0) {
566 $this->bom->ref =
'';
568 $this->bom->fetch_product();
569 $numref = $this->bom->getNextNumRef($this->bom->product);
570 $this->bom->ref = $numref;