IFinance_Logistics_LadingBillRepository.cs 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. using Dapper;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using XYY.Common.Standard;
  7. using XYY.Core.Standard.Data.Infrastructure;
  8. using XYY.Model.Standard;
  9. using XYY.Model.Standard.Finance;
  10. namespace XYY.Data.Standard.Finance
  11. {
  12. public interface IFinance_Logistics_LadingBillRepository : IBaseRepository<Finance_Logistics_LadingBill>
  13. {
  14. Task<Dictionary<int, int>> GetGroupCounts();
  15. Task<PageResult<View_Finance_Logistics_LadingBill>> GetData(QueryModel queryModel);
  16. new Task<PageResult<View_Finance_Logistics_LadingBill>> QueryPager(QueryModel queryModel);
  17. Task<View_Finance_Logistics_LadingBill> GetInfo(string billNumber);
  18. Task UpdateDetailFristFee(string billNumber);
  19. Task UpdateDetailClareFee(string billNumber);
  20. Task<List<View_FinanceBill2>> GetView_FinanceBills(int channelId, int customerId, DateTime start, DateTime end, SettlementCompany? SettlementCompany);
  21. Task<List<View_FinanceBill2>> GetBillsDetails(int channelId, int customerId, DateTime start, DateTime end, SettlementCompany? SettlementCompany);
  22. Task<List<Finance_Logistics_LadingBillLog>> GetLadingBillLogs(int BillId, FinanceIncidentalTypeEnum type);
  23. Task<List<EFFinanceBill>> GetEFFinanceBillGroup(DateTime start, DateTime end, int channelId, int customerId);
  24. Task<List<EFFinanceGroupBill>> GetEFFinanceBillGroupAsChannel(DateTime start, DateTime end);
  25. Task<List<EFFinanceGroupBill>> GetEFFinanceBillGroupAsCustomer(DateTime start, DateTime end);
  26. Task<List<View_FinanceBill2>> GetEFFinanceBillDetails(DateTime start, DateTime end, int channelId, int customerId);
  27. Task<List<EFFinanceGroupBill>> GetFinanceBillGroupAsChannel(DateTime start, DateTime end);
  28. Task<List<EFFinanceGroupBill>> GetFinanceBillGroupAsCustomer(DateTime start, DateTime end);
  29. Task<List<EFFinanceBill>> GetFinanceBillGroup(DateTime start, DateTime end, int channelId, int customerId);
  30. Task<List<View_FinanceBill2>> GetFinanceBillDetails(DateTime start, DateTime end, int channelId, int customerId);
  31. Task<List<View_FinanceBill_WithBranchCompany>> GetFinanceReportDetails(DateTime? receiveStart, DateTime? receiveTimeEnd, int? channelId, int? customerId, int? branchCompanyId, List<string> trackingNumbers, List<string> transferNumbers, List<string> ladingBillNumbers);
  32. Task<List<View_FinanceBill_WithBranchCompany>> GetFinanceSimpleReportDetails(DateTime? receiveStart, DateTime? receiveTimeEnd, int? channelId, int? customerId, int? branchCompanyId, List<string> trackingNumbers, List<string> transferNumbers, List<string> ladingBillNumbers);
  33. Task<PageResult<View_FinanceBill_WithBranchCompany>> QueryFinanceReportDetailPage(QueryModel queryModel);
  34. Task<List<CompletionRate>> GetCompletionRate(DateTime start, DateTime end, int channelId, int customerId);
  35. Task<FinanceBillDetailWeightDifferenceGroup> GetFinanceBillDetailWeightDifferenceGroup(DateTime start, DateTime end, int channelId, int customerId);
  36. Task<List<FinanceBillDetailWeightDifference>> GetDifferenceDetailsAsync(DateTime start, DateTime end, int channelId, int customerId);
  37. /// <summary>
  38. /// 更新状态
  39. /// 收款金额
  40. /// </summary>
  41. /// <param name="CustomerId"></param>
  42. /// <param name="start"></param>
  43. /// <param name="end"></param>
  44. /// <param name="aboutCusetomerCheques">是否更新金额</param>
  45. /// <returns></returns>
  46. Task UpdateFirstBillCusetomerChequesStatus(int CustomerId, DateTime start, DateTime end);
  47. Task CompleteFirstBillCusetomerChequesStatus(int CustomerId, DateTime end);
  48. Task<IEnumerable<Finance_Logistics_LadingBill>> GetLadingBillByChargesDetail(DateTime end, int customerId);
  49. }
  50. public class Finance_Logistics_LadingBillRepository : BaseRepository<Finance_Logistics_LadingBill>, IFinance_Logistics_LadingBillRepository
  51. {
  52. public Finance_Logistics_LadingBillRepository(IUnitOfWork unitOfWork) : base(unitOfWork)
  53. {
  54. }
  55. public async Task DeleteBill(int v, int billId)
  56. {
  57. await DeleteByIdAsync(billId);
  58. await _unitOfWork.Connection.ExecuteAsync("delete Finance_ServiceDeliveryBillDetail where BillId=" + billId, null, _unitOfWork.Transaction);
  59. }
  60. public async Task<PageResult<View_Finance_Logistics_LadingBill>> GetData(QueryModel queryModel)
  61. {
  62. return await _unitOfWork.GetPagingListAsync<View_Finance_Logistics_LadingBill>(queryModel);
  63. }
  64. public async Task<View_Finance_Logistics_LadingBill> GetInfo(string billNumber)
  65. {
  66. return (await _unitOfWork.QueryBySqlAsync<View_Finance_Logistics_LadingBill>("select * from View_Finance_Logistics_LadingBillInfo where LadingBillNumber='" + billNumber + "'")).FirstOrDefault();
  67. }
  68. public async Task<Dictionary<int, int>> GetGroupCounts()
  69. {
  70. string sql = @"select Status as [Key],count(0) as [Value] from Finance_Logistics_LadingBill group by Status";
  71. return (await _unitOfWork.QueryBySqlAsync<dynamic>(sql)).ToDictionary(x => (int)x.Key, x => (int)x.Value);
  72. }
  73. public new async Task<PageResult<View_Finance_Logistics_LadingBill>> QueryPager(QueryModel queryModel)
  74. {
  75. return await _unitOfWork.GetPagingListAsync<View_Finance_Logistics_LadingBill>(queryModel);
  76. }
  77. public async Task UpdateDetailFristFee(string billNumber)
  78. {
  79. await _unitOfWork.ExecuteStoredProcedureAsync("updateDetailFristFee", new { billNumber = billNumber });
  80. }
  81. public async Task UpdateDetailClareFee(string billNumber)
  82. {
  83. await _unitOfWork.ExecuteStoredProcedureAsync("updateDetailClareFee", new { billNumber = billNumber });
  84. }
  85. public async Task<List<View_FinanceBill2>> GetBillsDetails(int channelId, int customerId, DateTime start, DateTime end, SettlementCompany? SettlementCompany)
  86. {
  87. string where = "";
  88. if (channelId != 0)
  89. where += " and CreateOrderChannelId = " + channelId;
  90. if (customerId != 0)
  91. where += " and CustomerId = " + customerId;
  92. if (SettlementCompany.HasValue)
  93. where += " and SettlementCompany = " + SettlementCompany.GetValue();
  94. string sql = $@"
  95. select
  96. h.LadingBillNumber[提单号],
  97. a.TrackingNumber[跟踪号],
  98. a.OPWeight[称重重量],a.ReceiveTime[称重时间],lp.[Name] as [渠道名称],f.CompanyName[所属客户],
  99. case when b.ConfirmedAmount = 0 or b.ConfirmedAmount is null
  100. then d.OPCFreight else b.ConfirmedAmount end + isnull(b.AttachFee,0) [收入],
  101. case when c.ConfirmedAmount = 0 or c.ConfirmedAmount is null
  102. then d.OPSFreight else c.ConfirmedAmount end * isnull(g.ToCNY,0) [末端派送],
  103. d.OPSCurreny,
  104. isnull(ef.OperateCost,0)[营运成本],
  105. case when h.FirstTransferFeeCost = 0 or h.FirstTransferFeeCost is null
  106. then ef.FirstTransferPrice * a.OPWeight /1000.0 else h.FirstTransferFeeCost end [一级中转],
  107. case when h.SecondTransferFeeCost = 0 or h.SecondTransferFeeCost is null
  108. then ef.SecondTransferPrice * a.OPWeight /1000.0 else h.SecondTransferFeeCost end [二级中转],
  109. case when h.DomesticClearFeeCost = 0 or h.DomesticClearFeeCost is null
  110. then ef.ClearanceUnitWeightPrice * a.OPWeight /1000.0
  111. else h.DomesticClearFeeCost end [出口报关],
  112. case when h.FristFeeCost = 0 or h.FristFeeCost is null then
  113. ef. FirstLegUnitPrice * a.OPWeight/1000 else h.FristFeeCost end [空运成本],
  114. case when h.OverseasClearFeeCost =0 or h.OverseasClearFeeCost is null
  115. then ef.ClearanceUnitWeightPrice * a.OPWeight/1000.0 * isnull(effc.ToCNY,0) else h.OverseasClearFeeCost * isnull(ocfc.ToCny,0) end[清关成本],
  116. case when h.TaxFeeCost = 0 or h.TaxFeeCost is null then
  117. ef.TaxPrice * a.OPWeight /1000 * isnull(eftax.ToCNY,0) else isnull(h.TaxFeeCost,0)*isnull(tfc.ToCny,0) end [关税],
  118. case when h.OverSeaTransferFeeCost =0 or h.OverseasClearFeeCost is null
  119. then ef.OverSeaTransferPrice * a.OPWeight/1000.0 * ISNULL(eftfc.ToCNY,0)
  120. else isnull(h.OverSeaTransferFeeCost,0)*isnull(ostfc.ToCny,0) end [海外中转],
  121. case when h.Subsidy =0 or h.Subsidy is null
  122. then ef.SubsidyPrice * isnull(efsc.ToCNY,0) else isnull(h.Subsidy,0)*isnull(sc.ToCny,0) end[补贴]
  123. from Order_Order(nolock) a
  124. left join
  125. (select sum(isnull( ConfirmedAmount,0)) as ConfirmedAmount,TrackingNumber,sum(isnull(AttachFee,0)) as AttachFee from Finance_CustomerDeliveryBillDetail(nolock)group by trackingnumber) b on a.TrackingNumber=b.TrackingNumber
  126. left join
  127. (select sum(isnull( ConfirmedAmount,0)) as ConfirmedAmount,TrackingNumber from Finance_ServiceDeliveryBillDetail(nolock)group by trackingnumber) c on c.TrackingNumber=a.TrackingNumber
  128. left join
  129. Order_Fee(nolock) d on d.OrderId = a.Id
  130. left join
  131. Logistics_Channel(nolock) e on e.id=a.CreateOrderChannelId
  132. left join
  133. User_Customer(nolock) f on f.Id=a.CustomerId
  134. left join
  135. Finance_Rate(nolock) g on g.Currency = d.OPSCurreny
  136. and convert(date,a.ReceiveTime) between
  137. convert(date, g.StartDate) and convert(date, g.EndDate)
  138. left join
  139. Finance_OrderCost(nolock) h on h.OrderId = a.Id
  140. left join Logistics_Public(nolock) lp on lp.Code = e.PublicCode
  141. left join Finance_Logistics_LadingBill(nolock) llb on llb.BillNumber = h.LadingBillNumber
  142. outer apply( select top 1 * from Finance_EstimateFee(nolock) ef where ef.ChannelId = lp.Id
  143. and ef.EffectiveDate<a.ReceiveTime order by ef.EffectiveDate desc)ef
  144. left join
  145. Finance_Rate(nolock) ocfc on ocfc.Currency = h.OverseasClearFeeCurreny and
  146. convert(date, llb.OverseasClearBillTime)
  147. between convert(date, ocfc.StartDate) and convert(date, ocfc.EndDate)
  148. left join
  149. Finance_Rate(nolock) tfc on tfc.Currency = h.TaxFeeCurreny and
  150. convert(date, llb.TaxBillTime)
  151. between convert(date, tfc.StartDate) and convert(date, tfc.EndDate)
  152. left join
  153. Finance_Rate(nolock) ostfc on ostfc.Currency = h.OverSeaTransferFeeCurreny and
  154. convert(date, llb.OverSeaTransferBillTime)
  155. between convert(date, ostfc.StartDate) and convert(date, ostfc.EndDate)
  156. left join
  157. Finance_Rate(nolock) sc on sc.Currency = h.SubsidyCurreny and
  158. convert(date, llb.SubsidyBillTime)
  159. between convert(date, sc.StartDate) and convert(date,sc.EndDate)
  160. ---预计汇率
  161. left join
  162. Finance_Rate(nolock) effc on effc.Currency = ef.ClearancePriceCurrency and
  163. convert(date,a.ReceiveTime)
  164. between convert(date, effc.StartDate) and convert(date, effc.EndDate)
  165. left join
  166. Finance_Rate(nolock) eftax on eftax.Currency = ef.TaxPriceCurrency and
  167. convert(date,a.ReceiveTime)
  168. between convert(date, eftax.StartDate) and convert(date, eftax.EndDate)
  169. left join
  170. Finance_Rate(nolock) eftfc on eftfc.Currency = ef.OverSeaTransferPriceCurrency and
  171. convert(date, a.ReceiveTime)
  172. between convert(date, eftfc.StartDate) and convert(date, eftfc.EndDate)
  173. left join
  174. Finance_Rate(nolock) efsc on efsc.Currency = ef.SubsidyPriceCurrency and
  175. convert(date, a.ReceiveTime)
  176. between convert(date, efsc.StartDate) and convert(date,efsc.EndDate)
  177. where a.ReceiveTime between '{start.ToString_yyyyMMdd()}' and '{end.ToString_yyyyMMdd()}' {where}
  178. and a.OrderStatus>1
  179. ";
  180. return (await _unitOfWork.QueryBySqlAsync<View_FinanceBill2>(sql, 360)).ToList();
  181. }
  182. public async Task<List<EFFinanceBill>> GetEFFinanceBillGroup(DateTime start, DateTime end, int channelId, int customerId)
  183. {
  184. string where = "";
  185. if (channelId != 0)
  186. where += " and CreateOrderChannelId = " + channelId;
  187. if (customerId != 0)
  188. where += " and CustomerId = " + customerId;
  189. string sql = $@"
  190. select CONVERT(nvarchar(10),称重时间, 120)称重时间,sum(毛利) as 毛利,
  191. sum(收入)收入,sum(毛利)/sum(收入)毛利占比 from EFFinanceBillDetail
  192. where 称重时间 between '{start}' and '{end.AddDays(1)}' {where}
  193. group by CONVERT(nvarchar(10),称重时间, 120)
  194. order by CONVERT(nvarchar(10),称重时间, 120)
  195. ";
  196. return (await _unitOfWork.QueryBySqlAsync<EFFinanceBill>(sql, 360)).ToList();
  197. }
  198. public async Task<List<View_FinanceBill2>> GetEFFinanceBillDetails(DateTime start, DateTime end, int channelId, int customerId)
  199. {
  200. string where = "";
  201. if (channelId != 0)
  202. where += " and CreateOrderChannelId = " + channelId;
  203. if (customerId != 0)
  204. where += " and CustomerId = " + customerId;
  205. string sql = $@"
  206. select * from EFFinanceBillDetail(nolock)
  207. where 称重时间 between '{start}' and '{end.AddDays(1)}' {where}
  208. ";
  209. return (await _unitOfWork.QueryBySqlAsync<View_FinanceBill2>(sql, 360)).ToList();
  210. }
  211. public async Task<List<EFFinanceBill>> GetEFFinanceBillCustomerEveryDay(
  212. DateTime start,
  213. DateTime end)
  214. {
  215. string sql = $@"
  216. select [所属客户],CONVERT(nvarchar(10),称重时间, 120)称重时间,sum(毛利) as 毛利,sum(收入)收入,sum(毛利)/sum(收入)毛利占比 from EFFinanceBillDetail
  217. where 称重时间 between '{start}' and '{end.AddDays(1)}' and 收入>0
  218. group by CONVERT(nvarchar(10),称重时间, 120),[所属客户]
  219. order by CONVERT(nvarchar(10),称重时间, 120)
  220. ";
  221. return (await _unitOfWork.QueryBySqlAsync<EFFinanceBill>(sql, 360)).ToList();
  222. }
  223. public async Task<List<EFFinanceBill>> GetEFFinanceBillChannelEveryDay(
  224. DateTime start,
  225. DateTime end)
  226. {
  227. string sql = $@"
  228. select [渠道名称],CONVERT(nvarchar(10),称重时间, 120)称重时间,sum(毛利) as 毛利,sum(收入)收入,sum(毛利)/sum(收入)毛利占比 from EFFinanceBillDetail
  229. where 称重时间 between '{start}' and '{end.AddDays(1)}' and 收入>0
  230. group by CONVERT(nvarchar(10),称重时间, 120),[渠道名称]
  231. order by CONVERT(nvarchar(10),称重时间, 120)
  232. ";
  233. return (await _unitOfWork.QueryBySqlAsync<EFFinanceBill>(sql, 360)).ToList();
  234. }
  235. public async Task<List<EFFinanceGroupBill>> GetEFFinanceBillGroupAsChannel(DateTime start, DateTime end)
  236. {
  237. string sql = $@"
  238. select [渠道名称],
  239. sum(收入) 预计收入,
  240. count(0) 收货单量,
  241. sum(称重重量/1000.0) 收货重量,
  242. sum(毛利) as 毛利,
  243. sum(毛利)/sum(收入)毛利占比,
  244. sum(毛利)/count(0) 单件毛利,CreateOrderChannelId
  245. from EFFinanceBillDetail
  246. where 称重时间 between '{start}' and '{end.AddDays(1)}'
  247. and 收入>0
  248. group by [渠道名称],CreateOrderChannelId
  249. order by SUM([收入]) desc";
  250. var allChannelData = (await _unitOfWork.QueryBySqlAsync<EFFinanceGroupBill>(sql, 360)).ToList();
  251. var everyDay = await GetEFFinanceBillChannelEveryDay(start, end);
  252. foreach (var custometData in allChannelData)
  253. {
  254. custometData.EveryDayBill = everyDay.Where(x => x.渠道名称 == custometData.渠道名称).ToList();
  255. }
  256. return allChannelData;
  257. }
  258. public async Task<List<EFFinanceGroupBill>> GetEFFinanceBillGroupAsCustomer(DateTime start, DateTime end)
  259. {
  260. string sql = $@"
  261. select [所属客户],
  262. sum(收入) 预计收入,
  263. count(0) 收货单量,
  264. sum(称重重量/1000.0) 收货重量,
  265. sum(毛利) as 毛利,
  266. sum(毛利)/sum(收入)毛利占比,
  267. sum(毛利)/count(0) 单件毛利,CustomerId
  268. from EFFinanceBillDetail
  269. where 称重时间 between '{start}' and '{end.AddDays(1)}'
  270. and 收入>0
  271. group by [所属客户],CustomerId
  272. order by SUM([收入]) desc";
  273. var allCustomerData = (await _unitOfWork.QueryBySqlAsync<EFFinanceGroupBill>(sql, 360)).ToList();
  274. var everyDay = await GetEFFinanceBillCustomerEveryDay(start, end);
  275. foreach (var custometData in allCustomerData)
  276. {
  277. custometData.EveryDayBill = everyDay.Where(x => x.所属客户 == custometData.所属客户).ToList();
  278. }
  279. return allCustomerData;
  280. }
  281. public async Task<List<EFFinanceBill>> GetFinanceBillGroup(DateTime start, DateTime end, int channelId, int customerId)
  282. {
  283. string where = "";
  284. if (channelId != 0)
  285. where += " and CreateOrderChannelId = " + channelId;
  286. if (customerId != 0)
  287. where += " and CustomerId = " + customerId;
  288. string sql = $@"
  289. select CONVERT(nvarchar(10),称重时间, 120)称重时间,sum(毛利) as 毛利,
  290. sum(收入)收入,
  291. case when sum(isnull(收入,0))=0 then 0 else sum(毛利)/sum(收入) end 毛利占比 from FinanceBillDetail
  292. where 称重时间 between '{start}' and '{end.AddDays(1)}' {where}
  293. group by CONVERT(nvarchar(10),称重时间, 120)
  294. order by CONVERT(nvarchar(10),称重时间, 120)
  295. ";
  296. return (await _unitOfWork.QueryBySqlAsync<EFFinanceBill>(sql, 360)).ToList();
  297. }
  298. public async Task<List<View_FinanceBill2>> GetFinanceBillDetails(DateTime start, DateTime end, int channelId, int customerId)
  299. {
  300. string where = "";
  301. if (channelId != 0)
  302. where += " and CreateOrderChannelId = " + channelId;
  303. if (customerId != 0)
  304. where += " and CustomerId = " + customerId;
  305. string sql = $@"
  306. select * from FinanceBillDetail(nolock)
  307. where 称重时间 between '{start}' and '{end.AddDays(1)}' {where}
  308. ";
  309. return (await _unitOfWork.QueryBySqlAsync<View_FinanceBill2>(sql, 360)).ToList();
  310. }
  311. private string CreateFinanceReportDetailWhere(DateTime? receiveStart, DateTime? receiveTimeEnd, int? channelId, int? customerId, int? branchCompanyId, List<string> trackingNumbers, List<string> transferNumbers, List<string> ladingBillNumbers)
  312. {
  313. string where = "";
  314. if (channelId != null && channelId != 0)
  315. where += " and CreateOrderChannelId = " + channelId;
  316. if (customerId != null && customerId != 0)
  317. where += " and CustomerId = " + customerId;
  318. if (branchCompanyId != null && branchCompanyId != 0)
  319. where += " and BranchCompanyId = " + branchCompanyId;
  320. if (trackingNumbers?.Any() == true)
  321. {
  322. var p = string.Join(",", trackingNumbers.Select(i => $"'{i}'"));
  323. where += $" and 跟踪号 in ({p})";
  324. }
  325. if (transferNumbers?.Any() == true)
  326. {
  327. var p = string.Join(",", transferNumbers.Select(i => $"'{i}'"));
  328. where += $" and 订单号 in ({p})";
  329. }
  330. if (ladingBillNumbers?.Any() == true)
  331. {
  332. var p = string.Join(",", ladingBillNumbers.Select(i => $"'{i}'"));
  333. where += $" and 提单号 in ({p})";
  334. }
  335. if (receiveStart.HasValue)
  336. {
  337. var start = receiveStart.Value.ToString("yyyy-MM-dd 00:00:00");
  338. var p = $" and 称重时间 >='{start}'";
  339. where += p;
  340. }
  341. if (receiveTimeEnd.HasValue)
  342. {
  343. var end = receiveTimeEnd.Value.AddDays(1);
  344. var endStr = end.ToString("yyyy-MM-dd 00:00:00");
  345. var p = $" and 称重时间 <'{endStr}'";
  346. where += p;
  347. }
  348. return where;
  349. }
  350. public async Task<List<View_FinanceBill_WithBranchCompany>> GetFinanceSimpleReportDetails(DateTime? receiveStart, DateTime? receiveTimeEnd, int? channelId, int? customerId, int? branchCompanyId, List<string> trackingNumbers, List<string> transferNumbers, List<string> ladingBillNumbers)
  351. {
  352. string where = CreateFinanceReportDetailWhere(receiveStart, receiveTimeEnd, channelId, customerId, branchCompanyId, trackingNumbers, transferNumbers, ladingBillNumbers);
  353. where += " and PublicChannelId not in(9,15,16,21,32,38,39,42,43,54,55,56,63,64,65) and CustomerId not in(227,294)";
  354. string sql = $@"select * from View_FinanceBill_WithBranchCompanySimple(nolock) where 1=1 {where} ";
  355. return (await _unitOfWork.QueryBySqlAsync<View_FinanceBill_WithBranchCompany>(sql, 360)).ToList();
  356. }
  357. public async Task<List<View_FinanceBill_WithBranchCompany>> GetFinanceReportDetails(DateTime? receiveStart, DateTime? receiveTimeEnd, int? channelId, int? customerId, int? branchCompanyId, List<string> trackingNumbers, List<string> transferNumbers, List<string> ladingBillNumbers)
  358. {
  359. string where = CreateFinanceReportDetailWhere(receiveStart, receiveTimeEnd, channelId, customerId, branchCompanyId, trackingNumbers, transferNumbers, ladingBillNumbers);
  360. string sql = $@"select * from View_FinanceBill_WithBranchCompany(nolock) where 1=1 {where} ";
  361. return (await _unitOfWork.QueryBySqlAsync<View_FinanceBill_WithBranchCompany>(sql, 360)).ToList();
  362. }
  363. public async Task<PageResult<View_FinanceBill_WithBranchCompany>> QueryFinanceReportDetailPage(QueryModel queryModel)
  364. {
  365. return await _unitOfWork.GetPagingListAsync<View_FinanceBill_WithBranchCompany>(queryModel);
  366. }
  367. public async Task<List<FinanceBillDetailWeightDifference>> GetDifferenceDetailsAsync(DateTime start, DateTime end, int channelId, int customerId)
  368. {
  369. string where = "";
  370. if (channelId != 0)
  371. where += " and CreateOrderChannelId = " + channelId;
  372. if (customerId != 0)
  373. where += " and CustomerId = " + customerId;
  374. string sql = $"select * from FinanceBillDetailWeightDifference(nolock) where 称重时间 between '{start}' and '{end}' {where}";
  375. return (await _unitOfWork.QueryBySqlAsync<FinanceBillDetailWeightDifference>(sql)).ToList();
  376. }
  377. public async Task<FinanceBillDetailWeightDifferenceGroup> GetFinanceBillDetailWeightDifferenceGroup(DateTime start, DateTime end, int channelId, int customerId)
  378. {
  379. string where = "";
  380. if (channelId != 0)
  381. where += " and CreateOrderChannelId = " + channelId;
  382. if (customerId != 0)
  383. where += " and CustomerId = " + customerId;
  384. string sql = $@"select
  385. avg(末端派送差异) as 末端派送差异_票,
  386. avg(营运成本差异) as 营运成本差异_票,
  387. avg(一级中转差异) as 一级中转差异_票,
  388. avg(二级中转差异) as 二级中转差异_票,
  389. avg(出口报关差异) as 出口报关差异_票,
  390. avg(空运成本差异) as 空运成本差异_票,
  391. avg(海外清关差异) as 海外清关差异_票,
  392. avg(关税差异) as 关税差异_票,
  393. avg(海外中转差异) as 海外中转差异_票,
  394. avg(补贴差异) as 补贴差异_票,
  395. sum(末端派送差异)/(sum(称重重量)/1000.0) as 末端派送差异_KG,
  396. avg(营运成本差异)/(sum(称重重量)/1000.0) as 营运成本差异_KG,
  397. avg(一级中转差异)/(sum(称重重量)/1000.0) as 一级中转差异_KG,
  398. avg(二级中转差异)/(sum(称重重量)/1000.0) as 二级中转差异_KG,
  399. avg(出口报关差异)/(sum(称重重量)/1000.0) as 出口报关差异_KG,
  400. avg(空运成本差异)/(sum(称重重量)/1000.0) as 空运成本差异_KG,
  401. avg(海外清关差异)/(sum(称重重量)/1000.0) as 海外清关差异_KG,
  402. avg(关税差异)/(sum(称重重量)/1000.0) as 关税差异_KG,
  403. avg(海外中转差异)/(sum(称重重量)/1000.0) as 海外中转差异_KG,
  404. avg(补贴差异)/(sum(称重重量)/1000.0) as 补贴差异_KG,
  405. case when sum(末端派送)=0 then 1 else sum(末端派送差异)/SUM(末端派送) end as 末端派送差异_比例,
  406. case when sum(营运成本)=0 then 1 else sum(营运成本差异)/SUM(营运成本)end as 营运成本差异_比例,
  407. case when sum(一级中转)=0 then 1 else sum(一级中转差异)/SUM(一级中转)end as 一级中转差异_比例,
  408. case when sum(二级中转)=0 then 1 else sum(二级中转差异)/SUM(二级中转)end as 二级中转差异_比例,
  409. case when sum(出口报关)=0 then 1 else sum(出口报关差异)/SUM(出口报关)end as 出口报关差异_比例,
  410. case when sum(空运成本)=0 then 1 else sum(空运成本差异)/SUM(空运成本)end as 空运成本差异_比例,
  411. case when sum(海外清关)=0 then 1 else sum(海外清关差异)/SUM(海外清关)end as 海外清关差异_比例,
  412. case when sum(关税差异)=0 then 1 else sum(关税差异)/SUM(关税差异)end as 关税差异_比例,
  413. case when sum(海外中转)=0 then 1 else sum(海外中转差异)/SUM(海外中转)end as 海外中转差异_比例,
  414. case when sum(补贴)=0 then 1 else sum(补贴差异)/SUM(补贴)end as 补贴差异_比例
  415. from FinanceBillDetailWeightDifference(nolock)
  416. where 称重时间 between '{start}' and '{end}' {where}
  417. ";
  418. return (await _unitOfWork.QueryBySqlAsync<FinanceBillDetailWeightDifferenceGroup>(sql, 360)).FirstOrDefault();
  419. }
  420. public async Task<List<EFFinanceBill>> GetFinanceBillCustomerEveryDay(
  421. DateTime start,
  422. DateTime end)
  423. {
  424. string sql = $@"
  425. select [所属客户],CONVERT(nvarchar(10),称重时间, 120)称重时间,sum(毛利) as 毛利,sum(收入)收入,
  426. case when sum(isnull(收入,0))=0 then 0 else sum(毛利)/sum(收入) end 毛利占比 from FinanceBillDetail
  427. where 称重时间 between '{start}' and '{end.AddDays(1)}'
  428. group by CONVERT(nvarchar(10),称重时间, 120),[所属客户]
  429. order by CONVERT(nvarchar(10),称重时间, 120)
  430. ";
  431. return (await _unitOfWork.QueryBySqlAsync<EFFinanceBill>(sql, 360)).ToList();
  432. }
  433. public async Task<List<EFFinanceBill>> GetFinanceBillChannelEveryDay(
  434. DateTime start,
  435. DateTime end)
  436. {
  437. string sql = $@"
  438. select [渠道名称],CONVERT(nvarchar(10),称重时间, 120)称重时间,sum(毛利) as 毛利,sum(收入)收入,
  439. case when sum(isnull(收入,0))=0 then 0 else sum(毛利)/sum(收入) end 毛利占比 from FinanceBillDetail
  440. where 称重时间 between '{start}' and '{end.AddDays(1)}'
  441. group by CONVERT(nvarchar(10),称重时间, 120),[渠道名称]
  442. order by CONVERT(nvarchar(10),称重时间, 120)
  443. ";
  444. return (await _unitOfWork.QueryBySqlAsync<EFFinanceBill>(sql, 360)).ToList();
  445. }
  446. public async Task<List<EFFinanceGroupBill>> GetFinanceBillGroupAsChannel(DateTime start, DateTime end)
  447. {
  448. string sql = $@"
  449. select [渠道名称],
  450. sum(收入) 预计收入,
  451. count(0) 收货单量,
  452. sum(称重重量/1000.0) 收货重量,
  453. sum(毛利) as 毛利,
  454. case when sum(isnull(收入,0))=0 then 0 else sum(毛利)/sum(收入) end 毛利占比,
  455. sum(毛利)/count(0) 单件毛利,CreateOrderChannelId
  456. from FinanceBillDetail
  457. where 称重时间 between '{start}' and '{end.AddDays(1)}'
  458. group by [渠道名称],CreateOrderChannelId
  459. order by SUM([收入]) desc";
  460. var allChannelData = (await _unitOfWork.QueryBySqlAsync<EFFinanceGroupBill>(sql, 360)).ToList();
  461. var everyDay = await GetEFFinanceBillChannelEveryDay(start, end);
  462. foreach (var custometData in allChannelData)
  463. {
  464. custometData.EveryDayBill = everyDay.Where(x => x.渠道名称 == custometData.渠道名称).ToList();
  465. }
  466. return allChannelData;
  467. }
  468. public async Task<List<EFFinanceGroupBill>> GetFinanceBillGroupAsCustomer(DateTime start, DateTime end)
  469. {
  470. string sql = $@"
  471. select [所属客户],
  472. sum(收入) 预计收入,
  473. count(0) 收货单量,
  474. sum(称重重量/1000.0) 收货重量,
  475. sum(毛利) as 毛利,
  476. case when sum(isnull(收入,0))=0 then 0 else sum(毛利)/sum(收入) end 毛利占比,
  477. sum(毛利)/count(0) 单件毛利,CustomerId
  478. from FinanceBillDetail
  479. where 称重时间 between '{start}' and '{end.AddDays(1)}'
  480. group by [所属客户],CustomerId
  481. order by SUM([收入]) desc";
  482. var allCustomerData = (await _unitOfWork.QueryBySqlAsync<EFFinanceGroupBill>(sql, 360)).ToList();
  483. var everyDay = await GetEFFinanceBillCustomerEveryDay(start, end);
  484. foreach (var custometData in allCustomerData)
  485. {
  486. custometData.EveryDayBill = everyDay.Where(x => x.所属客户 == custometData.所属客户).ToList();
  487. }
  488. return allCustomerData;
  489. }
  490. public async Task<List<View_FinanceBill2>> GetView_FinanceBills(int channelId, int customerId, DateTime start, DateTime end, SettlementCompany? SettlementCompany)
  491. {
  492. string where = "";
  493. //if (Finance_Companys.HasValue && Finance_Companys > 0)
  494. //{
  495. // if (Finance_Companys == FinanceCompanys.行运易)
  496. // {
  497. // where += " and CustomerId <>1" +
  498. // " and CreateOrderChannelId <> 48 and CreateOrderChannelId<>49 and CreateOrderChannelId<>50 and CreateOrderChannelId<>101 and CreateOrderChannelId<>106";
  499. // }
  500. // else if (Finance_Companys == FinanceCompanys.逸达)
  501. // {
  502. // where += " and CustomerId =1 and CreateOrderChannelId <> 48 and CreateOrderChannelId<>49 and CreateOrderChannelId<>50 and CreateOrderChannelId<>101 and CreateOrderChannelId<>106";
  503. // }
  504. // else if (Finance_Companys == FinanceCompanys.朗天)
  505. // {
  506. // where += " and (CreateOrderChannelId = 48 or CreateOrderChannelId = 49 or CreateOrderChannelId = 50 or CreateOrderChannelId = 101 or CreateOrderChannelId = 106)";
  507. // }
  508. //}
  509. //else
  510. //{
  511. if (channelId != 0)
  512. where += " and CreateOrderChannelId = " + channelId;
  513. if (customerId != 0)
  514. where += " and CustomerId = " + customerId;
  515. if (SettlementCompany.HasValue)
  516. where += " and SettlementCompany = " + SettlementCompany.GetValue();
  517. //}
  518. string sql = $@"
  519. select
  520. e.PublicName as [渠道名称],f.CompanyName[所属客户],
  521. sum( case when b.ConfirmedAmount = 0 or b.ConfirmedAmount is null
  522. then d.OPCFreight else b.ConfirmedAmount end + isnull(b.AttachFee,0)) [收入],
  523. sum( case when c.ConfirmedAmount = 0 or c.ConfirmedAmount is null
  524. then d.OPSFreight else c.ConfirmedAmount end * isnull(g.ToCNY,0)) [末端派送],
  525. sum( isnull(ef.OperateCost,0))[营运成本],
  526. sum( case when h.FirstTransferFeeCost = 0 or h.FirstTransferFeeCost is null
  527. then ef.FirstTransferPrice * a.OPWeight /1000.0 else h.FirstTransferFeeCost end) [一级中转],
  528. sum( case when h.SecondTransferFeeCost = 0 or h.SecondTransferFeeCost is null
  529. then ef.SecondTransferPrice * a.OPWeight /1000.0 else h.SecondTransferFeeCost end) [二级中转],
  530. sum( case when h.DomesticClearFeeCost = 0 or h.DomesticClearFeeCost is null
  531. then ef.ClearanceUnitWeightPrice * a.OPWeight /1000.0
  532. else h.DomesticClearFeeCost end) [出口报关],
  533. sum( case when h.FristFeeCost = 0 or h.FristFeeCost is null then
  534. ef. FirstLegUnitPrice * a.OPWeight/1000 else h.FristFeeCost end ) [空运成本],
  535. sum( case when h.OverseasClearFeeCost =0 or h.OverseasClearFeeCost is null
  536. then ef.ClearanceUnitWeightPrice * a.OPWeight/1000.0 * isnull(effc.ToCNY,0) else h.OverseasClearFeeCost * isnull(ocfc.ToCny,0) end)[清关成本],
  537. sum( case when h.TaxFeeCost = 0 or h.TaxFeeCost is null then
  538. ef.TaxPrice * a.OPWeight /1000 * isnull(eftax.ToCNY,0) else isnull(h.TaxFeeCost,0)*isnull(tfc.ToCny,0) end) [关税],
  539. sum( case when h.OverSeaTransferFeeCost =0 or h.OverseasClearFeeCost is null
  540. then ef.OverSeaTransferPrice * a.OPWeight/1000.0 * ISNULL(eftfc.ToCNY,0)
  541. else isnull(h.OverSeaTransferFeeCost,0)*isnull(ostfc.ToCny,0) end) [海外中转],
  542. sum( case when h.Subsidy =0 or h.Subsidy is null
  543. then ef.SubsidyPrice * isnull(efsc.ToCNY,0) else isnull(h.Subsidy,0)*isnull(sc.ToCny,0) end)[补贴]
  544. from Order_Order(nolock) a
  545. left join
  546. (select sum(isnull( ConfirmedAmount,0)) as ConfirmedAmount,TrackingNumber,sum(isnull(AttachFee,0)) as AttachFee from Finance_CustomerDeliveryBillDetail(nolock)group by trackingnumber) b on a.TrackingNumber=b.TrackingNumber
  547. left join
  548. (select sum(isnull( ConfirmedAmount,0)) as ConfirmedAmount,TrackingNumber from Finance_ServiceDeliveryBillDetail(nolock)group by trackingnumber) c on c.TrackingNumber=a.TrackingNumber
  549. left join
  550. Order_Fee(nolock) d on d.OrderId = a.Id
  551. left join
  552. Logistics_Channel(nolock) e on e.id=a.CreateOrderChannelId
  553. left join
  554. User_Customer(nolock) f on f.Id=a.CustomerId
  555. left join
  556. Finance_Rate(nolock) g on g.Currency = d.OPSCurreny
  557. and convert(date,a.ReceiveTime) between
  558. convert(date, g.StartDate) and convert(date, g.EndDate)
  559. left join
  560. Finance_OrderCost(nolock) h on h.OrderId = a.Id
  561. left join Logistics_Public(nolock) lp on lp.Code = e.PublicCode
  562. left join Finance_Logistics_LadingBill(nolock) llb on llb.BillNumber = h.LadingBillNumber
  563. outer apply( select top 1 * from Finance_EstimateFee(nolock) ef where ef.ChannelId = lp.Id and ef.EffectiveDate<a.ReceiveTime order by ef.EffectiveDate desc)ef
  564. left join
  565. Finance_Rate(nolock) ocfc on ocfc.Currency = h.OverseasClearFeeCurreny and
  566. convert(date, llb.OverseasClearBillTime)
  567. between convert(date, ocfc.StartDate) and convert(date, ocfc.EndDate)
  568. left join
  569. Finance_Rate(nolock) tfc on tfc.Currency = h.TaxFeeCurreny and
  570. convert(date, llb.TaxBillTime)
  571. between convert(date, tfc.StartDate) and convert(date, tfc.EndDate)
  572. left join
  573. Finance_Rate(nolock) ostfc on ostfc.Currency = h.OverSeaTransferFeeCurreny and
  574. convert(date, llb.OverSeaTransferBillTime)
  575. between convert(date, ostfc.StartDate) and convert(date, ostfc.EndDate)
  576. left join
  577. Finance_Rate(nolock) sc on sc.Currency = h.SubsidyCurreny and
  578. convert(date, llb.SubsidyBillTime)
  579. between convert(date, sc.StartDate) and convert(date,sc.EndDate)
  580. ---预计汇率
  581. left join
  582. Finance_Rate(nolock) effc on effc.Currency = ef.ClearancePriceCurrency and
  583. convert(date,a.ReceiveTime)
  584. between convert(date, effc.StartDate) and convert(date, effc.EndDate)
  585. left join
  586. Finance_Rate(nolock) eftax on eftax.Currency = ef.TaxPriceCurrency and
  587. convert(date,a.ReceiveTime)
  588. between convert(date, eftax.StartDate) and convert(date, eftax.EndDate)
  589. left join
  590. Finance_Rate(nolock) eftfc on eftfc.Currency = ef.OverSeaTransferPriceCurrency and
  591. convert(date, a.ReceiveTime)
  592. between convert(date, eftfc.StartDate) and convert(date, eftfc.EndDate)
  593. left join
  594. Finance_Rate(nolock) efsc on efsc.Currency = ef.SubsidyPriceCurrency and
  595. convert(date, a.ReceiveTime)
  596. between convert(date, efsc.StartDate) and convert(date,efsc.EndDate)
  597. where a.ReceiveTime between '{start.ToString_yyyyMMdd()}' and '{end.ToString_yyyyMMdd()}' {where}
  598. and a.OrderStatus>1
  599. group by e.PublicName,f.CompanyName ";
  600. return (await _unitOfWork.QueryBySqlAsync<View_FinanceBill2>(sql, 360)).ToList();
  601. }
  602. public async Task<List<Finance_Logistics_LadingBillLog>> GetLadingBillLogs(int BillId, FinanceIncidentalTypeEnum type)
  603. {
  604. string name = "";
  605. switch (type)
  606. {
  607. case FinanceIncidentalTypeEnum.头程杂费:
  608. name = "PaymentFristFee";
  609. break;
  610. case FinanceIncidentalTypeEnum.国内清关杂费:
  611. name = "PaymentDomesticClearFee";
  612. break;
  613. case FinanceIncidentalTypeEnum.国外清关杂费:
  614. name = "PaymentOverseasClearFee";
  615. break;
  616. case FinanceIncidentalTypeEnum.收款:
  617. name = "CusetomerCheques";
  618. break;
  619. default:
  620. name = "";
  621. break;
  622. }
  623. string sql = $@"
  624. select a.OldValue,a.NewValue,a.CreateTime ,b.NewValue as Remark from Finance_Logistics_LadingBillLog(nolock) a left join Finance_Logistics_LadingBillLog(nolock)b on a.CreateTime=b.CreateTime and b.field= 'Remark'
  625. where a.Id={BillId} and a.Field = 'PaymentFristFee' ";
  626. var list = (await _unitOfWork.QueryBySqlAsync<Finance_Logistics_LadingBillLog>(sql)).ToList();
  627. return list;
  628. }
  629. public Task<List<EFFinanceBill>> GetEFFinanceBillGroup(DateTime start, DateTime end)
  630. {
  631. throw new NotImplementedException();
  632. }
  633. public async Task<List<CompletionRate>> GetCompletionRate(DateTime start, DateTime end, int channelId, int customerId)
  634. {
  635. string where = "";
  636. if (channelId != 0)
  637. where += " and CreateOrderChannelId = " + channelId;
  638. if (customerId != 0)
  639. where += " and CustomerId = " + customerId;
  640. string sql = $@"
  641. select
  642. sum(case when 末端派送 =0 then 0 else 1 end) /convert(decimal(18,2), count(0)) as 末端派送,
  643. sum(case when 营运成本 =0 then 0 else 1 end) / convert(decimal(18,2), count(0)) as 营运成本,
  644. sum(case when 一级中转 =0 then 0 else 1 end) / convert(decimal(18,2), count(0)) as 一级中转,
  645. sum(case when 二级中转 =0 then 0 else 1 end) / convert(decimal(18,2), count(0)) as 二级中转,
  646. sum(case when 出口报关 =0 then 0 else 1 end) / convert(decimal(18,2), count(0)) as 出口报关,
  647. sum(case when 空运成本 =0 then 0 else 1 end) / convert(decimal(18,2), count(0)) as 空运成本,
  648. sum(case when 海外清关 =0 then 0 else 1 end) / convert(decimal(18,2), count(0)) as 海外清关,
  649. sum(case when 关税 =0 then 0 else 1 end) / convert(decimal(18,2), count(0)) as 关税,
  650. sum(case when 海外中转 =0 then 0 else 1 end) / convert(decimal(18,2), count(0)) as 海外中转,
  651. sum(case when 补贴 =0 then 0 else 1 end) / convert(decimal(18,2), count(0)) as 补贴
  652. from FinanceBillDetail
  653. where 称重时间 between '{start}' and '{end.AddDays(1)} {where}'
  654. ";
  655. var allCustomerData = (await _unitOfWork.QueryBySqlAsync<CompletionRate>(sql, 360)).ToList();
  656. return allCustomerData;
  657. }
  658. public async Task UpdateFirstBillCusetomerChequesStatus(int CustomerId, DateTime start, DateTime end)
  659. {
  660. string aboutCusetomerChequesSql = string.Empty;
  661. int CusetomerChequesStatus = (int)PaymentStatus.结款中;
  662. string sql = @$" update Finance_Logistics_LadingBill set CusetomerChequesStatus={CusetomerChequesStatus}{aboutCusetomerChequesSql}
  663. from (select BillNo from Finance_ChargesDetail where Offset=0 and CustomerId=@CustomerId and AccountingDate>=@start and AccountingDate<@end and IncomeOrESxpense='支出' and BillType='客户头程账单费用'
  664. ) b
  665. where Finance_Logistics_LadingBill.BillNumber=b.BillNo ";
  666. await _unitOfWork.ExecuteAsync(sql, new { CustomerId = CustomerId, start = start, end = end });
  667. }
  668. public async Task CompleteFirstBillCusetomerChequesStatus(int CustomerId, DateTime end)
  669. {
  670. string aboutCusetomerChequesSql = string.Empty;
  671. int CusetomerChequesStatus = (int)PaymentStatus.结款结束;
  672. string sql = @$" update Finance_Logistics_LadingBill set CusetomerChequesStatus={CusetomerChequesStatus},CusetomerCheques=CusetomerFee
  673. from (select BillNo from Finance_ChargesDetail b left join Finance_Logistics_LadingBill a on b.BillNo=a.BillNumber where b.Offset=0 and b.CustomerId=@CustomerId and b.AccountingDate<@end and b.IncomeOrESxpense='支出' and b.BillType='客户头程账单费用' and a.CusetomerChequesStatus={(int)PaymentStatus.结款中}
  674. ) b
  675. where Finance_Logistics_LadingBill.BillNumber=b.BillNo ";
  676. await _unitOfWork.ExecuteAsync(sql, new { CustomerId = CustomerId, end = end });
  677. }
  678. public async Task<IEnumerable<Finance_Logistics_LadingBill>> GetLadingBillByChargesDetail(DateTime end, int customerId)
  679. {
  680. string sql = @$" select a.* from Finance_Logistics_LadingBill(nolock) a
  681. left join Finance_ChargesDetail(nolock) b on b.BillNo=a.BillNumber
  682. where b.Offset=0 and b.CustomerId=@customerId and b.AccountingDate<@end and b.IncomeOrESxpense='支出' and b.BillType='客户头程账单费用'
  683. and a.CusetomerChequesStatus={(int)PaymentStatus.结款中}
  684. order by a.id desc";
  685. return await _unitOfWork.QueryBySqlAsync<Finance_Logistics_LadingBill>(sql, null, new { customerId = customerId, end = end });
  686. }
  687. }
  688. }