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.1 3.6.2 3.6.3 3.6.4 3.7.0 3.7.1 3.7.2 3.7.3 3.7.4 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 3.9.1 3.9.10 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9
tutor / ecommerce / Tax.php
tutor / ecommerce Last commit date
PaymentGateways 1 year ago AdminMenu.php 1 year ago BillingController.php 1 year ago CartController.php 1 year ago CheckoutController.php 1 year ago CouponController.php 1 year ago Ecommerce.php 1 year ago EmailController.php 1 year ago HooksHandler.php 1 year ago OptionKeys.php 1 year ago OrderActivitiesController.php 1 year ago OrderController.php 1 year ago PaymentHandler.php 1 year ago Settings.php 1 year ago Tax.php 1 year ago currency.php 1 year ago
Tax.php
309 lines
1 <?php
2 /**
3 * Tax calculation class for tutor monetization.
4 *
5 * @package Tutor\Ecommerce
6 * @author Themeum
7 * @link https://themeum.com
8 * @since 3.0.0
9 */
10
11 namespace Tutor\Ecommerce;
12
13 use Tutor\Traits\JsonResponse;
14
15 /**
16 * Class Tax
17 *
18 * @since 3.0.0
19 */
20 class Tax {
21 use JsonResponse;
22
23 /**
24 * Tax type const.
25 */
26 const TYPE_INCLUSIVE = 'inclusive';
27 const TYPE_EXCLUSIVE = 'exclusive';
28
29 /**
30 * Register hooks and dependencies.
31 *
32 * @since 3.0.0
33 *
34 * @param boolean $register_hooks hook register or not.
35 */
36 public function __construct( $register_hooks = true ) {
37 if ( ! $register_hooks ) {
38 return;
39 }
40
41 add_filter( 'tutor_option_input', array( $this, 'format_tax_data_before_save' ) );
42 add_action( 'wp_ajax_tutor_get_tax_settings', array( $this, 'ajax_get_tax_settings' ) );
43 }
44
45 /**
46 * Format ecommerce tax setting data before save it to tutor settings.
47 *
48 * @param array $option option.
49 *
50 * @return array
51 */
52 public function format_tax_data_before_save( $option ) {
53 if ( ! empty( $option['ecommerce_tax'] ) ) {
54 $option['ecommerce_tax'] = wp_unslash( $option['ecommerce_tax'] );
55 }
56
57 return $option;
58 }
59
60 /**
61 * Get the tax settings from the tutor options.
62 *
63 * @since 3.0.0
64 *
65 * @return void
66 */
67 public function ajax_get_tax_settings() {
68 tutor_utils()->checking_nonce();
69 tutor_utils()->check_current_user_capability();
70
71 $tax_settings = self::get_settings();
72
73 if ( ! empty( $tax_settings->active_country ) ) {
74 $tax_settings->active_country = null;
75 }
76
77 $this->json_response( __( 'Success', 'tutor' ), $tax_settings );
78 }
79
80 /**
81 * Get tax settings.
82 *
83 * @since 3.0.0
84 *
85 * @return object
86 */
87 public static function get_settings() {
88 $tax_settings = tutor_utils()->get_option( 'ecommerce_tax' );
89
90 if ( ! empty( $tax_settings ) && is_string( $tax_settings ) ) {
91 $tax_settings = json_decode( $tax_settings );
92 }
93
94 return $tax_settings;
95 }
96
97 /**
98 * Get tax settings key data.
99 *
100 * @since 3.0.0
101 *
102 * @param string $key key.
103 * @param mixed $default default value.
104 *
105 * @return mixed
106 */
107 public static function get_setting( $key, $default = false ) {
108 $tax_settings = self::get_settings();
109
110 if ( ! empty( $tax_settings->$key ) ) {
111 return $tax_settings->$key;
112 }
113
114 return $default;
115 }
116
117 /**
118 * Calculate tax.
119 *
120 * @since 3.0.0
121 *
122 * @param float $amount amount.
123 * @param float $rate tax rate.
124 *
125 * @return float
126 */
127 public static function calculate_tax( $amount, $rate ) {
128 if ( 0 === $rate ) {
129 return $rate;
130 }
131
132 if ( self::is_tax_included_in_price() ) {
133 // Tax = (Tax Rate X Price) / (1 + Tax Rate).
134 $tax = $amount - ( $rate * $amount ) / ( 1 + $rate );
135 } else {
136 try {
137 $tax = $amount * ( $rate / 100 );
138 } catch ( \Throwable $th ) {
139 $tax = 0.0;
140 }
141 }
142
143 // Tax amount should not negative value.
144 return max( 0, round( $tax, 2 ) );
145 }
146
147 /**
148 * Get text rate for a user according to billing country and state.
149 *
150 * @param integer $user_id user id.
151 *
152 * @return float tax rate.
153 */
154 public static function get_user_tax_rate( $user_id = 0 ) {
155 $billing_info = ( new BillingController( false ) )->get_billing_info( $user_id );
156 $billing_country = $billing_info->billing_country ?? '';
157 $billing_state = $billing_info->billing_state ?? '';
158
159 return self::get_country_state_tax_rate( $billing_country, $billing_state );
160 }
161
162 /**
163 * Check site admin configured tax or not.
164 *
165 * @since 3.0.0
166 *
167 * @return boolean
168 */
169 public static function is_tax_configured() {
170 $tax_settings = self::get_settings();
171
172 return ( is_object( $tax_settings )
173 && isset( $tax_settings->rates )
174 && count( $tax_settings->rates ) );
175 }
176
177 /**
178 * Check tax is included in price or not.
179 *
180 * @since 3.0.0
181 *
182 * @return boolean
183 */
184 public static function is_tax_included_in_price() {
185 return (bool) self::get_setting( 'is_tax_included_in_price' );
186 }
187
188 /**
189 * Show price with tax in course list and details.
190 *
191 * @since 3.0.0
192 *
193 * @return bool
194 */
195 public static function show_price_with_tax() {
196 return (bool) self::get_setting( 'show_price_with_tax' );
197 }
198
199 /**
200 * Get tax type.
201 *
202 * @since 3.0.0
203 *
204 * @return string
205 */
206 public static function get_tax_type() {
207 return self::is_tax_included_in_price() ? self::TYPE_INCLUSIVE : self::TYPE_EXCLUSIVE;
208 }
209
210 /**
211 * Get country rate.
212 *
213 * @since 3.0.0
214 *
215 * @param string $country the country code for which the tax rate needs to be found.
216 * @param string $state state name.
217 *
218 * @return float tax rate value.
219 */
220 public static function get_country_state_tax_rate( $country, $state = null ) {
221 $zero_tax = 0.0;
222
223 if ( empty( $country ) ) {
224 return $zero_tax;
225 }
226
227 $country_info = self::get_country_info( $country );
228 if ( ! $country_info ) {
229 return $zero_tax;
230 }
231
232 $country_code = $country_info['numeric_code'] ?? '';
233 $country_rate_data = null;
234
235 $tax_settings = self::get_settings();
236 if ( empty( $tax_settings->rates ) ) {
237 return $zero_tax;
238 }
239
240 foreach ( $tax_settings->rates as $rate ) {
241 if ( $rate->country === $country_code ) {
242 $country_rate_data = $rate;
243 break;
244 }
245 }
246
247 if ( empty( $country_rate_data ) ) {
248 return $zero_tax;
249 }
250
251 if ( $country_rate_data->is_same_rate || 0 === count( $country_rate_data->states ) ) {
252 return floatval( $country_rate_data->rate );
253 } else {
254 // Get state rate.
255 $state_info = self::get_state_info( $country_info['states'], $state );
256 if ( empty( $state_info ) ) {
257 return $zero_tax;
258 }
259
260 $state_rate = null;
261 foreach ( $country_rate_data->states as $item ) {
262 if ( $item->id === $state_info['id'] ) {
263 $state_rate = floatval( $item->rate );
264 break;
265 }
266 }
267
268 return $state_rate;
269 }
270
271 }
272
273 /**
274 * Get country info by name.
275 *
276 * @since 3.0.0
277 *
278 * @param string $name name of country.
279 *
280 * @return array|null
281 */
282 public static function get_country_info( $name ) {
283 $countries = tutor_get_country_list();
284 foreach ( $countries as $country ) {
285 if ( strtolower( $country['name'] ) === strtolower( $name ) ) {
286 return $country;
287 }
288 }
289 }
290
291 /**
292 * Get state info of a country.
293 *
294 * @since 3.0.0
295 *
296 * @param array $states list of states of a country.
297 * @param string $state_name name of state.
298 *
299 * @return array|null
300 */
301 public static function get_state_info( $states, $state_name ) {
302 foreach ( $states as $state ) {
303 if ( strtolower( $state['name'] ) === strtolower( $state_name ) ) {
304 return $state;
305 }
306 }
307 }
308 }
309