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);
77 public function get(
$id)
98 return $this->
getCommon(0, $track_id,
'');
131 if (!DolibarrApiAccess::$user->hasRight(
'ticket',
'read')) {
132 throw new RestException(403);
136 if ((
$id < 0) && !$track_id && !$ref) {
137 throw new RestException(400,
'Wrong parameters');
139 if (empty(
$id) && empty($ref) && empty($track_id)) {
140 $result = $this->ticket->initAsSpecimen();
142 $result = $this->ticket->fetch(
$id, $ref, $track_id);
145 throw new RestException(404,
'Ticket not found');
149 if ($this->ticket->fk_user_assign > 0) {
150 $userStatic =
new User($this->db);
151 $userStatic->fetch($this->ticket->fk_user_assign);
152 $this->ticket->fk_user_assign_string = $userStatic->firstname.
' '.$userStatic->lastname;
157 $this->ticket->loadCacheMsgsTicket();
158 if (is_array($this->ticket->cache_msgs_ticket) && count($this->ticket->cache_msgs_ticket) > 0) {
159 $num = count($this->ticket->cache_msgs_ticket);
162 if ($this->ticket->cache_msgs_ticket[$i][
'fk_user_author'] > 0) {
163 $user_action =
new User($this->db);
164 $user_action->fetch($this->ticket->cache_msgs_ticket[$i][
'fk_user_author']);
169 'id' => $this->ticket->cache_msgs_ticket[$i][
'id'],
170 'fk_user_action' => $this->ticket->cache_msgs_ticket[$i][
'fk_user_author'],
171 'fk_user_action_socid' => $user_action->socid,
172 'fk_user_action_string' =>
dolGetFirstLastname($user_action->firstname, $user_action->lastname),
173 'message' => $this->ticket->cache_msgs_ticket[$i][
'message'],
174 'datec' => $this->ticket->cache_msgs_ticket[$i][
'datec'],
175 'private' => $this->ticket->cache_msgs_ticket[$i][
'private']
179 $this->ticket->messages = $messages;
183 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
205 public function index($socid = 0, $sortfield =
"t.rowid", $sortorder =
"ASC", $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
207 if (!DolibarrApiAccess::$user->hasRight(
'ticket',
'read')) {
208 throw new RestException(403);
213 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $socid;
218 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
219 $search_sale = DolibarrApiAccess::$user->id;
222 $sql =
"SELECT t.rowid";
223 $sql .=
" FROM ".MAIN_DB_PREFIX.
"ticket AS t";
224 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"ticket_extrafields AS ef ON (ef.fk_object = t.rowid)";
225 $sql .=
' WHERE t.entity IN ('.getEntity(
'ticket', 1).
')';
227 $sql .=
" AND t.fk_soc = ".((int) $socid);
230 if ($search_sale && $search_sale !=
'-1') {
231 if ($search_sale == -2) {
232 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
233 } elseif ($search_sale > 0) {
234 $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).
")";
242 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
247 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
249 $sql .= $this->db->order($sortfield, $sortorder);
255 $offset = $limit * $page;
257 $sql .= $this->db->plimit($limit, $offset);
260 $result = $this->db->query($sql);
262 $num = $this->db->num_rows($result);
265 $obj = $this->db->fetch_object($result);
266 $ticket_static =
new Ticket($this->db);
267 if ($ticket_static->fetch($obj->rowid)) {
268 if ($ticket_static->fk_user_assign > 0) {
269 $userStatic =
new User($this->db);
270 $userStatic->fetch($ticket_static->fk_user_assign);
271 $ticket_static->fk_user_assign_string = $userStatic->firstname.
' '.$userStatic->lastname;
278 throw new RestException(503,
'Error when retrieve ticket list');
282 if ($pagination_data) {
283 $totalsResult = $this->db->query($sqlTotals);
284 $total = $this->db->fetch_object($totalsResult)->total;
289 $obj_ret[
'data'] = $tmp;
290 $obj_ret[
'pagination'] = [
291 'total' => (int) $total,
293 'page_count' => ceil((
int) $total / $limit),
307 public function post($request_data =
null)
309 $ticketstatic =
new Ticket($this->db);
310 if (!DolibarrApiAccess::$user->hasRight(
'ticket',
'write')) {
311 throw new RestException(403);
314 $result = $this->
_validate($request_data);
316 foreach ($request_data as $field => $value) {
317 if ($field ===
'caller') {
319 $this->ticket->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
323 $this->ticket->$field = $this->
_checkValForAPI($field, $value, $this->ticket);
325 if (empty($this->ticket->ref)) {
326 $this->ticket->ref = $ticketstatic->getDefaultRef();
328 if (empty($this->ticket->track_id)) {
332 if ($this->ticket->create(DolibarrApiAccess::$user) < 0) {
333 throw new RestException(500,
"Error creating ticket", array_merge(array($this->ticket->error), $this->ticket->errors));
336 return $this->ticket->id;
348 $ticketstatic =
new Ticket($this->db);
349 if (!DolibarrApiAccess::$user->hasRight(
'ticket',
'write')) {
350 throw new RestException(403);
355 foreach ($request_data as $field => $value) {
356 if ($field ===
'caller') {
358 $this->ticket->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
362 $this->ticket->$field = $this->
_checkValForAPI($field, $value, $this->ticket);
364 $ticketMessageText = $this->ticket->message;
365 $result = $this->ticket->fetch(0,
'', $this->ticket->track_id);
367 throw new RestException(404,
'Ticket not found');
369 $this->ticket->message = $ticketMessageText;
370 if (!$this->ticket->createTicketMessage(DolibarrApiAccess::$user)) {
371 throw new RestException(500,
'Error when creating ticket');
373 return $this->ticket->id;
383 public function put(
$id, $request_data =
null)
385 if (!DolibarrApiAccess::$user->hasRight(
'ticket',
'write')) {
386 throw new RestException(403);
389 $result = $this->ticket->fetch(
$id);
391 throw new RestException(404,
'Ticket not found');
395 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
398 foreach ($request_data as $field => $value) {
399 if ($field ===
'caller') {
401 $this->ticket->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
405 $this->ticket->$field = $this->
_checkValForAPI($field, $value, $this->ticket);
408 if ($this->ticket->update(DolibarrApiAccess::$user) > 0) {
409 return $this->
get(
$id);
411 throw new RestException(500, $this->ticket->error);
422 public function delete(
$id)
424 if (!DolibarrApiAccess::$user->hasRight(
'ticket',
'delete')) {
425 throw new RestException(403);
427 $result = $this->ticket->fetch(
$id);
429 throw new RestException(404,
'Ticket not found');
433 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
436 if (!$this->ticket->delete(DolibarrApiAccess::$user)) {
437 throw new RestException(500,
'Error when deleting ticket');
443 'message' =>
'Ticket deleted'
459 foreach (Tickets::$FIELDS as $field) {
460 if (!isset($data[$field])) {
461 throw new RestException(400,
"$field field missing");
463 $ticket[$field] = $data[$field];
479 foreach (Tickets::$FIELDS_MESSAGES as $field) {
480 if (!isset($data[$field])) {
481 throw new RestException(400,
"$field field missing");
483 $ticket[$field] = $data[$field];
510 "table_element_line",
517 "barcode_type_label",
518 "barcode_type_coder",
522 "fk_delivery_address",
523 "shipping_method_id",
536 "location_incoterms",
544 "cache_types_tickets",
545 "cache_category_tickets",
549 "multicurrency_code",
551 "multicurrency_total_ht",
552 "multicurrency_total_ttc",
553 "multicurrency_total_tva",
554 "multicurrency_total_localtax1",
555 "multicurrency_total_localtax2"
557 foreach ($attr2clean as $toclean) {
563 $nboflines = count(
$object->lines);
564 for ($i = 0; $i < $nboflines; $i++) {
570 if (isset(
$object->linkedObjects) && count(
$object->linkedObjects) > 0) {
571 foreach (
$object->linkedObjects as $type_object => $linked_object) {
572 foreach ($linked_object as $object2clean) {
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $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.
getCommon($id=0, $track_id='', $ref='')
Get properties of a Ticket object Return an array with ticket information.
getByRef($ref)
Get properties of a Ticket object from ref.
__construct()
Constructor.
_cleanObjectDatas($object)
Clean sensible object datas.
index($socid=0, $sortfield="t.rowid", $sortorder="ASC", $limit=100, $page=0, $sqlfilters='', $properties='', $pagination_data=false)
List tickets.
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.
getByTrackId($track_id)
Get properties of a Ticket object from track id.
_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.