Untitled
unknown
plain_text
20 days ago
11 kB
6
Indexable
Never
// Ajouter des champs personnalisés pour le prix payé sur AliExpress add_action('woocommerce_admin_order_data_after_order_details', 'add_aliexpress_price_fields'); function add_aliexpress_price_fields($order) { echo '<h3>Prix payés sur AliExpress :</h3>'; $order_items = $order->get_items(); foreach ($order_items as $item_id => $item) { $product_name = $item->get_name(); $aliexpress_price = get_post_meta($order->get_id(), '_aliexpress_price_' . $item_id, true); echo '<p class="form-field aliexpress_price_' . $item_id . '_field" style="width: 100%;">'; woocommerce_wp_text_input( array( 'id' => 'aliexpress_price_' . $item_id, 'label' => '• <strong>' . $product_name . '</strong>', 'placeholder' => 'Prix payé pour ' . $product_name, 'value' => $aliexpress_price, 'data_type' => 'price', 'style' => 'width: 80%;', 'description' => '€', ) ); echo '</p>'; } } // Sauvegarder les champs personnalisés add_action('woocommerce_process_shop_order_meta', 'save_aliexpress_price_fields'); function save_aliexpress_price_fields($order_id) { $order = wc_get_order($order_id); $order_items = $order->get_items(); foreach ($order_items as $item_id => $item) { if (isset($_POST['aliexpress_price_' . $item_id])) { update_post_meta($order_id, '_aliexpress_price_' . $item_id, wc_format_decimal($_POST['aliexpress_price_' . $item_id])); } } } // Ajouter une page d'administration pour le tableau des revenus add_action('admin_menu', 'add_revenue_table_page'); function add_revenue_table_page() { $page_hook = add_menu_page('Tableau des revenus', 'Tableau des revenus', 'manage_options', 'revenue-table', 'display_revenue_table', 'dashicons-chart-bar', 55); add_action('load-' . $page_hook, 'redirect_revenue_table'); } function redirect_revenue_table() { if (!isset($_GET['year']) || !isset($_GET['month'])) { wp_redirect(add_query_arg(array( 'page' => 'revenue-table', 'year' => date('Y'), 'month' => date('m') ), admin_url('admin.php'))); exit; } } function display_revenue_table() { if (!current_user_can('manage_options')) { return; } $current_year = date('Y'); $current_month = date('m'); $selected_year = isset($_GET['year']) ? intval($_GET['year']) : $current_year; $selected_month = isset($_GET['month']) ? intval($_GET['month']) : $current_month; $args = array( 'limit' => -1, 'date_created' => $selected_year . '-' . sprintf('%02d', $selected_month) . '-01...' . $selected_year . '-' . sprintf('%02d', $selected_month) . '-31', 'type' => 'shop_order', 'status' => array('wc-completed', 'wc-processing', 'wc-refunded'), ); $orders = wc_get_orders($args); $monthly_data = array(); $yearly_total = array( 'received' => 0, 'fees' => 0, 'aliexpress' => 0, 'profit' => 0 ); foreach ($orders as $order) { $order_date = $order->get_date_created(); if ($order_date->format('Y-m') !== sprintf('%04d-%02d', $selected_year, $selected_month)) { continue; } $order_total = $order->get_total(); $fees = 0; // Récupération des frais Stripe ou PayPal $stripe_fee = $order->get_meta('_stripe_fee', true); $paypal_fee = $order->get_meta('_paypal_fee', true); $fees = $stripe_fee ? $stripe_fee : ($paypal_fee ? $paypal_fee : 0); $aliexpress_total = 0; foreach ($order->get_items() as $item_id => $item) { $aliexpress_price = get_post_meta($order->get_id(), '_aliexpress_price_' . $item_id, true); if ($aliexpress_price !== '') { $aliexpress_total += floatval($aliexpress_price); } } $profit = $order_total - $fees - $aliexpress_total; $order_month = $order_date->format('n'); if (!isset($monthly_data[$order_month])) { $monthly_data[$order_month] = array( 'received' => 0, 'fees' => 0, 'aliexpress' => 0, 'profit' => 0, 'orders' => array() ); } $monthly_data[$order_month]['received'] += $order_total; $monthly_data[$order_month]['fees'] += $fees; $monthly_data[$order_month]['aliexpress'] += $aliexpress_total; $monthly_data[$order_month]['profit'] += $profit; $monthly_data[$order_month]['orders'][] = $order; $yearly_total['received'] += $order_total; $yearly_total['fees'] += $fees; $yearly_total['aliexpress'] += $aliexpress_total; $yearly_total['profit'] += $profit; } echo '<h1>Tableau des revenus - ' . $selected_year . '</h1>'; echo '<h2 class="nav-tab-wrapper">'; for ($year = $current_year; $year >= 2024; $year--) { $year_url = add_query_arg(array('page' => 'revenue-table', 'year' => $year, 'month' => $selected_month), admin_url('admin.php')); $active = ($year == $selected_year) ? 'nav-tab-active' : ''; echo '<a href="' . esc_url($year_url) . '" class="nav-tab ' . $active . '">' . $year . '</a>'; } echo '</h2>'; $mois_francais = array( 1 => 'Janvier', 2 => 'Février', 3 => 'Mars', 4 => 'Avril', 5 => 'Mai', 6 => 'Juin', 7 => 'Juillet', 8 => 'Août', 9 => 'Septembre', 10 => 'Octobre', 11 => 'Novembre', 12 => 'Décembre' ); echo '<ul class="subsubsub">'; for ($month = 1; $month <= 12; $month++) { $month_url = add_query_arg(array('page' => 'revenue-table', 'year' => $selected_year, 'month' => $month), admin_url('admin.php')); $active = ($month == $selected_month) ? 'current' : ''; echo '<li><a href="' . esc_url($month_url) . '" class="' . $active . '">' . $mois_francais[$month] . '</a> | </li>'; } echo '</ul>'; echo '<div style="clear: both;"></div>'; if (isset($monthly_data[$selected_month])) { display_month_table($monthly_data[$selected_month], $selected_month, $mois_francais); } else { echo '<p>Aucune donnée disponible pour ce mois.</p>'; } echo '<div style="clear: both;"></div>'; display_yearly_total($yearly_total); } function display_month_table($month_data, $selected_month, $mois_francais) { echo '<h3 style="margin-top: 20px;">' . $mois_francais[$selected_month] . '</h3>'; echo '<table class="wp-list-table widefat fixed striped" style="margin-top: 20px;">'; echo '<thead><tr style="background-color: #f1f1f1;"><th>Jour</th><th>ID</th><th>Prix reçu</th><th>Frais bancaires</th><th>Prix payé AliExpress</th><th>Profit</th><th>Action</th></tr></thead>'; echo '<tbody>'; foreach ($month_data['orders'] as $order) { $order_id = $order->get_id(); $order_total = $order->get_total(); $fees = 0; // Récupération des frais Stripe ou PayPal $stripe_fee = $order->get_meta('_stripe_fee', true); $paypal_fee = $order->get_meta('_paypal_fee', true); $fees = $stripe_fee ? $stripe_fee : ($paypal_fee ? $paypal_fee : 0); $aliexpress_total = 0; foreach ($order->get_items() as $item_id => $item) { $aliexpress_price = get_post_meta($order_id, '_aliexpress_price_' . $item_id, true); if ($aliexpress_price !== '') { $aliexpress_total += floatval($aliexpress_price); } } $profit = $order_total - $fees - $aliexpress_total; $profit_style = $profit >= 0 ? 'color: green; font-weight: bold;' : 'color: red; font-weight: bold;'; echo '<tr>'; echo '<td>' . $order->get_date_created()->date('j') . '</td>'; echo '<td>' . $order_id . '</td>'; echo '<td>' . wc_price($order_total) . '</td>'; echo '<td>' . wc_price($fees) . '</td>'; echo '<td>' . wc_price($aliexpress_total) . '</td>'; echo '<td style="' . $profit_style . '">' . wc_price($profit) . '</td>'; echo '<td><a href="' . admin_url('post.php?post=' . $order_id . '&action=edit') . '" target="_blank"><span class="dashicons dashicons-visibility"></span></a></td>'; echo '</tr>'; } echo '</tbody>'; echo '<tfoot>'; echo '<tr style="background-color: #e9e9e9; font-weight: bold;">'; echo '<th colspan="2">Total du mois</th>'; echo '<th>' . wc_price($month_data['received']) . '</th>'; echo '<th>' . wc_price($month_data['fees']) . '</th>'; echo '<th>' . wc_price($month_data['aliexpress']) . '</th>'; echo '<th style="color: ' . ($month_data['profit'] >= 0 ? 'green' : 'red') . ';">' . wc_price($month_data['profit']) . '</th>'; echo '<th></th>'; echo '</tr>'; echo '</tfoot>'; echo '</table>'; } function display_yearly_total($yearly_total) { echo '<h3 style="margin-top: 20px;">Total annuel</h3>'; echo '<table class="wp-list-table widefat fixed striped" style="margin-top: 20px;">'; echo '<thead><tr style="background-color: #f1f1f1;"><th>Prix reçu</th><th>Frais bancaires</th><th>Prix payé AliExpress</th><th>Profit</th></tr></thead>'; echo '<tbody>'; echo '<tr style="font-weight: bold;">'; echo '<td>' . wc_price($yearly_total['received']) . '</td>'; echo '<td>' . wc_price($yearly_total['fees']) . '</td>'; echo '<td>' . wc_price($yearly_total['aliexpress']) . '</td>'; echo '<td style="color: ' . ($yearly_total['profit'] >= 0 ? 'green' : 'red') . ';">' . wc_price($yearly_total['profit']) . '</td>'; echo '</tr>'; echo '</tbody>'; echo '</table>'; } // Ajouter la ligne de marge dans le détail de la commande add_action('woocommerce_admin_order_totals_after_total', 'add_margin_to_order_details', 10, 1); function add_margin_to_order_details($order_id) { $order = wc_get_order($order_id); $order_total = $order->get_total(); $fees = 0; // Récupération des frais Stripe ou PayPal $stripe_fee = $order->get_meta('_stripe_fee', true); $paypal_fee = $order->get_meta('_paypal_fee', true); $fees = $stripe_fee ? $stripe_fee : ($paypal_fee ? $paypal_fee : 0); $aliexpress_total = 0; foreach ($order->get_items() as $item_id => $item) { $aliexpress_price = get_post_meta($order_id, '_aliexpress_price_' . $item_id, true); if ($aliexpress_price !== '') { $aliexpress_total += floatval($aliexpress_price); } } $margin = $order_total - $fees - $aliexpress_total; $margin_style = $margin >= 0 ? 'color: green; font-weight: bold;' : 'color: red; font-weight: bold;'; ?> <tr> <td class="label">Marge:</td> <td width="1%"></td> <td class="total"> <span class="woocommerce-Price-amount amount" style="<?php echo $margin_style; ?>"> <?php echo wc_price($margin); ?> </span> </td> </tr> <?php }
Leave a Comment