model = new DailyReport(); } public function index() { if (!$this->request->isAjax()) { return $this->view->fetch(); } $list = $this->model->sheepFilter()->order('settle_date', 'desc')->paginate($this->request->param('list_rows', 10)); $today = date('Y-m-d'); $yesterday = date('Y-m-d', strtotime('-1 day')); $days = \addons\shopro\service\partner\PartnerConfig::activeDays(); $since = time() - $days * 86400; $result = $list->toArray(); $cashTypes = \addons\shopro\service\partner\PartnerConfig::instantTypes(); $cashTypes = array_merge($cashTypes, ['venue_share', 'county_share']); $sumCash = function ($date) use ($cashTypes) { return (float) \app\admin\model\shopro\partner\Reward::where('settle_date', $date) ->where('status', 1) ->whereIn('reward_type', $cashTypes) ->sum('amount'); }; $todayCash = $sumCash($today); // 即时奖无 settle_date,按入账日合计 $todayStart = strtotime($today . ' 00:00:00'); $todayEnd = $todayStart + 86400 - 1; $yStart = strtotime($yesterday . ' 00:00:00'); $yEnd = $yStart + 86400 - 1; $instantToday = (float) \app\admin\model\shopro\partner\Reward::where('status', 1) ->whereNull('settle_date') ->whereBetween('createtime', [$todayStart, $todayEnd]) ->whereIn('reward_type', \addons\shopro\service\partner\PartnerConfig::instantTypes()) ->sum('amount'); $instantYesterday = (float) \app\admin\model\shopro\partner\Reward::where('status', 1) ->whereNull('settle_date') ->whereBetween('createtime', [$yStart, $yEnd]) ->whereIn('reward_type', \addons\shopro\service\partner\PartnerConfig::instantTypes()) ->sum('amount'); $yesterdayReport = DailyReport::where('settle_date', $yesterday)->find(); $yesterdayGmv = $yesterdayReport ? (float) $yesterdayReport->gmv : 0; $yesterdayTotalCash = $sumCash($yesterday) + $instantYesterday; $yesterdayPayoutRatio = $yesterdayGmv > 0 ? round(($yesterdayTotalCash / $yesterdayGmv) * 100, 2) : 0; $voucherTotal = (float) \app\admin\model\shopro\partner\VoucherPlan::where('status', 1)->sum('total_amount'); $voucherReleased = (float) \app\admin\model\shopro\partner\VoucherPlan::where('status', 1)->sum('released_amount'); $voucherDebt = max(0, $voucherTotal - $voucherReleased); $activeService = new \addons\shopro\service\partner\ActiveService(); $venueActiveCount = count($activeService->activeUserIds($since, time(), ['is_venue' => 1])); $countyActiveCount = count($activeService->activeUserIds($since, time(), ['is_county' => 1])); $result['kpi'] = [ 'today' => DailyReport::where('settle_date', $today)->find(), 'yesterday' => $yesterdayReport, 'today_cash' => number_format($todayCash + $instantToday, 2, '.', ''), 'yesterday_cash' => number_format($yesterdayTotalCash, 2, '.', ''), 'yesterday_payout_ratio' => $yesterdayPayoutRatio . '%', 'yesterday_payout_ratio_val' => $yesterdayPayoutRatio, 'voucher_debt' => number_format($voucherDebt, 2, '.', ''), 'venue_active_count' => $venueActiveCount, 'county_active_count' => $countyActiveCount, 'inactive_count' => \app\admin\model\shopro\commission\Agent::where(function ($q) use ($since) { $q->whereNull('last_self_order_time')->whereOr('last_self_order_time', '<', $since); })->count(), ]; $this->success('获取成功', null, $result); } public function preview() { $date = $this->request->param('settle_date', date('Y-m-d', strtotime('-1 day'))); $data = (new DailySettleService())->preview($date); $this->success('试算成功', null, $data); } public function rerun() { $date = $this->request->param('settle_date'); if (!$date) { $this->error('请选择日期'); } $report = (new DailySettleService())->run($date, true); $this->success('重跑完成', null, $report); } }