<?php
namespace App\Controller;
use App\Repository\GatewayLinkRepository;
use App\Repository\TransactionRepository;
use DateTime;
use Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\UX\Chartjs\Builder\ChartBuilderInterface;
use Symfony\UX\Chartjs\Model\Chart;
class DashboardController extends AbstractController
{
private GatewayLinkRepository $GLRepository;
private TransactionRepository $transactionRepository;
public function __construct(GatewayLinkRepository $glRepository, TransactionRepository $transactionRepository)
{
$this->GLRepository = $glRepository;
$this->transactionRepository = $transactionRepository;
}
#[Route('/', name: 'app_index')]
public function home(ChartBuilderInterface $chartBuilder): Response
{
if (!$this->getUser()) {
//return $this->redirectToRoute('app_login');
return $this->render('home.html.twig');
}
return $this->redirectToRoute('app_dashboard');
}
#[Route('/privacy', name: 'app_privacy')]
public function privacy(): Response
{
return $this->render('home/privacy.html.twig');
}
#[Route('/terms', name: 'app_terms')]
public function terms(): Response
{
return $this->render('home/terms.html.twig');
}
#[Route('/contact', name: 'app_contact')]
public function contact(): Response
{
return $this->render('home/contact.html.twig');
}
#[Route('/getit', name: 'app_getit')]
public function getit(): Response
{
return $this->render('home/getit.html.twig');
}
#[Route('/learn', name: 'app_learn')]
public function learn(): Response
{
return $this->render('home/getit.html.twig');
}
#[Route('/dashboard', name: 'app_dashboard')]
public function dashboard(ChartBuilderInterface $chartBuilder): Response
{
try {
$user = $this->getUser();
$tdate = new DateTime();
$tdate = $tdate->modify($user->getTimeZoneAdjustment());
$tdate = new DateTime($tdate->format($user->getCompletedStartDay()));
if ($user->getCompletedStart() == 'CQ'){
$currentMonth = intval($tdate->format('m'));
if ($currentMonth <= 3){
$tdate = new DateTime($tdate->format('Y') . '-1-1');
}elseif ($currentMonth <= 6){
$tdate = new DateTime($tdate->format('Y') . '-4-1');
}elseif ($currentMonth <= 9){
$tdate = new DateTime($tdate->format('Y') . '-7-1');
}elseif ($currentMonth <= 12){
$tdate = new DateTime($tdate->format('Y') . '-10-1');
}
}
$tdate = $tdate->modify($user->getCompletedStartDayAdjustment());
$tdate = $tdate->modify($user->getTimeZoneReAdjustment());
$edate = new DateTime();
$query = $this->transactionRepository->createQueryBuilder('t')
->select('t','g')
->leftJoin('t.gateway_link', 'g')
->andWhere('t.user = :uid1')
->setParameter('uid1', $user->getId())
->andWhere('t.created_at >= :cdate')
->setParameter('cdate', $tdate->format('Y-m-d H:i:s'))
->andWhere('t.created_at < :edate')
->setParameter('edate', $edate->format('Y-m-d H:i:s'));
// $query->getQuery()
// ->getResult();
$ctotal = 0.0;
foreach ($query as $row){
$ctotal += 1.00; //$row['transactiondata']['actions'][0]['amount'];
}
// if (!empty($newquery)){
// $tdate = $newquery[0]->getCreatedAt();
// $tdate = $tdate->modify($user->getTimeZoneAdjustment());
//// $tdate = new DateTime('' . $fdate->format('Y-m-d'));
// $edate = new DateTime($tdate->format('Y-m-d'));
// $fdate = new DateTime($tdate->format('Y-m-d'));
// $fdate = $fdate->modify($user->getTimeZoneReAdjustment());
// $edate = $edate->modify('+1 day');
// $edate = $edate->modify($user->getTimeZoneReAdjustment());
//
//// $this->addFlash('msg_error', 't ' . $tdate->format('Y-m-d H:i:s' . ' e ' . $edate->format('Y-m-d')));
//
// $query = $this->transactionRepository->createQueryBuilder('t')
// ->andWhere('t.user = :uid1')
// ->setParameter('uid1', $user->getId())
// ->andWhere('t.created_at >= :cdate')
// ->setParameter('cdate', $fdate->format('Y-m-d H:i:s'))
// ->andWhere('t.created_at < :edate')
// ->setParameter('edate', $edate->format('Y-m-d H:i:s'));
//
// if (!empty($search)) {
// $query->andWhere('t.transaction_data LIKE :search')
// ->setParameter('search', "%$search%");
// }
//
// }
// else
// {
// $tdate = new DateTime('now');
// $query = [];
// }
$dquery = $this->transactionRepository->createQueryBuilder('t')
->andWhere('t.user = :uid')
->setParameter('uid', $user->getId())
->orderBy('t.id', 'DESC')
->distinct()
->getQuery()
->getResult();
$tdate = $tdate->modify($user->getTimeZoneAdjustment());
$edate = $edate->modify($user->getTimeZoneAdjustment());
return $this->render('dashboard/index.html.twig', [
'master' => $this->getUser(),
'checks' => 5,
'credit_cards' => 47,
'other' => 0,
'chart_title' => 'Transaction Types the last 30 days',
]);
} catch (Exception $e) {
$this->addFlash('msg_error', $e->getMessage());
return $this->redirectToRoute('app_integration_settings');
}
}
}