PayrollComputation.cs 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733
  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> SalaryCalculatorAsync1(
  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. int work_days = await GetWorkDays(thisYearMonth);
  50. //获取所有打卡数据
  51. CheckInView checkIn = await _qiYeWeChatApiService.GetCheckin_MonthDataAsync(startDt, endDt); //时间段内所有 打卡数据
  52. if (checkIn.errcode != 0)
  53. {
  54. _result.Msg = "【企业微信】【打卡】【获取所有月打卡数据】【Msg】" + checkIn.errmsg;
  55. return _result;
  56. }
  57. List<Data> checkInDatas = checkIn.datas;
  58. //获取 请假类型 Sp_Detail.template_id
  59. string leave_template_id = "C4NzTJCh1onCUK915rRkvy7Fh5Vqz4YbiEV9jrBY1";
  60. List<VacationLeaveTypeView> vacationLeaveTypes = await GetVacationLeaveTypes(leave_template_id);
  61. if (vacationLeaveTypes.Count <= 0)
  62. {
  63. _result.Msg = startDt + " - " + endDt + "请假 类型数据 获取失败!";
  64. return _result;
  65. }
  66. _result.Msg = string.Empty;
  67. try
  68. {
  69. foreach (var pm_wsInfo in pm_WageSheetDattaSources)
  70. {
  71. string itemName = userNames.Where(it => it.Id == pm_wsInfo.UserId).FirstOrDefault().CnName;
  72. //补贴 金额
  73. decimal meal_subsidy = 0.00M; // 午餐(午餐10元/天) 补贴 * 计算方式:单日上午请假时长(小时)大于或者等于三小时 没有餐补
  74. //事假 病假 总金额
  75. decimal personalLeaveTotal = 0.00M, // 事假 日薪 *计算方式:日平均工资 = 月工资/当月应出勤天数。
  76. sickLeaveTotal = 0.00M; // 病假 日薪 *计算方式:日平均工资 = 成都市最低工资标准的80%/当月应出勤天数。 短期病假=当月15天内
  77. //扣款金额
  78. decimal beLate_deduction = 0.00M, // 迟到 扣款金额 *计算方式:
  79. // 一个自然月内,不足 10 分钟的迟到/早退,不超过 2 次的部分,不做处罚;3 次及以上,按50元 / 次处罚;
  80. // 超过 10 分钟(含 10 分钟),不足 60 分钟的迟到/早退,按 50 元/次处罚;
  81. // 超过 60 分钟(含 60 分钟),不足 3 小时的迟到/早退,且无请假者,按旷工半日处理;超过3 小时的迟到 / 早退,且无请假者,按旷工一日处理。
  82. early_deduction = 0.00M, // 早退 扣款金额
  83. absenteeism_deduction = 0.00M, // 旷工 扣款金额 *计算方式:旷工扣发当日工资
  84. unprinted_deduction = 0.00M, // 未打卡 扣款金额 *计算方式:
  85. // 试用期员工每月有 2 次 补卡机会,超过 2 次不足 5 次的部分,按 10 元 / 次处罚,5 次及以上的漏卡,按 50 元 / 次处罚;
  86. // 正式员工每月 3 次以内的补卡,按 10 元 / 次处罚,3 次及以上的漏卡,按 50 元 / 次处罚。
  87. sickLeave_deduction = 0.00M, // 病假
  88. other_deduction = 0.00M; // 其他 扣款金额
  89. decimal meal_deduction = 0.00M; // 餐补 扣款金额
  90. decimal reissuecard_deduction = 0.00M; // 补卡 扣款金额
  91. //打卡数据
  92. Data? checkInData = checkInDatas.Where(it => it.base_info.name == itemName).FirstOrDefault();
  93. if (checkInData == null) { continue; }
  94. string acctid = checkInData.base_info.acctid; //用户Id
  95. //当月总计数据
  96. Summary_Info? summary_Info = checkInData.summary_info;
  97. if (summary_Info == null) { continue; }
  98. int dk_work_days = summary_Info.work_days - 1; //应出勤天数
  99. //int regular_days = summary_Info.regular_days - 1; //正常出勤天数
  100. meal_subsidy = dk_work_days * 10; //应发放餐补
  101. #region 计算日工资 正常日薪 事假日薪 病假日薪
  102. //月 - 应发工资
  103. decimal amountPayable = pm_wsInfo.Basic + pm_wsInfo.Floats + pm_wsInfo.PostAllowance + pm_wsInfo.InformationSecurityFee + pm_wsInfo.OtherSubsidies;
  104. // 日薪 = *计算方式:日平均工资 = 月工资/当月应出勤天数。
  105. decimal dailyWage = ConvertToDecimal(amountPayable / work_days);
  106. // 病假日薪 *计算方式:日平均工资 = 成都市最低工资标准的80%/当月应出勤天数。 短期病假=当月15天内
  107. decimal sickLeave_dailywage = ConvertToDecimal(_chengDuMinimumWage / work_days);
  108. //病假 一天扣款
  109. sickLeave_deduction = dailyWage - sickLeave_dailywage;
  110. List<Ex_Items> ex_Items = new List<Ex_Items>();//假勤 And 打卡备注集合
  111. #endregion
  112. if (!itemName.Equals("张海麟")) //不算考勤
  113. {
  114. int annualLeaveNum = 0, //年假
  115. personalLeaveNum = 0, //事假
  116. sickLeaveNum = 0, //病假
  117. lieuLeaveNum = 0, //调休假
  118. marriageLeaveNum = 0, //婚嫁
  119. maternityLeaveNum = 0, //产假
  120. paternityLeaveNum = 0, //陪产假
  121. funeralLeaveNum = 0, //丧假
  122. reissueCardNum = 0, //补卡 次数
  123. evectionNum = 0, //出差 次数
  124. outIngNum = 0, //外出 次数
  125. outWorkNum = 0, //外勤 次数
  126. otherLeaveNum = 0; //其他
  127. #region 假勤 处理 1-请假;2-补卡;3-出差;4-外出;100-外勤;
  128. List<Sp_Item>? sp_items = checkInData.sp_items.Where(it => it.count != 0).ToList();
  129. if (sp_items != null && sp_items.Count > 0)
  130. {
  131. annualLeaveNum = Fallibilitydispose(sp_items, 1, "年假");
  132. personalLeaveNum = Fallibilitydispose(sp_items, 1, "事假");
  133. sickLeaveNum = Fallibilitydispose(sp_items, 1, "病假");
  134. lieuLeaveNum = Fallibilitydispose(sp_items, 1, "调休假");
  135. marriageLeaveNum = Fallibilitydispose(sp_items, 1, "婚嫁");
  136. maternityLeaveNum = Fallibilitydispose(sp_items, 1, "产假");
  137. paternityLeaveNum = Fallibilitydispose(sp_items, 1, "陪产假");
  138. funeralLeaveNum = Fallibilitydispose(sp_items, 1, "丧假");
  139. otherLeaveNum = Fallibilitydispose(sp_items, 1, "其他");
  140. reissueCardNum = Fallibilitydispose(sp_items, 2, "补卡次数");
  141. evectionNum = Fallibilitydispose(sp_items, 3, "出差");
  142. outIngNum = Fallibilitydispose(sp_items, 4, "外出");
  143. outWorkNum = Fallibilitydispose(sp_items, 3, "外勤");
  144. }
  145. #region 请假类型金额/餐补 处理
  146. List<Sp_Detail> sp_leave_details = new List<Sp_Detail>();
  147. sp_leave_details = await _qiYeWeChatApiService.GetApprovalDetailsAsync(startDt, endDt, acctid, 2, 1); //时间段内所有 已同意的 请假 审批数据
  148. if (sp_leave_details.Count <= 0)
  149. {
  150. _result.Msg += startDt + " - " + endDt + " " + itemName + " 请假 审批数据获取未获取到!\r\n";
  151. //continue;
  152. }
  153. Ex_Items ex_Items_jq = new Ex_Items() { Type = "假勤" }; //假勤
  154. List<Ex_Item> ex_ItemInfos = new List<Ex_Item>();
  155. foreach (Sp_Detail sp_item in sp_leave_details)
  156. {
  157. Apply_data? apply_data = sp_item.apply_data;
  158. if (apply_data != null)
  159. {
  160. List<ContentsItem> contents = apply_data.contents;
  161. ContentsItem content_Vacation = contents.Where(it => it.control == "Vacation").FirstOrDefault(); //请假类型
  162. ContentsItem content_Textarea = contents.Where(it => it.control == "Textarea").FirstOrDefault(); //多行文本
  163. if (content_Vacation != null)
  164. {
  165. Vacation vacation = content_Vacation.value.vacation;
  166. Attendance attendance = vacation.attendance; //假勤组件
  167. Selector selector = vacation.selector; //请假类型
  168. List<OptionsItem> optionsItems = selector.options; //key 请假类型 id
  169. List<TitleItem> value = optionsItems[0].value; // value 文本描述值
  170. int leaveType = int.Parse(optionsItems[0].key); //key 请假子类型 id
  171. Date_range date_Range = attendance.date_range;
  172. //筛选 不在工作日内的假勤申请
  173. if (startDt >= date_Range.new_begin_dt && date_Range.new_end_dt <= endDt)
  174. {
  175. continue;
  176. }
  177. decimal thisTypeDeduction = 0.00M;//当前类型扣款
  178. string leave_starttime = date_Range.new_begin_dt.ToString("HH:mm");
  179. string leave_endtime = date_Range.new_end_dt.ToString("HH:mm");
  180. string typeName = string.Empty;
  181. string unit = string.Empty;
  182. int leaveTypeId = leaveType;
  183. var leaveTypeData = vacationLeaveTypes.Where(it => it.id == leaveTypeId).FirstOrDefault();
  184. if (leaveTypeData != null) { typeName = leaveTypeData.name; }
  185. string startTime = string.Empty;
  186. string endTime = string.Empty;
  187. //计算请假类型扣款金额
  188. decimal new_duration = 0.00M;
  189. if (date_Range.type == "halfday")
  190. {
  191. new_duration = Convert.ToDecimal(date_Range.new_duration) / 86400M;
  192. unit = "天";
  193. startTime = date_Range.new_begin_dt.ToString("yyyy-MM-dd") + " 09:00";
  194. endTime = date_Range.new_begin_dt.ToString("yyyy-MM-dd") + " 18:00";
  195. }
  196. else if (date_Range.type == "hour")
  197. {
  198. new_duration = Convert.ToDecimal(date_Range.new_duration) / 3600M;
  199. unit = "小时";
  200. startTime = date_Range.new_begin_dt.ToString("yyyy-MM-dd HH:mm:ss");
  201. endTime = date_Range.new_end_dt.ToString("yyyy-MM-dd HH:mm:ss");
  202. }
  203. decimal leave_meals = 0.00M;
  204. //计算餐补 假勤类型扣款
  205. CalculateTypeFee(leaveType, date_Range.type, leave_starttime, leave_endtime, amountPayable, work_days, new_duration,
  206. out leave_meals, out thisTypeDeduction);
  207. #region 累计类型扣款
  208. //1年假;2事假;3病假;4调休假;5婚假;6产假;7陪产假;8其他;9丧假
  209. if (leaveType == 2) //事假
  210. {
  211. personalLeaveTotal += thisTypeDeduction;
  212. }
  213. else if (leaveType == 3) //病假
  214. {
  215. sickLeaveTotal = thisTypeDeduction;
  216. }
  217. #endregion
  218. meal_deduction += leave_meals;
  219. Ex_Item ex_Item = new Ex_Item()
  220. {
  221. SubTypeId = leaveType,
  222. SubType = typeName,
  223. StartTimeDt = Convert.ToDateTime(startTime),
  224. EndTimeDt = Convert.ToDateTime(endTime),
  225. Duration = new_duration,
  226. Unit = unit,
  227. Deduction = thisTypeDeduction,
  228. //Reason = apply_data.reason,
  229. Apply_time_dt = Convert.ToDateTime(sp_item.apply_time_dt.ToString("yyyy-MM-dd HH:mm:ss")),
  230. //Approval_name = sp_item.approval_name,
  231. };
  232. ex_ItemInfos.Add(ex_Item);
  233. }
  234. }
  235. }
  236. if (ex_ItemInfos.Count > 0)
  237. {
  238. ex_Items_jq.Ex_ItemInfo = ex_ItemInfos.OrderBy(it => it.SubTypeId).ThenBy(it => it.Apply_time_dt).ToList();
  239. ex_Items.Add(ex_Items_jq);
  240. }
  241. #endregion
  242. #endregion
  243. Ex_Items ex_Items_dk = new Ex_Items() { Type = "打卡" }; //打卡
  244. List<Ex_Item> ex_reissuecard_Items = new List<Ex_Item>();
  245. #region 打卡补卡 补卡次数 处理
  246. if (reissueCardNum > 0)
  247. {
  248. List<Sp_Detail> sp_buka_details = new List<Sp_Detail>();
  249. sp_leave_details = await _qiYeWeChatApiService.GetApprovalDetailsAsync(startDt, endDt, acctid, 2, 2); //时间段内所有 已同意的 请假 审批数据
  250. int bukaNum = 1;
  251. foreach (var item in sp_leave_details)
  252. {
  253. decimal bukaPrice = 0.00M;
  254. if (bukaNum <= 2)
  255. {
  256. bukaPrice = 10.00M;
  257. }
  258. else
  259. {
  260. bukaPrice = 50.00M;
  261. }
  262. var app_data = item.apply_data;
  263. var punch_correction1 = app_data.contents[0].value.punch_correction; //未打卡时间
  264. var punch_correction2 = app_data.contents[1].value;
  265. Ex_Item ex_reissueCard = new Ex_Item()
  266. {
  267. SubTypeId = 7,
  268. SubType = "打卡补卡",
  269. StartTimeDt = Convert.ToDateTime(punch_correction1.time_dt.ToString("yyyy-MM-dd HH:mm:ss")), //未打卡时间
  270. Deduction = bukaPrice,
  271. Reason = punch_correction2.text,
  272. Unit = string.Empty
  273. };
  274. unprinted_deduction += bukaPrice;
  275. ex_reissuecard_Items.Add(ex_reissueCard);
  276. bukaNum++;
  277. }
  278. }
  279. #endregion
  280. List<Sp_Detail> sp_reissuecard_details = new List<Sp_Detail>();
  281. #region 迟到早退处理
  282. //统计迟到次数
  283. int beLateNum = 0, // 1-迟到;
  284. leaveEarlyNum = 0, // 2-早退;
  285. dummyDeckNum = 0, // 3-缺卡;
  286. minerNum = 0, // 4-旷工;
  287. locationAnomalyNum = 0, // 5-地点异常;
  288. unitExNum = 0; // 6-设备异常;
  289. if (summary_Info.except_days > 0)
  290. {
  291. List<Exception_Info>? ex_infos = checkInData.exception_infos;
  292. if (ex_infos != null && ex_infos.Count >= 0)
  293. {
  294. beLateNum = ExceptionStatistics(ex_infos, 1);
  295. leaveEarlyNum = ExceptionStatistics(ex_infos, 2);
  296. dummyDeckNum = ExceptionStatistics(ex_infos, 3);
  297. minerNum = ExceptionStatistics(ex_infos, 4);
  298. locationAnomalyNum = ExceptionStatistics(ex_infos, 5);
  299. unitExNum = ExceptionStatistics(ex_infos, 6);
  300. }
  301. }
  302. //打卡记录
  303. CheckInDataView checkInDataView = new CheckInDataView();
  304. checkInDataView = await _qiYeWeChatApiService.GetCheckinDataAsync(new List<string>() { acctid }, 3, startDt, endDt);
  305. if (checkInDataView.errcode != 0)
  306. {
  307. _result.Msg += startDt + " - " + endDt + " " + itemName + " 打卡记录 " + checkInDataView.errmsg + " \r\n";
  308. }
  309. //筛选时间异常的打卡记录
  310. List<CheckInDataInfo> checkInDataInfos = checkInDataView.checkindata.Where(it => !string.IsNullOrEmpty(it.exception_type)).ToList();
  311. //处理 未打卡的记录是否已经通过假勤审批
  312. List<CheckInDataInfo> leave_checkInDataInfos = new List<CheckInDataInfo>();
  313. if (checkInDataInfos.Count > 0)
  314. {
  315. foreach (var leaveItem in ex_ItemInfos)
  316. {
  317. if (leaveItem.StartTimeDt.ToString("yyyy-MM-dd").Equals(leaveItem.EndTimeDt.ToString("yyyy-MM-dd"))) //单天
  318. {
  319. leave_checkInDataInfos.AddRange(checkInDataInfos.Where(it => it.checkin_time_dt.ToString("yyyy-MM-dd") == leaveItem.StartTimeDt.ToString("yyyy-MM-dd")).ToList());
  320. }
  321. else //多天
  322. {
  323. leave_checkInDataInfos.AddRange(checkInDataInfos.Where(it => it.checkin_time_dt >= leaveItem.StartTimeDt && it.checkin_time_dt <= leaveItem.EndTimeDt).ToList());
  324. }
  325. }
  326. }
  327. //处理 时间异常的记录是否已经通过补卡审批
  328. List<CheckInDataInfo> pullcard_checkInDataInfos = new List<CheckInDataInfo>();
  329. if (leave_checkInDataInfos.Count > 0)
  330. {
  331. //获取异常打卡数据 并且 未通过假勤审批
  332. pullcard_checkInDataInfos = checkInDataInfos.Except(leave_checkInDataInfos).ToList();
  333. if (pullcard_checkInDataInfos.Count > 0)
  334. {
  335. CheckInDayDataView checkInDayDataView = await _qiYeWeChatApiService.GetCheckInDayDataAsync(new List<string>() { acctid }, startDt, endDt);
  336. if (checkInDayDataView.errcode != 0)
  337. {
  338. _result.Msg += startDt + " - " + endDt + " " + itemName + " 异常信息数据获取未获取到!\r\n";
  339. }
  340. List<Root> roots_words = checkInDayDataView.datas.Where(it => it.base_info.day_type == 0).ToList(); //获取工作日日报信息
  341. List<Root> roots_exs = checkInDayDataView.datas.Where(it => it.exception_infos.Count > 0).ToList();
  342. int user_cd_zt_num = 0;
  343. foreach (var roots_ex in roots_exs)
  344. {
  345. foreach (var exception_info in roots_ex.exception_infos)
  346. {
  347. decimal timelength = ConvertToDecimal((Convert.ToDecimal(exception_info.duration) / 3600.00M) * 60.00M); //时长 分钟
  348. if (timelength == 9) timelength = 7.50M;
  349. int exception = exception_info.exception; //异常类型
  350. decimal day_miner_unit = ConvertToDecimal(dailyWage / 15); //以0.5小时为单位
  351. //1:一个自然月内,不足 10 分钟的迟到/早退,不超过 2 次的部分,不做处罚;3 次及以上,按50 元 / 次处罚;
  352. //2:超过 10 分钟(含 10 分钟),不足 60 分钟的迟到 / 早退,按 50 元 / 次处罚;
  353. //3:超过 60 分钟(含 60 分钟),不足 3 小时的迟到 / 早退,且无请假者,按旷工半日处理;超过 3 小时的迟到 / 早退,且无请假者,按旷工一日处理。
  354. long date = roots_ex.base_info.date; //当日工作日期
  355. long earliest_time = roots_ex.summary_info.earliest_time; //最早打卡时间
  356. long lastest_time = roots_ex.summary_info.lastest_time; //最晚打卡时间
  357. long this_date = date + earliest_time;
  358. DateTime thisDt = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)).AddTicks(this_date * 10000000);
  359. string thisDtStr = thisDt.ToString("yyyy-MM-dd");
  360. Ex_Item beLate_belate_ex = new Ex_Item()
  361. {
  362. SubTypeId = 4,
  363. SubType = "旷工",
  364. Duration = timelength,
  365. StartTimeDt = Convert.ToDateTime(roots_ex.base_info.dateDt.ToString("yyyy-MM-dd HH:mm:ss")),
  366. Unit = "分钟",
  367. };
  368. decimal day_deduction = 0.00M;
  369. //1 - 迟到;2 - 早退;3 - 缺卡;4 - 旷工;5 - 地点异常;6 - 设备异常
  370. if (exception == 1) //迟到
  371. {
  372. if (timelength < 10)
  373. {
  374. user_cd_zt_num++;
  375. beLate_belate_ex.SubTypeId = 1;
  376. beLate_belate_ex.SubType = "迟到";
  377. string thisStartDt = (TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)).AddTicks((date + earliest_time) * 10000000))
  378. .ToString("yyyy-MM-dd HH:mm:ss");
  379. beLate_belate_ex.StartTimeDt = Convert.ToDateTime(thisStartDt);
  380. if (reissueCardNum >= 3)
  381. {
  382. day_deduction = 50.00M;
  383. }
  384. else
  385. {
  386. day_deduction = 0.00M;
  387. }
  388. beLate_deduction += day_deduction; //迟到扣款 总额
  389. }
  390. else if (timelength >= 10 && timelength <= 60)
  391. {
  392. string thisStartDt = (TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)).AddTicks((date + earliest_time) * 10000000))
  393. .ToString("yyyy-MM-dd HH:mm:ss");
  394. beLate_belate_ex.StartTimeDt = Convert.ToDateTime(thisStartDt);
  395. day_deduction = 50.00M;
  396. beLate_deduction += day_deduction; //迟到扣款 总额
  397. beLate_belate_ex.SubTypeId = 1;
  398. beLate_belate_ex.SubType = "迟到";
  399. }
  400. else if (timelength > 60 && timelength <= 180)
  401. {
  402. day_deduction = day_miner_unit * 6; //3小时
  403. meal_deduction += 10.00M; //餐补扣款
  404. absenteeism_deduction += day_deduction; //矿工半日
  405. }
  406. else
  407. {
  408. day_deduction = dailyWage;
  409. absenteeism_deduction += day_deduction; //矿工一日
  410. meal_deduction += 10.00M;
  411. }
  412. beLate_belate_ex.Deduction = day_deduction;
  413. ex_reissuecard_Items.Add(beLate_belate_ex);
  414. }
  415. else if (exception == 2) //早退
  416. {
  417. if (timelength < 10)
  418. {
  419. user_cd_zt_num++;
  420. beLate_belate_ex.SubTypeId = 2;
  421. beLate_belate_ex.SubType = "早退";
  422. string thisEndDt = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)).AddTicks((date + lastest_time) * 10000000)
  423. .ToString("yyyy-MM-dd HH:mm:ss");
  424. beLate_belate_ex.StartTimeDt = Convert.ToDateTime(thisEndDt);
  425. if (reissueCardNum >= 3)
  426. {
  427. day_deduction = 50.00M;
  428. }
  429. else
  430. {
  431. day_deduction = 0.00M;
  432. }
  433. early_deduction += day_deduction; //早退扣款 总计
  434. }
  435. else if (timelength >= 10 && timelength <= 60)
  436. {
  437. day_deduction = 50.00M;
  438. early_deduction += day_deduction; //早退扣款 总计
  439. beLate_belate_ex.SubTypeId = 2;
  440. beLate_belate_ex.SubType = "早退";
  441. string thisEndDt = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)).AddTicks((date + lastest_time) * 10000000)
  442. .ToString("yyyy-MM-dd HH:mm:ss");
  443. beLate_belate_ex.StartTimeDt = Convert.ToDateTime(thisEndDt);
  444. }
  445. else if (timelength > 60 && timelength <= 180)
  446. {
  447. day_deduction = day_miner_unit * 6; //3小时
  448. absenteeism_deduction += day_deduction; //矿工半日
  449. }
  450. else
  451. {
  452. day_deduction = dailyWage;
  453. meal_deduction += 10.00M;
  454. absenteeism_deduction += day_deduction; //矿工一日
  455. }
  456. beLate_belate_ex.Deduction = day_deduction;
  457. ex_reissuecard_Items.Add(beLate_belate_ex);
  458. }
  459. else if (exception == 3) //缺卡
  460. {
  461. if (roots_ex.exception_infos.Count == 2)
  462. {
  463. day_deduction = dailyWage;
  464. meal_deduction += 10.00M;
  465. absenteeism_deduction += day_deduction;
  466. beLate_belate_ex.Reason = "上午-下午 缺卡/未打卡为旷工 一天";
  467. beLate_belate_ex.Deduction = day_deduction;
  468. ex_reissuecard_Items.Add(beLate_belate_ex);
  469. }
  470. else if (roots_ex.exception_infos.Count == 1)
  471. {
  472. if (earliest_time == lastest_time)
  473. {
  474. DateTime thisDt1 = Convert.ToDateTime(thisDtStr + " 12:00");
  475. //ex_ItemInfos
  476. Ex_Item zt_jq = new Ex_Item();
  477. zt_jq = ex_ItemInfos.Where(it => it.StartTimeDt.ToString("yyyy-MM-dd") == thisDtStr).FirstOrDefault();
  478. if (thisDt <= thisDt1) //旷工 下午
  479. {
  480. if (zt_jq != null) if (zt_jq.EndTimeDt == Convert.ToDateTime(thisDtStr + " 18:00")) continue; //排除已有假期的缺卡
  481. day_deduction = day_miner_unit * 9; //4.5小时
  482. meal_deduction += 10.00M;
  483. absenteeism_deduction += day_deduction; //矿工半日
  484. beLate_belate_ex.Reason = thisDtStr + " 下午(18:00)缺卡/未打卡视为下午旷工(4.5小时)";
  485. beLate_belate_ex.Deduction = day_deduction;
  486. ex_reissuecard_Items.Add(beLate_belate_ex);
  487. }
  488. else if (thisDt >= thisDt1) //旷工下午
  489. {
  490. if (zt_jq != null) if (zt_jq.EndTimeDt == Convert.ToDateTime(thisDtStr + " 09:00")) continue; //排除已有假期的缺卡
  491. day_deduction = day_miner_unit * 6; //3小时
  492. absenteeism_deduction += day_deduction; //矿工半日
  493. beLate_belate_ex.Reason = thisDtStr + " 上午(09:00)缺卡/未打卡视为上午旷工(3小时)";
  494. beLate_belate_ex.Deduction = day_deduction;
  495. ex_reissuecard_Items.Add(beLate_belate_ex);
  496. }
  497. else //矿工一日
  498. {
  499. day_deduction = dailyWage;
  500. meal_deduction += 10.00M;
  501. absenteeism_deduction += day_deduction;
  502. beLate_belate_ex.Reason = thisDtStr + " 上午(09:00)-下午(18:00) 缺卡/未打视为旷工一天(7.5小时)";
  503. beLate_belate_ex.Deduction = day_deduction;
  504. ex_reissuecard_Items.Add(beLate_belate_ex);
  505. }
  506. }
  507. }
  508. }
  509. else if (exception == 4) //旷工
  510. {
  511. if (timelength > 60 && timelength <= 180)
  512. {
  513. day_deduction = day_miner_unit * 6; //3小时
  514. beLate_belate_ex.Reason = thisDtStr + " 缺卡/未打视为旷工上午(3小时)";
  515. meal_deduction += 10.00M;
  516. absenteeism_deduction += day_deduction; //矿工半日
  517. }
  518. else
  519. {
  520. day_deduction = dailyWage;
  521. beLate_belate_ex.Reason = thisDtStr + " 上午(09:00)-下午(18:00) 缺卡/未打视为旷工一天(7.5小时)";
  522. meal_deduction += 10.00M;
  523. absenteeism_deduction += day_deduction; //矿工一日
  524. }
  525. beLate_belate_ex.Deduction = day_deduction;
  526. ex_reissuecard_Items.Add(beLate_belate_ex);
  527. }
  528. }
  529. }
  530. }
  531. }
  532. if (ex_reissuecard_Items.Count > 0)
  533. {
  534. ex_Items_dk.Ex_ItemInfo = ex_reissuecard_Items;
  535. ex_Items_dk.Ex_ItemInfo = ex_reissuecard_Items.OrderBy(it => it.SubTypeId).ThenBy(it => it.StartTimeDt).ToList();
  536. ex_Items.Add(ex_Items_dk);
  537. }
  538. #endregion
  539. }
  540. else
  541. {
  542. meal_subsidy = work_days * 10.00M;
  543. }
  544. #region 应发合计 实发合计 扣款合计(假勤扣款,其他扣款,社保扣款,公积金代扣,个税扣款)
  545. decimal mealTotal = meal_subsidy - meal_deduction; //餐补
  546. decimal salaryTotal = 0.00M;
  547. if (dk_work_days >= work_days)
  548. {
  549. dk_work_days = work_days;
  550. salaryTotal = amountPayable + mealTotal; //应发合计
  551. }
  552. else
  553. {
  554. if (itemName.Equals("张海麟"))
  555. {
  556. salaryTotal = amountPayable + mealTotal;
  557. }
  558. else
  559. {
  560. salaryTotal = (dk_work_days * dailyWage) + mealTotal; //应发合计
  561. }
  562. }
  563. //扣款合计 不含个税
  564. decimal eductionTotal = sickLeaveTotal + personalLeaveTotal + beLate_deduction + early_deduction + absenteeism_deduction + unprinted_deduction + other_deduction +
  565. pm_wsInfo.WithholdingInsurance + pm_wsInfo.ReservedFunds + pm_wsInfo.OtherDeductions;
  566. decimal actualReleaseTotal = salaryTotal - eductionTotal; //实发合计 * 不含个税
  567. #endregion
  568. #region 处理当月工资数据
  569. pm_wsInfo.YearMonth = thisYearMonth;
  570. pm_wsInfo.StartDate = startDt.ToString("yyyy-MM-dd");
  571. pm_wsInfo.EndDate = endDt.AddDays(-1).ToString("yyyy-MM-dd");
  572. pm_wsInfo.WorkDays = work_days; //当月应出勤天数
  573. pm_wsInfo.RegularDays = dk_work_days; //当月正常出勤天数
  574. pm_wsInfo.SickLeave = sickLeaveTotal; //病假
  575. pm_wsInfo.SomethingFalse = personalLeaveTotal; //事假
  576. pm_wsInfo.LateTo = beLate_deduction; //迟到
  577. pm_wsInfo.LeaveEarly = early_deduction; //早退
  578. pm_wsInfo.Absenteeism = absenteeism_deduction; //旷工
  579. pm_wsInfo.NotPunch = unprinted_deduction; //未打卡
  580. pm_wsInfo.OtherDeductions = other_deduction; //其他
  581. pm_wsInfo.Ex_ItemsRemark = JsonConvert.SerializeObject(ex_Items); //
  582. pm_wsInfo.Mealsupplement = mealTotal; //餐补
  583. pm_wsInfo.Should = salaryTotal; //应发合计
  584. pm_wsInfo.TotalDeductions = eductionTotal; //扣款合计
  585. pm_wsInfo.TotalRealHair = actualReleaseTotal; //实发合计
  586. pm_wsInfo.AfterTax = actualReleaseTotal - pm_wsInfo.WithholdingTax; //税后工资
  587. pm_wsInfo.LastUpdateUserId = userId;
  588. pm_wsInfo.LastUpdateDt = DateTime.Now;
  589. pm_wsInfo.CreateUserId = userId;
  590. pm_wsInfo.CreateTime = DateTime.Now;
  591. pm_wsInfo.DeleteUserId = null;
  592. pm_wsInfo.DeleteTime = null;
  593. #endregion
  594. }
  595. }
  596. catch (Exception ex)
  597. {
  598. _result.Msg = ex.Message;
  599. return _result;
  600. }
  601. _result.Code = 0;
  602. _result.Data = pm_WageSheetDattaSources;
  603. return _result;
  604. }
  605. /// <summary>
  606. /// 计算工资
  607. /// </summary>
  608. /// <param name="pm_WageSheetDattaSources"></param>
  609. /// <param name="userNames"></param>
  610. /// <param name="thisYearMonth"></param>
  611. /// <param name="startDt"></param>
  612. /// <param name="endDt"></param>
  613. /// <returns></returns>
  614. public static async Task<Result> SalaryCalculatorAsync(
  615. List<Pm_WageSheet> pm_WageSheetDattaSources, List<UserNameView> userNames, int userId, string thisYearMonth, DateTime startDt, DateTime endDt)
  616. {
  617. if (pm_WageSheetDattaSources.Count <= 0)
  618. {
  619. _result.Msg = "计算工资传入数据为空!";
  620. return _result;
  621. }
  622. if (userNames.Count <= 0)
  623. {
  624. var nameData = await _usersRep.GetUserNameList(1);
  625. userNames = nameData.Data;
  626. }
  627. //计算时间段内工作日
  628. int work_days = await GetWorkDays(thisYearMonth);
  629. if (work_days <=0)
  630. {
  631. _result.Msg = thisYearMonth+" 工作日未设置,请前往《工作日管理页面》设置!";
  632. return _result;
  633. }
  634. UserIdListView userIdListView = await _qiYeWeChatApiService.GetUserIdListAsync();
  635. if (userIdListView.errcode != 0)
  636. {
  637. _result.Msg = "【企业微信】【打卡】【获取员工ID】【Msg】" + userIdListView.errmsg;
  638. return _result;
  639. }
  640. List<string> qyWhchatIdList = userIdListView.dept_user.Select(it => it.userid).ToList();
  641. CheckInDayDataView checkInDayDataView = await _qiYeWeChatApiService.GetCheckInDayDataAsync(qyWhchatIdList, startDt, endDt);
  642. if (checkInDayDataView.errcode != 0)
  643. {
  644. _result.Msg = "【企业微信】【打卡】【获取时间段内所有日打卡】【Msg】" + checkInDayDataView.errmsg;
  645. return _result;
  646. }
  647. //筛选出工作日日报
  648. List<Root> workday_userRoots = checkInDayDataView.datas.Where(it => it.base_info.day_type == 0 && it.base_info.record_type == 1).ToList(); //工作日日报
  649. workday_userRoots = workday_userRoots.OrderBy(it => it.base_info.date).ToList();
  650. //获取 请假类型 Sp_Detail.template_id
  651. string leave_template_id = "C4NzTJCh1onCUK915rRkvy7Fh5Vqz4YbiEV9jrBY1";
  652. List<VacationLeaveTypeView> vacationLeaveTypes = await GetVacationLeaveTypes(leave_template_id);
  653. if (vacationLeaveTypes.Count <= 0)
  654. {
  655. _result.Msg = "【企业微信】【审批】【获取审批类型】【Msg】" + startDt + " - " + endDt + "请假 类型数据 获取失败!";
  656. return _result;
  657. }
  658. try
  659. {
  660. foreach (var pm_wsInfo in pm_WageSheetDattaSources)
  661. {
  662. string itemName = userNames.Where(it => it.Id == pm_wsInfo.UserId).FirstOrDefault().CnName;
  663. //补贴 金额
  664. decimal meal_subsidy = 0.00M; // 午餐(午餐10元/天) 补贴 * 计算方式:单日上午请假时长(小时)大于或者等于三小时 没有餐补
  665. //事假 病假 总金额
  666. decimal personalLeaveTotal = 0.00M, // 事假 日薪 *计算方式:日平均工资 = 月工资/当月应出勤天数。
  667. sickLeaveTotal = 0.00M; // 病假 日薪 *计算方式:日平均工资 = 成都市最低工资标准的80%/当月应出勤天数。 短期病假=当月15天内
  668. //扣款金额
  669. decimal beLate_deduction = 0.00M, // 迟到 扣款金额 *计算方式:
  670. // 一个自然月内,不足 10 分钟的迟到/早退,不超过 2 次的部分,不做处罚;3 次及以上,按50元 / 次处罚;
  671. // 超过 10 分钟(含 10 分钟),不足 60 分钟的迟到/早退,按 50 元/次处罚;
  672. // 超过 60 分钟(含 60 分钟),不足 3 小时的迟到/早退,且无请假者,按旷工半日处理;超过3 小时的迟到 / 早退,且无请假者,按旷工一日处理。
  673. early_deduction = 0.00M, // 早退 扣款金额
  674. absenteeism_deduction = 0.00M, // 旷工 扣款金额 *计算方式:旷工扣发当日工资
  675. unprinted_deduction = 0.00M, // 未打卡 扣款金额 *计算方式:
  676. // 试用期员工每月有 2 次 补卡机会,超过 2 次不足 5 次的部分,按 10 元 / 次处罚,5 次及以上的漏卡,按 50 元 / 次处罚;
  677. // 正式员工每月 3 次以内的补卡,按 10 元 / 次处罚,3 次及以上的漏卡,按 50 元 / 次处罚。
  678. sickLeave_deduction = 0.00M, // 病假
  679. other_deduction = 0.00M; // 其他 扣款金额
  680. decimal meal_deduction = 0.00M; // 餐补 扣款金额
  681. decimal reissuecard_deduction = 0.00M; // 补卡 扣款金额
  682. #region 计算日工资 正常日薪 事假日薪 病假日薪
  683. //月 - 应发工资
  684. decimal amountPayable = pm_wsInfo.Basic + pm_wsInfo.Floats + pm_wsInfo.PostAllowance + pm_wsInfo.InformationSecurityFee + pm_wsInfo.OtherSubsidies;
  685. // 日薪 = *计算方式:日平均工资 = 月工资/当月应出勤天数。
  686. decimal dailyWage = ConvertToDecimal(amountPayable / work_days);
  687. // 病假日薪 *计算方式:日平均工资 = 成都市最低工资标准的80%/当月应出勤天数。 短期病假=当月15天内
  688. decimal sickLeave_dailywage = ConvertToDecimal(_chengDuMinimumWage / work_days);
  689. //病假 一天扣款
  690. sickLeave_deduction = dailyWage - sickLeave_dailywage;
  691. List<Ex_Items> ex_Items = new List<Ex_Items>();//假勤 And 打卡备注集合
  692. Ex_Items ex_Items_dk = new Ex_Items() { Type = "打卡" }; //打卡
  693. Ex_Items ex_Items_jq = new Ex_Items() { Type = "假勤" }; //假勤
  694. #endregion
  695. List<Root> userRoots = workday_userRoots.Where(it => it.base_info.name == itemName).ToList(); //工作日日报 1-固定上下班;
  696. userRoots = userRoots.Distinct().ToList();
  697. int dk_work_days = userRoots.Count; //应出勤天数
  698. if (dk_work_days > work_days)
  699. {
  700. dk_work_days = work_days;
  701. }
  702. meal_subsidy = dk_work_days * 10; //应发放餐补
  703. if (!itemName.Equals("张海麟"))
  704. {
  705. string acctid = userRoots[0].base_info.acctid;
  706. List<Ex_Item> ex_reissuecard_Items = new List<Ex_Item>(); //打卡类型 数据
  707. List<Sp_items> acc_sp_items = new List<Sp_items>(); //审批数据
  708. #region 迟到 早退 矿工
  709. int user_cd_zt_num = 0; //早退/迟到 次数 10分钟内 2次以内不记处罚 三次及以上50一次
  710. foreach (var root in userRoots)
  711. {
  712. List<Holiday_infos> holiday_Infos = root.holiday_infos; //当天假勤信息
  713. List<Exception_infos> exception_infos = root.exception_infos; //当天校准状态信息
  714. List<Sp_items> sp_Items = root.sp_items;//当天假勤统计信息
  715. if (sp_Items.Count > 0)
  716. {
  717. sp_Items = sp_Items.Where(it => it.count > 0).ToList();
  718. acc_sp_items.AddRange(sp_Items);
  719. }
  720. foreach (var exception_info in exception_infos)
  721. {
  722. decimal timelength = ConvertToDecimal((Convert.ToDecimal(exception_info.duration) / 3600.00M) * 60.00M); //时长 分钟
  723. if (timelength == 9) timelength = 7.50M;
  724. int exception = exception_info.exception; //异常类型
  725. decimal day_miner_unit = ConvertToDecimal(dailyWage / 15); //以0.5小时为单位
  726. //1:一个自然月内,不足 10 分钟的迟到/早退,不超过 2 次的部分,不做处罚;3 次及以上,按50 元 / 次处罚;
  727. //2:超过 10 分钟(含 10 分钟),不足 60 分钟的迟到 / 早退,按 50 元 / 次处罚;
  728. //3:超过 60 分钟(含 60 分钟),不足 3 小时的迟到 / 早退,且无请假者,按旷工半日处理;超过 3 小时的迟到 / 早退,且无请假者,按旷工一日处理。
  729. long date = root.base_info.date; //当日工作日期
  730. long earliest_time = root.summary_info.earliest_time; //最早打卡时间
  731. long lastest_time = root.summary_info.lastest_time; //最晚打卡时间
  732. long this_date = date + earliest_time;
  733. DateTime thisDt = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)).AddTicks(this_date * 10000000);
  734. string thisDtStr = thisDt.ToString("yyyy-MM-dd");
  735. Ex_Item beLate_belate_ex = new Ex_Item()
  736. {
  737. SubTypeId = 4,
  738. SubType = "旷工",
  739. Duration = timelength,
  740. StartTimeDt = Convert.ToDateTime(root.base_info.dateDt.ToString("yyyy-MM-dd HH:mm:ss")),
  741. Unit = "分钟",
  742. };
  743. decimal day_deduction = 0.00M;
  744. //1 - 迟到;2 - 早退;3 - 缺卡;4 - 旷工;5 - 地点异常;6 - 设备异常
  745. if (exception == 1) //迟到
  746. {
  747. if (timelength < 10)
  748. {
  749. user_cd_zt_num++;
  750. beLate_belate_ex.SubTypeId = 1;
  751. beLate_belate_ex.SubType = "迟到";
  752. string thisStartDt = (TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)).AddTicks((date + earliest_time) * 10000000))
  753. .ToString("yyyy-MM-dd HH:mm:ss");
  754. beLate_belate_ex.StartTimeDt = Convert.ToDateTime(thisStartDt);
  755. if (user_cd_zt_num >= 3)
  756. {
  757. day_deduction = 50.00M;
  758. }
  759. else
  760. {
  761. day_deduction = 0.00M;
  762. }
  763. beLate_deduction += day_deduction; //迟到扣款 总额
  764. }
  765. else if (timelength >= 10 && timelength <= 60)
  766. {
  767. string thisStartDt = (TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)).AddTicks((date + earliest_time) * 10000000))
  768. .ToString("yyyy-MM-dd HH:mm:ss");
  769. beLate_belate_ex.StartTimeDt = Convert.ToDateTime(thisStartDt);
  770. day_deduction = 50.00M;
  771. beLate_deduction += day_deduction; //迟到扣款 总额
  772. beLate_belate_ex.SubTypeId = 1;
  773. beLate_belate_ex.SubType = "迟到";
  774. }
  775. else if (timelength > 60 && timelength <= 180)
  776. {
  777. day_deduction = day_miner_unit * 6; //3小时
  778. meal_deduction += 10.00M; //餐补扣款
  779. absenteeism_deduction += day_deduction; //矿工半日
  780. }
  781. else
  782. {
  783. day_deduction = dailyWage;
  784. absenteeism_deduction += day_deduction; //矿工一日
  785. meal_deduction += 10.00M;
  786. }
  787. beLate_belate_ex.Deduction = day_deduction;
  788. ex_reissuecard_Items.Add(beLate_belate_ex);
  789. }
  790. else if (exception == 2) //早退
  791. {
  792. if (timelength < 10)
  793. {
  794. user_cd_zt_num++;
  795. beLate_belate_ex.SubTypeId = 2;
  796. beLate_belate_ex.SubType = "早退";
  797. string thisEndDt = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)).AddTicks((date + lastest_time) * 10000000)
  798. .ToString("yyyy-MM-dd HH:mm:ss");
  799. beLate_belate_ex.StartTimeDt = Convert.ToDateTime(thisEndDt);
  800. if (user_cd_zt_num >= 3)
  801. {
  802. day_deduction = 50.00M;
  803. }
  804. else
  805. {
  806. day_deduction = 0.00M;
  807. }
  808. early_deduction += day_deduction; //早退扣款 总计
  809. }
  810. else if (timelength >= 10 && timelength <= 60)
  811. {
  812. day_deduction = 50.00M;
  813. early_deduction += day_deduction; //早退扣款 总计
  814. beLate_belate_ex.SubTypeId = 2;
  815. beLate_belate_ex.SubType = "早退";
  816. string thisEndDt = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)).AddTicks((date + lastest_time) * 10000000)
  817. .ToString("yyyy-MM-dd HH:mm:ss");
  818. beLate_belate_ex.StartTimeDt = Convert.ToDateTime(thisEndDt);
  819. }
  820. else if (timelength > 60 && timelength <= 180)
  821. {
  822. day_deduction = day_miner_unit * 6; //3小时
  823. absenteeism_deduction += day_deduction; //矿工半日
  824. }
  825. else
  826. {
  827. day_deduction = dailyWage;
  828. meal_deduction += 10.00M;
  829. absenteeism_deduction += day_deduction; //矿工一日
  830. }
  831. beLate_belate_ex.Deduction = day_deduction;
  832. ex_reissuecard_Items.Add(beLate_belate_ex);
  833. }
  834. else if (exception == 3) //缺卡
  835. {
  836. if (root.exception_infos.Count == 2)
  837. {
  838. day_deduction = dailyWage;
  839. meal_deduction += 10.00M;
  840. absenteeism_deduction += day_deduction;
  841. beLate_belate_ex.Reason = "上午-下午 缺卡/未打卡为旷工 一天";
  842. beLate_belate_ex.Deduction = day_deduction;
  843. ex_reissuecard_Items.Add(beLate_belate_ex);
  844. }
  845. else if (root.exception_infos.Count == 1)
  846. {
  847. if (earliest_time == lastest_time)
  848. {
  849. DateTime thisDt1 = Convert.ToDateTime(thisDtStr + " 12:00");
  850. if (thisDt <= thisDt1) //旷工 下午
  851. {
  852. day_deduction = day_miner_unit * 9; //4.5小时
  853. meal_deduction += 10.00M;
  854. absenteeism_deduction += day_deduction; //矿工半日
  855. beLate_belate_ex.Reason = thisDtStr + " 下午(18:00)缺卡/未打卡视为下午旷工(4.5小时)";
  856. beLate_belate_ex.Deduction = day_deduction;
  857. ex_reissuecard_Items.Add(beLate_belate_ex);
  858. }
  859. else if (thisDt >= thisDt1) //旷工下午
  860. {
  861. day_deduction = day_miner_unit * 6; //3小时
  862. absenteeism_deduction += day_deduction; //矿工半日
  863. beLate_belate_ex.Reason = thisDtStr + " 上午(09:00)缺卡/未打卡视为上午旷工(3小时)";
  864. beLate_belate_ex.Deduction = day_deduction;
  865. ex_reissuecard_Items.Add(beLate_belate_ex);
  866. }
  867. else //矿工一日
  868. {
  869. day_deduction = dailyWage;
  870. meal_deduction += 10.00M;
  871. absenteeism_deduction += day_deduction;
  872. beLate_belate_ex.Reason = thisDtStr + " 上午(09:00)-下午(18:00) 缺卡/未打视为旷工一天(7.5小时)";
  873. beLate_belate_ex.Deduction = day_deduction;
  874. ex_reissuecard_Items.Add(beLate_belate_ex);
  875. }
  876. }
  877. }
  878. }
  879. else if (exception == 4) //旷工
  880. {
  881. if (timelength > 60 && timelength <= 180)
  882. {
  883. day_deduction = day_miner_unit * 6; //3小时
  884. beLate_belate_ex.Reason = thisDtStr + " 缺卡/未打视为旷工上午(3小时)";
  885. meal_deduction += 10.00M;
  886. absenteeism_deduction += day_deduction; //矿工半日
  887. }
  888. else
  889. {
  890. day_deduction = dailyWage;
  891. beLate_belate_ex.Reason = thisDtStr + " 上午(09:00)-下午(18:00) 缺卡/未打视为旷工一天(7.5小时)";
  892. meal_deduction += 10.00M;
  893. absenteeism_deduction += day_deduction; //矿工一日
  894. }
  895. beLate_belate_ex.Deduction = day_deduction;
  896. ex_reissuecard_Items.Add(beLate_belate_ex);
  897. }
  898. }
  899. }
  900. #endregion
  901. #region 假勤/补卡次数 审批
  902. int leaveNum = 0; //请假次数
  903. int reissuecardNum = 0; //补卡次数\
  904. //类型:1 - 请假;2 - 补卡;3 - 出差;4 - 外出;100 - 外勤
  905. leaveNum = acc_sp_items.Where(it => it.type == 1).ToList().Count();
  906. reissuecardNum = acc_sp_items.Where(it => it.type == 2).ToList().Count();
  907. //请假审批
  908. if (leaveNum > 0)
  909. {
  910. List<Sp_Detail> sp_leave_details = new List<Sp_Detail>();
  911. sp_leave_details = await _qiYeWeChatApiService.GetApprovalDetailsAsync(startDt, endDt, acctid, 2, 1); //时间段内所有 已同意的 请假 审批数据
  912. if (sp_leave_details.Count <= 0)
  913. {
  914. _result.Msg += startDt + " - " + endDt + " " + itemName + " 请假 审批数据获取未获取到!\r\n";
  915. //continue;
  916. }
  917. List<Ex_Item> ex_ItemInfos = new List<Ex_Item>();
  918. foreach (Sp_Detail sp_item in sp_leave_details)
  919. {
  920. Apply_data? apply_data = sp_item.apply_data;
  921. if (apply_data != null)
  922. {
  923. List<ContentsItem> contents = apply_data.contents;
  924. ContentsItem content_Vacation = contents.Where(it => it.control == "Vacation").FirstOrDefault(); //请假类型
  925. ContentsItem content_Textarea = contents.Where(it => it.control == "Textarea").FirstOrDefault(); //多行文本
  926. if (content_Vacation != null)
  927. {
  928. Vacation vacation = content_Vacation.value.vacation;
  929. Attendance attendance = vacation.attendance; //假勤组件
  930. Selector selector = vacation.selector; //请假类型
  931. List<OptionsItem> optionsItems = selector.options; //key 请假类型 id
  932. List<TitleItem> value = optionsItems[0].value; // value 文本描述值
  933. int leaveType = int.Parse(optionsItems[0].key); //key 请假子类型 id
  934. Date_range date_Range = attendance.date_range;
  935. //筛选 不在工作日内的假勤申请
  936. if (startDt >= date_Range.new_begin_dt || date_Range.new_end_dt > endDt)
  937. {
  938. continue;
  939. }
  940. decimal thisTypeDeduction = 0.00M;//当前类型扣款
  941. string leave_starttime = date_Range.new_begin_dt.ToString("HH:mm");
  942. string leave_endtime = date_Range.new_end_dt.ToString("HH:mm");
  943. string typeName = string.Empty;
  944. string unit = string.Empty;
  945. int leaveTypeId = leaveType;
  946. var leaveTypeData = vacationLeaveTypes.Where(it => it.id == leaveTypeId).FirstOrDefault();
  947. if (leaveTypeData != null) { typeName = leaveTypeData.name; }
  948. string startTime = string.Empty;
  949. string endTime = string.Empty;
  950. //计算请假类型扣款金额
  951. decimal new_duration = 0.00M;
  952. if (date_Range.type == "halfday")
  953. {
  954. new_duration = Convert.ToDecimal(date_Range.new_duration) / 86400M;
  955. unit = "天";
  956. startTime = date_Range.new_begin_dt.ToString("yyyy-MM-dd") + " 09:00";
  957. endTime = date_Range.new_begin_dt.ToString("yyyy-MM-dd") + " 18:00";
  958. }
  959. else if (date_Range.type == "hour")
  960. {
  961. new_duration = Convert.ToDecimal(date_Range.new_duration) / 3600M;
  962. unit = "小时";
  963. startTime = date_Range.new_begin_dt.ToString("yyyy-MM-dd HH:mm:ss");
  964. endTime = date_Range.new_end_dt.ToString("yyyy-MM-dd HH:mm:ss");
  965. }
  966. decimal leave_meals = 0.00M;
  967. //计算餐补 假勤类型扣款
  968. CalculateTypeFee(leaveType, date_Range.type, leave_starttime, leave_endtime, amountPayable, work_days, new_duration,
  969. out leave_meals, out thisTypeDeduction);
  970. #region 累计类型扣款
  971. //1年假;2事假;3病假;4调休假;5婚假;6产假;7陪产假;8其他;9丧假
  972. if (leaveType == 2) //事假
  973. {
  974. personalLeaveTotal += thisTypeDeduction;
  975. }
  976. else if (leaveType == 3) //病假
  977. {
  978. sickLeaveTotal = thisTypeDeduction;
  979. }
  980. #endregion
  981. meal_deduction += leave_meals;
  982. Ex_Item ex_Item = new Ex_Item()
  983. {
  984. SubTypeId = leaveType,
  985. SubType = typeName,
  986. StartTimeDt = Convert.ToDateTime(startTime),
  987. EndTimeDt = Convert.ToDateTime(endTime),
  988. Duration = new_duration,
  989. Unit = unit,
  990. Deduction = thisTypeDeduction,
  991. //Reason = apply_data.reason,
  992. Apply_time_dt = Convert.ToDateTime(sp_item.apply_time_dt.ToString("yyyy-MM-dd HH:mm:ss")),
  993. //Approval_name = sp_item.approval_name,
  994. };
  995. ex_ItemInfos.Add(ex_Item);
  996. }
  997. }
  998. }
  999. if (ex_ItemInfos.Count > 0)
  1000. {
  1001. ex_Items_jq.Ex_ItemInfo = ex_ItemInfos.OrderBy(it => it.SubTypeId).ThenBy(it => it.Apply_time_dt).ToList();
  1002. ex_Items.Add(ex_Items_jq);
  1003. }
  1004. }
  1005. //打卡补卡
  1006. if (reissuecardNum > 0)
  1007. {
  1008. List<Sp_Detail> sp_buka_details = new List<Sp_Detail>();
  1009. sp_buka_details = await _qiYeWeChatApiService.GetApprovalDetailsAsync(startDt, endDt, acctid, 2, 2); //时间段内所有 已同意的 请假 审批数据
  1010. int bukaNum = 1;
  1011. foreach (var item in sp_buka_details)
  1012. {
  1013. Apply_data? apply_data = item.apply_data;
  1014. if (apply_data != null)
  1015. {
  1016. List<ContentsItem> contents = apply_data.contents;
  1017. ContentsItem content_Vacation = contents.Where(it => it.control == "PunchCorrection").FirstOrDefault(); //请假类型
  1018. ContentsItem content_Textarea = contents.Where(it => it.control == "Textarea").FirstOrDefault(); //多行文本
  1019. if (content_Vacation != null)
  1020. {
  1021. var punch_correction = content_Vacation.value.punch_correction;
  1022. DateTime bukaDt = punch_correction.time_dt;
  1023. //筛选 不在工作日内的假勤申请
  1024. if (startDt >= bukaDt || bukaDt > endDt)
  1025. {
  1026. continue;
  1027. }
  1028. decimal bukaPrice = 0.00M;
  1029. if (bukaNum <= 2)
  1030. {
  1031. bukaPrice = 10.00M;
  1032. }
  1033. else
  1034. {
  1035. bukaPrice = 50.00M;
  1036. }
  1037. var app_data = item.apply_data;
  1038. var punch_correction1 = app_data.contents[0].value.punch_correction; //未打卡时间
  1039. var punch_correction2 = app_data.contents[1].value;
  1040. Ex_Item ex_reissueCard = new Ex_Item()
  1041. {
  1042. SubTypeId = 7,
  1043. SubType = "打卡补卡",
  1044. StartTimeDt = Convert.ToDateTime(punch_correction1.time_dt.ToString("yyyy-MM-dd HH:mm:ss")), //未打卡时间
  1045. Deduction = bukaPrice,
  1046. Reason = punch_correction2.text,
  1047. Unit = string.Empty
  1048. };
  1049. unprinted_deduction += bukaPrice;
  1050. ex_reissuecard_Items.Add(ex_reissueCard);
  1051. bukaNum++;
  1052. }
  1053. }
  1054. }
  1055. }
  1056. if (ex_reissuecard_Items.Count > 0)
  1057. {
  1058. ex_Items_dk.Ex_ItemInfo = ex_reissuecard_Items;
  1059. ex_Items_dk.Ex_ItemInfo = ex_reissuecard_Items.OrderBy(it => it.SubTypeId).ThenBy(it => it.StartTimeDt).ToList();
  1060. ex_Items.Add(ex_Items_dk);
  1061. }
  1062. #endregion
  1063. }
  1064. else
  1065. {
  1066. meal_subsidy = work_days * 10.00M;
  1067. }
  1068. #region 应发合计 实发合计 扣款合计(假勤扣款,其他扣款,社保扣款,公积金代扣,个税扣款)
  1069. decimal mealTotal = meal_subsidy - meal_deduction; //餐补
  1070. decimal salaryTotal = 0.00M;
  1071. if (dk_work_days >= work_days)
  1072. {
  1073. dk_work_days = work_days;
  1074. salaryTotal = amountPayable + mealTotal; //应发合计
  1075. }
  1076. else
  1077. {
  1078. if (itemName.Equals("张海麟"))
  1079. {
  1080. salaryTotal = amountPayable + mealTotal; //应发合计
  1081. }
  1082. else
  1083. {
  1084. salaryTotal = (dk_work_days * dailyWage) + mealTotal; //应发合计
  1085. }
  1086. }
  1087. //扣款合计 不含个税
  1088. decimal eductionTotal = sickLeaveTotal + personalLeaveTotal + beLate_deduction + early_deduction + absenteeism_deduction + unprinted_deduction + other_deduction +
  1089. pm_wsInfo.WithholdingInsurance + pm_wsInfo.ReservedFunds + pm_wsInfo.OtherDeductions;
  1090. decimal actualReleaseTotal = salaryTotal - eductionTotal; //实发合计 * 不含个税
  1091. #endregion
  1092. #region 处理当月工资数据
  1093. pm_wsInfo.YearMonth = thisYearMonth;
  1094. pm_wsInfo.StartDate = startDt.ToString("yyyy-MM-dd");
  1095. pm_wsInfo.EndDate = endDt.ToString("yyyy-MM-dd");
  1096. pm_wsInfo.WorkDays = work_days; //当月应出勤天数
  1097. pm_wsInfo.RegularDays = dk_work_days; //当月正常出勤天数
  1098. pm_wsInfo.SickLeave = sickLeaveTotal; //病假
  1099. pm_wsInfo.SomethingFalse = personalLeaveTotal; //事假
  1100. pm_wsInfo.LateTo = beLate_deduction; //迟到
  1101. pm_wsInfo.LeaveEarly = early_deduction; //早退
  1102. pm_wsInfo.Absenteeism = absenteeism_deduction; //旷工
  1103. pm_wsInfo.NotPunch = unprinted_deduction; //未打卡
  1104. pm_wsInfo.OtherDeductions = other_deduction; //其他
  1105. pm_wsInfo.Ex_ItemsRemark = JsonConvert.SerializeObject(ex_Items); //
  1106. pm_wsInfo.Mealsupplement = mealTotal; //餐补
  1107. pm_wsInfo.Should = salaryTotal; //应发合计
  1108. pm_wsInfo.TotalDeductions = eductionTotal; //扣款合计
  1109. pm_wsInfo.TotalRealHair = actualReleaseTotal; //实发合计
  1110. pm_wsInfo.AfterTax = actualReleaseTotal - pm_wsInfo.WithholdingTax; //税后工资
  1111. pm_wsInfo.LastUpdateUserId = userId;
  1112. pm_wsInfo.LastUpdateDt = DateTime.Now;
  1113. pm_wsInfo.CreateUserId = userId;
  1114. pm_wsInfo.CreateTime = DateTime.Now;
  1115. pm_wsInfo.DeleteUserId = null;
  1116. pm_wsInfo.DeleteTime = null;
  1117. #endregion
  1118. }
  1119. }
  1120. catch (Exception ex)
  1121. {
  1122. _result.Msg = ex.Message;
  1123. return _result;
  1124. }
  1125. _result.Code = 0;
  1126. _result.Data = pm_WageSheetDattaSources;
  1127. return _result;
  1128. }
  1129. /// <summary>
  1130. /// decimal 保留两位小数 不四舍五入
  1131. /// </summary>
  1132. /// <param name="number"></param>
  1133. /// <returns></returns>
  1134. public static decimal ConvertToDecimal(decimal number)
  1135. {
  1136. return Convert.ToDecimal(number.ToString("0.00"));
  1137. }
  1138. /// <summary>
  1139. /// 获取请假类型
  1140. /// </summary>
  1141. /// <param name="template_id"></param>
  1142. /// <returns></returns>
  1143. public static async Task<List<VacationLeaveTypeView>> GetVacationLeaveTypes(string template_id)
  1144. {
  1145. List<VacationLeaveTypeView> vacationLeaveTypes = new List<VacationLeaveTypeView>();
  1146. TemplateDetailView templateDetailView = new TemplateDetailView();
  1147. templateDetailView = await _qiYeWeChatApiService.GetTemplateDetailAsync(template_id);
  1148. if (templateDetailView.errcode != 0)
  1149. {
  1150. Serilog.Log.Error("【企业微信】【审批】【获取假勤类型的审批】【Msg】"+ templateDetailView.errmsg);
  1151. return vacationLeaveTypes;
  1152. }
  1153. List<VacationItemInfo> VacationItemInfos = templateDetailView.vacation_list.item;
  1154. foreach (var item in VacationItemInfos)
  1155. {
  1156. ValueItem valueInfo = item.name.Where(it => it.lang == "zh_CN").FirstOrDefault();
  1157. if (valueInfo != null)
  1158. {
  1159. vacationLeaveTypes.Add(
  1160. new VacationLeaveTypeView()
  1161. {
  1162. id = item.id,
  1163. name = valueInfo.text
  1164. });
  1165. }
  1166. }
  1167. return vacationLeaveTypes;
  1168. }
  1169. /// <summary>
  1170. /// 计算类型费用
  1171. /// </summary>
  1172. /// <param name="leaveType">
  1173. /// 1年假;2事假;3病假;4调休假;5婚假;6产假;7陪产假;8其他;9丧假
  1174. /// </param>
  1175. /// <param name="date_Range_type">
  1176. /// halfday 全天
  1177. /// hour 小时
  1178. /// </param>
  1179. /// <param name="startTime"></param>
  1180. /// <param name="endTime"></param>
  1181. /// <param name="duration"></param>
  1182. /// <param name="mealDeduction"></param>
  1183. /// <param name="typeDeduction"></param>
  1184. public static void CalculateTypeFee(int leaveType, string date_Range_type, string startTime, string endTime,decimal amountPayable,int work_days,
  1185. decimal duration, out decimal mealDeduction, out decimal typeDeduction)
  1186. {
  1187. typeDeduction = 0;
  1188. mealDeduction = 0;
  1189. string am_starttime = "08:59";
  1190. string am_endtime = "13:01";
  1191. decimal personalkLeave_dailywage_day = ConvertToDecimal( amountPayable / work_days); //日薪 = 事假日薪 *计算方式:日平均工资 = 当月应发工资 /当月应出勤天数。
  1192. //半小时单位
  1193. decimal halfHour = Convert.ToDecimal(7.5) / Convert.ToDecimal(0.5);
  1194. switch (leaveType)
  1195. {
  1196. case 1: //年假
  1197. CalculateTypeFeeSub(date_Range_type, startTime, endTime, duration, out mealDeduction);
  1198. break;
  1199. case 2: //2事假
  1200. // 事假日薪 *计算方式:日平均工资 = 当月应发工资 /当月应出勤天数。
  1201. decimal personalkLeave_dailywage_halfhour = ConvertToDecimal( personalkLeave_dailywage_day / halfHour); //事假单位 0.5小时
  1202. if (date_Range_type == "halfday")
  1203. {
  1204. mealDeduction = 10; //餐补扣款
  1205. typeDeduction = personalkLeave_dailywage_day;
  1206. }
  1207. else if (date_Range_type == "hour")
  1208. {
  1209. decimal leave_halfHour = Convert.ToDecimal(duration) / Convert.ToDecimal(0.5);
  1210. typeDeduction = personalkLeave_dailywage_halfhour * leave_halfHour;
  1211. if (duration >= 3 && duration <= 7.5M) //单天请假三小时 && 请假时间在上午 则没有餐补
  1212. {
  1213. //处理开始时间
  1214. if (startTime.CompareTo(am_starttime) > 0 && startTime.CompareTo(am_endtime) < 0)
  1215. {
  1216. //处理结束时间
  1217. //if (endTime.CompareTo(am_starttime) > 0 && endTime.CompareTo(am_endtime) > 0)
  1218. //{
  1219. mealDeduction = 10; //餐补扣款
  1220. //}
  1221. }
  1222. }
  1223. else if (duration > 7.5M) //多天计算
  1224. {
  1225. decimal leave_halfHour1 = Convert.ToDecimal(duration) / Convert.ToDecimal(0.5);
  1226. typeDeduction = personalkLeave_dailywage_halfhour * leave_halfHour1;
  1227. decimal leaveDays = duration / 7.5M;
  1228. if (leaveDays % 1 == 0)
  1229. {
  1230. mealDeduction = 10 * leaveDays; //餐补扣款
  1231. }
  1232. else
  1233. {
  1234. mealDeduction = 10 * Convert.ToInt32(leaveDays);
  1235. //得到最后一天的请假时间 是否有餐补
  1236. int lastHours = (Convert.ToDateTime(endTime) - Convert.ToDateTime("09:00")).Hours;
  1237. if (lastHours >= 3)
  1238. {
  1239. //处理结束时间
  1240. if (endTime.CompareTo(am_starttime) > 0 && endTime.CompareTo(am_endtime) < 0)
  1241. {
  1242. mealDeduction += 10; //餐补扣款
  1243. }
  1244. }
  1245. }
  1246. }
  1247. }
  1248. break;
  1249. case 3: //3病假
  1250. // 病假日薪 *计算方式:日平均工资 = 成都市最低工资标准的80% /当月应出勤天数。 短期病假=当月15天内
  1251. decimal chengDuMinimumWage_halrHour = ConvertToDecimal( _chengDuMinimumWage / work_days) / halfHour;
  1252. decimal sickLeave_dailywage_halfhour_deduction = personalkLeave_dailywage_day - chengDuMinimumWage_halrHour; //病假单位 0.5小时 扣款金额
  1253. if (date_Range_type == "halfday")
  1254. {
  1255. mealDeduction = 10; //餐补扣款
  1256. typeDeduction = sickLeave_dailywage_halfhour_deduction * halfHour;
  1257. }
  1258. else if (date_Range_type == "hour")
  1259. {
  1260. decimal sickLeave_halfHour = duration / 0.5M;
  1261. typeDeduction = sickLeave_dailywage_halfhour_deduction *sickLeave_halfHour ;
  1262. if (duration >= 3 && duration <= 7.5M) //单天请假三小时 && 请假时间在上午 则没有餐补
  1263. {
  1264. //处理开始时间
  1265. if (startTime.CompareTo(am_starttime) > 0 && startTime.CompareTo(am_endtime) < 0)
  1266. {
  1267. //处理结束时间
  1268. if (endTime.CompareTo(am_starttime) > 0 && endTime.CompareTo(am_endtime) < 0)
  1269. {
  1270. mealDeduction = 10; //餐补扣款
  1271. }
  1272. }
  1273. }
  1274. else if (duration > 7.5M) //多天计算
  1275. {
  1276. decimal sickLeave_halfHour1 = duration / 0.5M;
  1277. typeDeduction = sickLeave_dailywage_halfhour_deduction * sickLeave_halfHour1;
  1278. decimal leaveDays = Convert.ToDecimal(duration / 7.5M);
  1279. if (leaveDays % 1 == 0)
  1280. {
  1281. mealDeduction = 10 * leaveDays; //餐补扣款
  1282. }
  1283. else
  1284. {
  1285. mealDeduction = 10 * Convert.ToInt32(leaveDays);
  1286. //得到最后一天的请假时间 是否有餐补
  1287. int lastHours = (Convert.ToDateTime(endTime) - Convert.ToDateTime("09:00")).Hours;
  1288. if (lastHours >= 3)
  1289. {
  1290. //处理结束时间
  1291. if (endTime.CompareTo(am_starttime) > 0 && endTime.CompareTo(am_endtime) < 0)
  1292. {
  1293. mealDeduction += 10; //餐补扣款
  1294. }
  1295. }
  1296. }
  1297. }
  1298. }
  1299. break;
  1300. case 4: //4调休假
  1301. CalculateTypeFeeSub(date_Range_type, startTime, endTime, duration, out mealDeduction);
  1302. break;
  1303. case 5: //5婚假
  1304. CalculateTypeFeeSub(date_Range_type, startTime, endTime, duration, out mealDeduction);
  1305. break;
  1306. case 6: //6产假
  1307. CalculateTypeFeeSub(date_Range_type, startTime, endTime, duration, out mealDeduction);
  1308. break;
  1309. case 7: //7陪产假
  1310. CalculateTypeFeeSub(date_Range_type, startTime, endTime, duration, out mealDeduction);
  1311. break;
  1312. case 8: //8其他
  1313. CalculateTypeFeeSub(date_Range_type, startTime, endTime, duration, out mealDeduction);
  1314. break;
  1315. case 9: //9丧假
  1316. CalculateTypeFeeSub(date_Range_type, startTime, endTime, duration, out mealDeduction);
  1317. break;
  1318. }
  1319. }
  1320. /// <summary>
  1321. /// 计算类型费用
  1322. /// </summary>
  1323. /// <param name="date_Range_type">
  1324. /// halfday 全天
  1325. /// hour 小时
  1326. /// </param>
  1327. /// <param name="startTime"></param>
  1328. /// <param name="endTime"></param>
  1329. /// <param name="duration"></param>
  1330. /// <param name="mealDeduction"></param>
  1331. public static void CalculateTypeFeeSub(string date_Range_type, string startTime, string endTime, decimal duration, out decimal mealDeduction)
  1332. {
  1333. mealDeduction = 0;
  1334. string am_starttime = "08:59";
  1335. string am_endtime = "13:01";
  1336. if (date_Range_type == "halfday")
  1337. {
  1338. mealDeduction = 10; //餐补扣款
  1339. }
  1340. else if (date_Range_type == "hour")
  1341. {
  1342. if (duration >= 3 && duration < 7) //单天请假三小时 && 请假时间在上午 则没有餐补
  1343. {
  1344. //处理开始时间
  1345. if (startTime.CompareTo(am_starttime) > 0 && startTime.CompareTo(am_endtime) < 0)
  1346. {
  1347. //处理结束时间
  1348. if (endTime.CompareTo(am_starttime) > 0 && endTime.CompareTo(am_endtime) < 0)
  1349. {
  1350. mealDeduction = 10; //餐补扣款
  1351. }
  1352. }
  1353. }
  1354. else if (duration >= 7 && duration <= 7.50M )
  1355. {
  1356. mealDeduction = 10; //餐补扣款
  1357. }
  1358. else if (duration >= 7.50M) //多天计算
  1359. {
  1360. decimal leaveDays = Convert.ToDecimal(duration / 7.50M);
  1361. if (leaveDays % 1 == 0)
  1362. {
  1363. mealDeduction = 10 * leaveDays; //餐补扣款
  1364. }
  1365. else
  1366. {
  1367. mealDeduction = 10 * Convert.ToInt32(leaveDays);
  1368. //得到最后一天的请假时间 是否有餐补
  1369. int lastHours = (Convert.ToDateTime(endTime) - Convert.ToDateTime("09:00")).Hours;
  1370. if (lastHours >= 3)
  1371. {
  1372. //处理结束时间
  1373. if (endTime.CompareTo(am_starttime) > 0 && endTime.CompareTo(am_endtime) < 0)
  1374. {
  1375. mealDeduction += 10; //餐补扣款
  1376. }
  1377. }
  1378. }
  1379. }
  1380. }
  1381. }
  1382. /// <summary>
  1383. /// 获取打卡补卡类型
  1384. /// </summary>
  1385. /// <param name="template_id"></param>
  1386. /// <returns></returns>
  1387. public static async Task<List<VacationLeaveTypeView>> GetVacationReissueCardTypes(string template_id)
  1388. {
  1389. List<VacationLeaveTypeView> vacationLeaveTypes = new List<VacationLeaveTypeView>();
  1390. TemplateDetailView templateDetailView = new TemplateDetailView();
  1391. templateDetailView = await _qiYeWeChatApiService.GetTemplateDetailAsync(template_id);
  1392. if (templateDetailView.errcode != 0)
  1393. {
  1394. return vacationLeaveTypes;
  1395. }
  1396. List<VacationItemInfo> VacationItemInfos = templateDetailView.vacation_list.item;
  1397. foreach (var item in VacationItemInfos)
  1398. {
  1399. ValueItem valueInfo = item.name.Where(it => it.lang == "zh_CN").FirstOrDefault();
  1400. if (valueInfo != null)
  1401. {
  1402. vacationLeaveTypes.Add(
  1403. new VacationLeaveTypeView()
  1404. {
  1405. id = item.id,
  1406. name = valueInfo.text
  1407. });
  1408. }
  1409. }
  1410. return vacationLeaveTypes;
  1411. }
  1412. /// <summary>
  1413. /// 打卡数据
  1414. /// 假勤数据 统计
  1415. /// </summary>
  1416. /// <param name="datas">数据源</param>
  1417. /// <param name="type">
  1418. /// 1-请假;2-补卡;3-出差;4-外出;100-外勤;
  1419. /// </param>
  1420. /// <param name="subTypeName">
  1421. /// 年假 事假 病假 调休假 婚嫁 产假 陪产假 丧假 补卡次数 出差 外出数 外勤 其他
  1422. /// </param>
  1423. /// <returns></returns>
  1424. private static int Fallibilitydispose(List<Sp_Item> datas, int type, string? subTypeName)
  1425. {
  1426. int num = 0;
  1427. Sp_Item _Info = datas.Where(it => it.type == type && it.name == subTypeName).FirstOrDefault();
  1428. if (_Info != null) { num = _Info.count; }
  1429. return num;
  1430. }
  1431. /// <summary>
  1432. /// 打卡数据
  1433. /// 异常数据 统计
  1434. /// </summary>
  1435. /// <returns></returns>
  1436. private static int ExceptionStatistics(List<Exception_Info> datas, int type)
  1437. {
  1438. int num = 0;
  1439. Exception_Info _Info = datas.Where(it => it.exception == type).FirstOrDefault();
  1440. if (_Info != null) { num = _Info.count; }
  1441. return num;
  1442. }
  1443. /// <summary>
  1444. /// 获取时间段内除周末 节假日外的 工作日
  1445. /// </summary>
  1446. /// <param name="startDt"></param>
  1447. /// <param name="endDt"></param>
  1448. /// <returns></returns>
  1449. public static async Task<int> GetWorkDays(string yearMonth)
  1450. {
  1451. int workdays = 0;
  1452. string sql = string.Format(@"Select * From Pm_WageIssueWorkingDay
  1453. Where Isdel = 0 And YearMonth = '{0}' ", yearMonth);
  1454. var data = await _usersRep._sqlSugar.SqlQueryable<WageYearMonthView>(sql).FirstAsync();
  1455. if (data != null)
  1456. {
  1457. workdays = data.Workdays;
  1458. }
  1459. return workdays;
  1460. }
  1461. }
  1462. }