| 1 |
<?php |
| 2 |
|
| 3 |
namespace StoreEngine\Utils; |
| 4 |
|
| 5 |
use StoreEngine; |
| 6 |
use StoreEngine\Classes\Countries; |
| 7 |
use StoreEngine\Classes\Customer; |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
class TaxUtil { |
| 14 |
|
| 15 |
protected static ?bool $prices_include_tax = null; |
| 16 |
|
| 17 |
public static function is_tax_enabled(): bool { |
| 18 |
// @TODO apply filter storeengine/tax_enabled |
| 19 |
return Helper::get_settings( 'enable_product_tax' ); |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Get rounding mode for internal tax calculations. |
| 24 |
* |
| 25 |
* @return int |
| 26 |
*/ |
| 27 |
public static function get_tax_rounding_mode(): int { |
| 28 |
$mode = self::prices_include_tax() ? PHP_ROUND_HALF_DOWN : PHP_ROUND_HALF_UP; |
| 29 |
|
| 30 |
return intval( apply_filters( 'storeengine/tax_rounding_mode', $mode ) ); |
| 31 |
} |
| 32 |
|
| 33 |
public static function tax_based_on() { |
| 34 |
return Helper::get_settings( 'tax_based_on', 'shipping' ); |
| 35 |
} |
| 36 |
|
| 37 |
public static function default_customer_address() { |
| 38 |
return Helper::get_settings( 'store_default_customer_address' ); |
| 39 |
} |
| 40 |
|
| 41 |
public static function tax_round_at_subtotal() { |
| 42 |
return Helper::get_settings( 'tax_round_at_subtotal', false ); |
| 43 |
} |
| 44 |
|
| 45 |
public static function prices_include_tax(): bool { |
| 46 |
if ( null === self::$prices_include_tax ) { |
| 47 |
self::$prices_include_tax = Helper::get_settings( 'prices_include_tax', false ); |
| 48 |
} |
| 49 |
|
| 50 |
return self::is_tax_enabled() && apply_filters( 'storeengine/prices_include_tax', self::$prices_include_tax ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Returns 'incl' if tax should be included in cart, otherwise returns 'excl'. |
| 55 |
* |
| 56 |
* @param Customer|null|false $customer |
| 57 |
* |
| 58 |
* @return string |
| 59 |
*/ |
| 60 |
public static function get_tax_price_display_mode( $customer = null ): string { |
| 61 |
if ( ! $customer ) { |
| 62 |
$customer = storeengine()->get_customer(); |
| 63 |
} |
| 64 |
|
| 65 |
$tax_display_mode = Helper::get_settings( 'tax_display_cart', 'excl' ); |
| 66 |
|
| 67 |
if ( $customer && $customer->get_is_vat_exempt() ) { |
| 68 |
$tax_display_mode = 'excl'; |
| 69 |
} |
| 70 |
|
| 71 |
return apply_filters( 'storeengine/tax/price_display_mode', $tax_display_mode, $customer ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Return whether-or-not the cart is displaying prices including tax, rather than excluding tax. |
| 76 |
* |
| 77 |
* @param Customer|null|false $customer |
| 78 |
* |
| 79 |
* @return bool |
| 80 |
*/ |
| 81 |
public static function display_prices_including_tax( $customer = null ): bool { |
| 82 |
/** |
| 83 |
* Filtering if display prices including tax or not! |
| 84 |
* |
| 85 |
* @param bool $including_tax True / False. |
| 86 |
*/ |
| 87 |
return apply_filters( |
| 88 |
'storeengine/tax/display_prices_including_tax', |
| 89 |
'incl' === self::get_tax_price_display_mode( $customer ) |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Return whether-or-not the cart is displaying prices excluding tax, rather than excluding tax. |
| 95 |
* |
| 96 |
* @param Customer|null|false $customer |
| 97 |
* |
| 98 |
* @return bool |
| 99 |
*/ |
| 100 |
public static function display_prices_excluding_tax( $customer = null ): bool { |
| 101 |
/** |
| 102 |
* Filtering if display prices including tax or not! |
| 103 |
* |
| 104 |
* @param bool $including_tax True / False. |
| 105 |
*/ |
| 106 |
return apply_filters( |
| 107 |
'storeengine/tax/display_prices_excluding_tax', |
| 108 |
'excl' === self::get_tax_price_display_mode( $customer ) |
| 109 |
); |
| 110 |
} |
| 111 |
|
| 112 |
public static function get_tax_label_suffix(): string { |
| 113 |
if ( ! StoreEngine::init()->customer ) { |
| 114 |
return ''; |
| 115 |
} |
| 116 |
|
| 117 |
$taxable_address = StoreEngine::init()->customer->get_taxable_address(); |
| 118 |
$label_suffix = ''; |
| 119 |
if ( StoreEngine::init()->customer->is_customer_outside_base() && ! StoreEngine::init()->customer->has_calculated_shipping() ) { |
| 120 |
/* translators: %s location. */ |
| 121 |
$label_suffix = sprintf( ' <small>' . esc_html__( '(estimated for %s)', 'storeengine' ) . '</small>', Countries::init()->estimated_for_prefix( $taxable_address[0] ) . Countries::get_instance()->get_country( $taxable_address[0] ) ); |
| 122 |
} |
| 123 |
|
| 124 |
return apply_filters( 'storeengine/tax/label_suffix', $label_suffix ); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
// End of file tax-util.php. |
| 129 |
|