using YLErp.BLL;
using YLErp.Commons;
using YLErp.DBModels.Consts;
using YLErp.DBModels.Enums;
using YLErp.Model;
using YLErp.Modules.ClientModule;
namespace YLErp.Modules.TradeModule.DealModule
{
///
/// 交易到期操作
///
public class TradeExpireService : TradeServiceBase
{
public TradeExpireService(YLBaseService baseService) : base(baseService)
{
}
public TradeExpireService(OptUserInfo userInfo) : base(userInfo)
{
}
///
/// 日终执行批量行权到期
/// 迁移自:ExeExpireTradeCashJson
///
public void ExeExpireTradeCash(TradeCashReq req, trade_cash formData)
{
var valueDate = valuedateBLL.ValueDate;
//修改交易主表信息交易状态为已执行
var td = DbContext.trade.FirstOrDefault(t => t.id == req.TradeId);
if (td == null)
{
throw new ServiceException("更新失败,找不到要交割的主交易!");
}
if (td.TradeType == "结构化交易")
{
throw new ServiceException("操作失败,结构化交易主交易不支持此操作");
}
if (PS.Config.TradeElement.ExecuteAfterGeneratedConfirmDoc && !td.HasGeneratedConfirmBook() && td.TradeType != "自定义交易")
{
throw new ServiceException("请先去生成交易确认书!");
}
if (td.TradeStatus != ConsTrade.确认成交 && td.TradeStatus != ConsTrade.提前终止拒绝 && td.TradeStatus != ConsTrade.行权待复核)
{
throw new ServiceException("行权失败,状态为" + td.TradeStatus + ",该交易状态不能行权!");
}
//增加执行日期
td.UnWindDate = valueDate;
td.OptDate = DateTime.Now;
td.OptId = UserId;
td.OptName = UserName;
td.FinalPrice = req.FinalPrice;
var tcspotPrice = DataCacheProvider.GetUnderlyingDataSource().GetPrice(td.UnderlyingCode);
if (req.IsPossibleExec.HasValue && req.IsPossibleExec.Value)
{
//修改交易状态
td.TradeStatus = ConsTrade.已执行;
//trade_cash
var tc = DbContext.trade_cash.FirstOrDefault(t => t.TradeId == td.id && t.Action == ClientCashInCashOut.系统操作_行权费 && t.ValidState != ConsGlobal.InValid && !t.IsDeleted);
if (tc == null)
{
tc = new trade_cash();
DbContext.trade_cash.Add(tc);
}
if (formData != null)
{
UpdateChanges(tc, formData);
}
tc.SpotPrice = tcspotPrice;
tc.ValidState = req.ValidState;
tc.OptId = UserId;
tc.OptName = UserName;
tc.OptDate = DateTime.Now;
tc.Action = ClientCashInCashOut.系统操作_行权费;
tc.IsLastAction = true;
tc.ValueDate = valueDate;
tc.FinalPrice = req.FinalPrice;
tc.Amount = req.Amount ?? 0;
tc.Strike = req.Strike;
tc.CallPut = req.CallPut;
tc.ExtraAmount = req.ExtraAmount;
tc.Status = TradeCashStatusEnum.已执行;
tc.TradeType = td.BuySell;
tc.TradeId = td.id;
tc.ParentTradeId = td.ParentTradeId;
tc.Notional = td.Notional;
tc.UnwindPercentRate = td.Notional / td.OriginalNotional;
tc.NotionalPercentRate = tc.UnwindPercentRate;
var CountRatio = UnderlyingDataProvider.GetCountRatio(td.UnderlyingCode);
tc.TradeAmount = tc.Notional / CountRatio;
tc.ExerciseWay = TradeCashExerciseWayEnum.到期行权;
DbContext.SaveChanges();
new TradeCashService(this).SaveTradeCashDetail(tc);
//增加出入金记录
var cl = ClientDataQueryService.GetClient(td.ClientId) ?? new Models.ClientMainInfo();
var ee = new ClientCashInCashOut
{
Direction = "应收",
Number = UniqueTimeId.GetStr(),
ClientId = cl.id,
ClientNumber = cl.Number,
ClientName = cl.Name,
//客户和交易员反方向
Money = tc.Amount * -1,
HappenDate = valueDate == DateTime.Now.Date ? DateTime.Now : valueDate,
State = ClientCashInCashOut.已确认,
//ee.OpenBankId = bc.id + "";
//ee.OpenBankCard = bc.Card;
TradeId = tc.TradeId,
TradeCashId = tc.id,
OptId = tc.OptId,
OptName = tc.OptName,
OptDate = tc.OptDate,
CreatorId = tc.OptId,
CreatorName = tc.OptName,
CreateDate = tc.OptDate,
Action = ClientCashInCashOut.系统操作_行权费,
TradeNumber = td.TradeNumber,
IsGroup = td.IsGroup
};
DbContext.ClientCashInCashOut.Add(ee);
DbContext.SaveChanges();
//Utilities.AddLog(tc.id, trade_cash.LogClass, "创建", "执行交割增加现金交割交易记录");
//Utilities.AddLog(ee.id, ClientCashInCashOut.LogClass, "创建", "执行交割增加应收账记录");
}
else
{
//到期交易增加一条交易金额为0的trade_cash记录 记录当时期末价格 以及行权费用
//修改交易状态
td.TradeStatus = ConsTrade.已到期;
//trade_cash
var tc = DbContext.trade_cash.FirstOrDefault(t => t.TradeId == td.id && t.Action == ClientCashInCashOut.系统操作_行权费 && t.ValidState != ConsGlobal.InValid && !t.IsDeleted);
if (tc == null)
{
tc = new trade_cash();
DbContext.trade_cash.Add(tc);
}
if (formData != null)
{
UpdateChanges(tc, formData);
}
tc.SpotPrice = tcspotPrice;
tc.ValidState = req.ValidState;
tc.OptId = UserId;
tc.OptName = UserName;
tc.OptDate = DateTime.Now;
tc.Action = ClientCashInCashOut.系统操作_行权费;
tc.IsLastAction = true;
tc.ValueDate = td.ExerciseDate.Value;
tc.FinalPrice = req.FinalPrice;
tc.Notional = td.Notional;
var CountRatio = UnderlyingDataProvider.GetCountRatio(td.UnderlyingCode);
tc.TradeAmount = tc.Notional / CountRatio;
tc.Amount = 0;
tc.ExtraAmount = req.ExtraAmount;
tc.Status = TradeCashStatusEnum.已执行;
tc.TradeType = td.BuySell;
tc.TradeId = req.TradeId;
tc.ExerciseWay = TradeCashExerciseWayEnum.到期行权;
DbContext.SaveChanges();
}
AddTradeOperationHistoryAndSetParentTradeInfo(false, td, "批量到期行权");
DbContext.SaveChanges();
//生成文档
if (PS.Config.IsAutoGenerateContracts)
{
new TradeContractGenerateService(this).GenerateContractsAsync(new List { td.id });
}
}
}
}