19 use Luracast\Restler\RestException;
21require_once DOL_DOCUMENT_ROOT.
'/ticket/class/ticket.class.php';
22require_once DOL_DOCUMENT_ROOT.
'/core/lib/ticket.lib.php';
36 public static $FIELDS = array(
44 public static $FIELDS_MESSAGES = array(
61 $this->ticket =
new Ticket($this->db);
76 public function get($id)
97 return $this->
getCommon(0, $track_id,
'');
128 private function getCommon($id = 0, $track_id =
'', $ref =
'')
130 if (!DolibarrApiAccess::$user->hasRight(
'ticket',
'read')) {
131 throw new RestException(403);
135 if (($id < 0) && !$track_id && !$ref) {
136 throw new RestException(400,
'Wrong parameters');
139 $result = $this->ticket->initAsSpecimen();
141 $result = $this->ticket->fetch($id, $ref, $track_id);
144 throw new RestException(404,
'Ticket not found');
148 if ($this->ticket->fk_user_assign > 0) {
149 $userStatic =
new User($this->db);
150 $userStatic->fetch($this->ticket->fk_user_assign);
151 $this->ticket->fk_user_assign_string = $userStatic->firstname.
' '.$userStatic->lastname;
156 $this->ticket->loadCacheMsgsTicket();
157 if (is_array($this->ticket->cache_msgs_ticket) && count($this->ticket->cache_msgs_ticket) > 0) {
158 $num = count($this->ticket->cache_msgs_ticket);
161 if ($this->ticket->cache_msgs_ticket[$i][
'fk_user_author'] > 0) {
162 $user_action =
new User($this->db);
163 $user_action->fetch($this->ticket->cache_msgs_ticket[$i][
'fk_user_author']);
168 'id' => $this->ticket->cache_msgs_ticket[$i][
'id'],
169 'fk_user_action' => $this->ticket->cache_msgs_ticket[$i][
'fk_user_author'],
170 'fk_user_action_socid' => $user_action->socid,
171 'fk_user_action_string' =>
dolGetFirstLastname($user_action->firstname, $user_action->lastname),
172 'message' => $this->ticket->cache_msgs_ticket[$i][
'message'],
173 'datec' => $this->ticket->cache_msgs_ticket[$i][
'datec'],
174 'private' => $this->ticket->cache_msgs_ticket[$i][
'private']
178 $this->ticket->messages = $messages;
182 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
203 public function index($socid = 0, $sortfield =
"t.rowid", $sortorder =
"ASC", $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'')
205 if (!DolibarrApiAccess::$user->hasRight(
'ticket',
'read')) {
206 throw new RestException(403);
211 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $socid;
216 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
217 $search_sale = DolibarrApiAccess::$user->id;
220 $sql =
"SELECT t.rowid";
221 $sql .=
" FROM ".MAIN_DB_PREFIX.
"ticket AS t";
222 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"ticket_extrafields AS ef ON (ef.fk_object = t.rowid)";
223 $sql .=
' WHERE t.entity IN ('.getEntity(
'ticket', 1).
')';
225 $sql .=
" AND t.fk_soc = ".((int) $socid);
228 if ($search_sale && $search_sale !=
'-1') {
229 if ($search_sale == -2) {
230 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
231 } elseif ($search_sale > 0) {
232 $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).
")";
240 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
244 $sql .= $this->db->order($sortfield, $sortorder);
250 $offset = $limit * $page;
252 $sql .= $this->db->plimit($limit, $offset);
255 $result = $this->db->query($sql);
257 $num = $this->db->num_rows($result);
260 $obj = $this->db->fetch_object($result);
261 $ticket_static =
new Ticket($this->db);
262 if ($ticket_static->fetch($obj->rowid)) {
263 if ($ticket_static->fk_user_assign > 0) {
264 $userStatic =
new User($this->db);
265 $userStatic->fetch($ticket_static->fk_user_assign);
266 $ticket_static->fk_user_assign_string = $userStatic->firstname.
' '.$userStatic->lastname;
273 throw new RestException(503,
'Error when retrieve ticket list');
285 public function post($request_data =
null)
287 $ticketstatic =
new Ticket($this->db);
288 if (!DolibarrApiAccess::$user->hasRight(
'ticket',
'write')) {
289 throw new RestException(403);
292 $result = $this->
_validate($request_data);
294 foreach ($request_data as $field => $value) {
295 if ($field ===
'caller') {
297 $this->ticket->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
301 $this->ticket->$field = $this->
_checkValForAPI($field, $value, $this->ticket);
303 if (empty($this->ticket->ref)) {
304 $this->ticket->ref = $ticketstatic->getDefaultRef();
306 if (empty($this->ticket->track_id)) {
310 if ($this->ticket->create(DolibarrApiAccess::$user) < 0) {
311 throw new RestException(500,
"Error creating ticket", array_merge(array($this->ticket->error), $this->ticket->errors));
314 return $this->ticket->id;
326 $ticketstatic =
new Ticket($this->db);
327 if (!DolibarrApiAccess::$user->hasRight(
'ticket',
'write')) {
328 throw new RestException(403);
333 foreach ($request_data as $field => $value) {
334 if ($field ===
'caller') {
336 $this->ticket->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
340 $this->ticket->$field = $this->
_checkValForAPI($field, $value, $this->ticket);
342 $ticketMessageText = $this->ticket->message;
343 $result = $this->ticket->fetch(
'',
'', $this->ticket->track_id);
345 throw new RestException(404,
'Ticket not found');
347 $this->ticket->message = $ticketMessageText;
348 if (!$this->ticket->createTicketMessage(DolibarrApiAccess::$user)) {
349 throw new RestException(500,
'Error when creating ticket');
351 return $this->ticket->id;
361 public function put($id, $request_data =
null)
363 if (!DolibarrApiAccess::$user->hasRight(
'ticket',
'write')) {
364 throw new RestException(403);
367 $result = $this->ticket->fetch($id);
369 throw new RestException(404,
'Ticket not found');
373 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
376 foreach ($request_data as $field => $value) {
377 if ($field ===
'caller') {
379 $this->ticket->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
383 $this->ticket->$field = $this->
_checkValForAPI($field, $value, $this->ticket);
386 if ($this->ticket->update(DolibarrApiAccess::$user) > 0) {
387 return $this->
get($id);
389 throw new RestException(500, $this->ticket->error);
400 public function delete($id)
402 if (!DolibarrApiAccess::$user->hasRight(
'ticket',
'delete')) {
403 throw new RestException(403);
405 $result = $this->ticket->fetch($id);
407 throw new RestException(404,
'Ticket not found');
411 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
414 if (!$this->ticket->delete(DolibarrApiAccess::$user)) {
415 throw new RestException(500,
'Error when deleting ticket');
421 'message' =>
'Ticket deleted'
437 foreach (Tickets::$FIELDS as $field) {
438 if (!isset($data[$field])) {
439 throw new RestException(400,
"$field field missing");
441 $ticket[$field] = $data[$field];
457 foreach (Tickets::$FIELDS_MESSAGES as $field) {
458 if (!isset($data[$field])) {
459 throw new RestException(400,
"$field field missing");
461 $ticket[$field] = $data[$field];
488 "table_element_line",
495 "barcode_type_label",
496 "barcode_type_coder",
500 "fk_delivery_address",
501 "shipping_method_id",
514 "location_incoterms",
522 "cache_types_tickets",
523 "cache_category_tickets",
527 "multicurrency_code",
529 "multicurrency_total_ht",
530 "multicurrency_total_ttc",
531 "multicurrency_total_tva",
532 "multicurrency_total_localtax1",
533 "multicurrency_total_localtax2"
535 foreach ($attr2clean as $toclean) {
541 $nboflines = count(
$object->lines);
542 for ($i = 0; $i < $nboflines; $i++) {
548 if (isset(
$object->linkedObjects) && count(
$object->linkedObjects) > 0) {
549 foreach (
$object->linkedObjects as $type_object => $linked_object) {
550 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.
index($socid=0, $sortfield="t.rowid", $sortorder="ASC", $limit=100, $page=0, $sqlfilters='', $properties='')
List tickets.
__construct()
Constructor.
_cleanObjectDatas($object)
Clean sensible object datas.
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.