format( 'Y-m-d' ) === $value ) ? $value : $fallback; } /** A stable key identifying the buyer behind an order (customer id, else email/phone). */ function videm_app_report_buyer_key( $order ) { if ( $order->get_customer_id() ) { return 'u' . (int) $order->get_customer_id(); } $email = strtolower( trim( (string) $order->get_billing_email() ) ); $mobile = videm_app_normalize_mobile( $order->get_billing_phone() ); return 'g' . md5( $email . '|' . $mobile ); } /** * Scan paid orders in a date range and build the sales and reconciliation data. * * One pass over the orders feeds both reports. Sales aggregates revenue and buyers * per course; reconciliation lists paid orders whose mapped courses are not all * registered in the panel yet. Orders carrying the legacy _videm_data access id are * treated as already handled by the old plugin and skipped for reconciliation. * * @return array{ * courses:array}>, * order_total:float, order_count:int, * incomplete:array, incomplete_courses:int, * scanned:int, truncated:bool * } */ function videm_app_collect_report_data( $from, $to ) { $result = array( 'courses' => array(), 'order_total' => 0.0, 'order_count' => 0, 'incomplete' => array(), 'incomplete_courses' => 0, 'scanned' => 0, 'truncated' => false, ); if ( ! function_exists( 'wc_get_orders' ) ) { return $result; } $titles = videm_app_available_courses(); $limit = (int) VIDEM_APP_REPORT_MAX_ORDERS; $orders = wc_get_orders( array( 'limit' => $limit + 1, 'status' => wc_get_is_paid_statuses(), 'date_created' => $from . '...' . $to, 'orderby' => 'date', 'order' => 'DESC', 'type' => 'shop_order', ) ); if ( count( $orders ) > $limit ) { $result['truncated'] = true; $orders = array_slice( $orders, 0, $limit ); } $result['scanned'] = count( $orders ); foreach ( $orders as $order ) { if ( ! $order instanceof WC_Order ) { continue; } $order_courses = videm_app_order_courses( $order ); if ( ! $order_courses ) { continue; } // Sales side: count the order once, add the line totals of its Videm items, // and attribute each item's total to every course the item maps to. $buyer = videm_app_report_buyer_key( $order ); $order_rev = 0.0; $seen_course = array(); foreach ( $order->get_items() as $item ) { if ( ! $item instanceof WC_Order_Item_Product ) { continue; } $item_courses = videm_app_product_courses( $item->get_product() ); if ( ! $item_courses ) { continue; } $item_total = (float) $item->get_total() + (float) $item->get_total_tax(); $order_rev += $item_total; foreach ( $item_courses as $uuid ) { if ( ! isset( $result['courses'][ $uuid ] ) ) { $result['courses'][ $uuid ] = array( 'title' => isset( $titles[ $uuid ] ) ? $titles[ $uuid ] : $uuid, 'orders' => 0, 'revenue' => 0.0, 'buyers' => array(), ); } $result['courses'][ $uuid ]['revenue'] += $item_total; $result['courses'][ $uuid ]['buyers'][ $buyer ] = true; if ( empty( $seen_course[ $uuid ] ) ) { $result['courses'][ $uuid ]['orders']++; $seen_course[ $uuid ] = true; } } } $result['order_total'] += $order_rev; $result['order_count']++; // Reconciliation side: how many mapped courses are registered in the panel. $legacy = $order->get_meta( '_videm_data', true ); if ( is_array( $legacy ) && ! empty( $legacy['_id'] ) ) { continue; } $done = array_filter( (array) $order->get_meta( '_videm_app_enrolled_courses', true ) ); $missing = array_diff( $order_courses, array_keys( $done ) ); if ( ! $missing ) { continue; } $type = $order->get_meta( '_videm_app_order_identity_type', true ); $type = in_array( $type, array( 'mobile', 'email' ), true ) ? $type : videm_app_woo_settings()['identity_type']; $mobile = videm_app_normalize_mobile( $order->get_billing_phone() ); $email = sanitize_email( $order->get_billing_email() ); $reason = ''; if ( 'mobile' === $type && ! $mobile ) { $reason = __( 'موبایل صورتحساب نامعتبر', 'videm-app' ); } elseif ( 'email' === $type && ! is_email( $email ) ) { $reason = __( 'ایمیل صورتحساب نامعتبر', 'videm-app' ); } $result['incomplete_courses'] += count( $missing ); $result['incomplete'][] = array( 'id' => $order->get_id(), 'number' => $order->get_order_number(), 'date' => $order->get_date_created() ? $order->get_date_created()->date_i18n( 'Y/m/d H:i' ) : '', 'customer' => trim( $order->get_formatted_billing_full_name() ), 'type' => $type, 'mapped' => count( $order_courses ), 'done' => count( $done ), 'missing' => count( $missing ), 'status' => wc_get_order_status_name( $order->get_status() ), 'reason' => $reason, ); } return $result; } /** URL of the reports tab inside the main Videm settings page. */ function videm_app_reports_url( $args = array() ) { return add_query_arg( array_merge( array( 'page' => 'videm-app-settings', 'tab' => 'reports' ), $args ), admin_url( 'admin.php' ) ); } /** Render the reports tab content (the settings page provides the wrap and heading). */ function videm_app_render_reports_tab() { if ( ! current_user_can( 'manage_options' ) ) { return; } echo '
'; echo '

' . esc_html__( 'فروش و پرداخت دوره‌ها فقط در ووکامرس ثبت می‌شود؛ پنل ویدم آمار سفارش/فروش ندارد و دسترسی هنرجو را به‌صورت ثبت‌نام (نه خرید) نگه می‌دارد. این گزارش‌ها از سفارش‌های پرداخت‌شدهٔ ووکامرس ساخته می‌شوند. برای آمار تماشا و فعال‌سازی هنرجوها به تب «تعامل» بروید.', 'videm-app' ) . '

'; if ( function_exists( 'wc_get_orders' ) && current_user_can( 'manage_woocommerce' ) ) { videm_app_render_sales_reports(); } else { echo '

' . esc_html__( 'بخش فروش و تطبیق ثبت‌نام به ووکامرس نیاز دارد و ووکامرس فعال نیست.', 'videm-app' ) . '

'; } echo '
'; } /** The WooCommerce-sourced part of the tab: date filter, sales, and reconciliation. */ function videm_app_render_sales_reports() { $to = videm_app_report_date( 'to', current_time( 'Y-m-d' ) ); $from = videm_app_report_date( 'from', gmdate( 'Y-m-d', strtotime( $to . ' -30 days' ) ) ); if ( strtotime( $from ) > strtotime( $to ) ) { list( $from, $to ) = array( $to, $from ); } $data = videm_app_collect_report_data( $from, $to ); // A notice from a retry action, if we just came back from one. $n = filter_input( INPUT_GET, 'videm_retried', FILTER_VALIDATE_INT, array( 'options' => array( 'min_range' => 0 ) ) ); if ( is_int( $n ) ) { /* translators: Number of orders retried. */ echo '

' . esc_html( sprintf( _n( 'ثبت مجدد برای %d سفارش اجرا شد. نتیجه را در ستون‌ها و یادداشت‌های سفارش ببینید.', 'ثبت مجدد برای %d سفارش اجرا شد. نتیجه را در ستون‌ها و یادداشت‌های سفارش ببینید.', $n, 'videm-app' ), $n ) ) . '

'; } // Date filter. echo '
'; echo ''; echo ''; printf( '', esc_html__( 'از تاریخ', 'videm-app' ), esc_attr( $from ) ); printf( '', esc_html__( 'تا تاریخ', 'videm-app' ), esc_attr( $to ) ); echo ''; echo '
'; if ( $data['truncated'] ) { /* translators: Maximum number of orders included in the report. */ echo '

' . esc_html( sprintf( __( 'تعداد سفارش‌های این بازه از سقف %d بیشتر است و فقط جدیدترین‌ها بررسی شدند. برای دقت کامل، بازهٔ کوتاه‌تری انتخاب کنید.', 'videm-app' ), (int) VIDEM_APP_REPORT_MAX_ORDERS ) ) . '

'; } videm_app_render_sales_summary( $data ); videm_app_render_sales_table( $data ); videm_app_render_reconciliation_table( $data, $from, $to ); } /** Top summary cards: orders, revenue, incomplete enrollments. */ function videm_app_render_sales_summary( $data ) { $cards = array( array( __( 'سفارش‌های دارای دورهٔ ویدم', 'videm-app' ), number_format_i18n( $data['order_count'] ) ), array( __( 'مجموع فروش (پرداخت‌شده)', 'videm-app' ), wp_strip_all_tags( wc_price( $data['order_total'] ) ) ), array( __( 'سفارش‌های ثبت‌نام ناقص', 'videm-app' ), number_format_i18n( count( $data['incomplete'] ) ) ), ); echo '
'; foreach ( $cards as $card ) { printf( '
%s
%s
', esc_html( $card[1] ), esc_html( $card[0] ) ); } echo '
'; } /** Per-course sales table. */ function videm_app_render_sales_table( $data ) { echo '

' . esc_html__( 'فروش دوره‌ها', 'videm-app' ) . '

'; $courses = $data['courses']; if ( ! $courses ) { echo '

' . esc_html__( 'در این بازه فروشی برای دوره‌های ویدم ثبت نشده است.', 'videm-app' ) . '

'; return; } uasort( $courses, static function ( $a, $b ) { return $b['revenue'] <=> $a['revenue']; } ); echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; foreach ( $courses as $course ) { echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; } echo '
' . esc_html__( 'دوره', 'videm-app' ) . '' . esc_html__( 'تعداد سفارش', 'videm-app' ) . '' . esc_html__( 'خریدار یکتا', 'videm-app' ) . '' . esc_html__( 'مبلغ فروش', 'videm-app' ) . '
' . esc_html( $course['title'] ) . '' . esc_html( number_format_i18n( $course['orders'] ) ) . '' . esc_html( number_format_i18n( count( $course['buyers'] ) ) ) . '' . wp_kses_post( wc_price( $course['revenue'] ) ) . '
'; echo '

' . esc_html__( 'اگر یک محصول به چند دوره نگاشت شده باشد، مبلغ آن خط برای هر دوره شمرده می‌شود؛ بنابراین جمع ستون مبلغ می‌تواند از «مجموع فروش» بالاتر باشد.', 'videm-app' ) . '

'; } /** Table of paid orders whose enrollment is not fully registered, with a retry button. */ function videm_app_render_reconciliation_table( $data, $from, $to ) { echo '

' . esc_html__( 'تطبیق ثبت‌نام (سفارش‌های جاافتاده)', 'videm-app' ) . '

'; $rows = $data['incomplete']; if ( ! $rows ) { echo '

' . esc_html__( 'همهٔ سفارش‌های پرداخت‌شدهٔ این بازه در پنل ویدم ثبت شده‌اند. 🎉', 'videm-app' ) . '

'; return; } echo '

' . esc_html__( 'این سفارش‌ها پرداخت شده‌اند ولی همهٔ دوره‌هایشان در پنل ثبت نشده است. «ثبت مجدد» همان روند پرداخت را دوباره اجرا می‌کند؛ دوره‌های ثبت‌شده دوباره فرستاده نمی‌شوند.', 'videm-app' ) . '

'; $ids = wp_list_pluck( $rows, 'id' ); echo '
'; echo ''; echo ''; echo ''; echo ''; wp_nonce_field( 'videm_app_report_retry' ); /* translators: Number of orders to retry. */ echo ''; echo '
'; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; foreach ( $rows as $row ) { $edit = get_edit_post_link( $row['id'] ); if ( ! $edit ) { $edit = admin_url( 'admin.php?page=wc-orders&action=edit&id=' . (int) $row['id'] ); } $retry = wp_nonce_url( admin_url( 'admin-post.php?action=videm_app_report_retry&order_ids=' . (int) $row['id'] . '&from=' . rawurlencode( $from ) . '&to=' . rawurlencode( $to ) ), 'videm_app_report_retry' ); echo ''; printf( '', esc_url( $edit ), esc_html( $row['number'] ) ); echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; printf( '', esc_url( $retry ), esc_html__( 'ثبت مجدد', 'videm-app' ) ); echo ''; } echo '
' . esc_html__( 'سفارش', 'videm-app' ) . '' . esc_html__( 'تاریخ', 'videm-app' ) . '' . esc_html__( 'مشتری', 'videm-app' ) . '' . esc_html__( 'وضعیت', 'videm-app' ) . '' . esc_html__( 'شناسه', 'videm-app' ) . '' . esc_html__( 'ثبت‌شده / کل', 'videm-app' ) . '' . esc_html__( 'توضیح', 'videm-app' ) . '
#%s' . esc_html( $row['date'] ) . '' . esc_html( $row['customer'] ) . '' . esc_html( $row['status'] ) . '' . esc_html( 'email' === $row['type'] ? __( 'ایمیل', 'videm-app' ) : __( 'موبایل', 'videm-app' ) ) . '' . esc_html( number_format_i18n( $row['done'] ) . ' / ' . number_format_i18n( $row['mapped'] ) ) . '' . esc_html( $row['reason'] ) . '%s
'; } /** Run enrollment again for the requested orders, then return to the report. */ function videm_app_report_retry_handler() { if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_die( esc_html__( 'دسترسی لازم را ندارید.', 'videm-app' ) ); } check_admin_referer( 'videm_app_report_retry' ); $raw = isset( $_REQUEST['order_ids'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['order_ids'] ) ) : ''; $ids = array_values( array_filter( array_map( 'absint', explode( ',', $raw ) ) ) ); $ids = array_slice( array_unique( $ids ), 0, (int) VIDEM_APP_REPORT_MAX_ORDERS ); foreach ( $ids as $id ) { $order = wc_get_order( $id ); if ( $order && videm_app_order_has_courses( $order ) ) { videm_app_enroll_order( $id ); } } $from = videm_app_report_date( 'from', gmdate( 'Y-m-d', strtotime( '-30 days' ) ) ); $to = videm_app_report_date( 'to', current_time( 'Y-m-d' ) ); wp_safe_redirect( videm_app_reports_url( array( 'from' => $from, 'to' => $to, 'videm_retried' => count( $ids ), ) ) ); exit; } add_action( 'admin_post_videm_app_report_retry', 'videm_app_report_retry_handler' ); /** * A small, cached sales/reconciliation snapshot of the last 30 days for the dashboard. * * Shares one transient so opening the dashboard scans the orders at most once per TTL. * * @return array{revenue_30d:float,orders_30d:int,incomplete:int}|null Null without WooCommerce. */ function videm_app_reports_dashboard_snapshot() { if ( ! function_exists( 'wc_get_orders' ) ) { return null; } $cached = get_transient( 'videm_app_reports_snapshot' ); if ( is_array( $cached ) ) { return $cached; } $to = current_time( 'Y-m-d' ); $from = gmdate( 'Y-m-d', strtotime( $to . ' -30 days' ) ); $data = videm_app_collect_report_data( $from, $to ); $snapshot = array( 'revenue_30d' => (float) $data['order_total'], 'orders_30d' => (int) $data['order_count'], 'incomplete' => count( $data['incomplete'] ), ); set_transient( 'videm_app_reports_snapshot', $snapshot, defined( 'VIDEM_APP_DASHBOARD_TTL' ) ? VIDEM_APP_DASHBOARD_TTL : 5 * MINUTE_IN_SECONDS ); return $snapshot; }