model = new \app\admin\model\shopro\regional_agent\Order; } public function index() { $this->request->filter(['strip_tags']); if ($this->request->isAjax()) { $nobuildfields = ['order_sn', 'agent_nickname', 'area_name', 'createtime']; list($where, $sort, $order, $offset, $limit) = $this->custombuildparams(null, $nobuildfields); $query = $this->buildSearch(); $total = $query->where($where)->count(); $list = $this->buildSearch() ->with(['buyer' => function ($q) { $q->field('id,nickname,mobile,avatar'); }, 'agent', 'agentUser' => function ($q) { $q->field('id,nickname,mobile,avatar'); }, 'order' => function ($q) { $q->field('id,order_sn,status,pay_fee,createtime'); }, 'reward']) ->where($where) ->order($sort, $order) ->limit($offset, $limit) ->select(); $rows = collection($list)->toArray(); foreach ($rows as &$row) { $rowCommission = 0; $rowCommissioned = 0; foreach ($row['reward'] as $reward) { $rowCommission += $reward['commission']; if ($reward['status'] == 1) { $rowCommissioned += $reward['commission']; } } $row['commission_total'] = number_format($rowCommission, 2, '.', ''); $row['commissioned_total'] = number_format($rowCommissioned, 2, '.', ''); } $waitingIds = $this->buildSearch()->where($where)->where('commission_reward_status', 0)->column('id'); $commissionWaiting = 0; if ($waitingIds) { $commissionWaiting = \app\admin\model\shopro\regional_agent\Reward::where('regional_order_id', 'in', $waitingIds)->sum('commission'); } $settledIds = $this->buildSearch()->where($where)->where('commission_reward_status', 1)->column('id'); $commissioned = 0; if ($settledIds) { $commissioned = \app\admin\model\shopro\regional_agent\Reward::where('regional_order_id', 'in', $settledIds)->where('status', 1)->sum('commission'); } return $this->success('区域公司订单', null, [ 'total' => $total, 'rows' => $rows, 'commission_waiting_total' => number_format($commissionWaiting, 2, '.', ''), 'commissioned_total' => number_format($commissioned, 2, '.', ''), ]); } return $this->view->fetch(); } public function export() { $nobuildfields = ['order_sn', 'agent_nickname', 'area_name', 'createtime']; list($where, $sort, $order) = $this->custombuildparams(null, $nobuildfields); $expCellName = [ 'id' => 'ID', 'order_sn' => '订单号', 'area_name' => '公司区域', 'agent_nickname' => '公司用户', 'amount' => '结算金额', 'commission_total' => '差价', 'status_text' => '结算状态', 'createtime' => '创建时间', ]; $list = $this->buildSearch() ->with(['agent', 'agentUser', 'order', 'reward']) ->where($where) ->order($sort, $order) ->limit(5000) ->select(); if (!$list) { $this->error('导出数据为空'); } $exportData = []; foreach ($list as $row) { $commission = 0; foreach ($row['reward'] as $reward) { $commission += $reward['commission']; } $statusMap = [0 => '待结算', 1 => '已结算', -1 => '已退回']; $exportData[] = [ 'id' => $row['id'], 'order_sn' => $row['order']['order_sn'] ?? $row['order_id'], 'area_name' => $row['agent']['area_name'] ?? '', 'agent_nickname' => $row['agent_user']['nickname'] ?? '', 'amount' => $row['amount'], 'commission_total' => number_format($commission, 2, '.', ''), 'status_text' => $statusMap[$row['commission_reward_status']] ?? '-', 'createtime' => date('Y-m-d H:i:s', $row['createtime']), ]; } $export = new Export(); $export->exportExcel('区域公司订单', $expCellName, $exportData); } public function runCommission($id = null) { if (!$id) { $id = $this->request->post('id'); } $row = $this->model->get($id); if (!$row) { $this->error('订单不存在'); } $result = Db::transaction(function () use ($row) { return (new \addons\shopro\library\regional_agent\Reward())->settleByRegionalOrder($row->id, 'admin'); }); $result ? $this->success('结算成功') : $this->error('订单已结算或没有要结算的差价'); } public function backCommission($id = null) { if (!$id) { $id = $this->request->post('id'); } $row = $this->model->get($id); if (!$row) { $this->error('订单不存在'); } $result = Db::transaction(function () use ($row) { return (new \addons\shopro\library\regional_agent\Reward())->backByRegionalOrder($row->id, 'admin'); }); $result ? $this->success('退回成功') : $this->error('订单已退回或未结算'); } protected function buildSearch() { $filter = (array) json_decode($this->request->get('filter', ''), true); $query = $this->model; if (!empty($filter['order_sn'])) { $orderIds = \app\admin\model\shopro\order\Order::where('order_sn', 'like', '%' . $filter['order_sn'] . '%')->column('id'); $query = $query->where('order_id', 'in', $orderIds ?: [0]); } if (!empty($filter['agent_nickname'])) { $userIds = \app\admin\model\shopro\user\User::where('nickname', 'like', '%' . $filter['agent_nickname'] . '%')->column('id'); $query = $query->where('agent_user_id', 'in', $userIds ?: [0]); } if (!empty($filter['area_name'])) { $query = $query->whereExists(function ($q) use ($filter) { $agentTable = (new Agent())->getQuery()->getTable(); $orderTable = (new \app\admin\model\shopro\regional_agent\Order())->getQuery()->getTable(); $q->table($agentTable)->where($agentTable . '.id=' . $orderTable . '.agent_id') ->where('area_name', 'like', '%' . $filter['area_name'] . '%'); }); } if (!empty($filter['createtime'])) { $parts = explode(' - ', $filter['createtime']); if (count($parts) === 2) { $start = strtotime($parts[0]); $end = strtotime($parts[1] . ' 23:59:59'); if ($start && $end) { $query->where('createtime', 'between', [$start, $end]); } } } return $query; } }