' . esc_html__( 'Tableau de bord', 'totalio-ereporting' ) . '' ); return $links; } public static function render(): void { if ( ! current_user_can( self::CAPABILITY ) ) { wp_die( esc_html__( 'Accès refusé.', 'totalio-ereporting' ) ); } $settings = self::settings(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- simple navigation en lecture. $tab = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : 'tableau'; if ( empty( $settings['configured'] ) ) { $tab = 'reglages'; } echo '
'; echo '
'; echo ''; echo '

' . esc_html( EREPORTING_BRAND ) . ' · ' . esc_html__( 'E-reporting des ventes aux particuliers', 'totalio-ereporting' ) . '

'; echo '' . esc_html( sprintf( /* translators: %s: numéro de version */ __( 'Version %s', 'totalio-ereporting' ), EREPORTING_VERSION ) ) . ''; echo '
'; self::tabs( $tab, ! empty( $settings['configured'] ) ); if ( 'reglages' === $tab ) { include EREPORTING_DIR . '/src/Admin/views/settings.php'; } elseif ( 'service' === $tab ) { include EREPORTING_DIR . '/src/Admin/views/service.php'; } else { $view = self::dashboardData( $settings ); include EREPORTING_DIR . '/src/Admin/views/dashboard.php'; } echo '
'; } private static function tabs( string $current, bool $configured ): void { $tabs = $configured ? array( 'tableau' => __( 'Tableau de bord', 'totalio-ereporting' ), 'reglages' => __( 'Réglages', 'totalio-ereporting' ), 'service' => __( 'Service en ligne', 'totalio-ereporting' ), ) : array( 'reglages' => __( 'Configuration', 'totalio-ereporting' ) ); echo ''; } /** Mois affiché : paramètre « mois » (AAAA-MM) ou mois en cours, heure de Paris. */ public static function selectedMonth(): string { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- simple filtre d'affichage. $month = isset( $_GET['mois'] ) ? sanitize_text_field( wp_unslash( $_GET['mois'] ) ) : ''; if ( ! preg_match( '/^(20[2-9]\d)-(0[1-9]|1[0-2])$/', $month ) ) { $month = ( new \DateTimeImmutable( 'now', new \DateTimeZone( 'Europe/Paris' ) ) )->format( 'Y-m' ); } return $month; } /** @return array{0:string,1:string} premier et dernier jour du mois. */ public static function monthRange( string $month ): array { $first = $month . '-01'; $last = ( new \DateTimeImmutable( $first ) )->format( 'Y-m-t' ); return array( $first, $last ); } private static function dashboardData( array $settings ): array { $month = self::selectedMonth(); list( $from, $to ) = self::monthRange( $month ); $result = Totals::compute( $from, $to, SettingsInput::toSettings( $settings ) ); $today = ( new \DateTimeImmutable( 'now', new \DateTimeZone( 'Europe/Paris' ) ) )->format( 'Y-m-d' ); $next = ReminderPlanner::nextDeadline( $settings['regime'], $settings['start_date'], $today ); $upcoming = Calendar::upcoming( $settings['regime'], $next->start, 4 ); $orderLinks = array(); foreach ( $result['anomalies'] as $anomaly ) { $order = wc_get_order( (int) $anomaly['order'] ); if ( $order && ! isset( $orderLinks[ $anomaly['order'] ] ) ) { $orderLinks[ $anomaly['order'] ] = $order->get_edit_order_url(); } } return array( 'month' => $month, 'result' => $result, 'today' => $today, 'next' => $next, 'daysLeft' => ReminderPlanner::daysBetween( $today, $next->deadline ), 'upcoming' => $upcoming, 'orderLinks' => $orderLinks, 'settings' => $settings, ); } public static function saveSettings(): void { if ( ! current_user_can( self::CAPABILITY ) ) { wp_die( esc_html__( 'Accès refusé.', 'totalio-ereporting' ), '', array( 'response' => 403 ) ); } check_admin_referer( 'ereporting_save_settings' ); $raw = isset( $_POST['ereporting'] ) && is_array( $_POST['ereporting'] ) ? map_deep( wp_unslash( $_POST['ereporting'] ), 'sanitize_text_field' ) : array(); list( $clean, $errors ) = SettingsInput::sanitize( $raw, (string) get_option( 'admin_email' ) ); update_option( self::OPTION, $clean, false ); set_transient( 'ereporting_settings_errors_' . get_current_user_id(), $errors, 60 ); wp_safe_redirect( admin_url( 'admin.php?page=' . self::PAGE . '&tab=' . ( empty( $errors ) ? 'tableau' : 'reglages' ) . '&enregistre=1' ) ); exit; } public static function exportCsv(): void { if ( ! current_user_can( self::CAPABILITY ) ) { wp_die( esc_html__( 'Accès refusé.', 'totalio-ereporting' ), '', array( 'response' => 403 ) ); } check_admin_referer( 'ereporting_export_csv' ); $month = isset( $_POST['mois'] ) ? sanitize_text_field( wp_unslash( $_POST['mois'] ) ) : ''; if ( ! preg_match( '/^(20[2-9]\d)-(0[1-9]|1[0-2])$/', $month ) ) { wp_die( esc_html__( 'Mois invalide.', 'totalio-ereporting' ), '', array( 'response' => 400 ) ); } list( $from, $to ) = self::monthRange( $month ); $result = Totals::compute( $from, $to, SettingsInput::toSettings( self::settings() ) ); nocache_headers(); header( 'Content-Type: text/csv; charset=utf-8' ); header( 'Content-Disposition: attachment; filename="e-reporting-' . $month . '.csv"' ); echo CsvExporter::export( $result['totals'] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- fichier CSV, cellules échappées par CsvExporter. exit; } /** Garde commune des actions de la rubrique « Service en ligne ». */ /** Droits de l'utilisateur ; le jeton de sécurité (nonce) est vérifié dans chaque action. */ private static function guard(): void { if ( ! current_user_can( self::CAPABILITY ) ) { wp_die( esc_html__( 'Accès refusé.', 'totalio-ereporting' ), '', array( 'response' => 403 ) ); } } private static function backToService( bool $ok, string $text ): void { set_transient( 'ereporting_service_message_' . get_current_user_id(), array( 'ok' => $ok, 'text' => $text, ), 60 ); wp_safe_redirect( admin_url( 'admin.php?page=' . self::PAGE . '&tab=service' ) ); exit; } public static function pair(): void { self::guard(); check_admin_referer( 'ereporting_pair' ); if ( empty( $_POST['consent'] ) ) { self::backToService( false, __( 'Cochez la case d\'accord pour relier votre boutique.', 'totalio-ereporting' ) ); } $code = isset( $_POST['code'] ) ? sanitize_text_field( wp_unslash( $_POST['code'] ) ) : ''; if ( '' === $code ) { self::backToService( false, __( 'Collez le code de liaison obtenu dans votre espace.', 'totalio-ereporting' ) ); } list( $ok, $text ) = \EReporting\Service\Client::pair( $code ); self::backToService( $ok, $text ); } public static function syncNow(): void { self::guard(); check_admin_referer( 'ereporting_sync_now' ); list( $ok, $problem ) = \EReporting\Service\Client::sync(); self::backToService( $ok, $ok ? __( 'Totaux envoyés.', 'totalio-ereporting' ) : sprintf( /* translators: %s: code d'erreur */ __( 'L\'envoi a échoué (%s). Un nouvel essai aura lieu automatiquement.', 'totalio-ereporting' ), $problem ) ); } public static function unlink(): void { self::guard(); check_admin_referer( 'ereporting_unlink' ); \EReporting\Service\Client::unlink(); self::backToService( true, __( 'Boutique déliée : plus rien n\'est envoyé au service en ligne.', 'totalio-ereporting' ) ); } public static function dashboardWidget(): void { if ( ! current_user_can( self::CAPABILITY ) ) { return; } wp_add_dashboard_widget( 'ereporting_next_deadline', __( 'E-reporting : prochaine échéance', 'totalio-ereporting' ), array( self::class, 'renderWidget' ) ); } public static function renderWidget(): void { $settings = self::settings(); if ( empty( $settings['configured'] ) ) { printf( '

%s %s

', esc_html__( 'Indiquez votre régime de TVA pour afficher vos échéances.', 'totalio-ereporting' ), esc_url( admin_url( 'admin.php?page=' . self::PAGE ) ), esc_html__( 'Configurer', 'totalio-ereporting' ) ); return; } $today = ( new \DateTimeImmutable( 'now', new \DateTimeZone( 'Europe/Paris' ) ) )->format( 'Y-m-d' ); $next = ReminderPlanner::nextDeadline( $settings['regime'], $settings['start_date'], $today ); $days = ReminderPlanner::daysBetween( $today, $next->deadline ); printf( '

%s

%s

%s

', esc_html( sprintf( /* translators: 1: début, 2: fin de période */ __( 'Ventes du %1$s au %2$s', 'totalio-ereporting' ), View::date( $next->start ), View::date( $next->end ) ) ), esc_html( sprintf( /* translators: 1: date limite, 2: nombre de jours */ __( 'À transmettre au plus tard le %1$s (dans %2$d jours)', 'totalio-ereporting' ), View::date( $next->deadline ), $days ) ), esc_url( admin_url( 'admin.php?page=' . self::PAGE ) ), esc_html__( 'Voir les totaux', 'totalio-ereporting' ) ); } }