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