PayrollComputation.cs 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432
  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. if (leaveDetails.Count > 0)
  515. {
  516. foreach (LeaveDetails leave_item in leaveDetails)
  517. {
  518. decimal leave_meals = 0.00M;
  519. decimal thisTypeDeduction = 0.00M;//当前类型扣款
  520. int leaveType = leave_item.TypeId;
  521. decimal new_duration = leave_item.New_Duration;
  522. //计算餐补 假勤类型扣款
  523. CalculateTypeFee(leaveDetails,leaveType, leave_item.DtType, leave_item.StartDt, leave_item.EndDt, amountPayable, work_days,
  524. new_duration,out leave_meals, out thisTypeDeduction);
  525. #region 累计类型扣款
  526. //1年假;2事假;3病假;4调休假;5婚假;6产假;7陪产假;8其他;9丧假
  527. if (leaveType == 2) //调休假
  528. {
  529. personalLeaveTotal += thisTypeDeduction;
  530. }
  531. else if (leaveType == 3) //病假
  532. {
  533. sickLeaveTotal = thisTypeDeduction;
  534. }
  535. #endregion
  536. meal_deduction += leave_meals;
  537. Ex_Item ex_Item = new Ex_Item()
  538. {
  539. SubTypeId = leaveType,
  540. SubType = leave_item.TypeName,
  541. StartTimeDt = leave_item.StartDt,
  542. EndTimeDt = leave_item.EndDt,
  543. Duration = new_duration,
  544. Unit = leave_item.Unit,
  545. Deduction = thisTypeDeduction,
  546. //Reason = apply_data.reason,
  547. Apply_time_dt = leave_item.ApplyDt,
  548. //Approval_name = sp_item.approval_name,
  549. };
  550. ex_ItemInfos.Add(ex_Item);
  551. }
  552. }
  553. if (ex_ItemInfos.Count > 0)
  554. {
  555. ex_Items_jq.Ex_ItemInfo = ex_ItemInfos.OrderBy(it => it.StartTimeDt).ThenBy(it => it.Apply_time_dt).ToList();
  556. ex_Items.Add(ex_Items_jq);
  557. }
  558. }
  559. //打卡补卡
  560. //补卡:员工发现自己漏打卡时,需及时提起补卡申请,并说明情况。
  561. //试用期员工每月有 2 次补卡机会,超过 2 次不足 5 次的部分,按 10 元/次处罚,5 次及以上的漏卡,按 50 元/次处罚;
  562. //正式员工每月 3 次以内的补卡,按 10 元/次处罚,3 次及以上的漏卡,按 50 元/次处罚。
  563. if (reissuecardNum > 0)
  564. {
  565. List<Sp_Detail> sp_buka_details = new List<Sp_Detail>();
  566. sp_buka_details = await _qiYeWeChatApiService.GetApprovalDetailsAsync(startDt, endDt, acctid, 2, 2); //时间段内所有 已同意的 请假 审批数据
  567. int bukaNum = 1;
  568. foreach (var item in sp_buka_details)
  569. {
  570. Apply_data? apply_data = item.apply_data;
  571. if (apply_data != null)
  572. {
  573. List<ContentsItem> contents = apply_data.contents;
  574. ContentsItem content_Vacation = contents.Where(it => it.control == "PunchCorrection").FirstOrDefault(); //请假类型
  575. ContentsItem content_Textarea = contents.Where(it => it.control == "Textarea").FirstOrDefault(); //多行文本
  576. if (content_Vacation != null)
  577. {
  578. var punch_correction = content_Vacation.value.punch_correction;
  579. DateTime bukaDt = punch_correction.time_dt;
  580. DateTime bukaDtJudge = Convert.ToDateTime(bukaDt.ToString("yyyy-MM-dd"));
  581. //筛选 不在工作日内的假勤申请
  582. if (startDt > bukaDtJudge || bukaDtJudge > endDt)
  583. {
  584. continue;
  585. }
  586. decimal bukaPrice = 0.00M;
  587. if (user_probationary_bk_decimal == 0) //计算试用员工补卡次数
  588. {
  589. if (bukaNum <= 2) bukaPrice = 0.00M;
  590. else if (bukaNum <= 4 && bukaNum > 2) bukaPrice = 10.00M;
  591. else bukaPrice = 50.00M;
  592. }
  593. else //计算正式员工补卡次数
  594. {
  595. if (bukaNum <= 2) bukaPrice = 10.00M;
  596. else bukaPrice = 50.00M;
  597. }
  598. var app_data = item.apply_data;
  599. var punch_correction1 = app_data.contents[0].value.punch_correction; //未打卡时间
  600. var punch_correction2 = app_data.contents[1].value;
  601. Ex_Item ex_reissueCard = new Ex_Item()
  602. {
  603. SubTypeId = 7,
  604. SubType = "打卡补卡",
  605. StartTimeDt = Convert.ToDateTime(punch_correction1.time_dt.ToString("yyyy-MM-dd HH:mm:ss")), //未打卡时间
  606. Deduction = bukaPrice,
  607. Reason = punch_correction2.text,
  608. Unit = string.Empty
  609. };
  610. unprinted_deduction += bukaPrice;
  611. ex_reissuecard_Items.Add(ex_reissueCard);
  612. bukaNum++;
  613. }
  614. }
  615. }
  616. //外出审批 已通过的数据 且 外出未打卡
  617. List<Sp_Detail> sp_goout_details = new List<Sp_Detail>();
  618. sp_goout_details = await _qiYeWeChatApiService.GetApprovalDetailsAsync(startDt, endDt, acctid, 2, 4); //时间段内所有 已同意的 外出 审批数据
  619. if (sp_goout_details.Count > 0)
  620. {
  621. //找出外出的具体日期
  622. List<string> goOutDts = new List<string>();
  623. foreach (var goOutItem in sp_goout_details)
  624. {
  625. Apply_data? apply_data = goOutItem.apply_data;
  626. if (apply_data != null)
  627. {
  628. List<ContentsItem> contents = apply_data.contents;
  629. ContentsItem content_Vacation = contents.Where(it => it.control == "Attendance").FirstOrDefault(); //请假类型
  630. ContentsItem content_Textarea = contents.Where(it => it.control == "Textarea").FirstOrDefault(); //多行文本
  631. if (content_Vacation != null)
  632. {
  633. var attendance = content_Vacation.value.attendance;
  634. DateTime goOutDt = attendance.date_range.new_begin_dt;
  635. DateTime goOutDtJudge = Convert.ToDateTime(goOutDt.ToString("yyyy-MM-dd"));
  636. //筛选 不在工作日内的假勤申请
  637. if (startDt >= goOutDtJudge || goOutDtJudge > endDt)
  638. {
  639. continue;
  640. }
  641. goOutDts.Add(goOutDt.ToString("yyyy-MM-dd"));
  642. }
  643. }
  644. }
  645. //处理日期范围内的数据
  646. List<CheckInDataInfo> filtratePrefix_checkInData = new List<CheckInDataInfo>();
  647. if (goOutDts.Count > 0)
  648. {
  649. foreach (var goOutDtItem in goOutDts)
  650. {
  651. List<CheckInDataInfo> filtrate_checkInData = new List<CheckInDataInfo>();
  652. filtrate_checkInData = checkInData2.Where(it => it.checkin_time_dt.ToString("yyyy-MM-dd").Equals(goOutDtItem)).ToList();
  653. if (filtrate_checkInData.Count > 0)
  654. {
  655. filtratePrefix_checkInData.AddRange(filtrate_checkInData);
  656. }
  657. }
  658. }
  659. //打卡记录里抓取的未打卡数据
  660. foreach (var item in filtratePrefix_checkInData)
  661. {
  662. decimal bukaPrice = 0.00M;
  663. if (user_probationary_bk_decimal == 0) //计算试用员工补卡次数
  664. {
  665. if (bukaNum <= 2) bukaPrice = 0.00M;
  666. else if (bukaNum <= 4 && bukaNum > 2) bukaPrice = 10.00M;
  667. else bukaPrice = 50.00M;
  668. }
  669. else //计算正式员工补卡次数
  670. {
  671. if (bukaNum <= 2) bukaPrice = 10.00M;
  672. else bukaPrice = 50.00M;
  673. }
  674. Ex_Item ex_reissueCard = new Ex_Item()
  675. {
  676. SubTypeId = 7,
  677. SubType = "打卡补卡",
  678. StartTimeDt = Convert.ToDateTime(item.checkin_time_dt.ToString("yyyy-MM-dd HH:mm:ss")), //未打卡时间
  679. Deduction = bukaPrice,
  680. Reason = "[打卡记录]抓取到的未打卡数据!",
  681. Unit = string.Empty
  682. };
  683. unprinted_deduction += bukaPrice;
  684. ex_reissuecard_Items.Add(ex_reissueCard);
  685. bukaNum++;
  686. }
  687. }
  688. }
  689. if (ex_reissuecard_Items.Count > 0)
  690. {
  691. ex_Items_dk.Ex_ItemInfo = ex_reissuecard_Items;
  692. ex_Items_dk.Ex_ItemInfo = ex_reissuecard_Items.OrderBy(it => it.SubTypeId).ThenBy(it => it.StartTimeDt).ToList();
  693. ex_Items.Add(ex_Items_dk);
  694. }
  695. //出差申请
  696. if (evectionNum > 0)
  697. {
  698. List<Sp_Detail> sp_leave_details = new List<Sp_Detail>();
  699. sp_leave_details = await _qiYeWeChatApiService.GetApprovalDetailsAsync(startDt, endDt, acctid, 2, 3); //时间段内所有 已同意的 出差 审批数据
  700. if (sp_leave_details.Count <= 0)
  701. {
  702. _result.Msg += startDt + " - " + endDt + " " + itemName + " 请假 审批数据获取未获取到!\r\n";
  703. //continue;
  704. }
  705. List<Ex_Item> cc_ex_ItemInfos = new List<Ex_Item>();
  706. foreach (Sp_Detail sp_item in sp_leave_details)
  707. {
  708. Apply_data? apply_data = sp_item.apply_data;
  709. if (apply_data != null)
  710. {
  711. List<ContentsItem> contents = apply_data.contents;
  712. ContentsItem content_Vacation = contents.Where(it => it.control == "Attendance").FirstOrDefault(); //出差类型
  713. ContentsItem content_Textarea = contents.Where(it => it.control == "Textarea").FirstOrDefault(); //多行文本
  714. if (content_Vacation != null)
  715. {
  716. //Vacation vacation = content_Vacation.value.vacation;
  717. Attendance attendance = content_Vacation.value.attendance; //假勤组件
  718. //Selector selector = vacation.selector; //请假类型
  719. //List<OptionsItem> optionsItems = selector.options; //key 请假类型 id
  720. //List<TitleItem> value = optionsItems[0].value; // value 文本描述值
  721. int leaveType = int.Parse("3"); //key 请假子类型 id
  722. Date_range date_Range = attendance.date_range;
  723. //筛选 不在工作日内的假勤申请
  724. if (startDt >= date_Range.new_begin_dt || Convert.ToDateTime(date_Range.new_end_dt.ToString("yyyy-MM-dd")) > endDt)
  725. {
  726. continue;
  727. }
  728. string leave_starttime = date_Range.new_begin_dt.ToString("HH:mm");
  729. string leave_endtime = date_Range.new_end_dt.ToString("HH:mm");
  730. string typeName = string.Empty;
  731. string unit = string.Empty;
  732. int leaveTypeId = leaveType;
  733. typeName = "出差";
  734. string startTime = string.Empty;
  735. string endTime = string.Empty;
  736. string startTime1 = string.Empty;
  737. string endTime1 = string.Empty;
  738. //计算请假类型扣款金额
  739. decimal new_duration = 0.00M;
  740. if (date_Range.type == "halfday")
  741. {
  742. new_duration = Convert.ToDecimal(date_Range.new_duration) / 86400.00M;
  743. unit = "天";
  744. startTime = date_Range.new_begin_dt.ToString("yyyy-MM-dd") + " 09:00";
  745. endTime = date_Range.new_begin_dt.ToString("yyyy-MM-dd") + " 18:00";
  746. startTime1 = "09:00:00";
  747. endTime1 = "18:00:00";
  748. }
  749. else if (date_Range.type == "hour")
  750. {
  751. new_duration = Convert.ToDecimal(date_Range.new_duration) / 3600.00M;
  752. unit = "小时";
  753. startTime = date_Range.new_begin_dt.ToString("yyyy-MM-dd HH:mm:ss");
  754. endTime = date_Range.new_end_dt.ToString("yyyy-MM-dd HH:mm:ss");
  755. startTime1 = date_Range.new_begin_dt.ToString("HH:mm:ss");
  756. endTime1 = date_Range.new_end_dt.ToString("HH:mm:ss");
  757. }
  758. //出差扣款
  759. decimal cckk = 0.00M;
  760. int days = (int)(date_Range.new_end_dt - date_Range.new_begin_dt).TotalDays;
  761. for (int i = 0; i <= days; i++)
  762. {
  763. DateTime thisDt = date_Range.new_begin_dt.AddDays(i);
  764. if (thisDt > date_Range.new_end_dt)
  765. {
  766. continue;
  767. }
  768. Sys_Calendar sys_Calendar = new Sys_Calendar();
  769. sys_Calendar = sys_Calendars.Where(it => it.Dt == thisDt.ToString("yyyy-MM-dd")).FirstOrDefault();
  770. if (sys_Calendar != null) {
  771. if (sys_Calendar.IsWorkDay)
  772. {
  773. cckk += 10.00M;
  774. }
  775. }
  776. }
  777. meal_deduction += cckk;
  778. Ex_Item ex_Item = new Ex_Item()
  779. {
  780. SubTypeId = leaveType,
  781. SubType = typeName,
  782. StartTimeDt = Convert.ToDateTime(date_Range.new_begin_dt.ToString("yyyy-MM-dd") + " " + startTime1),
  783. EndTimeDt = Convert.ToDateTime(date_Range.new_end_dt.ToString("yyyy-MM-dd") + " " + endTime1),
  784. Duration = new_duration,
  785. Unit = unit,
  786. Deduction = 0.00M,
  787. //Reason = apply_data.reason,
  788. Apply_time_dt = Convert.ToDateTime(sp_item.apply_time_dt.ToString("yyyy-MM-dd HH:mm:ss"))
  789. //Approval_name = sp_item.approval_name,
  790. };
  791. cc_ex_ItemInfos.Add(ex_Item);
  792. }
  793. }
  794. }
  795. if (cc_ex_ItemInfos.Count > 0)
  796. {
  797. //ex_Items_cc.Ex_ItemInfo = cc_ex_ItemInfos.OrderBy(it => it.StartTimeDt).ThenBy(it => it.Apply_time_dt).ToList();
  798. //ex_Items.Add(ex_Items_cc);
  799. List<Ex_Item> ex_jq_Items = new List<Ex_Item>();
  800. ex_jq_Items = (List<Ex_Item>)ex_Items_jq.Ex_ItemInfo;
  801. ex_jq_Items.AddRange(cc_ex_ItemInfos);
  802. ex_Items.Remove(ex_Items_jq);
  803. ex_Items.Add(
  804. new Ex_Items()
  805. {
  806. Type = "假勤",
  807. Ex_ItemInfo = ex_jq_Items.OrderBy(it => it.StartTimeDt).ThenBy(it => it.Apply_time_dt).ToList()
  808. }
  809. );
  810. }
  811. }
  812. #endregion
  813. }
  814. else
  815. {
  816. meal_subsidy = work_days * 10.00M;
  817. }
  818. #region 应发合计 实发合计 扣款合计(假勤扣款,其他扣款,社保扣款,公积金代扣,个税扣款)
  819. decimal mealTotal = meal_subsidy - meal_deduction; //餐补
  820. decimal salaryTotal = 0.00M;
  821. if (dk_work_days >= work_days)
  822. {
  823. dk_work_days = work_days;
  824. salaryTotal = amountPayable + mealTotal; //应发合计
  825. }
  826. else
  827. {
  828. if (itemName.Equals("张海麟"))
  829. {
  830. salaryTotal = amountPayable + mealTotal; //应发合计
  831. }
  832. else
  833. {
  834. salaryTotal = (dk_work_days * dailyWage) + mealTotal; //应发合计
  835. }
  836. }
  837. //扣款合计 不含个税
  838. decimal eductionTotal = sickLeaveTotal + personalLeaveTotal + beLate_deduction + early_deduction + absenteeism_deduction + unprinted_deduction + other_deduction +
  839. pm_wsInfo.WithholdingInsurance + pm_wsInfo.ReservedFunds + pm_wsInfo.OtherDeductions;
  840. decimal actualReleaseTotal = salaryTotal - eductionTotal; //实发合计 * 不含个税
  841. #endregion
  842. #region 处理当月工资数据
  843. pm_wsInfo.YearMonth = thisYearMonth;
  844. pm_wsInfo.StartDate = startDt.ToString("yyyy-MM-dd");
  845. pm_wsInfo.EndDate = endDt.ToString("yyyy-MM-dd");
  846. pm_wsInfo.WorkDays = work_days; //当月应出勤天数
  847. pm_wsInfo.RegularDays = dk_work_days; //当月正常出勤天数
  848. pm_wsInfo.SickLeave = sickLeaveTotal; //病假
  849. pm_wsInfo.SomethingFalse = personalLeaveTotal; //事假
  850. pm_wsInfo.LateTo = beLate_deduction; //迟到
  851. pm_wsInfo.LeaveEarly = early_deduction; //早退
  852. pm_wsInfo.Absenteeism = absenteeism_deduction; //旷工
  853. pm_wsInfo.NotPunch = unprinted_deduction; //未打卡
  854. pm_wsInfo.OtherDeductions = other_deduction; //其他
  855. pm_wsInfo.Ex_ItemsRemark = JsonConvert.SerializeObject(ex_Items); //
  856. pm_wsInfo.Mealsupplement = mealTotal; //餐补
  857. pm_wsInfo.Should = ConvertToDecimal( salaryTotal); //应发合计
  858. pm_wsInfo.TotalDeductions = ConvertToDecimal(eductionTotal); //扣款合计
  859. pm_wsInfo.TotalRealHair = ConvertToDecimal(actualReleaseTotal - pm_wsInfo.WithholdingTax); //实发合计
  860. pm_wsInfo.AfterTax = ConvertToDecimal(actualReleaseTotal - pm_wsInfo.WithholdingTax); //税后工资
  861. pm_wsInfo.LastUpdateUserId = userId;
  862. pm_wsInfo.LastUpdateDt = DateTime.Now;
  863. pm_wsInfo.CreateUserId = userId;
  864. pm_wsInfo.CreateTime = DateTime.Now;
  865. pm_wsInfo.DeleteUserId = null;
  866. pm_wsInfo.DeleteTime = null;
  867. #endregion
  868. }
  869. }
  870. catch (Exception ex)
  871. {
  872. _result.Msg = ex.Message;
  873. return _result;
  874. }
  875. _result.Code = 0;
  876. _result.Data = pm_WageSheetDattaSources;
  877. return _result;
  878. }
  879. /// <summary>
  880. /// decimal 保留两位小数 不四舍五入
  881. /// </summary>
  882. /// <param name="number"></param>
  883. /// <returns></returns>
  884. public static decimal ConvertToDecimal(decimal myDecimal)
  885. {
  886. var subDecimal = Math.Floor(myDecimal * 100) / 100;//保留两位小数,直接截取
  887. return subDecimal;
  888. }
  889. /// <summary>
  890. /// 获取请假类型
  891. /// </summary>
  892. /// <param name="template_id"></param>
  893. /// <returns></returns>
  894. public static async Task<List<VacationLeaveTypeView>> GetVacationLeaveTypes(string template_id)
  895. {
  896. List<VacationLeaveTypeView> vacationLeaveTypes = new List<VacationLeaveTypeView>();
  897. TemplateDetailView templateDetailView = new TemplateDetailView();
  898. templateDetailView = await _qiYeWeChatApiService.GetTemplateDetailAsync(template_id);
  899. if (templateDetailView.errcode != 0)
  900. {
  901. Serilog.Log.Error("【企业微信】【审批】【获取假勤类型的审批】【Msg】"+ templateDetailView.errmsg);
  902. return vacationLeaveTypes;
  903. }
  904. List<VacationItemInfo> VacationItemInfos = templateDetailView.vacation_list.item;
  905. foreach (var item in VacationItemInfos)
  906. {
  907. ValueItem valueInfo = item.name.Where(it => it.lang == "zh_CN").FirstOrDefault();
  908. if (valueInfo != null)
  909. {
  910. vacationLeaveTypes.Add(
  911. new VacationLeaveTypeView()
  912. {
  913. id = item.id,
  914. name = valueInfo.text
  915. });
  916. }
  917. }
  918. return vacationLeaveTypes;
  919. }
  920. /// <summary>
  921. /// 计算类型费用
  922. /// </summary>
  923. /// <param name="leaveType">
  924. /// 1年假;2事假;3病假;4调休假;5婚假;6产假;7陪产假;8其他;9丧假
  925. /// </param>
  926. /// <param name="date_Range_type">
  927. /// halfday 全天
  928. /// hour 小时
  929. /// </param>
  930. /// <param name="startTime"></param>
  931. /// <param name="endTime"></param>
  932. /// <param name="duration"></param>
  933. /// <param name="mealDeduction"></param>
  934. /// <param name="typeDeduction"></param>
  935. public static void CalculateTypeFee(List<LeaveDetails> leaveDetails,int leaveType, string date_Range_type, DateTime startTime, DateTime endTime,
  936. decimal amountPayable,int work_days, decimal duration, out decimal mealDeduction, out decimal typeDeduction)
  937. {
  938. typeDeduction = 0;
  939. mealDeduction = 0;
  940. string am_starttime = "08:59";
  941. string am_endtime = "13:01";
  942. //string startTime = startTime1.ToString("HH:mm:ss");
  943. //string endTime = endTime1.ToString("HH:mm:ss");
  944. decimal personalkLeave_dailywage_day = amountPayable / work_days; //日薪 = 事假日薪 *计算方式:日平均工资 = 当月应发工资 /当月应出勤天数。
  945. //半小时单位
  946. decimal halfHour = 7.50M / 0.50M;
  947. switch (leaveType)
  948. {
  949. case 1: //年假
  950. CalculateTypeFeeSub(leaveDetails, date_Range_type, startTime, endTime, duration, out mealDeduction);
  951. break;
  952. case 2: //2事假
  953. // 事假日薪 *计算方式:日平均工资 = 当月应发工资 /当月应出勤天数。
  954. decimal personalkLeave_dailywage_halfhour = personalkLeave_dailywage_day / halfHour; //事假单位 0.5小时
  955. if (date_Range_type == "halfday")
  956. {
  957. mealDeduction = 10; //餐补扣款
  958. typeDeduction = ConvertToDecimal(personalkLeave_dailywage_day);
  959. }
  960. else if (date_Range_type == "hour")
  961. {
  962. decimal leave_halfHour = Convert.ToDecimal(duration) / Convert.ToDecimal(0.5);
  963. typeDeduction = ConvertToDecimal(personalkLeave_dailywage_halfhour * leave_halfHour);
  964. //duration = 11M;
  965. if (duration >= 3 && duration < 7.5M) //单天请假三小时
  966. {
  967. mealDeduction = 10; //餐补扣款
  968. }
  969. else if (duration >= 7.5M) //多天计算
  970. {
  971. decimal leave_halfHour1 = Convert.ToDecimal(duration) / Convert.ToDecimal(0.5);
  972. decimal leaveDays = duration / 7.5M;
  973. if (leaveDays % 1 == 0)
  974. {
  975. typeDeduction = ConvertToDecimal(personalkLeave_dailywage_day * leaveDays);
  976. mealDeduction = 10 * leaveDays; //餐补扣款
  977. }
  978. else
  979. {
  980. typeDeduction = personalkLeave_dailywage_day * Convert.ToInt32(leaveDays);
  981. decimal sy_shijiaunit = leave_halfHour1 - Convert.ToDecimal (15.00M * Convert.ToInt32(leaveDays));
  982. if (sy_shijiaunit > 0)
  983. {
  984. typeDeduction += ConvertToDecimal(personalkLeave_dailywage_halfhour * sy_shijiaunit);
  985. }
  986. mealDeduction = 10 * Convert.ToInt32(leaveDays);
  987. //得到最后一天的请假时间 是否有餐补
  988. int lastHours = (Convert.ToDateTime(endTime) - Convert.ToDateTime("09:00")).Hours;
  989. if (lastHours >= 3)
  990. {
  991. mealDeduction += 10; //餐补扣款
  992. }
  993. }
  994. }
  995. }
  996. break;
  997. case 3: //3病假
  998. // 病假日薪 *计算方式:日平均工资 = 成都市最低工资标准的80% /当月应出勤天数。 短期病假=当月15天内
  999. decimal chengDuMinimumWage_halrHour = _chengDuMinimumWage / work_days / halfHour;
  1000. decimal sickLeave_dailywage_halfhour_deduction1 = (personalkLeave_dailywage_day / halfHour) - chengDuMinimumWage_halrHour; //病假单位 0.5小时 扣款金额
  1001. if (date_Range_type == "halfday")
  1002. {
  1003. mealDeduction = 10; //餐补扣款
  1004. typeDeduction = ConvertToDecimal( sickLeave_dailywage_halfhour_deduction1 * halfHour);
  1005. }
  1006. else if (date_Range_type == "hour")
  1007. {
  1008. decimal sickLeave_halfHour = duration / 0.5M;
  1009. typeDeduction = ConvertToDecimal(sickLeave_dailywage_halfhour_deduction1 * sickLeave_halfHour);
  1010. if (duration >= 3 && duration < 7.5M) //单天请假三小时 && 请假时间在上午 则没有餐补
  1011. {
  1012. mealDeduction = 10; //餐补扣款
  1013. }
  1014. else if (duration >= 7.5M) //多天计算
  1015. {
  1016. decimal sickLeave_halfHour1 = duration / 0.5M;
  1017. decimal leaveDays = Convert.ToDecimal(duration / 7.5M);
  1018. typeDeduction = ConvertToDecimal(sickLeave_dailywage_halfhour_deduction1 * sickLeave_halfHour1);
  1019. if (leaveDays % 1 == 0)
  1020. {
  1021. mealDeduction = 10 * leaveDays; //餐补扣款
  1022. }
  1023. else
  1024. {
  1025. mealDeduction = 10 * Convert.ToInt32(leaveDays);
  1026. typeDeduction = ConvertToDecimal(sickLeave_dailywage_halfhour_deduction1 * Convert.ToInt32(leaveDays));
  1027. decimal sy_bingjiaunit = sickLeave_halfHour1 - Convert.ToDecimal(15.00M * Convert.ToInt32(leaveDays));
  1028. if (sy_bingjiaunit > 0)
  1029. {
  1030. typeDeduction += ConvertToDecimal(sickLeave_dailywage_halfhour_deduction1 * sy_bingjiaunit);
  1031. }
  1032. //得到最后一天的请假时间 是否有餐补
  1033. int lastHours = (Convert.ToDateTime(endTime) - Convert.ToDateTime("09:00")).Hours;
  1034. if (lastHours >= 3)
  1035. {
  1036. mealDeduction += 10; //餐补扣款
  1037. }
  1038. }
  1039. }
  1040. }
  1041. break;
  1042. case 4: //4调休假
  1043. CalculateTypeFeeSub(leaveDetails,date_Range_type, startTime, endTime, duration, out mealDeduction);
  1044. break;
  1045. case 5: //5婚假
  1046. CalculateTypeFeeSub(leaveDetails,date_Range_type, startTime, endTime, duration, out mealDeduction);
  1047. break;
  1048. case 6: //6产假
  1049. CalculateTypeFeeSub(leaveDetails,date_Range_type, startTime, endTime, duration, out mealDeduction);
  1050. break;
  1051. case 7: //7陪产假
  1052. CalculateTypeFeeSub(leaveDetails,date_Range_type, startTime, endTime, duration, out mealDeduction);
  1053. break;
  1054. case 8: //8其他
  1055. CalculateTypeFeeSub(leaveDetails,date_Range_type, startTime, endTime, duration, out mealDeduction);
  1056. break;
  1057. case 9: //9丧假
  1058. CalculateTypeFeeSub(leaveDetails,date_Range_type, startTime, endTime, duration, out mealDeduction);
  1059. break;
  1060. }
  1061. }
  1062. /// <summary>
  1063. /// 计算类型费用
  1064. /// </summary>
  1065. /// <param name="date_Range_type">
  1066. /// halfday 全天
  1067. /// hour 小时
  1068. /// </param>
  1069. /// <param name="startTime"></param>
  1070. /// <param name="endTime"></param>
  1071. /// <param name="duration"></param>
  1072. /// <param name="mealDeduction"></param>
  1073. public static void CalculateTypeFeeSub(List<LeaveDetails> leaveDetails,string date_Range_type, DateTime startTime1, DateTime endTime1,
  1074. decimal duration, out decimal mealDeduction)
  1075. {
  1076. mealDeduction = 0;
  1077. string am_starttime = "09:00";
  1078. string am_endtime = "11:59";
  1079. string startTime = startTime1.ToString("HH:mm:ss");
  1080. string endTime = endTime1.ToString("HH:mm:ss");
  1081. if (date_Range_type == "halfday")
  1082. {
  1083. mealDeduction = 10; //餐补扣款
  1084. }
  1085. else if (date_Range_type == "hour")
  1086. {
  1087. List<LeaveDetails> leaveDetails1 = new List<LeaveDetails>();
  1088. leaveDetails1 = leaveDetails.Where(it => it.StartDt.ToString("yyyy-MM-dd").Equals(startTime1.ToString("yyyy-MM-dd")))
  1089. .OrderBy(it => it.StartDt).ToList();
  1090. if (leaveDetails1.Count > 1)
  1091. {
  1092. decimal timelenTatoal = leaveDetails1.Sum(it => it.New_Duration);
  1093. if (timelenTatoal >= 3.00M)
  1094. {
  1095. if (leaveDetails1[0].StartDt == startTime1)
  1096. {
  1097. mealDeduction = 10; //餐补扣款
  1098. }
  1099. }
  1100. }
  1101. else
  1102. {
  1103. if (duration >= 3 && duration < 7) //单天请假三小时 && 请假时间在上午 则没有餐补
  1104. {
  1105. mealDeduction = 10; //餐补扣款
  1106. }
  1107. else if (duration >= 7 && duration <= 7.50M)
  1108. {
  1109. mealDeduction = 10; //餐补扣款
  1110. }
  1111. else if (duration >= 7.50M) //多天计算
  1112. {
  1113. decimal leaveDays = Convert.ToDecimal(duration / 7.50M);
  1114. if (leaveDays % 1 == 0)
  1115. {
  1116. mealDeduction = 10 * leaveDays; //餐补扣款
  1117. }
  1118. else
  1119. {
  1120. mealDeduction = 10 * Convert.ToInt32(leaveDays);
  1121. //得到最后一天的请假时间 是否有餐补
  1122. int lastHours = (Convert.ToDateTime(endTime) - Convert.ToDateTime("09:00")).Hours;
  1123. if (lastHours >= 3)
  1124. {
  1125. mealDeduction += 10; //餐补扣款
  1126. ////处理结束时间
  1127. //if (endTime.CompareTo(am_starttime) > 0 && endTime.CompareTo(am_endtime) < 0)
  1128. //{
  1129. // mealDeduction += 10; //餐补扣款
  1130. //}
  1131. }
  1132. }
  1133. }
  1134. }
  1135. }
  1136. }
  1137. /// <summary>
  1138. /// 获取打卡补卡类型
  1139. /// </summary>
  1140. /// <param name="template_id"></param>
  1141. /// <returns></returns>
  1142. public static async Task<List<VacationLeaveTypeView>> GetVacationReissueCardTypes(string template_id)
  1143. {
  1144. List<VacationLeaveTypeView> vacationLeaveTypes = new List<VacationLeaveTypeView>();
  1145. TemplateDetailView templateDetailView = new TemplateDetailView();
  1146. templateDetailView = await _qiYeWeChatApiService.GetTemplateDetailAsync(template_id);
  1147. if (templateDetailView.errcode != 0)
  1148. {
  1149. return vacationLeaveTypes;
  1150. }
  1151. List<VacationItemInfo> VacationItemInfos = templateDetailView.vacation_list.item;
  1152. foreach (var item in VacationItemInfos)
  1153. {
  1154. ValueItem valueInfo = item.name.Where(it => it.lang == "zh_CN").FirstOrDefault();
  1155. if (valueInfo != null)
  1156. {
  1157. vacationLeaveTypes.Add(
  1158. new VacationLeaveTypeView()
  1159. {
  1160. id = item.id,
  1161. name = valueInfo.text
  1162. });
  1163. }
  1164. }
  1165. return vacationLeaveTypes;
  1166. }
  1167. /// <summary>
  1168. /// 打卡数据
  1169. /// 假勤数据 统计
  1170. /// </summary>
  1171. /// <param name="datas">数据源</param>
  1172. /// <param name="type">
  1173. /// 1-请假;2-补卡;3-出差;4-外出;100-外勤;
  1174. /// </param>
  1175. /// <param name="subTypeName">
  1176. /// 年假 事假 病假 调休假 婚嫁 产假 陪产假 丧假 补卡次数 出差 外出数 外勤 其他
  1177. /// </param>
  1178. /// <returns></returns>
  1179. private static int Fallibilitydispose(List<Sp_Item> datas, int type, string? subTypeName)
  1180. {
  1181. int num = 0;
  1182. Sp_Item _Info = datas.Where(it => it.type == type && it.name == subTypeName).FirstOrDefault();
  1183. if (_Info != null) { num = _Info.count; }
  1184. return num;
  1185. }
  1186. /// <summary>
  1187. /// 打卡数据
  1188. /// 异常数据 统计
  1189. /// </summary>
  1190. /// <returns></returns>
  1191. private static int ExceptionStatistics(List<Exception_Info> datas, int type)
  1192. {
  1193. int num = 0;
  1194. Exception_Info _Info = datas.Where(it => it.exception == type).FirstOrDefault();
  1195. if (_Info != null) { num = _Info.count; }
  1196. return num;
  1197. }
  1198. /// <summary>
  1199. /// 获取时间段内除周末 节假日外的 工作日
  1200. /// </summary>
  1201. /// <param name="startDt"></param>
  1202. /// <param name="endDt"></param>
  1203. /// <returns></returns>
  1204. public static async Task<int> GetWorkDays(string yearMonth)
  1205. {
  1206. int workdays = 0;
  1207. string sql = string.Format(@"Select * From Pm_WageIssueWorkingDay
  1208. Where Isdel = 0 And YearMonth = '{0}' ", yearMonth);
  1209. var data = await _usersRep._sqlSugar.SqlQueryable<WageYearMonthView>(sql).FirstAsync();
  1210. if (data != null)
  1211. {
  1212. workdays = data.Workdays;
  1213. }
  1214. return workdays;
  1215. }
  1216. }
  1217. }