<?php
/**
* @author Thomas HERISSON (contact@scaledev.fr)
* @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
* @license commercial
*/
declare(strict_types=1);
namespace Bluue\CashRegisterBundle\Controller;
use App\Services\SoftdeleteFilter;
use Bluue\CashRegisterBundle\Entity\Cashier;
use Bluue\CashRegisterBundle\Repository\CashierRepository;
use Bluue\CashRegisterBundle\Repository\CashRegisterRepository;
use Bluue\CashRegisterBundle\Repository\PaymentMethodRepository;
use DateTime;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Bluue\CashRegisterBundle\Services\Statistics;
use Bluue\CashRegisterBundle\Twig\SecurityExtension;
use Bluue\CashRegisterBundle\Security\CashRegisterVoter;
use Bluue\CashRegisterBundle\Repository\ReceiptRepository;
use Bluue\SalesBundle\Repository\DeliveryNoteRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class DashboardController extends AbstractController
{
/**
* @param CashRegisterVoter $voter
* @param SecurityExtension $securityExtension
* @param Request $request
* @param Statistics $stats
* @param ReceiptRepository $receiptRepo
* @param PaginatorInterface $paginator
* @param CashierRepository $cashierRepo
* @param DeliveryNoteRepository $deliveryNoteRepo
* @param PaymentMethodRepository $paymentMethodRepo
* @param CashRegisterRepository $cashRegisterRepo
* @param EntityManagerInterface $em
* @return Response
* @Route("/", name="cash_register_bundle__dashboard", host="%app.caisse_host%")
*/
public function dashboard(
CashRegisterVoter $voter,
SecurityExtension $securityExtension,
Request $request,
Statistics $stats,
ReceiptRepository $receiptRepo,
PaginatorInterface $paginator,
CashierRepository $cashierRepo,
DeliveryNoteRepository $deliveryNoteRepo,
PaymentMethodRepository $paymentMethodRepo,
CashRegisterRepository $cashRegisterRepo,
EntityManagerInterface $em
): Response {
if (!$voter->checkLogin()) {
return $this->redirectToRoute('cash_register_bundle__login');
}
$query = $request->query;
$dateBegin = $query->get('date_begin');
$dateEnd = $query->get('date_end');
$cashRegister = $securityExtension->getCashRegisterSession();
if (!$dateBegin && !$query->get('no_filter')) {
$now = (new DateTime())->format('Y-m-d');
$dateBegin = $now;
$dateEnd = $now;
}
$nbReceipts = $stats->getNumberOfReceipts($cashRegister->getId(), $dateBegin, $dateEnd . ' 23:59:59');
$turnover = $stats->getTurnoverForCashRegister($cashRegister->getId(), $dateBegin, $dateEnd . ' 23:59:59');
$margin = $stats->getMarginForCashRegister($cashRegister->getId(), $dateBegin, $dateEnd . ' 23:59:59');
if ($dateBegin) {
$dateRange = date('d-m-Y', strtotime($dateBegin)) . ' - ' . date('d-m-Y', strtotime($dateEnd));
} else {
$dateRange = null;
}
$interval = 0;
if ($dateBegin) {
$datetimeBegin = date_create_from_format('Y-m-d', $dateBegin);
$datetimeEnd = date_create_from_format('Y-m-d', $dateEnd);
$interval = $datetimeBegin->diff($datetimeEnd)->d;
}
SoftdeleteFilter::disable($em, [Cashier::class]);
$shop = $cashRegister->getShop();
$filters = $request->query->all('filter');
$query = $receiptRepo->search(
$filters,
['session' => ['cashRegister' => ['shop']], 'customer', 'validatedBy', 'payments' => ['paymentMethod']],
[],
false,
['u.validatedAt IS NOT NULL']
)
->andWhere('shop.id = :id')
->setParameter('id', $shop->getId()->toBinary());
$sort = $request->query->get('sort1', 'u.number');
$direction = $request->query->get('direction1', 'desc');
if ($sort == 'validatedBy.firstname') {
$sort = null;
$query->orderBy('validatedBy.firstname', $direction)->addOrderBy('validatedBy.lastname', $direction);
}
$receipts = $paginator->paginate(
$query,
1,
5,
[
'defaultSortFieldName' => $sort,
'defaultSortDirection' => $direction
]
);
$cashiers = $cashierRepo->findAllByCashRegister($cashRegister);
$cashRegisterIds = [];
foreach ($shop->getCashRegisters() as $cashRegisterEl) {
$cashRegisterIds[] = $cashRegisterEl->getId()->toRfc4122();
}
$filterDeliveryNote = $request->query->all('filterDeliveryNote');
$queryDeliveryNotes = $deliveryNoteRepo->search($filterDeliveryNote, ['customer'])
->andWhere(
"JSON_UNQUOTE(JSON_EXTRACT(u.options, '$.cash_register_bundle.cashRegisterId')) IN (:cashRegisterIds)"
)
->setParameter('cashRegisterIds', $cashRegisterIds);
$deliveryNotes = $paginator->paginate(
$queryDeliveryNotes,
1,
5,
[
'defaultSortFieldName' => $request->query->get('sort2', 'u.reference'),
'defaultSortDirection' => $request->query->get('direction2', 'desc'),
'sortFieldParameterName' => 'sort2',
'sortDirectionParameterName' => 'direction2'
]
);
$allPaymentsMethod = $paymentMethodRepo->findAll();
return $this->render(
'@CashRegister/dashboard.html.twig',
[
'cashRegister' => $cashRegister,
'cashRegisters' => $cashRegisterRepo->findBy(['shop' => $shop]),
'dateRange' => $dateRange,
'nbReceipts' => $nbReceipts,
'turnover' => $turnover,
'margin' => $margin,
'interval' => $interval,
'mainRequest' => $request,
'receipts' => $receipts,
'cashiers' => $cashiers,
'shop' => $shop,
'deliveryNotes' => $deliveryNotes,
'allPaymentsMethod' => $allPaymentsMethod
]
);
}
}