Skip to content

hany202507/finance-ai-skills / skills/법인세-통합고용

법인세-통합고용

Verified

월별 급여대장과 사원명부로 통합고용세액공제(조세특례제한법 제29조의8)의 상시근로자 수를 사람별 · 달별로 세고, 단계별 검증 시트와 법정 서식(조특 별지 제10호의9서식 공제세액계산서 · 제10호의10서식 상시근로자 명세서)을 엑셀 수식으로 채운 워크북을 만든다. 엔진이 같은 값을 따로 계산해 수식 값과 대조한다. "통합고용세액공제", "통합고용", "상시근로자 수", "청년등 상시근로자", "고용 세액공제 계산", "조특 10호의9", "10호의10", "상시근로자 명세서", "급여대장으로 고용 공제", "고용 증가 인원", "청년 간주"가 나오거나 급여대장과 사원명부로 고용 공제를 계산해야 할 때 쓴다. 2025 과세연도까지 계산하고, 2026 과세연도의 기존공제 · 신규공제도 계산하고 서식(2)와 종전 서식(1)을 채운다.

scripts/ctax/employ/calc.py

View on GitHub
"""통합고용세액공제 상시근로자 수와 공제액을 파이썬으로 계산한다(엑셀 수식과 따로 계산해 서로 맞춰 보는 쪽).

기준은 rules.yaml, 1인당 공제금액은 ctax/wp/rates_2025/credits.yaml 의 employment_integrated 다. 인원은 Fraction 으로 센다.
2026 과세연도부터는 인원을 두 벌 센다: 종전 범위(기존공제 2 · 3차 판정)와 새 범위(신규공제 · 추가공제, rules.yaml scope.scope_2026_from).
"""
from __future__ import annotations

import calendar
from dataclasses import dataclass, field
from datetime import date, timedelta
from fractions import Fraction

from .model import PERIODS, Company, Inputs, Person, rule

FAR = date(9999, 12, 31)
REASONS = {"a": "ⓐ근로계약기간 1년 미만", "b": "ⓑ단시간 근로자", "c": "ⓒ법인의 임원", "d": "ⓓ최대주주(출자자) 및 그 친족",
           "e": "ⓔ원천징수사실 미확인 근로자 등", "f": "ⓕ기타", "f_res": "ⓕ기타(내국인 아님)", "f_disp": "ⓕ기타(파견근로자)",
           "a_new": "ⓐ근로기간 1년 미만"}
YOUTH_KINDS = ("장애인", "고령자", "경력단절", "북한이탈주민", "청년")


def add_months(d: date, n: int) -> date:
    """엑셀 EDATE 와 같다: 달을 옮기고 날이 없으면 그 달 말일."""
    m = d.month - 1 + n
    y, m = d.year + m // 12, m % 12 + 1
    return date(y, m, min(d.day, calendar.monthrange(y, m)[1]))


def month_end(d: date) -> date:
    return date(d.year, d.month, calendar.monthrange(d.year, d.month)[1])


def floor2(x: Fraction) -> Fraction:
    """100분의 1 미만은 버린다."""
    return Fraction(int(x * 100), 100) if x > 0 else Fraction(0)


@dataclass
class Period:
    name: str                 # 해당 · 직전 · 직전전
    start: date
    end: date
    ends: list[date]          # 과세연도 안의 월말
    deemed: bool              # 청년 간주(계약 체결일 기준)를 쓰는지
    has_payroll: bool = False

    @property
    def months(self) -> int:
        return len(self.ends)


@dataclass
class Row:
    """한 사람의 한 과세연도 판정."""
    person: Person
    proof: str = ""                         # 납부 확인: Y · N · 자료없음
    regular: bool = False
    reason: str = ""
    weight: Fraction = Fraction(1)
    worked: list[int] = field(default_factory=list)       # 근무개월로 세는 달(0 · 1). 새 범위는 근로기간 1년이 된 달부터
    employed: list[int] = field(default_factory=list)     # 월말 재직이고 급여가 있는 달(근로기간 판정 전)
    youth_age: list[int] = field(default_factory=list)    # 청년등(현재 연령)
    youth_deem: list[int] = field(default_factory=list)   # 청년등(간주 포함)
    paid: list[int] = field(default_factory=list)         # 그 달 지급액
    kind: str = ""                          # 청년등 구분
    m_work: Fraction = Fraction(0)          # ⑳ 상시근로자 근무개월수(단시간 가중)
    n_work: Fraction = Fraction(0)          # ㉑ 계산한 수
    m_youth_age: Fraction = Fraction(0)
    n_youth_age: Fraction = Fraction(0)
    m_youth_deem: Fraction = Fraction(0)
    n_youth_deem: Fraction = Fraction(0)
    no_pay: int = 0                         # 월말 재직인데 급여가 없는 달 수
    odd_pay: int = 0                        # 재직 기간이 아닌데 급여가 있는 달 수


@dataclass
class Count:
    """한 과세연도 인원. 근로자별 방식과 월말 합 방식."""
    period: Period
    rows: list[Row]
    total: Fraction = Fraction(0)           # 근로자별 ② 상시
    youth_age: Fraction = Fraction(0)       # 근로자별 ⑨ 청년등(현재 연령)
    youth_deem: Fraction = Fraction(0)      # 근로자별 ⑨ 청년등(간주 포함)
    me_sum: Fraction = Fraction(0)          # 월말 합(상시)
    me_youth_sum: Fraction = Fraction(0)    # 월말 합(청년등, 현재 연령)
    me_total: Fraction = Fraction(0)        # 월말 합 방식 상시
    me_youth: Fraction = Fraction(0)        # 월말 합 방식 청년등
    monthly: list[Fraction] = field(default_factory=list)        # 달마다 월말 현재 상시근로자 수
    monthly_paid: list[int] = field(default_factory=list)        # 달마다 급여가 있는 사람 수
    monthly_worked: list[int] = field(default_factory=list)      # 달마다 월말 재직 사람 수(상시 여부와 무관)


# ---------------------------------------------------------------- 사람

def adjusted_birth(p: Person, rules: dict) -> date:
    """병역 기간(6년 한도)을 뺀 나이를 재려고 생년월일을 그만큼 늦춘다. 입대일 · 전역일이 있으면 날 수로, 없고 「병역 개월 수」 만
    있으면 개월 단위로 늦춘다(엑셀 EDATE 와 같다, rules.yaml youth.military_months)."""
    if p.mil_from and p.mil_to:
        days = min((p.mil_to - p.mil_from).days + 1, int(rule(rules, "youth", "military_cap_days")))
        return p.birth + timedelta(days=max(0, days))
    if p.mil_months:
        return add_months(p.birth, min(int(p.mil_months), int(rule(rules, "youth", "military_cap_months"))))
    return p.birth


def age_limits(p: Person, rules: dict) -> tuple[date, date]:
    """(15세가 되는 날, 병역을 뺀 나이로 35세가 되는 날). 이 사이의 날은 청년 나이다."""
    return (add_months(p.birth, 12 * int(rule(rules, "youth", "age_from"))),
            add_months(adjusted_birth(p, rules), 12 * (int(rule(rules, "youth", "age_to")) + 1)))


def deemed_until(p: Person, size: str, rules: dict) -> date | None:
    """계약 체결일에 청년 나이였던 정규직이 청년으로 간주되는 기간의 끝(그날부터 청년이 아니다). 해당 없으면 None.
    간주는 2025년 1월 1일 이후 체결한 계약에만 있다(2026-10-12 회계사 확인, rules.yaml youth.deemed_from)."""
    lo, hi = age_limits(p, rules)
    if p.kind != "정규직" or not (lo <= p.hired < hi) or p.hired < date.fromisoformat(rule(rules, "youth", "deemed_from")):
        return None
    key = "deemed_years_2025" if p.hired.year == 2025 else "deemed_years_other"
    return add_months(p.hired, 12 * int(rule(rules, "youth", key)[size]))


def youth_cutoff(p: Person, size: str, rules: dict, deemed: bool) -> date:
    """청년 제외시점: 이 날부터 청년(제1호)이 아니다."""
    hi = age_limits(p, rules)[1]
    until = deemed_until(p, size, rules) if deemed else None
    return max(hi, until) if until else hi


def is_senior(p: Person, rules: dict) -> bool:
    return p.hired >= add_months(p.birth, 12 * int(rule(rules, "youth", "senior_age")))


def defector_since(co: Company, rules: dict) -> date:
    """북한이탈주민을 나이와 상관없이 청년등으로 세는 입사일 하한: 2025년 1월 1일 이후 처음 개시하는 과세연도의 시작일.
    그 전 과세연도에 고용한 북한이탈주민은 종전 규정이라 청년등이 아니다(부칙 <대통령령 제35347호> 제9조, rules.yaml youth.defector_from)."""
    d0 = date.fromisoformat(rule(rules, "youth", "defector_from"))
    d = date(d0.year, co.fy_start.month, 1)
    return d if d >= d0 else date(d0.year + 1, co.fy_start.month, 1)


def is_defector(p: Person, since: date) -> bool:
    return p.defector == "Y" and p.hired >= since


def fixed_youth(p: Person, rules: dict, since: date) -> bool:
    """나이와 상관없이 청년등인 사람(장애인 등 · 고령자 · 경력단절 · 북한이탈주민). since 는 defector_since."""
    return p.disabled == "Y" or is_senior(p, rules) or p.career == "Y" or is_defector(p, since)


def youth_kind(p: Person, rules: dict, since: date) -> str:
    if p.disabled == "Y":
        return "장애인"
    if is_senior(p, rules):
        return "고령자"
    if p.career == "Y":
        return "경력단절"
    if is_defector(p, since):
        return "북한이탈주민"
    return "청년"


def one_year_day(p: Person) -> date:
    """근로기간이 1년이 되는 날(입사일부터 12개월 뒤의 전날). 이 날이 그 달 말일 이전이면 그 달부터 새 범위 근무개월에 넣는다
    (rules.yaml scope.service_year, 국세청 사전-2026-법규소득-0365 와 같다)."""
    return add_months(p.hired, 12) - timedelta(days=1)


def hours_for(p: Person, period: str, scope: str) -> tuple[float | None, bool]:
    """단시간근로자 판정에 쓰는 시간과, 새 범위에서 월 평균 근로시간이 없어 월 소정근로시간으로 대신했는지.
    종전 범위는 월 소정근로시간, 새 범위는 그 과세연도 월 평균 근로시간(직전전은 입력 열이 없어 소정근로시간)."""
    if scope != "새":
        return p.hours, False
    v = p.avg_hours if period == "해당" else p.avg_hours_prev if period == "직전" else None
    return (v, False) if v is not None else (p.hours, True)


def common_reason(p: Person, rules: dict, hours: float | None = None, scope: str = "종전") -> str:
    """과세연도와 상관없는 상시근로자 제외 사유(없으면 빈 글). 새 범위는 「계약 1년 미만」 을 보지 않고
    근로기간 1년(count_period)으로 가린다. hours 를 주지 않으면 월 소정근로시간."""
    h = p.hours if hours is None else hours
    if p.resident == "N":
        return REASONS["f_res"]
    if p.kind == "파견":
        return REASONS["f_disp"]
    if p.short == "Y" and scope != "새":
        return REASONS["a"]
    if p.kind == "단시간" and (h or 0) < float(rule(rules, "scope", "part_time_hours")):
        return REASONS["b"]
    if p.officer == "Y":
        return REASONS["c"]
    if p.owner == "Y":
        return REASONS["d"]
    if p.other == "Y":
        return REASONS["f"]
    return ""


def weight_of(p: Person, rules: dict, hours: float | None = None) -> Fraction:
    h = p.hours if hours is None else hours
    if p.kind == "단시간" and (h or 0) >= float(rule(rules, "scope", "part_time_hours")):
        key = "part_time_weight_supported" if p.supported == "Y" else "part_time_weight"
        return Fraction(str(rule(rules, "scope", key)))
    return Fraction(1)


# ---------------------------------------------------------------- 과세연도

def periods(co: Company, rules: dict) -> dict[str, Period]:
    """해당 · 직전 · 직전전 과세연도. 직전 · 직전전은 회사 시트 시작일이 있으면 그 날부터 다음 과세연도 시작 전날까지,
    없으면 12개월로 본다(rules.yaml count.prior_months). 인원은 그 기간의 월말 수(개월 수)로 나눈다."""
    out = {}
    deemed_from = date.fromisoformat(rule(rules, "youth", "deemed_from"))
    s1 = co.prev_start or add_months(co.fy_start, -12)
    s2 = co.prev2_start or add_months(s1, -12)
    spans = ((co.fy_start, co.fy_end), (s1, co.fy_start - timedelta(days=1)), (s2, s1 - timedelta(days=1)))
    for name, (s, e) in zip(PERIODS, spans):
        ends, m = [], s
        while month_end(m) <= e:
            ends.append(month_end(m))
            m = add_months(date(m.year, m.month, 1), 1)
        # 간주는 2025년 이후 계약만 있으므로 2025년 이후 개시 과세연도의 인원에만 의미가 있다(직전 · 직전전도 그 연도 신고서와 같게 센다)
        out[name] = Period(name, s, e, ends, deemed=s >= deemed_from and co.deem != "N")
    return out


def count_period(inp: Inputs, per: Period, rules: dict, scope: str = "종전", sum_floor: bool = False) -> Count:
    """한 과세연도 인원. scope 「새」 는 2026 과세연도부터의 상시근로자 범위(근로기간 1년이 된 달부터, 단시간은 월 평균 근로시간).
    sum_floor 면 사람별 계산한 수를 버리지 않고 더한 뒤 100분의 1 미만을 버린다(rules.yaml count.sum_floor_from)."""
    co = inp.company
    pays = [x for x in inp.pays if per.start <= x.month <= per.end]
    per.has_payroll = bool(pays)
    by_id: dict[str, list] = {}
    for x in pays:
        by_id.setdefault(x.id, []).append(x)
    cut_n = (lambda x: x) if sum_floor else floor2                      # noqa: E731  사람별 계산한 수
    since = defector_since(co, rules)
    rows = []
    for p in inp.staff:
        hours = hours_for(p, per.name, scope)[0]
        r = Row(person=p, weight=weight_of(p, rules, hours), kind=youth_kind(p, rules, since))
        mine = by_id.get(p.id, [])
        if not per.has_payroll:
            r.proof = "자료없음"
        else:
            r.proof = "Y" if sum(x.tax + x.pension + x.health for x in mine) > 0 else "N"
        common = common_reason(p, rules, hours, scope)
        r.regular = not common and r.proof != "N"
        r.reason = common or ("" if r.proof != "N" else REASONS["e"])
        lo, hi = age_limits(p, rules)
        cut = youth_cutoff(p, co.size, rules, per.deemed)
        fixed = fixed_youth(p, rules, since)
        left = p.left or FAR
        year1 = one_year_day(p) if scope == "새" else None
        for e in per.ends:
            first = date(e.year, e.month, 1)
            paid = sum(x.pay for x in mine if x.month == first)
            on = p.hired <= e <= left                    # 월말 재직
            # 급여대장이 있는 과세연도는 그 달 급여가 있어야 근무개월로 센다(2026-10-12 회계사 확인, rules.yaml count.unpaid_month)
            w = 1 if on and (paid or not per.has_payroll or co.unpaid == "Y") else 0
            r.employed.append(w)
            if year1 and year1 > e:                      # 새 범위: 월말 현재 근로기간 1년 미만인 달은 넣지 않는다
                w = 0
            r.worked.append(w)
            r.youth_age.append(1 if w and (fixed or (p.kind == "정규직" and lo <= e < hi)) else 0)
            r.youth_deem.append(1 if w and (fixed or (p.kind == "정규직" and lo <= e < cut)) else 0)
            r.paid.append(paid)
            if on and not paid and per.has_payroll:
                r.no_pay += 1
            if paid and (e < p.hired or first > left):
                r.odd_pay += 1
        if r.regular and any(r.employed) and not any(r.worked):
            r.regular, r.reason = False, REASONS["a_new"]          # 과세연도 안에 근로기간 1년이 되지 않았다
        if r.regular:
            m = Fraction(per.months)
            r.m_work = r.weight * sum(r.worked)
            r.n_work = cut_n(r.m_work / m)
            r.m_youth_age = r.weight * sum(r.youth_age)
            r.n_youth_age = cut_n(r.m_youth_age / m)
            r.m_youth_deem = r.weight * sum(r.youth_deem)
            r.n_youth_deem = cut_n(r.m_youth_deem / m)
        rows.append(r)
    c = Count(period=per, rows=rows)
    reg = [r for r in rows if r.regular]
    cut_t = floor2 if sum_floor else (lambda x: x)                     # noqa: E731  합계
    c.total = cut_t(sum((r.n_work for r in reg), Fraction(0)))
    c.youth_age = cut_t(sum((r.n_youth_age for r in reg), Fraction(0)))
    c.youth_deem = cut_t(sum((r.n_youth_deem for r in reg), Fraction(0)))
    for i in range(per.months):
        c.monthly.append(sum((r.weight * r.worked[i] for r in reg), Fraction(0)))
        c.monthly_paid.append(sum(1 for r in rows if r.paid[i] > 0))
        c.monthly_worked.append(sum(r.worked[i] for r in rows))
    c.me_sum = sum(c.monthly, Fraction(0))
    c.me_youth_sum = sum((r.weight * sum(r.youth_age) for r in reg), Fraction(0))
    if per.months:
        c.me_total = floor2(c.me_sum / per.months)
        c.me_youth = floor2(c.me_youth_sum / per.months)
    return c


# ---------------------------------------------------------------- 공제액

def per_head(size: str, capital: str, rates: dict) -> tuple[int, int]:
    """(청년등, 청년등 외) 1인당 공제금액(조세특례제한법 제29조의8 제1항 제1호 · 제2호)."""
    r = lambda k: int(rates["employment_integrated"][k]["value"])   # noqa: E731
    if size == "중소기업":
        tail = "capital" if capital == "Y" else "local"
        return r(f"youth_sme_{tail}"), r(f"other_sme_{tail}")
    if size == "중견기업":
        return r("youth_mid"), r("other_mid")
    return r("youth_general"), r("other_general")


def new_rate(rates: dict, part: str, term: str, size: str, capital: str) -> int:
    """신규공제(2026 과세연도부터) 1인당 금액. part 는 youth · other, term 은 a(직전 대비) · b(전전) · c(전전전)."""
    d = rates["employment_integrated_new"][f"{part}_{term}"]["value"]
    key = ("중소기업_수도권" if capital == "Y" else "중소기업_수도권밖") if size == "중소기업" else size
    return int(d[key])


def new_credit(size: str, capital: str, cur: tuple, priors: list, rates: dict) -> dict:
    """조세특례제한법 제29조의8 제1항(2025.12.23 개정)의 기본공제(신규공제).

    cur = (상시, 청년등), priors = [직전, 전전, 전전전] 각각 (상시, 청년등) 또는 None(비교에서 제외하거나 자료 없음).
    A 는 직전, B 는 전전(해당 · 직전 중 적은 수), C 는 전전전(해당 · 직전 · 전전 중 가장 적은 수) 대비 증가 인원이다.
    """
    n = rates["employment_integrated_new"]
    terms, base, stop = [], [cur[0], cur[1], cur[0] - cur[1]], False
    for k, pv in zip("abc", priors):
        if pv is None or stop:
            stop = True
            terms.append(None)
            continue
        po = pv[0] - pv[1]
        d_t, d_y, d_o = base[0] - pv[0], base[1] - pv[1], base[2] - po
        if d_t > 0:
            c_y, c_o = max(Fraction(0), min(d_y, d_t)), max(Fraction(0), min(d_o, d_t))
        else:
            c_y = c_o = Fraction(0)
        r_y, r_o = new_rate(rates, "youth", k, size, capital), new_rate(rates, "other", k, size, capital)
        terms.append({"term": k, "d_t": d_t, "d_y": d_y, "d_o": d_o, "n_y": c_y, "n_o": c_o, "rate_y": r_y, "rate_o": r_o,
                      "amt_y": round(c_y * r_y), "amt_o": round(c_o * r_o), "ded": 0, "min_rule": ""})
        base = [min(base[0], pv[0]), min(base[1], pv[1]), min(base[2], po)]
    got = [t for t in terms if t]
    out = {"terms": terms, "youth": sum(t["amt_y"] for t in got), "other": sum(t["amt_o"] for t in got), "ded": 0, "min_hire": 0,
           "gate": "해당 없음", "credit": 0}
    if size != "중소기업":
        m = int(n["min_hire"]["value"][size])
        out["min_hire"] = m
        mo, ms = n["min_ge"]["value"][size], n["min_short"]["value"][size]
        for t in got:
            if t["d_t"] <= 0:
                continue
            if t["n_o"] >= m:
                t["ded"] = int(mo[t["term"]]) * m
                t["min_rule"] = "청년등 외 증가 인원이 최소고용증가인원수 이상"
            else:
                first, second = ms[t["term"]]
                t["ded"] = round(t["n_o"] * int(first) + (m - t["n_o"]) * int(second))
                t["min_rule"] = "청년등 외 증가 인원이 최소고용증가인원수 미만"
                # 제3호의 「청년등외상시근로자의 증가 인원 수」 는 제2호가 「뺀 값」 으로 정해 음수일 수 있고, 제3호에는 0 으로 본다는
                # 글이 없다(「음수이면 0」 은 제1호 · 제2호 금액에 대한 말). 엔진은 0 으로 두고 음수를 넣은 값을 따로 알린다
                raw = min(t["d_o"], t["d_t"])
                if raw < 0:
                    t["n_o_raw"] = raw
                    t["ded_alt"] = round(raw * int(first) + (m - raw) * int(second))
        out["ded"] = sum(t["ded"] for t in got)
        if any("ded_alt" in t for t in got):
            out["ded_alt"] = sum(t.get("ded_alt", t["ded"]) for t in got)
        top = max((t["d_t"] for t in got), default=Fraction(0))
        out["gate"] = "충족" if top > m else "미충족"
    else:
        out["gate"] = "해당 없음"
    if out["gate"] != "미충족":
        out["credit"] = max(0, out["youth"] + out["other"] - out["ded"])
        if "ded_alt" in out:
            out["credit_alt"] = max(0, out["youth"] + out["other"] - out["ded_alt"])
    return out


def returnees(inp: Inputs, per: Period, rules: dict, rates: dict) -> list[dict]:
    """사원명부에서 해당 과세연도 육아휴직 복귀자를 가린다(조세특례제한법 제29조의8 제5항, rules.yaml extra.return_rule).
    「육아휴직 복직일」 이 있거나 「육아휴직 복귀자」 가 Y 인 사람마다 {id, name, counted, why, no_leave_from} 를 돌려준다."""
    until = date.fromisoformat(rates["employment_integrated_extra"]["return_until"]["value"])
    out = []
    for p in inp.staff:
        if not (p.returned_on or p.returned == "Y"):
            continue
        why = ""
        r = p.returned_on
        if r is None:
            why = "육아휴직 복직일이 없습니다"
        elif not (per.start <= r <= per.end):
            why = f"복직일 {r} 이 해당 과세연도가 아닙니다"
        elif r > until:
            why = f"복직일 {r} 이 기한({until})을 지났습니다"
        elif p.owner == "Y":
            why = "최대주주 · 최대출자자나 그 친족입니다(제5항 제3호)"
        elif p.left and p.left < r:
            why = f"퇴사일 {p.left} 이 복직일보다 앞섭니다"
        elif p.leave_from and r < add_months(p.leave_from, 6):
            why = "육아휴직 기간이 연속 6개월이 되지 않습니다(제5항 제2호)"
        elif p.leave_from and p.leave_from < add_months(p.hired, 12):
            why = "육아휴직 전 근무 기간이 1년이 되지 않습니다(제5항 제1호)"
        out.append({"id": p.id, "name": p.name, "counted": not why, "why": why, "no_leave_from": not why and p.leave_from is None,
                    "in_year": bool(r and per.start <= r <= per.end)})
    return out


def regime_of(co: Company, rates: dict) -> str:
    """"2025이전": 2025년 12월 31일이 속하는 과세연도까지(제29조의8 종전 규정, 서식(1)). "2026": 그 뒤 과세연도(새 규정, 서식(2))."""
    return "2026" if co.fy_start >= date.fromisoformat(rates["employment_integrated_new"]["applies_from"]["value"]) else "2025이전"


def compute(inp: Inputs, rules: dict, rates: dict) -> dict:
    """인원(세 과세연도)과 공제액. 2025 과세연도까지는 1 · 2 · 3차 연도 · 추가공제, 2026 과세연도부터는
    기존공제(2 · 3차 연도) · 신규공제 · 추가공제."""
    co = inp.company
    pers = periods(co, rules)
    cur = pers["해당"]
    # 2026 과세연도 신고부터 근로자별 인원은 계산한 수를 더한 뒤 버린다(부칙 <대통령령 제36342호> 제2조 제2항)
    sum_floor = cur.start >= date.fromisoformat(rule(rules, "count", "sum_floor_from"))
    new_scope = cur.start >= date.fromisoformat(rule(rules, "scope", "scope_2026_from"))
    counts = {name: count_period(inp, per, rules, "종전", sum_floor) for name, per in pers.items()}
    counts_new = {name: count_period(inp, per, rules, "새", sum_floor) for name, per in pers.items()}
    deemed_from = date.fromisoformat(rule(rules, "youth", "deemed_from"))
    method = "근로자별" if cur.start >= deemed_from else "월말합"
    regime = regime_of(co, rates)
    new_reg = regime == "2026"
    last_year = int(rule(rules, "regime", "last_supported_year"))
    unsupported = ""
    first_from = date.fromisoformat(rule(rules, "regime", "first_year_from"))
    if co.fy_start < first_from:
        unsupported = (f"통합고용세액공제는 {first_from.year}년 {first_from.month}월 {first_from.day}일 이후 개시하는 과세연도부터 적용합니다"
                       "(조세특례제한법 제29조의8 신설, 부칙 <법률 제19199호> 제2조 제1항). 그 전 과세연도는 고용증대 세액공제(제29조의7)와 "
                       "중소기업 사회보험료 세액공제(제30조의4)로 계산하십시오. 공제액을 계산하지 않았습니다")
    elif new_reg and "youth_a" not in rates["employment_integrated_new"]:
        unsupported = ("2026년 12월 31일이 속하는 과세연도부터는 신규공제 금액표가 필요한데 아직 기준 파일에 넣지 못했습니다. "
                       "공제액을 계산하지 않았습니다")
    elif new_reg and cur.end.year > last_year:
        unsupported = (f"{last_year + 1}년 12월 31일이 속하는 과세연도부터는 신규공제 C(전전전 과세연도 대비)에 {last_year - 2} 과세연도 인원이 "
                       "필요한데 이 워크북은 직전전 과세연도까지만 셉니다. 공제액을 계산하지 않았습니다")
    in_time = co.fy_start <= date(2025, 12, 31)                # 종전 규정의 1차 연도 공제는 2025년 12월 31일이 속하는 과세연도까지
    # 사원명부가 비어 있고 회사 시트에 인원을 직접 넣었으면 그 숫자로 잠정 계산한다(rules.yaml count.direct_input)
    direct = not inp.staff and co.direct_given
    direct_of = {"해당": (co.direct_cur_total, co.direct_cur_youth), "직전": (co.direct_prev_total, co.direct_prev_youth),
                 "직전전": (co.direct_prev2_total, co.direct_prev2_youth)}

    def direct_val(name: str):
        t, y = direct_of[name]
        if not direct or t is None:
            return None
        return floor2(Fraction(str(t))), floor2(Fraction(str(y or 0)))

    def filed(name: str):
        t = co.filed_prev_total if name == "직전" else co.filed_prev2_total
        y = co.filed_prev_youth if name == "직전" else co.filed_prev2_youth
        if t is None:
            return None
        t, y = floor2(Fraction(str(t))), floor2(Fraction(str(y or 0)))
        return t, y

    # 서식에 쓰는 인원(근로자별 방식)과 월말 합 방식 인원. 직전전은 2025년 이후 개시 과세연도일 때만 근로자별로 센다
    used, legacy, source = {}, {}, {}
    for name in PERIODS:
        c = counts[name]
        f = filed(name) if name != "해당" else None
        dv = direct_val(name)
        if dv:
            used[name] = legacy[name] = dv
            source[name] = "직접 입력"
        elif name == "해당" or c.period.has_payroll:
            if method == "근로자별" and (name != "직전전" or pers[name].start >= deemed_from):
                y = c.youth_deem if pers[name].deemed else c.youth_age
                used[name] = (c.total, y)
            else:
                used[name] = (c.me_total, c.me_youth)
            legacy[name] = (c.me_total, c.me_youth)
            source[name] = "급여대장"
        elif f:
            used[name], legacy[name], source[name] = f, f, "전기 신고서"
        elif name == "직전" and co.startup == "Y":
            used[name] = legacy[name] = (Fraction(0), Fraction(0))
            source[name] = "창업(0)"
        else:
            used[name] = legacy[name] = (Fraction(0), Fraction(0))
            source[name] = "없음"

    # 새 범위 인원(2026 과세연도부터 신규공제 · 추가공제). 비교 연도도 새 범위로 다시 센다(rules.yaml scope.prior_recount).
    # 급여대장이 없는 연도는 종전 인원(전기 신고서 · 창업 0)을 그대로 쓴다
    used_new = {}
    for name in PERIODS:
        c = counts_new[name]
        if source[name] == "급여대장":
            used_new[name] = (c.total, c.youth_deem if pers[name].deemed else c.youth_age)
        else:
            used_new[name] = used[name]

    (t_c, y_c), (t_p, y_p) = used["해당"], used["직전"]
    o_c, o_p = t_c - y_c, t_p - y_p
    d_t, d_y, d_o = t_c - t_p, y_c - y_p, o_c - o_p
    prev_ok = source["직전"] != "없음"
    if new_reg:
        a_y, a_o = new_rate(rates, "youth", "a", co.size, co.capital), new_rate(rates, "other", "a", co.size, co.capital)
        n_y = n_o = Fraction(0)
        first_y = first_o = 0
    else:
        a_y, a_o = per_head(co.size, co.capital, rates)
        ok = co.consumer != "Y" and in_time and prev_ok and d_t > 0 and not unsupported
        n_y = max(Fraction(0), min(d_y, d_t)) if ok else Fraction(0)
        n_o = max(Fraction(0), min(d_o, d_t)) if ok else Fraction(0)
        first_y, first_o = round(n_y * a_y), round(n_o * a_o)

    # 기존공제: 2차 · 3차 연도. 인원은 최초 공제연도가 2025년 이후면 근로자별, 그 전이면 월말 합 방식으로 센다
    def pick(*names, per_worker: bool):
        return [(used if per_worker else legacy)[n] for n in names]

    def judge(prev_amounts: tuple, vals: list, kind: str) -> tuple[int, str]:
        """vals = 판정에 쓰는 (상시, 청년등) 목록. 2차는 [해당, 직전], 3차는 [해당, 직전, 직전전]."""
        if kind == "2":
            (tc, yc), (tp, yp) = vals
            if tc < tp:
                return 0, "전체 감소"
            cut = yc < yp
            return (0 if cut else prev_amounts[0]) + prev_amounts[1], ("청년등 감소" if cut and prev_amounts[0] else "")
        (tc, yc), (tp, yp), (tpp, ypp) = vals
        if tp < tpp or tc < tpp:
            return 0, "전체 감소"
        cut = yp < ypp or yc < ypp
        return (0 if cut else prev_amounts[0]) + prev_amounts[1], ("청년등 감소" if cut and prev_amounts[0] else "")

    def youth_switch(prev_amounts: tuple, vals: list, kind: str) -> dict | None:
        """청년등만 줄고 전체는 유지됐을 때, 제29조의7 회신(서면-2023-법인-0978, 서면-2024-소득-4000)대로 최초 공제연도
        청년등 증가 인원에 청년등 외 1인당 금액을 계속 공제하면 얼마인지. 엔진은 이 금액을 공제액에 넣지 않는다."""
        y_rate, o_rate = per_head(co.size, co.capital, rates)
        if not (prev_amounts[0] and y_rate and o_rate):
            return None
        n1 = Fraction(int(prev_amounts[0])) / y_rate                    # 최초 공제연도 청년등 증가 인원(공제액 ÷ 1인당 금액)
        if kind == "2":
            (_, yc), (_, yp) = vals
            dec = yp - yc
        else:
            (_, yc), (_, yp), (_, ypp) = vals
            dec = ypp - min(yp, yc)
        dec = max(Fraction(0), min(dec, n1))
        return {"n1": n1, "dec": dec, "rate": o_rate, "amount": round(n1 * o_rate), "amount_dec": round(dec * o_rate)}

    pw2 = pers["직전"].start >= deemed_from
    pw3 = pers["직전전"].start >= deemed_from
    second, second_cut, second_alt = 0, "", None
    second_youth_alt = third_youth_alt = None
    f1 = (co.first_prev_youth, co.first_prev_other)
    if any(f1) and co.consumer != "Y":
        if pers["직전"].end > date(2025, 12, 31):              # 2026 과세연도부터 늘어난 인원은 신규공제라 2차 연도가 없다
            second_cut = "직전 과세연도 증가분은 신규공제(2차 연도 없음)"
        elif source["직전"] in ("없음", "창업(0)"):
            second_cut = "직전 인원 없음"
        else:
            second, second_cut = judge(f1, pick("해당", "직전", per_worker=pw2), "2")
            if second_cut == "청년등 감소":
                second_youth_alt = youth_switch(f1, pick("해당", "직전", per_worker=pw2), "2")
            if pw2:                                              # 월말 합 방식으로 세면 판정이 달라지는지 알린다
                alt, _ = judge(f1, pick("해당", "직전", per_worker=False), "2")
                second_alt = alt if alt != second else None
    third, third_cut, third_alt = 0, "", None
    f2 = (co.first_prev2_youth, co.first_prev2_other)
    if any(f2) and co.consumer != "Y":
        if pers["직전전"].end > date(2025, 12, 31):
            third_cut = "직전전 과세연도 증가분은 신규공제(3차 연도 없음)"
        elif co.size == "일반기업":
            third_cut = "일반기업"
        elif source["직전전"] == "없음" or source["직전"] in ("없음", "창업(0)"):
            third_cut = "직전전 인원 없음"
        else:
            third, third_cut = judge(f2, pick("해당", "직전", "직전전", per_worker=pw3), "3")
            if third_cut == "청년등 감소":
                third_youth_alt = youth_switch(f2, pick("해당", "직전", "직전전", per_worker=pw3), "3")
            if pw3:
                alt, _ = judge(f2, pick("해당", "직전", "직전전", per_worker=False), "3")
                third_alt = alt if alt != third else None

    # 신규공제(2026 과세연도부터): 직전 3개 과세연도 중 2024년 12월 31일 이전에 개시한 과세연도는 비교에서 뺀다
    cutoff = date.fromisoformat(rates["employment_integrated_new"]["exclude_before"]["value"])
    new = None
    if new_reg and not unsupported:
        priors = []
        for name in ("직전", "직전전"):
            ok_p = source[name] != "없음" and pers[name].start > cutoff
            priors.append(used_new[name] if ok_p else None)
        priors.append(None)                                      # 전전전은 이 워크북이 세지 않는다(지원 연도는 2027 까지)
        if co.consumer == "Y" or not prev_ok:
            priors = [None, None, None]
        new = new_credit(co.size, co.capital, used_new["해당"], priors, rates)
    new_total = new["credit"] if new else 0

    backs = returnees(inp, cur, rules, rates)
    returned_roster = sum(1 for x in backs if x["counted"])
    returned = co.returned if co.returned is not None else returned_roster
    amt_sizes = {"중소기업": int(rule(rules, "extra", "return_sme")), "중견기업": int(rule(rules, "extra", "return_mid"))}
    amt = amt_sizes.get(co.size, 0)
    return_in_time = co.fy_start <= date.fromisoformat(rates["employment_integrated_extra"]["return_until"]["value"])
    x_c, x_p = (used_new if new_scope else used)["해당"][0], (used_new if new_scope else used)["직전"][0]   # 추가공제 인원 비교(㉝ · ㉞)
    extra = int(round(float(returned) * amt)) if (co.consumer != "Y" and return_in_time and prev_ok and x_c >= x_p
                                                and co.size in amt_sizes and not unsupported) else 0
    if unsupported:
        second = third = 0

    return {"periods": pers, "counts": counts, "counts_new": counts_new, "new_scope": new_scope, "sum_floor": sum_floor,
            "method": method, "regime": regime, "in_time": in_time, "unsupported": unsupported,
            "used": used, "used_new": used_new, "legacy": legacy, "source": source, "d_t": d_t, "d_y": d_y, "d_o": d_o,
            "n_y": n_y, "n_o": n_o,
            "a_y": a_y, "a_o": a_o, "first_y": first_y, "first_o": first_o, "first": first_y + first_o, "second": second,
            "second_cut": second_cut, "second_alt": second_alt, "second_pw": pw2, "third": third, "third_cut": third_cut,
            "third_alt": third_alt, "third_pw": pw3, "second_youth_alt": second_youth_alt, "third_youth_alt": third_youth_alt,
            "defector_since": defector_since(co, rules), "new": new, "new_total": new_total, "returned": float(returned),
            "returned_roster": returned_roster, "returnees": backs, "direct": direct,
            "extra": extra, "total": first_y + first_o + second + third + new_total + extra}
slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack."Stars: 198kUpdated Claude CodeCodexVerifiedInstall
ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt.Stars: 194kUpdated Claude CodeCodexVerifiedInstall