136 lines
5.6 KiB
PHP
136 lines
5.6 KiB
PHP
<?php
|
||
|
||
namespace Modules\EcommerceFinance\Http\Controllers\Api;
|
||
|
||
use App\Abstracts\Http\ApiController;
|
||
use App\Utilities\SidebarPermissions;
|
||
use Illuminate\Http\JsonResponse;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Validation\ValidationException;
|
||
use Modules\EcommerceFinance\Http\Controllers\AccountBills;
|
||
use Modules\EcommerceFinance\Http\Controllers\AlipayBills;
|
||
use Modules\EcommerceFinance\Http\Controllers\DewuAccountBills;
|
||
use Modules\EcommerceFinance\Http\Controllers\PinduoduoAccountBills;
|
||
use Modules\EcommerceFinance\Http\Controllers\TaobaoWechatAccountBills;
|
||
use Symfony\Component\HttpFoundation\Response;
|
||
|
||
/**
|
||
* 店铺收款账户账单的只读外部接口。
|
||
*
|
||
* 各账户页签继续调用页面现有控制器,确保分页、汇总、余额和总账备注口径一致。
|
||
*/
|
||
class StoreReceivingBills extends ApiController
|
||
{
|
||
/** @var array<string,array{title:string,controller:class-string}> */
|
||
private const ACCOUNTS = [
|
||
'taobao-alipay' => ['title' => '全部支付宝账户', 'controller' => AlipayBills::class],
|
||
'taobao-wechat' => ['title' => '淘系微信账户', 'controller' => TaobaoWechatAccountBills::class],
|
||
'pinduoduo' => ['title' => '拼多多账户', 'controller' => PinduoduoAccountBills::class],
|
||
'jd-wallet' => ['title' => '京东网银钱包账户', 'controller' => AccountBills::class],
|
||
'jd-transactions' => ['title' => '京东账单', 'controller' => AccountBills::class],
|
||
'jingxi' => ['title' => '京喜账单', 'controller' => AccountBills::class],
|
||
'douyin' => ['title' => '抖音账户', 'controller' => AccountBills::class],
|
||
'dewu' => ['title' => '得物账户', 'controller' => DewuAccountBills::class],
|
||
'dewu-alipay' => ['title' => '得物支付宝账户', 'controller' => AccountBills::class],
|
||
'vipshop' => ['title' => '唯品会MP账户', 'controller' => AccountBills::class],
|
||
'vipshop-jx' => ['title' => '唯品会JX账户', 'controller' => AccountBills::class],
|
||
'xiaohongshu' => ['title' => '小红书账户', 'controller' => AccountBills::class],
|
||
];
|
||
|
||
public function accounts(): JsonResponse
|
||
{
|
||
$this->authorizeStoreReceivingBills();
|
||
|
||
$accounts = collect(self::ACCOUNTS)
|
||
->map(fn (array $account, string $slug): array => [
|
||
'slug' => $slug,
|
||
'title' => $account['title'],
|
||
'url' => route('api.ecommerce-finance.store-receiving-bills.index', ['account' => $slug]),
|
||
])
|
||
->values()
|
||
->all();
|
||
|
||
return response()->json(['data' => $accounts]);
|
||
}
|
||
|
||
public function index(Request $request, string $account): Response
|
||
{
|
||
$this->authorizeStoreReceivingBills();
|
||
$this->prepareDataRequest($request, $account);
|
||
|
||
$controller = app(self::ACCOUNTS[$account]['controller']);
|
||
$parameters = $controller instanceof AccountBills ? ['bill_type' => $account] : [];
|
||
$response = app()->call([$controller, 'index'], $parameters);
|
||
|
||
return $this->decorateResponse($response, $account);
|
||
}
|
||
|
||
public function filterOptions(string $account): Response
|
||
{
|
||
$this->authorizeStoreReceivingBills();
|
||
$this->ensureAccountExists($account);
|
||
|
||
$controller = app(self::ACCOUNTS[$account]['controller']);
|
||
$parameters = $controller instanceof AccountBills ? ['bill_type' => $account] : [];
|
||
$response = app()->call([$controller, 'filterOptions'], $parameters);
|
||
|
||
return $this->decorateResponse($response, $account);
|
||
}
|
||
|
||
private function prepareDataRequest(Request $request, string $account): void
|
||
{
|
||
$this->ensureAccountExists($account);
|
||
|
||
// 外部接口固定返回 JSON,不允许通过 export=1 触发 CSV 下载。
|
||
$request->query->remove('export');
|
||
|
||
$start = trim((string) $request->query->get('start', ''));
|
||
$end = trim((string) $request->query->get('end', ''));
|
||
|
||
if (($start === '') !== ($end === '')) {
|
||
throw ValidationException::withMessages([
|
||
'start' => ['开始日期和结束日期需要同时提供。'],
|
||
'end' => ['开始日期和结束日期需要同时提供。'],
|
||
]);
|
||
}
|
||
|
||
// 与页面默认口径一致:未指定日期时只读当前自然月,避免无界扫描全表。
|
||
// 指定日期时完全遵循调用方参数,不做账期改写。
|
||
if ($start === '') {
|
||
$request->query->set('start', now()->startOfMonth()->toDateString());
|
||
$request->query->set('end', now()->endOfMonth()->toDateString());
|
||
}
|
||
}
|
||
|
||
private function ensureAccountExists(string $account): void
|
||
{
|
||
abort_unless(isset(self::ACCOUNTS[$account]), 404, '未知的店铺收款账户类型。');
|
||
}
|
||
|
||
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, '当前账号没有店铺收款账户账单权限。');
|
||
}
|
||
|
||
private function decorateResponse(Response $response, string $account): Response
|
||
{
|
||
if (! $response instanceof JsonResponse || ! is_array($response->getData(true))) {
|
||
return $response;
|
||
}
|
||
|
||
$payload = $response->getData(true);
|
||
$payload['account'] = [
|
||
'slug' => $account,
|
||
'title' => self::ACCOUNTS[$account]['title'],
|
||
];
|
||
$response->setData($payload);
|
||
|
||
return $response;
|
||
}
|
||
}
|