From 1d709ca6f05acb0584202a5c1d08a7f051848080 Mon Sep 17 00:00:00 2001 From: Frank Cools Date: Fri, 3 Oct 2025 14:32:02 +0200 Subject: [PATCH] Add special VAT number formatting with custom spacing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Added formatVatNumber() method for special VAT number formatting - Space between every character - 3 extra spaces after first 2 characters - 43 extra spaces after first 4 characters - Example: FR85489417469 → F R 8 5 4 8 9 4 1 7 4 6 9 - Handles edge cases like empty or short VAT numbers - Cleans input by removing non-alphanumeric characters and converting to uppercase --- core/class/declarationtva_pdf.class.php | 44 ++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/core/class/declarationtva_pdf.class.php b/core/class/declarationtva_pdf.class.php index 6765011..4bd71ba 100644 --- a/core/class/declarationtva_pdf.class.php +++ b/core/class/declarationtva_pdf.class.php @@ -302,7 +302,7 @@ class DeclarationTVA_PDF $field_data['company_siret'] = $this->formatSiret($siret_value); - $field_data['company_vat_number'] = $mysoc->tva_intra; // VAT number + $field_data['company_vat_number'] = $this->formatVatNumber($mysoc->tva_intra); // VAT number $field_data['declaration_period_start'] = dol_print_date($declaration->start_date, 'day'); $field_data['declaration_period_end'] = dol_print_date($declaration->end_date, 'day'); $field_data['declaration_number'] = $declaration->declaration_number; @@ -1107,4 +1107,46 @@ class DeclarationTVA_PDF return $formatted_main . $formatted_last; } + + /** + * Format VAT number with special spacing + * Adds space between every character, with 3 spaces after first 2 chars and 43 spaces after first 4 chars + * + * @param string $vat_number VAT number + * @return string Formatted VAT number + */ + private function formatVatNumber($vat_number) + { + // Remove any existing spaces or special characters + $vat_number = preg_replace('/[^A-Z0-9]/', '', strtoupper($vat_number)); + + // If VAT number is not configured or too short, return as is + if (empty($vat_number) || strlen($vat_number) < 4) { + return $vat_number; + } + + $result = ''; + $length = strlen($vat_number); + + for ($i = 0; $i < $length; $i++) { + $result .= $vat_number[$i]; + + // Add space between every character + if ($i < $length - 1) { + $result .= ' '; + } + + // After first 2 characters, add 3 extra spaces + if ($i == 1) { + $result .= ' '; + } + + // After first 4 characters, add 43 extra spaces + if ($i == 3) { + $result .= str_repeat(' ', 43); + } + } + + return $result; + } }