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.             $from $request->request->get('from');
  91.             $to $request->request->get('to');
  92.             
  93.             $json['ticketsByStatus'] = $this->reportService->ticketsByStatus($from$to);
  94.             $json['ticketsByType'] = $this->reportService->ticketsByType($from$to);
  95.             $json['ticketsByPriority'] = $this->reportService->ticketsByPriority($from$to);
  96.             $json['ticketsPerAgent'] = $this->reportService->ticketsPerAgent($from$to);
  97.             $json['ticketsPerTeam'] = $this->reportService->ticketsPerTeam($from$to);
  98.             $json['kudos'] = $this->reportService->kudos($from$to);
  99.             
  100.             // Nuevas métricas mejoradas
  101.             $json['ticketsByDay'] = $this->reportService->ticketsByDay($from$to);
  102.             $json['resolutionRate'] = $this->reportService->resolutionRate($from$to);
  103.             $json['topAgents'] = $this->reportService->topAgentsByResolved($from$to);
  104.             $json['ticketsByHour'] = $this->reportService->ticketsByHour($from$to);
  105.             $json['topCustomers'] = $this->reportService->topCustomersByTickets($from$to);
  106.             $json['kpiSummary'] = $this->reportService->getKPISummary($from$to);
  107.             $json['ticketsOpenVsClosed'] = $this->reportService->ticketsOpenVsClosedByDay($from$to);
  108.             
  109.             // Métricas específicas del Looker Studio
  110.             $json['agentsWithCount'] = $this->reportService->agentsWithTicketCount($from$to);
  111.             $json['ticketsByTypeStacked'] = $this->reportService->ticketsByTypeStacked($from$to);
  112.             $json['incidentTypes'] = $this->reportService->incidentTypesDistribution($from$to);
  113.             $json['customersWithCount'] = $this->reportService->customersWithTicketCount($from$to);
  114.             $json['ticketsByMonth'] = $this->reportService->ticketsByMonth($from$to);
  115.             $json['closureTimeCategories'] = $this->reportService->ticketClosureTimeCategories($from$to);
  116.             $json['lastTicketDate'] = $this->reportService->getLastTicketDate();
  117.             $json['ticketsLessThan5Days'] = $this->reportService->getTicketsClosedLessThan5Days($from$to);
  118.         }
  119.         return new Response(json_encode($json), 200, ['Content-Type' => 'application/json']);
  120.     }
  121.     public function agentActivityData(Request $request)
  122.     {
  123.         if (!$this->userService->isAccessAuthorized('ROLE_AGENT_MANAGE_AGENT_ACTIVITY')){
  124.             throw new \Exception('Access Denied'403);
  125.         }
  126.         
  127.         $data = [];
  128.         $reportService $this->reportService;
  129.         $reportService->parameters $request->query->all();
  130.         
  131.         // Establecer fechas por defecto si no existen
  132.         $startDate = isset($reportService->parameters['after']) && !empty($reportService->parameters['after']) 
  133.             ? $reportService->parameters['after'
  134.             : date('Y-m-d'strtotime('-7 days'));
  135.         $endDate = isset($reportService->parameters['before']) && !empty($reportService->parameters['before']) 
  136.             ? $reportService->parameters['before'
  137.             : date('Y-m-d');
  138.         $agentIds = [];
  139.         if(isset($reportService->parameters['agent']))
  140.             $agentIds explode(','$reportService->parameters['agent']);
  141.         $userService $this->userService;
  142.         $from $startDate." 00:00:01";
  143.         $to $endDate." 23:59:59";
  144.         $reportService->parameters $request->query->all();
  145.         $qb $reportService->getAgentActivity($agentIds$from$to);
  146.         $paginator  $this->paginator;
  147.         $newQb = clone $qb;
  148.         $results $paginator->paginate(
  149.             $qb->getQuery()->setHydrationMode(Query::HYDRATE_ARRAY)->setHint('knp_paginator.count'count($newQb->getQuery()->getResult())),
  150.             $request->query->get('page') ?: 1,
  151.             20,
  152.             array('distinct' => true)
  153.         );
  154.         $paginationData $results->getPaginationData();
  155.         $queryParameters $results->getParams();
  156.         $queryParameters['page'] = "replacePage";
  157.         $paginationData['url'] = '#'.$this->uvdeskService->buildPaginationQuery($queryParameters);
  158.         $data = array();
  159.         $ticketIds = [];
  160.         $agentActivity = [];
  161.         $agentIds = [];
  162.         foreach ($results as $key => $activity) {
  163.             $ticket $this->getDoctrine()->getManager()->getRepository('UVDeskCoreFrameworkBundle:Ticket')->findOneById($activity['id']);
  164.             $currentDateTime  = new \DateTime('now');
  165.             $activityDateTime $activity['createdAt'];
  166.             $difference $currentDateTime->getTimeStamp() - $activityDateTime->getTimeStamp();
  167.             $lastReply $reportService->time2string($difference);
  168.             $ticketViewURL $this->get('router')->generate('helpdesk_member_ticket', ['ticketId' => $activity['ticketId']], UrlGeneratorInterface::ABSOLUTE_URL);
  169.             $data[] =   [
  170.                 'priorityDesc' => $this->get('translator')->trans($activity['priorityDesc']),
  171.                 'id' => $activity['id'],
  172.                 'ticketURL' => $ticketViewURL,
  173.                 'ticketId' => $activity['ticketId'],
  174.                 'subject' => $activity['subject'],
  175.                 'color'   => $activity['colorCode'],
  176.                 'customerName'=> $activity['customerName'],
  177.                 'threadType' => $activity['threadType'],
  178.                 'lastReply'  => $lastReply,
  179.                 'agentName'  => $activity['agentName']
  180.             ];
  181.             array_push($ticketIds$activity['ticketId']);
  182.             array_push($agentIds$activity['agentId']);
  183.         }
  184.         $threadDetails $reportService->getTotalReplies(array_unique($ticketIds), $agentIds$from$to);
  185.         foreach ($data as $index => $ticketDetail) {
  186.             foreach ($threadDetails as $detail) {
  187.                 if ($detail['ticketId'] == $ticketDetail['ticketId']) {
  188.                     $data[$index]['totalReply'] = $detail['ticketCount'];
  189.                 }
  190.             }
  191.         }
  192.         $agentActivity = [];
  193.         $agentActivity['data'] = $data;
  194.         $agentActivity['pagination_data'] = $paginationData;
  195.         
  196.         // Pasar los filtros de agente y fechas al método getAgentsActivityLogin
  197.         $agentId = isset($reportService->parameters['agent']) ? $reportService->parameters['agent'] : null;
  198.         $agentActivity['agentsLogin'] = $this->userService->getAgentsActivityLogin(null$agentId$from$to);
  199.         return $agentActivity;
  200.     }
  201.     public function achievementInsightsAction()
  202.     {
  203.         $this->userService->forceFormat true;
  204.         $startDate $this->userService->convertToTimezone(new \DateTime("-7 days"), 'Y-m-d');
  205.         $endDate $this->userService->convertToTimezone(new \DateTime("now"), 'Y-m-d');
  206.         $this->userService->forceFormat false;
  207.         return $this->render('@UVDeskCoreFramework/Reports/kudos-insights.html.twig',array(
  208.                 'startDate' => $startDate,
  209.                 'endDate' => $endDate
  210.             )
  211.         );
  212.     }
  213.     public function getAchievementsXhr(Request $request)
  214.     {
  215.         $json = array();
  216.         if( $request->isXmlHttpRequest()) {
  217.             $repository $this->getDoctrine()->getRepository('UVDeskCoreFrameworkBundle:TicketRating');
  218.             $json $repository->getRatedTicketList($request->query$this->container);
  219.             $json['data'] = $this->getAchievementsData($request);
  220.         }
  221.         $response = new Response(json_encode($json));
  222.        
  223.         $response->headers->set('Content-Type''application/json');
  224.         return $response;
  225.     }
  226.     public function getAchievementsData($request)
  227.     {
  228.         $data = array();
  229.         $reportService $this->get('report.service');
  230.         $reportService->parameters $request->query->all();
  231.         $startDate $reportService->parameters['start'];
  232.         $endDate $reportService->parameters['end'];
  233.         $userService $this->get('user.service');
  234.         $reportService->startDate $this->userService->convertToTimezone(new \DateTime($startDate),'Y-m-d H:i:s');
  235.         $reportService->endDate $this->userService->convertToTimezone(new \DateTime($endDate),'Y-m-d H:i:s');
  236.         $repository $this->getDoctrine()->getRepository('UVDeskCoreFrameworkBundle:TicketRating');
  237.         $data =  $repository->getRatingData($request->query$this->container);
  238.         for ($i 1$i <= 5$i++) {
  239.             $data['ratings'][$i] = $repository->getRatingByStarCount($request->query$i$this->container);
  240.         }
  241.         return $data;
  242.     }
  243. }