37 lines
946 B
Python
37 lines
946 B
Python
from app import db
|
|
from app.models import WorkEntry, LeaveBalance
|
|
import sqlalchemy as sa
|
|
from datetime import date
|
|
|
|
|
|
def compute_leave_used(year: int) -> dict[str, int]:
|
|
start = date(year, 1, 1)
|
|
end = date(year, 12, 31)
|
|
|
|
conges = db.session.scalar(
|
|
sa.select(sa.func.count()).where(
|
|
WorkEntry.date.between(start, end),
|
|
WorkEntry.day_type == "CONGE",
|
|
)
|
|
) or 0
|
|
|
|
rtt = db.session.scalar(
|
|
sa.select(sa.func.count()).where(
|
|
WorkEntry.date.between(start, end),
|
|
WorkEntry.day_type == "RTT",
|
|
)
|
|
) or 0
|
|
|
|
return {"conges": conges, "rtt": rtt}
|
|
|
|
|
|
def get_or_create_balance(year: int) -> LeaveBalance:
|
|
balance = db.session.scalar(
|
|
sa.select(LeaveBalance).where(LeaveBalance.year == year)
|
|
)
|
|
if balance is None:
|
|
balance = LeaveBalance(year=year)
|
|
db.session.add(balance)
|
|
db.session.commit()
|
|
return balance
|