PayrollComputation.cs 74 KB

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