2023-09-04 21:59:27 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
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"
|
2024-11-04 23:00:55 +08:00
|
|
|
"sort"
|
2023-09-04 21:59:27 +08:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
StaffSalaryTypeBank = "bank"
|
|
|
|
StaffSalaryTypeSummary = "summary"
|
|
|
|
StaffSalaryTypeAgent = "agent"
|
|
|
|
)
|
|
|
|
|
|
|
|
type StaffSalary struct {
|
|
|
|
}
|
|
|
|
|
2025-03-05 10:11:07 +08:00
|
|
|
func (s *StaffSalary) Agent(cid int64, month string, ctx *gin.Context) {
|
2023-09-04 21:59:27 +08:00
|
|
|
xls := ctx.Query("xls")
|
2025-03-05 10:11:07 +08:00
|
|
|
staffSalarys, err := dao.NewStaffSalaryDao().Query(cid, 0, month)
|
2023-09-04 21:59:27 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(config.ErrDb.New().Append(err))
|
|
|
|
}
|
|
|
|
|
2024-11-04 23:00:55 +08:00
|
|
|
header := []string{"姓名", "身份证号", "电话", "应发工资", "社保扣除", "公积金扣除", "个税扣除", "实发工资"}
|
2023-09-04 21:59:27 +08:00
|
|
|
datas := make([][]string, 0)
|
|
|
|
for _, staff := range staffSalarys {
|
2025-03-04 23:14:09 +08:00
|
|
|
userInfo, err := dao.NewStaffUserDao().Get(staff.UserId)
|
2023-09-04 21:59:27 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("db error :%s", err.Error())
|
|
|
|
continue
|
|
|
|
}
|
2025-03-04 23:14:09 +08:00
|
|
|
if userInfo == nil {
|
2023-09-04 21:59:27 +08:00
|
|
|
continue
|
|
|
|
}
|
2025-03-04 23:14:09 +08:00
|
|
|
userSalary := userInfo.GetSalary()
|
|
|
|
if userSalary.Base == "" {
|
2023-09-04 21:59:27 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
item := make([]string, 0)
|
2024-11-04 23:30:31 +08:00
|
|
|
|
2025-03-04 23:14:09 +08:00
|
|
|
item = append(item, userInfo.Realname)
|
|
|
|
item = append(item, cast.ToString(userInfo.Idno))
|
|
|
|
item = append(item, cast.ToString(userInfo.Phone))
|
2024-11-04 23:00:55 +08:00
|
|
|
item = append(item, cast.ToString(staff.GetShouldSalary()))
|
2025-03-04 23:14:09 +08:00
|
|
|
item = append(item, cast.ToString(staff.SocialDeduct))
|
|
|
|
item = append(item, cast.ToString(staff.HouseDeduct))
|
|
|
|
item = append(item, cast.ToString(staff.PersonalDeduct))
|
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{
|
2025-03-05 10:11:07 +08:00
|
|
|
"url": "/staff/salary?type=agent&xls=1&cid=" + cast.ToString(cid),
|
2023-09-04 22:21:04 +08:00
|
|
|
"name": "下载",
|
|
|
|
})
|
|
|
|
links = append(links, map[string]string{
|
2025-03-05 10:11:07 +08:00
|
|
|
"url": "/staff/salary?type=bank&cid=" + cast.ToString(cid),
|
2023-09-04 22:21:04 +08:00
|
|
|
"name": "银行",
|
|
|
|
})
|
|
|
|
links = append(links, map[string]string{
|
2025-03-05 10:11:07 +08:00
|
|
|
"url": "/staff/salary?type=summary&cid=" + cast.ToString(cid),
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-05 10:11:07 +08:00
|
|
|
func (s *StaffSalary) Bank(cid int64, month string, ctx *gin.Context) {
|
2023-09-04 21:59:27 +08:00
|
|
|
xls := ctx.Query("xls")
|
2025-03-05 10:11:07 +08:00
|
|
|
staffSalarys, err := dao.NewStaffSalaryDao().Query(cid, 0, month)
|
2023-09-04 21:59:27 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(config.ErrDb.New().Append(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
header := []string{"账号", "户名", "金额", "开户行", "开户地", "收款备注"}
|
|
|
|
datas := make([][]string, 0)
|
|
|
|
for _, staff := range staffSalarys {
|
2025-03-04 23:14:09 +08:00
|
|
|
userInfo, err := dao.NewStaffUserDao().Get(staff.UserId)
|
2023-09-04 21:59:27 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("db error :%s", err.Error())
|
|
|
|
continue
|
|
|
|
}
|
2025-03-04 23:14:09 +08:00
|
|
|
if userInfo == nil {
|
2023-09-04 21:59:27 +08:00
|
|
|
continue
|
|
|
|
}
|
2025-03-04 23:14:09 +08:00
|
|
|
userSalary := userInfo.GetSalary()
|
|
|
|
userPayee := userInfo.GetPayee()
|
|
|
|
if userSalary.Base == "" {
|
2023-09-04 21:59:27 +08:00
|
|
|
continue
|
|
|
|
}
|
2025-03-04 23:14:09 +08:00
|
|
|
|
2023-09-04 21:59:27 +08:00
|
|
|
item := make([]string, 0)
|
2025-03-04 23:14:09 +08:00
|
|
|
item = append(item, userPayee.BankCard)
|
|
|
|
item = append(item, userInfo.Realname)
|
2024-04-02 13:54:39 +08:00
|
|
|
item = append(item, cast.ToString(staff.GetRealSalary()))
|
2025-03-04 23:14:09 +08:00
|
|
|
item = append(item, cast.ToString(userPayee.BankName))
|
2023-09-04 21:59:27 +08:00
|
|
|
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{
|
2025-03-05 10:11:07 +08:00
|
|
|
"url": "/staff/salary?type=bank&xls=1&cid=" + cast.ToString(cid),
|
2023-09-04 22:21:04 +08:00
|
|
|
"name": "下载",
|
|
|
|
})
|
|
|
|
links = append(links, map[string]string{
|
2025-03-05 10:11:07 +08:00
|
|
|
"url": "/staff/salary?type=summary&cid=" + cast.ToString(cid),
|
2023-09-04 22:21:04 +08:00
|
|
|
"name": "汇总",
|
|
|
|
})
|
|
|
|
links = append(links, map[string]string{
|
2025-03-05 10:11:07 +08:00
|
|
|
"url": "/staff/salary?type=agent&cid=" + cast.ToString(cid),
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-05 10:11:07 +08:00
|
|
|
func (s *StaffSalary) Summary(cid int64, month string, ctx *gin.Context) {
|
2023-09-04 21:59:27 +08:00
|
|
|
xls := ctx.Query("xls")
|
2025-03-05 10:28:44 +08:00
|
|
|
staffSalarys, err := dao.NewStaffSalaryDao().Query(cid, 0, month)
|
2023-09-04 21:59:27 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(config.ErrDb.New().Append(err))
|
|
|
|
}
|
2024-11-04 23:00:55 +08:00
|
|
|
sort.Sort(model.StaffSalarySort(staffSalarys))
|
2023-09-04 21:59:27 +08:00
|
|
|
|
2025-03-06 00:01:59 +08:00
|
|
|
var header []string
|
|
|
|
if cid == 1002 {
|
|
|
|
header = []string{"姓名", "入职日期", "转正日期", "基本工资", "绩效工资", "出勤收入", "绩效收入", "奖金收入", "其他收入", "应出勤天数", "实际出勤天数", "请假天数", "实发工资", "状态"}
|
|
|
|
} else {
|
|
|
|
header = []string{"姓名", "入职日期", "转正日期", "基本工资", "绩效工资", "出勤收入", "绩效收入", "奖金收入", "其他收入", "社保扣除", "公积金扣除",
|
|
|
|
"个税扣除", "应出勤天数", "实际出勤天数", "请假天数", "实发工资", "状态"}
|
|
|
|
}
|
2023-09-04 21:59:27 +08:00
|
|
|
datas := make([][]string, 0)
|
|
|
|
summary := new(model.StaffSalary)
|
2024-11-05 12:44:09 +08:00
|
|
|
totalCount := 0
|
2025-03-04 23:14:09 +08:00
|
|
|
for _, salary := range staffSalarys {
|
|
|
|
userInfo, err := dao.NewStaffUserDao().Get(salary.UserId)
|
2023-09-04 21:59:27 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("db error :%s", err.Error())
|
|
|
|
continue
|
|
|
|
}
|
2025-03-04 23:14:09 +08:00
|
|
|
if userInfo == nil {
|
2023-09-04 21:59:27 +08:00
|
|
|
continue
|
|
|
|
}
|
2025-03-04 23:14:09 +08:00
|
|
|
userSalary := userInfo.GetSalary()
|
|
|
|
//extra := make(map[string]interface{})
|
|
|
|
//json.Unmarshal([]byte(salary.Extra), &extra)
|
2023-09-04 21:59:27 +08:00
|
|
|
item := make([]string, 0)
|
2025-03-04 23:14:09 +08:00
|
|
|
item = append(item, userInfo.Realname)
|
|
|
|
item = append(item, cast.ToString(userInfo.EntryDate))
|
|
|
|
item = append(item, cast.ToString(userInfo.OfficialDate))
|
|
|
|
item = append(item, userSalary.Base)
|
|
|
|
item = append(item, userSalary.Target)
|
|
|
|
item = append(item, cast.ToString(butil.FloatCut(salary.AttendSalary)))
|
|
|
|
item = append(item, cast.ToString(butil.FloatCut(salary.TargetSalary)))
|
|
|
|
item = append(item, cast.ToString(butil.FloatCut(salary.AwardSalary)))
|
|
|
|
item = append(item, cast.ToString(butil.FloatCut(salary.OtherSalary)))
|
|
|
|
|
2025-03-06 00:01:59 +08:00
|
|
|
if cid != 1002 {
|
|
|
|
item = append(item, cast.ToString(butil.FloatCut(salary.SocialDeduct)))
|
|
|
|
item = append(item, cast.ToString(butil.FloatCut(salary.HouseDeduct)))
|
|
|
|
item = append(item, cast.ToString(butil.FloatCut(salary.PersonalDeduct)))
|
|
|
|
}
|
2025-03-04 23:14:09 +08:00
|
|
|
item = append(item, cast.ToString(salary.ShouldDay))
|
|
|
|
|
|
|
|
item = append(item, cast.ToString(butil.FloatCut(salary.AttendDay)))
|
|
|
|
item = append(item, cast.ToString(butil.FloatCut(salary.HolidayDay)))
|
|
|
|
item = append(item, cast.ToString(salary.GetRealSalary()))
|
|
|
|
if cast.ToFloat64(salary.ShouldDay) != cast.ToFloat64(salary.AttendDay)+cast.ToFloat64(salary.HolidayDay) {
|
2024-03-01 18:35:03 +08:00
|
|
|
item = append(item, "【异常】")
|
|
|
|
} else {
|
|
|
|
item = append(item, "")
|
|
|
|
}
|
2025-03-04 23:14:09 +08:00
|
|
|
|
2024-11-05 12:44:09 +08:00
|
|
|
totalCount += 1
|
2025-03-04 23:14:09 +08:00
|
|
|
|
2023-09-04 21:59:27 +08:00
|
|
|
datas = append(datas, item)
|
2025-03-04 23:14:09 +08:00
|
|
|
summary.Salary += salary.Salary
|
|
|
|
|
|
|
|
summary.TargetSalary += salary.TargetSalary
|
|
|
|
summary.AttendSalary += salary.AttendSalary
|
|
|
|
summary.AwardSalary += salary.AwardSalary
|
|
|
|
summary.OtherSalary += salary.OtherSalary
|
|
|
|
|
|
|
|
summary.SocialDeduct += salary.SocialDeduct
|
|
|
|
summary.HouseDeduct += salary.HouseDeduct
|
|
|
|
summary.PersonalDeduct += salary.PersonalDeduct
|
|
|
|
summary.OtherDeduct += salary.OtherDeduct
|
|
|
|
summary.AttendDay += salary.AttendDay
|
|
|
|
summary.HolidayDay += salary.HolidayDay
|
2023-09-04 21:59:27 +08:00
|
|
|
}
|
2025-03-04 23:14:09 +08:00
|
|
|
summary.HolidayDay = butil.FloatCut(summary.HolidayDay)
|
|
|
|
summary.Salary = butil.FloatCut(summary.Salary)
|
|
|
|
summary.TargetSalary = butil.FloatCut(summary.TargetSalary)
|
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)
|
2025-03-04 23:14:09 +08:00
|
|
|
summary.SocialDeduct = butil.FloatCut(summary.SocialDeduct)
|
|
|
|
summary.HouseDeduct = butil.FloatCut(summary.HouseDeduct)
|
|
|
|
summary.PersonalDeduct = butil.FloatCut(summary.PersonalDeduct)
|
|
|
|
summary.OtherDeduct = butil.FloatCut(summary.OtherDeduct)
|
|
|
|
|
2025-03-06 00:01:59 +08:00
|
|
|
if cid != 1002 {
|
|
|
|
datas = append(datas, []string{"合计", cast.ToString(totalCount), "-",
|
|
|
|
cast.ToString(summary.Salary), "-", cast.ToString(summary.AttendSalary), cast.ToString(summary.TargetSalary), cast.ToString(summary.AwardSalary), cast.ToString(summary.OtherSalary),
|
|
|
|
cast.ToString(summary.SocialDeduct), cast.ToString(summary.HouseDeduct), cast.ToString(summary.PersonalDeduct),
|
|
|
|
"-", "-", cast.ToString(summary.HolidayDay),
|
|
|
|
cast.ToString(butil.FloatCut(summary.GetRealSalary())), "-"})
|
|
|
|
} else {
|
|
|
|
datas = append(datas, []string{"合计", cast.ToString(totalCount), "-",
|
|
|
|
cast.ToString(summary.Salary), "-", cast.ToString(summary.AttendSalary), cast.ToString(summary.TargetSalary), cast.ToString(summary.AwardSalary), cast.ToString(summary.OtherSalary),
|
|
|
|
"-", "-", cast.ToString(summary.HolidayDay),
|
|
|
|
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{
|
2025-03-05 10:11:07 +08:00
|
|
|
"url": "/staff/salary?type=bank&cid=" + cast.ToString(cid),
|
2023-09-04 22:21:04 +08:00
|
|
|
"name": "银行",
|
|
|
|
})
|
|
|
|
links = append(links, map[string]string{
|
2025-03-05 10:11:07 +08:00
|
|
|
"url": "/staff/salary?type=agent&cid=" + cast.ToString(cid),
|
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)
|
|
|
|
}
|