PayrollComputation.cs 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. using Google.Protobuf.WellKnownTypes;
  2. using NPOI.OpenXmlFormats.Spreadsheet;
  3. using OASystem.API.OAMethodLib.QiYeWeChatAPI;
  4. using OASystem.Domain;
  5. using OASystem.Domain.Entities.PersonnelModule;
  6. using OASystem.Domain.ViewModels.PersonnelModule;
  7. using OASystem.Domain.ViewModels.QiYeWeChat;
  8. using OASystem.Infrastructure.Repositories.Groups;
  9. using System;
  10. using System.Linq.Expressions;
  11. using TencentCloud.Ocr.V20181119.Models;
  12. namespace OASystem.API.OAMethodLib
  13. {
  14. /// <summary>
  15. /// 工资计算
  16. /// </summary>
  17. public static class PayrollComputation
  18. {
  19. private static Result _result = new Result();
  20. private static readonly IQiYeWeChatApiService _qiYeWeChatApiService = AutofacIocManager.Instance.GetService<IQiYeWeChatApiService>();
  21. private static readonly UsersRepository _usersRep = AutofacIocManager.Instance.GetService<UsersRepository>();
  22. private static readonly IMapper _mapper = AutofacIocManager.Instance.GetService<IMapper>();
  23. private static readonly decimal _chengDuMinimumWage = 2100.00M;
  24. /// <summary>
  25. /// 计算工资
  26. /// </summary>
  27. /// <param name="pm_WageSheetDattaSources"></param>
  28. /// <param name="userNames"></param>
  29. /// <param name="thisYearMonth"></param>
  30. /// <param name="startDt"></param>
  31. /// <param name="endDt"></param>
  32. /// <returns></returns>
  33. public static async Task<Result> SalaryCalculatorAsync(
  34. List<Pm_WageSheet> pm_WageSheetDattaSources, List<UserNameView> userNames,int userId, string thisYearMonth, DateTime startDt, DateTime endDt)
  35. {
  36. if (pm_WageSheetDattaSources.Count <= 0)
  37. {
  38. _result.Msg = "计算工资传入数据为空!";
  39. return _result;
  40. }
  41. if (userNames.Count <= 0 )
  42. {
  43. var nameData = await _usersRep.GetUserNameList(1);
  44. userNames = nameData.Data;
  45. }
  46. //获取所有打卡数据
  47. CheckInView checkIn = await _qiYeWeChatApiService.GetCheckin_MonthDataAsync(startDt, endDt); //时间段内所有 打卡数据
  48. if (checkIn.errcode != 0)
  49. {
  50. _result.Msg = "【企业微信】【打卡】【获取所有月打卡数据】【Msg】" + checkIn.errmsg;
  51. return _result;
  52. }
  53. List<Data> checkInDatas = checkIn.datas;
  54. //获取 请假类型 Sp_Detail.template_id
  55. string leave_template_id = "C4NzTJCh1onCUK915rRkvy7Fh5Vqz4YbiEV9jrBY1";
  56. List<VacationLeaveTypeView> vacationLeaveTypes = await GetVacationLeaveTypes(leave_template_id);
  57. if (vacationLeaveTypes.Count <= 0)
  58. {
  59. _result.Msg = startDt + " - " + endDt + "请假 类型数据 获取失败!";
  60. return _result;
  61. }
  62. _result.Msg = string.Empty;
  63. foreach (var pm_wsInfo in pm_WageSheetDattaSources)
  64. {
  65. string itemName = userNames.Where(it => it.Id == pm_wsInfo.UserId).FirstOrDefault().CnName;
  66. //补贴 金额
  67. decimal meal_subsidy = 0.00M; // 午餐(午餐10元/天) 补贴 * 计算方式:单日上午请假时长(小时)大于或者等于三小时 没有餐补
  68. //事假 病假 总金额
  69. decimal personalLeaveTotal = 0.00M, // 事假 日薪 *计算方式:日平均工资 = 月工资/当月应出勤天数。
  70. sickLeaveTotal = 0.00M; // 病假 日薪 *计算方式:日平均工资 = 成都市最低工资标准的80%/当月应出勤天数。 短期病假=当月15天内
  71. //扣款金额
  72. decimal beLate_deduction = 0.00M, // 迟到 扣款金额 *计算方式:
  73. // 一个自然月内,不足 10 分钟的迟到/早退,不超过 2 次的部分,不做处罚;3 次及以上,按50元 / 次处罚;
  74. // 超过 10 分钟(含 10 分钟),不足 60 分钟的迟到/早退,按 50 元/次处罚;
  75. // 超过 60 分钟(含 60 分钟),不足 3 小时的迟到/早退,且无请假者,按旷工半日处理;超过3 小时的迟到 / 早退,且无请假者,按旷工一日处理。
  76. early_deduction = 0.00M, // 早退 扣款金额
  77. absenteeism_deduction = 0.00M, // 旷工 扣款金额 *计算方式:旷工扣发当日工资
  78. unprinted_deduction = 0.00M, // 未打卡 扣款金额 *计算方式:
  79. // 试用期员工每月有 2 次 补卡机会,超过 2 次不足 5 次的部分,按 10 元 / 次处罚,5 次及以上的漏卡,按 50 元 / 次处罚;
  80. // 正式员工每月 3 次以内的补卡,按 10 元 / 次处罚,3 次及以上的漏卡,按 50 元 / 次处罚。
  81. sickLeave_deduction = 0.00M,
  82. other_deduction = 0.00M; // 其他 扣款金额
  83. decimal meal_deduction = 0.00M; // 餐补 扣款金额
  84. decimal reissuecard_deduction = 0.00M; // 补卡 扣款金额
  85. //打卡数据
  86. Data? checkInData = checkInDatas.Where(it => it.base_info.name == itemName).FirstOrDefault();
  87. if (checkInData == null) { continue; }
  88. string acctid = checkInData.base_info.acctid; //用户Id
  89. //当月总计数据
  90. Summary_Info? summary_Info = checkInData.summary_info;
  91. if (summary_Info == null) { continue; }
  92. int work_days = summary_Info.work_days; //应出勤天数
  93. int regular_days = summary_Info.regular_days; //正常出勤天数
  94. meal_subsidy = work_days * 10; //应发放餐补
  95. #region 计算日工资 正常日薪 事假日薪 病假日薪
  96. //月 - 应发工资
  97. decimal amountPayable = pm_wsInfo.Basic + pm_wsInfo.Floats + pm_wsInfo.PostAllowance + pm_wsInfo.InformationSecurityFee + pm_wsInfo.OtherSubsidies;
  98. // 日薪 = *计算方式:日平均工资 = 月工资/当月应出勤天数。
  99. decimal dailyWage = ConvertToDecimal(amountPayable / work_days);
  100. // 病假日薪 *计算方式:日平均工资 = 成都市最低工资标准的80%/当月应出勤天数。 短期病假=当月15天内
  101. decimal sickLeave_dailywage = ConvertToDecimal(_chengDuMinimumWage / work_days);
  102. //病假 一天扣款
  103. sickLeave_deduction = dailyWage - sickLeave_dailywage;
  104. #endregion
  105. int annualLeaveNum = 0, //年假
  106. personalLeaveNum = 0, //事假
  107. sickLeaveNum = 0, //病假
  108. lieuLeaveNum = 0, //调休假
  109. marriageLeaveNum = 0, //婚嫁
  110. maternityLeaveNum = 0, //产假
  111. paternityLeaveNum = 0, //陪产假
  112. funeralLeaveNum = 0, //丧假
  113. reissueCardNum = 0, //补卡 次数
  114. evectionNum = 0, //出差 次数
  115. outIngNum = 0, //外出 次数
  116. outWorkNum = 0, //外勤 次数
  117. otherLeaveNum = 0; //其他
  118. #region 假勤 处理 1-请假;2-补卡;3-出差;4-外出;100-外勤;
  119. List<Sp_Item>? sp_items = checkInData.sp_items.Where(it => it.count != 0).ToList();
  120. if (sp_items != null && sp_items.Count > 0)
  121. {
  122. annualLeaveNum = Fallibilitydispose(sp_items, 1, "年假");
  123. personalLeaveNum = Fallibilitydispose(sp_items, 1, "事假");
  124. sickLeaveNum = Fallibilitydispose(sp_items, 1, "病假");
  125. lieuLeaveNum = Fallibilitydispose(sp_items, 1, "调休假");
  126. marriageLeaveNum = Fallibilitydispose(sp_items, 1, "婚嫁");
  127. maternityLeaveNum = Fallibilitydispose(sp_items, 1, "产假");
  128. paternityLeaveNum = Fallibilitydispose(sp_items, 1, "陪产假");
  129. funeralLeaveNum = Fallibilitydispose(sp_items, 1, "丧假");
  130. otherLeaveNum = Fallibilitydispose(sp_items, 1, "其他");
  131. reissueCardNum = Fallibilitydispose(sp_items, 2, "补卡次数");
  132. evectionNum = Fallibilitydispose(sp_items, 3, "出差");
  133. outIngNum = Fallibilitydispose(sp_items, 4, "外出");
  134. outWorkNum = Fallibilitydispose(sp_items, 3, "外勤");
  135. }
  136. #region 请假类型金额/餐补 处理
  137. List<Sp_Detail> sp_leave_details = new List<Sp_Detail>();
  138. sp_leave_details = await _qiYeWeChatApiService.GetApprovalDetailsAsync(startDt, endDt, acctid, 2 , 1); //时间段内所有 已同意的 请假 审批数据
  139. if (sp_leave_details.Count <= 0)
  140. {
  141. _result.Msg += startDt + " - " + endDt +" "+itemName + " 请假 审批数据获取未获取到!\r\n";
  142. //continue;
  143. }
  144. List<Ex_Items> ex_Items = new List<Ex_Items>();//假勤 And 打卡备注集合
  145. Ex_Items ex_Items_jq = new Ex_Items() { Type = "假勤" }; //假勤
  146. List<Ex_Item> ex_ItemInfos = new List<Ex_Item>();
  147. foreach (Sp_Detail sp_item in sp_leave_details)
  148. {
  149. Apply_data? apply_data = sp_item.apply_data;
  150. if (apply_data != null)
  151. {
  152. List<ContentsItem> contents = apply_data.contents;
  153. ContentsItem content_Vacation = contents.Where(it => it.control== "Vacation").FirstOrDefault(); //请假类型
  154. ContentsItem content_Textarea = contents.Where(it => it.control == "Textarea").FirstOrDefault(); //多行文本
  155. if (content_Vacation != null)
  156. {
  157. Vacation vacation = content_Vacation.value.vacation;
  158. Attendance attendance = vacation.attendance; //假勤组件
  159. Selector selector = vacation.selector; //请假类型
  160. List<OptionsItem> optionsItems = selector.options; //key 请假类型 id
  161. List<TitleItem> value = optionsItems[0].value; // value 文本描述值
  162. int leaveType = int.Parse(optionsItems[0].key); //key 请假子类型 id
  163. Date_range date_Range = attendance.date_range;
  164. //筛选 不在工作日内的假勤申请
  165. if (startDt >= date_Range.new_begin_dt && date_Range.new_end_dt <= endDt)
  166. {
  167. continue;
  168. }
  169. decimal thisTypeDeduction = 0.00M;//当前类型扣款
  170. string leave_starttime = date_Range.new_begin_dt.ToString("HH:mm");
  171. string leave_endtime = date_Range.new_end_dt.ToString("HH:mm");
  172. string typeName = string.Empty;
  173. int leaveTypeId = leaveType;
  174. var leaveTypeData = vacationLeaveTypes.Where(it => it.id == leaveTypeId).FirstOrDefault();
  175. if (leaveTypeData != null) { typeName = leaveTypeData.name; }
  176. //计算请假类型扣款金额
  177. decimal new_duration = 0.00M;
  178. if (date_Range.type == "halfday")
  179. {
  180. new_duration = Convert.ToDecimal(date_Range.new_duration) / 86400M;
  181. }
  182. else if (date_Range.type == "hour")
  183. {
  184. new_duration = Convert.ToDecimal(date_Range.new_duration) / 3600M;
  185. }
  186. decimal leave_meals = 0.00M;
  187. //计算餐补 假勤类型扣款
  188. CalculateTypeFee(leaveType, date_Range.type, leave_starttime, leave_endtime, amountPayable, work_days, new_duration,
  189. out leave_meals, out thisTypeDeduction);
  190. #region 累计类型扣款
  191. //1年假;2事假;3病假;4调休假;5婚假;6产假;7陪产假;8其他;9丧假
  192. if (leaveType == 2) //事假
  193. {
  194. personalLeaveTotal += thisTypeDeduction;
  195. }
  196. else if (leaveType == 3) //病假
  197. {
  198. sickLeaveTotal = thisTypeDeduction;
  199. }
  200. #endregion
  201. meal_deduction += leave_meals;
  202. string startEndTiime = startEndTiime = date_Range.new_begin_dt.ToString("yyyy-MM-dd HH:mm") + " - " +
  203. date_Range.new_end_dt.ToString("yyyy-MM-dd HH:mm");
  204. Ex_Item ex_Item = new Ex_Item()
  205. {
  206. SubTypeId = leaveType,
  207. SubType = typeName,
  208. StartTimeDt = Convert.ToDateTime( date_Range.new_begin_dt.ToString("yyyy-MM-dd HH:mm:ss")),
  209. EndTimeDt = Convert.ToDateTime(date_Range.new_end_dt.ToString("yyyy-MM-dd HH:mm:ss")),
  210. Duration = new_duration,
  211. Deduction = thisTypeDeduction,
  212. //Reason = apply_data.reason,
  213. Apply_time_dt = Convert.ToDateTime(sp_item.apply_time_dt.ToString("yyyy-MM-dd HH:mm:ss")),
  214. //Approval_name = sp_item.approval_name,
  215. };
  216. ex_ItemInfos.Add(ex_Item);
  217. }
  218. }
  219. }
  220. if (ex_ItemInfos.Count > 0)
  221. {
  222. ex_Items_jq.Ex_ItemInfo = ex_ItemInfos.OrderBy(it => it.SubTypeId).ThenBy(it => it.Apply_time_dt).ToList();
  223. ex_Items.Add(ex_Items_jq);
  224. }
  225. #endregion
  226. #endregion
  227. Ex_Items ex_Items_dk = new Ex_Items() { Type = "打卡" }; //打卡
  228. List<Ex_Item> ex_Item_Infos = new List<Ex_Item>();
  229. #region 打卡补卡 补卡次数 处理
  230. if (reissueCardNum > 0)
  231. {
  232. if (reissueCardNum <= 3)
  233. {
  234. reissuecard_deduction += 10 * reissueCardNum;
  235. }
  236. else if (reissueCardNum > 3) //补卡超过三 每补卡一次 50 CNY
  237. {
  238. int for_reissueCardNum = reissueCardNum - 3;
  239. for (int i = 0; i < for_reissueCardNum; i++)
  240. {
  241. reissuecard_deduction += 50;
  242. }
  243. }
  244. Ex_Item ex_reissueCard = new Ex_Item()
  245. {
  246. SubTypeId = 7,
  247. SubType = "补卡次数",
  248. Deduction = reissuecard_deduction,
  249. Reason = "补卡:员工发现自己漏打卡时,需及时提起补卡申请,并说明情况。试用期员工每月有 2 次\r\n补卡机会,超过 2 次不足 5 次的部分,按 10 元/次处罚,5 次及以上的漏卡,按 50 元/次处罚;正式员工每月 3 次以内的补卡,按 10 元/次处罚,3 次及以上的漏卡,按 50 元/次处罚。\r\n补卡路径为:企微-工作台-审批-打卡补卡。",
  250. unit = string.Empty
  251. };
  252. ex_Item_Infos.Add(ex_reissueCard);
  253. }
  254. #endregion
  255. List<Ex_Item> ex_reissuecard_Items = new List<Ex_Item>();
  256. //List<dynamic> ex_reissuecard_Items = new List<dynamic>();
  257. List<Sp_Detail> sp_reissuecard_details = new List<Sp_Detail>();
  258. //统计迟到次数
  259. int beLateNum = 0, // 1-迟到;
  260. leaveEarlyNum = 0, // 2-早退;
  261. dummyDeckNum = 0, // 3-缺卡;
  262. minerNum = 0, // 4-旷工;
  263. locationAnomalyNum = 0, // 5-地点异常;
  264. unitExNum = 0; // 6-设备异常;
  265. if (summary_Info.except_days > 0)
  266. {
  267. List<Exception_Info>? ex_infos = checkInData.exception_infos;
  268. if (ex_infos != null && ex_infos.Count >= 0)
  269. {
  270. beLateNum = ExceptionStatistics(ex_infos, 1);
  271. leaveEarlyNum = ExceptionStatistics(ex_infos, 2);
  272. dummyDeckNum = ExceptionStatistics(ex_infos, 3);
  273. minerNum = ExceptionStatistics(ex_infos, 4);
  274. locationAnomalyNum = ExceptionStatistics(ex_infos, 5);
  275. unitExNum = ExceptionStatistics(ex_infos, 6);
  276. }
  277. }
  278. //打卡记录
  279. CheckInDataView checkInDataView = new CheckInDataView();
  280. checkInDataView = await _qiYeWeChatApiService.GetCheckinDataAsync(new List<string>() { acctid },3,startDt,endDt);
  281. if (checkInDataView.errcode != 0)
  282. {
  283. _result.Msg += startDt + " - " + endDt + " " + itemName + " 打卡记录 "+ checkInDataView.errmsg + " \r\n";
  284. }
  285. //筛选时间异常的打卡记录
  286. List<CheckInDataInfo> checkInDataInfos = checkInDataView.checkindata.Where(it => !string.IsNullOrEmpty(it.exception_type)).ToList();
  287. //处理 未打卡的记录是否已经通过假勤审批
  288. List<CheckInDataInfo> leave_checkInDataInfos = new List<CheckInDataInfo>();
  289. if (checkInDataInfos.Count > 0)
  290. {
  291. foreach (var leaveItem in ex_ItemInfos)
  292. {
  293. if (leaveItem.StartTimeDt.ToString("yyyy-MM-dd").Equals(leaveItem.EndTimeDt.ToString("yyyy-MM-dd"))) //单天
  294. {
  295. leave_checkInDataInfos.AddRange(checkInDataInfos.Where(it => it.checkin_time_dt.ToString("yyyy-MM-dd") == leaveItem.StartTimeDt.ToString("yyyy-MM-dd")).ToList());
  296. }
  297. else //多天
  298. {
  299. leave_checkInDataInfos.AddRange(checkInDataInfos.Where(it => it.checkin_time_dt >= leaveItem.StartTimeDt && it.checkin_time_dt <=leaveItem.EndTimeDt ).ToList());
  300. }
  301. }
  302. }
  303. //处理 时间异常的记录是否已经通过补卡审批
  304. List<CheckInDataInfo> pullcard_checkInDataInfos = new List<CheckInDataInfo>();
  305. if (leave_checkInDataInfos.Count > 0)
  306. {
  307. //获取异常打卡数据 并且 未通过假勤审批
  308. pullcard_checkInDataInfos = checkInDataInfos.Except(leave_checkInDataInfos).ToList();
  309. if (pullcard_checkInDataInfos.Count > 0)
  310. {
  311. #region 打卡异常处理 统计 1-迟到;2-早退;3-缺卡;4-旷工;5-地点异常;6-设备异常;
  312. //sp_reissuecard_details = await _qiYeWeChatApiService.GetApprovalDetailsAsync(startDt, endDt, acctid, 2, 2); //时间段内所有 已同意的 补卡 审批数据
  313. //if (sp_reissuecard_details.Count <= 0)
  314. //{
  315. // _result.Msg += startDt + " - " + endDt + " " + itemName + " 打卡补卡 审批数据获取未获取到!\r\n";
  316. // continue;
  317. //}
  318. //#region 打卡补卡
  319. //foreach (var pullcardItem in sp_reissuecard_details)
  320. //{
  321. // Apply_data? apply_data = pullcardItem.apply_data;
  322. // if (apply_data != null)
  323. // {
  324. // List<ContentsItem> contents = apply_data.contents;
  325. // ContentsItem content_Vacation = contents.Where(it => it.control == "Vacation").FirstOrDefault(); //请假类型
  326. // ContentsItem content_Textarea = contents.Where(it => it.control == "Textarea").FirstOrDefault(); //多行文本
  327. // if (content_Vacation != null)
  328. // {
  329. // Vacation vacation = content_Vacation.value.vacation;
  330. // Attendance attendance = vacation.attendance; //假勤组件
  331. // Selector selector = vacation.selector; //请假类型
  332. // List<OptionsItem> optionsItems = selector.options; //key 请假类型 id
  333. // List<TitleItem> value = optionsItems[0].value; // value 文本描述值
  334. // int leaveType = int.Parse(optionsItems[0].key); //key 请假子类型 id
  335. // Date_range date_Range = attendance.date_range;
  336. // decimal thisTypeDeduction = 0.00M;//当前类型扣款
  337. // string leave_starttime = date_Range.new_begin_dt.ToString("HH:mm");
  338. // string leave_endtime = date_Range.new_end_dt.ToString("HH:mm");
  339. // string typeName = string.Empty;
  340. // int leaveTypeId = leaveType;
  341. // var leaveTypeData = vacationLeaveTypes.Where(it => it.id == leaveTypeId).FirstOrDefault();
  342. // if (leaveTypeData != null) { typeName = leaveTypeData.name; }
  343. // //计算请假类型扣款金额
  344. // int new_duration = 0;
  345. // if (date_Range.type == "halfday")
  346. // {
  347. // new_duration = date_Range.new_duration / 86400;
  348. // }
  349. // else if (date_Range.type == "hour")
  350. // {
  351. // new_duration = date_Range.new_duration / 3600;
  352. // }
  353. // decimal leave_meals = 0.00M;
  354. // CalculateTypeFee(leaveType, date_Range.type, leave_starttime, leave_endtime, amountPayable, work_days, new_duration,
  355. // out leave_meals, out thisTypeDeduction);
  356. // meal_deduction += leave_meals;
  357. // string startEndTiime = startEndTiime = date_Range.new_begin_dt.ToString("yyyy-MM-dd HH:mm") + " - " +
  358. // date_Range.new_end_dt.ToString("yyyy-MM-dd HH:mm");
  359. // }
  360. // }
  361. //}
  362. #endregion
  363. foreach (var pc_ex_item in pullcard_checkInDataInfos)
  364. {
  365. CheckInDayDataView checkInDayDataView = await _qiYeWeChatApiService.GetCheckInDayDataAsync(new List<string>() { acctid },startDt,endDt);
  366. if (checkInDayDataView.errcode != 0)
  367. {
  368. _result.Msg += startDt + " - " + endDt + " " + itemName + " 异常信息数据获取未获取到!\r\n";
  369. continue;
  370. }
  371. List<Root> roots_words= checkInDayDataView.datas.Where(it => it.base_info.day_type == 0).ToList(); //获取工作日日报信息
  372. List<Root> roots_exs = checkInDayDataView.datas.Where(it => it.exception_infos.Count > 0).ToList();
  373. Root roots_ex = roots_exs.Where(it => it.base_info.dateDt == pc_ex_item.sch_checkin_time_dt).FirstOrDefault();
  374. if (roots_ex != null) {
  375. List<Exception_infos> exception_infos = roots_ex.exception_infos;
  376. List<Exception_infos> exception_infos1 = checkInDayDataView.datas[29].exception_infos;
  377. string exc_type = pc_ex_item.exception_type;
  378. if (exc_type.Contains("时间异常") || exc_type.Contains("未打卡")) // 时间异常 未打卡
  379. {
  380. int day_time_minute = (60 * 7) + 30;
  381. Exception_infos beLate_ex = exception_infos.Where(it => it.exception == 1).FirstOrDefault(); //迟到
  382. decimal day_miner_unit = dailyWage / 15; //以0.5小时为单位
  383. if (beLate_ex != null)
  384. {
  385. if (beLateNum > 0) //迟到次数
  386. {
  387. //1:一个自然月内,不足 10 分钟的迟到/早退,不超过 2 次的部分,不做处罚;3 次及以上,按50 元 / 次处罚;
  388. //2:超过 10 分钟(含 10 分钟),不足 60 分钟的迟到 / 早退,按 50 元 / 次处罚;
  389. //3:超过 60 分钟(含 60 分钟),不足 3 小时的迟到 / 早退,且无请假者,按旷工半日处理;超过 3 小时的迟到 / 早退,且无请假者,按旷工一日处理。
  390. Ex_Item beLate_belate_ex = new Ex_Item()
  391. {
  392. SubTypeId = 4,
  393. SubType = "旷工",
  394. unit = "分钟",
  395. };
  396. decimal day_beLate_deduction = 0.00M;
  397. if (beLate_ex.duration >= 10 && beLate_ex.duration <= 60)
  398. {
  399. day_beLate_deduction = beLateNum * 50;
  400. beLate_belate_ex.SubTypeId = 1;
  401. beLate_belate_ex.SubType = "迟到";
  402. }
  403. else if (beLate_ex.duration > 60 && beLate_ex.duration <= 180)
  404. {
  405. day_beLate_deduction = day_miner_unit * 6; //3小时
  406. }
  407. else day_beLate_deduction = dailyWage;
  408. beLate_deduction += day_beLate_deduction;
  409. beLate_belate_ex.Duration = beLate_ex.duration;
  410. beLate_belate_ex.Deduction = day_beLate_deduction;
  411. ex_reissuecard_Items.Add(beLate_belate_ex);
  412. }
  413. if (leaveEarlyNum > 0) // 早退次数
  414. {
  415. //1:一个自然月内,不足 10 分钟的迟到/早退,不超过 2 次的部分,不做处罚;3 次及以上,按50 元 / 次处罚;
  416. //2:超过 10 分钟(含 10 分钟),不足 60 分钟的迟到 / 早退,按 50 元 / 次处罚;
  417. //3:超过 60 分钟(含 60 分钟),不足 3 小时的迟到 / 早退,且无请假者,按旷工半日处理;超过 3 小时的迟到 / 早退,且无请假者,按旷工一日处理。
  418. Ex_Item beLate_belate_ex = new Ex_Item()
  419. {
  420. SubTypeId = 4,
  421. SubType = "旷工",
  422. unit = "分钟",
  423. };
  424. decimal day_beLate_deduction = 0.00M;
  425. if (beLate_ex.duration >= 10 && beLate_ex.duration <= 60)
  426. {
  427. day_beLate_deduction = beLateNum * 50;
  428. beLate_belate_ex.SubTypeId = 3;
  429. beLate_belate_ex.SubType = "未打卡/漏卡";
  430. }
  431. else if (beLate_ex.duration > 60 && beLate_ex.duration <= 180)
  432. {
  433. day_beLate_deduction = day_miner_unit * 6; //3小时
  434. }
  435. else day_beLate_deduction = dailyWage;
  436. beLate_deduction += day_beLate_deduction;
  437. beLate_belate_ex.Duration = beLate_ex.duration;
  438. beLate_belate_ex.Deduction = day_beLate_deduction;
  439. ex_reissuecard_Items.Add(beLate_belate_ex);
  440. }
  441. if (dummyDeckNum > 0) //缺卡
  442. {
  443. Ex_Item beLate_belate_ex = new Ex_Item()
  444. {
  445. SubTypeId = 4,
  446. SubType = "旷工",
  447. unit = "分钟",
  448. };
  449. decimal day_beLate_deduction = 0.00M;
  450. if (beLate_ex.duration >= 10 && beLate_ex.duration <= 60)
  451. {
  452. day_beLate_deduction = beLateNum * 50;
  453. beLate_belate_ex.SubTypeId = 2;
  454. beLate_belate_ex.SubType = "早退";
  455. }
  456. else if (beLate_ex.duration > 60 && beLate_ex.duration <= 180)
  457. {
  458. day_beLate_deduction = day_miner_unit * 6; //3小时
  459. }
  460. else day_beLate_deduction = dailyWage;
  461. beLate_deduction += day_beLate_deduction;
  462. beLate_belate_ex.Duration = beLate_ex.duration;
  463. beLate_belate_ex.Deduction = day_beLate_deduction;
  464. ex_reissuecard_Items.Add(beLate_belate_ex);
  465. }
  466. if (minerNum > 0) //矿工
  467. {
  468. Ex_Item beLate_belate_ex = new Ex_Item()
  469. {
  470. SubTypeId = 4,
  471. SubType = "旷工",
  472. unit = "分钟",
  473. };
  474. decimal day_beLate_deduction = 0.00M;
  475. if (beLate_ex.duration > 60 && beLate_ex.duration <= 180)
  476. {
  477. day_beLate_deduction = day_miner_unit * 6; //3小时
  478. }
  479. else day_beLate_deduction = dailyWage;
  480. beLate_deduction += day_beLate_deduction;
  481. beLate_belate_ex.Duration = beLate_ex.duration;
  482. beLate_belate_ex.Deduction = day_beLate_deduction;
  483. ex_reissuecard_Items.Add(beLate_belate_ex);
  484. }
  485. }
  486. }
  487. }
  488. }
  489. }
  490. }
  491. if (ex_reissuecard_Items.Count > 0)
  492. {
  493. ex_Items_dk.Ex_ItemInfo = ex_reissuecard_Items;
  494. }
  495. #region 应发合计 实发合计 扣款合计(假勤扣款,其他扣款,社保扣款,公积金代扣,个税扣款)
  496. decimal mealTotal = meal_subsidy - meal_deduction; //餐补
  497. decimal salaryTotal = amountPayable + mealTotal; //应发合计
  498. //扣款合计 不含个税
  499. decimal eductionTotal = sickLeaveTotal + personalLeaveTotal + beLate_deduction + early_deduction + absenteeism_deduction + unprinted_deduction + other_deduction +
  500. pm_wsInfo.WithholdingInsurance + pm_wsInfo.ReservedFunds + pm_wsInfo.OtherDeductions;
  501. decimal actualReleaseTotal = salaryTotal - eductionTotal; //实发合计 * 不含个税
  502. #endregion
  503. #region 处理当月工资数据
  504. pm_wsInfo.YearMonth = thisYearMonth;
  505. pm_wsInfo.StartDate = startDt.ToString("yyyy-MM-dd");
  506. pm_wsInfo.EndDate = endDt.ToString("yyyy-MM-dd");
  507. pm_wsInfo.SickLeave = sickLeaveTotal; //病假
  508. pm_wsInfo.SomethingFalse = personalLeaveTotal; //事假
  509. pm_wsInfo.LateTo = beLate_deduction; //迟到
  510. pm_wsInfo.LeaveEarly = early_deduction; //早退
  511. pm_wsInfo.Absenteeism = absenteeism_deduction; //旷工
  512. pm_wsInfo.NotPunch = unprinted_deduction; //未打卡
  513. pm_wsInfo.OtherDeductions = other_deduction; //其他
  514. pm_wsInfo.Ex_ItemsRemark = JsonConvert.SerializeObject(ex_Items); //
  515. pm_wsInfo.Mealsupplement = mealTotal; //餐补
  516. pm_wsInfo.Should = salaryTotal; //应发合计
  517. pm_wsInfo.TotalDeductions = eductionTotal; //扣款合计
  518. pm_wsInfo.TotalRealHair = actualReleaseTotal; //实发合计
  519. pm_wsInfo.AfterTax = actualReleaseTotal - pm_wsInfo.WithholdingTax; //税后工资
  520. pm_wsInfo.LastUpdateUserId = userId;
  521. pm_wsInfo.LastUpdateDt = DateTime.Now;
  522. pm_wsInfo.CreateUserId = userId;
  523. pm_wsInfo.CreateTime = DateTime.Now;
  524. pm_wsInfo.DeleteUserId = null;
  525. pm_wsInfo.DeleteTime = null;
  526. #endregion
  527. }
  528. _result.Code = 0;
  529. _result.Data = pm_WageSheetDattaSources;
  530. return _result;
  531. }
  532. /// <summary>
  533. /// decimal 保留两位小数 不四舍五入
  534. /// </summary>
  535. /// <param name="number"></param>
  536. /// <returns></returns>
  537. private static decimal ConvertToDecimal(decimal number)
  538. {
  539. return Convert.ToDecimal(number.ToString("0.00"));
  540. }
  541. /// <summary>
  542. /// 获取请假类型
  543. /// </summary>
  544. /// <param name="template_id"></param>
  545. /// <returns></returns>
  546. public static async Task<List<VacationLeaveTypeView>> GetVacationLeaveTypes(string template_id)
  547. {
  548. List<VacationLeaveTypeView> vacationLeaveTypes = new List<VacationLeaveTypeView>();
  549. TemplateDetailView templateDetailView = new TemplateDetailView();
  550. templateDetailView = await _qiYeWeChatApiService.GetTemplateDetailAsync(template_id);
  551. if (templateDetailView.errcode != 0)
  552. {
  553. Log.Error("【企业微信】【审批】【获取假勤类型的审批】【Msg】"+ templateDetailView.errmsg);
  554. return vacationLeaveTypes;
  555. }
  556. List<VacationItemInfo> VacationItemInfos = templateDetailView.vacation_list.item;
  557. foreach (var item in VacationItemInfos)
  558. {
  559. ValueItem valueInfo = item.name.Where(it => it.lang == "zh_CN").FirstOrDefault();
  560. if (valueInfo != null)
  561. {
  562. vacationLeaveTypes.Add(
  563. new VacationLeaveTypeView()
  564. {
  565. id = item.id,
  566. name = valueInfo.text
  567. });
  568. }
  569. }
  570. return vacationLeaveTypes;
  571. }
  572. /// <summary>
  573. /// 计算类型费用
  574. /// </summary>
  575. /// <param name="leaveType">
  576. /// 1年假;2事假;3病假;4调休假;5婚假;6产假;7陪产假;8其他;9丧假
  577. /// </param>
  578. /// <param name="date_Range_type">
  579. /// halfday 全天
  580. /// hour 小时
  581. /// </param>
  582. /// <param name="startTime"></param>
  583. /// <param name="endTime"></param>
  584. /// <param name="duration"></param>
  585. /// <param name="mealDeduction"></param>
  586. /// <param name="typeDeduction"></param>
  587. public static void CalculateTypeFee(int leaveType, string date_Range_type, string startTime, string endTime,decimal amountPayable,int work_days,
  588. decimal duration, out decimal mealDeduction, out decimal typeDeduction)
  589. {
  590. typeDeduction = 0;
  591. mealDeduction = 0;
  592. string am_starttime = "08:59";
  593. string am_endtime = "13:01";
  594. decimal personalkLeave_dailywage_day = ConvertToDecimal( amountPayable / work_days); //日薪 = 事假日薪 *计算方式:日平均工资 = 当月应发工资 /当月应出勤天数。
  595. //半小时单位
  596. decimal halfHour = Convert.ToDecimal(7.5) / Convert.ToDecimal(0.5);
  597. switch (leaveType)
  598. {
  599. case 1: //年假
  600. CalculateTypeFeeSub(date_Range_type, startTime, endTime, duration, out mealDeduction);
  601. break;
  602. case 2: //2事假
  603. // 事假日薪 *计算方式:日平均工资 = 当月应发工资 /当月应出勤天数。
  604. decimal personalkLeave_dailywage_halfhour = ConvertToDecimal( personalkLeave_dailywage_day / halfHour); //事假单位 0.5小时
  605. if (date_Range_type == "halfday")
  606. {
  607. mealDeduction = 10; //餐补扣款
  608. typeDeduction = personalkLeave_dailywage_day;
  609. }
  610. else if (date_Range_type == "hour")
  611. {
  612. decimal leave_halfHour = Convert.ToDecimal(duration) / Convert.ToDecimal(0.5);
  613. typeDeduction = personalkLeave_dailywage_halfhour * leave_halfHour;
  614. if (duration >= 3 && duration <= 7.5M) //单天请假三小时 && 请假时间在上午 则没有餐补
  615. {
  616. //处理开始时间
  617. if (startTime.CompareTo(am_starttime) > 0 && startTime.CompareTo(am_endtime) < 0)
  618. {
  619. //处理结束时间
  620. //if (endTime.CompareTo(am_starttime) > 0 && endTime.CompareTo(am_endtime) > 0)
  621. //{
  622. mealDeduction = 10; //餐补扣款
  623. //}
  624. }
  625. }
  626. else if (duration > 7.5M) //多天计算
  627. {
  628. decimal leave_halfHour1 = Convert.ToDecimal(duration) / Convert.ToDecimal(0.5);
  629. typeDeduction = personalkLeave_dailywage_halfhour * leave_halfHour1;
  630. decimal leaveDays = duration / 7.5M;
  631. if (leaveDays % 1 == 0)
  632. {
  633. mealDeduction = 10 * leaveDays; //餐补扣款
  634. }
  635. else
  636. {
  637. mealDeduction = 10 * Convert.ToInt32(leaveDays);
  638. //得到最后一天的请假时间 是否有餐补
  639. int lastHours = (Convert.ToDateTime(endTime) - Convert.ToDateTime("09:00")).Hours;
  640. if (lastHours >= 3)
  641. {
  642. //处理结束时间
  643. if (endTime.CompareTo(am_starttime) > 0 && endTime.CompareTo(am_endtime) < 0)
  644. {
  645. mealDeduction += 10; //餐补扣款
  646. }
  647. }
  648. }
  649. }
  650. }
  651. break;
  652. case 3: //3病假
  653. // 病假日薪 *计算方式:日平均工资 = 成都市最低工资标准的80% /当月应出勤天数。 短期病假=当月15天内
  654. decimal chengDuMinimumWage_halrHour = ConvertToDecimal( _chengDuMinimumWage / work_days) / halfHour;
  655. decimal sickLeave_dailywage_halfhour_deduction = personalkLeave_dailywage_day - chengDuMinimumWage_halrHour; //病假单位 0.5小时 扣款金额
  656. if (date_Range_type == "halfday")
  657. {
  658. mealDeduction = 10; //餐补扣款
  659. typeDeduction = sickLeave_dailywage_halfhour_deduction * halfHour;
  660. }
  661. else if (date_Range_type == "hour")
  662. {
  663. decimal sickLeave_halfHour = duration / 0.5M;
  664. typeDeduction = sickLeave_dailywage_halfhour_deduction *sickLeave_halfHour ;
  665. if (duration >= 3 && duration <= 7.5M) //单天请假三小时 && 请假时间在上午 则没有餐补
  666. {
  667. //处理开始时间
  668. if (startTime.CompareTo(am_starttime) > 0 && startTime.CompareTo(am_endtime) < 0)
  669. {
  670. //处理结束时间
  671. if (endTime.CompareTo(am_starttime) > 0 && endTime.CompareTo(am_endtime) < 0)
  672. {
  673. mealDeduction = 10; //餐补扣款
  674. }
  675. }
  676. }
  677. else if (duration > 7.5M) //多天计算
  678. {
  679. decimal sickLeave_halfHour1 = duration / 0.5M;
  680. typeDeduction = sickLeave_dailywage_halfhour_deduction * sickLeave_halfHour1;
  681. decimal leaveDays = Convert.ToDecimal(duration / 7.5M);
  682. if (leaveDays % 1 == 0)
  683. {
  684. mealDeduction = 10 * leaveDays; //餐补扣款
  685. }
  686. else
  687. {
  688. mealDeduction = 10 * Convert.ToInt32(leaveDays);
  689. //得到最后一天的请假时间 是否有餐补
  690. int lastHours = (Convert.ToDateTime(endTime) - Convert.ToDateTime("09:00")).Hours;
  691. if (lastHours >= 3)
  692. {
  693. //处理结束时间
  694. if (endTime.CompareTo(am_starttime) > 0 && endTime.CompareTo(am_endtime) < 0)
  695. {
  696. mealDeduction += 10; //餐补扣款
  697. }
  698. }
  699. }
  700. }
  701. }
  702. break;
  703. case 4: //4调休假
  704. CalculateTypeFeeSub(date_Range_type, startTime, endTime, duration, out mealDeduction);
  705. break;
  706. case 5: //5婚假
  707. CalculateTypeFeeSub(date_Range_type, startTime, endTime, duration, out mealDeduction);
  708. break;
  709. case 6: //6产假
  710. CalculateTypeFeeSub(date_Range_type, startTime, endTime, duration, out mealDeduction);
  711. break;
  712. case 7: //7陪产假
  713. CalculateTypeFeeSub(date_Range_type, startTime, endTime, duration, out mealDeduction);
  714. break;
  715. case 8: //8其他
  716. CalculateTypeFeeSub(date_Range_type, startTime, endTime, duration, out mealDeduction);
  717. break;
  718. case 9: //9丧假
  719. CalculateTypeFeeSub(date_Range_type, startTime, endTime, duration, out mealDeduction);
  720. break;
  721. }
  722. }
  723. /// <summary>
  724. /// 计算类型费用
  725. /// </summary>
  726. /// <param name="date_Range_type">
  727. /// halfday 全天
  728. /// hour 小时
  729. /// </param>
  730. /// <param name="startTime"></param>
  731. /// <param name="endTime"></param>
  732. /// <param name="duration"></param>
  733. /// <param name="mealDeduction"></param>
  734. public static void CalculateTypeFeeSub(string date_Range_type, string startTime, string endTime, decimal duration, out decimal mealDeduction)
  735. {
  736. mealDeduction = 0;
  737. string am_starttime = "08:59";
  738. string am_endtime = "13:01";
  739. if (date_Range_type == "halfday")
  740. {
  741. mealDeduction = 10; //餐补扣款
  742. }
  743. else if (date_Range_type == "hour")
  744. {
  745. if (duration >= 3 && duration < 7) //单天请假三小时 && 请假时间在上午 则没有餐补
  746. {
  747. //处理开始时间
  748. if (startTime.CompareTo(am_starttime) > 0 && startTime.CompareTo(am_endtime) < 0)
  749. {
  750. //处理结束时间
  751. if (endTime.CompareTo(am_starttime) > 0 && endTime.CompareTo(am_endtime) < 0)
  752. {
  753. mealDeduction = 10; //餐补扣款
  754. }
  755. }
  756. }
  757. else if (duration >= 7 && duration <= 7.50M )
  758. {
  759. mealDeduction = 10; //餐补扣款
  760. }
  761. else if (duration >= 7.50M) //多天计算
  762. {
  763. decimal leaveDays = Convert.ToDecimal(duration / 7.50M);
  764. if (leaveDays % 1 == 0)
  765. {
  766. mealDeduction = 10 * leaveDays; //餐补扣款
  767. }
  768. else
  769. {
  770. mealDeduction = 10 * Convert.ToInt32(leaveDays);
  771. //得到最后一天的请假时间 是否有餐补
  772. int lastHours = (Convert.ToDateTime(endTime) - Convert.ToDateTime("09:00")).Hours;
  773. if (lastHours >= 3)
  774. {
  775. //处理结束时间
  776. if (endTime.CompareTo(am_starttime) > 0 && endTime.CompareTo(am_endtime) < 0)
  777. {
  778. mealDeduction += 10; //餐补扣款
  779. }
  780. }
  781. }
  782. }
  783. }
  784. }
  785. /// <summary>
  786. /// 获取打卡补卡类型
  787. /// </summary>
  788. /// <param name="template_id"></param>
  789. /// <returns></returns>
  790. public static async Task<List<VacationLeaveTypeView>> GetVacationReissueCardTypes(string template_id)
  791. {
  792. List<VacationLeaveTypeView> vacationLeaveTypes = new List<VacationLeaveTypeView>();
  793. TemplateDetailView templateDetailView = new TemplateDetailView();
  794. templateDetailView = await _qiYeWeChatApiService.GetTemplateDetailAsync(template_id);
  795. if (templateDetailView.errcode != 0)
  796. {
  797. return vacationLeaveTypes;
  798. }
  799. List<VacationItemInfo> VacationItemInfos = templateDetailView.vacation_list.item;
  800. foreach (var item in VacationItemInfos)
  801. {
  802. ValueItem valueInfo = item.name.Where(it => it.lang == "zh_CN").FirstOrDefault();
  803. if (valueInfo != null)
  804. {
  805. vacationLeaveTypes.Add(
  806. new VacationLeaveTypeView()
  807. {
  808. id = item.id,
  809. name = valueInfo.text
  810. });
  811. }
  812. }
  813. return vacationLeaveTypes;
  814. }
  815. /// <summary>
  816. /// 打卡数据
  817. /// 假勤数据 统计
  818. /// </summary>
  819. /// <param name="datas">数据源</param>
  820. /// <param name="type">
  821. /// 1-请假;2-补卡;3-出差;4-外出;100-外勤;
  822. /// </param>
  823. /// <param name="subTypeName">
  824. /// 年假 事假 病假 调休假 婚嫁 产假 陪产假 丧假 补卡次数 出差 外出数 外勤 其他
  825. /// </param>
  826. /// <returns></returns>
  827. private static int Fallibilitydispose(List<Sp_Item> datas, int type, string? subTypeName)
  828. {
  829. int num = 0;
  830. Sp_Item _Info = datas.Where(it => it.type == type && it.name == subTypeName).FirstOrDefault();
  831. if (_Info != null) { num = _Info.count; }
  832. return num;
  833. }
  834. /// <summary>
  835. /// 打卡数据
  836. /// 异常数据 统计
  837. /// </summary>
  838. /// <returns></returns>
  839. private static int ExceptionStatistics(List<Exception_Info> datas, int type)
  840. {
  841. int num = 0;
  842. Exception_Info _Info = datas.Where(it => it.exception == type).FirstOrDefault();
  843. if (_Info != null) { num = _Info.count; }
  844. return num;
  845. }
  846. }
  847. }