diff --git a/app/Business/CompanyPlan/CompanyPlanRepository.php b/app/Business/CompanyPlan/CompanyPlanRepository.php index 7f9b080f..62a01fd7 100644 --- a/app/Business/CompanyPlan/CompanyPlanRepository.php +++ b/app/Business/CompanyPlan/CompanyPlanRepository.php @@ -281,6 +281,37 @@ SQL); return $statement->fetchAll(); } + /** + * 服务公司对账从公司盘点读取类型包含“服务公司”的主体。 + * + * @return list> + */ + public function serviceCompanyRows(int $companyId): array + { + $statement = $this->pdo()->prepare(<<<'SQL' +SELECT + id, + company_name, + company_type, + taxpayer_type, + business_registration_type, + unified_social_credit_code, + company_status, + sequence_no +FROM company_plan_rows +WHERE company_id = :company_id + AND company_type IS NOT NULL + AND TRIM(company_type) LIKE '%服务公司%' +ORDER BY + CASE WHEN sequence_no IS NULL THEN 1 ELSE 0 END, + sequence_no, + id +SQL); + $statement->execute([':company_id' => $companyId]); + + return $statement->fetchAll(); + } + public function findForCompany(int $companyId, int $id): ?array { $statement = $this->pdo()->prepare( diff --git a/app/Business/ServiceCompanyReconciliation/ServiceCompanyReconciliationRepository.php b/app/Business/ServiceCompanyReconciliation/ServiceCompanyReconciliationRepository.php index 2a27a782..1f82df39 100644 --- a/app/Business/ServiceCompanyReconciliation/ServiceCompanyReconciliationRepository.php +++ b/app/Business/ServiceCompanyReconciliation/ServiceCompanyReconciliationRepository.php @@ -38,6 +38,7 @@ class ServiceCompanyReconciliationRepository { $companies = $this->connection->table('svc_service_companies') ->where('company_id', $tenantId) + ->where('status', 'active') ->orderBy('service_type_label') ->orderBy('company_name') ->get(); @@ -109,6 +110,7 @@ class ServiceCompanyReconciliationRepository 'service_type_code' => (string) $company->service_type_code, 'service_type_label' => (string) $company->service_type_label, 'taxpayer_type' => $company->taxpayer_type, + 'business_registration_type' => $company->business_registration_type, 'company_status' => (string) ($company->company_status ?: '正常'), 'planning_target_amount' => $company->planning_target_amount !== null ? (float) $company->planning_target_amount @@ -149,6 +151,94 @@ class ServiceCompanyReconciliationRepository return $row ? (array) $row : null; } + /** + * 将公司盘点中“类型包含服务公司”的主体同步为服务公司档案。 + * + * 同步只更新公司名称、类型、税务类型、信用代码和公司状态;手工维护的 + * 规划量、备注和启停状态保持不变。 + * + * @param list> $rows + */ + public function syncCompanyPlanServiceCompanies(int $tenantId, array $rows): int + { + return $this->connection->transaction(function () use ($tenantId, $rows): int { + $seen = []; + $synced = 0; + $syncedAt = now(); + + foreach ($rows as $row) { + $typeLabel = trim((string) ($row['company_type'] ?? '')); + $companyName = trim((string) ($row['company_name'] ?? '')); + if ($companyName === '' || mb_stripos($typeLabel, '服务公司') === false) { + continue; + } + + $creditCode = strtoupper(trim((string) ($row['unified_social_credit_code'] ?? ''))); + $companyKey = $this->companyKey($companyName, $creditCode); + $serviceTypeCode = $this->serviceTypeCodeFromLabel($typeLabel); + $seen[$companyKey . '|' . $serviceTypeCode] = true; + $payload = [ + 'company_key' => $companyKey, + 'company_name' => mb_substr($companyName, 0, 200), + 'unified_social_credit_code' => $creditCode !== '' ? mb_substr($creditCode, 0, 64) : null, + 'service_type_code' => $serviceTypeCode, + 'service_type_label' => mb_substr($typeLabel, 0, 64), + 'taxpayer_type' => $this->nullableText($row['taxpayer_type'] ?? null, 64), + 'business_registration_type' => $this->nullableText( + $row['business_registration_type'] ?? null, + 64, + ), + 'company_status' => $this->nullableText($row['company_status'] ?? null, 32) ?: '正常', + 'source' => 'company_plan', + 'source_row_id' => isset($row['id']) ? (int) $row['id'] : null, + 'source_synced_at' => $syncedAt, + 'updated_at' => $syncedAt, + ]; + $existing = $this->connection->table('svc_service_companies') + ->where('company_id', $tenantId) + ->where('company_key', $companyKey) + ->where('service_type_code', $serviceTypeCode) + ->first(); + + if ($existing) { + $this->connection->table('svc_service_companies') + ->where('company_id', $tenantId) + ->where('id', $existing->id) + ->update($payload); + } else { + $this->connection->table('svc_service_companies')->insert([ + ...$payload, + 'company_id' => $tenantId, + 'status' => 'active', + 'created_by' => '公司盘点同步', + 'created_at' => $syncedAt, + ]); + } + $synced++; + } + + $imported = $this->connection->table('svc_service_companies') + ->where('company_id', $tenantId) + ->where('source', 'company_plan') + ->get(); + foreach ($imported as $company) { + $key = (string) $company->company_key . '|' . (string) $company->service_type_code; + if (! isset($seen[$key]) && (string) $company->status !== 'inactive') { + $this->connection->table('svc_service_companies') + ->where('company_id', $tenantId) + ->where('id', $company->id) + ->update([ + 'status' => 'inactive', + 'source_synced_at' => $syncedAt, + 'updated_at' => $syncedAt, + ]); + } + } + + return $synced; + }); + } + /** @return list> */ public function relations(int $tenantId, int $serviceCompanyId): array { @@ -349,6 +439,10 @@ class ServiceCompanyReconciliationRepository 'service_type_code' => $serviceTypeCode, 'service_type_label' => self::SERVICE_TYPES[$serviceTypeCode], 'taxpayer_type' => $this->nullableText($data['taxpayer_type'] ?? null, 64), + 'business_registration_type' => $this->nullableText( + $data['business_registration_type'] ?? null, + 64, + ), 'company_status' => $this->nullableText($data['company_status'] ?? null, 32) ?: '正常', 'planning_target_amount' => isset($data['planning_target_amount']) && $data['planning_target_amount'] !== '' @@ -944,4 +1038,19 @@ class ServiceCompanyReconciliationRepository return $text === '' ? null : mb_substr($text, 0, $length); } + + private function serviceTypeCodeFromLabel(string $label): string + { + if (mb_stripos($label, '客服') !== false) { + return 'customer_service'; + } + if (mb_stripos($label, '仓储') !== false) { + return 'warehouse'; + } + if (mb_stripos($label, '运营') !== false) { + return 'operations'; + } + + return 'other'; + } } diff --git a/app/Http/Controllers/Business/ServiceCompanyReconciliation.php b/app/Http/Controllers/Business/ServiceCompanyReconciliation.php index a61c9482..eed32d67 100644 --- a/app/Http/Controllers/Business/ServiceCompanyReconciliation.php +++ b/app/Http/Controllers/Business/ServiceCompanyReconciliation.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers\Business; +use App\Business\CompanyPlan\CompanyPlanRepository; use App\Business\ServiceCompanyReconciliation\ServiceCompanyReconciliationRepository; use App\Utilities\FinanceLedgerAccess; use Carbon\CarbonImmutable; @@ -14,6 +15,7 @@ class ServiceCompanyReconciliation extends Controller { public function __construct( private readonly ServiceCompanyReconciliationRepository $repository, + private readonly CompanyPlanRepository $companyPlanRepository, ) { $this->middleware('auth'); $this->middleware('company.identify'); @@ -23,6 +25,7 @@ class ServiceCompanyReconciliation extends Controller { $tenantId = (int) (company_id() ?: 1); $period = $this->period($request); + $companyPlanSyncError = $this->syncCompanyPlan($tenantId); $serviceCompanies = $this->repository->serviceCompanies($tenantId, $period); return view('business.pages.financial-operations.service-company-reconciliation', [ @@ -39,6 +42,7 @@ class ServiceCompanyReconciliation extends Controller 'serviceTypes' => ServiceCompanyReconciliationRepository::SERVICE_TYPES, 'baseMetrics' => ServiceCompanyReconciliationRepository::BASE_METRICS, 'companyDirectory' => $this->repository->companyDirectory(), + 'companyPlanSyncError' => $companyPlanSyncError, 'canEdit' => FinanceLedgerAccess::canEditMapping(user()), 'summary' => [ 'companies' => count($serviceCompanies), @@ -60,6 +64,7 @@ class ServiceCompanyReconciliation extends Controller public function show(Request $request, int $serviceCompany): View { $tenantId = (int) (company_id() ?: 1); + $companyPlanSyncError = $this->syncCompanyPlan($tenantId); $company = $this->repository->serviceCompany($tenantId, $serviceCompany); abort_if($company === null, 404); @@ -90,6 +95,7 @@ class ServiceCompanyReconciliation extends Controller 'serviceTypes' => ServiceCompanyReconciliationRepository::SERVICE_TYPES, 'baseMetrics' => ServiceCompanyReconciliationRepository::BASE_METRICS, 'companyDirectory' => $this->repository->companyDirectory(), + 'companyPlanSyncError' => $companyPlanSyncError, 'canEdit' => FinanceLedgerAccess::canEditMapping(user()), 'summary' => [ 'people' => count($currentPersonKeys), @@ -288,6 +294,20 @@ class ServiceCompanyReconciliation extends Controller abort_unless(FinanceLedgerAccess::canEditMapping(user()), 403, '当前账号没有维护服务公司对账的权限。'); } + private function syncCompanyPlan(int $tenantId): ?string + { + try { + $rows = $this->companyPlanRepository->serviceCompanyRows($tenantId); + $this->repository->syncCompanyPlanServiceCompanies($tenantId, $rows); + + return null; + } catch (\Throwable $exception) { + report($exception); + + return '公司盘点服务公司同步暂时失败,当前先显示已登记的服务公司。'; + } + } + /** @return array */ private function validateCompany(Request $request): array { @@ -298,6 +318,7 @@ class ServiceCompanyReconciliation extends Controller ServiceCompanyReconciliationRepository::SERVICE_TYPES, ))], 'taxpayer_type' => ['nullable', 'string', 'max:64'], + 'business_registration_type' => ['nullable', 'string', 'max:64'], 'company_status' => ['nullable', 'in:正常,注销中,已注销'], 'planning_target_amount' => ['nullable', 'numeric', 'min:0'], 'status' => ['required', 'in:active,inactive'], diff --git a/database/migrations/2026_11_11_180000_add_company_plan_source_to_service_companies.php b/database/migrations/2026_11_11_180000_add_company_plan_source_to_service_companies.php new file mode 100644 index 00000000..dc6e0f85 --- /dev/null +++ b/database/migrations/2026_11_11_180000_add_company_plan_source_to_service_companies.php @@ -0,0 +1,55 @@ +connection()); + if (! $schema->hasTable('svc_service_companies')) { + return; + } + + $schema->table('svc_service_companies', function (Blueprint $table) use ($schema): void { + if (! $schema->hasColumn('svc_service_companies', 'source')) { + $table->string('source', 32)->nullable()->after('company_status'); + } + if (! $schema->hasColumn('svc_service_companies', 'source_row_id')) { + $table->unsignedBigInteger('source_row_id')->nullable()->after('source'); + } + if (! $schema->hasColumn('svc_service_companies', 'source_synced_at')) { + $table->timestamp('source_synced_at')->nullable()->after('source_row_id'); + } + if (! $schema->hasIndex('svc_service_companies', 'svc_service_companies_source_index')) { + $table->index(['company_id', 'source'], 'svc_service_companies_source_index'); + } + }); + } + + public function down(): void + { + $schema = Schema::connection($this->connection()); + if (! $schema->hasTable('svc_service_companies')) { + return; + } + + $schema->table('svc_service_companies', function (Blueprint $table) use ($schema): void { + if ($schema->hasIndex('svc_service_companies', 'svc_service_companies_source_index')) { + $table->dropIndex('svc_service_companies_source_index'); + } + foreach (['source_synced_at', 'source_row_id', 'source'] as $column) { + if ($schema->hasColumn('svc_service_companies', $column)) { + $table->dropColumn($column); + } + } + }); + } +}; diff --git a/database/migrations/2026_11_11_180500_add_business_registration_type_to_service_companies.php b/database/migrations/2026_11_11_180500_add_business_registration_type_to_service_companies.php new file mode 100644 index 00000000..888c252e --- /dev/null +++ b/database/migrations/2026_11_11_180500_add_business_registration_type_to_service_companies.php @@ -0,0 +1,39 @@ +connection()); + if (! $schema->hasTable('svc_service_companies') + || $schema->hasColumn('svc_service_companies', 'business_registration_type')) { + return; + } + + $schema->table('svc_service_companies', function (Blueprint $table): void { + $table->string('business_registration_type', 64)->nullable()->after('taxpayer_type'); + }); + } + + public function down(): void + { + $schema = Schema::connection($this->connection()); + if (! $schema->hasTable('svc_service_companies') + || ! $schema->hasColumn('svc_service_companies', 'business_registration_type')) { + return; + } + + $schema->table('svc_service_companies', function (Blueprint $table): void { + $table->dropColumn('business_registration_type'); + }); + } +}; diff --git a/docs/服务公司对账配置方案.md b/docs/服务公司对账配置方案.md index b246a10f..9b2b9d36 100644 --- a/docs/服务公司对账配置方案.md +++ b/docs/服务公司对账配置方案.md @@ -357,7 +357,9 @@ - 入口:`经营报表 → 服务公司对账`。 - 服务公司总览与独立详情页,点击公司进入详情。 -- 服务公司显式登记,不再依赖上游“公司类型”文字自动猜测。 +- 服务公司自动读取“公司盘点”中类型包含“服务公司”的主体;人工登记仅用于盘点外例外。 +- 公司盘点的名称、类型、税务类型、工商类型、信用代码和状态自动同步;规划量、备注和启停状态不被覆盖。 +- 盘点类型改成非服务公司后,自动来源记录转为停用,历史关系和结算保留。 - 店铺公司来自店铺主体生命周期,关系按生效期间保存。 - 同一店铺公司 + 同一服务类型禁止重叠服务公司。 - 关系修改递增版本,并将历史快照保存在 `svc_service_relation_versions`。 diff --git a/resources/views/business/pages/financial-operations/service-company-reconciliation.blade.php b/resources/views/business/pages/financial-operations/service-company-reconciliation.blade.php index ea08981f..ba2d6855 100644 --- a/resources/views/business/pages/financial-operations/service-company-reconciliation.blade.php +++ b/resources/views/business/pages/financial-operations/service-company-reconciliation.blade.php @@ -275,7 +275,7 @@ .svc-table { width: 100%; - min-width: 930px; + min-width: 1080px; border-collapse: collapse; font-size: 12px; } @@ -718,12 +718,15 @@ @if ($errors->any()) @endif + @if (! empty($companyPlanSyncError)) + + @endif @if (! $isDetail)

服务公司总览

-

按服务公司查看人员、店铺公司关系和本月结算。点击公司名称进入详情。

+

服务公司自动来自“公司盘点”中类型包含“服务公司”的主体;点击公司名称进入详情。

@@ -769,7 +772,7 @@

服务公司列表

-

服务公司必须在这里显式登记;不会根据公司名称自动猜测。

+

来源:公司盘点 → 类型包含“服务公司”。人工补充仅用于公司盘点外的例外。

@@ -785,6 +788,8 @@ 服务公司 服务类型 + 税务类型 + 工商类型 状态 当前人数 人员工资快照 @@ -805,6 +810,8 @@ {{ $company['service_type_label'] }} + {{ $company['taxpayer_type'] ?: '—' }} + {{ $company['business_registration_type'] ?: '—' }} $company['company_status'] === '正常', 'is-warning' => $company['company_status'] === '注销中', 'is-danger' => $company['company_status'] === '已注销'])> {{ $company['company_status'] }} @@ -850,6 +857,8 @@

统一社会信用代码:{{ $serviceCompany['unified_social_credit_code'] ?: '未维护' }} + · 税务类型:{{ $serviceCompany['taxpayer_type'] ?: '未维护' }} + · 工商类型:{{ $serviceCompany['business_registration_type'] ?: '未维护' }}

@@ -1268,6 +1277,10 @@
+
+ + +