PayrollComputation.cs 50 KB

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