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\charge; |
12
|
|
|
|
13
|
|
|
use hiqdev\php\billing\action\ActionInterface; |
14
|
|
|
use hiqdev\php\billing\bill\BillInterface; |
15
|
|
|
use hiqdev\php\billing\event\EventAwareTrait; |
16
|
|
|
use hiqdev\php\billing\price\PriceInterface; |
17
|
|
|
use hiqdev\php\billing\type\TypeInterface; |
18
|
|
|
use hiqdev\php\billing\target\TargetInterface; |
19
|
|
|
use hiqdev\php\units\QuantityInterface; |
20
|
|
|
use Money\Money; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Charge. |
24
|
|
|
* |
25
|
|
|
* [[Action]] is charged with a number of [[Charge]]s. |
26
|
|
|
* |
27
|
|
|
* @author Andrii Vasyliev <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class Charge implements ChargeInterface |
30
|
|
|
{ |
31
|
|
|
use EventAwareTrait; |
32
|
|
|
|
33
|
|
|
/** @var int */ |
34
|
|
|
protected $id; |
35
|
|
|
|
36
|
|
|
/** @var TypeInterface */ |
37
|
|
|
protected $type; |
38
|
|
|
|
39
|
|
|
/** @var TargetInterface */ |
40
|
|
|
protected $target; |
41
|
|
|
|
42
|
|
|
/** @var ActionInterface */ |
43
|
|
|
protected $action; |
44
|
|
|
|
45
|
|
|
/** @var PriceInterface */ |
46
|
|
|
protected $price; |
47
|
|
|
|
48
|
|
|
/** @var QuantityInterface */ |
49
|
|
|
protected $usage; |
50
|
|
|
|
51
|
|
|
/** @var Money */ |
52
|
|
|
protected $sum; |
53
|
|
|
|
54
|
|
|
/** @var BillInterface */ |
55
|
|
|
protected $bill; |
56
|
|
|
|
57
|
|
|
/** @var ChargeState */ |
58
|
|
|
protected $state; |
59
|
|
|
|
60
|
|
|
/** @var string */ |
61
|
|
|
protected $comment; |
62
|
|
|
|
63
|
|
|
/** @var ChargeInterface|null */ |
64
|
|
|
protected $parent; |
65
|
|
|
|
66
|
17 |
|
public function __construct( |
67
|
|
|
$id, |
68
|
|
|
TypeInterface $type, |
69
|
|
|
TargetInterface $target, |
70
|
|
|
ActionInterface $action, |
71
|
|
|
PriceInterface $price, |
72
|
|
|
QuantityInterface $usage, |
73
|
|
|
Money $sum, |
74
|
|
|
BillInterface $bill = null |
75
|
|
|
) { |
76
|
17 |
|
$this->id = $id; |
77
|
17 |
|
$this->type = $type; |
78
|
17 |
|
$this->target = $target; |
79
|
17 |
|
$this->action = $action; |
80
|
17 |
|
$this->price = $price; |
81
|
17 |
|
$this->usage = $usage; |
82
|
17 |
|
$this->sum = $sum; |
83
|
17 |
|
$this->bill = $bill; |
84
|
17 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
|
|
public function getUniqueString(): string |
90
|
|
|
{ |
91
|
|
|
return implode('-', [ |
92
|
|
|
'currency' => $this->sum->getCurrency()->getCode(), |
93
|
|
|
'buyer' => $this->action->getCustomer()->getUniqueId(), |
94
|
|
|
'target' => $this->target->getUniqueId(), |
95
|
|
|
'type' => $this->type->getUniqueId(), |
96
|
|
|
'time' => $this->action->getTime()->format('c'), |
97
|
|
|
'parent' => '(' . ($this->parent !== null ? $this->parent->getUniqueString() : null) . ')', |
98
|
|
|
]); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function hasId(): bool |
102
|
|
|
{ |
103
|
|
|
return $this->id !== null; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function getId() |
107
|
|
|
{ |
108
|
|
|
return $this->id; |
109
|
|
|
} |
110
|
|
|
|
111
|
8 |
|
public function getType(): TypeInterface |
112
|
|
|
{ |
113
|
8 |
|
return $this->type; |
114
|
|
|
} |
115
|
|
|
|
116
|
6 |
|
public function getTarget(): TargetInterface |
117
|
|
|
{ |
118
|
6 |
|
return $this->target; |
119
|
|
|
} |
120
|
|
|
|
121
|
11 |
|
public function getAction(): ActionInterface |
122
|
|
|
{ |
123
|
11 |
|
return $this->action; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return PriceInterface |
128
|
|
|
*/ |
129
|
6 |
|
public function getPrice(): PriceInterface |
130
|
|
|
{ |
131
|
6 |
|
return $this->price; |
132
|
|
|
} |
133
|
|
|
|
134
|
13 |
|
public function getUsage(): QuantityInterface |
135
|
|
|
{ |
136
|
13 |
|
return $this->usage; |
137
|
|
|
} |
138
|
|
|
|
139
|
12 |
|
public function getSum(): Money |
140
|
|
|
{ |
141
|
12 |
|
return $this->sum; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function calculatePrice(): Money |
145
|
|
|
{ |
146
|
|
|
$usage = $this->usage->getQuantity(); |
147
|
|
|
|
148
|
|
|
return $usage ? $this->sum->divide($usage) : $this->sum; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function getBill(): ?BillInterface |
152
|
|
|
{ |
153
|
|
|
return $this->bill; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function hasBill() |
157
|
|
|
{ |
158
|
|
|
return $this->bill !== null; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function setBill(BillInterface $bill): ChargeInterface |
162
|
|
|
{ |
163
|
|
|
/*if ($this->hasBill()) { |
164
|
|
|
throw new \Exception('cannot reassign sale bill'); |
165
|
|
|
}*/ |
166
|
|
|
$this->bill = $bill; |
167
|
|
|
|
168
|
|
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function getState(): ?ChargeState |
172
|
|
|
{ |
173
|
|
|
return $this->state; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function setFinished(): ChargeInterface |
177
|
|
|
{ |
178
|
|
|
$this->state = ChargeState::finished(); |
179
|
|
|
|
180
|
|
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function isFinished(): ?bool |
184
|
|
|
{ |
185
|
|
|
return $this->state === null ? null : $this->state->isFinished(); |
186
|
|
|
} |
187
|
|
|
|
188
|
1 |
|
public function getComment(): ?string |
189
|
|
|
{ |
190
|
1 |
|
return $this->comment; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function setComment(string $comment): ChargeInterface |
194
|
|
|
{ |
195
|
|
|
$this->comment = $comment; |
196
|
|
|
|
197
|
|
|
return $this; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function setId($id): ChargeInterface |
201
|
|
|
{ |
202
|
|
|
if ((string)$this->id === (string)$id) { |
203
|
|
|
return $this; |
204
|
|
|
} |
205
|
|
|
if ($this->id !== null) { |
206
|
|
|
throw new \Exception('cannot reassign charge id'); |
207
|
|
|
} |
208
|
|
|
$this->id = $id; |
209
|
|
|
|
210
|
|
|
return $this; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function jsonSerialize() |
214
|
|
|
{ |
215
|
|
|
return get_object_vars($this); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @return ChargeInterface|null |
220
|
|
|
*/ |
221
|
|
|
public function getParent(): ?ChargeInterface |
222
|
|
|
{ |
223
|
|
|
return $this->parent; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @param ChargeInterface|null $parent |
228
|
|
|
* |
229
|
|
|
* @return Charge |
230
|
|
|
* @throws \Exception if parent is already set |
231
|
|
|
*/ |
232
|
4 |
|
public function setParent(ChargeInterface $parent): self |
233
|
|
|
{ |
234
|
4 |
|
if ($this->parent !== null) { |
235
|
|
|
throw new \Exception('cannot reassign charge parent'); |
236
|
|
|
} |
237
|
|
|
|
238
|
4 |
|
$this->parent = $parent; |
239
|
|
|
|
240
|
4 |
|
return $this; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Forcefully changes parent charge |
245
|
|
|
* |
246
|
|
|
* @param ChargeInterface $parent |
247
|
|
|
* @return Charge |
248
|
|
|
*/ |
249
|
|
|
public function overwriteParent(ChargeInterface $parent): self |
250
|
|
|
{ |
251
|
|
|
$this->parent = $parent; |
252
|
|
|
|
253
|
|
|
return $this; |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|