Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
46 / 46
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Transactions
100.00% covered (success)
100.00%
46 / 46
100.00% covered (success)
100.00%
2 / 2
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 show
100.00% covered (success)
100.00%
45 / 45
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3declare(strict_types=1);
4
5namespace App\Controller\Stores;
6
7use App\Controller\BaseController;
8use App\Entity\Store;
9use App\Helper\BreadcrumbTrait;
10use App\Helper\IntlConverter;
11use App\Repository\StoreRepository;
12use App\Repository\TransactionRepository;
13use App\Service\ChartBuilderService;
14use App\Service\TaxService;
15use App\Type\TransactionType;
16use Symfony\Component\Clock\ClockInterface;
17use Symfony\Component\HttpFoundation\Request;
18use Symfony\Component\HttpFoundation\Response;
19use Symfony\Component\Routing\Attribute\Route;
20
21class Transactions extends BaseController
22{
23    use BreadcrumbTrait;
24
25    public function __construct(
26        private readonly TransactionRepository $transactionRepository,
27        private readonly StoreRepository $storeRepository,
28        private readonly TaxService $taxService,
29        private readonly ChartBuilderService $chartBuilder,
30        private readonly ClockInterface $clock,
31    ) {}
32
33    #[Route(path: '/stores/{id}', name: 'stores_transactions', requirements: ['id' => '\d+',], methods: ['GET', 'POST',])]
34    public function show(
35        Store $store,
36        Request $request
37    ): Response
38    {
39        $this->denyAccessUnlessGranted('view', $store);
40        $year = $request->query->getInt('year', (int) $this->clock->now()->format('Y'));
41        $this->addBreadcrumb('Stores', 'stores_index')
42            ->addBreadcrumb('Store '.$store->getId());
43        $transactions = $this->transactionRepository->findByStoreAndYear($store, $year);
44        $headers = [];
45        /** @var array<int, float> $monthPayments */
46        $monthPayments = [];
47        $rentalValues = [];
48        $rentalValue = $this->taxService->addTax($store->getValAlq());
49        for ($i = 1; $i < 13; ++$i) {
50            $headers[] = IntlConverter::formatDate('1966-'.$i.'-1', 'MMMM');
51            $monthPayments[$i] = 0;
52            $rentalValues[$i] = $rentalValue;
53        }
54
55        foreach ($transactions as $transaction) {
56            if (TransactionType::payment === $transaction->getType()) {
57                $monthPayments[$transaction->getDate()->format('n')]
58                    += (float)$transaction->getAmount();
59            }
60        }
61
62        return $this->render(
63            'stores/transactions.html.twig',
64            [
65                'transactions' => $transactions,
66                'saldoAnterior' => $this->transactionRepository->getSaldoAnterior(
67                    $store,
68                    $year
69                ),
70                'headerStr' => json_encode($headers, JSON_THROW_ON_ERROR),
71                'monthPayments' => json_encode(
72                    array_values($monthPayments),
73                    JSON_THROW_ON_ERROR
74                ),
75                'rentalValStr' => json_encode(
76                    array_values($rentalValues),
77                    JSON_THROW_ON_ERROR
78                ),
79                'store' => $store,
80                'stores' => $this->storeRepository->findAll(),
81                'year' => $year,
82                'breadcrumbs' => $this->getBreadcrumbs(),
83                'chart' => $this->chartBuilder->getStoreChart(
84                    $headers,
85                    array_values($monthPayments),
86                    array_values($rentalValues)
87                ),
88            ]
89        );
90    }
91}