from decimal import Decimal from django.db.models import Sum from finance.models import PayrollOffCyclePayment def period_payment_summary(period, rows): extra = PayrollOffCyclePayment.objects.filter( paid_on__range=(period.date_from, period.date_to), currency=period.currency, voided_at__isnull=True, ).aggregate(total=Sum("amount"))["total"] or Decimal("0.00") payroll = sum((Decimal(row["payment_total"]) for row in rows), Decimal("0.00")) return { "payroll_recorded": str(payroll), "outside_period": str(extra), "total_recorded": str(payroll + extra), } def period_settlements(period): # A live projection beside immutable historical amounts. from finance.off_cycle.services import statement_settlement return ( { statement.employee_id: { key: str(value) if isinstance(value, Decimal) else value for key, value in statement_settlement(statement).items() } for statement in period.current_run.statements.filter( off_cycle_payments__isnull=False, ) .select_related("payment_confirmation") .distinct() } if period.current_run_id else {} )