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->hasRight(
'ficheinter',
'lire')) {
82 throw new RestException(403);
85 $result = $this->fichinter->fetch($id);
87 throw new RestException(404,
'Intervention not found');
91 throw new RestException(403,
'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 =
'')
115 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'lire')) {
116 throw new RestException(403);
122 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
126 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
127 $search_sale = DolibarrApiAccess::$user->id;
130 $sql =
"SELECT t.rowid";
131 $sql .=
" FROM ".MAIN_DB_PREFIX.
"fichinter AS t LEFT JOIN ".MAIN_DB_PREFIX.
"fichinter_extrafields AS ef ON (ef.fk_object = t.rowid)";
132 $sql .=
' WHERE t.entity IN ('.getEntity(
'intervention').
')';
134 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
137 if ($search_sale && $search_sale !=
'-1') {
138 if ($search_sale == -2) {
139 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
140 } elseif ($search_sale > 0) {
141 $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).
")";
149 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
153 $sql .= $this->db->order($sortfield, $sortorder);
158 $offset = $limit * $page;
160 $sql .= $this->db->plimit($limit + 1, $offset);
164 $result = $this->db->query($sql);
167 $num = $this->db->num_rows($result);
168 $min = min($num, ($limit <= 0 ? $num : $limit));
171 $obj = $this->db->fetch_object($result);
172 $fichinter_static =
new Fichinter($this->db);
173 if ($fichinter_static->fetch($obj->rowid)) {
179 throw new RestException(503,
'Error when retrieve intervention list : '.$this->db->lasterror());
191 public function post($request_data =
null)
193 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'creer')) {
194 throw new RestException(403,
"Insuffisant rights");
197 $result = $this->
_validate($request_data);
198 foreach ($request_data as $field => $value) {
199 if ($field ===
'caller') {
201 $this->fichinter->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
205 $this->fichinter->$field = $this->
_checkValForAPI($field, $value, $this->fichinter);
208 if ($this->fichinter->create(DolibarrApiAccess::$user) < 0) {
209 throw new RestException(500,
"Error creating intervention", array_merge(array($this->fichinter->error), $this->fichinter->errors));
212 return $this->fichinter->id;
259 public function postLine($id, $request_data =
null)
261 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'creer')) {
262 throw new RestException(403,
"Insuffisant rights");
267 foreach ($request_data as $field => $value) {
268 if ($field ===
'caller') {
270 $this->fichinter->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
274 $this->fichinter->$field = $this->
_checkValForAPI($field, $value, $this->fichinter);
278 throw new RestException(404,
'Intervention not found');
282 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
285 $updateRes = $this->fichinter->addLine(
286 DolibarrApiAccess::$user,
288 $this->fichinter->description,
289 $this->fichinter->date,
290 $this->fichinter->duree
293 if ($updateRes > 0) {
296 throw new RestException(400, $this->fichinter->error);
306 public function delete($id)
308 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'supprimer')) {
309 throw new RestException(403);
311 $result = $this->fichinter->fetch($id);
313 throw new RestException(404,
'Intervention not found');
317 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
320 if (!$this->fichinter->delete(DolibarrApiAccess::$user)) {
321 throw new RestException(500,
'Error when delete intervention : '.$this->fichinter->error);
327 'message' =>
'Intervention deleted'
349 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'creer')) {
350 throw new RestException(403,
"Insuffisant rights");
352 $result = $this->fichinter->fetch($id);
354 throw new RestException(404,
'Intervention not found');
358 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
361 $result = $this->fichinter->setValid(DolibarrApiAccess::$user, $notrigger);
363 throw new RestException(304,
'Error nothing done. May be object is already validated');
366 throw new RestException(500,
'Error when validating Intervention: '.$this->fichinter->error);
369 $this->fichinter->fetchObjectLinked();
385 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'creer')) {
386 throw new RestException(403,
"Insuffisant rights");
388 $result = $this->fichinter->fetch($id);
390 throw new RestException(404,
'Intervention not found');
394 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
397 $result = $this->fichinter->setStatut(3);
400 throw new RestException(304,
'Error nothing done. May be object is already closed');
403 throw new RestException(500,
'Error when closing Intervention: '.$this->fichinter->error);
406 $this->fichinter->fetchObjectLinked();
421 $fichinter = array();
422 foreach (Interventions::$FIELDS as $field) {
423 if (!isset($data[$field])) {
424 throw new RestException(400,
"$field field missing");
426 $fichinter[$field] = $data[$field];
445 unset(
$object->labelStatusShort);
460 $fichinter = array();
461 foreach (Interventions::$FIELDSLINE as $field) {
462 if (!isset($data[$field])) {
463 throw new RestException(400,
"$field field missing");
465 $fichinter[$field] = $data[$field];
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $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.
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
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.