66 lines
2.1 KiB
PHP
66 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Utilities;
|
|
|
|
use Illuminate\Database\Query\Builder;
|
|
|
|
class FinanceLedgerAccountShouldKeep
|
|
{
|
|
public const YES = '是';
|
|
|
|
public const NO = '否';
|
|
|
|
public const OPTIONS = [self::YES, self::NO];
|
|
|
|
public static function normalize(?string $value): string
|
|
{
|
|
$value = trim((string) $value);
|
|
if ($value === '') {
|
|
return '';
|
|
}
|
|
|
|
$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 '';
|
|
}
|
|
|
|
return str_contains($plan, '保留') ? self::YES : self::NO;
|
|
}
|
|
|
|
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', '%保留%');
|
|
});
|
|
});
|
|
}
|
|
}
|