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