@@ -272,3 +272,70 @@ func (app *application) DeletePayroll(c echo.Context) error {
272272 return c .JSON (http .StatusOK , nil )
273273
274274}
275+
276+ func (app * application ) updatePayrollHandler (c echo.Context ) error {
277+
278+ var input struct {
279+ BasicSalary * float64 `json:"basic_salary"`
280+ TIN * string `json:"tin"`
281+ BankName * string `json:"bank_name"`
282+ BankAccount * string `json:"bank_account"`
283+ IsActive * bool `json:"is_active"`
284+ }
285+
286+ if err := c .Bind (& input ); err != nil {
287+ return err
288+ }
289+
290+ if err := app .validator .Struct (input ); err != nil {
291+ return c .JSON (http .StatusBadRequest , map [string ]string {"error" : err .Error ()})
292+ }
293+
294+ e , err := app .store .JustGetPayroll (c .Request ().Context ())
295+
296+ if err != nil {
297+ slog .Error ("Error Getting employee for update " , "Error" , err .Error ())
298+ return c .JSON (http .StatusInternalServerError , map [string ]string {"error" : "internal server error" })
299+ }
300+
301+ if input .BasicSalary != nil {
302+ rounded := float64 (int (* input .BasicSalary * 100 )) / 100
303+ bs := strconv .FormatFloat (rounded , 'f' , 2 , 64 )
304+ e .BasicSalary = bs
305+ }
306+
307+ if input .BankAccount != nil {
308+ e .BankAccount = * input .BankAccount
309+ }
310+
311+ if input .BankName != nil {
312+ e .BankName = * input .BankName
313+ }
314+
315+ if input .TIN != nil {
316+ e .Tin = * input .TIN
317+ }
318+
319+ if input .IsActive != nil {
320+ e .IsActive = * input .IsActive
321+ }
322+
323+ args := db.UpdatePayrollParams {
324+ ID : e .ID ,
325+ BasicSalary : e .BasicSalary ,
326+ Tin : e .Tin ,
327+ BankName : e .BankName ,
328+ BankAccount : e .BankAccount ,
329+ IsActive : e .IsActive ,
330+ }
331+
332+ err = app .store .UpdatePayroll (c .Request ().Context (), args )
333+
334+ if err != nil {
335+ slog .Error ("Error updating payroll " , "Error" , err .Error ())
336+ return c .JSON (http .StatusInternalServerError , map [string ]string {"error" : "internal server error" })
337+ }
338+
339+ return c .JSON (http .StatusOK , map [string ]string {"success" : "payroll update successful" })
340+
341+ }
0 commit comments