18 use Luracast\Restler\RestException;
20 require_once DOL_DOCUMENT_ROOT.
'/ticket/class/ticket.class.php';
21 require_once DOL_DOCUMENT_ROOT.
'/core/lib/ticket.lib.php';
35 public static $FIELDS = array(
43 public static $FIELDS_MESSAGES = array(
60 $this->ticket =
new Ticket($this->
db);
75 public function get($id)
96 return $this->
getCommon(0, $track_id,
'');
131 private function getCommon($id = 0, $track_id =
'', $ref =
'')
133 if (!DolibarrApiAccess::$user->rights->ticket->read) {
134 throw new RestException(403);
138 if (($id < 0) && !$track_id && !$ref) {
139 throw new RestException(401,
'Wrong parameters');
142 $result = $this->ticket->initAsSpecimen();
144 $result = $this->ticket->fetch($id, $ref, $track_id);
147 throw new RestException(404,
'Ticket not found');
151 if ($this->ticket->fk_user_assign > 0) {
152 $userStatic =
new User($this->
db);
153 $userStatic->fetch($this->ticket->fk_user_assign);
154 $this->ticket->fk_user_assign_string = $userStatic->firstname.
' '.$userStatic->lastname;
159 $this->ticket->loadCacheMsgsTicket();
160 if (is_array($this->ticket->cache_msgs_ticket) && count($this->ticket->cache_msgs_ticket) > 0) {
161 $num = count($this->ticket->cache_msgs_ticket);
164 if ($this->ticket->cache_msgs_ticket[$i][
'fk_user_author'] > 0) {
165 $user_action =
new User($this->
db);
166 $user_action->fetch($this->ticket->cache_msgs_ticket[$i][
'fk_user_author']);
171 'id' => $this->ticket->cache_msgs_ticket[$i][
'id'],
172 'fk_user_action' => $this->ticket->cache_msgs_ticket[$i][
'fk_user_author'],
173 'fk_user_action_socid' => $user_action->socid,
174 'fk_user_action_string' =>
dolGetFirstLastname($user_action->firstname, $user_action->lastname),
175 'message' => $this->ticket->cache_msgs_ticket[$i][
'message'],
176 'datec' => $this->ticket->cache_msgs_ticket[$i][
'datec'],
177 'private' => $this->ticket->cache_msgs_ticket[$i][
'private']
181 $this->ticket->messages = $messages;
186 $this->ticket->loadCacheLogsTicket();
187 if (is_array($this->ticket->cache_logs_ticket) && count($this->ticket->cache_logs_ticket) > 0) {
188 $num = count($this->ticket->cache_logs_ticket);
191 if ($this->ticket->cache_logs_ticket[$i][
'fk_user_create'] > 0) {
192 $user_action =
new User($this->
db);
193 $user_action->fetch($this->ticket->cache_logs_ticket[$i][
'fk_user_create']);
198 'id' => $this->ticket->cache_logs_ticket[$i][
'id'],
199 'fk_user_author' => $this->ticket->cache_msgs_ticket[$i][
'fk_user_author'],
200 'fk_user_action' => $this->ticket->cache_logs_ticket[$i][
'fk_user_create'],
201 'fk_user_action_string' =>
dolGetFirstLastname($user_action->firstname, $user_action->lastname),
202 'message' => $this->ticket->cache_logs_ticket[$i][
'message'],
203 'datec' => $this->ticket->cache_logs_ticket[$i][
'datec'],
207 $this->ticket->history = $history;
211 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
231 public function index($socid = 0, $sortfield =
"t.rowid", $sortorder =
"ASC", $limit = 100, $page = 0, $sqlfilters =
'')
235 if (!DolibarrApiAccess::$user->rights->ticket->read) {
236 throw new RestException(403);
241 if (!$socid && DolibarrApiAccess::$user->socid) {
242 $socid = DolibarrApiAccess::$user->socid;
247 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
248 $search_sale = DolibarrApiAccess::$user->id;
251 $sql =
"SELECT t.rowid";
252 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
253 $sql .=
", sc.fk_soc, sc.fk_user";
255 $sql .=
" FROM ".MAIN_DB_PREFIX.
"ticket as t";
257 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
258 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
261 $sql .=
' WHERE t.entity IN ('.getEntity(
'ticket', 1).
')';
262 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
263 $sql .=
" AND t.fk_soc = sc.fk_soc";
266 $sql .=
" AND t.fk_soc = ".((int) $socid);
268 if ($search_sale > 0) {
269 $sql .=
" AND t.rowid = sc.fk_soc";
273 if ($search_sale > 0) {
274 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
280 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
282 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
283 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
286 $sql .= $this->
db->order($sortfield, $sortorder);
292 $offset = $limit * $page;
294 $sql .= $this->
db->plimit($limit, $offset);
297 $result = $this->
db->query($sql);
299 $num = $this->
db->num_rows($result);
302 $obj = $this->
db->fetch_object($result);
303 $ticket_static =
new Ticket($this->
db);
304 if ($ticket_static->fetch($obj->rowid)) {
305 if ($ticket_static->fk_user_assign > 0) {
306 $userStatic =
new User($this->
db);
307 $userStatic->fetch($ticket_static->fk_user_assign);
308 $ticket_static->fk_user_assign_string = $userStatic->firstname.
' '.$userStatic->lastname;
315 throw new RestException(503,
'Error when retrieve ticket list');
317 if (!count($obj_ret)) {
318 throw new RestException(404,
'No ticket found');
329 public function post($request_data =
null)
331 $ticketstatic =
new Ticket($this->
db);
332 if (!DolibarrApiAccess::$user->rights->ticket->write) {
333 throw new RestException(401);
336 $result = $this->
_validate($request_data);
338 foreach ($request_data as $field => $value) {
339 $this->ticket->$field = $value;
341 if (empty($this->ticket->ref)) {
342 $this->ticket->ref = $ticketstatic->getDefaultRef();
344 if (empty($this->ticket->track_id)) {
348 if ($this->ticket->create(DolibarrApiAccess::$user) < 0) {
349 throw new RestException(500,
"Error creating ticket", array_merge(array($this->ticket->error), $this->ticket->errors));
352 return $this->ticket->id;
364 $ticketstatic =
new Ticket($this->
db);
365 if (!DolibarrApiAccess::$user->rights->ticket->write) {
366 throw new RestException(401);
371 foreach ($request_data as $field => $value) {
372 $this->ticket->$field = $value;
374 $ticketMessageText = $this->ticket->message;
375 $result = $this->ticket->fetch(
'',
'', $this->ticket->track_id);
377 throw new RestException(404,
'Ticket not found');
379 $this->ticket->message = $ticketMessageText;
380 if (!$this->ticket->createTicketMessage(DolibarrApiAccess::$user)) {
381 throw new RestException(500,
'Error when creating ticket');
383 return $this->ticket->id;
394 public function put($id, $request_data =
null)
396 if (!DolibarrApiAccess::$user->rights->ticket->write) {
397 throw new RestException(401);
400 $result = $this->ticket->fetch($id);
402 throw new RestException(404,
'Ticket not found');
406 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
409 foreach ($request_data as $field => $value) {
410 $this->ticket->$field = $value;
413 if ($this->ticket->update($id, DolibarrApiAccess::$user)) {
414 return $this->
get($id);
427 public function delete($id)
429 if (!DolibarrApiAccess::$user->rights->ticket->delete) {
430 throw new RestException(401);
432 $result = $this->ticket->fetch($id);
434 throw new RestException(404,
'Ticket not found');
438 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
441 if (!$this->ticket->delete($id)) {
442 throw new RestException(500,
'Error when deleting ticket');
448 'message' =>
'Ticket deleted'
464 foreach (Tickets::$FIELDS as $field) {
465 if (!isset($data[$field])) {
466 throw new RestException(400,
"$field field missing");
468 $ticket[$field] = $data[$field];
484 foreach (Tickets::$FIELDS_MESSAGES as $field) {
485 if (!isset($data[$field])) {
486 throw new RestException(400,
"$field field missing");
488 $ticket[$field] = $data[$field];
506 $object = parent::_cleanObjectDatas($object);
515 "table_element_line",
522 "barcode_type_label",
523 "barcode_type_coder",
527 "fk_delivery_address",
528 "shipping_method_id",
541 "location_incoterms",
549 "cache_types_tickets",
550 "cache_category_tickets",
555 foreach ($attr2clean as $toclean) {
556 unset($object->$toclean);
560 if (isset($object->lines) && count($object->lines) > 0) {
561 $nboflines = count($object->lines);
562 for ($i = 0; $i < $nboflines; $i++) {
568 if (isset($object->linkedObjects) && count($object->linkedObjects) > 0) {
569 foreach ($object->linkedObjects as $type_object => $linked_object) {
570 foreach ($linked_object as $object2clean) {