Salary.go 8.4 KB

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