PayrollComputation.cs 78 KB

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