From b801dda0311057aa2340098631aac77bce80221e Mon Sep 17 00:00:00 2001 From: jiangyong27 Date: Mon, 4 Sep 2023 21:59:27 +0800 Subject: [PATCH] salary2 --- common/config/error.go | 15 +++ conf/template/table.html | 64 +++++++++++ server/controller/staff.go | 32 ++++++ server/service/staff_salary.go | 192 +++++++++++++++++++++++++++++++++ 4 files changed, 303 insertions(+) create mode 100644 common/config/error.go create mode 100644 conf/template/table.html create mode 100644 server/controller/staff.go create mode 100644 server/service/staff_salary.go diff --git a/common/config/error.go b/common/config/error.go new file mode 100644 index 0000000..fddb220 --- /dev/null +++ b/common/config/error.go @@ -0,0 +1,15 @@ +package config + +import "github.com/gogap/errors" + +// 这里是框架统一错误码,业务错误码以业务编号开头放到业务common目录下 +var ( + // 系统错误码 + ErrOK = errors.T(0, "操作成功") + + ErrPriv = errors.T(196, "没有权限") + ErrDb = errors.T(197, "数据库错误") + ErrRemote = errors.T(198, "第三方错误") + ErrInternal = errors.T(199, "内部错误") + ErrParam = errors.T(400, "参数错误") +) diff --git a/conf/template/table.html b/conf/template/table.html new file mode 100644 index 0000000..b5636b3 --- /dev/null +++ b/conf/template/table.html @@ -0,0 +1,64 @@ + + + + + + + + + +
+
{{.title}}
+
+ + + + {{range $i, $v := .header}} + + {{end}} + + + + + {{range $i, $arr := .data}} + + {{range $j, $v := $arr}} + + {{end}} + + {{end}} + +
{{ $v }}
{{ $v }}
+
+
+ + + + \ No newline at end of file diff --git a/server/controller/staff.go b/server/controller/staff.go new file mode 100644 index 0000000..24f4df5 --- /dev/null +++ b/server/controller/staff.go @@ -0,0 +1,32 @@ +package controller + +import ( + "enterprise/server/service" + "github.com/gin-gonic/gin" + "strings" + "time" +) + +type Staff struct { +} + +func (s *Staff) Salary(ctx *gin.Context) { + month := ctx.Query("month") + tp := ctx.Query("type") + if month == "" { + month = time.Now().AddDate(0, -1, 0).Format("200601") + } + month = strings.ReplaceAll(month, "-", "") + if tp == "" { + tp = service.StaffSalaryTypeSummary + } + serv := new(service.StaffSalary) + + if tp == service.StaffSalaryTypeAgent { + serv.Agent(month, ctx) + } else if tp == service.StaffSalaryTypeBank { + serv.Bank(month, ctx) + } else { + serv.Summary(month, ctx) + } +} diff --git a/server/service/staff_salary.go b/server/service/staff_salary.go new file mode 100644 index 0000000..48ea4aa --- /dev/null +++ b/server/service/staff_salary.go @@ -0,0 +1,192 @@ +package service + +import ( + "enterprise/common/config" + "enterprise/common/dao" + "enterprise/common/model" + "fmt" + "github.com/gin-gonic/gin" + log "github.com/sirupsen/logrus" + "github.com/spf13/cast" + "os" + "time" + + "github.com/xuri/excelize/v2" + "net/http" +) + +var ( + StaffSalaryTypeBank = "bank" + StaffSalaryTypeSummary = "summary" + StaffSalaryTypeAgent = "agent" +) + +type StaffSalary struct { +} + +func (s *StaffSalary) Agent(month string, ctx *gin.Context) { + xls := ctx.Query("xls") + staffSalarys, err := dao.NewStaffSalaryDao().Query(month) + if err != nil { + panic(config.ErrDb.New().Append(err)) + } + + header := []string{"姓名", "身份证号", "电话", "基本工资", "应发工资", "社保扣除", "个税扣除", "实发工资"} + datas := make([][]string, 0) + for _, staff := range staffSalarys { + baseInfo, err := dao.NewStaffInfoDao().GetByUsername(staff.Username) + if err != nil { + log.Errorf("db error :%s", err.Error()) + continue + } + if baseInfo == nil { + continue + } + if staff.BaseSalary == 0 { + continue + } + item := make([]string, 0) + item = append(item, baseInfo.Realname) + item = append(item, cast.ToString(baseInfo.Idno)) + item = append(item, cast.ToString(baseInfo.Phone)) + item = append(item, cast.ToString(staff.BaseSalary)) + item = append(item, cast.ToString(staff.RealSalary)) + item = append(item, cast.ToString(staff.SocialInsurence)) + item = append(item, cast.ToString(staff.PersonalTax)) + item = append(item, cast.ToString(staff.RealSalary-staff.SocialInsurence-staff.PersonalTax)) + + datas = append(datas, item) + } + + if xls != "" { + filename := fmt.Sprintf("agent_%d.xlsx", time.Now().Unix()) + s.toExcel(filename, header, datas, ctx) + os.Remove(filename) + } else { + ctx.HTML(http.StatusOK, "table.html", gin.H{ + "title": month, + "header": header, + "data": datas, + }) + } +} + +func (s *StaffSalary) Bank(month string, ctx *gin.Context) { + xls := ctx.Query("xls") + staffSalarys, err := dao.NewStaffSalaryDao().Query(month) + if err != nil { + panic(config.ErrDb.New().Append(err)) + } + + header := []string{"账号", "户名", "金额", "开户行", "开户地", "收款备注"} + datas := make([][]string, 0) + for _, staff := range staffSalarys { + baseInfo, err := dao.NewStaffInfoDao().GetByUsername(staff.Username) + if err != nil { + log.Errorf("db error :%s", err.Error()) + continue + } + if baseInfo == nil { + continue + } + if staff.BaseSalary == 0 { + continue + } + item := make([]string, 0) + item = append(item, baseInfo.BankCard) + item = append(item, baseInfo.Realname) + item = append(item, cast.ToString(staff.RealSalary-staff.SocialInsurence-staff.PersonalTax)) + item = append(item, cast.ToString(baseInfo.BankName)) + item = append(item, "重庆市") + item = append(item, fmt.Sprintf("%s工资", month)) + datas = append(datas, item) + } + + if xls != "" { + filename := fmt.Sprintf("bank_%d.xlsx", time.Now().Unix()) + s.toExcel(filename, header, datas, ctx) + os.Remove(filename) + } else { + ctx.HTML(http.StatusOK, "table.html", gin.H{ + "title": month, + "header": header, + "data": datas, + }) + } +} + +func (s *StaffSalary) Summary(month string, ctx *gin.Context) { + xls := ctx.Query("xls") + staffSalarys, err := dao.NewStaffSalaryDao().Query(month) + if err != nil { + panic(config.ErrDb.New().Append(err)) + } + + header := []string{"姓名", "身份证号", "入职日期", "转正日期", "基本工资", "出勤工资", "社保扣除", "个税扣除", "请假天数", "实发工资"} + datas := make([][]string, 0) + summary := new(model.StaffSalary) + for _, staff := range staffSalarys { + baseInfo, err := dao.NewStaffInfoDao().GetByUsername(staff.Username) + if err != nil { + log.Errorf("db error :%s", err.Error()) + continue + } + if baseInfo == nil { + continue + } + + item := make([]string, 0) + item = append(item, baseInfo.Realname) + item = append(item, cast.ToString(baseInfo.Idno)) + item = append(item, cast.ToString(baseInfo.EntryDate)) + item = append(item, cast.ToString(baseInfo.OfficialDate)) + item = append(item, cast.ToString(staff.BaseSalary)) + item = append(item, cast.ToString(staff.RealSalary)) + item = append(item, cast.ToString(staff.SocialInsurence)) + item = append(item, cast.ToString(staff.PersonalTax)) + item = append(item, cast.ToString(staff.Holiday)) + item = append(item, cast.ToString(staff.RealSalary-staff.SocialInsurence-staff.PersonalTax)) + + datas = append(datas, item) + summary.BaseSalary += staff.BaseSalary + summary.RealSalary += staff.RealSalary + summary.SocialInsurence += staff.SocialInsurence + summary.PersonalTax += staff.PersonalTax + summary.Holiday += staff.Holiday + } + datas = append(datas, []string{"合计", "-", "-", "-", + cast.ToString(summary.BaseSalary), cast.ToString(summary.RealSalary), cast.ToString(summary.SocialInsurence), + cast.ToString(summary.PersonalTax), cast.ToString(summary.Holiday), + cast.ToString(summary.RealSalary - summary.SocialInsurence - summary.PersonalTax)}) + + if xls != "" { + filename := fmt.Sprintf("summary_%d.xlsx", time.Now().Unix()) + s.toExcel(filename, header, datas, ctx) + os.Remove(filename) + } else { + ctx.HTML(http.StatusOK, "table.html", gin.H{ + "title": month, + "header": header, + "data": datas, + }) + } +} + +func (s *StaffSalary) toExcel(filePath string, header []string, datas [][]string, ctx *gin.Context) { + f := excelize.NewFile() + //声明工作表的名称 + sheetName := "工资单" + f.SetSheetName("Sheet1", sheetName) + + f.SetSheetRow(sheetName, "A1", &header) + for i, data := range datas { + f.SetSheetRow(sheetName, fmt.Sprintf("A%d", i+2), &data) + } + if err := f.SaveAs(filePath); err != nil { + panic(config.ErrInternal.New().Append(err)) + } + + ctx.Writer.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=%s", filePath)) + ctx.Writer.Header().Add("Content-Type", "application/msexcel") + ctx.File(filePath) +}