272 lines
11 KiB
PHP
272 lines
11 KiB
PHP
<?php
|
|
|
|
namespace App\Business\CompanyPlan;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
use Throwable;
|
|
|
|
class CompanyPlanPageData
|
|
{
|
|
public function __construct(
|
|
private readonly CompanyPlanRepository $repository,
|
|
private readonly CompanyPlanWorkbookImporter $importer,
|
|
private readonly CompanyPlanShopRepository $shops,
|
|
private readonly CompanyPlanCompanyRepository $companies,
|
|
) {}
|
|
|
|
/**
|
|
* @return array<string,mixed>
|
|
*/
|
|
public function page(Request $request): array
|
|
{
|
|
$companyId = (int) (company_id() ?: 1);
|
|
$userId = (int) (auth()->id() ?: 0);
|
|
$filters = self::filters($request);
|
|
$error = null;
|
|
$payload = [
|
|
'rows' => [],
|
|
'total' => 0,
|
|
'page' => 1,
|
|
'per_page' => $filters['per_page'],
|
|
'last_page' => 1,
|
|
];
|
|
$summary = [
|
|
'total' => 0,
|
|
'retained' => 0,
|
|
'cancelled_done' => 0,
|
|
'cancelling' => 0,
|
|
'plan_cancelled_pending' => 0,
|
|
];
|
|
$options = [
|
|
'plan' => ['保留', '注销', '待定'],
|
|
'company_status' => ['正常', '注销中', '已注销'],
|
|
'company_type' => [],
|
|
'taxpayer_type' => [],
|
|
'business_registration_type' => [],
|
|
];
|
|
$importPreview = null;
|
|
$shopFilters = self::shopFilters($request);
|
|
$shopPayload = [
|
|
'rows' => [],
|
|
'total' => 0,
|
|
'page' => 1,
|
|
'per_page' => $shopFilters['shop_per_page'],
|
|
'last_page' => 1,
|
|
];
|
|
$shopSummary = ['total' => 0, 'companies' => 0, 'statuses' => []];
|
|
$shopStatusOptions = [];
|
|
$shopCounts = [];
|
|
$shareholdingCounts = [];
|
|
|
|
try {
|
|
$payload = $this->repository->paginate(
|
|
$companyId,
|
|
$filters,
|
|
max((int) ($filters['page'] ?? 1), 1),
|
|
(int) $filters['per_page'],
|
|
);
|
|
$summary = $this->repository->summary($companyId);
|
|
$storedOptions = $this->repository->filterOptions($companyId);
|
|
foreach (['plan', 'company_status', 'company_type', 'taxpayer_type', 'business_registration_type'] as $field) {
|
|
$options[$field] = array_values(array_unique(array_merge(
|
|
$options[$field],
|
|
$storedOptions[$field] ?? [],
|
|
)));
|
|
}
|
|
|
|
$token = (string) $request->session()->get('company_plan_preview_token', '');
|
|
if ($token !== '') {
|
|
$importPreview = $this->importer->previewByToken($token, $companyId, $userId);
|
|
if ($importPreview === null) {
|
|
$request->session()->forget('company_plan_preview_token');
|
|
}
|
|
}
|
|
|
|
$shopPayload = $this->shops->paginate(
|
|
$companyId,
|
|
$shopFilters,
|
|
(int) $shopFilters['shop_page'],
|
|
(int) $shopFilters['shop_per_page'],
|
|
);
|
|
$shopSummary = $this->shops->summary($companyId, $shopFilters);
|
|
$shopStatusOptions = $this->shops->statusOptions($companyId);
|
|
$shopCounts = $this->shops->countsByCompany($companyId);
|
|
$shareholdingCounts = $this->companies->countsByShareholder($companyId);
|
|
} catch (Throwable $exception) {
|
|
report($exception);
|
|
$error = '公司盘点本地原型数据暂时无法读取。请确认 storage/app 可写后重新加载。';
|
|
}
|
|
|
|
$rows = new LengthAwarePaginator(
|
|
array_map(static fn (array $row): object => (object) $row, $payload['rows']),
|
|
(int) $payload['total'],
|
|
(int) $payload['per_page'],
|
|
(int) $payload['page'],
|
|
[
|
|
'path' => $request->url(),
|
|
'query' => $request->except(['page', 'shop_page']),
|
|
],
|
|
);
|
|
|
|
return [
|
|
'view' => 'business.pages.company-management.company-plan',
|
|
'companyPlanRows' => $rows,
|
|
'companyPlanFields' => CompanyPlanRepository::FIELDS,
|
|
'companyPlanFilters' => $filters,
|
|
'companyPlanSummary' => $summary,
|
|
'companyPlanSummaryCards' => $this->summaryCards($summary, $request),
|
|
'companyPlanOptions' => $options,
|
|
'companyPlanImportPreview' => $importPreview,
|
|
'companyPlanError' => $error,
|
|
'companyPlanShopRows' => array_map(static fn (array $row): object => (object) $row, $shopPayload['rows']),
|
|
'companyPlanShopFields' => CompanyPlanShopRepository::FIELDS,
|
|
'companyPlanShopFilters' => $shopFilters,
|
|
'companyPlanShopTotal' => (int) $shopPayload['total'],
|
|
'companyPlanShopPage' => (int) $shopPayload['page'],
|
|
'companyPlanShopLastPage' => (int) $shopPayload['last_page'],
|
|
'companyPlanShopSummary' => $shopSummary,
|
|
'companyPlanShopStatuses' => $shopStatusOptions,
|
|
'companyPlanShopCounts' => $shopCounts,
|
|
'companyPlanShareholdingCounts' => $shareholdingCounts,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array{
|
|
* keyword:string,
|
|
* plan:string,
|
|
* company_status:string,
|
|
* company_type:string,
|
|
* taxpayer_type:string,
|
|
* business_registration_type:string,
|
|
* remark_keyword:string,
|
|
* remark_state:string,
|
|
* sort:string,
|
|
* direction:string,
|
|
* page:int,
|
|
* per_page:int
|
|
* }
|
|
*/
|
|
public static function filters(Request $request): array
|
|
{
|
|
$sortable = array_column(CompanyPlanRepository::FIELDS, 'name');
|
|
$perPage = (int) $request->query('per_page', 25);
|
|
if (! in_array($perPage, [25, 50, 100], true)) {
|
|
$perPage = 25;
|
|
}
|
|
|
|
return [
|
|
'keyword' => mb_substr(trim((string) $request->query('keyword', '')), 0, 191),
|
|
'plan' => mb_substr(trim((string) $request->query('plan', '')), 0, 64),
|
|
'company_status' => mb_substr(trim((string) $request->query('company_status', '')), 0, 64),
|
|
'company_type' => mb_substr(trim((string) $request->query('company_type', '')), 0, 64),
|
|
'taxpayer_type' => mb_substr(trim((string) $request->query('taxpayer_type', '')), 0, 64),
|
|
'business_registration_type' => mb_substr(trim((string) $request->query('business_registration_type', '')), 0, 64),
|
|
'remark_keyword' => mb_substr(trim((string) $request->query('remark_keyword', '')), 0, 191),
|
|
'remark_state' => in_array($request->query('remark_state'), ['filled', 'empty'], true)
|
|
? (string) $request->query('remark_state')
|
|
: '',
|
|
'sort' => in_array($request->query('sort'), $sortable, true)
|
|
? (string) $request->query('sort')
|
|
: 'sequence_no',
|
|
'direction' => $request->query('direction') === 'desc' ? 'desc' : 'asc',
|
|
'page' => max((int) $request->query('page', 1), 1),
|
|
'per_page' => $perPage,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 未完结店铺表的筛选条件。
|
|
*
|
|
* @return array{shop_keyword:string,shop_status:string,shop_company:string,shop_page:int,shop_per_page:int}
|
|
*/
|
|
public static function shopFilters(Request $request): array
|
|
{
|
|
$perPage = (int) $request->query('shop_per_page', 25);
|
|
if (! in_array($perPage, [25, 50, 100], true)) {
|
|
$perPage = 25;
|
|
}
|
|
|
|
return [
|
|
'shop_keyword' => mb_substr(trim((string) $request->query('shop_keyword', '')), 0, 191),
|
|
'shop_status' => mb_substr(trim((string) $request->query('shop_status', '')), 0, 64),
|
|
'shop_company' => mb_substr(trim((string) $request->query('shop_company', '')), 0, 255),
|
|
'shop_page' => max((int) $request->query('shop_page', 1), 1),
|
|
'shop_per_page' => $perPage,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param array{total:int,retained:int,cancelled_done:int,cancelling:int,plan_cancelled_pending:int} $summary
|
|
* @return list<array{label:string,value:int,hint:string,tone:string,filter:string,url:string,active:bool}>
|
|
*/
|
|
private function summaryCards(array $summary, Request $request): array
|
|
{
|
|
$baseQuery = $request->query();
|
|
unset(
|
|
$baseQuery['page'],
|
|
$baseQuery['per_page'],
|
|
$baseQuery['plan'],
|
|
$baseQuery['company_status'],
|
|
$baseQuery['remark_state'],
|
|
);
|
|
|
|
$buildUrl = static function (array $extra) use ($request, $baseQuery): string {
|
|
return $request->url() . '?' . http_build_query(array_merge($baseQuery, $extra, ['page' => 1]))
|
|
. '#company-plan-table';
|
|
};
|
|
$currentPlan = (string) $request->query('plan', '');
|
|
$currentStatus = (string) $request->query('company_status', '');
|
|
$currentRemarkState = (string) $request->query('remark_state', '');
|
|
|
|
return [
|
|
[
|
|
'label' => '公司总数',
|
|
'value' => $summary['total'],
|
|
'hint' => '当前导入的全部主体',
|
|
'tone' => 'accent',
|
|
'filter' => 'all',
|
|
'url' => $buildUrl([]),
|
|
'active' => $currentPlan === '' && $currentStatus === '' && $currentRemarkState === '',
|
|
],
|
|
[
|
|
'label' => '规划保留',
|
|
'value' => $summary['retained'],
|
|
'hint' => '点击筛选规划为“保留”',
|
|
'tone' => 'success',
|
|
'filter' => 'plan:retained',
|
|
'url' => $buildUrl(['plan' => '保留']),
|
|
'active' => $currentPlan === '保留',
|
|
],
|
|
[
|
|
'label' => '已注销',
|
|
'value' => $summary['cancelled_done'],
|
|
'hint' => '点击筛选公司状态为“已注销”',
|
|
'tone' => 'danger',
|
|
'filter' => 'status:cancelled_done',
|
|
'url' => $buildUrl(['company_status' => '已注销']),
|
|
'active' => $currentStatus === '已注销',
|
|
],
|
|
[
|
|
'label' => '注销中',
|
|
'value' => $summary['cancelling'],
|
|
'hint' => '点击筛选注销办理中的主体',
|
|
'tone' => 'warning',
|
|
'filter' => 'status:cancelling',
|
|
'url' => $buildUrl(['company_status' => '注销中']),
|
|
'active' => $currentStatus === '注销中',
|
|
],
|
|
[
|
|
'label' => '规划注销未发起',
|
|
'value' => $summary['plan_cancelled_pending'],
|
|
'hint' => '规划为注销、公司状态仍是正常',
|
|
'tone' => 'info',
|
|
'filter' => 'plan:cancelled_pending',
|
|
'url' => $buildUrl(['plan' => '注销', 'company_status' => '正常']),
|
|
'active' => $currentPlan === '注销' && $currentStatus === '正常',
|
|
],
|
|
];
|
|
}
|
|
}
|