20use Luracast\Restler\RestException;
22require_once DOL_DOCUMENT_ROOT.
'/ticket/class/ticket.class.php';
23require_once DOL_DOCUMENT_ROOT.
'/core/lib/ticket.lib.php';
37 public static $FIELDS = array(
45 public static $FIELDS_MESSAGES = array(
62 $this->ticket =
new Ticket($this->db);
78 public function get(
$id, $contact_list = 1)
100 return $this->
getCommon(0, $track_id,
'', $contact_list);
120 return $this->
getCommon(0,
'', $ref, $contact_list);
133 private function getCommon(
$id = 0, $track_id =
'', $ref =
'', $contact_list = 1)
135 if (!DolibarrApiAccess::$user->hasRight(
'ticket',
'read')) {
136 throw new RestException(403);
140 if ((
$id < 0) && !$track_id && !$ref) {
141 throw new RestException(400,
'Wrong parameters');
143 if (empty(
$id) && empty($ref) && empty($track_id)) {
144 $result = $this->ticket->initAsSpecimen();
146 $result = $this->ticket->fetch(
$id, $ref, $track_id);
149 throw new RestException(404,
'Ticket not found');
153 if ($this->ticket->fk_user_assign > 0) {
154 $userStatic =
new User($this->db);
155 $userStatic->fetch($this->ticket->fk_user_assign);
156 $this->ticket->fk_user_assign_string = $userStatic->firstname.
' '.$userStatic->lastname;
161 $this->ticket->loadCacheMsgsTicket();
162 if (is_array($this->ticket->cache_msgs_ticket) && count($this->ticket->cache_msgs_ticket) > 0) {
163 $num = count($this->ticket->cache_msgs_ticket);
166 if ($this->ticket->cache_msgs_ticket[$i][
'fk_user_author'] > 0) {
167 $user_action =
new User($this->db);
168 $user_action->fetch($this->ticket->cache_msgs_ticket[$i][
'fk_user_author']);
175 'id' => $this->ticket->cache_msgs_ticket[$i][
'id'],
176 'fk_user_action' => $this->ticket->cache_msgs_ticket[$i][
'fk_user_author'],
177 'fk_user_action_socid' => $user_action ===
null ?
'' : $user_action->socid,
178 'fk_user_action_string' => $user_action ===
null ?
'' :
dolGetFirstLastname($user_action->firstname, $user_action->lastname),
179 'message' => $this->ticket->cache_msgs_ticket[$i][
'message'],
180 'datec' => $this->ticket->cache_msgs_ticket[$i][
'datec'],
181 'private' => $this->ticket->cache_msgs_ticket[$i][
'private']
185 $this->ticket->messages = $messages;
189 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
192 if ($contact_list > -1) {
194 $tmparray = $this->ticket->liste_contact(-1,
'external', $contact_list);
195 if (is_array($tmparray)) {
196 $this->ticket->contacts_ids = $tmparray;
198 $tmparray = $this->ticket->liste_contact(-1,
'internal', $contact_list);
199 if (is_array($tmparray)) {
200 $this->ticket->contacts_ids_internal = $tmparray;
226 public function index($socid = 0, $sortfield =
"t.rowid", $sortorder =
"ASC", $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'', $loadcontacts = 0, $pagination_data =
false)
228 if (!DolibarrApiAccess::$user->hasRight(
'ticket',
'read')) {
229 throw new RestException(403);
234 $socid = DolibarrApiAccess::$user->socid ?: $socid;
239 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
240 $search_sale = DolibarrApiAccess::$user->id;
243 $sql =
"SELECT t.rowid";
244 $sql .=
" FROM ".MAIN_DB_PREFIX.
"ticket AS t";
245 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe AS s ON (s.rowid = t.fk_soc)";
246 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"ticket_extrafields AS ef ON (ef.fk_object = t.rowid)";
247 $sql .=
' WHERE t.entity IN ('.getEntity(
'ticket', 1).
')';
249 $sql .=
" AND t.fk_soc = ".((int) $socid);
252 if ($search_sale && $search_sale !=
'-1') {
253 if ($search_sale == -2) {
254 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
255 } elseif ($search_sale > 0) {
256 $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).
")";
264 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
269 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
271 $sql .= $this->db->order($sortfield, $sortorder);
277 $offset = $limit * $page;
279 $sql .= $this->db->plimit($limit, $offset);
282 $result = $this->db->query($sql);
285 $num = $this->db->num_rows($result);
286 $min = min($num, ($limit <= 0 ? $num : $limit));
288 $obj = $this->db->fetch_object($result);
289 $ticket_static =
new Ticket($this->db);
290 if ($ticket_static->fetch($obj->rowid)) {
291 if ($ticket_static->fk_user_assign > 0) {
292 $userStatic =
new User($this->db);
293 $userStatic->fetch($ticket_static->fk_user_assign);
294 $ticket_static->fk_user_assign_string = $userStatic->firstname.
' '.$userStatic->lastname;
299 $tmparray = $ticket_static->liste_contact(-1,
'external', 1);
300 if (is_array($tmparray)) {
301 $ticket_static->contacts_ids = $tmparray;
303 $tmparray = $ticket_static->liste_contact(-1,
'internal', 1);
304 if (is_array($tmparray)) {
305 $ticket_static->contacts_ids_internal = $tmparray;
314 throw new RestException(503,
'Error when retrieve ticket list');
318 if ($pagination_data) {
319 $totalsResult = $this->db->query($sqlTotals);
320 $total = $this->db->fetch_object($totalsResult)->total;
325 $obj_ret[
'data'] = $tmp;
326 $obj_ret[
'pagination'] = [
327 'total' => (int) $total,
329 'page_count' => ceil((
int) $total / $limit),
345 public function post($request_data =
null)
347 $ticketstatic =
new Ticket($this->db);
348 if (!DolibarrApiAccess::$user->hasRight(
'ticket',
'write')) {
349 throw new RestException(403);
356 $socid = (int) ($request_data[
'socid'] ?? 0);
358 $thirdpartytmp =
new Societe($this->db);
359 $thirdparty_result = $thirdpartytmp->fetch($socid);
360 if ($thirdparty_result < 1) {
361 throw new RestException(404,
'Thirdparty with id='.$socid.
' not found or not allowed');
364 throw new RestException(404,
'Thirdparty with id='.$thirdpartytmp->id.
' not found or not allowed');
368 foreach ($request_data as $field => $value) {
369 if ($field ===
'caller') {
371 $this->ticket->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
375 $this->ticket->$field = $this->
_checkValForAPI($field, $value, $this->ticket);
377 if (empty($this->ticket->ref)) {
378 $this->ticket->ref = $ticketstatic->getDefaultRef();
380 if (empty($this->ticket->track_id)) {
384 if ($this->ticket->create(DolibarrApiAccess::$user) < 0) {
385 throw new RestException(500,
"Error creating ticket", array_merge(array($this->ticket->error), $this->ticket->errors));
388 return $this->ticket->id;
401 if (!DolibarrApiAccess::$user->hasRight(
'ticket',
'write')) {
402 throw new RestException(403);
408 foreach ($request_data as $field => $value) {
409 if ($field ===
'caller') {
411 $this->ticket->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
415 $this->ticket->$field = $this->
_checkValForAPI($field, $value, $this->ticket);
417 $ticketMessageText = $this->ticket->message;
418 $result = $this->ticket->fetch(0,
'', $this->ticket->track_id);
422 throw new RestException(404,
'Ticket not found');
426 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
429 $this->ticket->message = $ticketMessageText;
430 if (!$this->ticket->createTicketMessage(DolibarrApiAccess::$user)) {
431 throw new RestException(500,
'Error when creating ticket');
433 return $this->ticket->id;
445 public function put(
$id, $request_data =
null)
447 if (!DolibarrApiAccess::$user->hasRight(
'ticket',
'write')) {
448 throw new RestException(403);
451 $result = $this->ticket->fetch(
$id);
453 throw new RestException(404,
'Ticket not found');
457 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
461 $socid = (int) ($request_data[
'socid'] ?? 0);
463 $thirdpartytmp =
new Societe($this->db);
464 $thirdparty_result = $thirdpartytmp->fetch($socid);
465 if ($thirdparty_result < 1) {
466 throw new RestException(404,
'Thirdparty with id='.$socid.
' not found or not allowed');
469 throw new RestException(404,
'Thirdparty with id='.$thirdpartytmp->id.
' not found or not allowed');
473 foreach ($request_data as $field => $value) {
474 if ($field ===
'caller') {
476 $this->ticket->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
480 if ($field ==
'id') {
483 if ($field ==
'array_options' && is_array($value)) {
484 foreach ($value as $index => $val) {
485 $this->ticket->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->ticket);
490 $this->ticket->$field = $this->
_checkValForAPI($field, $value, $this->ticket);
493 if ($this->ticket->update(DolibarrApiAccess::$user) > 0) {
494 return $this->
get(
$id);
496 throw new RestException(500, $this->ticket->error);
508 public function delete(
$id)
510 if (!DolibarrApiAccess::$user->hasRight(
'ticket',
'delete')) {
511 throw new RestException(403);
513 $result = $this->ticket->fetch(
$id);
515 throw new RestException(404,
'Ticket not found');
519 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
522 if (!$this->ticket->delete(DolibarrApiAccess::$user)) {
523 throw new RestException(500,
'Error when deleting ticket');
529 'message' =>
'Ticket deleted'
544 if ($data ===
null) {
548 foreach (Tickets::$FIELDS as $field) {
549 if (!isset($data[$field])) {
550 throw new RestException(400,
"$field field missing");
552 $ticket[$field] = $data[$field];
567 if ($data ===
null) {
571 foreach (Tickets::$FIELDS_MESSAGES as $field) {
572 if (!isset($data[$field])) {
573 throw new RestException(400,
"$field field missing");
575 $ticket[$field] = $data[$field];
604 "table_element_line",
611 "barcode_type_label",
612 "barcode_type_coder",
616 "fk_delivery_address",
617 "shipping_method_id",
630 "location_incoterms",
638 "cache_types_tickets",
642 "multicurrency_code",
644 "multicurrency_total_ht",
645 "multicurrency_total_ttc",
646 "multicurrency_total_tva",
647 "multicurrency_total_localtax1",
648 "multicurrency_total_localtax2"
650 foreach ($attr2clean as $toclean) {
656 $nboflines = count(
$object->lines);
657 for ($i = 0; $i < $nboflines; $i++) {
663 if (isset(
$object->linkedObjects) && count(
$object->linkedObjects) > 0) {
664 foreach (
$object->linkedObjects as $type_object => $linked_object) {
665 foreach ($linked_object as $object2clean) {
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $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 third parties objects (customers, suppliers, prospects...)
getByTrackId($track_id, $contact_list=1)
Get properties of a Ticket object from track id.
getCommon($id=0, $track_id='', $ref='', $contact_list=1)
Get properties of a Ticket object Return an array with ticket information.
__construct()
Constructor.
index($socid=0, $sortfield="t.rowid", $sortorder="ASC", $limit=100, $page=0, $sqlfilters='', $properties='', $loadcontacts=0, $pagination_data=false)
List tickets.
_cleanObjectDatas($object)
Clean sensible object datas @phpstan-template T.
postNewMessage($request_data=null)
Add a new message to an existing ticket identified by property ->track_id into request.
post($request_data=null)
Create ticket object.
put($id, $request_data=null)
Update ticket.
_validateMessage($data)
Validate fields before create or update object message.
getByRef($ref, $contact_list=1)
Get properties of a Ticket object from ref.
_validate($data)
Validate fields before create or update object.
Class to manage Dolibarr users.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
dolGetFirstLastname($firstname, $lastname, $nameorder=-1)
Return firstname and lastname in correct order.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
Class to generate the form for creating a new ticket.
generate_random_id($car=16)
Generate a random id.