enterprise/server/service/staff_salary.go

251 lines
7.8 KiB
Go
Raw Normal View History

2023-09-04 21:59:27 +08:00
package service
import (
2024-03-01 17:52:24 +08:00
"encoding/json"
2023-09-05 10:49:24 +08:00
butil "enterprise/base/util"
2023-09-04 21:59:27 +08:00
"enterprise/common/config"
"enterprise/common/dao"
"enterprise/common/model"
"fmt"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"github.com/spf13/cast"
2023-10-06 10:17:32 +08:00
excelize "github.com/xuri/excelize/v2"
2023-09-04 21:59:27 +08:00
"os"
"time"
"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))
}
2023-10-07 14:30:31 +08:00
header := []string{"姓名", "身份证号", "电话", "基本工资", "出勤工资", "奖金", "社保扣除", "个税扣除", "应发工资"}
2023-09-04 21:59:27 +08:00
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))
2024-04-02 12:14:23 +08:00
item = append(item, cast.ToString(staff.AttendSalary))
2023-10-07 14:30:31 +08:00
item = append(item, cast.ToString(staff.AwardSalary))
2023-09-04 21:59:27 +08:00
item = append(item, cast.ToString(staff.SocialInsurence))
item = append(item, cast.ToString(staff.PersonalTax))
2024-04-02 13:54:39 +08:00
item = append(item, cast.ToString(staff.GetRealSalary()))
2023-09-04 21:59:27 +08:00
datas = append(datas, item)
}
if xls != "" {
2023-09-05 16:13:26 +08:00
filename := fmt.Sprintf("agent_%s.xlsx", time.Now().Format("20060102_150405"))
2023-09-04 21:59:27 +08:00
s.toExcel(filename, header, datas, ctx)
os.Remove(filename)
} else {
2023-09-04 22:21:04 +08:00
links := make([]map[string]string, 0)
links = append(links, map[string]string{
2023-09-05 20:39:09 +08:00
"url": "/staff/salary?type=agent&xls=1",
2023-09-04 22:21:04 +08:00
"name": "下载",
})
links = append(links, map[string]string{
2023-09-05 20:39:09 +08:00
"url": "/staff/salary?type=bank",
2023-09-04 22:21:04 +08:00
"name": "银行",
})
links = append(links, map[string]string{
2023-09-05 20:39:09 +08:00
"url": "/staff/salary?type=summary",
2023-09-04 22:21:04 +08:00
"name": "汇总",
})
ctx.HTML(http.StatusOK, "salary.html", gin.H{
"title": month + "工资汇总",
2023-09-04 21:59:27 +08:00
"header": header,
"data": datas,
2023-09-04 22:21:04 +08:00
"link": links,
2023-09-04 21:59:27 +08:00
})
}
}
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)
2024-04-02 13:54:39 +08:00
item = append(item, cast.ToString(staff.GetRealSalary()))
2023-09-04 21:59:27 +08:00
item = append(item, cast.ToString(baseInfo.BankName))
item = append(item, "重庆市")
item = append(item, fmt.Sprintf("%s工资", month))
datas = append(datas, item)
}
if xls != "" {
2023-09-05 16:13:26 +08:00
filename := fmt.Sprintf("bank_%s.xlsx", time.Now().Format("20060102_150405"))
2023-09-04 21:59:27 +08:00
s.toExcel(filename, header, datas, ctx)
os.Remove(filename)
} else {
2023-09-04 22:21:04 +08:00
links := make([]map[string]string, 0)
links = append(links, map[string]string{
2023-09-05 20:39:09 +08:00
"url": "/staff/salary?type=bank&xls=1",
2023-09-04 22:21:04 +08:00
"name": "下载",
})
links = append(links, map[string]string{
2023-09-05 20:39:09 +08:00
"url": "/staff/salary?type=summary",
2023-09-04 22:21:04 +08:00
"name": "汇总",
})
links = append(links, map[string]string{
2023-09-05 20:39:09 +08:00
"url": "/staff/salary?type=agent",
2023-09-04 22:21:04 +08:00
"name": "代理",
})
ctx.HTML(http.StatusOK, "salary.html", gin.H{
"title": month + "工资汇总",
2023-09-04 21:59:27 +08:00
"header": header,
"data": datas,
2023-09-04 22:21:04 +08:00
"link": links,
2023-09-04 21:59:27 +08:00
})
}
}
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))
}
2024-03-01 18:35:03 +08:00
header := []string{"姓名", "身份证号", "入职日期", "转正日期", "基本工资", "出勤工资", "奖金", "社保扣除", "个税扣除", "应出勤天数", "实际出勤天数", "补卡天数", "请假天数", "实发工资", "状态"}
2023-09-04 21:59:27 +08:00
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
}
2024-03-01 17:52:24 +08:00
extra := make(map[string]interface{})
json.Unmarshal([]byte(staff.Extra), &extra)
2023-09-04 21:59:27 +08:00
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))
2024-04-02 12:14:23 +08:00
item = append(item, cast.ToString(staff.AttendSalary))
2023-10-07 14:30:31 +08:00
item = append(item, cast.ToString(staff.AwardSalary))
2023-09-04 21:59:27 +08:00
item = append(item, cast.ToString(staff.SocialInsurence))
item = append(item, cast.ToString(staff.PersonalTax))
2024-01-31 20:49:43 +08:00
item = append(item, cast.ToString(staff.TotalDay))
item = append(item, cast.ToString(staff.RealDay))
2024-03-01 18:04:58 +08:00
item = append(item, cast.ToString(extra["approvalCheckinDay"]))
2023-09-04 21:59:27 +08:00
item = append(item, cast.ToString(staff.Holiday))
2024-04-02 13:54:39 +08:00
item = append(item, cast.ToString(staff.GetRealSalary()))
2024-03-01 18:35:03 +08:00
if staff.TotalDay != cast.ToFloat64(staff.RealDay)+cast.ToFloat64(staff.Holiday) {
item = append(item, "【异常】")
} else {
item = append(item, "")
}
2023-09-04 21:59:27 +08:00
datas = append(datas, item)
summary.BaseSalary += staff.BaseSalary
2024-04-02 12:14:23 +08:00
summary.AttendSalary += staff.AttendSalary
2023-10-07 14:30:31 +08:00
summary.AwardSalary += staff.AwardSalary
2023-09-04 21:59:27 +08:00
summary.SocialInsurence += staff.SocialInsurence
summary.PersonalTax += staff.PersonalTax
summary.Holiday += staff.Holiday
}
2023-09-05 10:49:24 +08:00
summary.Holiday = butil.FloatCut(summary.Holiday)
summary.BaseSalary = butil.FloatCut(summary.BaseSalary)
2024-04-02 12:14:23 +08:00
summary.AttendSalary = butil.FloatCut(summary.AttendSalary)
2023-10-07 14:30:31 +08:00
summary.AwardSalary = butil.FloatCut(summary.AwardSalary)
2023-09-05 10:49:24 +08:00
summary.SocialInsurence = butil.FloatCut(summary.SocialInsurence)
summary.PersonalTax = butil.FloatCut(summary.PersonalTax)
2023-09-04 21:59:27 +08:00
datas = append(datas, []string{"合计", "-", "-", "-",
2024-04-02 12:14:23 +08:00
cast.ToString(summary.BaseSalary), cast.ToString(summary.AttendSalary), cast.ToString(summary.AwardSalary), cast.ToString(summary.SocialInsurence),
2024-03-01 17:52:24 +08:00
cast.ToString(summary.PersonalTax), "-", "-", "-", cast.ToString(summary.Holiday),
2024-04-02 13:54:39 +08:00
cast.ToString(butil.FloatCut(summary.GetRealSalary())), "-"})
2023-09-04 21:59:27 +08:00
if xls != "" {
2023-09-05 16:13:26 +08:00
filename := fmt.Sprintf("summary_%s.xlsx", time.Now().Format("20060102_150405"))
2023-09-04 21:59:27 +08:00
s.toExcel(filename, header, datas, ctx)
os.Remove(filename)
} else {
2023-09-04 22:21:04 +08:00
links := make([]map[string]string, 0)
links = append(links, map[string]string{
2023-09-05 20:39:09 +08:00
"url": "/staff/salary?type=bank",
2023-09-04 22:21:04 +08:00
"name": "银行",
})
links = append(links, map[string]string{
2023-09-05 20:39:09 +08:00
"url": "/staff/salary?type=agent",
2023-09-04 22:21:04 +08:00
"name": "代理",
})
ctx.HTML(http.StatusOK, "salary.html", gin.H{
"title": month + "工资汇总",
2023-09-04 21:59:27 +08:00
"header": header,
"data": datas,
2023-09-04 22:21:04 +08:00
"link": links,
2023-09-04 21:59:27 +08:00
})
}
}
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)
}