Refactor invoice PDF generation and VAT validation
- Updated PDF template to display a fixed software code instead of "SOFTWARE". - Changed VAT label to include tax label dynamically and set to 0% for certain conditions. - Enhanced JavaScript for VAT number validation with asynchronous checks against the VIES database. - Implemented debounce for VAT number input to optimize validation calls. - Updated country settings to include country codes for VAT validation. - Modified email sending functions in webhook handlers to use dynamic attachment names for invoices.
This commit is contained in:
@@ -590,7 +590,6 @@ INSERT INTO taxes (country,rate) VALUES
|
||||
('Tunisia',19.00),
|
||||
('Algeria',19.00);
|
||||
INSERT INTO taxes (country,rate) VALUES
|
||||
('Egypt',14.00),
|
||||
('Ethiopia',15.00),
|
||||
('Tanzania',18.00),
|
||||
('Uganda',18.00),
|
||||
@@ -923,6 +922,8 @@ WHERE warranty_date IS NOT NULL;
|
||||
|
||||
alter table users add refreshkey varchar(255);
|
||||
|
||||
alter table taxes add country_code varchar(10);
|
||||
|
||||
UPDATE taxes SET eu = 1 WHERE country IN (
|
||||
'Austria', 'Belgium', 'Bulgaria', 'Croatia', 'Cyprus', 'Czech Republic',
|
||||
'Denmark', 'Estonia', 'Finland', 'France', 'Germany', 'Greece',
|
||||
@@ -933,4 +934,146 @@ UPDATE taxes SET eu = 1 WHERE country IN (
|
||||
|
||||
UPDATE taxes SET rate = 0.00 WHERE eu = 0 OR eu IS NULL;
|
||||
|
||||
UPDATE taxes SET country_code = 'AT' WHERE country = 'Austria';
|
||||
UPDATE taxes SET country_code = 'BE' WHERE country = 'Belgium';
|
||||
UPDATE taxes SET country_code = 'BG' WHERE country = 'Bulgaria';
|
||||
UPDATE taxes SET country_code = 'HR' WHERE country = 'Croatia';
|
||||
UPDATE taxes SET country_code = 'CY' WHERE country = 'Cyprus';
|
||||
UPDATE taxes SET country_code = 'CZ' WHERE country = 'Czech Republic';
|
||||
UPDATE taxes SET country_code = 'DK' WHERE country = 'Denmark';
|
||||
UPDATE taxes SET country_code = 'EE' WHERE country = 'Estonia';
|
||||
UPDATE taxes SET country_code = 'FI' WHERE country = 'Finland';
|
||||
UPDATE taxes SET country_code = 'FR' WHERE country = 'France';
|
||||
UPDATE taxes SET country_code = 'DE' WHERE country = 'Germany';
|
||||
UPDATE taxes SET country_code = 'GR' WHERE country = 'Greece';
|
||||
UPDATE taxes SET country_code = 'HU' WHERE country = 'Hungary';
|
||||
UPDATE taxes SET country_code = 'IE' WHERE country = 'Ireland';
|
||||
UPDATE taxes SET country_code = 'IT' WHERE country = 'Italy';
|
||||
UPDATE taxes SET country_code = 'LV' WHERE country = 'Latvia';
|
||||
UPDATE taxes SET country_code = 'LT' WHERE country = 'Lithuania';
|
||||
UPDATE taxes SET country_code = 'LU' WHERE country = 'Luxembourg';
|
||||
UPDATE taxes SET country_code = 'MT' WHERE country = 'Malta';
|
||||
UPDATE taxes SET country_code = 'NL' WHERE country = 'Netherlands';
|
||||
UPDATE taxes SET country_code = 'PL' WHERE country = 'Poland';
|
||||
UPDATE taxes SET country_code = 'PT' WHERE country = 'Portugal';
|
||||
UPDATE taxes SET country_code = 'RO' WHERE country = 'Romania';
|
||||
UPDATE taxes SET country_code = 'SK' WHERE country = 'Slovakia';
|
||||
UPDATE taxes SET country_code = 'SI' WHERE country = 'Slovenia';
|
||||
UPDATE taxes SET country_code = 'ES' WHERE country = 'Spain';
|
||||
UPDATE taxes SET country_code = 'SE' WHERE country = 'Sweden';
|
||||
UPDATE taxes SET country_code = 'GB' WHERE country = 'United Kingdom';
|
||||
UPDATE taxes SET country_code = 'CH' WHERE country = 'Switzerland';
|
||||
UPDATE taxes SET country_code = 'NO' WHERE country = 'Norway';
|
||||
UPDATE taxes SET country_code = 'IS' WHERE country = 'Iceland';
|
||||
UPDATE taxes SET country_code = 'AL' WHERE country = 'Albania';
|
||||
UPDATE taxes SET country_code = 'RS' WHERE country = 'Serbia';
|
||||
UPDATE taxes SET country_code = 'MK' WHERE country = 'North Macedonia';
|
||||
UPDATE taxes SET country_code = 'BA' WHERE country = 'Bosnia and Herzegovina';
|
||||
UPDATE taxes SET country_code = 'ME' WHERE country = 'Montenegro';
|
||||
UPDATE taxes SET country_code = 'MD' WHERE country = 'Moldova';
|
||||
UPDATE taxes SET country_code = 'UA' WHERE country = 'Ukraine';
|
||||
UPDATE taxes SET country_code = 'BY' WHERE country = 'Belarus';
|
||||
UPDATE taxes SET country_code = 'TR' WHERE country = 'Turkey';
|
||||
UPDATE taxes SET country_code = 'AD' WHERE country = 'Andorra';
|
||||
UPDATE taxes SET country_code = 'AU' WHERE country = 'Australia';
|
||||
UPDATE taxes SET country_code = 'NZ' WHERE country = 'New Zealand';
|
||||
UPDATE taxes SET country_code = 'JP' WHERE country = 'Japan';
|
||||
UPDATE taxes SET country_code = 'CN' WHERE country = 'China';
|
||||
UPDATE taxes SET country_code = 'IN' WHERE country = 'India';
|
||||
UPDATE taxes SET country_code = 'KR' WHERE country = 'South Korea';
|
||||
UPDATE taxes SET country_code = 'SG' WHERE country = 'Singapore';
|
||||
UPDATE taxes SET country_code = 'ID' WHERE country = 'Indonesia';
|
||||
UPDATE taxes SET country_code = 'TH' WHERE country = 'Thailand';
|
||||
UPDATE taxes SET country_code = 'VN' WHERE country = 'Vietnam';
|
||||
UPDATE taxes SET country_code = 'PH' WHERE country = 'Philippines';
|
||||
UPDATE taxes SET country_code = 'MY' WHERE country = 'Malaysia';
|
||||
UPDATE taxes SET country_code = 'TW' WHERE country = 'Taiwan';
|
||||
UPDATE taxes SET country_code = 'PK' WHERE country = 'Pakistan';
|
||||
UPDATE taxes SET country_code = 'BD' WHERE country = 'Bangladesh';
|
||||
UPDATE taxes SET country_code = 'LK' WHERE country = 'Sri Lanka';
|
||||
UPDATE taxes SET country_code = 'NP' WHERE country = 'Nepal';
|
||||
UPDATE taxes SET country_code = 'KH' WHERE country = 'Cambodia';
|
||||
UPDATE taxes SET country_code = 'MM' WHERE country = 'Myanmar';
|
||||
UPDATE taxes SET country_code = 'LA' WHERE country = 'Laos';
|
||||
UPDATE taxes SET country_code = 'MN' WHERE country = 'Mongolia';
|
||||
UPDATE taxes SET country_code = 'KZ' WHERE country = 'Kazakhstan';
|
||||
UPDATE taxes SET country_code = 'UZ' WHERE country = 'Uzbekistan';
|
||||
UPDATE taxes SET country_code = 'AM' WHERE country = 'Armenia';
|
||||
UPDATE taxes SET country_code = 'GE' WHERE country = 'Georgia';
|
||||
UPDATE taxes SET country_code = 'AZ' WHERE country = 'Azerbaijan';
|
||||
UPDATE taxes SET country_code = 'FJ' WHERE country = 'Fiji';
|
||||
UPDATE taxes SET country_code = 'PG' WHERE country = 'Papua New Guinea';
|
||||
UPDATE taxes SET country_code = 'WS' WHERE country = 'Samoa';
|
||||
UPDATE taxes SET country_code = 'TO' WHERE country = 'Tonga';
|
||||
UPDATE taxes SET country_code = 'VU' WHERE country = 'Vanuatu';
|
||||
UPDATE taxes SET country_code = 'BT' WHERE country = 'Bhutan';
|
||||
UPDATE taxes SET country_code = 'SA' WHERE country = 'Saudi Arabia';
|
||||
UPDATE taxes SET country_code = 'AE' WHERE country = 'United Arab Emirates';
|
||||
UPDATE taxes SET country_code = 'BH' WHERE country = 'Bahrain';
|
||||
UPDATE taxes SET country_code = 'KW' WHERE country = 'Kuwait';
|
||||
UPDATE taxes SET country_code = 'OM' WHERE country = 'Oman';
|
||||
UPDATE taxes SET country_code = 'QA' WHERE country = 'Qatar';
|
||||
UPDATE taxes SET country_code = 'IL' WHERE country = 'Israel';
|
||||
UPDATE taxes SET country_code = 'JO' WHERE country = 'Jordan';
|
||||
UPDATE taxes SET country_code = 'LB' WHERE country = 'Lebanon';
|
||||
UPDATE taxes SET country_code = 'EG' WHERE country = 'Egypt';
|
||||
UPDATE taxes SET country_code = 'ZA' WHERE country = 'South Africa';
|
||||
UPDATE taxes SET country_code = 'NG' WHERE country = 'Nigeria';
|
||||
UPDATE taxes SET country_code = 'KE' WHERE country = 'Kenya';
|
||||
UPDATE taxes SET country_code = 'GH' WHERE country = 'Ghana';
|
||||
UPDATE taxes SET country_code = 'MA' WHERE country = 'Morocco';
|
||||
UPDATE taxes SET country_code = 'TN' WHERE country = 'Tunisia';
|
||||
UPDATE taxes SET country_code = 'DZ' WHERE country = 'Algeria';
|
||||
UPDATE taxes SET country_code = 'ET' WHERE country = 'Ethiopia';
|
||||
UPDATE taxes SET country_code = 'TZ' WHERE country = 'Tanzania';
|
||||
UPDATE taxes SET country_code = 'UG' WHERE country = 'Uganda';
|
||||
UPDATE taxes SET country_code = 'ZW' WHERE country = 'Zimbabwe';
|
||||
UPDATE taxes SET country_code = 'ZM' WHERE country = 'Zambia';
|
||||
UPDATE taxes SET country_code = 'BW' WHERE country = 'Botswana';
|
||||
UPDATE taxes SET country_code = 'MU' WHERE country = 'Mauritius';
|
||||
UPDATE taxes SET country_code = 'NA' WHERE country = 'Namibia';
|
||||
UPDATE taxes SET country_code = 'RW' WHERE country = 'Rwanda';
|
||||
UPDATE taxes SET country_code = 'SN' WHERE country = 'Senegal';
|
||||
UPDATE taxes SET country_code = 'CI' WHERE country = 'Ivory Coast';
|
||||
UPDATE taxes SET country_code = 'CM' WHERE country = 'Cameroon';
|
||||
UPDATE taxes SET country_code = 'AO' WHERE country = 'Angola';
|
||||
UPDATE taxes SET country_code = 'MZ' WHERE country = 'Mozambique';
|
||||
UPDATE taxes SET country_code = 'MG' WHERE country = 'Madagascar';
|
||||
UPDATE taxes SET country_code = 'ML' WHERE country = 'Mali';
|
||||
UPDATE taxes SET country_code = 'BF' WHERE country = 'Burkina Faso';
|
||||
UPDATE taxes SET country_code = 'NE' WHERE country = 'Niger';
|
||||
UPDATE taxes SET country_code = 'BJ' WHERE country = 'Benin';
|
||||
UPDATE taxes SET country_code = 'TG' WHERE country = 'Togo';
|
||||
UPDATE taxes SET country_code = 'GN' WHERE country = 'Guinea';
|
||||
UPDATE taxes SET country_code = 'MW' WHERE country = 'Malawi';
|
||||
UPDATE taxes SET country_code = 'GA' WHERE country = 'Gabon';
|
||||
UPDATE taxes SET country_code = 'MR' WHERE country = 'Mauritania';
|
||||
UPDATE taxes SET country_code = 'LS' WHERE country = 'Lesotho';
|
||||
UPDATE taxes SET country_code = 'SZ' WHERE country = 'Eswatini';
|
||||
UPDATE taxes SET country_code = 'LR' WHERE country = 'Liberia';
|
||||
UPDATE taxes SET country_code = 'CA' WHERE country = 'Canada';
|
||||
UPDATE taxes SET country_code = 'US' WHERE country = 'United States';
|
||||
UPDATE taxes SET country_code = 'MX' WHERE country = 'Mexico';
|
||||
UPDATE taxes SET country_code = 'AR' WHERE country = 'Argentina';
|
||||
UPDATE taxes SET country_code = 'BR' WHERE country = 'Brazil';
|
||||
UPDATE taxes SET country_code = 'CL' WHERE country = 'Chile';
|
||||
UPDATE taxes SET country_code = 'CO' WHERE country = 'Colombia';
|
||||
UPDATE taxes SET country_code = 'PE' WHERE country = 'Peru';
|
||||
UPDATE taxes SET country_code = 'EC' WHERE country = 'Ecuador';
|
||||
UPDATE taxes SET country_code = 'UY' WHERE country = 'Uruguay';
|
||||
UPDATE taxes SET country_code = 'PY' WHERE country = 'Paraguay';
|
||||
UPDATE taxes SET country_code = 'BO' WHERE country = 'Bolivia';
|
||||
UPDATE taxes SET country_code = 'VE' WHERE country = 'Venezuela';
|
||||
UPDATE taxes SET country_code = 'CR' WHERE country = 'Costa Rica';
|
||||
UPDATE taxes SET country_code = 'PA' WHERE country = 'Panama';
|
||||
UPDATE taxes SET country_code = 'GT' WHERE country = 'Guatemala';
|
||||
UPDATE taxes SET country_code = 'HN' WHERE country = 'Honduras';
|
||||
UPDATE taxes SET country_code = 'SV' WHERE country = 'El Salvador';
|
||||
UPDATE taxes SET country_code = 'NI' WHERE country = 'Nicaragua';
|
||||
UPDATE taxes SET country_code = 'DO' WHERE country = 'Dominican Republic';
|
||||
UPDATE taxes SET country_code = 'JM' WHERE country = 'Jamaica';
|
||||
UPDATE taxes SET country_code = 'TT' WHERE country = 'Trinidad and Tobago';
|
||||
UPDATE taxes SET country_code = 'BB' WHERE country = 'Barbados';
|
||||
UPDATE taxes SET country_code = 'BS' WHERE country = 'Bahamas';
|
||||
|
||||
SET FOREIGN_KEY_CHECKS=1;
|
||||
@@ -1735,7 +1735,8 @@ function getProfile($profile, $permission){
|
||||
'software_download' => 'R',
|
||||
'software_available' => 'R',
|
||||
'history' => 'RU',
|
||||
'payment' => 'RU'
|
||||
'payment' => 'RU',
|
||||
'vat_check' => 'RU'
|
||||
];
|
||||
|
||||
// 1. Check if basic_permission_level is 4 (System-admin+) - always allow
|
||||
@@ -5725,7 +5726,7 @@ function generateSoftwareInvoice($invoice_data, $order_id, $language = 'US') {
|
||||
$lbl_quantity = $translations['quantity'] ?? 'Quantity';
|
||||
$lbl_price = $translations['price'] ?? 'Price';
|
||||
$lbl_subtotal = $translations['subtotal'] ?? 'Subtotal';
|
||||
$lbl_tax = $translations['tax'] ?? 'Tax';
|
||||
$lbl_tax = $translations['tax'] ?? 'Vat';
|
||||
$lbl_shipping = $translations['shipping'] ?? 'Shipping';
|
||||
$lbl_discount = $translations['discount'] ?? 'Discount';
|
||||
$lbl_total = $translations['total'] ?? 'Total';
|
||||
@@ -5865,7 +5866,8 @@ function generateCountriesFile($token){
|
||||
$countries[$tax['id']] = [
|
||||
'country' => $tax['country'] ?? '',
|
||||
'taxes' => $tax['rate'] ?? 0,
|
||||
'eu' => $tax['eu'] ?? 0
|
||||
'eu' => $tax['eu'] ?? 0,
|
||||
'country_code' => $tax['country_code'] ?? ''
|
||||
];
|
||||
}
|
||||
|
||||
@@ -5875,7 +5877,7 @@ function generateCountriesFile($token){
|
||||
$fileContent .= "// Generated on: " . date('Y-m-d H:i:s') . "\n\n";
|
||||
$fileContent .= "\$countries = [\n";
|
||||
foreach($countries as $id => $data){
|
||||
$fileContent .= " " . $id . " => ['country' => '" . addslashes($data['country']) . "', 'taxes' => " . $data['taxes'] . ",'eu' => " . $data['eu'] . "],\n";
|
||||
$fileContent .= " " . $id . " => ['country' => '" . addslashes($data['country']) . "', 'taxes' => " . $data['taxes'] . ",'eu' => " . $data['eu'] . ", 'country_code' => '" . addslashes($data['country_code']) . "'],\n";
|
||||
}
|
||||
$fileContent .= "];\n";
|
||||
|
||||
|
||||
@@ -70,7 +70,7 @@ $message = '
|
||||
<td style="padding: 3px 0;">' . htmlspecialchars($order_id) . '</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="padding: 3px 0;"><strong>Payment Methodr:</strong></td>
|
||||
<td style="padding: 3px 0;"><strong>Payment Method:</strong></td>
|
||||
<td style="padding: 3px 0;">' . (${$payment_method} ?? $invoice_data['header']['payment_method'] ). '</td>
|
||||
</tr>
|
||||
</table>
|
||||
@@ -94,7 +94,7 @@ $message = '
|
||||
foreach ($items as $item) {
|
||||
$line_total = $item['price'] * $item['quantity'];
|
||||
$message .= '<tr>
|
||||
<td style="padding: 10px 8px; border-bottom: 1px solid #dddddd; font-size: 13px;">SOFTWARE</td>
|
||||
<td style="padding: 10px 8px; border-bottom: 1px solid #dddddd; font-size: 13px;">110.600.000</td>
|
||||
<td style="padding: 10px 8px; border-bottom: 1px solid #dddddd; font-size: 13px;">' . htmlspecialchars($item['name']);
|
||||
|
||||
if ($item['serial_number'] !== 'N/A') {
|
||||
@@ -132,7 +132,7 @@ if ($tax_amount > 0) {
|
||||
<td style="text-align: right; padding: 5px 0;">€ ' . number_format($tax_amount, 2) . '</td>
|
||||
</tr>';
|
||||
} else {
|
||||
$vat_label = 'VAT';
|
||||
$vat_label = htmlspecialchars($lbl_tax) . ' (0%)';
|
||||
if (!empty($vat_note)) {
|
||||
$vat_label .= ' <small style="font-size: 11px; color: #888;">(' . htmlspecialchars($vat_note) . ')</small>';
|
||||
}
|
||||
|
||||
@@ -271,7 +271,7 @@ $pdf = '<!DOCTYPE html>
|
||||
foreach ($items as $item) {
|
||||
$line_total = $item['price'] * $item['quantity'];
|
||||
$pdf .= '<tr>
|
||||
<td>SOFTWARE</td>
|
||||
<td>110.600.000</td>
|
||||
<td>' . htmlspecialchars($item['name']);
|
||||
|
||||
if ($item['serial_number'] !== 'N/A') {
|
||||
@@ -308,7 +308,7 @@ $pdf .= '</tbody>
|
||||
<div class="total-amount">€ ' . number_format($tax_amount, 2) . '</div>
|
||||
</div>';
|
||||
} else {
|
||||
$vat_label = 'VAT';
|
||||
$vat_label = htmlspecialchars($lbl_tax) . ' (0%)';
|
||||
if (!empty($vat_note)) {
|
||||
$vat_label .= ' <small style="font-size: 9px; color: #666;">(' . htmlspecialchars($vat_note) . ')</small>';
|
||||
}
|
||||
|
||||
@@ -1482,6 +1482,48 @@ function showPaymentModal(option) {
|
||||
modal.appendChild(modalContent);
|
||||
document.body.appendChild(modal);
|
||||
|
||||
// VAT number validation state
|
||||
let vatValidationInProgress = false;
|
||||
let vatValidationResult = null;
|
||||
|
||||
// Function to check VAT number against VIES database (via server-side proxy)
|
||||
async function checkVATNumber(countryCode, vatNumber) {
|
||||
if (!countryCode || !vatNumber) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Use server-side proxy to avoid CORS issues
|
||||
const serviceToken = document.getElementById("servicetoken")?.innerHTML || '';
|
||||
const url = link + '/v2/vat_check';
|
||||
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json',
|
||||
'Authorization': 'Bearer ' + serviceToken
|
||||
},
|
||||
body: JSON.stringify({
|
||||
countryCode: countryCode,
|
||||
vatNumber: vatNumber
|
||||
})
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
console.warn('VAT check HTTP error:', response.status);
|
||||
return null;
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
console.log('VIES response:', data);
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.error('Error checking VAT:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Function to calculate and update tax
|
||||
function updateTaxDisplay() {
|
||||
const selectedCountry = document.getElementById("paymentCountry").value;
|
||||
@@ -1500,12 +1542,12 @@ function showPaymentModal(option) {
|
||||
// Netherlands: always take the tax percentage
|
||||
taxRate = countryTaxRate;
|
||||
} else if (isEU) {
|
||||
if (vatNumber) {
|
||||
// EU with VAT number: 0% VAT, reverse charge
|
||||
if (vatNumber && vatValidationResult && vatValidationResult.valid === true) {
|
||||
// EU with VALID VAT number: 0% VAT, reverse charge
|
||||
taxRate = 0;
|
||||
vatNote = 'Reverse charge VAT';
|
||||
} else {
|
||||
// EU without VAT number: use country VAT
|
||||
// EU without VAT number or invalid VAT: use country VAT
|
||||
taxRate = countryTaxRate;
|
||||
vatNote = 'Local VAT';
|
||||
}
|
||||
@@ -1564,9 +1606,112 @@ function showPaymentModal(option) {
|
||||
}
|
||||
}
|
||||
|
||||
// Debounce timer for VAT validation
|
||||
let vatValidationTimeout = null;
|
||||
|
||||
// Function to validate VAT number with visual feedback
|
||||
async function validateVATNumber() {
|
||||
const selectedCountry = document.getElementById("paymentCountry").value;
|
||||
const vatNumber = document.getElementById("paymentVatNumber").value.trim();
|
||||
const vatInput = document.getElementById("paymentVatNumber");
|
||||
|
||||
// Reset validation state
|
||||
vatValidationResult = null;
|
||||
vatInput.style.borderColor = '';
|
||||
|
||||
// Remove any existing validation message
|
||||
const existingMessage = document.getElementById('vatValidationMessage');
|
||||
if (existingMessage) {
|
||||
existingMessage.remove();
|
||||
}
|
||||
|
||||
if (!vatNumber) {
|
||||
updateTaxDisplay();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!selectedCountry || typeof COUNTRIES === 'undefined' || !COUNTRIES) {
|
||||
updateTaxDisplay();
|
||||
return;
|
||||
}
|
||||
|
||||
const countryData = Object.values(COUNTRIES).find(c => c.country === selectedCountry);
|
||||
if (!countryData || countryData.eu !== 1 || !countryData.country_code) {
|
||||
updateTaxDisplay();
|
||||
return;
|
||||
}
|
||||
|
||||
// For Netherlands, don't validate VAT (always apply VAT)
|
||||
if (selectedCountry === 'Netherlands') {
|
||||
updateTaxDisplay();
|
||||
return;
|
||||
}
|
||||
|
||||
// Show validating state
|
||||
vatInput.style.borderColor = '#ffc107';
|
||||
vatValidationInProgress = true;
|
||||
|
||||
const validationMsg = document.createElement('div');
|
||||
validationMsg.id = 'vatValidationMessage';
|
||||
validationMsg.style.cssText = 'margin-top: 5px; font-size: 12px; color: #ffc107;';
|
||||
validationMsg.innerHTML = '<i class="fa-solid fa-spinner fa-spin"></i> Validating VAT number...';
|
||||
vatInput.parentNode.appendChild(validationMsg);
|
||||
|
||||
// Call VIES API
|
||||
const result = await checkVATNumber(countryData.country_code, vatNumber);
|
||||
vatValidationInProgress = false;
|
||||
|
||||
if (result && result.valid === true) {
|
||||
// VAT number is valid
|
||||
vatValidationResult = result;
|
||||
vatInput.style.borderColor = '#28a745';
|
||||
validationMsg.style.color = '#28a745';
|
||||
validationMsg.innerHTML = '<i class="fa-solid fa-check-circle"></i> Valid VAT number';
|
||||
|
||||
// Format VAT number as CountryCode + VatNumber (e.g., DE115235681)
|
||||
const formattedVAT = result.countryCode + result.vatNumber;
|
||||
if (vatInput.value !== formattedVAT) {
|
||||
vatInput.value = formattedVAT;
|
||||
}
|
||||
} else {
|
||||
// VAT number is invalid or check failed
|
||||
vatValidationResult = null;
|
||||
vatInput.style.borderColor = '#dc3545';
|
||||
validationMsg.style.color = '#dc3545';
|
||||
validationMsg.innerHTML = '<i class="fa-solid fa-times-circle"></i> Invalid VAT number or validation failed';
|
||||
}
|
||||
|
||||
// Update tax display with new validation result
|
||||
updateTaxDisplay();
|
||||
}
|
||||
|
||||
// Add event listeners to country select and VAT number to update tax
|
||||
document.getElementById("paymentCountry").addEventListener('change', updateTaxDisplay);
|
||||
document.getElementById("paymentVatNumber").addEventListener('input', updateTaxDisplay);
|
||||
document.getElementById("paymentCountry").addEventListener('change', () => {
|
||||
vatValidationResult = null;
|
||||
const vatInput = document.getElementById("paymentVatNumber");
|
||||
vatInput.style.borderColor = '';
|
||||
const existingMessage = document.getElementById('vatValidationMessage');
|
||||
if (existingMessage) {
|
||||
existingMessage.remove();
|
||||
}
|
||||
updateTaxDisplay();
|
||||
|
||||
// Validate VAT if already entered
|
||||
if (vatInput.value.trim()) {
|
||||
if (vatValidationTimeout) {
|
||||
clearTimeout(vatValidationTimeout);
|
||||
}
|
||||
vatValidationTimeout = setTimeout(validateVATNumber, 500);
|
||||
}
|
||||
});
|
||||
|
||||
document.getElementById("paymentVatNumber").addEventListener('input', () => {
|
||||
// Debounce VAT validation
|
||||
if (vatValidationTimeout) {
|
||||
clearTimeout(vatValidationTimeout);
|
||||
}
|
||||
vatValidationTimeout = setTimeout(validateVATNumber, 1000);
|
||||
});
|
||||
|
||||
// Close modal on cancel
|
||||
document.getElementById("cancelPayment").onclick = () => {
|
||||
|
||||
Reference in New Issue
Block a user