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,
'');
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');
138 if (empty(
$id) && empty($ref) && empty($track_id)) {
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);
204 public function index($socid = 0, $sortfield =
"t.rowid", $sortorder =
"ASC", $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
206 if (!DolibarrApiAccess::$user->hasRight(
'ticket',
'read')) {
207 throw new RestException(403);
212 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $socid;
217 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
218 $search_sale = DolibarrApiAccess::$user->id;
221 $sql =
"SELECT t.rowid";
222 $sql .=
" FROM ".MAIN_DB_PREFIX.
"ticket AS t";
223 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"ticket_extrafields AS ef ON (ef.fk_object = t.rowid)";
224 $sql .=
' WHERE t.entity IN ('.getEntity(
'ticket', 1).
')';
226 $sql .=
" AND t.fk_soc = ".((int) $socid);
229 if ($search_sale && $search_sale !=
'-1') {
230 if ($search_sale == -2) {
231 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
232 } elseif ($search_sale > 0) {
233 $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).
")";
241 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
246 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
248 $sql .= $this->db->order($sortfield, $sortorder);
254 $offset = $limit * $page;
256 $sql .= $this->db->plimit($limit, $offset);
259 $result = $this->db->query($sql);
261 $num = $this->db->num_rows($result);
264 $obj = $this->db->fetch_object($result);
265 $ticket_static =
new Ticket($this->db);
266 if ($ticket_static->fetch($obj->rowid)) {
267 if ($ticket_static->fk_user_assign > 0) {
268 $userStatic =
new User($this->db);
269 $userStatic->fetch($ticket_static->fk_user_assign);
270 $ticket_static->fk_user_assign_string = $userStatic->firstname.
' '.$userStatic->lastname;
277 throw new RestException(503,
'Error when retrieve ticket list');
281 if ($pagination_data) {
282 $totalsResult = $this->db->query($sqlTotals);
283 $total = $this->db->fetch_object($totalsResult)->total;
288 $obj_ret[
'data'] = $tmp;
289 $obj_ret[
'pagination'] = [
290 'total' => (int) $total,
292 'page_count' => ceil((
int) $total / $limit),
306 public function post($request_data =
null)
308 $ticketstatic =
new Ticket($this->db);
309 if (!DolibarrApiAccess::$user->hasRight(
'ticket',
'write')) {
310 throw new RestException(403);
313 $result = $this->
_validate($request_data);
315 foreach ($request_data as $field => $value) {
316 if ($field ===
'caller') {
318 $this->ticket->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
322 $this->ticket->$field = $this->
_checkValForAPI($field, $value, $this->ticket);
324 if (empty($this->ticket->ref)) {
325 $this->ticket->ref = $ticketstatic->getDefaultRef();
327 if (empty($this->ticket->track_id)) {
331 if ($this->ticket->create(DolibarrApiAccess::$user) < 0) {
332 throw new RestException(500,
"Error creating ticket", array_merge(array($this->ticket->error), $this->ticket->errors));
335 return $this->ticket->id;
347 $ticketstatic =
new Ticket($this->db);
348 if (!DolibarrApiAccess::$user->hasRight(
'ticket',
'write')) {
349 throw new RestException(403);
354 foreach ($request_data as $field => $value) {
355 if ($field ===
'caller') {
357 $this->ticket->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
361 $this->ticket->$field = $this->
_checkValForAPI($field, $value, $this->ticket);
363 $ticketMessageText = $this->ticket->message;
364 $result = $this->ticket->fetch(
'',
'', $this->ticket->track_id);
366 throw new RestException(404,
'Ticket not found');
368 $this->ticket->message = $ticketMessageText;
369 if (!$this->ticket->createTicketMessage(DolibarrApiAccess::$user)) {
370 throw new RestException(500,
'Error when creating ticket');
372 return $this->ticket->id;
382 public function put(
$id, $request_data =
null)
384 if (!DolibarrApiAccess::$user->hasRight(
'ticket',
'write')) {
385 throw new RestException(403);
388 $result = $this->ticket->fetch(
$id);
390 throw new RestException(404,
'Ticket not found');
394 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
397 foreach ($request_data as $field => $value) {
398 if ($field ===
'caller') {
400 $this->ticket->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
404 $this->ticket->$field = $this->
_checkValForAPI($field, $value, $this->ticket);
407 if ($this->ticket->update(DolibarrApiAccess::$user) > 0) {
408 return $this->
get(
$id);
410 throw new RestException(500, $this->ticket->error);
421 public function delete(
$id)
423 if (!DolibarrApiAccess::$user->hasRight(
'ticket',
'delete')) {
424 throw new RestException(403);
426 $result = $this->ticket->fetch(
$id);
428 throw new RestException(404,
'Ticket not found');
432 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
435 if (!$this->ticket->delete(DolibarrApiAccess::$user)) {
436 throw new RestException(500,
'Error when deleting ticket');
442 'message' =>
'Ticket deleted'
458 foreach (Tickets::$FIELDS as $field) {
459 if (!isset($data[$field])) {
460 throw new RestException(400,
"$field field missing");
462 $ticket[$field] = $data[$field];
478 foreach (Tickets::$FIELDS_MESSAGES as $field) {
479 if (!isset($data[$field])) {
480 throw new RestException(400,
"$field field missing");
482 $ticket[$field] = $data[$field];
509 "table_element_line",
516 "barcode_type_label",
517 "barcode_type_coder",
521 "fk_delivery_address",
522 "shipping_method_id",
535 "location_incoterms",
543 "cache_types_tickets",
544 "cache_category_tickets",
548 "multicurrency_code",
550 "multicurrency_total_ht",
551 "multicurrency_total_ttc",
552 "multicurrency_total_tva",
553 "multicurrency_total_localtax1",
554 "multicurrency_total_localtax2"
556 foreach ($attr2clean as $toclean) {
562 $nboflines = count(
$object->lines);
563 for ($i = 0; $i < $nboflines; $i++) {
569 if (isset(
$object->linkedObjects) && count(
$object->linkedObjects) > 0) {
570 foreach (
$object->linkedObjects as $type_object => $linked_object) {
571 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.