model = new RewardModel(); } public function index() { if (!$this->request->isAjax()) { $exportConfig = (new \addons\shopro\library\Export())->getConfig(); $this->assignconfig("save_type", $exportConfig['save_type'] ?? 'download'); $this->assignconfig("rewardTypeMap", PartnerConfig::rewardTypeMap()); return $this->view->fetch(); } $showSkip = $this->request->param('show_skip', 0); $query = $this->model->sheepFilter()->with(['buyer', 'user', 'order']); if (!$showSkip) { $query->where(function ($q) { $q->where('amount', '>', 0)->whereOr('madow', '>', 0); }); } $list = $query->paginate($this->request->param('list_rows', 10)); $this->success('获取成功', null, $list); } public function export() { $cellTitles = [ 'id' => 'Id', 'createtime' => '入账时间', 'user_nickname' => '受益人', 'buyer_nickname' => '买家', 'order_id' => '订单', 'reward_type_text' => '奖励类型', 'role_text' => '合伙身份', 'amount' => '结算金额', 'madow' => '马豆', 'base_amount' => '基数', 'status_text' => '状态', 'formula' => '测算摘要', ]; $total = $this->model->sheepFilter()->count(); if ($total <= 0) { $this->error('导出数据为空'); } $export = new \addons\shopro\library\Export(); $params = [ 'file_name' => '合伙人服务收益结算台账', 'cell_titles' => $cellTitles, 'total' => $total, 'is_sub_cell' => false, ]; $totalAmount = 0; $totalMadow = 0; $result = $export->export($params, function ($pages) use (&$totalAmount, &$totalMadow, $total) { $datas = $this->model->sheepFilter()->with(['buyer', 'user', 'order']) ->limit((($pages['page'] - 1) * $pages['list_rows']), $pages['list_rows']) ->select(); $newDatas = []; foreach ($datas as $reward) { $newDatas[] = [ 'id' => $reward['id'], 'createtime' => $reward['createtime'], 'user_nickname' => $reward['user']['nickname'] ?? '-', 'buyer_nickname' => $reward['buyer']['nickname'] ?? '-', 'order_id' => $reward['order']['order_sn'] ?? $reward['order_id'], 'reward_type_text' => $reward['reward_type_text'], 'role_text' => $reward['role_text'] ?? $reward['role'], 'amount' => $reward['amount'], 'madow' => $reward['madow'], 'base_amount' => $reward['base_amount'], 'status_text' => $reward['status_text'], 'formula' => $reward['formula'], ]; } $totalAmount += array_sum(array_column($newDatas, 'amount')); $totalMadow += array_sum(array_column($newDatas, 'madow')); if ($pages['is_last_page']) { $newDatas[] = [ 'id' => '财务合计', 'createtime' => '-', 'user_nickname' => "共 {$total} 笔流水", 'buyer_nickname' => '-', 'order_id' => '-', 'reward_type_text' => '-', 'role_text' => '-', 'amount' => number_format($totalAmount, 2, '.', ''), 'madow' => number_format($totalMadow, 8, '.', ''), 'base_amount' => '-', 'status_text' => '已汇总', 'formula' => "总现金: {$totalAmount}, 总马豆: {$totalMadow}", ]; } return $newDatas; }); $this->success('导出成功', null, $result); } public function reissue() { $params = $this->request->only(['user_id', 'reward_type', 'amount', 'order_id', 'memo']); if (empty($params['user_id']) || empty($params['reward_type']) || empty($params['memo'])) { $this->error('用户、奖项、备注必填'); } $amount = round((float) ($params['amount'] ?? 0), 2); if ($amount == 0) { $this->error('请输入金额'); } if ($amount > 10000) { $this->error('单笔补发金额不能超过 10,000 元(财务风控限制)'); } Db::transaction(function () use ($params, $amount) { $row = PartnerRewardService::write([ 'order_id' => intval($params['order_id'] ?? 0) ?: -time(), 'user_id' => intval($params['user_id']), 'buyer_id' => 0, 'role' => 'system', 'reward_type' => 'manual_' . $params['reward_type'], 'amount' => $amount, 'base_amount' => $amount, 'ext' => ['formula' => '手工补发', 'memo' => $params['memo'], 'manual' => 1], 'status' => 1, ]); Wallet::change($params['user_id'], 'commission', $amount, 'reward_income', [ 'partner_reward_id' => $row->id ?? 0, 'manual' => 1, ], $params['memo']); }); $this->success('补发成功'); } public function back() { $id = $this->request->param('id'); $memo = trim((string) $this->request->param('memo', '')); if ($memo === '') { $this->error('请填写退回原因'); } $row = $this->model->where('id', $id)->find(); if (!$row) { $this->error('记录不存在'); } if ($row->status != 1 || $row->settle_date) { $this->error('仅已入账的即时奖可退回'); } Db::transaction(function () use ($row, $memo) { if ((float) $row->amount > 0 && $row->user_id > 0) { Wallet::change($row->user_id, 'commission', -$row->amount, 'reward_back', [ 'partner_reward_id' => $row->id, ], $memo); } $ext = is_array($row->ext) ? $row->ext : (json_decode($row->ext ?: '{}', true) ?: []); $ext['back_memo'] = $memo; $row->ext = $ext; $row->status = -2; $row->save(); }); $this->success('已退回'); } public function backOrder() { $orderId = intval($this->request->param('order_id')); if ($orderId <= 0) { $this->error('请选择订单'); } (new PartnerRewardService())->refund($orderId); $this->success('整单已退回'); } /** * 订单整单利益分配透视(财务与客服一键对账) */ public function orderTree() { $orderId = intval($this->request->param('order_id')); if ($orderId <= 0) { $this->error('订单ID不合法'); } $order = \app\admin\model\shopro\order\Order::with(['user'])->where('id', $orderId)->find(); if (!$order) { $this->error('订单不存在'); } $rewards = $this->model->with(['user', 'buyer']) ->where('order_id', $orderId) ->order('id', 'asc') ->select(); $rows = []; $totalCash = 0; $totalMadow = 0; foreach ($rewards as $r) { $rows[] = [ 'id' => $r->id, 'reward_type' => $r->reward_type, 'reward_type_text' => $r->reward_type_text, 'user_id' => $r->user_id, 'user_nickname' => $r->user ? ($r->user->nickname ?: $r->user->mobile) : '平台/买家', 'role_text' => $r->role_text ?: $r->role, 'amount' => $r->amount, 'madow' => $r->madow, 'base_amount' => $r->base_amount, 'status' => $r->status, 'status_text' => $r->status_text, 'formula' => $r->formula, 'createtime' => $r->createtime, ]; if ($r->status == 1) { if (in_array($r->reward_type, PartnerConfig::instantTypes())) { $totalCash = bcadd((string)$totalCash, (string)$r->amount, 2); } $totalMadow = bcadd((string)$totalMadow, (string)$r->madow, 8); } } $voucherPlan = \app\admin\model\shopro\partner\VoucherPlan::where('order_id', $orderId)->find(); $payFee = floatval($order->pay_fee); $payoutRatio = $payFee > 0 ? round(($totalCash / $payFee) * 100, 2) : 0; $this->success('获取成功', null, [ 'order' => [ 'id' => $order->id, 'order_sn' => $order->order_sn, 'pay_fee' => $order->pay_fee, 'status_text' => $order->status_text, 'paid_time' => $order->paid_time ? date('Y-m-d H:i:s', $order->paid_time) : '-', 'buyer_nickname' => $order->user ? ($order->user->nickname ?: $order->user->mobile) : '', ], 'rewards' => $rows, 'summary' => [ 'total_cash' => number_format($totalCash, 2, '.', ''), 'total_madow' => number_format($totalMadow, 8, '.', ''), 'payout_ratio' => $payoutRatio . '%', ], 'voucher_plan' => $voucherPlan ? [ 'total_amount' => $voucherPlan->total_amount, 'released_amount' => $voucherPlan->released_amount, 'released_days' => $voucherPlan->released_days, 'status_text' => $voucherPlan->status_text, ] : null, ]); } }