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-2020, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\php\billing\charge\modifiers; |
12
|
|
|
|
13
|
|
|
use DateTimeImmutable; |
14
|
|
|
use Exception; |
15
|
|
|
use hiqdev\php\billing\action\ActionInterface; |
16
|
|
|
use hiqdev\php\billing\charge\AddonsContainerInterface; |
17
|
|
|
use hiqdev\php\billing\charge\ChargeInterface; |
18
|
|
|
use hiqdev\php\billing\charge\ChargeModifier; |
19
|
|
|
use hiqdev\php\billing\charge\TimeLimitedModifierInterface; |
20
|
|
|
use hiqdev\php\billing\formula\FormulaRuntimeError; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Fixed discount. |
24
|
|
|
* |
25
|
|
|
* @author Andrii Vasyliev <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class Modifier implements ChargeModifier, AddonsContainerInterface, TimeLimitedModifierInterface |
28
|
|
|
{ |
29
|
|
|
use \hiqdev\php\billing\charge\modifiers\addons\WithReason; |
30
|
|
|
use \hiqdev\php\billing\charge\modifiers\addons\WithSince; |
31
|
|
|
use \hiqdev\php\billing\charge\modifiers\addons\WithTill; |
32
|
|
|
use \hiqdev\php\billing\charge\modifiers\addons\WithTerm; |
33
|
|
|
use \hiqdev\php\billing\charge\modifiers\addons\WithChargeType; |
34
|
|
|
|
35
|
|
|
/** |
36
|
28 |
|
* @var AddonInterface[] |
37
|
|
|
*/ |
38
|
28 |
|
protected $addons; |
39
|
28 |
|
|
40
|
|
|
public function __construct(array $addons = []) |
41
|
|
|
{ |
42
|
|
|
$this->addons = $addons; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function modifyCharge(?ChargeInterface $charge, ActionInterface $action): array |
46
|
|
|
{ |
47
|
|
|
throw new Exception('not finished modifier'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function isSuitable(?ChargeInterface $charge, ActionInterface $action): bool |
51
|
|
|
{ |
52
|
|
|
$month = $action->getTime()->modify('first day of this month midnight'); |
53
|
|
|
|
54
|
|
|
$since = $this->getSince(); |
55
|
|
|
if ($since && $since->getValue() > $month) { |
56
|
|
|
return false; |
57
|
|
|
} |
58
|
3 |
|
|
59
|
|
|
return true; |
60
|
3 |
|
} |
61
|
|
|
|
62
|
|
|
public function discount() |
63
|
|
|
{ |
64
|
|
|
return new Discount($this->addons); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function cap() |
68
|
25 |
|
{ |
69
|
|
|
return new Cap($this->addons); |
70
|
25 |
|
} |
71
|
|
|
|
72
|
|
|
public function installment() |
73
|
25 |
|
{ |
74
|
25 |
|
return new Installment($this->addons); |
75
|
|
|
} |
76
|
25 |
|
|
77
|
|
|
public function getNext() |
78
|
|
|
{ |
79
|
25 |
|
return new static($this->addons); |
80
|
|
|
} |
81
|
25 |
|
|
82
|
|
|
public function addAddon(string $name, AddonInterface $addon) |
83
|
|
|
{ |
84
|
12 |
|
if ($this->hasAddon($name)) { |
85
|
|
|
throw new Exception("'$name' is already set"); |
86
|
12 |
|
} |
87
|
|
|
$res = $this->getNext(); |
88
|
|
|
$res->addons[$name] = $addon; |
89
|
19 |
|
|
90
|
|
|
return $res; |
91
|
19 |
|
} |
92
|
|
|
|
93
|
|
|
public function replaceAddon(string $name, AddonInterface $addon) |
94
|
5 |
|
{ |
95
|
|
|
if (!$this->hasAddon($name)) { |
96
|
5 |
|
throw new Exception("Addong '$name' is not set"); |
97
|
5 |
|
} |
98
|
|
|
|
99
|
|
|
$res = $this->getNext(); |
100
|
|
|
$res->addons[$name] = $addon; |
101
|
5 |
|
|
102
|
5 |
|
return $res; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function getAddon(string $name): ?AddonInterface |
106
|
5 |
|
{ |
107
|
5 |
|
return empty($this->addons[$name]) ? null : $this->addons[$name]; |
108
|
1 |
|
} |
109
|
|
|
|
110
|
|
|
public function hasAddon(string $name): bool |
111
|
1 |
|
{ |
112
|
|
|
return isset($this->addons[$name]); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function checkPeriod(DateTimeImmutable $time): bool |
116
|
5 |
|
{ |
117
|
|
|
$since = $this->getSince(); |
118
|
|
|
if ($since && $since->getValue() > $time) { |
119
|
|
|
return false; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$till = $this->getTill(); |
123
|
|
|
if ($till && $till->getValue() <= $time) { |
124
|
|
|
return false; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$term = $this->getTerm(); |
128
|
|
|
if ($term) { |
|
|
|
|
129
|
|
|
if (!$since) { |
|
|
|
|
130
|
|
|
throw new FormulaRuntimeError('since must be set to use term'); |
131
|
|
|
} |
132
|
|
|
if ($term->countPeriodsPassed($since->getValue(), $time) >= 1) { |
133
|
|
|
return false; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return true; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|