CaiWuXiTong/app/Utilities/FinanceLedgerAccountShouldKeep.php
shanshi e76e87e4db feat(财务台账): 发布银行账单与账户资金管理 (#1385)
发布银行账单、账户资金管理、公司规划分布与余额联动。
2026-11-25 09:14:24 +08:00

85 lines
2.6 KiB
PHP

<?php
namespace App\Utilities;
use Illuminate\Database\Query\Builder;
class FinanceLedgerAccountShouldKeep
{
public const YES = '是';
public const TEMPORARY = '暂时保留';
public const NO = '否';
public const OPTIONS = [self::YES, self::TEMPORARY, self::NO];
public const LABELS = [
self::YES => '保留',
self::TEMPORARY => '暂时保留',
self::NO => '不保留',
];
public static function normalize(?string $value): string
{
$value = trim((string) $value);
if ($value === '') {
return '';
}
if ($value === self::TEMPORARY) {
return self::TEMPORARY;
}
$compact = mb_strtoupper((string) preg_replace('/\s+/u', '', $value));
if (in_array($compact, ['是', '保留', '需要保留', '需要', 'Y', 'YES', 'TRUE', '1'], true)) {
return self::YES;
}
if (in_array($compact, ['否', '不保留', '不需要保留', '不需要', 'N', 'NO', 'FALSE', '0'], true)) {
return self::NO;
}
return $value;
}
public static function display(?string $explicitValue, ?string $companyPlan): string
{
$normalized = self::normalize($explicitValue);
if ($normalized !== '') {
return $normalized;
}
$plan = trim((string) $companyPlan);
if ($plan === '') {
return '';
}
if (str_contains($plan, '不保留')) {
return self::TEMPORARY;
}
return str_contains($plan, '保留') ? self::YES : self::NO;
}
public static function label(?string $explicitValue, ?string $companyPlan): string
{
return self::LABELS[self::display($explicitValue, $companyPlan)] ?? '';
}
public static function applyUnretainedCondition(
Builder $query,
string $explicitColumn = 'account_should_keep',
string $companyPlanColumn = 'company_plan',
): Builder {
return $query->where(function (Builder $keep) use ($explicitColumn, $companyPlanColumn): void {
$keep->where($explicitColumn, self::NO)
->orWhere(function (Builder $inferred) use ($explicitColumn, $companyPlanColumn): void {
$inferred->where(function (Builder $blank) use ($explicitColumn): void {
$blank->whereNull($explicitColumn)->orWhere($explicitColumn, '');
})
->whereNotNull($companyPlanColumn)
->where($companyPlanColumn, '<>', '')
->where($companyPlanColumn, 'not like', '%保留%');
});
});
}
}