Salary.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. package controllers
  2. import (
  3. "ERP_salary/Nats"
  4. "ERP_salary/Nats/NatsServer"
  5. "ERP_salary/conf"
  6. "ERP_salary/logs"
  7. "ERP_salary/models/Account"
  8. "ERP_salary/models/Salary"
  9. "fmt"
  10. natslibs "git.baozhida.cn/ERP_libs/Nats"
  11. userlibs "git.baozhida.cn/ERP_libs/User"
  12. "git.baozhida.cn/ERP_libs/lib"
  13. "github.com/beego/beego/v2/adapter/orm"
  14. beego "github.com/beego/beego/v2/server/web"
  15. "github.com/xuri/excelize/v2"
  16. "math"
  17. "os"
  18. "strings"
  19. "time"
  20. )
  21. type SalaryController struct {
  22. beego.Controller
  23. User userlibs.User
  24. }
  25. func (c *SalaryController) Prepare() {
  26. c.User = *Account.User_r
  27. }
  28. // 管理员请假审批列表 只显示待审核
  29. func (c *SalaryController) Salary_List() {
  30. var r_jsons lib.R_JSONS
  31. page, _ := c.GetInt("page")
  32. if page < 1 {
  33. page = 1
  34. }
  35. page_z, _ := c.GetInt("page_z")
  36. if page_z < 1 {
  37. page_z = conf.Page_size
  38. }
  39. T_date := c.GetString("T_date")
  40. var err error
  41. // 年月 2023-01
  42. if len(T_date) == 0 {
  43. T_date = time.Now().AddDate(0, -1, 0).Format("2006-01")
  44. }
  45. _, err = time.Parse("2006-01", T_date)
  46. if err != nil {
  47. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  48. c.ServeJSON()
  49. return
  50. }
  51. userList, _ := NatsServer.Read_User_List_All()
  52. Account.Read_User_All_Map(userList)
  53. r_jsons.Data, r_jsons.Num = Salary.Read_Salary_List("", T_date, 0, page, page_z)
  54. r_jsons.Page = page
  55. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  56. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  57. c.ServeJSON()
  58. return
  59. }
  60. func (c *SalaryController) Salary_Get() {
  61. T_uuid := c.GetString("T_uuid")
  62. salary, err := Salary.Read_Latest_Salary_ByT_uid(T_uuid)
  63. if err != nil {
  64. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  65. c.ServeJSON()
  66. return
  67. }
  68. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: salary}
  69. c.ServeJSON()
  70. return
  71. }
  72. func (c *SalaryController) Salary_User_Get() {
  73. T_date := c.GetString("T_date")
  74. var err error
  75. // 年月 2023-01
  76. if len(T_date) == 0 {
  77. T_date = time.Now().AddDate(0, -1, 0).Format("2006-01")
  78. }
  79. salary, err := Salary.Read_Salary_ByT_uuid_T_date(c.User.T_uuid, T_date)
  80. if err != nil {
  81. if err.Error() == orm.ErrNoRows.Error() {
  82. c.Data["json"] = lib.JSONS{Code: 202, Msg: "暂无当月数据!"}
  83. c.ServeJSON()
  84. return
  85. }
  86. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  87. c.ServeJSON()
  88. return
  89. }
  90. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Salary.SalaryToSalary_R(salary)}
  91. c.ServeJSON()
  92. return
  93. }
  94. func (c *SalaryController) Salary_User_List() {
  95. var r_jsons lib.R_JSONS
  96. page, _ := c.GetInt("page")
  97. if page < 1 {
  98. page = 1
  99. }
  100. page_z, _ := c.GetInt("page_z")
  101. if page_z < 1 {
  102. page_z = conf.Page_size
  103. }
  104. r_jsons.Data, r_jsons.Num = Salary.Read_Salary_List(c.User.T_uuid, "", 2, page, page_z)
  105. r_jsons.Page = page
  106. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  107. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  108. c.ServeJSON()
  109. return
  110. }
  111. // 添加或修改
  112. func (c *SalaryController) Salary_Post() {
  113. T_uuid := c.GetString("T_uuid")
  114. // 年月 2023-01
  115. T_date := c.GetString("T_date")
  116. if len(T_date) > 0 {
  117. _, err := time.Parse("2006-01", T_date)
  118. if err != nil {
  119. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  120. c.ServeJSON()
  121. return
  122. }
  123. }
  124. T_base, _ := c.GetFloat("T_base")
  125. T_post, _ := c.GetFloat("T_post")
  126. T_seniority, _ := c.GetFloat("T_seniority")
  127. T_Perf, _ := c.GetFloat("T_Perf")
  128. T_Perf_score, _ := c.GetFloat("T_Perf_score")
  129. T_back_payment, _ := c.GetFloat("T_back_payment")
  130. T_attendance, _ := c.GetFloat("T_attendance")
  131. T_cut_payment, _ := c.GetFloat("T_cut_payment")
  132. T_pension_insurance, _ := c.GetFloat("T_pension_insurance")
  133. T_unemployment_insurance, _ := c.GetFloat("T_unemployment_insurance")
  134. T_medical_insurance, _ := c.GetFloat("T_medical_insurance")
  135. T_Large_medical_insurance, _ := c.GetFloat("T_Large_medical_insurance")
  136. T_housing_fund, _ := c.GetFloat("T_housing_fund")
  137. T_tax, _ := c.GetFloat("T_tax")
  138. salary, err := Salary.Read_Salary_ByT_uuid_T_date(T_uuid, T_date)
  139. if err != nil && err.Error() != orm.ErrNoRows.Error() {
  140. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
  141. c.ServeJSON()
  142. return
  143. }
  144. var_ := Salary.Salary{
  145. T_uid: T_uuid,
  146. T_date: T_date,
  147. T_base: float32(T_base),
  148. T_post: float32(T_post),
  149. T_seniority: float32(T_seniority),
  150. T_Perf: float32(T_Perf),
  151. T_Perf_score: float32(T_Perf_score),
  152. T_back_payment: float32(T_back_payment),
  153. T_attendance: float32(T_attendance),
  154. T_cut_payment: float32(T_cut_payment),
  155. T_pension_insurance: float32(T_pension_insurance),
  156. T_unemployment_insurance: float32(T_unemployment_insurance),
  157. T_medical_insurance: float32(T_medical_insurance),
  158. T_Large_medical_insurance: float32(T_Large_medical_insurance),
  159. T_housing_fund: float32(T_housing_fund),
  160. T_tax: float32(T_tax),
  161. }
  162. if salary.Id == 0 {
  163. _, err = Salary.Add_Salary(var_)
  164. if err != nil {
  165. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"}
  166. c.ServeJSON()
  167. return
  168. }
  169. } else {
  170. var_.Id = salary.Id
  171. cols := []string{"T_base", "T_post", "T_seniority", "T_Perf", "T_Perf_score", "T_back_payment", "T_attendance", "T_cut_payment",
  172. "T_pension_insurance", "T_unemployment_insurance", "T_medical_insurance", "T_Large_medical_insurance", "T_housing_fund", "T_tax"}
  173. _, err = Salary.Update_Salary(var_, cols...)
  174. if err != nil {
  175. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败"}
  176. c.ServeJSON()
  177. return
  178. }
  179. }
  180. NatsServer.AddUserLogs(c.User.T_uuid, "薪资管理", "添加", var_)
  181. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: var_.Id}
  182. c.ServeJSON()
  183. return
  184. }
  185. // 发送工资条
  186. func (c *SalaryController) Send_Salary() {
  187. T_id, _ := c.GetInt("T_id")
  188. salary, err := Salary.Read_Salary_ById(T_id)
  189. id, err := Salary.Send_Salary(salary)
  190. if err != nil {
  191. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败"}
  192. c.ServeJSON()
  193. return
  194. }
  195. NatsServer.AddUserLogs(c.User.T_uuid, "薪资管理", "发送工资条", id)
  196. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_id}
  197. c.ServeJSON()
  198. return
  199. }
  200. // 导出工资数据列表
  201. func (c *SalaryController) Salary_Excel() {
  202. // 年月 2023-01
  203. T_date := c.GetString("T_date")
  204. var da time.Time
  205. var err error
  206. if len(T_date) == 0 {
  207. T_date = time.Now().AddDate(0, -1, 0).Format("2006-01")
  208. }
  209. da, err = time.Parse("2006-01", T_date)
  210. if err != nil {
  211. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  212. c.ServeJSON()
  213. return
  214. }
  215. year, month := strings.Split(T_date, "-")[0], strings.Split(T_date, "-")[1]
  216. f := excelize.NewFile() // 设置单元格的值
  217. Style1, _ := f.NewStyle(
  218. &excelize.Style{
  219. Font: &excelize.Font{Bold: true, Size: 20, Family: "宋体"},
  220. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  221. })
  222. Style2, _ := f.NewStyle(
  223. &excelize.Style{
  224. Font: &excelize.Font{Bold: true, Size: 11, Family: "宋体"},
  225. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  226. })
  227. f.MergeCell("Sheet1", "A1", "V1")
  228. f.SetRowStyle("Sheet1", 1, 1, Style1)
  229. f.SetRowHeight("Sheet1", 1, 50)
  230. f.SetCellValue("Sheet1", "A1", fmt.Sprintf("贵州宝智达网络科技有限公司%s年%s月工资表", year, month))
  231. f.MergeCell("Sheet1", "A2", "V2")
  232. f.SetRowHeight("Sheet1", 2, 30)
  233. f.SetRowStyle("Sheet1", 2, 2, Style2)
  234. day := da.AddDate(0, 1, -1).Day()
  235. f.SetCellValue("Sheet1", "A2", fmt.Sprintf("工资期间:%s年%s月01日至%s年%s月%02d日", year, month, year, month, day))
  236. Style3, _ := f.NewStyle(
  237. &excelize.Style{
  238. Font: &excelize.Font{Bold: true, Size: 10, Family: "宋体"},
  239. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  240. Border: []excelize.Border{
  241. {Type: "left", Color: "000000", Style: 1},
  242. {Type: "top", Color: "000000", Style: 1},
  243. {Type: "bottom", Color: "000000", Style: 1},
  244. {Type: "right", Color: "000000", Style: 1},
  245. },
  246. })
  247. f.SetCellStyle("Sheet1", "A3", "V3", Style3)
  248. f.SetRowHeight("Sheet1", 3, 60)
  249. // 这里设置表头
  250. f.SetCellValue("Sheet1", "A3", "序号")
  251. f.SetCellValue("Sheet1", "B3", "姓名")
  252. f.SetCellValue("Sheet1", "C3", "部门")
  253. f.SetCellValue("Sheet1", "D3", "岗位")
  254. f.SetCellValue("Sheet1", "E3", "基本工资")
  255. f.SetCellValue("Sheet1", "F3", "岗位工资")
  256. f.SetCellValue("Sheet1", "G3", "工龄工资")
  257. f.SetCellValue("Sheet1", "H3", "绩效金额")
  258. f.SetCellValue("Sheet1", "I3", "绩效得分")
  259. f.SetCellValue("Sheet1", "J3", "实发绩效")
  260. f.SetCellValue("Sheet1", "K3", "其他补款")
  261. f.SetCellValue("Sheet1", "L3", "考勤扣款")
  262. f.SetCellValue("Sheet1", "M3", "其他扣款")
  263. f.SetCellValue("Sheet1", "N3", "应发合计")
  264. f.SetCellValue("Sheet1", "O3", "养老保险")
  265. f.SetCellValue("Sheet1", "P3", "失业保险")
  266. f.SetCellValue("Sheet1", "Q3", "基本医疗保险")
  267. f.SetCellValue("Sheet1", "R3", "大额医疗保险")
  268. f.SetCellValue("Sheet1", "S3", "公积金")
  269. f.SetCellValue("Sheet1", "T3", "个税扣款")
  270. f.SetCellValue("Sheet1", "U3", "扣款合计")
  271. f.SetCellValue("Sheet1", "V3", "实发合计")
  272. // 设置列宽
  273. f.SetColWidth("Sheet1", "A", "D", 10)
  274. f.SetColWidth("Sheet1", "E", "I", 5)
  275. f.SetColWidth("Sheet1", "J", "J", 10)
  276. f.SetColWidth("Sheet1", "K", "M", 5)
  277. f.SetColWidth("Sheet1", "O", "P", 5)
  278. f.SetColWidth("Sheet1", "Q", "R", 6)
  279. f.SetColWidth("Sheet1", "S", "T", 5)
  280. f.SetColWidth("Sheet1", "U", "V", 12)
  281. userList, _ := NatsServer.Read_User_List_All()
  282. Account.Read_User_All_Map(userList)
  283. salary_List, _ := Salary.Read_Salary_List("", T_date, 0, 0, 9999)
  284. // 循环写入数据
  285. line := 4
  286. var base, post, seniority, actual_Perf, back_payment, attendance, cut_payment, laballot float32
  287. var pension_insurance, unemployment_insurance, medical_insurance, Large_medical_insurance, housing_fund, tax, laborage, total float32
  288. for i, salary := range salary_List {
  289. if len(salary.T_user_post) < 7 {
  290. f.SetRowHeight("Sheet1", line, 22)
  291. }
  292. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), i+1)
  293. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), salary.T_user_name)
  294. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), salary.T_user_dept)
  295. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), salary.T_user_post)
  296. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), salary.T_base)
  297. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), salary.T_post)
  298. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), salary.T_seniority)
  299. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), salary.T_Perf)
  300. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), salary.T_Perf_score)
  301. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), salary.T_actual_Perf)
  302. f.SetCellValue("Sheet1", fmt.Sprintf("K%d", line), salary.T_back_payment)
  303. f.SetCellValue("Sheet1", fmt.Sprintf("L%d", line), salary.T_attendance)
  304. f.SetCellValue("Sheet1", fmt.Sprintf("M%d", line), salary.T_cut_payment)
  305. f.SetCellValue("Sheet1", fmt.Sprintf("N%d", line), salary.T_laballot)
  306. f.SetCellValue("Sheet1", fmt.Sprintf("O%d", line), salary.T_pension_insurance)
  307. f.SetCellValue("Sheet1", fmt.Sprintf("P%d", line), salary.T_unemployment_insurance)
  308. f.SetCellValue("Sheet1", fmt.Sprintf("Q%d", line), salary.T_medical_insurance)
  309. f.SetCellValue("Sheet1", fmt.Sprintf("R%d", line), salary.T_Large_medical_insurance)
  310. f.SetCellValue("Sheet1", fmt.Sprintf("S%d", line), salary.T_housing_fund)
  311. f.SetCellValue("Sheet1", fmt.Sprintf("T%d", line), salary.T_tax)
  312. f.SetCellValue("Sheet1", fmt.Sprintf("U%d", line), salary.T_laborage)
  313. f.SetCellValue("Sheet1", fmt.Sprintf("V%d", line), salary.T_total)
  314. base += salary.T_base
  315. post += salary.T_post
  316. seniority += salary.T_seniority
  317. actual_Perf += salary.T_actual_Perf
  318. back_payment += salary.T_back_payment
  319. attendance += salary.T_attendance
  320. cut_payment += salary.T_cut_payment
  321. laballot += salary.T_laballot
  322. pension_insurance += salary.T_pension_insurance
  323. unemployment_insurance += salary.T_unemployment_insurance
  324. medical_insurance += salary.T_medical_insurance
  325. Large_medical_insurance += salary.T_Large_medical_insurance
  326. housing_fund += salary.T_housing_fund
  327. tax += salary.T_tax
  328. laborage += salary.T_laborage
  329. total += salary.T_total
  330. line++
  331. }
  332. f.SetRowHeight("Sheet1", line, 22)
  333. f.MergeCell("Sheet1", fmt.Sprintf("B%d", line), fmt.Sprintf("D%d", line))
  334. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), "合计")
  335. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), base)
  336. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), post)
  337. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), seniority)
  338. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), actual_Perf)
  339. f.SetCellValue("Sheet1", fmt.Sprintf("K%d", line), back_payment)
  340. f.SetCellValue("Sheet1", fmt.Sprintf("L%d", line), attendance)
  341. f.SetCellValue("Sheet1", fmt.Sprintf("M%d", line), cut_payment)
  342. f.SetCellValue("Sheet1", fmt.Sprintf("N%d", line), laballot)
  343. f.SetCellValue("Sheet1", fmt.Sprintf("O%d", line), pension_insurance)
  344. f.SetCellValue("Sheet1", fmt.Sprintf("P%d", line), unemployment_insurance)
  345. f.SetCellValue("Sheet1", fmt.Sprintf("Q%d", line), medical_insurance)
  346. f.SetCellValue("Sheet1", fmt.Sprintf("R%d", line), Large_medical_insurance)
  347. f.SetCellValue("Sheet1", fmt.Sprintf("S%d", line), housing_fund)
  348. f.SetCellValue("Sheet1", fmt.Sprintf("T%d", line), tax)
  349. f.SetCellValue("Sheet1", fmt.Sprintf("U%d", line), laborage)
  350. f.SetCellValue("Sheet1", fmt.Sprintf("V%d", line), total)
  351. Style4, _ := f.NewStyle(
  352. &excelize.Style{
  353. Font: &excelize.Font{Size: 10, Family: "宋体"},
  354. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  355. Border: []excelize.Border{
  356. {Type: "left", Color: "000000", Style: 1},
  357. {Type: "top", Color: "000000", Style: 1},
  358. {Type: "bottom", Color: "000000", Style: 1},
  359. {Type: "right", Color: "000000", Style: 1},
  360. },
  361. })
  362. f.SetCellStyle("Sheet1", "A4", fmt.Sprintf("V%d", line), Style4)
  363. timeStr := time.Now().Format("20060102150405")
  364. // 保存文件
  365. if err := f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
  366. fmt.Println(err)
  367. }
  368. // 上传 OSS
  369. nats := natslibs.NewNats(Nats.Nats)
  370. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx")
  371. if !is {
  372. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  373. c.ServeJSON()
  374. return
  375. }
  376. //删除目录
  377. err = os.Remove("ofile/" + timeStr + ".xlsx")
  378. if err != nil {
  379. logs.Error(lib.FuncName(), err)
  380. }
  381. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  382. c.ServeJSON()
  383. return
  384. }