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 DateTimeImmutable */ |
49
|
|
|
protected $time; |
50
|
|
|
|
51
|
|
|
/** @var SaleInterface */ |
52
|
|
|
protected $sale; |
53
|
|
|
|
54
|
|
|
/** @var ActionState */ |
55
|
|
|
protected $state; |
56
|
|
|
|
57
|
|
|
/** @var ActionInterface */ |
58
|
|
|
protected $parent; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param TypeInterface $type |
62
|
|
|
* @param TargetInterface $target |
63
|
|
|
* @param QuantityInterface $quantity |
64
|
|
|
* @param CustomerInterface $customer |
65
|
|
|
* @param SaleInterface $sale |
66
|
25 |
|
* @param DateTimeImmutable $time |
67
|
|
|
* @param ActionInterface $parent |
68
|
|
|
*/ |
69
|
|
|
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
|
25 |
|
ActionState $state = null, |
78
|
25 |
|
ActionInterface $parent = null |
79
|
25 |
|
) { |
80
|
25 |
|
$this->id = $id; |
81
|
25 |
|
$this->type = $type; |
82
|
25 |
|
$this->target = $target; |
83
|
25 |
|
$this->quantity = $quantity; |
84
|
25 |
|
$this->customer = $customer; |
85
|
25 |
|
$this->time = $time; |
86
|
25 |
|
$this->sale = $sale; |
87
|
|
|
$this->state = $state; |
88
|
|
|
$this->parent = $parent; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Provides unique string. |
93
|
|
|
* Can be used to compare or aggregate actions. |
94
|
|
|
*/ |
95
|
|
|
public function getUniqueString(): string |
96
|
|
|
{ |
97
|
|
|
$parts = [ |
98
|
|
|
'buyer' => $this->customer->getUniqueId(), |
99
|
|
|
'target' => $this->target ? $this->target->getUniqueId() : null, |
100
|
|
|
'type' => $this->type->getUniqueId(), |
101
|
|
|
'time' => $this->time->format('c'), |
102
|
|
|
]; |
103
|
|
|
|
104
|
|
|
return implode('-', $parts); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function createSubaction(CustomerInterface $customer) |
108
|
|
|
{ |
109
|
|
|
return new static(null, $this->type, $this->target, $this->quantity, $customer, $this->time, $this->sale, $this->state, $this); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
*/ |
115
|
|
|
public function getId() |
116
|
|
|
{ |
117
|
|
|
return $this->id; |
118
|
|
|
} |
119
|
|
|
|
120
|
1 |
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
1 |
|
*/ |
123
|
|
|
public function getCustomer(): CustomerInterface |
124
|
|
|
{ |
125
|
|
|
return $this->customer; |
126
|
|
|
} |
127
|
|
|
|
128
|
21 |
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
21 |
|
*/ |
131
|
|
|
public function getTarget(): TargetInterface |
132
|
|
|
{ |
133
|
|
|
return $this->target; |
134
|
|
|
} |
135
|
|
|
|
136
|
21 |
|
/** |
137
|
|
|
* {@inheritdoc} |
138
|
21 |
|
*/ |
139
|
|
|
public function getType(): TypeInterface |
140
|
|
|
{ |
141
|
|
|
return $this->type; |
142
|
|
|
} |
143
|
|
|
|
144
|
21 |
|
/** |
145
|
|
|
* {@inheritdoc} |
146
|
21 |
|
*/ |
147
|
|
|
public function getQuantity(): QuantityInterface |
148
|
|
|
{ |
149
|
|
|
return $this->quantity; |
150
|
|
|
} |
151
|
|
|
|
152
|
2 |
|
/** |
153
|
|
|
* {@inheritdoc} |
154
|
2 |
|
*/ |
155
|
|
|
public function getSale(): ?SaleInterface |
156
|
|
|
{ |
157
|
|
|
return $this->sale; |
158
|
|
|
} |
159
|
|
|
|
160
|
14 |
|
/** |
161
|
|
|
* {@inheritdoc} |
162
|
14 |
|
*/ |
163
|
|
|
public function getTime(): DateTimeImmutable |
164
|
|
|
{ |
165
|
|
|
return $this->time; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function setTime(DateTimeImmutable $time) |
169
|
|
|
{ |
170
|
|
|
$this->time = $time; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function getState(): ?ActionState |
174
|
|
|
{ |
175
|
|
|
return $this->state; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function setFinished(): void |
179
|
|
|
{ |
180
|
4 |
|
$this->state = ActionState::finished(); |
181
|
|
|
} |
182
|
4 |
|
|
183
|
|
|
public function isFinished(): ?bool |
184
|
|
|
{ |
185
|
|
|
return $this->state === null ? null : $this->state->isFinished(); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* {@inheritdoc} |
190
|
|
|
*/ |
191
|
|
|
public function getParent(): ?ActionInterface |
192
|
|
|
{ |
193
|
|
|
return $this->parent; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* {@inheritdoc} |
198
|
|
|
*/ |
199
|
|
|
public function hasParent() |
200
|
|
|
{ |
201
|
|
|
return $this->parent !== null; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function hasId() |
205
|
|
|
{ |
206
|
|
|
return $this->id !== null; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function setId($id) |
210
|
|
|
{ |
211
|
|
|
if ((string) $this->id === (string) $id) { |
212
|
|
|
return; |
213
|
|
|
} |
214
|
|
|
if ($this->hasId()) { |
215
|
|
|
throw new \Exception('cannot reassign action id'); |
216
|
|
|
} |
217
|
8 |
|
$this->id = $id; |
218
|
|
|
} |
219
|
8 |
|
|
220
|
|
|
public function hasSale() |
221
|
|
|
{ |
222
|
8 |
|
return $this->sale !== null; |
223
|
|
|
} |
224
|
8 |
|
|
225
|
|
|
public function setSale(SaleInterface $sale) |
226
|
|
|
{ |
227
|
8 |
|
if ($this->hasSale()) { |
228
|
8 |
|
throw new \Exception('cannot reassign sale for action'); |
229
|
|
|
} |
230
|
|
|
$this->sale = $sale; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* {@inheritdoc} |
235
|
|
|
*/ |
236
|
|
|
public function jsonSerialize() |
237
|
|
|
{ |
238
|
|
|
return get_object_vars($this); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|