|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* Copyright (C) 2020-2025 Iain Cambridge |
|
7
|
|
|
* |
|
8
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
9
|
|
|
* it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as published by |
|
10
|
|
|
* the Free Software Foundation, either version 2.1 of the License, or |
|
11
|
|
|
* (at your option) any later version. |
|
12
|
|
|
* |
|
13
|
|
|
* This program is distributed in the hope that it will be useful, |
|
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16
|
|
|
* GNU Lesser General Public License for more details. |
|
17
|
|
|
* |
|
18
|
|
|
* You should have received a copy of the GNU General Public License |
|
19
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
namespace Parthenon\Invoice; |
|
23
|
|
|
|
|
24
|
|
|
use Brick\Math\RoundingMode; |
|
25
|
|
|
use Brick\Money\Context\AutoContext; |
|
26
|
|
|
use Brick\Money\Context\DefaultContext; |
|
27
|
|
|
use Brick\Money\Money; |
|
28
|
|
|
use Parthenon\Common\Address; |
|
29
|
|
|
|
|
30
|
|
|
final class Invoice |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @var ItemInterface[] |
|
34
|
|
|
*/ |
|
35
|
|
|
private array $items = []; |
|
36
|
|
|
private string $vatNumber; |
|
37
|
|
|
private ?Address $billerAddress = null; |
|
38
|
|
|
private Address $billingAddress; |
|
39
|
|
|
private Address $shippingAddress; |
|
40
|
|
|
private bool $addVat; |
|
41
|
|
|
private ?\DateTimeInterface $createdAt = null; |
|
42
|
|
|
private $invoiceNumber; |
|
43
|
|
|
private string $currency = 'EUR'; |
|
44
|
|
|
private string $deliveryAddress = ''; |
|
45
|
|
|
private string $paymentDetails = ''; |
|
46
|
|
|
|
|
47
|
|
|
public function __construct(bool $addVat = true) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->addVat = $addVat; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function addItem(ItemInterface $item) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->items[] = $item; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function getTotal(): string |
|
58
|
|
|
{ |
|
59
|
|
|
$total = $this->calculateTotal(); |
|
60
|
|
|
if (!isset($this->vatNumber)) { |
|
61
|
|
|
if ($this->addVat) { |
|
62
|
|
|
$total = $total->plus($this->calculateVat()); |
|
63
|
|
|
} else { |
|
64
|
|
|
$total = $total->minus($this->calculateVat()); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $total->getAmount()->__toString(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function getSubTotal(): string |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->calculateTotal()->getAmount()->__toString(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function getVat(): string |
|
77
|
|
|
{ |
|
78
|
|
|
if (isset($this->vatNumber)) { |
|
79
|
|
|
return Money::zero($this->currency)->getAmount()->__toString(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$vatAmount = $this->calculateVat(); |
|
83
|
|
|
|
|
84
|
|
|
return $vatAmount->getAmount()->__toString(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function setVatNumber(string $vatNumber) |
|
88
|
|
|
{ |
|
89
|
|
|
$this->vatNumber = $vatNumber; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return ItemInterface[] |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getItems(): array |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->items; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function setBillerAddress(Address $billerAddress): void |
|
101
|
|
|
{ |
|
102
|
|
|
$this->billerAddress = $billerAddress; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function getBillerAddress(): ?Address |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->billerAddress; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function getBillingAddress(): ?Address |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->billingAddress; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function setBillingAddress(Address $billingAddress): void |
|
116
|
|
|
{ |
|
117
|
|
|
$this->billingAddress = $billingAddress; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function getShippingAddress(): Address |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->shippingAddress; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function setShippingAddress(Address $shippingAddress): void |
|
126
|
|
|
{ |
|
127
|
|
|
$this->shippingAddress = $shippingAddress; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function setCreatedAt(\DateTimeInterface $createdAt) |
|
131
|
|
|
{ |
|
132
|
|
|
$this->createdAt = $createdAt; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function getCreatedAt(): ?\DateTimeInterface |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->createdAt; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function setCurrency(string $currency): void |
|
141
|
|
|
{ |
|
142
|
|
|
$this->currency = $currency; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function getDeliveryAddress(): string |
|
146
|
|
|
{ |
|
147
|
|
|
return $this->deliveryAddress; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public function setDeliveryAddress(string $deliveryAddress): void |
|
151
|
|
|
{ |
|
152
|
|
|
$this->deliveryAddress = $deliveryAddress; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function getInvoiceNumber() |
|
156
|
|
|
{ |
|
157
|
|
|
return $this->invoiceNumber; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
public function setInvoiceNumber($invoiceNumber): void |
|
161
|
|
|
{ |
|
162
|
|
|
$this->invoiceNumber = $invoiceNumber; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
public function getPaymentDetails(): string |
|
166
|
|
|
{ |
|
167
|
|
|
return $this->paymentDetails; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function setPaymentDetails(string $paymentDetails): void |
|
171
|
|
|
{ |
|
172
|
|
|
$this->paymentDetails = $paymentDetails; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
private function calculateTotal(): Money |
|
176
|
|
|
{ |
|
177
|
|
|
$total = Money::zero($this->currency); |
|
178
|
|
|
|
|
179
|
|
|
foreach ($this->items as $item) { |
|
180
|
|
|
$total = $total->plus($item->getSubTotal()); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
return $total; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
private function calculateVat(): Money |
|
187
|
|
|
{ |
|
188
|
|
|
$vat = Money::zero($this->currency, new AutoContext()); |
|
189
|
|
|
|
|
190
|
|
|
foreach ($this->items as $item) { |
|
191
|
|
|
$vat = $vat->plus($item->getVat()); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
$vat = $vat->to(new DefaultContext(), RoundingMode::HALF_UP); |
|
195
|
|
|
|
|
196
|
|
|
return $vat; |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|