PayrollComputation.cs 78 KB

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