// class WC_SE_Exporter { // public function __construct() { // add_action('admin_init', [$this, 'handle_export_request']); // add_action('wp_ajax_wc_se_export_ajax', [$this, 'handle_ajax_export']); // } // public function handle_export_request() { // if (!isset($_POST['action']) || $_POST['action'] !== 'wc_se_export') { // return; // } // check_admin_referer('wc_se_export'); // if (!current_user_can('manage_woocommerce')) { // wp_die('دسترسی غیرمجاز'); // } // $period = sanitize_text_field($_POST['period'] ?? 'today'); // $this->generate_report($period); // } // public function handle_ajax_export() { // check_ajax_referer('wc_se_export_ajax', 'security'); // if (!current_user_can('manage_woocommerce')) { // wp_send_json_error('دسترسی غیرمجاز', 403); // } // @set_time_limit(300); // افزایش زمان اجرا به 5 دقیقه // @ini_set('memory_limit', '512M'); // افزایش حافظه // $period = sanitize_text_field($_POST['period'] ?? 'today'); // $this->generate_report($period, true); // } // private function generate_report($period, $is_ajax = false) { // try { // $report_data = $this->prepare_report_data($period); // if (empty($report_data)) { // throw new Exception('هیچ سفارشی در این بازه زمانی یافت نشد'); // } // $filename = 'sales_report_' . current_time('Y-m-d') . '.xls'; // $excel_content = $this->generate_excel_content($report_data); // if ($is_ajax) { // wp_send_json_success([ // 'filename' => $filename, // 'content' => base64_encode($excel_content), // 'message' => 'گزارش با موفقیت تولید شد' // ]); // } else { // $this->force_download($filename, $excel_content); // } // } catch (Exception $e) { // $error_message = 'خطا در تولید گزارش: ' . $e->getMessage(); // error_log($error_message); // if ($is_ajax) { // wp_send_json_error($error_message); // } else { // wp_die($error_message); // } // } // } // private function prepare_report_data($period) { // $date_range = $this->get_date_range($period); // $args = [ // 'status' => array_keys(wc_get_order_statuses()), // 'limit' => 500, // محدودیت برای جلوگیری از overload // 'date_created' => $date_range['start'] . '...' . $date_range['end'], // 'return' => 'objects' // ]; // $orders = wc_get_orders($args); // $report = []; // $target_categories = [181, 182, 183, 184, 185]; // $category_names = [ // 181 => 'adidas', // 182 => 'Nike', // 183 => 'Puma', // 184 => 'Reebok', // 185 => 'Jordan' // ]; // foreach ($orders as $order) { // try { // $payment_method = $this->get_payment_gateway_name($order); // if (!isset($report[$payment_method])) { // $report[$payment_method] = array_fill_keys($target_categories, 0); // } // $processed_products = []; // foreach ($order->get_items() as $item) { // $product = $item->get_product(); // if (!$product || in_array($product->get_id(), $processed_products)) { // continue; // } // $processed_products[] = $product->get_id(); // $categories = $this->get_product_categories($product); // $item_total = (float) $item->get_total(); // foreach ($categories as $cat_id) { // if (in_array($cat_id, $target_categories)) { // $report[$payment_method][$cat_id] += $item_total; // } // } // } // } catch (Exception $e) { // error_log('Error processing order #' . $order->get_id() . ': ' . $e->getMessage()); // continue; // } // } // return $report; // } // private function get_payment_gateway_name($order) { // $payment_method = $order->get_payment_method(); // $gateways = WC()->payment_gateways->payment_gateways(); // if (isset($gateways[$payment_method])) { // return $gateways[$payment_method]->title; // } // // تبدیل نام‌های مختلف به فرمت یکسان // $method_lower = strtolower($payment_method); // if (strpos($method_lower, 'snap') !== false || strpos($method_lower, 'اسنپ') !== false) { // return 'پرداخت اقساطی اسنپ پی'; // } elseif (strpos($method_lower, 'mellat') !== false || strpos($method_lower, 'به پرداخت') !== false) { // return 'به پرداخت'; // } // return $payment_method ?: 'سایر'; // } // private function get_product_categories($product) { // $terms = wp_get_object_terms($product->get_id(), 'product_cat'); // return wp_list_pluck($terms, 'term_id'); // } // private function get_date_range($period) { // $current_date = current_time('Y-m-d'); // switch ($period) { // case 'week': // return [ // 'start' => date('Y-m-d', strtotime('monday this week')), // 'end' => $current_date // ]; // case 'month': // return [ // 'start' => date('Y-m-01'), // 'end' => $current_date // ]; // case 'custom': // $start = sanitize_text_field($_POST['start_date'] ?? ''); // $end = sanitize_text_field($_POST['end_date'] ?? ''); // if (empty($start) || empty($end)) { // throw new Exception('لطفاً هر دو تاریخ را انتخاب کنید'); // } // if ($start > $end) { // throw new Exception('تاریخ شروع نمی‌تواند بعد از تاریخ پایان باشد'); // } // return ['start' => $start, 'end' => $end]; // default: // today // return ['start' => $current_date, 'end' => $current_date]; // } // } // private function generate_excel_content($report_data) { // $category_names = [ // 181 => 'adidas', // 182 => 'Nike', // 183 => 'Puma', // 184 => 'Reebok', // 185 => 'Jordan' // ]; // $content = "\xEF\xBB\xBF"; // BOM for UTF-8 // foreach ($report_data as $payment_method => $categories) { // $content .= "*{$payment_method}:*\r\n"; // foreach ($categories as $cat_id => $amount) { // $content .= "- *category name : {$category_names[$cat_id]} id :{$cat_id}:* " // . number_format($amount) . " تومان\r\n"; // } // $content .= "\r\n"; // } // return $content; // } // private function force_download($filename, $content) { // header('Content-Description: File Transfer'); // header('Content-Type: application/vnd.ms-excel'); // header('Content-Disposition: attachment; filename="' . $filename . '"'); // header('Content-Length: ' . strlen($content)); // header('Pragma: public'); // header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); // header('Expires: 0'); // echo $content; // exit; // } // }