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'];
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);
420 throw new RestException(404,
'Ticket not found');
424 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
427 $this->ticket->message = $ticketMessageText;
428 if (!$this->ticket->createTicketMessage(DolibarrApiAccess::$user)) {
429 throw new RestException(500,
'Error when creating ticket');
431 return $this->ticket->id;
443 public function put(
$id, $request_data =
null)
445 if (!DolibarrApiAccess::$user->hasRight(
'ticket',
'write')) {
446 throw new RestException(403);
449 $result = $this->ticket->fetch(
$id);
451 throw new RestException(404,
'Ticket not found');
455 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
459 $socid = (int) $request_data[
'socid'];
461 $thirdpartytmp =
new Societe($this->db);
462 $thirdparty_result = $thirdpartytmp->fetch($socid);
463 if ($thirdparty_result < 1) {
464 throw new RestException(404,
'Thirdparty with id='.$socid.
' not found or not allowed');
467 throw new RestException(404,
'Thirdparty with id='.$thirdpartytmp->id.
' not found or not allowed');
471 foreach ($request_data as $field => $value) {
472 if ($field ===
'caller') {
474 $this->ticket->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
478 if ($field ==
'id') {
481 if ($field ==
'array_options' && is_array($value)) {
482 foreach ($value as $index => $val) {
483 $this->ticket->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->ticket);
488 $this->ticket->$field = $this->
_checkValForAPI($field, $value, $this->ticket);
491 if ($this->ticket->update(DolibarrApiAccess::$user) > 0) {
492 return $this->
get(
$id);
494 throw new RestException(500, $this->ticket->error);
506 public function delete(
$id)
508 if (!DolibarrApiAccess::$user->hasRight(
'ticket',
'delete')) {
509 throw new RestException(403);
511 $result = $this->ticket->fetch(
$id);
513 throw new RestException(404,
'Ticket not found');
517 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
520 if (!$this->ticket->delete(DolibarrApiAccess::$user)) {
521 throw new RestException(500,
'Error when deleting ticket');
527 'message' =>
'Ticket deleted'
542 if ($data ===
null) {
546 foreach (Tickets::$FIELDS as $field) {
547 if (!isset($data[$field])) {
548 throw new RestException(400,
"$field field missing");
550 $ticket[$field] = $data[$field];
565 if ($data ===
null) {
569 foreach (Tickets::$FIELDS_MESSAGES as $field) {
570 if (!isset($data[$field])) {
571 throw new RestException(400,
"$field field missing");
573 $ticket[$field] = $data[$field];
602 "table_element_line",
609 "barcode_type_label",
610 "barcode_type_coder",
614 "fk_delivery_address",
615 "shipping_method_id",
628 "location_incoterms",
636 "cache_types_tickets",
640 "multicurrency_code",
642 "multicurrency_total_ht",
643 "multicurrency_total_ttc",
644 "multicurrency_total_tva",
645 "multicurrency_total_localtax1",
646 "multicurrency_total_localtax2"
648 foreach ($attr2clean as $toclean) {
654 $nboflines = count(
$object->lines);
655 for ($i = 0; $i < $nboflines; $i++) {
661 if (isset(
$object->linkedObjects) && count(
$object->linkedObjects) > 0) {
662 foreach (
$object->linkedObjects as $type_object => $linked_object) {
663 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.