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