vendor/uvdesk/core-framework/Controller/Report.php line 52

Open in your IDE?
  1. <?php
  2. namespace Webkul\UVDesk\CoreFrameworkBundle\Controller;
  3. use Webkul\UVDesk\CoreFrameworkBundle\Entity;
  4. use Webkul\UVDesk\CoreFrameworkBundle\Form;
  5. use Webkul\UVDesk\CoreFrameworkBundle\Entity\User;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Webkul\UVDesk\CoreFrameworkBundle\Entity\SupportGroup;
  9. use Webkul\UVDesk\CoreFrameworkBundle\Entity\SupportTeam;
  10. use Webkul\UVDesk\CoreFrameworkBundle\Entity\UserInstance;
  11. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  12. use Webkul\UVDesk\CoreFrameworkBundle\Services\UserService;
  13. use Webkul\UVDesk\CoreFrameworkBundle\Services\UVDeskService;
  14. use Webkul\UVDesk\CoreFrameworkBundle\Services\ReportService;
  15. use Symfony\Component\Translation\TranslatorInterface;
  16. use Knp\Component\Pager\PaginatorInterface;
  17. use Doctrine\ORM\Query;
  18. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  19. class Report extends Controller
  20. {
  21.     private $userService;
  22.     private $reportService;
  23.     private $uvdeskService;
  24.     private $ticketService;
  25.     private $paginator;
  26.     private $translator;
  27.     public function __construct(UserService $userServiceUVDeskService $uvdeskService,ReportService $reportServicePaginatorInterface $paginatorTranslatorInterface $translator)
  28.     {
  29.         $this->userService $userService;
  30.         $this->reportService $reportService;
  31.         $this->uvdeskService $uvdeskService;
  32.         //$this->ticketService = $ticketService;
  33.         $this->paginator $paginator;
  34.         $this->translator $translator;
  35.     }
  36.     public function listAgentActivity(Request $request)
  37.     {
  38.         if (!$this->userService->isAccessAuthorized('ROLE_AGENT_MANAGE_AGENT_ACTIVITY')){
  39.             return $this->redirect($this->generateUrl('helpdesk_member_dashboard'));
  40.         }
  41.         return $this->render('@UVDeskCoreFramework/Reports/listAgentActivities.html.twig', [
  42.             'agents' => $this->userService->getAgentsPartialDetails(),
  43.         ]);
  44.     }
  45.     public function agentActivityXHR(Request $request)
  46.     {
  47.         $json = [];
  48.         if ($request->isXmlHttpRequest()) {
  49.             $json $this->agentActivityData($request);
  50.         }
  51.         return new Response(json_encode($json), 200, ['Content-Type' => 'application/json']);
  52.     }
  53.     public function listTicketsInsights(Request $request)
  54.     {
  55.         if (!$this->userService->isAccessAuthorized('ROLE_AGENT_MANAGE_AGENT_ACTIVITY')){
  56.             return $this->redirect($this->generateUrl('helpdesk_member_dashboard'));
  57.         }
  58.         return $this->render('@UVDeskCoreFramework/Reports/tickets-insights.html.twig', [
  59.             'tickets' => $this->reportService->getNewTicketsReport(),
  60.             'customers' => $this->reportService->getCustomersReport(),
  61.             'ticketsActivity' => $this->reportService->getTicketsReport(),
  62.             'ticketsResolved' => $this->reportService->getTicketsResolved(),
  63.             'avgReply' => $this->reportService->getAvgReply(),
  64.             'avgTicketsPerAgent' => $this->reportService->getAvgTicketsPerAgent(),
  65.             'avgResolution' => $this->reportService->getAvgResolution(),
  66.             'avgFirstReply' => $this->reportService->getAvgFirstReply(),
  67.             'fastestReply' => $this->reportService->getFastestReply(),
  68.         ]);
  69.     }
  70.     public function ticketsInsightsXHR(Request $request)
  71.     {
  72.         $json = [];
  73.         if ($request->isXmlHttpRequest()) {
  74.             $json[] = $this->reportService->getNewTicketsReportXHR($request->request->get('from'), $request->request->get('to'));
  75.             $json[] = $this->reportService->getCustomersReportXHR($request->request->get('from'), $request->request->get('to'));
  76.             $json[] = $this->reportService->getTicketsReportXHR($request->request->get('from'), $request->request->get('to'));
  77.             $json['ticketsResolved'] = $this->reportService->getTicketsResolvedXHR($request->request->get('from'), $request->request->get('to'));
  78.             $json['avgReply'] = $this->reportService->getAvgReplyXHR($request->request->get('from'), $request->request->get('to'));
  79.             $json['avgTicketsPerAgent'] = $this->reportService->getAvgTicketsPerAgentXHR($request->request->get('from'), $request->request->get('to'));
  80.             $json['avgResolution'] = $this->reportService->getAvgResolutionXHR($request->request->get('from'), $request->request->get('to'));
  81.             $json['avgFirstReply'] = $this->reportService->getAvgFirstReplyXHR($request->request->get('from'), $request->request->get('to'));
  82.             $json['fastestReply'] = $this->reportService->getFastestReplyXHR($request->request->get('from'), $request->request->get('to'));
  83.         }
  84.         return new Response(json_encode($json), 200, ['Content-Type' => 'application/json']);
  85.     }
  86.     public function chartsXHR(Request $request)
  87.     {
  88.         $json = [];
  89.         if ($request->isXmlHttpRequest()) {
  90.             $json['ticketsByStatus'] = $this->reportService->ticketsByStatus($request->request->get('from'), $request->request->get('to'));
  91.             $json['ticketsByType'] = $this->reportService->ticketsByType($request->request->get('from'), $request->request->get('to'));
  92.             $json['ticketsByPriority'] = $this->reportService->ticketsByPriority($request->request->get('from'), $request->request->get('to'));
  93.             $json['ticketsPerAgent'] = $this->reportService->ticketsPerAgent($request->request->get('from'), $request->request->get('to'));
  94.             $json['ticketsPerTeam'] = $this->reportService->ticketsPerTeam($request->request->get('from'), $request->request->get('to'));
  95.             $json['kudos'] = $this->reportService->kudos($request->request->get('from'), $request->request->get('to'));
  96.         }
  97.         return new Response(json_encode($json), 200, ['Content-Type' => 'application/json']);
  98.     }
  99.     public function agentActivityData(Request $request)
  100.     {
  101.         if (!$this->userService->isAccessAuthorized('ROLE_AGENT_MANAGE_AGENT_ACTIVITY')){
  102.             throw new \Exception('Access Denied'403);
  103.         }
  104.         
  105.         $data = [];
  106.         $reportService $this->reportService;
  107.         $reportService->parameters $request->query->all();
  108.           // Establecer fechas por defecto si no existen
  109.         $startDate = isset($reportService->parameters['after']) && !empty($reportService->parameters['after']) 
  110.             ? $reportService->parameters['after'
  111.             : date('Y-m-d'strtotime('-7 days'));
  112.         $endDate = isset($reportService->parameters['before']) && !empty($reportService->parameters['before']) 
  113.             ? $reportService->parameters['before'
  114.             : date('Y-m-d');
  115.         $agentIds = [];
  116.         if(isset($reportService->parameters['agent']))
  117.             $agentIds explode(','$reportService->parameters['agent']);
  118.         $userService $this->userService;
  119.         $from $startDate." 00:00:01";
  120.         $to $endDate." 23:59:59";
  121.         $reportService->parameters $request->query->all();
  122.         $qb $reportService->getAgentActivity($agentIds$from$to);
  123.         $paginator  $this->paginator;
  124.         $newQb = clone $qb;
  125.         $results $paginator->paginate(
  126.             $qb->getQuery()->setHydrationMode(Query::HYDRATE_ARRAY)->setHint('knp_paginator.count'count($newQb->getQuery()->getResult())),
  127.             $request->query->get('page') ?: 1,
  128.             20,
  129.             array('distinct' => true)
  130.         );
  131.         $paginationData $results->getPaginationData();
  132.         $queryParameters $results->getParams();
  133.         $queryParameters['page'] = "replacePage";
  134.         $paginationData['url'] = '#'.$this->uvdeskService->buildPaginationQuery($queryParameters);
  135.         $data = array();
  136.         $ticketIds = [];
  137.         $agentActivity = [];
  138.         $agentIds = [];
  139.         foreach ($results as $key => $activity) {
  140.             $ticket $this->getDoctrine()->getManager()->getRepository('UVDeskCoreFrameworkBundle:Ticket')->findOneById($activity['id']);
  141.             $currentDateTime  = new \DateTime('now');
  142.             $activityDateTime $activity['createdAt'];
  143.             $difference $currentDateTime->getTimeStamp() - $activityDateTime->getTimeStamp();
  144.             $lastReply $reportService->time2string($difference);
  145.             $ticketViewURL $this->get('router')->generate('helpdesk_member_ticket', ['ticketId' => $activity['ticketId']], UrlGeneratorInterface::ABSOLUTE_URL);
  146.             $data[] =   [
  147.                 'priorityDesc' => $this->get('translator')->trans($activity['priorityDesc']),
  148.                 'id' => $activity['id'],
  149.                 'ticketURL' => $ticketViewURL,
  150.                 'ticketId' => $activity['ticketId'],
  151.                 'subject' => $activity['subject'],
  152.                 'color'   => $activity['colorCode'],
  153.                 'customerName'=> $activity['customerName'],
  154.                 'threadType' => $activity['threadType'],
  155.                 'lastReply'  => $lastReply,
  156.                 'agentName'  => $activity['agentName']
  157.             ];
  158.             array_push($ticketIds$activity['ticketId']);
  159.             array_push($agentIds$activity['agentId']);
  160.         }
  161.         $threadDetails $reportService->getTotalReplies(array_unique($ticketIds), $agentIds$from$to);
  162.         foreach ($data as $index => $ticketDetail) {
  163.             foreach ($threadDetails as $detail) {
  164.                 if ($detail['ticketId'] == $ticketDetail['ticketId']) {
  165.                     $data[$index]['totalReply'] = $detail['ticketCount'];
  166.                 }
  167.             }
  168.         }
  169.         $agentActivity = [];
  170.         $agentActivity['data'] = $data;
  171.         $agentActivity['pagination_data'] = $paginationData;
  172.         
  173.         // Pasar los filtros de agente y fechas al método getAgentsActivityLogin
  174.         $agentId = isset($reportService->parameters['agent']) ? $reportService->parameters['agent'] : null;
  175.         $agentActivity['agentsLogin'] = $this->userService->getAgentsActivityLogin(null$agentId$from$to);
  176.         return $agentActivity;
  177.     }
  178.     public function achievementInsightsAction()
  179.     {
  180.         $this->userService->forceFormat true;
  181.         $startDate $this->userService->convertToTimezone(new \DateTime("-7 days"), 'Y-m-d');
  182.         $endDate $this->userService->convertToTimezone(new \DateTime("now"), 'Y-m-d');
  183.         $this->userService->forceFormat false;
  184.         return $this->render('@UVDeskCoreFramework/Reports/kudos-insights.html.twig',array(
  185.                 'startDate' => $startDate,
  186.                 'endDate' => $endDate
  187.             )
  188.         );
  189.     }
  190.     public function getAchievementsXhr(Request $request)
  191.     {
  192.         $json = array();
  193.         if( $request->isXmlHttpRequest()) {
  194.             $repository $this->getDoctrine()->getRepository('UVDeskCoreFrameworkBundle:TicketRating');
  195.             $json $repository->getRatedTicketList($request->query$this->container);
  196.             $json['data'] = $this->getAchievementsData($request);
  197.         }
  198.         $response = new Response(json_encode($json));
  199.        
  200.         $response->headers->set('Content-Type''application/json');
  201.         return $response;
  202.     }
  203.     public function getAchievementsData($request)
  204.     {
  205.         $data = array();
  206.         $reportService $this->get('report.service');
  207.         $reportService->parameters $request->query->all();
  208.         $startDate $reportService->parameters['start'];
  209.         $endDate $reportService->parameters['end'];
  210.         $userService $this->get('user.service');
  211.         $reportService->startDate $this->userService->convertToTimezone(new \DateTime($startDate),'Y-m-d H:i:s');
  212.         $reportService->endDate $this->userService->convertToTimezone(new \DateTime($endDate),'Y-m-d H:i:s');
  213.         $repository $this->getDoctrine()->getRepository('UVDeskCoreFrameworkBundle:TicketRating');
  214.         $data =  $repository->getRatingData($request->query$this->container);
  215.         for ($i 1$i <= 5$i++) {
  216.             $data['ratings'][$i] = $repository->getRatingByStarCount($request->query$i$this->container);
  217.         }
  218.         return $data;
  219.     }
  220. }