|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace byrokrat\billing; |
|
6
|
|
|
|
|
7
|
|
|
use byrokrat\amount\Amount; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Generic invoice container object |
|
11
|
|
|
*/ |
|
12
|
|
|
class Invoice |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var string Invoice serial number |
|
16
|
|
|
*/ |
|
17
|
|
|
private $serial; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var Seller Registered seller |
|
21
|
|
|
*/ |
|
22
|
|
|
private $seller; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var Buyer Registered buyer |
|
26
|
|
|
*/ |
|
27
|
|
|
private $buyer; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string Message to buyer |
|
31
|
|
|
*/ |
|
32
|
|
|
private $message; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string Payment reference number |
|
36
|
|
|
*/ |
|
37
|
|
|
private $ocr; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var ItemBasket Container for charged items |
|
41
|
|
|
*/ |
|
42
|
|
|
private $itemBasket = []; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var \DateTime Creation date |
|
46
|
|
|
*/ |
|
47
|
|
|
private $billDate; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var integer Number of days before invoice expires |
|
51
|
|
|
*/ |
|
52
|
|
|
private $expiresAfter; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var Amount Prepaid amound to deduct |
|
56
|
|
|
*/ |
|
57
|
|
|
private $deduction; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Construct invoice |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $serial Invoice serial number |
|
63
|
|
|
* @param Seller $seller Registered seller |
|
64
|
|
|
* @param Buyer $buyer Registered buyer |
|
65
|
|
|
* @param string $message Invoice message |
|
66
|
|
|
* @param string $ocr Payment reference number |
|
67
|
|
|
* @param ItemBasket $itemBasket Container for charged items |
|
68
|
|
|
* @param \DateTime $billDate Date of invoice creation |
|
69
|
|
|
* @param integer $expiresAfter Nr of days before invoice expires |
|
70
|
|
|
* @param Amount $deduction Prepaid amound to deduct |
|
71
|
|
|
*/ |
|
72
|
16 |
|
public function __construct( |
|
73
|
|
|
string $serial, |
|
74
|
|
|
Seller $seller, |
|
75
|
|
|
Buyer $buyer, |
|
76
|
|
|
string $message = '', |
|
77
|
|
|
string $ocr = '', |
|
78
|
|
|
ItemBasket $itemBasket = null, |
|
79
|
|
|
\DateTime $billDate = null, |
|
80
|
|
|
int $expiresAfter = 30, |
|
81
|
|
|
Amount $deduction = null |
|
82
|
|
|
) { |
|
83
|
16 |
|
$this->serial = $serial; |
|
84
|
16 |
|
$this->seller = $seller; |
|
85
|
16 |
|
$this->buyer = $buyer; |
|
86
|
16 |
|
$this->itemBasket = $itemBasket; |
|
87
|
16 |
|
$this->ocr = $ocr; |
|
88
|
16 |
|
$this->message = $message; |
|
89
|
16 |
|
$this->billDate = $billDate ?: new \DateTime; |
|
90
|
16 |
|
$this->expiresAfter = $expiresAfter; |
|
91
|
16 |
|
$this->deduction = $deduction; |
|
92
|
16 |
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Get invoice serial number |
|
96
|
|
|
*/ |
|
97
|
1 |
|
public function getSerial(): string |
|
98
|
|
|
{ |
|
99
|
1 |
|
return $this->serial; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Get seller |
|
104
|
|
|
*/ |
|
105
|
1 |
|
public function getSeller(): Seller |
|
106
|
|
|
{ |
|
107
|
1 |
|
return $this->seller; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Get buyer |
|
112
|
|
|
*/ |
|
113
|
1 |
|
public function getBuyer(): Buyer |
|
114
|
|
|
{ |
|
115
|
1 |
|
return $this->buyer; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Get invoice message |
|
120
|
|
|
*/ |
|
121
|
2 |
|
public function getMessage(): string |
|
122
|
|
|
{ |
|
123
|
2 |
|
return $this->message; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Get invoice reference number |
|
128
|
|
|
*/ |
|
129
|
4 |
|
public function getOcr(): string |
|
130
|
|
|
{ |
|
131
|
4 |
|
return $this->ocr; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Get item container |
|
136
|
|
|
*/ |
|
137
|
4 |
|
public function getItems(): ItemBasket |
|
138
|
|
|
{ |
|
139
|
4 |
|
return $this->itemBasket; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Get charged amount (including VAT and deduction) |
|
144
|
|
|
*/ |
|
145
|
3 |
|
public function getInvoiceTotal(): Amount |
|
146
|
|
|
{ |
|
147
|
3 |
|
return $this->getItems()->getTotalCost()->subtract($this->getDeduction()); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Get date of invoice creation |
|
152
|
|
|
*/ |
|
153
|
3 |
|
public function getBillDate(): \DateTime |
|
154
|
|
|
{ |
|
155
|
3 |
|
return $this->billDate; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Get number of days before invoice expires |
|
160
|
|
|
*/ |
|
161
|
2 |
|
public function getExpiresAfter(): int |
|
162
|
|
|
{ |
|
163
|
2 |
|
return $this->expiresAfter; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Get date when invoice expires |
|
168
|
|
|
*/ |
|
169
|
1 |
|
public function getExpirationDate(): \DateTime |
|
170
|
|
|
{ |
|
171
|
1 |
|
$expireDate = clone $this->billDate; |
|
172
|
1 |
|
$expireDate->add(new \DateInterval("P{$this->getExpiresAfter()}D")); |
|
173
|
|
|
|
|
174
|
1 |
|
return $expireDate; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Get prepaid amound to deduct |
|
179
|
|
|
*/ |
|
180
|
5 |
|
public function getDeduction(): Amount |
|
181
|
|
|
{ |
|
182
|
5 |
|
if (!isset($this->deduction)) { |
|
183
|
1 |
|
return $this->itemBasket->createCurrencyObject('0'); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
4 |
|
return $this->deduction; |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|