|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Taxed Money |
|
4
|
|
|
* |
|
5
|
|
|
* @author Pronamic <[email protected]> |
|
6
|
|
|
* @copyright 2005-2018 Pronamic |
|
7
|
|
|
* @license GPL-3.0-or-later |
|
8
|
|
|
* @package Pronamic\WordPress\Money |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Pronamic\WordPress\Money; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Taxed Money |
|
15
|
|
|
* |
|
16
|
|
|
* @author Remco Tolsma |
|
17
|
|
|
* @version 1.0.0 |
|
18
|
|
|
* @since 1.0.0 |
|
19
|
|
|
*/ |
|
20
|
|
|
class TaxedMoney extends Money { |
|
21
|
|
|
/** |
|
22
|
|
|
* Tax amount. |
|
23
|
|
|
* |
|
24
|
|
|
* @var float|null |
|
25
|
|
|
*/ |
|
26
|
|
|
private $tax_amount; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Tax percentage. |
|
30
|
|
|
* |
|
31
|
|
|
* @var float|null |
|
32
|
|
|
*/ |
|
33
|
|
|
private $tax_percentage; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Construct and initialize money object. |
|
37
|
|
|
* |
|
38
|
|
|
* @param string|int|float $amount Amount. |
|
39
|
|
|
* @param Currency|string|null $currency Currency. |
|
40
|
|
|
* @param string|int|float $tax_amount Tax amount. |
|
41
|
|
|
* @param string|int|float $tax_percentage Tax percentage. |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct( $amount = 0, $currency = null, $tax_amount = null, $tax_percentage = null ) { |
|
44
|
|
|
parent::__construct( $amount, $currency ); |
|
45
|
|
|
|
|
46
|
|
|
// Calculate tax amount if tax percentage is set. |
|
47
|
|
View Code Duplication |
if ( null === $tax_amount && null !== $tax_percentage ) { |
|
|
|
|
|
|
48
|
|
|
$tax_amount = $amount * ( $tax_percentage / 100 ); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$this->set_tax_amount( $tax_amount ); |
|
52
|
|
|
|
|
53
|
|
|
// Calculate tax percentage if tax amount is set. |
|
54
|
|
View Code Duplication |
if ( null === $tax_percentage && null !== $tax_amount && $amount > 0 ) { |
|
|
|
|
|
|
55
|
|
|
$tax_percentage = round( ( $tax_amount / $amount ) * 100, 2 ); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$this->set_tax_percentage( $tax_percentage ); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Get tax amount. |
|
63
|
|
|
* |
|
64
|
|
|
* @return float|null Tax amount. |
|
65
|
|
|
*/ |
|
66
|
|
|
public function get_tax_amount() { |
|
67
|
|
|
return $this->tax_amount; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Set tax amount. |
|
72
|
|
|
* |
|
73
|
|
|
* @param float|null $amount Tax amount. |
|
74
|
|
|
*/ |
|
75
|
|
|
public function set_tax_amount( $amount ) { |
|
76
|
|
|
$this->tax_amount = ( null === $amount ? null : floatval( $amount ) ); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Has tax? |
|
81
|
|
|
* |
|
82
|
|
|
* @return bool |
|
83
|
|
|
*/ |
|
84
|
|
|
public function has_tax() { |
|
85
|
|
|
return ( null !== $this->get_tax_amount() ); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Get tax percentage. |
|
90
|
|
|
* |
|
91
|
|
|
* @return float |
|
92
|
|
|
*/ |
|
93
|
|
|
public function get_tax_percentage() { |
|
94
|
|
|
return $this->tax_percentage; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Set tax percentage. |
|
99
|
|
|
* |
|
100
|
|
|
* 100% = 100 |
|
101
|
|
|
* 21% = 21 |
|
102
|
|
|
* 6% = 6 |
|
103
|
|
|
* 1.5% = 1.5 |
|
104
|
|
|
* |
|
105
|
|
|
* @param string|int|float|null $percentage Tax percentage. |
|
106
|
|
|
*/ |
|
107
|
|
|
public function set_tax_percentage( $percentage ) { |
|
108
|
|
|
$this->tax_percentage = floatval( $percentage ); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Get including tax. |
|
113
|
|
|
* |
|
114
|
|
|
* @return Money |
|
115
|
|
|
*/ |
|
116
|
|
|
public function get_including_tax() { |
|
117
|
|
|
return new Money( $this->get_amount(), $this->get_currency() ); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Get excluding tax. |
|
122
|
|
|
* |
|
123
|
|
|
* @return Money |
|
124
|
|
|
*/ |
|
125
|
|
|
public function get_excluding_tax() { |
|
126
|
|
|
$amount = $this->get_amount(); |
|
127
|
|
|
|
|
128
|
|
|
$use_bcmath = extension_loaded( 'bcmath' ); |
|
129
|
|
|
|
|
130
|
|
|
if ( $use_bcmath ) { |
|
131
|
|
|
// Use non-locale aware float value. |
|
132
|
|
|
// @link http://php.net/sprintf. |
|
133
|
|
|
$value = sprintf( '%F', $this->get_amount() ); |
|
134
|
|
|
|
|
135
|
|
|
$amount = bcsub( $value, $this->get_tax_amount(), 8 ); |
|
136
|
|
|
} else { |
|
137
|
|
|
$amount -= $this->get_tax_amount(); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return new Money( $amount, $this->get_currency() ); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.