Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.48% |
38 / 42 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| PayDay | |
90.48% |
38 / 42 |
|
50.00% |
1 / 2 |
8.06 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __invoke | |
90.24% |
37 / 41 |
|
0.00% |
0 / 1 |
7.05 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Controller\Admin; |
| 6 | |
| 7 | use App\Controller\BaseController; |
| 8 | use App\Repository\PaymentMethodRepository; |
| 9 | use App\Repository\StoreRepository; |
| 10 | use App\Repository\TransactionRepository; |
| 11 | use App\Service\TransactionFactory; |
| 12 | use Doctrine\ORM\EntityManagerInterface; |
| 13 | use Symfony\Component\HttpFoundation\Request; |
| 14 | use Symfony\Component\HttpFoundation\Response; |
| 15 | use Symfony\Component\Routing\Attribute\Route; |
| 16 | use Symfony\Component\Security\Http\Attribute\IsGranted; |
| 17 | use Symfony\Component\Serializer\Encoder\JsonEncoder; |
| 18 | use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer; |
| 19 | use Symfony\Component\Serializer\Serializer; |
| 20 | use UnexpectedValueException; |
| 21 | |
| 22 | #[Route(path: '/admin/pay-day', name: 'admin_pay_day', methods: ['GET', 'POST'])] |
| 23 | #[IsGranted('ROLE_ADMIN')] |
| 24 | class PayDay extends BaseController |
| 25 | { |
| 26 | public function __construct( |
| 27 | private readonly StoreRepository $storeRepository, |
| 28 | private readonly PaymentMethodRepository $paymentMethodRepository, |
| 29 | private readonly TransactionRepository $transactionRepository, |
| 30 | private readonly TransactionFactory $transactionFactory, |
| 31 | ) {} |
| 32 | |
| 33 | public function __invoke( |
| 34 | Request $request, |
| 35 | EntityManagerInterface $entityManager, |
| 36 | ): Response |
| 37 | { |
| 38 | $rawPayments = $request->request->all('payments'); |
| 39 | if ($rawPayments === []) { |
| 40 | $paymentMethods = $this->paymentMethodRepository->findAll(); |
| 41 | $serializer = new Serializer([new GetSetMethodNormalizer()], ['json' => new JsonEncoder()]); |
| 42 | return $this->render( |
| 43 | 'admin/payday.html.twig', |
| 44 | [ |
| 45 | 'stores' => $this->storeRepository->getActive(), |
| 46 | 'lastRecipeNo' => $this->transactionRepository->getLastRecipeNo() + 1, |
| 47 | 'paymentMethods' => $paymentMethods, |
| 48 | 'paymentMethodsString' => $serializer->serialize($paymentMethods, 'json'), |
| 49 | ] |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | /** @var array{date: list<string>, store: list<string>, method: list<string>, recipe: list<string>, document: list<string>, deposit: list<string>, amount: list<string>, comment: list<string>} $payments */ |
| 54 | $payments = $rawPayments; |
| 55 | foreach ($payments['date'] as $i => $dateCobro) { |
| 56 | if (!$dateCobro) { |
| 57 | continue; |
| 58 | } |
| 59 | |
| 60 | $store = $this->storeRepository->find((int)$payments['store'][$i]); |
| 61 | |
| 62 | if (!$store) { |
| 63 | continue; |
| 64 | } |
| 65 | |
| 66 | $method = $this->paymentMethodRepository->find((int)$payments['method'][$i]); |
| 67 | |
| 68 | if (!$method) { |
| 69 | throw new UnexpectedValueException('Invalid payment method.'); |
| 70 | } |
| 71 | |
| 72 | $user = $store->getUser(); |
| 73 | |
| 74 | if (!$user) { |
| 75 | throw new UnexpectedValueException('Store has no user.'); |
| 76 | } |
| 77 | |
| 78 | $transaction = $this->transactionFactory->createPayment( |
| 79 | $store, |
| 80 | $user, |
| 81 | $method, |
| 82 | $dateCobro, |
| 83 | (int) $payments['recipe'][$i], |
| 84 | (int) $payments['document'][$i], |
| 85 | (int) $payments['deposit'][$i], |
| 86 | $payments['amount'][$i], |
| 87 | $payments['comment'][$i], |
| 88 | ); |
| 89 | |
| 90 | $entityManager->persist($transaction); |
| 91 | } |
| 92 | |
| 93 | $entityManager->flush(); |
| 94 | $this->addFlash('success', 'Sa ha pagado...'); |
| 95 | |
| 96 | return $this->redirectToRoute('welcome'); |
| 97 | } |
| 98 | } |