| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Taxed Money | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @author    Pronamic <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @copyright 2005-2023 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 | 4 |  | 	public function __construct( $value = 0, $currency = 'EUR', $tax_value = null, $tax_percentage = null ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 4 |  | 		$value = Number::from_mixed( $value ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 4 |  | 		parent::__construct( $value, $currency ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 		// Calculate tax amount if tax percentage is set. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 4 |  | 		if ( null === $tax_value && null !== $tax_percentage ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 2 |  | 			$tax_percentage = Number::from_mixed( $tax_percentage ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 2 |  | 			$percentage = Number::from_string( '100' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 2 |  | 			$percentage = $percentage->add( $tax_percentage ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 			/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 			 * For some reason, Scrutinizer thinks the `add` function return a | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 			 * `int|double` in the `$percentage` variable. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 			 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 			 * @scrutinizer ignore-type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 			 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 2 |  | 			$one_percent_value = $value->divide( $percentage ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 2 |  | 			$tax_value = $one_percent_value->multiply( $tax_percentage ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 4 |  | 		$this->set_tax_value( $tax_value ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 4 |  | 		$this->set_tax_percentage( $tax_percentage ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 4 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	 * Get tax amount. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	 * @return Money|null Tax amount. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 2 |  | 	public function get_tax_amount() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 2 |  | 		if ( null === $this->tax_value ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 1 |  | 			return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  | 		return new Money( $this->tax_value, $this->get_currency() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	 * Get tax value. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	 * @return string|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 3 |  | 	public function get_tax_value() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 3 |  | 		return null === $this->tax_value ? null : $this->tax_value->get_value(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	 * Set tax value. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	 * @param float|int|string|null $value Tax value. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 	 * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 4 |  | 	public function set_tax_value( $value ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 4 |  | 		$this->tax_value = ( null === $value ? null : Number::from_mixed( $value ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 4 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 	 * Has tax? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 	 * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 2 |  | 	public function has_tax() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 2 |  | 		return ( null !== $this->get_tax_value() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 	 * Get tax percentage. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 	 * @return string|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 1 |  | 	public function get_tax_percentage() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 1 |  | 		return null === $this->tax_percentage ? null : $this->tax_percentage->get_value(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 	 * Set tax percentage. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 	 * 100% = 100 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 	 *  21% =  21 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 	 *   6% =   6 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 	 * 1.5% =   1.5 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 	 * @param mixed $percentage Tax percentage. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 	 * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 4 |  | 	public function set_tax_percentage( $percentage ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 4 |  | 		$this->tax_percentage = ( null === $percentage ? null : Number::from_mixed( $percentage ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 4 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 	 * Get including tax. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 	 * @return Money | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 2 |  | 	public function get_including_tax() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 2 |  | 		return new Money( $this->get_value(), $this->get_currency() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 	 * Get excluding tax. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 	 * @return Money | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 | 2 |  | 	public function get_excluding_tax() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 | 2 |  | 		$tax_amount = $this->get_tax_amount(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 | 2 |  | 		if ( null === $tax_amount ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 | 1 |  | 			return $this->get_including_tax(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 | 1 |  | 		return $this->subtract( $tax_amount ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 	 * JSON serialize. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 	 * @link https://www.php.net/manual/en/jsonserializable.jsonserialize.php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 	 * @return object | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 167 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 168 | 1 |  | 	public function jsonSerialize(): object { | 
            
                                                                        
                            
            
                                    
            
            
                | 169 | 1 |  | 		$object = parent::jsonSerialize(); | 
            
                                                                        
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 171 | 1 |  | 		$properties = (array) $object; | 
            
                                                                        
                            
            
                                    
            
            
                | 172 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 173 | 1 |  | 		if ( null !== $this->tax_value ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 174 | 1 |  | 			$properties['tax_value'] = $this->tax_value->jsonSerialize(); | 
            
                                                                        
                            
            
                                    
            
            
                | 175 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 176 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 177 | 1 |  | 		if ( null !== $this->tax_percentage ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 178 | 1 |  | 			$properties['tax_percentage'] = $this->tax_percentage->jsonSerialize(); | 
            
                                                                        
                            
            
                                    
            
            
                | 179 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 180 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 181 | 1 |  | 		$object = (object) $properties; | 
            
                                                                        
                            
            
                                    
            
            
                | 182 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 183 | 1 |  | 		return $object; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 185 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 186 |  |  |  |