PayrollComputation.cs 75 KB

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