|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace byrokrat\billing; |
|
6
|
|
|
|
|
7
|
|
|
use byrokrat\amount\Amount; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Create complex invoices |
|
11
|
|
|
*/ |
|
12
|
|
|
class InvoiceBuilder |
|
13
|
|
|
{ |
|
14
|
|
|
use AttributesTrait; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var string Invoice serial number |
|
18
|
|
|
*/ |
|
19
|
|
|
private $serial; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var AgentInterface Registered seller |
|
23
|
|
|
*/ |
|
24
|
|
|
private $seller; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var AgentInterface Registered buyer |
|
28
|
|
|
*/ |
|
29
|
|
|
private $buyer; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string Payment reference number |
|
33
|
|
|
*/ |
|
34
|
|
|
private $ocr; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var OcrTools Tools for validating and creating ocr numbers |
|
38
|
|
|
*/ |
|
39
|
|
|
private $ocrTools; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var ItemBasket Container of charged items |
|
43
|
|
|
*/ |
|
44
|
|
|
private $itemBasket; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var \DateTimeImmutable Invoice creation date |
|
48
|
|
|
*/ |
|
49
|
|
|
private $billDate; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var int Number of days before invoice expires |
|
53
|
|
|
*/ |
|
54
|
|
|
private $expiresAfter; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var Amount Prepaid amound to deduct |
|
58
|
|
|
*/ |
|
59
|
|
|
private $deduction; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Reset values at construct |
|
63
|
|
|
*/ |
|
64
|
8 |
|
public function __construct(OcrTools $ocrTools = null) |
|
65
|
|
|
{ |
|
66
|
8 |
|
$this->ocrTools = $ocrTools ?: new OcrTools; |
|
67
|
8 |
|
$this->reset(); |
|
68
|
8 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Reset builder values |
|
72
|
|
|
*/ |
|
73
|
8 |
|
public function reset(): self |
|
74
|
|
|
{ |
|
75
|
8 |
|
$this->serial = null; |
|
76
|
8 |
|
$this->seller = null; |
|
77
|
8 |
|
$this->buyer = null; |
|
78
|
8 |
|
$this->ocr = ''; |
|
79
|
8 |
|
$this->itemBasket = new ItemBasket; |
|
80
|
8 |
|
$this->billDate = null; |
|
81
|
8 |
|
$this->expiresAfter = 30; |
|
82
|
8 |
|
$this->deduction = null; |
|
83
|
8 |
|
$this->clearAttributes(); |
|
84
|
8 |
|
return $this; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Build invoice |
|
89
|
|
|
*/ |
|
90
|
5 |
|
public function buildInvoice(): Invoice |
|
91
|
|
|
{ |
|
92
|
5 |
|
$invoice = new Invoice( |
|
93
|
5 |
|
$this->getSerial(), |
|
94
|
5 |
|
$this->getSeller(), |
|
95
|
5 |
|
$this->getBuyer(), |
|
96
|
5 |
|
$this->ocr, |
|
97
|
5 |
|
$this->itemBasket, |
|
98
|
5 |
|
$this->billDate ?: new \DateTimeImmutable, |
|
99
|
5 |
|
$this->expiresAfter, |
|
100
|
5 |
|
$this->deduction |
|
101
|
|
|
); |
|
102
|
|
|
|
|
103
|
5 |
|
foreach ($this->getAttributes() as $key => $value) { |
|
104
|
1 |
|
$invoice->setAttribute($key, $value); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
5 |
|
return $invoice; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Set invoice serial number |
|
112
|
|
|
*/ |
|
113
|
8 |
|
public function setSerial(string $serial): self |
|
114
|
|
|
{ |
|
115
|
8 |
|
$this->serial = $serial; |
|
116
|
8 |
|
return $this; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Get invoice serial number |
|
121
|
|
|
* |
|
122
|
|
|
* @throws Exception If serial is not set |
|
123
|
|
|
*/ |
|
124
|
6 |
|
public function getSerial(): string |
|
125
|
|
|
{ |
|
126
|
6 |
|
if (isset($this->serial)) { |
|
127
|
5 |
|
return $this->serial; |
|
128
|
|
|
} |
|
129
|
1 |
|
throw new Exception("Unable to create invoice: serial not set"); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Set seller |
|
134
|
|
|
*/ |
|
135
|
8 |
|
public function setSeller(AgentInterface $seller): self |
|
136
|
|
|
{ |
|
137
|
8 |
|
$this->seller = $seller; |
|
138
|
8 |
|
return $this; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Get seller |
|
143
|
|
|
* |
|
144
|
|
|
* @throws Exception If seller is not set |
|
145
|
|
|
*/ |
|
146
|
6 |
|
public function getSeller(): AgentInterface |
|
147
|
|
|
{ |
|
148
|
6 |
|
if (isset($this->seller)) { |
|
149
|
5 |
|
return $this->seller; |
|
150
|
|
|
} |
|
151
|
1 |
|
throw new Exception("Unable to create Invoice: seller not set"); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Set buyer |
|
156
|
|
|
*/ |
|
157
|
8 |
|
public function setBuyer(AgentInterface $buyer): self |
|
158
|
|
|
{ |
|
159
|
8 |
|
$this->buyer = $buyer; |
|
160
|
8 |
|
return $this; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Get buyer |
|
165
|
|
|
* |
|
166
|
|
|
* @throws Exception If buyer is not set |
|
167
|
|
|
*/ |
|
168
|
6 |
|
public function getBuyer(): AgentInterface |
|
169
|
|
|
{ |
|
170
|
6 |
|
if (isset($this->buyer)) { |
|
171
|
5 |
|
return $this->buyer; |
|
172
|
|
|
} |
|
173
|
1 |
|
throw new Exception("Unable to create Invoice: buyer not set"); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Set invoice reference number |
|
178
|
|
|
*/ |
|
179
|
1 |
|
public function setOcr(string $ocr): self |
|
180
|
|
|
{ |
|
181
|
1 |
|
$this->ocrTools->validate($ocr); |
|
182
|
1 |
|
$this->ocr = $ocr; |
|
183
|
1 |
|
return $this; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Generate invoice reference number from serial number |
|
188
|
|
|
*/ |
|
189
|
1 |
|
public function generateOcr(): self |
|
190
|
|
|
{ |
|
191
|
1 |
|
$this->ocr = $this->ocrTools->create($this->getSerial()); |
|
192
|
1 |
|
return $this; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Add billable to invoice |
|
197
|
|
|
*/ |
|
198
|
1 |
|
public function addItem(Billable $billable): self |
|
199
|
|
|
{ |
|
200
|
1 |
|
$this->itemBasket->addItem(new ItemEnvelope($billable)); |
|
201
|
1 |
|
return $this; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Set date of invoice creation |
|
206
|
|
|
*/ |
|
207
|
1 |
|
public function setBillDate(\DateTimeImmutable $date): self |
|
208
|
|
|
{ |
|
209
|
1 |
|
$this->billDate = $date; |
|
210
|
1 |
|
return $this; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Set number of days before invoice expires |
|
215
|
|
|
*/ |
|
216
|
1 |
|
public function setExpiresAfter(int $nrOfDays): self |
|
217
|
|
|
{ |
|
218
|
1 |
|
$this->expiresAfter = $nrOfDays; |
|
219
|
1 |
|
return $this; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Set deduction (amount prepaid) |
|
224
|
|
|
*/ |
|
225
|
1 |
|
public function setDeduction(Amount $deduction): self |
|
226
|
|
|
{ |
|
227
|
1 |
|
$this->deduction = $deduction; |
|
228
|
1 |
|
return $this; |
|
229
|
|
|
} |
|
230
|
|
|
} |
|
231
|
|
|
|