TRS-ZS-183 一个客户一天所有开仓的多头交易都放到一个交易确认书里

This commit is contained in:
锦麟 王
2025-04-22 09:45:21 +08:00
parent 4b9bcfb2dc
commit 5ba29de3ed
21 changed files with 1176 additions and 393 deletions
@@ -1,15 +1,18 @@
using Newtonsoft.Json.Linq;
using Microsoft.VisualBasic;
using Newtonsoft.Json.Linq;
using NPOI.SS.Formula.Functions;
using System.Text.RegularExpressions;
using YLErp.Core.Helpers;
using YLErp.DBModels;
using YLErp.DBModels.Consts;
using YLErp.DBModels.Enums;
using YLErp.Enums;
using YLErp.Plugins.TradeDocGenerator;
using YLErp.Plugins.TradeDocGenerator.Abstracts;
namespace YLErp.Plugins.ShanXi.DocumentGenerator
{
internal class TradeConfirmationGenerator : BaseConfirmationGenerator, ITradeConfirmationGenerator
internal class TradeConfirmationGenerator : BaseConfirmationGenerator_Batch, ITradeConfirmationGenerator
{
public TradeConfirmationGenerator()
: base(ContractTypeEnum.Trade)
@@ -21,23 +24,29 @@ namespace YLErp.Plugins.ShanXi.DocumentGenerator
{
string templatePath = null;
var trade = Context.Trade;
if (trade.TradeType == "收益互换" && trade.StructureType != null)
if (trade.TradeType == "收益互换") //&& trade.StructureType != null)
{
var swapPosition = Context.GetSwapPositions(trade.id, true).Where(x => x.PositionType > 0).FirstOrDefault();
if (swapPosition != null)
{
switch (swapPosition.PositionType)
{
case 1:
templatePath = MosPathHelper.Combine(GlobalConfig.PluginFolder, "App_Docs\\交易确认书\\客户多头交易确认书模板.docx");
break;
case 2:
templatePath = MosPathHelper.Combine(GlobalConfig.PluginFolder, "App_Docs\\交易确认书\\客户空头交易确认书模板.docx");
break;
}
}
//if (trade.StructureType == "普通债券类收益互换" || trade.StructureType == "普通收益互换")
//{
// var swapPosition = Context.GetSwapPositions(trade.id, true).Where(x => x.PositionType > 0).FirstOrDefault();
// if (swapPosition != null)
// {
// switch (swapPosition.PositionType)
// {
// case 1:
// templatePath = MosPathHelper.Combine(GlobalConfig.PluginFolder, "App_Docs\\交易确认书\\客户多头交易确认书模板.docx");
// break;
// case 2:
// templatePath = MosPathHelper.Combine(GlobalConfig.PluginFolder, "App_Docs\\交易确认书\\客户空头交易确认书模板.docx");
// break;
// }
// }
//}
//else
//{
templatePath = MosPathHelper.Combine(GlobalConfig.PluginFolder, "App_Docs\\交易确认书\\浙商证券利率债收益互换合约交易确认书模版.docx");
//}
}
else
{
@@ -78,87 +87,159 @@ namespace YLErp.Plugins.ShanXi.DocumentGenerator
/// <param name="dic"></param>
public void SetSwapData(Dictionary<string, JToken> dic)
{
SetSingleSwap(dic);
var trade = Context.Trade;
SetSingleSwap(dic);
}
private void SetSingleSwap(Dictionary<string, JToken> dic)
{
var client = Context.GetClient();
var trade = Context.Trade;
var bank = Context.GetClientBankCard(false);
var clientduties = Context.GetClientDuties();
var swapPositions = Context.GetSwapPositions(trade.id, true);
var swapPosition = swapPositions.Where(x => x.PositionType == 1 || x.PositionType == 2).FirstOrDefault();
var interestMargin = swapPositions.Where(x => ConsTrade.InterestModels.Contains(x.InterestMode)).FirstOrDefault();
var initialMarginQuery = swapPositions.Where(x => x.InterestMode == 5);
var additionMarginQuery = swapPositions.Where(x => x.InterestMode == 6);
var underlyingCode = swapPosition?.UnderlyingCode;
var table1 = new JArray();
var table2 = new JArray();
var clientMarginRate = Context.GetClientMarginRate(client.id);
dic["交易编号"] = trade.TradeNumber;
dic["交易编号"] = GetContractNo(out string contractIndex);
dic["交易对手方全称"] = client.ClientType != "产品" ? client.Name : $"{client.Manager}作为管理人代表{client.Name}";
dic["主协议编号"] = client.MainProtocolCode;
dic["定义文件编号"] = client.SettleFileNumber;
FormatToDict("名义本金", trade.StockEqvNotionalReal, dic);
dic["成交日期"] = trade.TradeDate?.ToString("【yyyy】年【M】月【d】日");
dic["开始日期"] = trade.StartDate?.ToString("【yyyy】年【M】月【d】日");
dic["到期日期"] = trade.ExerciseDate?.ToString("【yyyy】年【M】月【d】日");
dic["成交日期2"] = trade.TradeDate?.ToString("【yyyy】年【M】月【d】日");
dic["户名"] = bank?.ClientName;
dic["银行账号"] = bank?.Card;
dic["开户行"] = bank?.Bank;
dic["大额行号"] = bank?.Payment;
var count = 0;
FormatToDict("期初标的交割全价", (double)((swapPosition?.PosiGrossPrice) ?? 0) * 100, dic);
FormatToDict("期初标的交割净价", (double)((swapPosition?.PosiNetNoFeePrice) ?? 0) * 100, dic);
if (string.IsNullOrWhiteSpace(interestMargin?.FloatRateUnderlyingCode))
var trades = Context.Trades;
var totalPosiQuantity = 0.0;
foreach (var trade in trades)
{
dic["利率类型"] = "固定利率";
dic["利差"] = "";
dic["固定利率"] = ((interestMargin?.InterestRateDefault) ?? 0) * 100;
dic["参考利率"] = "";
var swapPositions = Context.GetSwapPositions(trade.id, true);
var swapPosition = swapPositions.Where(x => x.PositionType == 1 || x.PositionType == 2).FirstOrDefault();
var interestMargin = swapPositions.Where(x => ConsTrade.InterestModels.Contains(x.InterestMode)).FirstOrDefault();
var initialMarginQuery = swapPositions.Where(x => x.InterestMode == 5);
var additionMarginQuery = swapPositions.Where(x => x.InterestMode == 6);
var underlyingCode = swapPosition?.UnderlyingCode;
if (count == 0)
{
dic["名义本金".Insert("名义本金".Length, "2?")] = trade.StockEqvNotionalReal.ToString("0.##");
dic["成交日期"] = trade.TradeDate?.ToString("yyyy年M月d日");
dic["开始日期"] = trade.StartDate?.ToString("yyyy年M月d日");
dic["到期日期"] = trade.ExerciseDate?.ToString("yyyy年M月d日");
bool posiLong = swapPosition?.PositionType == (int)PositionTypeFlag.Short ? false : true;
var interestRate = interestMargin?.InterestRateDefault * 100 ?? 0;
var basicFee = trade.StockEqvNotionalReal == 0 ? 0 : ((double)((swapPosition?.PosiTradingFeePending) ?? 0) / trade.StockEqvNotionalReal) * 100;
dic["基本费率"] = (basicFee * 2).ToString("f4");
dic["交易费用"] = (double)((swapPosition?.PosiTradingFeePending * 2) ?? 0);
dic["多头约定利率"] = "不适用";
dic["空头约定利率"] = "不适用";
if (posiLong)
{
dic["支付方"] = "乙方支付";
dic["接收方"] = "甲方收取";
}
else
{
dic["支付方"] = "甲方支付";
dic["接收方"] = "乙方收取";
}
if (string.IsNullOrEmpty(interestMargin?.FloatRateUnderlyingCode))
{
dic["利率类型"] = "固定利率";
dic["利差"] = "";
dic["固定利率"] = ((interestMargin?.InterestRateDefault) ?? 0) * 100;
dic["参考利率"] = "";
dic["重置频率"] = "";
if (posiLong)
{
dic["多头约定利率"] = ((double)interestRate).ToString("0.####") + "%";
}
else
{
dic["空头约定利率"] = ((double)interestRate).ToString("0.####") + "%";
}
}
else
{
dic["利率类型"] = "浮动利率";
dic["利差"] = ((interestMargin?.InterestRateDefault) ?? 0) * 10000;
dic["利差%"] = ((interestMargin?.InterestRateDefault) ?? 0) * 100;
dic["参考利率"] = interestMargin?.FloatRateUnderlyingCode;
dic["固定利率"] = "";
dic["重置频率"] = interestMargin?.interest_rest_days;
}
dic["初始预付金支付日"] = interestMargin?.HappenDate == null ? "" : interestMargin?.HappenDate?.ToString("【yyyy】年【M】月【d】日");
dic["管理人名称"] = client.Manager;
var calculationBasis = interestMargin?.FloatRateUnderlyingCode;
if (!string.IsNullOrEmpty(calculationBasis))
{
calculationBasis += interestRate > 0 ? " + " + ((double)interestRate).ToString("0.####") + "%" : ((double)interestRate).ToString("0.####") + "%";
}
else
{
calculationBasis = ((double)interestRate).ToString("0.####") + "%";
}
dic["计算基准"] = calculationBasis;
dic["初始保障金率"] = ((clientMarginRate?.InitMarginRate ?? 0) * 100).ToString("0.####") + "%";
dic["维持保障金率"] = ((clientMarginRate?.MaintenanceRate ?? 0) * 100).ToString("0.####") + "%";
}
var row = new JObject();
row["序号"] = ++count;
FormatToDict("名义本金", trade.StockEqvNotionalReal, row);
row["成交日期"] = trade.TradeDate?.ToString("【yyyy】年【M】月【d】日");
row["开始日期"] = trade.StartDate?.ToString("【yyyy】年【M】月【d】日");
row["到期日期"] = trade.ExerciseDate?.ToString("【yyyy】年【M】月【d】日");
row["成交日期2"] = trade.TradeDate?.ToString("【yyyy】年【M】月【d】日");
row["户名"] = bank?.ClientName;
row["银行账号"] = bank?.Card;
row["开户行"] = bank?.Bank;
row["大额行号"] = bank?.Payment;
FormatToDict("期初标的交割全价", (double)((swapPosition?.PosiGrossPrice) ?? 0) * 100, row);
FormatToDict("期初标的交割净价", (double)((swapPosition?.PosiNetNoFeePrice) ?? 0) * 100, row);
//row["利差"] = ((interestMargin?.InterestRateDefault) ?? 0) * 100;
//var basicFee = trade.StockEqvNotionalReal == 0 ? 0 : ((double)((swapPosition?.PosiTradingFeePending) ?? 0) / trade.StockEqvNotionalReal) * 100;
//row["基本费率"] = (basicFee * 2).ToString("f4");
var initialMarginSum = initialMarginQuery.Sum(x =>
x.InterestDirection == 2 ? x.InterestPrincipalFix * -1 : x.InterestPrincipalFix);
FormatToDict("交易费用", (double)((swapPosition?.PosiTradingFeePending * 2) ?? 0), row);
row["交易费率"] = row["基本费率"];
FormatToDict("期初预付金", (double)initialMarginSum, row);
row["期初预付金利率"] = ((initialMarginQuery.FirstOrDefault()?.InterestRateDefault) ?? 0) * 100;
row["追加预付金返息率"] = ((clientMarginRate?.AddToMarginRebateRate) ?? 0) * 100;
if (!string.IsNullOrEmpty(underlyingCode))
{
var um = Context.GetTradeUnderlying(underlyingCode);
row["标的代码"] = um.UnderlyingCode;
row["标的名称"] = um.UnderlyingName;
var bond = JsonHelper.Deserialize<UnderlyingBond>(um.ExJson) ??
new UnderlyingBond();
row["标的发行人"] = bond.UnderlyingIssuer;
decimal multiplier = um.IsBond() ? 100 : 1;
FormatToDict("期初价格", (double)((swapPosition?.PosiGrossPrice * multiplier) ?? 0), row);
}
else
{
row["标的名称"] = "";
row["标的代码"] = "";
row["期初价格"] = "";
}
FormatToDict("数量", (double)((swapPosition?.PosiQuantity) ?? 0), row);
totalPosiQuantity += (double)((swapPosition?.PosiQuantity) ?? 0);
FormatToDict("合约乘数", (double)((swapPosition?.ContractSize) ?? 0), row);
row["方向"] = swapPosition?.PositionType switch
{
1 => "买入",
2 => "卖出",
_ => ""
};
table1.Add(row);
}
else
{
dic["利率类型"] = "浮动利率";
dic["利差"] = ((interestMargin?.InterestRateDefault) ?? 0) * 10000;
dic["参考利率"] = interestMargin?.FloatRateUnderlyingCode;
dic["固定利率"] = "";
}
if (initialMarginQuery.Any())
{
dic["初始预付金支付日"] = initialMarginQuery.FirstOrDefault().HappenDate?.ToString("【yyyy】年【M】月【d】日");
}
else
{
dic["初始预付金支付日"] = "";
}
FormatToDict("标的期初全价", trade.StockEqvNotionalReal * (double)((swapPosition?.PosiGrossPrice) ?? 0), dic);
var basicFee = trade.StockEqvNotionalReal == 0 ? 0 : ((double)((swapPosition?.PosiTradingFeePending) ?? 0) / trade.StockEqvNotionalReal) * 100;
dic["基本费率"] = (basicFee * 2).ToString("f4");
var initialMarginSum = initialMarginQuery.Sum(x =>
x.InterestDirection == 2 ? x.InterestPrincipalFix * -1 : x.InterestPrincipalFix);
FormatToDict("交易费用", (double)((swapPosition?.PosiTradingFeePending * 2) ?? 0), dic);
dic["交易费率"] = dic["基本费率"];
FormatToDict("期初预付金", (double)initialMarginSum, dic);
dic["期初预付金利率"] = ((initialMarginQuery.FirstOrDefault()?.InterestRateDefault) ?? 0) * 100;
dic["追加预付金返息率"] = ((clientMarginRate?.AddToMarginRebateRate) ?? 0) * 100;
dic["标的代码"] = underlyingCode;
dic["标的名称"] = Context.GetTradeUnderlying(underlyingCode).UnderlyingName;
var bond = JsonHelper.Deserialize<UnderlyingBond>(Context.GetTradeUnderlying(underlyingCode).ExJson) ??
new UnderlyingBond();
dic["标的发行人"] = bond.UnderlyingIssuer;
clientduties = clientduties.Where(x=> x.ContactTypeId.Contains("3")).OrderBy(o=>o.id).ToList();//交易下达人
if (clientduties.Count>0)
clientduties = clientduties.Where(x => x.ContactTypeId.Contains("3")).OrderBy(o => o.id).ToList();//交易确认书邮件接收人
if (clientduties.Count > 0)
{
var clientduty= clientduties.First();
var clientduty = clientduties.First();
var row = new JObject();
row["姓名"] = clientduty.ContactName;
row["身份证号"] = clientduty.IdCardNo;
@@ -168,9 +249,9 @@ namespace YLErp.Plugins.ShanXi.DocumentGenerator
table2.Add(row);
}
dic["table1"] = table1;
dic["table2"] = table2;
}
}
}