fix(management): compute applied/common admin expense by monthly active project distribution

This commit is contained in:
2026-05-04 10:05:38 +09:00
parent eab0a5bb3f
commit b3207b5ce0

View File

@@ -3487,25 +3487,47 @@ class Handler(BaseHTTPRequestHandler):
} }
) )
management_split_rows = conn.execute( construction_project_rows = conn.execute(
"""
select
pm.project_code,
pm.start_date,
pm.end_date,
min(case when coalesce(t.transaction_date, '') <> '' then t.transaction_date end) as min_tx_date,
max(case when coalesce(t.transaction_date, '') <> '' then t.transaction_date end) as max_tx_date
from project_master pm
left join ptc_transactions t on t.project_code = pm.project_code
where coalesce(pm.project_type, '') = '시공'
group by pm.project_code, pm.start_date, pm.end_date
"""
).fetchall()
active_counts_by_month: dict[str, int] = defaultdict(int)
for row in construction_project_rows:
start_ym = _to_year_month(row["start_date"]) or _to_year_month(row["min_tx_date"])
end_ym = _to_year_month(row["end_date"]) or _to_year_month(row["max_tx_date"]) or start_ym
if not start_ym or not end_ym:
continue
for ym in _iter_year_months(start_ym, end_ym):
active_counts_by_month[ym] += 1
monthly_management_pool_rows = conn.execute(
f""" f"""
select select
substr(coalesce(transaction_date, ''), 1, 4) as year, substr(coalesce(transaction_date, ''), 1, 7) as ym,
coalesce(project_code, '') as project_code,
account_code_final as account_code, account_code_final as account_code,
coalesce(sum(case when in_out = '출금' then supply_amount else 0 end), 0) as expense_supply coalesce(sum(case when in_out = '출금' then supply_amount else 0 end), 0) as expense_supply
from ptc_transactions from ptc_transactions
{where} {where}
group by substr(coalesce(transaction_date, ''), 1, 4), coalesce(project_code, ''), account_code_final group by substr(coalesce(transaction_date, ''), 1, 7), account_code_final
having year <> '' having ym <> ''
""", """,
values, values,
).fetchall() ).fetchall()
for row in management_split_rows: for row in monthly_management_pool_rows:
year = (row["year"] or "").strip() or "미상" ym = (row["ym"] or "").strip()
year = ym[:4] if len(ym) >= 4 else "미상"
account_code = (row["account_code"] or "").strip() account_code = (row["account_code"] or "").strip()
project_code = (row["project_code"] or "").strip()
expense_supply = float(row["expense_supply"] or 0) expense_supply = float(row["expense_supply"] or 0)
if not account_code or expense_supply == 0: if not account_code or expense_supply == 0:
continue continue
@@ -3527,8 +3549,8 @@ class Handler(BaseHTTPRequestHandler):
"excluded_expense_total": 0.0, "excluded_expense_total": 0.0,
"excluded_accounts": [], "excluded_accounts": [],
} }
is_project_applied = bool(project_code) and "-관리-" not in project_code active_count = int(active_counts_by_month.get(ym) or 0)
if is_project_applied: if active_count > 0:
by_year[year]["project_applied_admin_expense"] += expense_supply by_year[year]["project_applied_admin_expense"] += expense_supply
else: else:
by_year[year]["common_admin_expense"] += expense_supply by_year[year]["common_admin_expense"] += expense_supply