diff --git a/app/Console/Commands/AssignFinanceLedgerCompanyPlans.php b/app/Console/Commands/AssignFinanceLedgerCompanyPlans.php index 882fde37..d376a15a 100644 --- a/app/Console/Commands/AssignFinanceLedgerCompanyPlans.php +++ b/app/Console/Commands/AssignFinanceLedgerCompanyPlans.php @@ -109,6 +109,7 @@ class AssignFinanceLedgerCompanyPlans extends Command $connection->table('fin_accounts') ->whereIn('id', $ids) ->update([ + 'company_plan' => '保留', 'account_should_keep' => '是', 'updated_at' => $now, ]); diff --git a/app/Http/Controllers/Business/FinanceLedger.php b/app/Http/Controllers/Business/FinanceLedger.php index f517eefa..579eac82 100644 --- a/app/Http/Controllers/Business/FinanceLedger.php +++ b/app/Http/Controllers/Business/FinanceLedger.php @@ -2924,15 +2924,12 @@ class FinanceLedger extends Controller ->where('account_short', $accountShort) ->first(['company_name', 'account_short', 'account_type', 'account_should_keep']); abort_if($account === null, 422, '账户简称不存在,请刷新账户总表后重试。'); - $allowedPlans = $connection->table('fin_accounts') + $allowedPlans = FinanceLedgerAccountShouldKeep::companyPlanOptions($connection->table('fin_accounts') ->whereNotNull('company_plan') ->where('company_plan', '<>', '') ->distinct() ->pluck('company_plan') - ->map(static fn (mixed $value): string => trim((string) $value)) - ->filter() - ->values() - ->all(); + ->all()); abort_unless($plan === '' || in_array($plan, $allowedPlans, true), 422, '请选择现有公司规划。'); $overrideType = $connection->table('fin_account_type_overrides') @@ -2941,6 +2938,7 @@ class FinanceLedger extends Controller $effectiveType = $overrideType !== null ? (string) $overrideType : (string) ($account->account_type ?? ''); + $effectivePlan = $plan; $accountUpdates = []; if (FinanceLedgerAccountType::companyPlanDrivesKeep($effectiveType)) { $accountUpdates = $companyPlans->synchronizeBankAccounts( @@ -2956,6 +2954,8 @@ class FinanceLedger extends Controller 'updated_at' => now(), ]; if (FinanceLedgerAccountType::isAlwaysRetained($effectiveType)) { + $effectivePlan = '保留'; + $values['company_plan'] = '保留'; $values['account_should_keep'] = FinanceLedgerAccountShouldKeep::YES; $account->account_should_keep = FinanceLedgerAccountShouldKeep::YES; } @@ -2969,8 +2969,8 @@ class FinanceLedger extends Controller 'ok' => true, 'account_short' => $accountShort, 'company_name' => (string) $account->company_name, - 'company_plan' => $plan, - 'label' => $plan !== '' ? $plan : '未填写', + 'company_plan' => $effectivePlan, + 'label' => $effectivePlan !== '' ? $effectivePlan : '未填写', 'account_should_keep' => $keep, 'keep_label' => $keep !== '' ? $keep : '未填写', 'account_updates' => $accountUpdates, diff --git a/app/Utilities/FinanceLedgerAccountShouldKeep.php b/app/Utilities/FinanceLedgerAccountShouldKeep.php index ebef6610..e533bdcb 100644 --- a/app/Utilities/FinanceLedgerAccountShouldKeep.php +++ b/app/Utilities/FinanceLedgerAccountShouldKeep.php @@ -14,6 +14,8 @@ class FinanceLedgerAccountShouldKeep public const OPTIONS = [self::YES, self::TEMPORARY, self::NO]; + public const COMPANY_PLAN_OPTIONS = ['保留', '不保留', '待注销']; + public const LABELS = [ self::YES => '保留', self::TEMPORARY => '暂时保留', @@ -71,6 +73,18 @@ class FinanceLedgerAccountShouldKeep : self::TEMPORARY; } + public static function companyPlanOptions(array $existing = []): array + { + $options = [ + ...self::COMPANY_PLAN_OPTIONS, + ...array_map(static fn (mixed $value): string => trim((string) $value), $existing), + ]; + $options = array_values(array_unique(array_filter($options))); + sort($options, SORT_NATURAL | SORT_FLAG_CASE); + + return $options; + } + public static function label(?string $explicitValue, ?string $companyPlan): string { return self::LABELS[self::display($explicitValue, $companyPlan)] ?? ''; diff --git a/modules/EcommerceFinance/Services/FinanceLedgerAccountCreator.php b/modules/EcommerceFinance/Services/FinanceLedgerAccountCreator.php index d520763b..48f1fccb 100644 --- a/modules/EcommerceFinance/Services/FinanceLedgerAccountCreator.php +++ b/modules/EcommerceFinance/Services/FinanceLedgerAccountCreator.php @@ -24,7 +24,9 @@ class FinanceLedgerAccountCreator $accountShort = $this->text($data['account_short'] ?? ''); $accountNo = $this->text($data['account_no'] ?? ''); $accountType = $this->text($data['account_type'] ?? ''); - $companyPlan = $this->text($data['company_plan'] ?? ''); + $companyPlan = FinanceLedgerAccountType::isAlwaysRetained($accountType) + ? '保留' + : $this->text($data['company_plan'] ?? ''); $companyFields = [ 'register_place' => $this->nullableText($data['company_register_place'] ?? null), 'business_type' => $this->nullableText($data['company_business_type'] ?? null), diff --git a/modules/EcommerceFinance/Services/FinanceLedgerAccountEditor.php b/modules/EcommerceFinance/Services/FinanceLedgerAccountEditor.php index 6e6b3633..48204ed7 100644 --- a/modules/EcommerceFinance/Services/FinanceLedgerAccountEditor.php +++ b/modules/EcommerceFinance/Services/FinanceLedgerAccountEditor.php @@ -59,7 +59,9 @@ class FinanceLedgerAccountEditor $accountType = $this->nullableText($data['account_type'] ?? null); $accountNo = $this->nullableText($data['account_no'] ?? null); $status = FinanceLedgerAccountStatus::normalize((string) ($data['status'] ?? '')); - $companyPlan = $this->nullableText($data['company_plan'] ?? null); + $companyPlan = FinanceLedgerAccountType::isAlwaysRetained($accountType) + ? '保留' + : $this->nullableText($data['company_plan'] ?? null); $syncCompanyPlan = array_key_exists('company_plan', $data) && FinanceLedgerAccountType::companyPlanDrivesKeep($accountType); $accountShouldKeep = FinanceLedgerAccountType::isAlwaysRetained($accountType) diff --git a/modules/EcommerceFinance/Services/FinanceLedgerBaseImporter.php b/modules/EcommerceFinance/Services/FinanceLedgerBaseImporter.php index 7ac4994c..f9371499 100644 --- a/modules/EcommerceFinance/Services/FinanceLedgerBaseImporter.php +++ b/modules/EcommerceFinance/Services/FinanceLedgerBaseImporter.php @@ -129,6 +129,7 @@ class FinanceLedgerBaseImporter $keepIndex === null ? null : $this->text($row[$keepIndex] ?? null), ); if (FinanceLedgerAccountType::isAlwaysRetained($accountType)) { + $companyPlan = '保留'; $accountShouldKeep = FinanceLedgerAccountShouldKeep::YES; } elseif (FinanceLedgerAccountType::companyPlanDrivesKeep($accountType) && $companyPlan !== '') { diff --git a/modules/EcommerceFinance/Services/FinanceLedgerCompanyPlanSynchronizer.php b/modules/EcommerceFinance/Services/FinanceLedgerCompanyPlanSynchronizer.php index e0537374..7c36e73f 100644 --- a/modules/EcommerceFinance/Services/FinanceLedgerCompanyPlanSynchronizer.php +++ b/modules/EcommerceFinance/Services/FinanceLedgerCompanyPlanSynchronizer.php @@ -73,13 +73,14 @@ final class FinanceLedgerCompanyPlanSynchronizer $currentPlan = trim((string) ($account->company_plan ?? '')); $currentKeep = trim((string) ($account->account_should_keep ?? '')); if (FinanceLedgerAccountType::isAlwaysRetained($accountType)) { - if ($currentKeep === FinanceLedgerAccountShouldKeep::YES) { + if ($currentPlan === '保留' + && $currentKeep === FinanceLedgerAccountShouldKeep::YES) { continue; } $personalCardChanges[] = [ 'id' => (int) $account->id, 'account_short' => $accountShort, - 'company_plan' => $currentPlan, + 'company_plan' => '保留', 'account_should_keep' => FinanceLedgerAccountShouldKeep::YES, ]; @@ -121,6 +122,7 @@ final class FinanceLedgerCompanyPlanSynchronizer $this->connection->table('fin_accounts') ->whereIn('id', array_column($personalCardChanges, 'id')) ->update([ + 'company_plan' => '保留', 'account_should_keep' => FinanceLedgerAccountShouldKeep::YES, 'updated_at' => $now, ]); diff --git a/modules/EcommerceFinance/View/FinanceLedgerPageData.php b/modules/EcommerceFinance/View/FinanceLedgerPageData.php index 4114e1bc..aa28bd88 100644 --- a/modules/EcommerceFinance/View/FinanceLedgerPageData.php +++ b/modules/EcommerceFinance/View/FinanceLedgerPageData.php @@ -2920,17 +2920,15 @@ class FinanceLedgerPageData private function accountPlanOptions(): array { - return FinanceLedgerIndependentAccounts::exclude($this->db()->table('fin_accounts')) + $existing = FinanceLedgerIndependentAccounts::exclude($this->db()->table('fin_accounts')) ->whereNotNull('company_plan') ->where('company_plan', '<>', '') ->distinct() ->orderBy('company_plan') ->pluck('company_plan') - ->map(static fn (mixed $value): string => trim((string) $value)) - ->filter() - ->unique() - ->values() ->all(); + + return FinanceLedgerAccountShouldKeep::companyPlanOptions($existing); } private function companyOptions(): array diff --git a/resources/views/business/pages/financial-ledger/ledger.blade.php b/resources/views/business/pages/financial-ledger/ledger.blade.php index 51ef1d5b..0a7328d4 100644 --- a/resources/views/business/pages/financial-ledger/ledger.blade.php +++ b/resources/views/business/pages/financial-ledger/ledger.blade.php @@ -1654,7 +1654,7 @@ {{ $row->company_plan ?: '未填写' }} @if ($canEditAccountStatus) - @foreach ($accountPlanOptions as $planOption) @@ -1765,7 +1765,7 @@ {{ $row->company_plan ?: '未填写' }} @if ($canEditAccountStatus) - @foreach ($accountPlanOptions as $planOption) @@ -5954,7 +5954,11 @@ accountKeepSelect.title = mode === 'always_retained' ? '个人卡固定为保留' : (mode === 'company_plan' ? '由公司规划自动生成' : ''); - if (mode === 'always_retained') accountKeepSelect.value = '是'; + if (accountPlanInput) accountPlanInput.readOnly = mode === 'always_retained'; + if (mode === 'always_retained') { + if (accountPlanInput) accountPlanInput.value = '保留'; + accountKeepSelect.value = '是'; + } if (mode === 'company_plan') { accountKeepSelect.value = keepForCompanyPlan(accountPlanInput?.value); } @@ -6005,7 +6009,8 @@ accountPanel?.classList.toggle('is-account-edit-mode', active); accountPanel?.querySelectorAll('[data-account-editor]').forEach((editor) => { const keepMode = editor.dataset.accountKeepMode || 'manual'; - editor.disabled = !active || keepMode !== 'manual'; + const planLocked = editor.dataset.accountPlanLocked === 'true'; + editor.disabled = !active || keepMode !== 'manual' || planLocked; }); accountEditToggle?.setAttribute('aria-checked', active ? 'true' : 'false'); if (accountAddCompanyButton) { @@ -6140,13 +6145,19 @@ const accountEditField = (name) => accountEditForm?.elements.namedItem(name) || null; const applyAccountEditKeepMode = (mode, companyPlan = '') => { const keepField = accountEditField('account_should_keep'); + const planField = accountEditField('company_plan'); if (!keepField) return; keepField.disabled = mode !== 'manual'; keepField.title = mode === 'always_retained' ? '个人卡固定为保留' : (mode === 'company_plan' ? '由公司规划自动生成' : ''); - if (mode === 'always_retained') keepField.value = '是'; - if (mode === 'company_plan') keepField.value = keepForCompanyPlan(companyPlan); + if (planField) planField.readOnly = mode === 'always_retained'; + if (mode === 'always_retained') { + if (planField) planField.value = '保留'; + keepField.value = '是'; + } else if (mode === 'company_plan') { + keepField.value = keepForCompanyPlan(companyPlan); + } }; const openAccountEditDialog = async (button) => { if (!accountEditDialog || !accountEditForm) return; diff --git a/tests/Feature/Business/FinanceLedgerAccountStatusTest.php b/tests/Feature/Business/FinanceLedgerAccountStatusTest.php index fadb3ef4..d3c5fc21 100644 --- a/tests/Feature/Business/FinanceLedgerAccountStatusTest.php +++ b/tests/Feature/Business/FinanceLedgerAccountStatusTest.php @@ -1088,7 +1088,8 @@ class FinanceLedgerAccountStatusTest extends FeatureTestCase $this->loginAs()->get($this->pageUrl()) ->assertOk() ->assertSee('data-account-keep-mode="company_plan"', false) - ->assertSee('data-account-keep-mode="always_retained"', false); + ->assertSee('data-account-keep-mode="always_retained"', false) + ->assertSee('data-account-plan-locked="true"', false); $this->loginAs()->postJson($this->planUrl(), [ 'account_short' => '联动公户甲', @@ -1106,7 +1107,7 @@ class FinanceLedgerAccountStatusTest extends FeatureTestCase } $this->assertDatabaseHas('fin_accounts', [ 'account_short' => '联动个人卡', - 'company_plan' => '不保留', + 'company_plan' => '保留', 'account_should_keep' => '是', ], 'business_mirror'); $this->assertDatabaseHas('fin_accounts', [ @@ -1130,7 +1131,7 @@ class FinanceLedgerAccountStatusTest extends FeatureTestCase } $this->assertDatabaseHas('fin_accounts', [ 'account_short' => '联动个人卡', - 'company_plan' => '不保留', + 'company_plan' => '保留', 'account_should_keep' => '是', ], 'business_mirror'); diff --git a/tests/Feature/Console/AssignFinanceLedgerCompanyPlansTest.php b/tests/Feature/Console/AssignFinanceLedgerCompanyPlansTest.php index f1878054..b4c32166 100644 --- a/tests/Feature/Console/AssignFinanceLedgerCompanyPlansTest.php +++ b/tests/Feature/Console/AssignFinanceLedgerCompanyPlansTest.php @@ -89,7 +89,7 @@ class AssignFinanceLedgerCompanyPlansTest extends FeatureTestCase '暂时公司银行账户' => '不保留/暂时保留', '保留公司支付宝' => '待注销/否', '暂时公司网商' => '保留/是', - '暂时公司个人卡' => '不保留/是', + '暂时公司个人卡' => '保留/是', ], $connection->table('fin_accounts') ->orderBy('id') ->get(['company_name', 'account_short', 'company_plan', 'account_should_keep']) @@ -122,7 +122,7 @@ class AssignFinanceLedgerCompanyPlansTest extends FeatureTestCase ], 'business_mirror'); $this->assertDatabaseHas('fin_accounts', [ 'account_short' => '暂时公司个人卡', - 'company_plan' => '不保留', + 'company_plan' => '保留', 'account_should_keep' => '是', ], 'business_mirror'); } diff --git a/tests/Unit/EcommerceFinance/FinanceLedgerBaseImporterTest.php b/tests/Unit/EcommerceFinance/FinanceLedgerBaseImporterTest.php index 2d44989d..2a84e8c4 100644 --- a/tests/Unit/EcommerceFinance/FinanceLedgerBaseImporterTest.php +++ b/tests/Unit/EcommerceFinance/FinanceLedgerBaseImporterTest.php @@ -66,6 +66,7 @@ class FinanceLedgerBaseImporterTest extends TestCase $rows = $this->importer()->parseAccounts($sheet); $this->assertCount(1, $rows); + $this->assertSame('保留', $rows[0]['company_plan']); $this->assertSame('是', $rows[0]['account_should_keep']); $this->assertSame('0001', $rows[0]['account_last4']); $this->assertSame('10000', $rows[0]['single_limit']); @@ -128,6 +129,7 @@ class FinanceLedgerBaseImporterTest extends TestCase $rows = $this->importer()->parseAccounts($sheet); $this->assertCount(1, $rows); + $this->assertSame('保留', $rows[0]['company_plan']); $this->assertSame('是', $rows[0]['account_should_keep']); $this->assertSame('0002', $rows[0]['account_last4']); $this->assertSame('1000', $rows[0]['single_limit']); diff --git a/tests/Unit/FinanceLedgerAccountShouldKeepTest.php b/tests/Unit/FinanceLedgerAccountShouldKeepTest.php index 7c52fe54..eac1ca1d 100644 --- a/tests/Unit/FinanceLedgerAccountShouldKeepTest.php +++ b/tests/Unit/FinanceLedgerAccountShouldKeepTest.php @@ -50,4 +50,11 @@ class FinanceLedgerAccountShouldKeepTest extends TestCase ); $this->assertNull(FinanceLedgerAccountShouldKeep::fromCompanyPlan('')); } + + public function test_company_plan_options_keep_fixed_values_available(): void + { + $this->assertContains('保留', FinanceLedgerAccountShouldKeep::companyPlanOptions()); + $this->assertContains('不保留', FinanceLedgerAccountShouldKeep::companyPlanOptions()); + $this->assertContains('待注销', FinanceLedgerAccountShouldKeep::companyPlanOptions()); + } }