FeeAuditRepository.cs 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  1. using EyeSoft.Extensions;
  2. using OASystem.Domain;
  3. using OASystem.Domain.Entities.Groups;
  4. using OASystem.Domain.Entities.Resource;
  5. using OASystem.Domain.ViewModels.Groups;
  6. namespace OASystem.Infrastructure.Repositories.Groups
  7. {
  8. /// <summary>
  9. /// 费用审核仓储
  10. /// </summary>
  11. public class FeeAuditRepository : BaseRepository<EntityBase, ViewBase>
  12. {
  13. public FeeAuditRepository(SqlSugarClient sqlSugar) : base(sqlSugar)
  14. {
  15. }
  16. /// <summary>
  17. /// 费用自动审核
  18. /// </summary>
  19. /// <param name="feeType">
  20. /// 1.酒店 76
  21. /// 2.op 79
  22. /// 3.其他费用-出行物资 98
  23. /// 4.保险 82
  24. /// 5.机票 85
  25. /// </param>
  26. /// <param name="diId">团组Id</param>
  27. /// <param name="dataId">数据Id(模块类型主表Id)</param>
  28. /// <returns></returns>
  29. public async Task<JsonView> FeeAutomaticAudit(int feeType, int diId, int dataId)
  30. {
  31. var _view = new JsonView() { Code = 201, Msg = "自动审核操作失败" };
  32. if (diId < 1) { _view.Msg = MsgTips.DiId; return _view; }
  33. if (dataId < 1) { _view.Msg = MsgTips.Id; return _view; }
  34. var groupDetails = _sqlSugar.Queryable<Grp_DelegationInfo>().Where(x => x.IsDel == 0 && x.Id == diId).First();
  35. if (groupDetails == null)
  36. {
  37. _view.Msg = $"团组信息为空,不可自动审核!";
  38. return _view;
  39. }
  40. var stids = new List<int>() { 17, 66, 91 };
  41. var setData = _sqlSugar.Queryable<Sys_SetData>().Where(x => x.IsDel == 0 && stids.Contains(x.STid)).ToList();
  42. var groupInfo = new Grp_GroupCostParameter();
  43. string _teamCurrency = string.Empty;
  44. decimal _teamRate = 1.0000M;
  45. var costContents = new List<GroupCostAuditView>();
  46. var subFeeTypeIds = new int[] {
  47. 4, // 保险 82
  48. 5, // 机票 85
  49. };
  50. if (!subFeeTypeIds.Contains(feeType))
  51. {
  52. groupInfo = _sqlSugar.Queryable<Grp_GroupCostParameter>().Where(x => x.IsDel == 0 && x.DiId == diId).First();
  53. if (groupInfo == null) { _view.Msg = $"团组成本信息未填写!"; return _view; }
  54. _teamCurrency = groupInfo.Currency;
  55. _teamRate = groupInfo.Rate;
  56. //币种验证 统一为currencycode三字码
  57. if (int.TryParse(_teamCurrency, out int currency)) _teamCurrency = setData.Find(x => x.Id == currency)?.Name ?? "";
  58. string costContentSql = $"Select * From Grp_GroupCost";
  59. costContents = _sqlSugar.SqlQueryable<GroupCostAuditView>(costContentSql).Where(x => x.IsDel == 0 && x.Diid == diId).ToList();
  60. if (costContents.Count < 1) { _view.Msg = $"团组成本信息未填写!"; return _view; }
  61. //处理 成本详细信息 日期为空
  62. for (int i = 0; i < costContents.Count; i++)
  63. {
  64. if (string.IsNullOrEmpty(costContents[i].Date))
  65. {
  66. int index = i - 1;
  67. if (index >= 0)
  68. {
  69. costContents[i].Date = costContents[index].Date;
  70. var dtBool = DateTime.TryParse(costContents[i].Date, out DateTime _dateTime);
  71. if (dtBool)
  72. {
  73. costContents[i].CurrTime = _dateTime;
  74. }
  75. }
  76. }
  77. else
  78. {
  79. var dtBool = DateTime.TryParse(costContents[i].Date, out DateTime _dateTime);
  80. if (dtBool)
  81. {
  82. costContents[i].CurrTime = _dateTime;
  83. }
  84. }
  85. }
  86. }
  87. if (feeType == 1)
  88. {
  89. //1089 对冲账或其他 不在审核范围
  90. var hotelCostInfo = _sqlSugar.Queryable<Grp_HotelReservations>().Where(x => x.IsDel == 0 && x.DiId == diId && x.Id == dataId && x.CheckType != 1089).First();
  91. var hotelCostDetails = _sqlSugar.Queryable<Grp_HotelReservationsContent>().Where(x => x.IsDel == 0 && x.DiId == diId && x.HrId == dataId).ToList();
  92. if (hotelCostInfo == null)
  93. {
  94. _view.Msg = $"酒店费用数据未填写";
  95. QuashAudit(76, diId, dataId);
  96. return _view;
  97. }
  98. //酒店费用金额 == 0 不自动审核
  99. if (hotelCostInfo.CardPrice == 0.0000M)
  100. {
  101. _view.Msg = $"酒店费用金额 == 0 不自动审核";
  102. QuashAudit(76, diId, dataId);
  103. return _view;
  104. }
  105. //获取C表汇率
  106. decimal _rate = 1.0000M;
  107. var roomFeeInfo = hotelCostDetails.Where(x => x.PriceType == 1).First();
  108. if (roomFeeInfo == null)
  109. {
  110. _view.Msg = $"酒店房间费用付款数据未填写";
  111. return _view;
  112. }
  113. _rate = roomFeeInfo.Rate == 0.0000M ? 1.0000M : roomFeeInfo.Rate;
  114. bool isAutoAudit = true; //是否自动审核
  115. DateTime checkIn = Convert.ToDateTime(hotelCostInfo.CheckInDate),
  116. checkOut = Convert.ToDateTime(hotelCostInfo.CheckOutDate);
  117. if (checkOut > checkIn) checkOut = checkOut.AddDays(-1); //房费计算,结束日期为前一天
  118. var hotelCostInfos = costContents.Where(x => x.CurrTime >= checkIn && x.CurrTime <= checkOut).ToList();
  119. if (hotelCostInfos.Count < 1) isAutoAudit = false;
  120. #region 其他费用(早餐、地税、城市税) 按照个人计算 2025-07-01 14:07:25
  121. decimal otherFee = hotelCostDetails.Where(x => x.PriceType != 1).Sum(x => x.Price * (x.Rate == 0.0000M ? 1.0000M : x.Rate));
  122. int guestCount = 0;
  123. if (hotelCostInfo.GuestName.Contains(',')) guestCount = hotelCostInfo.GuestName.Split(",").Length;
  124. else guestCount = 1;
  125. if (otherFee > 0) otherFee /= guestCount;
  126. #endregion
  127. var hotelCostInfosGroup = hotelCostInfos.GroupBy(x => x.Date);
  128. foreach (var item in hotelCostInfosGroup)
  129. {
  130. var hotelSingleRoomFee = item.Sum(x => x.HotelSingleRoomFee) * _teamRate; //成本单间费用
  131. var hotelDoubleRoomFee = item.Sum(x => x.HotelDoubleRoomFee) * _teamRate; //成本双人间费用
  132. var hotelSuiteFee = item.Sum(x => x.HotelSuiteFee) * _teamRate; //成本套房费用
  133. var hotelSuiteRoomFee = item.Sum(x => x.HotelSuiteRoomFee) * _teamRate; //成本其他房型间费用
  134. //1.判断费用是否 <= 成本费用
  135. //1.1 判断单间费用
  136. if (hotelCostInfo.SingleRoomPrice > 0)
  137. {
  138. decimal singleRoomPrice = (hotelCostInfo.SingleRoomPrice + otherFee) * _rate; //酒店录入费用
  139. if (singleRoomPrice > hotelSingleRoomFee) isAutoAudit = false;
  140. }
  141. //1.2 判断双人间费用
  142. if (hotelCostInfo.DoubleRoomPrice > 0)
  143. {
  144. decimal doubleRoomPrice = (hotelCostInfo.DoubleRoomPrice + otherFee) * _rate;//酒店录入费用
  145. if (doubleRoomPrice > hotelDoubleRoomFee) isAutoAudit = false;
  146. }
  147. //1.3 判断套房费用
  148. if (hotelCostInfo.SuiteRoomPrice > 0)
  149. {
  150. decimal suiteRoomPrice = (hotelCostInfo.SuiteRoomPrice + otherFee) * _rate;//酒店录入费用
  151. if (suiteRoomPrice > hotelSuiteFee) isAutoAudit = false;
  152. }
  153. //1.4 判断其他房型费用
  154. if (hotelCostInfo.OtherRoomPrice > 0)
  155. {
  156. decimal otherRoomPrice = (hotelCostInfo.OtherRoomPrice + otherFee) * _rate;//酒店录入费用
  157. if (otherRoomPrice > hotelSuiteRoomFee) isAutoAudit = false;
  158. }
  159. }
  160. //2.判断是否自动审核
  161. if (isAutoAudit)
  162. {
  163. var ccpUpdate = _sqlSugar.Updateable<Grp_CreditCardPayment>()
  164. .SetColumns(it => it.IsAuditGM == 3)
  165. .SetColumns(it => it.AuditGMOperate == 4)
  166. .SetColumns(it => it.AuditGMDate == DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
  167. .Where(s => s.DIId == diId && s.CTable == 76 && s.CId == dataId)
  168. .ExecuteCommand();
  169. if (ccpUpdate > 0)
  170. {
  171. _view.Code = 200;
  172. _view.Msg = "自动审核执行成功";
  173. return _view;
  174. }
  175. }
  176. else
  177. {
  178. //撤销该条数据的自动审核 --> 该条数据的审核状态是自动审核 3 --> 0
  179. var quashStatus = QuashAudit(76, diId, dataId);
  180. if (quashStatus)
  181. {
  182. _view.Code = 200;
  183. _view.Msg = "费用超团组成本,自动审核撤销成功!";
  184. return _view;
  185. }
  186. }
  187. }
  188. else if (feeType == 2)
  189. {
  190. //1.基础数据参数验证
  191. var priceType = new List<int>() {
  192. 1062 //1062 尾款
  193. };
  194. var opinfos = _sqlSugar.Queryable<Grp_CarTouristGuideGroundReservations>()
  195. .Where(x => x.IsDel == 0 && x.DiId == diId && x.Id == dataId && !priceType.Contains(x.PriceType))
  196. .First();
  197. //var opinfos = _sqlSugar.Queryable<Grp_CarTouristGuideGroundReservations>()
  198. // .Where(x => x.IsDel == 0 && x.DiId == diId && x.Id == dataId )
  199. // .First();
  200. if (opinfos == null)
  201. {
  202. _view.Msg = $"OP费用费用类型属于“尾款”或者 内容未填写";
  203. return _view;
  204. }
  205. //1.含超时费用/超支费用 手动审核
  206. if (opinfos.SelectCheck.Contains("超时") || opinfos.SelectCheck.Contains("超支") || opinfos.SelectCheck.Contains("尾款"))
  207. {
  208. _view.Msg = @$"OP费用含尾款/超支/超时费用,请手动审核";
  209. return _view;
  210. }
  211. //1.参数验证
  212. var opCheckPriceTyeps = opinfos.SelectCheck.Split(',');
  213. var opCheckPriceTyepIds = setData.Where(x => opinfos.SelectCheck.Split(',').Contains(x.Name)).Select(x => x.Id).ToList();
  214. var opContents = _sqlSugar.Queryable<Grp_CarTouristGuideGroundReservationsContent>()
  215. .Where(x => x.IsDel == 0 && x.DiId == diId && x.CTGGRId == dataId && opCheckPriceTyepIds.Contains(x.SId))
  216. .OrderBy(x => x.DatePrice, OrderByType.Asc)
  217. .ToList();
  218. if (opContents.Count < 1)
  219. {
  220. _view.Msg = $"OP费用费用内容未填写";
  221. return _view;
  222. }
  223. //获取C表汇率
  224. decimal _opRate = 1.0000M;
  225. decimal _opPayPercentage = 1.0000M;
  226. var payInfo = _sqlSugar.Queryable<Grp_CreditCardPayment>().Where(x => x.IsDel == 0 && x.DIId == diId && x.CTable == 79 && x.CId == dataId).First();
  227. if (payInfo == null)
  228. {
  229. _view.Msg = $"OP费用付款数据未填写";
  230. return _view;
  231. }
  232. _opRate = payInfo.DayRate;
  233. _opPayPercentage = payInfo.PayPercentage / 100.00M;
  234. string opCurrencyName = setData.Find(x => x.Id == opContents[0].Currency)?.Name ?? "";
  235. //团组、OP币种 验证是否一致(一致:只比较金额,不计算汇率(2024-04-18))
  236. if (opCurrencyName.Equals(_teamCurrency))
  237. {
  238. _opRate = payInfo.DayRate;
  239. _teamRate = payInfo.DayRate;
  240. }
  241. var opBasicDatas = setData.Where(x => x.STid == 17).ToList(); //费用类型基础数据
  242. bool isAutoAudit = true;
  243. if (!DateTime.TryParse(opinfos.ServiceStartTime, out DateTime startDt1) || !DateTime.TryParse(opinfos.ServiceEndTime, out DateTime endDt1))
  244. {
  245. _view.Msg = $"OP费用服务起止日期格式不正确!";
  246. return _view;
  247. }
  248. DateTime startDt = startDt1;
  249. DateTime endDt = endDt1;
  250. var opCostDatas = costContents.Where(it => Convert.ToDateTime(it.Date) >= startDt && Convert.ToDateTime(it.Date) <= endDt).ToList();
  251. if (opCostDatas.Count < 1)
  252. {
  253. _view.Msg = $"该时间段内团组成本未填写!";
  254. return _view;
  255. }
  256. var noAuditFeeTypeIds = new List<int> {
  257. 982 ,//982 车超时费 -- 暂无
  258. 96 ,//96 接送机费 -- 暂无
  259. 97 ,//97 其他费用 -- 暂无
  260. 992 ,//992 住补费用 -- 暂无
  261. 1059,//1059 导游超时费用 -- 暂无
  262. 1070,//1070 尾款金额 -- 暂无
  263. 1071,//1071 其他额外费用 -- 暂无
  264. 1073,//1073 翻译超时费 -- 暂无
  265. 1074,//1074 早餐超支费用 -- 暂无
  266. 1075,//1075 午餐超支费用 -- 暂无
  267. 1076,//1076 晚餐超支费用 -- 暂无
  268. };
  269. //费用类型筛选 包含 feeTypeIds && 包含这些类型费用大于0
  270. var noAuditFeeContents = opContents.Where(x => x.Price > 0 && noAuditFeeTypeIds.Contains(x.SId)).ToList();
  271. if (noAuditFeeContents.Count > 0)
  272. {
  273. _view.Msg = @$"OP费用含尾款/超支/超时费用,请手动审核";
  274. return _view;
  275. }
  276. //2.按天按项 检查费用是否超过预算
  277. var opDayContent = opContents.GroupBy(x => x.DatePrice);
  278. foreach (var item in opDayContent)
  279. {
  280. var opCostInfo = opCostDatas.Where(x => Convert.ToDateTime(x.Date) == item.Key).ToList();
  281. if (opCostInfo.Count < 1) continue;
  282. //车费 91
  283. var opCarCost = item.FirstOrDefault(x => x.SId == 91);
  284. if (opCarCost != null)
  285. if (opCarCost.Price * _opRate * _opPayPercentage > _teamRate * opCostInfo.Sum(x => x.CarFee) * _opPayPercentage) isAutoAudit = false;
  286. //982 车超时费 -- 暂无
  287. //92 导游费
  288. var opGuideCost = item.FirstOrDefault(x => x.SId == 92);
  289. if (opGuideCost != null)
  290. if (opGuideCost.Price * _opRate * _opPayPercentage > _teamRate * opCostInfo.Sum(x => x.GuideFee) * _opPayPercentage) isAutoAudit = false;
  291. //94 导游景点费
  292. var opGuideScenicCost = item.FirstOrDefault(x => x.SId == 94);
  293. if (opGuideScenicCost != null)
  294. if (opGuideScenicCost.Price * _opRate * _opPayPercentage > _teamRate * opCostInfo.Sum(x => x.GuideScenicFee) * _opPayPercentage) isAutoAudit = false;
  295. //95 导游小费
  296. var opGuideTipCost = item.FirstOrDefault(x => x.SId == 95);
  297. if (opGuideTipCost != null)
  298. if (opGuideTipCost.Price * _opRate * _opPayPercentage > _teamRate * opCostInfo.Sum(x => x.GuideTipFee) * _opPayPercentage) isAutoAudit = false;
  299. //983 导游餐补
  300. var opGuideMealCost = item.FirstOrDefault(x => x.SId == 983);
  301. if (opGuideMealCost != null)
  302. if (opGuideMealCost.Price * _opRate * _opPayPercentage > _teamRate * opCostInfo.Sum(x => x.GuideMealFee) * _opPayPercentage) isAutoAudit = false;
  303. //984 导游房补
  304. var opGuideRoomCost = item.FirstOrDefault(x => x.SId == 984);
  305. if (opGuideRoomCost != null)
  306. if (opGuideRoomCost.Price * _opRate * _opPayPercentage > _teamRate * opCostInfo.Sum(x => x.GuideRoomFee) * _opPayPercentage) isAutoAudit = false;
  307. //985 导游交通
  308. var opGuideTrafficCost = item.FirstOrDefault(x => x.SId == 985);
  309. if (opGuideTrafficCost != null)
  310. if (opGuideTrafficCost.Price * _opRate * _opPayPercentage > _teamRate * opCostInfo.Sum(x => x.GuideTrafficFee) * _opPayPercentage) isAutoAudit = false;
  311. //96 接送机费 -- 暂无
  312. //97 其他费用 -- 暂无
  313. //979 司机工资
  314. var opDriverCost = item.FirstOrDefault(x => x.SId == 979);
  315. if (opDriverCost != null)
  316. if (opDriverCost.Price * _opRate > _teamRate * _opPayPercentage * opCostInfo.Sum(x => x.DriverFee) * _opPayPercentage) isAutoAudit = false;
  317. //980 司机小费
  318. var opDriverTipCost = item.FirstOrDefault(x => x.SId == 980);
  319. if (opDriverTipCost != null)
  320. if (opDriverTipCost.Price * _opRate * _opPayPercentage > _teamRate * opCostInfo.Sum(x => x.DriverTipFee) * _opPayPercentage) isAutoAudit = false;
  321. //981 司机餐补
  322. var opDriverMealCost = item.FirstOrDefault(x => x.SId == 981);
  323. if (opDriverMealCost != null)
  324. if (opDriverMealCost.Price * _opRate * _opPayPercentage > _teamRate * opCostInfo.Sum(x => x.DriverMealFee) * _opPayPercentage) isAutoAudit = false;
  325. //988 客户早餐费用
  326. var opClientBreakfastCost = item.FirstOrDefault(x => x.SId == 988);
  327. if (opClientBreakfastCost != null)
  328. if (opClientBreakfastCost.Price * _opRate * _opPayPercentage > _teamRate * opCostInfo.Sum(x => x.ClientBreakfastFee) * _opPayPercentage) isAutoAudit = false;
  329. //93 客户午餐费用
  330. var opClientLunchCost = item.FirstOrDefault(x => x.SId == 93);
  331. if (opClientLunchCost != null)
  332. if (opClientLunchCost.Price * _opRate * _opPayPercentage > _teamRate * opCostInfo.Sum(x => x.ClientLunchFee) * _opPayPercentage) isAutoAudit = false;
  333. //989 客户晚餐费用
  334. var opClientDinnerCost = item.FirstOrDefault(x => x.SId == 989);
  335. if (opClientDinnerCost != null)
  336. if (opClientDinnerCost.Price * _opRate * _opPayPercentage > _teamRate * opCostInfo.Sum(x => x.ClientDinnerFee) * _opPayPercentage) isAutoAudit = false;
  337. //990 景点门票费
  338. var opScenicTicketCost = item.FirstOrDefault(x => x.SId == 990);
  339. if (opScenicTicketCost != null)
  340. if (opScenicTicketCost.Price * _opRate * _opPayPercentage > _teamRate * opCostInfo.Sum(x => x.ScenicTicketFee) * _opPayPercentage) isAutoAudit = false;
  341. //991 饮料/零食/水果
  342. var opDrinkSnackFruitCost = item.FirstOrDefault(x => x.SId == 991);
  343. if (opDrinkSnackFruitCost != null)
  344. if (opDrinkSnackFruitCost.Price * _opRate * _opPayPercentage > _teamRate * opCostInfo.Sum(x => x.DrinkSnackFruitFee) * _opPayPercentage) isAutoAudit = false;
  345. //992 住补费用 -- 暂无
  346. //994 翻译费
  347. var opTranslatorCost = item.FirstOrDefault(x => x.SId == 994);
  348. if (opTranslatorCost != null)
  349. if (opTranslatorCost.Price * _opRate * _opPayPercentage > _teamRate * opCostInfo.Sum(x => x.TranslatorFee) * _opPayPercentage) isAutoAudit = false;
  350. //1059 导游超时费用 -- 暂无
  351. //1070 尾款金额 -- 暂无
  352. //1071 其他额外费用 -- 暂无
  353. //1073 翻译超时费 -- 暂无
  354. //1074 早餐超支费用 -- 暂无
  355. //1075 午餐超支费用 -- 暂无
  356. //1076 晚餐超支费用 -- 暂无
  357. //1556 零用金
  358. var opCashCost = item.FirstOrDefault(x => x.SId == 1556);
  359. if (opCashCost != null)
  360. if (opCashCost.Price * _opRate * _opPayPercentage > _teamRate * opCostInfo.Sum(x => x.CashFee) * _opPayPercentage) isAutoAudit = false;
  361. }
  362. //更改审核状态
  363. if (isAutoAudit)
  364. {
  365. var ccpUpdate = _sqlSugar.Updateable<Grp_CreditCardPayment>()
  366. .SetColumns(it => it.IsAuditGM == 3)
  367. .SetColumns(it => it.AuditGMOperate == 4)
  368. .SetColumns(it => it.AuditGMDate == DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
  369. .Where(s => s.DIId == diId && s.CTable == 79 && s.CId == dataId)
  370. .ExecuteCommand();
  371. if (ccpUpdate > 0)
  372. {
  373. _view.Code = 200;
  374. _view.Msg = "自动审核执行成功!";
  375. return _view;
  376. }
  377. }
  378. else
  379. {
  380. //撤销该条数据的自动审核 --> 该条数据的审核状态是自动审核 3 --> 0
  381. if (QuashAudit(79, diId, dataId))
  382. {
  383. _view.Code = 200;
  384. _view.Msg = "费用超团组成本,自动审核撤销成功!";
  385. return _view;
  386. }
  387. }
  388. }
  389. else if (feeType == 3)
  390. {
  391. #region 出行物资的功能及相关费用自动审核
  392. var isAutoAudit = false;
  393. var currModule = 98; //其他款项
  394. int groupSize = groupDetails.VisitPNumber;
  395. decimal otherSubTotal = costContents.Sum(x => x.TeFee);
  396. decimal groupCostCNYTotal = costContents.Sum(x => x.TeFee) * _teamRate * groupSize; //团组成本出行物资总金额
  397. if (groupCostCNYTotal <= 0.00M)
  398. {
  399. _view.Msg = $"团组成本出行物资费用数据未填写";
  400. QuashAudit(currModule, diId, dataId);
  401. return _view;
  402. }
  403. var teNames = setData.Where(x => x.STid == 91).Select(x => x.Name).ToList();
  404. var otherFeeDatas = _sqlSugar.Queryable<Grp_DecreasePayments>()
  405. .InnerJoin<Grp_CreditCardPayment>((dp, ccp) => dp.Id == ccp.CId && ccp.CTable == 98 && ccp.IsDel == 0)
  406. .Where((dp, ccp) => dp.IsDel == 0 && ccp.IsDel == 0 && dp.DiId == diId)
  407. .ToList();
  408. var ids = new List<int>();
  409. foreach (var item in otherFeeDatas)
  410. {
  411. if (item.PriceName.Contains('、'))
  412. {
  413. var priceNames = item.PriceName.Split('、');
  414. foreach (var priceName in priceNames)
  415. {
  416. if (teNames.Contains(priceName))
  417. {
  418. ids.Add(item.Id);
  419. continue;
  420. }
  421. }
  422. }
  423. else if (teNames.Contains(item.PriceName))
  424. {
  425. ids.Add(item.Id);
  426. }
  427. }
  428. ids = ids.Distinct().ToList();
  429. // 验证该费用是否是出行物资费用 是:审核 否:不审核
  430. if (!ids.Contains(dataId))
  431. {
  432. _view.Msg = $"该费用不属于出行物资,不执行自动审核!";
  433. QuashAudit(currModule, diId, dataId);
  434. return _view;
  435. }
  436. decimal otherFeeCNYTotal = otherFeeDatas.Where(x => ids.Contains(x.Id)).Sum(x => x.FeeTotal); //其他费用出行物资总金额
  437. if (otherFeeCNYTotal <= 0.00M)
  438. {
  439. _view.Msg = $"其他款项出行物资费用数据未填写";
  440. QuashAudit(currModule, diId, dataId);
  441. return _view;
  442. }
  443. if (otherFeeCNYTotal > groupCostCNYTotal)
  444. {
  445. _view.Msg = $"其他款项出行物资费用超出团组成本物资费用";
  446. QuashAudit(currModule, diId, dataId);
  447. return _view;
  448. }
  449. isAutoAudit = true;
  450. //2.判断是否自动审核
  451. if (isAutoAudit)
  452. {
  453. var ccpUpdate = _sqlSugar.Updateable<Grp_CreditCardPayment>()
  454. .SetColumns(it => it.IsAuditGM == 3)
  455. .SetColumns(it => it.AuditGMOperate == 4)
  456. .SetColumns(it => it.AuditGMDate == DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
  457. .Where(s => s.DIId == diId && s.CTable == currModule && s.CId == dataId)
  458. .ExecuteCommand();
  459. if (ccpUpdate > 0)
  460. {
  461. // 审核尾款
  462. ChangeFinalPaymentAuditStatus(diId,dataId,FinalPaymentAction.Approve);
  463. _view.Code = 200;
  464. _view.Msg = "自动审核执行成功";
  465. return _view;
  466. }
  467. }
  468. else
  469. {
  470. //撤销该条数据的自动审核 --> 该条数据的审核状态是自动审核 3 --> 0
  471. var quashStatus = QuashAudit(currModule, diId, dataId);
  472. if (quashStatus)
  473. {
  474. _view.Code = 200;
  475. _view.Msg = "费用超团组成本,自动审核撤销成功!";
  476. return _view;
  477. }
  478. }
  479. #endregion
  480. }
  481. else if (feeType == 4)
  482. {
  483. #region 保险费用录入自动审核
  484. var currModule = 82;
  485. var insuranceCountryCostData = _sqlSugar.Queryable<Res_BasicInsuranceCost>().Where(x => x.IsDel == 0).ToList();
  486. if (!insuranceCountryCostData.Any())
  487. {
  488. _view.Msg = $"保险国家费用信息为空,不可自动审核!";
  489. return _view;
  490. }
  491. var insuranceType = _sqlSugar.Queryable<Grp_InsuranceCost>().Where(x => x.IsDel == 0 && x.Id != 2).Select(x => x.Id).ToList();
  492. var insuranceCostData1 = _sqlSugar.Queryable<Grp_Customers>()
  493. .LeftJoin<Grp_CreditCardPayment>((c, ccp) => c.Id == ccp.CId && ccp.CTable == 82)
  494. .Where((c, ccp) => c.IsDel == 0 && c.DiId == diId)
  495. .Select((c, ccp) => new
  496. {
  497. c.Id,
  498. c.Iid,
  499. ccpId = ccp.Id,
  500. CNYPrice = ccp.PayMoney * ccp.DayRate
  501. })
  502. .ToList();
  503. if (!insuranceCostData1.Any())
  504. {
  505. _view.Msg = $"暂无保险数据,不可自动审核!";
  506. return _view;
  507. }
  508. if (insuranceCostData1.Where(x => x.Iid == 2).ToList().Any())
  509. {
  510. _view.Msg = $"保险数据为类型为“新数据请不要选此项”,不可自动审核!";
  511. return _view;
  512. }
  513. #region 处理已审核通过的 “新数据请不要选此项” 数据
  514. var insuranceCostData2 = insuranceCostData1.Where(x => x.Iid == 2).ToList();
  515. if (insuranceCostData2.Any())
  516. {
  517. //_view.Msg = $"保险数据为类型为“新数据请不要选此项”,不可自动审核!";
  518. //return _view;
  519. var ccpIds = insuranceCostData2.Select(x => x.ccpId).ToList();
  520. var auditCcpIds = _sqlSugar.Queryable<Grp_CreditCardPayment>().Where(x => x.IsDel == 0 && ccpIds.Contains(x.Id) && x.IsAuditGM == 3).Select(x => x.Id).ToList();
  521. if (auditCcpIds.Any())
  522. {
  523. var ccpUpd = _sqlSugar.Updateable<Grp_CreditCardPayment>()
  524. .SetColumns(it => it.IsAuditGM == 0)
  525. .SetColumns(it => it.AuditGMOperate == 0)
  526. .SetColumns(it => it.AuditGMDate == string.Empty)
  527. .Where(s => auditCcpIds.Contains(s.Id))
  528. .ExecuteCommand();
  529. }
  530. }
  531. #endregion
  532. //var insuranceCostData = insuranceCostData1.Where(x => insuranceType.Contains(x.Iid)).ToList();
  533. var insuranceCostData = insuranceCostData1.ToList();
  534. var currInsuranceInfo = insuranceCostData.Where(x => x.Id == dataId).FirstOrDefault();
  535. if (!insuranceCostData.Any() && currInsuranceInfo == null)
  536. {
  537. _view.Msg = $"暂无保险数据,不可自动审核!";
  538. return _view;
  539. }
  540. if (currInsuranceInfo.CNYPrice == 0.00M)
  541. {
  542. _view.Msg = $"保险数据未录入费用信息,不可自动审核!";
  543. QuashAudit(currModule, diId, dataId);
  544. return _view;
  545. }
  546. var groupPeopleNum = groupDetails.VisitPNumber;
  547. var visitCountrys = groupDetails.VisitCountry.Split("|").ToList();
  548. if (!visitCountrys.Any())
  549. {
  550. _view.Msg = $"出访国家为空,不可自动审核!";
  551. return _view;
  552. }
  553. var basicCountrys = insuranceCountryCostData.Select(x => x.CountryName).ToList();
  554. var schengenCountry = visitCountrys.Intersect(basicCountrys); //申根国
  555. var unSchengenCountry = visitCountrys.Except(basicCountrys); //非申根国
  556. var schengenCost = insuranceCountryCostData.Where(x => schengenCountry.Contains(x.CountryName)).Sum(x => x.Cost); // 申根国费用
  557. var unSchengenCost = unSchengenCountry.Count() * 35; // 非申根国费用
  558. var groupBudgetCost = (schengenCost + unSchengenCost) * groupPeopleNum;
  559. var groupActialCost = insuranceCostData.Sum(x => x.CNYPrice);
  560. if (groupActialCost > groupBudgetCost)
  561. {
  562. _view.Msg = $"保险费用超出团组成本费用";
  563. QuashAudit(currModule, diId, dataId);
  564. return _view;
  565. }
  566. //自动审核
  567. var ccpUpdate = _sqlSugar.Updateable<Grp_CreditCardPayment>()
  568. .SetColumns(it => it.IsAuditGM == 3)
  569. .SetColumns(it => it.AuditGMOperate == 4)
  570. .SetColumns(it => it.AuditGMDate == DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
  571. .Where(s => s.DIId == diId && s.CTable == currModule && s.CId == dataId)
  572. .ExecuteCommand();
  573. if (ccpUpdate > 0)
  574. {
  575. _view.Code = 200;
  576. _view.Msg = "自动审核执行成功";
  577. }
  578. else _view.Msg = "自动审核执行失败";
  579. return _view;
  580. #endregion
  581. }
  582. else if (feeType == 5)
  583. {
  584. #region 机票费用自动审核只处理舱位相关的费用
  585. var currModule = 85;
  586. var auditFeeTypeIds = new List<int>() {
  587. 457, //头等舱
  588. 458, //公务舱
  589. 459, //超经舱
  590. 460, //经济舱
  591. 1430, //公务舱(实际经济舱)
  592. 1431, //头等舱(实际公务舱)
  593. 1432, //头等舱(实际经济舱)
  594. };
  595. var airInfo = await _sqlSugar.Queryable<Grp_AirTicketReservations>()
  596. .InnerJoin<Grp_CreditCardPayment>((x, y) => x.Id == y.CId && y.CTable == currModule && y.IsDel == 0)
  597. .Where((x, y) => x.Id == dataId && x.IsDel == 0 && auditFeeTypeIds.Contains(x.CType))
  598. .Select((x, y) => new
  599. {
  600. x.Id,
  601. CcpId = y.Id,
  602. x.Price,
  603. x.CType,
  604. x.ClientNum,
  605. x.DIId
  606. })
  607. .FirstAsync();
  608. if (airInfo == null)
  609. {
  610. _view.Msg = $"机票信息为空或费用类型不在自动审核范围内,不可自动审核!";
  611. return _view;
  612. }
  613. //团组成本 经济舱、头等舱、公务舱
  614. int ecoPaxCount = groupInfo.JJCRS, //经济舱人数
  615. firstClassCnt = groupInfo.TDCRS, //头等舱人数
  616. bizClassCnt = groupInfo.GWCRS; //公务舱人数
  617. decimal ecoCost = groupInfo.JJCCB, //经济舱成本费用
  618. firstCost = groupInfo.TDCCB, //头等舱成本费用
  619. bizCost = groupInfo.GWCCB; //公务舱成本费用
  620. decimal ecoTotalCost = ecoPaxCount * ecoCost, //经济舱成本费用合计
  621. firstTotalCost = firstClassCnt * firstCost, //头等舱成本费用合计
  622. bizTotalCost = bizClassCnt * bizCost; //公务舱成本费用合计
  623. int currAirType = airInfo.CType; //当前舱位类型
  624. int currAirTypeCnt = 0; //当前舱位人数
  625. //decimal currAirFee = 0.00M; //当前舱位录入费用
  626. decimal currAirCost = 0.00M; //当前舱位成本费用
  627. switch (airInfo.CType)
  628. {
  629. //头等舱
  630. case 457:
  631. currAirTypeCnt = groupInfo.TDCRS; //头等舱人数
  632. currAirCost = currAirTypeCnt * groupInfo.TDCCB; //头等舱成本费用合计
  633. break;
  634. //公务舱
  635. case 458:
  636. currAirTypeCnt = groupInfo.GWCRS; //公务舱人数
  637. currAirCost = currAirTypeCnt * groupInfo.GWCCB; //公务舱成本费用合计
  638. break;
  639. case 459: //超经舱
  640. currAirTypeCnt = groupInfo.JJCRS; //经济舱人数
  641. currAirCost = currAirTypeCnt * groupInfo.JJCCB; //经济舱成本费用合计
  642. break;
  643. case 460: //经济舱
  644. currAirTypeCnt = groupInfo.JJCRS; //经济舱人数
  645. currAirCost = currAirTypeCnt * groupInfo.JJCCB; //经济舱成本费用合计
  646. break;
  647. case 1430: //公务舱(实际经济舱)
  648. currAirTypeCnt = groupInfo.GWCRS; //公务舱人数
  649. currAirCost = currAirTypeCnt * groupInfo.GWCCB; //公务舱成本费用合计
  650. break;
  651. case 1431: //头等舱(实际公务舱)
  652. currAirTypeCnt = groupInfo.TDCRS; //头等舱人数
  653. currAirCost = currAirTypeCnt * groupInfo.TDCCB; //头等舱成本费用合计
  654. break;
  655. case 1432: //头等舱(实际经济舱)
  656. currAirTypeCnt = groupInfo.TDCRS; //头等舱人数
  657. currAirCost = currAirTypeCnt * groupInfo.TDCCB; //头等舱成本费用合计
  658. break;
  659. default:
  660. _view.Msg = $"机票费用类型不在自动审核范围内,不可自动审核!";
  661. return _view;
  662. }
  663. var currAirTypeDatas = await _sqlSugar.Queryable<Grp_AirTicketReservations>()
  664. .Where(x => x.IsDel == 0 && x.DIId == diId && x.CType == airInfo.CType && x.ClientNum == currAirTypeCnt)
  665. .ToListAsync();
  666. var airTypeText = airInfo.CType switch
  667. {
  668. 457 => "头等舱",
  669. 458 => "公务舱",
  670. 459 => "超经舱",
  671. 460 => "经济舱",
  672. 1430 => "公务舱(实际经济舱)",
  673. 1431 => "头等舱(实际公务舱)",
  674. 1432 => "头等舱(实际经济舱)",
  675. _ => "未知舱位"
  676. };
  677. if (!currAirTypeDatas.Any())
  678. {
  679. _view.Msg = $"{airTypeText}机票费用数据未填写或者舱位人数不匹配,不可自动审核!";
  680. return _view;
  681. }
  682. //验证费用类型
  683. if (!currAirTypeDatas.Any(x => x.Id == airInfo.Id))
  684. {
  685. _view.Msg = $"机票费用({airTypeText})超出团组成本费用";
  686. return _view;
  687. }
  688. //验证是否超出舱位成本费用
  689. decimal currAirTypeFee = currAirTypeDatas.Sum(x => x.Price);
  690. if (currAirTypeFee > currAirCost)
  691. {
  692. _view.Msg = $"机票费用({airTypeText})超出团组成本费用";
  693. QuashAudit(currModule, diId, currAirTypeDatas.Select(x => x.Id).ToArray());
  694. return _view;
  695. }
  696. //执行自动审核及相关字段更改
  697. var upd = _sqlSugar.Updateable<Grp_CreditCardPayment>()
  698. .SetColumns(x => x.IsAuditGM == 3)
  699. .SetColumns(x => x.AuditGMOperate == 4)
  700. .SetColumns(x => x.AuditGMDate == DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
  701. .Where(x => x.Id == airInfo.CcpId)
  702. .ExecuteCommand();
  703. if (upd > 0)
  704. {
  705. _view.Code = StatusCodes.Status200OK;
  706. _view.Msg = "自动审核执行成功";
  707. return _view;
  708. }
  709. _view.Msg = "自动审核执行失败";
  710. return _view;
  711. #endregion
  712. }
  713. else _view.Msg = $"请传入有效的feeType参数";
  714. return _view;
  715. }
  716. /// <summary>
  717. /// 变更尾款审核状态(自动通过或撤销)
  718. /// </summary>
  719. /// <param name="declarationId">GroupID</param>
  720. /// <param name="expenseId">费用记录ID</param>
  721. /// <param name="action">审核动作:Approve=自动通过, Revoke=撤销审核</param>
  722. /// <param name="moduleId">模块编号,默认98</param>
  723. public void ChangeFinalPaymentAuditStatus(int groupId, int expenseId, FinalPaymentAction action, int moduleId = 98)
  724. {
  725. var finalRecord = GetFinalPaymentRecord(groupId, expenseId);
  726. if (finalRecord == null) return;
  727. var (isAuditGM, auditGMOperate, auditGMDate) = action switch
  728. {
  729. FinalPaymentAction.Approve => (3, 4, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")),
  730. FinalPaymentAction.Revoke => (0, 0, string.Empty),
  731. _ => throw new ArgumentOutOfRangeException(nameof(action))
  732. };
  733. _sqlSugar.Updateable<Grp_CreditCardPayment>()
  734. .SetColumns(it => it.IsAuditGM == isAuditGM)
  735. .SetColumns(it => it.AuditGMOperate == auditGMOperate)
  736. .SetColumns(it => it.AuditGMDate == auditGMDate)
  737. .Where(s => s.DIId == groupId && s.CTable == moduleId && s.CId == finalRecord.Id)
  738. .ExecuteCommand();
  739. }
  740. /// <summary>
  741. /// 根据费用ID查找关联的尾款记录
  742. /// </summary>
  743. /// <param name="groupId"></param>
  744. /// <param name="expenseId"></param>
  745. /// <returns></returns>
  746. private Grp_DecreasePayments GetFinalPaymentRecord(int groupId, int expenseId)
  747. {
  748. var currentExpense = _sqlSugar.Queryable<Grp_DecreasePayments>()
  749. .Where(x => x.IsDel == 0 && x.DiId == groupId && x.Id == expenseId)
  750. .First();
  751. if (currentExpense == null) return null;
  752. return _sqlSugar.Queryable<Grp_DecreasePayments>()
  753. .Where(x => x.IsDel == 0
  754. && x.DiId == groupId
  755. && x.PriceName == currentExpense.PriceName
  756. && x.PaymentType.Contains("尾款"))
  757. .First();
  758. }
  759. public enum FinalPaymentAction
  760. {
  761. /// <summary>
  762. /// 自动通过审核
  763. /// </summary>
  764. Approve = 1,
  765. /// <summary>
  766. /// 撤销审核
  767. /// </summary>
  768. Revoke = 2
  769. }
  770. /// <summary>
  771. /// hotel、op 撤销自动审核的数据
  772. /// </summary>
  773. /// <param name="type">
  774. /// 酒店 76
  775. /// op 79
  776. /// </param>
  777. /// <param name="diId"></param>
  778. /// <param name="dataId"></param>
  779. /// <returns></returns>
  780. private bool QuashAudit(int type, int diId, int dataId)
  781. {
  782. // 撤销该条数据的自动审核 --> 该条数据的审核状态是自动审核 3 --> 0
  783. var ccpInfo = _sqlSugar.Queryable<Grp_CreditCardPayment>()
  784. .Where(s => s.DIId == diId && s.CTable == type && s.CId == dataId && s.IsAuditGM == 3)
  785. .First();
  786. if (ccpInfo != null)
  787. {
  788. var ccpUpdate = _sqlSugar.Updateable<Grp_CreditCardPayment>()
  789. .SetColumns(it => it.IsAuditGM == 0)
  790. .SetColumns(it => it.AuditGMOperate == 0)
  791. .SetColumns(it => it.AuditGMDate == string.Empty)
  792. .Where(s => s.Id == ccpInfo.Id)
  793. .ExecuteCommand();
  794. if (ccpUpdate > 0)
  795. {
  796. // 其他款项是撤销尾款审核
  797. if (type == 98)
  798. {
  799. // 撤销审核尾款
  800. ChangeFinalPaymentAuditStatus(diId, dataId, FinalPaymentAction.Revoke);
  801. }
  802. return true;
  803. }
  804. }
  805. return false;
  806. }
  807. /// <summary>
  808. /// air 撤销自动审核的数据
  809. /// </summary>
  810. /// <param name="type">
  811. /// 机票
  812. /// </param>
  813. /// <param name="diId"></param>
  814. /// <param name="dataId"></param>
  815. /// <returns></returns>
  816. private bool QuashAudit(int type, int diId, int[] dataId)
  817. {
  818. //撤销该条数据的自动审核 --> 该条数据的审核状态是自动审核 3 --> 0
  819. var ccpInfos = _sqlSugar.Queryable<Grp_CreditCardPayment>()
  820. .Where(s => s.DIId == diId && s.CTable == type && dataId.Contains(s.CId) && s.IsAuditGM == 3)
  821. .ToList();
  822. if (ccpInfos.Any())
  823. {
  824. ccpInfos.ForEach(x =>
  825. {
  826. x.IsAuditGM = 0;
  827. x.AuditGMOperate = 0;
  828. x.AuditGMDate = string.Empty;
  829. });
  830. var ccpUpd = _sqlSugar.Updateable(ccpInfos)
  831. .UpdateColumns(x => new { x.IsAuditGM, x.AuditGMOperate, x.AuditGMDate })
  832. .ExecuteCommand();
  833. if (ccpUpd > 0)
  834. {
  835. return true;
  836. }
  837. }
  838. return false;
  839. }
  840. /// <summary>
  841. /// 费用自动审核
  842. /// </summary>
  843. /// <param name="feeType">
  844. /// 1.酒店 76
  845. /// 2.op 79
  846. /// </param>
  847. /// <param name="diId">团组Id</param>
  848. /// <param name="dataId">数据Id(模块类型主表Id)</param>
  849. /// <returns></returns>
  850. public string IsOverBudget(int feeType, int diId, int dataId)
  851. {
  852. string _view = "-";
  853. if (diId < 1) { return _view; }
  854. if (dataId < 1) { return _view; }
  855. List<int> stids = new() { 17, 66 };
  856. var setData = _sqlSugar.Queryable<Sys_SetData>().Where(x => x.IsDel == 0 && stids.Contains(x.STid)).ToList();
  857. string _teamCurrency = string.Empty;
  858. var groupInfo = _sqlSugar.Queryable<Grp_GroupCostParameter>().Where(x => x.IsDel == 0 && x.DiId == diId).First();
  859. if (groupInfo == null) { return _view; }
  860. _teamCurrency = groupInfo.Currency;
  861. //币种验证 统一为currencycode三字码
  862. if (int.TryParse(_teamCurrency, out int currency)) _teamCurrency = setData.Find(x => x.Id == currency)?.Name ?? "";
  863. string costContentSql = $"Select * From Grp_GroupCost";
  864. var costContents = _sqlSugar.SqlQueryable<GroupCostAuditView>(costContentSql).Where(x => x.IsDel == 0 && x.Diid == diId).ToList();
  865. if (costContents.Count < 1) { return _view; }
  866. //处理 成本详细信息 日期为空
  867. for (int i = 0; i < costContents.Count; i++)
  868. {
  869. if (string.IsNullOrEmpty(costContents[i].Date))
  870. {
  871. int index = i - 1;
  872. if (index >= 0)
  873. {
  874. costContents[i].Date = costContents[index].Date;
  875. }
  876. }
  877. }
  878. if (feeType == 1)
  879. {
  880. var hotelCostInfo = _sqlSugar.Queryable<Grp_HotelReservations>().Where(x => x.IsDel == 0 && x.DiId == diId && x.Id == dataId).First();
  881. var hotelCostDetails = _sqlSugar.Queryable<Grp_HotelReservationsContent>().Where(x => x.IsDel == 0 && x.DiId == diId && x.HrId == dataId).ToList();
  882. if (hotelCostInfo == null) return _view;
  883. //获取C表汇率
  884. decimal _rate = 1.0000M;
  885. var roomFeeInfo = hotelCostDetails.Where(x => x.PriceType == 1).First();
  886. if (roomFeeInfo == null) return _view;
  887. _rate = roomFeeInfo.Rate == 0.0000M ? 1.0000M : roomFeeInfo.Rate;
  888. bool isAutoAudit = true; //是否自动审核
  889. DateTime checkIn = Convert.ToDateTime(hotelCostInfo.CheckInDate),
  890. checkOut = Convert.ToDateTime(hotelCostInfo.CheckOutDate);
  891. if (checkOut > checkIn) checkOut = checkOut.AddDays(-1); //房费计算,结束日期为前一天
  892. var hotelCostInfos = costContents.Where(x => Convert.ToDateTime(x.Date) >= checkIn && Convert.ToDateTime(x.Date) <= checkOut).ToList();
  893. if (hotelCostInfos.Count < 1) isAutoAudit = false;
  894. decimal otherFee = hotelCostDetails.Where(x => x.PriceType != 1).Sum(x => x.Price * (x.Rate == 0.0000M ? 1.0000M : x.Rate));
  895. if (otherFee > 0) { otherFee /= (checkOut - checkIn).Days; }
  896. var hotelCostInfosGroup = hotelCostInfos.GroupBy(x => x.Date);
  897. foreach (var item in hotelCostInfosGroup)
  898. {
  899. decimal hotelSingleRoomFee = item.Sum(x => x.HotelSingleRoomFee);
  900. decimal hotelDoubleRoomFee = item.Sum(x => x.HotelDoubleRoomFee);
  901. decimal hotelSuiteFee = item.Sum(x => x.HotelSuiteFee);
  902. decimal hotelSuiteRoomFee = item.Sum(x => x.HotelSuiteRoomFee);
  903. //1.判断费用是否 <= 成本费用
  904. //1.1 判断单间费用
  905. decimal singleRoomPrice = (hotelCostInfo.SingleRoomPrice + otherFee) * _rate;
  906. if (singleRoomPrice > 0) if (singleRoomPrice > hotelSingleRoomFee * _rate) isAutoAudit = false;
  907. //1.2 判断双人间费用
  908. decimal doubleRoomPrice = (hotelCostInfo.DoubleRoomPrice + otherFee) * _rate;
  909. if (doubleRoomPrice > 0) if (doubleRoomPrice > hotelDoubleRoomFee * _rate) isAutoAudit = false;
  910. //1.3 判断套房费用
  911. decimal suiteRoomPrice = (hotelCostInfo.SuiteRoomPrice + otherFee) * _rate;
  912. if (suiteRoomPrice > 0) if (suiteRoomPrice > hotelSuiteFee * _rate) isAutoAudit = false;
  913. //1.4 判断其他房型费用
  914. decimal otherRoomPrice = (hotelCostInfo.OtherRoomPrice + otherFee) * _rate;
  915. if (otherRoomPrice > 0) if (otherRoomPrice > hotelSuiteRoomFee * _rate) isAutoAudit = false;
  916. }
  917. //2.判断是否自动审核
  918. if (isAutoAudit)
  919. {
  920. return $"未超预算";
  921. }
  922. }
  923. else if (feeType == 2)
  924. {
  925. //1.基础数据参数验证
  926. var priceType = new List<int>() { 1062 };
  927. var opinfos = _sqlSugar.Queryable<Grp_CarTouristGuideGroundReservations>()
  928. .Where(x => x.IsDel == 0 && x.DiId == diId && x.Id == dataId && !priceType.Contains(x.PriceType))
  929. .First();
  930. //var opinfos = _sqlSugar.Queryable<Grp_CarTouristGuideGroundReservations>()
  931. // .Where(x => x.IsDel == 0 && x.DiId == diId && x.Id == dataId )
  932. // .First();
  933. if (opinfos == null) return _view;
  934. //1.含超时费用/超支费用 手动审核
  935. if (opinfos.SelectCheck.Contains("超时") || opinfos.SelectCheck.Contains("超支") || opinfos.SelectCheck.Contains("尾款")) return _view;
  936. //1.参数验证
  937. var opCheckPriceTyeps = opinfos.SelectCheck.Split(',');
  938. var opCheckPriceTyepIds = setData.Where(x => opinfos.SelectCheck.Split(',').Contains(x.Name)).Select(x => x.Id).ToList();
  939. var opContents = _sqlSugar.Queryable<Grp_CarTouristGuideGroundReservationsContent>()
  940. .Where(x => x.IsDel == 0 && x.DiId == diId && x.CTGGRId == dataId && opCheckPriceTyepIds.Contains(x.SId))
  941. .OrderBy(x => x.DatePrice, OrderByType.Asc)
  942. .ToList();
  943. if (opContents.Count < 1) return _view;
  944. //获取C表汇率
  945. decimal _rate = 1.0000M;
  946. var payInfo = _sqlSugar.Queryable<Grp_CreditCardPayment>().Where(x => x.IsDel == 0 && x.DIId == diId && x.CTable == 79 && x.Id == dataId).First();
  947. if (payInfo == null) return _view;
  948. _rate = payInfo.DayRate;
  949. string opCurrencyName = setData.Find(x => x.Id == opContents[0].Currency)?.Name ?? "";
  950. var opBasicDatas = setData.Where(x => x.STid == 17).ToList(); //费用类型基础数据
  951. bool isAutoAudit = true;
  952. if (!DateTime.TryParse(opinfos.ServiceStartTime, out DateTime startDt1) || !DateTime.TryParse(opinfos.ServiceEndTime, out DateTime endDt1)) return _view;
  953. DateTime startDt = startDt1;
  954. DateTime endDt = endDt1;
  955. var opCostDatas = costContents.Where(it => Convert.ToDateTime(it.Date) >= startDt && Convert.ToDateTime(it.Date) <= endDt).ToList();
  956. if (opCostDatas.Count < 1) return _view;
  957. var noAuditFeeTypeIds = new List<int> {
  958. 982 ,//982 车超时费 -- 暂无
  959. 96 ,//96 接送机费 -- 暂无
  960. 97 ,//97 其他费用 -- 暂无
  961. 992 ,//992 住补费用 -- 暂无
  962. 1059,//1059 导游超时费用 -- 暂无
  963. 1070,//1070 尾款金额 -- 暂无
  964. 1071,//1071 其他额外费用 -- 暂无
  965. 1073,//1073 翻译超时费 -- 暂无
  966. 1074,//1074 早餐超支费用 -- 暂无
  967. 1075,//1075 午餐超支费用 -- 暂无
  968. 1076,//1076 晚餐超支费用 -- 暂无
  969. };
  970. //费用类型筛选 包含 feeTypeIds && 包含这些类型费用大于0
  971. var noAuditFeeContents = opContents.Where(x => x.Price > 0 && noAuditFeeTypeIds.Contains(x.SId)).ToList();
  972. if (noAuditFeeContents.Count > 0) return _view;
  973. //2.按天按项 检查费用是否超过预算
  974. var opDayContent = opContents.GroupBy(x => x.DatePrice);
  975. foreach (var item in opDayContent)
  976. {
  977. var opCostInfo = opCostDatas.Where(x => Convert.ToDateTime(x.Date) == item.Key).ToList();
  978. if (opCostInfo.Count < 1) continue;
  979. //车费 91
  980. var opCarCost = item.FirstOrDefault(x => x.SId == 91);
  981. if (opCarCost != null) if (opCarCost.Price * _rate > _rate * opCostInfo.Sum(x => x.CarFee)) isAutoAudit = false;
  982. //982 车超时费 -- 暂无
  983. //92 导游费
  984. var opGuideCost = item.FirstOrDefault(x => x.SId == 92);
  985. if (opGuideCost != null) if (opGuideCost.Price * _rate > _rate * opCostInfo.Sum(x => x.GuideFee)) isAutoAudit = false;
  986. //94 导游景点费
  987. var opGuideScenicCost = item.FirstOrDefault(x => x.SId == 94);
  988. if (opGuideScenicCost != null) if (opGuideScenicCost.Price * _rate > _rate * opCostInfo.Sum(x => x.GuideScenicFee)) isAutoAudit = false;
  989. //95 导游小费
  990. var opGuideTipCost = item.FirstOrDefault(x => x.SId == 95);
  991. if (opGuideTipCost != null) if (opGuideTipCost.Price * _rate > _rate * opCostInfo.Sum(x => x.GuideTipFee)) isAutoAudit = false;
  992. //983 导游餐补
  993. var opGuideMealCost = item.FirstOrDefault(x => x.SId == 983);
  994. if (opGuideMealCost != null) if (opGuideMealCost.Price * _rate > _rate * opCostInfo.Sum(x => x.GuideMealFee)) isAutoAudit = false;
  995. //984 导游房补
  996. var opGuideRoomCost = item.FirstOrDefault(x => x.SId == 984);
  997. if (opGuideRoomCost != null) if (opGuideRoomCost.Price * _rate > _rate * opCostInfo.Sum(x => x.GuideRoomFee)) isAutoAudit = false;
  998. //985 导游交通
  999. var opGuideTrafficCost = item.FirstOrDefault(x => x.SId == 985);
  1000. if (opGuideTrafficCost != null) if (opGuideTrafficCost.Price * _rate > _rate * opCostInfo.Sum(x => x.GuideTrafficFee)) isAutoAudit = false;
  1001. //96 接送机费 -- 暂无
  1002. //97 其他费用 -- 暂无
  1003. //979 司机工资
  1004. var opDriverCost = item.FirstOrDefault(x => x.SId == 979);
  1005. if (opDriverCost != null) if (opDriverCost.Price * _rate > _rate * opCostInfo.Sum(x => x.DriverFee)) isAutoAudit = false;
  1006. //980 司机小费
  1007. var opDriverTipCost = item.FirstOrDefault(x => x.SId == 980);
  1008. if (opDriverTipCost != null) if (opDriverTipCost.Price * _rate > _rate * opCostInfo.Sum(x => x.DriverTipFee)) isAutoAudit = false;
  1009. //981 司机餐补
  1010. var opDriverMealCost = item.FirstOrDefault(x => x.SId == 981);
  1011. if (opDriverMealCost != null) if (opDriverMealCost.Price * _rate > _rate * opCostInfo.Sum(x => x.DriverMealFee)) isAutoAudit = false;
  1012. //988 客户早餐费用
  1013. var opClientBreakfastCost = item.FirstOrDefault(x => x.SId == 988);
  1014. if (opClientBreakfastCost != null) if (opClientBreakfastCost.Price * _rate > _rate * opCostInfo.Sum(x => x.ClientBreakfastFee)) isAutoAudit = false;
  1015. //93 客户午餐费用
  1016. var opClientLunchCost = item.FirstOrDefault(x => x.SId == 93);
  1017. if (opClientLunchCost != null) if (opClientLunchCost.Price * _rate > _rate * opCostInfo.Sum(x => x.ClientLunchFee)) isAutoAudit = false;
  1018. //989 客户晚餐费用
  1019. var opClientDinnerCost = item.FirstOrDefault(x => x.SId == 989);
  1020. if (opClientDinnerCost != null) if (opClientDinnerCost.Price * _rate > _rate * opCostInfo.Sum(x => x.ClientDinnerFee)) isAutoAudit = false;
  1021. //990 景点门票费
  1022. var opScenicTicketCost = item.FirstOrDefault(x => x.SId == 990);
  1023. if (opScenicTicketCost != null) if (opScenicTicketCost.Price * _rate > _rate * opCostInfo.Sum(x => x.ScenicTicketFee)) isAutoAudit = false;
  1024. //991 饮料/零食/水果
  1025. var opDrinkSnackFruitCost = item.FirstOrDefault(x => x.SId == 991);
  1026. if (opDrinkSnackFruitCost != null) if (opDrinkSnackFruitCost.Price * _rate > _rate * opCostInfo.Sum(x => x.DrinkSnackFruitFee)) isAutoAudit = false;
  1027. //992 住补费用 -- 暂无
  1028. //994 翻译费
  1029. var opTranslatorCost = item.FirstOrDefault(x => x.SId == 994);
  1030. if (opTranslatorCost != null) if (opTranslatorCost.Price * _rate > _rate * opCostInfo.Sum(x => x.TranslatorFee)) isAutoAudit = false;
  1031. //1059 导游超时费用 -- 暂无
  1032. //1070 尾款金额 -- 暂无
  1033. //1071 其他额外费用 -- 暂无
  1034. //1073 翻译超时费 -- 暂无
  1035. //1074 早餐超支费用 -- 暂无
  1036. //1075 午餐超支费用 -- 暂无
  1037. //1076 晚餐超支费用 -- 暂无
  1038. }
  1039. //更改审核状态
  1040. if (isAutoAudit) return _view;
  1041. }
  1042. else return _view;
  1043. return _view;
  1044. }
  1045. }
  1046. }