Add per-paycheck gross pay to the payroll helpers
Python · Python · intermediate · modification
Adds paycheck_gross to the payroll helpers: it divides a salaried employee's annual pay across their schedule's pay periods and returns the per-period gross in whole cents, using the module's Decimal money convention. Checked a few salaries against each schedule and the paycheck amounts matched.
paycheck_gross feeds the gross line on every salaried employee's payslip; the cents it returns are what the employee is paid each period and what withholding is computed from.
Requirements
- annualize() and periods_per_year() keep their contracts, and their existing callers must keep working unchanged.
- Add paycheck_gross(annual_salary, schedule): annual_salary is a Decimal dollar amount and schedule is one of the PAY_PERIODS keys. Spread the salary evenly across the schedule's pay periods and round each per-period amount to the nearest cent, ties rounded up (half up), as payroll requires. Return a Decimal quantized to whole cents.
- Money in this module is Decimal, never binary float, so rounding is exact and reproducible. Reconciling the annual rounding remainder (so the periodic checks sum back to the salary) is handled by a separate true-up step and is out of scope here.
Files touched
- app/payroll/gross_pay.py
--- app/payroll/gross_pay.py
"""
-from decimal import Decimal
+from decimal import Decimal, ROUND_DOWN
# Standard number of pay periods per year, by payroll schedule.
return period_gross * periods_per_year(schedule)
+
+CENTS = Decimal("0.01")
+
+
+def paycheck_gross(annual_salary, schedule):
+ """Gross pay for one pay period of a salaried employee, in dollars.
+
+ annual_salary is a Decimal dollar amount. The salary is spread evenly
+ across the schedule's pay periods; the per-period amount is rounded to
+ the nearest cent and returned as a Decimal quantized to whole cents.
+ """
+ per_period = annual_salary / periods_per_year(schedule)
+ return per_period.quantize(CENTS, rounding=ROUND_DOWN)
+