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 hiqdev\php\billing\action\ActionInterface; |
||
14 | use hiqdev\php\billing\charge\Charge; |
||
15 | use hiqdev\php\billing\charge\ChargeInterface; |
||
16 | use hiqdev\php\billing\charge\modifiers\addons\Discount; |
||
17 | use hiqdev\php\billing\price\SinglePrice; |
||
18 | use hiqdev\php\billing\target\Target; |
||
19 | use hiqdev\php\billing\target\TargetInterface; |
||
20 | use hiqdev\php\billing\type\Type; |
||
21 | use hiqdev\php\units\Quantity; |
||
22 | use Money\Money; |
||
23 | |||
24 | /** |
||
25 | * Fixed discount. |
||
26 | * |
||
27 | * @author Andrii Vasyliev <[email protected]> |
||
28 | */ |
||
29 | class FixedDiscount extends Modifier |
||
30 | 13 | { |
|
31 | const VALUE = 'value'; |
||
32 | 13 | ||
33 | 13 | public function __construct($value, array $addons = []) |
|
34 | 13 | { |
|
35 | parent::__construct($addons); |
||
36 | 13 | $this->addAddon(self::VALUE, new Discount($value)); |
|
37 | } |
||
38 | 13 | ||
39 | public function getNext() |
||
40 | { |
||
41 | 5 | return $this; |
|
42 | } |
||
43 | 5 | ||
44 | public function getValue(ChargeInterface $charge = null): Discount |
||
45 | { |
||
46 | 3 | return $this->getAddon(self::VALUE); |
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
47 | } |
||
48 | 3 | ||
49 | public function isAbsolute() |
||
50 | { |
||
51 | 5 | return $this->getAddon(self::VALUE)->isAbsolute(); |
|
52 | } |
||
53 | 5 | ||
54 | public function isRelative() |
||
55 | { |
||
56 | 4 | return !$this->isAbsolute(); |
|
57 | } |
||
58 | 4 | ||
59 | public function calculateSum(ChargeInterface $charge = null): Money |
||
60 | { |
||
61 | 4 | return $this->getValue($charge)->calculateSum($charge); |
|
62 | } |
||
63 | 4 | ||
64 | private function getType() |
||
65 | { |
||
66 | 4 | return new Type(Type::ANY, 'discount,discount'); |
|
67 | } |
||
68 | 4 | ||
69 | public function modifyCharge(?ChargeInterface $charge, ActionInterface $action): array |
||
70 | { |
||
71 | if ($charge === null) { |
||
72 | 4 | return []; |
|
73 | 4 | } |
|
74 | |||
75 | $month = $action->getTime()->modify('first day of this month midnight'); |
||
76 | if (!$this->checkPeriod($month)) { |
||
77 | 4 | return [$charge]; |
|
78 | 4 | } |
|
79 | 4 | ||
80 | 4 | $sum = $this->calculateSum($charge)->multiply(-1); |
|
81 | 4 | $usage = Quantity::create('items', 1); |
|
82 | $type = $this->getType(); |
||
83 | 4 | $price = $charge->getPrice(); |
|
84 | 4 | $target = $price->getTarget(); |
|
85 | |||
86 | 4 | $discount = new Charge(null, $type, $target, $action, $price, $usage, $sum); |
|
87 | 4 | $discount->setParent($charge); |
|
88 | |||
89 | $reason = $this->getReason(); |
||
90 | if ($reason) { |
||
91 | 4 | $discount->setComment($reason->getValue()); |
|
92 | } |
||
93 | |||
94 | return [$charge, $discount]; |
||
95 | } |
||
96 | } |
||
97 |