CaiWuXiTong/modules/EcommerceFinance/Http/Controllers/StoreReceivingBalanceOverview.php

99 lines
3.3 KiB
PHP

<?php
namespace Modules\EcommerceFinance\Http\Controllers;
use App\Utilities\SidebarPermissions;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\EcommerceFinance\Services\StoreReceivingBalanceOverviewService;
use Throwable;
class StoreReceivingBalanceOverview extends Controller
{
public function index(Request $request, StoreReceivingBalanceOverviewService $service)
{
$this->authorizeStoreReceivingBills();
try {
return response()->json($service->overview(...$this->requestedPeriod($request)));
} catch (Throwable $exception) {
report($exception);
return response()->json([
'message' => '账户余额总览暂时无法读取,请稍后重试。',
], 503);
}
}
public function incomeDetail(Request $request, StoreReceivingBalanceOverviewService $service)
{
$this->authorizeStoreReceivingBills();
try {
return response()->json($service->incomeDetail(...$this->requestedPeriod($request)));
} catch (Throwable $exception) {
report($exception);
return response()->json([
'message' => '收入明细暂时无法读取,请稍后重试。',
], 503);
}
}
public function expenseDetail(Request $request, StoreReceivingBalanceOverviewService $service)
{
$this->authorizeStoreReceivingBills();
try {
return response()->json($service->expenseDetail(...$this->requestedPeriod($request)));
} catch (Throwable $exception) {
report($exception);
return response()->json([
'message' => '支出明细暂时无法读取,请稍后重试。',
], 503);
}
}
public function balanceDetail(Request $request, StoreReceivingBalanceOverviewService $service)
{
$this->authorizeStoreReceivingBills();
try {
return response()->json($service->balanceDetail(...$this->requestedPeriod($request)));
} catch (Throwable $exception) {
report($exception);
return response()->json([
'message' => '期末余额明细暂时无法读取,请稍后重试。',
], 503);
}
}
/** @return array{0:string,1?:string} */
private function requestedPeriod(Request $request): array
{
$validated = $request->validate([
'month' => ['nullable', 'date_format:Y-m'],
'start' => ['nullable', 'date_format:Y-m-d', 'required_with:end'],
'end' => ['nullable', 'date_format:Y-m-d', 'required_with:start', 'after_or_equal:start'],
]);
if (! empty($validated['start']) && ! empty($validated['end'])) {
return [(string) $validated['start'], (string) $validated['end']];
}
return [(string) ($validated['month'] ?? now('Asia/Shanghai')->format('Y-m'))];
}
private function authorizeStoreReceivingBills(): void
{
$section = collect(SidebarPermissions::navigationFor(user()))
->firstWhere('slug', 'billing-transactions');
$page = collect($section['pages'] ?? [])
->firstWhere('slug', 'store-receiving-bills');
abort_if(empty($page), 403, '当前账号没有店铺收款账户账单权限。');
}
}