PayrollComputation.cs 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555
  1. using Google.Protobuf.WellKnownTypes;
  2. using NPOI.HPSF;
  3. using NPOI.OpenXmlFormats.Dml.Diagram;
  4. using NPOI.OpenXmlFormats.Spreadsheet;
  5. using NPOI.SS.Formula.Functions;
  6. using NPOI.Util;
  7. using OASystem.API.OAMethodLib.QiYeWeChatAPI;
  8. using OASystem.Domain;
  9. using OASystem.Domain.Entities.PersonnelModule;
  10. using OASystem.Domain.ViewModels.PersonnelModule;
  11. using OASystem.Domain.ViewModels.QiYeWeChat;
  12. using OASystem.Infrastructure.Repositories.Groups;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Diagnostics.Eventing.Reader;
  16. using System.Drawing;
  17. using System.Linq.Expressions;
  18. using TencentCloud.Ocr.V20181119.Models;
  19. namespace OASystem.API.OAMethodLib
  20. {
  21. /// <summary>
  22. /// 工资计算
  23. /// </summary>
  24. public static class PayrollComputation
  25. {
  26. private static Result _result = new Result();
  27. private static readonly IQiYeWeChatApiService _qiYeWeChatApiService = AutofacIocManager.Instance.GetService<IQiYeWeChatApiService>();
  28. private static readonly UsersRepository _usersRep = AutofacIocManager.Instance.GetService<UsersRepository>();
  29. private static readonly IMapper _mapper = AutofacIocManager.Instance.GetService<IMapper>();
  30. private static readonly decimal _chengDuMinimumWage = 2100.00M * 0.80M; //员工在病假医疗期限内的病假工资按照成都市最低工资标准的 80%发放
  31. /// <summary>
  32. /// 计算工资
  33. /// </summary>
  34. /// <param name="pm_WageSheetDattaSources"></param>
  35. /// <param name="userNames"></param>
  36. /// <param name="thisYearMonth"></param>
  37. /// <param name="startDt"></param>
  38. /// <param name="endDt"></param>
  39. /// <returns></returns>
  40. public static async Task<Result> SalaryCalculatorAsync(
  41. List<Pm_WageSheet> pm_WageSheetDattaSources, List<UserNameView> userNames, int userId, string thisYearMonth, DateTime startDt, DateTime endDt)
  42. {
  43. if (pm_WageSheetDattaSources.Count <= 0)
  44. {
  45. _result.Msg = "计算工资传入数据为空!";
  46. return _result;
  47. }
  48. if (userNames.Count <= 0)
  49. {
  50. var nameData = await _usersRep.GetUserNameList(1);
  51. userNames = nameData.Data;
  52. }
  53. //计算时间段内工作日
  54. int work_days = await GetWorkDays(thisYearMonth);
  55. if (work_days <=0)
  56. {
  57. _result.Msg = thisYearMonth+" 工作日未设置,请前往《工作日管理页面》设置!";
  58. return _result;
  59. }
  60. UserIdListView userIdListView = await _qiYeWeChatApiService.GetUserIdListAsync();
  61. if (userIdListView.errcode != 0)
  62. {
  63. _result.Msg = "【企业微信】【打卡】【获取员工ID】【Msg】" + userIdListView.errmsg;
  64. return _result;
  65. }
  66. List<string> qyWhchatIdList = new List<string>();
  67. qyWhchatIdList = userIdListView.dept_user.Select(it => it.userid).ToList();
  68. CheckInDayDataView checkInDayDataView = await _qiYeWeChatApiService.GetCheckInDayDataAsync(qyWhchatIdList, startDt, endDt);
  69. if (checkInDayDataView.errcode != 0)
  70. {
  71. _result.Msg = "【企业微信】【打卡】【获取时间段内所有日打卡】【Msg】" + checkInDayDataView.errmsg;
  72. return _result;
  73. }
  74. //获取所有打卡记录 外出
  75. CheckInDataView checkInDataView = new CheckInDataView();
  76. checkInDataView = await _qiYeWeChatApiService.GetCheckinDataAsync(qyWhchatIdList, 3,startDt, endDt);
  77. if (checkInDataView.errcode != 0)
  78. {
  79. _result.Msg = "【企业微信】【打卡】【获取时间段内所有日打卡记录】【Msg】" + checkInDataView.errmsg;
  80. return _result;
  81. }
  82. //查询工作日
  83. List<Sys_Calendar> sys_Calendars = new List<Sys_Calendar>();
  84. string sys_sql = string.Format("Select * From Sys_Calendar Where Isdel = 0 And Dt between '{0}' And '{1}'",
  85. startDt.ToString("yyyy-MM-dd"), endDt.ToString("yyyy-MM-dd"));
  86. sys_Calendars = await _usersRep._sqlSugar.SqlQueryable<Sys_Calendar>(sys_sql).ToListAsync();
  87. //筛选出工作日日报
  88. List<Root> workday_userRoots = checkInDayDataView.datas.Where(it => it.base_info.day_type == 0 && it.base_info.record_type == 1).ToList();
  89. //工作日日报
  90. workday_userRoots = workday_userRoots.OrderBy(it => it.base_info.date).ToList();
  91. //获取 请假类型 Sp_Detail.template_id
  92. string leave_template_id = "C4NzTJCh1onCUK915rRkvy7Fh5Vqz4YbiEV9jrBY1";
  93. List<VacationLeaveTypeView> vacationLeaveTypes = await GetVacationLeaveTypes(leave_template_id);
  94. if (vacationLeaveTypes.Count <= 0)
  95. {
  96. _result.Msg = "【企业微信】【审批】【获取审批类型】【Msg】" + startDt + " - " + endDt + "请假 类型数据 获取失败!";
  97. return _result;
  98. }
  99. try
  100. {
  101. foreach (var pm_wsInfo in pm_WageSheetDattaSources)
  102. {
  103. string itemName = userNames.Where(it => it.Id == pm_wsInfo.UserId).FirstOrDefault().CnName;
  104. //补贴 金额
  105. decimal meal_subsidy = 0.00M; // 午餐(午餐10元/天) 补贴 * 计算方式:单日上午请假时长(小时)大于或者等于三小时 没有餐补
  106. //事假 病假 总金额
  107. decimal personalLeaveTotal = 0.00M, // 事假 日薪 *计算方式:日平均工资 = 月工资/当月应出勤天数。
  108. sickLeaveTotal = 0.00M; // 病假 日薪 *计算方式:日平均工资 = 成都市最低工资标准的80%/当月应出勤天数。 短期病假=当月15天内
  109. //扣款金额
  110. decimal beLate_deduction = 0.00M, // 迟到 扣款金额 *计算方式:
  111. // 一个自然月内,不足 10 分钟的迟到/早退,不超过 2 次的部分,不做处罚;3 次及以上,按50元 / 次处罚;
  112. // 超过 10 分钟(含 10 分钟),不足 60 分钟的迟到/早退,按 50 元/次处罚;
  113. // 超过 60 分钟(含 60 分钟),不足 3 小时的迟到/早退,且无请假者,按旷工半日处理;超过3 小时的迟到 / 早退,且无请假者,按旷工一日处理。
  114. early_deduction = 0.00M, // 早退 扣款金额
  115. absenteeism_deduction = 0.00M, // 旷工 扣款金额 *计算方式:旷工扣发当日工资
  116. unprinted_deduction = 0.00M, // 未打卡 扣款金额 *计算方式:
  117. // 试用期员工每月有 2 次 补卡机会,超过 2 次不足 5 次的部分,按 10 元 / 次处罚,5 次及以上的漏卡,按 50 元 / 次处罚;
  118. // 正式员工每月 3 次以内的补卡,按 10 元 / 次处罚,3 次及以上的漏卡,按 50 元 / 次处罚。
  119. sickLeave_deduction = 0.00M, // 病假
  120. other_deduction = 0.00M; // 其他 扣款金额
  121. decimal meal_deduction = 0.00M; // 餐补 扣款金额
  122. decimal reissuecard_deduction = 0.00M; // 补卡 扣款金额
  123. #region 计算日工资 正常日薪 事假日薪 病假日薪
  124. //月 - 应发工资
  125. decimal amountPayable = pm_wsInfo.Basic + pm_wsInfo.Floats + pm_wsInfo.PostAllowance + pm_wsInfo.InformationSecurityFee +
  126. pm_wsInfo.OtherSubsidies + pm_wsInfo.OtherHandle;
  127. // 日薪 = *计算方式:日平均工资 = 月工资/当月应出勤天数。
  128. decimal dailyWage = amountPayable / work_days;
  129. // 病假日薪 *计算方式:日平均工资 = 成都市最低工资标准的80%/当月应出勤天数。 短期病假=当月15天内
  130. decimal sickLeave_dailywage = _chengDuMinimumWage / work_days;
  131. //病假 一天扣款
  132. sickLeave_deduction = dailyWage - sickLeave_dailywage;
  133. List<Ex_Items> ex_Items = new List<Ex_Items>();//假勤 And 打卡备注集合
  134. Ex_Items ex_Items_dk = new Ex_Items() { Type = "打卡" }; //打卡
  135. Ex_Items ex_Items_jq = new Ex_Items() { Type = "假勤" }; //假勤
  136. Ex_Items ex_Items_cc = new Ex_Items() { Type = "出差" }; //出差
  137. #endregion
  138. List<Root> userRoots = new List<Root>();
  139. if (itemName == "蔡雯")
  140. {
  141. userRoots = workday_userRoots.Where(it => it.base_info.name == "蔡蔡" || it.base_info.name == "蔡雯").ToList(); //工作日日报 1-固定上下班;
  142. }
  143. else
  144. {
  145. userRoots = workday_userRoots.Where(it => it.base_info.name == itemName).ToList(); //工作日日报 1-固定上下班;
  146. }
  147. //userRoots = userRoots.Distinct().ToList();
  148. userRoots = userRoots.OrderBy(it => it.base_info.date).ToList();
  149. int dk_work_days = userRoots.Count; //应出勤天数
  150. if (dk_work_days > work_days)
  151. {
  152. dk_work_days = work_days;
  153. }
  154. meal_subsidy = dk_work_days * 10; //应发放餐补
  155. if (!itemName.Equals("张海麟"))
  156. {
  157. if (userRoots.Count <= 0)
  158. {
  159. _result.Msg = "【企业微信】【打卡】【获取打卡数据】【Msg】" + startDt + " - " + endDt + "打卡日数据 获取失败!";
  160. continue;
  161. }
  162. string acctid = userRoots[0].base_info.acctid;
  163. List<Ex_Item> ex_reissuecard_Items = new List<Ex_Item>(); //打卡类型 数据
  164. List<Sp_items> acc_sp_items = new List<Sp_items>(); //审批数据
  165. int user_probationary_bk_num = 0;
  166. decimal user_probationary_bk_decimal = pm_wsInfo.Floats; //绩效工资为0 则为试用员工
  167. //处理外出打卡记录
  168. List<CheckInDataInfo> checkInData1 = new List<CheckInDataInfo>();
  169. checkInData1 = checkInDataView.checkindata;
  170. List<CheckInDataInfo> checkInDatas = new List<CheckInDataInfo>();
  171. checkInDatas = checkInData1.Where(it => it.userid == acctid).ToList();
  172. //找出外出的打卡记录
  173. List<CheckInDataInfo> checkInData2 = new List<CheckInDataInfo>();
  174. checkInData2 = checkInDatas.Where(it => it.exception_type.Equals("未打卡")).ToList();
  175. #region 迟到 早退 旷工
  176. int user_cd_zt_num = 0; //早退/迟到 次数 10分钟内 2次以内不记处罚 三次及以上50一次
  177. foreach (var root in userRoots)
  178. {
  179. List<Holiday_infos> holiday_Infos = root.holiday_infos; //当天假勤信息
  180. List<Exception_infos> exception_infos = root.exception_infos; //当天校准状态信息
  181. List<Sp_items> sp_Items = root.sp_items;//当天假勤统计信息
  182. if (sp_Items.Count > 0)
  183. {
  184. sp_Items = sp_Items.Where(it => it.count > 0).ToList();
  185. acc_sp_items.AddRange(sp_Items);
  186. }
  187. foreach (var exception_info in exception_infos)
  188. {
  189. decimal timelength = ConvertToDecimal((Convert.ToDecimal(exception_info.duration) / 3600.00M) * 60.00M); //时长 分钟
  190. if (timelength == 9) timelength = 7.50M;
  191. int exception = exception_info.exception; //异常类型
  192. decimal day_miner_unit = dailyWage / 15; //以0.5小时为单位
  193. //1:一个自然月内,不足 10 分钟的迟到/早退,不超过 2 次的部分,不做处罚;3 次及以上,按50 元 / 次处罚;
  194. //2:超过 10 分钟(含 10 分钟),不足 60 分钟的迟到 / 早退,按 50 元 / 次处罚;
  195. //3:超过 60 分钟(含 60 分钟),不足 3 小时的迟到 / 早退,且无请假者,按旷工半日处理;超过 3 小时的迟到 / 早退,且无请假者,按旷工一日处理。
  196. long date = root.base_info.date; //当日工作日期
  197. long earliest_time = root.summary_info.earliest_time; //最早打卡时间
  198. long lastest_time = root.summary_info.lastest_time; //最晚打卡时间
  199. long this_date = date + earliest_time;
  200. DateTime thisDt = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)).AddTicks(this_date * 10000000);
  201. string thisDtStr = thisDt.ToString("yyyy-MM-dd");
  202. Ex_Item beLate_belate_ex = new Ex_Item()
  203. {
  204. SubTypeId = 4,
  205. SubType = "旷工",
  206. Duration = timelength,
  207. StartTimeDt = Convert.ToDateTime(root.base_info.dateDt.ToString("yyyy-MM-dd HH:mm:ss")),
  208. Unit = "分钟",
  209. };
  210. decimal day_deduction = 0.00M;
  211. //1 - 迟到;2 - 早退;3 - 缺卡;4 - 旷工;5 - 地点异常;6 - 设备异常
  212. if (exception == 1) //迟到
  213. {
  214. if (timelength < 10)
  215. {
  216. user_cd_zt_num++;
  217. beLate_belate_ex.SubTypeId = 1;
  218. beLate_belate_ex.SubType = "迟到";
  219. string thisStartDt = (TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)).AddTicks((date + earliest_time) * 10000000))
  220. .ToString("yyyy-MM-dd HH:mm:ss");
  221. beLate_belate_ex.StartTimeDt = Convert.ToDateTime(thisStartDt);
  222. if (user_cd_zt_num >= 3)
  223. {
  224. day_deduction = 50.00M;
  225. }
  226. else
  227. {
  228. day_deduction = 0.00M;
  229. }
  230. beLate_deduction += day_deduction; //迟到扣款 总额
  231. }
  232. else if (timelength >= 10 && timelength <= 60)
  233. {
  234. string thisStartDt = (TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1))
  235. .AddTicks((date + earliest_time) * 10000000))
  236. .ToString("yyyy-MM-dd HH:mm:ss");
  237. beLate_belate_ex.StartTimeDt = Convert.ToDateTime(thisStartDt);
  238. day_deduction = 50.00M;
  239. beLate_deduction += day_deduction; //迟到扣款 总额
  240. beLate_belate_ex.SubTypeId = 1;
  241. beLate_belate_ex.SubType = "迟到";
  242. }
  243. else if (timelength > 60 && timelength <= 180)
  244. {
  245. day_deduction = ConvertToDecimal(day_miner_unit * 6); //3小时
  246. meal_deduction += 10.00M; //餐补扣款
  247. absenteeism_deduction += day_deduction; //矿工半日
  248. beLate_belate_ex.Reason = thisDtStr + " 上午(09:00)缺卡/未打卡视为下午旷工(3小时)";
  249. }
  250. else
  251. {
  252. day_deduction = ConvertToDecimal(dailyWage);
  253. absenteeism_deduction += day_deduction; //矿工一日
  254. meal_deduction += 10.00M;
  255. beLate_belate_ex.Reason = thisDtStr + " 上午(09:00)-下午(18:00) 缺卡/未打视为旷工一天(7.5小时)";
  256. }
  257. beLate_belate_ex.Deduction = day_deduction;
  258. ex_reissuecard_Items.Add(beLate_belate_ex);
  259. }
  260. else if (exception == 2) //早退
  261. {
  262. if (timelength < 10)
  263. {
  264. user_cd_zt_num++;
  265. beLate_belate_ex.SubTypeId = 2;
  266. beLate_belate_ex.SubType = "早退";
  267. string thisEndDt = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1))
  268. .AddTicks((date + lastest_time) * 10000000)
  269. .ToString("yyyy-MM-dd HH:mm:ss");
  270. beLate_belate_ex.StartTimeDt = Convert.ToDateTime(thisEndDt);
  271. if (user_cd_zt_num >= 3)
  272. {
  273. day_deduction = 50.00M;
  274. }
  275. else
  276. {
  277. day_deduction = 0.00M;
  278. }
  279. early_deduction += day_deduction; //早退扣款 总计
  280. }
  281. else if (timelength >= 10 && timelength <= 60)
  282. {
  283. day_deduction = 50.00M;
  284. early_deduction += day_deduction; //早退扣款 总计
  285. beLate_belate_ex.SubTypeId = 2;
  286. beLate_belate_ex.SubType = "早退";
  287. string thisEndDt = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1))
  288. .AddTicks((date + lastest_time) * 10000000)
  289. .ToString("yyyy-MM-dd HH:mm:ss");
  290. beLate_belate_ex.StartTimeDt = Convert.ToDateTime(thisEndDt);
  291. }
  292. else if (timelength > 60 && timelength <= 180)
  293. {
  294. day_deduction = ConvertToDecimal(day_miner_unit * 6); //3小时
  295. meal_deduction += 10.00M;
  296. absenteeism_deduction += day_deduction; //矿工半日
  297. }
  298. else
  299. {
  300. day_deduction = ConvertToDecimal(dailyWage);
  301. meal_deduction += 10.00M;
  302. absenteeism_deduction += day_deduction; //矿工一日
  303. }
  304. beLate_belate_ex.Deduction = day_deduction;
  305. ex_reissuecard_Items.Add(beLate_belate_ex);
  306. }
  307. else if (exception == 3) //缺卡
  308. {
  309. if (root.exception_infos.Count == 2)
  310. {
  311. if (ex_reissuecard_Items.Where(it => it.StartTimeDt.ToString("yyyy-MM-dd").Equals(thisDtStr)).Count() > 0)
  312. {
  313. if (earliest_time == lastest_time)
  314. {
  315. DateTime thisDt1 = Convert.ToDateTime(thisDtStr + " 12:00");
  316. if (thisDt >= thisDt1) //旷工 下午
  317. {
  318. day_deduction = ConvertToDecimal(day_miner_unit * 9); //4.5小时
  319. meal_deduction += 10.00M;
  320. beLate_belate_ex.Duration = 270;
  321. absenteeism_deduction += day_deduction; //矿工半日
  322. beLate_belate_ex.Reason = thisDtStr + " 下午(18:00)缺卡/未打卡视为下午旷工(4.5小时)";
  323. beLate_belate_ex.Deduction = day_deduction;
  324. ex_reissuecard_Items.Add(beLate_belate_ex);
  325. }
  326. else if (thisDt <= thisDt1) //旷工 上午
  327. {
  328. day_deduction = ConvertToDecimal(day_miner_unit * 6); //3小时
  329. meal_deduction += 10.00M;
  330. absenteeism_deduction += day_deduction; //矿工半日
  331. beLate_belate_ex.Duration = 180;
  332. beLate_belate_ex.Reason = thisDtStr + " 上午(09:00)缺卡/未打卡视为上午旷工(3小时)";
  333. beLate_belate_ex.Deduction = day_deduction;
  334. ex_reissuecard_Items.Add(beLate_belate_ex);
  335. }
  336. else //矿工一日
  337. {
  338. day_deduction = ConvertToDecimal(dailyWage);
  339. meal_deduction += 10.00M;
  340. absenteeism_deduction += day_deduction;
  341. beLate_belate_ex.Reason = thisDtStr + " 上午(09:00)-下午(18:00) 缺卡/未打视为旷工一天(7.5小时)";
  342. beLate_belate_ex.Deduction = day_deduction;
  343. ex_reissuecard_Items.Add(beLate_belate_ex);
  344. }
  345. }
  346. }
  347. else
  348. {
  349. day_deduction = dailyWage;
  350. meal_deduction += 10.00M;
  351. absenteeism_deduction += day_deduction;
  352. beLate_belate_ex.Reason = "上午-下午 缺卡/未打卡为旷工 一天";
  353. beLate_belate_ex.Deduction = day_deduction;
  354. ex_reissuecard_Items.Add(beLate_belate_ex);
  355. }
  356. }
  357. else if (root.exception_infos.Count == 1)
  358. {
  359. if (earliest_time == lastest_time)
  360. {
  361. DateTime thisDt1 = Convert.ToDateTime(thisDtStr + " 12:00");
  362. if (thisDt <= thisDt1) //旷工 下午
  363. {
  364. day_deduction = ConvertToDecimal(day_miner_unit * 9); //4.5小时
  365. meal_deduction += 10.00M;
  366. beLate_belate_ex.Duration = 270;
  367. absenteeism_deduction += day_deduction; //矿工半日
  368. beLate_belate_ex.Reason = thisDtStr + " 下午(18:00)缺卡/未打卡视为下午旷工(4.5小时)";
  369. beLate_belate_ex.Deduction = day_deduction;
  370. ex_reissuecard_Items.Add(beLate_belate_ex);
  371. }
  372. else if (thisDt >= thisDt1) //旷工 上午
  373. {
  374. day_deduction = ConvertToDecimal(day_miner_unit * 6); //3小时
  375. meal_deduction += 10.00M;
  376. absenteeism_deduction += day_deduction; //矿工半日
  377. beLate_belate_ex.Duration = 180;
  378. beLate_belate_ex.Reason = thisDtStr + " 上午(09:00)缺卡/未打卡视为上午旷工(3小时)";
  379. beLate_belate_ex.Deduction = day_deduction;
  380. ex_reissuecard_Items.Add(beLate_belate_ex);
  381. }
  382. else //矿工一日
  383. {
  384. day_deduction = ConvertToDecimal(dailyWage);
  385. meal_deduction += 10.00M;
  386. absenteeism_deduction += day_deduction;
  387. beLate_belate_ex.Reason = thisDtStr + " 上午(09:00)-下午(18:00) 缺卡/未打视为旷工一天(7.5小时)";
  388. beLate_belate_ex.Deduction = day_deduction;
  389. ex_reissuecard_Items.Add(beLate_belate_ex);
  390. }
  391. }
  392. }
  393. }
  394. else if (exception == 4) //旷工
  395. {
  396. if (timelength > 60 && timelength <= 180)
  397. {
  398. day_deduction = ConvertToDecimal(day_miner_unit * 6); //3小时
  399. beLate_belate_ex.Reason = thisDtStr + " 缺卡/未打视为旷工上午(3小时)";
  400. meal_deduction += 10.00M;
  401. absenteeism_deduction += day_deduction; //矿工半日
  402. }
  403. else
  404. {
  405. day_deduction = ConvertToDecimal(dailyWage);
  406. beLate_belate_ex.Reason = thisDtStr + " 上午(09:00)-下午(18:00) 缺卡/未打视为旷工一天(7.5小时)";
  407. meal_deduction += 10.00M;
  408. absenteeism_deduction += day_deduction; //矿工一日
  409. }
  410. beLate_belate_ex.Deduction = day_deduction;
  411. ex_reissuecard_Items.Add(beLate_belate_ex);
  412. }
  413. }
  414. }
  415. #endregion
  416. #region 假勤/补卡次数 审批
  417. int leaveNum = 0; //请假次数
  418. int reissuecardNum = 0; //补卡次数
  419. int evectionNum = 0; //出差次数
  420. //类型:1 - 请假;2 - 补卡;3 - 出差;4 - 外出;100 - 外勤
  421. leaveNum = acc_sp_items.Where(it => it.type == 1).ToList().Count();
  422. reissuecardNum = acc_sp_items.Where(it => it.type == 2).ToList().Count();
  423. List<Sp_items> acc_sp_items_evection = new List<Sp_items>();
  424. acc_sp_items_evection = acc_sp_items.Where(it => it.type == 3).ToList();
  425. evectionNum = acc_sp_items_evection.Count();
  426. //请假审批
  427. if (leaveNum > 0)
  428. {
  429. List<Sp_Detail> sp_leave_details = new List<Sp_Detail>();
  430. sp_leave_details = await _qiYeWeChatApiService.GetApprovalDetailsAsync(startDt, endDt, acctid, 2, 1); //时间段内所有 已同意的 请假 审批数据
  431. if (sp_leave_details.Count <= 0)
  432. {
  433. _result.Msg += startDt + " - " + endDt + " " + itemName + " 请假 审批数据获取未获取到!\r\n";
  434. //continue;
  435. }
  436. List<Ex_Item> ex_ItemInfos = new List<Ex_Item>();
  437. List<LeaveDetails> leaveDetails = new List<LeaveDetails>();
  438. foreach (Sp_Detail sp_item in sp_leave_details)
  439. {
  440. Apply_data? apply_data = sp_item.apply_data;
  441. if (apply_data != null)
  442. {
  443. List<ContentsItem> contents = apply_data.contents;
  444. ContentsItem content_Vacation = contents.Where(it => it.control == "Vacation").FirstOrDefault(); //请假类型
  445. ContentsItem content_Textarea = contents.Where(it => it.control == "Textarea").FirstOrDefault(); //多行文本
  446. if (content_Vacation != null)
  447. {
  448. Vacation vacation = content_Vacation.value.vacation;
  449. Attendance attendance = vacation.attendance; //假勤组件
  450. Selector selector = vacation.selector; //请假类型
  451. List<OptionsItem> optionsItems = selector.options; //key 请假类型 id
  452. List<TitleItem> value = optionsItems[0].value; // value 文本描述值
  453. int leaveType = int.Parse(optionsItems[0].key); //key 请假子类型 id
  454. Date_range date_Range = attendance.date_range;
  455. //筛选 不在工作日内的假勤申请
  456. if (startDt >= date_Range.new_begin_dt || Convert.ToDateTime( date_Range.new_end_dt.ToString("yyyy-MM-dd")) > endDt)
  457. {
  458. continue;
  459. }
  460. string leave_starttime = date_Range.new_begin_dt.ToString("HH:mm");
  461. string leave_endtime = date_Range.new_end_dt.ToString("HH:mm");
  462. string typeName = string.Empty;
  463. string unit = string.Empty;
  464. int leaveTypeId = leaveType;
  465. var leaveTypeData = vacationLeaveTypes.Where(it => it.id == leaveTypeId).FirstOrDefault();
  466. if (leaveTypeData != null) { typeName = leaveTypeData.name; }
  467. string startTime = string.Empty;
  468. string endTime = string.Empty;
  469. string startTime1 = string.Empty;
  470. string endTime1 = string.Empty;
  471. //计算请假类型扣款金额
  472. decimal new_duration = 0.00M;
  473. if (date_Range.type == "halfday")
  474. {
  475. new_duration = Convert.ToDecimal(date_Range.new_duration) / 86400.00M;
  476. unit = "天";
  477. startTime = date_Range.new_begin_dt.ToString("yyyy-MM-dd") + " 09:00";
  478. endTime = date_Range.new_begin_dt.ToString("yyyy-MM-dd") + " 18:00";
  479. if (new_duration == 1)
  480. {
  481. startTime1 = "09:00:00";
  482. endTime1 = "18:00:00";
  483. }
  484. else
  485. {
  486. startTime1 = date_Range.new_begin_dt.ToString("HH:mm:ss");
  487. endTime1 = date_Range.new_end_dt.ToString("HH:mm:ss");
  488. }
  489. }
  490. else if (date_Range.type == "hour")
  491. {
  492. new_duration = Convert.ToDecimal(date_Range.new_duration) / 3600.00M;
  493. unit = "小时";
  494. startTime = date_Range.new_begin_dt.ToString("yyyy-MM-dd HH:mm:ss");
  495. endTime = date_Range.new_end_dt.ToString("yyyy-MM-dd HH:mm:ss");
  496. startTime1 = date_Range.new_begin_dt.ToString("HH:mm:ss");
  497. endTime1 = date_Range.new_end_dt.ToString("HH:mm:ss");
  498. }
  499. LeaveDetails leaveDetails1 = new LeaveDetails()
  500. {
  501. TypeId = leaveType,
  502. TypeName = typeName,
  503. StartDt = Convert.ToDateTime(date_Range.new_begin_dt.ToString("yyyy-MM-dd") + " " + startTime1),
  504. EndDt = Convert.ToDateTime(date_Range.new_end_dt.ToString("yyyy-MM-dd") + " " + endTime1),
  505. DtType = date_Range.type,
  506. Unit = unit,
  507. New_Duration = new_duration,
  508. ApplyDt = Convert.ToDateTime(sp_item.apply_time_dt.ToString("yyyy-MM-dd HH:mm:ss"))
  509. };
  510. leaveDetails.Add(leaveDetails1);
  511. }
  512. }
  513. }
  514. #region 年假测试数据
  515. leaveDetails.Add(new LeaveDetails() {
  516. TypeId = 1,
  517. TypeName = "年假",
  518. StartDt = Convert.ToDateTime("2023-11-21 09:00:00"),
  519. EndDt = Convert.ToDateTime("2023-11-21 12:00:00"),
  520. DtType = "halfday",
  521. Unit = "天",
  522. New_Duration = 0.50M
  523. });
  524. leaveDetails.Add(new LeaveDetails()
  525. {
  526. TypeId = 1,
  527. TypeName = "年假",
  528. StartDt = Convert.ToDateTime("2023-11-21 12:00:00"),
  529. EndDt = Convert.ToDateTime("2023-11-23 18:00:00"),
  530. DtType = "halfday",
  531. Unit = "天",
  532. New_Duration = 2.50M
  533. });
  534. #endregion
  535. if (leaveDetails.Count > 0)
  536. {
  537. foreach (LeaveDetails leave_item in leaveDetails)
  538. {
  539. decimal leave_meals = 0.00M;
  540. decimal thisTypeDeduction = 0.00M;//当前类型扣款
  541. int leaveType = leave_item.TypeId;
  542. decimal new_duration = leave_item.New_Duration;
  543. //计算餐补 假勤类型扣款
  544. CalculateTypeFee(leaveDetails,leaveType, leave_item.DtType, leave_item.StartDt, leave_item.EndDt, amountPayable, work_days,
  545. new_duration,out leave_meals, out thisTypeDeduction);
  546. #region 累计类型扣款
  547. //1年假;2事假;3病假;4调休假;5婚假;6产假;7陪产假;8其他;9丧假
  548. if (leaveType == 2) //调休假
  549. {
  550. personalLeaveTotal += thisTypeDeduction;
  551. }
  552. else if (leaveType == 3) //病假
  553. {
  554. sickLeaveTotal = thisTypeDeduction;
  555. }
  556. #endregion
  557. meal_deduction += leave_meals;
  558. Ex_Item ex_Item = new Ex_Item()
  559. {
  560. SubTypeId = leaveType,
  561. SubType = leave_item.TypeName,
  562. StartTimeDt = leave_item.StartDt,
  563. EndTimeDt = leave_item.EndDt,
  564. Duration = new_duration,
  565. Unit = leave_item.Unit,
  566. Deduction = thisTypeDeduction,
  567. //Reason = apply_data.reason,
  568. Apply_time_dt = leave_item.ApplyDt,
  569. //Approval_name = sp_item.approval_name,
  570. };
  571. ex_ItemInfos.Add(ex_Item);
  572. }
  573. }
  574. if (ex_ItemInfos.Count > 0)
  575. {
  576. ex_Items_jq.Ex_ItemInfo = ex_ItemInfos.OrderBy(it => it.StartTimeDt).ThenBy(it => it.Apply_time_dt).ToList();
  577. ex_Items.Add(ex_Items_jq);
  578. }
  579. }
  580. //打卡补卡
  581. //补卡:员工发现自己漏打卡时,需及时提起补卡申请,并说明情况。
  582. //试用期员工每月有 2 次补卡机会,超过 2 次不足 5 次的部分,按 10 元/次处罚,5 次及以上的漏卡,按 50 元/次处罚;
  583. //正式员工每月 3 次以内的补卡,按 10 元/次处罚,3 次及以上的漏卡,按 50 元/次处罚。
  584. if (reissuecardNum > 0)
  585. {
  586. List<Sp_Detail> sp_buka_details = new List<Sp_Detail>();
  587. sp_buka_details = await _qiYeWeChatApiService.GetApprovalDetailsAsync(startDt, endDt, acctid, 2, 2); //时间段内所有 已同意的 请假 审批数据
  588. int bukaNum = 1;
  589. foreach (var item in sp_buka_details)
  590. {
  591. Apply_data? apply_data = item.apply_data;
  592. if (apply_data != null)
  593. {
  594. List<ContentsItem> contents = apply_data.contents;
  595. ContentsItem content_Vacation = contents.Where(it => it.control == "PunchCorrection").FirstOrDefault(); //请假类型
  596. ContentsItem content_Textarea = contents.Where(it => it.control == "Textarea").FirstOrDefault(); //多行文本
  597. if (content_Vacation != null)
  598. {
  599. var punch_correction = content_Vacation.value.punch_correction;
  600. DateTime bukaDt = punch_correction.time_dt;
  601. DateTime bukaDtJudge = Convert.ToDateTime(bukaDt.ToString("yyyy-MM-dd"));
  602. //筛选 不在工作日内的假勤申请
  603. if (startDt > bukaDtJudge || bukaDtJudge > endDt)
  604. {
  605. continue;
  606. }
  607. decimal bukaPrice = 0.00M;
  608. if (user_probationary_bk_decimal == 0) //计算试用员工补卡次数
  609. {
  610. if (bukaNum <= 2) bukaPrice = 0.00M;
  611. else if (bukaNum <= 4 && bukaNum > 2) bukaPrice = 10.00M;
  612. else bukaPrice = 50.00M;
  613. }
  614. else //计算正式员工补卡次数
  615. {
  616. if (bukaNum <= 2) bukaPrice = 10.00M;
  617. else bukaPrice = 50.00M;
  618. }
  619. var app_data = item.apply_data;
  620. var punch_correction1 = app_data.contents[0].value.punch_correction; //未打卡时间
  621. var punch_correction2 = app_data.contents[1].value;
  622. Ex_Item ex_reissueCard = new Ex_Item()
  623. {
  624. SubTypeId = 7,
  625. SubType = "打卡补卡",
  626. StartTimeDt = Convert.ToDateTime(punch_correction1.time_dt.ToString("yyyy-MM-dd HH:mm:ss")), //未打卡时间
  627. Deduction = bukaPrice,
  628. Reason = punch_correction2.text,
  629. Unit = string.Empty
  630. };
  631. unprinted_deduction += bukaPrice;
  632. ex_reissuecard_Items.Add(ex_reissueCard);
  633. bukaNum++;
  634. }
  635. }
  636. }
  637. //外出审批 已通过的数据 且 外出未打卡
  638. List<Sp_Detail> sp_goout_details = new List<Sp_Detail>();
  639. sp_goout_details = await _qiYeWeChatApiService.GetApprovalDetailsAsync(startDt, endDt, acctid, 2, 4); //时间段内所有 已同意的 外出 审批数据
  640. if (sp_goout_details.Count > 0)
  641. {
  642. //找出外出的具体日期
  643. List<string> goOutDts = new List<string>();
  644. foreach (var goOutItem in sp_goout_details)
  645. {
  646. Apply_data? apply_data = goOutItem.apply_data;
  647. if (apply_data != null)
  648. {
  649. List<ContentsItem> contents = apply_data.contents;
  650. ContentsItem content_Vacation = contents.Where(it => it.control == "Attendance").FirstOrDefault(); //请假类型
  651. ContentsItem content_Textarea = contents.Where(it => it.control == "Textarea").FirstOrDefault(); //多行文本
  652. if (content_Vacation != null)
  653. {
  654. var attendance = content_Vacation.value.attendance;
  655. DateTime goOutDt = attendance.date_range.new_begin_dt;
  656. DateTime goOutDtJudge = Convert.ToDateTime(goOutDt.ToString("yyyy-MM-dd"));
  657. //筛选 不在工作日内的假勤申请
  658. if (startDt >= goOutDtJudge || goOutDtJudge > endDt)
  659. {
  660. continue;
  661. }
  662. goOutDts.Add(goOutDt.ToString("yyyy-MM-dd"));
  663. }
  664. }
  665. }
  666. //处理日期范围内的数据
  667. List<CheckInDataInfo> filtratePrefix_checkInData = new List<CheckInDataInfo>();
  668. if (goOutDts.Count > 0)
  669. {
  670. foreach (var goOutDtItem in goOutDts)
  671. {
  672. List<CheckInDataInfo> filtrate_checkInData = new List<CheckInDataInfo>();
  673. filtrate_checkInData = checkInData2.Where(it => it.checkin_time_dt.ToString("yyyy-MM-dd").Equals(goOutDtItem)).ToList();
  674. if (filtrate_checkInData.Count > 0)
  675. {
  676. filtratePrefix_checkInData.AddRange(filtrate_checkInData);
  677. }
  678. }
  679. }
  680. //打卡记录里抓取的未打卡数据
  681. foreach (var item in filtratePrefix_checkInData)
  682. {
  683. decimal bukaPrice = 0.00M;
  684. if (user_probationary_bk_decimal == 0) //计算试用员工补卡次数
  685. {
  686. if (bukaNum <= 2) bukaPrice = 0.00M;
  687. else if (bukaNum <= 4 && bukaNum > 2) bukaPrice = 10.00M;
  688. else bukaPrice = 50.00M;
  689. }
  690. else //计算正式员工补卡次数
  691. {
  692. if (bukaNum <= 2) bukaPrice = 10.00M;
  693. else bukaPrice = 50.00M;
  694. }
  695. Ex_Item ex_reissueCard = new Ex_Item()
  696. {
  697. SubTypeId = 7,
  698. SubType = "打卡补卡",
  699. StartTimeDt = Convert.ToDateTime(item.checkin_time_dt.ToString("yyyy-MM-dd HH:mm:ss")), //未打卡时间
  700. Deduction = bukaPrice,
  701. Reason = "[打卡记录]抓取到的未打卡数据!",
  702. Unit = string.Empty
  703. };
  704. unprinted_deduction += bukaPrice;
  705. ex_reissuecard_Items.Add(ex_reissueCard);
  706. bukaNum++;
  707. }
  708. }
  709. }
  710. if (ex_reissuecard_Items.Count > 0)
  711. {
  712. ex_Items_dk.Ex_ItemInfo = ex_reissuecard_Items;
  713. ex_Items_dk.Ex_ItemInfo = ex_reissuecard_Items.OrderBy(it => it.SubTypeId).ThenBy(it => it.StartTimeDt).ToList();
  714. ex_Items.Add(ex_Items_dk);
  715. }
  716. //出差申请
  717. if (evectionNum > 0)
  718. {
  719. List<Sp_Detail> sp_leave_details = new List<Sp_Detail>();
  720. sp_leave_details = await _qiYeWeChatApiService.GetApprovalDetailsAsync(startDt, endDt, acctid, 2, 3); //时间段内所有 已同意的 出差 审批数据
  721. if (sp_leave_details.Count <= 0)
  722. {
  723. _result.Msg += startDt + " - " + endDt + " " + itemName + " 请假 审批数据获取未获取到!\r\n";
  724. //continue;
  725. }
  726. List<Ex_Item> cc_ex_ItemInfos = new List<Ex_Item>();
  727. foreach (Sp_Detail sp_item in sp_leave_details)
  728. {
  729. Apply_data? apply_data = sp_item.apply_data;
  730. if (apply_data != null)
  731. {
  732. List<ContentsItem> contents = apply_data.contents;
  733. ContentsItem content_Vacation = contents.Where(it => it.control == "Attendance").FirstOrDefault(); //出差类型
  734. ContentsItem content_Textarea = contents.Where(it => it.control == "Textarea").FirstOrDefault(); //多行文本
  735. if (content_Vacation != null)
  736. {
  737. //Vacation vacation = content_Vacation.value.vacation;
  738. Attendance attendance = content_Vacation.value.attendance; //假勤组件
  739. //Selector selector = vacation.selector; //请假类型
  740. //List<OptionsItem> optionsItems = selector.options; //key 请假类型 id
  741. //List<TitleItem> value = optionsItems[0].value; // value 文本描述值
  742. int leaveType = int.Parse("3"); //key 请假子类型 id
  743. Date_range date_Range = attendance.date_range;
  744. //筛选 不在工作日内的假勤申请
  745. if (startDt >= date_Range.new_begin_dt || Convert.ToDateTime(date_Range.new_end_dt.ToString("yyyy-MM-dd")) > endDt)
  746. {
  747. continue;
  748. }
  749. string leave_starttime = date_Range.new_begin_dt.ToString("HH:mm");
  750. string leave_endtime = date_Range.new_end_dt.ToString("HH:mm");
  751. string typeName = string.Empty;
  752. string unit = string.Empty;
  753. int leaveTypeId = leaveType;
  754. typeName = "出差";
  755. string startTime = string.Empty;
  756. string endTime = string.Empty;
  757. string startTime1 = string.Empty;
  758. string endTime1 = string.Empty;
  759. //计算请假类型扣款金额
  760. decimal new_duration = 0.00M;
  761. if (date_Range.type == "halfday")
  762. {
  763. new_duration = Convert.ToDecimal(date_Range.new_duration) / 86400.00M;
  764. unit = "天";
  765. startTime = date_Range.new_begin_dt.ToString("yyyy-MM-dd") + " 09:00";
  766. endTime = date_Range.new_begin_dt.ToString("yyyy-MM-dd") + " 18:00";
  767. startTime1 = "09:00:00";
  768. endTime1 = "18:00:00";
  769. }
  770. else if (date_Range.type == "hour")
  771. {
  772. new_duration = Convert.ToDecimal(date_Range.new_duration) / 3600.00M;
  773. unit = "小时";
  774. startTime = date_Range.new_begin_dt.ToString("yyyy-MM-dd HH:mm:ss");
  775. endTime = date_Range.new_end_dt.ToString("yyyy-MM-dd HH:mm:ss");
  776. startTime1 = date_Range.new_begin_dt.ToString("HH:mm:ss");
  777. endTime1 = date_Range.new_end_dt.ToString("HH:mm:ss");
  778. }
  779. //出差扣款
  780. decimal cckk = 0.00M;
  781. int days = (int)(Convert.ToDateTime( date_Range.new_end_dt.ToString("yyyy-MM-dd")) -
  782. Convert.ToDateTime(date_Range.new_begin_dt.ToString("yyyy-MM-dd"))).TotalDays;
  783. for (int i = 0; i <= days; i++)
  784. {
  785. DateTime thisDt = date_Range.new_begin_dt.AddDays(i);
  786. //处理开始时间 是否在当天工作时间内
  787. if (i == 0) //开始时间
  788. {
  789. string ccStartTime = thisDt.ToString("HH:mm:ss");
  790. if (!IsWorkTime(ccStartTime))
  791. {
  792. continue;
  793. }
  794. }
  795. //处理结束时间 是否在当天工作时间内
  796. if (i == days - 1)
  797. {
  798. string ccStartTime = date_Range.new_end_dt.ToString("HH:mm:ss");
  799. if (!IsWorkTime(ccStartTime))
  800. {
  801. continue;
  802. }
  803. }
  804. Sys_Calendar sys_Calendar = new Sys_Calendar();
  805. sys_Calendar = sys_Calendars.Where(it => it.Dt == thisDt.ToString("yyyy-MM-dd")).FirstOrDefault();
  806. if (sys_Calendar != null) {
  807. if (sys_Calendar.IsWorkDay)
  808. {
  809. cckk += 10.00M;
  810. }
  811. }
  812. }
  813. meal_deduction += cckk;
  814. Ex_Item ex_Item = new Ex_Item()
  815. {
  816. SubTypeId = leaveType,
  817. SubType = typeName,
  818. StartTimeDt = Convert.ToDateTime(date_Range.new_begin_dt.ToString("yyyy-MM-dd") + " " + startTime1),
  819. EndTimeDt = Convert.ToDateTime(date_Range.new_end_dt.ToString("yyyy-MM-dd") + " " + endTime1),
  820. Duration = new_duration,
  821. Unit = unit,
  822. Deduction = 0.00M,
  823. //Reason = apply_data.reason,
  824. Apply_time_dt = Convert.ToDateTime(sp_item.apply_time_dt.ToString("yyyy-MM-dd HH:mm:ss"))
  825. //Approval_name = sp_item.approval_name,
  826. };
  827. cc_ex_ItemInfos.Add(ex_Item);
  828. }
  829. }
  830. }
  831. if (cc_ex_ItemInfos.Count > 0)
  832. {
  833. //ex_Items_cc.Ex_ItemInfo = cc_ex_ItemInfos.OrderBy(it => it.StartTimeDt).ThenBy(it => it.Apply_time_dt).ToList();
  834. //ex_Items.Add(ex_Items_cc);
  835. List<Ex_Item> ex_jq_Items = new List<Ex_Item>();
  836. ex_jq_Items = (List<Ex_Item>)ex_Items_jq.Ex_ItemInfo;
  837. ex_jq_Items.AddRange(cc_ex_ItemInfos);
  838. ex_Items.Remove(ex_Items_jq);
  839. ex_Items.Add(
  840. new Ex_Items()
  841. {
  842. Type = "假勤",
  843. Ex_ItemInfo = ex_jq_Items.OrderBy(it => it.StartTimeDt).ThenBy(it => it.Apply_time_dt).ToList()
  844. }
  845. );
  846. }
  847. }
  848. #endregion
  849. }
  850. else
  851. {
  852. meal_subsidy = work_days * 10.00M;
  853. }
  854. #region 应发合计 实发合计 扣款合计(假勤扣款,其他扣款,社保扣款,公积金代扣,个税扣款)
  855. decimal mealTotal = meal_subsidy - meal_deduction; //餐补
  856. decimal salaryTotal = 0.00M;
  857. if (dk_work_days >= work_days)
  858. {
  859. dk_work_days = work_days;
  860. salaryTotal = amountPayable + mealTotal; //应发合计
  861. }
  862. else
  863. {
  864. if (itemName.Equals("张海麟"))
  865. {
  866. salaryTotal = amountPayable + mealTotal; //应发合计
  867. }
  868. else
  869. {
  870. salaryTotal = (dk_work_days * dailyWage) + mealTotal; //应发合计
  871. }
  872. }
  873. //扣款合计 不含个税
  874. decimal eductionTotal = sickLeaveTotal + personalLeaveTotal + beLate_deduction + early_deduction + absenteeism_deduction + unprinted_deduction + other_deduction +
  875. pm_wsInfo.WithholdingInsurance + pm_wsInfo.ReservedFunds + pm_wsInfo.OtherDeductions;
  876. decimal actualReleaseTotal = salaryTotal - eductionTotal; //实发合计 * 不含个税
  877. #endregion
  878. #region 处理当月工资数据
  879. pm_wsInfo.YearMonth = thisYearMonth;
  880. pm_wsInfo.StartDate = startDt.ToString("yyyy-MM-dd");
  881. pm_wsInfo.EndDate = endDt.ToString("yyyy-MM-dd");
  882. pm_wsInfo.WorkDays = work_days; //当月应出勤天数
  883. pm_wsInfo.RegularDays = dk_work_days; //当月正常出勤天数
  884. pm_wsInfo.SickLeave = sickLeaveTotal; //病假
  885. pm_wsInfo.SomethingFalse = personalLeaveTotal; //事假
  886. pm_wsInfo.LateTo = beLate_deduction; //迟到
  887. pm_wsInfo.LeaveEarly = early_deduction; //早退
  888. pm_wsInfo.Absenteeism = absenteeism_deduction; //旷工
  889. pm_wsInfo.NotPunch = unprinted_deduction; //未打卡
  890. pm_wsInfo.OtherDeductions = other_deduction; //其他
  891. pm_wsInfo.Ex_ItemsRemark = JsonConvert.SerializeObject(ex_Items); //
  892. pm_wsInfo.Mealsupplement = mealTotal; //餐补
  893. pm_wsInfo.Should = ConvertToDecimal( salaryTotal); //应发合计
  894. pm_wsInfo.TotalDeductions = ConvertToDecimal(eductionTotal); //扣款合计
  895. pm_wsInfo.TotalRealHair = ConvertToDecimal(actualReleaseTotal - pm_wsInfo.WithholdingTax); //实发合计
  896. pm_wsInfo.AfterTax = ConvertToDecimal(actualReleaseTotal - pm_wsInfo.WithholdingTax); //税后工资
  897. pm_wsInfo.LastUpdateUserId = userId;
  898. pm_wsInfo.LastUpdateDt = DateTime.Now;
  899. pm_wsInfo.CreateUserId = userId;
  900. pm_wsInfo.CreateTime = DateTime.Now;
  901. pm_wsInfo.DeleteUserId = null;
  902. pm_wsInfo.DeleteTime = null;
  903. #endregion
  904. }
  905. }
  906. catch (Exception ex)
  907. {
  908. _result.Msg = ex.Message;
  909. return _result;
  910. }
  911. _result.Code = 0;
  912. _result.Data = pm_WageSheetDattaSources;
  913. return _result;
  914. }
  915. /// <summary>
  916. /// 是否在工作时间内
  917. /// </summary>
  918. /// <param name="timeStr"></param>
  919. /// <returns></returns>
  920. public static bool IsWorkTime(string timeStr)
  921. {
  922. string thisDayWorkStartTime = "09:00:00";
  923. string thisDayWorkEndTime = "18:00:00";
  924. TimeSpan dspWorkingDayAM = DateTime.Parse(thisDayWorkStartTime).TimeOfDay;
  925. TimeSpan dspWorkingDayPM = DateTime.Parse(thisDayWorkEndTime).TimeOfDay;
  926. DateTime dt = Convert.ToDateTime(timeStr);
  927. TimeSpan dspNow = dt.TimeOfDay;
  928. if (dspNow >= dspWorkingDayAM && dspNow <= dspWorkingDayPM)
  929. {
  930. return true;
  931. }
  932. return false;
  933. }
  934. /// <summary>
  935. /// decimal 保留两位小数 不四舍五入
  936. /// </summary>
  937. /// <param name="number"></param>
  938. /// <returns></returns>
  939. public static decimal ConvertToDecimal(decimal myDecimal)
  940. {
  941. var subDecimal = Math.Floor(myDecimal * 100) / 100;//保留两位小数,直接截取
  942. return subDecimal;
  943. }
  944. /// <summary>
  945. /// decimal 保留两位小数 不四舍五入
  946. /// </summary>
  947. /// <param name="number"></param>
  948. /// <returns></returns>
  949. public static decimal ConvertToDecimal1(this decimal myDecimal)
  950. {
  951. var subDecimal = Math.Floor(myDecimal * 100) / 100;//保留两位小数,直接截取
  952. return subDecimal;
  953. }
  954. /// <summary>
  955. /// 列表的成员移动到列表的前面
  956. /// </summary>
  957. /// <typeparam name="T"></typeparam>
  958. /// <param name="list"></param>
  959. /// <param name="index"></param>
  960. public static void MoveItemAtIndexToFront<T>(this List<T> list, int index)
  961. {
  962. T item = list[index];
  963. list.RemoveAt(index);
  964. list.Insert(0, item);
  965. }
  966. /// <summary>
  967. /// 获取请假类型
  968. /// </summary>
  969. /// <param name="template_id"></param>
  970. /// <returns></returns>
  971. public static async Task<List<VacationLeaveTypeView>> GetVacationLeaveTypes(string template_id)
  972. {
  973. List<VacationLeaveTypeView> vacationLeaveTypes = new List<VacationLeaveTypeView>();
  974. TemplateDetailView templateDetailView = new TemplateDetailView();
  975. templateDetailView = await _qiYeWeChatApiService.GetTemplateDetailAsync(template_id);
  976. if (templateDetailView.errcode != 0)
  977. {
  978. Serilog.Log.Error("【企业微信】【审批】【获取假勤类型的审批】【Msg】"+ templateDetailView.errmsg);
  979. return vacationLeaveTypes;
  980. }
  981. List<VacationItemInfo> VacationItemInfos = templateDetailView.vacation_list.item;
  982. foreach (var item in VacationItemInfos)
  983. {
  984. ValueItem valueInfo = item.name.Where(it => it.lang == "zh_CN").FirstOrDefault();
  985. if (valueInfo != null)
  986. {
  987. vacationLeaveTypes.Add(
  988. new VacationLeaveTypeView()
  989. {
  990. id = item.id,
  991. name = valueInfo.text
  992. });
  993. }
  994. }
  995. return vacationLeaveTypes;
  996. }
  997. /// <summary>
  998. /// 计算类型费用
  999. /// </summary>
  1000. /// <param name="leaveType">
  1001. /// 1年假;2事假;3病假;4调休假;5婚假;6产假;7陪产假;8其他;9丧假
  1002. /// </param>
  1003. /// <param name="date_Range_type">
  1004. /// halfday 全天
  1005. /// hour 小时
  1006. /// </param>
  1007. /// <param name="startTime"></param>
  1008. /// <param name="endTime"></param>
  1009. /// <param name="duration"></param>
  1010. /// <param name="mealDeduction"></param>
  1011. /// <param name="typeDeduction"></param>
  1012. public static void CalculateTypeFee(List<LeaveDetails> leaveDetails,int leaveType, string date_Range_type, DateTime startTime, DateTime endTime,
  1013. decimal amountPayable,int work_days, decimal duration, out decimal mealDeduction, out decimal typeDeduction)
  1014. {
  1015. typeDeduction = 0;
  1016. mealDeduction = 0;
  1017. string am_starttime = "08:59";
  1018. string am_endtime = "13:01";
  1019. //string startTime = startTime1.ToString("HH:mm:ss");
  1020. //string endTime = endTime1.ToString("HH:mm:ss");
  1021. decimal personalkLeave_dailywage_day = amountPayable / work_days; //日薪 = 事假日薪 *计算方式:日平均工资 = 当月应发工资 /当月应出勤天数。
  1022. //半小时单位
  1023. decimal halfHour = 7.50M / 0.50M;
  1024. switch (leaveType)
  1025. {
  1026. case 1: //年假
  1027. if (date_Range_type == "halfday")
  1028. {
  1029. if (duration >= 0.5M && duration <= 1M) //一天
  1030. {
  1031. var njItem = leaveDetails.Where(it => it.StartDt.ToString("yyyy-MM-dd") == startTime.ToString("yyyy-MM-dd") &&
  1032. it.EndDt.ToString("yyyy-MM-dd") == endTime.ToString("yyyy-MM-dd")).ToList();
  1033. if (njItem.Count > 1)
  1034. {
  1035. if (njItem[0].StartDt == startTime)
  1036. {
  1037. mealDeduction = 10; //餐补扣款
  1038. }
  1039. }
  1040. else
  1041. {
  1042. mealDeduction = 10; //餐补扣款
  1043. }
  1044. }
  1045. else // 多天
  1046. {
  1047. var njManyDaysItem = leaveDetails.Where(it => it.StartDt.ToString("yyyy-MM-dd") == startTime.ToString("yyyy-MM-dd")).ToList();
  1048. if (njManyDaysItem.Count > 1)
  1049. {
  1050. mealDeduction = 10 * Math.Floor(duration);
  1051. }
  1052. else
  1053. {
  1054. mealDeduction = 10 * Math.Ceiling(duration);
  1055. }
  1056. }
  1057. }
  1058. break;
  1059. case 2: //2事假
  1060. // 事假日薪 *计算方式:日平均工资 = 当月应发工资 /当月应出勤天数。
  1061. decimal personalkLeave_dailywage_halfhour = personalkLeave_dailywage_day / halfHour; //事假单位 0.5小时
  1062. if (date_Range_type == "halfday")
  1063. {
  1064. mealDeduction = 10; //餐补扣款
  1065. typeDeduction = ConvertToDecimal(personalkLeave_dailywage_day);
  1066. }
  1067. else if (date_Range_type == "hour")
  1068. {
  1069. decimal leave_halfHour = Convert.ToDecimal(duration) / Convert.ToDecimal(0.5);
  1070. typeDeduction = ConvertToDecimal(personalkLeave_dailywage_halfhour * leave_halfHour);
  1071. //duration = 11M;
  1072. if (duration >= 3 && duration < 7.5M) //单天请假三小时
  1073. {
  1074. mealDeduction = 10; //餐补扣款
  1075. }
  1076. else if (duration >= 7.5M) //多天计算
  1077. {
  1078. decimal leave_halfHour1 = Convert.ToDecimal(duration) / Convert.ToDecimal(0.5);
  1079. decimal leaveDays = duration / 7.5M;
  1080. if (leaveDays % 1 == 0)
  1081. {
  1082. typeDeduction = ConvertToDecimal(personalkLeave_dailywage_day * leaveDays);
  1083. mealDeduction = 10 * leaveDays; //餐补扣款
  1084. }
  1085. else
  1086. {
  1087. typeDeduction = personalkLeave_dailywage_day * Convert.ToInt32(leaveDays);
  1088. decimal sy_shijiaunit = leave_halfHour1 - Convert.ToDecimal (15.00M * Convert.ToInt32(leaveDays));
  1089. if (sy_shijiaunit > 0)
  1090. {
  1091. typeDeduction += ConvertToDecimal(personalkLeave_dailywage_halfhour * sy_shijiaunit);
  1092. }
  1093. mealDeduction = 10 * Convert.ToInt32(leaveDays);
  1094. //得到最后一天的请假时间 是否有餐补
  1095. int lastHours = (Convert.ToDateTime(endTime) - Convert.ToDateTime("09:00")).Hours;
  1096. if (lastHours >= 3)
  1097. {
  1098. mealDeduction += 10; //餐补扣款
  1099. }
  1100. }
  1101. }
  1102. }
  1103. break;
  1104. case 3: //3病假
  1105. // 病假日薪 *计算方式:日平均工资 = 成都市最低工资标准的80% /当月应出勤天数。 短期病假=当月15天内
  1106. decimal chengDuMinimumWage_halrHour = _chengDuMinimumWage / work_days / halfHour;
  1107. decimal sickLeave_dailywage_halfhour_deduction1 = (personalkLeave_dailywage_day / halfHour) - chengDuMinimumWage_halrHour; //病假单位 0.5小时 扣款金额
  1108. if (date_Range_type == "halfday")
  1109. {
  1110. mealDeduction = 10; //餐补扣款
  1111. typeDeduction = ConvertToDecimal( sickLeave_dailywage_halfhour_deduction1 * halfHour);
  1112. }
  1113. else if (date_Range_type == "hour")
  1114. {
  1115. decimal sickLeave_halfHour = duration / 0.5M;
  1116. typeDeduction = ConvertToDecimal(sickLeave_dailywage_halfhour_deduction1 * sickLeave_halfHour);
  1117. if (duration >= 3 && duration < 7.5M) //单天请假三小时 && 请假时间在上午 则没有餐补
  1118. {
  1119. mealDeduction = 10; //餐补扣款
  1120. }
  1121. else if (duration >= 7.5M) //多天计算
  1122. {
  1123. decimal sickLeave_halfHour1 = duration / 0.5M;
  1124. decimal leaveDays = Convert.ToDecimal(duration / 7.5M);
  1125. typeDeduction = ConvertToDecimal(sickLeave_dailywage_halfhour_deduction1 * sickLeave_halfHour1);
  1126. if (leaveDays % 1 == 0)
  1127. {
  1128. mealDeduction = 10 * leaveDays; //餐补扣款
  1129. }
  1130. else
  1131. {
  1132. mealDeduction = 10 * Convert.ToInt32(leaveDays);
  1133. typeDeduction = ConvertToDecimal(sickLeave_dailywage_halfhour_deduction1 * Convert.ToInt32(leaveDays));
  1134. decimal sy_bingjiaunit = sickLeave_halfHour1 - Convert.ToDecimal(15.00M * Convert.ToInt32(leaveDays));
  1135. if (sy_bingjiaunit > 0)
  1136. {
  1137. typeDeduction += ConvertToDecimal(sickLeave_dailywage_halfhour_deduction1 * sy_bingjiaunit);
  1138. }
  1139. //得到最后一天的请假时间 是否有餐补
  1140. int lastHours = (Convert.ToDateTime(endTime) - Convert.ToDateTime("09:00")).Hours;
  1141. if (lastHours >= 3)
  1142. {
  1143. mealDeduction += 10; //餐补扣款
  1144. }
  1145. }
  1146. }
  1147. }
  1148. break;
  1149. case 4: //4调休假
  1150. CalculateTypeFeeSub(leaveDetails,date_Range_type, startTime, endTime, duration, out mealDeduction);
  1151. break;
  1152. case 5: //5婚假
  1153. CalculateTypeFeeSub(leaveDetails,date_Range_type, startTime, endTime, duration, out mealDeduction);
  1154. break;
  1155. case 6: //6产假
  1156. CalculateTypeFeeSub(leaveDetails,date_Range_type, startTime, endTime, duration, out mealDeduction);
  1157. break;
  1158. case 7: //7陪产假
  1159. CalculateTypeFeeSub(leaveDetails,date_Range_type, startTime, endTime, duration, out mealDeduction);
  1160. break;
  1161. case 8: //8其他
  1162. CalculateTypeFeeSub(leaveDetails,date_Range_type, startTime, endTime, duration, out mealDeduction);
  1163. break;
  1164. case 9: //9丧假
  1165. CalculateTypeFeeSub(leaveDetails,date_Range_type, startTime, endTime, duration, out mealDeduction);
  1166. break;
  1167. }
  1168. }
  1169. /// <summary>
  1170. /// 计算类型费用
  1171. /// </summary>
  1172. /// <param name="date_Range_type">
  1173. /// halfday 全天
  1174. /// hour 小时
  1175. /// </param>
  1176. /// <param name="startTime"></param>
  1177. /// <param name="endTime"></param>
  1178. /// <param name="duration"></param>
  1179. /// <param name="mealDeduction"></param>
  1180. public static void CalculateTypeFeeSub(List<LeaveDetails> leaveDetails,string date_Range_type, DateTime startTime1, DateTime endTime1,
  1181. decimal duration, out decimal mealDeduction)
  1182. {
  1183. mealDeduction = 0;
  1184. string am_starttime = "09:00";
  1185. string am_endtime = "11:59";
  1186. string startTime = startTime1.ToString("HH:mm:ss");
  1187. string endTime = endTime1.ToString("HH:mm:ss");
  1188. if (date_Range_type == "halfday")
  1189. {
  1190. mealDeduction = 10; //餐补扣款
  1191. }
  1192. else if (date_Range_type == "hour")
  1193. {
  1194. List<LeaveDetails> leaveDetails1 = new List<LeaveDetails>();
  1195. leaveDetails1 = leaveDetails.Where(it => it.StartDt.ToString("yyyy-MM-dd").Equals(startTime1.ToString("yyyy-MM-dd")))
  1196. .OrderBy(it => it.StartDt).ToList();
  1197. if (leaveDetails1.Count > 1)
  1198. {
  1199. decimal timelenTatoal = leaveDetails1.Sum(it => it.New_Duration);
  1200. if (timelenTatoal >= 3.00M)
  1201. {
  1202. if (leaveDetails1[0].StartDt == startTime1)
  1203. {
  1204. mealDeduction = 10; //餐补扣款
  1205. }
  1206. }
  1207. }
  1208. else
  1209. {
  1210. if (duration >= 3 && duration < 7) //单天请假三小时 && 请假时间在上午 则没有餐补
  1211. {
  1212. mealDeduction = 10; //餐补扣款
  1213. }
  1214. else if (duration >= 7 && duration <= 7.50M)
  1215. {
  1216. mealDeduction = 10; //餐补扣款
  1217. }
  1218. else if (duration >= 7.50M) //多天计算
  1219. {
  1220. decimal leaveDays = Convert.ToDecimal(duration / 7.50M);
  1221. if (leaveDays % 1 == 0)
  1222. {
  1223. mealDeduction = 10 * leaveDays; //餐补扣款
  1224. }
  1225. else
  1226. {
  1227. mealDeduction = 10 * Convert.ToInt32(leaveDays);
  1228. //得到最后一天的请假时间 是否有餐补
  1229. int lastHours = (Convert.ToDateTime(endTime) - Convert.ToDateTime("09:00")).Hours;
  1230. if (lastHours >= 3)
  1231. {
  1232. mealDeduction += 10; //餐补扣款
  1233. ////处理结束时间
  1234. //if (endTime.CompareTo(am_starttime) > 0 && endTime.CompareTo(am_endtime) < 0)
  1235. //{
  1236. // mealDeduction += 10; //餐补扣款
  1237. //}
  1238. }
  1239. }
  1240. }
  1241. }
  1242. }
  1243. }
  1244. /// <summary>
  1245. /// 获取打卡补卡类型
  1246. /// </summary>
  1247. /// <param name="template_id"></param>
  1248. /// <returns></returns>
  1249. public static async Task<List<VacationLeaveTypeView>> GetVacationReissueCardTypes(string template_id)
  1250. {
  1251. List<VacationLeaveTypeView> vacationLeaveTypes = new List<VacationLeaveTypeView>();
  1252. TemplateDetailView templateDetailView = new TemplateDetailView();
  1253. templateDetailView = await _qiYeWeChatApiService.GetTemplateDetailAsync(template_id);
  1254. if (templateDetailView.errcode != 0)
  1255. {
  1256. return vacationLeaveTypes;
  1257. }
  1258. List<VacationItemInfo> VacationItemInfos = templateDetailView.vacation_list.item;
  1259. foreach (var item in VacationItemInfos)
  1260. {
  1261. ValueItem valueInfo = item.name.Where(it => it.lang == "zh_CN").FirstOrDefault();
  1262. if (valueInfo != null)
  1263. {
  1264. vacationLeaveTypes.Add(
  1265. new VacationLeaveTypeView()
  1266. {
  1267. id = item.id,
  1268. name = valueInfo.text
  1269. });
  1270. }
  1271. }
  1272. return vacationLeaveTypes;
  1273. }
  1274. /// <summary>
  1275. /// 打卡数据
  1276. /// 假勤数据 统计
  1277. /// </summary>
  1278. /// <param name="datas">数据源</param>
  1279. /// <param name="type">
  1280. /// 1-请假;2-补卡;3-出差;4-外出;100-外勤;
  1281. /// </param>
  1282. /// <param name="subTypeName">
  1283. /// 年假 事假 病假 调休假 婚嫁 产假 陪产假 丧假 补卡次数 出差 外出数 外勤 其他
  1284. /// </param>
  1285. /// <returns></returns>
  1286. private static int Fallibilitydispose(List<Sp_Item> datas, int type, string? subTypeName)
  1287. {
  1288. int num = 0;
  1289. Sp_Item _Info = datas.Where(it => it.type == type && it.name == subTypeName).FirstOrDefault();
  1290. if (_Info != null) { num = _Info.count; }
  1291. return num;
  1292. }
  1293. /// <summary>
  1294. /// 打卡数据
  1295. /// 异常数据 统计
  1296. /// </summary>
  1297. /// <returns></returns>
  1298. private static int ExceptionStatistics(List<Exception_Info> datas, int type)
  1299. {
  1300. int num = 0;
  1301. Exception_Info _Info = datas.Where(it => it.exception == type).FirstOrDefault();
  1302. if (_Info != null) { num = _Info.count; }
  1303. return num;
  1304. }
  1305. /// <summary>
  1306. /// 获取时间段内除周末 节假日外的 工作日
  1307. /// </summary>
  1308. /// <param name="startDt"></param>
  1309. /// <param name="endDt"></param>
  1310. /// <returns></returns>
  1311. public static async Task<int> GetWorkDays(string yearMonth)
  1312. {
  1313. int workdays = 0;
  1314. string sql = string.Format(@"Select * From Pm_WageIssueWorkingDay
  1315. Where Isdel = 0 And YearMonth = '{0}' ", yearMonth);
  1316. var data = await _usersRep._sqlSugar.SqlQueryable<WageYearMonthView>(sql).FirstAsync();
  1317. if (data != null)
  1318. {
  1319. workdays = data.Workdays;
  1320. }
  1321. return workdays;
  1322. }
  1323. }
  1324. }