PayrollComputation.cs 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. using Google.Protobuf.WellKnownTypes;
  2. using NPOI.OpenXmlFormats.Spreadsheet;
  3. using NPOI.SS.Formula.Functions;
  4. using OASystem.API.OAMethodLib.QiYeWeChatAPI;
  5. using OASystem.Domain;
  6. using OASystem.Domain.Entities.PersonnelModule;
  7. using OASystem.Domain.ViewModels.PersonnelModule;
  8. using OASystem.Domain.ViewModels.QiYeWeChat;
  9. using OASystem.Infrastructure.Repositories.Groups;
  10. using System;
  11. using System.Diagnostics.Eventing.Reader;
  12. using System.Linq.Expressions;
  13. using TencentCloud.Ocr.V20181119.Models;
  14. namespace OASystem.API.OAMethodLib
  15. {
  16. /// <summary>
  17. /// 工资计算
  18. /// </summary>
  19. public static class PayrollComputation
  20. {
  21. private static Result _result = new Result();
  22. private static readonly IQiYeWeChatApiService _qiYeWeChatApiService = AutofacIocManager.Instance.GetService<IQiYeWeChatApiService>();
  23. private static readonly UsersRepository _usersRep = AutofacIocManager.Instance.GetService<UsersRepository>();
  24. private static readonly IMapper _mapper = AutofacIocManager.Instance.GetService<IMapper>();
  25. private static readonly decimal _chengDuMinimumWage = 2100.00M;
  26. /// <summary>
  27. /// 计算工资
  28. /// </summary>
  29. /// <param name="pm_WageSheetDattaSources"></param>
  30. /// <param name="userNames"></param>
  31. /// <param name="thisYearMonth"></param>
  32. /// <param name="startDt"></param>
  33. /// <param name="endDt"></param>
  34. /// <returns></returns>
  35. public static async Task<Result> SalaryCalculatorAsync(
  36. List<Pm_WageSheet> pm_WageSheetDattaSources, List<UserNameView> userNames, int userId, string thisYearMonth, DateTime startDt, DateTime endDt)
  37. {
  38. if (pm_WageSheetDattaSources.Count <= 0)
  39. {
  40. _result.Msg = "计算工资传入数据为空!";
  41. return _result;
  42. }
  43. if (userNames.Count <= 0)
  44. {
  45. var nameData = await _usersRep.GetUserNameList(1);
  46. userNames = nameData.Data;
  47. }
  48. //获取所有打卡数据
  49. CheckInView checkIn = await _qiYeWeChatApiService.GetCheckin_MonthDataAsync(startDt, endDt); //时间段内所有 打卡数据
  50. if (checkIn.errcode != 0)
  51. {
  52. _result.Msg = "【企业微信】【打卡】【获取所有月打卡数据】【Msg】" + checkIn.errmsg;
  53. return _result;
  54. }
  55. List<Data> checkInDatas = checkIn.datas;
  56. //获取 请假类型 Sp_Detail.template_id
  57. string leave_template_id = "C4NzTJCh1onCUK915rRkvy7Fh5Vqz4YbiEV9jrBY1";
  58. List<VacationLeaveTypeView> vacationLeaveTypes = await GetVacationLeaveTypes(leave_template_id);
  59. if (vacationLeaveTypes.Count <= 0)
  60. {
  61. _result.Msg = startDt + " - " + endDt + "请假 类型数据 获取失败!";
  62. return _result;
  63. }
  64. _result.Msg = string.Empty;
  65. try
  66. {
  67. foreach (var pm_wsInfo in pm_WageSheetDattaSources)
  68. {
  69. string itemName = userNames.Where(it => it.Id == pm_wsInfo.UserId).FirstOrDefault().CnName;
  70. //补贴 金额
  71. decimal meal_subsidy = 0.00M; // 午餐(午餐10元/天) 补贴 * 计算方式:单日上午请假时长(小时)大于或者等于三小时 没有餐补
  72. //事假 病假 总金额
  73. decimal personalLeaveTotal = 0.00M, // 事假 日薪 *计算方式:日平均工资 = 月工资/当月应出勤天数。
  74. sickLeaveTotal = 0.00M; // 病假 日薪 *计算方式:日平均工资 = 成都市最低工资标准的80%/当月应出勤天数。 短期病假=当月15天内
  75. //扣款金额
  76. decimal beLate_deduction = 0.00M, // 迟到 扣款金额 *计算方式:
  77. // 一个自然月内,不足 10 分钟的迟到/早退,不超过 2 次的部分,不做处罚;3 次及以上,按50元 / 次处罚;
  78. // 超过 10 分钟(含 10 分钟),不足 60 分钟的迟到/早退,按 50 元/次处罚;
  79. // 超过 60 分钟(含 60 分钟),不足 3 小时的迟到/早退,且无请假者,按旷工半日处理;超过3 小时的迟到 / 早退,且无请假者,按旷工一日处理。
  80. early_deduction = 0.00M, // 早退 扣款金额
  81. absenteeism_deduction = 0.00M, // 旷工 扣款金额 *计算方式:旷工扣发当日工资
  82. unprinted_deduction = 0.00M, // 未打卡 扣款金额 *计算方式:
  83. // 试用期员工每月有 2 次 补卡机会,超过 2 次不足 5 次的部分,按 10 元 / 次处罚,5 次及以上的漏卡,按 50 元 / 次处罚;
  84. // 正式员工每月 3 次以内的补卡,按 10 元 / 次处罚,3 次及以上的漏卡,按 50 元 / 次处罚。
  85. sickLeave_deduction = 0.00M, // 病假
  86. other_deduction = 0.00M; // 其他 扣款金额
  87. decimal meal_deduction = 0.00M; // 餐补 扣款金额
  88. decimal reissuecard_deduction = 0.00M; // 补卡 扣款金额
  89. //打卡数据
  90. Data? checkInData = checkInDatas.Where(it => it.base_info.name == itemName).FirstOrDefault();
  91. if (checkInData == null) { continue; }
  92. string acctid = checkInData.base_info.acctid; //用户Id
  93. //当月总计数据
  94. Summary_Info? summary_Info = checkInData.summary_info;
  95. if (summary_Info == null) { continue; }
  96. int work_days = summary_Info.work_days; //应出勤天数
  97. int regular_days = summary_Info.regular_days; //正常出勤天数
  98. meal_subsidy = work_days * 10; //应发放餐补
  99. #region 计算日工资 正常日薪 事假日薪 病假日薪
  100. //月 - 应发工资
  101. decimal amountPayable = pm_wsInfo.Basic + pm_wsInfo.Floats + pm_wsInfo.PostAllowance + pm_wsInfo.InformationSecurityFee + pm_wsInfo.OtherSubsidies;
  102. // 日薪 = *计算方式:日平均工资 = 月工资/当月应出勤天数。
  103. decimal dailyWage = ConvertToDecimal(amountPayable / work_days);
  104. // 病假日薪 *计算方式:日平均工资 = 成都市最低工资标准的80%/当月应出勤天数。 短期病假=当月15天内
  105. decimal sickLeave_dailywage = ConvertToDecimal(_chengDuMinimumWage / work_days);
  106. //病假 一天扣款
  107. sickLeave_deduction = dailyWage - sickLeave_dailywage;
  108. #endregion
  109. int annualLeaveNum = 0, //年假
  110. personalLeaveNum = 0, //事假
  111. sickLeaveNum = 0, //病假
  112. lieuLeaveNum = 0, //调休假
  113. marriageLeaveNum = 0, //婚嫁
  114. maternityLeaveNum = 0, //产假
  115. paternityLeaveNum = 0, //陪产假
  116. funeralLeaveNum = 0, //丧假
  117. reissueCardNum = 0, //补卡 次数
  118. evectionNum = 0, //出差 次数
  119. outIngNum = 0, //外出 次数
  120. outWorkNum = 0, //外勤 次数
  121. otherLeaveNum = 0; //其他
  122. #region 假勤 处理 1-请假;2-补卡;3-出差;4-外出;100-外勤;
  123. List<Sp_Item>? sp_items = checkInData.sp_items.Where(it => it.count != 0).ToList();
  124. if (sp_items != null && sp_items.Count > 0)
  125. {
  126. annualLeaveNum = Fallibilitydispose(sp_items, 1, "年假");
  127. personalLeaveNum = Fallibilitydispose(sp_items, 1, "事假");
  128. sickLeaveNum = Fallibilitydispose(sp_items, 1, "病假");
  129. lieuLeaveNum = Fallibilitydispose(sp_items, 1, "调休假");
  130. marriageLeaveNum = Fallibilitydispose(sp_items, 1, "婚嫁");
  131. maternityLeaveNum = Fallibilitydispose(sp_items, 1, "产假");
  132. paternityLeaveNum = Fallibilitydispose(sp_items, 1, "陪产假");
  133. funeralLeaveNum = Fallibilitydispose(sp_items, 1, "丧假");
  134. otherLeaveNum = Fallibilitydispose(sp_items, 1, "其他");
  135. reissueCardNum = Fallibilitydispose(sp_items, 2, "补卡次数");
  136. evectionNum = Fallibilitydispose(sp_items, 3, "出差");
  137. outIngNum = Fallibilitydispose(sp_items, 4, "外出");
  138. outWorkNum = Fallibilitydispose(sp_items, 3, "外勤");
  139. }
  140. #region 请假类型金额/餐补 处理
  141. List<Sp_Detail> sp_leave_details = new List<Sp_Detail>();
  142. sp_leave_details = await _qiYeWeChatApiService.GetApprovalDetailsAsync(startDt, endDt, acctid, 2, 1); //时间段内所有 已同意的 请假 审批数据
  143. if (sp_leave_details.Count <= 0)
  144. {
  145. _result.Msg += startDt + " - " + endDt + " " + itemName + " 请假 审批数据获取未获取到!\r\n";
  146. //continue;
  147. }
  148. List<Ex_Items> ex_Items = new List<Ex_Items>();//假勤 And 打卡备注集合
  149. Ex_Items ex_Items_jq = new Ex_Items() { Type = "假勤" }; //假勤
  150. List<Ex_Item> ex_ItemInfos = new List<Ex_Item>();
  151. foreach (Sp_Detail sp_item in sp_leave_details)
  152. {
  153. Apply_data? apply_data = sp_item.apply_data;
  154. if (apply_data != null)
  155. {
  156. List<ContentsItem> contents = apply_data.contents;
  157. ContentsItem content_Vacation = contents.Where(it => it.control == "Vacation").FirstOrDefault(); //请假类型
  158. ContentsItem content_Textarea = contents.Where(it => it.control == "Textarea").FirstOrDefault(); //多行文本
  159. if (content_Vacation != null)
  160. {
  161. Vacation vacation = content_Vacation.value.vacation;
  162. Attendance attendance = vacation.attendance; //假勤组件
  163. Selector selector = vacation.selector; //请假类型
  164. List<OptionsItem> optionsItems = selector.options; //key 请假类型 id
  165. List<TitleItem> value = optionsItems[0].value; // value 文本描述值
  166. int leaveType = int.Parse(optionsItems[0].key); //key 请假子类型 id
  167. Date_range date_Range = attendance.date_range;
  168. //筛选 不在工作日内的假勤申请
  169. if (startDt >= date_Range.new_begin_dt && date_Range.new_end_dt <= endDt)
  170. {
  171. continue;
  172. }
  173. decimal thisTypeDeduction = 0.00M;//当前类型扣款
  174. string leave_starttime = date_Range.new_begin_dt.ToString("HH:mm");
  175. string leave_endtime = date_Range.new_end_dt.ToString("HH:mm");
  176. string typeName = string.Empty;
  177. int leaveTypeId = leaveType;
  178. var leaveTypeData = vacationLeaveTypes.Where(it => it.id == leaveTypeId).FirstOrDefault();
  179. if (leaveTypeData != null) { typeName = leaveTypeData.name; }
  180. //计算请假类型扣款金额
  181. decimal new_duration = 0.00M;
  182. if (date_Range.type == "halfday")
  183. {
  184. new_duration = Convert.ToDecimal(date_Range.new_duration) / 86400M;
  185. }
  186. else if (date_Range.type == "hour")
  187. {
  188. new_duration = Convert.ToDecimal(date_Range.new_duration) / 3600M;
  189. }
  190. decimal leave_meals = 0.00M;
  191. //计算餐补 假勤类型扣款
  192. CalculateTypeFee(leaveType, date_Range.type, leave_starttime, leave_endtime, amountPayable, work_days, new_duration,
  193. out leave_meals, out thisTypeDeduction);
  194. #region 累计类型扣款
  195. //1年假;2事假;3病假;4调休假;5婚假;6产假;7陪产假;8其他;9丧假
  196. if (leaveType == 2) //事假
  197. {
  198. personalLeaveTotal += thisTypeDeduction;
  199. }
  200. else if (leaveType == 3) //病假
  201. {
  202. sickLeaveTotal = thisTypeDeduction;
  203. }
  204. #endregion
  205. meal_deduction += leave_meals;
  206. string startEndTiime = startEndTiime = date_Range.new_begin_dt.ToString("yyyy-MM-dd HH:mm") + " - " +
  207. date_Range.new_end_dt.ToString("yyyy-MM-dd HH:mm");
  208. Ex_Item ex_Item = new Ex_Item()
  209. {
  210. SubTypeId = leaveType,
  211. SubType = typeName,
  212. StartTimeDt = Convert.ToDateTime(date_Range.new_begin_dt.ToString("yyyy-MM-dd HH:mm:ss")),
  213. EndTimeDt = Convert.ToDateTime(date_Range.new_end_dt.ToString("yyyy-MM-dd HH:mm:ss")),
  214. Duration = new_duration,
  215. Deduction = thisTypeDeduction,
  216. //Reason = apply_data.reason,
  217. Apply_time_dt = Convert.ToDateTime(sp_item.apply_time_dt.ToString("yyyy-MM-dd HH:mm:ss")),
  218. //Approval_name = sp_item.approval_name,
  219. };
  220. ex_ItemInfos.Add(ex_Item);
  221. }
  222. }
  223. }
  224. if (ex_ItemInfos.Count > 0)
  225. {
  226. ex_Items_jq.Ex_ItemInfo = ex_ItemInfos.OrderBy(it => it.SubTypeId).ThenBy(it => it.Apply_time_dt).ToList();
  227. ex_Items.Add(ex_Items_jq);
  228. }
  229. #endregion
  230. #endregion
  231. Ex_Items ex_Items_dk = new Ex_Items() { Type = "打卡" }; //打卡
  232. List<Ex_Item> ex_reissuecard_Items = new List<Ex_Item>();
  233. #region 打卡补卡 补卡次数 处理
  234. if (reissueCardNum > 0)
  235. {
  236. List<Sp_Detail> sp_buka_details = new List<Sp_Detail>();
  237. sp_leave_details = await _qiYeWeChatApiService.GetApprovalDetailsAsync(startDt, endDt, acctid, 2, 2); //时间段内所有 已同意的 请假 审批数据
  238. int bukaNum = 1;
  239. foreach (var item in sp_leave_details)
  240. {
  241. decimal bukaPrice = 0.00M;
  242. if (bukaNum <= 2)
  243. {
  244. bukaPrice = 10.00M;
  245. }
  246. else {
  247. bukaPrice = 50.00M;
  248. }
  249. var app_data = item.apply_data;
  250. var punch_correction1 = app_data.contents[0].value.punch_correction; //未打卡时间
  251. var punch_correction2 = app_data.contents[1].value;
  252. Ex_Item ex_reissueCard = new Ex_Item()
  253. {
  254. SubTypeId = 7,
  255. SubType = "打卡补卡",
  256. StartTimeDt = Convert.ToDateTime(punch_correction1.time_dt.ToString("yyyy-MM-dd HH:mm:ss")), //未打卡时间
  257. Deduction = bukaPrice,
  258. Reason = punch_correction2.text,
  259. unit = string.Empty
  260. };
  261. unprinted_deduction += bukaPrice;
  262. ex_reissuecard_Items.Add(ex_reissueCard);
  263. bukaNum++;
  264. }
  265. }
  266. #endregion
  267. List<Sp_Detail> sp_reissuecard_details = new List<Sp_Detail>();
  268. //统计迟到次数
  269. int beLateNum = 0, // 1-迟到;
  270. leaveEarlyNum = 0, // 2-早退;
  271. dummyDeckNum = 0, // 3-缺卡;
  272. minerNum = 0, // 4-旷工;
  273. locationAnomalyNum = 0, // 5-地点异常;
  274. unitExNum = 0; // 6-设备异常;
  275. if (summary_Info.except_days > 0)
  276. {
  277. List<Exception_Info>? ex_infos = checkInData.exception_infos;
  278. if (ex_infos != null && ex_infos.Count >= 0)
  279. {
  280. beLateNum = ExceptionStatistics(ex_infos, 1);
  281. leaveEarlyNum = ExceptionStatistics(ex_infos, 2);
  282. dummyDeckNum = ExceptionStatistics(ex_infos, 3);
  283. minerNum = ExceptionStatistics(ex_infos, 4);
  284. locationAnomalyNum = ExceptionStatistics(ex_infos, 5);
  285. unitExNum = ExceptionStatistics(ex_infos, 6);
  286. }
  287. }
  288. //打卡记录
  289. CheckInDataView checkInDataView = new CheckInDataView();
  290. checkInDataView = await _qiYeWeChatApiService.GetCheckinDataAsync(new List<string>() { acctid }, 3, startDt, endDt);
  291. if (checkInDataView.errcode != 0)
  292. {
  293. _result.Msg += startDt + " - " + endDt + " " + itemName + " 打卡记录 " + checkInDataView.errmsg + " \r\n";
  294. }
  295. //筛选时间异常的打卡记录
  296. List<CheckInDataInfo> checkInDataInfos = checkInDataView.checkindata.Where(it => !string.IsNullOrEmpty(it.exception_type)).ToList();
  297. //处理 未打卡的记录是否已经通过假勤审批
  298. List<CheckInDataInfo> leave_checkInDataInfos = new List<CheckInDataInfo>();
  299. if (checkInDataInfos.Count > 0)
  300. {
  301. foreach (var leaveItem in ex_ItemInfos)
  302. {
  303. if (leaveItem.StartTimeDt.ToString("yyyy-MM-dd").Equals(leaveItem.EndTimeDt.ToString("yyyy-MM-dd"))) //单天
  304. {
  305. leave_checkInDataInfos.AddRange(checkInDataInfos.Where(it => it.checkin_time_dt.ToString("yyyy-MM-dd") == leaveItem.StartTimeDt.ToString("yyyy-MM-dd")).ToList());
  306. }
  307. else //多天
  308. {
  309. leave_checkInDataInfos.AddRange(checkInDataInfos.Where(it => it.checkin_time_dt >= leaveItem.StartTimeDt && it.checkin_time_dt <= leaveItem.EndTimeDt).ToList());
  310. }
  311. }
  312. }
  313. //处理 时间异常的记录是否已经通过补卡审批
  314. List<CheckInDataInfo> pullcard_checkInDataInfos = new List<CheckInDataInfo>();
  315. if (leave_checkInDataInfos.Count > 0)
  316. {
  317. //获取异常打卡数据 并且 未通过假勤审批
  318. pullcard_checkInDataInfos = checkInDataInfos.Except(leave_checkInDataInfos).ToList();
  319. if (pullcard_checkInDataInfos.Count > 0)
  320. {
  321. CheckInDayDataView checkInDayDataView = await _qiYeWeChatApiService.GetCheckInDayDataAsync(new List<string>() { acctid }, startDt, endDt);
  322. if (checkInDayDataView.errcode != 0)
  323. {
  324. _result.Msg += startDt + " - " + endDt + " " + itemName + " 异常信息数据获取未获取到!\r\n";
  325. }
  326. List<Root> roots_words = checkInDayDataView.datas.Where(it => it.base_info.day_type == 0).ToList(); //获取工作日日报信息
  327. List<Root> roots_exs = checkInDayDataView.datas.Where(it => it.exception_infos.Count > 0).ToList();
  328. foreach (var roots_ex in roots_exs)
  329. {
  330. foreach (var exception_info in roots_ex.exception_infos)
  331. {
  332. decimal timelength = (Convert.ToDecimal(exception_info.duration) / 3600.00M) * 60.00M; //时长 分钟
  333. if (timelength == 9) timelength = 7.50M;
  334. int exception = exception_info.exception; //异常类型
  335. decimal day_miner_unit = ConvertToDecimal(dailyWage / 15); //以0.5小时为单位
  336. //1:一个自然月内,不足 10 分钟的迟到/早退,不超过 2 次的部分,不做处罚;3 次及以上,按50 元 / 次处罚;
  337. //2:超过 10 分钟(含 10 分钟),不足 60 分钟的迟到 / 早退,按 50 元 / 次处罚;
  338. //3:超过 60 分钟(含 60 分钟),不足 3 小时的迟到 / 早退,且无请假者,按旷工半日处理;超过 3 小时的迟到 / 早退,且无请假者,按旷工一日处理。
  339. Ex_Item beLate_belate_ex = new Ex_Item()
  340. {
  341. SubTypeId = 4,
  342. SubType = "旷工",
  343. Duration = timelength,
  344. StartTimeDt = roots_ex.base_info.dateDt,
  345. unit = "分钟",
  346. };
  347. decimal day_deduction = 0.00M;
  348. //1 - 迟到;2 - 早退;3 - 缺卡;4 - 旷工;5 - 地点异常;6 - 设备异常
  349. if (exception == 1) //迟到
  350. {
  351. if (timelength < 10)
  352. {
  353. reissueCardNum++;
  354. beLate_belate_ex.SubTypeId = 1;
  355. beLate_belate_ex.SubType = "迟到";
  356. if (reissueCardNum >= 3)
  357. {
  358. day_deduction = 50.00M;
  359. beLate_deduction += day_deduction; //迟到扣款 总额
  360. }
  361. }
  362. else if (timelength >= 10 && timelength <= 60)
  363. {
  364. day_deduction = 50.00M;
  365. beLate_deduction += day_deduction; //迟到扣款 总额
  366. beLate_belate_ex.SubTypeId = 1;
  367. beLate_belate_ex.SubType = "迟到";
  368. }
  369. else if (timelength > 60 && timelength <= 180)
  370. {
  371. day_deduction = day_miner_unit * 6; //3小时
  372. meal_deduction += 10.00M; //餐补扣款
  373. absenteeism_deduction += day_deduction; //矿工半日
  374. }
  375. else
  376. {
  377. day_deduction = dailyWage;
  378. absenteeism_deduction += day_deduction; //矿工一日
  379. meal_deduction += 10.00M;
  380. }
  381. beLate_belate_ex.Deduction = day_deduction;
  382. ex_reissuecard_Items.Add(beLate_belate_ex);
  383. }
  384. else if (exception == 2) //早退
  385. {
  386. if (timelength < 10)
  387. {
  388. reissueCardNum++;
  389. beLate_belate_ex.SubTypeId = 2;
  390. beLate_belate_ex.SubType = "早退";
  391. if (reissueCardNum >= 3)
  392. {
  393. day_deduction = 50.00M;
  394. early_deduction += day_deduction; //早退扣款 总计
  395. }
  396. }
  397. else if (timelength >= 10 && timelength <= 60)
  398. {
  399. day_deduction = 50.00M;
  400. early_deduction += day_deduction; //早退扣款 总计
  401. beLate_belate_ex.SubTypeId = 2;
  402. beLate_belate_ex.SubType = "早退";
  403. }
  404. else if (timelength > 60 && timelength <= 180)
  405. {
  406. day_deduction = day_miner_unit * 6; //3小时
  407. absenteeism_deduction += day_deduction; //矿工半日
  408. }
  409. else
  410. {
  411. day_deduction = dailyWage;
  412. meal_deduction += 10.00M;
  413. absenteeism_deduction += day_deduction; //矿工一日
  414. }
  415. beLate_belate_ex.Deduction = day_deduction;
  416. ex_reissuecard_Items.Add(beLate_belate_ex);
  417. }
  418. else if (exception == 3) //缺卡
  419. {
  420. if (roots_ex.exception_infos.Count == 2)
  421. {
  422. day_deduction = dailyWage;
  423. meal_deduction += 10.00M;
  424. absenteeism_deduction += day_deduction;
  425. beLate_belate_ex.Reason = "上午-下午 缺卡/未打卡为旷工 一天";
  426. beLate_belate_ex.Deduction = day_deduction;
  427. ex_reissuecard_Items.Add(beLate_belate_ex);
  428. }
  429. else if (roots_ex.exception_infos.Count == 1)
  430. {
  431. long date = roots_ex.base_info.date; //当日工作日期
  432. long earliest_time = roots_ex.summary_info.earliest_time; //最早打卡时间
  433. long lastest_time = roots_ex.summary_info.lastest_time; //最晚打卡时间
  434. if (earliest_time == lastest_time)
  435. {
  436. long this_date = date + earliest_time;
  437. DateTime thisDt = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)).AddTicks(this_date * 10000000);
  438. DateTime thisDt1 = Convert.ToDateTime(thisDt.ToString("yyyy-MM-dd") + " 12:00");
  439. if (thisDt <= thisDt1) //旷工 下午
  440. {
  441. day_deduction = day_miner_unit * 9; //4.5小时
  442. meal_deduction += 10.00M;
  443. absenteeism_deduction += day_deduction; //矿工半日
  444. beLate_belate_ex.Reason = thisDt.ToString("yyyy-MM-dd") + "下午缺卡/未打卡为旷工 4.5小时";
  445. }
  446. else if (thisDt >= thisDt1) //旷工下午
  447. {
  448. day_deduction = day_miner_unit * 6; //3小时
  449. absenteeism_deduction += day_deduction; //矿工半日
  450. beLate_belate_ex.Reason = thisDt.ToString("yyyy-MM-dd") + "上午缺卡/未打卡为旷工 3小时";
  451. }
  452. else //矿工一日
  453. {
  454. day_deduction = dailyWage;
  455. meal_deduction += 10.00M;
  456. absenteeism_deduction += day_deduction;
  457. beLate_belate_ex.Reason = thisDt.ToString("yyyy-MM-dd") + "上午-下午 缺卡/未打卡为旷工 一天";
  458. }
  459. beLate_belate_ex.Deduction = day_deduction;
  460. ex_reissuecard_Items.Add(beLate_belate_ex);
  461. }
  462. }
  463. }
  464. else if (exception == 4) //旷工
  465. {
  466. if (timelength > 60 && timelength <= 180)
  467. {
  468. day_deduction = day_miner_unit * 6; //3小时
  469. meal_deduction += 10.00M;
  470. absenteeism_deduction += day_deduction; //矿工半日
  471. }
  472. else
  473. {
  474. day_deduction = dailyWage;
  475. meal_deduction += 10.00M;
  476. absenteeism_deduction += day_deduction; //矿工一日
  477. }
  478. beLate_belate_ex.Deduction = day_deduction;
  479. ex_reissuecard_Items.Add(beLate_belate_ex);
  480. }
  481. }
  482. }
  483. }
  484. }
  485. if (ex_reissuecard_Items.Count > 0)
  486. {
  487. ex_Items_dk.Ex_ItemInfo = ex_reissuecard_Items;
  488. ex_Items_dk.Ex_ItemInfo = ex_reissuecard_Items.OrderBy(it => it.SubTypeId).ThenBy(it => it.StartTimeDt).ToList();
  489. ex_Items.Add(ex_Items_dk);
  490. }
  491. #region 应发合计 实发合计 扣款合计(假勤扣款,其他扣款,社保扣款,公积金代扣,个税扣款)
  492. decimal mealTotal = meal_subsidy - meal_deduction; //餐补
  493. decimal salaryTotal = amountPayable + mealTotal; //应发合计
  494. //扣款合计 不含个税
  495. decimal eductionTotal = sickLeaveTotal + personalLeaveTotal + beLate_deduction + early_deduction + absenteeism_deduction + unprinted_deduction + other_deduction +
  496. pm_wsInfo.WithholdingInsurance + pm_wsInfo.ReservedFunds + pm_wsInfo.OtherDeductions;
  497. decimal actualReleaseTotal = salaryTotal - eductionTotal; //实发合计 * 不含个税
  498. #endregion
  499. #region 处理当月工资数据
  500. pm_wsInfo.YearMonth = thisYearMonth;
  501. pm_wsInfo.StartDate = startDt.ToString("yyyy-MM-dd");
  502. pm_wsInfo.EndDate = endDt.ToString("yyyy-MM-dd");
  503. pm_wsInfo.WorkDays = work_days; //当月应出勤天数
  504. pm_wsInfo.RegularDays = regular_days; //当月正常出勤天数
  505. pm_wsInfo.SickLeave = sickLeaveTotal; //病假
  506. pm_wsInfo.SomethingFalse = personalLeaveTotal; //事假
  507. pm_wsInfo.LateTo = beLate_deduction; //迟到
  508. pm_wsInfo.LeaveEarly = early_deduction; //早退
  509. pm_wsInfo.Absenteeism = absenteeism_deduction; //旷工
  510. pm_wsInfo.NotPunch = unprinted_deduction; //未打卡
  511. pm_wsInfo.OtherDeductions = other_deduction; //其他
  512. pm_wsInfo.Ex_ItemsRemark = JsonConvert.SerializeObject(ex_Items); //
  513. pm_wsInfo.Mealsupplement = mealTotal; //餐补
  514. pm_wsInfo.Should = salaryTotal; //应发合计
  515. pm_wsInfo.TotalDeductions = eductionTotal; //扣款合计
  516. pm_wsInfo.TotalRealHair = actualReleaseTotal; //实发合计
  517. pm_wsInfo.AfterTax = actualReleaseTotal - pm_wsInfo.WithholdingTax; //税后工资
  518. pm_wsInfo.LastUpdateUserId = userId;
  519. pm_wsInfo.LastUpdateDt = DateTime.Now;
  520. pm_wsInfo.CreateUserId = userId;
  521. pm_wsInfo.CreateTime = DateTime.Now;
  522. pm_wsInfo.DeleteUserId = null;
  523. pm_wsInfo.DeleteTime = null;
  524. #endregion
  525. }
  526. }
  527. catch (Exception ex)
  528. {
  529. _result.Msg = ex.Message;
  530. return _result;
  531. }
  532. _result.Code = 0;
  533. _result.Data = pm_WageSheetDattaSources;
  534. return _result;
  535. }
  536. /// <summary>
  537. /// decimal 保留两位小数 不四舍五入
  538. /// </summary>
  539. /// <param name="number"></param>
  540. /// <returns></returns>
  541. private static decimal ConvertToDecimal(decimal number)
  542. {
  543. return Convert.ToDecimal(number.ToString("0.00"));
  544. }
  545. /// <summary>
  546. /// 获取请假类型
  547. /// </summary>
  548. /// <param name="template_id"></param>
  549. /// <returns></returns>
  550. public static async Task<List<VacationLeaveTypeView>> GetVacationLeaveTypes(string template_id)
  551. {
  552. List<VacationLeaveTypeView> vacationLeaveTypes = new List<VacationLeaveTypeView>();
  553. TemplateDetailView templateDetailView = new TemplateDetailView();
  554. templateDetailView = await _qiYeWeChatApiService.GetTemplateDetailAsync(template_id);
  555. if (templateDetailView.errcode != 0)
  556. {
  557. Serilog.Log.Error("【企业微信】【审批】【获取假勤类型的审批】【Msg】"+ templateDetailView.errmsg);
  558. return vacationLeaveTypes;
  559. }
  560. List<VacationItemInfo> VacationItemInfos = templateDetailView.vacation_list.item;
  561. foreach (var item in VacationItemInfos)
  562. {
  563. ValueItem valueInfo = item.name.Where(it => it.lang == "zh_CN").FirstOrDefault();
  564. if (valueInfo != null)
  565. {
  566. vacationLeaveTypes.Add(
  567. new VacationLeaveTypeView()
  568. {
  569. id = item.id,
  570. name = valueInfo.text
  571. });
  572. }
  573. }
  574. return vacationLeaveTypes;
  575. }
  576. /// <summary>
  577. /// 计算类型费用
  578. /// </summary>
  579. /// <param name="leaveType">
  580. /// 1年假;2事假;3病假;4调休假;5婚假;6产假;7陪产假;8其他;9丧假
  581. /// </param>
  582. /// <param name="date_Range_type">
  583. /// halfday 全天
  584. /// hour 小时
  585. /// </param>
  586. /// <param name="startTime"></param>
  587. /// <param name="endTime"></param>
  588. /// <param name="duration"></param>
  589. /// <param name="mealDeduction"></param>
  590. /// <param name="typeDeduction"></param>
  591. public static void CalculateTypeFee(int leaveType, string date_Range_type, string startTime, string endTime,decimal amountPayable,int work_days,
  592. decimal duration, out decimal mealDeduction, out decimal typeDeduction)
  593. {
  594. typeDeduction = 0;
  595. mealDeduction = 0;
  596. string am_starttime = "08:59";
  597. string am_endtime = "13:01";
  598. decimal personalkLeave_dailywage_day = ConvertToDecimal( amountPayable / work_days); //日薪 = 事假日薪 *计算方式:日平均工资 = 当月应发工资 /当月应出勤天数。
  599. //半小时单位
  600. decimal halfHour = Convert.ToDecimal(7.5) / Convert.ToDecimal(0.5);
  601. switch (leaveType)
  602. {
  603. case 1: //年假
  604. CalculateTypeFeeSub(date_Range_type, startTime, endTime, duration, out mealDeduction);
  605. break;
  606. case 2: //2事假
  607. // 事假日薪 *计算方式:日平均工资 = 当月应发工资 /当月应出勤天数。
  608. decimal personalkLeave_dailywage_halfhour = ConvertToDecimal( personalkLeave_dailywage_day / halfHour); //事假单位 0.5小时
  609. if (date_Range_type == "halfday")
  610. {
  611. mealDeduction = 10; //餐补扣款
  612. typeDeduction = personalkLeave_dailywage_day;
  613. }
  614. else if (date_Range_type == "hour")
  615. {
  616. decimal leave_halfHour = Convert.ToDecimal(duration) / Convert.ToDecimal(0.5);
  617. typeDeduction = personalkLeave_dailywage_halfhour * leave_halfHour;
  618. if (duration >= 3 && duration <= 7.5M) //单天请假三小时 && 请假时间在上午 则没有餐补
  619. {
  620. //处理开始时间
  621. if (startTime.CompareTo(am_starttime) > 0 && startTime.CompareTo(am_endtime) < 0)
  622. {
  623. //处理结束时间
  624. //if (endTime.CompareTo(am_starttime) > 0 && endTime.CompareTo(am_endtime) > 0)
  625. //{
  626. mealDeduction = 10; //餐补扣款
  627. //}
  628. }
  629. }
  630. else if (duration > 7.5M) //多天计算
  631. {
  632. decimal leave_halfHour1 = Convert.ToDecimal(duration) / Convert.ToDecimal(0.5);
  633. typeDeduction = personalkLeave_dailywage_halfhour * leave_halfHour1;
  634. decimal leaveDays = duration / 7.5M;
  635. if (leaveDays % 1 == 0)
  636. {
  637. mealDeduction = 10 * leaveDays; //餐补扣款
  638. }
  639. else
  640. {
  641. mealDeduction = 10 * Convert.ToInt32(leaveDays);
  642. //得到最后一天的请假时间 是否有餐补
  643. int lastHours = (Convert.ToDateTime(endTime) - Convert.ToDateTime("09:00")).Hours;
  644. if (lastHours >= 3)
  645. {
  646. //处理结束时间
  647. if (endTime.CompareTo(am_starttime) > 0 && endTime.CompareTo(am_endtime) < 0)
  648. {
  649. mealDeduction += 10; //餐补扣款
  650. }
  651. }
  652. }
  653. }
  654. }
  655. break;
  656. case 3: //3病假
  657. // 病假日薪 *计算方式:日平均工资 = 成都市最低工资标准的80% /当月应出勤天数。 短期病假=当月15天内
  658. decimal chengDuMinimumWage_halrHour = ConvertToDecimal( _chengDuMinimumWage / work_days) / halfHour;
  659. decimal sickLeave_dailywage_halfhour_deduction = personalkLeave_dailywage_day - chengDuMinimumWage_halrHour; //病假单位 0.5小时 扣款金额
  660. if (date_Range_type == "halfday")
  661. {
  662. mealDeduction = 10; //餐补扣款
  663. typeDeduction = sickLeave_dailywage_halfhour_deduction * halfHour;
  664. }
  665. else if (date_Range_type == "hour")
  666. {
  667. decimal sickLeave_halfHour = duration / 0.5M;
  668. typeDeduction = sickLeave_dailywage_halfhour_deduction *sickLeave_halfHour ;
  669. if (duration >= 3 && duration <= 7.5M) //单天请假三小时 && 请假时间在上午 则没有餐补
  670. {
  671. //处理开始时间
  672. if (startTime.CompareTo(am_starttime) > 0 && startTime.CompareTo(am_endtime) < 0)
  673. {
  674. //处理结束时间
  675. if (endTime.CompareTo(am_starttime) > 0 && endTime.CompareTo(am_endtime) < 0)
  676. {
  677. mealDeduction = 10; //餐补扣款
  678. }
  679. }
  680. }
  681. else if (duration > 7.5M) //多天计算
  682. {
  683. decimal sickLeave_halfHour1 = duration / 0.5M;
  684. typeDeduction = sickLeave_dailywage_halfhour_deduction * sickLeave_halfHour1;
  685. decimal leaveDays = Convert.ToDecimal(duration / 7.5M);
  686. if (leaveDays % 1 == 0)
  687. {
  688. mealDeduction = 10 * leaveDays; //餐补扣款
  689. }
  690. else
  691. {
  692. mealDeduction = 10 * Convert.ToInt32(leaveDays);
  693. //得到最后一天的请假时间 是否有餐补
  694. int lastHours = (Convert.ToDateTime(endTime) - Convert.ToDateTime("09:00")).Hours;
  695. if (lastHours >= 3)
  696. {
  697. //处理结束时间
  698. if (endTime.CompareTo(am_starttime) > 0 && endTime.CompareTo(am_endtime) < 0)
  699. {
  700. mealDeduction += 10; //餐补扣款
  701. }
  702. }
  703. }
  704. }
  705. }
  706. break;
  707. case 4: //4调休假
  708. CalculateTypeFeeSub(date_Range_type, startTime, endTime, duration, out mealDeduction);
  709. break;
  710. case 5: //5婚假
  711. CalculateTypeFeeSub(date_Range_type, startTime, endTime, duration, out mealDeduction);
  712. break;
  713. case 6: //6产假
  714. CalculateTypeFeeSub(date_Range_type, startTime, endTime, duration, out mealDeduction);
  715. break;
  716. case 7: //7陪产假
  717. CalculateTypeFeeSub(date_Range_type, startTime, endTime, duration, out mealDeduction);
  718. break;
  719. case 8: //8其他
  720. CalculateTypeFeeSub(date_Range_type, startTime, endTime, duration, out mealDeduction);
  721. break;
  722. case 9: //9丧假
  723. CalculateTypeFeeSub(date_Range_type, startTime, endTime, duration, out mealDeduction);
  724. break;
  725. }
  726. }
  727. /// <summary>
  728. /// 计算类型费用
  729. /// </summary>
  730. /// <param name="date_Range_type">
  731. /// halfday 全天
  732. /// hour 小时
  733. /// </param>
  734. /// <param name="startTime"></param>
  735. /// <param name="endTime"></param>
  736. /// <param name="duration"></param>
  737. /// <param name="mealDeduction"></param>
  738. public static void CalculateTypeFeeSub(string date_Range_type, string startTime, string endTime, decimal duration, out decimal mealDeduction)
  739. {
  740. mealDeduction = 0;
  741. string am_starttime = "08:59";
  742. string am_endtime = "13:01";
  743. if (date_Range_type == "halfday")
  744. {
  745. mealDeduction = 10; //餐补扣款
  746. }
  747. else if (date_Range_type == "hour")
  748. {
  749. if (duration >= 3 && duration < 7) //单天请假三小时 && 请假时间在上午 则没有餐补
  750. {
  751. //处理开始时间
  752. if (startTime.CompareTo(am_starttime) > 0 && startTime.CompareTo(am_endtime) < 0)
  753. {
  754. //处理结束时间
  755. if (endTime.CompareTo(am_starttime) > 0 && endTime.CompareTo(am_endtime) < 0)
  756. {
  757. mealDeduction = 10; //餐补扣款
  758. }
  759. }
  760. }
  761. else if (duration >= 7 && duration <= 7.50M )
  762. {
  763. mealDeduction = 10; //餐补扣款
  764. }
  765. else if (duration >= 7.50M) //多天计算
  766. {
  767. decimal leaveDays = Convert.ToDecimal(duration / 7.50M);
  768. if (leaveDays % 1 == 0)
  769. {
  770. mealDeduction = 10 * leaveDays; //餐补扣款
  771. }
  772. else
  773. {
  774. mealDeduction = 10 * Convert.ToInt32(leaveDays);
  775. //得到最后一天的请假时间 是否有餐补
  776. int lastHours = (Convert.ToDateTime(endTime) - Convert.ToDateTime("09:00")).Hours;
  777. if (lastHours >= 3)
  778. {
  779. //处理结束时间
  780. if (endTime.CompareTo(am_starttime) > 0 && endTime.CompareTo(am_endtime) < 0)
  781. {
  782. mealDeduction += 10; //餐补扣款
  783. }
  784. }
  785. }
  786. }
  787. }
  788. }
  789. /// <summary>
  790. /// 获取打卡补卡类型
  791. /// </summary>
  792. /// <param name="template_id"></param>
  793. /// <returns></returns>
  794. public static async Task<List<VacationLeaveTypeView>> GetVacationReissueCardTypes(string template_id)
  795. {
  796. List<VacationLeaveTypeView> vacationLeaveTypes = new List<VacationLeaveTypeView>();
  797. TemplateDetailView templateDetailView = new TemplateDetailView();
  798. templateDetailView = await _qiYeWeChatApiService.GetTemplateDetailAsync(template_id);
  799. if (templateDetailView.errcode != 0)
  800. {
  801. return vacationLeaveTypes;
  802. }
  803. List<VacationItemInfo> VacationItemInfos = templateDetailView.vacation_list.item;
  804. foreach (var item in VacationItemInfos)
  805. {
  806. ValueItem valueInfo = item.name.Where(it => it.lang == "zh_CN").FirstOrDefault();
  807. if (valueInfo != null)
  808. {
  809. vacationLeaveTypes.Add(
  810. new VacationLeaveTypeView()
  811. {
  812. id = item.id,
  813. name = valueInfo.text
  814. });
  815. }
  816. }
  817. return vacationLeaveTypes;
  818. }
  819. /// <summary>
  820. /// 打卡数据
  821. /// 假勤数据 统计
  822. /// </summary>
  823. /// <param name="datas">数据源</param>
  824. /// <param name="type">
  825. /// 1-请假;2-补卡;3-出差;4-外出;100-外勤;
  826. /// </param>
  827. /// <param name="subTypeName">
  828. /// 年假 事假 病假 调休假 婚嫁 产假 陪产假 丧假 补卡次数 出差 外出数 外勤 其他
  829. /// </param>
  830. /// <returns></returns>
  831. private static int Fallibilitydispose(List<Sp_Item> datas, int type, string? subTypeName)
  832. {
  833. int num = 0;
  834. Sp_Item _Info = datas.Where(it => it.type == type && it.name == subTypeName).FirstOrDefault();
  835. if (_Info != null) { num = _Info.count; }
  836. return num;
  837. }
  838. /// <summary>
  839. /// 打卡数据
  840. /// 异常数据 统计
  841. /// </summary>
  842. /// <returns></returns>
  843. private static int ExceptionStatistics(List<Exception_Info> datas, int type)
  844. {
  845. int num = 0;
  846. Exception_Info _Info = datas.Where(it => it.exception == type).FirstOrDefault();
  847. if (_Info != null) { num = _Info.count; }
  848. return num;
  849. }
  850. }
  851. }