GroupCommission.cs 10.0 KB

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