Completed
Push — develop ( 35d7b6...76a0ea )
by Remco
02:08
created

TaxedMoney::set_tax_value()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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 value.
23
	 *
24
	 * @var float|null
25
	 */
26
	private $tax_value;
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     $value          Amount value.
39
	 * @param Currency|string|null $currency       Currency.
40
	 * @param string|int|float     $tax_value      Tax value.
41
	 * @param string|int|float     $tax_percentage Tax percentage.
42
	 */
43
	public function __construct( $value = 0, $currency = null, $tax_value = null, $tax_percentage = null ) {
44
		parent::__construct( $value, $currency );
45
46
		// Calculate tax amount if tax percentage is set.
47
		if ( null === $tax_value && null !== $tax_percentage ) {
48
			$tax_value = ( $value / ( 100 + $tax_percentage ) ) * $tax_percentage;
49
		}
50
51
		$this->set_tax_value( $tax_value );
52
		$this->set_tax_percentage( $tax_percentage );
53
	}
54
55
	/**
56
	 * Get tax amount.
57
	 *
58
	 * @return Money|null Tax amount.
59
	 */
60
	public function get_tax_amount() {
61
		if ( null === $this->tax_value ) {
62
			return null;
63
		}
64
65
		return new Money( $this->tax_value, $this->get_currency() );
66
	}
67
68
	/**
69
	 * Get tax value.
70
	 *
71
	 * @return float|null
72
	 */
73
	public function get_tax_value() {
74
		return $this->tax_value;
75
	}
76
77
	/**
78
	 * Set tax value.
79
	 *
80
	 * @param float|null $value Tax value.
81
	 */
82
	public function set_tax_value( $value ) {
83
		$this->tax_value = ( null === $value ? null : floatval( $value ) );
84
	}
85
86
	/**
87
	 * Has tax?
88
	 *
89
	 * @return bool
90
	 */
91
	public function has_tax() {
92
		return ( null !== $this->get_tax_value() );
93
	}
94
95
	/**
96
	 * Get tax percentage.
97
	 *
98
	 * @return float
99
	 */
100
	public function get_tax_percentage() {
101
		return $this->tax_percentage;
102
	}
103
104
	/**
105
	 * Set tax percentage.
106
	 *
107
	 * 100% = 100
108
	 *  21% =  21
109
	 *   6% =   6
110
	 * 1.5% =   1.5
111
	 *
112
	 * @param string|int|float|null $percentage Tax percentage.
113
	 */
114
	public function set_tax_percentage( $percentage ) {
115
		$this->tax_percentage = ( null === $percentage ? null : floatval( $percentage ) );
116
	}
117
118
	/**
119
	 * Get including tax.
120
	 *
121
	 * @return Money
122
	 */
123
	public function get_including_tax() {
124
		return new Money( $this->get_amount(), $this->get_currency() );
125
	}
126
127
	/**
128
	 * Get excluding tax.
129
	 *
130
	 * @return Money
131
	 */
132
	public function get_excluding_tax() {
133
		$amount = $this->get_amount();
134
135
		$use_bcmath = extension_loaded( 'bcmath' );
136
137
		if ( $use_bcmath ) {
138
			// Use non-locale aware float value.
139
			// @link http://php.net/sprintf.
140
			$value = sprintf( '%F', $this->get_amount() );
141
142
			$amount = bcsub( $value, $this->get_tax_value(), 8 );
143
		} else {
144
			$amount -= $this->get_tax_value();
145
		}
146
147
		return new Money( $amount, $this->get_currency() );
148
	}
149
}
150