| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- // Code generated by fkit v3.11.0. DO NOT EDIT.
- //
- // Source: schema/payroll/aggregates.fkit
- // Regenerate with: go run ./tools/fkitgen ./schema/payroll
- package payroll
- import (
- "context"
- "database/sql"
- )
- // PayrollCycleTotals is the generated aggregate row for a payroll cycle.
- type PayrollCycleTotals struct {
- CycleID string
- Payslips int64
- GrossCents int64
- DeductionCents int64
- NetCents int64
- }
- // CalculatePayrollCycleTotals runs the generated SUM aggregate over the
- // payslip rows of one cycle. It totals what is already stored; it does not
- // calculate any payslip.
- func CalculatePayrollCycleTotals(ctx context.Context, db *sql.DB, cycleID string) (PayrollCycleTotals, error) {
- const q = `SELECT count(*), COALESCE(sum(gross_cents), 0), COALESCE(sum(deduction_cents), 0),
- COALESCE(sum(net_cents), 0)
- FROM payslip WHERE cycle_id = $1`
- var t PayrollCycleTotals
- t.CycleID = cycleID
- err := db.QueryRowContext(ctx, q, cycleID).Scan(&t.Payslips, &t.GrossCents, &t.DeductionCents, &t.NetCents)
- return t, err
- }
- // CalculatePayslipNet recomputes net from the stored gross and deduction
- // columns of one row. Pure column arithmetic — no pay rules.
- func CalculatePayslipNet(row PayslipRow) int64 {
- return row.GrossCents - row.DeductionCents
- }
- // CalculatePayrollCycleAverage averages the stored net over a cycle.
- func CalculatePayrollCycleAverage(ctx context.Context, db *sql.DB, cycleID string) (int64, error) {
- totals, err := CalculatePayrollCycleTotals(ctx, db, cycleID)
- if err != nil {
- return 0, err
- }
- if totals.Payslips == 0 {
- return 0, nil
- }
- return totals.NetCents / totals.Payslips, nil
- }
- // CalculateEmployeeYearToDate sums an employee's stored payslips for a year.
- func CalculateEmployeeYearToDate(ctx context.Context, db *sql.DB, employeeID string, year int) (int64, error) {
- const q = `SELECT COALESCE(sum(net_cents), 0) FROM payslip
- WHERE employee_id = $1 AND extract(year from period_from) = $2`
- var n int64
- err := db.QueryRowContext(ctx, q, employeeID, year).Scan(&n)
- return n, err
- }
|