21use Luracast\Restler\RestException;
23require_once DOL_DOCUMENT_ROOT.
'/bom/class/bom.class.php';
53 $this->bom =
new BOM($this->db);
69 public function get(
$id)
71 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'read')) {
72 throw new RestException(403);
75 $result = $this->bom->fetch(
$id);
77 throw new RestException(404,
'BOM not found');
81 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
105 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'')
107 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'read')) {
108 throw new RestException(403);
112 $tmpobject =
new BOM($this->db);
114 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
116 $restrictonsocid = 0;
120 if ($restrictonsocid && !DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
121 $search_sale = DolibarrApiAccess::$user->id;
124 $sql =
"SELECT t.rowid";
125 $sql .=
" FROM ".MAIN_DB_PREFIX.$tmpobject->table_element.
" AS t";
126 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.$tmpobject->table_element.
"_extrafields AS ef ON (ef.fk_object = t.rowid)";
127 $sql .=
" WHERE 1 = 1";
128 if ($tmpobject->ismultientitymanaged) {
129 $sql .=
' AND t.entity IN ('.getEntity($tmpobject->element).
')';
131 if ($restrictonsocid && $socid) {
132 $sql .=
" AND t.fk_soc = ".((int) $socid);
135 if ($search_sale && $search_sale !=
'-1') {
136 if ($search_sale == -2) {
137 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
138 } elseif ($search_sale > 0) {
139 $sql .=
" AND EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc AND sc.fk_user = ".((int) $search_sale).
")";
146 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
150 $sql .= $this->db->order($sortfield, $sortorder);
155 $offset = $limit * $page;
157 $sql .= $this->db->plimit($limit + 1, $offset);
160 $result = $this->db->query($sql);
162 $num = $this->db->num_rows($result);
165 $obj = $this->db->fetch_object($result);
166 $bom_static =
new BOM($this->db);
167 if ($bom_static->fetch($obj->rowid)) {
173 throw new RestException(503,
'Error when retrieve bom list');
188 public function post($request_data =
null)
190 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'write')) {
191 throw new RestException(403);
194 $result = $this->
_validate($request_data);
196 foreach ($request_data as $field => $value) {
197 if ($field ===
'caller') {
199 $this->bom->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
203 $this->bom->$field = $this->
_checkValForAPI($field, $value, $this->bom);
208 if (!$this->bom->create(DolibarrApiAccess::$user)) {
209 throw new RestException(500,
"Error creating BOM", array_merge(array($this->bom->error), $this->bom->errors));
211 return $this->bom->id;
225 public function put(
$id, $request_data =
null)
227 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'write')) {
228 throw new RestException(403);
231 $result = $this->bom->fetch(
$id);
233 throw new RestException(404,
'BOM not found');
237 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
240 foreach ($request_data as $field => $value) {
241 if ($field ==
'id') {
244 if ($field ===
'caller') {
246 $this->bom->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
250 if ($field ==
'array_options' && is_array($value)) {
251 foreach ($value as $index => $val) {
252 $this->bom->array_options[$index] = $this->
_checkValForAPI(
'extrafields', $val, $this->bom);
257 $this->bom->$field = $this->
_checkValForAPI($field, $value, $this->bom);
262 if ($this->bom->update(DolibarrApiAccess::$user) > 0) {
263 return $this->
get(
$id);
265 throw new RestException(500, $this->bom->error);
279 public function delete(
$id)
281 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'delete')) {
282 throw new RestException(403);
284 $result = $this->bom->fetch(
$id);
286 throw new RestException(404,
'BOM not found');
290 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
293 if (!$this->bom->delete(DolibarrApiAccess::$user)) {
294 throw new RestException(500,
'Error when deleting BOM : '.$this->bom->error);
300 'message' =>
'BOM deleted'
319 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'read')) {
320 throw new RestException(403);
323 $result = $this->bom->fetch(
$id);
325 throw new RestException(404,
'BOM not found');
329 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
331 $this->bom->getLinesArray();
333 foreach ($this->bom->lines as $line) {
355 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'write')) {
356 throw new RestException(403);
359 $result = $this->bom->fetch(
$id);
361 throw new RestException(404,
'BOM not found');
365 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
368 $request_data = (object) $request_data;
370 $updateRes = $this->bom->addLine(
371 $request_data->fk_product,
373 $request_data->qty_frozen,
374 $request_data->disable_stock_change,
375 $request_data->efficiency,
376 $request_data->position,
377 $request_data->fk_bom_child,
378 $request_data->import_key,
379 $request_data->fk_unit
382 if ($updateRes > 0) {
385 throw new RestException(500, $this->bom->error);
405 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'write')) {
406 throw new RestException(403);
409 $result = $this->bom->fetch(
$id);
411 throw new RestException(404,
'BOM not found');
415 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
418 $request_data = (object) $request_data;
420 $updateRes = $this->bom->updateLine(
423 $request_data->qty_frozen,
424 $request_data->disable_stock_change,
425 $request_data->efficiency,
426 $request_data->position,
427 $request_data->import_key,
428 $request_data->fk_unit
431 if ($updateRes > 0) {
432 $result = $this->
get(
$id);
433 unset($result->line);
456 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'write')) {
457 throw new RestException(403);
460 $result = $this->bom->fetch(
$id);
462 throw new RestException(404,
'BOM not found');
466 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
470 $lineIdIsFromObject =
false;
471 foreach ($this->bom->lines as $bl) {
472 if ($bl->id == $lineid) {
473 $lineIdIsFromObject =
true;
477 if (!$lineIdIsFromObject) {
478 throw new RestException(500,
'Line to delete (rowid: '.$lineid.
') is not a line of BOM (id: '.$this->bom->id.
')');
481 $updateRes = $this->bom->deleteLine(DolibarrApiAccess::$user, $lineid);
482 if ($updateRes > 0) {
486 'message' =>
'line ' .$lineid.
' deleted'
490 throw new RestException(500, $this->bom->error);
523 unset(
$object->barcode_type_code);
524 unset(
$object->barcode_type_label);
525 unset(
$object->barcode_type_coder);
528 unset(
$object->total_localtax1);
529 unset(
$object->total_localtax2);
534 unset(
$object->mode_reglement_id);
535 unset(
$object->cond_reglement_id);
536 unset(
$object->cond_reglement);
537 unset(
$object->shipping_method_id);
539 unset(
$object->label_incoterms);
540 unset(
$object->location_incoterms);
541 unset(
$object->multicurrency_code);
542 unset(
$object->multicurrency_tx);
543 unset(
$object->multicurrency_total_ht);
544 unset(
$object->multicurrency_total_ttc);
545 unset(
$object->multicurrency_total_tva);
546 unset(
$object->multicurrency_total_localtax1);
547 unset(
$object->multicurrency_total_localtax2);
552 $nboflines = count(
$object->lines);
553 for ($i = 0; $i < $nboflines; $i++) {
556 unset(
$object->lines[$i]->lines);
557 unset(
$object->lines[$i]->note);
575 foreach ($this->bom->fields as $field => $propfield) {
576 if (in_array($field, array(
'rowid',
'entity',
'date_creation',
'tms',
'fk_user_creat')) || $propfield[
'notnull'] != 1) {
579 if (!isset($data[$field])) {
580 throw new RestException(400,
"$field field missing");
582 $myobject[$field] = $data[$field];
594 $ref = substr($this->bom->ref, 1, 4);
595 if ($this->bom->status > 0 && $ref ==
'PROV') {
596 throw new RestException(400,
"Wrong naming scheme '(PROV%)' is only allowed on 'DRAFT' status. For automatic increment use 'auto' on the 'ref' field.");
599 if (strtolower($this->bom->ref) ==
'auto') {
600 if (empty($this->bom->id) && $this->bom->status == 0) {
601 $this->bom->ref =
'';
603 $this->bom->fetch_product();
604 $numref = $this->bom->getNextNumRef($this->bom->product);
605 $this->bom->ref = $numref;
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
checkRefNumbering()
Validate the ref field and get the next Number if it's necessary.
put($id, $request_data=null)
Update bom.
_cleanObjectDatas($object)
Clean sensible object datas.
putLine($id, $lineid, $request_data=null)
Update a line to given BOM.
post($request_data=null)
Create bom object.
getLines($id)
Get lines of an BOM.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='')
List boms.
__construct()
Constructor.
deleteLine($id, $lineid)
Delete a line to given BOM.
postLine($id, $request_data=null)
Add a line to given BOM.
_validate($data)
Validate fields before create or update object.
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.