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, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\php\billing\bill; |
12
|
|
|
|
13
|
|
|
use DateTime; |
14
|
|
|
use hiqdev\php\billing\charge\ChargeInterface; |
15
|
|
|
use hiqdev\php\billing\customer\CustomerInterface; |
16
|
|
|
use hiqdev\php\billing\plan\PlanInterface; |
17
|
|
|
use hiqdev\php\billing\target\TargetInterface; |
18
|
|
|
use hiqdev\php\billing\type\Type; |
19
|
|
|
use hiqdev\php\units\QuantityInterface; |
20
|
|
|
use Money\Money; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Bill. |
24
|
|
|
* |
25
|
|
|
* @author Andrii Vasyliev <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class Bill implements BillInterface |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var int|string |
31
|
|
|
*/ |
32
|
|
|
protected $id; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var TypeInterface |
36
|
|
|
*/ |
37
|
|
|
protected $type; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var DateTime |
41
|
|
|
*/ |
42
|
|
|
protected $time; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var Money |
46
|
|
|
*/ |
47
|
|
|
protected $sum; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var QuantityInterface |
51
|
|
|
*/ |
52
|
|
|
protected $quantity; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var CustomerInterface |
56
|
|
|
*/ |
57
|
|
|
protected $customer; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var TargetInterface |
61
|
|
|
*/ |
62
|
|
|
protected $target; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var PlanInterface |
66
|
|
|
*/ |
67
|
|
|
protected $plan; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var bool |
71
|
|
|
*/ |
72
|
|
|
protected $isFinished; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var ChargeInterface[] |
76
|
|
|
*/ |
77
|
|
|
protected $charges = []; |
78
|
|
|
|
79
|
|
|
public function __construct( |
80
|
|
|
$id, |
81
|
|
|
TypeInterface $type, |
82
|
|
|
DateTime $time, |
83
|
|
|
Money $sum, |
84
|
|
|
QuantityInterface $quantity, |
85
|
|
|
CustomerInterface $customer, |
86
|
|
|
TargetInterface $target = null, |
87
|
|
|
PlanInterface $plan = null, |
88
|
|
|
array $charges = [] |
89
|
|
|
) { |
90
|
|
|
$this->id = $id; |
91
|
|
|
$this->type = $type; |
92
|
|
|
$this->time = $time; |
93
|
|
|
$this->sum = $sum; |
94
|
|
|
$this->quantity = $quantity; |
95
|
|
|
$this->customer = $customer; |
96
|
|
|
$this->target = $target; |
97
|
|
|
$this->plan = $plan; |
98
|
|
|
$this->charges = $charges; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function getUniqueId() |
102
|
|
|
{ |
103
|
|
|
$parts = [ |
104
|
|
|
$this->customer->getUniqueId(), |
105
|
|
|
$this->sum->getCurrency()->getCode(), |
106
|
|
|
$this->target ? $this->target->getUniqueId() : null, |
107
|
|
|
$this->type->getUniqueId(), |
108
|
|
|
$this->time->format('c'), |
109
|
|
|
$this->plan ? $this->plan->getUniqueId() : null, |
110
|
|
|
]; |
111
|
|
|
|
112
|
|
|
return implode('-', array_filter($parts)); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function calculatePrice() |
116
|
|
|
{ |
117
|
|
|
$quantity = $this->quantity->getQuantity(); |
118
|
|
|
|
119
|
|
|
return $quantity ? $this->sum->divide($quantity) : $this->sum; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function getId() |
123
|
|
|
{ |
124
|
|
|
return $this->id; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return TypeInterface |
129
|
|
|
*/ |
130
|
|
|
public function getType() |
131
|
|
|
{ |
132
|
|
|
return $this->type; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return TargetInterface |
137
|
|
|
*/ |
138
|
|
|
public function getTarget() |
139
|
|
|
{ |
140
|
|
|
return $this->target; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return PriceInterface |
145
|
|
|
*/ |
146
|
|
|
public function getPrice() |
147
|
|
|
{ |
148
|
|
|
return $this->price; |
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return QuantityInterface |
153
|
|
|
*/ |
154
|
|
|
public function getQuantity() |
155
|
|
|
{ |
156
|
|
|
return $this->quantity; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return Money |
161
|
|
|
*/ |
162
|
|
|
public function getSum() |
163
|
|
|
{ |
164
|
|
|
return $this->sum; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function jsonSerialize() |
168
|
|
|
{ |
169
|
|
|
return get_object_vars($this); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|