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

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