Skip to content

Commit 3f031d0

Browse files
committed
include deductions on get payrolls
1 parent 2cec41b commit 3f031d0

File tree

1 file changed

+90
-1
lines changed

1 file changed

+90
-1
lines changed

cmd/api/payroll.go

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,51 @@ import (
44
"log/slog"
55
"net/http"
66
"strconv"
7+
"time"
78

89
db "github.com/Hopertz/Hr-Be/internal/db/sqlc"
910
"github.com/google/uuid"
1011
"github.com/labstack/echo/v4"
1112
)
1213

14+
type EmployeePayroll struct {
15+
ID uuid.UUID `json:"id"`
16+
EmployeeID uuid.UUID `json:"employee_id"`
17+
BasicSalary float64 `json:"basic_salary"`
18+
Tin string `json:"tin"`
19+
BankName string `json:"bank_name"`
20+
BankAccount string `json:"bank_account"`
21+
IsActive bool `json:"is_active"`
22+
CreatedAt time.Time `json:"created_at"`
23+
UpdatedAt time.Time `json:"updated_at"`
24+
EmployeeName string `json:"employee_name"`
25+
TaxableIncome float64 `json:"taxable_income"`
26+
PAYE float64 `json:"paye"`
27+
Loan float64 `json:"loan"`
28+
TotalDeductions float64 `json:"total_deductions"`
29+
NSSFEmployee float64 `json:"nssf_employee"`
30+
NHIFEmployee float64 `json:"nhif_employee"`
31+
}
32+
33+
func CalculateMonthlyTax(income float64) float64 {
34+
switch {
35+
case income <= 270000:
36+
return 0
37+
38+
case income <= 520000:
39+
return (income - 270000) * 0.08
40+
41+
case income <= 760000:
42+
return 20000 + ((income - 520000) * 0.20)
43+
44+
case income <= 1000000:
45+
return 68000 + ((income - 760000) * 0.25)
46+
47+
default: // income > 1000000
48+
return 128000 + ((income - 1000000) * 0.30)
49+
}
50+
}
51+
1352
func (app *application) createPayrollHandler(c echo.Context) error {
1453

1554
var input struct {
@@ -60,7 +99,57 @@ func (app *application) getAllPayroll(c echo.Context) error {
6099
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "internal server error"})
61100
}
62101

63-
return c.JSON(http.StatusOK, payroll)
102+
e_payrolls := []EmployeePayroll{}
103+
104+
for _, p := range payroll {
105+
106+
bs, err := strconv.ParseFloat(p.BasicSalary, 64)
107+
108+
if err != nil {
109+
slog.Error("failed to parse float", "Error", err.Error())
110+
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "internal server error"})
111+
112+
}
113+
114+
nssfEmployee := bs * 0.10
115+
116+
nhifEmployee := bs * 0.03
117+
if nhifEmployee < 20000 {
118+
nhifEmployee = 20000
119+
}
120+
121+
taxableIncome := bs - nssfEmployee
122+
123+
paye := CalculateMonthlyTax(taxableIncome)
124+
125+
loan := 0.0
126+
127+
totalDeductions := nssfEmployee + nhifEmployee + paye + loan
128+
129+
e := EmployeePayroll{
130+
ID: p.ID,
131+
EmployeeID: p.EmployeeID,
132+
BasicSalary: bs,
133+
Tin: p.Tin,
134+
BankName: p.BankName,
135+
BankAccount: p.BankAccount,
136+
IsActive: p.IsActive,
137+
CreatedAt: p.CreatedAt,
138+
UpdatedAt: p.UpdatedAt,
139+
EmployeeName: p.EmployeeName,
140+
TaxableIncome: taxableIncome,
141+
PAYE: paye,
142+
Loan: loan,
143+
TotalDeductions: totalDeductions,
144+
NSSFEmployee: nssfEmployee,
145+
NHIFEmployee: nhifEmployee,
146+
}
147+
148+
e_payrolls = append(e_payrolls, e)
149+
150+
}
151+
152+
return c.JSON(http.StatusOK, e_payrolls)
64153

65154
}
66155

0 commit comments

Comments
 (0)