| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- // Code generated by fkit v3.11.0. DO NOT EDIT.
- //
- // Source: schema/payroll/payslip.fkit
- // Regenerate with: go run ./tools/fkitgen ./schema/payroll
- package payroll
- import (
- "context"
- "database/sql"
- "time"
- )
- // PayslipRow is the generated row type for table payslip.
- type PayslipRow struct {
- ID string
- CycleID string
- EmployeeID string
- Currency string
- PeriodFrom time.Time
- PeriodTo time.Time
- GrossCents int64
- DeductionCents int64
- NetCents int64
- Underwater bool
- RunAt time.Time
- RunReason string
- CreatedAt time.Time
- UpdatedAt time.Time
- }
- // PayslipCreateRequest is the generated create payload for table payslip.
- type PayslipCreateRequest struct {
- CycleID string `json:"cycleId"`
- EmployeeID string `json:"employeeId"`
- Currency string `json:"currency"`
- GrossCents int64 `json:"grossCents"`
- DeductionCents int64 `json:"deductionCents"`
- NetCents int64 `json:"netCents"`
- }
- // PayslipUpdateRequest is the generated update payload for table payslip.
- type PayslipUpdateRequest struct {
- GrossCents *int64 `json:"grossCents,omitempty"`
- DeductionCents *int64 `json:"deductionCents,omitempty"`
- NetCents *int64 `json:"netCents,omitempty"`
- RunReason *string `json:"runReason,omitempty"`
- }
- // CreatePayslip inserts one payslip row.
- func CreatePayslip(ctx context.Context, db *sql.DB, req PayslipCreateRequest) (PayslipRow, error) {
- const q = `INSERT INTO payslip (cycle_id, employee_id, currency, gross_cents, deduction_cents, net_cents)
- VALUES ($1, $2, $3, $4, $5, $6) RETURNING *`
- row := db.QueryRowContext(ctx, q, req.CycleID, req.EmployeeID, req.Currency, req.GrossCents, req.DeductionCents, req.NetCents)
- return scanPayslip(row)
- }
- // GetPayslip selects one payslip row by primary key.
- func GetPayslip(ctx context.Context, db *sql.DB, id string) (PayslipRow, error) {
- const q = `SELECT * FROM payslip WHERE id = $1`
- return scanPayslip(db.QueryRowContext(ctx, q, id))
- }
- // UpdatePayslip patches one payslip row.
- func UpdatePayslip(ctx context.Context, db *sql.DB, id string, req PayslipUpdateRequest) (PayslipRow, error) {
- const q = `UPDATE payslip SET gross_cents = COALESCE($2, gross_cents),
- deduction_cents = COALESCE($3, deduction_cents),
- net_cents = COALESCE($4, net_cents),
- run_reason = COALESCE($5, run_reason),
- updated_at = now() WHERE id = $1 RETURNING *`
- return scanPayslip(db.QueryRowContext(ctx, q, id, req.GrossCents, req.DeductionCents, req.NetCents, req.RunReason))
- }
- // DeletePayslip removes one payslip row.
- func DeletePayslip(ctx context.Context, db *sql.DB, id string) error {
- const q = `DELETE FROM payslip WHERE id = $1`
- _, err := db.ExecContext(ctx, q, id)
- return err
- }
- // ListPayslipsByCycle selects every payslip row for a cycle.
- func ListPayslipsByCycle(ctx context.Context, db *sql.DB, cycleID string) ([]PayslipRow, error) {
- const q = `SELECT * FROM payslip WHERE cycle_id = $1 ORDER BY employee_id`
- rows, err := db.QueryContext(ctx, q, cycleID)
- if err != nil {
- return nil, err
- }
- defer rows.Close()
- var out []PayslipRow
- for rows.Next() {
- var r PayslipRow
- if err := rows.Scan(&r.ID, &r.CycleID, &r.EmployeeID, &r.Currency, &r.PeriodFrom, &r.PeriodTo,
- &r.GrossCents, &r.DeductionCents, &r.NetCents, &r.Underwater, &r.RunAt, &r.RunReason,
- &r.CreatedAt, &r.UpdatedAt); err != nil {
- return nil, err
- }
- out = append(out, r)
- }
- return out, rows.Err()
- }
- // CountPayslipsByCycle counts payslip rows for a cycle.
- func CountPayslipsByCycle(ctx context.Context, db *sql.DB, cycleID string) (int64, error) {
- const q = `SELECT count(*) FROM payslip WHERE cycle_id = $1`
- var n int64
- err := db.QueryRowContext(ctx, q, cycleID).Scan(&n)
- return n, err
- }
- // BuildPayslip maps a create request onto a row. Field copy only — the
- // generator has no knowledge of pay rules.
- func BuildPayslip(req PayslipCreateRequest) PayslipRow {
- return PayslipRow{
- CycleID: req.CycleID,
- EmployeeID: req.EmployeeID,
- Currency: req.Currency,
- GrossCents: req.GrossCents,
- DeductionCents: req.DeductionCents,
- NetCents: req.NetCents,
- }
- }
- func scanPayslip(row *sql.Row) (PayslipRow, error) {
- var r PayslipRow
- err := row.Scan(&r.ID, &r.CycleID, &r.EmployeeID, &r.Currency, &r.PeriodFrom, &r.PeriodTo,
- &r.GrossCents, &r.DeductionCents, &r.NetCents, &r.Underwater, &r.RunAt, &r.RunReason,
- &r.CreatedAt, &r.UpdatedAt)
- return r, err
- }
|