diff --git a/application/admin/controller/shopro/order/Order.php b/application/admin/controller/shopro/order/Order.php index 8f43364..ad3f76a 100644 --- a/application/admin/controller/shopro/order/Order.php +++ b/application/admin/controller/shopro/order/Order.php @@ -145,6 +145,7 @@ class Order extends Common public function detail($id) { if (!$this->request->isAjax()) { + $this->assignconfig('order_id', $id); return $this->view->fetch(); } @@ -177,10 +178,15 @@ class Order extends Common $item->suggest_refund_fee = $item->suggest_refund_fee; } - // 处理未支付订单 item status_code + // 处理未支付订单 item status_code(此方法会将 $order 转为数组) $order = $order->setOrderItemStatusByOrder($order); - $order->partner_split = $this->buildPartnerSplit($order); + $partnerSplit = $this->buildPartnerSplit($order); + if (is_array($order)) { + $order['partner_split'] = $partnerSplit; + } else { + $order->partner_split = $partnerSplit; + } $this->success('获取成功', null, $order); } @@ -190,128 +196,237 @@ class Order extends Common */ protected function buildPartnerSplit($order) { - $orderId = intval($order->id ?? 0); - $shareTypes = ['venue_share', 'county_share', 'madow_share']; - $items = []; - $totalCash = 0; - - $rows = \app\admin\model\shopro\partner\Reward::with(['user']) - ->where('order_id', $orderId) - ->order('id', 'asc') - ->select(); - - foreach ($rows as $row) { - $type = (string) ($row->reward_type ?? ''); - if (in_array($type, $shareTypes, true)) { - continue; + try { + $orderId = intval(is_array($order) ? ($order['id'] ?? 0) : ($order->id ?? 0)); + if ($orderId <= 0) { + return [ + 'items' => [], + 'total_cash' => '0.00', + 'total_voucher' => '0.00', + 'settle_date' => null, + 'has_data' => false, + ]; } - $amount = round((float) $row->amount, 2); - $madow = (float) ($row->madow ?? 0); - if (intval($row->status) === 1 && $amount > 0) { - $totalCash = round($totalCash + $amount, 2); - } - $items[] = [ - 'display' => 'user', - 'id' => intval($row->id), - 'reward_type' => $type, - 'reward_type_text' => $row->reward_type_text, - 'role_text' => $row->role_text ?? '', - 'user_id' => intval($row->user_id), - 'user' => $row->user, - 'amount' => number_format($amount, 2, '.', ''), - 'madow' => \addons\shopro\service\partner\IdentityService::formatMadow($madow), - 'status' => intval($row->status), - 'status_text' => $row->status_text, - 'formula' => $row->formula, - 'skip_reason_text' => $row->skip_reason_text, - ]; - } - $settleDate = null; - $paidRaw = $order->getData('paid_time') ?? ($order['paid_time'] ?? null); - if ($paidRaw) { - $paidTs = is_numeric($paidRaw) ? intval($paidRaw) : strtotime((string) $paidRaw); - if ($paidTs > 0) { - $settleDate = date('Y-m-d', $paidTs); - } - } + $shareTypes = ['venue_share', 'county_share', 'madow_share']; + $items = []; + $totalCash = 0; + $totalVoucher = 0; - if ($settleDate) { - foreach ($shareTypes as $type) { - $shareRows = \app\admin\model\shopro\partner\Reward::where('settle_date', $settleDate) - ->where('reward_type', $type) - ->where('status', 1) - ->where('user_id', '>', 0) - ->select(); - $userCount = count($shareRows); - if ($userCount <= 0) { - // 分母为 0 进平台的记录 - $platform = \app\admin\model\shopro\partner\Reward::where('settle_date', $settleDate) - ->where('reward_type', $type) - ->where('status', 1) - ->where('user_id', 0) - ->find(); - if (!$platform) { - continue; - } - $items[] = [ - 'display' => 'share', - 'reward_type' => $type, - 'reward_type_text' => \addons\shopro\service\partner\PartnerConfig::rewardTypeText($type), - 'user_count' => 0, - 'each_amount' => '0.00', - 'each_madow' => '0.00000000', - 'total_amount' => number_format((float) $platform->amount, 2, '.', ''), - 'total_madow' => \addons\shopro\service\partner\IdentityService::formatMadow($platform->madow ?? 0), - 'settle_date' => $settleDate, - 'status_text' => '已入账', - 'unit' => $type === 'madow_share' ? '马豆' : '元', - 'formula' => $platform->formula, - 'tip' => '付款日日结均分:当日无人参与,奖池进平台', - ]; + // 1. 查询合伙股东即时收益流水 + $rows = \app\admin\model\shopro\partner\Reward::with(['user']) + ->where('order_id', $orderId) + ->order('id', 'asc') + ->select(); + + foreach ($rows as $row) { + $type = (string) ($row->reward_type ?? ''); + if (in_array($type, $shareTypes, true)) { continue; } + $amount = round((float) ($row->amount ?? 0), 2); + $baseAmount = round((float) ($row->base_amount ?? 0), 2); + $madow = (float) ($row->madow ?? 0); + $status = intval($row->status ?? 0); - $totalAmount = 0; - $totalMadow = 0; - foreach ($shareRows as $sr) { - $totalAmount = round($totalAmount + (float) $sr->amount, 2); - $totalMadow = function_exists('bcadd') - ? (float) bcadd((string) $totalMadow, (string) ($sr->madow ?? 0), 8) - : ($totalMadow + (float) ($sr->madow ?? 0)); + if ($status === 1 && $amount > 0) { + $totalCash = round($totalCash + $amount, 2); } - $first = $shareRows[0]; - $eachAmount = number_format((float) $first->amount, 2, '.', ''); - $eachMadow = \addons\shopro\service\partner\IdentityService::formatMadow($first->madow ?? 0); - $ext = is_array($first->ext) ? $first->ext : []; - if (!empty($ext['share_user_count'])) { - $userCount = intval($ext['share_user_count']); + if ($type === 'voucher_grant' && $status === 1 && $baseAmount > 0) { + $totalVoucher = round($totalVoucher + $baseAmount, 2); + } + + // 友好展示金额 + if ($type === 'voucher_grant') { + $amountText = number_format($baseAmount, 2, '.', '') . ' 兑换券'; + } elseif ($madow > 0) { + $amountText = \addons\shopro\service\partner\IdentityService::formatMadow($madow) . ' 马豆'; + } elseif ($amount > 0) { + $amountText = '¥' . number_format($amount, 2, '.', ''); + } else { + $amountText = '¥0.00'; } $items[] = [ - 'display' => 'share', + 'display' => 'user', + 'id' => intval($row->id), 'reward_type' => $type, - 'reward_type_text' => \addons\shopro\service\partner\PartnerConfig::rewardTypeText($type), - 'user_count' => $userCount, - 'each_amount' => $eachAmount, - 'each_madow' => $eachMadow, - 'total_amount' => number_format($totalAmount, 2, '.', ''), - 'total_madow' => \addons\shopro\service\partner\IdentityService::formatMadow($totalMadow), - 'settle_date' => $settleDate, - 'status_text' => '已入账', - 'unit' => $type === 'madow_share' ? '马豆' : '元', - 'formula' => $first->formula, - 'tip' => '付款日日结均分(不列出参与用户名单)', + 'reward_type_text' => (string) ($row->reward_type_text ?: \addons\shopro\service\partner\PartnerConfig::rewardTypeText($type)), + 'role' => (string) ($row->role ?? ''), + 'role_text' => (string) ($row->role_text ?? ''), + 'user_id' => intval($row->user_id), + 'user' => $row->user ? (is_object($row->user) ? $row->user->toArray() : $row->user) : null, + 'amount' => number_format($amount, 2, '.', ''), + 'base_amount' => number_format($baseAmount, 2, '.', ''), + 'amount_text' => $amountText, + 'madow' => \addons\shopro\service\partner\IdentityService::formatMadow($madow), + 'status' => $status, + 'status_text' => (string) ($row->status_text ?: ( + $status === 1 ? '已入账' : ($status === 0 ? '待入账' : ($status === -1 ? '已取消' : '已退回')) + )), + 'formula' => (string) ($row->formula ?? ''), + 'skip_reason_text' => (string) ($row->skip_reason_text ?? ''), ]; } - } - return [ - 'items' => $items, - 'total_cash' => number_format($totalCash, 2, '.', ''), - 'settle_date' => $settleDate, - 'has_data' => count($items) > 0, - ]; + // 2. 兼容传统分销佣金记录(如存在) + try { + $commRows = \app\admin\model\shopro\commission\Reward::with(['agent.user']) + ->where('order_id', $orderId) + ->order('id', 'asc') + ->select(); + if ($commRows) { + foreach ($commRows as $crow) { + $cCommission = round((float) ($crow->commission ?? 0), 2); + $cStatus = intval($crow->status ?? 0); + if ($cStatus === 1 && $cCommission > 0) { + $totalCash = round($totalCash + $cCommission, 2); + } + $items[] = [ + 'display' => 'user', + 'id' => intval($crow->id), + 'reward_type' => 'commission', + 'reward_type_text' => '分销佣金', + 'role' => 'commission', + 'role_text' => '分销商', + 'user_id' => intval($crow->agent ? ($crow->agent->user_id ?? 0) : 0), + 'user' => ($crow->agent && $crow->agent->user) ? (is_object($crow->agent->user) ? $crow->agent->user->toArray() : $crow->agent->user) : null, + 'amount' => number_format($cCommission, 2, '.', ''), + 'base_amount' => '0.00', + 'amount_text' => '¥' . number_format($cCommission, 2, '.', ''), + 'madow' => '0.00000000', + 'status' => $cStatus, + 'status_text' => (string) ($crow->status_text ?: ($cStatus === 1 ? '已入账' : '待入账')), + 'formula' => '分销规则佣金', + 'skip_reason_text' => '', + ]; + } + } + } catch (\Throwable $ce) { + // 忽略非核心分销查询异常 + } + + // 3. 统计支付日日结均分奖池概况 + $settleDate = null; + $paidRaw = is_array($order) + ? ($order['paid_time'] ?? null) + : ($order->getData('paid_time') ?? ($order['paid_time'] ?? null)); + if ($paidRaw) { + $paidTs = is_numeric($paidRaw) ? intval($paidRaw) : strtotime((string) $paidRaw); + if ($paidTs > 0) { + $settleDate = date('Y-m-d', $paidTs); + } + } + + if ($settleDate) { + foreach ($shareTypes as $type) { + $shareRows = \app\admin\model\shopro\partner\Reward::where('settle_date', $settleDate) + ->where('reward_type', $type) + ->where('status', 1) + ->where('user_id', '>', 0) + ->select(); + $userCount = count($shareRows); + if ($userCount <= 0) { + // 分母为 0 进平台的记录 + $platform = \app\admin\model\shopro\partner\Reward::where('settle_date', $settleDate) + ->where('reward_type', $type) + ->where('status', 1) + ->where('user_id', 0) + ->find(); + if (!$platform) { + continue; + } + $platAmount = (float) ($platform->amount ?? 0); + $platMadow = (float) ($platform->madow ?? 0); + $amountText = $type === 'madow_share' + ? \addons\shopro\service\partner\IdentityService::formatMadow($platMadow) . ' 马豆' + : '¥' . number_format($platAmount, 2, '.', ''); + + $items[] = [ + 'display' => 'share', + 'id' => intval($platform->id ?? 0), + 'reward_type' => $type, + 'reward_type_text' => \addons\shopro\service\partner\PartnerConfig::rewardTypeText($type), + 'role' => 'system', + 'role_text' => '平台', + 'user_id' => 0, + 'user' => null, + 'user_count' => 0, + 'each_amount' => '0.00', + 'each_madow' => '0.00000000', + 'total_amount' => number_format($platAmount, 2, '.', ''), + 'total_madow' => \addons\shopro\service\partner\IdentityService::formatMadow($platMadow), + 'amount_text' => $amountText, + 'settle_date' => $settleDate, + 'status' => 1, + 'status_text' => '已入账', + 'unit' => $type === 'madow_share' ? '马豆' : '元', + 'formula' => (string) ($platform->formula ?? ''), + 'skip_reason_text' => '', + 'tip' => '付款日日结均分:当日无人参与,奖池进平台', + ]; + continue; + } + + $totalAmount = 0; + $totalMadow = 0; + foreach ($shareRows as $sr) { + $totalAmount = round($totalAmount + (float) ($sr->amount ?? 0), 2); + $totalMadow = function_exists('bcadd') + ? (float) bcadd((string) $totalMadow, (string) ($sr->madow ?? 0), 8) + : ($totalMadow + (float) ($sr->madow ?? 0)); + } + $first = $shareRows[0]; + $eachAmount = number_format((float) ($first->amount ?? 0), 2, '.', ''); + $eachMadow = \addons\shopro\service\partner\IdentityService::formatMadow($first->madow ?? 0); + $ext = is_array($first->ext) ? $first->ext : []; + if (!empty($ext['share_user_count'])) { + $userCount = intval($ext['share_user_count']); + } + + $items[] = [ + 'display' => 'share', + 'id' => intval($first->id ?? 0), + 'reward_type' => $type, + 'reward_type_text' => \addons\shopro\service\partner\PartnerConfig::rewardTypeText($type), + 'role' => '', + 'role_text' => '', + 'user_id' => 0, + 'user' => null, + 'user_count' => $userCount, + 'each_amount' => $eachAmount, + 'each_madow' => $eachMadow, + 'total_amount' => number_format($totalAmount, 2, '.', ''), + 'total_madow' => \addons\shopro\service\partner\IdentityService::formatMadow($totalMadow), + 'amount_text' => $type === 'madow_share' ? ($eachMadow . ' 马豆/人') : ('¥' . $eachAmount . '/人'), + 'settle_date' => $settleDate, + 'status' => 1, + 'status_text' => '已入账', + 'unit' => $type === 'madow_share' ? '马豆' : '元', + 'formula' => (string) ($first->formula ?? ''), + 'skip_reason_text' => '', + 'tip' => '付款日日结均分(不列出参与用户名单)', + ]; + } + } + + return [ + 'items' => $items, + 'total_cash' => number_format($totalCash, 2, '.', ''), + 'total_voucher' => number_format($totalVoucher, 2, '.', ''), + 'settle_date' => $settleDate, + 'has_data' => count($items) > 0, + ]; + } catch (\Throwable $e) { + \think\Log::error('buildPartnerSplit error: ' . $e->getMessage()); + return [ + 'items' => [], + 'total_cash' => '0.00', + 'total_voucher' => '0.00', + 'settle_date' => null, + 'has_data' => false, + ]; + } } diff --git a/application/admin/view/shopro/order/order/detail.html b/application/admin/view/shopro/order/order/detail.html index b1e43c6..f567298 100644 --- a/application/admin/view/shopro/order/order/detail.html +++ b/application/admin/view/shopro/order/order/detail.html @@ -1,6 +1,40 @@ {include file="/shopro/common/script" /}