StatisticsController.cs 109 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998
  1. using Microsoft.AspNetCore.Mvc;
  2. using NPOI.POIFS.Properties;
  3. using NPOI.SS.Formula.Functions;
  4. using OASystem.API.OAMethodLib;
  5. using OASystem.Domain.Dtos.Groups;
  6. using OASystem.Domain.Dtos.Statistics;
  7. using OASystem.Domain.Entities.Financial;
  8. using OASystem.Domain.ViewModels.Financial;
  9. using OASystem.Domain.ViewModels.Groups;
  10. using OASystem.Domain.ViewModels.Statistics;
  11. using OASystem.Infrastructure.Repositories.Groups;
  12. using static OpenAI.GPT3.ObjectModels.SharedModels.IOpenAiModels;
  13. using System;
  14. using OASystem.Domain.Entities.Customer;
  15. using System.Collections.Generic;
  16. using Microsoft.AspNetCore.Mvc.RazorPages;
  17. using Microsoft.VisualBasic;
  18. using OASystem.Domain.Entities.Groups;
  19. using Microsoft.Extensions.DependencyInjection;
  20. using Aspose.Words.Lists;
  21. using static OASystem.API.OAMethodLib.GeneralMethod;
  22. using TypeInfo = OASystem.Domain.ViewModels.Statistics.TypeInfo;
  23. using System.Net.NetworkInformation;
  24. namespace OASystem.API.Controllers
  25. {
  26. /// <summary>
  27. /// 统计模块
  28. /// </summary>
  29. [Route("api/[controller]")]
  30. [ApiController]
  31. public class StatisticsController : ControllerBase
  32. {
  33. private readonly int _decimalPlaces;
  34. private readonly IMapper _mapper;
  35. private readonly SqlSugarClient _sqlSugar;
  36. private readonly DelegationInfoRepository _groupRep;
  37. private readonly SetDataRepository _setDataRep;
  38. private readonly TeamRateRepository _teamRateRep;
  39. /// <summary>
  40. /// Init
  41. /// </summary>
  42. /// <param name="mapper"></param>
  43. /// <param name="sqlSugar"></param>
  44. /// <param name="groupRep"></param>
  45. /// <param name="setDataRep"></param>
  46. public StatisticsController(IMapper mapper, SqlSugarClient sqlSugar, DelegationInfoRepository groupRep, SetDataRepository setDataRep, TeamRateRepository teamRate)
  47. {
  48. _mapper = mapper;
  49. _groupRep = groupRep;
  50. _setDataRep = setDataRep;
  51. _sqlSugar = sqlSugar;
  52. _teamRateRep = teamRate;
  53. }
  54. #region 团组报表
  55. /// <summary>
  56. /// 团组报表
  57. /// Items
  58. /// </summary>
  59. /// <param name="_dto">团组列表请求dto</param>
  60. /// <returns></returns>
  61. [HttpPost("PostGroupStatementItems")]
  62. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  63. public async Task<IActionResult> PostGroupStatementItems(GroupStatementItemsDto _dto)
  64. {
  65. #region 参数验证
  66. if (_dto.UserId < 1) return Ok(JsonView(false, "员工Id为空"));
  67. if (_dto.PageId < 1) return Ok(JsonView(false, "页面Id为空"));
  68. PageFunAuthViewBase pageFunAuthView = new PageFunAuthViewBase();
  69. #region 页面操作权限验证
  70. pageFunAuthView = await GeneralMethod.PostUserPageFuncDatas(_dto.UserId, _dto.PageId);
  71. if (pageFunAuthView.CheckAuth == 0) return Ok(JsonView(false, "您没有查看权限"));
  72. #endregion
  73. #endregion
  74. if (_dto.PortType == 1 || _dto.PortType == 2 || _dto.PortType == 3) // web/Android/IOS
  75. {
  76. string sqlWhere = string.Empty;
  77. if (_dto.IsSure == 0) //未完成
  78. {
  79. sqlWhere += string.Format(@" And IsSure = 0");
  80. }
  81. else if (_dto.IsSure == 1) //已完成
  82. {
  83. sqlWhere += string.Format(@" And IsSure = 1");
  84. }
  85. if (!string.IsNullOrEmpty(_dto.SearchCriteria))
  86. {
  87. string tj = _dto.SearchCriteria;
  88. sqlWhere += string.Format(@"And (ssd.Name Like '%{0}%' Or TeamName Like '%{1}%' Or ClientName Like '%{2}%' Or ClientName Like '%{3}%' Or su.CnName Like '%{4}%')",
  89. tj, tj, tj, tj, tj);
  90. }
  91. string sql = string.Format(@"Select row_number() over(order by gdi.VisitDate Desc) as Row_Number,
  92. gdi.Id,TourCode,ssd1.Id TeamLevId,ssd1.Name TeamLev,TeamName,
  93. ClientName,ClientUnit,VisitDate,ssd.Id TeamTypeId, ssd.Name TeamType,
  94. VisitDays,VisitPNumber,su.CnName JietuanOperator,IsSure,gdi.CreateTime,
  95. pr.LastCollectionTime
  96. From Grp_DelegationInfo gdi
  97. Left Join Sys_SetData ssd On gdi.TeamDid = ssd.Id
  98. Left Join Sys_SetData ssd1 On gdi.TeamLevSId = ssd1.Id
  99. Left Join Sys_Users su On gdi.JietuanOperator = su.Id
  100. Left Join (
  101. SELECT Diid, MAX(CreateTime) LastCollectionTime
  102. FROM Fin_ProceedsReceived
  103. Where IsDel = 0
  104. GROUP BY Diid
  105. ) pr On gdi.Id = pr.Diid
  106. Where gdi.IsDel = 0 {0} ", sqlWhere);
  107. RefAsync<int> total = 0;//REF和OUT不支持异步,想要真的异步这是最优解
  108. var _DelegationList = await _sqlSugar.SqlQueryable<GroupStatementItemView>(sql).ToPageListAsync(_dto.PageIndex, _dto.PageSize, total);//ToPageAsync
  109. var _view = new
  110. {
  111. PageFuncAuth = pageFunAuthView,
  112. Data = _DelegationList
  113. };
  114. return Ok(JsonView(true, "查询成功!", _view, total));
  115. }
  116. else
  117. {
  118. return Ok(JsonView(false, "查询失败"));
  119. }
  120. }
  121. /// <summary>
  122. /// 团组报表
  123. /// Details
  124. /// </summary>
  125. /// <param name="_dto">团组列表请求dto</param>
  126. /// <returns></returns>
  127. [HttpPost("PostGroupStatementDetails")]
  128. //[JsonConverter(typeof(DecimalConverter), 2)]
  129. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  130. public async Task<IActionResult> PostGroupStatementDetails(GroupStatementDetailsDto _dto)
  131. {
  132. /*
  133. * 团组报表计算方式
  134. * 当前总支出 = 团组支出.Sum() + 超支费用.Sum()
  135. * 应收金额 = 应收表.Sum()
  136. * 已收金额 = 已收表.Sum()
  137. * 应收利润(应收-支出) = 应收金额 - 收款退还 - 当前总支出
  138. * 已收利润(已收-支出) = 已收金额 - 收款退还 - 当前总支出
  139. *
  140. */
  141. #region 参数验证
  142. if (_dto.UserId < 1) return Ok(JsonView(false, "员工Id为空"));
  143. if (_dto.PageId < 1) _dto.PageId = 38; //团组报表页面Id
  144. if (_dto.DiId < 1) return Ok(JsonView(false, "团组Id为空"));
  145. PageFunAuthViewBase pageFunAuthView = new PageFunAuthViewBase();
  146. #region 页面操作权限验证
  147. pageFunAuthView = await GeneralMethod.PostUserPageFuncDatas(_dto.UserId, _dto.PageId);
  148. if (pageFunAuthView.CheckAuth == 0) return Ok(JsonView(false, "您没有查看权限"));
  149. #endregion
  150. #endregion
  151. if (_dto.PortType == 1 || _dto.PortType == 2 || _dto.PortType == 3) // web/Android/IOS
  152. {
  153. GroupStatementDetailsView _view = new GroupStatementDetailsView();
  154. #region 费用类型 币种,转账,客户信息
  155. List<Sys_SetData> _setDatas = await _sqlSugar.Queryable<Sys_SetData>().Where(it => it.IsDel == 0).ToListAsync();
  156. var _clientDatas = await _sqlSugar.Queryable<Crm_DeleClient>().Where(it => it.IsDel == 0).ToListAsync();
  157. #endregion
  158. #region 团组收入
  159. GroupIncomeView _giView = new GroupIncomeView();
  160. /*
  161. * 应收报表
  162. */
  163. decimal frTotalAmount = 0.00M;//应收总金额
  164. string _frSql = string.Format(@"Select fr.Id,fr.Diid,fr.PriceName,fr.Price,fr.Count,fr.Unit,fr.Currency,
  165. sd.Name As CurrencyCode,sd.Remark As CurrencyName,fr.Rate,fr.ItemSumPrice,fr.CreateTime
  166. From Fin_ForeignReceivables fr
  167. Left Join Sys_SetData sd On fr.Currency = sd.Id
  168. Where fr.IsDel = 0 And fr.Diid = {0} Order By CreateTime", _dto.DiId);
  169. List<Gsd_ForeignReceivablesView> _frViews = await _sqlSugar.SqlQueryable<Gsd_ForeignReceivablesView>(_frSql).ToListAsync();
  170. frTotalAmount = _frViews.Sum(it => it.ItemSumPrice);
  171. _giView.Receivables = _frViews;
  172. _giView.ReceivableStr = string.Format(@"应收款合计:{0} CNY(人民币)", frTotalAmount.ConvertToDecimal1().ToString("#0.00"));
  173. /*
  174. * 已收报表
  175. */
  176. decimal prTotalAmount = 0.00M;//已收总金额
  177. string _prSql = string.Format(@"Select pr.Id,pr.Diid,pr.SectionTime As SectionTimeDt,pr.Price,pr.Currency,
  178. sd1.Name As CurrencyCode,sd1.Remark As CurrencyName,pr.Client,
  179. pr.ReceivablesType,sd2.Name As ReceivablesTypeName,pr.Remark,pr.CreateTime
  180. From Fin_ProceedsReceived pr
  181. Left Join Sys_SetData sd1 On pr.Currency = sd1.Id
  182. Left Join Sys_SetData sd2 On pr.ReceivablesType = sd2.Id
  183. Where pr.IsDel = 0 and pr.Diid = {0} Order By CreateTime", _dto.DiId);
  184. List<Gsd_ProceedsReceivedView> _prViews = await _sqlSugar.SqlQueryable<Gsd_ProceedsReceivedView>(_prSql).ToListAsync();
  185. prTotalAmount = _prViews.Sum(it => it.Price);
  186. _giView.ProceedsReceivedViews = _prViews;
  187. _giView.ProceedsReceivedStr = string.Format(@$"应收合计:{frTotalAmount.ToString("#0.00")} CNY 已收款合计:{prTotalAmount.ConvertToDecimal1().ToString("#0.00")} CNY");
  188. /*
  189. * 超支费用
  190. */
  191. decimal exTotalAmount = 0.00M;
  192. string _ecSql = string.Format(@"Select gec.Id As GECId,gec.DiId As GECDiId,gec.PriceName,ccp.PayMoney,sd1.Name As PaymentCurrency,
  193. ccp.RMBPrice As CNYPrice,ccp.DayRate,ccp.Payee,ccp.OrbitalPrivateTransfer,sd2.Name As PayWay,
  194. sd3.Name As CardType,ccp.IsPay,u.CnName As Applicant,gec.CreateTime
  195. From OA2023DB.dbo.Fin_GroupExtraCost gec
  196. Left Join Grp_CreditCardPayment ccp On gec.Id = ccp.CId
  197. Left Join Sys_SetData sd1 On ccp.PaymentCurrency = sd1.Id
  198. Left Join Sys_SetData sd2 On ccp.PayDId = sd2.Id
  199. Left Join Sys_SetData sd3 On ccp.CTDId = sd3.Id
  200. Left Join Sys_Users u On ccp.CreateUserId = u.Id
  201. Where ccp.IsDel = 0 And ccp.CTable = 1015 And ccp.IsAuditGM = 1 And ccp.IsPay = 1 And ccp.DiId = {0} Order By CreateTime", _dto.DiId);
  202. List<Gsd_ExtraCostsView> _ExtraCostsViews = await _sqlSugar.SqlQueryable<Gsd_ExtraCostsView>(_ecSql).ToListAsync();
  203. #region 超支费用 - 模拟数据
  204. //if (_ExtraCostsViews.Count < 1)
  205. //{
  206. // _ExtraCostsViews.Add(new Gsd_ExtraCostsView()
  207. // {
  208. // GECId = 0,
  209. // GECDiId = 2334,
  210. // PriceName = "模拟数据-超支费用名称",
  211. // PayMoney = 1000.00M,
  212. // PaymentCurrency = "CNY",
  213. // DayRate = 1.0000M,
  214. // CNYPrice = 1000.00M,
  215. // Payee = "模拟数据-超支费用收款方",
  216. // OrbitalPrivateTransfer = 1,
  217. // PayWay = "刷卡",
  218. // CardType = "招行卡",
  219. // IsPay = 1,
  220. // Applicant = "刘华举"
  221. // });
  222. // _ExtraCostsViews.Add(new Gsd_ExtraCostsView()
  223. // {
  224. // GECId = 0,
  225. // GECDiId = 2334,
  226. // PriceName = "模拟数据-超支费用名称",
  227. // PayMoney = 1000.00M,
  228. // PaymentCurrency = "CNY",
  229. // DayRate = 1.0000M,
  230. // CNYPrice = 1000.00M,
  231. // Payee = "模拟数据-超支费用收款方",
  232. // OrbitalPrivateTransfer = 1,
  233. // PayWay = "刷卡",
  234. // CardType = "招行卡",
  235. // IsPay = 1,
  236. // Applicant = "刘华举"
  237. // });
  238. //}
  239. #endregion
  240. exTotalAmount = _ExtraCostsViews.Sum(it => it.CNYPrice);
  241. _giView.ExtraCostsViews = _ExtraCostsViews;
  242. _giView.ExtraCostsStr = string.Format(@"人民币总费用:{0} CNY", exTotalAmount.ConvertToDecimal1().ToString("#0.00"));
  243. /*
  244. * 收款退还
  245. */
  246. decimal promTotalAmount = 0.00M;// 收款退还总金额
  247. List<Gsd_PaymentRefundAndOtherMoneyView> _promView = new List<Gsd_PaymentRefundAndOtherMoneyView>();
  248. //删除了 And prom.PriceType = 1
  249. string _ropSql = string.Format(@"Select u.CnName As Appliction,prom.Id As PrId,prom.DiId As PrDiId,prom.Price As PrPrice,
  250. prom.PriceName AS PrPriceName,prom.CurrencyId As PrCurrencyId,
  251. prom.PayType As PrPayType,prom.PriceType As PrPriceType,ccp.*,prom.CreateTime As PrCreateTime
  252. From Fin_PaymentRefundAndOtherMoney prom
  253. Left Join Grp_CreditCardPayment ccp On prom.DiId = ccp.DIId And prom.Id = ccp.CId
  254. Left Join Sys_Users u On ccp.CreateUserId = u.Id
  255. Where prom.IsDel = 0 And prom.PayType = 1 And ccp.CTable = 285
  256. And ccp.IsAuditGM = 1 And ccp.IsPay = 1
  257. And prom.DiId = {0} Order By PrCreateTime", _dto.DiId);
  258. var _promDatas = await _sqlSugar.SqlQueryable<Gsd_PaymentRefundAndOtherMoneyDataSource1View>(_ropSql).ToListAsync();
  259. foreach (var ropItem in _promDatas)
  260. {
  261. string thisCueencyCode = "Unknown";
  262. string thisCueencyName = "Unknown";
  263. var currency = _setDatas.Where(it => it.Id == ropItem.PaymentCurrency).FirstOrDefault();
  264. if (currency != null)
  265. {
  266. thisCueencyCode = currency.Name;
  267. thisCueencyName = currency.Remark;
  268. }
  269. string orbitalPrivateTransferStr = "Unknown";
  270. var orbitalPrivateTransfer = _setDatas.Where(it => it.Id == ropItem.OrbitalPrivateTransfer).FirstOrDefault();
  271. if (orbitalPrivateTransfer != null)
  272. {
  273. orbitalPrivateTransferStr = orbitalPrivateTransfer.Name;
  274. }
  275. string payStr = "Unknown";
  276. var pay = _setDatas.Where(it => it.Id == ropItem.PayDId).FirstOrDefault();
  277. if (pay != null)
  278. {
  279. payStr = pay.Name;
  280. }
  281. Gsd_PaymentRefundAndOtherMoneyView gsd_PaymentRefund = new Gsd_PaymentRefundAndOtherMoneyView()
  282. {
  283. Id = ropItem.Id,
  284. DiId = ropItem.DIId,
  285. PriceName = ropItem.PrPriceName,
  286. PayCurrencyCode = thisCueencyCode,
  287. PayCurrencyName = thisCueencyName,
  288. Price = ropItem.PrPrice,
  289. CNYPrice = ropItem.RMBPrice,
  290. ThisRate = ropItem.DayRate,
  291. Payee = ropItem.Payee,
  292. PayTime = ropItem.AuditGMDate,
  293. OrbitalPrivateTransfer = ropItem.OrbitalPrivateTransfer,
  294. PayType = payStr,
  295. IsPay = ropItem.IsPay,
  296. Applicant = ropItem.Appliction
  297. };
  298. _promView.Add(gsd_PaymentRefund);
  299. }
  300. #region 收款退还 - 模拟数据
  301. //if (_promView.Count < 1)
  302. //{
  303. // _promView.Add(new Gsd_PaymentRefundAndOtherMoneyView()
  304. // {
  305. // Id = 0,
  306. // DiId = 2334,
  307. // PriceName = "模拟数据-费用名称",
  308. // PayCurrencyCode = "CNY",
  309. // PayCurrencyName = "人民币",
  310. // Price = 1000.00M,
  311. // CNYPrice = 1000.00M,
  312. // ThisRate = 1.00M,
  313. // Payee = "模拟数据-收款方",
  314. // PayTime = "2023-01-01 15:20:01",
  315. // OrbitalPrivateTransfer = 1,
  316. // PayType = "刷卡",
  317. // IsPay = 1,
  318. // Applicant = "刘华举"
  319. // });
  320. // _promView.Add(new Gsd_PaymentRefundAndOtherMoneyView()
  321. // {
  322. // Id = 0,
  323. // DiId = 2334,
  324. // PriceName = "模拟数据-费用名称",
  325. // PayCurrencyCode = "CNY",
  326. // PayCurrencyName = "人民币",
  327. // Price = 1000.00M,
  328. // CNYPrice = 1000.00M,
  329. // ThisRate = 1.00M,
  330. // Payee = "模拟数据-收款方",
  331. // PayTime = "2023-01-01 15:20:01",
  332. // OrbitalPrivateTransfer = 1,
  333. // PayType = "刷卡",
  334. // IsPay = 1,
  335. // Applicant = "刘华举"
  336. // });
  337. //}
  338. #endregion
  339. promTotalAmount = _promView.Sum(it => it.CNYPrice);
  340. _giView.PaymentRefundAndOtherMoneyViews = _promView;
  341. _giView.PaymentRefundAndOtherMoneyStr = string.Format(@"人民币总费用:{0} CNY", promTotalAmount.ConvertToDecimal1().ToString("#0.00"));
  342. decimal BalancePayment = frTotalAmount - prTotalAmount + promTotalAmount;
  343. _view.GroupIncome = _giView;
  344. _view.GroupIncomeStr = string.Format(@"<span style='color:red;'>剩余尾款:{0} CNY(包含了收款退还费用数据)</span>", BalancePayment.ConvertToDecimal1().ToString("#0.00"));
  345. #endregion
  346. #region 团组支出
  347. GroupExpenditureView _geView = new GroupExpenditureView();
  348. #region 酒店预定费用
  349. List<GroupHotelFeeView> groupHotelFeeViews = new List<GroupHotelFeeView>();
  350. string hotelFeeSql = string.Format(@"Select hr.Id As HrId,hr.DiId As HrDiId,hr.City,hr.HotelName,hr.CheckInDate,hr.CheckOutDate,
  351. sd1.Name As PaymentCurrency,hr.SingleRoomPrice,hr.SingleRoomCount,hr.DoubleRoomPrice,
  352. hr.DoubleRoomCount,hr.SuiteRoomPrice,hr.SuiteRoomCount,hr.OtherRoomPrice,hr.OtherRoomCount,
  353. hr.BreakfastPrice,sd4.Name As BreakfastCurrency,hr.Isoppay,hr.GovernmentRent,
  354. sd5.Name As GovernmentRentCurrency,hr.CityTax,sd6.Name As CityTaxCurrency,
  355. ccp.PayMoney,ccp.RMBPrice As CNYPrice,ccp.DayRate,ccp.Payee,ccp.OrbitalPrivateTransfer,
  356. sd2.Name As PayWay,sd3.Name As CardType,ccp.IsPay,u.CnName As Applicant
  357. From Grp_HotelReservations hr
  358. Left Join Grp_CreditCardPayment ccp On hr.Id = ccp.CId
  359. Left Join Sys_SetData sd1 On ccp.PaymentCurrency = sd1.Id
  360. Left Join Sys_SetData sd2 On ccp.PayDId = sd2.Id
  361. Left Join Sys_SetData sd3 On ccp.CTDId = sd3.Id
  362. Left Join Sys_Users u On ccp.CreateUserId = u.Id
  363. Left Join Sys_SetData sd4 On hr.BreakfastCurrency = sd4.Id
  364. Left Join Sys_SetData sd5 On hr.GovernmentRentCurrency = sd5.Id
  365. Left Join Sys_SetData sd6 On hr.CityTaxCurrency = sd6.Id
  366. Where hr.IsDel = 0 And ccp.IsDel = 0 And ccp.CTable = 76 And ccp.IsAuditGM = 1 And ccp.PayMoney<> 0 And hr.DiId = {0}
  367. Order By CheckInDate Asc", _dto.DiId);
  368. groupHotelFeeViews = await _sqlSugar.SqlQueryable<GroupHotelFeeView>(hotelFeeSql).ToListAsync();
  369. decimal HotelCNYTotalPrice = 0.00M;
  370. var teamRateData = await _teamRateRep.PostGroupRateInfoByDiId(_dto.DiId);
  371. foreach (var item in groupHotelFeeViews)
  372. {
  373. HotelCNYTotalPrice += item.CNYPrice;
  374. item.PayMoney = item.PayMoney.ConvertToDecimal1();
  375. item.CNYPrice = item.CNYPrice.ConvertToDecimal1();
  376. string currencyRateStr = "";
  377. List<string> currencys = new List<string>();
  378. if (!string.IsNullOrEmpty(item.BreakfastCurrency)) currencys.Add(item.BreakfastCurrency);
  379. if (!string.IsNullOrEmpty(item.GovernmentRentCurrency)) currencys.Add(item.GovernmentRentCurrency);
  380. if (!string.IsNullOrEmpty(item.CityTaxCurrency)) currencys.Add(item.CityTaxCurrency);
  381. if (!string.IsNullOrEmpty(item.PaymentCurrency)) currencys.Add(item.PaymentCurrency);
  382. currencyRateStr = await GeneralMethod.PostGroupRateByCTableAndCurrency(teamRateData, 76, currencys);
  383. item.CurrencyRateStr = currencyRateStr;
  384. }
  385. _geView.GroupHotelFeeViews = groupHotelFeeViews;
  386. _geView.GroupHotelFeeStr = string.Format(@"人民币总费用:{0} CNY", HotelCNYTotalPrice.ToString("#0.00"));
  387. #endregion
  388. #region 地接费用
  389. List<GroupCTGGRFeeView> groupCTGGRFeeViews = new List<GroupCTGGRFeeView>();
  390. string CTGGRFeeSql = string.Format(@"Select ctggr.Id As CTGGRId,ctggr.DiId As CTGGRDiId,ctggr.Area,ctggrc.*,ctggrc.Price As PayMoney,
  391. sd2.name As PaymentCurrency,ccp.PayPercentage,
  392. (ctggrc.Price / (ccp.PayPercentage / 100)) As AmountPaid,
  393. (ctggrc.Price / (ccp.PayPercentage / 100) - ctggrc.Price) As BalancePayment,
  394. ccp.DayRate,(ctggrc.Price * ccp.DayRate) As CNYPrice,ccp.Payee,ccp.AuditGMDate,
  395. ccp.OrbitalPrivateTransfer,sd1.Name As PayWay,ccp.IsPay,u.CnName As Applicant,ctggr.CreateTime
  396. From Grp_CarTouristGuideGroundReservations ctggr
  397. Left Join ( Select cggrc.CTGGRId,sd1.Name As PriceName,cggrc.Price,sd2.Name As PriceCurrency,
  398. cggrc.PriceContent
  399. From Grp_CarTouristGuideGroundReservationsContent cggrc
  400. Left Join Sys_SetData sd1 On cggrc.SId = sd1.Id
  401. Left Join Sys_SetData sd2 On cggrc.Currency = sd2.Id
  402. Where cggrc.ISdel = 0 And cggrc.Price != 0.00
  403. ) ctggrc On ctggr.Id = ctggrc.CTGGRId
  404. Left Join Grp_CreditCardPayment ccp On ccp.IsDel = 0 And ccp.CTable = 79 And ctggr.Id = ccp.CId
  405. Left Join Sys_SetData sd1 On ccp.PayDId = sd1.Id
  406. Left Join Sys_SetData sd2 On ccp.PaymentCurrency = sd2.Id
  407. Left Join Sys_Users u On ccp.CreateUserId = u.Id
  408. Where ctggr.IsDel = 0 And ccp.IsAuditGM = 1 And ctggr.DiId = {0}
  409. Order By CreateTime", _dto.DiId);
  410. groupCTGGRFeeViews = await _sqlSugar.SqlQueryable<GroupCTGGRFeeView>(CTGGRFeeSql).ToListAsync();
  411. string CTGGRFeeStr = "";
  412. decimal CTGGRCNYTotalPrice = 0.00M;
  413. //按1 地区,2 币种,3 汇率 分组计算
  414. var groupCTGGRFeeDatas = groupCTGGRFeeViews.GroupBy(it => it.Area);
  415. foreach (var ctggfr in groupCTGGRFeeDatas)
  416. {
  417. var ctggfr_curr = ctggfr.GroupBy(it => it.PaymentCurrency);
  418. if (ctggfr_curr.Count() > 0)
  419. {
  420. foreach (var curr in ctggfr_curr)
  421. {
  422. var ctggfr_rate = curr.GroupBy(it => it.DayRate);
  423. if (ctggfr_rate.Count() > 0)
  424. {
  425. foreach (var rate in ctggfr_rate)
  426. {
  427. CTGGRFeeStr += string.Format(@$"{ctggfr.Key} 总费用:{rate.Sum(it => it.AmountPaid).ToString("#0.00")}
  428. {rate.FirstOrDefault()?.PaymentCurrency}(人民币:
  429. {rate.Sum(it => Convert.ToDecimal(it.CNYPrice.ToString("#0.00"))).ToString("#0.00")} CNY 当时支付汇率:
  430. {rate.FirstOrDefault()?.DayRate.ToString("#0.0000")})\r\n");
  431. CTGGRCNYTotalPrice += rate.Sum(it => Convert.ToDecimal(it.CNYPrice.ToString("#0.00")));
  432. }
  433. }
  434. else
  435. {
  436. CTGGRFeeStr += string.Format(@$"{ctggfr.Key} 总费用:{curr.Sum(it => it.AmountPaid).ToString("#0.00")}
  437. {curr.FirstOrDefault()?.PaymentCurrency}(人民币:
  438. {curr.Sum(it => Convert.ToDecimal(it.CNYPrice.ToString("#0.00"))).ToString("#0.00")} CNY 当时支付汇率:
  439. {curr.FirstOrDefault()?.DayRate.ToString("#0.0000")})\r\n");
  440. CTGGRCNYTotalPrice += curr.Sum(it => Convert.ToDecimal(it.CNYPrice.ToString("#0.00")));
  441. }
  442. }
  443. }
  444. else
  445. {
  446. CTGGRFeeStr += string.Format(@$"{ctggfr.Key} 总费用:{ctggfr.Sum(it => it.AmountPaid).ToString("#0.00")}
  447. {ctggfr.FirstOrDefault()?.PaymentCurrency}(人民币:
  448. {ctggfr.Sum(it => Convert.ToDecimal(it.CNYPrice.ToString("#0.00"))).ToString("#0.00")} CNY 当时支付汇率:
  449. {ctggfr.FirstOrDefault()?.DayRate.ToString("#0.0000")})\r\n");
  450. CTGGRCNYTotalPrice += ctggfr.Sum(it => Convert.ToDecimal(it.CNYPrice.ToString("#0.00")));
  451. }
  452. }
  453. foreach (var item in groupCTGGRFeeViews)
  454. {
  455. if (!string.IsNullOrEmpty(item.AuditGMDate))
  456. {
  457. item.AuditGMDate = Convert.ToDateTime(item.AuditGMDate).ToString("yyyy-MM-dd HH:mm:ss");
  458. }
  459. //CTGGRFeeStr += string.Format(@"{0} 总费用:{1} {2}(人民币:{3} CNY 当时支付汇率:{4})\r\n",
  460. // item.Area, item.AmountPaid.ConvertToDecimal1().ToString("#0.00"), item.PaymentCurrency, item.CNYPrice.ToString("#0.0000"), item.DayRate.ToString("#0.0000"));
  461. //CTGGRCNYTotalPrice += item.CNYPrice;
  462. }
  463. _geView.GroupCTGGRFeeViews = groupCTGGRFeeViews;
  464. _geView.GroupCTGGRFeeStr = string.Format(@"{0}人民币总费用:{1} CNY", CTGGRFeeStr, CTGGRCNYTotalPrice.ToString("#0.00"));
  465. #endregion
  466. #region 机票预订费用
  467. List<GroupAirFeeView> groupAirFeeViews = new List<GroupAirFeeView>();
  468. string groupAirFeeSql = string.Format(@"Select atr.Id As AirId,atr.DIId As AirDiId,atr.FlightsCode,atr.FlightsCity,sd4.Name As AirTypeName,
  469. atr.FlightsDate,atr.FlightsTime,atr.ClientName,atr.ClientNum,ccp.PayMoney,
  470. sd1.Name As PayMoneyCurrency,ccp.RMBPrice As CNYPrice,ccp.DayRate,ccp.Payee,ccp.AuditGMDate,
  471. ccp.OrbitalPrivateTransfer,sd2.Name As PayWay,sd3.Name As CardType,ccp.IsPay,u.CnName As Applicant,atr.CreateTime,
  472. atr.CType
  473. From Grp_AirTicketReservations atr
  474. Left Join Grp_CreditCardPayment ccp On ccp.isdel = 0 And ccp.CTable = 85 And atr.Id = ccp.CId
  475. Left Join Sys_SetData sd1 On ccp.PaymentCurrency = sd1.Id
  476. Left Join Sys_SetData sd2 On ccp.PayDId = sd2.Id
  477. Left Join Sys_SetData sd3 On ccp.CTDId = sd3.Id
  478. Left Join Sys_SetData sd4 On atr.CType = sd4.Id
  479. Left Join Sys_Users u On ccp.CreateUserId = u.Id
  480. Where atr.IsDel = 0 And ccp.IsAuditGM = 1 And atr.DiId = {0} Order By CreateTime", _dto.DiId);
  481. groupAirFeeViews = await _sqlSugar.SqlQueryable<GroupAirFeeView>(groupAirFeeSql).ToListAsync();
  482. string str = "";
  483. List<dynamic> airClientPris = new List<dynamic>();
  484. decimal AirCNYTotalPrice = 0.00M;
  485. decimal JJCCNYTotalPrice = 0.00M, JJCPeopleNum = 0.00M, JJCAveragePrice = 0.00M;
  486. decimal GWCCNYTotalPrice = 0.00M, GWCPeopleNum = 0.00M, GWCAveragePrice = 0.00M;
  487. //if (groupAirFeeViews.Count > 0)
  488. //{
  489. // JJCCNYTotalPrice = groupAirFeeViews.Where(it => it.AirTypeName.Equals("经济舱")).Sum(it => it.CNYPrice);
  490. // JJCPeopleNum = groupAirFeeViews.Where(it => it.AirTypeName.Equals("经济舱")).Sum(it => it.ClientNum);
  491. // JJCAveragePrice = (JJCCNYTotalPrice / JJCPeopleNum).ConvertToDecimal1();
  492. // GWCCNYTotalPrice = groupAirFeeViews.Where(it => it.AirTypeName.Equals("公务舱")).Sum(it => it.CNYPrice);
  493. // GWCPeopleNum = groupAirFeeViews.Where(it => it.AirTypeName.Equals("公务舱")).Sum(it => it.ClientNum);
  494. // GWCAveragePrice = (GWCCNYTotalPrice / GWCPeopleNum).ConvertToDecimal1();
  495. //}
  496. int Index = 0;
  497. foreach (var item in groupAirFeeViews)
  498. {
  499. if (item.AirId > 2924)
  500. {
  501. string itemClientName = "";
  502. if (!string.IsNullOrEmpty(item.ClientName))
  503. {
  504. System.Text.RegularExpressions.Match m_EnName = Regex.Match(item.ClientName, @"[A-Za-z]+");
  505. System.Text.RegularExpressions.Match m_ZHName = Regex.Match(item.ClientName, @"[\u4e00-\u9fa5]");
  506. if (m_EnName.Success || m_ZHName.Success)
  507. {
  508. itemClientName = item.ClientName;
  509. continue;
  510. }
  511. string[] clientIds = new string[] { };
  512. if (item.ClientName.Contains(','))
  513. {
  514. clientIds = item.ClientName.Split(',');
  515. }
  516. else
  517. {
  518. clientIds = new string[] { item.ClientName };
  519. }
  520. if (clientIds.Length > 0)
  521. {
  522. int[] output = Array.ConvertAll<string, int>(clientIds, delegate (string s) { return int.Parse(s); });
  523. if (output.Contains(-1))
  524. {
  525. itemClientName += $@"行程单";
  526. output = output.Where(val => val != -1).ToArray();
  527. }
  528. var clients = _clientDatas.Where(it => output.Contains(it.Id)).ToList();
  529. decimal unitCost = 0.00M;
  530. unitCost = (item.PayMoney / item.ClientNum).ConvertToDecimal1();
  531. int clienIndex = 1;
  532. foreach (var client in clients)
  533. {
  534. airClientPris.Add(new
  535. {
  536. CnName = client.LastName + client.FirstName,
  537. EnName = client.Pinyin,
  538. Price = unitCost,
  539. AirType = item.AirTypeName
  540. });
  541. string six = "";
  542. if (client.Sex == 0) six = "Mr";
  543. else if (client.Sex == 1) six = "Ms";
  544. itemClientName += string.Format(@"{0}.{1} {2};", clienIndex, client.LastName + client.FirstName, six);
  545. clienIndex++;
  546. }
  547. }
  548. }
  549. item.ClientName = itemClientName;
  550. }
  551. else
  552. {
  553. string clientPinYinName = "";
  554. decimal unitCost = 0.00M;
  555. unitCost = (item.PayMoney / item.ClientNum).ConvertToDecimal1();
  556. Regex r = new Regex("[0-9]");
  557. string name1 = item.ClientName;
  558. name1 = r.Replace(name1, "");
  559. string[] clientNames = name1.Split('.');
  560. for (int i = 0; i < item.ClientNum; i++)
  561. {
  562. string name = "";
  563. if (clientNames.Length > 0)
  564. {
  565. int index = i + 1;
  566. if (index < clientNames.Length)
  567. {
  568. name = clientNames[index].Replace("MR", "").Replace("MS", "").Trim();
  569. if (!string.IsNullOrEmpty(name))
  570. {
  571. airClientPris.Add(new
  572. {
  573. CnName = name,
  574. EnName = name,
  575. Price = unitCost,
  576. AirType = item.AirTypeName
  577. });
  578. }
  579. //if (name.Length > 0)
  580. //{
  581. // string nameLastStr = name[name.Length - 1].ToString();
  582. // if (nameLastStr.IsNumeric())
  583. // {
  584. // name = name.Substring(0, name.Length - 1).Trim();
  585. // }
  586. //}
  587. }
  588. }
  589. clientPinYinName += string.Format(@"{0}.{1}出票价为:{2} CNY;", Index + 1, name, unitCost.ToString("#0.00"));
  590. }
  591. }
  592. if (!string.IsNullOrEmpty(item.AuditGMDate))
  593. {
  594. item.AuditGMDate = Convert.ToDateTime(item.AuditGMDate).ToString("yyyy-MM-dd HH:mm:ss");
  595. }
  596. AirCNYTotalPrice += item.CNYPrice;
  597. }
  598. _geView.GroupAirFeeViews = groupAirFeeViews;
  599. if (airClientPris.Count > 0)
  600. {
  601. var peoplePriStr = "";
  602. var airClientPris1 = airClientPris.GroupBy(item => item.CnName)
  603. .Select(group => group.First())
  604. .ToList();
  605. int airClientPrisIndex = 1;
  606. foreach (var item in airClientPris1)
  607. {
  608. decimal price = 0.00M;
  609. var prices = airClientPris.Where(it => it.CnName == item.CnName).ToList();
  610. foreach (var pri in prices)
  611. {
  612. price += pri.Price;
  613. }
  614. peoplePriStr += $@"{airClientPrisIndex}.{item.EnName}出票价为: {price.ToString("#0.00")} CNY;";
  615. airClientPrisIndex++;
  616. }
  617. if (!string.IsNullOrEmpty(peoplePriStr))
  618. {
  619. str = $@"其中:{peoplePriStr}";
  620. }
  621. //经济舱均价
  622. var airJJCPris = airClientPris.Where(it => it.AirType == "经济舱").ToList();
  623. if (airJJCPris.Count > 0)
  624. {
  625. decimal jjcTotalPrice = 0.00M;
  626. foreach (var item in airJJCPris)
  627. {
  628. jjcTotalPrice += item.Price;
  629. }
  630. decimal jjcPeopleNum = airJJCPris.GroupBy(item => item.CnName)
  631. .Select(group => group.First())
  632. .ToList().Count(); ;
  633. JJCAveragePrice = jjcTotalPrice / jjcPeopleNum;
  634. }
  635. //公务舱均价
  636. var airGWCPris = airClientPris.Where(it => it.AirType == "公务舱").ToList();
  637. if (airGWCPris.Count > 0)
  638. {
  639. decimal gwcTotalPrice = 0.00M;
  640. foreach (var item in airGWCPris)
  641. {
  642. gwcTotalPrice += item.Price;
  643. }
  644. decimal gwcPeopleNum = airGWCPris.GroupBy(item => item.CnName)
  645. .Select(group => group.First())
  646. .ToList().Count();
  647. GWCAveragePrice = gwcTotalPrice / gwcPeopleNum;
  648. }
  649. }
  650. _geView.GroupAirFeeStr = $@"人民币总费用:{AirCNYTotalPrice.ToString("#0.00")} CNY\r\n{str}\r\n经济舱均价为:{JJCAveragePrice.ToString("#0.00")}CNY/人;公务舱均价为:{GWCAveragePrice.ToString("#0.00")}CNY/人;";
  651. #endregion
  652. #region 签证费用
  653. List<GroupVisaFeeView> groupVisaFeeViews = new List<GroupVisaFeeView>();
  654. string groupVisaFeeSql = string.Format(@"Select vi.Id As VisaId,vi.DIId As VisaDiId,vi.VisaClient,ccp.PayMoney,sd1.Name As PayMoneyCurrency,
  655. ccp.DayRate,ccp.Payee,ccp.AuditGMDate,ccp.OrbitalPrivateTransfer,sd2.Name As PayWay,
  656. sd3.Name As CardTypeName,ccp.IsPay,u.CnName As Applicant,vi.CreateTime
  657. From Grp_VisaInfo vi
  658. Left Join Grp_CreditCardPayment ccp On ccp.isdel = 0 And ccp.CTable = 80 And vi.Id = ccp.CId
  659. Left Join Sys_SetData sd1 On ccp.PaymentCurrency = sd1.Id
  660. Left Join Sys_SetData sd2 On ccp.PayDId = sd2.Id
  661. Left Join Sys_SetData sd3 On ccp.CTDId = sd3.Id
  662. Left Join Sys_Users u On ccp.CreateUserId = u.Id
  663. Where vi.IsDel = 0 And ccp.IsAuditGM = 1 And vi.DIId = {0} Order By CreateTime", _dto.DiId);
  664. groupVisaFeeViews = await _sqlSugar.SqlQueryable<GroupVisaFeeView>(groupVisaFeeSql).ToListAsync();
  665. decimal VisaCNYTotalPirce = 0.00M;
  666. foreach (var item in groupVisaFeeViews)
  667. {
  668. string itemClientName = "";
  669. string visaClients = item.VisaClient;
  670. if (!string.IsNullOrEmpty(visaClients))
  671. {
  672. System.Text.RegularExpressions.Match m_EnName = Regex.Match(item.ClientName, @"[A-Za-z]+");
  673. System.Text.RegularExpressions.Match m_ZHName = Regex.Match(item.ClientName, @"[\u4e00-\u9fa5]");
  674. if (m_EnName.Success || m_ZHName.Success)
  675. {
  676. itemClientName = visaClients;
  677. continue;
  678. }
  679. string[] clientIds = new string[] { };
  680. if (visaClients.Contains(','))
  681. {
  682. clientIds = visaClients.Split(',');
  683. }
  684. else
  685. {
  686. clientIds = new string[] { visaClients };
  687. }
  688. if (clientIds.Length > 0)
  689. {
  690. List<int> clientIds1 = new List<int>() { };
  691. foreach (var clientIdStr in clientIds)
  692. {
  693. if (clientIdStr.IsNumeric())
  694. {
  695. clientIds1.Add(int.Parse(clientIdStr));
  696. }
  697. }
  698. if (clientIds1.Count > 0)
  699. {
  700. var clients = _clientDatas.Where(it => clientIds1.Contains(it.Id)).ToList();
  701. foreach (var client in clients)
  702. {
  703. itemClientName += $"{client.LastName + client.FirstName},";
  704. }
  705. }
  706. else
  707. {
  708. itemClientName = visaClients;
  709. }
  710. }
  711. }
  712. if (itemClientName.Length > 0)
  713. {
  714. itemClientName = itemClientName.Substring(0, itemClientName.Length - 1);
  715. }
  716. item.VisaClient = itemClientName;
  717. VisaCNYTotalPirce += item.PayMoney;
  718. if (!string.IsNullOrEmpty(item.AuditGMDate))
  719. {
  720. item.AuditGMDate = Convert.ToDateTime(item.AuditGMDate).ToString("yyyy-MM-dd HH:mm:ss");
  721. }
  722. }
  723. _geView.GroupVisaFeeViews = groupVisaFeeViews;
  724. _geView.GroupVisaFeeStr = string.Format(@"人民币总费用:{0} CNY", VisaCNYTotalPirce.ConvertToDecimal1());
  725. #endregion
  726. #region 邀请/公务活动 CTable = 81
  727. List<GroupInvitationalFeeView> groupInvitationalFeeViews = new List<GroupInvitationalFeeView>();
  728. string groupInvitationalFeeSql = string.Format(@"Select ioa.Id As IOAId,ioa.DiId As IOADiId,ioa.InviterArea,ioa.Inviter,ioa.InviteTime,
  729. ioa.InviteCost,sd3.Name As InviteCurrency,ioa.SendCost,sd4.Name As SendCurrency,ioa.EventsCost,
  730. sd5.Name As EventsCurrency,ioa.TranslateCost,sd6.Name As TranslateCurrency,ccp.PayMoney,
  731. sd7.Name As PaymentCurrency,ccp.RMBPrice As CNYPrice,ccp.Payee,ccp.AuditGMDate,
  732. ccp.OrbitalPrivateTransfer,sd2.Name As PayWay,ccp.IsPay,u.CnName As Applicant,ioa.CreateTime
  733. From Grp_InvitationOfficialActivities ioa
  734. Left Join Grp_CreditCardPayment ccp On ccp.isdel = 0 And ccp.CTable = 81 And ioa.Id = ccp.CId
  735. Left Join Sys_SetData sd1 On ccp.PaymentCurrency = sd1.Id
  736. Left Join Sys_SetData sd2 On ccp.PayDId = sd2.Id
  737. Left Join Sys_SetData sd3 On ioa.InviteCurrency = sd3.Id
  738. Left Join Sys_SetData sd4 On ioa.SendCurrency = sd4.Id
  739. Left Join Sys_SetData sd5 On ioa.EventsCurrency = sd5.Id
  740. Left Join Sys_SetData sd6 On ioa.TranslateCurrency = sd6.Id
  741. Left Join Sys_SetData sd7 On ccp.PaymentCurrency = sd7.Id
  742. Left Join Sys_Users u On ccp.CreateUserId = u.Id
  743. Where ioa.IsDel = 0 And ccp.IsAuditGM = 1 And ioa.Diid = {0} Order By CreateTime", _dto.DiId);
  744. groupInvitationalFeeViews = await _sqlSugar.SqlQueryable<GroupInvitationalFeeView>(groupInvitationalFeeSql).ToListAsync();
  745. #region 邀请/公务活动 - 模拟数据
  746. //if (groupInvitationalFeeViews.Count < 1)
  747. //{
  748. // groupInvitationalFeeViews.Add(new GroupInvitationalFeeView()
  749. // {
  750. // IOAId = 0,
  751. // IOADiId = 2334,
  752. // InviterArea = "模拟数据-邀请方地区",
  753. // Inviter = "模拟数据-邀请方",
  754. // InviteTime = "2023-10-10",
  755. // InviteCost = 100.00M,
  756. // InviteCurrency = "EUR",
  757. // SendCost = 100.00M,
  758. // SendCurrency = "EUR",
  759. // EventsCost = 10000.00M,
  760. // EventsCurrency = "EUR",
  761. // TranslateCost = 300.00M,
  762. // TranslateCurrency = "EUR",
  763. // PayMoney = 10500.00M,
  764. // PaymentCurrency = "EUR",
  765. // CNYPrice = 76765.50M,
  766. // Payee = "模拟数据-收款方",
  767. // AuditGMDate = "2023-12-05",
  768. // OrbitalPrivateTransfer = 1,
  769. // PayWay = "刷卡",
  770. // IsPay = 1,
  771. // Applicant = "刘华举"
  772. // });
  773. // groupInvitationalFeeViews.Add(new GroupInvitationalFeeView()
  774. // {
  775. // IOAId = 0,
  776. // IOADiId = 2334,
  777. // InviterArea = "模拟数据-邀请方地区",
  778. // Inviter = "模拟数据-邀请方",
  779. // InviteTime = "2023-10-10",
  780. // InviteCost = 100.00M,
  781. // InviteCurrency = "EUR",
  782. // SendCost = 100.00M,
  783. // SendCurrency = "EUR",
  784. // EventsCost = 10000.00M,
  785. // EventsCurrency = "EUR",
  786. // TranslateCost = 300.00M,
  787. // TranslateCurrency = "EUR",
  788. // PayMoney = 10500.00M,
  789. // PaymentCurrency = "EUR",
  790. // CNYPrice = 76765.50M,
  791. // Payee = "模拟数据-收款方",
  792. // AuditGMDate = "2023-12-05",
  793. // OrbitalPrivateTransfer = 1,
  794. // PayWay = "刷卡",
  795. // IsPay = 1,
  796. // Applicant = "刘华举"
  797. // });
  798. //}
  799. #endregion
  800. decimal InvitationalCNYTotalPrice = 0.00M;
  801. foreach (var item in groupInvitationalFeeViews)
  802. {
  803. InvitationalCNYTotalPrice += item.CNYPrice;
  804. if (!string.IsNullOrEmpty(item.AuditGMDate))
  805. {
  806. item.AuditGMDate = Convert.ToDateTime(item.AuditGMDate).ToString("yyyy-MM-dd HH:mm:ss");
  807. }
  808. string currencyRateStr = "";
  809. List<string> currencys = new List<string>();
  810. if (!string.IsNullOrEmpty(item.InviteCurrency)) currencys.Add(item.InviteCurrency);
  811. if (!string.IsNullOrEmpty(item.SendCurrency)) currencys.Add(item.SendCurrency);
  812. if (!string.IsNullOrEmpty(item.EventsCurrency)) currencys.Add(item.EventsCurrency);
  813. if (!string.IsNullOrEmpty(item.TranslateCurrency)) currencys.Add(item.TranslateCurrency);
  814. if (!string.IsNullOrEmpty(item.PaymentCurrency)) currencys.Add(item.PaymentCurrency);
  815. currencyRateStr = await GeneralMethod.PostGroupRateByCTableAndCurrency(teamRateData, 81, currencys);
  816. item.CurrencyRateStr = currencyRateStr;
  817. }
  818. _geView.GroupInvitationalFeeViews = groupInvitationalFeeViews;
  819. _geView.GroupInvitationalFeeStr = string.Format(@"人民币总费用:{0} CNY", InvitationalCNYTotalPrice);
  820. #endregion
  821. #region 保险费用
  822. List<GroupInsuranceFeeView> groupInsuranceFeeViews = new List<GroupInsuranceFeeView>();
  823. string groupInsuranceFeeSql = string.Format(@"Select ic.Id As InsuranceId,ic.Diid As InsuranceDiId,ClientName,ccp.PayMoney,ccp.RMBPrice As CNYPrice,
  824. sd1.Name As PayMoneyCurrency,ccp.Payee,ccp.AuditGMDate,ccp.OrbitalPrivateTransfer,
  825. sd2.Name As PayWay,ccp.IsPay,u.CnName As Applicant,ic.CreateTime
  826. From Grp_Customers ic
  827. Left Join Grp_CreditCardPayment ccp On ccp.isdel = 0 And ccp.CTable = 82 And ic.Id = ccp.CId
  828. Left Join Sys_SetData sd1 On ccp.PaymentCurrency = sd1.Id
  829. Left Join Sys_SetData sd2 On ccp.PayDId = sd2.Id
  830. Left Join Sys_Users u On ccp.CreateUserId = u.Id
  831. Where ic.IsDel = 0 And ccp.IsAuditGM = 1 And ic.DiId = {0} Order By CreateTime", _dto.DiId);
  832. groupInsuranceFeeViews = await _sqlSugar.SqlQueryable<GroupInsuranceFeeView>(groupInsuranceFeeSql).ToListAsync();
  833. decimal InsuranceCNYTotalPrice = 0.00M;
  834. foreach (var item in groupInsuranceFeeViews)
  835. {
  836. InsuranceCNYTotalPrice += item.CNYPrice;
  837. string itemClientName = "";
  838. string insClients = item.ClientName;
  839. if (!string.IsNullOrEmpty(insClients))
  840. {
  841. System.Text.RegularExpressions.Match m_EnName = Regex.Match(item.ClientName, @"[A-Za-z]+");
  842. System.Text.RegularExpressions.Match m_ZHName = Regex.Match(item.ClientName, @"[\u4e00-\u9fa5]");
  843. if (m_EnName.Success || m_ZHName.Success)
  844. {
  845. itemClientName = insClients;
  846. continue;
  847. }
  848. string[] clientIds = new string[] { };
  849. if (insClients.Contains(','))
  850. {
  851. clientIds = insClients.Split(',');
  852. }
  853. else
  854. {
  855. clientIds = new string[] { insClients };
  856. }
  857. if (clientIds.Length > 0)
  858. {
  859. List<int> output = new List<int>();
  860. foreach (var clientId in clientIds)
  861. {
  862. if (clientId.IsNumeric())
  863. {
  864. output.Add(int.Parse(clientId));
  865. }
  866. }
  867. if (output.Count > 0)
  868. {
  869. var clients = _clientDatas.Where(it => output.Contains(it.Id)).ToList();
  870. foreach (var client in clients)
  871. {
  872. itemClientName += $"{client.LastName + client.FirstName},";
  873. }
  874. if (itemClientName.Length > 0)
  875. {
  876. itemClientName = itemClientName.Substring(0, itemClientName.Length - 1);
  877. }
  878. }
  879. else
  880. {
  881. itemClientName = insClients;
  882. }
  883. }
  884. }
  885. item.ClientName = itemClientName;
  886. if (!string.IsNullOrEmpty(item.AuditGMDate))
  887. {
  888. item.AuditGMDate = Convert.ToDateTime(item.AuditGMDate).ToString("yyyy-MM-dd HH:mm:ss");
  889. }
  890. }
  891. _geView.GroupInsuranceFeeViews = groupInsuranceFeeViews;
  892. _geView.GroupInsuranceFeeStr = string.Format(@"人民币总费用:{0} CNY", InsuranceCNYTotalPrice.ToString("#0.00"));
  893. #endregion
  894. #region 其他款项费用 98
  895. List<GroupDecreaseFeeView> groupDecreaseFeeViews = new List<GroupDecreaseFeeView>();
  896. string groupDecreaseFeeSql = string.Format(@"Select dp.Id As DPId,dp.DiId As DPDiId,dp.PriceName,ccp.PayMoney,sd1.Name As PayMoneyCurrency,
  897. (((ccp.PayMoney * ccp.DayRate) / ccp.PayPercentage) * 100) As CNYPrice,
  898. ccp.DayRate,ccp.Payee,ccp.AuditGMDate,ccp.OrbitalPrivateTransfer,
  899. sd2.Name As PayWay,ccp.IsPay,u.CnName As Applicant,dp.CreateTime
  900. From Grp_DecreasePayments dp
  901. Left Join Grp_CreditCardPayment ccp On ccp.isdel = 0 And ccp.CTable = 98 And dp.Id = ccp.CId
  902. Left Join Sys_SetData sd1 On ccp.PaymentCurrency = sd1.Id
  903. Left Join Sys_SetData sd2 On ccp.PayDId = sd2.Id
  904. Left Join Sys_Users u On ccp.CreateUserId = u.Id
  905. Where dp.IsDel = 0 And ccp.Ctable = 98 And ccp.IsAuditGM = 1 And dp.Diid = {0}
  906. Order By CreateTime", _dto.DiId);
  907. groupDecreaseFeeViews = await _sqlSugar.SqlQueryable<GroupDecreaseFeeView>(groupDecreaseFeeSql).ToListAsync();
  908. #region 保险费用 - 模拟数据
  909. //if (groupDecreaseFeeViews.Count < 1)
  910. //{
  911. // groupDecreaseFeeViews.Add(new GroupDecreaseFeeView()
  912. // {
  913. // DPId = 0,
  914. // DPDiId = 2334,
  915. // PriceName = "模拟数据-费用名称",
  916. // PayMoney = 1000.00M,
  917. // PayMoneyCurrency = "CNY",
  918. // DayRate = 1.0000M,
  919. // CNYPrice = 1.0000M,
  920. // AuditGMDate = "2023-12-10 12:13:00",
  921. // Payee = "模拟数据-付款方",
  922. // OrbitalPrivateTransfer = 1,
  923. // PayWay = "现金",
  924. // IsPay = 1,
  925. // Applicant = "刘华举"
  926. // });
  927. // groupDecreaseFeeViews.Add(new GroupDecreaseFeeView()
  928. // {
  929. // DPId = 0,
  930. // DPDiId = 2334,
  931. // PriceName = "模拟数据-费用名称",
  932. // PayMoney = 1000.00M,
  933. // PayMoneyCurrency = "CNY",
  934. // DayRate = 1.0000M,
  935. // CNYPrice = 1.0000M,
  936. // AuditGMDate = "2023-12-10 12:13:00",
  937. // Payee = "模拟数据-付款方",
  938. // OrbitalPrivateTransfer = 1,
  939. // PayWay = "现金",
  940. // IsPay = 1,
  941. // Applicant = "刘华举"
  942. // });
  943. //}
  944. #endregion
  945. decimal DecreaseCNYTotalPrice = 0.00M;
  946. foreach (var item in groupDecreaseFeeViews)
  947. {
  948. item.CNYPrice = Convert.ToDecimal(item.CNYPrice.ToString("#0.00"));
  949. DecreaseCNYTotalPrice += item.CNYPrice;
  950. if (!string.IsNullOrEmpty(item.AuditGMDate))
  951. {
  952. item.AuditGMDate = Convert.ToDateTime(item.AuditGMDate).ToString("yyyy-MM-dd HH:mm:ss");
  953. }
  954. }
  955. _geView.GroupDecreaseFeeViews = groupDecreaseFeeViews;
  956. _geView.GroupDecreaseFeeStr = string.Format(@"人民币总费用:{0} CNY", DecreaseCNYTotalPrice.ToString("#0.00"));
  957. #endregion
  958. _view.GroupExpenditure = _geView;
  959. #endregion
  960. /*
  961. * 团组报表计算方式
  962. * 当前总支出 = 团组支出.Sum() + 超支费用.Sum()
  963. * 应收金额 = 应收表.Sum()
  964. * 已收金额 = 已收表.Sum()
  965. * 应收利润(应收-支出) = 应收金额 - 收款退还 - 当前总支出
  966. * 已收利润(已收-支出) = 已收金额 - 收款退还 - 当前总支出
  967. *
  968. */
  969. decimal _totalExpenditure = 0.00M; //总支出
  970. decimal _amountReceivable = 0.00M; //应收金额
  971. decimal _amountReceived = 0.00M; //已收金额
  972. decimal _receivableProfit = 0.00M; //应收利润
  973. decimal _receivedProfit = 0.00M; //已收利润
  974. _totalExpenditure = HotelCNYTotalPrice + CTGGRCNYTotalPrice + AirCNYTotalPrice + VisaCNYTotalPirce + InvitationalCNYTotalPrice +
  975. InsuranceCNYTotalPrice + DecreaseCNYTotalPrice + exTotalAmount;
  976. _amountReceivable = frTotalAmount;
  977. _amountReceived = prTotalAmount;
  978. _receivableProfit = _amountReceivable - promTotalAmount - _totalExpenditure;
  979. _receivedProfit = _amountReceived - promTotalAmount - _totalExpenditure;
  980. _view.FeeTotalStr = string.Format(@$"<span>
  981. <span>当前总支出:{_totalExpenditure.ToString("#0.00")} CNY</span>
  982. <span style='padding-left:10px;color: Green;'>应收金额:{_amountReceivable.ToString("#0.00")} CNY</span>
  983. <span style='padding-left:10px;color: Green;'>已收金额:{_amountReceived.ToString("#0.00")} CNY</span>
  984. <span style='padding-left:10px;color: Green;'>应收利润(应收-支出):{_receivableProfit.ToString("#0.00")} CNY</span>
  985. <span style='padding-left:10px;color: Green;'>已收利润(已收-支出):{_receivedProfit.ToString("#0.00")} CNY</span>
  986. </span>");
  987. return Ok(JsonView(true, "查询成功!", _view));
  988. }
  989. else
  990. {
  991. return Ok(JsonView(false, "查询成功"));
  992. }
  993. }
  994. #endregion
  995. #region 报表/折线图统计
  996. //企业利润-团组利润
  997. //企业利润-会务利润
  998. /// <summary>
  999. /// 企业利润
  1000. /// Details
  1001. /// 待添加权限验证
  1002. /// </summary>
  1003. /// <param name="_dto">团组列表请求dto</param>
  1004. /// <returns></returns>
  1005. [HttpPost("PostCorporateProfit")]
  1006. //[JsonConverter(typeof(DecimalConverter), 2)]
  1007. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  1008. public async Task<IActionResult> PostCorporateProfit(PostCorporateProfitDto _dto)
  1009. {
  1010. #region 参数验证
  1011. if (_dto.UserId < 1) return Ok(JsonView(false, "员工Id为空"));
  1012. if (_dto.PageId < 1) _dto.PageId = 38; //团组报表页面Id
  1013. if (_dto.Year < 1) return Ok(JsonView(false, "请输入有效的Year参数!"));
  1014. if (_dto.StatisticsType > 2 && _dto.StatisticsType < 1) return Ok(JsonView(false, "请输入有效的StatisticsType参数,1 月份 2 季度"));
  1015. if (_dto.BusinessType > 3 && _dto.BusinessType < 1) return Ok(JsonView(false, "请输入有效的BusinessType参数,1 所有 2 团组 3 会务"));
  1016. PageFunAuthViewBase pageFunAuthView = new PageFunAuthViewBase();
  1017. #region 页面操作权限验证
  1018. //pageFunAuthView = await GeneralMethod.PostUserPageFuncDatas(_dto.UserId, _dto.PageId);
  1019. //if (pageFunAuthView.CheckAuth == 0) return Ok(JsonView(false, "您没有查看权限"));
  1020. #endregion
  1021. string sqlWhere = string.Empty;
  1022. //起止时间
  1023. DateTime beginDt = Convert.ToDateTime($"{_dto.Year}-01-01 00:00:00");
  1024. DateTime endDt = Convert.ToDateTime($"{_dto.Year}-12-31 23:59:59");
  1025. sqlWhere = string.Format(@$" Where Isdel = 0 ");
  1026. //业务类型
  1027. List<int> groupTypeId = new List<int>();
  1028. if (_dto.BusinessType == 2) //团组
  1029. {
  1030. groupTypeId.AddRange(new List<int>() {
  1031. 38, // 政府团
  1032. 39, // 企业团
  1033. 40, // 散客团
  1034. 1048 // 高校团
  1035. });
  1036. }
  1037. else if (_dto.BusinessType == 3) //会务
  1038. {
  1039. groupTypeId.AddRange(new List<int>() {
  1040. 102, // 未知
  1041. 248, // 非团组
  1042. 302, // 成都-会务活动
  1043. 691, // 四川-会务活动
  1044. 762, // 四川-赛事项目收入
  1045. 1047 // 成都-赛事项目收入
  1046. });
  1047. }
  1048. if (groupTypeId.Count > 0)
  1049. {
  1050. sqlWhere += string.Format(@$" And TeamDid In ({string.Join(',', groupTypeId)})");
  1051. }
  1052. string sql = string.Format(@$"Select * From Grp_DelegationInfo {sqlWhere}");
  1053. var groupInfos = await _sqlSugar.SqlQueryable<Grp_DelegationInfo>(sql).Where(it => it.CreateTime >= beginDt && it.CreateTime <= endDt).ToListAsync();
  1054. if (groupInfos.Count < 1) return Ok(JsonView(false, "暂无相关团组!"));
  1055. List<int> diIds = groupInfos.Select(it => it.Id).ToList();
  1056. List<CorporateProfit> corporateProfits = await CorporateProfit(diIds);
  1057. List<MonthInfo> months = new List<MonthInfo>();
  1058. if (_dto.StatisticsType == 1) //月份
  1059. {
  1060. months = GeneralMethod.GetMonthInfos(Convert.ToInt32(_dto.Year));
  1061. }
  1062. else if (_dto.StatisticsType == 1) //季度
  1063. {
  1064. months = GeneralMethod.GetQuarter(Convert.ToInt32(_dto.Year));
  1065. }
  1066. List<CorporateProfitMonthView> _view = new List<CorporateProfitMonthView>();
  1067. foreach (var item in months)
  1068. {
  1069. DateTime monthBeginDt = Convert.ToDateTime($"{_dto.Year}-{item.Month}-{item.Days.BeginDays} 00:00:00");
  1070. DateTime monthEndDt = Convert.ToDateTime($"{_dto.Year}-{item.Month}-{item.Days.EndDays} 23:59:59");
  1071. var corporateProfit = corporateProfits.Where(it => it.CreateDt >= monthBeginDt && it.CreateDt <= monthEndDt).ToList();
  1072. _view.Add(new CorporateProfitMonthView()
  1073. {
  1074. Month = item.Month,
  1075. Profit = corporateProfit.Sum(it => it.ReceivedProfit),
  1076. GroupInfos = corporateProfit.OrderBy(it => it.CreateDt).ToList()
  1077. });
  1078. }
  1079. return Ok(JsonView(true, "操作成功!", _view));
  1080. #endregion
  1081. }
  1082. /// <summary>
  1083. /// 计算团组利润
  1084. /// </summary>
  1085. /// <param name="diIds"></param>
  1086. /// <returns></returns>
  1087. private async Task<List<CorporateProfit>> CorporateProfit(List<int> diIds)
  1088. {
  1089. List<CorporateProfit> corporateProfits = new List<CorporateProfit>();
  1090. if (diIds.Count < 1)
  1091. {
  1092. return corporateProfits;
  1093. }
  1094. #region 计算团组利润
  1095. /*
  1096. * 团组报表计算方式
  1097. * 当前总支出 = 团组支出.Sum() + 超支费用.Sum()
  1098. * 应收金额 = 应收表.Sum()
  1099. * 已收金额 = 已收表.Sum()
  1100. * 应收利润(应收-支出) = 应收金额 - 收款退还 - 当前总支出
  1101. * 已收利润(已收-支出) = 已收金额 - 收款退还 - 当前总支出
  1102. *
  1103. */
  1104. string diIdStr = string.Join(",", diIds);
  1105. string sql = string.Format(@$"Select * From Grp_DelegationInfo Where Isdel = 0 And Id In ({diIdStr})");
  1106. var groupInfos = await _sqlSugar.SqlQueryable<Grp_DelegationInfo>(sql).ToListAsync();
  1107. #region 费用类型 币种,转账,客户信息
  1108. List<Sys_SetData> _setDatas = await _sqlSugar.Queryable<Sys_SetData>().Where(it => it.IsDel == 0).ToListAsync();
  1109. var _clientDatas = await _sqlSugar.Queryable<Crm_DeleClient>().Where(it => it.IsDel == 0).ToListAsync();
  1110. #endregion
  1111. foreach (var _diId in diIds)
  1112. {
  1113. List<ExpenditureInfo> expenditureInfos = new List<ExpenditureInfo>();
  1114. #region 团组收入
  1115. /*
  1116. * 应收报表
  1117. */
  1118. decimal frTotalAmount = 0.00M;//应收总金额
  1119. string _frSql = string.Format(@"Select fr.Id,fr.Diid,fr.PriceName,fr.Price,fr.Count,fr.Unit,fr.Currency,
  1120. sd.Name As CurrencyCode,sd.Remark As CurrencyName,fr.Rate,fr.ItemSumPrice,fr.CreateTime
  1121. From Fin_ForeignReceivables fr
  1122. Left Join Sys_SetData sd On fr.Currency = sd.Id
  1123. Where fr.IsDel = 0 And fr.Diid = {0} Order By CreateTime", _diId);
  1124. List<Gsd_ForeignReceivablesView> _frViews = await _sqlSugar.SqlQueryable<Gsd_ForeignReceivablesView>(_frSql).ToListAsync();
  1125. frTotalAmount = _frViews.Sum(it => it.ItemSumPrice);
  1126. expenditureInfos.Add(new ExpenditureInfo() { ItemName = "应收项", Amount = frTotalAmount });
  1127. /*
  1128. * 已收报表
  1129. */
  1130. decimal prTotalAmount = 0.00M;//已收总金额
  1131. string _prSql = string.Format(@"Select pr.Id,pr.Diid,pr.SectionTime As SectionTimeDt,pr.Price,pr.Currency,
  1132. sd1.Name As CurrencyCode,sd1.Remark As CurrencyName,pr.Client,
  1133. pr.ReceivablesType,sd2.Name As ReceivablesTypeName,pr.Remark,pr.CreateTime
  1134. From Fin_ProceedsReceived pr
  1135. Left Join Sys_SetData sd1 On pr.Currency = sd1.Id
  1136. Left Join Sys_SetData sd2 On pr.ReceivablesType = sd2.Id
  1137. Where pr.IsDel = 0 and pr.Diid = {0} Order By CreateTime", _diId);
  1138. List<Gsd_ProceedsReceivedView> _prViews = await _sqlSugar.SqlQueryable<Gsd_ProceedsReceivedView>(_prSql).ToListAsync();
  1139. prTotalAmount = _prViews.Sum(it => it.Price);
  1140. expenditureInfos.Add(new ExpenditureInfo() { ItemName = "已收项", Amount = prTotalAmount });
  1141. /*
  1142. * 超支费用
  1143. */
  1144. decimal exTotalAmount = 0.00M;
  1145. string _ecSql = string.Format(@"Select gec.Id As GECId,gec.DiId As GECDiId,gec.PriceName,ccp.PayMoney,sd1.Name As PaymentCurrency,
  1146. ccp.RMBPrice As CNYPrice,ccp.DayRate,ccp.Payee,ccp.OrbitalPrivateTransfer,sd2.Name As PayWay,
  1147. sd3.Name As CardType,ccp.IsPay,u.CnName As Applicant,gec.CreateTime
  1148. From OA2023DB.dbo.Fin_GroupExtraCost gec
  1149. Left Join Grp_CreditCardPayment ccp On gec.Id = ccp.CId
  1150. Left Join Sys_SetData sd1 On ccp.PaymentCurrency = sd1.Id
  1151. Left Join Sys_SetData sd2 On ccp.PayDId = sd2.Id
  1152. Left Join Sys_SetData sd3 On ccp.CTDId = sd3.Id
  1153. Left Join Sys_Users u On ccp.CreateUserId = u.Id
  1154. Where ccp.IsDel = 0 And ccp.CTable = 1015 And ccp.IsAuditGM = 1 And ccp.IsPay = 1 And ccp.DiId = {0} Order By CreateTime", _diId);
  1155. List<Gsd_ExtraCostsView> _ExtraCostsViews = await _sqlSugar.SqlQueryable<Gsd_ExtraCostsView>(_ecSql).ToListAsync();
  1156. exTotalAmount = _ExtraCostsViews.Sum(it => it.CNYPrice);
  1157. expenditureInfos.Add(new ExpenditureInfo() { ItemName = "超支费用", Amount = exTotalAmount });
  1158. /*
  1159. * 收款退还
  1160. */
  1161. decimal promTotalAmount = 0.00M;// 收款退还总金额
  1162. List<Gsd_PaymentRefundAndOtherMoneyView> _promView = new List<Gsd_PaymentRefundAndOtherMoneyView>();
  1163. //删除了 And prom.PriceType = 1
  1164. string _ropSql = string.Format(@"Select u.CnName As Appliction,prom.Id As PrId,prom.DiId As PrDiId,prom.Price As PrPrice,
  1165. prom.PriceName AS PrPriceName,prom.CurrencyId As PrCurrencyId,
  1166. prom.PayType As PrPayType,prom.PriceType As PrPriceType,ccp.*,prom.CreateTime As PrCreateTime
  1167. From Fin_PaymentRefundAndOtherMoney prom
  1168. Left Join Grp_CreditCardPayment ccp On prom.DiId = ccp.DIId And prom.Id = ccp.CId
  1169. Left Join Sys_Users u On ccp.CreateUserId = u.Id
  1170. Where prom.IsDel = 0 And prom.PayType = 1 And ccp.CTable = 285
  1171. And ccp.IsAuditGM = 1 And ccp.IsPay = 1
  1172. And prom.DiId = {0} Order By PrCreateTime", _diId);
  1173. var _promDatas = await _sqlSugar.SqlQueryable<Gsd_PaymentRefundAndOtherMoneyDataSource1View>(_ropSql).ToListAsync();
  1174. foreach (var ropItem in _promDatas)
  1175. {
  1176. string thisCueencyCode = "Unknown";
  1177. string thisCueencyName = "Unknown";
  1178. var currency = _setDatas.Where(it => it.Id == ropItem.PaymentCurrency).FirstOrDefault();
  1179. if (currency != null)
  1180. {
  1181. thisCueencyCode = currency.Name;
  1182. thisCueencyName = currency.Remark;
  1183. }
  1184. string orbitalPrivateTransferStr = "Unknown";
  1185. var orbitalPrivateTransfer = _setDatas.Where(it => it.Id == ropItem.OrbitalPrivateTransfer).FirstOrDefault();
  1186. if (orbitalPrivateTransfer != null)
  1187. {
  1188. orbitalPrivateTransferStr = orbitalPrivateTransfer.Name;
  1189. }
  1190. string payStr = "Unknown";
  1191. var pay = _setDatas.Where(it => it.Id == ropItem.PayDId).FirstOrDefault();
  1192. if (pay != null)
  1193. {
  1194. payStr = pay.Name;
  1195. }
  1196. Gsd_PaymentRefundAndOtherMoneyView gsd_PaymentRefund = new Gsd_PaymentRefundAndOtherMoneyView()
  1197. {
  1198. Id = ropItem.Id,
  1199. DiId = ropItem.DIId,
  1200. PriceName = ropItem.PrPriceName,
  1201. PayCurrencyCode = thisCueencyCode,
  1202. PayCurrencyName = thisCueencyName,
  1203. Price = ropItem.PrPrice,
  1204. CNYPrice = ropItem.RMBPrice,
  1205. ThisRate = ropItem.DayRate,
  1206. Payee = ropItem.Payee,
  1207. PayTime = ropItem.AuditGMDate,
  1208. OrbitalPrivateTransfer = ropItem.OrbitalPrivateTransfer,
  1209. PayType = payStr,
  1210. IsPay = ropItem.IsPay,
  1211. Applicant = ropItem.Appliction
  1212. };
  1213. _promView.Add(gsd_PaymentRefund);
  1214. }
  1215. promTotalAmount = _promView.Sum(it => it.CNYPrice);
  1216. expenditureInfos.Add(new ExpenditureInfo() { ItemName = "收款退还", Amount = promTotalAmount });
  1217. #endregion
  1218. #region 团组支出
  1219. GroupExpenditureView _geView = new GroupExpenditureView();
  1220. #region 酒店预定费用
  1221. List<GroupHotelFeeView> groupHotelFeeViews = new List<GroupHotelFeeView>();
  1222. string hotelFeeSql = string.Format(@"Select hr.Id As HrId,hr.DiId As HrDiId,hr.City,hr.HotelName,hr.CheckInDate,hr.CheckOutDate,
  1223. sd1.Name As PaymentCurrency,hr.SingleRoomPrice,hr.SingleRoomCount,hr.DoubleRoomPrice,
  1224. hr.DoubleRoomCount,hr.SuiteRoomPrice,hr.SuiteRoomCount,hr.OtherRoomPrice,hr.OtherRoomCount,
  1225. hr.BreakfastPrice,sd4.Name As BreakfastCurrency,hr.Isoppay,hr.GovernmentRent,
  1226. sd5.Name As GovernmentRentCurrency,hr.CityTax,sd6.Name As CityTaxCurrency,
  1227. ccp.PayMoney,ccp.RMBPrice As CNYPrice,ccp.DayRate,ccp.Payee,ccp.OrbitalPrivateTransfer,
  1228. sd2.Name As PayWay,sd3.Name As CardType,ccp.IsPay,u.CnName As Applicant
  1229. From Grp_HotelReservations hr
  1230. Left Join Grp_CreditCardPayment ccp On hr.Id = ccp.CId
  1231. Left Join Sys_SetData sd1 On ccp.PaymentCurrency = sd1.Id
  1232. Left Join Sys_SetData sd2 On ccp.PayDId = sd2.Id
  1233. Left Join Sys_SetData sd3 On ccp.CTDId = sd3.Id
  1234. Left Join Sys_Users u On ccp.CreateUserId = u.Id
  1235. Left Join Sys_SetData sd4 On hr.BreakfastCurrency = sd4.Id
  1236. Left Join Sys_SetData sd5 On hr.GovernmentRentCurrency = sd5.Id
  1237. Left Join Sys_SetData sd6 On hr.CityTaxCurrency = sd6.Id
  1238. Where hr.IsDel = 0 And ccp.IsDel = 0 And ccp.CTable = 76 And ccp.IsAuditGM = 1 And ccp.IsPay = 1 And hr.DiId = {0}
  1239. Order By CheckInDate Asc", _diId);
  1240. groupHotelFeeViews = await _sqlSugar.SqlQueryable<GroupHotelFeeView>(hotelFeeSql).ToListAsync();
  1241. decimal HotelCNYTotalPrice = groupHotelFeeViews.Sum(it => it.CNYPrice);
  1242. expenditureInfos.Add(new ExpenditureInfo() { ItemName = "酒店预定", Amount = HotelCNYTotalPrice });
  1243. #endregion
  1244. #region 地接费用
  1245. List<GroupCTGGRFeeView> groupCTGGRFeeViews = new List<GroupCTGGRFeeView>();
  1246. string CTGGRFeeSql = string.Format(@"Select ctggr.Id As CTGGRId,ctggr.DiId As CTGGRDiId,ctggr.Area,ctggrc.*,ctggrc.Price As PayMoney,
  1247. sd2.name As PaymentCurrency,ccp.PayPercentage,
  1248. (ctggrc.Price / (ccp.PayPercentage / 100)) As AmountPaid,
  1249. (ctggrc.Price / (ccp.PayPercentage / 100) - ctggrc.Price) As BalancePayment,
  1250. ccp.DayRate,(ctggrc.Price * ccp.DayRate) As CNYPrice,ccp.Payee,ccp.AuditGMDate,
  1251. ccp.OrbitalPrivateTransfer,sd1.Name As PayWay,ccp.IsPay,u.CnName As Applicant,ctggr.CreateTime
  1252. From Grp_CarTouristGuideGroundReservations ctggr
  1253. Left Join ( Select cggrc.CTGGRId,sd1.Name As PriceName,cggrc.Price,sd2.Name As PriceCurrency,
  1254. cggrc.PriceContent
  1255. From Grp_CarTouristGuideGroundReservationsContent cggrc
  1256. Left Join Sys_SetData sd1 On cggrc.SId = sd1.Id
  1257. Left Join Sys_SetData sd2 On cggrc.Currency = sd2.Id
  1258. Where cggrc.ISdel = 0 And cggrc.Price != 0.00
  1259. ) ctggrc On ctggr.Id = ctggrc.CTGGRId
  1260. Left Join Grp_CreditCardPayment ccp On ccp.IsDel = 0 And ccp.CTable = 79 And ctggr.Id = ccp.CId
  1261. Left Join Sys_SetData sd1 On ccp.PayDId = sd1.Id
  1262. Left Join Sys_SetData sd2 On ccp.PaymentCurrency = sd2.Id
  1263. Left Join Sys_Users u On ccp.CreateUserId = u.Id
  1264. Where ctggr.IsDel = 0 And ccp.IsAuditGM = 1 And ccp.IsPay = 1 And ctggr.DiId = {0}
  1265. Order By CreateTime", _diId);
  1266. groupCTGGRFeeViews = await _sqlSugar.SqlQueryable<GroupCTGGRFeeView>(CTGGRFeeSql).ToListAsync();
  1267. decimal CTGGRCNYTotalPrice = groupCTGGRFeeViews.Sum(it => Convert.ToDecimal(it.CNYPrice.ToString("#0.00")));
  1268. expenditureInfos.Add(new ExpenditureInfo() { ItemName = "地接", Amount = CTGGRCNYTotalPrice });
  1269. #endregion
  1270. #region 机票预订费用
  1271. List<GroupAirFeeView> groupAirFeeViews = new List<GroupAirFeeView>();
  1272. string groupAirFeeSql = string.Format(@"Select atr.Id As AirId,atr.DIId As AirDiId,atr.FlightsCode,atr.FlightsCity,sd4.Name As AirTypeName,
  1273. atr.FlightsDate,atr.FlightsTime,atr.ClientName,atr.ClientNum,ccp.PayMoney,
  1274. sd1.Name As PayMoneyCurrency,ccp.RMBPrice As CNYPrice,ccp.DayRate,ccp.Payee,ccp.AuditGMDate,
  1275. ccp.OrbitalPrivateTransfer,sd2.Name As PayWay,sd3.Name As CardType,ccp.IsPay,u.CnName As Applicant,atr.CreateTime
  1276. From Grp_AirTicketReservations atr
  1277. Left Join Grp_CreditCardPayment ccp On ccp.isdel = 0 And ccp.CTable = 85 And atr.Id = ccp.CId
  1278. Left Join Sys_SetData sd1 On ccp.PaymentCurrency = sd1.Id
  1279. Left Join Sys_SetData sd2 On ccp.PayDId = sd2.Id
  1280. Left Join Sys_SetData sd3 On ccp.CTDId = sd3.Id
  1281. Left Join Sys_SetData sd4 On atr.CType = sd4.Id
  1282. Left Join Sys_Users u On ccp.CreateUserId = u.Id
  1283. Where atr.IsDel = 0 And ccp.IsAuditGM = 1 And ccp.IsPay = 1 And atr.DiId = {0} Order By CreateTime", _diId);
  1284. groupAirFeeViews = await _sqlSugar.SqlQueryable<GroupAirFeeView>(groupAirFeeSql).ToListAsync();
  1285. decimal AirCNYTotalPrice = groupAirFeeViews.Sum(it => it.CNYPrice);
  1286. expenditureInfos.Add(new ExpenditureInfo() { ItemName = "机票预订", Amount = AirCNYTotalPrice });
  1287. #endregion
  1288. #region 签证费用
  1289. List<GroupVisaFeeView> groupVisaFeeViews = new List<GroupVisaFeeView>();
  1290. string groupVisaFeeSql = string.Format(@"Select vi.Id As VisaId,vi.DIId As VisaDiId,vi.VisaClient,ccp.PayMoney,sd1.Name As PayMoneyCurrency,
  1291. ccp.DayRate,ccp.Payee,ccp.AuditGMDate,ccp.OrbitalPrivateTransfer,sd2.Name As PayWay,
  1292. sd3.Name As CardTypeName,ccp.IsPay,u.CnName As Applicant,vi.CreateTime
  1293. From Grp_VisaInfo vi
  1294. Left Join Grp_CreditCardPayment ccp On ccp.isdel = 0 And ccp.CTable = 80 And vi.Id = ccp.CId
  1295. Left Join Sys_SetData sd1 On ccp.PaymentCurrency = sd1.Id
  1296. Left Join Sys_SetData sd2 On ccp.PayDId = sd2.Id
  1297. Left Join Sys_SetData sd3 On ccp.CTDId = sd3.Id
  1298. Left Join Sys_Users u On ccp.CreateUserId = u.Id
  1299. Where vi.IsDel = 0 And ccp.IsAuditGM = 1 And ccp.IsPay = 1 And vi.DIId = {0} Order By CreateTime", _diId);
  1300. groupVisaFeeViews = await _sqlSugar.SqlQueryable<GroupVisaFeeView>(groupVisaFeeSql).ToListAsync();
  1301. decimal VisaCNYTotalPirce = groupVisaFeeViews.Sum(it => it.PayMoney);
  1302. expenditureInfos.Add(new ExpenditureInfo() { ItemName = "签证", Amount = VisaCNYTotalPirce });
  1303. #endregion
  1304. #region 邀请/公务活动 CTable = 81
  1305. List<GroupInvitationalFeeView> groupInvitationalFeeViews = new List<GroupInvitationalFeeView>();
  1306. string groupInvitationalFeeSql = string.Format(@"Select ioa.Id As IOAId,ioa.DiId As IOADiId,ioa.InviterArea,ioa.Inviter,ioa.InviteTime,
  1307. ioa.InviteCost,sd3.Name As InviteCurrency,ioa.SendCost,sd4.Name As SendCurrency,ioa.EventsCost,
  1308. sd5.Name As EventsCurrency,ioa.TranslateCost,sd6.Name As TranslateCurrency,ccp.PayMoney,
  1309. sd7.Name As PaymentCurrency,ccp.RMBPrice As CNYPrice,ccp.Payee,ccp.AuditGMDate,
  1310. ccp.OrbitalPrivateTransfer,sd2.Name As PayWay,ccp.IsPay,u.CnName As Applicant,ioa.CreateTime
  1311. From Grp_InvitationOfficialActivities ioa
  1312. Left Join Grp_CreditCardPayment ccp On ccp.isdel = 0 And ccp.CTable = 81 And ioa.Id = ccp.CId
  1313. Left Join Sys_SetData sd1 On ccp.PaymentCurrency = sd1.Id
  1314. Left Join Sys_SetData sd2 On ccp.PayDId = sd2.Id
  1315. Left Join Sys_SetData sd3 On ioa.InviteCurrency = sd3.Id
  1316. Left Join Sys_SetData sd4 On ioa.SendCurrency = sd4.Id
  1317. Left Join Sys_SetData sd5 On ioa.EventsCurrency = sd5.Id
  1318. Left Join Sys_SetData sd6 On ioa.TranslateCurrency = sd6.Id
  1319. Left Join Sys_SetData sd7 On ccp.PaymentCurrency = sd7.Id
  1320. Left Join Sys_Users u On ccp.CreateUserId = u.Id
  1321. Where ioa.IsDel = 0 And ccp.IsAuditGM = 1 And ccp.IsPay = 1 And ioa.Diid = {0} Order By CreateTime", _diId);
  1322. groupInvitationalFeeViews = await _sqlSugar.SqlQueryable<GroupInvitationalFeeView>(groupInvitationalFeeSql).ToListAsync();
  1323. decimal InvitationalCNYTotalPrice = groupInvitationalFeeViews.Sum(it => it.CNYPrice);
  1324. expenditureInfos.Add(new ExpenditureInfo() { ItemName = "邀请/公务活动", Amount = InvitationalCNYTotalPrice });
  1325. #endregion
  1326. #region 保险费用
  1327. List<GroupInsuranceFeeView> groupInsuranceFeeViews = new List<GroupInsuranceFeeView>();
  1328. string groupInsuranceFeeSql = string.Format(@"Select ic.Id As InsuranceId,ic.Diid As InsuranceDiId,ClientName,ccp.PayMoney,ccp.RMBPrice As CNYPrice,
  1329. sd1.Name As PayMoneyCurrency,ccp.Payee,ccp.AuditGMDate,ccp.OrbitalPrivateTransfer,
  1330. sd2.Name As PayWay,ccp.IsPay,u.CnName As Applicant,ic.CreateTime
  1331. From Grp_Customers ic
  1332. Left Join Grp_CreditCardPayment ccp On ccp.isdel = 0 And ccp.CTable = 82 And ic.Id = ccp.CId
  1333. Left Join Sys_SetData sd1 On ccp.PaymentCurrency = sd1.Id
  1334. Left Join Sys_SetData sd2 On ccp.PayDId = sd2.Id
  1335. Left Join Sys_Users u On ccp.CreateUserId = u.Id
  1336. Where ic.IsDel = 0 And ccp.IsAuditGM = 1 And ccp.IsPay = 1 And ic.DiId = {0} Order By CreateTime", _diId);
  1337. groupInsuranceFeeViews = await _sqlSugar.SqlQueryable<GroupInsuranceFeeView>(groupInsuranceFeeSql).ToListAsync();
  1338. decimal InsuranceCNYTotalPrice = groupInsuranceFeeViews.Sum(it => it.CNYPrice);
  1339. expenditureInfos.Add(new ExpenditureInfo() { ItemName = "保险费用", Amount = InsuranceCNYTotalPrice });
  1340. #endregion
  1341. #region 其他款项费用 98
  1342. List<GroupDecreaseFeeView> groupDecreaseFeeViews = new List<GroupDecreaseFeeView>();
  1343. string groupDecreaseFeeSql = string.Format(@"Select dp.Id As DPId,dp.DiId As DPDiId,dp.PriceName,ccp.PayMoney,sd1.Name As PayMoneyCurrency,
  1344. (((ccp.PayMoney * ccp.DayRate) / ccp.PayPercentage) * 100) As CNYPrice,
  1345. ccp.DayRate,ccp.Payee,ccp.AuditGMDate,ccp.OrbitalPrivateTransfer,
  1346. sd2.Name As PayWay,ccp.IsPay,u.CnName As Applicant,dp.CreateTime
  1347. From Grp_DecreasePayments dp
  1348. Left Join Grp_CreditCardPayment ccp On ccp.isdel = 0 And ccp.CTable = 98 And dp.Id = ccp.CId
  1349. Left Join Sys_SetData sd1 On ccp.PaymentCurrency = sd1.Id
  1350. Left Join Sys_SetData sd2 On ccp.PayDId = sd2.Id
  1351. Left Join Sys_Users u On ccp.CreateUserId = u.Id
  1352. Where dp.IsDel = 0 And ccp.Ctable = 98 And ccp.IsAuditGM = 1 And ccp.IsPay = 1 And dp.Diid = {0}
  1353. Order By CreateTime", _diId);
  1354. groupDecreaseFeeViews = await _sqlSugar.SqlQueryable<GroupDecreaseFeeView>(groupDecreaseFeeSql).ToListAsync();
  1355. decimal DecreaseCNYTotalPrice = groupDecreaseFeeViews.Sum(it => Convert.ToDecimal(it.CNYPrice.ToString("#0.00")));
  1356. expenditureInfos.Add(new ExpenditureInfo() { ItemName = "其他款项", Amount = DecreaseCNYTotalPrice });
  1357. #endregion
  1358. #endregion
  1359. /*
  1360. * 团组报表计算方式
  1361. * 当前总支出 = 团组支出.Sum() + 超支费用.Sum()
  1362. * 应收金额 = 应收表.Sum()
  1363. * 已收金额 = 已收表.Sum()
  1364. * 应收利润(应收-支出) = 应收金额 - 收款退还 - 当前总支出
  1365. * 已收利润(已收-支出) = 已收金额 - 收款退还 - 当前总支出
  1366. *
  1367. */
  1368. decimal _totalExpenditure = 0.00M; //总支出
  1369. decimal _amountReceivable = 0.00M; //应收金额
  1370. decimal _amountReceived = 0.00M; //已收金额
  1371. decimal _receivableProfit = 0.00M; //应收利润
  1372. decimal _receivedProfit = 0.00M; //已收利润
  1373. _totalExpenditure = HotelCNYTotalPrice + CTGGRCNYTotalPrice + AirCNYTotalPrice + VisaCNYTotalPirce + InvitationalCNYTotalPrice +
  1374. InsuranceCNYTotalPrice + DecreaseCNYTotalPrice + exTotalAmount;
  1375. _amountReceivable = frTotalAmount;
  1376. _amountReceived = prTotalAmount;
  1377. _receivableProfit = _amountReceivable - promTotalAmount - _totalExpenditure;
  1378. _receivedProfit = _amountReceived - promTotalAmount - _totalExpenditure;
  1379. var groupInfo = groupInfos.Find(it => it.Id == _diId);
  1380. corporateProfits.Add(new CorporateProfit()
  1381. {
  1382. DiId = _diId,
  1383. TeamName = groupInfo?.TeamName ?? "Unkwnon",
  1384. CreateDt = Convert.ToDateTime(groupInfo?.CreateTime),
  1385. TotalExpenditure = _totalExpenditure,
  1386. ExpenditureItem = expenditureInfos,
  1387. AmountReceivable = _amountReceivable,
  1388. AmountReceived = _amountReceived,
  1389. ReceivableProfit = _receivableProfit,
  1390. ReceivedProfit = _receivedProfit,
  1391. });
  1392. }
  1393. #endregion
  1394. return corporateProfits;
  1395. }
  1396. //未来预测-地区接团/出团量
  1397. //未来预测-地区酒店预订量
  1398. //未来预测-地区机票预订量
  1399. //未来预测-地区车辆预订量
  1400. /// <summary>
  1401. /// (国家/城市)地区预订数量(团,酒店,机票,车辆)
  1402. /// Details
  1403. /// 待添加权限验证
  1404. /// </summary>
  1405. /// <param name="_dto">团组列表请求dto</param>
  1406. /// <returns></returns>
  1407. [HttpPost("PostRegionalBookingsNumber")]
  1408. //[JsonConverter(typeof(DecimalConverter), 2)]
  1409. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  1410. public async Task<IActionResult> PostRegionalBookingsNumber(PostRegionalBookingsNumberDto _dto)
  1411. {
  1412. #region 参数验证
  1413. if (_dto.UserId < 1) return Ok(JsonView(false, "员工Id为空"));
  1414. //if (_dto.PageId < 1) _dto.PageId = 38; //团组报表页面Id
  1415. if (_dto.Type > 1 && _dto.Type > 5) return Ok(JsonView(false, "请输入有效的Type参数,1 团 2 酒店 3 机票 4 车辆"));
  1416. if (_dto.Year < 1) return Ok(JsonView(false, "请输入有效的Year参数!"));
  1417. PageFunAuthViewBase pageFunAuthView = new PageFunAuthViewBase();
  1418. #region 页面操作权限验证
  1419. //pageFunAuthView = await GeneralMethod.PostUserPageFuncDatas(_dto.UserId, _dto.PageId);
  1420. //if (pageFunAuthView.CheckAuth == 0) return Ok(JsonView(false, "您没有查看权限"));
  1421. #endregion
  1422. string sqlWhere = string.Empty;
  1423. //起止时间
  1424. DateTime beginDt = Convert.ToDateTime($"{_dto.Year}-01-01 00:00:00");
  1425. DateTime endDt = Convert.ToDateTime($"{_dto.Year}-12-31 23:59:59");
  1426. sqlWhere = string.Format(@$" Where Isdel = 0 ");
  1427. string sql = string.Format(@$"Select * From Grp_DelegationInfo {sqlWhere}");
  1428. var groupInfos = await _sqlSugar.SqlQueryable<Grp_DelegationInfo>(sql).Where(it => it.CreateTime >= beginDt && it.CreateTime <= endDt).ToListAsync();
  1429. if (groupInfos.Count < 1) return Ok(JsonView(false, "暂无相关团组!"));
  1430. List<GroupTypeNumberInfo> datas = new List<GroupTypeNumberInfo>();
  1431. foreach (var item in groupInfos)
  1432. {
  1433. var data = await GroupBookingsNumber(_dto.Type, item);
  1434. if (data.TypeItem.Count > 0)
  1435. {
  1436. datas.Add(data);
  1437. }
  1438. }
  1439. //类型处理
  1440. if (_dto.Type == 1)//接团
  1441. {
  1442. List<GroupBookingNumberView> views = new List<GroupBookingNumberView>();
  1443. dynamic groupData = null;
  1444. foreach (var item in datas)
  1445. {
  1446. if (item.TypeItem.Count > 0)
  1447. {
  1448. foreach (var item1 in item.TypeItem)
  1449. {
  1450. if (item1.RegionItem.Count > 0)
  1451. {
  1452. GroupInfo groupInfo = new GroupInfo()
  1453. {
  1454. DiId = item.DiId,
  1455. TeamName = item.GroupName,
  1456. CreateTime = groupInfos.Find(it => it.Id == item.DiId)?.CreateTime.ToString("yyyy-MM-dd HH:mm:ss") ?? "Unknown",
  1457. ClientUnit = groupInfos.Find(it => it.Id == item.DiId)?.ClientUnit ?? "Unknown",
  1458. Principal = groupInfos.Find(it => it.Id == item.DiId)?.ClientName ?? "Unknown",
  1459. };
  1460. views.Add(new GroupBookingNumberView() { Name = item1.RegionItem[0].Name, Number = item1.RegionItem[0].Number, GroupItem = new List<GroupInfo>() { groupInfo } });
  1461. }
  1462. }
  1463. }
  1464. }
  1465. var viewsGroup = views.GroupBy(it => it.Name);
  1466. List<GroupBookingNumberView> _view = new List<GroupBookingNumberView>();
  1467. foreach (var item in viewsGroup)
  1468. {
  1469. List<GroupInfo> infos = new List<GroupInfo>();
  1470. foreach (var item1 in item)
  1471. {
  1472. infos.AddRange(item1.GroupItem);
  1473. }
  1474. infos = infos.OrderByDescending(it => it.CreateTime).ToList(); //
  1475. _view.Add(new GroupBookingNumberView() { Name = item.Key, Number = item.Count(), GroupItem = infos });
  1476. }
  1477. _view = _view.OrderByDescending(it => it.Number).Take(10).ToList();
  1478. return Ok(JsonView(true, "操作成功!", _view, _view.Count));
  1479. }
  1480. else if (_dto.Type == 2)
  1481. {
  1482. List<HotelBookingNumberView> views = new List<HotelBookingNumberView>();
  1483. foreach (var item in datas)
  1484. {
  1485. if (item.TypeItem.Count > 0)
  1486. {
  1487. foreach (var item1 in item.TypeItem)
  1488. {
  1489. if (item1.RegionItem.Count > 0)
  1490. {
  1491. foreach (var item2 in item1.RegionItem)
  1492. {
  1493. GroupInfo groupInfo = new GroupInfo()
  1494. {
  1495. DiId = item.DiId,
  1496. TeamName = item.GroupName,
  1497. CreateTime = groupInfos.Find(it => it.Id == item.DiId)?.CreateTime.ToString("yyyy-MM-dd HH:mm:ss") ?? "Unknown",
  1498. ClientUnit = groupInfos.Find(it => it.Id == item.DiId)?.ClientUnit ?? "Unknown",
  1499. Principal = groupInfos.Find(it => it.Id == item.DiId)?.ClientName ?? "Unknown",
  1500. };
  1501. List<HotelInfo> hotels = new List<HotelInfo>();
  1502. foreach (var item3 in item2.Data)
  1503. {
  1504. StatisticsHotelInfo statisticsHotelInfos = JsonConvert.DeserializeObject<StatisticsHotelInfo>(JsonConvert.SerializeObject(item3));
  1505. HotelInfo hotelInfo = new HotelInfo()
  1506. {
  1507. HotelName = statisticsHotelInfos.HotelName,
  1508. SingleRoomNum = statisticsHotelInfos.SingleRoomNum,
  1509. DoubleRoomNum = statisticsHotelInfos.DoubleRoomNum,
  1510. SuiteRoomNum = statisticsHotelInfos.SuiteRoomNum,
  1511. OtherRoomNum = statisticsHotelInfos.OtherRoomNum,
  1512. GroupInfo = groupInfo
  1513. };
  1514. hotels.Add(hotelInfo);
  1515. }
  1516. views.Add(new HotelBookingNumberView() { Name = item1.RegionItem[0].Name, Number = item1.RegionItem[0].Number, HotelItem = hotels });
  1517. }
  1518. }
  1519. }
  1520. }
  1521. }
  1522. var viewsGroup = views.GroupBy(it => it.Name);
  1523. List<HotelBookingNumberView> _view = new List<HotelBookingNumberView>();
  1524. foreach (var item in viewsGroup)
  1525. {
  1526. List<HotelInfo> infos = new List<HotelInfo>();
  1527. foreach (var item1 in item)
  1528. {
  1529. infos.AddRange(item1.HotelItem);
  1530. }
  1531. _view.Add(new HotelBookingNumberView() { Name = item.Key, Number = item.Count(), HotelItem = infos });
  1532. }
  1533. _view = _view.OrderByDescending(it => it.Number).Take(10).ToList();
  1534. return Ok(JsonView(true, "操作成功!", views, views.Count));
  1535. }
  1536. return Ok(JsonView(false, "操作失败!"));
  1537. #endregion
  1538. }
  1539. /// <summary>
  1540. /// 计算团组ALLType预订数量
  1541. /// </summary>
  1542. /// <param name="diIds"></param>
  1543. /// <returns></returns>
  1544. private async Task<GroupTypeNumberInfo> GroupBookingsNumber(int type, Grp_DelegationInfo info)
  1545. {
  1546. GroupTypeNumberInfo _view = new GroupTypeNumberInfo();
  1547. if (info == null)
  1548. {
  1549. return _view;
  1550. }
  1551. _view.DiId = info.Id;
  1552. _view.GroupName = info.TeamName;
  1553. List<TypeInfo> _types = new List<TypeInfo>();
  1554. #region 计算团组ALLType预订数量
  1555. if (type == 1)
  1556. {
  1557. //接团 客户集团所在地区
  1558. string group_region = string.Empty;
  1559. int group_number = 0;
  1560. if (!string.IsNullOrEmpty(info.ClientUnit))
  1561. {
  1562. var _NewClientData = await _sqlSugar.Queryable<Crm_NewClientData>().Where(it => it.IsDel == 0 && it.Client.Equals(info.ClientUnit)).FirstAsync();
  1563. if (_NewClientData != null)
  1564. {
  1565. var regionInfo = await _sqlSugar.Queryable<Sys_SetData>().Where(it => it.Id == _NewClientData.Lvlid).FirstAsync();
  1566. if (regionInfo != null)
  1567. {
  1568. group_region = regionInfo.Name.Replace("级", "");
  1569. group_number++;
  1570. }
  1571. }
  1572. }
  1573. if (group_number > 0)
  1574. {
  1575. _types.Add(new TypeInfo() { Id = 1, RegionItem = new List<RegionInfo>() { new RegionInfo() { Name = group_region, Number = group_number } } });
  1576. }
  1577. }
  1578. else if (type == 2)
  1579. {
  1580. //酒店
  1581. var hotelInfos = await _sqlSugar.Queryable<Grp_HotelReservations>().Where(it => it.IsDel == 0 && it.DiId == info.Id).ToListAsync();
  1582. if (hotelInfos.Count > 0)
  1583. {
  1584. List<RegionInfo> hotelRegions = new List<RegionInfo>();
  1585. foreach (var item in hotelInfos)
  1586. {
  1587. var hotelNumberInfo = new StatisticsHotelInfo()
  1588. {
  1589. HotelName = item.HotelName,
  1590. SingleRoomNum = item.SingleRoomCount,
  1591. DoubleRoomNum = item.DoubleRoomCount,
  1592. SuiteRoomNum = item.SuiteRoomCount,
  1593. OtherRoomNum = item.OtherRoomCount,
  1594. };
  1595. int hotelRoomTotal = item.SingleRoomCount + item.DoubleRoomCount + item.SuiteRoomCount + item.OtherRoomCount;
  1596. if (hotelRegions.Select(it => it.Name).ToList().Contains(item.City))
  1597. {
  1598. RegionInfo hotelRegion = hotelRegions.Find(it => it.Name.Equals(item.City));
  1599. if (hotelRegion != null)
  1600. {
  1601. hotelRegions.Remove(hotelRegion);
  1602. if (hotelRegion.Data.Count > 0)
  1603. {
  1604. hotelRegion.Data.Add(hotelNumberInfo);
  1605. }
  1606. hotelRegion.Number += hotelRoomTotal;
  1607. hotelRegions.Add(hotelRegion);
  1608. }
  1609. }
  1610. else
  1611. {
  1612. hotelRegions.Add(new RegionInfo() { Name = item.City, Number = hotelRoomTotal, Data = new List<dynamic>() { hotelNumberInfo } });
  1613. }
  1614. }
  1615. _types.Add(new TypeInfo() { Id = 2, RegionItem = hotelRegions });
  1616. }
  1617. }
  1618. else if (type == 3)
  1619. {
  1620. //机票
  1621. var airTicketInfos = await _sqlSugar.Queryable<Grp_AirTicketReservations>()
  1622. .LeftJoin<Grp_CreditCardPayment>((atr, ccp) => atr.Id == ccp.CId && ccp.IsPay == 1)
  1623. .LeftJoin<Sys_SetData>((atr, ccp, sd) => atr.CType == sd.Id)
  1624. .Where((atr, ccp, sd) => atr.IsDel == 0 && atr.DIId == info.Id)
  1625. .Select((atr, ccp, sd) => new { atr.ClientNum, atr.CType, ccp.Payee, AirType = sd.Name })
  1626. .ToListAsync();
  1627. if (airTicketInfos.Count > 0)
  1628. {
  1629. List<RegionInfo> airTicketRegions = new List<RegionInfo>();
  1630. foreach (var item in airTicketInfos)
  1631. {
  1632. var ticketClass = new
  1633. {
  1634. TiketClass = item.AirType,
  1635. Number = item.ClientNum
  1636. };
  1637. if (airTicketRegions.Select(it => it.Name).ToList().Contains(item.Payee))
  1638. {
  1639. RegionInfo airTicketRegion = airTicketRegions.Find(it => it.Name.Equals(item.Payee));
  1640. if (airTicketRegion != null)
  1641. {
  1642. airTicketRegions.Remove(airTicketRegion);
  1643. if (airTicketRegion.Data.Count > 0)
  1644. {
  1645. airTicketRegion.Data.Add(ticketClass);
  1646. }
  1647. airTicketRegion.Number += item.ClientNum;
  1648. airTicketRegions.Add(airTicketRegion);
  1649. }
  1650. }
  1651. else
  1652. {
  1653. airTicketRegions.Add(new RegionInfo() { Name = item.Payee, Number = item.ClientNum, Data = new List<dynamic>() { ticketClass } });
  1654. }
  1655. }
  1656. _types.Add(new TypeInfo() { Id = 3, RegionItem = airTicketRegions });
  1657. }
  1658. }
  1659. else if (type == 4)
  1660. {
  1661. //车辆
  1662. var opInfos = await _sqlSugar.Queryable<Grp_CarTouristGuideGroundReservations>().Where(it => it.IsDel == 0 && it.DiId == info.Id).ToListAsync();
  1663. var opContentInfos = await _sqlSugar.Queryable<Grp_CarTouristGuideGroundReservationsContent>().Where(it => it.IsDel == 0 && it.DiId == info.Id).ToListAsync();
  1664. if (opInfos.Count > 0)
  1665. {
  1666. List<RegionInfo> opRegions = new List<RegionInfo>();
  1667. foreach (var item in opInfos)
  1668. {
  1669. int carNum = 0;
  1670. var opContentInfo = opContentInfos.Where(it => it.CTGGRId == item.Id && it.SId == 91).ToList();
  1671. if (opContentInfo.Count > 0)
  1672. {
  1673. foreach (var item1 in opContentInfo)
  1674. {
  1675. if (item1.Price > 0 && item1.Count > 0)
  1676. {
  1677. carNum += item1.Count;
  1678. }
  1679. }
  1680. }
  1681. if (carNum > 0)
  1682. {
  1683. var opData = new
  1684. {
  1685. ServiceCompany = item.ServiceCompany,
  1686. BusName = item.BusName,
  1687. Numbuer = carNum
  1688. };
  1689. if (opRegions.Select(it => it.Name).ToList().Contains(item.Area))
  1690. {
  1691. RegionInfo opRegion = opRegions.Find(it => it.Name.Equals(item.Area));
  1692. if (opRegion != null)
  1693. {
  1694. opRegions.Remove(opRegion);
  1695. if (opRegion.Data.Count > 0)
  1696. {
  1697. opRegion.Data.Add(opData);
  1698. }
  1699. opRegion.Number += carNum;
  1700. opRegions.Add(opRegion);
  1701. }
  1702. }
  1703. else
  1704. {
  1705. opRegions.Add(new RegionInfo() { Name = item.Area, Number = carNum, Data = new List<dynamic>() { opData } });
  1706. }
  1707. }
  1708. }
  1709. _types.Add(new TypeInfo() { Id = 4, RegionItem = opRegions });
  1710. }
  1711. }
  1712. #endregion
  1713. _view.TypeItem = _types;
  1714. return _view;
  1715. }
  1716. #endregion
  1717. }
  1718. }