366 lines
18 KiB
PHP
366 lines
18 KiB
PHP
<?php
|
|
/**
|
|
* Native WP admin: AKSRT_Admin_Pay_After_Sale.
|
|
*
|
|
* Extracted from AKSRT_Admin_Pay_Menus.
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
class AKSRT_Admin_Pay_After_Sale {
|
|
|
|
public const CAPABILITY = 'manage_options';
|
|
public const PAGE_SLUG = 'aksrt_after_sale';
|
|
|
|
private static $instance = null;
|
|
|
|
public static function get_instance(): self {
|
|
if (null === self::$instance) {
|
|
self::$instance = new self();
|
|
}
|
|
return self::$instance;
|
|
}
|
|
|
|
public function init(): void {
|
|
if (!is_admin()) {
|
|
return;
|
|
}
|
|
add_action('admin_post_aksrt_pay_update_after_sale', [$this, 'handle_update_after_sale']);
|
|
add_action('admin_post_aksrt_pay_create_after_sale', [$this, 'handle_create_after_sale']);
|
|
add_action('admin_post_aksrt_pay_after_sale_action', [$this, 'handle_after_sale_action']);
|
|
}
|
|
|
|
public function handle_create_after_sale(): void {
|
|
if (!current_user_can(self::CAPABILITY)) {
|
|
wp_die(esc_html__('权限不足', 'aksrt'));
|
|
}
|
|
$order_number = isset($_POST['order_number']) ? sanitize_text_field((string) wp_unslash($_POST['order_number'])) : '';
|
|
$redirect_page = isset($_POST['redirect_page']) ? sanitize_key((string) wp_unslash($_POST['redirect_page'])) : 'order';
|
|
$nonce_action = $redirect_page === 'after_sale' ? 'aksrt_pay_create_after_sale' : ('aksrt_pay_create_after_sale_' . $order_number);
|
|
check_admin_referer($nonce_action);
|
|
|
|
$ok = false;
|
|
if ($order_number !== '' && class_exists('AKSRT_After_Sale')) {
|
|
$user_id = isset($_POST['user_id']) ? (int) $_POST['user_id'] : 0;
|
|
if ($user_id <= 0 && class_exists('AKSRT_Order_Repository')) {
|
|
$order = AKSRT_Order_Repository::get_instance()->get($order_number);
|
|
$user_id = (int) ($order['user_id'] ?? 0);
|
|
}
|
|
$id = AKSRT_After_Sale::get_instance()->create([
|
|
'order_number' => $order_number,
|
|
'user_id' => $user_id,
|
|
'type' => sanitize_key((string) wp_unslash($_POST['type'] ?? 'refund')),
|
|
'reason' => sanitize_textarea_field((string) wp_unslash($_POST['reason'] ?? '')),
|
|
'amount' => (float) ($_POST['amount'] ?? 0),
|
|
'note' => sanitize_textarea_field((string) wp_unslash($_POST['note'] ?? '')),
|
|
]);
|
|
$ok = $id > 0;
|
|
}
|
|
|
|
$msg = $ok ? 'after_sale_created' : 'after_sale_create_failed';
|
|
if ($redirect_page === 'after_sale') {
|
|
wp_safe_redirect($this->page_url('aksrt_after_sale', ['aksrt_pay_msg' => $msg, 's' => $order_number]));
|
|
exit;
|
|
}
|
|
wp_safe_redirect($this->page_url('aksrt_orders', [
|
|
'order' => $order_number,
|
|
'aksrt_pay_msg' => $msg,
|
|
]));
|
|
exit;
|
|
}
|
|
|
|
public function render_after_sale_page(): void {
|
|
$this->ensure_cap();
|
|
$this->render_header(__('售后管理', 'aksrt'));
|
|
|
|
if (!class_exists('AKSRT_After_Sale')) {
|
|
echo '<div class="notice notice-error"><p>' . esc_html__('售后模块未初始化。', 'aksrt') . '</p></div>';
|
|
$this->render_footer();
|
|
return;
|
|
}
|
|
|
|
$status = isset($_GET['status']) ? sanitize_key((string) wp_unslash($_GET['status'])) : '';
|
|
$type = isset($_GET['type']) ? sanitize_key((string) wp_unslash($_GET['type'])) : '';
|
|
$search = isset($_GET['s']) ? sanitize_text_field((string) wp_unslash($_GET['s'])) : '';
|
|
if ($search === '' && isset($_GET['order_number'])) {
|
|
$search = sanitize_text_field((string) wp_unslash($_GET['order_number']));
|
|
}
|
|
$page_num = max(1, (int) ($_GET['paged'] ?? 1));
|
|
$per_page = 20;
|
|
$svc = AKSRT_After_Sale::get_instance();
|
|
$stats = $svc->get_stats();
|
|
$status_options = $this->after_sale_status_options(true);
|
|
$type_options = $this->after_sale_type_options(true);
|
|
|
|
echo '<p>' . esc_html(sprintf(
|
|
/* translators: 1: total 2: pending */
|
|
__('售后工单共 %1$d · 待处理 %2$d', 'aksrt'),
|
|
(int) ($stats['total'] ?? 0),
|
|
(int) ($stats['pending'] ?? 0)
|
|
)) . '</p>';
|
|
|
|
echo '<h2>' . esc_html__('新建售后', 'aksrt') . '</h2>';
|
|
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '" style="margin:8px 0 20px;display:flex;flex-wrap:wrap;gap:8px;align-items:end;">';
|
|
echo '<input type="hidden" name="action" value="aksrt_pay_create_after_sale" />';
|
|
echo '<input type="hidden" name="redirect_page" value="after_sale" />';
|
|
wp_nonce_field('aksrt_pay_create_after_sale');
|
|
echo '<p><label>' . esc_html__('订单号', 'aksrt') . '<br><input type="text" name="order_number" value="' . esc_attr($search) . '" required class="regular-text" /></label></p>';
|
|
echo '<p><label>' . esc_html__('类型', 'aksrt') . '<br><select name="type">';
|
|
foreach ($this->after_sale_type_options(false) as $k => $label) {
|
|
printf('<option value="%s">%s</option>', esc_attr($k), esc_html($label));
|
|
}
|
|
echo '</select></label></p>';
|
|
echo '<p><label>' . esc_html__('金额', 'aksrt') . '<br><input type="number" step="0.01" min="0" name="amount" value="0" /></label></p>';
|
|
echo '<p><label>' . esc_html__('原因', 'aksrt') . '<br><input type="text" name="reason" required class="regular-text" /></label></p>';
|
|
echo '<p><label>' . esc_html__('备注', 'aksrt') . '<br><input type="text" name="note" /></label></p>';
|
|
submit_button(__('创建售后', 'aksrt'), 'primary', '', false);
|
|
echo '</form>';
|
|
|
|
echo '<form method="get" style="margin:12px 0;">';
|
|
echo '<input type="hidden" name="page" value="aksrt_after_sale" />';
|
|
echo '<select name="status">';
|
|
foreach ($status_options as $key => $label) {
|
|
printf('<option value="%s"%s>%s</option>', esc_attr($key), selected($status, $key, false), esc_html($label));
|
|
}
|
|
echo '</select> ';
|
|
echo '<select name="type">';
|
|
foreach ($type_options as $key => $label) {
|
|
printf('<option value="%s"%s>%s</option>', esc_attr($key), selected($type, $key, false), esc_html($label));
|
|
}
|
|
echo '</select> ';
|
|
echo '<input type="search" name="s" value="' . esc_attr($search) . '" placeholder="' . esc_attr__('订单号', 'aksrt') . '" /> ';
|
|
submit_button(__('筛选', 'aksrt'), 'secondary', '', false);
|
|
echo '</form>';
|
|
|
|
$list_args = [
|
|
'status' => $status,
|
|
'type' => $type,
|
|
'limit' => $per_page,
|
|
'offset' => ($page_num - 1) * $per_page,
|
|
];
|
|
if ($search !== '') {
|
|
$list_args['order_number'] = $search;
|
|
}
|
|
$result = $svc->get_list($list_args);
|
|
$items = $result['items'] ?? [];
|
|
$total = (int) ($result['total'] ?? 0);
|
|
$pages = max(1, (int) ceil(max(1, $total) / $per_page));
|
|
$row_status_options = $this->after_sale_status_options(false);
|
|
|
|
echo '<table class="wp-list-table widefat fixed striped"><thead><tr>';
|
|
echo '<th>ID</th><th>' . esc_html__('订单号', 'aksrt') . '</th><th>' . esc_html__('类型', 'aksrt') . '</th><th>' . esc_html__('用户', 'aksrt') . '</th><th>' . esc_html__('状态', 'aksrt') . '</th><th>' . esc_html__('时间', 'aksrt') . '</th><th>' . esc_html__('操作', 'aksrt') . '</th>';
|
|
echo '</tr></thead><tbody>';
|
|
if (!$items) {
|
|
echo '<tr><td colspan="7">' . esc_html__('暂无售后工单', 'aksrt') . '</td></tr>';
|
|
}
|
|
foreach ($items as $row) {
|
|
$id = (int) ($row['id'] ?? 0);
|
|
$user_id = (int) ($row['user_id'] ?? 0);
|
|
$user = $user_id > 0 ? get_userdata($user_id) : null;
|
|
$st = (string) ($row['status'] ?? '');
|
|
$order_number = (string) ($row['order_number'] ?? '');
|
|
echo '<tr>';
|
|
echo '<td>' . esc_html((string) $id) . '</td>';
|
|
echo '<td><a href="' . esc_url($this->page_url('aksrt_orders', ['order' => $order_number])) . '"><code>' . esc_html($order_number) . '</code></a></td>';
|
|
echo '<td>' . esc_html((string) ($row['type_label'] ?? $type_options[$row['type'] ?? ''] ?? $row['type'] ?? '')) . '</td>';
|
|
echo '<td>' . ($user ? esc_html($user->display_name) : esc_html((string) $user_id)) . '</td>';
|
|
echo '<td>' . esc_html((string) ($row['status_label'] ?? $row_status_options[$st] ?? $st)) . '</td>';
|
|
echo '<td>' . esc_html((string) ($row['created_at'] ?? '')) . '</td>';
|
|
echo '<td style="display:flex;gap:6px;flex-wrap:wrap;align-items:center;">';
|
|
if ($st === 'pending') {
|
|
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '">';
|
|
echo '<input type="hidden" name="action" value="aksrt_pay_after_sale_action" />';
|
|
echo '<input type="hidden" name="ticket_id" value="' . esc_attr((string) $id) . '" />';
|
|
echo '<input type="hidden" name="as_action" value="approve" />';
|
|
wp_nonce_field('aksrt_pay_after_sale_action_' . $id);
|
|
submit_button(__('同意', 'aksrt'), 'small', '', false);
|
|
echo '</form>';
|
|
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '">';
|
|
echo '<input type="hidden" name="action" value="aksrt_pay_after_sale_action" />';
|
|
echo '<input type="hidden" name="ticket_id" value="' . esc_attr((string) $id) . '" />';
|
|
echo '<input type="hidden" name="as_action" value="reject" />';
|
|
wp_nonce_field('aksrt_pay_after_sale_action_' . $id);
|
|
echo '<button type="submit" class="button button-small" onclick="return confirm(\'' . esc_js(__('确认拒绝该售后?', 'aksrt')) . '\');">' . esc_html__('拒绝', 'aksrt') . '</button>';
|
|
echo '</form>';
|
|
}
|
|
if ($st === 'processing') {
|
|
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '">';
|
|
echo '<input type="hidden" name="action" value="aksrt_pay_after_sale_action" />';
|
|
echo '<input type="hidden" name="ticket_id" value="' . esc_attr((string) $id) . '" />';
|
|
echo '<input type="hidden" name="as_action" value="confirm_return" />';
|
|
wp_nonce_field('aksrt_pay_after_sale_action_' . $id);
|
|
submit_button(__('确认收货', 'aksrt'), 'small', '', false);
|
|
echo '</form>';
|
|
}
|
|
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '" style="display:flex;gap:4px;align-items:center;flex-wrap:wrap;">';
|
|
echo '<input type="hidden" name="action" value="aksrt_pay_update_after_sale" />';
|
|
echo '<input type="hidden" name="ticket_id" value="' . esc_attr((string) $id) . '" />';
|
|
wp_nonce_field('aksrt_pay_update_after_sale_' . $id);
|
|
echo '<select name="new_status">';
|
|
foreach ($row_status_options as $key => $label) {
|
|
printf('<option value="%s"%s>%s</option>', esc_attr($key), selected($st, $key, false), esc_html($label));
|
|
}
|
|
echo '</select>';
|
|
echo '<input type="text" name="note" placeholder="' . esc_attr__('备注', 'aksrt') . '" />';
|
|
submit_button(__('更新', 'aksrt'), 'small', '', false);
|
|
echo '</form>';
|
|
echo '</td></tr>';
|
|
}
|
|
echo '</tbody></table>';
|
|
$this->render_pagination($pages, $page_num);
|
|
$this->render_footer();
|
|
}
|
|
|
|
public function handle_update_after_sale(): void {
|
|
if (!current_user_can(self::CAPABILITY)) {
|
|
wp_die(esc_html__('权限不足', 'aksrt'));
|
|
}
|
|
$id = isset($_POST['ticket_id']) ? (int) $_POST['ticket_id'] : 0;
|
|
$new_status = isset($_POST['new_status']) ? sanitize_key((string) wp_unslash($_POST['new_status'])) : '';
|
|
$note = isset($_POST['note']) ? sanitize_textarea_field((string) wp_unslash($_POST['note'])) : '';
|
|
check_admin_referer('aksrt_pay_update_after_sale_' . $id);
|
|
|
|
$ok = false;
|
|
if ($id > 0 && $new_status !== '' && class_exists('AKSRT_After_Sale')) {
|
|
$ok = AKSRT_After_Sale::get_instance()->update_status($id, $new_status, get_current_user_id(), $note);
|
|
}
|
|
wp_safe_redirect($this->page_url('aksrt_after_sale', ['aksrt_pay_msg' => $ok ? 'after_sale_updated' : 'after_sale_failed']));
|
|
exit;
|
|
}
|
|
|
|
public function handle_after_sale_action(): void {
|
|
if (!current_user_can(self::CAPABILITY)) {
|
|
wp_die(esc_html__('权限不足', 'aksrt'));
|
|
}
|
|
$id = isset($_POST['ticket_id']) ? (int) $_POST['ticket_id'] : 0;
|
|
$as_action = isset($_POST['as_action']) ? sanitize_key((string) wp_unslash($_POST['as_action'])) : '';
|
|
check_admin_referer('aksrt_pay_after_sale_action_' . $id);
|
|
|
|
$ok = false;
|
|
if ($id > 0 && class_exists('AKSRT_After_Sale')) {
|
|
$svc = AKSRT_After_Sale::get_instance();
|
|
$operator_id = get_current_user_id();
|
|
if ($as_action === 'approve') {
|
|
$ok = $svc->merchant_approve($id, $operator_id);
|
|
} elseif ($as_action === 'reject') {
|
|
$ok = $svc->merchant_reject($id, $operator_id, __('商家拒绝', 'aksrt'));
|
|
} elseif ($as_action === 'confirm_return') {
|
|
$ok = $svc->merchant_confirm_return($id, $operator_id);
|
|
}
|
|
}
|
|
|
|
wp_safe_redirect($this->page_url('aksrt_after_sale', [
|
|
'aksrt_pay_msg' => $ok ? 'after_sale_action_ok' : 'after_sale_action_failed',
|
|
]));
|
|
exit;
|
|
}
|
|
|
|
|
|
private function after_sale_status_options(bool $include_all = false): array {
|
|
$options = [];
|
|
if ($include_all) {
|
|
$options[''] = __('全部状态', 'aksrt');
|
|
}
|
|
if (class_exists('AKSRT_After_Sale')) {
|
|
foreach (AKSRT_After_Sale::get_status_options() as $item) {
|
|
$value = (string) ($item['value'] ?? '');
|
|
if ($value === '') {
|
|
continue;
|
|
}
|
|
$options[$value] = (string) ($item['label'] ?? $value);
|
|
}
|
|
}
|
|
return $options;
|
|
}
|
|
|
|
/**
|
|
* @return array<string, string>
|
|
*/
|
|
public function after_sale_type_options_public(bool $include_all = false): array {
|
|
return $this->after_sale_type_options($include_all);
|
|
}
|
|
|
|
private function after_sale_type_options(bool $include_all = false): array {
|
|
$options = [];
|
|
if ($include_all) {
|
|
$options[''] = __('全部类型', 'aksrt');
|
|
}
|
|
// Keep operational create options broader than public get_type_options().
|
|
$defaults = [
|
|
'refund' => __('仅退款', 'aksrt'),
|
|
'refund_return' => __('退货退款', 'aksrt'),
|
|
'exchange' => __('换货', 'aksrt'),
|
|
'repair' => __('维修', 'aksrt'),
|
|
];
|
|
if (class_exists('AKSRT_After_Sale')) {
|
|
foreach (AKSRT_After_Sale::get_type_options() as $item) {
|
|
$value = (string) ($item['value'] ?? '');
|
|
if ($value === '') {
|
|
continue;
|
|
}
|
|
$defaults[$value] = (string) ($item['label'] ?? $value);
|
|
}
|
|
}
|
|
return array_merge($options, $defaults);
|
|
}
|
|
|
|
|
|
private function ensure_cap(): void {
|
|
if (!current_user_can(self::CAPABILITY)) {
|
|
wp_die(esc_html__('您没有权限访问此页面。', 'aksrt'));
|
|
}
|
|
}
|
|
|
|
private function render_header(string $title): void {
|
|
echo '<div class="wrap">';
|
|
echo '<h1>' . esc_html($title) . '</h1>';
|
|
$this->render_flash();
|
|
}
|
|
|
|
private function render_footer(): void {
|
|
echo '</div>';
|
|
}
|
|
|
|
private function render_flash(): void {
|
|
if (empty($_GET['aksrt_pay_msg'])) {
|
|
return;
|
|
}
|
|
$code = sanitize_key((string) wp_unslash($_GET['aksrt_pay_msg']));
|
|
$map = [
|
|
'after_sale_updated' => __('售后状态已更新。', 'aksrt'),
|
|
'after_sale_failed' => __('售后状态更新失败。', 'aksrt'),
|
|
'after_sale_created' => __('售后工单已创建。', 'aksrt'),
|
|
'after_sale_create_failed' => __('售后工单创建失败。', 'aksrt'),
|
|
'after_sale_action_ok' => __('售后操作已完成。', 'aksrt'),
|
|
'after_sale_action_failed' => __('售后操作失败。', 'aksrt'),
|
|
];
|
|
if (!isset($map[$code])) {
|
|
return;
|
|
}
|
|
$class = str_contains($code, 'failed') ? 'notice-error' : 'notice-success';
|
|
echo '<div class="notice ' . esc_attr($class) . ' is-dismissible"><p>' . esc_html($map[$code]) . '</p></div>';
|
|
}
|
|
|
|
private function page_url(string $slug, array $args = []): string {
|
|
return add_query_arg(array_merge(['page' => $slug], $args), admin_url('admin.php'));
|
|
}
|
|
|
|
private function render_pagination(int $pages, int $page_num): void {
|
|
if ($pages <= 1) {
|
|
return;
|
|
}
|
|
echo '<div class="tablenav"><div class="tablenav-pages">';
|
|
echo paginate_links([
|
|
'base' => add_query_arg('paged', '%#%'),
|
|
'format' => '',
|
|
'prev_text' => '«',
|
|
'next_text' => '»',
|
|
'total' => $pages,
|
|
'current' => $page_num,
|
|
]);
|
|
echo '</div></div>';
|
|
}
|
|
}
|