27use Luracast\Restler\RestException;
29require_once DOL_DOCUMENT_ROOT.
'/fichinter/class/fichinter.class.php';
30include_once DOL_DOCUMENT_ROOT.
"/fichinter/class/fichinterligne.class.php";
47 public static $FIELDS = array(
56 public static $FIELDSLINE = array(
74 $this->fichinter =
new Fichinter($this->db);
91 public function get(
$id, $ref =
'', $ref_ext =
'', $contact_list = 1)
93 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'lire')) {
94 throw new RestException(403);
97 $result = $this->fichinter->fetch(
$id, $ref, $ref_ext);
99 throw new RestException(404,
'Intervention not found');
103 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
106 if ($contact_list > -1) {
108 $tmparray = $this->fichinter->liste_contact(-1,
'external', $contact_list);
109 if (is_array($tmparray)) {
110 $this->fichinter->contacts_ids = $tmparray;
112 $tmparray = $this->fichinter->liste_contact(-1,
'internal', $contact_list);
113 if (is_array($tmparray)) {
114 $this->fichinter->contacts_ids_internal = $tmparray;
118 $this->fichinter->fetchObjectLinked();
146 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $properties =
'', $contact_type =
'', $pagination_data =
false, $loadlinkedobjects = 0)
148 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'lire')) {
149 throw new RestException(403);
157 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
161 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
162 $search_sale = DolibarrApiAccess::$user->id;
165 $sql =
"SELECT t.rowid";
166 $sql .=
" FROM ".MAIN_DB_PREFIX.
"fichinter AS t";
167 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"societe AS s ON (s.rowid = t.fk_soc)";
168 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"fichinter_extrafields AS ef ON (ef.fk_object = t.rowid)";
169 $sql .=
' WHERE t.entity IN ('.getEntity(
'intervention').
')';
171 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
174 if ($search_sale && $search_sale !=
'-1') {
175 if ($search_sale == -2) {
176 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
177 } elseif ($search_sale > 0) {
178 $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).
")";
183 $parameters = array(
'sqlfilters' => $sqlfilters,
'apiroute' =>
'interventions',
'apimethod' => __METHOD__);
185 $reshook = $hookmanager->executeHooks(
'printFieldListWhere', $parameters, $this->fichinter, $action);
187 $sql = $hookmanager->resPrint;
188 } elseif ($reshook == 0) {
189 $sql .= $hookmanager->resPrint;
193 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
195 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
200 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
202 $sql .= $this->db->order($sortfield, $sortorder);
207 $offset = $limit * $page;
209 $sql .= $this->db->plimit($limit + 1, $offset);
213 $result = $this->db->query($sql);
216 $num = $this->db->num_rows($result);
217 $min = min($num, ($limit <= 0 ? $num : $limit));
220 $obj = $this->db->fetch_object($result);
221 $fichinter_static =
new Fichinter($this->db);
222 if ($fichinter_static->fetch($obj->rowid)) {
224 $fichinter_static->contacts_ids = $fichinter_static->liste_contact(-1, $contact_type, 1);
227 if ($loadlinkedobjects) {
229 $fichinter_static->fetchObjectLinked();
237 throw new RestException(503,
'Error when retrieve intervention list : '.$this->db->lasterror());
241 if ($pagination_data) {
242 $totalsResult = $this->db->query($sqlTotals);
243 $total = $this->db->fetch_object($totalsResult)->total;
248 $obj_ret[
'data'] = $tmp;
249 $obj_ret[
'pagination'] = [
250 'total' => (int) $total,
252 'page_count' => ceil((
int) $total / $limit),
272 public function post($request_data =
null)
274 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'creer')) {
275 throw new RestException(403,
"Insufficiant rights");
278 $result = $this->
_validate($request_data);
279 foreach ($request_data as $field => $value) {
280 if ($field ===
'caller') {
282 $this->fichinter->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
286 $this->fichinter->$field = $this->
_checkValForAPI($field, $value, $this->fichinter);
289 if ($this->fichinter->create(DolibarrApiAccess::$user) < 0) {
290 throw new RestException(500,
"Error creating intervention", array_merge(array($this->fichinter->error), $this->fichinter->errors));
293 return $this->fichinter->id;
309 public function put(
$id, $request_data =
null)
311 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'creer')) {
312 throw new RestException(403);
315 $result = $this->fichinter->fetch(
$id);
317 throw new RestException(404,
'Fichinter not found');
321 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
323 foreach ($request_data as $field => $value) {
324 if ($field ==
'id') {
327 if ($field ===
'caller') {
329 $this->fichinter->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
332 if ($field ==
'array_options' && is_array($value)) {
333 foreach ($value as $index => $val) {
339 $this->fichinter->$field = $this->
_checkValForAPI($field, $value, $this->fichinter);
342 if ($this->fichinter->update(DolibarrApiAccess::$user) > 0) {
343 return $this->
get(
$id);
345 throw new RestException(500, $this->fichinter->error);
400 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'creer')) {
401 throw new RestException(403,
"Insufficiant rights");
406 foreach ($request_data as $field => $value) {
407 if ($field ===
'caller') {
409 $this->fichinter->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
413 $this->fichinter->$field = $this->
_checkValForAPI($field, $value, $this->fichinter);
417 throw new RestException(404,
'Intervention not found');
421 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
424 $updateRes = $this->fichinter->addLine(
425 DolibarrApiAccess::$user,
427 $this->fichinter->description,
428 $this->fichinter->date,
429 $this->fichinter->duration
432 if ($updateRes > 0) {
435 throw new RestException(400, $this->fichinter->error);
451 public function delete(
$id)
453 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'supprimer')) {
454 throw new RestException(403);
456 $result = $this->fichinter->fetch(
$id);
458 throw new RestException(404,
'Intervention not found');
462 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
465 if (!$this->fichinter->delete(DolibarrApiAccess::$user)) {
466 throw new RestException(500,
'Error when delete intervention : '.$this->fichinter->error);
472 'message' =>
'Intervention deleted'
498 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'creer')) {
499 throw new RestException(403,
"Insufficiant rights");
501 $result = $this->fichinter->fetch(
$id);
503 throw new RestException(404,
'Intervention not found');
507 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
510 $result = $this->fichinter->setValid(DolibarrApiAccess::$user, $notrigger);
512 throw new RestException(304,
'Error nothing done. May be object is already validated');
515 throw new RestException(500,
'Error when validating Intervention: '.$this->fichinter->error);
518 $this->fichinter->fetchObjectLinked();
546 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'creer')) {
547 throw new RestException(403,
"Insufficiant rights");
549 $result = $this->fichinter->fetch(
$id);
551 throw new RestException(404,
'Intervention not found');
555 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
558 $result = $this->fichinter->setClose(DolibarrApiAccess::$user, $notrigger);
560 throw new RestException(304,
'Error nothing done. May be object is already closed');
563 throw new RestException(500,
'Error when closing Intervention: '.$this->fichinter->error);
566 $this->fichinter->fetchObjectLinked();
589 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'creer')) {
590 throw new RestException(403);
593 $result = $this->fichinter->fetch(
$id);
595 throw new RestException(404,
'Intervention not found');
598 if ($this->fichinter->status != 0) {
599 throw new RestException(403,
'Intervention not in draft status : '.$this->fichinter->getLibStatut(1));
603 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
607 if ($objectline->fetch($lineid) <= 0) {
608 throw new RestException(404,
'Intervention line not found');
611 $updateRes = $objectline->deleteLine(DolibarrApiAccess::$user);
613 if ($updateRes >= 0) {
616 throw new RestException(405, $this->fichinter->error);
638 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'creer')) {
639 throw new RestException(403,
"Insufficiant rights");
641 $result = $this->fichinter->fetch(
$id);
643 throw new RestException(404,
'Intervention not found');
647 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
650 $result = $this->fichinter->setDraft(DolibarrApiAccess::$user);
652 throw new RestException(304,
'Nothing done. . May be object is already set as draft.');
655 throw new RestException(500,
'Error when closing intervention: '.$this->fichinter->error);
658 $this->fichinter->fetchObjectLinked();
680 public function addContact(
$id, $fk_socpeople, $type_contact, $source, $notrigger = 0)
682 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'creer')) {
683 throw new RestException(403);
685 $result = $this->fichinter->fetch(
$id);
687 throw new RestException(404,
'Interventional not found');
691 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
694 $result = $this->fichinter->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
696 throw new RestException(500,
'Error : '.$this->fichinter->error);
699 $result = $this->fichinter->fetch(
$id);
701 throw new RestException(404,
'Interventional not found');
723 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'lire')) {
724 throw new RestException(403);
727 $result = $this->fichinter->fetch(
$id);
729 throw new RestException(404,
'Interventional not found');
733 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
736 if (empty($source) || $source ==
'external') {
737 $contacts = $this->fichinter->liste_contact(-1,
'external', 0, $type);
741 if (empty($source) || $source ==
'internal') {
742 $socpeoples = $this->fichinter->liste_contact(-1,
'internal', 0, $type);
744 $socpeoples = array();
747 $contacts = array_merge($contacts, $socpeoples);
769 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'creer')) {
770 throw new RestException(403);
773 $result = $this->fichinter->fetch(
$id);
776 throw new RestException(404,
'Interventional not found');
780 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
782 foreach (array(
'internal',
'external') as $source) {
783 $contacts = $this->fichinter->liste_contact(-1, $source);
785 foreach ($contacts as $contact) {
786 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
787 $result = $this->fichinter->delete_contact($contact[
'rowid']);
789 throw new RestException(500,
'Error when deleted the contact');
815 $result = $this->fichinter->fetch(
$id);
817 throw new RestException(404,
'Intervention not found');
820 if ($this->fichinter->status != 0) {
821 throw new RestException(403,
'Intervention not in draft status : '.$this->fichinter->getLibStatut(1));
825 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
829 if ($objectline->fetch($lineid) <= 0) {
830 throw new RestException(404,
'Intervention line not found');
832 $request_data = (object) $request_data;
834 if (isset($request_data->desc) || isset($request_data->description)) {
835 $objectline->desc =
sanitizeVal($request_data->desc ?? $request_data->description,
'restricthtml');
837 if (isset($request_data->date)) {
838 $objectline->date = (int)
sanitizeVal($request_data->date);
840 if (isset($request_data->duration)) {
841 $objectline->duration = (int)
sanitizeVal($request_data->duration);
843 if (isset($request_data->rang)) {
844 $objectline->rang = (int)
sanitizeVal($request_data->rang);
847 $updateRes = $objectline->update(DolibarrApiAccess::$user);
849 if ($updateRes >= 0) {
850 $result = $this->fichinter->fetch(
$id);
854 throw new RestException(500, $this->fichinter->error);
857 throw new RestException(500, $objectline->error);
872 $fichinter = array();
873 foreach (Interventions::$FIELDS as $field) {
874 if (!isset($data[$field])) {
875 throw new RestException(400,
"$field field missing");
877 $fichinter[$field] = $data[$field];
899 unset(
$object->labelStatusShort);
914 if ($data ===
null) {
917 $fichinter = array();
918 foreach (Interventions::$FIELDSLINE as $field) {
919 if (!isset($data[$field])) {
920 throw new RestException(400,
"$field field missing");
922 $fichinter[$field] = $data[$field];
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
_checkValExtrafieldsForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_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 intervention lines.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='', $properties='', $contact_type='', $pagination_data=false, $loadlinkedobjects=0)
List interventions.
addContact($id, $fk_socpeople, $type_contact, $source, $notrigger=0)
Adds a contact to an interventional.
_cleanObjectDatas($object)
Clean sensible object data @phpstan-template T.
validate($id, $notrigger=0)
Validate an intervention.
_validateLine($data)
Validate fields before create or update object.
getContacts($id, $type='', $source='')
Get contacts of given interventional.
put($id, $request_data=null)
Update intervention general fields (won't touch lines of fichinter)
post($request_data=null)
Create an intervention.
_validate($data)
Validate fields before create or update object.
__construct()
Constructor.
close($id, $notrigger=0)
Close an intervention.
putLine($id, $lineid, $request_data)
Update a line of an intervention.
deleteLine($id, $lineid)
Delete the line of the intervention.
deleteContact($id, $contactid, $type)
Delete a contact type of given interventional.
settodraft($id)
Sets an intervention as draft.
postLine($id, $request_data=null)
Get lines of intervention.
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.