src/Controller/DashboardController.php line 23
<?php
namespace App\Controller;
use App\Entity\Order;
use App\Entity\SupportTicket;
use App\Entity\User;
use App\Repository\OrderRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
class DashboardController extends AbstractController
{
#[Route('/test', name: 'app_home')]
public function home(): Response
{
return $this->redirectToRoute('app_dashboard');
}
#[Route('/app', name: 'app_dashboard')]
public function index(EntityManagerInterface $entityManager): Response
{
$todayCount = $entityManager->getRepository(Order::class)->getOrderCount('today', $this->getUser());
$yesterdayCount = $entityManager->getRepository(Order::class)->getOrderCount('yesterday', $this->getUser());
$allByYesterday = $entityManager->getRepository(Order::class)->getOrderCount('all_by_yesterday', $this->getUser());
$allByToday = $entityManager->getRepository(Order::class)->getOrderCount('all_by_today', $this->getUser());
$today = new \DateTime('today');
$today->setTime(0,0);
$sevenDayBefore = new \DateTime('today');
$sevenDayBefore->modify('-7 days');
$sevenDayBefore->setTime(0,0);
$lastSevenDays = $entityManager->getRepository(Order::class)->getOrderCountPerDayForCurrentWeek($this->getUser(), $today, $sevenDayBefore);
$lastSevenDaysOrdersTotal = array_reduce($lastSevenDays, function($carry, $item) {
return $carry + $item['count'];
}, 0);
$lastSevenDaysOrders['data'] = array_map(function($item) {
return $item['count'];
}, $lastSevenDays);
// Extract the 'createdAt' values
$lastSevenDaysOrders['categories'] = array_map(function($item) {
return $item['createdDate'];
}, $lastSevenDays);
$lasttoLastSevenDaysBefore = new \DateTime('today');
$lasttoLastSevenDaysBefore->modify('-14 days');
$lasttoLastSevenDaysBefore->setTime(0,0);
$lasttoLastSevenDays = $entityManager->getRepository(Order::class)
->getOrderCountPerDayForCurrentWeek($this->getUser(), $sevenDayBefore, $lasttoLastSevenDaysBefore);
$lasttoLastSevenDaysOrdersTotal = array_reduce($lasttoLastSevenDays, function($carry, $item) {
return $carry + $item['count'];
}, 0);
$lastSevenDaysPercentage = $this->getPercentage($lasttoLastSevenDaysOrdersTotal,$lastSevenDaysOrdersTotal);
$currentMonth = new \DateTime('first day of last month');
$currentMonth->setTime(0,0);
$currentMonthOrders = $entityManager->getRepository(Order::class)->getOrderCountPerDayForCurrentMonth($this->getUser(), $today, $currentMonth);
$currentMonthOrdersTotal = array_reduce($currentMonthOrders, function($carry, $item) {
return $carry + $item['count'];
}, 0);
$currentMonthOrdersData['data'] = array_map(function($item) {
return $item['count'];
}, $currentMonthOrders);
$currentMonthOrdersData['categories'] = array_map(function($item) {
return $item['createdWeek'];
}, $currentMonthOrders);
$lastMonth = new \DateTime('first day of last month');
$lastMonth->setTime(0,0);
$lastMonthOrders = $entityManager->getRepository(Order::class)->getOrderCountPerDayForCurrentMonth($this->getUser(), $currentMonth, $lastMonth);
$lastMonthOrdersTotal = array_reduce($lastMonthOrders, function($carry, $item) {
return $carry + $item['count'];
}, 0);
$currentMonthPercentage = $this->getPercentage($currentMonthOrdersTotal,$lastMonthOrdersTotal);
// $lastMonthOrdersData['data'] = array_map(function($item) {
// return $item['count'];
// }, $lastMonthOrders);
//
// $lastMonthOrdersData['categories'] = array_map(function($item) {
// return $item['createdDate'];
// }, $lastMonthOrders);
// $lasttoLastSevenDays['data'] = array_map(function($item) {
// return $item['count'];
// }, $lastSevenDays);
//
// // Extract the 'createdAt' values
// $lastSevenDaysOrders['categories'] = array_map(function($item) {
// return $item['createdDate'];
// }, $lastSevenDays);
$todayCountData = $this->getPercentage($yesterdayCount,$todayCount);
$totalCountData = $this->getPercentage($allByYesterday,$allByToday);
$supportTicketData = $entityManager->getRepository(SupportTicket::class)->findBy( [
'status' => 'open',
'customer' => $this->getUser()
],['id'=>'DESC'],5,0);
$orderData = $entityManager->getRepository(Order::class)->findBy(['customer' => $this->getUser()], ['id'=>'DESC'],5,0);
//dd($totalCountData);
//$balance = $entityManager->getRepository(Wa)
$chartData = [
// 'latestOrders' => [
// 'data' => [3844, 3855, 3841, 3867, 3822, 3843, 3821, 3841, 3856, 3827, 3843],
// 'categories' => ["Jan 01 2022", "Jan 02 2022", "Jan 03 2022", "Jan 04 2022", "Jan 05 2022", "Jan 06 2022", "Jan 07 2022", "Jan 08 2022", "Jan 09 2022", "Jan 10 2022", "Jan 11 2022",]
// ],
'latestOrders' => $lastSevenDaysOrders,
'currentMonthOrders' => $currentMonthOrdersData
];
return $this->render('dashboard/index.html.twig', [
'lastSevenDaysOrdersTotal' => $lastSevenDaysOrdersTotal,
'lastSevenDaysPercentage' => $lastSevenDaysPercentage,
'currentMonthOrderTotal' => $currentMonthOrdersTotal,
'currentMonthPercentage' => $currentMonthPercentage,
'todayCountData' => $todayCountData,
'totalCountData' => $totalCountData,
'supportTicketData' => $supportTicketData,
'orderData' => $orderData,
'chartData' => $chartData
]);
}
public function getPercentage(int $baseValue, int $secondValue): array
{
// echo $baseValue;
// echo $secondValue;
// exit;
$data = [];
if ($secondValue > $baseValue){
$data['orderTextStyle'] = 'text-success';
$data['orderArrowStyle'] = 'arrow-up';
}else if ($baseValue > $secondValue){
$data['orderTextStyle'] = 'text-danger';
$data['orderArrowStyle'] = 'arrow-down';
}else{
$data['orderTextStyle'] = 'text-info';
$data['orderArrowStyle'] = '';
}
$data['todayCount'] = $secondValue;
$data['yesterdayCount'] = $baseValue;
$onePercentage = $baseValue/100;
$difference = $secondValue - $baseValue;
$percentage = $onePercentage > 0 ? $difference/$onePercentage : 0;
$data['dataPercentage'] = number_format($percentage,2);
return $data;
}
}