<?php
namespace App\Controller;
use App\Form\PaymentStatisticFormType;
use App\Gateway\BiReport\PaymentStatisticGateway;
use App\Report\ReportOlap;
use Doctrine\DBAL\Connection;
use Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class PaymentStatisticController extends AbstractController
{
/**
* @Route("/payment-statistic", name="payment_statistic")
* @throws Exception
*/
public function paymentStatistic(Request $request, Connection $connection): Response
{
$form = $this->get('form.factory')->createNamed('', PaymentStatisticFormType::class, null, [
'clear_group_by_default' => !! $request->get('groupBy'),
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$groupingFields = $data['groupBy'];
$paymentStatisticGateway = new PaymentStatisticGateway($connection);
$userStat = $paymentStatisticGateway->getUserStat(
$data['dateFrom'],
$data['dateTo'],
$data['timeGroup'],
$data['groupBy'],
$data['affiliatePlatform'],
$data['utmSource']
);
$transactionsStat = $paymentStatisticGateway->getTransactionsStat(
$data['additionalDateFrom'],
$data['additionalDateTo'],
$data['dateFrom'],
$data['dateTo'],
$data['filterTransactionsByUser'],
$data['timeGroup'],
$data['groupBy'],
$data['affiliatePlatform'],
$data['utmSource']
);
$eventsStat = $paymentStatisticGateway->getEventsStat(
$data['additionalDateFrom'],
$data['additionalDateTo'],
$data['dateFrom'],
$data['dateTo'],
$data['filterTransactionsByUser'],
$data['timeGroup'],
$data['groupBy'],
$data['affiliatePlatform'],
$data['utmSource']
);
$rows = $this->getUniqueByKeysFromStats([$userStat, $transactionsStat], $groupingFields);
$olap = new ReportOlap($rows);
$userOlap = new ReportOlap($userStat);
$eventOlap = new ReportOlap($eventsStat);
$transactionOlap = new ReportOlap($transactionsStat);
$columnsPeriods = array_unique(array_column(
$this->getUniqueByKeysFromStats([$eventsStat, $transactionsStat], ['event_date'], ['-']),
'event_date'
));
sort($columnsPeriods);
} else {
$olap = $userOlap = $eventOlap = $transactionOlap = $groupingFields = null;
$columnsPeriods = $rows = [];
}
return $this->render('payment_statistic.html.twig', [
'olap' => $olap,
'userOlap' => $userOlap,
'eventOlap' => $eventOlap,
'transactionOlap' => $transactionOlap,
'form' => $form->createView(),
'rows' => $rows,
'columnsPeriods' => $columnsPeriods,
'groupingFields' => $groupingFields,
]);
}
/**
* @param array $stats
* @param array $columns
* @param array $filter
* @return array
*/
private function getUniqueByKeysFromStats(array $stats, array $columns, array $filter = []): array
{
$result = [];
$columns = array_flip($columns);
$key = 0;
if (empty ($filter)) {
foreach ($stats as $stat) {
foreach ($stat as $val) {
$result[$key++] = array_intersect_key($val, $columns);
}
}
} else {
foreach ($stats as $stat) {
foreach ($stat as $val) {
if (! in_array($val, $filter)) {
$result[$key++] = array_intersect_key($val, $columns);
}
}
}
}
return $result;
}
}