Refactor authorization and token refresh logic; update tax handling and invoice generation
- Changed variable name from `$stmt_service` to `$stmt_refreshkey` for clarity in `authorization.php` and `token_refresh.php`. - Added null coalescing operator to ensure criteria are set to an empty string if not provided in `products_software_versions.php`. - Modified SQL script to add `eu` column to `taxes` table and update tax rates based on EU membership. - Enhanced invoice generation logic in `functions.php` to include VAT notes based on customer country and VAT number. - Updated email and PDF templates to display VAT notes and percentages correctly. - Adjusted JavaScript tax calculation logic to handle VAT based on country and VAT number. - Fixed API URL in `index.php` for token refresh endpoint. - Updated countries data structure in `countries.php` to include EU membership status.
This commit is contained in:
@@ -5627,12 +5627,12 @@ function generateSoftwareInvoice($invoice_data, $order_id, $language = 'US') {
|
||||
// Extract customer data
|
||||
$customer = $invoice_data['customer'] ?? [];
|
||||
$customer_email = $customer['email'] ?? $invoice_data['payer_email'] ?? '';
|
||||
$customer_name = trim(($customer['first_name'] ?? '') . ' ' . ($customer['last_name'] ?? ''));
|
||||
$customer_address = $customer['address_street'] ?? '';
|
||||
$customer_city = $customer['address_city'] ?? '';
|
||||
$customer_state = $customer['address_state'] ?? '';
|
||||
$customer_zip = $customer['address_zip'] ?? '';
|
||||
$customer_country = $customer['address_country'] ?? '';
|
||||
$customer_name = $customer['name'] ?? trim(($customer['first_name'] ?? '') . ' ' . ($customer['last_name'] ?? ''));
|
||||
$customer_address = $customer['street'] ?? $customer['address_street'] ?? '';
|
||||
$customer_city = $customer['city'] ?? $customer['address_city'] ?? '';
|
||||
$customer_state = $customer['state'] ?? $customer['address_state'] ?? '';
|
||||
$customer_zip = $customer['zip'] ?? $customer['address_zip'] ?? '';
|
||||
$customer_country = $customer['country'] ?? $customer['address_country'] ?? '';
|
||||
|
||||
// Extract transaction data
|
||||
$pricing = $invoice_data['pricing'] ?? [];
|
||||
@@ -5744,6 +5744,54 @@ function generateSoftwareInvoice($invoice_data, $order_id, $language = 'US') {
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate VAT note based on country and VAT number
|
||||
$vat_note = '';
|
||||
$customer_vat_number = $customer['vat_number'] ?? '';
|
||||
|
||||
// Load countries array
|
||||
include dirname(__FILE__,2).'/settings/countries.php';
|
||||
|
||||
debuglog("Customer Country: " . ($customer_country ?? 'NULL'));
|
||||
debuglog("Customer VAT: " . ($customer_vat_number ?? 'NULL'));
|
||||
|
||||
if (!empty($customer_country) && isset($countries)) {
|
||||
// Find country data
|
||||
$country_data = null;
|
||||
foreach ($countries as $country_info) {
|
||||
if ($country_info['country'] === $customer_country) {
|
||||
$country_data = $country_info;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($country_data) {
|
||||
$is_eu = $country_data['eu'] === 1;
|
||||
$is_netherlands = $customer_country === 'Netherlands';
|
||||
|
||||
if ($is_netherlands) {
|
||||
// Netherlands: no special note needed
|
||||
$vat_note = '';
|
||||
} elseif ($is_eu) {
|
||||
if (!empty($customer_vat_number)) {
|
||||
// EU with VAT number: reverse charge
|
||||
$vat_note = 'Reverse charge VAT';
|
||||
} else {
|
||||
// EU without VAT number: local VAT
|
||||
$vat_note = 'Local VAT';
|
||||
}
|
||||
} else {
|
||||
// Non-EU: out of scope
|
||||
$vat_note = 'Out of scope of EU VAT. Buyer is responsible for local taxes and duties';
|
||||
}
|
||||
|
||||
debuglog("VAT Note set to: " . ($vat_note ?: 'EMPTY'));
|
||||
} else {
|
||||
debuglog("Country NOT found in array: " . $customer_country);
|
||||
}
|
||||
} else {
|
||||
debuglog("Empty customer_country or countries not loaded");
|
||||
}
|
||||
|
||||
// Build HTML for PDF and EMAIL
|
||||
include dirname(__FILE__,2).'/assets/mail/email_template_invoice.php';
|
||||
include dirname(__FILE__,2).'/assets/mail/pdf_template_invoice.php';
|
||||
@@ -5816,7 +5864,8 @@ function generateCountriesFile($token){
|
||||
foreach($taxes as $tax){
|
||||
$countries[$tax['id']] = [
|
||||
'country' => $tax['country'] ?? '',
|
||||
'taxes' => $tax['rate'] ?? 0
|
||||
'taxes' => $tax['rate'] ?? 0,
|
||||
'eu' => $tax['eu'] ?? 0
|
||||
];
|
||||
}
|
||||
|
||||
@@ -5826,7 +5875,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'] . "],\n";
|
||||
$fileContent .= " " . $id . " => ['country' => '" . addslashes($data['country']) . "', 'taxes' => " . $data['taxes'] . ",'eu' => " . $data['eu'] . "],\n";
|
||||
}
|
||||
$fileContent .= "];\n";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user