This commit is contained in:
admin
2026-01-22 22:10:17 +09:00
parent 536266d70b
commit 50e534094d
9 changed files with 590 additions and 17 deletions

View File

@@ -2,7 +2,7 @@
給与設定・税率・保険料管理API (Settings Management API)
"""
from fastapi import APIRouter, HTTPException, status, UploadFile, File
from typing import List
from typing import List, Optional
from datetime import date
import csv
import io
@@ -17,10 +17,40 @@ from ...core.database import get_connection
router = APIRouter(prefix="/payroll/settings", tags=["Payroll - Settings"])
# ========================================
# ヘルパー関数:年度フォールバック処理
# ========================================
def get_child_support_rate_with_fallback(target_year: int) -> Optional[dict]:
"""子ども・子育て拠出金率を取得指定年度がない場合は前年度を参照、最大3年遡る"""
with get_connection() as conn:
with conn.cursor() as cur:
# まず指定年度を検索
cur.execute(
"SELECT * FROM child_support_contribution_rates WHERE rate_year = %s ORDER BY contribution_id DESC LIMIT 1",
(target_year,)
)
result = cur.fetchone()
# 見つからない場合は前年度以前を検索
if not result:
for year_offset in range(1, 4):
previous_year = target_year - year_offset
cur.execute(
"SELECT * FROM child_support_contribution_rates WHERE rate_year = %s ORDER BY contribution_id DESC LIMIT 1",
(previous_year,)
)
result = cur.fetchone()
if result:
break
return result
# ========================================
# 給与基本設定
# ========================================
@router.post("/salary", response_model=schemas.SalarySetting, status_code=status.HTTP_201_CREATED)
def create_salary_setting(setting: schemas.SalarySettingCreate):
"""給与設定を作成"""
@@ -744,6 +774,7 @@ def calculate_income_tax(monthly_income: Decimal, dependents_count: int = 0, tar
with get_connection() as conn:
with conn.cursor() as cur:
# 月収がmonthly_income_from <= income < monthly_income_toの範囲に入るか検索
cur.execute(
"""
SELECT tax_amount FROM income_tax_table
@@ -751,13 +782,29 @@ def calculate_income_tax(monthly_income: Decimal, dependents_count: int = 0, tar
AND (valid_to IS NULL OR valid_to >= %s)
AND dependents_count = %s
AND monthly_income_from <= %s
AND monthly_income_to >= %s
AND monthly_income_to > %s
LIMIT 1
""",
(target_date, target_date, dependents_count, monthly_income, monthly_income)
)
result = cur.fetchone()
# 見つからない場合は、Toが同値のケースも検索
if not result:
cur.execute(
"""
SELECT tax_amount FROM income_tax_table
WHERE valid_from <= %s
AND (valid_to IS NULL OR valid_to >= %s)
AND dependents_count = %s
AND monthly_income_from <= %s
AND monthly_income_to = %s
LIMIT 1
""",
(target_date, target_date, dependents_count, monthly_income, monthly_income)
)
result = cur.fetchone()
if not result:
return {"monthly_income": monthly_income, "dependents_count": dependents_count, "tax_amount": 0}