| 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 |
/** |
| 19 |
* Whether the tax engine is active. |
| 20 |
* |
| 21 |
* Defaults to the merchant's `enable_product_tax` setting, but addons that |
| 22 |
* compute tax externally (e.g. Stripe Automatic Tax) can force it on so the |
| 23 |
* computed tax actually lands in the cart/order total and is persisted as a |
| 24 |
* tax line — keeping charge, order and invoice a single source of truth. |
| 25 |
* |
| 26 |
* @param bool $enabled Whether product tax is enabled. |
| 27 |
*/ |
| 28 |
return (bool) apply_filters( 'storeengine/tax_enabled', Helper::get_settings( 'enable_product_tax' ) ); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Get rounding mode for internal tax calculations. |
| 33 |
* |
| 34 |
* @return int |
| 35 |
*/ |
| 36 |
public static function get_tax_rounding_mode(): int { |
| 37 |
$mode = self::prices_include_tax() ? PHP_ROUND_HALF_DOWN : PHP_ROUND_HALF_UP; |
| 38 |
|
| 39 |
return intval( apply_filters( 'storeengine/tax_rounding_mode', $mode ) ); |
| 40 |
} |
| 41 |
|
| 42 |
public static function tax_based_on() { |
| 43 |
return Helper::get_settings( 'tax_based_on', 'shipping' ); |
| 44 |
} |
| 45 |
|
| 46 |
public static function default_customer_address() { |
| 47 |
return Helper::get_settings( 'store_default_customer_address' ); |
| 48 |
} |
| 49 |
|
| 50 |
public static function tax_round_at_subtotal() { |
| 51 |
return Helper::get_settings( 'tax_round_at_subtotal', false ); |
| 52 |
} |
| 53 |
|
| 54 |
public static function prices_include_tax(): bool { |
| 55 |
if ( null === self::$prices_include_tax ) { |
| 56 |
self::$prices_include_tax = Helper::get_settings( 'prices_include_tax', false ); |
| 57 |
} |
| 58 |
|
| 59 |
return self::is_tax_enabled() && apply_filters( 'storeengine/prices_include_tax', self::$prices_include_tax ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Returns 'incl' if tax should be included in cart, otherwise returns 'excl'. |
| 64 |
* |
| 65 |
* @param Customer|null|false $customer |
| 66 |
* |
| 67 |
* @return string |
| 68 |
*/ |
| 69 |
public static function get_tax_price_display_mode( $customer = null ): string { |
| 70 |
if ( ! $customer ) { |
| 71 |
$customer = storeengine()->get_customer(); |
| 72 |
} |
| 73 |
|
| 74 |
$tax_display_mode = Helper::get_settings( 'tax_display_cart', 'excl' ); |
| 75 |
|
| 76 |
if ( $customer && $customer->get_is_vat_exempt() ) { |
| 77 |
$tax_display_mode = 'excl'; |
| 78 |
} |
| 79 |
|
| 80 |
return apply_filters( 'storeengine/tax/price_display_mode', $tax_display_mode, $customer ); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Return whether-or-not the cart is displaying prices including tax, rather than excluding tax. |
| 85 |
* |
| 86 |
* @param Customer|null|false $customer |
| 87 |
* |
| 88 |
* @return bool |
| 89 |
*/ |
| 90 |
public static function display_prices_including_tax( $customer = null ): bool { |
| 91 |
/** |
| 92 |
* Filtering if display prices including tax or not! |
| 93 |
* |
| 94 |
* @param bool $including_tax True / False. |
| 95 |
*/ |
| 96 |
return apply_filters( |
| 97 |
'storeengine/tax/display_prices_including_tax', |
| 98 |
'incl' === self::get_tax_price_display_mode( $customer ) |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Return whether-or-not the cart is displaying prices excluding tax, rather than excluding tax. |
| 104 |
* |
| 105 |
* @param Customer|null|false $customer |
| 106 |
* |
| 107 |
* @return bool |
| 108 |
*/ |
| 109 |
public static function display_prices_excluding_tax( $customer = null ): bool { |
| 110 |
/** |
| 111 |
* Filtering if display prices including tax or not! |
| 112 |
* |
| 113 |
* @param bool $including_tax True / False. |
| 114 |
*/ |
| 115 |
return apply_filters( |
| 116 |
'storeengine/tax/display_prices_excluding_tax', |
| 117 |
'excl' === self::get_tax_price_display_mode( $customer ) |
| 118 |
); |
| 119 |
} |
| 120 |
|
| 121 |
public static function get_tax_label_suffix(): string { |
| 122 |
if ( ! StoreEngine::init()->customer ) { |
| 123 |
return ''; |
| 124 |
} |
| 125 |
|
| 126 |
$taxable_address = StoreEngine::init()->customer->get_taxable_address(); |
| 127 |
$label_suffix = ''; |
| 128 |
if ( StoreEngine::init()->customer->is_customer_outside_base() && ! StoreEngine::init()->customer->has_calculated_shipping() ) { |
| 129 |
/* translators: %s location. */ |
| 130 |
$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] ) ); |
| 131 |
} |
| 132 |
|
| 133 |
return apply_filters( 'storeengine/tax/label_suffix', $label_suffix ); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
// End of file tax-util.php. |
| 138 |
|