25use Luracast\Restler\RestException;
27require_once DOL_DOCUMENT_ROOT.
'/fichinter/class/fichinter.class.php';
43 public static $FIELDS = array(
52 public static $FIELDSLINE = array(
70 $this->fichinter =
new Fichinter($this->db);
87 public function get(
$id, $ref =
'', $ref_ext =
'', $contact_list = 1)
89 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'lire')) {
90 throw new RestException(403);
93 $result = $this->fichinter->fetch(
$id, $ref, $ref_ext);
95 throw new RestException(404,
'Intervention not found');
99 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
102 if ($contact_list > -1) {
104 $tmparray = $this->fichinter->liste_contact(-1,
'external', $contact_list);
105 if (is_array($tmparray)) {
106 $this->fichinter->contacts_ids = $tmparray;
108 $tmparray = $this->fichinter->liste_contact(-1,
'internal', $contact_list);
109 if (is_array($tmparray)) {
110 $this->fichinter->contacts_ids_internal = $tmparray;
114 $this->fichinter->fetchObjectLinked();
141 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $properties =
'', $contact_type =
'', $pagination_data =
false)
143 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'lire')) {
144 throw new RestException(403);
150 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
154 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
155 $search_sale = DolibarrApiAccess::$user->id;
158 $sql =
"SELECT t.rowid";
159 $sql .=
" FROM ".MAIN_DB_PREFIX.
"fichinter AS t LEFT JOIN ".MAIN_DB_PREFIX.
"fichinter_extrafields AS ef ON (ef.fk_object = t.rowid)";
160 $sql .=
' WHERE t.entity IN ('.getEntity(
'intervention').
')';
162 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
165 if ($search_sale && $search_sale !=
'-1') {
166 if ($search_sale == -2) {
167 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
168 } elseif ($search_sale > 0) {
169 $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).
")";
177 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
182 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
184 $sql .= $this->db->order($sortfield, $sortorder);
189 $offset = $limit * $page;
191 $sql .= $this->db->plimit($limit + 1, $offset);
195 $result = $this->db->query($sql);
198 $num = $this->db->num_rows($result);
199 $min = min($num, ($limit <= 0 ? $num : $limit));
202 $obj = $this->db->fetch_object($result);
203 $fichinter_static =
new Fichinter($this->db);
204 if ($fichinter_static->fetch($obj->rowid)) {
206 $fichinter_static->contacts_ids = $fichinter_static->liste_contact(-1, $contact_type, 1);
213 throw new RestException(503,
'Error when retrieve intervention list : '.$this->db->lasterror());
217 if ($pagination_data) {
218 $totalsResult = $this->db->query($sqlTotals);
219 $total = $this->db->fetch_object($totalsResult)->total;
224 $obj_ret[
'data'] = $tmp;
225 $obj_ret[
'pagination'] = [
226 'total' => (int) $total,
228 'page_count' => ceil((
int) $total / $limit),
248 public function post($request_data =
null)
250 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'creer')) {
251 throw new RestException(403,
"Insuffisant rights");
254 $result = $this->
_validate($request_data);
255 foreach ($request_data as $field => $value) {
256 if ($field ===
'caller') {
258 $this->fichinter->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
262 $this->fichinter->$field = $this->
_checkValForAPI($field, $value, $this->fichinter);
265 if ($this->fichinter->create(DolibarrApiAccess::$user) < 0) {
266 throw new RestException(500,
"Error creating intervention", array_merge(array($this->fichinter->error), $this->fichinter->errors));
269 return $this->fichinter->id;
285 public function put(
$id, $request_data =
null)
287 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'creer')) {
288 throw new RestException(403);
291 $result = $this->fichinter->fetch(
$id);
293 throw new RestException(404,
'Fichinter not found');
297 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
299 foreach ($request_data as $field => $value) {
300 if ($field ==
'id') {
303 if ($field ===
'caller') {
305 $this->fichinter->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
308 if ($field ==
'array_options' && is_array($value)) {
309 foreach ($value as $index => $val) {
310 $this->fichinter->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->fichinter);
315 $this->fichinter->$field = $this->
_checkValForAPI($field, $value, $this->fichinter);
318 if ($this->fichinter->update(DolibarrApiAccess::$user) > 0) {
319 return $this->
get(
$id);
321 throw new RestException(500, $this->fichinter->error);
376 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'creer')) {
377 throw new RestException(403,
"Insuffisant rights");
382 foreach ($request_data as $field => $value) {
383 if ($field ===
'caller') {
385 $this->fichinter->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
389 $this->fichinter->$field = $this->
_checkValForAPI($field, $value, $this->fichinter);
393 throw new RestException(404,
'Intervention not found');
397 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
400 $updateRes = $this->fichinter->addLine(
401 DolibarrApiAccess::$user,
403 $this->fichinter->description,
404 $this->fichinter->date,
405 $this->fichinter->duration
408 if ($updateRes > 0) {
411 throw new RestException(400, $this->fichinter->error);
427 public function delete(
$id)
429 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'supprimer')) {
430 throw new RestException(403);
432 $result = $this->fichinter->fetch(
$id);
434 throw new RestException(404,
'Intervention not found');
438 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
441 if (!$this->fichinter->delete(DolibarrApiAccess::$user)) {
442 throw new RestException(500,
'Error when delete intervention : '.$this->fichinter->error);
448 'message' =>
'Intervention deleted'
468 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'creer')) {
469 throw new RestException(403,
"Insuffisant rights");
471 $result = $this->fichinter->fetch(
$id);
473 throw new RestException(404,
'Intervention not found');
477 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
479 $result = $this->fichinter->setDraft(DolibarrApiAccess::$user);
481 throw new RestException(304,
'Error nothing done. May be object is already set as draft');
484 throw new RestException(500,
'Error when closing Intervention: '.$this->fichinter->error);
486 $this->fichinter->fetchObjectLinked();
511 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'creer')) {
512 throw new RestException(403,
"Insuffisant rights");
514 $result = $this->fichinter->fetch(
$id);
516 throw new RestException(404,
'Intervention not found');
520 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
523 $result = $this->fichinter->setValid(DolibarrApiAccess::$user, $notrigger);
525 throw new RestException(304,
'Error nothing done. May be object is already validated');
528 throw new RestException(500,
'Error when validating Intervention: '.$this->fichinter->error);
531 $this->fichinter->fetchObjectLinked();
551 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'creer')) {
552 throw new RestException(403,
"Insuffisant rights");
554 $result = $this->fichinter->fetch(
$id);
556 throw new RestException(404,
'Intervention not found');
560 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
563 $result = $this->fichinter->setStatut(3);
566 throw new RestException(304,
'Error nothing done. May be object is already closed');
569 throw new RestException(500,
'Error when closing Intervention: '.$this->fichinter->error);
572 $this->fichinter->fetchObjectLinked();
587 $fichinter = array();
588 foreach (Interventions::$FIELDS as $field) {
589 if (!isset($data[$field])) {
590 throw new RestException(400,
"$field field missing");
592 $fichinter[$field] = $data[$field];
611 unset(
$object->labelStatusShort);
626 if ($data ===
null) {
629 $fichinter = array();
630 foreach (Interventions::$FIELDSLINE as $field) {
631 if (!isset($data[$field])) {
632 throw new RestException(400,
"$field field missing");
634 $fichinter[$field] = $data[$field];
$id
Support class for third parties, contacts, members, users or resources.
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.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='', $properties='', $contact_type='', $pagination_data=false)
List interventions.
_cleanObjectDatas($object)
Clean sensible object data.
validate($id, $notrigger=0)
Validate an intervention.
_validateLine($data)
Validate fields before create or update object.
put($id, $request_data=null)
Update intervention general fields (won't touch lines of fichinter)
post($request_data=null)
Create an intervention.
closeFichinter($id)
Close an intervention.
_validate($data)
Validate fields before create or update object.
__construct()
Constructor.
reopen($id)
Reopen an intervention.
postLine($id, $request_data=null)
Get lines of 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.