Leave.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. package controllers
  2. import (
  3. "ERP_ams/Nats/NatsServer"
  4. "ERP_ams/conf"
  5. "ERP_ams/models/Account"
  6. "ERP_ams/models/Attendance"
  7. "fmt"
  8. beego "github.com/beego/beego/v2/server/web"
  9. userlibs "gogs.baozhida.cn/zoie/ERP_libs/User"
  10. "gogs.baozhida.cn/zoie/ERP_libs/lib"
  11. "math"
  12. "strconv"
  13. "time"
  14. )
  15. type LeaveController struct {
  16. beego.Controller
  17. User userlibs.User
  18. }
  19. func (c *LeaveController) Prepare() {
  20. c.User = *Account.User_r
  21. }
  22. // 管理员请假审批列表 只显示待审核
  23. func (c *LeaveController) Leave_List() {
  24. var r_jsons lib.R_JSONS
  25. page, _ := c.GetInt("page")
  26. if page < 1 {
  27. page = 1
  28. }
  29. page_z, _ := c.GetInt("page_z")
  30. if page_z < 1 {
  31. page_z = conf.Page_size
  32. }
  33. userList, _ := NatsServer.Read_User_List_All()
  34. Account.Read_User_All_Map(userList)
  35. r_jsons.Data, r_jsons.Num = Attendance.Read_Attendance_List("", c.User.T_uuid, 0, Attendance.AttendanceWaitAudit, page, page_z)
  36. r_jsons.Page = page
  37. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  38. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  39. c.ServeJSON()
  40. return
  41. }
  42. func (c *LeaveController) Leave_User_List() {
  43. var r_jsons lib.R_JSONS
  44. page, _ := c.GetInt("page")
  45. if page < 1 {
  46. page = 1
  47. }
  48. page_z, _ := c.GetInt("page_z")
  49. if page_z < 1 {
  50. page_z = conf.Page_size
  51. }
  52. T_uuid := c.GetString("T_uuid")
  53. if len(T_uuid) == 0 {
  54. T_uuid = c.User.T_uuid
  55. }
  56. userList, _ := NatsServer.Read_User_List_All()
  57. Account.Read_User_All_Map(userList)
  58. r_jsons.Data, r_jsons.Num = Attendance.Read_Attendance_List(T_uuid, "", 0, 0, page, page_z)
  59. r_jsons.Page = page
  60. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  61. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  62. c.ServeJSON()
  63. return
  64. }
  65. // 财务
  66. func (c *LeaveController) Leave_Finance_List() {
  67. var r_jsons lib.R_JSONS
  68. T_uuid := c.GetString("T_uuid")
  69. if len(T_uuid) == 0 {
  70. T_uuid = c.User.T_uuid
  71. }
  72. // 年月 2023-01
  73. T_month := c.GetString("T_month")
  74. if len(T_month) > 0 {
  75. _, err := time.Parse("2006-01", T_month)
  76. if err != nil {
  77. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  78. c.ServeJSON()
  79. return
  80. }
  81. }
  82. r_jsons.Data = Attendance.Read_Attendance_List_For_Finance(T_uuid, T_month)
  83. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  84. c.ServeJSON()
  85. return
  86. }
  87. func (c *LeaveController) Leave_Type_List() {
  88. T_deduct, _ := c.GetInt("T_deduct")
  89. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Attendance.Read_AttendanceType_All(T_deduct)}
  90. c.ServeJSON()
  91. return
  92. }
  93. func (c *LeaveController) Leave_Add() {
  94. T_type, _ := c.GetInt("T_type")
  95. T_start_time := c.GetString("T_start_time")
  96. T_end_time := c.GetString("T_end_time")
  97. T_duration, _ := c.GetInt("T_duration")
  98. T_text := c.GetString("T_text")
  99. T_approver := c.GetString("T_approver")
  100. startTime, is := lib.TimeStrToTime(T_start_time)
  101. if !is {
  102. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式不正确"}
  103. c.ServeJSON()
  104. return
  105. }
  106. endTime, is := lib.TimeStrToTime(T_end_time)
  107. if !is {
  108. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式不正确"}
  109. c.ServeJSON()
  110. return
  111. }
  112. if startTime.After(endTime) {
  113. c.Data["json"] = lib.JSONS{Code: 202, Msg: "开始时间不能大于结束时间"}
  114. c.ServeJSON()
  115. return
  116. }
  117. if T_type == Attendance.AttendanceDaysOff {
  118. duration := Attendance.GetRemainingDaysOff(c.User.T_uuid)
  119. if T_duration > duration {
  120. c.Data["json"] = lib.JSONS{Code: 202, Msg: "超过可调休时长!"}
  121. c.ServeJSON()
  122. return
  123. }
  124. }
  125. var_ := Attendance.Attendance{
  126. T_uid: c.User.T_uuid,
  127. T_type: T_type,
  128. T_start_time: startTime,
  129. T_end_time: endTime,
  130. T_duration: T_duration,
  131. T_text: T_text,
  132. T_approver: T_approver,
  133. T_State: Attendance.AttendanceWaitAudit,
  134. }
  135. Id, err := Attendance.Add_Attendance(var_)
  136. if err != nil {
  137. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"}
  138. c.ServeJSON()
  139. return
  140. }
  141. NatsServer.AddUserLogs(c.User.T_uuid, "我的请假", "添加", var_)
  142. NatsServer.AddNews(T_approver, fmt.Sprintf("【请假审批】您有一条%s的请假审批待处理", c.User.T_name), conf.LeaveApprovalNewsUrl)
  143. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
  144. c.ServeJSON()
  145. return
  146. }
  147. // 扣除
  148. func (c *LeaveController) Leave_Deduct() {
  149. T_type, _ := c.GetInt("T_type")
  150. T_duration, _ := c.GetInt("T_duration")
  151. T_month := c.GetString("T_month")
  152. T_text := c.GetString("T_text")
  153. T_uuid := c.GetString("T_uuid")
  154. month, is := lib.MonthStrToTime(T_month)
  155. if !is {
  156. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式不正确"}
  157. c.ServeJSON()
  158. return
  159. }
  160. duration := Attendance.GetRemainingDaysOff(T_uuid)
  161. if T_duration > duration {
  162. c.Data["json"] = lib.JSONS{Code: 202, Msg: "超过加班总时长!"}
  163. c.ServeJSON()
  164. return
  165. }
  166. month = month.AddDate(0, 1, -1)
  167. var_ := Attendance.Attendance{
  168. T_uid: T_uuid,
  169. T_type: T_type + 100,
  170. T_duration: T_duration,
  171. T_text: T_text,
  172. T_approver: c.User.T_uuid,
  173. T_start_time: month,
  174. T_end_time: month,
  175. T_State: Attendance.AttendancePass,
  176. }
  177. Id, err := Attendance.Add_Attendance(var_)
  178. if err != nil {
  179. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"}
  180. c.ServeJSON()
  181. return
  182. }
  183. NatsServer.AddUserLogs(c.User.T_uuid, "我的请假", "扣除", var_)
  184. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
  185. c.ServeJSON()
  186. return
  187. }
  188. func (c *LeaveController) Leave_Update() {
  189. T_id, _ := c.GetInt("T_id")
  190. T_type, _ := c.GetInt("T_type")
  191. T_start_time := c.GetString("T_start_time")
  192. T_end_time := c.GetString("T_end_time")
  193. T_duration, _ := c.GetInt("T_duration")
  194. T_text := c.GetString("T_text")
  195. T_approver := c.GetString("T_approver")
  196. leave, err := Attendance.Read_Attendance_ById(T_id)
  197. if err != nil {
  198. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  199. c.ServeJSON()
  200. return
  201. }
  202. if leave.T_State == Attendance.AttendancePass {
  203. c.Data["json"] = lib.JSONS{Code: 202, Msg: "请假审批已通过,禁止修改!"}
  204. c.ServeJSON()
  205. return
  206. }
  207. if T_type > 0 {
  208. leave.T_type = T_type
  209. }
  210. if len(T_start_time) > 0 {
  211. startTime, is := lib.TimeStrToTime(T_start_time)
  212. if !is {
  213. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式不正确"}
  214. c.ServeJSON()
  215. return
  216. }
  217. leave.T_start_time = startTime
  218. }
  219. if len(T_end_time) > 0 {
  220. endTime, is := lib.TimeStrToTime(T_end_time)
  221. if !is {
  222. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式不正确"}
  223. c.ServeJSON()
  224. return
  225. }
  226. leave.T_end_time = endTime
  227. }
  228. if T_duration > 0 {
  229. leave.T_duration = T_duration
  230. }
  231. if len(T_text) > 0 {
  232. leave.T_text = T_text
  233. }
  234. if len(T_approver) > 0 {
  235. leave.T_approver = T_approver
  236. }
  237. leave.T_State = Attendance.AttendanceWaitAudit
  238. _, err = Attendance.Update_Attendance(leave, "T_type", "T_start_time", "T_end_time", "T_duration", "T_text", "T_approver", "T_State")
  239. if err != nil {
  240. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"}
  241. c.ServeJSON()
  242. return
  243. }
  244. NatsServer.AddUserLogs(c.User.T_uuid, "我的请假", "修改", leave)
  245. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_id}
  246. c.ServeJSON()
  247. return
  248. }
  249. func (c *LeaveController) Leave_Approval() {
  250. T_id, _ := c.GetInt("T_id")
  251. T_State, _ := c.GetInt("T_State")
  252. leave, err := Attendance.Read_Attendance_ById(T_id)
  253. if err != nil {
  254. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  255. c.ServeJSON()
  256. return
  257. }
  258. if leave.T_approver != c.User.T_uuid {
  259. c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权操作!"}
  260. c.ServeJSON()
  261. return
  262. }
  263. T_State_str := ""
  264. if T_State == 1 {
  265. leave.T_State = Attendance.AttendancePass
  266. T_State_str = "已通过"
  267. }
  268. if T_State == 0 {
  269. leave.T_State = Attendance.AttendanceNotPass
  270. T_State_str = "未通过"
  271. }
  272. id, err := Attendance.Update_Attendance(leave, "T_State")
  273. if err != nil {
  274. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"}
  275. c.ServeJSON()
  276. return
  277. }
  278. NatsServer.AddUserLogs(c.User.T_uuid, "我的请假", "审批", leave)
  279. NatsServer.AddNews(leave.T_uid, "【请假申请】您的请假申请审批"+T_State_str, conf.MyLeaveNewsUrl)
  280. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: id}
  281. c.ServeJSON()
  282. return
  283. }
  284. func (c *LeaveController) Leave_Del() {
  285. T_id, _ := c.GetInt("T_id")
  286. leave, err := Attendance.Read_Attendance_ById(T_id)
  287. if err != nil {
  288. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  289. c.ServeJSON()
  290. return
  291. }
  292. if leave.T_State == Attendance.AttendancePass {
  293. c.Data["json"] = lib.JSONS{Code: 202, Msg: "请假审批已通过,禁止删除!"}
  294. c.ServeJSON()
  295. return
  296. }
  297. id, err := Attendance.Delete_Attendance(leave)
  298. if err != nil {
  299. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  300. c.ServeJSON()
  301. return
  302. }
  303. NatsServer.AddUserLogs(c.User.T_uuid, "我的请假", "删除", strconv.Itoa(T_id))
  304. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: id}
  305. c.ServeJSON()
  306. return
  307. }
  308. // 获取剩余调休时长
  309. func (c *LeaveController) Leave_RemainingDaysOff() {
  310. duration := Attendance.GetRemainingDaysOff(c.User.T_uuid)
  311. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: duration}
  312. c.ServeJSON()
  313. return
  314. }
  315. // 计算请假时长
  316. func (c *LeaveController) Leave_Duration() {
  317. T_start_time := c.GetString("T_start_time")
  318. T_end_time := c.GetString("T_end_time")
  319. startTime, is := lib.TimeStrToTime(T_start_time)
  320. if !is {
  321. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式不正确"}
  322. c.ServeJSON()
  323. return
  324. }
  325. endTime, is := lib.TimeStrToTime(T_end_time)
  326. if !is {
  327. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式不正确"}
  328. c.ServeJSON()
  329. return
  330. }
  331. endTime2 := time.Date(endTime.Year(), endTime.Month(), endTime.Day()-1, 17, 30, 0, 0, time.Local)
  332. endTime3 := time.Date(endTime.Year(), endTime.Month(), endTime.Day(), 9, 0, 0, 0, time.Local)
  333. endTime4 := time.Date(endTime.Year(), endTime.Month(), endTime.Day(), 17, 30, 0, 0, time.Local)
  334. endTime5 := time.Date(endTime.Year(), endTime.Month(), endTime.Day()+1, 9, 0, 0, 0, time.Local)
  335. if endTime.After(endTime2) && endTime.Before(endTime3) {
  336. endTime = endTime2
  337. }
  338. if endTime.After(endTime4) && endTime.Before(endTime5) {
  339. endTime = endTime4
  340. }
  341. startTime2 := time.Date(startTime.Year(), startTime.Month(), startTime.Day()-1, 17, 30, 0, 0, time.Local)
  342. startTime3 := time.Date(startTime.Year(), startTime.Month(), startTime.Day(), 9, 0, 0, 0, time.Local)
  343. startTime4 := time.Date(startTime.Year(), startTime.Month(), startTime.Day(), 17, 30, 0, 0, time.Local)
  344. startTime5 := time.Date(startTime.Year(), startTime.Month(), startTime.Day()+1, 9, 0, 0, 0, time.Local)
  345. if startTime.After(startTime2) && startTime.Before(startTime3) {
  346. startTime = startTime3
  347. }
  348. if startTime.After(startTime4) && startTime.Before(startTime5) {
  349. startTime = startTime5
  350. }
  351. if startTime.After(endTime) {
  352. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: 0}
  353. c.ServeJSON()
  354. return
  355. }
  356. //开始时间的小时和分钟
  357. s_h := startTime.Hour()
  358. s_mm := startTime.Minute()
  359. e_h := endTime.Hour()
  360. e_mm := endTime.Minute()
  361. diff_day := int(endTime.Sub(startTime).Hours() / 24) // 间隔天数
  362. var diff_hours float32 // 间隔小时
  363. var diff_minutes float32 // 间隔分钟
  364. if e_mm < s_mm {
  365. // 结束分钟数<开始分钟数,向小时借
  366. e_mm += 60
  367. e_h--
  368. }
  369. diff_minutes = float32(e_mm - s_mm)
  370. if diff_day > 1 {
  371. // 跨天
  372. diff_hours = 17.5 - float32(s_h) + float32(e_h-9)
  373. // 如果开始时间小于12点 请假小时数-1
  374. // 如果结束时间大于13点,请假小时数-1
  375. if s_h <= 12 {
  376. diff_hours = diff_hours - 1
  377. }
  378. if e_h >= 13 {
  379. diff_hours = diff_hours - 1
  380. }
  381. } else {
  382. // 不跨天
  383. // 开始时间-结束时间跨越午休时间,间隔小时数-1
  384. diff_hours = float32(e_h - s_h)
  385. if s_h <= 12 && e_h >= 13 {
  386. diff_hours = float32(e_h - s_h - 1)
  387. }
  388. }
  389. if diff_day > 1 {
  390. diff_day -= 1
  391. }
  392. diff_day_hours := float32(diff_day) * 7.5
  393. diff_m := int((diff_day_hours+diff_hours)*60 + diff_minutes) // 分钟数量
  394. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: diff_m}
  395. c.ServeJSON()
  396. return
  397. }