1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace hiqdev\php\billing\price; |
6
|
|
|
|
7
|
|
|
use hiqdev\php\billing\plan\PlanInterface; |
8
|
|
|
use hiqdev\php\billing\target\TargetInterface; |
9
|
|
|
use hiqdev\php\billing\type\TypeInterface; |
10
|
|
|
use hiqdev\php\units\Quantity; |
11
|
|
|
use hiqdev\php\units\QuantityInterface; |
12
|
|
|
use Money\Money; |
13
|
|
|
|
14
|
|
|
class ProgressivePrice extends AbstractPrice |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
public const SIGN_GREATER = '>'; |
18
|
|
|
public const SIGN_LESS = '<'; |
19
|
|
|
public const SIGN_GREATER_EQUAL = '>='; |
20
|
|
|
public const SIGN_LESS_EQUAL = '<='; |
21
|
|
|
public const SIGN_EQUAL = '='; |
22
|
|
|
|
23
|
|
|
/* @psalm-var array{array{ |
24
|
|
|
* "price": numeric, |
25
|
|
|
* "currency": string, |
26
|
|
|
* "sign_from": string, |
27
|
|
|
* "value_from": numeric, |
28
|
|
|
* "sign_till": string, |
29
|
|
|
* "value_till", numeric |
30
|
|
|
* }} $condition |
31
|
|
|
*/ |
32
|
|
|
protected array $condition; |
33
|
|
|
/** |
34
|
|
|
* @var QuantityInterface prepaid quantity also implies Unit |
35
|
|
|
* XXX cannot be null cause Unit is required |
36
|
|
|
*/ |
37
|
|
|
protected $prepaid; |
38
|
|
|
|
39
|
|
|
public function __construct( |
40
|
|
|
$id, |
41
|
|
|
TypeInterface $type, |
42
|
|
|
TargetInterface $target, |
43
|
|
|
QuantityInterface $prepaid, |
44
|
|
|
array $condition, |
45
|
|
|
PlanInterface $plan = null, |
46
|
|
|
) { |
47
|
|
|
parent::__construct($id, $type, $target, $plan); |
48
|
|
|
$this->prepaid = $prepaid; |
49
|
|
|
$this->condition = $condition; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getPrepaid() |
53
|
|
|
{ |
54
|
|
|
return $this->prepaid; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function getCondition(): array |
58
|
|
|
{ |
59
|
|
|
return $this->condition; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function setCondition(string $key, array $condition): self |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$this->condition[] = $condition; |
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function prepareCondition(): void |
69
|
|
|
{ |
70
|
|
|
uksort($this->condition, function($a, $b) |
71
|
|
|
{ |
72
|
|
|
return $b['value_till'] - $a['value_till']; |
73
|
|
|
} |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @inheritDoc |
79
|
|
|
*/ |
80
|
|
|
public function calculateUsage(QuantityInterface $quantity): ?QuantityInterface |
81
|
|
|
{ |
82
|
|
|
$usage = $quantity->convert($this->prepaid->getUnit())->subtract($this->prepaid); |
83
|
|
|
|
84
|
|
|
if ($usage->isPositive()) { |
85
|
|
|
return $usage; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return Quantity::create($this->prepaid->getUnit()->getName(), 0); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @inheritDoc |
93
|
|
|
*/ |
94
|
|
|
public function calculatePrice(QuantityInterface $quantity): ?Money |
95
|
|
|
{ |
96
|
|
|
$result = null; |
97
|
|
|
$this->prepareCondition(); |
98
|
|
|
$usage = $this->calculateUsage($quantity); |
99
|
|
|
$quantity = $usage->getQuantity(); |
|
|
|
|
100
|
|
|
foreach ($this->condition as $condition) { |
101
|
|
|
$quantity = $usage->getQuantity(); |
102
|
|
|
if (isset($condition['sign_from'])) { |
103
|
|
|
if ($condition['sign_till'] === self::SIGN_EQUAL) { |
104
|
|
|
$result = $usage->getQuantity(); |
105
|
|
|
} |
106
|
|
|
} else { |
107
|
|
|
if ($condition['sign_till'] === self::SIGN_EQUAL || $condition['sign_till'] === self::SIGN_GREATER) { |
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
return $result; |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
private function getIntervalBoundary(string $sign, int $value): float |
|
|
|
|
116
|
|
|
{ |
117
|
|
|
switch ($sign) { |
118
|
|
|
case self::SIGN_GREATER: |
119
|
|
|
return $value + 0.01; |
120
|
|
|
case self::SIGN_LESS: |
121
|
|
|
return $value - 0.01; |
122
|
|
|
default: |
123
|
|
|
return $value; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.