DecreasePaymentsRepository.cs 41 KB

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