PayrollComputation.cs 109 KB

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