20 use Luracast\Restler\RestException;
22 require_once DOL_DOCUMENT_ROOT.
'/bom/class/bom.class.php';
51 $this->bom =
new BOM($this->
db);
65 public function get($id)
67 if (!DolibarrApiAccess::$user->rights->bom->read) {
68 throw new RestException(401);
71 $result = $this->bom->fetch($id);
73 throw new RestException(404,
'BOM not found');
77 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
98 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'')
102 if (!DolibarrApiAccess::$user->rights->bom->read) {
103 throw new RestException(401);
107 $tmpobject =
new BOM($this->
db);
109 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
111 $restrictonsocid = 0;
115 if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
116 $search_sale = DolibarrApiAccess::$user->id;
119 $sql =
"SELECT t.rowid";
120 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
121 $sql .=
", sc.fk_soc, sc.fk_user";
123 $sql .=
" FROM ".MAIN_DB_PREFIX.$tmpobject->table_element.
" as t";
125 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
126 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
128 $sql .=
" WHERE 1 = 1";
134 if ($tmpobject->ismultientitymanaged) {
135 $sql .=
' AND t.entity IN ('.getEntity($tmpobject->element).
')';
137 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
138 $sql .=
" AND t.fk_soc = sc.fk_soc";
140 if ($restrictonsocid && $socid) {
141 $sql .=
" AND t.fk_soc = ".((int) $socid);
143 if ($restrictonsocid && $search_sale > 0) {
144 $sql .=
" AND t.rowid = sc.fk_soc";
147 if ($restrictonsocid && $search_sale > 0) {
148 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
153 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
155 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
156 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
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;
207 if (!$this->bom->create(DolibarrApiAccess::$user)) {
208 throw new RestException(500,
"Error creating BOM", array_merge(array($this->bom->error), $this->bom->errors));
210 return $this->bom->id;
221 public function put($id, $request_data =
null)
223 if (!DolibarrApiAccess::$user->rights->bom->write) {
224 throw new RestException(401);
227 $result = $this->bom->fetch($id);
229 throw new RestException(404,
'BOM not found');
233 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
236 foreach ($request_data as $field => $value) {
237 if ($field ==
'id') {
240 $this->bom->$field = $value;
243 if ($this->bom->update(DolibarrApiAccess::$user) > 0) {
244 return $this->
get($id);
246 throw new RestException(500, $this->bom->error);
256 public function delete($id)
258 if (!DolibarrApiAccess::$user->rights->bom->delete) {
259 throw new RestException(401);
261 $result = $this->bom->fetch($id);
263 throw new RestException(404,
'BOM not found');
267 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
270 if (!$this->bom->delete(DolibarrApiAccess::$user)) {
271 throw new RestException(500,
'Error when deleting BOM : '.$this->bom->error);
277 'message' =>
'BOM deleted'
293 $object = parent::_cleanObjectDatas($object);
295 unset($object->rowid);
296 unset($object->canvas);
298 unset($object->name);
299 unset($object->lastname);
300 unset($object->firstname);
301 unset($object->civility_id);
302 unset($object->statut);
303 unset($object->state);
304 unset($object->state_id);
305 unset($object->state_code);
306 unset($object->region);
307 unset($object->region_code);
308 unset($object->country);
309 unset($object->country_id);
310 unset($object->country_code);
311 unset($object->barcode_type);
312 unset($object->barcode_type_code);
313 unset($object->barcode_type_label);
314 unset($object->barcode_type_coder);
315 unset($object->total_ht);
316 unset($object->total_tva);
317 unset($object->total_localtax1);
318 unset($object->total_localtax2);
319 unset($object->total_ttc);
320 unset($object->fk_account);
321 unset($object->comments);
322 unset($object->note);
323 unset($object->mode_reglement_id);
324 unset($object->cond_reglement_id);
325 unset($object->cond_reglement);
326 unset($object->shipping_method_id);
327 unset($object->fk_incoterms);
328 unset($object->label_incoterms);
329 unset($object->location_incoterms);
332 if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
333 $nboflines = count($object->lines);
334 for ($i = 0; $i < $nboflines; $i++) {
337 unset($object->lines[$i]->lines);
338 unset($object->lines[$i]->note);
356 foreach ($this->bom->fields as $field => $propfield) {
357 if (in_array($field, array(
'rowid',
'entity',
'date_creation',
'tms',
'fk_user_creat')) || $propfield[
'notnull'] != 1) {
360 if (!isset($data[$field])) {
361 throw new RestException(400,
"$field field missing");
363 $myobject[$field] = $data[$field];