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->error);
401 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'creer')) {
402 throw new RestException(403,
"Insufficiant rights");
407 foreach ($request_data as $field => $value) {
408 if ($field ===
'caller') {
410 $this->fichinter->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
414 $this->fichinter->$field = $this->
_checkValForAPI($field, $value, $this->fichinter);
418 throw new RestException(404,
'Intervention not found');
422 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
425 $updateRes = $this->fichinter->addLine(
426 DolibarrApiAccess::$user,
428 $this->fichinter->description,
429 $this->fichinter->date,
430 $this->fichinter->duration
433 if ($updateRes > 0) {
436 throw new RestException(400, $this->fichinter->error);
452 public function delete(
$id)
454 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'supprimer')) {
455 throw new RestException(403);
457 $result = $this->fichinter->fetch(
$id);
459 throw new RestException(404,
'Intervention not found');
463 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
466 if (!$this->fichinter->delete(DolibarrApiAccess::$user)) {
467 throw new RestException(500,
'Error when delete intervention : '.$this->fichinter->error);
473 'message' =>
'Intervention deleted'
499 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'creer')) {
500 throw new RestException(403,
"Insufficiant rights");
502 $result = $this->fichinter->fetch(
$id);
504 throw new RestException(404,
'Intervention not found');
508 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
511 $result = $this->fichinter->setValid(DolibarrApiAccess::$user, $notrigger);
513 throw new RestException(304,
'Error nothing done. May be object is already validated');
516 throw new RestException(500,
'Error when validating Intervention: '.$this->fichinter->error);
519 $this->fichinter->fetchObjectLinked();
547 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'creer')) {
548 throw new RestException(403,
"Insufficiant rights");
550 $result = $this->fichinter->fetch(
$id);
552 throw new RestException(404,
'Intervention not found');
556 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
559 $result = $this->fichinter->setClose(DolibarrApiAccess::$user, $notrigger);
561 throw new RestException(304,
'Error nothing done. May be object is already closed');
564 throw new RestException(500,
'Error when closing Intervention: '.$this->fichinter->error);
567 $this->fichinter->fetchObjectLinked();
590 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'creer')) {
591 throw new RestException(403);
594 $result = $this->fichinter->fetch(
$id);
596 throw new RestException(404,
'Intervention not found');
599 if ($this->fichinter->status != 0) {
600 throw new RestException(403,
'Intervention not in draft status : '.$this->fichinter->getLibStatut(1));
604 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
608 if ($objectline->fetch($lineid) <= 0) {
609 throw new RestException(404,
'Intervention line not found');
612 $updateRes = $objectline->deleteLine(DolibarrApiAccess::$user);
614 if ($updateRes >= 0) {
617 throw new RestException(405, $this->fichinter->error);
639 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'creer')) {
640 throw new RestException(403,
"Insufficiant rights");
642 $result = $this->fichinter->fetch(
$id);
644 throw new RestException(404,
'Intervention not found');
648 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
651 $result = $this->fichinter->setDraft(DolibarrApiAccess::$user);
653 throw new RestException(304,
'Nothing done. . May be object is already set as draft.');
656 throw new RestException(500,
'Error when closing intervention: '.$this->fichinter->error);
659 $this->fichinter->fetchObjectLinked();
681 public function addContact(
$id, $fk_socpeople, $type_contact, $source, $notrigger = 0)
683 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'creer')) {
684 throw new RestException(403);
686 $result = $this->fichinter->fetch(
$id);
688 throw new RestException(404,
'Interventional not found');
692 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
695 $result = $this->fichinter->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
697 throw new RestException(500,
'Error : '.$this->fichinter->error);
700 $result = $this->fichinter->fetch(
$id);
702 throw new RestException(404,
'Interventional not found');
724 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'lire')) {
725 throw new RestException(403);
728 $result = $this->fichinter->fetch(
$id);
730 throw new RestException(404,
'Interventional not found');
734 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
737 if (empty($source) || $source ==
'external') {
738 $contacts = $this->fichinter->liste_contact(-1,
'external', 0, $type);
742 if (empty($source) || $source ==
'internal') {
743 $socpeoples = $this->fichinter->liste_contact(-1,
'internal', 0, $type);
745 $socpeoples = array();
748 $contacts = array_merge($contacts, $socpeoples);
770 if (!DolibarrApiAccess::$user->hasRight(
'ficheinter',
'creer')) {
771 throw new RestException(403);
774 $result = $this->fichinter->fetch(
$id);
777 throw new RestException(404,
'Interventional not found');
781 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
783 foreach (array(
'internal',
'external') as $source) {
784 $contacts = $this->fichinter->liste_contact(-1, $source);
786 foreach ($contacts as $contact) {
787 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
788 $result = $this->fichinter->delete_contact($contact[
'rowid']);
790 throw new RestException(500,
'Error when deleted the contact');
816 $result = $this->fichinter->fetch(
$id);
818 throw new RestException(404,
'Intervention not found');
821 if ($this->fichinter->status != 0) {
822 throw new RestException(403,
'Intervention not in draft status : '.$this->fichinter->getLibStatut(1));
826 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
830 if ($objectline->fetch($lineid) <= 0) {
831 throw new RestException(404,
'Intervention line not found');
833 $request_data = (object) $request_data;
835 if (isset($request_data->desc) || isset($request_data->description)) {
836 $objectline->desc =
sanitizeVal($request_data->desc ?? $request_data->description,
'restricthtml');
838 if (isset($request_data->date)) {
839 $objectline->date = (int)
sanitizeVal($request_data->date);
841 if (isset($request_data->duration)) {
842 $objectline->duration = (int)
sanitizeVal($request_data->duration);
844 if (isset($request_data->rang)) {
845 $objectline->rang = (int)
sanitizeVal($request_data->rang);
848 $updateRes = $objectline->update(DolibarrApiAccess::$user);
850 if ($updateRes >= 0) {
851 $result = $this->fichinter->fetch(
$id);
855 throw new RestException(500, $this->fichinter->error);
858 throw new RestException(500, $objectline->error);
873 $fichinter = array();
874 foreach (Interventions::$FIELDS as $field) {
875 if (!isset($data[$field])) {
876 throw new RestException(400,
"$field field missing");
878 $fichinter[$field] = $data[$field];
900 unset(
$object->labelStatusShort);
915 if ($data ===
null) {
918 $fichinter = array();
919 foreach (Interventions::$FIELDSLINE as $field) {
920 if (!isset($data[$field])) {
921 throw new RestException(400,
"$field field missing");
923 $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.
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.
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.