19 use Luracast\Restler\RestException;
21 require_once DOL_DOCUMENT_ROOT.
'/mrp/class/mo.class.php';
50 $this->mo =
new Mo($this->
db);
64 public function get($id)
66 if (!DolibarrApiAccess::$user->rights->mrp->read) {
67 throw new RestException(401);
70 $result = $this->mo->fetch($id);
72 throw new RestException(404,
'MO not found');
76 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
97 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'')
102 $tmpobject =
new Mo($this->
db);
104 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
106 $restrictonsocid = 0;
110 if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
111 $search_sale = DolibarrApiAccess::$user->id;
114 $sql =
"SELECT t.rowid";
115 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
116 $sql .=
", sc.fk_soc, sc.fk_user";
118 $sql .=
" FROM ".MAIN_DB_PREFIX.$tmpobject->table_element.
" as t";
120 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
121 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
123 $sql .=
" WHERE 1 = 1";
129 if ($tmpobject->ismultientitymanaged) {
130 $sql .=
' AND t.entity IN ('.getEntity($tmpobject->element).
')';
132 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
133 $sql .=
" AND t.fk_soc = sc.fk_soc";
135 if ($restrictonsocid && $socid) {
136 $sql .=
" AND t.fk_soc = ".$socid;
138 if ($restrictonsocid && $search_sale > 0) {
139 $sql .=
" AND t.rowid = sc.fk_soc";
142 if ($restrictonsocid && $search_sale > 0) {
143 $sql .=
" AND sc.fk_user = ".$search_sale;
147 throw new RestException(503,
'Error when validating parameter sqlfilters '.$sqlfilters);
149 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
150 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
153 $sql .= $this->
db->order($sortfield, $sortorder);
158 $offset = $limit * $page;
160 $sql .= $this->
db->plimit($limit + 1, $offset);
163 $result = $this->
db->query($sql);
165 $num = $this->
db->num_rows($result);
168 $obj = $this->
db->fetch_object($result);
169 $tmp_object =
new Mo($this->
db);
170 if ($tmp_object->fetch($obj->rowid)) {
176 throw new RestException(503,
'Error when retrieve MO list');
178 if (!count($obj_ret)) {
179 throw new RestException(404,
'No MO found');
190 public function post($request_data = null)
192 if (!DolibarrApiAccess::$user->rights->mrp->write) {
193 throw new RestException(401);
196 $result = $this->
_validate($request_data);
198 foreach ($request_data as $field => $value) {
199 $this->mo->$field = $value;
201 if (!$this->mo->create(DolibarrApiAccess::$user)) {
202 throw new RestException(500,
"Error creating MO", array_merge(array($this->mo->error), $this->mo->errors));
204 return $this->mo->id;
215 public function put($id, $request_data = null)
217 if (!DolibarrApiAccess::$user->rights->mrp->write) {
218 throw new RestException(401);
221 $result = $this->mo->fetch($id);
223 throw new RestException(404,
'MO not found');
227 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
230 foreach ($request_data as $field => $value) {
231 if ($field ==
'id') {
234 $this->mo->$field = $value;
237 if ($this->mo->update(DolibarrApiAccess::$user) > 0) {
238 return $this->
get($id);
240 throw new RestException(500, $this->mo->error);
250 public function delete($id)
252 if (!DolibarrApiAccess::$user->rights->mrp->delete) {
253 throw new RestException(401);
255 $result = $this->mo->fetch($id);
257 throw new RestException(404,
'MO not found');
261 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
264 if (!$this->mo->delete(DolibarrApiAccess::$user)) {
265 throw new RestException(500,
'Error when deleting MO : '.$this->mo->error);
271 'message' =>
'MO deleted'
287 $object = parent::_cleanObjectDatas($object);
289 unset($object->rowid);
290 unset($object->canvas);
292 unset($object->name);
293 unset($object->lastname);
294 unset($object->firstname);
295 unset($object->civility_id);
296 unset($object->statut);
297 unset($object->state);
298 unset($object->state_id);
299 unset($object->state_code);
300 unset($object->region);
301 unset($object->region_code);
302 unset($object->country);
303 unset($object->country_id);
304 unset($object->country_code);
305 unset($object->barcode_type);
306 unset($object->barcode_type_code);
307 unset($object->barcode_type_label);
308 unset($object->barcode_type_coder);
309 unset($object->total_ht);
310 unset($object->total_tva);
311 unset($object->total_localtax1);
312 unset($object->total_localtax2);
313 unset($object->total_ttc);
314 unset($object->fk_account);
315 unset($object->comments);
316 unset($object->note);
317 unset($object->mode_reglement_id);
318 unset($object->cond_reglement_id);
319 unset($object->cond_reglement);
320 unset($object->shipping_method_id);
321 unset($object->fk_incoterms);
322 unset($object->label_incoterms);
323 unset($object->location_incoterms);
326 if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
327 $nboflines = count($object->lines);
328 for ($i = 0; $i < $nboflines; $i++) {
331 unset($object->lines[$i]->lines);
332 unset($object->lines[$i]->note);
350 foreach ($this->mo->fields as $field => $propfield) {
351 if (in_array($field, array(
'rowid',
'entity',
'date_creation',
'tms',
'fk_user_creat')) || $propfield[
'notnull'] != 1) {
354 if (!isset($data[$field])) {
355 throw new RestException(400,
"$field field missing");
357 $myobject[$field] = $data[$field];
put($id, $request_data=null)
Update MO.
$conf db
API class for accounts.
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
index($sortfield="t.rowid", $sortorder= 'ASC', $limit=100, $page=0, $sqlfilters= '')
List Mos.
__construct()
Constructor.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid')
Check user access to a resource.
post($request_data=null)
Create MO object.
_validate($data)
Validate fields before create or update object.
_cleanObjectDatas($object)
Clean sensible object datas.