Salary.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package Salary
  2. import (
  3. "ERP_salary/logs"
  4. "ERP_salary/models/Account"
  5. "fmt"
  6. "git.baozhida.cn/ERP_libs/lib"
  7. "github.com/beego/beego/v2/adapter/orm"
  8. orm2 "github.com/beego/beego/v2/client/orm"
  9. "time"
  10. )
  11. // 考勤
  12. type Salary struct {
  13. Id int `orm:"column(ID);size(11);auto;pk"`
  14. T_uid string `orm:"index;size(64);null"` // 用户uuid
  15. T_date string `orm:"index;size(64);null"` // 记薪周期,2023-03
  16. T_base float32 `orm:"digits(12);decimals(2)"` // 基础工资
  17. T_post float32 `orm:"digits(12);decimals(2)"` // 岗位工资
  18. T_seniority float32 `orm:"digits(12);decimals(2)"` // 工龄工资
  19. T_Perf float32 `orm:"digits(12);decimals(2)"` // 绩效金额
  20. T_Perf_score float32 `orm:"digits(12);decimals(2)"` // 绩效得分
  21. T_back_payment float32 `orm:"digits(12);decimals(2)"` // 其他补款
  22. T_attendance float32 `orm:"digits(12);decimals(2)"` // 考勤扣款
  23. T_cut_payment float32 `orm:"digits(12);decimals(2)"` // 其他扣款
  24. T_pension_insurance float32 `orm:"digits(12);decimals(2)"` // 需缴养老保险
  25. T_unemployment_insurance float32 `orm:"digits(12);decimals(2)"` // 需缴纳失业保险
  26. T_medical_insurance float32 `orm:"digits(12);decimals(2)"` // 需缴医疗保险
  27. T_Large_medical_insurance float32 `orm:"digits(12);decimals(2)"` // 需缴大额医疗保险
  28. T_housing_fund float32 `orm:"digits(12);decimals(2)"` // 需缴住房公积金
  29. T_tax float32 `orm:"digits(12);decimals(2)"` // 需缴个人所得税
  30. T_State int `orm:"size(200);default(1)"` // 1 未发送 2 已发送
  31. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  32. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  33. }
  34. func (t *Salary) TableName() string {
  35. return "salary"
  36. }
  37. func init() {
  38. //注册模型
  39. orm2.Debug = true
  40. orm.RegisterModel(new(Salary))
  41. }
  42. type Salary_R struct {
  43. Id int
  44. T_uid string // 用户uuid
  45. T_user_name string // 员工名称
  46. T_user_dept string // 部门
  47. T_user_post string // 岗位
  48. T_date string // 记薪周期,2023-03
  49. T_base float32 // 基础工资
  50. T_post float32 // 岗位工资
  51. T_seniority float32 // 工龄工资
  52. T_Perf float32 // 绩效金额
  53. T_Perf_score float32 // 绩效得分
  54. T_actual_Perf float32 // 实发绩效
  55. T_back_payment float32 // 其他补款
  56. T_attendance float32 // 考勤扣款
  57. T_cut_payment float32 // 其他扣款
  58. T_laballot float32 // 应发合计=基本工资+岗位工资+工龄工资+实发绩效+其他补款-考勤扣款-其他扣款
  59. T_pension_insurance float32 // 需缴养老保险
  60. T_unemployment_insurance float32 // 需缴纳失业保险
  61. T_medical_insurance float32 // 需缴医疗保险
  62. T_Large_medical_insurance float32 // 需缴大额医疗保险
  63. T_housing_fund float32 // 需缴住房公积金
  64. T_tax float32 // 需缴个人所得税
  65. T_laborage float32 // 扣款合计=养老保险+失业保险+基本医疗保险+大额医疗保险+公积金+个税扣款
  66. T_total float32 // 实发合计
  67. }
  68. // ---------------- 特殊方法 -------------------
  69. func SalaryToSalary_R(t Salary) (r Salary_R) {
  70. r.Id = t.Id
  71. r.T_uid = t.T_uid
  72. r.T_user_name = Account.Read_User_T_name_Get(t.T_uid)
  73. r.T_user_dept = Account.Read_User_T_dept_Get(t.T_uid)
  74. r.T_user_post = Account.Read_User_T_post_Get(t.T_uid)
  75. r.T_date = t.T_date
  76. r.T_base = t.T_base
  77. r.T_post = t.T_post
  78. r.T_seniority = t.T_seniority
  79. r.T_Perf = t.T_Perf
  80. r.T_Perf_score = t.T_Perf_score
  81. r.T_actual_Perf = t.T_Perf * t.T_Perf_score
  82. r.T_back_payment = t.T_back_payment
  83. r.T_attendance = t.T_attendance
  84. r.T_cut_payment = t.T_cut_payment
  85. // 应发合计=基本工资+岗位工资+工龄工资+实发绩效+其他补款-考勤扣款-其他扣款
  86. r.T_laballot = t.T_base + t.T_post + t.T_seniority + r.T_actual_Perf + t.T_back_payment - t.T_attendance - t.T_cut_payment
  87. r.T_pension_insurance = t.T_pension_insurance
  88. r.T_unemployment_insurance = t.T_unemployment_insurance
  89. r.T_medical_insurance = t.T_medical_insurance
  90. r.T_Large_medical_insurance = t.T_Large_medical_insurance
  91. r.T_housing_fund = t.T_housing_fund
  92. r.T_tax = t.T_tax
  93. // 扣款合计=养老保险+失业保险+基本医疗保险+大额医疗保险+公积金+个税扣款
  94. r.T_laborage = t.T_pension_insurance + t.T_unemployment_insurance + t.T_medical_insurance + t.T_Large_medical_insurance + t.T_housing_fund + t.T_tax
  95. r.T_total = r.T_laballot - r.T_laborage
  96. return r
  97. }
  98. // 获取 ById
  99. func Read_Salary_ById(id int) (r Salary, err error) {
  100. o := orm.NewOrm()
  101. r = Salary{Id: id}
  102. err = o.Read(&r)
  103. if err != nil {
  104. logs.Error(lib.FuncName(), err)
  105. return r, err
  106. }
  107. return r, err
  108. }
  109. func Read_Salary_ByT_uuid_T_date(T_uuid, T_date string) (r Salary, err error) {
  110. o := orm.NewOrm()
  111. r = Salary{T_uid: T_uuid, T_date: T_date}
  112. err = o.Read(&r, "T_uid", "T_date")
  113. if err != nil {
  114. logs.Error(lib.FuncName(), err)
  115. return r, err
  116. }
  117. return r, err
  118. }
  119. // 添加
  120. func Add_Salary(m Salary) (id int64, err error) {
  121. o := orm.NewOrm()
  122. id, err = o.Insert(&m)
  123. if err != nil {
  124. logs.Error(lib.FuncName(), err)
  125. return
  126. }
  127. return
  128. }
  129. // 修改
  130. func Update_Salary(m Salary, cols ...string) (id int64, err error) {
  131. o := orm.NewOrm()
  132. id, err = o.Update(&m, cols...)
  133. if err != nil {
  134. logs.Error(lib.FuncName(), err)
  135. return
  136. }
  137. fmt.Println("Number of records updated in database:", id)
  138. return id, nil
  139. }
  140. // 发送工资条
  141. func Send_Salary(m Salary) (id int64, err error) {
  142. m.T_State = 2
  143. o := orm.NewOrm()
  144. id, err = o.Update(&m, "T_State")
  145. if err != nil {
  146. logs.Error(lib.FuncName(), err)
  147. return
  148. }
  149. fmt.Println("Number of records updated in database:", id)
  150. return id, nil
  151. }
  152. // 获取列表
  153. func Read_Salary_List(T_uid, T_date string, T_state, page, page_z int) (r []Salary_R, cnt int64) {
  154. o := orm.NewOrm()
  155. // 也可以直接使用 Model 结构体作为表名
  156. var map_r []Salary
  157. qs := o.QueryTable(new(Salary))
  158. var offset int64
  159. if page <= 1 {
  160. offset = 0
  161. } else {
  162. offset = int64((page - 1) * page_z)
  163. }
  164. cond := orm.NewCondition()
  165. if len(T_uid) > 0 {
  166. cond = cond.And("T_uid", T_uid)
  167. }
  168. if len(T_date) > 0 {
  169. cond = cond.And("T_date", T_date)
  170. }
  171. if T_state == 2 {
  172. cond = cond.And("T_State", T_state)
  173. }
  174. var err error
  175. if page_z == 9999 {
  176. _, err = qs.SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&map_r)
  177. } else {
  178. _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&map_r)
  179. }
  180. if err != nil {
  181. logs.Error(lib.FuncName(), err)
  182. return
  183. }
  184. cnt, err = qs.SetCond((*orm2.Condition)(cond)).Count()
  185. if err != nil {
  186. logs.Error(lib.FuncName(), err)
  187. return
  188. }
  189. for _, v := range map_r {
  190. r = append(r, SalaryToSalary_R(v))
  191. }
  192. return r, cnt
  193. }
  194. // 获取列表
  195. func Read_Latest_Salary_ByT_uid(T_uid string) (r Salary_R, err error) {
  196. o := orm.NewOrm()
  197. // 也可以直接使用 Model 结构体作为表名
  198. var map_r []Salary
  199. qs := o.QueryTable(new(Salary))
  200. _, err = qs.Limit(1, 0).Filter("T_uid", T_uid).OrderBy("-T_date").All(&map_r)
  201. if err != nil {
  202. logs.Error(lib.FuncName(), err)
  203. return
  204. }
  205. if len(map_r) == 0 {
  206. return
  207. }
  208. return SalaryToSalary_R(map_r[0]), nil
  209. }