diff --git a/app/Http/Controllers/Api/StatisticsController.php b/app/Http/Controllers/Api/StatisticsController.php index 4b07090..5df0369 100644 --- a/app/Http/Controllers/Api/StatisticsController.php +++ b/app/Http/Controllers/Api/StatisticsController.php @@ -55,6 +55,55 @@ class StatisticsController extends Controller ]; } + /** + * 总账统计 + */ + public function ledger(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'], + 'before_start_at' => ['bail', 'required', 'date_format:Y-m-d'], + 'before_end_at' => ['bail', 'required', 'date_format:Y-m-d'], + ], + attributes: [ + 'start_at' => '开始日期', + 'end_at' => '结束日期', + 'before_start_at' => '对比开始日期', + 'before_end_at' => '对比结束日期', + ], + ); + + $input = $this->defaultFilterInput($request); + + $ledger = $statisticService->ledger( + Carbon::parse($request->input('start_at')), + Carbon::parse($request->input('end_at')), + $input, + ); + + $beforeLedger = $statisticService->ledger( + Carbon::parse($request->input('before_start_at')), + Carbon::parse($request->input('before_end_at')), + $input, + ); + + // 销售涨幅 + $salesGrowthRate = 0; + + if (bccomp($beforeLedger['sales'], '0', 2) === 0) { + $salesGrowthRate = '-'; + } else { + $diff = bcsub($ledger['sales'], $beforeLedger['sales'], 2); + $salesGrowthRate = bcdiv(bcmul($diff, '100'), $beforeLedger['sales'], 2); + } + + return array_merge($ledger, [ + 'sales_growth_rate' => $salesGrowthRate, + ]); + } + /** * 门店统计 */ @@ -104,27 +153,6 @@ 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), - ); - } - /** * 处理区域和门店过滤条件 */ diff --git a/app/Services/StatisticService.php b/app/Services/StatisticService.php index 16f04bb..abb310c 100644 --- a/app/Services/StatisticService.php +++ b/app/Services/StatisticService.php @@ -16,7 +16,7 @@ use Illuminate\Support\Facades\DB; class StatisticService { /** - * 上报数据统计 + * 总账统计 */ public function ledger(Carbon $start, Carbon $end, array $input = []): array { @@ -32,7 +32,7 @@ class StatisticService } /** - * 上报数据趋势 + * 总账数据趋势 */ public function ledgerTrends(Carbon $start, Carbon $end, array $input = []): array { diff --git a/routes/api.php b/routes/api.php index 62764cf..5718540 100644 --- a/routes/api.php +++ b/routes/api.php @@ -41,6 +41,8 @@ Route::group([ Route::get('/statistics/stores', [StatisticsController::class, 'stores']); // 统计数据 - 销售统计 Route::get('/statistics/sales', [StatisticsController::class, 'sales']); + // 统计数据 - 总账统计 + Route::get('/statistics/ledger', [StatisticsController::class, 'ledger']); // 数据上报 Route::apiResource('/ledgers', LedgerController::class)->only(['store', 'show']);