new sallary3

This commit is contained in:
jiangyong27 2024-01-31 22:54:06 +08:00
parent 55384e1ce3
commit 4bf3fdd053
6 changed files with 84 additions and 56 deletions

View File

@ -19,13 +19,3 @@ func main() {
panic(err) panic(err)
} }
} }
func main22() {
config.LoadServerConfig()
global.InitGlobal()
//cfg := config.GetConfig()
//new(worker.Checkin).SyncCheckinDay("2024-01-17")
//new(worker.Approval).Sync("202401", model.ApprovalTypeRefund)
new(worker.Staff).SyncStaffSalary("202401")
}

View File

@ -65,7 +65,7 @@ func (d *ApprovalCheckinDao) GetBySpNo(spNo string) (*model.ApprovalCheckin, err
return &u, nil return &u, nil
} }
func (d *ApprovalCheckinDao) GetByUsername(username, month string) ([]*model.ApprovalCheckin, error) { func (d *ApprovalCheckinDao) GetByUsernameMonth(username, month string) ([]*model.ApprovalCheckin, error) {
var u []*model.ApprovalCheckin var u []*model.ApprovalCheckin
tx := GetDB().Table(d.TableName()) tx := GetDB().Table(d.TableName())
tx = tx.Where("month = ?", month) tx = tx.Where("month = ?", month)
@ -76,3 +76,21 @@ func (d *ApprovalCheckinDao) GetByUsername(username, month string) ([]*model.App
} }
return u, nil return u, nil
} }
func (d *ApprovalCheckinDao) GetByUsernameDay(username, day string) (*model.ApprovalCheckin, error) {
var u model.ApprovalCheckin
tx := GetDB().Table(d.TableName())
tx = tx.Where("checkin_date = ?", day)
tx = tx.Where("username = ?", username)
res := tx.First(&u)
if res.Error == gorm.ErrRecordNotFound {
return nil, nil
}
if res.Error != nil {
return nil, res.Error
}
return &u, nil
}

View File

@ -80,7 +80,7 @@ func (d *CheckinDao) CountUsername(month string) ([]*model.UsernameCount, error)
return userCount, nil return userCount, nil
} }
func (d *CheckinDao) GetUsernameCount(username, month string, filterException bool) (int64, error) { func (d *CheckinDao) Query(username, month string, filterException bool) ([]*model.Checkin, error) {
tx := GetDB().Table(d.TableName()) tx := GetDB().Table(d.TableName())
tx = tx.Where("month = ?", month) tx = tx.Where("month = ?", month)
tx = tx.Where("username = ?", username) tx = tx.Where("username = ?", username)
@ -88,11 +88,10 @@ func (d *CheckinDao) GetUsernameCount(username, month string, filterException bo
tx = tx.Where("exception = ''") tx = tx.Where("exception = ''")
} }
var count int64 var u []*model.Checkin
tx.Select("COUNT(1) AS count").Pluck("count", &count) tx = tx.Find(&u)
if tx.Error != nil { if tx.Error != nil {
return 0, tx.Error return nil, tx.Error
} }
return count, nil return u, nil
} }

View File

@ -9,6 +9,7 @@ var (
CheckinOffdutyMoney = "checkin.offduty.money" //下班打卡金额 CheckinOffdutyMoney = "checkin.offduty.money" //下班打卡金额
StaffSalaryExpDiscount = "staff.salary.exp.discount" //试用期薪资折扣 StaffSalaryExpDiscount = "staff.salary.exp.discount" //试用期薪资折扣
StaffSalarySocialInsurence = "staff.salary.social.insurence" //社保扣除金额 StaffSalarySocialInsurence = "staff.salary.social.insurence" //社保扣除金额
StaffSalaryPerDay = "staff.salary.per.day" //按天计算的工资
) )
var ( var (
UserConfigStatusNormal = 1 UserConfigStatusNormal = 1

View File

@ -90,6 +90,7 @@ func (c *Checkin) saveToDB(user *qyweixin.UserCheckIn) error {
checkin.StartTime = user.StartTime checkin.StartTime = user.StartTime
checkin.EndTime = user.EndTime checkin.EndTime = user.EndTime
checkin.Month = user.Month checkin.Month = user.Month
checkin.Rawdata = user.Rawdata
if isNew { if isNew {
_, err = dao.NewCheckinDao().Create(checkin) _, err = dao.NewCheckinDao().Create(checkin)

View File

@ -100,47 +100,34 @@ func (s *Staff) SyncStaffSalary(month string) {
month = time.Now().AddDate(0, -1, 0).Format("2006-01") month = time.Now().AddDate(0, -1, 0).Format("2006-01")
} }
month = strings.ReplaceAll(month, "-", "") month = strings.ReplaceAll(month, "-", "")
totalDays := s.getTotalWordDay(month)
monthTime, _ := time.ParseInLocation("200601", month, time.Local)
startDate := cast.ToInt(monthTime.Format("20060102"))
endDate := cast.ToInt(monthTime.AddDate(0, 1, -1).Format("20060102"))
salaryDao := dao.NewStaffSalaryDao()
staffs, err := dao.NewStaffInfoDao().Query(model.StaffInfoStatusNormal) staffs, err := dao.NewStaffInfoDao().Query(model.StaffInfoStatusNormal)
if err != nil { if err != nil {
log.Errorf("query staff db error :%s", err.Error()) log.Errorf("query staff db error :%s", err.Error())
return return
} }
monthTime, _ := time.ParseInLocation("200601", month, time.Local)
startDate := cast.ToInt(monthTime.Format("20060102"))
endDate := cast.ToInt(monthTime.AddDate(0, 1, -1).Format("20060102"))
salaryDao := dao.NewStaffSalaryDao()
// 计算应出勤天数,选择最大的一个作为应出勤天数
userCounts, err := dao.NewCheckinDao().CountUsername(month)
if err != nil {
log.Errorf("db error :%s", err.Error())
return
}
totalDays := int64(0)
for _, uc := range userCounts {
if uc.Count > totalDays {
totalDays = uc.Count
}
}
for _, staff := range staffs { for _, staff := range staffs {
salary, err := salaryDao.GetBy(staff.Username, month) salary, err := salaryDao.GetBy(staff.Username, month)
if err != nil { if err != nil {
log.Errorf("db error :%s", err.Error()) log.Errorf("db error :%s", err.Error())
continue continue
} }
entryTime, _ := time.ParseInLocation("2006-01-02", staff.EntryDate, time.Local)
officalTime, _ := time.ParseInLocation("2006-01-02", staff.OfficialDate, time.Local)
isEntryMonth := goutil.If(cast.ToInt(entryTime.Format("200601")) == cast.ToInt(month), true, false)
isOfficialMonth := goutil.If(cast.ToInt(officalTime.Format("200601")) == cast.ToInt(month), true, false)
if salary == nil { if salary == nil {
salary = new(model.StaffSalary) salary = new(model.StaffSalary)
salary.Month = month salary.Month = month
salary.Username = staff.Username salary.Username = staff.Username
} }
entryTime, _ := time.ParseInLocation("2006-01-02", staff.EntryDate, time.Local)
officalTime, _ := time.ParseInLocation("2006-01-02", staff.OfficialDate, time.Local)
isEntryMonth := goutil.If(cast.ToInt(entryTime.Format("200601")) == cast.ToInt(month), true, false)
isOfficialMonth := goutil.If(cast.ToInt(officalTime.Format("200601")) == cast.ToInt(month), true, false)
config, err := dao.NewUserConfigDao().GetByUsername(staff.Username) config, err := dao.NewUserConfigDao().GetByUsername(staff.Username)
if err != nil { if err != nil {
log.Errorf("db error :%s", err.Error()) log.Errorf("db error :%s", err.Error())
@ -162,18 +149,7 @@ func (s *Staff) SyncStaffSalary(month string) {
socialInsurence = 0 socialInsurence = 0
} }
approveCheckins, err := dao.NewApprovalCheckinDao().GetByUsername(staff.Username, month) // 休假申请
if err != nil {
log.Errorf("db error :%s", err.Error())
continue
}
checkinCount, err := dao.NewCheckinDao().GetUsernameCount(staff.Username, month, true)
if err != nil {
log.Errorf("db error :%s", err.Error())
continue
}
approveVacations, err := dao.NewApprovalVacationDao().GetByUsername(staff.Username, month) approveVacations, err := dao.NewApprovalVacationDao().GetByUsername(staff.Username, month)
if err != nil { if err != nil {
log.Errorf("db error :%s", err.Error()) log.Errorf("db error :%s", err.Error())
@ -186,11 +162,27 @@ func (s *Staff) SyncStaffSalary(month string) {
surplusHoliday += goutil.If(vac.VacationDuration <= 1, 1-vac.VacationDuration, 0) surplusHoliday += goutil.If(vac.VacationDuration <= 1, 1-vac.VacationDuration, 0)
} }
extra := make(map[string]interface{}) // 打卡正常天数+补卡天数 为正常出勤天数
realWorkdays := 0
userCheckins, err := dao.NewCheckinDao().Query(staff.Username, month, false)
if err != nil {
log.Errorf("db error :%s", err.Error())
continue
}
for _, checkin := range userCheckins {
if checkin.Exception == "" {
realWorkdays += 1
continue
}
approvalCheckin, _ := dao.NewApprovalCheckinDao().GetByUsernameDay(staff.Username, checkin.Day)
if approvalCheckin != nil {
realWorkdays += 1
}
}
extra := make(map[string]interface{})
salary.BaseSalary = cast.ToFloat64(staff.Salary) salary.BaseSalary = cast.ToFloat64(staff.Salary)
salary.Holiday = holiday salary.Holiday = holiday
entryDate := cast.ToInt(strings.ReplaceAll(staff.EntryDate, "-", "")) entryDate := cast.ToInt(strings.ReplaceAll(staff.EntryDate, "-", ""))
officialDate := cast.ToInt(strings.ReplaceAll(staff.OfficialDate, "-", "")) officialDate := cast.ToInt(strings.ReplaceAll(staff.OfficialDate, "-", ""))
@ -199,8 +191,12 @@ func (s *Staff) SyncStaffSalary(month string) {
discount = discount*float64(officialDate-startDate)/totalMonthDay + 1*float64(endDate-officialDate+1)/totalMonthDay discount = discount*float64(officialDate-startDate)/totalMonthDay + 1*float64(endDate-officialDate+1)/totalMonthDay
} }
realWorkDays := float64(checkinCount+int64(len(approveCheckins))) + surplusHoliday realWorkDays := float64(realWorkdays) + surplusHoliday
realSalary := salary.BaseSalary * (realWorkDays / float64(totalDays)) * discount realSalary := salary.BaseSalary * (realWorkDays / float64(totalDays)) * discount
staffSalaryPerDay := cast.ToFloat64(config.Get(model.StaffSalaryPerDay))
if staffSalaryPerDay > 0 {
realSalary = staffSalaryPerDay * realWorkDays
}
extra["discount"] = discount extra["discount"] = discount
extra["socialInsurence"] = socialInsurence extra["socialInsurence"] = socialInsurence
@ -234,3 +230,26 @@ func (s *Staff) SyncStaffSalary(month string) {
} }
} }
func (s *Staff) getTotalWordDay(month string) int64 {
// 最多人数的应出勤天数 为真正的出勤天数
userCounts, err := dao.NewCheckinDao().CountUsername(month)
if err != nil {
log.Errorf("db error :%s", err.Error())
return 0
}
mp := make(map[int64]int)
totalDays := int64(0)
for _, uc := range userCounts {
mp[uc.Count] += 1
}
minCount := 0
for k, v := range mp {
if v > minCount {
minCount = v
totalDays = k
}
}
return totalDays
}