generated from liutk/owl-admin-base
Update
parent
4dc454bd8a
commit
18349a9ca5
|
|
@ -2,12 +2,9 @@
|
||||||
|
|
||||||
namespace App\Http\Controllers\Api;
|
namespace App\Http\Controllers\Api;
|
||||||
|
|
||||||
use App\Admin\Filters\LedgerFilter;
|
|
||||||
use App\Models\Ledger;
|
|
||||||
use App\Services\StatisticService;
|
use App\Services\StatisticService;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Carbon;
|
use Illuminate\Support\Carbon;
|
||||||
use Illuminate\Support\Facades\DB;
|
|
||||||
|
|
||||||
class StatisticsController extends Controller
|
class StatisticsController extends Controller
|
||||||
{
|
{
|
||||||
|
|
@ -18,44 +15,43 @@ class StatisticsController extends Controller
|
||||||
{
|
{
|
||||||
$input = $this->defaultFilterInput($request);
|
$input = $this->defaultFilterInput($request);
|
||||||
|
|
||||||
$query = Ledger::filter($input, LedgerFilter::class);
|
|
||||||
|
|
||||||
// 昨天
|
// 昨天
|
||||||
$yesterday = Carbon::yesterday();
|
$yesterday = Carbon::yesterday();
|
||||||
|
|
||||||
//--------------------------------------------------------------------------
|
// 本月上报数据统计
|
||||||
// 本月总账录入
|
$currentMonthLedger = array_merge(
|
||||||
//--------------------------------------------------------------------------
|
['deadline' => $yesterday->format('Y-m-d')],
|
||||||
$currentMonthLedger = (clone $query)
|
$statisticService->ledger(
|
||||||
->select([DB::raw('SUM(sales) as sales'), DB::raw('SUM(expenditure) as expenditure')])
|
$yesterday->copy()->startOfMonth(),
|
||||||
->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(),
|
$yesterday->copy(),
|
||||||
$input,
|
$input,
|
||||||
),
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
// 昨日总账录入
|
||||||
|
$yesterdayLedger = array_merge(
|
||||||
|
['date' => $yesterday->format('Y-m-d')],
|
||||||
|
$statisticService->ledger(
|
||||||
|
$yesterday->copy(),
|
||||||
|
$yesterday->copy(),
|
||||||
|
$input,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
// 近30天趋势数据
|
||||||
|
$trendsOf30days = $statisticService->ledgerTrends(
|
||||||
|
$yesterday->copy()->subDays(29),
|
||||||
|
$yesterday->copy(),
|
||||||
|
$input,
|
||||||
|
);
|
||||||
|
|
||||||
|
return [
|
||||||
|
// 本月总账录入
|
||||||
|
'current_month_ledger' => $currentMonthLedger,
|
||||||
|
// 昨日累计金额
|
||||||
|
'yesterday_ledger' => $yesterdayLedger,
|
||||||
|
// 近30天趋势数据
|
||||||
|
'trends_of_30days' => $trendsOf30days,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -108,6 +104,27 @@ class StatisticsController extends Controller
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function sales2(Request $request, StatisticService $statisticService): array
|
||||||
|
{
|
||||||
|
$request->validate(
|
||||||
|
rules: [
|
||||||
|
'start_at' => ['bail', 'required', 'date_format:Y-m-d'],
|
||||||
|
],
|
||||||
|
attributes: [
|
||||||
|
'start_at' => '开始日期',
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
// yesterday
|
||||||
|
//
|
||||||
|
|
||||||
|
return $statisticService->sales(
|
||||||
|
Carbon::parse($request->input('start_at')),
|
||||||
|
Carbon::parse($request->input('end_at')),
|
||||||
|
$this->defaultFilterInput($request),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理区域和门店过滤条件
|
* 处理区域和门店过滤条件
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,23 @@ use Illuminate\Support\Facades\DB;
|
||||||
class StatisticService
|
class StatisticService
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* 总账趋势
|
* 上报数据统计
|
||||||
|
*/
|
||||||
|
public function ledger(Carbon $start, Carbon $end, array $input = []): array
|
||||||
|
{
|
||||||
|
$ledger = Ledger::filter($input, LedgerFilter::class)
|
||||||
|
->select([DB::raw('SUM(sales) as sales'), DB::raw('SUM(expenditure) as expenditure')])
|
||||||
|
->whereBetween('date', [$start->format('Y-m-d'), $end->format('Y-m-d')])
|
||||||
|
->first();
|
||||||
|
|
||||||
|
return [
|
||||||
|
'sales' => trim_zeros($ledger->sales ?? 0),
|
||||||
|
'expenditure' => trim_zeros($ledger->expenditure ?? 0),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上报数据趋势
|
||||||
*/
|
*/
|
||||||
public function ledgerTrends(Carbon $start, Carbon $end, array $input = []): array
|
public function ledgerTrends(Carbon $start, Carbon $end, array $input = []): array
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue