27use Luracast\Restler\RestException;
29require_once DOL_DOCUMENT_ROOT.
'/fichinter/class/fichinter.class.php';
30include_once DOL_DOCUMENT_ROOT.
"/fichinter/class/fichinterligne.class.php";
31require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
48 public static $FIELDS = array(
57 public static $FIELDSLINE = array(
75 $this->fichinter =
new Fichinter($this->db);
92 public function get(
$id, $ref =
'', $ref_ext =
'', $contact_list = 1)
94 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'lire')) {
95 throw new RestException(403);
98 $result = $this->fichinter->fetch(
$id, $ref, $ref_ext);
100 throw new RestException(404,
'Intervention not found');
104 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
107 if ($contact_list > -1) {
109 $tmparray = $this->fichinter->liste_contact(-1,
'external', $contact_list);
110 if (is_array($tmparray)) {
111 $this->fichinter->contacts_ids = $tmparray;
113 $tmparray = $this->fichinter->liste_contact(-1,
'internal', $contact_list);
114 if (is_array($tmparray)) {
115 $this->fichinter->contacts_ids_internal = $tmparray;
119 $this->fichinter->fetchObjectLinked();
147 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $properties =
'', $contact_type =
'', $pagination_data =
false, $loadlinkedobjects = 0)
149 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'lire')) {
150 throw new RestException(403);
158 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
162 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
163 $search_sale = DolibarrApiAccess::$user->id;
166 $sql =
"SELECT t.rowid";
167 $sql .=
" FROM ".MAIN_DB_PREFIX.
"fichinter AS t";
168 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"societe AS s ON (s.rowid = t.fk_soc)";
169 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"fichinter_extrafields AS ef ON (ef.fk_object = t.rowid)";
170 $sql .=
' WHERE t.entity IN ('.getEntity(
'intervention').
')';
172 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
175 if ($search_sale && $search_sale !=
'-1') {
176 if ($search_sale == -2) {
177 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', 0, 1);
178 } elseif ($search_sale > 0) {
179 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', (
int) $search_sale);
184 $parameters = array(
'sqlfilters' => $sqlfilters,
'apiroute' =>
'interventions',
'apimethod' => __METHOD__);
186 $reshook = $hookmanager->executeHooks(
'printFieldListWhere', $parameters, $this->fichinter, $action);
188 $sql = $hookmanager->resPrint;
189 } elseif ($reshook == 0) {
190 $sql .= $hookmanager->resPrint;
196 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
201 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
203 $sql .= $this->db->order($sortfield, $sortorder);
208 $offset = $limit * $page;
210 $sql .= $this->db->plimit($limit + 1, $offset);
214 $result = $this->db->query($sql);
217 $num = $this->db->num_rows($result);
218 $min = min($num, ($limit <= 0 ? $num : $limit));
221 $obj = $this->db->fetch_object($result);
222 $fichinter_static =
new Fichinter($this->db);
223 if ($fichinter_static->fetch($obj->rowid)) {
225 $fichinter_static->contacts_ids = $fichinter_static->liste_contact(-1, $contact_type, 1);
228 if ($loadlinkedobjects) {
230 $fichinter_static->fetchObjectLinked();
238 throw new RestException(503,
'Error when retrieve intervention list : '.$this->db->lasterror());
242 if ($pagination_data) {
243 $totalsResult = $this->db->query($sqlTotals);
244 $total = $this->db->fetch_object($totalsResult)->total;
249 $obj_ret[
'data'] = $tmp;
250 $obj_ret[
'pagination'] = [
251 'total' => (int) $total,
253 'page_count' => ceil((
int) $total / $limit),
273 public function post($request_data =
null)
275 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'creer')) {
276 throw new RestException(403,
"Insufficiant rights");
279 $result = $this->
_validate($request_data);
280 foreach ($request_data as $field => $value) {
281 if ($field ===
'caller') {
283 $this->fichinter->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
287 $this->fichinter->$field = $this->
_checkValForAPI($field, $value, $this->fichinter);
290 if ($this->fichinter->create(DolibarrApiAccess::$user) < 0) {
291 throw new RestException(500,
"Error creating intervention", array_merge(array($this->fichinter->error), $this->fichinter->errors));
294 return $this->fichinter->id;
310 public function put(
$id, $request_data =
null)
312 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'creer')) {
313 throw new RestException(403);
316 $result = $this->fichinter->fetch(
$id);
318 throw new RestException(404,
'Fichinter not found');
322 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
324 foreach ($request_data as $field => $value) {
325 if ($field ==
'id') {
328 if ($field ===
'caller') {
330 $this->fichinter->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
333 if ($field ==
'array_options' && is_array($value)) {
334 foreach ($value as $index => $val) {
340 $this->fichinter->$field = $this->
_checkValForAPI($field, $value, $this->fichinter);
343 if ($this->fichinter->update(DolibarrApiAccess::$user) > 0) {
344 return $this->
get(
$id);
346 throw new RestException(500, $this->fichinter->errorsToString());
366 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'lire')) {
367 throw new RestException(403);
370 $result = $this->fichinter->fetch(
$id);
372 throw new RestException(404,
'Intervention not found');
376 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
379 $this->fichinter->fetch_lines();
381 foreach ($this->fichinter->lines as $line) {
405 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'creer')) {
406 throw new RestException(403,
"Insufficiant rights");
411 foreach ($request_data as $field => $value) {
412 if ($field ===
'caller') {
414 $this->fichinter->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
418 $this->fichinter->$field = $this->
_checkValForAPI($field, $value, $this->fichinter);
422 throw new RestException(404,
'Intervention not found');
426 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
429 $updateRes = $this->fichinter->addLine(
430 DolibarrApiAccess::$user,
432 $this->fichinter->description,
433 $this->fichinter->date,
434 $this->fichinter->duration
437 if ($updateRes > 0) {
440 throw new RestException(400, $this->fichinter->errorsToString());
456 public function delete(
$id)
458 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'supprimer')) {
459 throw new RestException(403);
461 $result = $this->fichinter->fetch(
$id);
463 throw new RestException(404,
'Intervention not found');
467 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
470 if (!$this->fichinter->delete(DolibarrApiAccess::$user)) {
471 throw new RestException(500,
'Error when delete intervention : '.$this->fichinter->errorsToString());
477 'message' =>
'Intervention deleted'
503 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'creer')) {
504 throw new RestException(403,
"Insufficiant rights");
506 $result = $this->fichinter->fetch(
$id);
508 throw new RestException(404,
'Intervention not found');
512 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
515 $result = $this->fichinter->setValid(DolibarrApiAccess::$user, $notrigger);
517 throw new RestException(304,
'Error nothing done. May be object is already validated');
520 throw new RestException(500,
'Error when validating Intervention: '.$this->fichinter->errorsToString());
523 $this->fichinter->fetchObjectLinked();
551 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'creer')) {
552 throw new RestException(403,
"Insufficiant 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->setClose(DolibarrApiAccess::$user, $notrigger);
565 throw new RestException(304,
'Error nothing done. May be object is already closed');
568 throw new RestException(500,
'Error when closing Intervention: '.$this->fichinter->errorsToString());
571 $this->fichinter->fetchObjectLinked();
594 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'creer')) {
595 throw new RestException(403);
598 $result = $this->fichinter->fetch(
$id);
600 throw new RestException(404,
'Intervention not found');
603 if ($this->fichinter->status != 0) {
604 throw new RestException(403,
'Intervention not in draft status : '.$this->fichinter->getLibStatut(1));
608 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
612 if ($objectline->fetch($lineid) <= 0) {
613 throw new RestException(404,
'Intervention line not found');
615 if ($objectline->fk_fichinter != $this->fichinter->id) {
616 throw new RestException(403,
'Line does not belong to this intervention');
619 $updateRes = $objectline->deleteLine(DolibarrApiAccess::$user);
621 if ($updateRes >= 0) {
624 throw new RestException(405, $this->fichinter->errorsToString());
646 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'creer')) {
647 throw new RestException(403,
"Insufficiant rights");
649 $result = $this->fichinter->fetch(
$id);
651 throw new RestException(404,
'Intervention not found');
655 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
658 $result = $this->fichinter->setDraft(DolibarrApiAccess::$user);
660 throw new RestException(304,
'Nothing done. . May be object is already set as draft.');
663 throw new RestException(500,
'Error when closing intervention: '.$this->fichinter->errorsToString());
666 $this->fichinter->fetchObjectLinked();
688 public function addContact(
$id, $fk_socpeople, $type_contact, $source, $notrigger = 0)
690 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'creer')) {
691 throw new RestException(403);
693 $result = $this->fichinter->fetch(
$id);
695 throw new RestException(404,
'Interventional not found');
699 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
702 $result = $this->fichinter->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
704 throw new RestException(500,
'Error : '.$this->fichinter->errorsToString());
707 $result = $this->fichinter->fetch(
$id);
709 throw new RestException(404,
'Interventional not found');
731 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'lire')) {
732 throw new RestException(403);
735 $result = $this->fichinter->fetch(
$id);
737 throw new RestException(404,
'Interventional not found');
741 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
744 if (empty($source) || $source ==
'external') {
745 $contacts = $this->fichinter->liste_contact(-1,
'external', 0, $type);
749 if (empty($source) || $source ==
'internal') {
750 $socpeoples = $this->fichinter->liste_contact(-1,
'internal', 0, $type);
752 $socpeoples = array();
755 $contacts = array_merge($contacts, $socpeoples);
777 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'creer')) {
778 throw new RestException(403);
781 $result = $this->fichinter->fetch(
$id);
784 throw new RestException(404,
'Interventional not found');
788 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
790 foreach (array(
'internal',
'external') as $source) {
791 $contacts = $this->fichinter->liste_contact(-1, $source);
793 foreach ($contacts as $contact) {
794 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
795 $result = $this->fichinter->delete_contact($contact[
'rowid']);
797 throw new RestException(500,
'Error when deleted the contact');
823 $result = $this->fichinter->fetch(
$id);
825 throw new RestException(404,
'Intervention not found');
828 if ($this->fichinter->status != 0) {
829 throw new RestException(403,
'Intervention not in draft status : '.$this->fichinter->getLibStatut(1));
833 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
837 if ($objectline->fetch($lineid) <= 0) {
838 throw new RestException(404,
'Intervention line not found');
840 if ($objectline->fk_fichinter != $this->fichinter->id) {
841 throw new RestException(403,
'Line does not belong to this intervention');
843 $request_data = (object) $request_data;
845 if (isset($request_data->desc) || isset($request_data->description)) {
846 $objectline->desc =
sanitizeVal($request_data->desc ?? $request_data->description,
'restricthtml');
848 if (isset($request_data->date)) {
849 $objectline->date = (int)
sanitizeVal($request_data->date);
851 if (isset($request_data->duration)) {
852 $objectline->duration = (int)
sanitizeVal($request_data->duration);
854 if (isset($request_data->rang)) {
855 $objectline->rang = (int)
sanitizeVal($request_data->rang);
858 $updateRes = $objectline->update(DolibarrApiAccess::$user);
860 if ($updateRes >= 0) {
861 $result = $this->fichinter->fetch(
$id);
865 throw new RestException(500, $this->fichinter->errorsToString());
868 throw new RestException(500, $objectline->errorsToString());
883 $fichinter = array();
884 foreach (Interventions::$FIELDS as $field) {
885 if (!isset($data[$field])) {
886 throw new RestException(400,
"$field field missing");
888 $fichinter[$field] = $data[$field];
910 unset(
$object->labelStatusShort);
925 if ($data ===
null) {
928 $fichinter = array();
929 foreach (Interventions::$FIELDSLINE as $field) {
930 if (!isset($data[$field])) {
931 throw new RestException(400,
"$field field missing");
933 $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.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $parenttableforentity='')
Check access by user to a given resource.
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.
getLines($id)
Get lines of 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)
Add a line to an intervention.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
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.