24use Luracast\Restler\RestException;
26require_once DOL_DOCUMENT_ROOT.
'/fichinter/class/fichinter.class.php';
40 public static $FIELDS = array(
49 public static $FIELDSLINE = array(
67 $this->fichinter =
new Fichinter($this->db);
79 public function get($id)
81 if (!DolibarrApiAccess::$user->rights->ficheinter->lire) {
82 throw new RestException(401);
85 $result = $this->fichinter->fetch($id);
87 throw new RestException(404,
'Intervention not found');
91 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
94 $this->fichinter->fetchObjectLinked();
113 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $properties =
'')
117 if (!DolibarrApiAccess::$user->rights->ficheinter->lire) {
118 throw new RestException(401);
124 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
128 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
129 $search_sale = DolibarrApiAccess::$user->id;
132 $sql =
"SELECT t.rowid";
133 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
134 $sql .=
", sc.fk_soc, sc.fk_user";
136 $sql .=
" FROM ".MAIN_DB_PREFIX.
"fichinter AS t LEFT JOIN ".MAIN_DB_PREFIX.
"fichinter_extrafields AS ef ON (ef.fk_object = t.rowid)";
138 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
139 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
142 $sql .=
' WHERE t.entity IN ('.getEntity(
'intervention').
')';
143 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
144 $sql .=
" AND t.fk_soc = sc.fk_soc";
147 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
149 if ($search_sale > 0) {
150 $sql .=
" AND t.rowid = sc.fk_soc";
153 if ($search_sale > 0) {
154 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
161 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
165 $sql .= $this->db->order($sortfield, $sortorder);
170 $offset = $limit * $page;
172 $sql .= $this->db->plimit($limit + 1, $offset);
176 $result = $this->db->query($sql);
179 $num = $this->db->num_rows($result);
180 $min = min($num, ($limit <= 0 ? $num : $limit));
183 $obj = $this->db->fetch_object($result);
184 $fichinter_static =
new Fichinter($this->db);
185 if ($fichinter_static->fetch($obj->rowid)) {
191 throw new RestException(503,
'Error when retrieve intervention list : '.$this->db->lasterror());
203 public function post($request_data =
null)
205 if (!DolibarrApiAccess::$user->rights->ficheinter->creer) {
206 throw new RestException(401,
"Insuffisant rights");
209 $result = $this->
_validate($request_data);
210 foreach ($request_data as $field => $value) {
211 if ($field ===
'caller') {
213 $this->fichinter->context[
'caller'] = $request_data[
'caller'];
217 $this->fichinter->$field = $value;
220 if ($this->fichinter->create(DolibarrApiAccess::$user) < 0) {
221 throw new RestException(500,
"Error creating intervention", array_merge(array($this->fichinter->error), $this->fichinter->errors));
224 return $this->fichinter->id;
271 public function postLine($id, $request_data =
null)
273 if (!DolibarrApiAccess::$user->rights->ficheinter->creer) {
274 throw new RestException(401,
"Insuffisant rights");
279 foreach ($request_data as $field => $value) {
280 if ($field ===
'caller') {
282 $this->fichinter->context[
'caller'] = $request_data[
'caller'];
286 $this->fichinter->$field = $value;
290 throw new RestException(404,
'Intervention not found');
294 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
297 $updateRes = $this->fichinter->addLine(
298 DolibarrApiAccess::$user,
300 $this->fichinter->description,
301 $this->fichinter->date,
302 $this->fichinter->duree
305 if ($updateRes > 0) {
308 throw new RestException(400, $this->fichinter->error);
318 public function delete($id)
320 if (!DolibarrApiAccess::$user->rights->ficheinter->supprimer) {
321 throw new RestException(401);
323 $result = $this->fichinter->fetch($id);
325 throw new RestException(404,
'Intervention not found');
329 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
332 if (!$this->fichinter->delete(DolibarrApiAccess::$user)) {
333 throw new RestException(500,
'Error when delete intervention : '.$this->fichinter->error);
339 'message' =>
'Intervention deleted'
361 if (!DolibarrApiAccess::$user->rights->ficheinter->creer) {
362 throw new RestException(401,
"Insuffisant rights");
364 $result = $this->fichinter->fetch($id);
366 throw new RestException(404,
'Intervention not found');
370 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
373 $result = $this->fichinter->setValid(DolibarrApiAccess::$user, $notrigger);
375 throw new RestException(304,
'Error nothing done. May be object is already validated');
378 throw new RestException(500,
'Error when validating Intervention: '.$this->fichinter->error);
381 $this->fichinter->fetchObjectLinked();
397 if (!DolibarrApiAccess::$user->rights->ficheinter->creer) {
398 throw new RestException(401,
"Insuffisant rights");
400 $result = $this->fichinter->fetch($id);
402 throw new RestException(404,
'Intervention not found');
406 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
409 $result = $this->fichinter->setStatut(3);
412 throw new RestException(304,
'Error nothing done. May be object is already closed');
415 throw new RestException(500,
'Error when closing Intervention: '.$this->fichinter->error);
418 $this->fichinter->fetchObjectLinked();
433 $fichinter = array();
434 foreach (Interventions::$FIELDS as $field) {
435 if (!isset($data[$field])) {
436 throw new RestException(400,
"$field field missing");
438 $fichinter[$field] = $data[$field];
454 $object = parent::_cleanObjectDatas($object);
456 unset($object->labelStatus);
457 unset($object->labelStatusShort);
472 $fichinter = array();
473 foreach (Interventions::$FIELDSLINE as $field) {
474 if (!isset($data[$field])) {
475 throw new RestException(400,
"$field field missing");
477 $fichinter[$field] = $data[$field];
_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.
Class to manage interventions.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='', $properties='')
List of interventions Return a list of interventions.
_cleanObjectDatas($object)
Clean sensible object datas.
validate($id, $notrigger=0)
Validate an intervention.
_validateLine($data)
Validate fields before create or update object.
post($request_data=null)
Create intervention object.
closeFichinter($id)
Close an intervention.
_validate($data)
Validate fields before create or update object.
__construct()
Constructor.
postLine($id, $request_data=null)
Get lines of an intervention.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.