|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* PHP Billing Library |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/hiqdev/php-billing |
|
6
|
|
|
* @package php-billing |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2017-2018, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hiqdev\php\billing\bill; |
|
12
|
|
|
|
|
13
|
|
|
use DateTimeImmutable; |
|
14
|
|
|
use hiqdev\php\billing\charge\ChargeInterface; |
|
15
|
|
|
use hiqdev\php\billing\customer\CustomerInterface; |
|
16
|
|
|
use hiqdev\php\billing\plan\PlanInterface; |
|
17
|
|
|
use hiqdev\php\billing\target\TargetInterface; |
|
18
|
|
|
use hiqdev\php\billing\type\TypeInterface; |
|
19
|
|
|
use hiqdev\php\units\QuantityInterface; |
|
20
|
|
|
use Money\Money; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Bill. |
|
24
|
|
|
* |
|
25
|
|
|
* @author Andrii Vasyliev <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
class Bill implements BillInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var int|string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $id; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var TypeInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $type; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var DateTimeImmutable |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $time; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var Money |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $sum; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var QuantityInterface |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $quantity; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var CustomerInterface |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $customer; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var TargetInterface |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $target; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @var PlanInterface |
|
66
|
|
|
*/ |
|
67
|
|
|
protected $plan; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @var bool |
|
71
|
|
|
*/ |
|
72
|
|
|
protected $isFinished = false; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @var ChargeInterface[] |
|
76
|
|
|
*/ |
|
77
|
|
|
protected $charges = []; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @var string |
|
81
|
|
|
*/ |
|
82
|
|
|
protected $comment; |
|
83
|
|
|
|
|
84
|
1 |
|
public function __construct( |
|
85
|
|
|
$id, |
|
86
|
|
|
TypeInterface $type, |
|
87
|
|
|
DateTimeImmutable $time, |
|
88
|
|
|
Money $sum, |
|
89
|
|
|
QuantityInterface $quantity, |
|
90
|
|
|
CustomerInterface $customer, |
|
91
|
|
|
TargetInterface $target = null, |
|
92
|
|
|
PlanInterface $plan = null, |
|
93
|
|
|
array $charges = [], |
|
94
|
|
|
bool $isFinished = false |
|
95
|
|
|
) { |
|
96
|
1 |
|
$this->id = $id; |
|
97
|
1 |
|
$this->type = $type; |
|
98
|
1 |
|
$this->time = $time; |
|
99
|
1 |
|
$this->sum = $sum; |
|
100
|
1 |
|
$this->quantity = $quantity; |
|
101
|
1 |
|
$this->customer = $customer; |
|
102
|
1 |
|
$this->target = $target; |
|
103
|
1 |
|
$this->plan = $plan; |
|
104
|
1 |
|
$this->charges = $charges; |
|
105
|
1 |
|
$this->isFinished = $isFinished; |
|
106
|
1 |
|
} |
|
107
|
|
|
|
|
108
|
1 |
|
public function getUniqueString() |
|
109
|
|
|
{ |
|
110
|
|
|
$parts = [ |
|
111
|
1 |
|
'buyer' => $this->customer->getUniqueId(), |
|
112
|
1 |
|
'currency' => $this->sum->getCurrency()->getCode(), |
|
113
|
1 |
|
'target' => $this->target ? $this->target->getUniqueId() : null, |
|
114
|
1 |
|
'type' => $this->type->getUniqueId(), |
|
|
|
|
|
|
115
|
1 |
|
'time' => $this->time->format('c'), |
|
116
|
1 |
|
'plan' => $this->plan ? $this->plan->getUniqueId() : null, |
|
|
|
|
|
|
117
|
|
|
]; |
|
118
|
|
|
|
|
119
|
1 |
|
return implode('-', $parts); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function calculatePrice() |
|
123
|
|
|
{ |
|
124
|
|
|
$quantity = $this->quantity->getQuantity(); |
|
125
|
|
|
|
|
126
|
|
|
return $quantity ? $this->sum->divide($quantity) : $this->sum; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return int|string |
|
131
|
|
|
*/ |
|
132
|
|
|
public function getId() |
|
133
|
|
|
{ |
|
134
|
|
|
return $this->id; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function setId($id) |
|
138
|
|
|
{ |
|
139
|
|
|
if ($this->id === $id) { |
|
140
|
|
|
return; |
|
141
|
|
|
} |
|
142
|
|
|
if ($this->id !== null) { |
|
143
|
|
|
throw new \Exception('cannot reassign bill id'); |
|
144
|
|
|
} |
|
145
|
|
|
$this->id = $id; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @return TypeInterface |
|
150
|
|
|
*/ |
|
151
|
1 |
|
public function getType() |
|
152
|
|
|
{ |
|
153
|
1 |
|
return $this->type; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @return DateTimeImmutable |
|
158
|
|
|
*/ |
|
159
|
1 |
|
public function getTime() |
|
160
|
|
|
{ |
|
161
|
1 |
|
return $this->time; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @return TargetInterface |
|
166
|
|
|
*/ |
|
167
|
1 |
|
public function getTarget() |
|
168
|
|
|
{ |
|
169
|
1 |
|
return $this->target; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @return CustomerInterface |
|
174
|
|
|
*/ |
|
175
|
1 |
|
public function getCustomer() |
|
176
|
|
|
{ |
|
177
|
1 |
|
return $this->customer; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @return QuantityInterface |
|
182
|
|
|
*/ |
|
183
|
1 |
|
public function getQuantity() |
|
184
|
|
|
{ |
|
185
|
1 |
|
return $this->quantity; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @return Money |
|
190
|
|
|
*/ |
|
191
|
1 |
|
public function getSum() |
|
192
|
|
|
{ |
|
193
|
1 |
|
return $this->sum; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* @return PlanInterface |
|
198
|
|
|
*/ |
|
199
|
1 |
|
public function getPlan() |
|
200
|
|
|
{ |
|
201
|
1 |
|
return $this->plan; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* @return ChargeInterface[] |
|
206
|
|
|
*/ |
|
207
|
1 |
|
public function getCharges() |
|
208
|
|
|
{ |
|
209
|
1 |
|
return $this->charges; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @return bool |
|
214
|
|
|
*/ |
|
215
|
|
|
public function getIsFinished() |
|
216
|
|
|
{ |
|
217
|
|
|
return $this->isFinished; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
public function getComment() |
|
221
|
|
|
{ |
|
222
|
|
|
return $this->comment; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
public function setComment(string $comment) |
|
226
|
|
|
{ |
|
227
|
|
|
$this->comment = $comment; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
public function jsonSerialize() |
|
231
|
|
|
{ |
|
232
|
|
|
return get_object_vars($this); |
|
233
|
|
|
} |
|
234
|
|
|
} |
|
235
|
|
|
|