<?php
namespace Webkul\UVDesk\CoreFrameworkBundle\Controller;
use Webkul\UVDesk\CoreFrameworkBundle\Entity;
use Webkul\UVDesk\CoreFrameworkBundle\Form;
use Webkul\UVDesk\CoreFrameworkBundle\Entity\User;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Webkul\UVDesk\CoreFrameworkBundle\Entity\SupportGroup;
use Webkul\UVDesk\CoreFrameworkBundle\Entity\SupportTeam;
use Webkul\UVDesk\CoreFrameworkBundle\Entity\UserInstance;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Webkul\UVDesk\CoreFrameworkBundle\Services\UserService;
use Webkul\UVDesk\CoreFrameworkBundle\Services\UVDeskService;
use Webkul\UVDesk\CoreFrameworkBundle\Services\ReportService;
use Symfony\Component\Translation\TranslatorInterface;
use Knp\Component\Pager\PaginatorInterface;
use Doctrine\ORM\Query;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class Report extends Controller
{
private $userService;
private $reportService;
private $uvdeskService;
private $ticketService;
private $paginator;
private $translator;
public function __construct(UserService $userService, UVDeskService $uvdeskService,ReportService $reportService, PaginatorInterface $paginator, TranslatorInterface $translator)
{
$this->userService = $userService;
$this->reportService = $reportService;
$this->uvdeskService = $uvdeskService;
//$this->ticketService = $ticketService;
$this->paginator = $paginator;
$this->translator = $translator;
}
public function listAgentActivity(Request $request)
{
if (!$this->userService->isAccessAuthorized('ROLE_AGENT_MANAGE_AGENT_ACTIVITY')){
return $this->redirect($this->generateUrl('helpdesk_member_dashboard'));
}
return $this->render('@UVDeskCoreFramework/Reports/listAgentActivities.html.twig', [
'agents' => $this->userService->getAgentsPartialDetails(),
]);
}
public function agentActivityXHR(Request $request)
{
$json = [];
if ($request->isXmlHttpRequest()) {
$json = $this->agentActivityData($request);
}
return new Response(json_encode($json), 200, ['Content-Type' => 'application/json']);
}
public function listTicketsInsights(Request $request)
{
if (!$this->userService->isAccessAuthorized('ROLE_AGENT_MANAGE_AGENT_ACTIVITY')){
return $this->redirect($this->generateUrl('helpdesk_member_dashboard'));
}
return $this->render('@UVDeskCoreFramework/Reports/tickets-insights.html.twig', [
'tickets' => $this->reportService->getNewTicketsReport(),
'customers' => $this->reportService->getCustomersReport(),
'ticketsActivity' => $this->reportService->getTicketsReport(),
'ticketsResolved' => $this->reportService->getTicketsResolved(),
'avgReply' => $this->reportService->getAvgReply(),
'avgTicketsPerAgent' => $this->reportService->getAvgTicketsPerAgent(),
'avgResolution' => $this->reportService->getAvgResolution(),
'avgFirstReply' => $this->reportService->getAvgFirstReply(),
'fastestReply' => $this->reportService->getFastestReply(),
]);
}
public function ticketsInsightsXHR(Request $request)
{
$json = [];
if ($request->isXmlHttpRequest()) {
$json[] = $this->reportService->getNewTicketsReportXHR($request->request->get('from'), $request->request->get('to'));
$json[] = $this->reportService->getCustomersReportXHR($request->request->get('from'), $request->request->get('to'));
$json[] = $this->reportService->getTicketsReportXHR($request->request->get('from'), $request->request->get('to'));
$json['ticketsResolved'] = $this->reportService->getTicketsResolvedXHR($request->request->get('from'), $request->request->get('to'));
$json['avgReply'] = $this->reportService->getAvgReplyXHR($request->request->get('from'), $request->request->get('to'));
$json['avgTicketsPerAgent'] = $this->reportService->getAvgTicketsPerAgentXHR($request->request->get('from'), $request->request->get('to'));
$json['avgResolution'] = $this->reportService->getAvgResolutionXHR($request->request->get('from'), $request->request->get('to'));
$json['avgFirstReply'] = $this->reportService->getAvgFirstReplyXHR($request->request->get('from'), $request->request->get('to'));
$json['fastestReply'] = $this->reportService->getFastestReplyXHR($request->request->get('from'), $request->request->get('to'));
}
return new Response(json_encode($json), 200, ['Content-Type' => 'application/json']);
}
public function chartsXHR(Request $request)
{
$json = [];
if ($request->isXmlHttpRequest()) {
$json['ticketsByStatus'] = $this->reportService->ticketsByStatus($request->request->get('from'), $request->request->get('to'));
$json['ticketsByType'] = $this->reportService->ticketsByType($request->request->get('from'), $request->request->get('to'));
$json['ticketsByPriority'] = $this->reportService->ticketsByPriority($request->request->get('from'), $request->request->get('to'));
$json['ticketsPerAgent'] = $this->reportService->ticketsPerAgent($request->request->get('from'), $request->request->get('to'));
$json['ticketsPerTeam'] = $this->reportService->ticketsPerTeam($request->request->get('from'), $request->request->get('to'));
$json['kudos'] = $this->reportService->kudos($request->request->get('from'), $request->request->get('to'));
}
return new Response(json_encode($json), 200, ['Content-Type' => 'application/json']);
}
public function agentActivityData(Request $request)
{
if (!$this->userService->isAccessAuthorized('ROLE_AGENT_MANAGE_AGENT_ACTIVITY')){
throw new \Exception('Access Denied', 403);
}
$data = [];
$reportService = $this->reportService;
$reportService->parameters = $request->query->all();
$startDate = $reportService->parameters['after'];
$endDate = $reportService->parameters['before'];
$agentIds = [];
if(isset($reportService->parameters['agent']))
$agentIds = explode(',', $reportService->parameters['agent']);
$userService = $this->userService;
$from = $startDate." 00:00:01";
$to = $endDate." 23:59:59";
$reportService->parameters = $request->query->all();
$qb = $reportService->getAgentActivity($agentIds, $from, $to);
$paginator = $this->paginator;
$newQb = clone $qb;
$results = $paginator->paginate(
$qb->getQuery()->setHydrationMode(Query::HYDRATE_ARRAY)->setHint('knp_paginator.count', count($newQb->getQuery()->getResult())),
$request->query->get('page') ?: 1,
20,
array('distinct' => true)
);
$paginationData = $results->getPaginationData();
$queryParameters = $results->getParams();
$queryParameters['page'] = "replacePage";
$paginationData['url'] = '#'.$this->uvdeskService->buildPaginationQuery($queryParameters);
$data = array();
$ticketIds = [];
$agentActivity = [];
$agentIds = [];
foreach ($results as $key => $activity) {
$ticket = $this->getDoctrine()->getManager()->getRepository('UVDeskCoreFrameworkBundle:Ticket')->findOneById($activity['id']);
$currentDateTime = new \DateTime('now');
$activityDateTime = $activity['createdAt'];
$difference = $currentDateTime->getTimeStamp() - $activityDateTime->getTimeStamp();
$lastReply = $reportService->time2string($difference);
$ticketViewURL = $this->get('router')->generate('helpdesk_member_ticket', ['ticketId' => $activity['ticketId']], UrlGeneratorInterface::ABSOLUTE_URL);
$data[] = [
'priorityDesc' => $this->get('translator')->trans($activity['priorityDesc']),
'id' => $activity['id'],
'ticketURL' => $ticketViewURL,
'ticketId' => $activity['ticketId'],
'subject' => $activity['subject'],
'color' => $activity['colorCode'],
'customerName'=> $activity['customerName'],
'threadType' => $activity['threadType'],
'lastReply' => $lastReply,
'agentName' => $activity['agentName']
];
array_push($ticketIds, $activity['ticketId']);
array_push($agentIds, $activity['agentId']);
}
$threadDetails = $reportService->getTotalReplies(array_unique($ticketIds), $agentIds, $from, $to);
foreach ($data as $index => $ticketDetail) {
foreach ($threadDetails as $detail) {
if ($detail['ticketId'] == $ticketDetail['ticketId']) {
$data[$index]['totalReply'] = $detail['ticketCount'];
}
}
}
$agentActivity = [];
$agentActivity['data'] = $data;
$agentActivity['pagination_data'] = $paginationData;
$agentActivity['agentsLogin'] = $this->userService->getAgentsActivityLogin();
return $agentActivity;
}
public function achievementInsightsAction()
{
$this->userService->forceFormat = true;
$startDate = $this->userService->convertToTimezone(new \DateTime("-7 days"), 'Y-m-d');
$endDate = $this->userService->convertToTimezone(new \DateTime("now"), 'Y-m-d');
$this->userService->forceFormat = false;
return $this->render('@UVDeskCoreFramework/Reports/kudos-insights.html.twig',array(
'startDate' => $startDate,
'endDate' => $endDate
)
);
}
public function getAchievementsXhr(Request $request)
{
$json = array();
if( $request->isXmlHttpRequest()) {
$repository = $this->getDoctrine()->getRepository('UVDeskCoreFrameworkBundle:TicketRating');
$json = $repository->getRatedTicketList($request->query, $this->container);
$json['data'] = $this->getAchievementsData($request);
}
$response = new Response(json_encode($json));
$response->headers->set('Content-Type', 'application/json');
return $response;
}
public function getAchievementsData($request)
{
$data = array();
$reportService = $this->get('report.service');
$reportService->parameters = $request->query->all();
$startDate = $reportService->parameters['start'];
$endDate = $reportService->parameters['end'];
$userService = $this->get('user.service');
$reportService->startDate = $this->userService->convertToTimezone(new \DateTime($startDate),'Y-m-d H:i:s');
$reportService->endDate = $this->userService->convertToTimezone(new \DateTime($endDate),'Y-m-d H:i:s');
$repository = $this->getDoctrine()->getRepository('UVDeskCoreFrameworkBundle:TicketRating');
$data = $repository->getRatingData($request->query, $this->container);
for ($i = 1; $i <= 5; $i++) {
$data['ratings'][$i] = $repository->getRatingByStarCount($request->query, $i, $this->container);
}
return $data;
}
}