|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* Copyright Humbly Arrogant Ltd 2020-2022. |
|
7
|
|
|
* |
|
8
|
|
|
* Use of this software is governed by the Business Source License included in the LICENSE file and at https://getparthenon.com/docs/next/license. |
|
9
|
|
|
* |
|
10
|
|
|
* Change Date: TBD ( 3 years after 2.0.0 release ) |
|
11
|
|
|
* |
|
12
|
|
|
* On the date above, in accordance with the Business Source License, use of this software will be governed by the open source license specified in the LICENSE file. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Parthenon\Invoice; |
|
16
|
|
|
|
|
17
|
|
|
use Brick\Money\Money; |
|
18
|
|
|
use DateTime; |
|
19
|
|
|
use Parthenon\Common\Address; |
|
20
|
|
|
|
|
21
|
|
|
final class InvoiceBuilder |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var ItemInterface[] |
|
25
|
|
|
*/ |
|
26
|
|
|
private array $items = []; |
|
27
|
|
|
|
|
28
|
|
|
private string $vatNumber; |
|
29
|
|
|
|
|
30
|
|
|
private CountryRules $countryRules; |
|
31
|
|
|
|
|
32
|
|
|
private Address $billerAddress; |
|
33
|
|
|
|
|
34
|
|
|
private float $vatRate = 0.0; |
|
35
|
|
|
|
|
36
|
|
|
private bool $addVat = true; |
|
37
|
|
|
|
|
38
|
|
|
private DateTime $createdAt; |
|
39
|
|
|
|
|
40
|
|
|
private string $currency = 'EUR'; |
|
41
|
|
|
|
|
42
|
|
|
private InvoiceNumberGeneratorInterface $invoiceNumberGenerator; |
|
43
|
|
|
|
|
44
|
|
|
private $invoiceNumber; |
|
45
|
|
|
|
|
46
|
|
|
private string $freeHandAddress; |
|
47
|
|
|
|
|
48
|
|
|
private string $paymentDetails; |
|
49
|
|
|
|
|
50
|
|
|
public function setInvoiceNumberGenerator(InvoiceNumberGeneratorInterface $invoiceNumberGenerator): void |
|
51
|
|
|
{ |
|
52
|
|
|
$this->invoiceNumberGenerator = $invoiceNumberGenerator; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function setInvoiceNumber($invoiceNumber): void |
|
56
|
|
|
{ |
|
57
|
|
|
$this->invoiceNumber = $invoiceNumber; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function addVatToTotal(): self |
|
61
|
|
|
{ |
|
62
|
|
|
$this->addVat = true; |
|
63
|
|
|
|
|
64
|
|
|
return $this; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function deductVatFromTotal(): self |
|
68
|
|
|
{ |
|
69
|
|
|
$this->addVat = false; |
|
70
|
|
|
|
|
71
|
|
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function addItem($name, Money $value, array $options = []): self |
|
75
|
|
|
{ |
|
76
|
|
|
$quantity = $options['quantity'] ?? 1; |
|
77
|
|
|
$vat = $options['vat'] ?? $this->vatRate; |
|
78
|
|
|
$type = $options['type'] ?? ''; |
|
79
|
|
|
$description = $options['description'] ?? ''; |
|
80
|
|
|
|
|
81
|
|
|
$item = new Item($name, $description, $value, $quantity, $vat, $this->currency); |
|
82
|
|
|
$item->setType($type); |
|
83
|
|
|
|
|
84
|
|
|
$this->items[] = $item; |
|
85
|
|
|
|
|
86
|
|
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function build(): Invoice |
|
90
|
|
|
{ |
|
91
|
|
|
$invoice = new Invoice($this->addVat); |
|
92
|
|
|
$invoice->setCurrency($this->currency); |
|
93
|
|
|
|
|
94
|
|
|
if (isset($this->invoiceNumber)) { |
|
95
|
|
|
$invoice->setInvoiceNumber($this->invoiceNumber); |
|
96
|
|
|
} elseif (isset($this->invoiceNumberGenerator)) { |
|
97
|
|
|
$invoice->setInvoiceNumber($this->invoiceNumberGenerator->generateNumber()); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if (isset($this->freeHandAddress)) { |
|
101
|
|
|
$invoice->setFreeHandAddress($this->freeHandAddress); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
if (isset($this->billerAddress)) { |
|
105
|
|
|
$invoice->setBillerAddress($this->billerAddress); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
if (isset($this->createdAt)) { |
|
109
|
|
|
$invoice->setCreatedAt($this->createdAt); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
if (isset($this->vatNumber)) { |
|
113
|
|
|
$invoice->setVatNumber($this->vatNumber); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
if (isset($this->paymentDetails)) { |
|
117
|
|
|
$invoice->setPaymentDetails($this->paymentDetails); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
foreach ($this->items as $item) { |
|
121
|
|
|
if (isset($this->billerAddress) && isset($this->countryRules)) { |
|
122
|
|
|
$this->countryRules->handleRules($item, $this->billerAddress); |
|
123
|
|
|
} |
|
124
|
|
|
$invoice->addItem($item); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $invoice; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function addVatNumber(string $vatNumber) |
|
131
|
|
|
{ |
|
132
|
|
|
$this->vatNumber = $vatNumber; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function setBillerAddress(Address $address) |
|
136
|
|
|
{ |
|
137
|
|
|
$this->billerAddress = $address; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function setCountryRules(CountryRules $countryRules): void |
|
141
|
|
|
{ |
|
142
|
|
|
$this->countryRules = $countryRules; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function setCreatedAt(DateTime $createdAt): void |
|
146
|
|
|
{ |
|
147
|
|
|
$this->createdAt = $createdAt; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public function setCurrency(string $currency): void |
|
151
|
|
|
{ |
|
152
|
|
|
$this->currency = $currency; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function setFreeHandAddress(string $freeHandAddress): void |
|
156
|
|
|
{ |
|
157
|
|
|
$this->freeHandAddress = $freeHandAddress; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
public function setPaymentDetails(string $paymentDetails): void |
|
161
|
|
|
{ |
|
162
|
|
|
$this->paymentDetails = $paymentDetails; |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|