<?php
/**
* Created by PhpStorm.
* User: michael
* Date: 04/02/2019
* Time: 15:39
*/
namespace App\Controller\Front;
use App\Entity\HearingBrand;
use App\Entity\Prospect;
use App\Entity\Stat\Report;
use DateTime;
use App\Repository\Stat\ReportRepository;
use App\Service\StatService;
use Ob\HighchartsBundle\Highcharts\Highchart;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
class StatController extends AbstractController
{
/**
* @Route("/stat/event/display", name="stat_display_center", methods={"POST"}, options={"expose"=true},)
* @return Response
*/
public function saveDisplayCentersAction(StatService $statService, LoggerInterface $logger, Request $request)
{
if ($request->isXmlHttpRequest()) {
$storesIds = $request->request->get('storesIds');
$statService->saveDisplayStoreEvent($storesIds);
return new JsonResponse();
} else {
throw new NotFoundHttpException();
}
}
/**
* @Route("/stat/event/phone", name="stat_display_phone", methods={"POST"}, options={"expose"=true},)
* @return Response
*/
public function saveDisplayPhoneAction(StatService $statService, LoggerInterface $logger, Request $request)
{
if ($request->isXmlHttpRequest()) {
$storeId = $request->request->get('storeId');
$statService->saveDisplayPhoneEvent($storeId);
return new JsonResponse();
} else {
throw new NotFoundException();
}
}
/**
* @Route("/stat/brand/{token}", name="stat_display_brand_report", methods={"GET"})
* @return Response
*/
public function displayBrandReport(Report $report, ReportRepository $reportRepository, LoggerInterface $logger, Request $request, StatService $statService)
{
//get report for each store of the brand
$storesReports = $reportRepository->findByTypeForStoresOfBrand($report->getType(), $report->getBrand(), $report->getStartDate());
//get the annual report
$annualReport = $reportRepository->findByTypeForBrand(Report::TYPE_YEARLY, $report->getBrand(), $report->getStartDate());
//prepare data for chart
$periodReport = $report;
$dataCheckup = [];
$dataAppointment = [];
$months = [];
while (!is_null($periodReport)) {
array_unshift($dataCheckup, $periodReport->getNbCheckup());
array_unshift($dataAppointment, $periodReport->getNbAppointement());
if ($periodReport->getType() == Report::TYPE_MONTHLY) {
$key = $periodReport->getStartDate()->format('m/y');
} elseif ($periodReport->getType() == Report::TYPE_YEARLY) {
$key = $periodReport->getStartDate()->format('Y');
} else {
$key = $periodReport->getStartDate()->format('d/m/y');
}
array_unshift($months, $key);
$periodReport = $periodReport->getPreviousReport();
}
// Chart
$series = array(
array("name" => "Bilan auditif", "data" => $dataCheckup),
array("name" => "RDV", "data" => $dataAppointment)
);
$ob = new Highchart();
$ob->chart->type('column');
$ob->chart->renderTo('linechart'); // The #id of the div where to render the chart
$ob->title->text('Volume de contacts générés en ' . $report->getStartDate()->format('Y'));
$ob->series($series);
$ob->xAxis->categories($months);
$ob->plotOptions->series(array(
'dataLabels' => array('enabled' => true),
));
return $this->render('stat/report/report_brand.html.twig',
[
'report' => $report,
'annualReport' => $annualReport,
'storesReports' => $storesReports,
'chart' => $ob
]);
}
/**
* @Route("/stat/store/{token}", name="stat_display_store_report", methods={"GET"})
* @return Response
*/
public function displayStoreReport(Report $report, ReportRepository $reportRepository, LoggerInterface $logger, Request $request)
{
//get the annual report
$annualReport = $reportRepository->findByTypeForStore(Report::TYPE_YEARLY, $report->getStore(), $report->getStartDate());
//prepare data for chart
$periodReport = $report;
$dataCheckup = [];
$dataAppointment = [];
$months = [];
while (!is_null($periodReport)) {
array_unshift($dataCheckup, $periodReport->getNbCheckup());
array_unshift($dataAppointment, $periodReport->getNbAppointement());
if ($periodReport->getType() == Report::TYPE_MONTHLY) {
$key = $periodReport->getStartDate()->format('m/y');
} elseif ($periodReport->getType() == Report::TYPE_YEARLY) {
$key = $periodReport->getStartDate()->format('Y');
} else {
$key = $periodReport->getStartDate()->format('d/m/y');
}
array_unshift($months, $key);
$periodReport = $periodReport->getPreviousReport();
}
// Chart
$series = array(
array("name" => "Bilan auditif", "data" => $dataCheckup),
array("name" => "RDV", "data" => $dataAppointment)
);
$ob = new Highchart();
$ob->chart->type('column');
$ob->chart->renderTo('linechart'); // The #id of the div where to render the chart
$ob->title->text('Lead en ' . $report->getStartDate()->format('Y'));
$ob->series($series);
$ob->xAxis->categories($months);
return $this->render('stat/report/report_store.html.twig',
[
'report' => $report,
'annualReport' => $annualReport,
'chart' => $ob
]);
}
}