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
|
13 |
|
/** @var string */ |
50
|
|
|
protected $comment; |
51
|
|
|
|
52
|
|
|
public function __construct( |
53
|
|
|
$id, |
54
|
|
|
ActionInterface $action, |
55
|
|
|
PriceInterface $price, |
56
|
|
|
QuantityInterface $usage, |
57
|
13 |
|
Money $sum, |
58
|
13 |
|
BillInterface $bill = null |
59
|
13 |
|
) { |
60
|
13 |
|
$this->id = $id; |
61
|
13 |
|
$this->action = $action; |
62
|
13 |
|
$this->price = $price; |
63
|
13 |
|
$this->usage = $usage; |
64
|
|
|
$this->sum = $sum; |
65
|
|
|
$this->bill = $bill; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function getId() |
69
|
|
|
{ |
70
|
10 |
|
return $this->id; |
71
|
|
|
} |
72
|
10 |
|
|
73
|
|
|
public function getAction() |
74
|
|
|
{ |
75
|
|
|
return $this->action; |
76
|
|
|
} |
77
|
|
|
|
78
|
8 |
|
/** |
79
|
|
|
* @return PriceInterface |
80
|
8 |
|
*/ |
81
|
|
|
public function getPrice() |
82
|
|
|
{ |
83
|
12 |
|
return $this->price; |
84
|
|
|
} |
85
|
12 |
|
|
86
|
|
|
public function getUsage() |
87
|
|
|
{ |
88
|
12 |
|
return $this->usage; |
89
|
|
|
} |
90
|
12 |
|
|
91
|
|
|
public function getSum() |
92
|
|
|
{ |
93
|
|
|
return $this->sum; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function calculatePrice() |
97
|
|
|
{ |
98
|
|
|
$usage = $this->usage->getQuantity(); |
99
|
|
|
|
100
|
|
|
return $usage ? $this->sum->divide($usage) : $this->sum; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function getBill() |
104
|
|
|
{ |
105
|
|
|
return $this->bill; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function hasBill() |
109
|
|
|
{ |
110
|
|
|
return $this->bill !== null; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function setBill(BillInterface $bill): ChargeInterface |
114
|
|
|
{ |
115
|
|
|
if ($this->hasBill()) { |
116
|
|
|
throw new \Exception('cannot reassign sale bill'); |
117
|
|
|
} |
118
|
|
|
$this->bill = $bill; |
119
|
|
|
|
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function getState(): ?ChargeState |
124
|
|
|
{ |
125
|
|
|
return $this->state; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function setFinished(): ChargeInterface |
129
|
|
|
{ |
130
|
|
|
$this->state = ChargeState::finished(); |
131
|
|
|
|
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function isFinished(): ?bool |
136
|
|
|
{ |
137
|
|
|
return $this->state === null ? null : $this->state->isFinished(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function getComment() |
141
|
|
|
{ |
142
|
|
|
return $this->comment; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function setComment(string $comment): ChargeInterface |
146
|
|
|
{ |
147
|
|
|
$this->comment = $comment; |
148
|
|
|
|
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function setId($id): ChargeInterface |
153
|
|
|
{ |
154
|
|
|
if ($this->id === $id) { |
155
|
|
|
return $this; |
156
|
|
|
} |
157
|
|
|
if ($this->id !== null) { |
158
|
|
|
throw new \Exception('cannot reassign sale id'); |
159
|
|
|
} |
160
|
|
|
$this->id = $id; |
161
|
|
|
|
162
|
|
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function jsonSerialize() |
166
|
|
|
{ |
167
|
|
|
return get_object_vars($this); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|