GroupCommission.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. using Microsoft.AspNetCore.Components.Web;
  2. using MySqlX.XDevAPI.Relational;
  3. using OASystem.Domain.Dtos.Financial;
  4. using OASystem.Domain.Dtos.Groups;
  5. using OASystem.Domain.Entities.Financial;
  6. using OASystem.Domain.Entities.Groups;
  7. using OASystem.Domain.ViewModels.Financial;
  8. using OASystem.Domain.ViewModels.Groups;
  9. using OASystem.Infrastructure.Repositories.Financial;
  10. using OASystem.Infrastructure.Repositories.Groups;
  11. namespace OASystem.API.OAMethodLib
  12. {
  13. /// <summary>
  14. /// 团组提成
  15. /// 雷怡 2023-08-14 16:04
  16. /// </summary>
  17. public static class GroupCommission
  18. {
  19. //团组信息
  20. private readonly static DelegationInfoRepository _dirRep = AutofacIocManager.Instance.GetService<DelegationInfoRepository>();
  21. //团组实付金额
  22. private readonly static CreditCardPaymentRepository _ccpRep = AutofacIocManager.Instance.GetService<CreditCardPaymentRepository>();
  23. //团组应收款项
  24. private readonly static ForeignReceivablesRepository _frRep = AutofacIocManager.Instance.GetService<ForeignReceivablesRepository>();
  25. //团组已收款项
  26. private readonly static ProceedsReceivedRepository _prRep = AutofacIocManager.Instance.GetService<ProceedsReceivedRepository>();
  27. //团组其他款项
  28. private readonly static OtherPriceRepository _opRep = AutofacIocManager.Instance.GetService<OtherPriceRepository>();
  29. /// <summary>
  30. /// 获取团组提成 Page List
  31. /// </summary>
  32. public static async Task<Result> GetCommissionPageList(GroupCommissionDto dto)
  33. {
  34. Result result = new Result() { Code = -1 };
  35. var groupResult = await _dirRep.GetGroupPageList(dto);
  36. if (groupResult.Code != 0)
  37. {
  38. result.Code = groupResult.Code;
  39. result.Msg = groupResult.Msg;
  40. return result;
  41. }
  42. if (groupResult.Data == null) { }
  43. // ListViewBase<DelegationPageListView> groupData = JsonConvert.DeserializeObject<ListViewBase<DelegationPageListView>>(groupResult.Data);
  44. result.Data = groupResult.Data;
  45. List<int> diids = new List<int>();
  46. foreach (var item in result.Data.DataList)
  47. {
  48. diids.Add(item.Id);
  49. }
  50. await GetCommissionByDiids(diids.ToArray());
  51. return result;
  52. }
  53. /// <summary>
  54. /// 根据团组Id计算 团组利润
  55. /// </summary>
  56. /// <param name="DiId">团组Id</param>
  57. public static async Task<Fin_GroupProfitInfoView> GetCommissionByDiid(int DiId)
  58. {
  59. int[] ints = new int[DiId];
  60. decimal sumFr = 0.00M, //团组 应收款项
  61. sumPr = 0.00M, //团组 已收款项
  62. refund = 0.00M, //团组 退款和其他费用
  63. cost = 0.00M; //团组 成本费用
  64. if (DiId == 0) return new Fin_GroupProfitInfoView();
  65. #region 计算各项费用
  66. //应收款项
  67. Result frData = await _frRep.GetGroupReceivablesByDiid(DiId);
  68. if (frData.Code == 0 && frData.Data != null)
  69. {
  70. foreach (var item in frData.Data)
  71. {
  72. sumFr += item.Price;
  73. }
  74. }
  75. //已收款项
  76. Result prData = await _prRep.GetGroupReceivedByDiid(DiId);
  77. if (frData.Code == 0 && frData.Data != null)
  78. {
  79. foreach (var item in prData.Data)
  80. {
  81. sumPr += item.Price;
  82. }
  83. }
  84. //收款退还费用
  85. Result ccpData = await _ccpRep.GetGroupRefundByDiid(DiId, true);
  86. if (ccpData.Code == 0 && ccpData.Data != null)
  87. {
  88. foreach (var item in ccpData.Data)
  89. {
  90. refund += item.Price;
  91. }
  92. }
  93. //实际的团组类型已收金额需除去退款
  94. sumPr = sumPr - refund;
  95. //团组与非团组产生的成本费用数据
  96. Result _ccpData = await _ccpRep.GetGroupPaymentInfoByDiid(DiId, true);
  97. if (_ccpData.Code == 0 && _ccpData.Data != null)
  98. {
  99. foreach (Grp_Fin_CreditCardPaymentView item in _ccpData.Data)
  100. {
  101. if (item.PayThenMoney != 0M)
  102. cost += item.PayThenMoney * item.DayRate;
  103. else
  104. cost += item.PayMoney * (item.PayPercentage * 0.01M) * item.DayRate;
  105. }
  106. //实际的团组成本需除去退款
  107. cost -= refund;
  108. }
  109. #endregion
  110. //团组净利润=已收金额-成本
  111. decimal groupProfit = sumPr - cost;
  112. //团组尾款=应收款项-已收款项
  113. decimal groupRatainage = sumPr - sumPr;
  114. return new Fin_GroupProfitInfoView() { ReceivableFund = sumFr, ReceivedFund = sumPr,Refund = refund,Cost=cost,RetainedProfits=groupProfit,FinalPayment = groupRatainage };
  115. }
  116. /// <summary>
  117. /// 根据团组 Ids 计算 团组利润
  118. /// </summary>
  119. /// <param name="DiIds"></param>
  120. /// <returns></returns>
  121. public static async Task<List<Fin_GroupProfitInfoView>> GetCommissionByDiids(int[] DiIds)
  122. {
  123. if (DiIds.Length <= 0) return new List<Fin_GroupProfitInfoView>();
  124. List<Fin_GroupProfitInfoView> fin_GroupProfitInfoViews = new List<Fin_GroupProfitInfoView>();
  125. #region string
  126. //string _diidsStr = string.Empty;
  127. //#region 处理Diid
  128. //StringBuilder diidSb = new StringBuilder();
  129. //for (int i = 0; i < DiIds.Length; i++)
  130. //{
  131. // if (i == DiIds.Length - 1)
  132. // {
  133. // diidSb.Append(DiIds[i]);
  134. // }
  135. // else
  136. // {
  137. // diidSb.Append(DiIds[i]).Append(",");
  138. // }
  139. //}
  140. //_diidsStr = diidSb.ToString().Trim();
  141. //#endregion
  142. #endregion
  143. #region 计算各项费用
  144. //应收款项
  145. Result frData = await _frRep.GetGroupReceivablesByDiids(DiIds);
  146. //已收款项
  147. Result prData = await _prRep.GetGroupReceivedByDiids(DiIds);
  148. //收款退还费用
  149. Result ccpData = await _ccpRep.GetGroupRefundByDiids(DiIds, true);
  150. //团组与非团组产生的成本费用数据
  151. Result ccpData1 = await _ccpRep.GetGroupPaymentInfoByDiids(DiIds, true);
  152. foreach (int item in DiIds)
  153. {
  154. decimal sumFr = 0.00M, //团组 应收款项
  155. sumPr = 0.00M, //团组 已收款项
  156. refund = 0.00M, //团组 退款和其他费用
  157. cost = 0.00M; //团组 成本费用
  158. //团组 应收款项
  159. if (frData.Code == 0 && frData.Data != null)
  160. {
  161. List<Fin_ForeignReceivables> _frData = JsonConvert.DeserializeObject<List<Fin_ForeignReceivables>>
  162. (JsonConvert.SerializeObject(frData.Data));
  163. _frData = _frData.Where(fr => item == fr.Diid).ToList();
  164. foreach (Fin_ForeignReceivables itemFr in _frData)
  165. {
  166. sumPr += itemFr.Price;
  167. }
  168. }
  169. //团组 已收款项
  170. if (prData.Code == 0 && prData.Data != null)
  171. {
  172. List<Fin_ProceedsReceived> _prData = JsonConvert.DeserializeObject<List<Fin_ProceedsReceived>>
  173. (JsonConvert.SerializeObject(prData.Data));
  174. _prData = _prData.Where(pr => item == pr.Diid).ToList();
  175. foreach (var itemPr in _prData)
  176. {
  177. sumFr += itemPr.Price;
  178. }
  179. }
  180. //团组 退款和其他费用
  181. if (ccpData.Code == 0 && ccpData.Data != null)
  182. {
  183. List<GroupRefundView> _ccpData = JsonConvert.DeserializeObject<List<GroupRefundView>>
  184. (JsonConvert.SerializeObject(ccpData.Data));
  185. _ccpData = _ccpData.Where(ccp => item == ccp.DIId).ToList();
  186. foreach (var itemCcp in _ccpData)
  187. {
  188. refund += itemCcp.Price;
  189. }
  190. }
  191. //实际的团组类型已收金额需除去退款
  192. sumPr = sumPr - refund;
  193. if (ccpData1.Code == 0 && ccpData1.Data != null)
  194. {
  195. List<Grp_CreditCardPayment> _ccpData1 = JsonConvert.DeserializeObject<List<Grp_CreditCardPayment>>
  196. (JsonConvert.SerializeObject(ccpData1.Data));
  197. _ccpData1 = _ccpData1.Where(ccp => item == ccp.DIId).ToList();
  198. foreach (Grp_CreditCardPayment itemCcp in _ccpData1)
  199. {
  200. if (itemCcp.PayThenMoney != 0M)
  201. cost += itemCcp.PayThenMoney * itemCcp.DayRate;
  202. else
  203. cost += itemCcp.PayMoney * (itemCcp.PayPercentage * 0.01M) * itemCcp.DayRate;
  204. }
  205. //实际的团组成本需除去退款
  206. cost -= refund;
  207. }
  208. #endregion
  209. decimal groupProfit = 0.00M, //团组净利润=已收金额-成本
  210. groupRatainage = 0.00M; //团组尾款=应收款项-已收款项
  211. groupProfit = sumPr - cost;
  212. groupRatainage = sumPr - sumPr;
  213. fin_GroupProfitInfoViews.Add(new Fin_GroupProfitInfoView() { Diid = item, ReceivableFund = sumFr, ReceivedFund = sumPr, Refund = refund, Cost = cost, RetainedProfits = groupProfit, FinalPayment = groupRatainage });
  214. }
  215. return fin_GroupProfitInfoViews;
  216. }
  217. }
  218. }