Co-authored-by: Cursor <cursoragent@cursor.com> Co-authored-by: multica-agent <github@multica.ai>
4180 lines
201 KiB
PHP
Executable File
4180 lines
201 KiB
PHP
Executable File
<?php
|
||
declare(strict_types=1);
|
||
|
||
require_once __DIR__ . '/../app/auth.php';
|
||
require_once __DIR__ . '/../app/helpers.php';
|
||
require_once __DIR__ . '/../app/services/quick_queries.php';
|
||
require_once __DIR__ . '/../app/views/layout.php';
|
||
if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
|
||
require_once __DIR__ . '/../vendor/autoload.php';
|
||
}
|
||
|
||
$pdo = db();
|
||
$action = (string)($_GET['action'] ?? '');
|
||
if ($action === '') {
|
||
$listHints = ['dir_id', 'q', 'status', 'kind', 'search_field', 'page', 'sort_by', 'view'];
|
||
$hasList = false;
|
||
foreach ($listHints as $k) {
|
||
if (isset($_GET[$k])) { $hasList = true; break; }
|
||
}
|
||
$action = $hasList ? 'home' : 'dashboard';
|
||
}
|
||
|
||
if (!defined('ARCHIVE_KIND_BANK_LOAN')) {
|
||
define('ARCHIVE_KIND_BANK_LOAN', 'bank_loan');
|
||
define('ARCHIVE_KIND_EXTERNAL_GUARANTEE', 'external_guarantee');
|
||
}
|
||
|
||
function normalizeArchiveKind(?string $v): string
|
||
{
|
||
$v = trim((string) $v);
|
||
return $v === ARCHIVE_KIND_EXTERNAL_GUARANTEE ? ARCHIVE_KIND_EXTERNAL_GUARANTEE : ARCHIVE_KIND_BANK_LOAN;
|
||
}
|
||
|
||
function archiveKindLabel(string $k): string
|
||
{
|
||
return $k === ARCHIVE_KIND_EXTERNAL_GUARANTEE ? '对外担保' : '银行贷款';
|
||
}
|
||
|
||
if ($action === 'logout') {
|
||
session_destroy();
|
||
header('Location: index.php?action=login');
|
||
exit;
|
||
}
|
||
|
||
if ($action === 'login') {
|
||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||
$username = trim((string)($_POST['username'] ?? ''));
|
||
$password = (string)($_POST['password'] ?? '');
|
||
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :u');
|
||
$stmt->execute([':u' => $username]);
|
||
$user = $stmt->fetch();
|
||
if ($user && password_verify($password, $user['password_hash'])) {
|
||
$_SESSION['uid'] = (int)$user['id'];
|
||
header('Location: index.php');
|
||
exit;
|
||
}
|
||
flash('error', '用户名或密码错误');
|
||
header('Location: index.php?action=login');
|
||
exit;
|
||
}
|
||
$flash = getFlash();
|
||
fusionLoginPage($flash);
|
||
exit;
|
||
}
|
||
|
||
requireLogin();
|
||
$me = currentUser();
|
||
if ($action === 'home') {
|
||
$qs = (string)($_SERVER['QUERY_STRING'] ?? '');
|
||
$_SESSION['last_list_url'] = 'index.php' . ($qs !== '' ? ('?' . $qs) : '');
|
||
}
|
||
|
||
function fetchDirectories(PDO $pdo): array
|
||
{
|
||
return $pdo->query('SELECT * FROM directories ORDER BY level ASC, id ASC')->fetchAll();
|
||
}
|
||
|
||
function safePathPart(string $name): string
|
||
{
|
||
$name = trim($name);
|
||
$name = preg_replace('/[\/\\\\:\*\?"<>\|]+/u', '_', $name);
|
||
$name = preg_replace('/\s+/u', '_', (string)$name);
|
||
$name = trim((string)$name, '._');
|
||
return $name !== '' ? $name : 'unnamed';
|
||
}
|
||
|
||
function buildArchiveUploadSubdir(PDO $pdo, int $archiveId): string
|
||
{
|
||
$stmt = $pdo->prepare('SELECT id,name,directory_id FROM archives WHERE id=:id');
|
||
$stmt->execute([':id' => $archiveId]);
|
||
$archive = $stmt->fetch();
|
||
if (!$archive) {
|
||
return 'unknown/archive_' . $archiveId;
|
||
}
|
||
$dirs = fetchDirectories($pdo);
|
||
$map = [];
|
||
foreach ($dirs as $d) {
|
||
$map[(int)$d['id']] = $d;
|
||
}
|
||
$parts = [];
|
||
$cur = (int)$archive['directory_id'];
|
||
while (isset($map[$cur])) {
|
||
$parts[] = safePathPart((string)$map[$cur]['name']);
|
||
$pid = $map[$cur]['parent_id'];
|
||
if ($pid === null) {
|
||
break;
|
||
}
|
||
$cur = (int)$pid;
|
||
}
|
||
$parts = array_reverse($parts);
|
||
$parts[] = 'archive_' . (int)$archive['id'] . '_' . safePathPart((string)$archive['name']);
|
||
return implode('/', $parts);
|
||
}
|
||
|
||
function maskUsername(string $username): string
|
||
{
|
||
$len = mb_strlen($username);
|
||
if ($len <= 2) {
|
||
return $username;
|
||
}
|
||
return mb_substr($username, 0, 1) . '***' . mb_substr($username, -1);
|
||
}
|
||
|
||
function roleLabel(string $role): string
|
||
{
|
||
return $role === 'admin' ? '管理员' : '普通用户';
|
||
}
|
||
|
||
function repaymentPlanStorePath(): string
|
||
{
|
||
return __DIR__ . '/../data/repayment_plans.json';
|
||
}
|
||
|
||
function loadRepaymentPlans(): array
|
||
{
|
||
$path = repaymentPlanStorePath();
|
||
if (!is_file($path)) {
|
||
return [];
|
||
}
|
||
$raw = file_get_contents($path);
|
||
if ($raw === false || $raw === '') {
|
||
return [];
|
||
}
|
||
$data = json_decode($raw, true);
|
||
return is_array($data) ? $data : [];
|
||
}
|
||
|
||
function saveRepaymentPlans(array $data): void
|
||
{
|
||
$path = repaymentPlanStorePath();
|
||
$dir = dirname($path);
|
||
if (!is_dir($dir)) {
|
||
mkdir($dir, 0775, true);
|
||
}
|
||
file_put_contents($path, json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT), LOCK_EX);
|
||
}
|
||
|
||
function getArchiveRepaymentPlan(array $all, int $archiveId): array
|
||
{
|
||
return $all[(string)$archiveId] ?? ['enabled' => false, 'plans' => []];
|
||
}
|
||
|
||
function financeStorePath(): string
|
||
{
|
||
return __DIR__ . '/../data/archive_finance.json';
|
||
}
|
||
|
||
function loadFinanceConfigs(): array
|
||
{
|
||
$path = financeStorePath();
|
||
if (!is_file($path)) {
|
||
return [];
|
||
}
|
||
$raw = file_get_contents($path);
|
||
if ($raw === false || $raw === '') {
|
||
return [];
|
||
}
|
||
$data = json_decode($raw, true);
|
||
return is_array($data) ? $data : [];
|
||
}
|
||
|
||
function saveFinanceConfigs(array $data): void
|
||
{
|
||
$path = financeStorePath();
|
||
$dir = dirname($path);
|
||
if (!is_dir($dir)) {
|
||
mkdir($dir, 0775, true);
|
||
}
|
||
file_put_contents($path, json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT), LOCK_EX);
|
||
}
|
||
|
||
function getArchiveFinanceConfig(array $all, int $archiveId): array
|
||
{
|
||
$cfg = $all[(string)$archiveId] ?? [];
|
||
return [
|
||
'interest_mode' => $cfg['interest_mode'] ?? 'monthly',
|
||
'settlement_rule' => $cfg['settlement_rule'] ?? 'monthly_day',
|
||
'settlement_day' => (int)($cfg['settlement_day'] ?? 21),
|
||
'overdue_float_pct' => (float)($cfg['overdue_float_pct'] ?? 50),
|
||
'manual_overdue' => !empty($cfg['manual_overdue']),
|
||
'manual_early_settled' => !empty($cfg['manual_early_settled']),
|
||
'remark' => (string)($cfg['remark'] ?? ''),
|
||
'manual_principal_repaid' => (float)($cfg['manual_principal_repaid'] ?? 0),
|
||
'manual_interest_paid' => (float)($cfg['manual_interest_paid'] ?? 0),
|
||
];
|
||
}
|
||
|
||
/** 计息用本金:优先剩余额度,否则授信额度 */
|
||
function effectivePrincipalForInterest(array $archive): float
|
||
{
|
||
if (isset($archive['remaining_limit']) && $archive['remaining_limit'] !== null && $archive['remaining_limit'] !== '') {
|
||
return max(0.0, (float)$archive['remaining_limit']);
|
||
}
|
||
if ($archive['credit_limit'] === null || $archive['credit_limit'] === '') {
|
||
return 0.0;
|
||
}
|
||
return max(0.0, (float)$archive['credit_limit']);
|
||
}
|
||
|
||
function archiveStatusWithConfig(?string $endDate, array $financeCfg): string
|
||
{
|
||
if (!empty($financeCfg['manual_early_settled'])) {
|
||
return '已完成';
|
||
}
|
||
if (!empty($financeCfg['manual_overdue'])) {
|
||
return '逾期中';
|
||
}
|
||
// 与 helpers::archiveStatus 一致;内联避免因 helpers 未同步部署仍按旧逻辑显示「到期自动已完成」
|
||
if (!$endDate) {
|
||
return '待更新';
|
||
}
|
||
$today = date('Y-m-d');
|
||
if ($endDate >= $today) {
|
||
return '履行中';
|
||
}
|
||
return '待更新';
|
||
}
|
||
|
||
/** 便捷查询汇总:履行中与逾期中均视为未结清存续业务 */
|
||
function archiveStatusIsOngoingAggregate(?string $endDate, array $financeCfg): bool
|
||
{
|
||
$s = archiveStatusWithConfig($endDate, $financeCfg);
|
||
return $s === '履行中' || $s === '逾期中';
|
||
}
|
||
|
||
/** 与当前结息周期对齐的区间起点(按 refDate 所在周期),用于「当前应付利息」 */
|
||
function interestPeriodStartForRefDate(array $archive, array $financeCfg, string $refDate): string
|
||
{
|
||
$startDate = $archive['start_date'] ?: $refDate;
|
||
$mode = (string)($financeCfg['interest_mode'] ?? 'monthly');
|
||
$settlementRule = (string)($financeCfg['settlement_rule'] ?? 'monthly_day');
|
||
$day = max(1, min(28, (int)($financeCfg['settlement_day'] ?? 20)));
|
||
$periodStart = $startDate;
|
||
if ($settlementRule === 'monthly_day' && $mode !== 'bullet') {
|
||
$curYm = date('Y-m', strtotime($refDate));
|
||
$thisSettle = $curYm . '-' . str_pad((string)$day, 2, '0', STR_PAD_LEFT);
|
||
if ($refDate >= $thisSettle) {
|
||
$periodStart = $thisSettle;
|
||
} else {
|
||
$periodStart = date('Y-m-d', strtotime('-1 month', strtotime($thisSettle)));
|
||
}
|
||
} elseif ($settlementRule === 'quarterly_day' && $mode !== 'bullet') {
|
||
$m = (int)date('n', strtotime($refDate));
|
||
$qStartMonth = ((int)(($m - 1) / 3)) * 3 + 1;
|
||
$curQuarterSettle = date('Y', strtotime($refDate)) . '-' . str_pad((string)$qStartMonth, 2, '0', STR_PAD_LEFT) . '-' . str_pad((string)$day, 2, '0', STR_PAD_LEFT);
|
||
if ($refDate >= $curQuarterSettle) {
|
||
$periodStart = $curQuarterSettle;
|
||
} else {
|
||
$periodStart = date('Y-m-d', strtotime('-3 month', strtotime($curQuarterSettle)));
|
||
}
|
||
}
|
||
if ($periodStart < $startDate) {
|
||
$periodStart = $startDate;
|
||
}
|
||
return $periodStart;
|
||
}
|
||
|
||
/**
|
||
* 下一结息日所在「整段结息周期」的起始日:上一结息日与起息日孰晚。
|
||
* 用于结息日应付利息(自周期起点至结息日,而非自今日至结息日)。
|
||
*/
|
||
function settlementPeriodStartForNextSettle(string $nextSettle, array $archive, array $financeCfg): string
|
||
{
|
||
$startDate = $archive['start_date'] ?: $nextSettle;
|
||
$mode = (string)($financeCfg['interest_mode'] ?? 'monthly');
|
||
$settlementRule = (string)($financeCfg['settlement_rule'] ?? 'monthly_day');
|
||
$day = max(1, min(28, (int)($financeCfg['settlement_day'] ?? 20)));
|
||
if ($settlementRule === 'maturity_once' || $mode === 'bullet') {
|
||
return $startDate;
|
||
}
|
||
$prevAnchor = $startDate;
|
||
if ($settlementRule === 'monthly_day') {
|
||
$prevAnchor = date('Y-m-d', strtotime('-1 month', strtotime($nextSettle)));
|
||
} elseif ($settlementRule === 'quarterly_day') {
|
||
$prevAnchor = date('Y-m-d', strtotime('-3 months', strtotime($nextSettle)));
|
||
}
|
||
return max($startDate, $prevAnchor);
|
||
}
|
||
|
||
/** 自 periodStart 起至 throughDate(含区间逻辑与 calcCurrentInterest 一致)的应计利息,不含「已归还利息」扣减 */
|
||
function calcAccruedInterestForRange(array $archive, array $financeCfg, array $repaymentPlans, string $periodStart, string $throughDate): float
|
||
{
|
||
$principal = effectivePrincipalForInterest($archive);
|
||
$annualRate = $archive['annual_rate'] === null ? 0.0 : (float)$archive['annual_rate'];
|
||
if ($principal <= 0 || $annualRate <= 0 || $throughDate < $periodStart) {
|
||
return 0.0;
|
||
}
|
||
|
||
$status = archiveStatusWithConfig($archive['end_date'], $financeCfg);
|
||
$overdueRate = $annualRate * (1 + ((float)($financeCfg['overdue_float_pct'] ?? 0) / 100));
|
||
$events = [];
|
||
$overduePrincipal = 0.0;
|
||
foreach ($repaymentPlans as $p) {
|
||
$amount = (float)($p['amount'] ?? 0);
|
||
if ($amount <= 0) {
|
||
continue;
|
||
}
|
||
$pStatus = (string)($p['status'] ?? 'pending');
|
||
if ($pStatus === 'paid' && !empty($p['paid_at'])) {
|
||
$paidAt = (string)$p['paid_at'];
|
||
if ($paidAt >= $periodStart && $paidAt <= $throughDate) {
|
||
$events[] = ['date' => $paidAt, 'type' => 'paid', 'amount' => $amount];
|
||
}
|
||
continue;
|
||
}
|
||
if ($pStatus === 'overdue') {
|
||
$dueDate = (string)($p['due_date'] ?? '');
|
||
if ($dueDate === '' || $dueDate > $throughDate) {
|
||
continue;
|
||
}
|
||
if ($dueDate < $periodStart) {
|
||
$overduePrincipal += $amount;
|
||
} else {
|
||
$events[] = ['date' => $dueDate, 'type' => 'overdue_start', 'amount' => $amount];
|
||
}
|
||
}
|
||
}
|
||
usort($events, function ($a, $b) {
|
||
$cmp = strcmp((string)$a['date'], (string)$b['date']);
|
||
if ($cmp !== 0) {
|
||
return $cmp;
|
||
}
|
||
$orderA = ($a['type'] ?? '') === 'overdue_start' ? 0 : 1;
|
||
$orderB = ($b['type'] ?? '') === 'overdue_start' ? 0 : 1;
|
||
return $orderA <=> $orderB;
|
||
});
|
||
|
||
$interest = 0.0;
|
||
$cursor = $periodStart;
|
||
$currentPrincipal = $principal;
|
||
foreach ($events as $ev) {
|
||
$evDate = (string)($ev['date'] ?? '');
|
||
if ($evDate > $cursor) {
|
||
$days = (strtotime($evDate) - strtotime($cursor)) / 86400;
|
||
if ($days > 0) {
|
||
$normalPrincipal = max(0, $currentPrincipal - $overduePrincipal);
|
||
$interest += $normalPrincipal * ($annualRate / 100) * ($days / 360);
|
||
$interest += $overduePrincipal * ($overdueRate / 100) * ($days / 360);
|
||
}
|
||
}
|
||
$amount = (float)($ev['amount'] ?? 0);
|
||
if (($ev['type'] ?? '') === 'overdue_start') {
|
||
$overduePrincipal = min($currentPrincipal, $overduePrincipal + $amount);
|
||
} elseif (($ev['type'] ?? '') === 'paid') {
|
||
$currentPrincipal = max(0, $currentPrincipal - $amount);
|
||
$overduePrincipal = min($overduePrincipal, $currentPrincipal);
|
||
}
|
||
$cursor = $evDate;
|
||
}
|
||
if ($throughDate > $cursor) {
|
||
$days = (strtotime($throughDate) - strtotime($cursor)) / 86400;
|
||
if ($days > 0) {
|
||
$normalPrincipal = max(0, $currentPrincipal - $overduePrincipal);
|
||
if ($overduePrincipal <= 0 && $status === '逾期中') {
|
||
$interest += $currentPrincipal * ($overdueRate / 100) * ($days / 360);
|
||
} else {
|
||
$interest += $normalPrincipal * ($annualRate / 100) * ($days / 360);
|
||
$interest += $overduePrincipal * ($overdueRate / 100) * ($days / 360);
|
||
}
|
||
}
|
||
}
|
||
return round($interest, 2);
|
||
}
|
||
|
||
function applyInterestPaidCredit(float $accrued, array $financeCfg): float
|
||
{
|
||
$paid = (float)($financeCfg['manual_interest_paid'] ?? 0);
|
||
return round(max(0.0, $accrued - $paid), 2);
|
||
}
|
||
|
||
function calcCurrentInterest(array $archive, array $financeCfg, array $repaymentPlans): float
|
||
{
|
||
$today = date('Y-m-d');
|
||
$periodStart = interestPeriodStartForRefDate($archive, $financeCfg, $today);
|
||
$raw = calcAccruedInterestForRange($archive, $financeCfg, $repaymentPlans, $periodStart, $today);
|
||
return applyInterestPaidCredit($raw, $financeCfg);
|
||
}
|
||
|
||
/** 最近一个结息日应付的整周期利息(与利息查询一致) */
|
||
function calcSettlementPeriodInterest(array $archive, array $financeCfg, array $repaymentPlans, string $todayRef): float
|
||
{
|
||
$mode = (string)($financeCfg['interest_mode'] ?? 'monthly');
|
||
$annualRate = $archive['annual_rate'] === null ? 0.0 : (float)$archive['annual_rate'];
|
||
if (effectivePrincipalForInterest($archive) <= 0 || $annualRate <= 0) {
|
||
return 0.0;
|
||
}
|
||
$nextSettle = $mode === 'bullet'
|
||
? (((string)($archive['end_date'] ?? '') !== '' && (string)$archive['end_date'] >= $todayRef) ? (string)$archive['end_date'] : null)
|
||
: nextSettlementDate($todayRef, $mode, (int)($financeCfg['settlement_day'] ?? 21));
|
||
if ($nextSettle === null || $nextSettle === '') {
|
||
return 0.0;
|
||
}
|
||
$periodStart = settlementPeriodStartForNextSettle($nextSettle, $archive, $financeCfg);
|
||
$raw = calcAccruedInterestForRange($archive, $financeCfg, $repaymentPlans, $periodStart, $nextSettle);
|
||
return applyInterestPaidCredit($raw, $financeCfg);
|
||
}
|
||
|
||
function recalcRemainingLimitByPlans(PDO $pdo, int $archiveId, array $plans): void
|
||
{
|
||
if ($archiveId <= 0) {
|
||
return;
|
||
}
|
||
$st = $pdo->prepare('SELECT credit_limit FROM archives WHERE id=:id');
|
||
$st->execute([':id' => $archiveId]);
|
||
$credit = $st->fetchColumn();
|
||
if ($credit === false || $credit === null) {
|
||
return;
|
||
}
|
||
$paidSum = 0.0;
|
||
foreach ($plans as $p) {
|
||
if ((string)($p['status'] ?? '') === 'paid') {
|
||
$paidSum += (float)($p['amount'] ?? 0);
|
||
}
|
||
}
|
||
$financeAll = loadFinanceConfigs();
|
||
$manualPrincipal = (float)(getArchiveFinanceConfig($financeAll, $archiveId)['manual_principal_repaid'] ?? 0);
|
||
$remaining = max(0, (float)$credit - $paidSum - $manualPrincipal);
|
||
$pdo->prepare('UPDATE archives SET remaining_limit=:r,updated_at=:u WHERE id=:id')
|
||
->execute([':r' => $remaining, ':u' => now(), ':id' => $archiveId]);
|
||
}
|
||
|
||
function uploadArchiveFiles(PDO $pdo, int $archiveId, array $files): void
|
||
{
|
||
if ($archiveId <= 0 || empty($files) || empty($files['name']) || !is_array($files['name'])) {
|
||
return;
|
||
}
|
||
$names = $files['name'];
|
||
$tmpNames = $files['tmp_name'] ?? [];
|
||
$sizes = $files['size'] ?? [];
|
||
$errors = $files['error'] ?? [];
|
||
|
||
$subDir = buildArchiveUploadSubdir($pdo, $archiveId);
|
||
$targetDir = rtrim(UPLOAD_DIR . '/' . $subDir, '/');
|
||
if (!is_dir($targetDir)) {
|
||
mkdir($targetDir, 0775, true);
|
||
}
|
||
for ($i = 0; $i < count($names); $i++) {
|
||
if ((int)($errors[$i] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_OK) {
|
||
continue;
|
||
}
|
||
if ((int)($sizes[$i] ?? 0) > MAX_FILE_SIZE) {
|
||
continue;
|
||
}
|
||
$original = (string)$names[$i];
|
||
$ext = strtolower(pathinfo($original, PATHINFO_EXTENSION));
|
||
if (!in_array($ext, ALLOWED_EXTENSIONS, true)) {
|
||
continue;
|
||
}
|
||
$normalized = normalizeFileName($original);
|
||
$stored = $subDir . '/' . uniqid('f_', true) . '_' . $normalized;
|
||
if (move_uploaded_file((string)$tmpNames[$i], UPLOAD_DIR . '/' . $stored)) {
|
||
$pdo->prepare('INSERT INTO files(archive_id,original_name,stored_name,file_ext,file_size,created_at) VALUES(:a,:o,:s,:e,:z,:c)')
|
||
->execute([':a' => $archiveId, ':o' => $original, ':s' => $stored, ':e' => $ext, ':z' => (int)$sizes[$i], ':c' => now()]);
|
||
}
|
||
}
|
||
}
|
||
|
||
function deleteApprovalStorePath(): string
|
||
{
|
||
return __DIR__ . '/../data/delete_approvals.json';
|
||
}
|
||
|
||
function loadDeleteApprovals(): array
|
||
{
|
||
$path = deleteApprovalStorePath();
|
||
if (!is_file($path)) {
|
||
return [];
|
||
}
|
||
$raw = file_get_contents($path);
|
||
if ($raw === false || $raw === '') {
|
||
return [];
|
||
}
|
||
$data = json_decode($raw, true);
|
||
return is_array($data) ? $data : [];
|
||
}
|
||
|
||
function saveDeleteApprovals(array $data): void
|
||
{
|
||
$path = deleteApprovalStorePath();
|
||
$dir = dirname($path);
|
||
if (!is_dir($dir)) {
|
||
mkdir($dir, 0775, true);
|
||
}
|
||
file_put_contents($path, json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT), LOCK_EX);
|
||
}
|
||
|
||
function currentUrlFromServer(): string
|
||
{
|
||
$uri = (string)($_SERVER['REQUEST_URI'] ?? '');
|
||
if ($uri === '' || strpos($uri, 'index.php') === false) {
|
||
return 'index.php';
|
||
}
|
||
return $uri;
|
||
}
|
||
|
||
function redirectBackOr(string $fallback): void
|
||
{
|
||
$posted = trim((string)($_POST['redirect_to'] ?? ''));
|
||
$query = trim((string)($_GET['redirect_to'] ?? ''));
|
||
$ref = trim((string)($_SERVER['HTTP_REFERER'] ?? ''));
|
||
$target = $posted !== '' ? $posted : ($query !== '' ? $query : $ref);
|
||
if ($target === '' || strpos($target, 'index.php') === false) {
|
||
$target = $fallback;
|
||
}
|
||
header('Location: ' . $target);
|
||
exit;
|
||
}
|
||
|
||
function fetchArchiveSimpleMap(PDO $pdo): array
|
||
{
|
||
$rows = $pdo->query('SELECT id,name,loan_institution,borrower,archive_kind FROM archives')->fetchAll();
|
||
$map = [];
|
||
foreach ($rows as $r) {
|
||
$id = (int)($r['id'] ?? 0);
|
||
if ($id <= 0) {
|
||
continue;
|
||
}
|
||
$map[$id] = [
|
||
'name' => (string)($r['name'] ?? ''),
|
||
'loan_institution' => (string)($r['loan_institution'] ?? ''),
|
||
'borrower' => (string)($r['borrower'] ?? ''),
|
||
'archive_kind' => normalizeArchiveKind((string)($r['archive_kind'] ?? '')),
|
||
];
|
||
}
|
||
return $map;
|
||
}
|
||
|
||
function approvalTargetLabel(array $ap, array $archiveMap): string
|
||
{
|
||
$type = (string)($ap['target_type'] ?? '');
|
||
if ($type === 'archive_batch') {
|
||
$ids = array_values(array_unique(array_filter(array_map('intval', (array)($ap['target_ids'] ?? [])))));
|
||
$labels = [];
|
||
foreach (array_slice($ids, 0, 5) as $aid) {
|
||
if (!isset($archiveMap[$aid])) {
|
||
$labels[] = 'ID:' . $aid . '(已删除)';
|
||
continue;
|
||
}
|
||
$arc = $archiveMap[$aid];
|
||
$labels[] = '[' . archiveKindLabel((string)$arc['archive_kind']) . '] ' . (string)$arc['name'];
|
||
}
|
||
$more = count($ids) > 5 ? ' 等' . count($ids) . '条' : '';
|
||
return '批量' . (int)($ap['target_count'] ?? count($ids)) . '条:' . implode(';', $labels) . $more;
|
||
}
|
||
$aid = (int)($ap['target_id'] ?? 0);
|
||
if ($aid <= 0) {
|
||
return 'ID:0';
|
||
}
|
||
if (!isset($archiveMap[$aid])) {
|
||
return 'ID:' . $aid . '(已删除/不存在)';
|
||
}
|
||
$arc = $archiveMap[$aid];
|
||
$name = (string)$arc['name'];
|
||
$inst = (string)$arc['loan_institution'];
|
||
$borrower = (string)$arc['borrower'];
|
||
return 'ID:' . $aid . ' [' . archiveKindLabel((string)$arc['archive_kind']) . '] ' . $name . ' / ' . $inst . ' / ' . $borrower;
|
||
}
|
||
|
||
function nextSettlementDate(string $today, string $mode, int $day): ?string
|
||
{
|
||
if ($mode === 'bullet') {
|
||
return null;
|
||
}
|
||
$day = max(1, min(28, $day));
|
||
if ($mode === 'quarterly') {
|
||
$m = (int)date('n', strtotime($today));
|
||
$qStartMonth = ((int)(($m - 1) / 3)) * 3 + 1;
|
||
$cand = date('Y', strtotime($today)) . '-' . str_pad((string)$qStartMonth, 2, '0', STR_PAD_LEFT) . '-' . str_pad((string)$day, 2, '0', STR_PAD_LEFT);
|
||
if ($cand <= $today) {
|
||
$cand = date('Y-m-d', strtotime('+3 month', strtotime($cand)));
|
||
}
|
||
return $cand;
|
||
}
|
||
$curYm = date('Y-m', strtotime($today));
|
||
$cand = $curYm . '-' . str_pad((string)$day, 2, '0', STR_PAD_LEFT);
|
||
if ($cand <= $today) {
|
||
$cand = date('Y-m-d', strtotime('+1 month', strtotime($cand)));
|
||
}
|
||
return $cand;
|
||
}
|
||
|
||
function ensureDirectoryPath(PDO $pdo, string $lv1, string $lv2, string $lv3): int
|
||
{
|
||
$clean = function (string $v): string {
|
||
$v = str_replace("\xC2\xA0", ' ', $v);
|
||
$v = preg_replace('/\s+/u', ' ', trim($v));
|
||
return (string)$v;
|
||
};
|
||
$lv1 = $clean($lv1);
|
||
$lv2 = $clean($lv2);
|
||
$lv3 = $clean($lv3);
|
||
|
||
$findOrCreate = function (?int $parentId, int $level, string $name) use ($pdo): int {
|
||
if ($parentId === null) {
|
||
$q = $pdo->prepare('SELECT id FROM directories WHERE name=:n AND level=:l AND parent_id IS NULL');
|
||
$q->execute([':n' => $name, ':l' => $level]);
|
||
} else {
|
||
$q = $pdo->prepare('SELECT id FROM directories WHERE name=:n AND level=:l AND parent_id=:p');
|
||
$q->execute([':n' => $name, ':l' => $level, ':p' => $parentId]);
|
||
}
|
||
$id = (int)$q->fetchColumn();
|
||
if ($id > 0) {
|
||
return $id;
|
||
}
|
||
$ins = $pdo->prepare('INSERT INTO directories(name,parent_id,level,created_at) VALUES(:n,:p,:l,:c)');
|
||
$ins->bindValue(':n', $name);
|
||
$ins->bindValue(':p', $parentId, $parentId === null ? PDO::PARAM_NULL : PDO::PARAM_INT);
|
||
$ins->bindValue(':l', $level, PDO::PARAM_INT);
|
||
$ins->bindValue(':c', now());
|
||
$ins->execute();
|
||
return (int)$pdo->lastInsertId();
|
||
};
|
||
$id1 = $findOrCreate(null, 1, $lv1);
|
||
$id2 = $findOrCreate($id1, 2, $lv2);
|
||
return $findOrCreate($id2, 3, $lv3);
|
||
}
|
||
|
||
function hardDeleteArchive(PDO $pdo, int $archiveId): void
|
||
{
|
||
$stmt = $pdo->prepare('SELECT stored_name FROM files WHERE archive_id=:a');
|
||
$stmt->execute([':a' => $archiveId]);
|
||
foreach ($stmt->fetchAll() as $f) {
|
||
$filePath = UPLOAD_DIR . '/' . $f['stored_name'];
|
||
if (is_file($filePath)) {
|
||
unlink($filePath);
|
||
}
|
||
}
|
||
$pdo->prepare('DELETE FROM archives WHERE id=:id')->execute([':id' => $archiveId]);
|
||
}
|
||
|
||
function loanInstitutionStorePath(): string
|
||
{
|
||
return __DIR__ . '/../data/loan_institutions.json';
|
||
}
|
||
|
||
function defaultLoanInstitutions(): array
|
||
{
|
||
return [
|
||
'河南农商银行登封支行',
|
||
'郑州银行登封支行',
|
||
'浦发银行郑州金水支行',
|
||
'中国银行登封支行',
|
||
'建设银行登封支行',
|
||
'邮储银行登封支行',
|
||
'河南农商银行郑州关虎屯支行',
|
||
'中信银行登封支行',
|
||
];
|
||
}
|
||
|
||
function loadLoanInstitutions(): array
|
||
{
|
||
$path = loanInstitutionStorePath();
|
||
if (!is_file($path)) {
|
||
return defaultLoanInstitutions();
|
||
}
|
||
$raw = file_get_contents($path);
|
||
if ($raw === false || $raw === '') {
|
||
return defaultLoanInstitutions();
|
||
}
|
||
$arr = json_decode($raw, true);
|
||
if (!is_array($arr)) {
|
||
return defaultLoanInstitutions();
|
||
}
|
||
$out = [];
|
||
foreach ($arr as $x) {
|
||
$v = trim((string)$x);
|
||
if ($v !== '') {
|
||
$out[] = $v;
|
||
}
|
||
}
|
||
return $out ?: defaultLoanInstitutions();
|
||
}
|
||
|
||
function saveLoanInstitutions(array $items): void
|
||
{
|
||
$out = [];
|
||
foreach ($items as $x) {
|
||
$v = trim((string)$x);
|
||
if ($v !== '') {
|
||
$out[] = $v;
|
||
}
|
||
}
|
||
$out = array_values(array_unique($out));
|
||
if (empty($out)) {
|
||
$out = defaultLoanInstitutions();
|
||
}
|
||
$path = loanInstitutionStorePath();
|
||
$dir = dirname($path);
|
||
if (!is_dir($dir)) {
|
||
mkdir($dir, 0775, true);
|
||
}
|
||
file_put_contents($path, json_encode($out, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT), LOCK_EX);
|
||
}
|
||
|
||
function autoArchiveName(string $borrower, ?float $creditLimit): string
|
||
{
|
||
$b = trim($borrower);
|
||
if ($b === '') {
|
||
return '';
|
||
}
|
||
$wan = $creditLimit === null ? '' : rtrim(rtrim(number_format($creditLimit / 10000, 2, '.', ''), '0'), '.');
|
||
return $wan === '' ? $b : ($b . $wan . '万元');
|
||
}
|
||
|
||
/** 对外担保:担保人 +「对外担保」+ 授信/10000 +「万元」 */
|
||
function autoExternalGuaranteeArchiveName(string $guarantor, ?float $creditLimit): string
|
||
{
|
||
$g = trim($guarantor);
|
||
if ($g === '') {
|
||
return '';
|
||
}
|
||
$wan = $creditLimit === null ? '' : rtrim(rtrim(number_format($creditLimit / 10000, 2, '.', ''), '0'), '.');
|
||
return $wan === '' ? '' : ($g . '对外担保' . $wan . '万元');
|
||
}
|
||
|
||
function ensureAutoBankLoanDirectory(PDO $pdo, ?string $startDate, string $loanInstitution): int
|
||
{
|
||
$year = $startDate ? date('Y', strtotime((string) $startDate)) : '';
|
||
$bank = trim($loanInstitution);
|
||
if ($year === '' || $bank === '') {
|
||
return 0;
|
||
}
|
||
return ensureDirectoryPath($pdo, '银行贷款', $year, $bank);
|
||
}
|
||
|
||
function ensureAutoExternalGuaranteeDirectory(PDO $pdo, ?string $startDate, string $guarantor): int
|
||
{
|
||
$year = $startDate ? date('Y', strtotime((string) $startDate)) : '';
|
||
$g = trim($guarantor);
|
||
if ($year === '' || $g === '') {
|
||
return 0;
|
||
}
|
||
return ensureDirectoryPath($pdo, '对外担保', $year, $g);
|
||
}
|
||
|
||
function collectAncestorIds(array $dirs, ?int $selectedId): array
|
||
{
|
||
if (!$selectedId) {
|
||
return [];
|
||
}
|
||
$parentMap = [];
|
||
foreach ($dirs as $d) {
|
||
$parentMap[(int)$d['id']] = $d['parent_id'] === null ? null : (int)$d['parent_id'];
|
||
}
|
||
$ids = [$selectedId];
|
||
$cur = $selectedId;
|
||
while (isset($parentMap[$cur]) && $parentMap[$cur] !== null) {
|
||
$cur = (int)$parentMap[$cur];
|
||
$ids[] = $cur;
|
||
}
|
||
return array_values(array_unique($ids));
|
||
}
|
||
|
||
/** 二级目录排序:从名称中提取年份(如 2025、2025年)升序;无年份则按名称排在后段。 */
|
||
function directoryLevel2TimeSortKey(string $name): int
|
||
{
|
||
$name = trim($name);
|
||
if ($name === '') {
|
||
return 9999;
|
||
}
|
||
if (preg_match('/^(\d{4})(?:\b|年)/u', $name, $m)) {
|
||
return (int) $m[1];
|
||
}
|
||
if (preg_match('/(\d{4})/u', $name, $m)) {
|
||
return (int) $m[1];
|
||
}
|
||
return 9999;
|
||
}
|
||
|
||
function buildTreeHtml(array $dirs, ?int $selectedId, bool $admin, array $pathIds): string
|
||
{
|
||
$byParent = [];
|
||
$dirMap = [];
|
||
foreach ($dirs as $d) {
|
||
$pid = $d['parent_id'] === null ? 0 : (int) $d['parent_id'];
|
||
$byParent[$pid][] = $d;
|
||
$dirMap[(int)$d['id']] = $d;
|
||
}
|
||
foreach ($byParent as &$children) {
|
||
if ($children === []) {
|
||
continue;
|
||
}
|
||
if ((int) ($children[0]['level'] ?? 0) === 2) {
|
||
usort($children, static function (array $a, array $b): int {
|
||
$ka = directoryLevel2TimeSortKey((string) $a['name']);
|
||
$kb = directoryLevel2TimeSortKey((string) $b['name']);
|
||
if ($ka !== $kb) {
|
||
return $ka <=> $kb;
|
||
}
|
||
return strcmp((string) $a['name'], (string) $b['name']);
|
||
});
|
||
}
|
||
}
|
||
unset($children);
|
||
$pathSet = array_flip($pathIds);
|
||
$rootNameById = [];
|
||
foreach ($dirMap as $id => $d) {
|
||
$cur = $id;
|
||
$rootName = (string)$d['name'];
|
||
while (isset($dirMap[$cur]) && $dirMap[$cur]['parent_id'] !== null) {
|
||
$cur = (int)$dirMap[$cur]['parent_id'];
|
||
if (!isset($dirMap[$cur])) {
|
||
break;
|
||
}
|
||
$rootName = (string)$dirMap[$cur]['name'];
|
||
}
|
||
$rootNameById[$id] = $rootName;
|
||
}
|
||
$render = function ($parent) use (&$render, $byParent, $selectedId, $admin, $pathSet, $rootNameById): string {
|
||
if (empty($byParent[$parent])) {
|
||
return '';
|
||
}
|
||
$html = '<ul>';
|
||
foreach ($byParent[$parent] as $d) {
|
||
$id = (int)$d['id'];
|
||
$name = e($d['name']);
|
||
$hasChildren = !empty($byParent[$id]);
|
||
$isCurrent = $selectedId === $id;
|
||
$isAncestor = isset($pathSet[$id]);
|
||
$linkClass = 'tree-link' . ($isCurrent ? ' current' : ($isAncestor ? ' ancestor' : ''));
|
||
$actions = '';
|
||
$kindQ = '';
|
||
$rootName = (string)($rootNameById[$id] ?? '');
|
||
if ($rootName === '银行贷款') {
|
||
$kindQ = '&kind=' . ARCHIVE_KIND_BANK_LOAN;
|
||
} elseif ($rootName === '对外担保') {
|
||
$kindQ = '&kind=' . ARCHIVE_KIND_EXTERNAL_GUARANTEE;
|
||
}
|
||
if ($admin) {
|
||
$actions .= '<span class="tree-actions">';
|
||
$actions .= '<a class="tree-btn edit" title="编辑目录" href="index.php?action=edit_dir&id=' . $id . '">改名</a>';
|
||
if ((int)$d['level'] < 3) {
|
||
$actions .= '<a class="tree-btn add" title="新增子目录" href="index.php?action=new_dir&parent_id=' . $id . '">新增</a>';
|
||
}
|
||
$actions .= '<a class="tree-btn del" title="删除目录" href="index.php?action=delete_dir&id=' . $id . '" onclick="return confirm(\'仅空目录可删除,确认?\')">删除</a>';
|
||
$actions .= '</span>';
|
||
}
|
||
$html .= '<li>';
|
||
if ($hasChildren) {
|
||
$open = $isAncestor ? ' open' : '';
|
||
$html .= '<details' . $open . '><summary><div class="tree-item"><span class="tree-toggle">▶</span><a class="' . $linkClass . '" href="index.php?dir_id=' . $id . $kindQ . '" title="' . $name . '">' . $name . '</a>' . $actions . '</div></summary>';
|
||
} else {
|
||
$html .= '<div class="tree-item"><span class="tree-toggle"></span><a class="' . $linkClass . '" href="index.php?dir_id=' . $id . $kindQ . '" title="' . $name . '">' . $name . '</a>' . $actions . '</div>';
|
||
}
|
||
$html .= $render($id);
|
||
if ($hasChildren) {
|
||
$html .= '</details>';
|
||
}
|
||
$html .= '</li>';
|
||
}
|
||
$html .= '</ul>';
|
||
return $html;
|
||
};
|
||
return $render(0);
|
||
}
|
||
|
||
/** 便捷查询侧栏 / 便捷查询页:折叠菜单内链接 */
|
||
function quickQueryMenuHtml(): string
|
||
{
|
||
$links = [
|
||
['query_payable_interest', '应付本息查询'],
|
||
['query_external_guarantee', '对外担保查询'],
|
||
['query_totals', '查询总金额'],
|
||
['interest_calculator', '利息计算器'],
|
||
['acceptance_discount_calculator', '承兑贴现计算器'],
|
||
['repayment_history', '历史还款记录查询'],
|
||
];
|
||
$inner = '';
|
||
foreach ($links as $pair) {
|
||
$inner .= '<a class="tree-link" href="index.php?action=' . e($pair[0]) . '">' . e($pair[1]) . '</a>';
|
||
}
|
||
return '<details class="quick-menu-details">'
|
||
. '<summary class="quick-menu-summary"><span>便捷查询</span><span class="quick-menu-hint">点击展开</span></summary>'
|
||
. '<div class="quick-links-col">' . $inner . '</div>'
|
||
. '</details>';
|
||
}
|
||
|
||
if ($action === 'new_dir') {
|
||
requireAdmin();
|
||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||
$name = trim((string)($_POST['name'] ?? ''));
|
||
$parentId = (int)($_POST['parent_id'] ?? 0);
|
||
if ($name === '') {
|
||
flash('error', '目录名称不能为空');
|
||
header('Location: index.php');
|
||
exit;
|
||
}
|
||
$level = 1;
|
||
$parentValue = null;
|
||
if ($parentId > 0) {
|
||
$stmt = $pdo->prepare('SELECT * FROM directories WHERE id=:id');
|
||
$stmt->execute([':id' => $parentId]);
|
||
$p = $stmt->fetch();
|
||
if (!$p) {
|
||
flash('error', '父目录不存在');
|
||
header('Location: index.php');
|
||
exit;
|
||
}
|
||
$level = (int)$p['level'] + 1;
|
||
$parentValue = $parentId;
|
||
if ($level > 3) {
|
||
flash('error', '最多只支持三级目录');
|
||
header('Location: index.php?dir_id=' . $parentId);
|
||
exit;
|
||
}
|
||
}
|
||
$stmt = $pdo->prepare('INSERT INTO directories(name,parent_id,level,created_at) VALUES(:n,:p,:l,:c)');
|
||
$stmt->bindValue(':n', $name);
|
||
$stmt->bindValue(':p', $parentValue, $parentValue === null ? PDO::PARAM_NULL : PDO::PARAM_INT);
|
||
$stmt->bindValue(':l', $level, PDO::PARAM_INT);
|
||
$stmt->bindValue(':c', now());
|
||
$stmt->execute();
|
||
flash('ok', '目录已创建');
|
||
header('Location: index.php' . ($parentId ? '?dir_id=' . $parentId : ''));
|
||
exit;
|
||
}
|
||
$parentId = (int)($_GET['parent_id'] ?? 0);
|
||
pageHeader($me); ?>
|
||
<div class="page-head">
|
||
<div>
|
||
<h1 class="page-title">新建目录</h1>
|
||
<div class="page-sub">最多三级;仅新建与命名,不新增停用或排序</div>
|
||
</div>
|
||
</div>
|
||
<div class="card" style="max-width:480px">
|
||
<form method="post">
|
||
<input type="hidden" name="parent_id" value="<?= $parentId ?>">
|
||
<div class="form-field-stack">
|
||
<label for="dir_name">目录名称</label>
|
||
<input id="dir_name" name="name" placeholder="目录名称" required>
|
||
</div>
|
||
<div class="form-foot">
|
||
<a class="btn btn--plain" href="index.php<?= $parentId ? ('?dir_id=' . $parentId) : '?action=home&view=dirs' ?>">返回</a>
|
||
<span class="spacer"></span>
|
||
<button type="submit" class="btn btn--primary">保存</button>
|
||
</div>
|
||
</form>
|
||
</div>
|
||
<?php pageFooter(); exit;
|
||
}
|
||
|
||
if ($action === 'edit_dir') {
|
||
requireAdmin();
|
||
$id = (int)($_GET['id'] ?? 0);
|
||
$stmt = $pdo->prepare('SELECT * FROM directories WHERE id=:id');
|
||
$stmt->execute([':id' => $id]);
|
||
$d = $stmt->fetch();
|
||
if (!$d) {
|
||
flash('error', '目录不存在');
|
||
header('Location: index.php');
|
||
exit;
|
||
}
|
||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||
$name = trim((string)($_POST['name'] ?? ''));
|
||
if ($name === '') {
|
||
flash('error', '目录名称不能为空');
|
||
} else {
|
||
$up = $pdo->prepare('UPDATE directories SET name=:n WHERE id=:id');
|
||
$up->execute([':n' => $name, ':id' => $id]);
|
||
flash('ok', '目录已更新');
|
||
header('Location: index.php?dir_id=' . $id);
|
||
exit;
|
||
}
|
||
}
|
||
pageHeader($me); ?>
|
||
<div class="page-head">
|
||
<div>
|
||
<h1 class="page-title">编辑目录</h1>
|
||
<div class="page-sub">仅改名称;不新增停用或排序</div>
|
||
</div>
|
||
</div>
|
||
<div class="card" style="max-width:480px">
|
||
<form method="post">
|
||
<div class="form-field-stack">
|
||
<label for="dir_name">目录名称</label>
|
||
<input id="dir_name" name="name" value="<?= e($d['name']) ?>" required>
|
||
</div>
|
||
<div class="form-foot">
|
||
<a class="btn btn--plain" href="index.php?dir_id=<?= $id ?>">返回</a>
|
||
<span class="spacer"></span>
|
||
<button type="submit" class="btn btn--primary">保存</button>
|
||
</div>
|
||
</form>
|
||
</div>
|
||
<?php pageFooter(); exit;
|
||
}
|
||
|
||
if ($action === 'delete_dir') {
|
||
requireAdmin();
|
||
$id = (int)($_GET['id'] ?? 0);
|
||
$c1 = $pdo->prepare('SELECT COUNT(*) FROM directories WHERE parent_id=:id');
|
||
$c1->execute([':id' => $id]);
|
||
$c2 = $pdo->prepare('SELECT COUNT(*) FROM archives WHERE directory_id=:id');
|
||
$c2->execute([':id' => $id]);
|
||
if ((int)$c1->fetchColumn() > 0 || (int)$c2->fetchColumn() > 0) {
|
||
flash('error', '目录非空,不能删除');
|
||
header('Location: index.php?dir_id=' . $id);
|
||
exit;
|
||
}
|
||
$del = $pdo->prepare('DELETE FROM directories WHERE id=:id');
|
||
$del->execute([':id' => $id]);
|
||
flash('ok', '目录已删除');
|
||
header('Location: index.php');
|
||
exit;
|
||
}
|
||
|
||
if ($action === 'save_archive') {
|
||
requireAdmin();
|
||
$id = (int)($_POST['id'] ?? 0);
|
||
$directoryId = (int)($_POST['directory_id'] ?? 0);
|
||
$archiveKind = normalizeArchiveKind((string)($_POST['archive_kind'] ?? ''));
|
||
$loanInstitution = trim((string)($_POST['loan_institution'] ?? ''));
|
||
$borrower = trim((string)($_POST['borrower'] ?? ''));
|
||
$guarantorEarly = trim((string)($_POST['guarantor'] ?? ''));
|
||
$creditLimitVal = ($_POST['credit_limit'] ?? '') === '' ? null : (float)$_POST['credit_limit'];
|
||
$startDateVal = $_POST['start_date'] ?: null;
|
||
if ($archiveKind === ARCHIVE_KIND_EXTERNAL_GUARANTEE) {
|
||
$name = autoExternalGuaranteeArchiveName($guarantorEarly, $creditLimitVal);
|
||
if ($name === '') {
|
||
flash('error', '对外担保需填写担保人、授信额度(用于自动生成档案名称)');
|
||
header('Location: index.php');
|
||
exit;
|
||
}
|
||
} else {
|
||
$name = autoArchiveName($borrower, $creditLimitVal);
|
||
if ($name === '') {
|
||
flash('error', '借款主体与授信额度必填(用于自动生成档案名称)');
|
||
header('Location: index.php');
|
||
exit;
|
||
}
|
||
}
|
||
if (isset($_POST['loan_institutions_json'])) {
|
||
$json = json_decode((string)$_POST['loan_institutions_json'], true);
|
||
if (is_array($json)) {
|
||
saveLoanInstitutions($json);
|
||
}
|
||
}
|
||
if ($directoryId > 0) {
|
||
$check = $pdo->prepare('SELECT level FROM directories WHERE id=:id');
|
||
$check->execute([':id' => $directoryId]);
|
||
$lv = (int)$check->fetchColumn();
|
||
if ($lv !== 3) {
|
||
flash('error', '手动选择目录时必须是三级目录');
|
||
header('Location: index.php?dir_id=' . $directoryId);
|
||
exit;
|
||
}
|
||
} else {
|
||
if ($archiveKind === ARCHIVE_KIND_EXTERNAL_GUARANTEE) {
|
||
$directoryId = ensureAutoExternalGuaranteeDirectory($pdo, $startDateVal, $guarantorEarly);
|
||
if ($directoryId <= 0) {
|
||
flash('error', '未选择三级目录时,对外担保需填写起始日与担保人,系统将自动创建目录:对外担保 / 年份 / 担保人');
|
||
header('Location: index.php');
|
||
exit;
|
||
}
|
||
} else {
|
||
$directoryId = ensureAutoBankLoanDirectory($pdo, $startDateVal, $loanInstitution);
|
||
if ($directoryId <= 0) {
|
||
flash('error', '未选择三级目录时,银行贷款需填写贷款机构与起始日,系统将自动创建目录:银行贷款 / 年份 / 贷款机构');
|
||
header('Location: index.php');
|
||
exit;
|
||
}
|
||
}
|
||
}
|
||
$planEnabled = ($_POST['plan_enabled'] ?? '0') === '1';
|
||
$allPlans = loadRepaymentPlans();
|
||
$existing = $id > 0 ? getArchiveRepaymentPlan($allPlans, $id) : ['enabled' => false, 'plans' => []];
|
||
$existingMap = [];
|
||
foreach (($existing['plans'] ?? []) as $ep) {
|
||
if (!empty($ep['id'])) {
|
||
$existingMap[(string)$ep['id']] = $ep;
|
||
}
|
||
}
|
||
$dueDates = (array)($_POST['plan_due_date'] ?? []);
|
||
$amounts = (array)($_POST['plan_amount'] ?? []);
|
||
$planIds = (array)($_POST['plan_id'] ?? []);
|
||
$planStatuses = (array)($_POST['plan_status'] ?? []);
|
||
$delayDates = (array)($_POST['plan_delay_date'] ?? []);
|
||
$plans = [];
|
||
$paidSum = 0.0;
|
||
if ($planEnabled) {
|
||
$n = max(count($dueDates), count($amounts), count($planStatuses));
|
||
for ($i = 0; $i < $n; $i++) {
|
||
$due = trim((string)($dueDates[$i] ?? ''));
|
||
$amtRaw = trim((string)($amounts[$i] ?? ''));
|
||
if ($due === '' || $amtRaw === '') {
|
||
continue;
|
||
}
|
||
$amt = (float)$amtRaw;
|
||
if ($amt <= 0) {
|
||
continue;
|
||
}
|
||
$pid = trim((string)($planIds[$i] ?? ''));
|
||
$status = (string)($planStatuses[$i] ?? 'pending');
|
||
if (!in_array($status, ['pending', 'paid', 'delayed', 'overdue'], true)) {
|
||
$status = 'pending';
|
||
}
|
||
$today = date('Y-m-d');
|
||
if (($status === 'delayed' || $status === 'overdue') && $due > $today) {
|
||
$status = 'pending';
|
||
}
|
||
$delayDate = trim((string)($delayDates[$i] ?? ''));
|
||
if ($status === 'delayed' && $delayDate !== '') {
|
||
$due = $delayDate;
|
||
}
|
||
$old = $existingMap[$pid] ?? null;
|
||
$paidAt = null;
|
||
if ($status === 'paid') {
|
||
$paidAt = ($old && ($old['status'] ?? '') === 'paid' && !empty($old['paid_at'])) ? $old['paid_at'] : date('Y-m-d');
|
||
$paidSum += $amt;
|
||
}
|
||
$plans[] = [
|
||
'id' => $pid !== '' ? $pid : uniqid('rp_', true),
|
||
'due_date' => $due,
|
||
'amount' => $amt,
|
||
'status' => $status,
|
||
'paid_at' => $paidAt,
|
||
'updated_at' => now(),
|
||
];
|
||
}
|
||
}
|
||
|
||
$financeAllWork = loadFinanceConfigs();
|
||
$rawPrevFinance = $id > 0 ? ($financeAllWork[(string)$id] ?? []) : [];
|
||
$prevManualPrincipal = (float)($rawPrevFinance['manual_principal_repaid'] ?? 0);
|
||
$prevManualInterest = (float)($rawPrevFinance['manual_interest_paid'] ?? 0);
|
||
$addPrincipal = max(0, (float)($_POST['principal_repayment_now'] ?? 0));
|
||
$addInterest = max(0, (float)($_POST['interest_repayment_now'] ?? 0));
|
||
if ($creditLimitVal !== null) {
|
||
$repayCap = max(0, $creditLimitVal - $paidSum - $prevManualPrincipal);
|
||
if ($addPrincipal > $repayCap + 1e-6) {
|
||
flash('error', '本次归还本金超过可还金额(授信 − 分期已还 − 已累计归还本金)');
|
||
header('Location: index.php?action=archive_form' . ($id > 0 ? ('&id=' . $id) : ''));
|
||
exit;
|
||
}
|
||
}
|
||
|
||
$oldEndDate = '';
|
||
if ($id > 0) {
|
||
$stEnd = $pdo->prepare('SELECT end_date FROM archives WHERE id=:id');
|
||
$stEnd->execute([':id' => $id]);
|
||
$oldEndRow = $stEnd->fetchColumn();
|
||
if ($oldEndRow !== false && $oldEndRow !== null) {
|
||
$oldEndDate = (string)$oldEndRow;
|
||
}
|
||
}
|
||
$loanExtendTo = trim((string)($_POST['loan_extend_to'] ?? ''));
|
||
$endDatePosted = ($_POST['end_date'] ?? '') !== '' ? (string)$_POST['end_date'] : null;
|
||
$endDateFinal = $endDatePosted;
|
||
if ($loanExtendTo !== '') {
|
||
if ($startDateVal && $loanExtendTo < $startDateVal) {
|
||
flash('error', '延期后的到期日不能早于起始日');
|
||
header('Location: index.php?action=archive_form' . ($id > 0 ? ('&id=' . $id) : ''));
|
||
exit;
|
||
}
|
||
if ($oldEndDate !== '' && $loanExtendTo < $oldEndDate) {
|
||
flash('error', '延期后的到期日不能早于当前到期日');
|
||
header('Location: index.php?action=archive_form' . ($id > 0 ? ('&id=' . $id) : ''));
|
||
exit;
|
||
}
|
||
$endDateFinal = $loanExtendTo;
|
||
}
|
||
|
||
$manualPrincipalTotal = $prevManualPrincipal + $addPrincipal;
|
||
$manualInterestTotal = $prevManualInterest + $addInterest;
|
||
$remainingAuto = $creditLimitVal === null ? null : max(0, $creditLimitVal - $paidSum - $manualPrincipalTotal);
|
||
|
||
$payload = [
|
||
':name' => $name,
|
||
':directory_id' => $directoryId,
|
||
':loan_institution' => $loanInstitution,
|
||
':borrower' => $borrower,
|
||
':credit_limit' => $creditLimitVal,
|
||
':remaining_limit' => $remainingAuto,
|
||
':start_date' => $startDateVal,
|
||
':end_date' => $endDateFinal,
|
||
':annual_rate' => ($_POST['annual_rate'] ?? '') === '' ? null : (float)$_POST['annual_rate'],
|
||
':guarantee_type' => trim((string)($_POST['guarantee_type'] ?? '')),
|
||
':guarantor' => $guarantorEarly,
|
||
':archive_kind' => $archiveKind,
|
||
':updated_at' => now(),
|
||
];
|
||
if ($id > 0) {
|
||
$sql = 'UPDATE archives SET name=:name,directory_id=:directory_id,archive_kind=:archive_kind,loan_institution=:loan_institution,borrower=:borrower,credit_limit=:credit_limit,remaining_limit=:remaining_limit,start_date=:start_date,end_date=:end_date,annual_rate=:annual_rate,guarantee_type=:guarantee_type,guarantor=:guarantor,updated_at=:updated_at WHERE id=:id';
|
||
$payload[':id'] = $id;
|
||
$pdo->prepare($sql)->execute($payload);
|
||
$archiveId = $id;
|
||
flash('ok', '档案已更新(' . date('H:i:s') . ')');
|
||
} else {
|
||
$sql = 'INSERT INTO archives(name,directory_id,archive_kind,loan_institution,borrower,credit_limit,remaining_limit,start_date,end_date,annual_rate,guarantee_type,guarantor,created_at,updated_at) VALUES(:name,:directory_id,:archive_kind,:loan_institution,:borrower,:credit_limit,:remaining_limit,:start_date,:end_date,:annual_rate,:guarantee_type,:guarantor,:created_at,:updated_at)';
|
||
$payload[':created_at'] = now();
|
||
$pdo->prepare($sql)->execute($payload);
|
||
$archiveId = (int)$pdo->lastInsertId();
|
||
flash('ok', '档案已创建(' . date('H:i:s') . ')');
|
||
}
|
||
|
||
if ($planEnabled) {
|
||
$allPlans[(string)$archiveId] = ['enabled' => true, 'plans' => $plans];
|
||
} else {
|
||
$allPlans[(string)$archiveId] = ['enabled' => false, 'plans' => []];
|
||
}
|
||
saveRepaymentPlans($allPlans);
|
||
|
||
$financeAll = loadFinanceConfigs();
|
||
$interestMode = (string)($_POST['interest_mode'] ?? 'monthly');
|
||
if (!in_array($interestMode, ['monthly', 'quarterly', 'bullet'], true)) {
|
||
$interestMode = 'monthly';
|
||
}
|
||
$settlementRule = $interestMode === 'quarterly' ? 'quarterly_day' : ($interestMode === 'bullet' ? 'maturity_once' : 'monthly_day');
|
||
$settlementDay = $interestMode === 'bullet' ? 0 : max(1, min(28, (int)($_POST['settlement_day'] ?? 21)));
|
||
$todaySave = date('Y-m-d');
|
||
$endDatePosted = $endDateFinal !== null && $endDateFinal !== '' ? (string)$endDateFinal : '';
|
||
$postedEarlyRaw = $_POST['is_early_settled'] ?? null;
|
||
$prevFinance = getArchiveFinanceConfig($financeAll, $archiveId);
|
||
if ($endDatePosted !== '' && $endDatePosted < $todaySave) {
|
||
// 已过到期日:禁止在编辑页首次标「已完成」(须走「待更新」);已是已完成的保留,除非表单显式提交为否
|
||
if (!empty($prevFinance['manual_early_settled'])) {
|
||
$manualEarlySettled = ($postedEarlyRaw === null) ? true : ($postedEarlyRaw === '1');
|
||
} else {
|
||
$manualEarlySettled = false;
|
||
}
|
||
} else {
|
||
$manualEarlySettled = $endDatePosted !== '' && ($postedEarlyRaw === '1');
|
||
}
|
||
$manualOverdue = (($_POST['is_overdue'] ?? '0') === '1');
|
||
if ($manualEarlySettled) {
|
||
$manualOverdue = false;
|
||
}
|
||
$remarkRaw = trim((string)($_POST['archive_remark'] ?? ''));
|
||
if (function_exists('mb_substr')) {
|
||
$remarkRaw = mb_substr($remarkRaw, 0, 4000, 'UTF-8');
|
||
} elseif (strlen($remarkRaw) > 4000) {
|
||
$remarkRaw = substr($remarkRaw, 0, 4000);
|
||
}
|
||
$financeMergeBase = $financeAll[(string)$archiveId] ?? [];
|
||
$financeAll[(string)$archiveId] = array_merge($financeMergeBase, [
|
||
'interest_mode' => $interestMode,
|
||
'settlement_rule' => $settlementRule,
|
||
'settlement_day' => $settlementDay,
|
||
'overdue_float_pct' => max(0, (float)($_POST['overdue_float_pct'] ?? 50)),
|
||
'manual_overdue' => $manualOverdue,
|
||
'manual_early_settled' => $manualEarlySettled,
|
||
'remark' => $remarkRaw,
|
||
'manual_principal_repaid' => $manualPrincipalTotal,
|
||
'manual_interest_paid' => $manualInterestTotal,
|
||
'updated_at' => now(),
|
||
]);
|
||
saveFinanceConfigs($financeAll);
|
||
uploadArchiveFiles($pdo, $archiveId, $_FILES['new_files'] ?? []);
|
||
header('Location: index.php?action=archive_form&id=' . $archiveId);
|
||
exit;
|
||
}
|
||
|
||
if ($action === 'reminder_action') {
|
||
requireAdmin();
|
||
$op = (string)($_POST['op'] ?? $_GET['op'] ?? '');
|
||
$archiveId = (int)($_POST['archive_id'] ?? $_GET['archive_id'] ?? 0);
|
||
$planId = trim((string)($_POST['plan_id'] ?? $_GET['plan_id'] ?? ''));
|
||
$redirectType = (string)($_POST['redirect_type'] ?? $_GET['redirect_type'] ?? 'all');
|
||
if (!in_array($redirectType, ['all', 'due', 'plan', 'overdue'], true)) {
|
||
$redirectType = 'all';
|
||
}
|
||
$back = 'index.php?action=reminders&type=' . $redirectType;
|
||
|
||
if ($archiveId <= 0) {
|
||
flash('error', '参数错误');
|
||
redirectBackOr($back);
|
||
}
|
||
|
||
if ($op === 'early_settle') {
|
||
$financeAll = loadFinanceConfigs();
|
||
$cfg = getArchiveFinanceConfig($financeAll, $archiveId);
|
||
$cfg['manual_early_settled'] = true;
|
||
$cfg['manual_overdue'] = false;
|
||
$cfg['updated_at'] = now();
|
||
$financeAll[(string)$archiveId] = $cfg;
|
||
saveFinanceConfigs($financeAll);
|
||
flash('ok', '已设为提前还款,状态改为已完成');
|
||
redirectBackOr($back);
|
||
}
|
||
|
||
if ($op === 'plan_delay') {
|
||
$newDue = trim((string)($_POST['new_due_date'] ?? $_GET['new_due_date'] ?? ''));
|
||
if ($planId === '' || $newDue === '') {
|
||
flash('error', '延期参数不完整');
|
||
redirectBackOr($back);
|
||
}
|
||
$allPlans = loadRepaymentPlans();
|
||
$cfg = getArchiveRepaymentPlan($allPlans, $archiveId);
|
||
if (empty($cfg['enabled']) || empty($cfg['plans']) || !is_array($cfg['plans'])) {
|
||
flash('error', '分期计划不存在');
|
||
redirectBackOr($back);
|
||
}
|
||
$ok = false;
|
||
foreach ($cfg['plans'] as &$p) {
|
||
if ((string)($p['id'] ?? '') !== $planId) {
|
||
continue;
|
||
}
|
||
$p['due_date'] = $newDue;
|
||
$p['status'] = 'delayed';
|
||
$p['paid_at'] = null;
|
||
$p['updated_at'] = now();
|
||
$ok = true;
|
||
break;
|
||
}
|
||
unset($p);
|
||
if ($ok) {
|
||
$allPlans[(string)$archiveId] = $cfg;
|
||
saveRepaymentPlans($allPlans);
|
||
flash('ok', '还款计划已延期');
|
||
} else {
|
||
flash('error', '未找到对应还款计划');
|
||
}
|
||
redirectBackOr($back);
|
||
}
|
||
|
||
if ($op === 'overdue_paid') {
|
||
$allPlans = loadRepaymentPlans();
|
||
$cfg = getArchiveRepaymentPlan($allPlans, $archiveId);
|
||
$handled = false;
|
||
if ($planId !== '' && !empty($cfg['enabled']) && !empty($cfg['plans']) && is_array($cfg['plans'])) {
|
||
foreach ($cfg['plans'] as &$p) {
|
||
if ((string)($p['id'] ?? '') !== $planId) {
|
||
continue;
|
||
}
|
||
$p['status'] = 'paid';
|
||
$p['paid_at'] = date('Y-m-d');
|
||
$p['updated_at'] = now();
|
||
$handled = true;
|
||
break;
|
||
}
|
||
unset($p);
|
||
if ($handled) {
|
||
$allPlans[(string)$archiveId] = $cfg;
|
||
saveRepaymentPlans($allPlans);
|
||
recalcRemainingLimitByPlans($pdo, $archiveId, (array)$cfg['plans']);
|
||
flash('ok', '逾期分期已标记为已归还');
|
||
}
|
||
} else {
|
||
$financeAll = loadFinanceConfigs();
|
||
$f = getArchiveFinanceConfig($financeAll, $archiveId);
|
||
$f['manual_early_settled'] = true;
|
||
$f['manual_overdue'] = false;
|
||
$f['updated_at'] = now();
|
||
$financeAll[(string)$archiveId] = $f;
|
||
saveFinanceConfigs($financeAll);
|
||
flash('ok', '整笔逾期已归还,状态改为已完成');
|
||
$handled = true;
|
||
}
|
||
if (!$handled) {
|
||
flash('error', '未找到可处理的逾期项');
|
||
}
|
||
redirectBackOr($back);
|
||
}
|
||
|
||
flash('error', '不支持的操作');
|
||
redirectBackOr($back);
|
||
}
|
||
|
||
if ($action === 'pending_update_action') {
|
||
requireAdmin();
|
||
$op = (string)($_POST['op'] ?? '');
|
||
$archiveId = (int)($_POST['archive_id'] ?? 0);
|
||
$planId = trim((string)($_POST['plan_id'] ?? ''));
|
||
$todayPu = date('Y-m-d');
|
||
$dirIdPost = (int)($_POST['dir_id'] ?? 0);
|
||
$back = 'index.php?action=pending_updates' . ($dirIdPost > 0 ? ('&dir_id=' . $dirIdPost) : '');
|
||
if ($archiveId <= 0) {
|
||
flash('error', '参数错误');
|
||
redirectBackOr($back);
|
||
}
|
||
|
||
if ($op === 'pending_loan_repaid' || $op === 'pending_loan_overdue') {
|
||
$st = $pdo->prepare('SELECT end_date FROM archives WHERE id=:id');
|
||
$st->execute([':id' => $archiveId]);
|
||
$endRow = $st->fetchColumn();
|
||
$endStr = $endRow !== false && $endRow !== null ? (string)$endRow : '';
|
||
if ($endStr === '' || $endStr >= $todayPu) {
|
||
flash('error', '该贷款尚未到期待更新处理');
|
||
redirectBackOr($back);
|
||
}
|
||
$financeAll = loadFinanceConfigs();
|
||
$f = getArchiveFinanceConfig($financeAll, $archiveId);
|
||
if (archiveStatusWithConfig($endStr, $f) !== '待更新') {
|
||
flash('error', '该项已处理或状态已变更');
|
||
redirectBackOr($back);
|
||
}
|
||
if ($op === 'pending_loan_repaid') {
|
||
$f['manual_early_settled'] = true;
|
||
$f['manual_overdue'] = false;
|
||
$f['updated_at'] = now();
|
||
$financeAll[(string)$archiveId] = $f;
|
||
saveFinanceConfigs($financeAll);
|
||
flash('ok', '已标记为已还款,状态改为已完成');
|
||
} else {
|
||
$f['manual_overdue'] = true;
|
||
$f['manual_early_settled'] = false;
|
||
$f['updated_at'] = now();
|
||
$financeAll[(string)$archiveId] = $f;
|
||
saveFinanceConfigs($financeAll);
|
||
flash('ok', '已标记为已逾期,状态改为逾期中');
|
||
}
|
||
redirectBackOr($back);
|
||
}
|
||
|
||
if ($op === 'pending_plan_repaid' || $op === 'pending_plan_overdue') {
|
||
if ($planId === '') {
|
||
flash('error', '参数错误');
|
||
redirectBackOr($back);
|
||
}
|
||
$allPlans = loadRepaymentPlans();
|
||
$cfg = getArchiveRepaymentPlan($allPlans, $archiveId);
|
||
$handled = false;
|
||
if (!empty($cfg['enabled']) && !empty($cfg['plans']) && is_array($cfg['plans'])) {
|
||
foreach ($cfg['plans'] as &$p) {
|
||
if ((string)($p['id'] ?? '') !== $planId) {
|
||
continue;
|
||
}
|
||
$pst = (string)($p['status'] ?? 'pending');
|
||
$d = (string)($p['due_date'] ?? '');
|
||
if ($d === '' || $d >= $todayPu || !in_array($pst, ['pending', 'delayed'], true)) {
|
||
break;
|
||
}
|
||
if ($op === 'pending_plan_repaid') {
|
||
$p['status'] = 'paid';
|
||
$p['paid_at'] = date('Y-m-d');
|
||
} else {
|
||
$p['status'] = 'overdue';
|
||
$p['paid_at'] = null;
|
||
}
|
||
$p['updated_at'] = now();
|
||
$handled = true;
|
||
break;
|
||
}
|
||
unset($p);
|
||
}
|
||
if ($handled) {
|
||
$allPlans[(string)$archiveId] = $cfg;
|
||
saveRepaymentPlans($allPlans);
|
||
if ($op === 'pending_plan_repaid') {
|
||
recalcRemainingLimitByPlans($pdo, $archiveId, (array)$cfg['plans']);
|
||
}
|
||
flash('ok', $op === 'pending_plan_repaid' ? '已标记为已还款(按期还款)' : '已标记为已逾期(逾期中)');
|
||
} else {
|
||
flash('error', '未找到可处理的还款计划或状态已变更');
|
||
}
|
||
redirectBackOr($back);
|
||
}
|
||
|
||
flash('error', '不支持的操作');
|
||
redirectBackOr($back);
|
||
}
|
||
|
||
if ($action === 'download_import_template') {
|
||
requireAdmin();
|
||
$manualTemplatePath = __DIR__ . '/../data/import_template.xlsx';
|
||
if (is_file($manualTemplatePath)) {
|
||
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
||
header('Content-Disposition: attachment; filename="' . rawurlencode('档案导入模板.xlsx') . '"');
|
||
readfile($manualTemplatePath);
|
||
exit;
|
||
}
|
||
$headers = ['一级目录', '二级目录', '三级目录', '档案名称', '贷款机构', '借款主体', '授信额度', '起止日', '到期日', '年利率', '担保类型', '担保人', '计息方式', '结息日', '是否有分期还款计划'];
|
||
$sample = ['银行贷款', '2026', 'XX银行', 'A公司流动资金贷款合同', 'XX银行', 'A公司', '5000000', '2026-01-15', '2027-01-14', '5.5', '抵押', 'B公司', '按月计息', '21', '否'];
|
||
if (class_exists('\PhpOffice\PhpSpreadsheet\Spreadsheet') && class_exists('\PhpOffice\PhpSpreadsheet\Writer\Xlsx')) {
|
||
$filename = '档案导入模板.xlsx';
|
||
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
|
||
$sheet = $spreadsheet->getActiveSheet();
|
||
$sheet->setCellValue(
|
||
'A1',
|
||
'说明:第1行可写模板使用提示(导入时忽略);第2行为列标题勿改;从第3行起填数据。'
|
||
. '一级目录填「银行贷款」或「对外担保」;系统据此识别档案类型。对外担保导入时「贷款机构」「年利率」可留空;「档案名称」可留空,将按担保人+对外担保+授信万元自动生成。'
|
||
. '银行贷款必填至担保人(含档案名称列);选填:计息方式、结息日、是否有分期还款计划。'
|
||
);
|
||
$sheet->fromArray($headers, null, 'A2');
|
||
$sheet->fromArray([$sample], null, 'A3');
|
||
foreach (range('A', 'O') as $col) {
|
||
$sheet->getColumnDimension($col)->setWidth(20);
|
||
}
|
||
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
||
header('Content-Disposition: attachment; filename="' . rawurlencode($filename) . '"');
|
||
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
|
||
$writer->save('php://output');
|
||
exit;
|
||
}
|
||
flash('error', '未找到可用xlsx模板。请安装PhpSpreadsheet或将模板放到 data/import_template.xlsx');
|
||
header('Location: index.php');
|
||
exit;
|
||
}
|
||
|
||
if ($action === 'import_archives') {
|
||
requireAdmin();
|
||
if (empty($_FILES['import_file']) || (int)$_FILES['import_file']['error'] !== UPLOAD_ERR_OK) {
|
||
flash('error', '请选择有效导入文件');
|
||
header('Location: index.php');
|
||
exit;
|
||
}
|
||
$name = (string)$_FILES['import_file']['name'];
|
||
$ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
|
||
$rows = [];
|
||
if ($ext === 'csv') {
|
||
$fp = fopen((string)$_FILES['import_file']['tmp_name'], 'rb');
|
||
if ($fp) {
|
||
fgetcsv($fp);
|
||
$header = fgetcsv($fp);
|
||
while (($line = fgetcsv($fp)) !== false) {
|
||
$rows[] = $line;
|
||
}
|
||
fclose($fp);
|
||
}
|
||
} elseif (in_array($ext, ['xlsx', 'xls'], true) && class_exists('\PhpOffice\PhpSpreadsheet\IOFactory')) {
|
||
$sheet = \PhpOffice\PhpSpreadsheet\IOFactory::load((string)$_FILES['import_file']['tmp_name'])->getActiveSheet();
|
||
$arr = $sheet->toArray();
|
||
array_shift($arr);
|
||
$header = array_shift($arr);
|
||
$rows = $arr;
|
||
} else {
|
||
flash('error', '仅支持CSV,或安装PhpSpreadsheet后支持Excel');
|
||
header('Location: index.php');
|
||
exit;
|
||
}
|
||
$created = 0;
|
||
$skipped = 0;
|
||
$financeAll = loadFinanceConfigs();
|
||
$planAll = loadRepaymentPlans();
|
||
$seen = [];
|
||
foreach ($rows as $r) {
|
||
$lv1 = (string)($r[0] ?? '');
|
||
$lv2 = (string)($r[1] ?? '');
|
||
$lv3 = (string)($r[2] ?? '');
|
||
$archiveName = trim((string)($r[3] ?? ''));
|
||
$loanInstitution = trim((string)($r[4] ?? ''));
|
||
$borrower = trim((string)($r[5] ?? ''));
|
||
$creditRaw = trim((string)($r[6] ?? ''));
|
||
$startDate = trim((string)($r[7] ?? ''));
|
||
$endDate = trim((string)($r[8] ?? ''));
|
||
$annualRateRaw = trim((string)($r[9] ?? ''));
|
||
$guaranteeType = trim((string)($r[10] ?? ''));
|
||
$guarantor = trim((string)($r[11] ?? ''));
|
||
$importKind = trim($lv1) === '对外担保' ? ARCHIVE_KIND_EXTERNAL_GUARANTEE : ARCHIVE_KIND_BANK_LOAN;
|
||
if ($importKind === ARCHIVE_KIND_EXTERNAL_GUARANTEE) {
|
||
if ($lv1 === '' || $lv2 === '' || $lv3 === '' || $borrower === '' || $creditRaw === '' || $startDate === '' || $endDate === '' || $guaranteeType === '' || $guarantor === '') {
|
||
continue;
|
||
}
|
||
$annualRate = $annualRateRaw === '' ? null : (float) $annualRateRaw;
|
||
} else {
|
||
if ($lv1 === '' || $lv2 === '' || $lv3 === '' || $archiveName === '' || $loanInstitution === '' || $borrower === '' || $creditRaw === '' || $startDate === '' || $endDate === '' || $annualRateRaw === '' || $guaranteeType === '' || $guarantor === '') {
|
||
continue;
|
||
}
|
||
$annualRate = (float) $annualRateRaw;
|
||
}
|
||
// 无论目录是否预先存在,都会自动按三级路径创建并返回三级目录ID。
|
||
$dirId = ensureDirectoryPath($pdo, $lv1, $lv2, $lv3);
|
||
$credit = (float) $creditRaw;
|
||
$remain = $credit;
|
||
if ($importKind === ARCHIVE_KIND_EXTERNAL_GUARANTEE) {
|
||
$archiveName = autoExternalGuaranteeArchiveName($guarantor, $credit);
|
||
if ($archiveName === '') {
|
||
continue;
|
||
}
|
||
}
|
||
|
||
$fingerprint = implode('|', [
|
||
$importKind,
|
||
(string) $dirId,
|
||
mb_strtolower($archiveName),
|
||
mb_strtolower($loanInstitution),
|
||
mb_strtolower($borrower),
|
||
(string) $credit,
|
||
$startDate,
|
||
$endDate,
|
||
(string) ($annualRate ?? ''),
|
||
mb_strtolower($guaranteeType),
|
||
mb_strtolower($guarantor),
|
||
]);
|
||
if (isset($seen[$fingerprint])) {
|
||
$skipped++;
|
||
continue;
|
||
}
|
||
$seen[$fingerprint] = true;
|
||
|
||
$dupStmt = $pdo->prepare('SELECT COUNT(*) FROM archives WHERE directory_id=:d AND archive_kind=:ak AND name=:n AND COALESCE(loan_institution,\'\')=:li AND borrower=:b AND credit_limit=:cl AND start_date=:sd AND end_date=:ed AND COALESCE(annual_rate,-999999.99)=COALESCE(:ar,-999999.99) AND guarantee_type=:gt AND guarantor=:g');
|
||
$dupStmt->execute([
|
||
':d' => $dirId,
|
||
':ak' => $importKind,
|
||
':n' => $archiveName,
|
||
':li' => $loanInstitution,
|
||
':b' => $borrower,
|
||
':cl' => $credit,
|
||
':sd' => $startDate,
|
||
':ed' => $endDate,
|
||
':ar' => $annualRate,
|
||
':gt' => $guaranteeType,
|
||
':g' => $guarantor,
|
||
]);
|
||
if ((int)$dupStmt->fetchColumn() > 0) {
|
||
$skipped++;
|
||
continue;
|
||
}
|
||
|
||
$pdo->prepare('INSERT INTO archives(name,directory_id,archive_kind,loan_institution,borrower,credit_limit,remaining_limit,start_date,end_date,annual_rate,guarantee_type,guarantor,created_at,updated_at) VALUES(:name,:directory_id,:archive_kind,:loan_institution,:borrower,:credit_limit,:remaining_limit,:start_date,:end_date,:annual_rate,:guarantee_type,:guarantor,:created_at,:updated_at)')
|
||
->execute([
|
||
':name' => $archiveName,
|
||
':directory_id' => $dirId,
|
||
':archive_kind' => $importKind,
|
||
':loan_institution' => $loanInstitution,
|
||
':borrower' => $borrower,
|
||
':credit_limit' => $credit,
|
||
':remaining_limit' => $remain,
|
||
':start_date' => $startDate ?: null,
|
||
':end_date' => $endDate ?: null,
|
||
':annual_rate' => $annualRate,
|
||
':guarantee_type' => $guaranteeType,
|
||
':guarantor' => $guarantor,
|
||
':created_at' => now(),
|
||
':updated_at' => now(),
|
||
]);
|
||
$archiveId = (int)$pdo->lastInsertId();
|
||
$modeText = trim((string)($r[12] ?? ''));
|
||
$mode = $modeText === '按季付息' ? 'quarterly' : ($modeText === '到期一次性付清' ? 'bullet' : 'monthly');
|
||
$settlementRule = $mode === 'quarterly' ? 'quarterly_day' : ($mode === 'bullet' ? 'maturity_once' : 'monthly_day');
|
||
$settlementDay = $mode === 'bullet' ? 0 : max(1, min(28, (int)trim((string)($r[13] ?? '21'))));
|
||
$financeAll[(string)$archiveId] = [
|
||
'interest_mode' => $mode,
|
||
'settlement_rule' => $settlementRule,
|
||
'settlement_day' => $settlementDay,
|
||
'overdue_float_pct' => 50,
|
||
'manual_overdue' => false,
|
||
'manual_early_settled' => false,
|
||
'remark' => '',
|
||
'updated_at' => now(),
|
||
];
|
||
$planEnabledText = trim((string)($r[14] ?? '否'));
|
||
$planAll[(string)$archiveId] = ['enabled' => in_array($planEnabledText, ['是', 'yes', 'YES', '1'], true), 'plans' => []];
|
||
$created++;
|
||
}
|
||
saveFinanceConfigs($financeAll);
|
||
saveRepaymentPlans($planAll);
|
||
flash('ok', '导入完成:新增 ' . $created . ' 条,重复忽略 ' . $skipped . ' 条');
|
||
header('Location: index.php');
|
||
exit;
|
||
}
|
||
|
||
if ($action === 'request_delete_archive') {
|
||
requireAdmin();
|
||
$archiveId = (int)($_GET['id'] ?? 0);
|
||
if ($archiveId <= 0) {
|
||
flash('error', '参数错误');
|
||
header('Location: index.php');
|
||
exit;
|
||
}
|
||
$approvals = loadDeleteApprovals();
|
||
foreach ($approvals as $ap) {
|
||
if (($ap['target_type'] ?? '') === 'archive' && (int)($ap['target_id'] ?? 0) === $archiveId && ($ap['status'] ?? '') === 'pending') {
|
||
flash('error', '该档案已有待审核删除申请');
|
||
header('Location: index.php');
|
||
exit;
|
||
}
|
||
}
|
||
$approvals[] = [
|
||
'id' => uniqid('del_', true),
|
||
'target_type' => 'archive',
|
||
'target_id' => $archiveId,
|
||
'status' => 'pending',
|
||
'requested_by' => (int)$me['id'],
|
||
'requested_name' => (string)$me['username'],
|
||
'requested_at' => now(),
|
||
'approved_by' => null,
|
||
'approved_at' => null,
|
||
];
|
||
saveDeleteApprovals($approvals);
|
||
flash('ok', '已提交删除申请,需另一位管理员审核');
|
||
header('Location: index.php');
|
||
exit;
|
||
}
|
||
|
||
if ($action === 'request_delete_selected') {
|
||
requireAdmin();
|
||
$idsRaw = $_GET['ids'] ?? '';
|
||
if (is_array($idsRaw)) {
|
||
$ids = array_values(array_unique(array_filter(array_map('intval', $idsRaw))));
|
||
} else {
|
||
$ids = array_values(array_unique(array_filter(array_map('intval', explode(',', (string)$idsRaw)))));
|
||
}
|
||
if (empty($ids)) {
|
||
flash('error', '请先选择要删除的档案');
|
||
header('Location: index.php');
|
||
exit;
|
||
}
|
||
$approvals = loadDeleteApprovals();
|
||
$pendingSet = [];
|
||
foreach ($approvals as $ap) {
|
||
if (($ap['status'] ?? '') !== 'pending') {
|
||
continue;
|
||
}
|
||
if (($ap['target_type'] ?? '') === 'archive' && !empty($ap['target_id'])) {
|
||
$pendingSet[] = (string)$ap['target_id'];
|
||
}
|
||
if (($ap['target_type'] ?? '') === 'archive_batch' && !empty($ap['target_ids']) && is_array($ap['target_ids'])) {
|
||
foreach ($ap['target_ids'] as $x) {
|
||
$pendingSet[] = (string)(int)$x;
|
||
}
|
||
}
|
||
}
|
||
foreach ($ids as $id) {
|
||
if (in_array((string)$id, $pendingSet, true)) {
|
||
flash('error', '所选档案中存在待审核删除项,请先完成审核');
|
||
header('Location: index.php');
|
||
exit;
|
||
}
|
||
}
|
||
$approvals[] = [
|
||
'id' => uniqid('del_', true),
|
||
'target_type' => 'archive_batch',
|
||
'target_ids' => $ids,
|
||
'target_count' => count($ids),
|
||
'status' => 'pending',
|
||
'requested_by' => (int)$me['id'],
|
||
'requested_name' => (string)$me['username'],
|
||
'requested_at' => now(),
|
||
'approved_by' => null,
|
||
'approved_at' => null,
|
||
];
|
||
saveDeleteApprovals($approvals);
|
||
flash('ok', '已提交选中档案删除申请(' . count($ids) . ' 条),需另一位管理员审核');
|
||
header('Location: index.php');
|
||
exit;
|
||
}
|
||
|
||
if ($action === 'delete_approvals') {
|
||
requireAdmin();
|
||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||
$approvalId = trim((string)($_POST['approval_id'] ?? ''));
|
||
$do = (string)($_POST['do'] ?? '');
|
||
$approvals = loadDeleteApprovals();
|
||
foreach ($approvals as &$ap) {
|
||
if (($ap['id'] ?? '') !== $approvalId || ($ap['status'] ?? '') !== 'pending') {
|
||
continue;
|
||
}
|
||
if ((int)$ap['requested_by'] === (int)$me['id']) {
|
||
flash('error', '不能由申请人本人审核');
|
||
break;
|
||
}
|
||
if ($do === 'approve' && ($ap['target_type'] ?? '') === 'archive') {
|
||
hardDeleteArchive($pdo, (int)$ap['target_id']);
|
||
$ap['status'] = 'approved';
|
||
$ap['approved_by'] = (int)$me['id'];
|
||
$ap['approved_at'] = now();
|
||
flash('ok', '删除已由第二管理员审核并执行');
|
||
break;
|
||
}
|
||
if ($do === 'approve' && ($ap['target_type'] ?? '') === 'archive_batch') {
|
||
$ids = array_values(array_unique(array_filter(array_map('intval', (array)($ap['target_ids'] ?? [])))));
|
||
foreach ($ids as $aid) {
|
||
hardDeleteArchive($pdo, $aid);
|
||
}
|
||
$ap['status'] = 'approved';
|
||
$ap['approved_by'] = (int)$me['id'];
|
||
$ap['approved_at'] = now();
|
||
flash('ok', '批量删除已审核并执行(' . count($ids) . ' 条)');
|
||
break;
|
||
}
|
||
if ($do === 'reject') {
|
||
$ap['status'] = 'rejected';
|
||
$ap['approved_by'] = (int)$me['id'];
|
||
$ap['approved_at'] = now();
|
||
flash('ok', '已驳回删除申请');
|
||
break;
|
||
}
|
||
}
|
||
unset($ap);
|
||
saveDeleteApprovals($approvals);
|
||
redirectBackOr('index.php?action=delete_approvals');
|
||
}
|
||
$approvals = array_reverse(loadDeleteApprovals());
|
||
$filterStatus = (string)($_GET['status'] ?? 'pending');
|
||
$filterTarget = trim((string)($_GET['target_id'] ?? ''));
|
||
$filterRequester = trim((string)($_GET['requester'] ?? ''));
|
||
$approvals = array_values(array_filter($approvals, function ($ap) use ($filterStatus, $filterTarget, $filterRequester) {
|
||
if ($filterStatus !== 'all' && ($ap['status'] ?? '') !== $filterStatus) {
|
||
return false;
|
||
}
|
||
if ($filterTarget !== '' && (string)($ap['target_id'] ?? '') !== $filterTarget) {
|
||
return false;
|
||
}
|
||
if ($filterRequester !== '' && mb_stripos((string)($ap['requested_name'] ?? ''), $filterRequester) === false) {
|
||
return false;
|
||
}
|
||
return true;
|
||
}));
|
||
$usersMap = [];
|
||
$archiveMap = fetchArchiveSimpleMap($pdo);
|
||
foreach ($pdo->query('SELECT id,username FROM users')->fetchAll() as $u) {
|
||
$usersMap[(int)$u['id']] = (string)$u['username'];
|
||
}
|
||
pageHeader($me); ?>
|
||
<div class="page-head">
|
||
<div>
|
||
<h1 class="page-title">删除审批</h1>
|
||
<div class="page-sub">双管理员;申请人不能审核自己的申请</div>
|
||
</div>
|
||
</div>
|
||
<div class="card">
|
||
<form method="get" class="toolbar-left" style="margin-bottom:10px">
|
||
<input type="hidden" name="action" value="delete_approvals">
|
||
<select name="status">
|
||
<option value="pending" <?= $filterStatus === 'pending' ? 'selected' : '' ?>>待审核</option>
|
||
<option value="approved" <?= $filterStatus === 'approved' ? 'selected' : '' ?>>已通过</option>
|
||
<option value="rejected" <?= $filterStatus === 'rejected' ? 'selected' : '' ?>>已驳回</option>
|
||
<option value="all" <?= $filterStatus === 'all' ? 'selected' : '' ?>>全部</option>
|
||
</select>
|
||
<input name="target_id" placeholder="目标ID" value="<?= e($filterTarget) ?>">
|
||
<input name="requester" placeholder="申请人" value="<?= e($filterRequester) ?>">
|
||
<button type="submit" class="btn btn--plain">筛选</button>
|
||
<a class="clear-link" href="index.php?action=delete_approvals">清空</a>
|
||
</form>
|
||
<div class="table-wrap"><table>
|
||
<thead><tr><th>申请时间</th><th>类型</th><th>目标</th><th>申请人</th><th>状态</th><th>审核人</th><th>审核时间</th><th>操作</th></tr></thead>
|
||
<tbody>
|
||
<?php
|
||
$apStatusMap = ['pending' => '待审批', 'approved' => '已通过', 'rejected' => '已驳回'];
|
||
foreach ($approvals as $ap):
|
||
$stKey = (string)($ap['status'] ?? '');
|
||
$stLabel = $apStatusMap[$stKey] ?? $stKey;
|
||
$isSelf = (int)($ap['requested_by'] ?? 0) === (int)$me['id'];
|
||
?>
|
||
<tr>
|
||
<td class="num"><?= e((string)$ap['requested_at']) ?></td>
|
||
<td><?= e((string)$ap['target_type']) ?></td>
|
||
<td><?= e(approvalTargetLabel($ap, $archiveMap)) ?></td>
|
||
<td><?= e((string)$ap['requested_name']) ?></td>
|
||
<td><?= fusionStatusPill($stLabel) ?></td>
|
||
<td><?php if (isset($usersMap[(int)($ap['approved_by'] ?? 0)])): ?><?= e($usersMap[(int)$ap['approved_by']]) ?><?php else: ?><span class="muted">-</span><?php endif; ?></td>
|
||
<td class="num"><?php if (!empty($ap['approved_at'])): ?><?= e((string)$ap['approved_at']) ?><?php else: ?><span class="muted">-</span><?php endif; ?></td>
|
||
<td>
|
||
<?php if ($stKey === 'pending' && !$isSelf): ?>
|
||
<form class="inline" method="post" style="display:inline-flex;gap:8px;align-items:center">
|
||
<input type="hidden" name="approval_id" value="<?= e((string)$ap['id']) ?>">
|
||
<input type="hidden" name="do" value="approve">
|
||
<input type="hidden" name="redirect_to" value="<?= e(currentUrlFromServer()) ?>">
|
||
<button type="button" class="hold-btn" style="flex:none;min-width:148px">
|
||
<span class="fill"></span>
|
||
<span class="lbl">按住确认删除</span>
|
||
</button>
|
||
</form>
|
||
<form class="inline" method="post" style="display:inline">
|
||
<input type="hidden" name="approval_id" value="<?= e((string)$ap['id']) ?>">
|
||
<input type="hidden" name="do" value="reject">
|
||
<input type="hidden" name="redirect_to" value="<?= e(currentUrlFromServer()) ?>">
|
||
<button class="btn btn--plain" type="submit">驳回</button>
|
||
</form>
|
||
<?php elseif ($stKey === 'pending' && $isSelf): ?>
|
||
<span class="muted">待另一位管理员审核</span>
|
||
<?php else: ?>
|
||
<span class="muted">-</span>
|
||
<?php endif; ?>
|
||
</td>
|
||
</tr>
|
||
<?php endforeach; ?>
|
||
<?php if (empty($approvals)): ?>
|
||
<tr><td colspan="8"><div class="empty-action"><div class="ea-title">暂无符合条件的审核记录</div>调整筛选条件,或等待新的删除申请</div></td></tr>
|
||
<?php endif; ?>
|
||
</tbody>
|
||
</table></div>
|
||
</div>
|
||
<?php pageFooter(); exit;
|
||
}
|
||
|
||
if ($action === 'delete_archive') {
|
||
requireAdmin();
|
||
flash('error', '请通过双管理员删除流程操作');
|
||
header('Location: index.php');
|
||
exit;
|
||
}
|
||
|
||
if ($action === 'upload_file') {
|
||
requireAdmin();
|
||
$archiveId = (int)($_GET['archive_id'] ?? $_POST['archive_id'] ?? 0);
|
||
if ($archiveId <= 0 || empty($_FILES['files'])) {
|
||
flash('error', '上传参数错误');
|
||
header('Location: index.php');
|
||
exit;
|
||
}
|
||
uploadArchiveFiles($pdo, $archiveId, $_FILES['files']);
|
||
flash('ok', '上传完成');
|
||
header('Location: index.php?action=archive_form&id=' . $archiveId);
|
||
exit;
|
||
}
|
||
|
||
if ($action === 'delete_file') {
|
||
requireAdmin();
|
||
$id = (int)($_GET['id'] ?? 0);
|
||
$stmt = $pdo->prepare('SELECT * FROM files WHERE id=:id');
|
||
$stmt->execute([':id' => $id]);
|
||
$f = $stmt->fetch();
|
||
if ($f) {
|
||
$p = UPLOAD_DIR . '/' . $f['stored_name'];
|
||
if (is_file($p)) {
|
||
unlink($p);
|
||
}
|
||
$pdo->prepare('DELETE FROM files WHERE id=:id')->execute([':id' => $id]);
|
||
flash('ok', '文件已删除');
|
||
header('Location: index.php?action=archive_form&id=' . (int)$f['archive_id']);
|
||
exit;
|
||
}
|
||
flash('error', '文件不存在');
|
||
header('Location: index.php');
|
||
exit;
|
||
}
|
||
|
||
if ($action === 'rename_file') {
|
||
requireAdmin();
|
||
$fileId = (int)($_POST['file_id'] ?? 0);
|
||
$newNameRaw = trim((string)($_POST['new_name'] ?? ''));
|
||
if ($fileId <= 0 || $newNameRaw === '') {
|
||
flash('error', '重命名参数错误');
|
||
header('Location: index.php');
|
||
exit;
|
||
}
|
||
$stmt = $pdo->prepare('SELECT * FROM files WHERE id=:id');
|
||
$stmt->execute([':id' => $fileId]);
|
||
$f = $stmt->fetch();
|
||
if (!$f) {
|
||
flash('error', '文件不存在');
|
||
header('Location: index.php');
|
||
exit;
|
||
}
|
||
|
||
$currentStored = (string)$f['stored_name'];
|
||
$dirPart = dirname($currentStored);
|
||
$dirPart = $dirPart === '.' ? '' : $dirPart;
|
||
$prefix = pathinfo($currentStored, PATHINFO_FILENAME);
|
||
$dotPos = strpos($prefix, '_');
|
||
$uniqPrefix = $dotPos === false ? uniqid('f_', true) : substr($prefix, 0, $dotPos);
|
||
$ext = strtolower((string)$f['file_ext']);
|
||
|
||
$newOriginal = $newNameRaw;
|
||
if (strtolower((string)pathinfo($newOriginal, PATHINFO_EXTENSION)) !== $ext) {
|
||
$newOriginal .= '.' . $ext;
|
||
}
|
||
$normalized = normalizeFileName($newOriginal);
|
||
$newBase = $uniqPrefix . '_' . $normalized;
|
||
$newStored = ($dirPart !== '' ? $dirPart . '/' : '') . $newBase;
|
||
|
||
$oldPath = UPLOAD_DIR . '/' . $currentStored;
|
||
$newPath = UPLOAD_DIR . '/' . $newStored;
|
||
if (!is_file($oldPath)) {
|
||
flash('error', '原文件不存在');
|
||
header('Location: index.php?action=archive_form&id=' . (int)$f['archive_id']);
|
||
exit;
|
||
}
|
||
if (is_file($newPath)) {
|
||
$newStored = ($dirPart !== '' ? $dirPart . '/' : '') . uniqid('f_', true) . '_' . $normalized;
|
||
$newPath = UPLOAD_DIR . '/' . $newStored;
|
||
}
|
||
if (!rename($oldPath, $newPath)) {
|
||
flash('error', '文件重命名失败');
|
||
header('Location: index.php?action=archive_form&id=' . (int)$f['archive_id']);
|
||
exit;
|
||
}
|
||
|
||
$pdo->prepare('UPDATE files SET original_name=:o, stored_name=:s WHERE id=:id')
|
||
->execute([':o' => $newOriginal, ':s' => $newStored, ':id' => $fileId]);
|
||
flash('ok', '文件已重命名');
|
||
header('Location: index.php?action=archive_form&id=' . (int)$f['archive_id']);
|
||
exit;
|
||
}
|
||
|
||
if ($action === 'download_file') {
|
||
requireLogin();
|
||
$id = (int)($_GET['id'] ?? 0);
|
||
$stmt = $pdo->prepare('SELECT * FROM files WHERE id=:id');
|
||
$stmt->execute([':id' => $id]);
|
||
$f = $stmt->fetch();
|
||
if (!$f) {
|
||
http_response_code(404);
|
||
exit('Not found');
|
||
}
|
||
$p = UPLOAD_DIR . '/' . $f['stored_name'];
|
||
if (!is_file($p)) {
|
||
http_response_code(404);
|
||
exit('File missing');
|
||
}
|
||
header('Content-Type: application/octet-stream');
|
||
header('Content-Disposition: attachment; filename="' . rawurlencode($f['original_name']) . '"');
|
||
readfile($p);
|
||
exit;
|
||
}
|
||
|
||
if ($action === 'preview_file') {
|
||
requireLogin();
|
||
$id = (int)($_GET['id'] ?? 0);
|
||
$stmt = $pdo->prepare('SELECT * FROM files WHERE id=:id');
|
||
$stmt->execute([':id' => $id]);
|
||
$f = $stmt->fetch();
|
||
if (!$f || !canPreview($f['file_ext'])) {
|
||
http_response_code(403);
|
||
exit('不支持预览');
|
||
}
|
||
$p = UPLOAD_DIR . '/' . $f['stored_name'];
|
||
if (!is_file($p)) {
|
||
http_response_code(404);
|
||
exit('文件不存在');
|
||
}
|
||
if ($f['file_ext'] === 'pdf') {
|
||
header('Content-Type: application/pdf');
|
||
} else {
|
||
header('Content-Type: image/' . ($f['file_ext'] === 'jpg' ? 'jpeg' : $f['file_ext']));
|
||
}
|
||
readfile($p);
|
||
exit;
|
||
}
|
||
|
||
if ($action === 'archive_detail') {
|
||
requireLogin();
|
||
$id = (int)($_GET['id'] ?? 0);
|
||
$stmt = $pdo->prepare('SELECT a.*, d.name AS dir_name FROM archives a JOIN directories d ON a.directory_id=d.id WHERE a.id=:id');
|
||
$stmt->execute([':id' => $id]);
|
||
$a = $stmt->fetch();
|
||
if (!$a) {
|
||
flash('error', '档案不存在');
|
||
header('Location: index.php');
|
||
exit;
|
||
}
|
||
$planAll = loadRepaymentPlans();
|
||
$planCfg = getArchiveRepaymentPlan($planAll, $id);
|
||
$repaymentPlans = $planCfg['plans'] ?? [];
|
||
$financeAll = loadFinanceConfigs();
|
||
$financeCfg = getArchiveFinanceConfig($financeAll, $id);
|
||
$status = archiveStatusWithConfig($a['end_date'], $financeCfg);
|
||
$detailKind = normalizeArchiveKind($a['archive_kind'] ?? '');
|
||
$interestPayable = calcCurrentInterest($a, $financeCfg, $repaymentPlans);
|
||
$settlementInterestPayable = $detailKind === ARCHIVE_KIND_BANK_LOAN
|
||
? calcSettlementPeriodInterest($a, $financeCfg, $repaymentPlans, date('Y-m-d'))
|
||
: 0.0;
|
||
usort($repaymentPlans, function ($x, $y) {
|
||
return strcmp((string)($x['due_date'] ?? ''), (string)($y['due_date'] ?? ''));
|
||
});
|
||
$files = $pdo->prepare('SELECT * FROM files WHERE archive_id=:a ORDER BY id DESC');
|
||
$files->execute([':a' => $id]);
|
||
pageHeader($me); ?>
|
||
<div class="crumbs"><a href="index.php?action=home">档案库</a><span>/</span><span><?= e($a['name']) ?></span></div>
|
||
<div class="page-head">
|
||
<div>
|
||
<h1 class="page-title"><?= e((string)$a['borrower']) ?></h1>
|
||
<div class="page-sub"><?= e(archiveKindLabel($detailKind)) ?> · <?= e($a['name']) ?> · <?= e((string)$a['dir_name']) ?> <?= fusionStatusPill($status) ?></div>
|
||
</div>
|
||
<div style="display:flex;gap:10px;flex-wrap:wrap">
|
||
<?php if (isAdmin()): ?>
|
||
<a class="btn btn--primary" href="index.php?action=archive_form&id=<?= $id ?>">编辑档案</a>
|
||
<?php endif; ?>
|
||
<a class="btn btn--plain" href="index.php?action=home">返回列表</a>
|
||
</div>
|
||
</div>
|
||
<?php
|
||
$remainAmt = $a['remaining_limit'] !== null ? (float)$a['remaining_limit'] : 0.0;
|
||
$creditAmt = $a['credit_limit'] !== null ? (float)$a['credit_limit'] : 0.0;
|
||
$paidPct = $creditAmt > 0 ? (int)round((($creditAmt - $remainAmt) / $creditAmt) * 100) : 0;
|
||
$rateTxt = $detailKind === ARCHIVE_KIND_EXTERNAL_GUARANTEE ? '—' : ($a['annual_rate'] === null ? '' : ((string)$a['annual_rate'] . '%'));
|
||
$settleMode = $financeCfg['interest_mode'] === 'quarterly' ? '按季计息' : ($financeCfg['interest_mode'] === 'bullet' ? '到期一次性付清' : '按月计息');
|
||
$rule = (string)$financeCfg['settlement_rule'];
|
||
if ($rule === 'quarterly_day') {
|
||
$settleDayTxt = '每季' . (string)$financeCfg['settlement_day'] . '日';
|
||
} elseif ($rule === 'maturity_once') {
|
||
$settleDayTxt = '到期一次性付息';
|
||
} else {
|
||
$settleDayTxt = '每月' . (string)$financeCfg['settlement_day'] . '日';
|
||
}
|
||
?>
|
||
<div class="detail-grid">
|
||
<div>
|
||
<div class="hero-amount"<?= $remainAmt <= 0 ? ' data-zero' : '' ?>>
|
||
<div>
|
||
<div class="ha-label">剩余额度</div>
|
||
<div class="ha-value num"><?= e(amount($a['remaining_limit'] !== null ? (float)$a['remaining_limit'] : null)) ?></div>
|
||
</div>
|
||
<div class="ha-side">授信额度 <b class="num"><?= e(amount($a['credit_limit'] !== null ? (float)$a['credit_limit'] : null)) ?></b><br>已还 <b class="num"><?= (int)$paidPct ?>%</b> · 年利率 <b class="num"><?= e($rateTxt) ?></b></div>
|
||
</div>
|
||
<div class="section-title" style="margin-top:0;">概要</div>
|
||
<div class="field-grid">
|
||
<div class="field"><label><?= $detailKind === ARCHIVE_KIND_EXTERNAL_GUARANTEE ? '债权人' : '贷款机构' ?></label><div class="val"><?= e((string)$a['loan_institution']) ?></div></div>
|
||
<div class="field"><label>业务类型</label><div class="val"><?= e(archiveKindLabel($detailKind)) ?></div></div>
|
||
<div class="field"><label>起始日 / 到期日</label><div class="val num"><?= e((string)$a['start_date']) ?> — <?= e((string)$a['end_date']) ?></div></div>
|
||
<div class="field"><label>结息方式</label><div class="val"><?= e($settleMode) ?>(<?= e($settleDayTxt) ?>)</div></div>
|
||
<div class="field"><label>当前应付利息</label><div class="val num" style="color:var(--gold);"><?= e(amount($interestPayable)) ?></div></div>
|
||
<div class="field"><label>结息日应付利息</label><div class="val num"><?= $detailKind === ARCHIVE_KIND_BANK_LOAN ? e(amount($settlementInterestPayable)) : '—' ?></div></div>
|
||
<div class="field"><label>逾期利率上浮</label><div class="val num"><?= e(amount((float)$financeCfg['overdue_float_pct'])) ?>%</div></div>
|
||
<div class="field"><label>担保人</label><div class="val"><?= e((string)$a['guarantor']) ?></div></div>
|
||
</div>
|
||
<?php $detailRemark = trim((string)($financeCfg['remark'] ?? '')); ?>
|
||
<?php if ($detailRemark !== ''): ?>
|
||
<div class="field" style="margin-top:16px;"><label>备注</label><div class="val" style="font-weight:500;font-size:13px;line-height:1.7;white-space:pre-wrap"><?= e($detailRemark) ?></div></div>
|
||
<?php endif; ?>
|
||
<div class="section-title">还款计划</div>
|
||
<?php if (!($planCfg['enabled'] ?? false) || empty($repaymentPlans)): ?>
|
||
<div class="empty">暂无分期还款计划</div>
|
||
<?php else: ?>
|
||
<div class="timeline">
|
||
<?php foreach ($repaymentPlans as $p):
|
||
$st = (string)($p['status'] ?? 'pending');
|
||
$tl = $st === 'paid' ? 'paid' : (($st === 'overdue') ? 'over' : 'due');
|
||
$kind = (string)($p['kind'] ?? '分期');
|
||
?>
|
||
<div class="plan-row" data-tl="<?= e($tl) ?>">
|
||
<span class="plan-kind"><?= e($kind) ?></span>
|
||
<span class="plan-date num"><?= e((string)($p['due_date'] ?? '')) ?></span>
|
||
<?php
|
||
if ($st === 'paid') echo fusionStatusPill('已还款');
|
||
elseif ($st === 'delayed') echo fusionStatusPill('已延期');
|
||
elseif ($st === 'overdue') echo fusionStatusPill('已逾期');
|
||
else echo fusionStatusPill('待处理');
|
||
?>
|
||
<span class="plan-amt num"><?= e(amount((float)($p['amount'] ?? 0))) ?></span>
|
||
</div>
|
||
<?php endforeach; ?>
|
||
</div>
|
||
<?php if (isAdmin()): ?><p class="small muted">还款计划状态和延期调整请在「编辑档案」页面操作。</p><?php endif; ?>
|
||
<?php endif; ?>
|
||
</div>
|
||
<aside class="side-panel">
|
||
<div class="section-title" style="margin:0 0 12px;">附件(<?php
|
||
$fileRows = is_array($files) ? $files : $files->fetchAll();
|
||
echo count($fileRows);
|
||
?>)</div>
|
||
<?php if (!$fileRows): ?>
|
||
<div class="empty">暂无附件</div>
|
||
<?php else: foreach ($fileRows as $f):
|
||
$ext = strtoupper((string)$f['file_ext']);
|
||
?>
|
||
<div class="file-row">
|
||
<span class="file-ico"><?= e(substr($ext, 0, 3)) ?></span>
|
||
<div style="min-width:0">
|
||
<div class="file-name"><?= e($f['original_name']) ?></div>
|
||
<div class="file-meta num"><?= number_format(((int)$f['file_size']) / 1024, 1) ?> KB</div>
|
||
</div>
|
||
<div class="file-act">
|
||
<?php if (canPreview($f['file_ext'])): ?><a href="index.php?action=preview_file&id=<?= (int)$f['id'] ?>" target="_blank">预览</a><?php endif; ?>
|
||
<a href="index.php?action=download_file&id=<?= (int)$f['id'] ?>">下载</a>
|
||
</div>
|
||
</div>
|
||
<?php endforeach; endif; ?>
|
||
<?php if (isAdmin()): ?>
|
||
<a class="btn btn--plain" style="width:100%;margin-top:4px;box-sizing:border-box;justify-content:center;" href="index.php?action=archive_form&id=<?= $id ?>">+ 上传附件</a>
|
||
<?php endif; ?>
|
||
<div class="section-title">提醒</div>
|
||
<div class="setting-row" style="border-top:0; padding-top:0;">
|
||
<div>到期与还款提醒<div class="hint">原系统无独立提醒开关,任务中心按到期日自动列出</div></div>
|
||
<button type="button" class="switch" role="switch" aria-checked="true" aria-disabled="true" aria-label="到期与还款提醒(只读占位)"></button>
|
||
</div>
|
||
<?php if (isAdmin()): ?>
|
||
<div class="section-title">危险操作</div>
|
||
<button type="button" class="hold-btn" data-hold-href="index.php?action=request_delete_archive&id=<?= $id ?>" aria-label="按住以申请删除">
|
||
<span class="fill" aria-hidden="true"></span><span class="lbl">按住申请删除(双管理员审批)</span>
|
||
</button>
|
||
<?php endif; ?>
|
||
</aside>
|
||
</div>
|
||
<?php pageFooter(); exit;
|
||
}
|
||
|
||
if ($action === 'archive_form') {
|
||
requireAdmin();
|
||
$id = (int)($_GET['id'] ?? 0);
|
||
$editing = null;
|
||
if ($id > 0) {
|
||
$st = $pdo->prepare('SELECT * FROM archives WHERE id=:id');
|
||
$st->execute([':id' => $id]);
|
||
$editing = $st->fetch();
|
||
if (!$editing) {
|
||
flash('error', '档案不存在');
|
||
header('Location: index.php');
|
||
exit;
|
||
}
|
||
}
|
||
$dirs = fetchDirectories($pdo);
|
||
$loanInstitutions = loadLoanInstitutions();
|
||
$dirId = (int)($_GET['dir_id'] ?? ($editing['directory_id'] ?? 0));
|
||
$dirPath = dirPathMap($dirs);
|
||
$planAll = loadRepaymentPlans();
|
||
$planCfg = $editing ? getArchiveRepaymentPlan($planAll, (int)$editing['id']) : ['enabled' => false, 'plans' => []];
|
||
$financeAll = loadFinanceConfigs();
|
||
$financeCfg = $editing ? getArchiveFinanceConfig($financeAll, (int)$editing['id']) : [
|
||
'interest_mode' => '',
|
||
'settlement_rule' => 'monthly_day',
|
||
'settlement_day' => 21,
|
||
'overdue_float_pct' => 50,
|
||
'manual_overdue' => false,
|
||
'manual_early_settled' => false,
|
||
'remark' => '',
|
||
'manual_principal_repaid' => 0.0,
|
||
'manual_interest_paid' => 0.0,
|
||
];
|
||
$planRows = $planCfg['plans'] ?? [];
|
||
if (empty($planRows)) {
|
||
for ($i = 0; $i < 6; $i++) {
|
||
$planRows[] = ['id' => '', 'due_date' => '', 'amount' => ''];
|
||
}
|
||
}
|
||
$editArchiveId = (int)($editing['id'] ?? 0);
|
||
$editFiles = [];
|
||
if ($editArchiveId > 0) {
|
||
$fs = $pdo->prepare('SELECT * FROM files WHERE archive_id=:a ORDER BY id DESC');
|
||
$fs->execute([':a' => $editArchiveId]);
|
||
$editFiles = $fs->fetchAll();
|
||
}
|
||
pageHeader($me); ?>
|
||
<div class="crumbs"><a href="index.php?action=home">档案库</a><span>/</span><span><?= $editing ? '编辑档案' : '新建档案' ?></span></div>
|
||
<div class="page-head">
|
||
<div>
|
||
<h1 class="page-title"><?= $editing ? '编辑档案' : '新建档案' ?></h1>
|
||
<div class="page-sub">字段、联动与校验与原系统一致,仅按步骤分组呈现</div>
|
||
</div>
|
||
</div>
|
||
<div class="steps" aria-label="表单步骤">
|
||
<div class="step" data-current><span class="no">1</span>归档定位</div><span class="step-line"></span>
|
||
<div class="step"><span class="no">2</span>主体与额度</div><span class="step-line"></span>
|
||
<div class="step"><span class="no">3</span>计息规则</div><span class="step-line"></span>
|
||
<div class="step"><span class="no">4</span>还款计划</div><span class="step-line"></span>
|
||
<div class="step"><span class="no">5</span>收尾</div>
|
||
</div>
|
||
<div class="card">
|
||
<?php $formKind = $editing ? normalizeArchiveKind($editing['archive_kind'] ?? '') : ARCHIVE_KIND_BANK_LOAN; ?>
|
||
<form method="post" action="index.php?action=save_archive" class="archive-form-grid" enctype="multipart/form-data">
|
||
<input type="hidden" name="id" value="<?= (int)($editing['id'] ?? 0) ?>">
|
||
<div class="form-row-2">
|
||
<div class="form-field-stack">
|
||
<label for="archive_kind">档案类型</label>
|
||
<select name="archive_kind" id="archive_kind">
|
||
<option value="<?= e(ARCHIVE_KIND_BANK_LOAN) ?>" <?= $formKind === ARCHIVE_KIND_BANK_LOAN ? 'selected' : '' ?>>银行贷款</option>
|
||
<option value="<?= e(ARCHIVE_KIND_EXTERNAL_GUARANTEE) ?>" <?= $formKind === ARCHIVE_KIND_EXTERNAL_GUARANTEE ? 'selected' : '' ?>>对外担保</option>
|
||
</select>
|
||
</div>
|
||
<div class="form-field-stack">
|
||
<label class="muted" style="font-size:12px">自动目录说明</label>
|
||
<p class="small muted" id="archive_auto_dir_hint" style="margin:0;line-height:1.5"></p>
|
||
</div>
|
||
</div>
|
||
<div class="form-row-2">
|
||
<div class="form-field-stack">
|
||
<label for="archive_name_auto">档案名称</label>
|
||
<input id="archive_name_auto" placeholder="根据档案类型自动生成" readonly value="<?= e((string)($editing['name'] ?? '')) ?>">
|
||
</div>
|
||
<div class="form-field-stack">
|
||
<label for="archive_directory_id">三级目录</label>
|
||
<select name="directory_id" id="archive_directory_id">
|
||
<option value="">自动(按档案类型与表单字段创建三级目录)</option>
|
||
<?php foreach ($dirs as $d): if ((int)$d['level'] !== 3) continue; ?>
|
||
<option value="<?= (int)$d['id'] ?>" <?= ((int)($editing['directory_id'] ?? $dirId) === (int)$d['id']) ? 'selected' : '' ?>>
|
||
<?= e($dirPath[(int)$d['id']] ?? $d['name']) ?>
|
||
</option>
|
||
<?php endforeach; ?>
|
||
</select>
|
||
</div>
|
||
</div>
|
||
<div class="form-row-2">
|
||
<div class="form-field-stack">
|
||
<label for="loan_institution_select" id="loan_institution_label">贷款机构</label>
|
||
<select name="loan_institution" id="loan_institution_select">
|
||
<option value="">请选择贷款机构</option>
|
||
<?php foreach ($loanInstitutions as $li): ?>
|
||
<option value="<?= e($li) ?>" <?= ((string)($editing['loan_institution'] ?? '') === $li) ? 'selected' : '' ?>><?= e($li) ?></option>
|
||
<?php endforeach; ?>
|
||
</select>
|
||
<input type="hidden" name="loan_institutions_json" id="loan_institutions_json" value="">
|
||
<p class="small muted" style="margin:0">
|
||
<a href="javascript:void(0)" onclick="addInstitutionOption()">+ 添加机构</a>
|
||
<span class="muted"> | </span>
|
||
<a href="javascript:void(0)" onclick="removeInstitutionOption()">- 删除当前机构</a>
|
||
</p>
|
||
</div>
|
||
<div class="form-field-stack">
|
||
<label for="borrower_input">借款主体</label>
|
||
<input name="borrower" id="borrower_input" placeholder="借款主体" value="<?= e((string)($editing['borrower'] ?? '')) ?>">
|
||
</div>
|
||
</div>
|
||
<div class="form-row-2">
|
||
<div class="form-field-stack">
|
||
<label for="credit_limit_input">授信额度</label>
|
||
<input type="number" step="0.01" name="credit_limit" id="credit_limit_input" placeholder="授信额度" value="<?= e((string)($editing['credit_limit'] ?? '')) ?>">
|
||
</div>
|
||
<div class="form-field-stack">
|
||
<label for="remaining_limit_display">剩余额度</label>
|
||
<input type="text" id="remaining_limit_display" value="<?= e(amount($editing && $editing['remaining_limit'] !== null ? (float)$editing['remaining_limit'] : null)) ?>" placeholder="剩余额度自动计算" readonly>
|
||
</div>
|
||
</div>
|
||
<div class="form-row-2">
|
||
<div class="date-labeled">
|
||
<span class="date-fixed-label">起始日</span>
|
||
<input type="date" name="start_date" id="archive_start_date" value="<?= e((string)($editing['start_date'] ?? '')) ?>">
|
||
</div>
|
||
<div class="date-labeled">
|
||
<span class="date-fixed-label">到期日</span>
|
||
<input type="date" name="end_date" id="loan_end_date" value="<?= e((string)($editing['end_date'] ?? '')) ?>">
|
||
</div>
|
||
</div>
|
||
<div class="form-row-2">
|
||
<div class="form-field-stack">
|
||
<label for="guarantee_type_input">担保类型</label>
|
||
<input name="guarantee_type" id="guarantee_type_input" placeholder="担保类型" value="<?= e((string)($editing['guarantee_type'] ?? '')) ?>">
|
||
</div>
|
||
<div class="form-field-stack">
|
||
<label for="guarantor_input">担保人</label>
|
||
<input name="guarantor" id="guarantor_input" placeholder="担保人" value="<?= e((string)($editing['guarantor'] ?? '')) ?>">
|
||
</div>
|
||
</div>
|
||
<div class="form-row-3">
|
||
<div class="form-field-stack">
|
||
<label for="annual_rate_input">年利率</label>
|
||
<input type="number" step="0.01" name="annual_rate" id="annual_rate_input" placeholder="年利率" value="<?= e((string)($editing['annual_rate'] ?? '')) ?>">
|
||
</div>
|
||
<div class="form-field-stack">
|
||
<label for="interest_mode_select">结息方式</label>
|
||
<select name="interest_mode" id="interest_mode_select">
|
||
<option value="" disabled <?= $financeCfg['interest_mode'] === '' ? 'selected' : '' ?> style="color:#9ca3af">请选择</option>
|
||
<option value="monthly" <?= $financeCfg['interest_mode'] === 'monthly' ? 'selected' : '' ?>>按月计息</option>
|
||
<option value="quarterly" <?= $financeCfg['interest_mode'] === 'quarterly' ? 'selected' : '' ?>>按季计息</option>
|
||
<option value="bullet" <?= $financeCfg['interest_mode'] === 'bullet' ? 'selected' : '' ?>>到期一次性付清</option>
|
||
</select>
|
||
</div>
|
||
<div class="form-field-stack">
|
||
<label for="settlement_day">结息日</label>
|
||
<input type="number" min="1" max="28" name="settlement_day" id="settlement_day" placeholder="1-28" value="<?= (int)$financeCfg['settlement_day'] ?>">
|
||
</div>
|
||
</div>
|
||
<?php
|
||
$endDateVal = (string)($editing['end_date'] ?? '');
|
||
$overdueSelectable = $endDateVal !== '' && $endDateVal < date('Y-m-d');
|
||
$earlySettleSelectable = $endDateVal !== '' && $endDateVal > date('Y-m-d');
|
||
?>
|
||
<div class="form-row-overdue">
|
||
<div class="form-field-stack">
|
||
<label for="is_overdue">是否逾期</label>
|
||
<select name="is_overdue" id="is_overdue" <?= $overdueSelectable ? '' : 'disabled' ?>>
|
||
<option value="0" <?= empty($financeCfg['manual_overdue']) ? 'selected' : '' ?>><?= $overdueSelectable ? '否' : '否(未超到期日不可选)' ?></option>
|
||
<option value="1" <?= !empty($financeCfg['manual_overdue']) ? 'selected' : '' ?>>是</option>
|
||
</select>
|
||
</div>
|
||
<div class="form-field-stack form-col-float">
|
||
<label for="overdue_float_pct">上浮%</label>
|
||
<input type="number" step="0.01" min="0" name="overdue_float_pct" id="overdue_float_pct" value="<?= e((string)$financeCfg['overdue_float_pct']) ?>" title="逾期利息上浮百分比">
|
||
</div>
|
||
<div class="form-field-stack">
|
||
<label for="plan_enabled">是否有分期还款计划</label>
|
||
<select name="plan_enabled" id="plan_enabled">
|
||
<option value="0" <?= empty($planCfg['enabled']) ? 'selected' : '' ?>>否</option>
|
||
<option value="1" <?= !empty($planCfg['enabled']) ? 'selected' : '' ?>>是</option>
|
||
</select>
|
||
</div>
|
||
</div>
|
||
<div class="form-row-2">
|
||
<div class="form-field-stack">
|
||
<label for="is_early_settled">是否提前还款</label>
|
||
<select name="is_early_settled" id="is_early_settled" <?= $earlySettleSelectable ? '' : 'disabled' ?>>
|
||
<option value="0" <?= empty($financeCfg['manual_early_settled']) ? 'selected' : '' ?>><?= $earlySettleSelectable ? '否' : '否(仅到期日前可选提前还款/标已完成)' ?></option>
|
||
<option value="1" <?= !empty($financeCfg['manual_early_settled']) ? 'selected' : '' ?>>是(状态设为已完成)</option>
|
||
</select>
|
||
</div>
|
||
</div>
|
||
<div class="full form-field-stack">
|
||
<label for="archive_remark">备注</label>
|
||
<textarea name="archive_remark" id="archive_remark" rows="3" placeholder="可填写补充说明、内部备忘等"><?= e((string)($financeCfg['remark'] ?? '')) ?></textarea>
|
||
</div>
|
||
<div class="full" id="plan_wrap" style="<?= empty($planCfg['enabled']) ? 'display:none' : '' ?>">
|
||
<table id="plan_table">
|
||
<tr><th>还款日期</th><th>还款金额</th><th>状态操作</th><th>延期日期</th><th>行操作</th></tr>
|
||
<?php foreach ($planRows as $row): ?>
|
||
<tr>
|
||
<td>
|
||
<input type="hidden" name="plan_id[]" value="<?= e((string)($row['id'] ?? '')) ?>">
|
||
<input type="date" name="plan_due_date[]" value="<?= e((string)($row['due_date'] ?? '')) ?>" onchange="syncPlanAction(this)">
|
||
</td>
|
||
<td><input type="number" step="0.01" name="plan_amount[]" value="<?= e((string)($row['amount'] ?? '')) ?>"></td>
|
||
<td>
|
||
<?php $st = (string)($row['status'] ?? 'pending'); ?>
|
||
<select name="plan_status[]" onchange="syncDelayInput(this)">
|
||
<option value="pending" <?= $st === 'pending' ? 'selected' : '' ?>>未还款</option>
|
||
<option value="paid" <?= $st === 'paid' ? 'selected' : '' ?>><?= (!empty($row['due_date']) && $row['due_date'] > date('Y-m-d')) ? '提前还款' : '按期还款' ?></option>
|
||
<option value="delayed" <?= $st === 'delayed' ? 'selected' : '' ?>>延期</option>
|
||
<option value="overdue" <?= $st === 'overdue' ? 'selected' : '' ?>>逾期</option>
|
||
</select>
|
||
</td>
|
||
<td><input type="date" name="plan_delay_date[]" value="" <?= $st === 'delayed' ? '' : 'disabled' ?>></td>
|
||
<td><button type="button" class="btn-danger" onclick="this.closest('tr').remove()">删除</button></td>
|
||
</tr>
|
||
<?php endforeach; ?>
|
||
</table>
|
||
<p><button type="button" class="btn-gray" onclick="addPlanRow()">增加一期</button></p>
|
||
<p class="small muted">说明:到期日前可选“提前还款”;到期日后可选“按期还款/延期/逾期”。逾期未归还计划本金按上浮利率计息,剩余额度按已还款计划与手动归还本金自动计算。</p>
|
||
</div>
|
||
<?php if ($editing): ?>
|
||
<div class="full" style="border-top:1px solid var(--line);padding-top:12px;margin-top:4px">
|
||
<h4 class="section-title" style="margin:0 0 10px;font-size:15px">贷款延期与归还</h4>
|
||
<div class="form-row-2">
|
||
<div class="form-field-stack">
|
||
<label for="loan_extend_to">延期新到期日(选填)</label>
|
||
<input type="date" name="loan_extend_to" id="loan_extend_to" value="">
|
||
<p class="small muted" style="margin:0">填写后保存为新的到期日,须不早于当前到期日与起始日。</p>
|
||
</div>
|
||
<?php if ($formKind === ARCHIVE_KIND_BANK_LOAN): ?>
|
||
<div class="form-field-stack">
|
||
<label>已累计归还本金 / 利息(元)</label>
|
||
<p class="small" style="margin:0"><?= e(amount((float)$financeCfg['manual_principal_repaid'])) ?> / <?= e(amount((float)$financeCfg['manual_interest_paid'])) ?></p>
|
||
</div>
|
||
<?php else: ?>
|
||
<div class="form-field-stack">
|
||
<label class="muted">对外担保</label>
|
||
<p class="small muted" style="margin:0">可仅使用延期;本金利息归还适用于银行贷款。</p>
|
||
</div>
|
||
<?php endif; ?>
|
||
</div>
|
||
<?php if ($formKind === ARCHIVE_KIND_BANK_LOAN): ?>
|
||
<div class="form-row-2">
|
||
<div class="form-field-stack">
|
||
<label for="principal_repayment_now">本次归还本金(元)</label>
|
||
<input type="number" step="0.01" min="0" name="principal_repayment_now" id="principal_repayment_now" placeholder="0" value="">
|
||
</div>
|
||
<div class="form-field-stack">
|
||
<label for="interest_repayment_now">本次归还利息(元)</label>
|
||
<input type="number" step="0.01" min="0" name="interest_repayment_now" id="interest_repayment_now" placeholder="0" value="">
|
||
</div>
|
||
</div>
|
||
<p class="small muted" style="margin:0">归还本金从剩余额度中扣减;归还利息计入「已归还利息」,减少当前应付与结息日应付利息。</p>
|
||
<?php endif; ?>
|
||
</div>
|
||
<?php endif; ?>
|
||
<?php if (!$editing): ?>
|
||
<div class="full">
|
||
<label>上传文件(新建时可直接选择)</label>
|
||
<input type="file" name="new_files[]" multiple>
|
||
<p class="small muted">点击“创建”后会自动上传并在编辑页文件管理列表中显示。</p>
|
||
</div>
|
||
<?php endif; ?>
|
||
<div class="form-foot">
|
||
<a class="btn btn--plain" href="index.php?action=home">取消</a>
|
||
<span class="spacer"></span>
|
||
<button type="submit" class="btn btn--primary"><?= $editing ? '保存' : '创建' ?></button>
|
||
</div>
|
||
</form>
|
||
</div>
|
||
<?php if ($editArchiveId > 0): ?>
|
||
<div class="card" style="margin-top:12px">
|
||
<h3 class="section-title">文件管理(编辑页)</h3>
|
||
<form method="post" action="index.php?action=upload_file" enctype="multipart/form-data">
|
||
<input type="hidden" name="archive_id" value="<?= $editArchiveId ?>">
|
||
<input type="file" name="files[]" multiple required>
|
||
<button type="submit">上传并刷新列表</button>
|
||
</form>
|
||
<table style="margin-top:8px">
|
||
<tr><th>文件名</th><th>大小</th><th>重命名</th><th>操作</th></tr>
|
||
<?php foreach ($editFiles as $f): ?>
|
||
<tr>
|
||
<td><?= e((string)$f['original_name']) ?></td>
|
||
<td><?= number_format(((int)$f['file_size']) / 1024, 1) ?> KB</td>
|
||
<td>
|
||
<form method="post" action="index.php?action=rename_file" class="inline">
|
||
<input type="hidden" name="file_id" value="<?= (int)$f['id'] ?>">
|
||
<input name="new_name" value="<?= e((string)pathinfo((string)$f['original_name'], PATHINFO_FILENAME)) ?>" placeholder="新文件名" required>
|
||
<button class="btn-gray" type="submit">重命名</button>
|
||
</form>
|
||
</td>
|
||
<td>
|
||
<div class="icon-actions">
|
||
<?php if (canPreview((string)$f['file_ext'])): ?><a class="icon-btn" target="_blank" href="index.php?action=preview_file&id=<?= (int)$f['id'] ?>">预览</a><?php endif; ?>
|
||
<a class="icon-btn" href="index.php?action=download_file&id=<?= (int)$f['id'] ?>">下载</a>
|
||
<a class="icon-btn danger" href="index.php?action=delete_file&id=<?= (int)$f['id'] ?>" onclick="return confirm('确认删除?')">删除</a>
|
||
</div>
|
||
</td>
|
||
</tr>
|
||
<?php endforeach; ?>
|
||
<?php if (empty($editFiles)): ?>
|
||
<tr><td colspan="4" class="muted">暂无文件</td></tr>
|
||
<?php endif; ?>
|
||
</table>
|
||
</div>
|
||
<?php endif; ?>
|
||
<script>
|
||
(function(){
|
||
var sel=document.getElementById('plan_enabled');
|
||
var wrap=document.getElementById('plan_wrap');
|
||
function setPlanVisibility(){
|
||
if(sel&&wrap){wrap.style.display=sel.value==='1'?'block':'none';}
|
||
}
|
||
if(sel&&wrap){
|
||
sel.addEventListener('change',setPlanVisibility);
|
||
}
|
||
setPlanVisibility();
|
||
var archiveKind=document.getElementById('archive_kind');
|
||
var loanInstLabel=document.getElementById('loan_institution_label');
|
||
var dirHint=document.getElementById('archive_auto_dir_hint');
|
||
function syncArchiveKindUi(){
|
||
if(!dirHint){return;}
|
||
if(archiveKind&&archiveKind.value==='external_guarantee'){
|
||
dirHint.textContent='未手动选择目录时:将按「对外担保 / 起始日年份 / 担保人」创建或归入目录(三级目录为担保人名称,非借款主体)。下方「债权人」为选填。';
|
||
if(loanInstLabel){loanInstLabel.textContent='债权人(选填)';}
|
||
}else{
|
||
dirHint.textContent='未手动选择目录时:将按「银行贷款 / 起始日年份 / 贷款机构」创建或归入目录。';
|
||
if(loanInstLabel){loanInstLabel.textContent='贷款机构';}
|
||
}
|
||
syncAutoArchiveName();
|
||
}
|
||
if(archiveKind){archiveKind.addEventListener('change',syncArchiveKindUi);}
|
||
syncArchiveKindUi();
|
||
document.querySelectorAll('input[name="plan_due_date[]"]').forEach(function(x){syncPlanAction(x);});
|
||
document.querySelectorAll('select[name="plan_status[]"]').forEach(function(x){syncDelayInput(x);});
|
||
var interestMode=document.querySelector('select[name="interest_mode"]');
|
||
var settlementDay=document.getElementById('settlement_day');
|
||
var borrowerInput=document.getElementById('borrower_input');
|
||
var creditInput=document.getElementById('credit_limit_input');
|
||
var archiveNameAuto=document.getElementById('archive_name_auto');
|
||
var guarantorInput=document.getElementById('guarantor_input');
|
||
var institutionSelect=document.getElementById('loan_institution_select');
|
||
var institutionsJson=document.getElementById('loan_institutions_json');
|
||
var loanEndDate=document.getElementById('loan_end_date');
|
||
var isOverdue=document.getElementById('is_overdue');
|
||
var isEarlySettled=document.getElementById('is_early_settled');
|
||
var overduePct=document.getElementById('overdue_float_pct');
|
||
function syncInterestMode(){
|
||
if(!interestMode||!settlementDay){return;}
|
||
if(interestMode.value==='bullet'){
|
||
settlementDay.value='';
|
||
settlementDay.disabled=true;
|
||
settlementDay.placeholder='到期一次性付息';
|
||
}else{
|
||
settlementDay.disabled=false;
|
||
if(!settlementDay.value){settlementDay.value='21';}
|
||
settlementDay.placeholder=interestMode.value==='quarterly'?'每季几号(1-28)':'每月几号(1-28)';
|
||
}
|
||
}
|
||
if(interestMode){interestMode.addEventListener('change',syncInterestMode);}
|
||
syncInterestMode();
|
||
function syncOverdueAvailability(){
|
||
if(!loanEndDate||!isOverdue){return;}
|
||
var endDate=loanEndDate.value;
|
||
var today=new Date().toISOString().slice(0,10);
|
||
var canSelect=(endDate && endDate<today);
|
||
if(!canSelect){
|
||
isOverdue.value='0';
|
||
isOverdue.disabled=true;
|
||
if(isOverdue.options.length>0){isOverdue.options[0].text='否(未超到期日不可选)';}
|
||
}else{
|
||
isOverdue.disabled=false;
|
||
if(isOverdue.options.length>0 && isOverdue.options[0].value==='0'){isOverdue.options[0].text='否';}
|
||
}
|
||
}
|
||
if(loanEndDate){loanEndDate.addEventListener('change',syncOverdueAvailability);}
|
||
syncOverdueAvailability();
|
||
function syncEarlySettleAvailability(){
|
||
if(!loanEndDate||!isEarlySettled){return;}
|
||
var endDate=loanEndDate.value;
|
||
var today=new Date().toISOString().slice(0,10);
|
||
var canSelect=(endDate && endDate>today);
|
||
if(!canSelect){
|
||
isEarlySettled.value='0';
|
||
isEarlySettled.disabled=true;
|
||
if(isEarlySettled.options.length>0){isEarlySettled.options[0].text='否(仅到期日前可选)';}
|
||
}else{
|
||
isEarlySettled.disabled=false;
|
||
if(isEarlySettled.options.length>0 && isEarlySettled.options[0].value==='0'){isEarlySettled.options[0].text='否';}
|
||
}
|
||
}
|
||
function syncMutualStatusSelection(){
|
||
if(!isOverdue||!isEarlySettled){return;}
|
||
if(isEarlySettled.value==='1'){
|
||
isOverdue.value='0';
|
||
isOverdue.disabled=true;
|
||
}else{
|
||
syncOverdueAvailability();
|
||
}
|
||
}
|
||
if(loanEndDate){loanEndDate.addEventListener('change',syncEarlySettleAvailability);}
|
||
if(isEarlySettled){isEarlySettled.addEventListener('change',syncMutualStatusSelection);}
|
||
syncEarlySettleAvailability();
|
||
syncMutualStatusSelection();
|
||
|
||
function syncAutoArchiveName(){
|
||
if(!creditInput||!archiveNameAuto||!archiveKind){return;}
|
||
var c=parseFloat(creditInput.value||'');
|
||
if(isNaN(c)){ archiveNameAuto.value=''; return; }
|
||
var wan=(c/10000).toFixed(2).replace(/\.00$/,'').replace(/(\.\d)0$/,'$1');
|
||
if(archiveKind.value==='external_guarantee'){
|
||
if(!guarantorInput){return;}
|
||
var g=(guarantorInput.value||'').trim();
|
||
if(!g){ archiveNameAuto.value=''; return; }
|
||
archiveNameAuto.value=g+'对外担保'+wan+'万元';
|
||
}else{
|
||
if(!borrowerInput){return;}
|
||
var b=(borrowerInput.value||'').trim();
|
||
if(!b){ archiveNameAuto.value=''; return; }
|
||
archiveNameAuto.value=b+wan+'万元';
|
||
}
|
||
}
|
||
if(borrowerInput){borrowerInput.addEventListener('input',syncAutoArchiveName);}
|
||
if(guarantorInput){guarantorInput.addEventListener('input',syncAutoArchiveName);}
|
||
if(creditInput){creditInput.addEventListener('input',syncAutoArchiveName);}
|
||
syncAutoArchiveName();
|
||
|
||
function syncInstitutionsJson(){
|
||
if(!institutionSelect||!institutionsJson){return;}
|
||
var arr=[...institutionSelect.options].map(function(o){return o.value;}).filter(function(v){return v!=='';});
|
||
institutionsJson.value=JSON.stringify(arr);
|
||
}
|
||
window.addInstitutionOption=function(){
|
||
if(!institutionSelect){return;}
|
||
var v=prompt('请输入贷款机构名称');
|
||
if(!v){return;}
|
||
v=v.trim();
|
||
if(!v){return;}
|
||
var exists=[...institutionSelect.options].some(function(o){return o.value===v;});
|
||
if(!exists){
|
||
var op=document.createElement('option');
|
||
op.value=v;op.textContent=v;
|
||
institutionSelect.appendChild(op);
|
||
}
|
||
institutionSelect.value=v;
|
||
syncInstitutionsJson();
|
||
};
|
||
window.removeInstitutionOption=function(){
|
||
if(!institutionSelect){return;}
|
||
var v=institutionSelect.value;
|
||
if(!v){return;}
|
||
if(!confirm('确认删除当前机构选项?')){return;}
|
||
[...institutionSelect.options].forEach(function(o){if(o.value===v){o.remove();}});
|
||
institutionSelect.value='';
|
||
syncInstitutionsJson();
|
||
};
|
||
if(institutionSelect){institutionSelect.addEventListener('change',syncInstitutionsJson);}
|
||
syncInstitutionsJson();
|
||
})();
|
||
function syncPlanAction(el){
|
||
var tr=el.closest('tr'); if(!tr){return;}
|
||
var sel=tr.querySelector('select[name="plan_status[]"]');
|
||
if(!sel){return;}
|
||
var due=el.value;
|
||
var today=new Date().toISOString().slice(0,10);
|
||
var paidOpt=sel.querySelector('option[value="paid"]');
|
||
if(paidOpt){paidOpt.textContent=(due && due>today)?'提前还款':'按期还款';}
|
||
var delayedOpt=sel.querySelector('option[value="delayed"]');
|
||
var overdueOpt=sel.querySelector('option[value="overdue"]');
|
||
var canAfterDue=(due && due<=today);
|
||
if(delayedOpt){
|
||
delayedOpt.disabled=!canAfterDue;
|
||
if(!canAfterDue && sel.value==='delayed'){sel.value='pending';}
|
||
}
|
||
if(overdueOpt){
|
||
overdueOpt.disabled=!canAfterDue;
|
||
if(!canAfterDue && sel.value==='overdue'){sel.value='pending';}
|
||
}
|
||
syncDelayInput(sel);
|
||
}
|
||
function syncDelayInput(el){
|
||
var tr=el.closest('tr'); if(!tr){return;}
|
||
var d=tr.querySelector('input[name="plan_delay_date[]"]');
|
||
if(!d){return;}
|
||
d.disabled=(el.value!=='delayed');
|
||
if(d.disabled){d.value='';}
|
||
}
|
||
function addPlanRow(){
|
||
var tb=document.getElementById('plan_table');
|
||
if(!tb){return;}
|
||
var tr=document.createElement('tr');
|
||
tr.innerHTML='<td><input type="hidden" name="plan_id[]" value=""><input type="date" name="plan_due_date[]" onchange="syncPlanAction(this)"></td><td><input type="number" step="0.01" name="plan_amount[]"></td><td><select name="plan_status[]" onchange="syncDelayInput(this)"><option value="pending">未还款</option><option value="paid">提前还款</option><option value="delayed">延期</option><option value="overdue">逾期</option></select></td><td><input type="date" name="plan_delay_date[]" disabled></td><td><button type="button" class="btn-danger" onclick="this.closest(\'tr\').remove()">删除</button></td>';
|
||
tb.appendChild(tr);
|
||
}
|
||
</script>
|
||
<?php pageFooter(); exit;
|
||
}
|
||
|
||
if ($action === 'users') {
|
||
requireAdmin();
|
||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||
$do = $_POST['do'] ?? '';
|
||
if ($do === 'create') {
|
||
$u = trim((string)$_POST['username']);
|
||
$p = (string)$_POST['password'];
|
||
if (!preg_match('/^[a-zA-Z0-9_]{4,20}$/', $u) || strlen($p) < 6 || strlen($p) > 20) {
|
||
flash('error', '用户名或密码格式不正确');
|
||
} else {
|
||
try {
|
||
$pdo->prepare('INSERT INTO users(username,password_hash,role,created_at) VALUES(:u,:p,:r,:c)')
|
||
->execute([':u' => $u, ':p' => password_hash($p, PASSWORD_DEFAULT), ':r' => 'user', ':c' => now()]);
|
||
flash('ok', '用户已创建');
|
||
} catch (Throwable $e) {
|
||
flash('error', '用户名已存在');
|
||
}
|
||
}
|
||
} elseif ($do === 'role') {
|
||
$uid = (int)$_POST['uid'];
|
||
$role = $_POST['role'] === 'admin' ? 'admin' : 'user';
|
||
$pdo->prepare('UPDATE users SET role=:r WHERE id=:id')->execute([':r' => $role, ':id' => $uid]);
|
||
flash('ok', '角色已更新');
|
||
} elseif ($do === 'reset') {
|
||
$uid = (int)$_POST['uid'];
|
||
$np = (string)$_POST['new_password'];
|
||
if (strlen($np) < 6 || strlen($np) > 20) {
|
||
flash('error', '密码长度需为6-20');
|
||
} else {
|
||
$pdo->prepare('UPDATE users SET password_hash=:p WHERE id=:id')->execute([':p' => password_hash($np, PASSWORD_DEFAULT), ':id' => $uid]);
|
||
flash('ok', '密码已重置');
|
||
}
|
||
} elseif ($do === 'delete') {
|
||
$uid = (int)$_POST['uid'];
|
||
if ($uid === (int)$me['id']) {
|
||
flash('error', '不能删除当前登录账号');
|
||
} else {
|
||
$pdo->prepare('DELETE FROM users WHERE id=:id')->execute([':id' => $uid]);
|
||
flash('ok', '用户已删除');
|
||
}
|
||
}
|
||
header('Location: index.php?action=users');
|
||
exit;
|
||
}
|
||
$users = $pdo->query('SELECT id,username,role,created_at FROM users ORDER BY id ASC')->fetchAll();
|
||
pageHeader($me); ?>
|
||
<div class="page-head">
|
||
<div>
|
||
<h1 class="page-title">用户管理</h1>
|
||
<div class="page-sub">仅管理员可见;角色、重置密码、删除规则不变</div>
|
||
</div>
|
||
</div>
|
||
<div class="card">
|
||
<form method="post" class="toolbar-left" style="margin-bottom:14px;flex-wrap:wrap">
|
||
<input type="hidden" name="do" value="create">
|
||
<input name="username" placeholder="用户名(4-20位字母数字下划线)" required>
|
||
<input name="password" placeholder="初始密码(6-20位)" required>
|
||
<button type="submit" class="btn btn--primary">创建用户</button>
|
||
</form>
|
||
<div class="table-wrap"><table>
|
||
<thead><tr><th>ID</th><th>用户名</th><th>角色</th><th>创建时间</th><th>操作</th></tr></thead>
|
||
<tbody>
|
||
<?php foreach ($users as $u): ?>
|
||
<tr>
|
||
<td class="num"><?= (int)$u['id'] ?></td><td><?= e(maskUsername((string)$u['username'])) ?></td>
|
||
<td>
|
||
<form method="post" class="inline">
|
||
<input type="hidden" name="do" value="role">
|
||
<input type="hidden" name="uid" value="<?= (int)$u['id'] ?>">
|
||
<select name="role" onchange="this.form.submit()">
|
||
<option value="user" <?= $u['role'] === 'user' ? 'selected' : '' ?>>普通用户</option>
|
||
<option value="admin" <?= $u['role'] === 'admin' ? 'selected' : '' ?>>管理员</option>
|
||
</select>
|
||
</form>
|
||
</td>
|
||
<td class="num"><?= e($u['created_at']) ?></td>
|
||
<td>
|
||
<form method="post" class="inline">
|
||
<input type="hidden" name="do" value="reset">
|
||
<input type="hidden" name="uid" value="<?= (int)$u['id'] ?>">
|
||
<input name="new_password" placeholder="新密码" required>
|
||
<button class="btn btn--plain" type="submit">重置密码</button>
|
||
</form>
|
||
<form method="post" class="inline" onsubmit="return confirm('确认删除用户?')">
|
||
<input type="hidden" name="do" value="delete">
|
||
<input type="hidden" name="uid" value="<?= (int)$u['id'] ?>">
|
||
<button class="btn-danger" type="submit">删除</button>
|
||
</form>
|
||
</td>
|
||
</tr>
|
||
<?php endforeach; ?>
|
||
</tbody>
|
||
</table></div>
|
||
</div>
|
||
<?php pageFooter(); exit;
|
||
}
|
||
|
||
if ($action === 'profile') {
|
||
requireLogin();
|
||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||
$cur = (string)($_POST['current_password'] ?? '');
|
||
$new = (string)($_POST['new_password'] ?? '');
|
||
$confirm = (string)($_POST['confirm_password'] ?? '');
|
||
$stmt = $pdo->prepare('SELECT * FROM users WHERE id=:id');
|
||
$stmt->execute([':id' => (int)$me['id']]);
|
||
$u = $stmt->fetch();
|
||
if (!$u || !password_verify($cur, $u['password_hash'])) {
|
||
flash('error', '当前密码错误');
|
||
} elseif (strlen($new) < 6 || strlen($new) > 20) {
|
||
flash('error', '新密码长度需为6-20位');
|
||
} elseif ($new !== $confirm) {
|
||
flash('error', '新密码与确认密码不一致');
|
||
} else {
|
||
$pdo->prepare('UPDATE users SET password_hash=:p WHERE id=:id')->execute([':p' => password_hash($new, PASSWORD_DEFAULT), ':id' => (int)$me['id']]);
|
||
flash('ok', '密码修改成功');
|
||
}
|
||
header('Location: index.php?action=profile');
|
||
exit;
|
||
}
|
||
pageHeader($me);
|
||
$isAdmin = (($me['role'] ?? '') === 'admin');
|
||
$masked = maskUsername((string)$me['username']);
|
||
$roleTxt = roleLabel((string)$me['role']);
|
||
$avatar = $isAdmin ? '管' : '办';
|
||
$perms = $isAdmin
|
||
? [['档案、附件、还款记录全量管理', true], ['用户管理(创建 / 重置密码)', true], ['删除审批(双管理员闭环,申请人与审批人分离)', true], ['全部查询、统计与计算工具', true]]
|
||
: [['档案查阅、导出与计算工具', true], ['删除需提交管理员审批,不能自审', true], ['用户管理(仅管理员)', false], ['删除审批(仅管理员)', false]];
|
||
?>
|
||
<div class="page-head">
|
||
<div>
|
||
<h1 class="page-title">个人设置</h1>
|
||
<div class="page-sub">账户中心 · 身份信息、安全与偏好</div>
|
||
</div>
|
||
</div>
|
||
<div class="acct-grid">
|
||
<div class="editor-card id-card">
|
||
<div class="id-head">
|
||
<span class="id-avatar" aria-hidden="true"><?= e($avatar) ?></span>
|
||
<div>
|
||
<div class="id-name"><?= e($roleTxt) ?></div>
|
||
<div class="id-acct num"><?= e($masked) ?> · <?= e($roleTxt) ?></div>
|
||
</div>
|
||
</div>
|
||
<div class="id-rows">
|
||
<div class="id-row"><span>账号</span><div class="num"><?= e($masked) ?><span class="ro-tag">只读</span></div></div>
|
||
<div class="id-row"><span>角色</span><div><?= e($roleTxt) ?><span class="ro-tag">只读</span></div></div>
|
||
<div class="id-row"><span>注册时间</span><div class="num"><?= e((string)($me['created_at'] ?? '')) ?><span class="ro-tag">只读</span></div></div>
|
||
<div class="id-row"><span>权限摘要</span><div>
|
||
<ul class="perm-list">
|
||
<?php foreach ($perms as $p): ?>
|
||
<li<?= $p[1] ? '' : ' data-off' ?>><?= e($p[0]) ?></li>
|
||
<?php endforeach; ?>
|
||
</ul>
|
||
</div></div>
|
||
</div>
|
||
<div class="calc-note" style="margin-top:14px; padding-top:12px; border-top:1px solid var(--hairline);">
|
||
身份信息由管理员在「用户管理」中维护;账户仅含账号与角色,无姓名 / 机构等资料字段。
|
||
</div>
|
||
</div>
|
||
<div class="acct-col">
|
||
<div class="editor-card">
|
||
<div class="section-title" style="margin-top:0;">密码与安全</div>
|
||
<div class="calc-group-note" style="margin:-4px 0 12px;">校验语义与原系统一致:验证当前密码 · 新密码 6-20 位 · 两次输入须一致</div>
|
||
<form method="post">
|
||
<div class="form-grid">
|
||
<div class="form-field"><label for="p-old">当前密码</label><input id="p-old" name="current_password" type="password" placeholder="请输入当前密码" autocomplete="current-password" required><div class="field-err"></div></div>
|
||
<div></div>
|
||
<div class="form-field"><label for="p-new">新密码(6-20 位)</label><input id="p-new" name="new_password" type="password" placeholder="6-20 位" autocomplete="new-password" required><div class="field-err"></div></div>
|
||
<div class="form-field"><label for="p-new2">确认新密码</label><input id="p-new2" name="confirm_password" type="password" placeholder="再次输入新密码" autocomplete="new-password" required><div class="field-err"></div></div>
|
||
</div>
|
||
<div style="display:flex; justify-content:space-between; align-items:center; margin-top:12px; flex-wrap:wrap; gap:10px;">
|
||
<span class="cr-hint" style="margin:0;">初始密码由管理员发放,首次登录后请在此修改;忘记密码请联系管理员重置。</span>
|
||
<button type="submit" class="btn btn--primary">保存新密码</button>
|
||
</div>
|
||
</form>
|
||
</div>
|
||
<div class="editor-card">
|
||
<div class="section-title" style="margin-top:0;">主题与偏好</div>
|
||
<div class="setting-row" style="border-top:0; padding-top:0;">
|
||
<div>日间 / 夜间主题<div class="hint">与左下角账户区的主题切换同源,即时生效并记住选择</div></div>
|
||
<div class="seg" role="group" aria-label="主题偏好">
|
||
<button type="button" data-theme-btn="day">日间</button>
|
||
<button type="button" data-theme-btn="night">夜间</button>
|
||
</div>
|
||
</div>
|
||
<div class="setting-row">
|
||
<div>到期与还款提醒<div class="hint">原系统无独立提醒开关,任务中心按到期日自动列出;此处仅保持版面,不写入设置。</div></div>
|
||
<button type="button" class="switch" role="switch" aria-checked="true" aria-disabled="true" aria-label="到期与还款提醒(只读占位)"></button>
|
||
</div>
|
||
</div>
|
||
<div class="editor-card">
|
||
<div class="section-title" style="margin-top:0;">会话</div>
|
||
<div class="setting-row" style="border-top:0; padding-top:0;">
|
||
<div>退出登录<div class="hint">结束当前会话并返回登录页;左下账户区亦有同一入口</div></div>
|
||
<a class="btn btn--plain" href="index.php?action=logout" style="color:var(--red); border:1px solid var(--red-line);">退出登录</a>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<?php pageFooter(); exit;
|
||
}
|
||
|
||
if ($action === 'quick_queries') {
|
||
requireLogin();
|
||
pageHeader($me); ?>
|
||
<div class="page-head">
|
||
<div>
|
||
<h1 class="page-title">统计查询</h1>
|
||
<div class="page-sub">口径说明;统计语义与原系统一致</div>
|
||
</div>
|
||
</div>
|
||
<?php fusionQueryTabs('query_totals'); ?>
|
||
<div class="card">
|
||
<div class="section-title" style="margin-top:0">便捷查询说明</div>
|
||
<?= quickQueryMenuHtml() ?>
|
||
<p class="small muted" style="margin-top:12px">应付本息:统计<strong>履行中及逾期中</strong>银行贷款按机构当前应付利息、本结息周期整段结息日应付利息,以及当月还款计划金额。</p>
|
||
<p class="small muted">对外担保:按主体名称检索“该主体作为担保人”的<strong>履行中及逾期中</strong>银行贷款与对外担保档案。</p>
|
||
<p class="small muted">查询总金额:按选项汇总<strong>履行中及逾期中</strong>档案的授信总额与剩余额度合计。</p>
|
||
<p class="small muted">利息计算器 / 承兑贴现计算器:本地浏览器计算,结果仅供参考。</p>
|
||
<p class="small muted">历史还款记录:汇总分期计划中「已还款」本金及状态为「已完成」档案的授信本金规模。</p>
|
||
</div>
|
||
<?php pageFooter(); exit;
|
||
}
|
||
|
||
if ($action === 'repayment_history') {
|
||
requireLogin();
|
||
$rowsAll = $pdo->query('SELECT * FROM archives ORDER BY id DESC')->fetchAll();
|
||
$planAll = loadRepaymentPlans();
|
||
$financeAll = loadFinanceConfigs();
|
||
$hr = qqBuildRepaymentHistory($rowsAll, $planAll, $financeAll);
|
||
$planRows = $hr['plan_rows'];
|
||
$cb = $hr['completed_bank'];
|
||
$cg = $hr['completed_guar'];
|
||
pageHeader($me); ?>
|
||
<div class="page-head">
|
||
<div>
|
||
<h1 class="page-title">统计查询</h1>
|
||
<div class="page-sub">分期已还款与已完成档案本金规模</div>
|
||
</div>
|
||
</div>
|
||
<?php fusionQueryTabs('repayment_history'); ?>
|
||
<div class="card">
|
||
<div class="section-title" style="margin-top:0">汇总</div>
|
||
<div class="table-wrap">
|
||
<table>
|
||
<tr><th>项目</th><th>笔数</th><th>金额合计(元)</th></tr>
|
||
<tr>
|
||
<td>分期还款计划中「已还款」本金</td>
|
||
<td><?= count($planRows) ?></td>
|
||
<td><strong><?= e(amount((float)$hr['plan_paid_total'])) ?></strong></td>
|
||
</tr>
|
||
<tr>
|
||
<td>已完成 — 银行贷款(授信本金规模)</td>
|
||
<td><?= (int)$cb['count'] ?></td>
|
||
<td><strong><?= e(amount((float)$cb['sum_credit'])) ?></strong></td>
|
||
</tr>
|
||
<tr>
|
||
<td>已完成 — 对外担保(授信本金规模)</td>
|
||
<td><?= (int)$cg['count'] ?></td>
|
||
<td><strong><?= e(amount((float)$cg['sum_credit'])) ?></strong></td>
|
||
</tr>
|
||
</table>
|
||
</div>
|
||
<p class="small muted" style="margin:10px 0 14px">「已完成」指合同状态为已完成;授信本金规模为档案授信额度合计,用于对应到期/结清贷款本金规模参考。分期已还与整笔结清可能同时存在,请勿简单相加避免重复理解。</p>
|
||
<div class="section-title">分期计划已还款明细</div>
|
||
<div class="table-wrap">
|
||
<table>
|
||
<tr><th>档案ID</th><th>档案名称</th><th>机构</th><th>借款主体</th><th>计划到期日</th><th>标记还款日</th><th>还款金额</th><th>操作</th></tr>
|
||
<?php foreach ($planRows as $pr): ?>
|
||
<tr>
|
||
<td><?= (int)$pr['archive_id'] ?></td>
|
||
<td><?= e((string)$pr['archive_name']) ?></td>
|
||
<td><?= e((string)$pr['loan_institution']) ?></td>
|
||
<td><?= e((string)$pr['borrower']) ?></td>
|
||
<td><?= e((string)$pr['due_date']) ?></td>
|
||
<td><?= e((string)$pr['paid_at']) ?></td>
|
||
<td><?= e(amount((float)$pr['amount'])) ?></td>
|
||
<td><?php if ((int)$pr['archive_id'] > 0): ?><a class="icon-btn" href="index.php?action=archive_detail&id=<?= (int)$pr['archive_id'] ?>">查看</a><?php else: ?>—<?php endif; ?></td>
|
||
</tr>
|
||
<?php endforeach; ?>
|
||
<?php if (empty($planRows)): ?><tr><td colspan="8"><div class="empty-action"><div class="ea-title">暂无已标记还款的分期计划</div>已还款记录将按机构与档案汇总在此</div></td></tr><?php endif; ?>
|
||
</table>
|
||
</div>
|
||
</div>
|
||
<?php pageFooter(); exit;
|
||
}
|
||
|
||
if ($action === 'interest_calculator') {
|
||
requireLogin();
|
||
pageHeader($me);
|
||
require __DIR__ . '/../app/views/interest_calculator.php';
|
||
pageFooter();
|
||
exit;
|
||
}
|
||
|
||
if ($action === 'acceptance_discount_calculator') {
|
||
requireLogin();
|
||
pageHeader($me);
|
||
require __DIR__ . '/../app/views/acceptance_discount_calculator.php';
|
||
pageFooter();
|
||
exit;
|
||
}
|
||
|
||
if ($action === 'query_totals') {
|
||
requireLogin();
|
||
$scope = (string)($_GET['scope'] ?? 'all_bank');
|
||
$inst = trim((string)($_GET['inst'] ?? ''));
|
||
$allowedScopes = ['all_bank', 'all_guar', 'bank_inst', 'guar_inst'];
|
||
if (!in_array($scope, $allowedScopes, true)) {
|
||
$scope = 'all_bank';
|
||
}
|
||
$rowsAll = $pdo->query('SELECT * FROM archives ORDER BY id DESC')->fetchAll();
|
||
$financeAll = loadFinanceConfigs();
|
||
$tot = qqBuildTotalsQuery($rowsAll, $financeAll, $scope, $inst !== '' ? $inst : null);
|
||
$loanInstitutions = loadLoanInstitutions();
|
||
pageHeader($me); ?>
|
||
<div class="page-head">
|
||
<div>
|
||
<h1 class="page-title">统计查询</h1>
|
||
<div class="page-sub">四口径汇总,统计语义与原系统一致</div>
|
||
</div>
|
||
</div>
|
||
<?php fusionQueryTabs('query_totals'); ?>
|
||
<div class="card">
|
||
<form method="get" class="toolbar-left" style="flex-wrap:wrap;gap:10px;align-items:flex-end">
|
||
<input type="hidden" name="action" value="query_totals">
|
||
<div class="form-field-stack" style="min-width:200px">
|
||
<label for="qt_scope">统计范围</label>
|
||
<select name="scope" id="qt_scope">
|
||
<option value="all_bank" <?= $scope === 'all_bank' ? 'selected' : '' ?>>履行中及逾期中 — 全部银行贷款</option>
|
||
<option value="all_guar" <?= $scope === 'all_guar' ? 'selected' : '' ?>>履行中及逾期中 — 全部对外担保</option>
|
||
<option value="bank_inst" <?= $scope === 'bank_inst' ? 'selected' : '' ?>>履行中及逾期中 — 指定机构的银行贷款</option>
|
||
<option value="guar_inst" <?= $scope === 'guar_inst' ? 'selected' : '' ?>>履行中及逾期中 — 指定机构的对外担保</option>
|
||
</select>
|
||
</div>
|
||
<div class="form-field-stack" id="qt_inst_wrap" style="min-width:220px;<?= ($scope === 'bank_inst' || $scope === 'guar_inst') ? '' : 'opacity:.45' ?>">
|
||
<label for="qt_inst">机构名称(后两项必填)</label>
|
||
<input name="inst" id="qt_inst" list="qt_inst_list" value="<?= e($inst) ?>" placeholder="与档案中贷款机构/债权人一致">
|
||
<datalist id="qt_inst_list">
|
||
<?php foreach ($loanInstitutions as $li): ?><option value="<?= e($li) ?>"><?php endforeach; ?>
|
||
</datalist>
|
||
</div>
|
||
<button type="submit">查询</button>
|
||
</form>
|
||
<script>
|
||
(function(){
|
||
var sel=document.getElementById('qt_scope');
|
||
var wrap=document.getElementById('qt_inst_wrap');
|
||
function sync(){if(!sel||!wrap)return;var need=sel.value==='bank_inst'||sel.value==='guar_inst';wrap.style.opacity=need?'1':'.45';}
|
||
if(sel){sel.addEventListener('change',sync);}sync();
|
||
})();
|
||
</script>
|
||
</div>
|
||
<div class="card" style="margin-top:12px">
|
||
<div class="section-title" style="margin-top:0">汇总结果</div>
|
||
<div class="table-wrap"><table>
|
||
<tr><th>笔数(履行+逾期)</th><th>授信总额合计</th><th>剩余额度合计</th></tr>
|
||
<tr>
|
||
<td><?= (int)($tot['count'] ?? 0) ?></td>
|
||
<td><?= e(amount((float)($tot['sum_credit'] ?? 0))) ?></td>
|
||
<td><?= e(amount((float)($tot['sum_remaining'] ?? 0))) ?></td>
|
||
</tr>
|
||
</table></div>
|
||
<p class="small muted" style="margin-top:10px">统计范围含状态为「履行中」与「逾期中」的档案。授信总额、剩余额度均来自各档案字段;剩余额度含分期已还与手动归还本金后的结果。</p>
|
||
</div>
|
||
<?php pageFooter(); exit;
|
||
}
|
||
|
||
if ($action === 'query_payable_interest') {
|
||
requireLogin();
|
||
$exportFormat = (string)($_GET['format'] ?? '');
|
||
$rowsAll = $pdo->query('SELECT * FROM archives ORDER BY id DESC')->fetchAll();
|
||
$financeAll = loadFinanceConfigs();
|
||
$planAll = loadRepaymentPlans();
|
||
$quickStats = qqBuildPayableInterestData($rowsAll, $financeAll, $planAll);
|
||
$instRows = $quickStats['instRows'];
|
||
$planRows = $quickStats['planRows'];
|
||
$planTotal = (float)($quickStats['planTotal'] ?? 0);
|
||
|
||
if ($exportFormat === 'xlsx' && class_exists('\PhpOffice\PhpSpreadsheet\Spreadsheet') && class_exists('\PhpOffice\PhpSpreadsheet\Writer\Xlsx')) {
|
||
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
|
||
$sheet1 = $spreadsheet->getActiveSheet();
|
||
$sheet1->setTitle('机构利息汇总');
|
||
$sheet1->fromArray(['机构', '笔数(履行+逾期)', '当前应付利息', '结息日', '结息日应付利息'], null, 'A1');
|
||
$sheet1->fromArray(array_map(fn($x) => [$x['loan_institution'], $x['loan_count'], amount((float)$x['current_interest']), (string)($x['settlement_date'] ?? ''), amount((float)$x['settlement_interest'])], $instRows), null, 'A2');
|
||
foreach (range('A', 'E') as $col) { $sheet1->getColumnDimension($col)->setWidth(24); }
|
||
|
||
$sheet2 = $spreadsheet->createSheet();
|
||
$sheet2->setTitle('当月还款计划');
|
||
$sheet2->fromArray(['机构', '档案名称', '借款主体', '到期日', '金额', '状态'], null, 'A1');
|
||
$sheet2->fromArray(array_map(fn($x) => [$x['loan_institution'], $x['archive_name'], $x['borrower'], $x['due_date'], amount((float)$x['amount']), $x['status']], $planRows), null, 'A2');
|
||
$sheet2->setCellValue('A' . (count($planRows) + 3), '合计');
|
||
$sheet2->setCellValue('E' . (count($planRows) + 3), amount($planTotal));
|
||
foreach (range('A', 'F') as $col) { $sheet2->getColumnDimension($col)->setWidth(22); }
|
||
|
||
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
||
header('Content-Disposition: attachment; filename="' . rawurlencode('应付本息查询_' . date('Y-m-d') . '.xlsx') . '"');
|
||
(new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet))->save('php://output');
|
||
exit;
|
||
}
|
||
pageHeader($me); ?>
|
||
<div class="page-head">
|
||
<div>
|
||
<h1 class="page-title">统计查询</h1>
|
||
<div class="page-sub">履行中及逾期中银行贷款利息与当月还款计划</div>
|
||
</div>
|
||
<a class="btn btn--plain" href="index.php?action=query_payable_interest&format=xlsx">导出xlsx</a>
|
||
</div>
|
||
<?php fusionQueryTabs('query_payable_interest'); ?>
|
||
<div class="card">
|
||
<div class="section-title" style="margin-top:0">一、银行贷款机构利息统计(履行中及逾期中)</div>
|
||
<div class="table-wrap"><table>
|
||
<tr><th>机构</th><th>笔数</th><th>当前应付利息</th><th>结息日</th><th title="本结息周期整段应计,非自今日至结息日">结息日应付利息</th></tr>
|
||
<?php foreach ($instRows as $r): ?>
|
||
<tr>
|
||
<td><?= e((string)$r['loan_institution']) ?></td>
|
||
<td><?= (int)$r['loan_count'] ?></td>
|
||
<td><?= e(amount((float)$r['current_interest'])) ?></td>
|
||
<td><?= e((string)($r['settlement_date'] ?? '')) ?></td>
|
||
<td><?= e(amount((float)$r['settlement_interest'])) ?></td>
|
||
</tr>
|
||
<?php endforeach; ?>
|
||
<?php if (!empty($instRows)): ?>
|
||
<?php
|
||
$sumCur = 0.0;
|
||
$sumSettle = 0.0;
|
||
foreach ($instRows as $x) {
|
||
$sumCur += (float)$x['current_interest'];
|
||
$sumSettle += (float)$x['settlement_interest'];
|
||
}
|
||
?>
|
||
<tr><td><strong>合计</strong></td><td>-</td><td><strong><?= e(amount($sumCur)) ?></strong></td><td>-</td><td><strong><?= e(amount($sumSettle)) ?></strong></td></tr>
|
||
<?php else: ?><tr><td colspan="5"><div class="empty-action"><div class="ea-title">暂无应付利息数据</div>履行中及逾期中的银行贷款将在此汇总</div></td></tr><?php endif; ?>
|
||
</table></div>
|
||
</div>
|
||
<div class="card" style="margin-top:12px">
|
||
<div class="section-title" style="margin-top:0">二、当月还款计划金额</div>
|
||
<div class="table-wrap" style="margin-top:10px">
|
||
<table>
|
||
<tr><th>机构</th><th>档案名称</th><th>借款主体</th><th>到期日</th><th>金额</th><th>状态</th></tr>
|
||
<?php foreach ($planRows as $r): ?>
|
||
<tr>
|
||
<td><?= e((string)$r['loan_institution']) ?></td>
|
||
<td><?= e((string)$r['archive_name']) ?></td>
|
||
<td><?= e((string)$r['borrower']) ?></td>
|
||
<td><?= e((string)$r['due_date']) ?></td>
|
||
<td><?= e(amount((float)$r['amount'])) ?></td>
|
||
<td><?= e((string)$r['status']) ?></td>
|
||
</tr>
|
||
<?php endforeach; ?>
|
||
<?php if (!empty($planRows)): ?><tr><td><strong>合计</strong></td><td colspan="3">-</td><td><strong><?= e(amount($planTotal)) ?></strong></td><td>-</td></tr><?php endif; ?>
|
||
<?php if (empty($planRows)): ?><tr><td colspan="6"><div class="empty-action"><div class="ea-title">暂无当月还款计划</div>当月到期的本金 / 利息计划会列在这里</div></td></tr><?php endif; ?>
|
||
</table>
|
||
</div>
|
||
</div>
|
||
<?php pageFooter(); exit;
|
||
}
|
||
|
||
if ($action === 'query_external_guarantee') {
|
||
requireLogin();
|
||
$kw = trim((string)($_GET['kw'] ?? ''));
|
||
$format = (string)($_GET['format'] ?? '');
|
||
$rows = $pdo->query('SELECT * FROM archives ORDER BY id DESC')->fetchAll();
|
||
$financeAll = loadFinanceConfigs();
|
||
$resultRows = array_values(array_filter(qqSearchGuarantorArchives($rows, $kw), function ($r) use ($financeAll) {
|
||
return archiveStatusIsOngoingAggregate((string)($r['end_date'] ?? ''), getArchiveFinanceConfig($financeAll, (int)$r['id']));
|
||
}));
|
||
if ($format === 'xlsx' && $kw !== '' && class_exists('\PhpOffice\PhpSpreadsheet\Spreadsheet') && class_exists('\PhpOffice\PhpSpreadsheet\Writer\Xlsx')) {
|
||
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
|
||
$sheet = $spreadsheet->getActiveSheet();
|
||
$sheet->setTitle('对外担保查询');
|
||
$sheet->fromArray(['档案类型', '档案名称', '贷款机构/债权人', '借款主体', '担保人', '授信额度', '剩余额度', '到期日', '状态'], null, 'A1');
|
||
$sheet->fromArray(array_map(function ($r) use ($financeAll) {
|
||
return [
|
||
archiveKindLabel(normalizeArchiveKind((string)($r['archive_kind'] ?? ''))),
|
||
(string)($r['name'] ?? ''),
|
||
(string)($r['loan_institution'] ?? ''),
|
||
(string)($r['borrower'] ?? ''),
|
||
(string)($r['guarantor'] ?? ''),
|
||
amount($r['credit_limit'] !== null ? (float)$r['credit_limit'] : null),
|
||
amount($r['remaining_limit'] !== null ? (float)$r['remaining_limit'] : null),
|
||
(string)($r['end_date'] ?? ''),
|
||
archiveStatusWithConfig((string)($r['end_date'] ?? ''), getArchiveFinanceConfig($financeAll, (int)$r['id'])),
|
||
];
|
||
}, $resultRows), null, 'A2');
|
||
foreach (range('A', 'I') as $col) { $sheet->getColumnDimension($col)->setWidth(18); }
|
||
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
||
header('Content-Disposition: attachment; filename="' . rawurlencode('对外担保查询_' . date('Y-m-d') . '.xlsx') . '"');
|
||
(new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet))->save('php://output');
|
||
exit;
|
||
}
|
||
pageHeader($me); ?>
|
||
<div class="page-head">
|
||
<div>
|
||
<h1 class="page-title">统计查询</h1>
|
||
<div class="page-sub">按担保人检索履行中及逾期中档案</div>
|
||
</div>
|
||
</div>
|
||
<?php fusionQueryTabs('query_external_guarantee'); ?>
|
||
<div class="card">
|
||
<form method="get" class="toolbar-left">
|
||
<input type="hidden" name="action" value="query_external_guarantee">
|
||
<input name="kw" value="<?= e($kw) ?>" placeholder="输入主体名称(匹配担保人)" required>
|
||
<button type="submit" class="btn btn--primary">查询</button>
|
||
<?php if ($kw !== ''): ?><a class="btn btn--plain" href="index.php?action=query_external_guarantee&kw=<?= urlencode($kw) ?>&format=xlsx">导出xlsx</a><?php endif; ?>
|
||
</form>
|
||
<p class="small muted" style="margin-top:8px">仅列出状态为「履行中」或「逾期中」的档案。</p>
|
||
</div>
|
||
<div class="card" style="margin-top:12px">
|
||
<div class="table-wrap"><table>
|
||
<tr><th>档案类型</th><th>档案名称</th><th>贷款机构/债权人</th><th>借款主体</th><th>担保人</th><th>授信额度</th><th>剩余额度</th><th>到期日</th><th>状态</th></tr>
|
||
<?php foreach ($resultRows as $r): ?>
|
||
<?php $st = archiveStatusWithConfig((string)($r['end_date'] ?? ''), getArchiveFinanceConfig($financeAll, (int)$r['id'])); ?>
|
||
<tr>
|
||
<td><?= e(archiveKindLabel(normalizeArchiveKind((string)($r['archive_kind'] ?? '')))) ?></td>
|
||
<td><?= e((string)($r['name'] ?? '')) ?></td>
|
||
<td><?= e((string)($r['loan_institution'] ?? '')) ?></td>
|
||
<td><?= e((string)($r['borrower'] ?? '')) ?></td>
|
||
<td><?= e((string)($r['guarantor'] ?? '')) ?></td>
|
||
<td><?= e(amount($r['credit_limit'] !== null ? (float)$r['credit_limit'] : null)) ?></td>
|
||
<td><?= e(amount($r['remaining_limit'] !== null ? (float)$r['remaining_limit'] : null)) ?></td>
|
||
<td><?= e((string)($r['end_date'] ?? '')) ?></td>
|
||
<td><?= e($st) ?></td>
|
||
</tr>
|
||
<?php endforeach; ?>
|
||
<?php if (empty($resultRows)): ?><tr><td colspan="9"><div class="empty-action"><div class="ea-title"><?= $kw === '' ? '请输入主体名称后查询' : '未命中履行中或逾期中的相关档案' ?></div><?= $kw === '' ? '按担保人检索对外担保与相关银行贷款' : '已完成等状态已排除' ?></div></td></tr><?php endif; ?>
|
||
</table></div>
|
||
</div>
|
||
<?php pageFooter(); exit;
|
||
}
|
||
|
||
if ($action === 'export') {
|
||
requireLogin();
|
||
$mode = $_GET['mode'] ?? 'all';
|
||
$format = $_GET['format'] ?? 'xlsx';
|
||
$kindFilter = (string)($_GET['kind'] ?? 'bank_loan');
|
||
if (!in_array($kindFilter, ['all', ARCHIVE_KIND_BANK_LOAN, ARCHIVE_KIND_EXTERNAL_GUARANTEE], true)) {
|
||
$kindFilter = ARCHIVE_KIND_BANK_LOAN;
|
||
}
|
||
$selectedRaw = $_GET['ids'] ?? [];
|
||
if (!is_array($selectedRaw)) {
|
||
$selectedRaw = explode(',', (string)$selectedRaw);
|
||
}
|
||
$selected = array_values(array_filter(array_map('intval', $selectedRaw)));
|
||
$dirId = (int)($_GET['dir_id'] ?? 0);
|
||
$dirs = fetchDirectories($pdo);
|
||
$dirFilter = [];
|
||
if ($dirId > 0) {
|
||
$dirFilter = collectSubDirIds($dirs, $dirId);
|
||
}
|
||
$rows = $pdo->query('SELECT * FROM archives ORDER BY id DESC')->fetchAll();
|
||
$financeAll = loadFinanceConfigs();
|
||
$rows = array_values(array_filter($rows, function ($r) use ($mode, $selected, $dirFilter, $financeAll, $kindFilter) {
|
||
if ($dirFilter && !in_array((int)$r['directory_id'], $dirFilter, true)) {
|
||
return false;
|
||
}
|
||
$rowKind = normalizeArchiveKind((string)($r['archive_kind'] ?? ''));
|
||
if ($kindFilter !== 'all' && $rowKind !== $kindFilter) {
|
||
return false;
|
||
}
|
||
$status = archiveStatusWithConfig($r['end_date'], getArchiveFinanceConfig($financeAll, (int)$r['id']));
|
||
if ($mode === 'selected' && !in_array((int)$r['id'], $selected, true)) {
|
||
return false;
|
||
}
|
||
return true;
|
||
}));
|
||
$modeName = ['all' => '全部', 'selected' => '选中'][$mode] ?? '全部';
|
||
$headers = ['档案类型', '贷款机构', '借款主体', '授信额度', '剩余额度', '起止日', '到期日', '年利率', '担保类型', '担保人', '合同状态'];
|
||
$dataRows = [];
|
||
foreach ($rows as $r) {
|
||
$dataRows[] = [
|
||
archiveKindLabel(normalizeArchiveKind($r['archive_kind'] ?? '')),
|
||
$r['loan_institution'],
|
||
$r['borrower'],
|
||
amount($r['credit_limit'] !== null ? (float)$r['credit_limit'] : null),
|
||
amount($r['remaining_limit'] !== null ? (float)$r['remaining_limit'] : null),
|
||
$r['start_date'],
|
||
$r['end_date'],
|
||
$r['annual_rate'] === null ? '' : ((string)$r['annual_rate'] . '%'),
|
||
$r['guarantee_type'],
|
||
$r['guarantor'],
|
||
archiveStatusWithConfig($r['end_date'], getArchiveFinanceConfig($financeAll, (int)$r['id'])),
|
||
];
|
||
}
|
||
|
||
$xlsxEnabled = class_exists('\PhpOffice\PhpSpreadsheet\Spreadsheet') && class_exists('\PhpOffice\PhpSpreadsheet\Writer\Xlsx');
|
||
if ($format === 'xlsx' && $xlsxEnabled) {
|
||
$filename = '档案汇总_' . $modeName . '_' . date('Y-m-d') . '.xlsx';
|
||
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
|
||
$sheet = $spreadsheet->getActiveSheet();
|
||
$sheet->fromArray($headers, null, 'A1');
|
||
$sheet->fromArray($dataRows, null, 'A2');
|
||
foreach (range('A', 'K') as $col) {
|
||
$sheet->getColumnDimension($col)->setWidth(16);
|
||
}
|
||
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
||
header('Content-Disposition: attachment; filename="' . rawurlencode($filename) . '"');
|
||
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
|
||
$writer->save('php://output');
|
||
exit;
|
||
}
|
||
|
||
$filename = '档案汇总_' . $modeName . '_' . date('Y-m-d') . '.csv';
|
||
header('Content-Type: text/csv; charset=UTF-8');
|
||
header('Content-Disposition: attachment; filename="' . rawurlencode($filename) . '"');
|
||
echo "\xEF\xBB\xBF";
|
||
$out = fopen('php://output', 'wb');
|
||
fputcsv($out, $headers);
|
||
foreach ($dataRows as $row) {
|
||
fputcsv($out, $row);
|
||
}
|
||
fclose($out);
|
||
exit;
|
||
}
|
||
|
||
$dirs = fetchDirectories($pdo);
|
||
$dirId = (int)($_GET['dir_id'] ?? 0);
|
||
$q = trim((string)($_GET['q'] ?? ''));
|
||
$searchField = $_GET['search_field'] ?? 'all';
|
||
$statusFilter = $_GET['status'] ?? 'not_done';
|
||
$kindFilter = (string)($_GET['kind'] ?? ARCHIVE_KIND_BANK_LOAN);
|
||
if ($action === 'dashboard') {
|
||
$kindFilter = 'all';
|
||
$statusFilter = 'all';
|
||
}
|
||
if (!in_array($kindFilter, ['all', ARCHIVE_KIND_BANK_LOAN, ARCHIVE_KIND_EXTERNAL_GUARANTEE], true)) {
|
||
$kindFilter = ARCHIVE_KIND_BANK_LOAN;
|
||
}
|
||
$sortBy = $_GET['sort_by'] ?? 'end_date';
|
||
$sortOrder = strtolower((string)($_GET['sort_order'] ?? 'asc')) === 'desc' ? 'desc' : 'asc';
|
||
$rows = $pdo->query('SELECT * FROM archives ORDER BY id DESC')->fetchAll();
|
||
$financeAllGlobal = loadFinanceConfigs();
|
||
$dirPath = dirPathMap($dirs);
|
||
$dirScope = [];
|
||
if ($dirId > 0) {
|
||
$dirScope = collectSubDirIds($dirs, $dirId);
|
||
}
|
||
$archives = array_values(array_filter($rows, function ($r) use ($q, $statusFilter, $dirScope, $searchField, $financeAllGlobal, $kindFilter) {
|
||
if ($dirScope && !in_array((int)$r['directory_id'], $dirScope, true)) {
|
||
return false;
|
||
}
|
||
$rowKind = normalizeArchiveKind((string)($r['archive_kind'] ?? ''));
|
||
if ($kindFilter !== 'all' && $rowKind !== $kindFilter) {
|
||
return false;
|
||
}
|
||
$status = archiveStatusWithConfig($r['end_date'], getArchiveFinanceConfig($financeAllGlobal, (int)$r['id']));
|
||
if ($statusFilter === 'not_done' && $status === '已完成') {
|
||
return false;
|
||
}
|
||
if (!in_array($statusFilter, ['all', 'not_done'], true) && $statusFilter !== $status) {
|
||
return false;
|
||
}
|
||
if ($q !== '') {
|
||
if (!in_array($searchField, ['all', 'borrower', 'guarantor'], true)) {
|
||
$searchField = 'all';
|
||
}
|
||
if ($searchField === 'borrower') {
|
||
$hay = mb_strtolower((string)$r['borrower']);
|
||
} elseif ($searchField === 'guarantor') {
|
||
$hay = mb_strtolower((string)$r['guarantor']);
|
||
} else {
|
||
$hay = mb_strtolower(implode(' ', [
|
||
$r['name'],
|
||
$r['loan_institution'],
|
||
$r['borrower'],
|
||
$r['guarantee_type'],
|
||
$r['guarantor'],
|
||
]));
|
||
}
|
||
if (mb_strpos($hay, mb_strtolower($q)) === false) {
|
||
return false;
|
||
}
|
||
}
|
||
return true;
|
||
}));
|
||
|
||
if (!in_array($sortBy, ['end_date', 'created_at', 'loan_institution'], true)) {
|
||
$sortBy = 'end_date';
|
||
}
|
||
usort($archives, function ($a, $b) use ($sortBy, $sortOrder, $financeAllGlobal) {
|
||
$sa = archiveStatusWithConfig($a['end_date'], getArchiveFinanceConfig($financeAllGlobal, (int)$a['id']));
|
||
$sb = archiveStatusWithConfig($b['end_date'], getArchiveFinanceConfig($financeAllGlobal, (int)$b['id']));
|
||
$w = ['履行中' => 0, '逾期中' => 1, '待更新' => 2, '已完成' => 3];
|
||
$sw = ($w[$sa] ?? 9) <=> ($w[$sb] ?? 9);
|
||
if ($sw !== 0) {
|
||
return $sw;
|
||
}
|
||
$av = (string)($a[$sortBy] ?? '');
|
||
$bv = (string)($b[$sortBy] ?? '');
|
||
if ($sortBy === 'loan_institution') {
|
||
$av = mb_strtolower($av);
|
||
$bv = mb_strtolower($bv);
|
||
}
|
||
$cmp = $av <=> $bv;
|
||
if ($cmp === 0) {
|
||
$cmp = ((int)$a['id']) <=> ((int)$b['id']);
|
||
}
|
||
return $sortOrder === 'asc' ? $cmp : -$cmp;
|
||
});
|
||
|
||
$totalCount = count($archives);
|
||
$activeCount = 0;
|
||
$doneCount = 0;
|
||
foreach ($archives as $item) {
|
||
$status = archiveStatusWithConfig($item['end_date'], getArchiveFinanceConfig($financeAllGlobal, (int)$item['id']));
|
||
if ($status === '履行中') {
|
||
$activeCount++;
|
||
} elseif ($status === '已完成') {
|
||
$doneCount++;
|
||
} elseif ($status === '逾期中') {
|
||
$activeCount++;
|
||
}
|
||
}
|
||
|
||
$today = date('Y-m-d');
|
||
$day60 = date('Y-m-d', strtotime('+60 days'));
|
||
$dueSoonLoans = array_values(array_filter($archives, function ($a) use ($today, $day60, $financeAllGlobal) {
|
||
if (empty($a['end_date'])) {
|
||
return false;
|
||
}
|
||
$status = archiveStatusWithConfig($a['end_date'], getArchiveFinanceConfig($financeAllGlobal, (int)$a['id']));
|
||
if ($status === '已完成') {
|
||
return false;
|
||
}
|
||
return $a['end_date'] >= $today && $a['end_date'] <= $day60;
|
||
}));
|
||
$dueSoonCount = count($dueSoonLoans);
|
||
$dueSoonLoans = array_slice($dueSoonLoans, 0, 8);
|
||
$overdueLoans = array_values(array_filter($archives, function ($a) use ($financeAllGlobal) {
|
||
$status = archiveStatusWithConfig($a['end_date'], getArchiveFinanceConfig($financeAllGlobal, (int)$a['id']));
|
||
return $status === '逾期中';
|
||
}));
|
||
$overdueLoanCount = count($overdueLoans);
|
||
$overdueLoans = array_slice($overdueLoans, 0, 8);
|
||
$repayPlanSoon = [];
|
||
$overduePlanRows = [];
|
||
$planAllForReminder = loadRepaymentPlans();
|
||
foreach ($planAllForReminder as $aid => $cfg) {
|
||
if (empty($cfg['enabled']) || empty($cfg['plans']) || !is_array($cfg['plans'])) {
|
||
continue;
|
||
}
|
||
$aidInt = (int)$aid;
|
||
$arc = null;
|
||
foreach ($archives as $tmpArc) {
|
||
if ((int)$tmpArc['id'] === $aidInt) {
|
||
$arc = $tmpArc;
|
||
break;
|
||
}
|
||
}
|
||
if (!$arc) {
|
||
continue;
|
||
}
|
||
$day30 = date('Y-m-d', strtotime('+30 days'));
|
||
foreach ($cfg['plans'] as $p) {
|
||
$status = (string)($p['status'] ?? 'pending');
|
||
$d = (string)($p['due_date'] ?? '');
|
||
if ($status === 'paid' || $d === '') {
|
||
continue;
|
||
}
|
||
if ($d >= $today && $d <= $day30) {
|
||
$repayPlanSoon[] = [
|
||
'archive_id' => $aidInt,
|
||
'plan_id' => (string)($p['id'] ?? ''),
|
||
'archive_kind' => normalizeArchiveKind((string)($arc['archive_kind'] ?? '')),
|
||
'loan_institution' => $arc['loan_institution'] ?? '',
|
||
'borrower' => $arc['borrower'] ?? '',
|
||
'amount' => (float)($p['amount'] ?? 0),
|
||
'due_date' => $d,
|
||
];
|
||
}
|
||
if ($status === 'overdue' && $d <= $today) {
|
||
$overduePlanRows[] = [
|
||
'archive_id' => $aidInt,
|
||
'plan_id' => (string)($p['id'] ?? ''),
|
||
'archive_kind' => normalizeArchiveKind((string)($arc['archive_kind'] ?? '')),
|
||
'loan_institution' => $arc['loan_institution'] ?? '',
|
||
'borrower' => $arc['borrower'] ?? '',
|
||
'amount' => (float)($p['amount'] ?? 0),
|
||
'due_date' => $d,
|
||
];
|
||
}
|
||
}
|
||
}
|
||
$repaySoonCount = count($repayPlanSoon);
|
||
$repayPlanSoon = array_slice($repayPlanSoon, 0, 8);
|
||
$overdueCount = $overdueLoanCount + count($overduePlanRows);
|
||
|
||
$pendingUpdateRows = [];
|
||
foreach ($archives as $a) {
|
||
$aid = (int)$a['id'];
|
||
$end = (string)($a['end_date'] ?? '');
|
||
if ($end === '' || $end >= $today) {
|
||
continue;
|
||
}
|
||
if (archiveStatusWithConfig($end, getArchiveFinanceConfig($financeAllGlobal, $aid)) !== '待更新') {
|
||
continue;
|
||
}
|
||
$pendingUpdateRows[] = [
|
||
'row_kind' => 'loan',
|
||
'archive_id' => $aid,
|
||
'type_label' => '贷款到期',
|
||
'loan_institution' => (string)($a['loan_institution'] ?? ''),
|
||
'borrower' => (string)($a['borrower'] ?? ''),
|
||
'guarantor' => (string)($a['guarantor'] ?? ''),
|
||
'amount' => $a['remaining_limit'] !== null ? (float)$a['remaining_limit'] : null,
|
||
'date' => $end,
|
||
'plan_id' => '',
|
||
];
|
||
}
|
||
$archiveIdsInScope = [];
|
||
foreach ($archives as $a) {
|
||
$archiveIdsInScope[(int)$a['id']] = true;
|
||
}
|
||
foreach ($planAllForReminder as $aidStr => $cfg) {
|
||
$aidInt = (int)$aidStr;
|
||
if (empty($archiveIdsInScope[$aidInt])) {
|
||
continue;
|
||
}
|
||
if (empty($cfg['enabled']) || empty($cfg['plans']) || !is_array($cfg['plans'])) {
|
||
continue;
|
||
}
|
||
$arc = null;
|
||
foreach ($archives as $tmp) {
|
||
if ((int)$tmp['id'] === $aidInt) {
|
||
$arc = $tmp;
|
||
break;
|
||
}
|
||
}
|
||
if (!$arc) {
|
||
continue;
|
||
}
|
||
foreach ($cfg['plans'] as $p) {
|
||
$pst = (string)($p['status'] ?? 'pending');
|
||
$d = (string)($p['due_date'] ?? '');
|
||
if ($d === '' || $d >= $today) {
|
||
continue;
|
||
}
|
||
if (!in_array($pst, ['pending', 'delayed'], true)) {
|
||
continue;
|
||
}
|
||
$pendingUpdateRows[] = [
|
||
'row_kind' => 'plan',
|
||
'archive_id' => $aidInt,
|
||
'type_label' => '还款计划到期',
|
||
'loan_institution' => (string)($arc['loan_institution'] ?? ''),
|
||
'borrower' => (string)($arc['borrower'] ?? ''),
|
||
'guarantor' => (string)($arc['guarantor'] ?? ''),
|
||
'amount' => (float)($p['amount'] ?? 0),
|
||
'date' => $d,
|
||
'plan_id' => (string)($p['id'] ?? ''),
|
||
];
|
||
}
|
||
}
|
||
usort($pendingUpdateRows, function ($x, $y) {
|
||
$c = strcmp((string)($x['date'] ?? ''), (string)($y['date'] ?? ''));
|
||
if ($c !== 0) {
|
||
return $c;
|
||
}
|
||
return ((int)($x['archive_id'] ?? 0)) <=> ((int)($y['archive_id'] ?? 0));
|
||
});
|
||
$pendingUpdateCount = count($pendingUpdateRows);
|
||
$GLOBALS['fusion_task_badge'] = $pendingUpdateCount + $dueSoonCount + $repaySoonCount + $overdueCount;
|
||
|
||
if ($action === 'pending_updates') {
|
||
requireLogin();
|
||
$puBack = 'index.php';
|
||
if ($dirId > 0) {
|
||
$puBack .= '?dir_id=' . $dirId;
|
||
}
|
||
pageHeader($me); ?>
|
||
<div class="page-head">
|
||
<div>
|
||
<h1 class="page-title">任务中心</h1>
|
||
<div class="page-sub">已过到期日且尚未标记结果的贷款与还款计划</div>
|
||
<div class="seg head-switch" role="tablist">
|
||
<a role="tab" href="index.php?action=reminders&type=due">贷款到期</a>
|
||
<a role="tab" href="index.php?action=reminders&type=plan">还款计划</a>
|
||
<a role="tab" href="index.php?action=reminders&type=overdue">逾期中</a>
|
||
<a role="tab" href="index.php?action=pending_updates" data-active>待更新</a>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="card">
|
||
<p class="small muted">以下为已过到期日且尚未在系统中标记结果的贷款与还款计划。处理后将从本列表移除。</p>
|
||
<div class="table-wrap"><table>
|
||
<thead><tr><th>类型</th><th>贷款机构</th><th>借款主体</th><th>担保人</th><th>金额</th><th>日期</th><th>操作</th></tr></thead>
|
||
<tbody>
|
||
<?php foreach ($pendingUpdateRows as $r): ?>
|
||
<tr>
|
||
<td><span class="badge <?= ($r['row_kind'] ?? '') === 'loan' ? 'b1' : 'b2' ?>"><?= e((string)($r['type_label'] ?? '')) ?></span></td>
|
||
<td><?= e((string)($r['loan_institution'] ?? '')) ?></td>
|
||
<td><?= e((string)($r['borrower'] ?? '')) ?></td>
|
||
<td><?= e((string)($r['guarantor'] ?? '')) ?></td>
|
||
<td><?= e(amount(isset($r['amount']) && $r['amount'] !== null ? (float)$r['amount'] : null)) ?></td>
|
||
<td><?= e((string)($r['date'] ?? '')) ?></td>
|
||
<td>
|
||
<a class="icon-btn" href="index.php?action=archive_detail&id=<?= (int)($r['archive_id'] ?? 0) ?>">查看档案</a>
|
||
<?php if (isAdmin()): ?>
|
||
<?php if (($r['row_kind'] ?? '') === 'loan'): ?>
|
||
<form method="post" action="index.php?action=pending_update_action" class="inline" style="display:inline" onsubmit="return confirm('确认标记为已还款?状态将改为已完成。')">
|
||
<input type="hidden" name="op" value="pending_loan_repaid">
|
||
<input type="hidden" name="archive_id" value="<?= (int)($r['archive_id'] ?? 0) ?>">
|
||
<?php if ($dirId > 0): ?><input type="hidden" name="dir_id" value="<?= $dirId ?>"><?php endif; ?>
|
||
<input type="hidden" name="redirect_to" value="<?= e(currentUrlFromServer()) ?>">
|
||
<button type="submit" class="btn-gray">已还款</button>
|
||
</form>
|
||
<form method="post" action="index.php?action=pending_update_action" class="inline" style="display:inline" onsubmit="return confirm('确认标记为已逾期?状态将改为逾期中。')">
|
||
<input type="hidden" name="op" value="pending_loan_overdue">
|
||
<input type="hidden" name="archive_id" value="<?= (int)($r['archive_id'] ?? 0) ?>">
|
||
<?php if ($dirId > 0): ?><input type="hidden" name="dir_id" value="<?= $dirId ?>"><?php endif; ?>
|
||
<input type="hidden" name="redirect_to" value="<?= e(currentUrlFromServer()) ?>">
|
||
<button type="submit" class="btn-gray">已逾期</button>
|
||
</form>
|
||
<?php else: ?>
|
||
<form method="post" action="index.php?action=pending_update_action" class="inline" style="display:inline" onsubmit="return confirm('确认该期已还款?状态将改为按期还款(已还款)。')">
|
||
<input type="hidden" name="op" value="pending_plan_repaid">
|
||
<input type="hidden" name="archive_id" value="<?= (int)($r['archive_id'] ?? 0) ?>">
|
||
<input type="hidden" name="plan_id" value="<?= e((string)($r['plan_id'] ?? '')) ?>">
|
||
<?php if ($dirId > 0): ?><input type="hidden" name="dir_id" value="<?= $dirId ?>"><?php endif; ?>
|
||
<input type="hidden" name="redirect_to" value="<?= e(currentUrlFromServer()) ?>">
|
||
<button type="submit" class="btn-gray">已还款</button>
|
||
</form>
|
||
<form method="post" action="index.php?action=pending_update_action" class="inline" style="display:inline" onsubmit="return confirm('确认该期已逾期?状态将改为逾期中。')">
|
||
<input type="hidden" name="op" value="pending_plan_overdue">
|
||
<input type="hidden" name="archive_id" value="<?= (int)($r['archive_id'] ?? 0) ?>">
|
||
<input type="hidden" name="plan_id" value="<?= e((string)($r['plan_id'] ?? '')) ?>">
|
||
<?php if ($dirId > 0): ?><input type="hidden" name="dir_id" value="<?= $dirId ?>"><?php endif; ?>
|
||
<input type="hidden" name="redirect_to" value="<?= e(currentUrlFromServer()) ?>">
|
||
<button type="submit" class="btn-gray">已逾期</button>
|
||
</form>
|
||
<?php endif; ?>
|
||
<?php endif; ?>
|
||
</td>
|
||
</tr>
|
||
<?php endforeach; ?>
|
||
<?php if (empty($pendingUpdateRows)): ?>
|
||
<tr><td colspan="7"><div class="empty-action"><div class="ea-title">暂无待更新项</div>已到结息日、待经办确认的档案会列在这里</div></td></tr>
|
||
<?php endif; ?>
|
||
</tbody>
|
||
</table></div>
|
||
</div>
|
||
<?php pageFooter();
|
||
exit;
|
||
}
|
||
|
||
if ($action === 'reminders') {
|
||
requireLogin();
|
||
$reminderType = (string)($_GET['type'] ?? 'due');
|
||
$exportFormat = (string)($_GET['format'] ?? '');
|
||
$allReminderRows = [];
|
||
foreach ($dueSoonLoans as $r) {
|
||
$allReminderRows[] = ['type' => '贷款到期(60天)', 'archive_id' => (int)$r['id'], 'plan_id' => '', 'archive_kind' => archiveKindLabel(normalizeArchiveKind((string)($r['archive_kind'] ?? ''))), 'loan_institution' => (string)$r['loan_institution'], 'borrower' => (string)$r['borrower'], 'amount' => amount($r['remaining_limit'] !== null ? (float)$r['remaining_limit'] : null), 'date' => (string)$r['end_date']];
|
||
}
|
||
foreach ($repayPlanSoon as $r) {
|
||
$allReminderRows[] = ['type' => '还款计划(30天)', 'archive_id' => (int)$r['archive_id'], 'plan_id' => (string)$r['plan_id'], 'archive_kind' => archiveKindLabel(normalizeArchiveKind((string)($r['archive_kind'] ?? ''))), 'loan_institution' => (string)$r['loan_institution'], 'borrower' => (string)$r['borrower'], 'amount' => amount((float)$r['amount']), 'date' => (string)$r['due_date']];
|
||
}
|
||
foreach ($overdueLoans as $r) {
|
||
$allReminderRows[] = ['type' => '逾期中', 'archive_id' => (int)$r['id'], 'plan_id' => '', 'archive_kind' => archiveKindLabel(normalizeArchiveKind((string)($r['archive_kind'] ?? ''))), 'loan_institution' => (string)$r['loan_institution'], 'borrower' => (string)$r['borrower'], 'amount' => amount($r['remaining_limit'] !== null ? (float)$r['remaining_limit'] : null), 'date' => (string)$r['end_date']];
|
||
}
|
||
foreach ($overduePlanRows as $r) {
|
||
$allReminderRows[] = ['type' => '逾期中', 'archive_id' => (int)$r['archive_id'], 'plan_id' => (string)$r['plan_id'], 'archive_kind' => archiveKindLabel(normalizeArchiveKind((string)($r['archive_kind'] ?? ''))), 'loan_institution' => (string)$r['loan_institution'], 'borrower' => (string)$r['borrower'], 'amount' => amount((float)$r['amount']), 'date' => (string)$r['due_date']];
|
||
}
|
||
$rows = array_values(array_filter($allReminderRows, function ($row) use ($reminderType) {
|
||
if ($reminderType === 'all') return true;
|
||
if ($reminderType === 'due') return $row['type'] === '贷款到期(60天)';
|
||
if ($reminderType === 'plan') return $row['type'] === '还款计划(30天)';
|
||
if ($reminderType === 'overdue') return $row['type'] === '逾期中';
|
||
return true;
|
||
}));
|
||
if ($exportFormat !== '') {
|
||
$headers = ['类型', '档案类型', '贷款机构', '借款主体', '金额', '日期'];
|
||
if ($exportFormat === 'xlsx' && class_exists('\PhpOffice\PhpSpreadsheet\Spreadsheet') && class_exists('\PhpOffice\PhpSpreadsheet\Writer\Xlsx')) {
|
||
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
|
||
$sheet = $spreadsheet->getActiveSheet();
|
||
$sheet->fromArray($headers, null, 'A1');
|
||
$sheet->fromArray(array_map(fn($x) => [$x['type'], $x['archive_kind'], $x['loan_institution'], $x['borrower'], $x['amount'], $x['date']], $rows), null, 'A2');
|
||
foreach (range('A', 'F') as $col) { $sheet->getColumnDimension($col)->setWidth(20); }
|
||
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
||
header('Content-Disposition: attachment; filename="' . rawurlencode('提醒明细_' . date('Y-m-d') . '.xlsx') . '"');
|
||
(new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet))->save('php://output');
|
||
exit;
|
||
}
|
||
header('Content-Type: text/csv; charset=UTF-8');
|
||
header('Content-Disposition: attachment; filename="' . rawurlencode('提醒明细_' . date('Y-m-d') . '.csv') . '"');
|
||
echo "\xEF\xBB\xBF";
|
||
$out = fopen('php://output', 'wb');
|
||
fputcsv($out, $headers);
|
||
foreach ($rows as $r) { fputcsv($out, [$r['type'], $r['archive_kind'], $r['loan_institution'], $r['borrower'], $r['amount'], $r['date']]); }
|
||
fclose($out);
|
||
exit;
|
||
}
|
||
pageHeader($me); ?>
|
||
<div class="page-head">
|
||
<div>
|
||
<h1 class="page-title">任务中心</h1>
|
||
<div class="page-sub">贷款到期 / 还款计划 / 逾期中 / 待更新</div>
|
||
<div class="seg head-switch" role="tablist" aria-label="任务分组">
|
||
<a role="tab" href="index.php?action=reminders&type=due"<?= $reminderType === 'due' ? ' data-active' : '' ?>>贷款到期</a>
|
||
<a role="tab" href="index.php?action=reminders&type=plan"<?= $reminderType === 'plan' ? ' data-active' : '' ?>>还款计划</a>
|
||
<a role="tab" href="index.php?action=reminders&type=overdue"<?= $reminderType === 'overdue' ? ' data-active' : '' ?>>逾期中</a>
|
||
<a role="tab" href="index.php?action=pending_updates">待更新</a>
|
||
<a role="tab" href="index.php?action=reminders&type=all"<?= $reminderType === 'all' ? ' data-active' : '' ?>>全部</a>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="card">
|
||
<form method="get" class="filter-bar" style="margin-top:0">
|
||
<input type="hidden" name="action" value="reminders">
|
||
<select name="type">
|
||
<option value="all" <?= $reminderType === 'all' ? 'selected' : '' ?>>全部类型</option>
|
||
<option value="due" <?= $reminderType === 'due' ? 'selected' : '' ?>>贷款到期</option>
|
||
<option value="plan" <?= $reminderType === 'plan' ? 'selected' : '' ?>>还款计划</option>
|
||
<option value="overdue" <?= $reminderType === 'overdue' ? 'selected' : '' ?>>贷款逾期</option>
|
||
</select>
|
||
<button type="submit">筛选</button>
|
||
<a class="clear-link" href="index.php?action=reminders&type=due">清空</a>
|
||
<button type="submit" name="format" value="xlsx" class="toolbar-btn">导出xlsx</button>
|
||
<button type="submit" name="format" value="csv" class="toolbar-btn">导出csv</button>
|
||
</form>
|
||
<div class="table-wrap"><table>
|
||
<thead><tr><th>类型</th><th>档案类型</th><th>贷款机构</th><th>借款主体</th><th>金额</th><th>日期</th><th>操作</th></tr></thead>
|
||
<tbody>
|
||
<?php foreach ($rows as $r): ?>
|
||
<tr>
|
||
<td><span class="badge <?= $r['type'] === '贷款到期(60天)' ? 'b1' : ($r['type'] === '还款计划(30天)' ? 'b2' : 'b4') ?>"><?= e($r['type']) ?></span></td>
|
||
<td><?= e((string)($r['archive_kind'] ?? '')) ?></td>
|
||
<td><?= e($r['loan_institution']) ?></td>
|
||
<td><?= e($r['borrower']) ?></td>
|
||
<td><?= e($r['amount']) ?></td>
|
||
<td><?= e($r['date']) ?></td>
|
||
<td>
|
||
<?php if ((int)($r['archive_id'] ?? 0) > 0): ?>
|
||
<a class="icon-btn" href="index.php?action=archive_detail&id=<?= (int)$r['archive_id'] ?>">查看档案</a>
|
||
<?php endif; ?>
|
||
<?php if ((int)($r['archive_id'] ?? 0) > 0 && $r['type'] === '贷款到期(60天)' && isAdmin()): ?>
|
||
<form method="post" action="index.php?action=reminder_action" class="inline" onsubmit="return confirm('确认设置该档案为提前还款并标记已完成?')">
|
||
<input type="hidden" name="op" value="early_settle">
|
||
<input type="hidden" name="archive_id" value="<?= (int)$r['archive_id'] ?>">
|
||
<input type="hidden" name="redirect_type" value="<?= e($reminderType) ?>">
|
||
<input type="hidden" name="redirect_to" value="<?= e(currentUrlFromServer()) ?>">
|
||
<button type="submit" class="btn-gray">提前还款</button>
|
||
</form>
|
||
<?php endif; ?>
|
||
<?php if ((int)($r['archive_id'] ?? 0) > 0 && $r['type'] === '还款计划(30天)' && isAdmin()): ?>
|
||
<form method="post" action="index.php?action=reminder_action" class="inline">
|
||
<input type="hidden" name="op" value="plan_delay">
|
||
<input type="hidden" name="archive_id" value="<?= (int)$r['archive_id'] ?>">
|
||
<input type="hidden" name="plan_id" value="<?= e((string)($r['plan_id'] ?? '')) ?>">
|
||
<input type="hidden" name="redirect_type" value="<?= e($reminderType) ?>">
|
||
<input type="hidden" name="redirect_to" value="<?= e(currentUrlFromServer()) ?>">
|
||
<input type="date" name="new_due_date" required style="max-width:140px">
|
||
<button type="submit" class="btn-gray">延期</button>
|
||
</form>
|
||
<?php endif; ?>
|
||
<?php if ((int)($r['archive_id'] ?? 0) > 0 && $r['type'] === '逾期中' && isAdmin()): ?>
|
||
<form method="post" action="index.php?action=reminder_action" class="inline" onsubmit="return confirm('确认将该逾期项标记为已归还?')">
|
||
<input type="hidden" name="op" value="overdue_paid">
|
||
<input type="hidden" name="archive_id" value="<?= (int)$r['archive_id'] ?>">
|
||
<input type="hidden" name="plan_id" value="<?= e((string)($r['plan_id'] ?? '')) ?>">
|
||
<input type="hidden" name="redirect_type" value="<?= e($reminderType) ?>">
|
||
<input type="hidden" name="redirect_to" value="<?= e(currentUrlFromServer()) ?>">
|
||
<button type="submit" class="btn-gray">已归还</button>
|
||
</form>
|
||
<?php endif; ?>
|
||
<?php if ((int)($r['archive_id'] ?? 0) <= 0): ?>
|
||
<span class="muted">-</span>
|
||
<?php endif; ?>
|
||
</td>
|
||
</tr>
|
||
<?php endforeach; ?>
|
||
<?php if (empty($rows)): ?>
|
||
<tr><td colspan="7"><div class="empty-action"><div class="ea-title">暂无提醒内容</div>到期、还款计划与逾期项会按类型列在这里</div></td></tr>
|
||
<?php endif; ?>
|
||
</tbody>
|
||
</table></div>
|
||
</div>
|
||
<?php pageFooter(); exit;
|
||
}
|
||
|
||
$perPage = (int)($_GET['per_page'] ?? 10);
|
||
if (!in_array($perPage, [10, 15, 30, 50], true)) {
|
||
$perPage = 10;
|
||
}
|
||
$currentPage = max(1, (int)($_GET['page'] ?? 1));
|
||
$totalPages = max(1, (int)ceil($totalCount / $perPage));
|
||
if ($currentPage > $totalPages) {
|
||
$currentPage = $totalPages;
|
||
}
|
||
$offset = ($currentPage - 1) * $perPage;
|
||
$archivesPage = array_slice($archives, $offset, $perPage);
|
||
|
||
$baseQuery = ['q' => $q, 'search_field' => $searchField, 'status' => $statusFilter, 'kind' => $kindFilter, 'per_page' => $perPage, 'sort_by' => $sortBy, 'sort_order' => $sortOrder];
|
||
if ($dirId) {
|
||
$baseQuery['dir_id'] = $dirId;
|
||
}
|
||
unset($baseQuery['page']);
|
||
$pathIds = collectAncestorIds($dirs, $dirId ?: null);
|
||
|
||
|
||
$sumCredit = 0.0;
|
||
$sumRemain = 0.0;
|
||
$listOver = 0;
|
||
foreach ($archives as $item) {
|
||
if ($item['credit_limit'] !== null && $item['credit_limit'] !== '') {
|
||
$sumCredit += (float)$item['credit_limit'];
|
||
}
|
||
if ($item['remaining_limit'] !== null && $item['remaining_limit'] !== '') {
|
||
$sumRemain += (float)$item['remaining_limit'];
|
||
}
|
||
if (archiveStatusWithConfig($item['end_date'], getArchiveFinanceConfig($financeAllGlobal, (int)$item['id'])) === '逾期中') {
|
||
$listOver++;
|
||
}
|
||
}
|
||
|
||
if ($action === 'dashboard') {
|
||
pageHeader($me);
|
||
$weekdays = ['日','一','二','三','四','五','六'];
|
||
$dashSub = date('Y-m-d') . ' 星期' . $weekdays[(int)date('w')];
|
||
?>
|
||
<div class="page-head">
|
||
<div>
|
||
<h1 class="page-title">工作台</h1>
|
||
<div class="page-sub">每日巡检 · <?= e($dashSub) ?></div>
|
||
</div>
|
||
<div style="display:flex; gap:10px; align-items:center; flex-wrap:wrap;">
|
||
<?php if (isAdmin()): ?>
|
||
<button type="button" class="btn btn--plain" data-open-modal="import">导入</button>
|
||
<?php endif; ?>
|
||
<a class="btn btn--plain" href="index.php?action=export&mode=all&format=xlsx&kind=all">导出</a>
|
||
<?php if (isAdmin()): ?>
|
||
<a class="btn btn--primary" href="index.php?action=archive_form">+ 新建档案</a>
|
||
<?php endif; ?>
|
||
</div>
|
||
</div>
|
||
<div class="stats">
|
||
<a class="stat stat--link stagger-in" href="index.php?action=pending_updates">
|
||
<div class="stat-label">待更新</div>
|
||
<div class="stat-value num"><?= (int)$pendingUpdateCount ?><small>笔</small></div>
|
||
<div class="stat-foot">已到结息日,待经办确认 →</div>
|
||
</a>
|
||
<a class="stat stat--link stat--warn stagger-in" style="animation-delay:45ms" href="index.php?action=reminders&type=due">
|
||
<div class="stat-label">60 日内贷款到期</div>
|
||
<div class="stat-value num"><?= (int)$dueSoonCount ?><small>笔</small></div>
|
||
<div class="stat-foot">需提前安排资金 →</div>
|
||
</a>
|
||
<a class="stat stat--link stat--gold stagger-in" style="animation-delay:90ms" href="index.php?action=reminders&type=plan">
|
||
<div class="stat-label">待处理还款计划</div>
|
||
<div class="stat-value num"><?= (int)$repaySoonCount ?><small>笔</small></div>
|
||
<div class="stat-foot">本金 / 利息到期未还 →</div>
|
||
</a>
|
||
<a class="stat stat--link stat--alert stagger-in" style="animation-delay:135ms" href="index.php?action=reminders&type=overdue">
|
||
<div class="stat-label">贷款逾期</div>
|
||
<div class="stat-value num"><?= (int)$overdueCount ?><small>笔</small></div>
|
||
<div class="stat-foot">已计逾期利息 →</div>
|
||
</a>
|
||
</div>
|
||
<div class="section-title" style="margin-top:0;">临期与风险</div>
|
||
<div class="table-card">
|
||
<table>
|
||
<thead><tr><th>事项</th><th>机构 / 主体</th><th class="cell-right">金额</th><th>日期</th><th>状态</th><th class="cell-right">操作</th></tr></thead>
|
||
<tbody>
|
||
<?php
|
||
$todo = [];
|
||
foreach (array_slice($pendingUpdateRows, 0, 3) as $r) {
|
||
$todo[] = ['待更新确认', (string)($r['loan_institution'] ?? '') . ' · ' . (string)($r['borrower'] ?? ''), $r['amount'] ?? null, (string)($r['date'] ?? ''), '待更新', 'index.php?action=pending_updates'];
|
||
}
|
||
foreach (array_slice($dueSoonLoans, 0, 2) as $r) {
|
||
$todo[] = ['贷款到期安排', (string)($r['loan_institution'] ?? '') . ' · ' . (string)($r['borrower'] ?? ''), $r['remaining_limit'] ?? null, (string)($r['end_date'] ?? ''), '即将到期', 'index.php?action=reminders&type=due'];
|
||
}
|
||
foreach (array_slice($overdueLoans, 0, 2) as $r) {
|
||
$todo[] = ['逾期跟进', (string)($r['loan_institution'] ?? '') . ' · ' . (string)($r['borrower'] ?? ''), $r['remaining_limit'] ?? null, (string)($r['end_date'] ?? ''), '逾期中', 'index.php?action=reminders&type=overdue'];
|
||
}
|
||
if (!$todo): ?>
|
||
<tr><td colspan="6"><div class="empty-action"><div class="ea-title">当前没有待办风险项</div>可到档案库查看全部档案,或使用快捷入口开展日常工作</div></td></tr>
|
||
<?php else:
|
||
foreach ($todo as $i => $t): ?>
|
||
<tr class="stagger-in" style="animation-delay:<?= $i * 45 ?>ms">
|
||
<td><div class="cell-main"><?= e($t[0]) ?></div></td>
|
||
<td><?= e($t[1]) ?></td>
|
||
<td class="cell-right num cell-remaining"><?= e(amount($t[2] !== null ? (float)$t[2] : null)) ?></td>
|
||
<td class="num"><?= e($t[3]) ?></td>
|
||
<td><?= fusionStatusPill($t[4]) ?></td>
|
||
<td class="cell-right"><a class="btn btn--plain btn--sm" href="<?= e($t[5]) ?>">去处理</a></td>
|
||
</tr>
|
||
<?php endforeach; endif; ?>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
<div class="section-title">快捷入口</div>
|
||
<?php fusionQuickDock(isAdmin()); ?>
|
||
<?php pageFooter(); exit;
|
||
}
|
||
|
||
$listView = (string)($_GET['view'] ?? 'files');
|
||
pageHeader($me);
|
||
|
||
if ($listView === 'dirs'):
|
||
$byParent = [];
|
||
foreach ($dirs as $d) {
|
||
$pid = $d['parent_id'] === null ? 0 : (int)$d['parent_id'];
|
||
$byParent[$pid][] = $d;
|
||
}
|
||
$sel1 = (int)($_GET['l1'] ?? 0);
|
||
$sel2 = (int)($_GET['l2'] ?? 0);
|
||
$sel3 = (int)($_GET['l3'] ?? ($dirId ?: 0));
|
||
if ($sel3 > 0 && $sel1 === 0) {
|
||
$map = [];
|
||
foreach ($dirs as $d) { $map[(int)$d['id']] = $d; }
|
||
$cur = $sel3;
|
||
$chain = [];
|
||
while (isset($map[$cur])) {
|
||
array_unshift($chain, $map[$cur]);
|
||
$pid = $map[$cur]['parent_id'];
|
||
if ($pid === null) break;
|
||
$cur = (int)$pid;
|
||
}
|
||
if (isset($chain[0])) $sel1 = (int)$chain[0]['id'];
|
||
if (isset($chain[1])) $sel2 = (int)$chain[1]['id'];
|
||
if (isset($chain[2])) $sel3 = (int)$chain[2]['id'];
|
||
}
|
||
$countByDir = [];
|
||
foreach ($rows as $r) {
|
||
$did = (int)$r['directory_id'];
|
||
$countByDir[$did] = ($countByDir[$did] ?? 0) + 1;
|
||
}
|
||
?>
|
||
<div class="page-head">
|
||
<div>
|
||
<h1 class="page-title">档案库</h1>
|
||
<div class="page-sub">三级目录归档 · 一级目录 / 二级目录 / 三级目录</div>
|
||
<div class="seg head-switch" role="tablist" aria-label="档案库视图切换">
|
||
<a role="tab" href="index.php?action=home">档案列表</a>
|
||
<a role="tab" href="index.php?action=home&view=dirs" data-active>目录管理</a>
|
||
</div>
|
||
</div>
|
||
<?php if (isAdmin()): ?><a class="btn btn--plain" href="index.php?action=new_dir">+ 一级目录</a><?php endif; ?>
|
||
</div>
|
||
<div class="dir-hint">点击目录行查看下级;新增、改名、删除直接作用于目录树。列表「目录路径」、按归档目录筛选与新增档案第 1 步均使用同一套目录。</div>
|
||
<div class="dir-cols">
|
||
<?php
|
||
$cols = [
|
||
[1, '一级目录', $byParent[0] ?? [], $sel1, '暂无一级目录', 0],
|
||
[2, '二级目录', $sel1 ? ($byParent[$sel1] ?? []) : [], $sel2, '请先选择左侧一级目录', $sel1],
|
||
[3, '三级目录', $sel2 ? ($byParent[$sel2] ?? []) : [], $sel3, '请先选择左侧二级目录', $sel2],
|
||
];
|
||
foreach ($cols as $col):
|
||
[$lv, $title, $nodes, $selected, $empty, $parentForAdd] = $col;
|
||
?>
|
||
<div class="dir-col">
|
||
<div class="dir-col-head">
|
||
<div class="dir-col-title"><?= e($title) ?><small><?= count($nodes) ?></small></div>
|
||
<?php if (isAdmin() && ($lv === 1 || $parentForAdd > 0)): ?>
|
||
<a class="btn btn--sm btn--plain" href="index.php?action=new_dir<?= $lv > 1 ? ('&parent_id=' . (int)$parentForAdd) : '' ?>">+ 新增</a>
|
||
<?php endif; ?>
|
||
</div>
|
||
<?php if (!$nodes): ?>
|
||
<div class="dir-empty"><?= e($empty) ?></div>
|
||
<?php else: foreach ($nodes as $d):
|
||
$id = (int)$d['id'];
|
||
$href = 'index.php?action=home&view=dirs&l1=' . ($lv === 1 ? $id : $sel1) . '&l2=' . ($lv === 2 ? $id : ($lv > 2 ? $sel2 : 0)) . '&l3=' . ($lv === 3 ? $id : 0);
|
||
$cnt = (int)($countByDir[$id] ?? 0);
|
||
if ($lv < 3) {
|
||
foreach ($byParent[$id] ?? [] as $ch) {
|
||
$cnt += (int)($countByDir[(int)$ch['id']] ?? 0);
|
||
foreach ($byParent[(int)$ch['id']] ?? [] as $gch) {
|
||
$cnt += (int)($countByDir[(int)$gch['id']] ?? 0);
|
||
}
|
||
}
|
||
}
|
||
?>
|
||
<div class="dir-row"<?= $selected === $id ? ' data-active' : '' ?>>
|
||
<a class="dir-name" href="<?= e($href) ?>"><?= e((string)$d['name']) ?></a>
|
||
<span class="nav-badge num"><?= $cnt ?></span>
|
||
<?php if (isAdmin()): ?>
|
||
<span class="dir-acts">
|
||
<a href="index.php?action=edit_dir&id=<?= $id ?>">改名</a>
|
||
<?php if ($lv < 3): ?><a href="index.php?action=new_dir&parent_id=<?= $id ?>">+</a><?php endif; ?>
|
||
<a href="index.php?action=delete_dir&id=<?= $id ?>" onclick="return confirm('仅空目录可删除,确认?')">删除</a>
|
||
</span>
|
||
<?php endif; ?>
|
||
</div>
|
||
<?php endforeach; endif; ?>
|
||
</div>
|
||
<?php endforeach; ?>
|
||
</div>
|
||
<?php
|
||
$previewId = $sel3 ?: ($sel2 ?: $sel1);
|
||
$previewPath = $previewId ? ($dirPath[$previewId] ?? '') : '';
|
||
$previewRows = [];
|
||
if ($previewId > 0) {
|
||
$scope = collectSubDirIds($dirs, $previewId);
|
||
foreach ($rows as $r) {
|
||
if (in_array((int)$r['directory_id'], $scope, true)) {
|
||
$previewRows[] = $r;
|
||
}
|
||
if (count($previewRows) >= 6) break;
|
||
}
|
||
}
|
||
?>
|
||
<div class="section-title">所选目录档案</div>
|
||
<p class="dir-hint"><?= $previewPath !== '' ? e($previewPath) : '请选择目录后查看该路径下的档案' ?></p>
|
||
<div class="table-card">
|
||
<table>
|
||
<thead><tr><th>档案名称</th><th>借款主体</th><th>机构</th><th class="cell-right">剩余额度</th><th>状态</th></tr></thead>
|
||
<tbody>
|
||
<?php if (!$previewRows): ?>
|
||
<tr><td colspan="5"><div class="empty-action"><div class="ea-title">该目录下暂无档案</div>选择其他目录,或到档案列表查看全部档案</div></td></tr>
|
||
<?php else: foreach ($previewRows as $a):
|
||
$st = archiveStatusWithConfig($a['end_date'], getArchiveFinanceConfig($financeAllGlobal, (int)$a['id'])); ?>
|
||
<tr>
|
||
<td><a href="index.php?action=archive_detail&id=<?= (int)$a['id'] ?>"><?= e((string)$a['name']) ?></a></td>
|
||
<td><?= e((string)$a['borrower']) ?></td>
|
||
<td><?= e((string)$a['loan_institution']) ?></td>
|
||
<td class="cell-right num cell-remaining"><?= e(amount($a['remaining_limit'] !== null ? (float)$a['remaining_limit'] : null)) ?></td>
|
||
<td><?= fusionStatusPill($st) ?></td>
|
||
</tr>
|
||
<?php endforeach; endif; ?>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
<?php if ($previewId): ?>
|
||
<p style="margin-top:12px"><a class="btn btn--plain" href="index.php?action=home&dir_id=<?= (int)$previewId ?>&kind=all">在档案列表中查看 ›</a></p>
|
||
<?php endif; ?>
|
||
<?php pageFooter(); exit; endif; ?>
|
||
|
||
<div class="page-head">
|
||
<div>
|
||
<h1 class="page-title">档案库</h1>
|
||
<div class="page-sub">金牛集团 · 银行贷款与对外担保归档</div>
|
||
<div class="seg head-switch" role="tablist" aria-label="档案库视图切换">
|
||
<a role="tab" href="index.php?action=home" data-active>档案列表</a>
|
||
<a role="tab" href="index.php?action=home&view=dirs">目录管理</a>
|
||
</div>
|
||
</div>
|
||
<div style="display:flex; gap:10px; align-items:center; flex-wrap:wrap;">
|
||
<?php if (isAdmin()): ?>
|
||
<button type="button" class="btn btn--plain" data-open-modal="import">导入</button>
|
||
<a class="btn btn--primary" href="index.php?action=archive_form<?= $dirId ? '&dir_id=' . $dirId : '' ?>">+ 新建档案</a>
|
||
<?php endif; ?>
|
||
</div>
|
||
</div>
|
||
<div class="stats">
|
||
<div class="stat stat--gold"><div class="stat-label">在保剩余额度</div><div class="stat-value num"><?= e(amount($sumRemain)) ?></div><div class="stat-foot">授信合计 <?= e(amount($sumCredit)) ?></div></div>
|
||
<div class="stat"><div class="stat-label">当前结果</div><div class="stat-value num"><?= (int)$totalCount ?><small>笔</small></div><div class="stat-foot">履行中 <?= (int)$activeCount ?> · 已完成 <?= (int)$doneCount ?></div></div>
|
||
<a class="stat stat--link" href="index.php?action=reminders&type=due"><div class="stat-label">60 日内到期</div><div class="stat-value num"><?= (int)$dueSoonCount ?><small>笔</small></div><div class="stat-foot">需提前安排资金</div></a>
|
||
<a class="stat stat--link stat--alert" href="index.php?action=reminders&type=overdue"><div class="stat-label">逾期中</div><div class="stat-value num"><?= (int)$listOver ?><small>笔</small></div><div class="stat-foot">已计逾期利息</div></a>
|
||
</div>
|
||
<form method="get" action="index.php">
|
||
<input type="hidden" name="action" value="home">
|
||
<div class="seg" role="tablist" aria-label="业务类型筛选" style="margin-bottom:12px">
|
||
<a role="tab" href="index.php?action=home&kind=all<?= $dirId ? '&dir_id='.$dirId : '' ?>"<?= $kindFilter === 'all' ? ' data-active' : '' ?>>全部</a>
|
||
<a role="tab" href="index.php?action=home&kind=<?= e(ARCHIVE_KIND_BANK_LOAN) ?><?= $dirId ? '&dir_id='.$dirId : '' ?>"<?= $kindFilter === ARCHIVE_KIND_BANK_LOAN ? ' data-active' : '' ?>>银行贷款</a>
|
||
<a role="tab" href="index.php?action=home&kind=<?= e(ARCHIVE_KIND_EXTERNAL_GUARANTEE) ?><?= $dirId ? '&dir_id='.$dirId : '' ?>"<?= $kindFilter === ARCHIVE_KIND_EXTERNAL_GUARANTEE ? ' data-active' : '' ?>>对外担保</a>
|
||
</div>
|
||
<div class="filter-bar">
|
||
<select name="dir_id" aria-label="按归档目录筛选" onchange="this.form.submit()">
|
||
<option value="0">全部目录</option>
|
||
<?php foreach ($dirs as $d): if ((int)$d['level'] !== 3) continue; ?>
|
||
<option value="<?= (int)$d['id'] ?>" <?= $dirId === (int)$d['id'] ? 'selected' : '' ?>><?= e($dirPath[(int)$d['id']] ?? $d['name']) ?></option>
|
||
<?php endforeach; ?>
|
||
</select>
|
||
<select name="search_field" aria-label="搜索字段">
|
||
<option value="all" <?= $searchField === 'all' ? 'selected' : '' ?>>全部字段</option>
|
||
<option value="borrower" <?= $searchField === 'borrower' ? 'selected' : '' ?>>借款主体</option>
|
||
<option value="guarantor" <?= $searchField === 'guarantor' ? 'selected' : '' ?>>担保人</option>
|
||
</select>
|
||
<div class="search"><input name="q" type="search" placeholder="搜索借款人 / 编号 / 机构" value="<?= e($q) ?>" aria-label="搜索档案"></div>
|
||
<select name="status" aria-label="按状态筛选">
|
||
<option value="all" <?= $statusFilter === 'all' ? 'selected' : '' ?>>全部状态</option>
|
||
<option value="not_done" <?= $statusFilter === 'not_done' ? 'selected' : '' ?>>默认(隐藏已完成)</option>
|
||
<option value="履行中" <?= $statusFilter === '履行中' ? 'selected' : '' ?>>履行中</option>
|
||
<option value="逾期中" <?= $statusFilter === '逾期中' ? 'selected' : '' ?>>逾期中</option>
|
||
<option value="已完成" <?= $statusFilter === '已完成' ? 'selected' : '' ?>>已完成</option>
|
||
<option value="待更新" <?= $statusFilter === '待更新' ? 'selected' : '' ?>>待更新</option>
|
||
</select>
|
||
<select name="sort_by">
|
||
<option value="created_at" <?= $sortBy === 'created_at' ? 'selected' : '' ?>>按创建时间</option>
|
||
<option value="end_date" <?= $sortBy === 'end_date' ? 'selected' : '' ?>>按到期日</option>
|
||
<option value="loan_institution" <?= $sortBy === 'loan_institution' ? 'selected' : '' ?>>按贷款机构</option>
|
||
</select>
|
||
<select name="sort_order">
|
||
<option value="desc" <?= $sortOrder === 'desc' ? 'selected' : '' ?>>倒序</option>
|
||
<option value="asc" <?= $sortOrder === 'asc' ? 'selected' : '' ?>>正序</option>
|
||
</select>
|
||
<select name="per_page">
|
||
<option value="10" <?= $perPage === 10 ? 'selected' : '' ?>>每页10条</option>
|
||
<option value="15" <?= $perPage === 15 ? 'selected' : '' ?>>每页15条</option>
|
||
<option value="30" <?= $perPage === 30 ? 'selected' : '' ?>>每页30条</option>
|
||
<option value="50" <?= $perPage === 50 ? 'selected' : '' ?>>每页50条</option>
|
||
</select>
|
||
<input type="hidden" name="kind" value="<?= e($kindFilter) ?>">
|
||
<button type="submit" class="btn btn--plain btn--sm">筛选</button>
|
||
<a class="btn btn--plain btn--sm" href="index.php?action=home">清除筛选</a>
|
||
<span class="filter-note num">共 <?= (int)$totalCount ?> 条</span>
|
||
</div>
|
||
</form>
|
||
<form method="get" action="index.php" id="archive-batch-form">
|
||
<input type="hidden" name="action" value="export">
|
||
<input type="hidden" name="mode" value="selected">
|
||
<input type="hidden" name="kind" value="<?= e($kindFilter) ?>">
|
||
<?php if ($dirId): ?><input type="hidden" name="dir_id" value="<?= $dirId ?>"><?php endif; ?>
|
||
<div data-batch-slot hidden class="batch-bar">
|
||
已选 <b class="num" data-batch-count>0</b> 笔
|
||
<select name="format"><option value="xlsx">xlsx</option><option value="csv">csv</option></select>
|
||
<button type="submit" class="btn btn--plain btn--sm">导出选中</button>
|
||
<?php if (isAdmin()): ?>
|
||
<button type="button" class="btn btn--danger btn--sm" onclick="requestDeleteSelected()">删除选中(双管理员)</button>
|
||
<?php endif; ?>
|
||
<a class="btn btn--plain btn--sm" href="index.php?action=export&mode=all&format=xlsx&kind=<?= e($kindFilter) ?><?= $dirId ? '&dir_id='.$dirId : '' ?>">导出全部</a>
|
||
</div>
|
||
<div class="table-card">
|
||
<table class="archive-table">
|
||
<thead>
|
||
<tr>
|
||
<th class="cell-check"><input type="checkbox" data-check-all aria-label="全选当前筛选结果"></th>
|
||
<th>类型</th>
|
||
<th>档案名称</th>
|
||
<th>目录路径</th>
|
||
<th>机构</th>
|
||
<th>借款主体</th>
|
||
<th class="cell-right">授信额度</th>
|
||
<th class="cell-right">剩余额度</th>
|
||
<th>到期日</th>
|
||
<th>年利率</th>
|
||
<th>状态</th>
|
||
<th class="cell-right">操作</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<?php if (empty($archivesPage)): ?>
|
||
<tr><td colspan="12">
|
||
<div class="empty-action">
|
||
<div class="ea-title">当前筛选无匹配档案</div>
|
||
尝试调整机构 / 状态筛选,或清空搜索关键词
|
||
<br><a class="btn btn--plain" href="index.php?action=home">清除全部筛选</a>
|
||
</div>
|
||
</td></tr>
|
||
<?php endif; ?>
|
||
<?php foreach ($archivesPage as $a):
|
||
$s = archiveStatusWithConfig($a['end_date'], getArchiveFinanceConfig($financeAllGlobal, (int)$a['id']));
|
||
$rowKind = normalizeArchiveKind($a['archive_kind'] ?? '');
|
||
$isExtGuarantee = $rowKind === ARCHIVE_KIND_EXTERNAL_GUARANTEE;
|
||
$stKey = $s === '逾期中' ? 'over' : ($s === '已完成' ? 'done' : ($s === '履行中' ? 'ok' : 'pending'));
|
||
?>
|
||
<tr data-status="<?= e($stKey) ?>">
|
||
<td class="cell-check"><input class="cid" type="checkbox" name="ids[]" value="<?= (int)$a['id'] ?>" data-row-check></td>
|
||
<td><?php if ($isExtGuarantee): ?><span class="badge-k-guar">担保</span><?php else: ?><span class="badge-k-loan">贷款</span><?php endif; ?></td>
|
||
<td><div class="cell-main"><a href="index.php?action=archive_detail&id=<?= (int)$a['id'] ?>"><?= e((string)$a['name']) ?></a></div></td>
|
||
<td class="cell-sub"><?= e($dirPath[(int)$a['directory_id']] ?? '') ?></td>
|
||
<td><?= e((string)$a['loan_institution']) ?></td>
|
||
<td><?= e((string)$a['borrower']) ?></td>
|
||
<td class="cell-right num"><?= e(amount($a['credit_limit'] !== null ? (float)$a['credit_limit'] : null)) ?></td>
|
||
<td class="cell-right num cell-remaining"><?= e(amount($a['remaining_limit'] !== null ? (float)$a['remaining_limit'] : null)) ?></td>
|
||
<td class="num"><?= e((string)$a['end_date']) ?></td>
|
||
<td class="num"><?= e($a['annual_rate'] === null ? '' : ((string)$a['annual_rate'] . '%')) ?></td>
|
||
<td><?= fusionStatusPill($s) ?></td>
|
||
<td class="cell-right">
|
||
<div class="icon-actions">
|
||
<a class="btn btn--plain btn--sm" href="index.php?action=archive_detail&id=<?= (int)$a['id'] ?>">查看</a>
|
||
<?php if (isAdmin()): ?>
|
||
<a class="btn btn--plain btn--sm" href="index.php?action=archive_form&id=<?= (int)$a['id'] ?>">编辑</a>
|
||
<a class="btn btn--danger btn--sm" href="index.php?action=request_delete_archive&id=<?= (int)$a['id'] ?>" onclick="return confirm('将提交双管理员删除申请,确认?')">删除</a>
|
||
<?php endif; ?>
|
||
</div>
|
||
</td>
|
||
</tr>
|
||
<?php endforeach; ?>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
</form>
|
||
<script>
|
||
function requestDeleteSelected(){
|
||
var ids=[].slice.call(document.querySelectorAll('.cid:checked')).map(function(x){return x.value;});
|
||
if(ids.length===0){alert('请先选择要删除的档案');return;}
|
||
if(!confirm('将提交双管理员删除申请,确认?')){return;}
|
||
window.location.href='index.php?action=request_delete_selected&ids='+encodeURIComponent(ids.join(','));
|
||
}
|
||
</script>
|
||
<div class="pager">
|
||
<div>第 <?= $currentPage ?> / <?= $totalPages ?> 页,共 <?= $totalCount ?> 条</div>
|
||
<div>
|
||
<?php
|
||
$prevQuery = http_build_query(array_merge($baseQuery, ['page' => max(1, $currentPage - 1)]));
|
||
$nextQuery = http_build_query(array_merge($baseQuery, ['page' => min($totalPages, $currentPage + 1)]));
|
||
?>
|
||
<?php if ($currentPage > 1): ?><a href="index.php?<?= e($prevQuery) ?>">上一页</a><?php else: ?><span>上一页</span><?php endif; ?>
|
||
<span> · </span>
|
||
<?php if ($currentPage < $totalPages): ?><a href="index.php?<?= e($nextQuery) ?>">下一页</a><?php else: ?><span>下一页</span><?php endif; ?>
|
||
</div>
|
||
</div>
|
||
<?php
|
||
pageFooter();
|