From f6835b4da7f2d23738b3cef8245a2646a8a67b21 Mon Sep 17 00:00:00 2001 From: jps Date: Fri, 23 Jan 2026 12:24:18 +0100 Subject: [PATCH] Ajout check num factu --- app/controllers/Peppol_controller.php | 45 ++++++++++++++++++++++++++- app/controllers/Plugin.php | 30 ++++++++++++++++++ templates/admin/dashboard.php | 31 ++++++++++++++---- templates/admin/settings.php | 36 +++++++++++++++++++++ 4 files changed, 135 insertions(+), 7 deletions(-) diff --git a/app/controllers/Peppol_controller.php b/app/controllers/Peppol_controller.php index 3431181..98ab33f 100644 --- a/app/controllers/Peppol_controller.php +++ b/app/controllers/Peppol_controller.php @@ -509,6 +509,25 @@ class PEPPOL_peppol_controller { public static function build_payload_from_order(\WC_Order $order): array { $order_id = $order->get_id(); $order_number = $order->get_order_number(); + $invoice_number = $order_number; + + $wpo_invoice_number = (string) $order->get_meta('_wcpdf_invoice_number', true); + if ($wpo_invoice_number !== '') { + $invoice_number = $wpo_invoice_number; + } else { + $use_backup_format = !\ESI_PEPPOL\helpers\PEPPOL_Woo_Helper::esi_has_wpo_invoice_numbers(); + if ($use_backup_format) { + $backup_format = (string) \get_option('esi_peppol_vat_number_format', ''); + if ($backup_format !== '') { + $generated = self::build_invoice_number_from_format($backup_format, $order); + if ($generated !== '') { + $invoice_number = $generated; + $order->update_meta_data('_wcpdf_invoice_number', $generated); + $order->save(); + } + } + } + } $currency_code = $order->get_currency(); $invoice_date = $order->get_date_created() @@ -995,7 +1014,7 @@ class PEPPOL_peppol_controller { 'document_type' => 'invoice', 'external_reference' => $external_reference, 'invoice' => [ - 'invoice_number' => $order_number, + 'invoice_number' => $invoice_number, 'invoice_date' => $invoice_date, 'due_date' => $due_date, 'currency_code' => $currency_code, @@ -1014,4 +1033,28 @@ class PEPPOL_peppol_controller { return \apply_filters('esi_peppol_payload_from_order', $payload, $order); } + + /** + * Construit un numéro de facture depuis un format de secours. + * Placeholders supportés : {YYYY}, {YY}, {MM}, {DD}, {ORDER_ID}, {ORDER_NUMBER} + */ + private static function build_invoice_number_from_format(string $format, \WC_Order $order): string { + $date = $order->get_date_created(); + $year = $date ? $date->date('Y') : \gmdate('Y'); + $month = $date ? $date->date('m') : \gmdate('m'); + $day = $date ? $date->date('d') : \gmdate('d'); + + $replacements = [ + '{YYYY}' => $year, + '{YY}' => substr($year, -2), + '{MM}' => $month, + '{DD}' => $day, + '{ORDER_ID}' => (string) $order->get_id(), + '{ORDER_NUMBER}' => (string) $order->get_order_number(), + ]; + + $result = strtr($format, $replacements); + + return trim($result); + } } \ No newline at end of file diff --git a/app/controllers/Plugin.php b/app/controllers/Plugin.php index 3b987d9..5a37997 100644 --- a/app/controllers/Plugin.php +++ b/app/controllers/Plugin.php @@ -155,12 +155,17 @@ class PEPPOL_Plugin { $password = isset($_POST['esi_peppol_password']) ? sanitize_text_field(wp_unslash($_POST['esi_peppol_password'])) : ''; $email = isset($_POST['esi_peppol_email']) ? sanitize_email(wp_unslash($_POST['esi_peppol_email'])) : ''; $logo_email_id = isset($_POST['esi_peppol_logo_email_id']) ? absint(wp_unslash($_POST['esi_peppol_logo_email_id'])) : 0; + $vat_number_format = get_option('esi_peppol_vat_number_format', ''); + if (isset($_POST['esi_peppol_vat_number_format'])) { + $vat_number_format = sanitize_text_field(wp_unslash($_POST['esi_peppol_vat_number_format'])); + } // Sauvegarde systématique des options update_option('esi_peppol_api_key', $api_key); update_option('esi_peppol_password', $password); update_option('esi_peppol_email', $email); update_option('esi_peppol_logo_email_id', $logo_email_id); + update_option('esi_peppol_vat_number_format', $vat_number_format); if ($action === 'save') { $notice = __('Paramètres enregistrés avec succès.', 'esi_peppol'); @@ -1291,6 +1296,14 @@ class PEPPOL_Plugin { 'action_url' => admin_url('admin.php?page=esi-peppol-settings'), 'action_label' => __('Détecter le champ TVA', 'esi_peppol'), ], + 'vat_number_format' => [ + 'completed' => false, + 'value' => '', + 'label' => __('Format num TVA', 'esi_peppol'), + 'description' => __('Format du numéro de TVA (backup si aucun format détecté automatiquement)', 'esi_peppol'), + 'action_url' => admin_url('admin.php?page=esi-peppol-settings'), + 'action_label' => __('Configurer le format', 'esi_peppol'), + ], 'api_credentials' => [ 'completed' => false, 'value' => '', @@ -1332,6 +1345,23 @@ class PEPPOL_Plugin { $status['vat_field']['value'] = $vat_field_key; } + // Vérifier le format num TVA (détecté ou backup) + $detected_format = ''; + if (\ESI_PEPPOL\helpers\PEPPOL_Woo_Helper::esi_has_wpo_invoice_numbers()) { + $detected_format = \ESI_PEPPOL\helpers\PEPPOL_Woo_Helper::esi_get_wpo_invoice_number_format_label(); + } + $backup_format = (string) get_option('esi_peppol_vat_number_format', ''); + if ($detected_format !== '') { + $status['vat_number_format']['completed'] = true; + $status['vat_number_format']['value'] = sprintf( + __('Format détecté : %s', 'esi_peppol'), + $detected_format + ); + } elseif ($backup_format !== '') { + $status['vat_number_format']['completed'] = true; + $status['vat_number_format']['value'] = $backup_format; + } + // Vérifier les identifiants API $api_key = (string) get_option('esi_peppol_api_key', ''); $password = (string) get_option('esi_peppol_password', ''); diff --git a/templates/admin/dashboard.php b/templates/admin/dashboard.php index c809024..0d89ba0 100644 --- a/templates/admin/dashboard.php +++ b/templates/admin/dashboard.php @@ -64,9 +64,28 @@ $settings_status = \ESI_PEPPOL\controllers\PEPPOL_Plugin::check_settings_status( - + +
+
+

+

+ +
+ + +
+ + + + + +
+
+ +
+ data-step="">

@@ -83,8 +102,8 @@ $settings_status = \ESI_PEPPOL\controllers\PEPPOL_Plugin::check_settings_status(
- -
+ +

@@ -108,9 +127,9 @@ $settings_status = \ESI_PEPPOL\controllers\PEPPOL_Plugin::check_settings_status(
- +
+ data-step="">

diff --git a/templates/admin/settings.php b/templates/admin/settings.php index 926fe1d..c12e899 100644 --- a/templates/admin/settings.php +++ b/templates/admin/settings.php @@ -193,6 +193,42 @@ if ($logo_email_id) { + + + + + + + +

+ + + + +

+

+ +

+ + +

+ +

+ + + +