-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathEditQuotationAction.php
More file actions
64 lines (56 loc) · 2.62 KB
/
EditQuotationAction.php
File metadata and controls
64 lines (56 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
declare(strict_types=1);
namespace AppBundle\Controller\Admin\Accounting\Quotation;
use AppBundle\Accounting\Form\QuotationType;
use AppBundle\Accounting\Model\Repository\InvoicingDetailRepository;
use AppBundle\Accounting\Model\Repository\InvoicingRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class EditQuotationAction extends AbstractController
{
public function __construct(
private readonly InvoicingRepository $invoicingRepository,
private readonly InvoicingDetailRepository $invoicingDetailRepository,
) {}
public function __invoke(Request $request): Response
{
$quotationId = $request->query->getInt('quotationId');
$quotation = $this->invoicingRepository->getById($quotationId);
if ($quotation === null) {
throw $this->createNotFoundException("Ce devis n'existe pas");
}
$form = $this->createForm(QuotationType::class, $quotation, ['actionType' => 'edit']);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
try {
$idsToRemove = $this->invoicingDetailRepository->getRowsIdsPerInvoicingId($quotation->getId());
$existingIds = [];
$this->invoicingRepository->startTransaction();
$this->invoicingRepository->save($quotation);
foreach ($quotation->getDetails() as $detail) {
if ($detail->getId() !== null) {
$existingIds[] = $detail->getId();
}
$detail->setInvoicingId($quotation->getId());
$this->invoicingDetailRepository->save($detail);
}
$idsToRemove = array_diff($idsToRemove, $existingIds);
if ($idsToRemove) {
$this->invoicingDetailRepository->removeRowsPerIds($idsToRemove);
}
$this->invoicingRepository->commit();
$this->addFlash('success', 'L\'écriture a été modifiée');
return $this->redirectToRoute('admin_accounting_quotations_list');
} catch (\Exception $e) {
$this->invoicingRepository->rollback();
$this->addFlash('error', 'L\'écriture n\'a pas pu être enregistrée');
}
}
return $this->render('admin/accounting/quotation/edit.html.twig', [
'quotation' => $quotation,
'form' => $form->createView(),
'submitLabel' => 'Modifier',
]);
}
}