PayrollComputation.cs 74 KB

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