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\price\PriceInterface; |
16
|
|
|
use hiqdev\php\units\QuantityInterface; |
17
|
|
|
use Money\Money; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Charge. |
21
|
|
|
* |
22
|
|
|
* [[Action]] is charged with a number of [[Charge]]s. |
23
|
|
|
* |
24
|
|
|
* @author Andrii Vasyliev <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class Charge implements ChargeInterface |
27
|
|
|
{ |
28
|
|
|
/** @var int */ |
29
|
|
|
protected $id; |
30
|
|
|
|
31
|
|
|
/** @var ActionInterface */ |
32
|
|
|
protected $action; |
33
|
|
|
|
34
|
|
|
/** @var PriceInterface */ |
35
|
|
|
protected $price; |
36
|
|
|
|
37
|
|
|
/** @var QuantityInterface */ |
38
|
|
|
protected $usage; |
39
|
|
|
|
40
|
|
|
/** @var Money */ |
41
|
|
|
protected $sum; |
42
|
|
|
|
43
|
|
|
/** @var BillInterface */ |
44
|
|
|
protected $bill; |
45
|
|
|
|
46
|
|
|
/** @var ChargeState */ |
47
|
|
|
protected $state; |
48
|
|
|
|
49
|
|
|
/** @var string */ |
50
|
|
|
protected $comment; |
51
|
|
|
|
52
|
|
|
/** @var ChargeInterface|null */ |
53
|
|
|
protected $parent; |
54
|
|
|
|
55
|
17 |
|
public function __construct( |
56
|
|
|
$id, |
57
|
|
|
ActionInterface $action, |
58
|
|
|
PriceInterface $price, |
59
|
|
|
QuantityInterface $usage, |
60
|
|
|
Money $sum, |
61
|
|
|
BillInterface $bill = null |
62
|
|
|
) { |
63
|
17 |
|
$this->id = $id; |
64
|
17 |
|
$this->action = $action; |
65
|
17 |
|
$this->price = $price; |
66
|
17 |
|
$this->usage = $usage; |
67
|
17 |
|
$this->sum = $sum; |
68
|
17 |
|
$this->bill = $bill; |
69
|
17 |
|
} |
70
|
|
|
|
71
|
|
|
public function getId() |
72
|
|
|
{ |
73
|
|
|
return $this->id; |
74
|
|
|
} |
75
|
|
|
|
76
|
10 |
|
public function getAction() |
77
|
|
|
{ |
78
|
10 |
|
return $this->action; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return PriceInterface |
83
|
|
|
*/ |
84
|
8 |
|
public function getPrice() |
85
|
|
|
{ |
86
|
8 |
|
return $this->price; |
87
|
|
|
} |
88
|
|
|
|
89
|
12 |
|
public function getUsage() |
90
|
|
|
{ |
91
|
12 |
|
return $this->usage; |
92
|
|
|
} |
93
|
|
|
|
94
|
12 |
|
public function getSum() |
95
|
|
|
{ |
96
|
12 |
|
return $this->sum; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function calculatePrice() |
100
|
|
|
{ |
101
|
|
|
$usage = $this->usage->getQuantity(); |
102
|
|
|
|
103
|
|
|
return $usage ? $this->sum->divide($usage) : $this->sum; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function getBill() |
107
|
|
|
{ |
108
|
|
|
return $this->bill; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function hasBill() |
112
|
|
|
{ |
113
|
|
|
return $this->bill !== null; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function setBill(BillInterface $bill): ChargeInterface |
117
|
|
|
{ |
118
|
|
|
if ($this->hasBill()) { |
119
|
|
|
throw new \Exception('cannot reassign sale bill'); |
120
|
|
|
} |
121
|
|
|
$this->bill = $bill; |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function getState(): ?ChargeState |
127
|
|
|
{ |
128
|
|
|
return $this->state; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function setFinished(): ChargeInterface |
132
|
|
|
{ |
133
|
|
|
$this->state = ChargeState::finished(); |
134
|
|
|
|
135
|
|
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function isFinished(): ?bool |
139
|
|
|
{ |
140
|
|
|
return $this->state === null ? null : $this->state->isFinished(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function getComment() |
144
|
|
|
{ |
145
|
|
|
return $this->comment; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function setComment(string $comment): ChargeInterface |
149
|
|
|
{ |
150
|
|
|
$this->comment = $comment; |
151
|
|
|
|
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function setId($id): ChargeInterface |
156
|
|
|
{ |
157
|
|
|
if ((string)$this->id === (string)$id) { |
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
|
|
if ($this->id !== null) { |
161
|
|
|
throw new \Exception('cannot reassign charge id'); |
162
|
|
|
} |
163
|
|
|
$this->id = $id; |
164
|
|
|
|
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function jsonSerialize() |
169
|
|
|
{ |
170
|
|
|
return get_object_vars($this); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @return ChargeInterface|null |
175
|
|
|
*/ |
176
|
|
|
public function getParent(): ?ChargeInterface |
177
|
|
|
{ |
178
|
|
|
return $this->parent; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param ChargeInterface|null $parent |
183
|
|
|
* |
184
|
|
|
* @return Charge |
185
|
|
|
* @throws \Exception if parent is already set |
186
|
|
|
*/ |
187
|
4 |
|
public function setParent(ChargeInterface $parent): self |
188
|
|
|
{ |
189
|
4 |
|
if ($this->parent !== null) { |
190
|
|
|
throw new \Exception('cannot reassign charge parent'); |
191
|
|
|
} |
192
|
|
|
|
193
|
4 |
|
$this->parent = $parent; |
194
|
|
|
|
195
|
4 |
|
return $this; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|