This commit is contained in:
jiangyong27 2023-09-04 21:59:27 +08:00
parent 98b0466fb9
commit b801dda031
4 changed files with 303 additions and 0 deletions

15
common/config/error.go Normal file
View File

@ -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, "参数错误")
)

64
conf/template/table.html Normal file
View File

@ -0,0 +1,64 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
body {
display: flex;
justify-content: center;
}
table {
border-collapse: collapse;
}
table, table tr th, table tr td {
border: 1px solid #000000;
}
p {
margin: 0
}
table th {
background-color: rgba(128, 128, 128, 0.5);
}
.title{
width: 100%;
text-align: center;
}
</style>
</head>
<body>
<div class="table-container">
<div class="title">{{.title}}</div>
<div id="table-body">
<table>
<thead>
<tr>
{{range $i, $v := .header}}
<th>{{ $v }}</th>
{{end}}
</tr>
</thead>
<tbody>
{{range $i, $arr := .data}}
<tr>
{{range $j, $v := $arr}}
<td>{{ $v }}</td>
{{end}}
</tr>
{{end}}
</tbody>
</table>
</div>
</div>
<script>
</script>
</body>
</html>

View File

@ -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)
}
}

View File

@ -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)
}