77 lines
3.0 KiB
PHP
77 lines
3.0 KiB
PHP
<?php
|
||
|
||
namespace Modules\EcommerceFinance\Http\Controllers;
|
||
|
||
use App\Utilities\SidebarPermissions;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Routing\Controller;
|
||
use Modules\EcommerceFinance\Models\AlipayBill;
|
||
use Modules\EcommerceFinance\Repositories\StoreReceivingAlipayBillMirrorRepository;
|
||
use RuntimeException;
|
||
|
||
class AlipayBills extends Controller
|
||
{
|
||
/**
|
||
* 淘系支付宝账户账单列表(店铺收款账户账单 › 淘系支付宝账户)。
|
||
* 经由 admin 路由组(web 会话)暴露,前端带 CSRF 调用即可。
|
||
*/
|
||
public function index(Request $request, StoreReceivingAlipayBillMirrorRepository $repository)
|
||
{
|
||
$this->authorizeStoreReceivingBills();
|
||
|
||
$sortable = ['occurred_at', 'income', 'expense', 'balance', 'record_code'];
|
||
$sort = in_array($request->get('sort'), $sortable, true) ? $request->get('sort') : 'occurred_at';
|
||
$dir = $request->get('dir') === 'asc' ? 'asc' : 'desc';
|
||
$filters = $request->validate([
|
||
'store_name' => ['nullable', 'string', 'max:255'],
|
||
'alipay_account' => ['nullable', 'string', 'max:128'],
|
||
'bill_item' => ['nullable', 'string', 'max:255'],
|
||
'business_type' => ['nullable', 'string', 'max:255'],
|
||
'start' => ['nullable', 'date_format:Y-m-d'],
|
||
'end' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start'],
|
||
'keyword' => ['nullable', 'string', 'max:255'],
|
||
]);
|
||
|
||
try {
|
||
return response()->json($repository->paginate(
|
||
array_map(static fn (mixed $value): string => trim((string) $value), $filters),
|
||
$sort,
|
||
$dir,
|
||
max((int) $request->get('page', 1), 1),
|
||
min(max((int) $request->get('limit', 10), 1), 100),
|
||
));
|
||
} catch (RuntimeException $exception) {
|
||
if ($exception->getMessage() !== 'store_receiving_alipay_bill_mirror_not_ready') {
|
||
throw $exception;
|
||
}
|
||
|
||
return response()->json([
|
||
'message' => '淘系支付宝账单共享业务镜像正在首次同步,完成后将自动显示。',
|
||
'data' => [],
|
||
'total' => 0,
|
||
'current_page' => 1,
|
||
'last_page' => 1,
|
||
'per_page' => min(max((int) $request->get('limit', 10), 1), 100),
|
||
'summary' => ['income' => 0, 'expense' => 0, 'balance' => 0, 'count' => 0],
|
||
], 503);
|
||
}
|
||
}
|
||
|
||
public function show(AlipayBill $bill)
|
||
{
|
||
$this->authorizeStoreReceivingBills();
|
||
|
||
return response()->json($bill);
|
||
}
|
||
|
||
protected 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, '当前账号没有店铺收款账户账单权限。');
|
||
}
|
||
}
|