Leave.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. userlibs "git.baozhida.cn/ERP_libs/User"
  9. "git.baozhida.cn/ERP_libs/lib"
  10. beego "github.com/beego/beego/v2/server/web"
  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 T_type == Attendance.AttendanceDaysOff {
  113. duration := Attendance.GetRemainingDaysOff(c.User.T_uuid)
  114. if T_duration > duration {
  115. c.Data["json"] = lib.JSONS{Code: 202, Msg: "超过可调休时长!"}
  116. c.ServeJSON()
  117. return
  118. }
  119. }
  120. var_ := Attendance.Attendance{
  121. T_uid: c.User.T_uuid,
  122. T_type: T_type,
  123. T_start_time: startTime,
  124. T_end_time: endTime,
  125. T_duration: T_duration,
  126. T_text: T_text,
  127. T_approver: T_approver,
  128. T_State: Attendance.AttendanceWaitAudit,
  129. }
  130. Id, err := Attendance.Add_Attendance(var_)
  131. if err != nil {
  132. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"}
  133. c.ServeJSON()
  134. return
  135. }
  136. NatsServer.AddUserLogs(c.User.T_uuid, "我的请假", "添加", var_)
  137. NatsServer.AddNews(T_approver, fmt.Sprintf("【请假审批】您有一条%s的请假审批待处理", c.User.T_name), conf.LeaveApprovalNewsUrl)
  138. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
  139. c.ServeJSON()
  140. return
  141. }
  142. // 扣除
  143. func (c *LeaveController) Leave_Deduct() {
  144. T_type, _ := c.GetInt("T_type")
  145. T_duration, _ := c.GetInt("T_duration")
  146. T_month := c.GetString("T_month")
  147. T_text := c.GetString("T_text")
  148. T_uuid := c.GetString("T_uuid")
  149. month, is := lib.MonthStrToTime(T_month)
  150. if !is {
  151. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式不正确"}
  152. c.ServeJSON()
  153. return
  154. }
  155. duration := Attendance.GetRemainingDaysOff(T_uuid)
  156. if T_duration > duration {
  157. c.Data["json"] = lib.JSONS{Code: 202, Msg: "超过加班总时长!"}
  158. c.ServeJSON()
  159. return
  160. }
  161. month = month.AddDate(0, 1, -1)
  162. var_ := Attendance.Attendance{
  163. T_uid: T_uuid,
  164. T_type: T_type + 100,
  165. T_duration: T_duration,
  166. T_text: T_text,
  167. T_approver: c.User.T_uuid,
  168. T_start_time: month,
  169. T_end_time: month,
  170. T_State: Attendance.AttendancePass,
  171. }
  172. Id, err := Attendance.Add_Attendance(var_)
  173. if err != nil {
  174. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"}
  175. c.ServeJSON()
  176. return
  177. }
  178. NatsServer.AddUserLogs(c.User.T_uuid, "我的请假", "扣除", var_)
  179. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
  180. c.ServeJSON()
  181. return
  182. }
  183. func (c *LeaveController) Leave_Update() {
  184. T_id, _ := c.GetInt("T_id")
  185. T_type, _ := c.GetInt("T_type")
  186. T_start_time := c.GetString("T_start_time")
  187. T_end_time := c.GetString("T_end_time")
  188. T_duration, _ := c.GetInt("T_duration")
  189. T_text := c.GetString("T_text")
  190. T_approver := c.GetString("T_approver")
  191. leave, err := Attendance.Read_Attendance_ById(T_id)
  192. if err != nil {
  193. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  194. c.ServeJSON()
  195. return
  196. }
  197. if leave.T_State == Attendance.AttendancePass {
  198. c.Data["json"] = lib.JSONS{Code: 202, Msg: "请假审批已通过,禁止修改!"}
  199. c.ServeJSON()
  200. return
  201. }
  202. if T_type > 0 {
  203. leave.T_type = T_type
  204. }
  205. if len(T_start_time) > 0 {
  206. startTime, is := lib.TimeStrToTime(T_start_time)
  207. if !is {
  208. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式不正确"}
  209. c.ServeJSON()
  210. return
  211. }
  212. leave.T_start_time = startTime
  213. }
  214. if len(T_end_time) > 0 {
  215. endTime, is := lib.TimeStrToTime(T_end_time)
  216. if !is {
  217. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式不正确"}
  218. c.ServeJSON()
  219. return
  220. }
  221. leave.T_end_time = endTime
  222. }
  223. if T_duration > 0 {
  224. leave.T_duration = T_duration
  225. }
  226. if len(T_text) > 0 {
  227. leave.T_text = T_text
  228. }
  229. if len(T_approver) > 0 {
  230. leave.T_approver = T_approver
  231. }
  232. leave.T_State = Attendance.AttendanceWaitAudit
  233. _, err = Attendance.Update_Attendance(leave, "T_type", "T_start_time", "T_end_time", "T_duration", "T_text", "T_approver", "T_State")
  234. if err != nil {
  235. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"}
  236. c.ServeJSON()
  237. return
  238. }
  239. NatsServer.AddUserLogs(c.User.T_uuid, "我的请假", "修改", leave)
  240. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_id}
  241. c.ServeJSON()
  242. return
  243. }
  244. func (c *LeaveController) Leave_Approval() {
  245. T_id, _ := c.GetInt("T_id")
  246. T_State, _ := c.GetInt("T_State")
  247. leave, err := Attendance.Read_Attendance_ById(T_id)
  248. if err != nil {
  249. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  250. c.ServeJSON()
  251. return
  252. }
  253. if leave.T_approver != c.User.T_uuid {
  254. c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权操作!"}
  255. c.ServeJSON()
  256. return
  257. }
  258. if T_State == 1 {
  259. leave.T_State = Attendance.AttendancePass
  260. }
  261. if T_State == 0 {
  262. leave.T_State = Attendance.AttendanceNotPass
  263. }
  264. id, err := Attendance.Update_Attendance(leave, "T_State")
  265. if err != nil {
  266. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"}
  267. c.ServeJSON()
  268. return
  269. }
  270. NatsServer.AddUserLogs(c.User.T_uuid, "我的请假", "审批", leave)
  271. NatsServer.AddNews(leave.T_uid, "【请假申请】您的请假申请已通过", conf.MyLeaveNewsUrl)
  272. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: id}
  273. c.ServeJSON()
  274. return
  275. }
  276. func (c *LeaveController) Leave_Del() {
  277. T_id, _ := c.GetInt("T_id")
  278. leave, err := Attendance.Read_Attendance_ById(T_id)
  279. if err != nil {
  280. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  281. c.ServeJSON()
  282. return
  283. }
  284. if leave.T_State == Attendance.AttendancePass {
  285. c.Data["json"] = lib.JSONS{Code: 202, Msg: "请假审批已通过,禁止删除!"}
  286. c.ServeJSON()
  287. return
  288. }
  289. id, err := Attendance.Delete_Attendance(leave)
  290. if err != nil {
  291. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  292. c.ServeJSON()
  293. return
  294. }
  295. NatsServer.AddUserLogs(c.User.T_uuid, "我的请假", "删除", strconv.Itoa(T_id))
  296. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: id}
  297. c.ServeJSON()
  298. return
  299. }
  300. // 获取剩余调休时长
  301. func (c *LeaveController) Leave_RemainingDaysOff() {
  302. duration := Attendance.GetRemainingDaysOff(c.User.T_uuid)
  303. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: duration}
  304. c.ServeJSON()
  305. return
  306. }
  307. // 计算请假时长
  308. func (c *LeaveController) Leave_Duration() {
  309. T_start_time := c.GetString("T_start_time")
  310. T_end_time := c.GetString("T_end_time")
  311. startTime, is := lib.TimeStrToTime(T_start_time)
  312. if !is {
  313. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式不正确"}
  314. c.ServeJSON()
  315. return
  316. }
  317. endTime, is := lib.TimeStrToTime(T_end_time)
  318. if !is {
  319. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式不正确"}
  320. c.ServeJSON()
  321. return
  322. }
  323. //开始时间的小时和分钟
  324. s_h := startTime.Hour()
  325. s_mm := startTime.Minute()
  326. e_h := endTime.Hour()
  327. e_mm := endTime.Minute()
  328. diff_day := int(endTime.Sub(startTime).Hours() / 24) // 间隔天数
  329. var diff_hours float32 // 间隔小时
  330. var diff_minutes float32 // 间隔分钟
  331. if s_h < 9 {
  332. // 开始小时早于9点,从9点起算
  333. s_h = 9
  334. s_mm = 0
  335. }
  336. if e_h >= 17 && e_mm > 30 {
  337. // 结束时间晚于17:30点,到17:30点止
  338. e_h = 17
  339. e_mm = 30
  340. }
  341. if e_mm < s_mm {
  342. // 结束分钟数<开始分钟数,向小时借
  343. e_mm += 60
  344. e_h--
  345. }
  346. diff_minutes = float32(e_mm - s_mm)
  347. if diff_day > 1 {
  348. // 跨天
  349. diff_hours = 17.5 - float32(s_h) + float32(e_h-9)
  350. // 如果开始时间小于12点 请假小时数-1
  351. // 如果结束时间大于13点,请假小时数-1
  352. if s_h <= 12 {
  353. diff_hours = diff_hours - 1
  354. }
  355. if e_h >= 13 {
  356. diff_hours = diff_hours - 1
  357. }
  358. } else {
  359. // 不跨天
  360. // 开始时间-结束时间跨越午休时间,间隔小时数-1
  361. diff_hours = float32(e_h - s_h)
  362. if s_h <= 12 && e_h >= 13 {
  363. diff_hours = float32(e_h - s_h - 1)
  364. }
  365. }
  366. if diff_day > 1 {
  367. diff_day -= 1
  368. }
  369. diff_day_hours := float32(diff_day) * 7.5
  370. diff_m := int((diff_day_hours+diff_hours)*60 + diff_minutes) // 分钟数量
  371. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: diff_m}
  372. c.ServeJSON()
  373. return
  374. }