| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Services\Renderer; |
| 4 |
|
| 5 |
use FluentCart\Framework\Support\Arr; |
| 6 |
use FluentCart\App\Modules\Tax\TaxModule; |
| 7 |
use FluentCart\App\Services\Localization\LocalizationManager; |
| 8 |
|
| 9 |
class VatFieldRenderer |
| 10 |
{ |
| 11 |
protected $taxApplicableCountry = ''; |
| 12 |
|
| 13 |
public function __construct($taxApplicableCountry = '') |
| 14 |
{ |
| 15 |
$this->taxApplicableCountry = $taxApplicableCountry; |
| 16 |
} |
| 17 |
|
| 18 |
public function render($cart) |
| 19 |
{ |
| 20 |
if (!CheckoutFieldsSchema::isVatNumberEnabled()) { |
| 21 |
return ''; |
| 22 |
} |
| 23 |
?> |
| 24 |
<div class="fct_tax_field" data-fluent-cart-checkout-page-tax-wrapper> |
| 25 |
<?php $this->renderInner($cart->checkout_data); ?> |
| 26 |
</div> |
| 27 |
<?php |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Renders only the inner content of the tax wrapper — used for fragment replacement |
| 32 |
* so the fragment does not create a nested [data-fluent-cart-checkout-page-tax-wrapper]. |
| 33 |
* Accepts $checkoutData directly so the caller can pass the freshly-updated data |
| 34 |
* instead of relying on the model's in-memory property. |
| 35 |
*/ |
| 36 |
public function renderInner($checkoutData) |
| 37 |
{ |
| 38 |
$vatNumber = Arr::get($checkoutData, 'tax_data.vat_number', ''); |
| 39 |
$isValid = Arr::get($checkoutData, 'tax_data.valid', false); |
| 40 |
$isRequired = CheckoutFieldsSchema::isVatNumberRequired(); |
| 41 |
$isB2BActive = Arr::get($checkoutData, 'form_data.is_business', 'no') === 'yes' |
| 42 |
|| CheckoutFieldsSchema::isB2BOnlyMode(); |
| 43 |
$isEuCountry = TaxModule::isTaxEnabled() |
| 44 |
&& $this->taxApplicableCountry |
| 45 |
&& LocalizationManager::getInstance()->isEuTaxCountry($this->taxApplicableCountry); |
| 46 |
?> |
| 47 |
<div data-fluent-cart-checkout-page-form-input-wrapper class="fct_tax_input_wrapper" |
| 48 |
id="fct_billing_tax_id_wrapper" |
| 49 |
> |
| 50 |
<label for="fct_billing_tax_id" class="sr-only"> |
| 51 |
<?php echo esc_html__('VAT Number', 'fluent-cart'); ?> |
| 52 |
</label> |
| 53 |
|
| 54 |
<input |
| 55 |
data-fluent-cart-checkout-page-tax-id |
| 56 |
type="text" |
| 57 |
name="fct_billing_tax_id" |
| 58 |
autocomplete="tax-id" |
| 59 |
placeholder="<?php echo esc_attr__('Enter VAT / Tax ID', 'fluent-cart') . ($isRequired ? ' *' : ''); ?>" |
| 60 |
id="fct_billing_tax_id" |
| 61 |
value="<?php echo esc_attr($vatNumber); ?>" |
| 62 |
aria-describedby="fct_billing_tax_id_error" |
| 63 |
<?php if ($isRequired): ?> data-b2b-required="yes"<?php endif; ?> |
| 64 |
aria-required="<?php echo ($isRequired && $isB2BActive) ? 'true' : 'false'; ?>" |
| 65 |
<?php if ($isRequired && $isB2BActive): ?> required<?php endif; ?> |
| 66 |
<?php echo ($isValid && $isEuCountry) ? 'readonly' : ''; ?> |
| 67 |
/> |
| 68 |
|
| 69 |
<?php if ($isEuCountry): ?> |
| 70 |
<?php if ($isValid): ?> |
| 71 |
<button |
| 72 |
type="button" |
| 73 |
data-fluent-cart-tax-remove-btn |
| 74 |
aria-label="<?php echo esc_attr__('Remove VAT number', 'fluent-cart'); ?>" |
| 75 |
> |
| 76 |
<?php echo esc_html__('Remove', 'fluent-cart'); ?> |
| 77 |
</button> |
| 78 |
<?php else: ?> |
| 79 |
<button |
| 80 |
type="button" |
| 81 |
data-fluent-cart-checkout-page-tax-apply-btn |
| 82 |
aria-label="<?php echo esc_attr__('Apply VAT number', 'fluent-cart'); ?>" |
| 83 |
> |
| 84 |
<?php echo esc_html__('Apply', 'fluent-cart'); ?> |
| 85 |
</button> |
| 86 |
<?php endif; ?> |
| 87 |
<?php endif; ?> |
| 88 |
</div> |
| 89 |
|
| 90 |
<span |
| 91 |
data-fluent-cart-checkout-page-tax-loading |
| 92 |
class="fct_tax_loading" |
| 93 |
role="status" |
| 94 |
aria-live="polite" |
| 95 |
aria-label="<?php echo esc_attr__('Validating VAT number', 'fluent-cart'); ?>"> |
| 96 |
</span> |
| 97 |
|
| 98 |
<span |
| 99 |
data-fluent-cart-checkout-page-form-error |
| 100 |
class="fct_form_error" |
| 101 |
id="fct_billing_tax_id_error" |
| 102 |
role="alert" |
| 103 |
aria-live="assertive"> |
| 104 |
</span> |
| 105 |
|
| 106 |
<?php $this->renderValidNote($checkoutData); ?> |
| 107 |
<?php |
| 108 |
} |
| 109 |
|
| 110 |
public function renderValidNote($checkoutData) |
| 111 |
{ |
| 112 |
$isValid = Arr::get($checkoutData, 'tax_data.valid', false); |
| 113 |
$name = Arr::get($checkoutData, 'tax_data.name', ''); |
| 114 |
$taxTotal = Arr::get($checkoutData, 'tax_data.tax_total', 0); |
| 115 |
?> |
| 116 |
<div |
| 117 |
class="fct_vat_valid_note <?php echo !$isValid ? 'is-hidden' : ''; ?>" |
| 118 |
data-fluent-cart-tax-valid-note-wrapper |
| 119 |
data-vat-valid="<?php echo $isValid ? 'true' : 'false'; ?>" |
| 120 |
data-vat-name="<?php echo esc_attr($name); ?>" |
| 121 |
aria-live="polite" |
| 122 |
<?php echo $isValid ? '' : 'aria-hidden="true"'; ?> |
| 123 |
> |
| 124 |
<?php if ($taxTotal != 0 && $isValid): ?> |
| 125 |
<span class="fct_vat_reverse_charge_warning"> |
| 126 |
<?php echo esc_html__('(Reverse Charge not applied)', 'fluent-cart'); ?> |
| 127 |
</span> |
| 128 |
<?php endif; ?> |
| 129 |
</div> |
| 130 |
<?php |
| 131 |
} |
| 132 |
} |
| 133 |
|