1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Lenius Basket, a PHP package to handle |
5
|
|
|
* your shopping basket. |
6
|
|
|
* |
7
|
|
|
* Copyright (c) 2022 Lenius. |
8
|
|
|
* https://github.com/lenius/basket |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
* |
13
|
|
|
* @author Carsten Jonstrup<[email protected]> |
14
|
|
|
* @copyright 2022 Lenius. |
15
|
|
|
* |
16
|
|
|
* @version production |
17
|
|
|
* |
18
|
|
|
* @see https://github.com/lenius/basket |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace Lenius\Basket; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class Tax. |
25
|
|
|
*/ |
26
|
|
|
class Tax |
27
|
|
|
{ |
28
|
|
|
/** @var float */ |
29
|
|
|
public $percentage; |
30
|
|
|
|
31
|
|
|
/** @var float */ |
32
|
|
|
public $deductModifier; |
33
|
|
|
|
34
|
|
|
/** @var float */ |
35
|
|
|
public $addModifier; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* When constructing the tax class, you can either |
39
|
|
|
* pass in a percentage, or a price before and after |
40
|
|
|
* tax and have the library work out the tax rate |
41
|
|
|
* automatically. |
42
|
|
|
* |
43
|
|
|
* @param float $value |
44
|
|
|
* @param mixed|null $after |
45
|
|
|
*/ |
46
|
28 |
|
public function __construct(float $value, mixed $after = null) |
47
|
|
|
{ |
48
|
28 |
|
$this->percentage = $value; |
49
|
|
|
|
50
|
28 |
|
if ($after != null && is_numeric($after)) { |
51
|
1 |
|
$this->percentage = ((($after - $value) / $value) * 100); |
52
|
|
|
} |
53
|
|
|
|
54
|
28 |
|
$this->deductModifier = (1 - ($this->percentage / 100)); |
55
|
28 |
|
$this->addModifier = (1 + ($this->percentage / 100)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Deduct tax from a specified price. |
60
|
|
|
* |
61
|
|
|
* @param float $price |
62
|
|
|
* |
63
|
|
|
* @return float |
64
|
|
|
*/ |
65
|
4 |
|
public function deduct(float $price): float |
66
|
|
|
{ |
67
|
4 |
|
return $price * $this->deductModifier; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Add tax to a specified price. |
72
|
|
|
* |
73
|
|
|
* @param float $price |
74
|
|
|
* |
75
|
|
|
* @return float |
76
|
|
|
*/ |
77
|
9 |
|
public function add(float $price): float |
78
|
|
|
{ |
79
|
9 |
|
return $price * $this->addModifier; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Calculate the tax rate from a price. |
84
|
|
|
* |
85
|
|
|
* @param float $price |
86
|
|
|
* |
87
|
|
|
* @return float |
88
|
|
|
*/ |
89
|
4 |
|
public function rate(float $price): float |
90
|
|
|
{ |
91
|
4 |
|
return $price - $this->deduct($price); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|