Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
3.33% |
1 / 30 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| Create | |
3.33% |
1 / 30 |
|
50.00% |
1 / 2 |
82.17 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| new | |
0.00% |
0 / 29 |
|
0.00% |
0 / 1 |
72 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Controller\Contracts; |
| 6 | |
| 7 | use App\Controller\BaseController; |
| 8 | use App\Entity\Contract; |
| 9 | use App\Form\ContractType; |
| 10 | use App\Repository\ContractRepository; |
| 11 | use App\Repository\StoreRepository; |
| 12 | use App\Repository\UserRepository; |
| 13 | use App\Service\ContractFactory; |
| 14 | use App\Service\TaxService; |
| 15 | use Doctrine\ORM\EntityManagerInterface; |
| 16 | use Symfony\Component\HttpFoundation\Request; |
| 17 | use Symfony\Component\HttpFoundation\Response; |
| 18 | use Symfony\Component\Routing\Attribute\Route; |
| 19 | use Symfony\Component\Security\Http\Attribute\IsGranted; |
| 20 | |
| 21 | #[IsGranted('ROLE_ADMIN')] |
| 22 | class Create extends BaseController |
| 23 | { |
| 24 | public function __construct( |
| 25 | private readonly StoreRepository $storeRepo, |
| 26 | private readonly UserRepository $userRepo, |
| 27 | private readonly ContractRepository $contractRepo, |
| 28 | private readonly TaxService $taxService, |
| 29 | private readonly ContractFactory $contractFactory, |
| 30 | ) {} |
| 31 | |
| 32 | #[Route(path: '/contracts/create', name: 'contracts_create', methods: ['POST'])] |
| 33 | public function new( |
| 34 | Request $request, |
| 35 | EntityManagerInterface $entityManager, |
| 36 | ): Response { |
| 37 | $store = $this->storeRepo->find($request->request->getInt('store')); |
| 38 | $user = $this->userRepo->find($request->request->getInt('user')); |
| 39 | $template = $this->contractRepo->findTemplate(); |
| 40 | $template = $template instanceof Contract ? $template : null; |
| 41 | |
| 42 | $contract = $store !== null |
| 43 | ? $this->contractFactory->createFromStore($store, $template) |
| 44 | : new Contract(); |
| 45 | |
| 46 | if ($store === null && $template instanceof Contract) { |
| 47 | $contract->setText($template->getText()); |
| 48 | } |
| 49 | |
| 50 | if ($user !== null) { |
| 51 | $contract |
| 52 | ->setInqNombreapellido((string) $user->getName()) |
| 53 | ->setInqCi($user->getInqCi()); |
| 54 | } |
| 55 | |
| 56 | $form = $this->createForm(ContractType::class, $contract); |
| 57 | $form->handleRequest($request); |
| 58 | if ($form->isSubmitted() && $form->isValid()) { |
| 59 | $entityManager->persist($contract); |
| 60 | $entityManager->flush(); |
| 61 | |
| 62 | $this->addFlash('success', 'El contrato fue guardado.'); |
| 63 | |
| 64 | return $this->redirectToRoute('contracts_index'); |
| 65 | } |
| 66 | |
| 67 | return $this->render( |
| 68 | 'contracts/form.html.twig', |
| 69 | [ |
| 70 | 'form' => $form, |
| 71 | 'data' => $contract, |
| 72 | 'ivaMultiplier' => $this->taxService->getTaxValue(), |
| 73 | 'title' => 'Nuevo Contrato', |
| 74 | ] |
| 75 | ); |
| 76 | } |
| 77 | } |