FeeAuditRepository.cs 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. using EyeSoft.Extensions;
  2. using EyeSoft.Logging;
  3. using NPOI.SS.Formula.Functions;
  4. using OASystem.Domain;
  5. using OASystem.Domain.Entities.Groups;
  6. using OASystem.Domain.Entities.Resource;
  7. using OASystem.Domain.ViewModels.Groups;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. namespace OASystem.Infrastructure.Repositories.Groups
  14. {
  15. /// <summary>
  16. /// 费用审核仓储
  17. /// </summary>
  18. public class FeeAuditRepository:BaseRepository<EntityBase,ViewBase>
  19. {
  20. public FeeAuditRepository(SqlSugarClient sqlSugar) : base(sqlSugar)
  21. {
  22. }
  23. /// <summary>
  24. /// 费用自动审核
  25. /// </summary>
  26. /// <param name="feeType">
  27. /// 1.酒店 76
  28. /// 2.op 79
  29. /// 3.其他费用-出行物资 98
  30. /// 4.保险 82
  31. /// </param>
  32. /// <param name="diId">团组Id</param>
  33. /// <param name="dataId">数据Id(模块类型主表Id)</param>
  34. /// <returns></returns>
  35. public async Task<JsonView> FeeAutomaticAudit(int feeType, int diId, int dataId)
  36. {
  37. var _view = new JsonView() { Code = 201, Msg = "自动审核操作失败" };
  38. if (diId < 1) { _view.Msg = MsgTips.DiId; return _view; }
  39. if (dataId < 1) { _view.Msg = MsgTips.Id; return _view; }
  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. string _teamCurrency = string.Empty;
  43. decimal _teamRate = 0.00M;
  44. var costContents = new List<GroupCostAuditView>();
  45. if (feeType != 4)
  46. {
  47. var groupInfo = _sqlSugar.Queryable<Grp_GroupCostParameter>().Where(x => x.IsDel == 0 && x.DiId == diId).First();
  48. if (groupInfo == null) { _view.Msg = $"团组成本信息未填写!"; return _view; }
  49. _teamCurrency = groupInfo.Currency;
  50. _teamRate = groupInfo.Rate;
  51. //币种验证 统一为currencycode三字码
  52. if (int.TryParse(_teamCurrency, out int currency)) _teamCurrency = setData.Find(x => x.Id == currency)?.Name ?? "";
  53. string costContentSql = $"Select * From Grp_GroupCost";
  54. costContents = _sqlSugar.SqlQueryable<GroupCostAuditView>(costContentSql).Where(x => x.IsDel == 0 && x.Diid == diId).ToList();
  55. if (costContents.Count < 1) { _view.Msg = $"团组成本信息未填写!"; return _view; }
  56. //处理 成本详细信息 日期为空
  57. for (int i = 0; i < costContents.Count; i++)
  58. {
  59. if (string.IsNullOrEmpty(costContents[i].Date))
  60. {
  61. int index = i - 1;
  62. if (index >= 0)
  63. {
  64. costContents[i].Date = costContents[index].Date;
  65. var dtBool = DateTime.TryParse(costContents[i].Date, out DateTime _dateTime);
  66. if (dtBool)
  67. {
  68. costContents[i].CurrTime = _dateTime;
  69. }
  70. }
  71. }
  72. else
  73. {
  74. var dtBool = DateTime.TryParse(costContents[i].Date, out DateTime _dateTime);
  75. if (dtBool)
  76. {
  77. costContents[i].CurrTime = _dateTime;
  78. }
  79. }
  80. }
  81. }
  82. if (feeType == 1)
  83. {
  84. //1089 对冲账或其他 不在审核范围
  85. var hotelCostInfo = _sqlSugar.Queryable<Grp_HotelReservations>().Where(x => x.IsDel == 0 && x.DiId == diId && x.Id == dataId && x.CheckType != 1089).First();
  86. var hotelCostDetails = _sqlSugar.Queryable<Grp_HotelReservationsContent>().Where(x => x.IsDel == 0 && x.DiId == diId && x.HrId == dataId).ToList();
  87. if (hotelCostInfo == null)
  88. {
  89. _view.Msg = $"酒店费用数据未填写";
  90. QuashAudit(76, diId, dataId);
  91. return _view;
  92. }
  93. //酒店费用金额 == 0 不自动审核
  94. if (hotelCostInfo.CardPrice == 0.0000M)
  95. {
  96. _view.Msg = $"酒店费用金额 == 0 不自动审核";
  97. QuashAudit(76, diId, dataId);
  98. return _view;
  99. }
  100. //获取C表汇率
  101. decimal _rate = 1.0000M;
  102. var roomFeeInfo = hotelCostDetails.Where(x => x.PriceType == 1).First();
  103. if (roomFeeInfo == null)
  104. {
  105. _view.Msg = $"酒店房间费用付款数据未填写";
  106. return _view;
  107. }
  108. _rate = roomFeeInfo.Rate == 0.0000M ? 1.0000M : roomFeeInfo.Rate;
  109. bool isAutoAudit = true; //是否自动审核
  110. DateTime checkIn = Convert.ToDateTime(hotelCostInfo.CheckInDate),
  111. checkOut = Convert.ToDateTime(hotelCostInfo.CheckOutDate);
  112. if (checkOut > checkIn) checkOut = checkOut.AddDays(-1); //房费计算,结束日期为前一天
  113. var hotelCostInfos = costContents.Where(x => x.CurrTime >= checkIn && x.CurrTime <= checkOut).ToList();
  114. if (hotelCostInfos.Count < 1) isAutoAudit = false;
  115. decimal otherFee = hotelCostDetails.Where(x => x.PriceType != 1).Sum(x => x.Price * (x.Rate == 0.0000M ? 1.0000M : x.Rate));
  116. if (otherFee > 0) { otherFee /= (checkOut - checkIn).Days; }
  117. var hotelCostInfosGroup = hotelCostInfos.GroupBy(x => x.Date);
  118. foreach (var item in hotelCostInfosGroup)
  119. {
  120. var hotelSingleRoomFee = item.Sum(x => x.HotelSingleRoomFee) * _teamRate; //成本单间费用
  121. var hotelDoubleRoomFee = item.Sum(x => x.HotelDoubleRoomFee) * _teamRate; //成本双人间费用
  122. var hotelSuiteFee = item.Sum(x => x.HotelSuiteFee) * _teamRate; //成本套房费用
  123. var hotelSuiteRoomFee = item.Sum(x => x.HotelSuiteRoomFee) * _teamRate; //成本其他房型间费用
  124. //1.判断费用是否 <= 成本费用
  125. //1.1 判断单间费用
  126. decimal singleRoomPrice = (hotelCostInfo.SingleRoomPrice + otherFee) * _rate; //酒店录入费用
  127. if (singleRoomPrice > 0) if (singleRoomPrice > hotelSingleRoomFee) isAutoAudit = false;
  128. //1.2 判断双人间费用
  129. decimal doubleRoomPrice = (hotelCostInfo.DoubleRoomPrice + otherFee) * _rate;//酒店录入费用
  130. if (doubleRoomPrice > 0) if (doubleRoomPrice > hotelDoubleRoomFee) isAutoAudit = false;
  131. //1.3 判断套房费用
  132. decimal suiteRoomPrice = (hotelCostInfo.SuiteRoomPrice + otherFee) * _rate;//酒店录入费用
  133. if (suiteRoomPrice > 0) if (suiteRoomPrice > hotelSuiteFee) isAutoAudit = false;
  134. //1.4 判断其他房型费用
  135. decimal otherRoomPrice = (hotelCostInfo.OtherRoomPrice + otherFee) * _rate;//酒店录入费用
  136. if (otherRoomPrice > 0) if (otherRoomPrice > hotelSuiteRoomFee) isAutoAudit = false;
  137. }
  138. //2.判断是否自动审核
  139. if (isAutoAudit)
  140. {
  141. var ccpUpdate = _sqlSugar.Updateable<Grp_CreditCardPayment>()
  142. .SetColumns(it => it.IsAuditGM == 3)
  143. .SetColumns(it => it.AuditGMOperate == 4)
  144. .SetColumns(it => it.AuditGMDate == DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
  145. .Where(s => s.DIId == diId && s.CTable == 76 && s.CId == dataId)
  146. .ExecuteCommand();
  147. if (ccpUpdate > 0)
  148. {
  149. _view.Code = 200;
  150. _view.Msg = "自动审核执行成功";
  151. return _view;
  152. }
  153. }
  154. else
  155. {
  156. //撤销该条数据的自动审核 --> 该条数据的审核状态是自动审核 3 --> 0
  157. var quashStatus = QuashAudit(76, diId, dataId);
  158. if (quashStatus)
  159. {
  160. _view.Code = 200;
  161. _view.Msg = "费用超团组成本,自动审核撤销成功!";
  162. return _view;
  163. }
  164. }
  165. }
  166. else if (feeType == 2)
  167. {
  168. //1.基础数据参数验证
  169. var priceType = new List<int>() {
  170. 1062 //1062 尾款
  171. };
  172. var opinfos = _sqlSugar.Queryable<Grp_CarTouristGuideGroundReservations>()
  173. .Where(x => x.IsDel == 0 && x.DiId == diId && x.Id == dataId && !priceType.Contains(x.PriceType))
  174. .First();
  175. //var opinfos = _sqlSugar.Queryable<Grp_CarTouristGuideGroundReservations>()
  176. // .Where(x => x.IsDel == 0 && x.DiId == diId && x.Id == dataId )
  177. // .First();
  178. if (opinfos == null)
  179. {
  180. _view.Msg = $"OP费用费用类型属于“尾款”或者 内容未填写";
  181. return _view;
  182. }
  183. //1.含超时费用/超支费用 手动审核
  184. if (opinfos.SelectCheck.Contains("超时") || opinfos.SelectCheck.Contains("超支") || opinfos.SelectCheck.Contains("尾款"))
  185. {
  186. _view.Msg = @$"OP费用含尾款/超支/超时费用,请手动审核";
  187. return _view;
  188. }
  189. //1.参数验证
  190. var opCheckPriceTyeps = opinfos.SelectCheck.Split(',');
  191. var opCheckPriceTyepIds = setData.Where(x => opinfos.SelectCheck.Split(',').Contains(x.Name)).Select(x => x.Id).ToList();
  192. var opContents = _sqlSugar.Queryable<Grp_CarTouristGuideGroundReservationsContent>()
  193. .Where(x => x.IsDel == 0 && x.DiId == diId && x.CTGGRId == dataId && opCheckPriceTyepIds.Contains(x.SId))
  194. .OrderBy(x => x.DatePrice, OrderByType.Asc)
  195. .ToList();
  196. if (opContents.Count < 1)
  197. {
  198. _view.Msg = $"OP费用费用内容未填写";
  199. return _view;
  200. }
  201. //获取C表汇率
  202. decimal _opRate = 1.0000M;
  203. var payInfo = _sqlSugar.Queryable<Grp_CreditCardPayment>().Where(x => x.IsDel == 0 && x.DIId == diId && x.CTable == 79 && x.CId == dataId).First();
  204. if (payInfo == null)
  205. {
  206. _view.Msg = $"OP费用付款数据未填写";
  207. return _view;
  208. }
  209. _opRate = payInfo.DayRate;
  210. string opCurrencyName = setData.Find(x => x.Id == opContents[0].Currency)?.Name ?? "";
  211. //团组、OP币种 验证是否一致
  212. if (opCurrencyName.Equals(_teamCurrency))
  213. {
  214. _opRate = payInfo.DayRate;
  215. _teamRate = payInfo.DayRate;
  216. }
  217. var opBasicDatas = setData.Where(x => x.STid == 17).ToList(); //费用类型基础数据
  218. bool isAutoAudit = true;
  219. if (!DateTime.TryParse(opinfos.ServiceStartTime, out DateTime startDt1) || !DateTime.TryParse(opinfos.ServiceEndTime, out DateTime endDt1))
  220. {
  221. _view.Msg = $"OP费用服务起止日期格式不正确!";
  222. return _view;
  223. }
  224. DateTime startDt = startDt1;
  225. DateTime endDt = endDt1;
  226. var opCostDatas = costContents.Where(it => Convert.ToDateTime(it.Date) >= startDt && Convert.ToDateTime(it.Date) <= endDt).ToList();
  227. if (opCostDatas.Count < 1)
  228. {
  229. _view.Msg = $"该时间段内团组成本未填写!";
  230. return _view;
  231. }
  232. var noAuditFeeTypeIds = new List<int> {
  233. 982 ,//982 车超时费 -- 暂无
  234. 96 ,//96 接送机费 -- 暂无
  235. 97 ,//97 其他费用 -- 暂无
  236. 992 ,//992 住补费用 -- 暂无
  237. 1059,//1059 导游超时费用 -- 暂无
  238. 1070,//1070 尾款金额 -- 暂无
  239. 1071,//1071 其他额外费用 -- 暂无
  240. 1073,//1073 翻译超时费 -- 暂无
  241. 1074,//1074 早餐超支费用 -- 暂无
  242. 1075,//1075 午餐超支费用 -- 暂无
  243. 1076,//1076 晚餐超支费用 -- 暂无
  244. };
  245. //费用类型筛选 包含 feeTypeIds && 包含这些类型费用大于0
  246. var noAuditFeeContents = opContents.Where(x => x.Price > 0 && noAuditFeeTypeIds.Contains(x.SId)).ToList();
  247. if (noAuditFeeContents.Count > 0)
  248. {
  249. _view.Msg = @$"OP费用含尾款/超支/超时费用,请手动审核";
  250. return _view;
  251. }
  252. //2.按天按项 检查费用是否超过预算
  253. var opDayContent = opContents.GroupBy(x => x.DatePrice);
  254. foreach (var item in opDayContent)
  255. {
  256. var opCostInfo = opCostDatas.Where(x => Convert.ToDateTime(x.Date) == item.Key).ToList();
  257. if (opCostInfo.Count < 1) continue;
  258. //车费 91
  259. var opCarCost = item.FirstOrDefault(x => x.SId == 91);
  260. if (opCarCost != null) if (opCarCost.Price * _opRate > _teamRate * opCostInfo.Sum(x => x.CarFee)) isAutoAudit = false;
  261. //982 车超时费 -- 暂无
  262. //92 导游费
  263. var opGuideCost = item.FirstOrDefault(x => x.SId == 92);
  264. if (opGuideCost != null) if (opGuideCost.Price * _opRate > _teamRate * opCostInfo.Sum(x => x.GuideFee)) isAutoAudit = false;
  265. //94 导游景点费
  266. var opGuideScenicCost = item.FirstOrDefault(x => x.SId == 94);
  267. if (opGuideScenicCost != null) if (opGuideScenicCost.Price * _opRate > _teamRate * opCostInfo.Sum(x => x.GuideScenicFee)) isAutoAudit = false;
  268. //95 导游小费
  269. var opGuideTipCost = item.FirstOrDefault(x => x.SId == 95);
  270. if (opGuideTipCost != null) if (opGuideTipCost.Price * _opRate > _teamRate * opCostInfo.Sum(x => x.GuideTipFee)) isAutoAudit = false;
  271. //983 导游餐补
  272. var opGuideMealCost = item.FirstOrDefault(x => x.SId == 983);
  273. if (opGuideMealCost != null) if (opGuideMealCost.Price * _opRate > _teamRate * opCostInfo.Sum(x => x.GuideMealFee)) isAutoAudit = false;
  274. //984 导游房补
  275. var opGuideRoomCost = item.FirstOrDefault(x => x.SId == 984);
  276. if (opGuideRoomCost != null) if (opGuideRoomCost.Price * _opRate > _teamRate * opCostInfo.Sum(x => x.GuideRoomFee)) isAutoAudit = false;
  277. //985 导游交通
  278. var opGuideTrafficCost = item.FirstOrDefault(x => x.SId == 985);
  279. if (opGuideTrafficCost != null) if (opGuideTrafficCost.Price * _opRate > _teamRate * opCostInfo.Sum(x => x.GuideTrafficFee)) isAutoAudit = false;
  280. //96 接送机费 -- 暂无
  281. //97 其他费用 -- 暂无
  282. //979 司机工资
  283. var opDriverCost = item.FirstOrDefault(x => x.SId == 979);
  284. if (opDriverCost != null) if (opDriverCost.Price * _opRate > _teamRate * opCostInfo.Sum(x => x.DriverFee)) isAutoAudit = false;
  285. //980 司机小费
  286. var opDriverTipCost = item.FirstOrDefault(x => x.SId == 980);
  287. if (opDriverTipCost != null) if (opDriverTipCost.Price * _opRate > _teamRate * opCostInfo.Sum(x => x.DriverTipFee)) isAutoAudit = false;
  288. //981 司机餐补
  289. var opDriverMealCost = item.FirstOrDefault(x => x.SId == 981);
  290. if (opDriverMealCost != null) if (opDriverMealCost.Price * _opRate > _teamRate * opCostInfo.Sum(x => x.DriverMealFee)) isAutoAudit = false;
  291. //988 客户早餐费用
  292. var opClientBreakfastCost = item.FirstOrDefault(x => x.SId == 988);
  293. if (opClientBreakfastCost != null) if (opClientBreakfastCost.Price * _opRate > _teamRate * opCostInfo.Sum(x => x.ClientBreakfastFee)) isAutoAudit = false;
  294. //93 客户午餐费用
  295. var opClientLunchCost = item.FirstOrDefault(x => x.SId == 93);
  296. if (opClientLunchCost != null) if (opClientLunchCost.Price * _opRate > _teamRate * opCostInfo.Sum(x => x.ClientLunchFee)) isAutoAudit = false;
  297. //989 客户晚餐费用
  298. var opClientDinnerCost = item.FirstOrDefault(x => x.SId == 989);
  299. if (opClientDinnerCost != null) if (opClientDinnerCost.Price * _opRate > _teamRate * opCostInfo.Sum(x => x.ClientDinnerFee)) isAutoAudit = false;
  300. //990 景点门票费
  301. var opScenicTicketCost = item.FirstOrDefault(x => x.SId == 990);
  302. if (opScenicTicketCost != null) if (opScenicTicketCost.Price * _opRate > _teamRate * opCostInfo.Sum(x => x.ScenicTicketFee)) isAutoAudit = false;
  303. //991 饮料/零食/水果
  304. var opDrinkSnackFruitCost = item.FirstOrDefault(x => x.SId == 991);
  305. if (opDrinkSnackFruitCost != null) if (opDrinkSnackFruitCost.Price * _opRate > _teamRate * opCostInfo.Sum(x => x.DrinkSnackFruitFee)) isAutoAudit = false;
  306. //992 住补费用 -- 暂无
  307. //994 翻译费
  308. var opTranslatorCost = item.FirstOrDefault(x => x.SId == 994);
  309. if (opTranslatorCost != null) if (opTranslatorCost.Price * _opRate > _teamRate * opCostInfo.Sum(x => x.TranslatorFee)) isAutoAudit = false;
  310. //1059 导游超时费用 -- 暂无
  311. //1070 尾款金额 -- 暂无
  312. //1071 其他额外费用 -- 暂无
  313. //1073 翻译超时费 -- 暂无
  314. //1074 早餐超支费用 -- 暂无
  315. //1075 午餐超支费用 -- 暂无
  316. //1076 晚餐超支费用 -- 暂无
  317. }
  318. //更改审核状态
  319. if (isAutoAudit)
  320. {
  321. var ccpUpdate = _sqlSugar.Updateable<Grp_CreditCardPayment>()
  322. .SetColumns(it => it.IsAuditGM == 3)
  323. .SetColumns(it => it.AuditGMOperate == 4)
  324. .SetColumns(it => it.AuditGMDate == DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
  325. .Where(s => s.DIId == diId && s.CTable == 79 && s.CId == dataId)
  326. .ExecuteCommand();
  327. if (ccpUpdate > 0)
  328. {
  329. _view.Code = 200;
  330. _view.Msg = "自动审核执行成功!";
  331. return _view;
  332. }
  333. }
  334. else
  335. {
  336. //撤销该条数据的自动审核 --> 该条数据的审核状态是自动审核 3 --> 0
  337. if (QuashAudit(79, diId, dataId))
  338. {
  339. _view.Code = 200;
  340. _view.Msg = "费用超团组成本,自动审核撤销成功!";
  341. return _view;
  342. }
  343. }
  344. }
  345. else if (feeType == 3)
  346. {
  347. #region 出行物资的功能及相关费用自动审核
  348. var isAutoAudit = false;
  349. var currModule = 98; //其他款项
  350. int groupSize = 0; // 团组人数
  351. var groupDetails = await _sqlSugar.Queryable<Grp_DelegationInfo>().Where(x => x.IsDel == 0 && x.Id == diId).FirstAsync();
  352. if (groupDetails != null) groupSize = groupDetails.VisitPNumber;
  353. decimal otherSubTotal = costContents.Sum(x => x.TeFee);
  354. decimal groupCostCNYTotal = costContents.Sum(x => x.TeFee) * _teamRate * groupSize; //团组成本出行物资总金额
  355. if (groupCostCNYTotal <= 0.00M)
  356. {
  357. _view.Msg = $"团组成本出行物资费用数据未填写";
  358. QuashAudit(currModule, diId, dataId);
  359. return _view;
  360. }
  361. var teNames = setData.Where(x => x.STid == 91).Select(x => x.Name).ToList();
  362. var otherFeeDatas = _sqlSugar.Queryable<Grp_DecreasePayments>()
  363. .InnerJoin<Grp_CreditCardPayment>((dp, ccp) => dp.Id == ccp.CId && ccp.CTable == 98 && ccp.IsDel == 0)
  364. .Where((dp, ccp) => dp.IsDel == 0 && ccp.IsDel == 0 && dp.DiId == diId)
  365. .ToList();
  366. var ids = new List<int>();
  367. foreach (var item in otherFeeDatas)
  368. {
  369. if (item.PriceName.Contains("、"))
  370. {
  371. var priceNames = item.PriceName.Split('、');
  372. foreach (var priceName in priceNames)
  373. {
  374. if (teNames.Contains(priceName))
  375. {
  376. ids.Add(item.Id);
  377. continue;
  378. }
  379. }
  380. }
  381. else if (teNames.Contains(item.PriceName))
  382. {
  383. ids.Add(item.Id);
  384. }
  385. }
  386. ids = ids.Distinct().ToList();
  387. //验证该费用是不是出行物资费用 是:审核 不是:不审核
  388. if (!ids.Contains(dataId))
  389. {
  390. _view.Msg = $"该费用不属于出行物资,不执行自动审核!";
  391. QuashAudit(currModule, diId, dataId);
  392. return _view;
  393. }
  394. decimal otherFeeCNYTotal = otherFeeDatas.Where(x => ids.Contains(x.Id)).Sum(x => x.FeeTotal); //其他费用出行物资总金额
  395. if (otherFeeCNYTotal <= 0.00M)
  396. {
  397. _view.Msg = $"其他款项出行物资费用数据未填写";
  398. QuashAudit(currModule, diId, dataId);
  399. return _view;
  400. }
  401. if (otherFeeCNYTotal > groupCostCNYTotal)
  402. {
  403. _view.Msg = $"其他款项出行物资费用超出团组成本物资费用";
  404. QuashAudit(currModule, diId, dataId);
  405. return _view;
  406. }
  407. isAutoAudit = true;
  408. //2.判断是否自动审核
  409. if (isAutoAudit)
  410. {
  411. var ccpUpdate = _sqlSugar.Updateable<Grp_CreditCardPayment>()
  412. .SetColumns(it => it.IsAuditGM == 3)
  413. .SetColumns(it => it.AuditGMOperate == 4)
  414. .SetColumns(it => it.AuditGMDate == DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
  415. .Where(s => s.DIId == diId && s.CTable == currModule && s.CId == dataId)
  416. .ExecuteCommand();
  417. if (ccpUpdate > 0)
  418. {
  419. _view.Code = 200;
  420. _view.Msg = "自动审核执行成功";
  421. return _view;
  422. }
  423. }
  424. else
  425. {
  426. //撤销该条数据的自动审核 --> 该条数据的审核状态是自动审核 3 --> 0
  427. var quashStatus = QuashAudit(currModule, diId, dataId);
  428. if (quashStatus)
  429. {
  430. _view.Code = 200;
  431. _view.Msg = "费用超团组成本,自动审核撤销成功!";
  432. return _view;
  433. }
  434. }
  435. #endregion
  436. }
  437. else if (feeType == 4)
  438. {
  439. #region 保险费用录入自动审核
  440. var currModule = 82;
  441. var groupDetails = _sqlSugar.Queryable<Grp_DelegationInfo>().Where(x => x.IsDel == 0 && x.Id == diId).First();
  442. if (groupDetails == null)
  443. {
  444. _view.Msg = $"团组信息为空,不可自动审核!";
  445. return _view;
  446. }
  447. var insuranceCountryCostData = _sqlSugar.Queryable<Res_BasicInsuranceCost>().Where(x => x.IsDel == 0).ToList();
  448. if (!insuranceCountryCostData.Any())
  449. {
  450. _view.Msg = $"保险国家费用信息为空,不可自动审核!";
  451. return _view;
  452. }
  453. var insuranceType = _sqlSugar.Queryable<Grp_InsuranceCost>().Where(x => x.IsDel == 0 && x.Id != 2).Select(x => x.Id).ToList();
  454. var insuranceCostData1 = _sqlSugar.Queryable<Grp_Customers>()
  455. .LeftJoin<Grp_CreditCardPayment>((c, ccp) => c.Id == ccp.CId && ccp.CTable == 82)
  456. .Where((c, ccp) => c.IsDel == 0 && c.DiId == diId)
  457. .Select((c, ccp) => new
  458. {
  459. c.Id,
  460. c.Iid,
  461. ccpId = ccp.Id,
  462. CNYPrice = ccp.PayMoney * ccp.DayRate
  463. })
  464. .ToList();
  465. if (!insuranceCostData1.Any())
  466. {
  467. _view.Msg = $"暂无保险数据,不可自动审核!";
  468. return _view;
  469. }
  470. if (insuranceCostData1.Where(x => x.Iid == 2).ToList().Any())
  471. {
  472. _view.Msg = $"保险数据为类型为“新数据请不要选此项”,不可自动审核!";
  473. return _view;
  474. }
  475. #region 处理已审核通过的 “新数据请不要选此项” 数据
  476. var insuranceCostData2 = insuranceCostData1.Where(x => x.Iid == 2).ToList();
  477. if (insuranceCostData2.Any())
  478. {
  479. //_view.Msg = $"保险数据为类型为“新数据请不要选此项”,不可自动审核!";
  480. //return _view;
  481. var ccpIds = insuranceCostData2.Select(x => x.ccpId).ToList();
  482. var auditCcpIds = _sqlSugar.Queryable<Grp_CreditCardPayment>().Where(x => x.IsDel == 0 && ccpIds.Contains(x.Id) && x.IsAuditGM == 3).Select(x => x.Id).ToList();
  483. if (auditCcpIds.Any())
  484. {
  485. var ccpUpd = _sqlSugar.Updateable<Grp_CreditCardPayment>()
  486. .SetColumns(it => it.IsAuditGM == 0)
  487. .SetColumns(it => it.AuditGMOperate == 0)
  488. .SetColumns(it => it.AuditGMDate == string.Empty)
  489. .Where(s => auditCcpIds.Contains(s.Id))
  490. .ExecuteCommand();
  491. }
  492. }
  493. #endregion
  494. //var insuranceCostData = insuranceCostData1.Where(x => insuranceType.Contains(x.Iid)).ToList();
  495. var insuranceCostData = insuranceCostData1.ToList();
  496. var currInsuranceInfo = insuranceCostData.Where(x => x.Id == dataId).FirstOrDefault();
  497. if (!insuranceCostData.Any() && currInsuranceInfo == null)
  498. {
  499. _view.Msg = $"暂无保险数据,不可自动审核!";
  500. return _view;
  501. }
  502. if (currInsuranceInfo.CNYPrice == 0.00M)
  503. {
  504. _view.Msg = $"保险数据未录入费用信息,不可自动审核!";
  505. QuashAudit(currModule, diId, dataId);
  506. return _view;
  507. }
  508. var groupPeopleNum = groupDetails.VisitPNumber;
  509. var visitCountrys = groupDetails.VisitCountry.Split("|").ToList();
  510. if (!visitCountrys.Any())
  511. {
  512. _view.Msg = $"出访国家为空,不可自动审核!";
  513. return _view;
  514. }
  515. var basicCountrys = insuranceCountryCostData.Select(x => x.CountryName).ToList();
  516. var schengenCountry = visitCountrys.Intersect(basicCountrys); //申根国
  517. var unSchengenCountry = visitCountrys.Except(basicCountrys); //非申根国
  518. var schengenCost = insuranceCountryCostData.Where(x => schengenCountry.Contains(x.CountryName)).Sum(x => x.Cost); // 申根国费用
  519. var unSchengenCost = unSchengenCountry.Count() * 35; // 非申根国费用
  520. var groupBudgetCost = (schengenCost + unSchengenCost) * groupPeopleNum;
  521. var groupActialCost = insuranceCostData.Sum(x => x.CNYPrice);
  522. if (groupActialCost > groupBudgetCost)
  523. {
  524. _view.Msg = $"保险费用超出团组成本费用";
  525. QuashAudit(currModule, diId, dataId);
  526. return _view;
  527. }
  528. //自动审核
  529. var ccpUpdate = _sqlSugar.Updateable<Grp_CreditCardPayment>()
  530. .SetColumns(it => it.IsAuditGM == 3)
  531. .SetColumns(it => it.AuditGMOperate == 4)
  532. .SetColumns(it => it.AuditGMDate == DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
  533. .Where(s => s.DIId == diId && s.CTable == currModule && s.CId == dataId)
  534. .ExecuteCommand();
  535. if (ccpUpdate > 0)
  536. {
  537. _view.Code = 200;
  538. _view.Msg = "自动审核执行成功";
  539. }
  540. else _view.Msg = "自动审核执行失败";
  541. return _view;
  542. #endregion
  543. }
  544. else _view.Msg = $"请传入有效的feeType参数";
  545. return _view;
  546. }
  547. /// <summary>
  548. /// hotel、op 撤销自动审核的数据
  549. /// </summary>
  550. /// <param name="type">
  551. /// 酒店 76
  552. /// op 79
  553. /// </param>
  554. /// <param name="diId"></param>
  555. /// <param name="dataId"></param>
  556. /// <returns></returns>
  557. private bool QuashAudit(int type, int diId, int dataId)
  558. {
  559. //撤销该条数据的自动审核 --> 该条数据的审核状态是自动审核 3 --> 0
  560. var ccpInfo = _sqlSugar.Queryable<Grp_CreditCardPayment>()
  561. .Where(s => s.DIId == diId && s.CTable == type && s.CId == dataId && s.IsAuditGM == 3)
  562. .First();
  563. if (ccpInfo != null)
  564. {
  565. var ccpUpdate = _sqlSugar.Updateable<Grp_CreditCardPayment>()
  566. .SetColumns(it => it.IsAuditGM == 0)
  567. .SetColumns(it => it.AuditGMOperate == 0)
  568. .SetColumns(it => it.AuditGMDate == string.Empty)
  569. .Where(s => s.Id == ccpInfo.Id)
  570. .ExecuteCommand();
  571. if (ccpUpdate > 0)
  572. {
  573. return true;
  574. }
  575. }
  576. return false;
  577. }
  578. //
  579. /// <summary>
  580. /// 费用自动审核
  581. /// </summary>
  582. /// <param name="feeType">
  583. /// 1.酒店 76
  584. /// 2.op 79
  585. /// </param>
  586. /// <param name="diId">团组Id</param>
  587. /// <param name="dataId">数据Id(模块类型主表Id)</param>
  588. /// <returns></returns>
  589. public string IsOverBudget(int feeType, int diId, int dataId)
  590. {
  591. string _view ="-";
  592. if (diId < 1) { return _view; }
  593. if (dataId < 1) { return _view; }
  594. List<int> stids = new List<int>() { 17, 66 };
  595. var setData = _sqlSugar.Queryable<Sys_SetData>().Where(x => x.IsDel == 0 && stids.Contains(x.STid)).ToList();
  596. string _teamCurrency = string.Empty;
  597. var groupInfo = _sqlSugar.Queryable<Grp_GroupCostParameter>().Where(x => x.IsDel == 0 && x.DiId == diId).First();
  598. if (groupInfo == null) { return _view; }
  599. _teamCurrency = groupInfo.Currency;
  600. //币种验证 统一为currencycode三字码
  601. if (int.TryParse(_teamCurrency, out int currency)) _teamCurrency = setData.Find(x => x.Id == currency)?.Name ?? "";
  602. string costContentSql = $"Select * From Grp_GroupCost";
  603. var costContents = _sqlSugar.SqlQueryable<GroupCostAuditView>(costContentSql).Where(x => x.IsDel == 0 && x.Diid == diId).ToList();
  604. if (costContents.Count < 1) { return _view; }
  605. //处理 成本详细信息 日期为空
  606. for (int i = 0; i < costContents.Count; i++)
  607. {
  608. if (string.IsNullOrEmpty(costContents[i].Date))
  609. {
  610. int index = i - 1;
  611. if (index >= 0)
  612. {
  613. costContents[i].Date = costContents[index].Date;
  614. }
  615. }
  616. }
  617. if (feeType == 1)
  618. {
  619. var hotelCostInfo = _sqlSugar.Queryable<Grp_HotelReservations>().Where(x => x.IsDel == 0 && x.DiId == diId && x.Id == dataId).First();
  620. var hotelCostDetails = _sqlSugar.Queryable<Grp_HotelReservationsContent>().Where(x => x.IsDel == 0 && x.DiId == diId && x.HrId == dataId).ToList();
  621. if (hotelCostInfo == null) return _view;
  622. //获取C表汇率
  623. decimal _rate = 1.0000M;
  624. var roomFeeInfo = hotelCostDetails.Where(x => x.PriceType == 1).First();
  625. if (roomFeeInfo == null) return _view;
  626. _rate = roomFeeInfo.Rate == 0.0000M ? 1.0000M : roomFeeInfo.Rate;
  627. bool isAutoAudit = true; //是否自动审核
  628. DateTime checkIn = Convert.ToDateTime(hotelCostInfo.CheckInDate),
  629. checkOut = Convert.ToDateTime(hotelCostInfo.CheckOutDate);
  630. if (checkOut > checkIn) checkOut = checkOut.AddDays(-1); //房费计算,结束日期为前一天
  631. var hotelCostInfos = costContents.Where(x => Convert.ToDateTime(x.Date) >= checkIn && Convert.ToDateTime(x.Date) <= checkOut).ToList();
  632. if (hotelCostInfos.Count < 1) isAutoAudit = false;
  633. decimal otherFee = hotelCostDetails.Where(x => x.PriceType != 1).Sum(x => x.Price * (x.Rate == 0.0000M ? 1.0000M : x.Rate));
  634. if (otherFee > 0) { otherFee /= (checkOut - checkIn).Days; }
  635. var hotelCostInfosGroup = hotelCostInfos.GroupBy(x => x.Date);
  636. foreach (var item in hotelCostInfosGroup)
  637. {
  638. decimal hotelSingleRoomFee = item.Sum(x => x.HotelSingleRoomFee);
  639. decimal hotelDoubleRoomFee = item.Sum(x => x.HotelDoubleRoomFee);
  640. decimal hotelSuiteFee = item.Sum(x => x.HotelSuiteFee);
  641. decimal hotelSuiteRoomFee = item.Sum(x => x.HotelSuiteRoomFee);
  642. //1.判断费用是否 <= 成本费用
  643. //1.1 判断单间费用
  644. decimal singleRoomPrice = (hotelCostInfo.SingleRoomPrice + otherFee) * _rate;
  645. if (singleRoomPrice > 0) if (singleRoomPrice > hotelSingleRoomFee * _rate) isAutoAudit = false;
  646. //1.2 判断双人间费用
  647. decimal doubleRoomPrice = (hotelCostInfo.DoubleRoomPrice + otherFee) * _rate;
  648. if (doubleRoomPrice > 0) if (doubleRoomPrice > hotelDoubleRoomFee * _rate) isAutoAudit = false;
  649. //1.3 判断套房费用
  650. decimal suiteRoomPrice = (hotelCostInfo.SuiteRoomPrice + otherFee) * _rate;
  651. if (suiteRoomPrice > 0) if (suiteRoomPrice > hotelSuiteFee * _rate) isAutoAudit = false;
  652. //1.4 判断其他房型费用
  653. decimal otherRoomPrice = (hotelCostInfo.OtherRoomPrice + otherFee) * _rate;
  654. if (otherRoomPrice > 0) if (otherRoomPrice > hotelSuiteRoomFee * _rate) isAutoAudit = false;
  655. }
  656. //2.判断是否自动审核
  657. if (isAutoAudit)
  658. {
  659. return $"未超预算";
  660. }
  661. }
  662. else if (feeType == 2)
  663. {
  664. //1.基础数据参数验证
  665. var priceType = new List<int>() { 1062 };
  666. var opinfos = _sqlSugar.Queryable<Grp_CarTouristGuideGroundReservations>()
  667. .Where(x => x.IsDel == 0 && x.DiId == diId && x.Id == dataId && !priceType.Contains(x.PriceType))
  668. .First();
  669. //var opinfos = _sqlSugar.Queryable<Grp_CarTouristGuideGroundReservations>()
  670. // .Where(x => x.IsDel == 0 && x.DiId == diId && x.Id == dataId )
  671. // .First();
  672. if (opinfos == null) return _view;
  673. //1.含超时费用/超支费用 手动审核
  674. if (opinfos.SelectCheck.Contains("超时") || opinfos.SelectCheck.Contains("超支") || opinfos.SelectCheck.Contains("尾款")) return _view;
  675. //1.参数验证
  676. var opCheckPriceTyeps = opinfos.SelectCheck.Split(',');
  677. var opCheckPriceTyepIds = setData.Where(x => opinfos.SelectCheck.Split(',').Contains(x.Name)).Select(x => x.Id).ToList();
  678. var opContents = _sqlSugar.Queryable<Grp_CarTouristGuideGroundReservationsContent>()
  679. .Where(x => x.IsDel == 0 && x.DiId == diId && x.CTGGRId == dataId && opCheckPriceTyepIds.Contains(x.SId))
  680. .OrderBy(x => x.DatePrice, OrderByType.Asc)
  681. .ToList();
  682. if (opContents.Count < 1) return _view;
  683. //获取C表汇率
  684. decimal _rate = 1.0000M;
  685. var payInfo = _sqlSugar.Queryable<Grp_CreditCardPayment>().Where(x => x.IsDel == 0 && x.DIId == diId && x.CTable == 79 && x.Id == dataId).First();
  686. if (payInfo == null) return _view;
  687. _rate = payInfo.DayRate;
  688. string opCurrencyName = setData.Find(x => x.Id == opContents[0].Currency)?.Name ?? "";
  689. var opBasicDatas = setData.Where(x => x.STid == 17).ToList(); //费用类型基础数据
  690. bool isAutoAudit = true;
  691. if (!DateTime.TryParse(opinfos.ServiceStartTime, out DateTime startDt1) || !DateTime.TryParse(opinfos.ServiceEndTime, out DateTime endDt1)) return _view;
  692. DateTime startDt = startDt1;
  693. DateTime endDt = endDt1;
  694. var opCostDatas = costContents.Where(it => Convert.ToDateTime(it.Date) >= startDt && Convert.ToDateTime(it.Date) <= endDt).ToList();
  695. if (opCostDatas.Count < 1) return _view;
  696. var noAuditFeeTypeIds = new List<int> {
  697. 982 ,//982 车超时费 -- 暂无
  698. 96 ,//96 接送机费 -- 暂无
  699. 97 ,//97 其他费用 -- 暂无
  700. 992 ,//992 住补费用 -- 暂无
  701. 1059,//1059 导游超时费用 -- 暂无
  702. 1070,//1070 尾款金额 -- 暂无
  703. 1071,//1071 其他额外费用 -- 暂无
  704. 1073,//1073 翻译超时费 -- 暂无
  705. 1074,//1074 早餐超支费用 -- 暂无
  706. 1075,//1075 午餐超支费用 -- 暂无
  707. 1076,//1076 晚餐超支费用 -- 暂无
  708. };
  709. //费用类型筛选 包含 feeTypeIds && 包含这些类型费用大于0
  710. var noAuditFeeContents = opContents.Where(x => x.Price > 0 && noAuditFeeTypeIds.Contains(x.SId)).ToList();
  711. if (noAuditFeeContents.Count > 0) return _view;
  712. //2.按天按项 检查费用是否超过预算
  713. var opDayContent = opContents.GroupBy(x => x.DatePrice);
  714. foreach (var item in opDayContent)
  715. {
  716. var opCostInfo = opCostDatas.Where(x => Convert.ToDateTime(x.Date) == item.Key).ToList();
  717. if (opCostInfo.Count < 1) continue;
  718. //车费 91
  719. var opCarCost = item.FirstOrDefault(x => x.SId == 91);
  720. if (opCarCost != null) if (opCarCost.Price * _rate > _rate * opCostInfo.Sum(x => x.CarFee)) isAutoAudit = false;
  721. //982 车超时费 -- 暂无
  722. //92 导游费
  723. var opGuideCost = item.FirstOrDefault(x => x.SId == 92);
  724. if (opGuideCost != null) if (opGuideCost.Price * _rate > _rate * opCostInfo.Sum(x => x.GuideFee)) isAutoAudit = false;
  725. //94 导游景点费
  726. var opGuideScenicCost = item.FirstOrDefault(x => x.SId == 94);
  727. if (opGuideScenicCost != null) if (opGuideScenicCost.Price * _rate > _rate * opCostInfo.Sum(x => x.GuideScenicFee)) isAutoAudit = false;
  728. //95 导游小费
  729. var opGuideTipCost = item.FirstOrDefault(x => x.SId == 95);
  730. if (opGuideTipCost != null) if (opGuideTipCost.Price * _rate > _rate * opCostInfo.Sum(x => x.GuideTipFee)) isAutoAudit = false;
  731. //983 导游餐补
  732. var opGuideMealCost = item.FirstOrDefault(x => x.SId == 983);
  733. if (opGuideMealCost != null) if (opGuideMealCost.Price * _rate > _rate * opCostInfo.Sum(x => x.GuideMealFee)) isAutoAudit = false;
  734. //984 导游房补
  735. var opGuideRoomCost = item.FirstOrDefault(x => x.SId == 984);
  736. if (opGuideRoomCost != null) if (opGuideRoomCost.Price * _rate > _rate * opCostInfo.Sum(x => x.GuideRoomFee)) isAutoAudit = false;
  737. //985 导游交通
  738. var opGuideTrafficCost = item.FirstOrDefault(x => x.SId == 985);
  739. if (opGuideTrafficCost != null) if (opGuideTrafficCost.Price * _rate > _rate * opCostInfo.Sum(x => x.GuideTrafficFee)) isAutoAudit = false;
  740. //96 接送机费 -- 暂无
  741. //97 其他费用 -- 暂无
  742. //979 司机工资
  743. var opDriverCost = item.FirstOrDefault(x => x.SId == 979);
  744. if (opDriverCost != null) if (opDriverCost.Price * _rate > _rate * opCostInfo.Sum(x => x.DriverFee)) isAutoAudit = false;
  745. //980 司机小费
  746. var opDriverTipCost = item.FirstOrDefault(x => x.SId == 980);
  747. if (opDriverTipCost != null) if (opDriverTipCost.Price * _rate > _rate * opCostInfo.Sum(x => x.DriverTipFee)) isAutoAudit = false;
  748. //981 司机餐补
  749. var opDriverMealCost = item.FirstOrDefault(x => x.SId == 981);
  750. if (opDriverMealCost != null) if (opDriverMealCost.Price * _rate > _rate * opCostInfo.Sum(x => x.DriverMealFee)) isAutoAudit = false;
  751. //988 客户早餐费用
  752. var opClientBreakfastCost = item.FirstOrDefault(x => x.SId == 988);
  753. if (opClientBreakfastCost != null) if (opClientBreakfastCost.Price * _rate > _rate * opCostInfo.Sum(x => x.ClientBreakfastFee)) isAutoAudit = false;
  754. //93 客户午餐费用
  755. var opClientLunchCost = item.FirstOrDefault(x => x.SId == 93);
  756. if (opClientLunchCost != null) if (opClientLunchCost.Price * _rate > _rate * opCostInfo.Sum(x => x.ClientLunchFee)) isAutoAudit = false;
  757. //989 客户晚餐费用
  758. var opClientDinnerCost = item.FirstOrDefault(x => x.SId == 989);
  759. if (opClientDinnerCost != null) if (opClientDinnerCost.Price * _rate > _rate * opCostInfo.Sum(x => x.ClientDinnerFee)) isAutoAudit = false;
  760. //990 景点门票费
  761. var opScenicTicketCost = item.FirstOrDefault(x => x.SId == 990);
  762. if (opScenicTicketCost != null) if (opScenicTicketCost.Price * _rate > _rate * opCostInfo.Sum(x => x.ScenicTicketFee)) isAutoAudit = false;
  763. //991 饮料/零食/水果
  764. var opDrinkSnackFruitCost = item.FirstOrDefault(x => x.SId == 991);
  765. if (opDrinkSnackFruitCost != null) if (opDrinkSnackFruitCost.Price * _rate > _rate * opCostInfo.Sum(x => x.DrinkSnackFruitFee)) isAutoAudit = false;
  766. //992 住补费用 -- 暂无
  767. //994 翻译费
  768. var opTranslatorCost = item.FirstOrDefault(x => x.SId == 994);
  769. if (opTranslatorCost != null) if (opTranslatorCost.Price * _rate > _rate * opCostInfo.Sum(x => x.TranslatorFee)) isAutoAudit = false;
  770. //1059 导游超时费用 -- 暂无
  771. //1070 尾款金额 -- 暂无
  772. //1071 其他额外费用 -- 暂无
  773. //1073 翻译超时费 -- 暂无
  774. //1074 早餐超支费用 -- 暂无
  775. //1075 午餐超支费用 -- 暂无
  776. //1076 晚餐超支费用 -- 暂无
  777. }
  778. //更改审核状态
  779. if (isAutoAudit) return _view;
  780. }
  781. else return _view;
  782. return _view;
  783. }
  784. }
  785. }