vendor/bluue/cash-register-bundle/src/Controller/DashboardController.php line 49

Open in your IDE?
  1. <?php
  2. /**
  3.  * @author Thomas HERISSON (contact@scaledev.fr)
  4.  * @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
  5.  * @license commercial
  6.  */
  7. declare(strict_types=1);
  8. namespace Bluue\CashRegisterBundle\Controller;
  9. use App\Services\SoftdeleteFilter;
  10. use Bluue\CashRegisterBundle\Entity\Cashier;
  11. use Bluue\CashRegisterBundle\Repository\CashierRepository;
  12. use Bluue\CashRegisterBundle\Repository\CashRegisterRepository;
  13. use Bluue\CashRegisterBundle\Repository\PaymentMethodRepository;
  14. use DateTime;
  15. use Knp\Component\Pager\PaginatorInterface;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. use Bluue\CashRegisterBundle\Services\Statistics;
  20. use Bluue\CashRegisterBundle\Twig\SecurityExtension;
  21. use Bluue\CashRegisterBundle\Security\CashRegisterVoter;
  22. use Bluue\CashRegisterBundle\Repository\ReceiptRepository;
  23. use Bluue\CustomersBundle\Entity\Customer;
  24. use Bluue\SalesBundle\Repository\DeliveryNoteRepository;
  25. use Doctrine\ORM\EntityManagerInterface;
  26. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  27. class DashboardController extends AbstractController
  28. {
  29.     /**
  30.      * @param CashRegisterVoter $voter
  31.      * @param SecurityExtension $securityExtension
  32.      * @param Request $request
  33.      * @param Statistics $stats
  34.      * @param ReceiptRepository $receiptRepo
  35.      * @param PaginatorInterface $paginator
  36.      * @param CashierRepository $cashierRepo
  37.      * @param DeliveryNoteRepository $deliveryNoteRepo
  38.      * @param PaymentMethodRepository $paymentMethodRepo
  39.      * @param CashRegisterRepository $cashRegisterRepo
  40.      * @param EntityManagerInterface $em
  41.      * @return Response
  42.      * @Route("/", name="cash_register_bundle__dashboard", host="%app.caisse_host%")
  43.      */
  44.     public function dashboard(
  45.         CashRegisterVoter $voter,
  46.         SecurityExtension $securityExtension,
  47.         Request $request,
  48.         Statistics $stats,
  49.         ReceiptRepository $receiptRepo,
  50.         PaginatorInterface $paginator,
  51.         CashierRepository $cashierRepo,
  52.         DeliveryNoteRepository $deliveryNoteRepo,
  53.         PaymentMethodRepository $paymentMethodRepo,
  54.         CashRegisterRepository $cashRegisterRepo,
  55.         EntityManagerInterface $em
  56.     ): Response {
  57.         if (!$voter->checkLogin()) {
  58.             return $this->redirectToRoute('cash_register_bundle__login');
  59.         }
  60.         $query $request->query;
  61.         $dateBegin $query->get('date_begin');
  62.         $dateEnd $query->get('date_end');
  63.         $cashRegister $securityExtension->getCashRegisterSession();
  64.         if (!$dateBegin && !$query->get('no_filter')) {
  65.             $now = (new DateTime())->format('Y-m-d');
  66.             $dateBegin $now;
  67.             $dateEnd $now;
  68.         }
  69.         $nbReceipts $stats->getNumberOfReceipts($cashRegister->getId(), $dateBegin$dateEnd ' 23:59:59');
  70.         $turnover $stats->getTurnoverForCashRegister($cashRegister->getId(), $dateBegin$dateEnd ' 23:59:59');
  71.         $margin $stats->getMarginForCashRegister($cashRegister->getId(), $dateBegin$dateEnd ' 23:59:59');
  72.         if ($dateBegin) {
  73.             $dateRange date('d-m-Y'strtotime($dateBegin)) . ' - ' date('d-m-Y'strtotime($dateEnd));
  74.         } else {
  75.             $dateRange null;
  76.         }
  77.         $interval 0;
  78.         if ($dateBegin) {
  79.             $datetimeBegin date_create_from_format('Y-m-d'$dateBegin);
  80.             $datetimeEnd date_create_from_format('Y-m-d'$dateEnd);
  81.             $interval $datetimeBegin->diff($datetimeEnd)->d;
  82.         }
  83.         SoftdeleteFilter::disable($em, [Cashier::class, Customer::class]);
  84.         $shop $cashRegister->getShop();
  85.         $filters $request->query->all('filter');
  86.         $query $receiptRepo->search(
  87.             $filters,
  88.             ['session' => ['cashRegister' => ['shop']], 'customer''validatedBy''payments' => ['paymentMethod']],
  89.             [],
  90.             false,
  91.             ['u.validatedAt IS NOT NULL']
  92.         )
  93.             ->andWhere('shop.id = :id')
  94.             ->setParameter('id'$shop->getId()->toBinary());
  95.         $sort $request->query->get('sort1''u.number');
  96.         $direction $request->query->get('direction1''desc');
  97.         if ($sort == 'validatedBy.firstname') {
  98.             $sort null;
  99.             $query->orderBy('validatedBy.firstname'$direction)->addOrderBy('validatedBy.lastname'$direction);
  100.         }
  101.         $receipts $paginator->paginate(
  102.             $query,
  103.             1,
  104.             5,
  105.             [
  106.                 'defaultSortFieldName' => $sort,
  107.                 'defaultSortDirection' => $direction
  108.             ]
  109.         );
  110.         $cashiers $cashierRepo->findAllByCashRegister($cashRegister);
  111.         $cashRegisterIds = [];
  112.         foreach ($shop->getCashRegisters() as $cashRegisterEl) {
  113.             $cashRegisterIds[] = $cashRegisterEl->getId()->toRfc4122();
  114.         }
  115.         $filterDeliveryNote $request->query->all('filterDeliveryNote');
  116.         $queryDeliveryNotes $deliveryNoteRepo->search($filterDeliveryNote, ['customer'])
  117.             ->andWhere(
  118.                 "JSON_UNQUOTE(JSON_EXTRACT(u.options, '$.cash_register_bundle.cashRegisterId')) IN (:cashRegisterIds)"
  119.             )
  120.             ->setParameter('cashRegisterIds'$cashRegisterIds);
  121.         $deliveryNotes $paginator->paginate(
  122.             $queryDeliveryNotes,
  123.             1,
  124.             5,
  125.             [
  126.                 'defaultSortFieldName' => $request->query->get('sort2''u.reference'),
  127.                 'defaultSortDirection' => $request->query->get('direction2''desc'),
  128.                 'sortFieldParameterName' => 'sort2',
  129.                 'sortDirectionParameterName' => 'direction2'
  130.             ]
  131.         );
  132.         $allPaymentsMethod $paymentMethodRepo->findAll();
  133.         return $this->render(
  134.             '@CashRegister/dashboard.html.twig',
  135.             [
  136.                 'cashRegister' => $cashRegister,
  137.                 'cashRegisters' => $cashRegisterRepo->findBy(['shop' => $shop]),
  138.                 'dateRange' => $dateRange,
  139.                 'nbReceipts' => $nbReceipts,
  140.                 'turnover' => $turnover,
  141.                 'margin' => $margin,
  142.                 'interval' => $interval,
  143.                 'mainRequest' => $request,
  144.                 'receipts' => $receipts,
  145.                 'cashiers' => $cashiers,
  146.                 'shop' => $shop,
  147.                 'deliveryNotes' => $deliveryNotes,
  148.                 'allPaymentsMethod' => $allPaymentsMethod
  149.             ]
  150.         );
  151.     }
  152. }