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 @@