generated from liutk/owl-admin-base
119 lines
4.0 KiB
PHP
119 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Admin\Filters\LedgerFilter;
|
|
use App\Models\Employee;
|
|
use App\Models\Ledger;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class StatsController extends Controller
|
|
{
|
|
public function dashboard(Request $request)
|
|
{
|
|
/** @var \App\Models\Employee */
|
|
$user = $request->user();
|
|
|
|
$query = Ledger::filter(
|
|
$this->defaultInput($user, $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();
|
|
|
|
//--------------------------------------------------------------------------
|
|
// 近 30 天趋势数据
|
|
//--------------------------------------------------------------------------
|
|
$start = $yesterday->copy()->subDays(29);
|
|
$end = $yesterday->copy();
|
|
/** @var \Illuminate\Database\Eloquent\Collection */
|
|
$ledgers30days = (clone $query)
|
|
->whereBetween('date', [$start, $end])
|
|
->get(['date', 'sales', 'expenditure'])
|
|
->keyBy('date');
|
|
|
|
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' => $this->prepareTrendData($start->copy(), $end->copy(), $ledgers30days),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 准备趋势数据
|
|
*/
|
|
protected function prepareTrendData(Carbon $start, Carbon $end, Collection $ledgers): array
|
|
{
|
|
$data = collect();
|
|
|
|
do {
|
|
$ledger = $ledgers->get(
|
|
$date = $start->format('Y-m-d')
|
|
);
|
|
|
|
$data->push([
|
|
'date' => $date,
|
|
'sales' => trim_zeros($ledger->sales ?? 0),
|
|
'expenditure' => trim_zeros($ledger->expenditure ?? 0),
|
|
]);
|
|
|
|
$start->addDay();
|
|
} while ($start->lte($end));
|
|
|
|
return $data->all();
|
|
}
|
|
|
|
protected function defaultInput(Employee $employee, Request $request): array
|
|
{
|
|
$input = [];
|
|
|
|
if (! $employee->isAdministrator()) {
|
|
$input['store_id'] = $employee->store_id;
|
|
} elseif ($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('cityCode')) {
|
|
$region['city_code'] = $request->input('city_code');
|
|
}
|
|
|
|
$input['region'] = $region;
|
|
}
|
|
|
|
return $input;
|
|
}
|
|
}
|