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,
380 $request_data->array_options,
381 $request_data->fk_default_workstation
384 if ($updateRes > 0) {
387 throw new RestException(500, $this->bom->error);
407 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'write')) {
408 throw new RestException(403);
411 $result = $this->bom->fetch(
$id);
413 throw new RestException(404,
'BOM not found');
417 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
420 $request_data = (object) $request_data;
422 $updateRes = $this->bom->updateLine(
425 $request_data->qty_frozen,
426 $request_data->disable_stock_change,
427 $request_data->efficiency,
428 $request_data->position,
429 $request_data->import_key,
430 $request_data->fk_unit,
431 $request_data->array_options,
432 $request_data->fk_default_workstation
435 if ($updateRes > 0) {
436 $result = $this->
get(
$id);
437 unset($result->line);
460 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'write')) {
461 throw new RestException(403);
464 $result = $this->bom->fetch(
$id);
466 throw new RestException(404,
'BOM not found');
470 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
474 $lineIdIsFromObject =
false;
475 foreach ($this->bom->lines as $bl) {
476 if ($bl->id == $lineid) {
477 $lineIdIsFromObject =
true;
481 if (!$lineIdIsFromObject) {
482 throw new RestException(500,
'Line to delete (rowid: '.$lineid.
') is not a line of BOM (id: '.$this->bom->id.
')');
485 $updateRes = $this->bom->deleteLine(DolibarrApiAccess::$user, $lineid);
486 if ($updateRes > 0) {
490 'message' =>
'line ' .$lineid.
' deleted'
494 throw new RestException(500, $this->bom->error);
527 unset(
$object->barcode_type_code);
528 unset(
$object->barcode_type_label);
529 unset(
$object->barcode_type_coder);
532 unset(
$object->total_localtax1);
533 unset(
$object->total_localtax2);
538 unset(
$object->mode_reglement_id);
539 unset(
$object->cond_reglement_id);
540 unset(
$object->cond_reglement);
541 unset(
$object->shipping_method_id);
543 unset(
$object->label_incoterms);
544 unset(
$object->location_incoterms);
545 unset(
$object->multicurrency_code);
546 unset(
$object->multicurrency_tx);
547 unset(
$object->multicurrency_total_ht);
548 unset(
$object->multicurrency_total_ttc);
549 unset(
$object->multicurrency_total_tva);
550 unset(
$object->multicurrency_total_localtax1);
551 unset(
$object->multicurrency_total_localtax2);
556 $nboflines = count(
$object->lines);
557 for ($i = 0; $i < $nboflines; $i++) {
560 unset(
$object->lines[$i]->lines);
561 unset(
$object->lines[$i]->note);
579 foreach ($this->bom->fields as $field => $propfield) {
580 if (in_array($field, array(
'rowid',
'entity',
'date_creation',
'tms',
'fk_user_creat')) || $propfield[
'notnull'] != 1) {
583 if (!isset($data[$field])) {
584 throw new RestException(400,
"$field field missing");
586 $myobject[$field] = $data[$field];
598 $ref = substr($this->bom->ref, 1, 4);
599 if ($this->bom->status > 0 && $ref ==
'PROV') {
600 throw new RestException(400,
"Wrong naming scheme '(PROV%)' is only allowed on 'DRAFT' status. For automatic increment use 'auto' on the 'ref' field.");
603 if (strtolower($this->bom->ref) ==
'auto') {
604 if (empty($this->bom->id) && $this->bom->status == 0) {
605 $this->bom->ref =
'';
607 $this->bom->fetch_product();
608 $numref = $this->bom->getNextNumRef($this->bom->product);
609 $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.