PayrollComputation.cs 115 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076
  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)缺卡/未打卡视为旷工半天";
  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)缺卡/未打卡视为旷工半天";
  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)缺卡/未打卡视为旷工半天";
  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 + " 缺卡/未打视为旷工半天";
  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 + " 缺卡/未打视为旷工半天";
  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. //外出审批 已通过的数据 且 外出未打卡
  691. List<Sp_Detail> sp_goout_details = new List<Sp_Detail>();
  692. sp_goout_details = await _qiYeWeChatApiService.GetApprovalDetailsAsync(startDt, endDt, acctid, 2, 4); //时间段内所有 已同意的 外出 审批数据
  693. //打卡记录-未打卡 处理日期范围内的数据
  694. List<CheckInDataInfo> filtratePrefix_checkInData = new List<CheckInDataInfo>();
  695. //打卡记录-未打卡 扣款
  696. if (sp_goout_details.Count > 0)
  697. {
  698. //找出外出的具体日期
  699. List<string> goOutDts = new List<string>();
  700. foreach (var goOutItem in sp_goout_details)
  701. {
  702. Apply_data? apply_data = goOutItem.apply_data;
  703. if (apply_data != null)
  704. {
  705. List<ContentsItem> contents = apply_data.contents;
  706. ContentsItem content_Vacation = contents.Where(it => it.control == "Attendance").FirstOrDefault(); //请假类型
  707. ContentsItem content_Textarea = contents.Where(it => it.control == "Textarea").FirstOrDefault(); //多行文本
  708. if (content_Vacation != null)
  709. {
  710. var attendance = content_Vacation.value.attendance;
  711. DateTime goOutDt = attendance.date_range.new_begin_dt;
  712. DateTime goOutDtJudge = Convert.ToDateTime(goOutDt.ToString("yyyy-MM-dd"));
  713. //筛选 不在工作日内的假勤申请
  714. if (startDt >= goOutDtJudge || goOutDtJudge > endDt)
  715. {
  716. continue;
  717. }
  718. goOutDts.Add(goOutDt.ToString("yyyy-MM-dd"));
  719. }
  720. }
  721. }
  722. if (goOutDts.Count > 0)
  723. {
  724. foreach (var goOutDtItem in goOutDts)
  725. {
  726. List<CheckInDataInfo> filtrate_checkInData = new List<CheckInDataInfo>();
  727. filtrate_checkInData = checkInData2.Where(it => it.checkin_time_dt.ToString("yyyy-MM-dd").Equals(goOutDtItem)).ToList();
  728. if (filtrate_checkInData.Count > 0)
  729. {
  730. filtratePrefix_checkInData.AddRange(filtrate_checkInData);
  731. }
  732. }
  733. }
  734. }
  735. int bukaNum = 1;
  736. foreach (var item in sp_buka_details)
  737. {
  738. Apply_data? apply_data = item.apply_data;
  739. if (apply_data != null)
  740. {
  741. List<ContentsItem> contents = apply_data.contents;
  742. ContentsItem content_Vacation = contents.Where(it => it.control == "PunchCorrection").FirstOrDefault(); //请假类型
  743. ContentsItem content_Textarea = contents.Where(it => it.control == "Textarea").FirstOrDefault(); //多行文本
  744. if (content_Vacation != null)
  745. {
  746. var punch_correction = content_Vacation.value.punch_correction;
  747. DateTime bukaDt = punch_correction.time_dt;
  748. DateTime bukaDtJudge = Convert.ToDateTime(bukaDt.ToString("yyyy-MM-dd"));
  749. //筛选 不在工作日内的假勤申请
  750. if (startDt > bukaDtJudge || bukaDtJudge > endDt)
  751. {
  752. continue;
  753. }
  754. decimal bukaPrice = 0.00M;
  755. if (user_probationary_bk_decimal == 0) //计算试用员工补卡次数
  756. {
  757. if (bukaNum <= 2) bukaPrice = 0.00M;
  758. else if (bukaNum <= 4 && bukaNum > 2) bukaPrice = 10.00M;
  759. else bukaPrice = 50.00M;
  760. }
  761. else //计算正式员工补卡次数
  762. {
  763. if (bukaNum <= 2) bukaPrice = 10.00M;
  764. else bukaPrice = 50.00M;
  765. }
  766. var app_data = item.apply_data;
  767. var punch_correction1 = app_data.contents[0].value.punch_correction; //未打卡时间
  768. var punch_correction2 = app_data.contents[1].value;
  769. #region 处理未打卡记录表数据 已补卡的数据
  770. if (filtratePrefix_checkInData.Count > 0)
  771. {
  772. var filtratePrefix_checkInItems = filtratePrefix_checkInData.Where(it => it.sch_checkin_time_dt.ToString("yyyy-MM-dd")
  773. .Equals(punch_correction1.time_dt.ToString("yyyy-MM-dd"))).ToList();
  774. if (filtratePrefix_checkInItems.Count > 0)
  775. {
  776. //按时间段移除 上午补卡时间段 09:00 - 12:00 下午补卡时间段 12:00 - 18:00
  777. TimeSpan _strWorkingDayAM = DateTime.Parse("09:00").TimeOfDay;
  778. TimeSpan _endWorkingDayAM = DateTime.Parse("12:00").TimeOfDay;
  779. TimeSpan _strWorkingDayPM = DateTime.Parse("13:30").TimeOfDay;
  780. TimeSpan _endWorkingDayPM = DateTime.Parse("18:00").TimeOfDay;
  781. TimeSpan _dspNow = punch_correction1.time_dt.TimeOfDay;
  782. //上午时间段 打卡记录数据
  783. var filtratePrefix_checkInItemAMs = filtratePrefix_checkInItems.Where(it => it.sch_checkin_time_dt.TimeOfDay >= _strWorkingDayAM &&
  784. it.sch_checkin_time_dt.TimeOfDay <= _endWorkingDayAM ).ToList();
  785. if (filtratePrefix_checkInItemAMs.Count > 0)
  786. {
  787. foreach (var filtratePrefix_checkInItem1 in filtratePrefix_checkInItemAMs)
  788. {
  789. filtratePrefix_checkInData.Remove(filtratePrefix_checkInItem1);
  790. }
  791. }
  792. //下午时间段 打卡记录数据
  793. var filtratePrefix_checkInItemPMs = filtratePrefix_checkInItems.Where(it => it.sch_checkin_time_dt.TimeOfDay >= _strWorkingDayPM &&
  794. it.sch_checkin_time_dt.TimeOfDay <= _endWorkingDayPM).ToList();
  795. if (filtratePrefix_checkInItemPMs.Count > 0)
  796. {
  797. foreach (var filtratePrefix_checkInItem1 in filtratePrefix_checkInItemPMs)
  798. {
  799. filtratePrefix_checkInData.Remove(filtratePrefix_checkInItem1);
  800. }
  801. }
  802. }
  803. }
  804. #endregion
  805. Ex_Item ex_reissueCard = new Ex_Item()
  806. {
  807. SubTypeId = 7,
  808. SubType = "打卡补卡",
  809. StartTimeDt = Convert.ToDateTime(punch_correction1.time_dt.ToString("yyyy-MM-dd HH:mm:ss")), //未打卡时间
  810. Deduction = bukaPrice,
  811. Reason = punch_correction2.text,
  812. Unit = string.Empty
  813. };
  814. unprinted_deduction += bukaPrice;
  815. ex_reissuecard_Items.Add(ex_reissueCard);
  816. bukaNum++;
  817. }
  818. }
  819. }
  820. //打卡记录里抓取的未打卡数据 扣款
  821. foreach (var item in filtratePrefix_checkInData)
  822. {
  823. decimal bukaPrice = 0.00M;
  824. if (user_probationary_bk_decimal == 0) //计算试用员工补卡次数
  825. {
  826. if (bukaNum <= 2) bukaPrice = 0.00M;
  827. else if (bukaNum <= 4 && bukaNum > 2) bukaPrice = 10.00M;
  828. else bukaPrice = 50.00M;
  829. }
  830. else //计算正式员工补卡次数
  831. {
  832. if (bukaNum <= 2) bukaPrice = 10.00M;
  833. else bukaPrice = 50.00M;
  834. }
  835. Ex_Item ex_reissueCard = new Ex_Item()
  836. {
  837. SubTypeId = 7,
  838. SubType = "打卡补卡",
  839. StartTimeDt = Convert.ToDateTime(item.checkin_time_dt.ToString("yyyy-MM-dd HH:mm:ss")), //未打卡时间
  840. Deduction = bukaPrice,
  841. Reason = "[打卡记录]抓取到的未打卡数据!",
  842. Unit = string.Empty
  843. };
  844. unprinted_deduction += bukaPrice;
  845. ex_reissuecard_Items.Add(ex_reissueCard);
  846. bukaNum++;
  847. }
  848. }
  849. if (ex_reissuecard_Items.Count > 0)
  850. {
  851. ex_Items_dk.Ex_ItemInfo = ex_reissuecard_Items;
  852. ex_Items_dk.Ex_ItemInfo = ex_reissuecard_Items.OrderBy(it => it.SubTypeId).ThenBy(it => it.StartTimeDt).ToList();
  853. ex_Items.Add(ex_Items_dk);
  854. }
  855. //出差申请
  856. if (evectionNum > 0)
  857. {
  858. List<Sp_Detail> sp_leave_details = new List<Sp_Detail>();
  859. sp_leave_details = await _qiYeWeChatApiService.GetApprovalDetailsAsync(startDt, endDt, acctid, 2, 3); //时间段内所有 已同意的 出差 审批数据
  860. if (sp_leave_details.Count <= 0)
  861. {
  862. _result.Msg += startDt + " - " + endDt + " " + itemName + " 请假 审批数据获取未获取到!\r\n";
  863. //continue;
  864. }
  865. List<Ex_Item> cc_ex_ItemInfos = new List<Ex_Item>();
  866. foreach (Sp_Detail sp_item in sp_leave_details)
  867. {
  868. Apply_data? apply_data = sp_item.apply_data;
  869. if (apply_data != null)
  870. {
  871. List<ContentsItem> contents = apply_data.contents;
  872. ContentsItem content_Vacation = contents.Where(it => it.control == "Attendance").FirstOrDefault(); //出差类型
  873. ContentsItem content_Textarea = contents.Where(it => it.control == "Textarea").FirstOrDefault(); //多行文本
  874. if (content_Vacation != null)
  875. {
  876. //Vacation vacation = content_Vacation.value.vacation;
  877. Attendance attendance = content_Vacation.value.attendance; //假勤组件
  878. //Selector selector = vacation.selector; //请假类型
  879. //List<OptionsItem> optionsItems = selector.options; //key 请假类型 id
  880. //List<TitleItem> value = optionsItems[0].value; // value 文本描述值
  881. int leaveType = int.Parse("3"); //key 请假子类型 id
  882. Date_range date_Range = attendance.date_range;
  883. //筛选 不在工作日内的假勤申请
  884. if (startDt >= date_Range.new_begin_dt || Convert.ToDateTime(date_Range.new_end_dt.ToString("yyyy-MM-dd")) > endDt)
  885. {
  886. continue;
  887. }
  888. string leave_starttime = date_Range.new_begin_dt.ToString("HH:mm");
  889. string leave_endtime = date_Range.new_end_dt.ToString("HH:mm");
  890. string typeName = string.Empty;
  891. string unit = string.Empty;
  892. int leaveTypeId = leaveType;
  893. typeName = "出差";
  894. string startTime = string.Empty;
  895. string endTime = string.Empty;
  896. string startTime1 = string.Empty;
  897. string endTime1 = string.Empty;
  898. //计算请假类型扣款金额
  899. decimal new_duration = 0.00M;
  900. if (date_Range.type == "halfday")
  901. {
  902. new_duration = Convert.ToDecimal(date_Range.new_duration) / 86400.00M;
  903. unit = "天";
  904. startTime = date_Range.new_begin_dt.ToString("yyyy-MM-dd") + " 09:00";
  905. endTime = date_Range.new_begin_dt.ToString("yyyy-MM-dd") + " 18:00";
  906. startTime1 = "09:00:00";
  907. endTime1 = "18:00:00";
  908. }
  909. else if (date_Range.type == "hour")
  910. {
  911. new_duration = Convert.ToDecimal(date_Range.new_duration) / 3600.00M;
  912. unit = "小时";
  913. startTime = date_Range.new_begin_dt.ToString("yyyy-MM-dd HH:mm:ss");
  914. endTime = date_Range.new_end_dt.ToString("yyyy-MM-dd HH:mm:ss");
  915. startTime1 = date_Range.new_begin_dt.ToString("HH:mm:ss");
  916. endTime1 = date_Range.new_end_dt.ToString("HH:mm:ss");
  917. }
  918. //出差扣款
  919. decimal cckk = 0.00M;
  920. int days = (int)(Convert.ToDateTime(date_Range.new_end_dt.ToString("yyyy-MM-dd")) -
  921. Convert.ToDateTime(date_Range.new_begin_dt.ToString("yyyy-MM-dd"))).TotalDays + 1;
  922. for (int i = 0; i < days; i++)
  923. {
  924. DateTime thisDt = date_Range.new_begin_dt.AddDays(i);
  925. //处理开始时间 是否在当天工作时间内
  926. if (i == 0) //开始时间
  927. {
  928. string ccStartTime = thisDt.ToString("HH:mm:ss");
  929. if (!IsWorkTime(ccStartTime))
  930. {
  931. continue;
  932. }
  933. }
  934. //处理结束时间 是否在当天工作时间内
  935. if (i == days - 1)
  936. {
  937. string ccStartTime = date_Range.new_end_dt.ToString("HH:mm:ss");
  938. if (!IsWorkTime(ccStartTime))
  939. {
  940. continue;
  941. }
  942. }
  943. Sys_Calendar sys_Calendar = new Sys_Calendar();
  944. sys_Calendar = sys_Calendars.Where(it => it.Dt == thisDt.ToString("yyyy-MM-dd")).FirstOrDefault();
  945. if (sys_Calendar != null) {
  946. if (sys_Calendar.IsWorkDay)
  947. {
  948. cckk += 10.00M;
  949. }
  950. }
  951. }
  952. meal_deduction += cckk;
  953. Ex_Item ex_Item = new Ex_Item()
  954. {
  955. SubTypeId = leaveType,
  956. SubType = typeName,
  957. StartTimeDt = Convert.ToDateTime(date_Range.new_begin_dt.ToString("yyyy-MM-dd") + " " + startTime1),
  958. EndTimeDt = Convert.ToDateTime(date_Range.new_end_dt.ToString("yyyy-MM-dd") + " " + endTime1),
  959. Duration = new_duration,
  960. Unit = unit,
  961. Deduction = 0.00M,
  962. //Reason = apply_data.reason,
  963. Apply_time_dt = Convert.ToDateTime(sp_item.apply_time_dt.ToString("yyyy-MM-dd HH:mm:ss"))
  964. //Approval_name = sp_item.approval_name,
  965. };
  966. cc_ex_ItemInfos.Add(ex_Item);
  967. }
  968. }
  969. }
  970. if (cc_ex_ItemInfos.Count > 0)
  971. {
  972. //ex_Items_cc.Ex_ItemInfo = cc_ex_ItemInfos.OrderBy(it => it.StartTimeDt).ThenBy(it => it.Apply_time_dt).ToList();
  973. //;
  974. //ex_Items.Add(ex_Items_cc);
  975. List<Ex_Item> ex_jq_Items = new List<Ex_Item>();
  976. List<Ex_Item> ex_Items1 = new List<Ex_Item>();
  977. ex_Items1 = (List<Ex_Item>)ex_Items_jq.Ex_ItemInfo;
  978. if (ex_Items1 != null && ex_Items1.Count > 0)
  979. {
  980. ex_jq_Items = ex_Items1;
  981. }
  982. ex_jq_Items.AddRange(cc_ex_ItemInfos);
  983. ex_Items.Remove(ex_Items_jq);
  984. ex_Items.Add(
  985. new Ex_Items()
  986. {
  987. Type = "假勤",
  988. Ex_ItemInfo = ex_jq_Items.OrderBy(it => it.StartTimeDt).ThenBy(it => it.Apply_time_dt).ToList()
  989. }
  990. );
  991. }
  992. }
  993. #endregion
  994. }
  995. else
  996. {
  997. meal_subsidy = work_days * 10.00M;
  998. }
  999. #region 应发合计 实发合计 扣款合计(假勤扣款,其他扣款,社保扣款,公积金代扣,个税扣款)
  1000. decimal mealTotal = meal_subsidy - meal_deduction; //餐补
  1001. decimal salaryTotal = 0.00M;
  1002. if (dk_work_days >= work_days)
  1003. {
  1004. dk_work_days = work_days;
  1005. salaryTotal = amountPayable + mealTotal + pm_wsInfo.OtherHandle; //应发合计
  1006. }
  1007. else
  1008. {
  1009. if (itemName.Equals("张海麟"))
  1010. {
  1011. salaryTotal = amountPayable + mealTotal + pm_wsInfo.OtherHandle; //应发合计
  1012. }
  1013. else
  1014. {
  1015. salaryTotal = (dk_work_days * dailyWage) + mealTotal + pm_wsInfo.OtherHandle; //应发合计
  1016. }
  1017. }
  1018. //扣款合计 不含个税
  1019. decimal eductionTotal = sickLeaveTotal + personalLeaveTotal + beLate_deduction + early_deduction + absenteeism_deduction + unprinted_deduction + other_deduction +
  1020. pm_wsInfo.WithholdingInsurance + pm_wsInfo.ReservedFunds + pm_wsInfo.OtherDeductions;
  1021. decimal actualReleaseTotal = salaryTotal - eductionTotal; //实发合计 * 不含个税
  1022. #endregion
  1023. #region 处理当月工资数据
  1024. pm_wsInfo.YearMonth = thisYearMonth;
  1025. pm_wsInfo.StartDate = startDt.ToString("yyyy-MM-dd");
  1026. pm_wsInfo.EndDate = endDt.ToString("yyyy-MM-dd");
  1027. pm_wsInfo.WorkDays = work_days; //当月应出勤天数
  1028. pm_wsInfo.RegularDays = dk_work_days; //当月正常出勤天数
  1029. pm_wsInfo.SickLeave = sickLeaveTotal; //病假
  1030. pm_wsInfo.SomethingFalse = personalLeaveTotal; //事假
  1031. pm_wsInfo.LateTo = beLate_deduction; //迟到
  1032. pm_wsInfo.LeaveEarly = early_deduction; //早退
  1033. pm_wsInfo.Absenteeism = absenteeism_deduction; //旷工
  1034. pm_wsInfo.NotPunch = unprinted_deduction; //未打卡
  1035. pm_wsInfo.OtherDeductions = other_deduction; //其他
  1036. pm_wsInfo.Ex_ItemsRemark = JsonConvert.SerializeObject(ex_Items); //
  1037. pm_wsInfo.Mealsupplement = mealTotal; //餐补
  1038. pm_wsInfo.Should = ConvertToDecimal( salaryTotal); //应发合计
  1039. pm_wsInfo.TotalDeductions = ConvertToDecimal(eductionTotal); //扣款合计
  1040. pm_wsInfo.TotalRealHair = ConvertToDecimal(actualReleaseTotal - pm_wsInfo.WithholdingTax); //实发合计
  1041. pm_wsInfo.AfterTax = ConvertToDecimal(actualReleaseTotal - pm_wsInfo.WithholdingTax); //税后工资
  1042. pm_wsInfo.LastUpdateUserId = userId;
  1043. pm_wsInfo.LastUpdateDt = DateTime.Now;
  1044. pm_wsInfo.CreateUserId = userId;
  1045. pm_wsInfo.CreateTime = DateTime.Now;
  1046. pm_wsInfo.DeleteUserId = null;
  1047. pm_wsInfo.DeleteTime = null;
  1048. #endregion
  1049. }
  1050. }
  1051. catch (Exception ex)
  1052. {
  1053. _result.Msg = "【" + name + "】【Msg:" + ex.Message + "】";
  1054. return _result;
  1055. }
  1056. _result.Code = 0;
  1057. _result.Data = pm_WageSheetDattaSources;
  1058. return _result;
  1059. }
  1060. /// <summary>
  1061. /// 是否在工作时间内
  1062. /// </summary>
  1063. /// <param name="timeStr"></param>
  1064. /// <returns></returns>
  1065. public static bool IsWorkTime(string timeStr)
  1066. {
  1067. string thisDayWorkStartTime = "09:00:00";
  1068. string thisDayWorkEndTime = "18:00:00";
  1069. TimeSpan dspWorkingDayAM = DateTime.Parse(thisDayWorkStartTime).TimeOfDay;
  1070. TimeSpan dspWorkingDayPM = DateTime.Parse(thisDayWorkEndTime).TimeOfDay;
  1071. DateTime dt = Convert.ToDateTime(timeStr);
  1072. TimeSpan dspNow = dt.TimeOfDay;
  1073. if (dspNow >= dspWorkingDayAM && dspNow <= dspWorkingDayPM)
  1074. {
  1075. return true;
  1076. }
  1077. return false;
  1078. }
  1079. /// <summary>
  1080. /// decimal 保留两位小数 不四舍五入
  1081. /// </summary>
  1082. /// <param name="number"></param>
  1083. /// <returns></returns>
  1084. public static decimal ConvertToDecimal(decimal myDecimal)
  1085. {
  1086. var subDecimal = Math.Floor(myDecimal * 100) / 100;//保留两位小数,直接截取
  1087. return subDecimal;
  1088. }
  1089. /// <summary>
  1090. /// decimal 保留两位小数 不四舍五入
  1091. /// </summary>
  1092. /// <param name="number"></param>
  1093. /// <returns></returns>
  1094. public static decimal ConvertToDecimal1(this decimal myDecimal)
  1095. {
  1096. var subDecimal = Math.Floor(myDecimal * 100) / 100;//保留两位小数,直接截取
  1097. return subDecimal;
  1098. }
  1099. /// <summary>
  1100. /// 列表的成员移动到列表的前面
  1101. /// </summary>
  1102. /// <typeparam name="T"></typeparam>
  1103. /// <param name="list"></param>
  1104. /// <param name="index"></param>
  1105. public static void MoveItemAtIndexToFront<T>(this List<T> list, int index)
  1106. {
  1107. T item = list[index];
  1108. list.RemoveAt(index);
  1109. list.Insert(0, item);
  1110. }
  1111. /// <summary>
  1112. /// 获取请假类型
  1113. /// </summary>
  1114. /// <param name="template_id"></param>
  1115. /// <returns></returns>
  1116. public static async Task<List<VacationLeaveTypeView>> GetVacationLeaveTypes(string template_id)
  1117. {
  1118. List<VacationLeaveTypeView> vacationLeaveTypes = new List<VacationLeaveTypeView>();
  1119. TemplateDetailView templateDetailView = new TemplateDetailView();
  1120. templateDetailView = await _qiYeWeChatApiService.GetTemplateDetailAsync(template_id);
  1121. if (templateDetailView.errcode != 0)
  1122. {
  1123. Serilog.Log.Error("【企业微信】【审批】【获取假勤类型的审批】【Msg】"+ templateDetailView.errmsg);
  1124. return vacationLeaveTypes;
  1125. }
  1126. List<VacationItemInfo> VacationItemInfos = templateDetailView.vacation_list.item;
  1127. foreach (var item in VacationItemInfos)
  1128. {
  1129. ValueItem valueInfo = item.name.Where(it => it.lang == "zh_CN").FirstOrDefault();
  1130. if (valueInfo != null)
  1131. {
  1132. vacationLeaveTypes.Add(
  1133. new VacationLeaveTypeView()
  1134. {
  1135. id = item.id,
  1136. name = valueInfo.text
  1137. });
  1138. }
  1139. }
  1140. return vacationLeaveTypes;
  1141. }
  1142. /// <summary>
  1143. /// 计算类型费用
  1144. /// 病假 事假 计算 按天计算
  1145. /// </summary>
  1146. /// <param name="leaveType">
  1147. /// 1年假;2事假;3病假;4调休假;5婚假;6产假;7陪产假;8其他;9丧假
  1148. /// </param>
  1149. /// <param name="date_Range_type">
  1150. /// halfday 全天
  1151. /// hour 小时
  1152. /// </param>
  1153. /// <param name="startTime"></param>
  1154. /// <param name="endTime"></param>
  1155. /// <param name="duration"></param>
  1156. /// <param name="mealDeduction"></param>
  1157. /// <param name="typeDeduction"></param>
  1158. public static void CalculateTypeFee1(List<LeaveDetails> leaveDetails, int leaveType, string date_Range_type, DateTime startTime, DateTime endTime,
  1159. decimal amountPayable, int work_days, decimal duration, out decimal mealDeduction, out decimal typeDeduction)
  1160. {
  1161. typeDeduction = 0;
  1162. mealDeduction = 0;
  1163. decimal personalkLeave_dailywage_day = amountPayable / work_days; //日薪 = 事假日薪 *计算方式:日平均工资 = 当月应发工资 /当月应出勤天数。
  1164. //半小时单位
  1165. decimal halfHour = 7.50M / 0.50M;
  1166. switch (leaveType)
  1167. {
  1168. case 1: //年假
  1169. if (date_Range_type == "halfday")
  1170. {
  1171. if (duration >= 0.5M && duration <= 1M) //一天
  1172. {
  1173. var njItem = leaveDetails.Where(it => it.StartDt.ToString("yyyy-MM-dd") == startTime.ToString("yyyy-MM-dd") &&
  1174. it.EndDt.ToString("yyyy-MM-dd") == endTime.ToString("yyyy-MM-dd")).ToList();
  1175. if (njItem.Count > 1)
  1176. {
  1177. if (njItem[0].StartDt == startTime)
  1178. {
  1179. mealDeduction = 10; //餐补扣款
  1180. }
  1181. }
  1182. else
  1183. {
  1184. mealDeduction = 10; //餐补扣款
  1185. }
  1186. }
  1187. else // 多天
  1188. {
  1189. var njManyDaysItem = leaveDetails.Where(it => it.StartDt.ToString("yyyy-MM-dd") == startTime.ToString("yyyy-MM-dd")).ToList();
  1190. if (njManyDaysItem.Count > 1)
  1191. {
  1192. var njManyDaysItem1 = njManyDaysItem.Where(it => it.StartDt != startTime).ToList();
  1193. if (njManyDaysItem1.Count > 0)
  1194. {
  1195. if (njManyDaysItem1[0].Unit.Equals("天"))
  1196. {
  1197. mealDeduction = 10 * Math.Floor(duration);
  1198. }
  1199. else if (njManyDaysItem1[0].Unit.Equals("小时") && njManyDaysItem1[0].New_Duration >= 3)
  1200. {
  1201. mealDeduction = 10 * Math.Floor(duration);
  1202. }
  1203. else
  1204. {
  1205. mealDeduction = 10 * Math.Ceiling(duration);
  1206. }
  1207. }
  1208. }
  1209. else
  1210. {
  1211. mealDeduction = 10 * Math.Ceiling(duration);
  1212. }
  1213. }
  1214. }
  1215. break;
  1216. case 2: //2事假
  1217. // 事假日薪 *计算方式:日平均工资 = 当月应发工资 /当月应出勤天数。
  1218. decimal personalkLeave_dailywage_halfhour = personalkLeave_dailywage_day / 7.50M; //事假单位 0.5小时
  1219. if (date_Range_type == "halfday")
  1220. {
  1221. #region 处理当天是否扣除餐补
  1222. string formatDt_begin = startTime.ToString("yyyy-MM-dd");
  1223. List<LeaveDetails> leaveDetails1 = leaveDetails.Where(it => it.IsDeduction && it.EndDt.ToString("yyyy-MM-dd") == formatDt_begin).ToList();
  1224. if (leaveDetails1.Count > 0) mealDeduction = 10.00M * Math.Floor(duration); //餐补扣款
  1225. else mealDeduction = 10.00M * Math.Ceiling(duration); //餐补扣款
  1226. #endregion
  1227. if (duration % 1 == 0) //整天
  1228. {
  1229. typeDeduction = ConvertToDecimal(personalkLeave_dailywage_day * duration);
  1230. }
  1231. else //多含 半天 另外处理
  1232. {
  1233. decimal sj_wholeDay = Math.Floor(duration); //整天
  1234. decimal sj_halfDay = duration % 1; //半天
  1235. if (duration % 1 > 0)
  1236. {
  1237. typeDeduction = (personalkLeave_dailywage_day / 2.00M) * (duration / 0.50M);
  1238. }
  1239. else
  1240. {
  1241. typeDeduction = (personalkLeave_dailywage_day / 2.00M);
  1242. }
  1243. typeDeduction = ConvertToDecimal(typeDeduction);
  1244. }
  1245. }
  1246. else if (date_Range_type == "hour")
  1247. {
  1248. decimal leave_halfHour = Convert.ToDecimal(duration) / Convert.ToDecimal(0.5);
  1249. typeDeduction = ConvertToDecimal(personalkLeave_dailywage_halfhour * leave_halfHour);
  1250. //duration = 11M;
  1251. if (duration >= 3 && duration < 7.5M) //单天请假三小时
  1252. {
  1253. mealDeduction = 10; //餐补扣款
  1254. }
  1255. else if (duration >= 7.5M) //多天计算
  1256. {
  1257. decimal leave_halfHour1 = Convert.ToDecimal(duration) / Convert.ToDecimal(0.5);
  1258. decimal leaveDays = duration / 7.5M;
  1259. if (leaveDays % 1 == 0)
  1260. {
  1261. typeDeduction = ConvertToDecimal(personalkLeave_dailywage_day * leaveDays);
  1262. mealDeduction = 10 * leaveDays; //餐补扣款
  1263. }
  1264. else
  1265. {
  1266. typeDeduction = personalkLeave_dailywage_day * Convert.ToInt32(leaveDays);
  1267. decimal sy_shijiaunit = leave_halfHour1 - Convert.ToDecimal(15.00M * Convert.ToInt32(leaveDays));
  1268. if (sy_shijiaunit > 0)
  1269. {
  1270. typeDeduction += ConvertToDecimal(personalkLeave_dailywage_halfhour * sy_shijiaunit);
  1271. }
  1272. mealDeduction = 10 * Convert.ToInt32(leaveDays);
  1273. //得到最后一天的请假时间 是否有餐补
  1274. int lastHours = (Convert.ToDateTime(endTime) - Convert.ToDateTime("09:00")).Hours;
  1275. if (lastHours >= 3)
  1276. {
  1277. mealDeduction += 10; //餐补扣款
  1278. }
  1279. }
  1280. }
  1281. }
  1282. break;
  1283. case 3: //3病假
  1284. // 病假日薪 *计算方式:日平均工资 = 成都市最低工资标准的80% /当月应出勤天数。 短期病假=当月15天内
  1285. decimal chengDuMinimumWage_Day = _chengDuMinimumWage / work_days;
  1286. decimal chengDuMinimumWage_halrHour = chengDuMinimumWage_Day / 7.50M;
  1287. decimal sickLeave_dailywage_halfhour_deduction1 = (personalkLeave_dailywage_day / 7.50M) - chengDuMinimumWage_halrHour; //病假单位 0.5小时 扣款金额
  1288. if (date_Range_type == "halfday")
  1289. {
  1290. mealDeduction = 10.00M * Math.Ceiling(duration); //餐补扣款
  1291. decimal pl_dailywage_day = personalkLeave_dailywage_day - chengDuMinimumWage_Day;
  1292. if (duration % 1 == 0) //整天
  1293. {
  1294. typeDeduction = ConvertToDecimal(pl_dailywage_day * duration);
  1295. }
  1296. else //多含 半天 另外处理
  1297. {
  1298. #region 处理当天是否扣除餐补
  1299. string formatDt_begin = startTime.ToString("yyyy-MM-dd");
  1300. List<LeaveDetails> leaveDetails1 = leaveDetails.Where(it => it.IsDeduction && it.EndDt.ToString("yyyy-MM-dd") == formatDt_begin).ToList();
  1301. if (leaveDetails1.Count > 0) mealDeduction = 10.00M * Math.Floor(duration); //餐补扣款
  1302. else mealDeduction = 10.00M * Math.Ceiling(duration); //餐补扣款
  1303. #endregion
  1304. if (duration % 1 > 0)
  1305. {
  1306. typeDeduction = (pl_dailywage_day / 2.00M) * (duration / 0.50M); ;
  1307. }
  1308. else
  1309. {
  1310. typeDeduction = (pl_dailywage_day / 2.00M);
  1311. }
  1312. typeDeduction = ConvertToDecimal(typeDeduction);
  1313. }
  1314. }
  1315. else if (date_Range_type == "hour")
  1316. {
  1317. decimal sickLeave_halfHour = duration / 0.5M;
  1318. typeDeduction = ConvertToDecimal(sickLeave_dailywage_halfhour_deduction1 * sickLeave_halfHour);
  1319. if (duration >= 3 && duration < 7.5M) //单天请假三小时 && 请假时间在上午 则没有餐补
  1320. {
  1321. mealDeduction = 10; //餐补扣款
  1322. }
  1323. else if (duration >= 7.5M) //多天计算
  1324. {
  1325. decimal sickLeave_halfHour1 = duration / 0.5M;
  1326. decimal leaveDays = Convert.ToDecimal(duration / 7.5M);
  1327. typeDeduction = ConvertToDecimal(sickLeave_dailywage_halfhour_deduction1 * sickLeave_halfHour1);
  1328. if (leaveDays % 1 == 0)
  1329. {
  1330. mealDeduction = 10 * leaveDays; //餐补扣款
  1331. }
  1332. else
  1333. {
  1334. mealDeduction = 10 * Convert.ToInt32(leaveDays);
  1335. typeDeduction = ConvertToDecimal(sickLeave_dailywage_halfhour_deduction1 * Convert.ToInt32(leaveDays));
  1336. decimal sy_bingjiaunit = sickLeave_halfHour1 - Convert.ToDecimal(15.00M * Convert.ToInt32(leaveDays));
  1337. if (sy_bingjiaunit > 0)
  1338. {
  1339. typeDeduction += ConvertToDecimal(sickLeave_dailywage_halfhour_deduction1 * sy_bingjiaunit);
  1340. }
  1341. //得到最后一天的请假时间 是否有餐补
  1342. int lastHours = (Convert.ToDateTime(endTime) - Convert.ToDateTime("09:00")).Hours;
  1343. if (lastHours >= 3)
  1344. {
  1345. mealDeduction += 10; //餐补扣款
  1346. }
  1347. }
  1348. }
  1349. }
  1350. break;
  1351. case 4: //4调休假
  1352. CalculateTypeFeeSub(leaveDetails, date_Range_type, startTime, endTime, duration, out mealDeduction);
  1353. break;
  1354. case 5: //5婚假
  1355. CalculateTypeFeeSub(leaveDetails, date_Range_type, startTime, endTime, duration, out mealDeduction);
  1356. break;
  1357. case 6: //6产假
  1358. CalculateTypeFeeSub(leaveDetails, date_Range_type, startTime, endTime, duration, out mealDeduction);
  1359. break;
  1360. case 7: //7陪产假
  1361. CalculateTypeFeeSub(leaveDetails, date_Range_type, startTime, endTime, duration, out mealDeduction);
  1362. break;
  1363. case 8: //8其他
  1364. CalculateTypeFeeSub(leaveDetails, date_Range_type, startTime, endTime, duration, out mealDeduction);
  1365. break;
  1366. case 9: //9丧假
  1367. if (date_Range_type == "halfday")
  1368. {
  1369. if (duration >= 0.5M && duration <= 1M) //一天
  1370. {
  1371. var njItem = leaveDetails.Where(it => it.StartDt.ToString("yyyy-MM-dd") == startTime.ToString("yyyy-MM-dd") &&
  1372. it.EndDt.ToString("yyyy-MM-dd") == endTime.ToString("yyyy-MM-dd")).ToList();
  1373. if (njItem.Count > 1)
  1374. {
  1375. if (njItem[0].StartDt == startTime)
  1376. {
  1377. mealDeduction = 10; //餐补扣款
  1378. }
  1379. }
  1380. else
  1381. {
  1382. mealDeduction = 10; //餐补扣款
  1383. }
  1384. }
  1385. else // 多天
  1386. {
  1387. var njManyDaysItem = leaveDetails.Where(it => it.StartDt.ToString("yyyy-MM-dd") == startTime.ToString("yyyy-MM-dd")).ToList();
  1388. if (njManyDaysItem.Count > 1)
  1389. {
  1390. var njManyDaysItem1 = njManyDaysItem.Where(it => it.StartDt != startTime).ToList();
  1391. if (njManyDaysItem1.Count > 0)
  1392. {
  1393. if (njManyDaysItem1[0].Unit.Equals("天"))
  1394. {
  1395. mealDeduction = 10 * Math.Floor(duration);
  1396. }
  1397. else if (njManyDaysItem1[0].Unit.Equals("小时") && njManyDaysItem1[0].New_Duration >= 3)
  1398. {
  1399. mealDeduction = 10 * Math.Floor(duration);
  1400. }
  1401. else
  1402. {
  1403. mealDeduction = 10 * Math.Ceiling(duration);
  1404. }
  1405. }
  1406. }
  1407. else
  1408. {
  1409. mealDeduction = 10 * Math.Ceiling(duration);
  1410. }
  1411. }
  1412. }
  1413. break;
  1414. }
  1415. }
  1416. /// <summary>
  1417. /// 计算类型费用
  1418. /// 病假 事假 计算 按小时计算
  1419. /// </summary>
  1420. /// <param name="leaveType">
  1421. /// 1年假;2事假;3病假;4调休假;5婚假;6产假;7陪产假;8其他;9丧假
  1422. /// </param>
  1423. /// <param name="date_Range_type">
  1424. /// halfday 全天
  1425. /// hour 小时
  1426. /// </param>
  1427. /// <param name="startTime"></param>
  1428. /// <param name="endTime"></param>
  1429. /// <param name="duration"></param>
  1430. /// <param name="mealDeduction"></param>
  1431. /// <param name="typeDeduction"></param>
  1432. public static void CalculateTypeFee2(List<LeaveDetails> leaveDetails,int leaveType, string date_Range_type, DateTime startTime, DateTime endTime,
  1433. decimal amountPayable,int work_days, decimal duration, out decimal mealDeduction, out decimal typeDeduction)
  1434. {
  1435. typeDeduction = 0;
  1436. mealDeduction = 0;
  1437. string am_starttime = "08:59";
  1438. string am_endtime = "13:01";
  1439. decimal personalkLeave_dailywage_day = amountPayable / work_days; //日薪 = 事假日薪 *计算方式:日平均工资 = 当月应发工资 /当月应出勤天数。
  1440. //半小时单位
  1441. decimal halfHour = 7.50M / 0.50M;
  1442. switch (leaveType)
  1443. {
  1444. case 1: //年假
  1445. if (date_Range_type == "halfday")
  1446. {
  1447. if (duration >= 0.5M && duration <= 1M) //一天
  1448. {
  1449. var njItem = leaveDetails.Where(it => it.StartDt.ToString("yyyy-MM-dd") == startTime.ToString("yyyy-MM-dd") &&
  1450. it.EndDt.ToString("yyyy-MM-dd") == endTime.ToString("yyyy-MM-dd")).ToList();
  1451. if (njItem.Count > 1)
  1452. {
  1453. if (njItem[0].StartDt == startTime)
  1454. {
  1455. mealDeduction = 10; //餐补扣款
  1456. }
  1457. }
  1458. else
  1459. {
  1460. mealDeduction = 10; //餐补扣款
  1461. }
  1462. }
  1463. else // 多天
  1464. {
  1465. var njManyDaysItem = leaveDetails.Where(it => it.StartDt.ToString("yyyy-MM-dd") == startTime.ToString("yyyy-MM-dd")).ToList();
  1466. if (njManyDaysItem.Count > 1)
  1467. {
  1468. var njManyDaysItem1 = njManyDaysItem.Where(it => it.StartDt != startTime).ToList();
  1469. if (njManyDaysItem1.Count > 0)
  1470. {
  1471. if (njManyDaysItem1[0].Unit.Equals("天"))
  1472. {
  1473. mealDeduction = 10 * Math.Floor(duration);
  1474. }
  1475. else if (njManyDaysItem1[0].Unit.Equals("小时") && njManyDaysItem1[0].New_Duration >= 3)
  1476. {
  1477. mealDeduction = 10 * Math.Floor(duration);
  1478. }
  1479. else
  1480. {
  1481. mealDeduction = 10 * Math.Ceiling(duration);
  1482. }
  1483. }
  1484. }
  1485. else
  1486. {
  1487. mealDeduction = 10 * Math.Ceiling(duration);
  1488. }
  1489. }
  1490. }
  1491. break;
  1492. case 2: //2事假
  1493. // 事假日薪 *计算方式:日平均工资 = 当月应发工资 /当月应出勤天数。
  1494. decimal personalkLeave_dailywage_halfhour = personalkLeave_dailywage_day / 7.50M; //事假单位 0.5小时
  1495. if (date_Range_type == "halfday")
  1496. {
  1497. mealDeduction = 10.00M * Math.Floor(duration); //餐补扣款
  1498. if (duration % 1 == 0) //整天
  1499. {
  1500. typeDeduction = ConvertToDecimal(personalkLeave_dailywage_day * duration);
  1501. }
  1502. else //多含 半天 另外处理
  1503. {
  1504. decimal sj_wholeDay = Math.Floor(duration); //整天
  1505. decimal sj_halfDay = duration % 1; //半天
  1506. if (sj_halfDay > 0)
  1507. {
  1508. LeaveDetails sjDetails = leaveDetails.Where(it => it.StartDt == startTime && it.EndDt == endTime).FirstOrDefault();
  1509. decimal sjPrice = 0.00M;
  1510. if (sjDetails != null)
  1511. {
  1512. Slice_info sj_slice_Info = sjDetails.SliceInfo;
  1513. if (sj_slice_Info != null)
  1514. {
  1515. decimal sjLongTime = (sj_slice_Info.duration / 3600.00M);
  1516. typeDeduction = (personalkLeave_dailywage_day / 7.50M) * sjLongTime;
  1517. }
  1518. }
  1519. LeaveDetails sjDetailsMeal = leaveDetails.Where(it => it.EndDt == startTime ).FirstOrDefault();
  1520. if (sjDetailsMeal == null)
  1521. {
  1522. mealDeduction += 10.00M;
  1523. }
  1524. }
  1525. else
  1526. {
  1527. typeDeduction = personalkLeave_dailywage_day* sj_wholeDay;
  1528. }
  1529. typeDeduction = ConvertToDecimal(typeDeduction);
  1530. }
  1531. }
  1532. else if (date_Range_type == "hour")
  1533. {
  1534. decimal leave_halfHour = Convert.ToDecimal(duration) / Convert.ToDecimal(0.5);
  1535. typeDeduction = ConvertToDecimal(personalkLeave_dailywage_halfhour * leave_halfHour);
  1536. //duration = 11M;
  1537. if (duration >= 3 && duration < 7.5M) //单天请假三小时
  1538. {
  1539. mealDeduction = 10; //餐补扣款
  1540. }
  1541. else if (duration >= 7.5M) //多天计算
  1542. {
  1543. decimal leave_halfHour1 = Convert.ToDecimal(duration) / Convert.ToDecimal(0.5);
  1544. decimal leaveDays = duration / 7.5M;
  1545. if (leaveDays % 1 == 0)
  1546. {
  1547. typeDeduction = ConvertToDecimal(personalkLeave_dailywage_day * leaveDays);
  1548. mealDeduction = 10 * leaveDays; //餐补扣款
  1549. }
  1550. else
  1551. {
  1552. typeDeduction = personalkLeave_dailywage_day * Convert.ToInt32(leaveDays);
  1553. decimal sy_shijiaunit = leave_halfHour1 - Convert.ToDecimal (15.00M * Convert.ToInt32(leaveDays));
  1554. if (sy_shijiaunit > 0)
  1555. {
  1556. typeDeduction += ConvertToDecimal(personalkLeave_dailywage_halfhour * sy_shijiaunit);
  1557. }
  1558. mealDeduction = 10 * Convert.ToInt32(leaveDays);
  1559. //得到最后一天的请假时间 是否有餐补
  1560. int lastHours = (Convert.ToDateTime(endTime) - Convert.ToDateTime("09:00")).Hours;
  1561. if (lastHours >= 3)
  1562. {
  1563. mealDeduction += 10; //餐补扣款
  1564. }
  1565. }
  1566. }
  1567. }
  1568. break;
  1569. case 3: //3病假
  1570. // 病假日薪 *计算方式:日平均工资 = 成都市最低工资标准的80% /当月应出勤天数。 短期病假=当月15天内
  1571. decimal chengDuMinimumWage_Day = _chengDuMinimumWage / work_days;
  1572. decimal chengDuMinimumWage_halrHour = chengDuMinimumWage_Day / 7.50M;
  1573. decimal sickLeave_dailywage_halfhour_deduction1 = (personalkLeave_dailywage_day / 7.50M) - chengDuMinimumWage_halrHour; //病假单位 0.5小时 扣款金额
  1574. if (date_Range_type == "halfday")
  1575. {
  1576. mealDeduction = 10.00M * Math.Ceiling(duration); //餐补扣款
  1577. decimal pl_dailywage_day = personalkLeave_dailywage_day - chengDuMinimumWage_Day;
  1578. if (duration % 1 == 0) //整天
  1579. {
  1580. typeDeduction = ConvertToDecimal(pl_dailywage_day * duration);
  1581. }
  1582. else //多含 半天 另外处理
  1583. {
  1584. decimal bj_wholeDay = Math.Floor(duration); //整天
  1585. decimal bj_halfDay = duration % 1; //半天
  1586. decimal bj_wholeDayWage = pl_dailywage_day * bj_wholeDay;
  1587. decimal bj_halfDayWage = 0.00M;
  1588. if (bj_halfDay > 0)
  1589. {
  1590. LeaveDetails bjDetails = leaveDetails.Where(it => it.StartDt == startTime && it.EndDt == endTime).FirstOrDefault();
  1591. decimal bjPrice = 0.00M;
  1592. if (bjDetails != null)
  1593. {
  1594. Slice_info bj_slice_Info = bjDetails.SliceInfo;
  1595. decimal bjLongTime = bj_slice_Info.duration / 3600.00M;
  1596. if (bjLongTime == 3.00M)
  1597. {
  1598. bj_halfDayWage = (pl_dailywage_day / 7.50M) * 3.00M;
  1599. }
  1600. else if (bjLongTime == 4.50M)
  1601. {
  1602. bj_halfDayWage = (pl_dailywage_day / 7.50M) * 4.50M;
  1603. }
  1604. }
  1605. }
  1606. typeDeduction = ConvertToDecimal(bj_wholeDayWage + bj_halfDayWage);
  1607. }
  1608. }
  1609. else if (date_Range_type == "hour")
  1610. {
  1611. decimal sickLeave_halfHour = duration / 0.5M;
  1612. typeDeduction = ConvertToDecimal(sickLeave_dailywage_halfhour_deduction1 * sickLeave_halfHour);
  1613. if (duration >= 3 && duration < 7.5M) //单天请假三小时 && 请假时间在上午 则没有餐补
  1614. {
  1615. mealDeduction = 10; //餐补扣款
  1616. }
  1617. else if (duration >= 7.5M) //多天计算
  1618. {
  1619. decimal sickLeave_halfHour1 = duration / 0.5M;
  1620. decimal leaveDays = Convert.ToDecimal(duration / 7.5M);
  1621. typeDeduction = ConvertToDecimal(sickLeave_dailywage_halfhour_deduction1 * sickLeave_halfHour1);
  1622. if (leaveDays % 1 == 0)
  1623. {
  1624. mealDeduction = 10 * leaveDays; //餐补扣款
  1625. }
  1626. else
  1627. {
  1628. mealDeduction = 10 * Convert.ToInt32(leaveDays);
  1629. typeDeduction = ConvertToDecimal(sickLeave_dailywage_halfhour_deduction1 * Convert.ToInt32(leaveDays));
  1630. decimal sy_bingjiaunit = sickLeave_halfHour1 - Convert.ToDecimal(15.00M * Convert.ToInt32(leaveDays));
  1631. if (sy_bingjiaunit > 0)
  1632. {
  1633. typeDeduction += ConvertToDecimal(sickLeave_dailywage_halfhour_deduction1 * sy_bingjiaunit);
  1634. }
  1635. //得到最后一天的请假时间 是否有餐补
  1636. int lastHours = (Convert.ToDateTime(endTime) - Convert.ToDateTime("09:00")).Hours;
  1637. if (lastHours >= 3)
  1638. {
  1639. mealDeduction += 10; //餐补扣款
  1640. }
  1641. }
  1642. }
  1643. }
  1644. break;
  1645. case 4: //4调休假
  1646. CalculateTypeFeeSub(leaveDetails,date_Range_type, startTime, endTime, duration, out mealDeduction);
  1647. break;
  1648. case 5: //5婚假
  1649. CalculateTypeFeeSub(leaveDetails,date_Range_type, startTime, endTime, duration, out mealDeduction);
  1650. break;
  1651. case 6: //6产假
  1652. CalculateTypeFeeSub(leaveDetails,date_Range_type, startTime, endTime, duration, out mealDeduction);
  1653. break;
  1654. case 7: //7陪产假
  1655. CalculateTypeFeeSub(leaveDetails,date_Range_type, startTime, endTime, duration, out mealDeduction);
  1656. break;
  1657. case 8: //8其他
  1658. CalculateTypeFeeSub(leaveDetails,date_Range_type, startTime, endTime, duration, out mealDeduction);
  1659. break;
  1660. case 9: //9丧假
  1661. CalculateTypeFeeSub(leaveDetails,date_Range_type, startTime, endTime, duration, out mealDeduction);
  1662. break;
  1663. }
  1664. }
  1665. /// <summary>
  1666. /// 计算类型费用
  1667. /// </summary>
  1668. /// <param name="date_Range_type">
  1669. /// halfday 全天
  1670. /// hour 小时
  1671. /// </param>
  1672. /// <param name="startTime"></param>
  1673. /// <param name="endTime"></param>
  1674. /// <param name="duration"></param>
  1675. /// <param name="mealDeduction"></param>
  1676. public static void CalculateTypeFeeSub(List<LeaveDetails> leaveDetails,string date_Range_type, DateTime startTime1, DateTime endTime1,
  1677. decimal duration, out decimal mealDeduction)
  1678. {
  1679. mealDeduction = 0;
  1680. string am_starttime = "09:00";
  1681. string am_endtime = "11:59";
  1682. string startTime = startTime1.ToString("HH:mm:ss");
  1683. string endTime = endTime1.ToString("HH:mm:ss");
  1684. if (date_Range_type == "halfday")
  1685. {
  1686. mealDeduction = duration * 10; //餐补扣款
  1687. }
  1688. else if (date_Range_type == "hour")
  1689. {
  1690. List<LeaveDetails> leaveDetails1 = new List<LeaveDetails>();
  1691. leaveDetails1 = leaveDetails.Where(it => it.StartDt.ToString("yyyy-MM-dd").Equals(startTime1.ToString("yyyy-MM-dd")))
  1692. .OrderBy(it => it.StartDt).ToList();
  1693. if (leaveDetails1.Count > 1)
  1694. {
  1695. decimal timelenTatoal = leaveDetails1.Sum(it => it.New_Duration);
  1696. if (timelenTatoal >= 3.00M)
  1697. {
  1698. if (leaveDetails1[0].StartDt == startTime1)
  1699. {
  1700. mealDeduction = 10; //餐补扣款
  1701. }
  1702. }
  1703. }
  1704. else
  1705. {
  1706. if (duration >= 3 && duration < 7) //单天请假三小时 && 请假时间在上午 则没有餐补
  1707. {
  1708. mealDeduction = 10; //餐补扣款
  1709. }
  1710. else if (duration >= 7 && duration <= 7.50M)
  1711. {
  1712. mealDeduction = 10; //餐补扣款
  1713. }
  1714. else if (duration >= 7.50M) //多天计算
  1715. {
  1716. decimal leaveDays = Convert.ToDecimal(duration / 7.50M);
  1717. if (leaveDays % 1 == 0)
  1718. {
  1719. mealDeduction = 10 * leaveDays; //餐补扣款
  1720. }
  1721. else
  1722. {
  1723. mealDeduction = 10 * Convert.ToInt32(leaveDays);
  1724. //得到最后一天的请假时间 是否有餐补
  1725. int lastHours = (Convert.ToDateTime(endTime) - Convert.ToDateTime("09:00")).Hours;
  1726. if (lastHours >= 3)
  1727. {
  1728. mealDeduction += 10; //餐补扣款
  1729. ////处理结束时间
  1730. //if (endTime.CompareTo(am_starttime) > 0 && endTime.CompareTo(am_endtime) < 0)
  1731. //{
  1732. // mealDeduction += 10; //餐补扣款
  1733. //}
  1734. }
  1735. }
  1736. }
  1737. }
  1738. }
  1739. }
  1740. /// <summary>
  1741. /// 获取打卡补卡类型
  1742. /// </summary>
  1743. /// <param name="template_id"></param>
  1744. /// <returns></returns>
  1745. public static async Task<List<VacationLeaveTypeView>> GetVacationReissueCardTypes(string template_id)
  1746. {
  1747. List<VacationLeaveTypeView> vacationLeaveTypes = new List<VacationLeaveTypeView>();
  1748. TemplateDetailView templateDetailView = new TemplateDetailView();
  1749. templateDetailView = await _qiYeWeChatApiService.GetTemplateDetailAsync(template_id);
  1750. if (templateDetailView.errcode != 0)
  1751. {
  1752. return vacationLeaveTypes;
  1753. }
  1754. List<VacationItemInfo> VacationItemInfos = templateDetailView.vacation_list.item;
  1755. foreach (var item in VacationItemInfos)
  1756. {
  1757. ValueItem valueInfo = item.name.Where(it => it.lang == "zh_CN").FirstOrDefault();
  1758. if (valueInfo != null)
  1759. {
  1760. vacationLeaveTypes.Add(
  1761. new VacationLeaveTypeView()
  1762. {
  1763. id = item.id,
  1764. name = valueInfo.text
  1765. });
  1766. }
  1767. }
  1768. return vacationLeaveTypes;
  1769. }
  1770. /// <summary>
  1771. /// 打卡数据
  1772. /// 假勤数据 统计
  1773. /// </summary>
  1774. /// <param name="datas">数据源</param>
  1775. /// <param name="type">
  1776. /// 1-请假;2-补卡;3-出差;4-外出;100-外勤;
  1777. /// </param>
  1778. /// <param name="subTypeName">
  1779. /// 年假 事假 病假 调休假 婚嫁 产假 陪产假 丧假 补卡次数 出差 外出数 外勤 其他
  1780. /// </param>
  1781. /// <returns></returns>
  1782. private static int Fallibilitydispose(List<Sp_Item> datas, int type, string? subTypeName)
  1783. {
  1784. int num = 0;
  1785. Sp_Item _Info = datas.Where(it => it.type == type && it.name == subTypeName).FirstOrDefault();
  1786. if (_Info != null) { num = _Info.count; }
  1787. return num;
  1788. }
  1789. /// <summary>
  1790. /// 打卡数据
  1791. /// 异常数据 统计
  1792. /// </summary>
  1793. /// <returns></returns>
  1794. private static int ExceptionStatistics(List<Exception_Info> datas, int type)
  1795. {
  1796. int num = 0;
  1797. Exception_Info _Info = datas.Where(it => it.exception == type).FirstOrDefault();
  1798. if (_Info != null) { num = _Info.count; }
  1799. return num;
  1800. }
  1801. /// <summary>
  1802. /// 获取时间段内除周末 节假日外的 工作日
  1803. /// </summary>
  1804. /// <param name="startDt"></param>
  1805. /// <param name="endDt"></param>
  1806. /// <returns></returns>
  1807. public static async Task<int> GetWorkDays(string yearMonth)
  1808. {
  1809. int workdays = 0;
  1810. string sql = string.Format(@"Select * From Pm_WageIssueWorkingDay
  1811. Where Isdel = 0 And YearMonth = '{0}' ", yearMonth);
  1812. var data = await _usersRep._sqlSugar.SqlQueryable<WageYearMonthView>(sql).FirstAsync();
  1813. if (data != null)
  1814. {
  1815. workdays = data.Workdays;
  1816. }
  1817. return workdays;
  1818. }
  1819. }
  1820. }