feat: leave balance calculation (congés/RTT)
This commit is contained in:
36
app/business/leave_calc.py
Normal file
36
app/business/leave_calc.py
Normal file
@@ -0,0 +1,36 @@
|
||||
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
|
||||
Reference in New Issue
Block a user