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
|
|
|
/** |
29
|
|
|
* @var int |
30
|
|
|
*/ |
31
|
|
|
protected $id; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var ActionInterface |
35
|
|
|
*/ |
36
|
|
|
protected $action; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var PriceInterface |
40
|
|
|
*/ |
41
|
|
|
protected $price; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var QuantityInterface |
45
|
|
|
*/ |
46
|
|
|
protected $usage; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var Money |
50
|
|
|
*/ |
51
|
|
|
protected $sum; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var BillInterface |
55
|
|
|
*/ |
56
|
|
|
protected $bill; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
protected $comment; |
62
|
|
|
|
63
|
8 |
|
public function __construct( |
64
|
|
|
$id, |
65
|
|
|
ActionInterface $action, |
66
|
|
|
PriceInterface $price, |
67
|
|
|
QuantityInterface $usage, |
68
|
|
|
Money $sum, |
69
|
|
|
BillInterface $bill = null |
70
|
|
|
) { |
71
|
8 |
|
$this->id = $id; |
72
|
8 |
|
$this->action = $action; |
73
|
8 |
|
$this->price = $price; |
74
|
8 |
|
$this->usage = $usage; |
75
|
8 |
|
$this->sum = $sum; |
76
|
8 |
|
$this->bill = $bill; |
77
|
8 |
|
} |
78
|
|
|
|
79
|
|
|
public function getId() |
80
|
|
|
{ |
81
|
|
|
return $this->id; |
82
|
|
|
} |
83
|
|
|
|
84
|
6 |
|
public function getAction() |
85
|
|
|
{ |
86
|
6 |
|
return $this->action; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return PriceInterface |
91
|
|
|
*/ |
92
|
6 |
|
public function getPrice() |
93
|
|
|
{ |
94
|
6 |
|
return $this->price; |
95
|
|
|
} |
96
|
|
|
|
97
|
8 |
|
public function getUsage() |
98
|
|
|
{ |
99
|
8 |
|
return $this->usage; |
100
|
|
|
} |
101
|
|
|
|
102
|
8 |
|
public function getSum() |
103
|
|
|
{ |
104
|
8 |
|
return $this->sum; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function calculatePrice() |
108
|
|
|
{ |
109
|
|
|
$usage = $this->usage->getQuantity(); |
110
|
|
|
|
111
|
|
|
return $usage ? $this->sum->divide($usage) : $this->sum; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function getBill() |
115
|
|
|
{ |
116
|
|
|
return $this->bill; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function hasBill() |
120
|
|
|
{ |
121
|
|
|
return $this->bill !== null; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function setBill(BillInterface $bill) |
125
|
|
|
{ |
126
|
|
|
if ($this->hasBill()) { |
127
|
|
|
throw new \Exception('cannot reassign sale bill'); |
128
|
|
|
} |
129
|
|
|
$this->bill = $bill; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function getComment() |
133
|
|
|
{ |
134
|
|
|
return $this->comment; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function setComment(string $comment) |
138
|
|
|
{ |
139
|
|
|
$this->comment = $comment; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
View Code Duplication |
public function setId($id) |
|
|
|
|
143
|
|
|
{ |
144
|
|
|
if ($this->id === $id) { |
145
|
|
|
return; |
146
|
|
|
} |
147
|
|
|
if ($this->id !== null) { |
148
|
|
|
throw new \Exception('cannot reassign sale id'); |
149
|
|
|
} |
150
|
|
|
$this->id = $id; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function jsonSerialize() |
154
|
|
|
{ |
155
|
|
|
return get_object_vars($this); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.