payslip.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Code generated by fkit v3.11.0. DO NOT EDIT.
  2. //
  3. // Source: schema/payroll/payslip.fkit
  4. // Regenerate with: go run ./tools/fkitgen ./schema/payroll
  5. package payroll
  6. import (
  7. "context"
  8. "database/sql"
  9. "time"
  10. )
  11. // PayslipRow is the generated row type for table payslip.
  12. type PayslipRow struct {
  13. ID string
  14. CycleID string
  15. EmployeeID string
  16. Currency string
  17. PeriodFrom time.Time
  18. PeriodTo time.Time
  19. GrossCents int64
  20. DeductionCents int64
  21. NetCents int64
  22. Underwater bool
  23. RunAt time.Time
  24. RunReason string
  25. CreatedAt time.Time
  26. UpdatedAt time.Time
  27. }
  28. // PayslipCreateRequest is the generated create payload for table payslip.
  29. type PayslipCreateRequest struct {
  30. CycleID string `json:"cycleId"`
  31. EmployeeID string `json:"employeeId"`
  32. Currency string `json:"currency"`
  33. GrossCents int64 `json:"grossCents"`
  34. DeductionCents int64 `json:"deductionCents"`
  35. NetCents int64 `json:"netCents"`
  36. }
  37. // PayslipUpdateRequest is the generated update payload for table payslip.
  38. type PayslipUpdateRequest struct {
  39. GrossCents *int64 `json:"grossCents,omitempty"`
  40. DeductionCents *int64 `json:"deductionCents,omitempty"`
  41. NetCents *int64 `json:"netCents,omitempty"`
  42. RunReason *string `json:"runReason,omitempty"`
  43. }
  44. // CreatePayslip inserts one payslip row.
  45. func CreatePayslip(ctx context.Context, db *sql.DB, req PayslipCreateRequest) (PayslipRow, error) {
  46. const q = `INSERT INTO payslip (cycle_id, employee_id, currency, gross_cents, deduction_cents, net_cents)
  47. VALUES ($1, $2, $3, $4, $5, $6) RETURNING *`
  48. row := db.QueryRowContext(ctx, q, req.CycleID, req.EmployeeID, req.Currency, req.GrossCents, req.DeductionCents, req.NetCents)
  49. return scanPayslip(row)
  50. }
  51. // GetPayslip selects one payslip row by primary key.
  52. func GetPayslip(ctx context.Context, db *sql.DB, id string) (PayslipRow, error) {
  53. const q = `SELECT * FROM payslip WHERE id = $1`
  54. return scanPayslip(db.QueryRowContext(ctx, q, id))
  55. }
  56. // UpdatePayslip patches one payslip row.
  57. func UpdatePayslip(ctx context.Context, db *sql.DB, id string, req PayslipUpdateRequest) (PayslipRow, error) {
  58. const q = `UPDATE payslip SET gross_cents = COALESCE($2, gross_cents),
  59. deduction_cents = COALESCE($3, deduction_cents),
  60. net_cents = COALESCE($4, net_cents),
  61. run_reason = COALESCE($5, run_reason),
  62. updated_at = now() WHERE id = $1 RETURNING *`
  63. return scanPayslip(db.QueryRowContext(ctx, q, id, req.GrossCents, req.DeductionCents, req.NetCents, req.RunReason))
  64. }
  65. // DeletePayslip removes one payslip row.
  66. func DeletePayslip(ctx context.Context, db *sql.DB, id string) error {
  67. const q = `DELETE FROM payslip WHERE id = $1`
  68. _, err := db.ExecContext(ctx, q, id)
  69. return err
  70. }
  71. // ListPayslipsByCycle selects every payslip row for a cycle.
  72. func ListPayslipsByCycle(ctx context.Context, db *sql.DB, cycleID string) ([]PayslipRow, error) {
  73. const q = `SELECT * FROM payslip WHERE cycle_id = $1 ORDER BY employee_id`
  74. rows, err := db.QueryContext(ctx, q, cycleID)
  75. if err != nil {
  76. return nil, err
  77. }
  78. defer rows.Close()
  79. var out []PayslipRow
  80. for rows.Next() {
  81. var r PayslipRow
  82. if err := rows.Scan(&r.ID, &r.CycleID, &r.EmployeeID, &r.Currency, &r.PeriodFrom, &r.PeriodTo,
  83. &r.GrossCents, &r.DeductionCents, &r.NetCents, &r.Underwater, &r.RunAt, &r.RunReason,
  84. &r.CreatedAt, &r.UpdatedAt); err != nil {
  85. return nil, err
  86. }
  87. out = append(out, r)
  88. }
  89. return out, rows.Err()
  90. }
  91. // CountPayslipsByCycle counts payslip rows for a cycle.
  92. func CountPayslipsByCycle(ctx context.Context, db *sql.DB, cycleID string) (int64, error) {
  93. const q = `SELECT count(*) FROM payslip WHERE cycle_id = $1`
  94. var n int64
  95. err := db.QueryRowContext(ctx, q, cycleID).Scan(&n)
  96. return n, err
  97. }
  98. // BuildPayslip maps a create request onto a row. Field copy only — the
  99. // generator has no knowledge of pay rules.
  100. func BuildPayslip(req PayslipCreateRequest) PayslipRow {
  101. return PayslipRow{
  102. CycleID: req.CycleID,
  103. EmployeeID: req.EmployeeID,
  104. Currency: req.Currency,
  105. GrossCents: req.GrossCents,
  106. DeductionCents: req.DeductionCents,
  107. NetCents: req.NetCents,
  108. }
  109. }
  110. func scanPayslip(row *sql.Row) (PayslipRow, error) {
  111. var r PayslipRow
  112. err := row.Scan(&r.ID, &r.CycleID, &r.EmployeeID, &r.Currency, &r.PeriodFrom, &r.PeriodTo,
  113. &r.GrossCents, &r.DeductionCents, &r.NetCents, &r.Underwater, &r.RunAt, &r.RunReason,
  114. &r.CreatedAt, &r.UpdatedAt)
  115. return r, err
  116. }