PayrollComputation.cs 51 KB

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