DecreasePaymentsRepository.cs 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. using AutoMapper;
  2. using NPOI.HPSF;
  3. using NPOI.SS.Formula.Functions;
  4. using OASystem.Domain;
  5. using OASystem.Domain.AesEncryption;
  6. using OASystem.Domain.Dtos.Groups;
  7. using OASystem.Domain.Entities.Financial;
  8. using OASystem.Domain.Entities.Groups;
  9. using OASystem.Domain.Entities.Resource;
  10. using OASystem.Domain.ViewModels.Financial;
  11. using OASystem.Domain.ViewModels.Groups;
  12. using OASystem.Domain.ViewModels.QiYeWeChat;
  13. using OASystem.Domain.ViewModels.Resource;
  14. using OASystem.Infrastructure.Repositories.System;
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Linq;
  18. using System.Runtime.Intrinsics.Arm;
  19. using System.Text;
  20. using System.Threading.Tasks;
  21. namespace OASystem.Infrastructure.Repositories.Groups
  22. {
  23. public class DecreasePaymentsRepository : BaseRepository<Grp_DecreasePayments, Grp_DecreasePayments>
  24. {
  25. private readonly IMapper _mapper;
  26. private readonly TeamRateRepository _teamRateRep;
  27. private readonly SetDataRepository _setDataRepository;
  28. public DecreasePaymentsRepository(SqlSugarClient sqlSugar, IMapper mapper, TeamRateRepository teamRateRep, SetDataRepository setDataRepository)
  29. : base(sqlSugar)
  30. {
  31. this._mapper = mapper;
  32. _teamRateRep = teamRateRep;
  33. this._setDataRepository = setDataRepository;
  34. }
  35. /// <summary>
  36. /// 根据团组Id查询数据
  37. /// </summary>
  38. /// <param name="dto"></param>
  39. /// <returns></returns>
  40. public async Task<JsonView> DecreasePaymentsList(DecreasePaymentsListDto dto)
  41. {
  42. string sqlWhere = "";
  43. if (dto.IsPaySign != -1)
  44. {
  45. sqlWhere += string.Format(@" And ccp.IsPay = {0} ", dto.IsPaySign);
  46. }
  47. //雷怡 2024-5-8 15:14 新增View字段 CreateTime
  48. string sql = string.Format(@" Select gdp.Id,gdp.DiId,gdp.PriceName,gdp.FeeTotal,sd1.[Name] as Currency,gdp.FilePath,su.CnName as CreateUserName,
  49. ccp.IsAuditGM as isAudit,ccp.IsPay,gdp.CreateTime
  50. From Grp_DecreasePayments as gdp With(Nolock) Left Join Grp_CreditCardPayment as ccp With(Nolock) On gdp.Id = ccp.CId
  51. Left Join Sys_SetData as sd1 On gdp.Currency = sd1.Id
  52. Left Join Sys_Users as su On gdp.CreateUserId = su.Id
  53. Where gdp.DiId = {0} And ccp.CTable = 98 {2} And ccp.IsDel = 0 And gdp.IsDel = 0 And gdp.CreateUserId in ({1}) ", dto.DiId, dto.UserId, sqlWhere);
  54. List<DecreasePaymentsView> _DecreasePayments = await _sqlSugar.SqlQueryable<DecreasePaymentsView>(sql).ToListAsync();
  55. return new JsonView() { Code = 200, Msg = MsgTips.Succeed, Data = _DecreasePayments };
  56. }
  57. public async Task<JsonView> DecreasePaymentsSelect(DecreasePaymentsDto dto)
  58. {
  59. #region 团组下拉框
  60. List<Grp_GroupsTaskAssignment> grp_GroupsTaskAssignment = Query<Grp_GroupsTaskAssignment>(a => a.IsDel == 0 && a.UId == dto.UserId && a.CTId == dto.CTId).ToList();
  61. string DiId = "";
  62. List<DecreasePaymentGroupView> _Delegations = new List<DecreasePaymentGroupView>();
  63. if (grp_GroupsTaskAssignment.Count > 0)
  64. {
  65. foreach (var item in grp_GroupsTaskAssignment)
  66. {
  67. DiId += item.DIId + ",";
  68. }
  69. if (DiId != "0")
  70. {
  71. DiId = DiId.Substring(0, DiId.Length - 1);
  72. }
  73. string sql = string.Format($@"Select
  74. (select ssd.name from Sys_SetData ssd WHERE ssd.id = di.TeamLevSId) as TeamLevSId,
  75. di.Id,
  76. di.TeamName,
  77. di.ClientUnit,
  78. di.ClientName,
  79. di.TourCode,
  80. di.TeamDid,
  81. sd.[Name] As TeamTypeName,
  82. di.VisitCountry,
  83. di.VisitStartDate,
  84. di.VisitEndDate,
  85. di.VisitDays,
  86. di.VisitPNumber,
  87. di.CreateTime
  88. From Grp_DelegationInfo di With(NoLock)
  89. Left Join Sys_SetData sd On di.TeamDid = sd.Id
  90. Where di.Id in({DiId}) and di.IsDel=0
  91. Order By di.VisitStartDate Desc");
  92. //DecreasePaymentGroupView
  93. _Delegations = _sqlSugar.SqlQueryable<DecreasePaymentGroupView>(sql).ToList();
  94. }
  95. #endregion
  96. #region 其他下拉框查询
  97. //支付方式
  98. List<Sys_SetData> Payment = _sqlSugar.Queryable<Sys_SetData>().Where(a => a.STid == 14 && a.IsDel == 0).ToList();
  99. List<SetDataInfoView> _Payment = _mapper.Map<List<SetDataInfoView>>(Payment);
  100. //供应商 supplier
  101. List<Sys_SetData> supplier = _sqlSugar.Queryable<Sys_SetData>().Where(a => a.STid == 21 && a.IsDel == 0).ToList();
  102. List<SetDataInfoView> _supplier = _mapper.Map<List<SetDataInfoView>>(supplier);
  103. if (_supplier.Count > 0)
  104. {
  105. var d1 = _supplier.Find(it => it.Name.Equals("策划 公司"));
  106. if (d1 != null) _supplier.Remove(d1);
  107. var d2 = _supplier.Find(it => it.Name.Equals("礼仪"));
  108. if (d2 != null) _supplier.Remove(d2);
  109. var d3 = _supplier.Find(it => it.Name.Equals("日常办公用品"));
  110. if (d3 != null) _supplier.Remove(d3);
  111. var d4 = _supplier.Find(it => it.Name.Equals("其他"));
  112. if (d4 != null)
  113. {
  114. _supplier.Remove(d4);
  115. _supplier.Add(d4);
  116. }
  117. }
  118. List<dynamic> _supplierArea = new List<dynamic>() {
  119. new { id=1,name = "国内" },
  120. new { id=2,name = "国外" },
  121. };
  122. #endregion
  123. var data = new
  124. {
  125. payment = _Payment,
  126. groupName = _Delegations,
  127. supplier = _supplier,
  128. supplierArea = _supplierArea
  129. };
  130. return new JsonView() { Code = 200, Msg = MsgTips.Succeed, Data = data };
  131. }
  132. public async Task<JsonView> OpDecreasePayments(DecreasePaymentsOpDto dto)
  133. {
  134. BeginTran();
  135. int id = 0;
  136. var grp_Decrease = _mapper.Map<Grp_DecreasePayments>(dto);
  137. var recheck = true;
  138. //处理费用总计
  139. if (grp_Decrease.FeeTotal == 0.00M)
  140. {
  141. grp_Decrease.FeeTotal = grp_Decrease.Price * grp_Decrease.Price;
  142. }
  143. List<TeamRateDescAddCurrencyIdView> teamRates = await _teamRateRep.PostGroupTeamRateItemByDiIdAndCTableId(1, dto.DiId, 98);
  144. var teamRate = teamRates.Where(it => it.CurrencyId == grp_Decrease.Currency).FirstOrDefault();
  145. if (teamRate == null)
  146. {
  147. return new JsonView() { Code = 400, Msg = $"添加失败!团组汇率未设置{_sqlSugar.Queryable<Sys_SetData>().First(it => it.IsDel == 0 && it.Id == grp_Decrease.Currency)?.Name ?? ""}该币种汇率!" };
  148. }
  149. if (dto.Status == 1)//添加
  150. {
  151. id = await AddAsyncReturnId(grp_Decrease);
  152. grp_Decrease.Id = id;
  153. #region C表赋值
  154. Grp_CreditCardPayment C = new Grp_CreditCardPayment();
  155. C.PayDId = dto.PayDId;
  156. C.ConsumptionPatterns = "";
  157. C.ConsumptionDate = "";
  158. C.CTDId = 0;
  159. C.BankNo = "";
  160. C.CardholderName = "";
  161. C.PayMoney = grp_Decrease.FeeTotal;
  162. C.PaymentCurrency = grp_Decrease.Currency;
  163. C.CompanyBankNo = "";
  164. C.OtherBankName = dto.OtherBankName;
  165. C.OtherSideNo = dto.OtherSideNo;
  166. C.OtherSideName = dto.OtherSideName;
  167. C.Remark = "";
  168. C.CreateUserId = grp_Decrease.CreateUserId;
  169. C.MFOperator = 0;
  170. C.MFOperatorDate = "";
  171. C.IsAuditDM = 0;
  172. C.AuditDMOperate = 0;
  173. C.AuditDMDate = "";
  174. C.IsAuditMF = 0;
  175. C.AuditMFOperate = 0;
  176. C.AuditMFDate = "";
  177. C.IsAuditGM = 0;
  178. C.AuditGMOperate = 21;
  179. C.AuditGMDate = "";
  180. //2025-04-07 第四次更改 PayDId == 72(刷卡) IsPay == 1
  181. if (C.PayDId == 72) C.IsPay = 1;
  182. else C.IsPay = 0;
  183. C.DIId = grp_Decrease.DiId;
  184. C.CId = id;
  185. C.CTable = 98;
  186. C.PayPercentage = 100;
  187. C.PayThenMoney = 0;
  188. C.PayPercentageOld = 0;
  189. C.PayThenMoneyOld = 0;
  190. C.UpdateDate = "";
  191. C.Payee = dto.SupplierName;
  192. C.OrbitalPrivateTransfer = dto.OrbitalPrivateTransfer;
  193. C.ExceedBudget = 0;
  194. //C.RMBPrice = 0.00f;
  195. if (teamRate.CurrencyCode.Equals("CNY"))
  196. {
  197. C.DayRate = 1.0000M;
  198. C.RMBPrice = C.PayMoney;
  199. }
  200. else
  201. {
  202. C.DayRate = teamRate.Rate;
  203. C.RMBPrice = C.PayMoney * C.DayRate;
  204. }
  205. #endregion
  206. int cId = await _sqlSugar.Insertable(C).ExecuteReturnIdentityAsync();
  207. if (cId > 1)
  208. {
  209. #region 策划部内容同步添加至资料库
  210. //var userInfo = _sqlSugar.Queryable<Sys_Users>()
  211. // .LeftJoin<Sys_Department>((u,d)=> d.IsDel == 0 && u.DepId == d.Id )
  212. // .Where((u, d) => u.Id == dto.CreateUserId && d.DepName == "策划部" && u.IsDel == 0)
  213. // .First();
  214. //if (userInfo != null)
  215. //{
  216. // Res_MediaSuppliers mediaSuppliersData = new Res_MediaSuppliers
  217. // {
  218. // CreateTime = DateTime.Now,
  219. // CreateUserId = dto.CreateUserId,
  220. // UnitName = dto.SupplierName,
  221. // TypeId = dto.SupplierTypeId,
  222. // UnitAddress = dto.SupplierAddress,
  223. // Contact = dto.SupplierContact,
  224. // Tel = dto.SupplierContactNumber,
  225. // Email = dto.SupplierEmail,
  226. // Privince = dto.SupplierArea == 1 ? "国内" :"国外",
  227. // };
  228. // EncryptionProcessor.EncryptProperties(mediaSuppliersData);
  229. // _sqlSugar.Insertable(mediaSuppliersData).ExecuteCommand();
  230. //}
  231. #endregion
  232. #region 世运会自动审核处理
  233. if (dto.DiId == 2590)
  234. {
  235. bool isAuto = false;
  236. var spPriceName = dto.PriceName.Split('-').ToArray();
  237. var expression = Expressionable.Create<Grp_GamesBudgetMaster>()
  238. .And(x => x.IsDel == 0);
  239. if (spPriceName.Length >= 2)
  240. {
  241. expression.And(x => x.T0 == spPriceName[0])
  242. .And(x => x.CalculationContent == spPriceName[1]);
  243. var setting = _sqlSugar.Queryable<Grp_GamesBudgetMaster>()
  244. .First(expression.ToExpression());
  245. if (setting != null)
  246. {
  247. var dbList = _sqlSugar.Queryable<Grp_DecreasePayments>()
  248. .LeftJoin<Grp_CreditCardPayment>((x, a) => x.Id == a.CId && a.IsDel == 0 && a.CTable == 98 && a.DIId == dto.DiId)
  249. .Where((x, a) => x.IsDel == 0 && x.PriceName.Contains(spPriceName[0]) && x.PriceName.Contains(spPriceName[1]) && x.Id != id)
  250. .Select((x, a) => new
  251. {
  252. x,
  253. a.RMBPrice,
  254. })
  255. .ToList();
  256. //币种问题 (836 CNY)
  257. if (dto.Currency != 836)
  258. {
  259. var rate = teamRates.Where(it => it.CurrencyId == grp_Decrease.Currency).FirstOrDefault();
  260. if (rate != null)
  261. {
  262. //dto.Price *= rate.Rate;
  263. dto.FeeTotal *= rate.Rate;
  264. }
  265. }
  266. //现有金额
  267. var sumPrice = dbList.Sum(x => x.RMBPrice);
  268. //现有数量
  269. //var sumCount = dbList.Sum(x => x.Quantity);
  270. var availableAmount = setting.ItemTotal - sumPrice;
  271. //var availableQuantity = setting.Quantity - sumCount;
  272. if (availableAmount > 0) // && availableQuantity > 0
  273. {
  274. //单价计算
  275. //var settingPrice = setting.UnitPrice;
  276. //if (int.TryParse(setting.CycleUnit, out int cycleUnit))
  277. //{
  278. // settingPrice *= cycleUnit;
  279. //}
  280. if (dto.FeeTotal <= availableAmount) //dto.Price <= settingPrice && && dto.Quantity <= setting.Quantity
  281. {
  282. isAuto = true;
  283. }
  284. }
  285. }
  286. }
  287. if (isAuto)
  288. {
  289. var execCount = _sqlSugar.Updateable<Grp_CreditCardPayment>()
  290. .SetColumns(a => new Grp_CreditCardPayment
  291. {
  292. IsAuditGM = 3, //自动审核通过
  293. AuditGMOperate = 4,
  294. AuditGMDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  295. })
  296. .Where(a => a.CId == id && a.CTable == 98 && a.DIId == 2590)
  297. .ExecuteCommand();
  298. recheck = false;
  299. }
  300. }
  301. #endregion
  302. #region 会务成本自动审核处理
  303. var hwids = _sqlSugar.Queryable<Sys_SetData>()
  304. .Where(x => x.IsDel == 0 && x.STid == 10 && x.Name.Contains("会务活动"))
  305. .Select(x => x.Id)
  306. .ToList();
  307. var groupTypeIsTrue = _sqlSugar.Queryable<Grp_DelegationInfo>()
  308. .Where(x => x.Id == dto.DiId && x.IsDel == 0)
  309. .Any(x => hwids.Contains(x.TeamDid));
  310. if (groupTypeIsTrue)
  311. {
  312. var main = _sqlSugar.Queryable<Grp_ConferenceAffairsCost>()
  313. .First(x => x.Diid == dto.DiId && x.IsDel == 0);
  314. if (main != null)
  315. {
  316. var chiArr = _sqlSugar.Queryable<Grp_ConferenceAffairsCostChild>()
  317. .Where(x => x.Diid == dto.DiId && x.ConferenceAffairsCostId == main.Id && x.IsDel == 0)
  318. .ToList();
  319. var find = chiArr.FirstOrDefault(x => x.PriceName == dto.PriceName && x.ReviewStatus == 1);
  320. var isAuto = false;
  321. //存在已审的会务成本
  322. if (find != null)
  323. {
  324. //检索历史金额
  325. var histyPrice = _sqlSugar.Queryable<Grp_DecreasePayments>()
  326. .LeftJoin<Grp_CreditCardPayment>((x, a) => a.IsDel == 0 && a.CTable == 98 && a.CId == x.Id )
  327. .Where((x, a) => x.IsDel == 0 && x.PriceName == dto.PriceName && x.Id != grp_Decrease.Id)
  328. .Select((x, a) => new
  329. {
  330. x.FeeTotal,
  331. x.Currency,
  332. a.DayRate,
  333. a.RMBPrice,
  334. x.DiId,
  335. x.PriceName,
  336. x.Price,
  337. x.Quantity,
  338. })
  339. .ToList();
  340. //币种不同情况
  341. if (true)
  342. {
  343. //币种全转换为人民币
  344. var rate = teamRates.Where(it => it.CurrencyId == grp_Decrease.Currency).FirstOrDefault();
  345. if (rate != null)
  346. {
  347. grp_Decrease.FeeTotal *= rate.Rate;
  348. }
  349. var ConferenceAffairsCostFeeTotal = 0.00M;
  350. ConferenceAffairsCostFeeTotal = find.Count * find.CostPrice * find.Rate;
  351. var histyPriceValue = histyPrice.Sum(x => x.FeeTotal * x.DayRate);
  352. if (ConferenceAffairsCostFeeTotal > histyPriceValue)
  353. {
  354. isAuto = ConferenceAffairsCostFeeTotal - histyPriceValue >= grp_Decrease.FeeTotal;
  355. }
  356. }
  357. }
  358. if (isAuto) {
  359. var execCount = _sqlSugar.Updateable<Grp_CreditCardPayment>()
  360. .SetColumns(a => new Grp_CreditCardPayment
  361. {
  362. IsAuditGM = 3, //自动审核通过
  363. AuditGMOperate = 4,
  364. AuditGMDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  365. })
  366. .Where(a => a.CId == grp_Decrease.Id && a.CTable == 98 && a.DIId == grp_Decrease.DiId)
  367. .ExecuteCommand();
  368. recheck = false;
  369. }
  370. }
  371. }
  372. #endregion
  373. CommitTran();
  374. var data = new { ccpId = cId, sign = 1, dataId = id, recheck };
  375. return new JsonView() { Code = 200, Msg = "添加成功!", Data = data };
  376. }
  377. RollbackTran();
  378. return new JsonView() { Code = 400, Msg = "添加失败!" };
  379. }
  380. else if (dto.Status == 2)
  381. {
  382. //已审核、已支付 不可修改
  383. var grp_CreditCardPayment = _sqlSugar.Queryable<Grp_CreditCardPayment>().First(a => a.CId == grp_Decrease.Id && a.CTable == 98 && a.CId == grp_Decrease.Id && a.IsDel == 0);
  384. if (grp_CreditCardPayment == null) return new JsonView() { Code = 400, Msg = MsgTips.Fail };
  385. var isAuditGM = grp_CreditCardPayment.IsAuditGM;
  386. if (isAuditGM == 1 || isAuditGM == 3) return new JsonView() { Code = 400, Msg = $"该费用已通过审核,不可编辑" };
  387. if (grp_CreditCardPayment.IsPay == 1) return new JsonView() { Code = 400, Msg = $"该费用已付款,不可编辑!" };
  388. bool res = await UpdateAsync(a => a.Id == grp_Decrease.Id, a => new Grp_DecreasePayments
  389. {
  390. DiId = grp_Decrease.DiId,
  391. SupplierArea = grp_Decrease.SupplierArea,
  392. SupplierTypeId = grp_Decrease.SupplierTypeId,
  393. SupplierName = grp_Decrease.SupplierName,
  394. SupplierContact = grp_Decrease.SupplierContact,
  395. SupplierContactNumber = grp_Decrease.SupplierContactNumber,
  396. SupplierSocialAccount = grp_Decrease.SupplierSocialAccount,
  397. SupplierEmail = grp_Decrease.SupplierEmail,
  398. SupplierAddress = grp_Decrease.SupplierAddress,
  399. PriceName = grp_Decrease.PriceName,
  400. Price = grp_Decrease.Price,
  401. Quantity = grp_Decrease.Quantity,
  402. FeeTotal = grp_Decrease.FeeTotal,
  403. Currency = grp_Decrease.Currency,
  404. FilePath = grp_Decrease.FilePath,
  405. OTAOrderNo = grp_Decrease.OTAOrderNo,
  406. Remark = grp_Decrease.Remark,
  407. });
  408. if (res)
  409. {
  410. if (teamRate != null)
  411. {
  412. if (teamRate.CurrencyCode.Equals("CNY"))
  413. {
  414. grp_CreditCardPayment.DayRate = 1.0000M;
  415. grp_CreditCardPayment.RMBPrice = grp_CreditCardPayment.PayMoney;
  416. }
  417. else
  418. {
  419. grp_CreditCardPayment.DayRate = teamRate.Rate;
  420. grp_CreditCardPayment.RMBPrice = grp_CreditCardPayment.PayMoney * grp_CreditCardPayment.DayRate;
  421. }
  422. }
  423. //2025-04-07 第四次更改 PayDId == 72(刷卡) IsPay == 1
  424. int isPay = 0;
  425. if (dto.PayDId == 72) isPay = 1;
  426. int CTable = await _sqlSugar.Updateable<Grp_CreditCardPayment>()
  427. .Where(a => a.Id == grp_CreditCardPayment.Id)
  428. .SetColumns(a => new Grp_CreditCardPayment
  429. {
  430. OtherSideName = dto.OtherSideName,
  431. OtherSideNo = dto.OtherSideNo,
  432. OtherBankName = dto.OtherBankName,
  433. PayDId = dto.PayDId,
  434. IsPay = isPay,
  435. PayMoney = grp_Decrease.FeeTotal,
  436. PaymentCurrency = grp_Decrease.Currency,
  437. Payee = dto.SupplierName,
  438. OrbitalPrivateTransfer = dto.OrbitalPrivateTransfer,
  439. DayRate = grp_CreditCardPayment.DayRate,
  440. RMBPrice = grp_CreditCardPayment.RMBPrice,
  441. //IsAuditGM = auto ? 3 : grp_CreditCardPayment.IsAuditGM
  442. })
  443. .ExecuteCommandAsync();
  444. if (CTable > 0)
  445. {
  446. #region 世运会自动审核处理
  447. if (dto.DiId == 2590)
  448. {
  449. bool isAuto = false;
  450. string[] spPriceName = dto.PriceName.Split('-').ToArray();
  451. var expression = Expressionable.Create<Grp_GamesBudgetMaster>()
  452. .And(x => x.IsDel == 0);
  453. if (spPriceName.Length >= 2)
  454. {
  455. expression.And(x => x.T0 == spPriceName[0])
  456. .And(x => x.CalculationContent == spPriceName[1]);
  457. var setting = _sqlSugar.Queryable<Grp_GamesBudgetMaster>()
  458. .First(expression.ToExpression());
  459. if (setting != null)
  460. {
  461. var dbList = _sqlSugar.Queryable<Grp_DecreasePayments>()
  462. .LeftJoin<Grp_CreditCardPayment>((x,a) => x.Id == a.CId && a.IsDel == 0 && a.CTable == 98 && a.DIId == dto.DiId )
  463. .Where((x, a) => x.IsDel == 0 && x.PriceName.Contains(spPriceName[0]) && x.PriceName.Contains(spPriceName[1]) && x.Id != dto.Id)
  464. .Select((x, a) => new
  465. {
  466. x,
  467. a.RMBPrice,
  468. })
  469. .ToList();
  470. //币种问题 (836 CNY)
  471. if (dto.Currency != 836)
  472. {
  473. var rate = teamRates.Where(it => it.CurrencyId == grp_Decrease.Currency).FirstOrDefault();
  474. if (rate != null)
  475. {
  476. //dto.Price *= rate.Rate;
  477. dto.FeeTotal *= rate.Rate;
  478. }
  479. }
  480. //现有金额
  481. var sumPrice = dbList.Sum(x => x.RMBPrice);
  482. //现有数量
  483. //var sumCount = dbList.Sum(x => x.Quantity);
  484. var availableAmount = setting.ItemTotal - sumPrice;
  485. //var availableQuantity = setting.Quantity - sumCount;
  486. if (availableAmount > 0) // && availableQuantity > 0
  487. {
  488. //单价计算
  489. //var settingPrice = setting.UnitPrice;
  490. //if (int.TryParse(setting.CycleUnit, out int cycleUnit))
  491. //{
  492. // settingPrice *= cycleUnit;
  493. //}
  494. if (dto.FeeTotal <= availableAmount) //dto.Price <= settingPrice && && dto.Quantity <= setting.Quantity
  495. {
  496. isAuto = true;
  497. }
  498. }
  499. }
  500. }
  501. if (isAuto)
  502. {
  503. _sqlSugar.Updateable<Grp_CreditCardPayment>()
  504. .SetColumns(a => new Grp_CreditCardPayment
  505. {
  506. IsAuditGM = 3, //自动审核通过
  507. AuditGMOperate = 4,
  508. AuditGMDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  509. })
  510. .Where(a => a.CId == dto.Id && a.CTable == 98 && a.DIId == 2590)
  511. .ExecuteCommand();
  512. recheck = false;
  513. }
  514. }
  515. #endregion
  516. #region 会务成本自动审核处理
  517. var hwids = _sqlSugar.Queryable<Sys_SetData>()
  518. .Where(x => x.IsDel == 0 && x.STid == 10 && x.Name.Contains("会务活动"))
  519. .Select(x => x.Id)
  520. .ToList();
  521. var groupTypeIsTrue = _sqlSugar.Queryable<Grp_DelegationInfo>()
  522. .Where(x => x.Id == dto.DiId && x.IsDel == 0)
  523. .Any(x => hwids.Contains(x.TeamDid));
  524. if (groupTypeIsTrue)
  525. {
  526. var main = _sqlSugar.Queryable<Grp_ConferenceAffairsCost>()
  527. .First(x => x.Diid == dto.DiId && x.IsDel == 0);
  528. if (main != null)
  529. {
  530. var chiArr = _sqlSugar.Queryable<Grp_ConferenceAffairsCostChild>()
  531. .Where(x => x.Diid == dto.DiId && x.ConferenceAffairsCostId == main.Id && x.IsDel == 0)
  532. .ToList();
  533. var find = chiArr.FirstOrDefault(x => x.PriceName == dto.PriceName && x.ReviewStatus == 1);
  534. var isAuto = false;
  535. //存在已审的会务成本
  536. if (find != null)
  537. {
  538. //检索历史金额
  539. var histyPrice = _sqlSugar.Queryable<Grp_DecreasePayments>()
  540. .LeftJoin<Grp_CreditCardPayment>((x, a) => a.IsDel == 0 && a.CTable == 98 && a.CId == x.Id)
  541. .Where((x, a) => x.IsDel == 0 && x.PriceName == dto.PriceName && x.Id != grp_Decrease.Id)
  542. .Select((x, a) => new
  543. {
  544. x.FeeTotal,
  545. x.Currency,
  546. a.DayRate,
  547. a.RMBPrice,
  548. x.DiId,
  549. x.PriceName,
  550. x.Price,
  551. x.Quantity,
  552. })
  553. .ToList();
  554. if (true)
  555. {
  556. //币种全转换为人民币
  557. var rate = teamRates.Where(it => it.CurrencyId == grp_Decrease.Currency).FirstOrDefault();
  558. if (rate != null)
  559. {
  560. grp_Decrease.FeeTotal *= rate.Rate;
  561. }
  562. var ConferenceAffairsCostFeeTotal = 0.00M;
  563. ConferenceAffairsCostFeeTotal = find.Count * find.CostPrice * find.Rate;
  564. var histyPriceValue = histyPrice.Sum(x => x.FeeTotal * x.DayRate);
  565. if (ConferenceAffairsCostFeeTotal > histyPriceValue)
  566. {
  567. isAuto = ConferenceAffairsCostFeeTotal - histyPriceValue >= grp_Decrease.FeeTotal;
  568. }
  569. }
  570. }
  571. if (isAuto)
  572. {
  573. var execCount = _sqlSugar.Updateable<Grp_CreditCardPayment>()
  574. .SetColumns(a => new Grp_CreditCardPayment
  575. {
  576. IsAuditGM = 3, //自动审核通过
  577. AuditGMOperate = 4,
  578. AuditGMDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  579. })
  580. .Where(a => a.CId == grp_Decrease.Id && a.CTable == 98 && a.DIId == grp_Decrease.DiId)
  581. .ExecuteCommand();
  582. recheck = false;
  583. }
  584. }
  585. }
  586. #endregion
  587. CommitTran();
  588. var data = new { ccpId = grp_CreditCardPayment.Id, sign = 2, dataId = grp_Decrease.Id ,recheck };
  589. return new JsonView() { Code = 200, Msg = "修改成功!", Data = data };
  590. }
  591. }
  592. }
  593. RollbackTran();
  594. return new JsonView() { Code = 400, Msg = MsgTips.Fail };
  595. }
  596. public async Task<Result> PostGroupNameAndEasy(DecreasePaymentsDto dto)
  597. {
  598. Result result = new Result() { Code = -2, Msg = "未知错误" };
  599. try
  600. {
  601. #region 团组下拉框
  602. List<Grp_GroupsTaskAssignment> grp_GroupsTaskAssignment = Query<Grp_GroupsTaskAssignment>(a => a.IsDel == 0 && a.UId == dto.UserId && a.CTId == dto.CTId).ToList();
  603. string DiId = "";
  604. foreach (var item in grp_GroupsTaskAssignment)
  605. {
  606. DiId += item.DIId + ",";
  607. }
  608. if (!string.IsNullOrWhiteSpace(DiId))
  609. {
  610. DiId = DiId.Substring(0, DiId.Length - 1);
  611. }
  612. else
  613. {
  614. DiId = "0";
  615. }
  616. string sql = string.Format(@"select Id,TourCode,TeamName,ClientName,VisitStartDate,VisitEndDate,VisitCountry,VisitDays,VisitPNumber from Grp_DelegationInfo where Id in({0}) and IsDel={1}", DiId, 0);
  617. List<DelegationEasyView> grp_Delegations = _sqlSugar.SqlQueryable<DelegationEasyView>(sql).ToList();
  618. if (grp_Delegations.Count != 0)
  619. {
  620. int count = grp_Delegations.Count;
  621. float totalPage = (float)count / dto.PageSize;//总页数
  622. if (totalPage == 0) totalPage = 1;
  623. else totalPage = (int)Math.Ceiling((double)totalPage);
  624. List<DelegationEasyView> delegationEasyViews = new List<DelegationEasyView>();
  625. for (int i = 0; i < dto.PageSize; i++)
  626. {
  627. var RowIndex = i + (dto.PageIndex - 1) * dto.PageSize;
  628. if (RowIndex < grp_Delegations.Count)
  629. {
  630. delegationEasyViews.Add(grp_Delegations[RowIndex]);
  631. }
  632. else
  633. {
  634. break;
  635. }
  636. }
  637. ListViewBase<DelegationEasyView> rst = new ListViewBase<DelegationEasyView>();
  638. rst.DataList = delegationEasyViews;
  639. rst.DataCount = count;
  640. rst.CurrPageIndex = dto.PageIndex;
  641. rst.CurrPageSize = dto.PageSize;
  642. return result = new Result() { Code = 0, Msg = "查询成功!", Data = rst };
  643. }
  644. else
  645. {
  646. ListViewBase<DelegationEasyView> rst = new ListViewBase<DelegationEasyView>();
  647. rst.DataList = new List<DelegationEasyView>();
  648. return result = new Result() { Code = 0, Msg = "暂无数据!", Data = rst };
  649. }
  650. #endregion
  651. }
  652. catch (Exception ex)
  653. {
  654. return result = new Result() { Code = -2, Msg = "程序错误" };
  655. throw;
  656. }
  657. }
  658. /// <summary>
  659. /// 根据团组增减款项表Id查询数据
  660. /// </summary>
  661. /// <param name="dto"></param>
  662. /// <returns></returns>
  663. /// <exception cref="NotImplementedException"></exception>
  664. public async Task<JsonView> QueryDecreasePaymentsById(DecreasePaymentsByIdDto dto)
  665. {
  666. string sql = string.Format($@"Select
  667. dp.Id,
  668. dp.DiId,
  669. dp.SupplierArea,
  670. dp.SupplierTypeId,
  671. dp.SupplierName,
  672. dp.SupplierContact,
  673. dp.SupplierContactNumber,
  674. dp.SupplierSocialAccount,
  675. dp.SupplierEmail,
  676. dp.SupplierAddress,
  677. dp.PriceName,
  678. dp.Price,
  679. dp.Quantity,
  680. dp.FeeTotal,
  681. dp.Currency,
  682. dp.FilePath,
  683. dp.OTAOrderNo,
  684. dp.Remark,
  685. ccp.PayDId,
  686. ccp.OrbitalPrivateTransfer,
  687. ccp.OtherBankName,
  688. ccp.OtherSideName,
  689. ccp.OtherSideNo,
  690. ccp.IsAuditGM
  691. From Grp_DecreasePayments dp With(NoLock)
  692. Left Join Grp_CreditCardPayment ccp With(NoLock) On dp.Id = ccp.CId And dp.DiId = ccp.DIId And ccp.CTable = 98
  693. Where dp.IsDel = 0 And dp.Id = {dto.Id}");
  694. var info = await _sqlSugar.SqlQueryable<DecreasePaymentsInfoView>(sql).FirstAsync();
  695. return new JsonView() { Code = 200, Msg = MsgTips.Succeed, Data = info };
  696. }
  697. /// <summary>
  698. /// Del
  699. /// </summary>
  700. /// <param name="dto"></param>
  701. /// <returns></returns>
  702. /// <exception cref="NotImplementedException"></exception>
  703. public async Task<Result> _Del(int id, int userId)
  704. {
  705. Result result = new Result() { Code = -2, Msg = "删除失败!" };
  706. var ccpInfo = await _sqlSugar.Queryable<Grp_CreditCardPayment>()
  707. .FirstAsync(x => x.CId == id && x.CTable == 98 && x.IsDel == 0);
  708. if (ccpInfo == null) return new Result(-2, "数据不存在,不可删除!");
  709. var auditStatus = new List<int>() {
  710. 1, // 已通过
  711. };
  712. if (auditStatus.Any(x => x == ccpInfo.IsAuditGM)) return new Result(-2, "该费用已手动审核,不可删除!");
  713. if (ccpInfo.IsPay == 1) return new Result(-2, "该费用已付款,不可删除!");
  714. _sqlSugar.BeginTran();
  715. var del = await _sqlSugar.Updateable<Grp_DecreasePayments>()
  716. .SetColumns(it => new Grp_DecreasePayments()
  717. {
  718. IsDel = 1,
  719. DeleteUserId = userId,
  720. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  721. }).Where(it => it.Id == id)
  722. .ExecuteCommandAsync();
  723. if (del > 0)
  724. {
  725. var del1 = await _sqlSugar.Updateable<Grp_CreditCardPayment>()
  726. .SetColumns(a => new Grp_CreditCardPayment()
  727. {
  728. IsDel = 1,
  729. DeleteUserId = userId,
  730. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  731. })
  732. .Where(a => a.CId == id && a.CTable == 98)
  733. .ExecuteCommandAsync();
  734. if (del1 > 0)
  735. {
  736. _sqlSugar.CommitTran();
  737. result.Code = 0;
  738. result.Msg = "删除成功!";
  739. return result;
  740. }
  741. }
  742. _sqlSugar.RollbackTran();
  743. return result;
  744. }
  745. }
  746. }