|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file has been created by developers from BitBag. |
|
5
|
|
|
* Feel free to contact us once you face any issues or want to start |
|
6
|
|
|
* another great project. |
|
7
|
|
|
* You can find more information about us on https://bitbag.shop and write us |
|
8
|
|
|
* an email on [email protected]. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
declare(strict_types=1); |
|
12
|
|
|
|
|
13
|
|
|
namespace BitBag\SyliusInvoicingPlugin\Form\Extension; |
|
14
|
|
|
|
|
15
|
|
|
use BitBag\SyliusInvoicingPlugin\Entity\InvoiceInterface; |
|
16
|
|
|
use BitBag\SyliusInvoicingPlugin\Form\Type\InvoiceType; |
|
17
|
|
|
use BitBag\SyliusInvoicingPlugin\Repository\InvoiceRepositoryInterface; |
|
18
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
19
|
|
|
use Sylius\Bundle\AddressingBundle\Form\Type\AddressType; |
|
20
|
|
|
use Symfony\Component\Form\AbstractTypeExtension; |
|
21
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
|
22
|
|
|
use Symfony\Component\Form\FormEvent; |
|
23
|
|
|
use Symfony\Component\Form\FormEvents; |
|
24
|
|
|
|
|
25
|
|
|
final class AddressTypeExtension extends AbstractTypeExtension |
|
26
|
|
|
{ |
|
27
|
|
|
/** @var InvoiceRepositoryInterface */ |
|
28
|
|
|
private $invoiceRepository; |
|
29
|
|
|
|
|
30
|
|
|
/** @var EntityManagerInterface */ |
|
31
|
|
|
private $invoiceEntityManager; |
|
32
|
|
|
|
|
33
|
|
|
public function __construct( |
|
34
|
|
|
InvoiceRepositoryInterface $invoiceRepository, |
|
35
|
|
|
EntityManagerInterface $invoiceEntityManager |
|
36
|
|
|
) { |
|
37
|
|
|
$this->invoiceRepository = $invoiceRepository; |
|
38
|
|
|
$this->invoiceEntityManager = $invoiceEntityManager; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function buildForm(FormBuilderInterface $builder, array $options): void |
|
42
|
|
|
{ |
|
43
|
|
|
$builder |
|
44
|
|
|
->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event): void { |
|
45
|
|
|
$parentForm = $event->getForm()->getParent(); |
|
46
|
|
|
|
|
47
|
|
|
if (null === $parentForm) { |
|
48
|
|
|
return; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$billingAddressForm = $parentForm->get('billingAddress'); |
|
52
|
|
|
|
|
53
|
|
|
if ($event->getForm() !== $billingAddressForm) { |
|
54
|
|
|
return; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$order = $billingAddressForm->getParent()->getData(); |
|
58
|
|
|
$invoice = $this->invoiceRepository->findByOrderId($order->getId()); |
|
59
|
|
|
|
|
60
|
|
|
$billingAddressForm->add('invoice', InvoiceType::class, [ |
|
61
|
|
|
'label' => false, |
|
62
|
|
|
'mapped' => false, |
|
63
|
|
|
'data' => $invoice, |
|
64
|
|
|
]); |
|
65
|
|
|
}) |
|
66
|
|
|
->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event): void { |
|
67
|
|
|
if (!$event->getForm()->has('invoice')) { |
|
68
|
|
|
return; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** @var InvoiceInterface $invoice */ |
|
72
|
|
|
$billingAddressForm = $event->getForm(); |
|
73
|
|
|
$invoice = $billingAddressForm->get('invoice')->getData(); |
|
74
|
|
|
|
|
75
|
|
|
if (null !== $invoice->getVatNumber() && true === $billingAddressForm->isValid()) { |
|
76
|
|
|
$order = $billingAddressForm->getParent()->getData(); |
|
77
|
|
|
|
|
78
|
|
|
$invoice->setOrder($order); |
|
79
|
|
|
|
|
80
|
|
|
$invoice->getId() ?: $this->invoiceEntityManager->persist($invoice); |
|
81
|
|
|
$this->invoiceEntityManager->flush(); |
|
82
|
|
|
} |
|
83
|
|
|
}) |
|
84
|
|
|
; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function getExtendedType(): string |
|
88
|
|
|
{ |
|
89
|
|
|
return AddressType::class; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|