Test Failed
Push — develop ( ced990...c5bf27 )
by Remco
04:11
created

src/TaxedMoney.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * Taxed Money
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2021 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Money
9
 */
10
11
namespace Pronamic\WordPress\Money;
12
13
use JsonSerializable;
14
use Pronamic\WordPress\Number\Number;
15
16
/**
17
 * Taxed Money
18
 *
19
 * @author Remco Tolsma
20
 * @version 2.0.0
21
 * @since   1.2.0
22
 */
23
class TaxedMoney extends Money {
24
	/**
25
	 * Tax value.
26
	 *
27
	 * @var Number|null
28
	 */
29
	private $tax_value;
30
31
	/**
32
	 * Tax percentage.
33
	 *
34
	 * @var Number|null
35
	 */
36
	private $tax_percentage;
37
38
	/**
39
	 * Construct and initialize money object.
40
	 *
41
	 * @param mixed           $value          Amount value.
42
	 * @param Currency|string $currency       Currency.
43
	 * @param mixed           $tax_value      Tax value.
44
	 * @param mixed           $tax_percentage Tax percentage.
45
	 */
46
	public function __construct( $value = 0, $currency = 'EUR', $tax_value = null, $tax_percentage = null ) {
47
		$value = Number::from_mixed( $value );
48
49
		parent::__construct( $value, $currency );
50
51
		// Calculate tax amount if tax percentage is set.
52
		if ( null === $tax_value && null !== $tax_percentage ) {
53
			$tax_percentage = Number::from_mixed( $tax_percentage );
54
55
			$one_percent_value = $value->divide( $tax_percentage->add( Number::from_int( 100 ) ) );
0 ignored issues
show
$tax_percentage->add(Pro...\Number::from_int(100)) of type double|integer is incompatible with the type Pronamic\WordPress\Number\Number expected by parameter $divisor of Pronamic\WordPress\Number\Number::divide(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
			$one_percent_value = $value->divide( /** @scrutinizer ignore-type */ $tax_percentage->add( Number::from_int( 100 ) ) );
Loading history...
56
57
			$tax_value = $one_percent_value->multiply( $tax_percentage );
58
		}
59
60
		$this->set_tax_value( $tax_value );
61
		$this->set_tax_percentage( $tax_percentage );
62
	}
63
64
	/**
65
	 * Get tax amount.
66
	 *
67
	 * @return Money|null Tax amount.
68
	 */
69
	public function get_tax_amount() {
70
		if ( null === $this->tax_value ) {
71
			return null;
72
		}
73
74
		return new Money( $this->tax_value, $this->get_currency() );
75
	}
76
77
	/**
78
	 * Get tax value.
79
	 *
80
	 * @return string|null
81
	 */
82
	public function get_tax_value() {
83
		return null === $this->tax_value ? null : $this->tax_value->get_value();
84
	}
85
86
	/**
87
	 * Set tax value.
88
	 *
89
	 * @param float|int|string|null $value Tax value.
90
	 * @return void
91
	 */
92
	public function set_tax_value( $value ) {
93
		$this->tax_value = ( null === $value ? null : Number::from_mixed( $value ) );
94
	}
95
96
	/**
97
	 * Has tax?
98
	 *
99
	 * @return bool
100
	 */
101
	public function has_tax() {
102
		return ( null !== $this->get_tax_value() );
103
	}
104
105
	/**
106
	 * Get tax percentage.
107
	 *
108
	 * @return string|null
109
	 */
110
	public function get_tax_percentage() {
111
		return null === $this->tax_percentage ? null : $this->tax_percentage->get_value();
112
	}
113
114
	/**
115
	 * Set tax percentage.
116
	 *
117
	 * 100% = 100
118
	 *  21% =  21
119
	 *   6% =   6
120
	 * 1.5% =   1.5
121
	 *
122
	 * @param mixed $percentage Tax percentage.
123
	 * @return void
124
	 */
125
	public function set_tax_percentage( $percentage ) {
126
		$this->tax_percentage = ( null === $percentage ? null : Number::from_mixed( $percentage ) );
127
	}
128
129
	/**
130
	 * Get including tax.
131
	 *
132
	 * @return Money
133
	 */
134
	public function get_including_tax() {
135
		return new Money( $this->get_value(), $this->get_currency() );
136
	}
137
138
	/**
139
	 * Get excluding tax.
140
	 *
141
	 * @return Money
142
	 */
143
	public function get_excluding_tax() {
144
		$tax_amount = $this->get_tax_amount();
145
146
		if ( null === $tax_amount ) {
147
			return $this->get_including_tax();
148
		}
149
150
		return $this->subtract( $tax_amount );
151
	}
152
153
	/**
154
	 * JSON serialize.
155
	 *
156
	 * @link https://www.php.net/manual/en/jsonserializable.jsonserialize.php
157
	 * @return object
158
	 */
159
	public function jsonSerialize() {
160
		$object = parent::jsonSerialize();
161
162
		$properties = (array) $object;
163
164
		if ( null !== $this->tax_value ) {
165
			$properties['tax_value'] = $this->tax_value->jsonSerialize();
166
		}
167
168
		if ( null !== $this->tax_percentage ) {
169
			$properties['tax_percentage'] = $this->tax_percentage->jsonSerialize();
170
		}
171
172
		$object = (object) $properties;
173
174
		return $object;
175
	}
176
}
177