generated from liutk/owl-admin-base
135 lines
4.2 KiB
PHP
135 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Admin\Filters\LedgerFilter;
|
|
use App\Models\Ledger;
|
|
use App\Services\StatisticService;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class StatisticsController extends Controller
|
|
{
|
|
/**
|
|
* 首页统计
|
|
*/
|
|
public function dashboard(Request $request, StatisticService $statisticService): array
|
|
{
|
|
$query = Ledger::filter(
|
|
$this->defaultFilterInput($request), LedgerFilter::class
|
|
);
|
|
|
|
// 昨天
|
|
$yesterday = Carbon::yesterday();
|
|
|
|
//--------------------------------------------------------------------------
|
|
// 本月总账录入
|
|
//--------------------------------------------------------------------------
|
|
$currentMonthLedger = (clone $query)
|
|
->select([DB::raw('SUM(sales) as sales'), DB::raw('SUM(expenditure) as expenditure')])
|
|
->whereBetween('date', [$yesterday->copy()->startOfMonth()->format('Y-m-d'), $yesterday->format('Y-m-d')])
|
|
->first();
|
|
|
|
//--------------------------------------------------------------------------
|
|
// 昨日总账录入
|
|
//--------------------------------------------------------------------------
|
|
$yesterdayLedger = (clone $query)->where('date', $yesterday->format('Y-m-d'))->first();
|
|
|
|
return [
|
|
// 本月总账录入
|
|
'current_month_ledger' => [
|
|
// 截止日期
|
|
'deadline' => $yesterday->format('Y-m-d'),
|
|
'sales' => trim_zeros($currentMonthLedger->sales ?? 0),
|
|
'expenditure' => trim_zeros($currentMonthLedger->expenditure ?? 0),
|
|
],
|
|
// 昨日累计金额
|
|
'yesterday_ledger' => [
|
|
'date' => $yesterday->format('Y-m-d'),
|
|
'sales' => trim_zeros($yesterdayLedger->sales ?? 0),
|
|
'expenditure' => trim_zeros($yesterdayLedger->expenditure ?? 0),
|
|
],
|
|
// 近30天趋势数据
|
|
'trend_data_of_30days' => $statisticService->ledgerTrends(
|
|
$yesterday->copy()->subDays(29), $yesterday->copy()
|
|
),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 门店统计
|
|
*/
|
|
public function stores(Request $request, StatisticService $statisticService)
|
|
{
|
|
$request->validate(
|
|
rules: [
|
|
'start_at' => ['bail', 'required', 'date_format:Y-m-d'],
|
|
'end_at' => ['bail', 'required', 'date_format:Y-m-d'],
|
|
],
|
|
attributes: [
|
|
'start_at' => '开始日期',
|
|
'end_at' => '结束日期',
|
|
],
|
|
);
|
|
|
|
$input = array_merge(
|
|
$this->defaultFilterInput($request),
|
|
$request->only(['start_at', 'end_at']),
|
|
);
|
|
|
|
$sorts = [['sales', 'desc'], ['id', 'asc']];
|
|
|
|
return $statisticService->stores($input, $sorts);
|
|
}
|
|
|
|
/**
|
|
* 销售统计
|
|
*/
|
|
public function sales(Request $request, StatisticService $statisticService): array
|
|
{
|
|
$request->validate(
|
|
rules: [
|
|
'start_at' => ['bail', 'required', 'date_format:Y-m-d'],
|
|
'end_at' => ['bail', 'required', 'date_format:Y-m-d'],
|
|
],
|
|
attributes: [
|
|
'start_at' => '开始日期',
|
|
'end_at' => '结束日期',
|
|
],
|
|
);
|
|
|
|
return $statisticService->sales(
|
|
Carbon::parse($request->input('start_at')),
|
|
Carbon::parse($request->input('end_at')),
|
|
$this->defaultFilterInput($request),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 处理区域和门店过滤条件
|
|
*/
|
|
protected function defaultFilterInput(Request $request): array
|
|
{
|
|
$input = [];
|
|
|
|
if ($request->filled('store_id')) {
|
|
$input['store_id'] = $request->input('store_id');
|
|
} else {
|
|
$region = [];
|
|
|
|
if ($request->filled('province_code')) {
|
|
$region['provinceCode'] = $request->input('province_code');
|
|
}
|
|
|
|
if ($request->filled('city_code')) {
|
|
$region['cityCode'] = $request->input('city_code');
|
|
}
|
|
|
|
$input['region'] = $region;
|
|
}
|
|
|
|
return $input;
|
|
}
|
|
}
|