//otcformat禁止千分位分组 window.otcformat.options.disableGrouping = true; const consView = { ContractSize: 1 }; const consTraders = page.traderList; const consAssetUnits = page.CanSelectTrader ? ylotc.assetunits : ylotc.assetunits.filter(x => x.TraderIds.includes(page.Trade.TraderId)); const consCommissionType = Object.freeze({ 不收取: 0, 系统计算: 1, 手动录入: 2 }); const consInstrumentTypes = tradeHelper.InstrumentTypes; const amountNum = 0.01; const lotAmountNum = 100; var instTypes = []; //标的信息管理 const leftPaneMgr = (new function () { var _init = true, _autoVariety, _autoUnderlying, _autoExchangeOption; const consUnFlag = tradeHelper.UnderlyingSelectFlag; const consSelectFlag = consUnFlag.UseForTrading | consUnFlag.UsePinYinFilter | consUnFlag.IncludeMatured | consUnFlag.CheckLaunch; //变更交易类型 function changeTradeType() { instTypes = []; let selectFlag = 0; let tradeType = $('#TradeType').val(); switch (tradeType) { case "商品期货": instTypes = ['CommodityFutures', 'StockIF']; selectFlag = consUnFlag.CommodityFutures | consUnFlag.StockIF; break; case "商品现货": instTypes = ['CommoditySpot']; selectFlag = consUnFlag.CommoditySpot; break; case "股票": instTypes = ['Stock', 'StockIndex']; selectFlag = consUnFlag.Stock | consUnFlag.StockIndex; break; case "信用债": instTypes = ['CreditBonds']; selectFlag = consUnFlag.CreditBonds; break; case "利率债": instTypes = ['Bonds', 'TBonds']; selectFlag = consUnFlag.Bonds | consUnFlag.TBonds; break; case "其它债券": instTypes = ['OtherBonds']; selectFlag = consUnFlag.OtherBonds; break; case "场内期权": instTypes = ['CommodityFutures', 'CommoditySpot', 'Stock', 'StockIF', 'StockIndex']; break; } //和场内期权相关的输入控件显示 $('.ExchangeOption').toggle(tradeType === '场内期权'); //重置标的过滤选项 _autoUnderlying.setFlag(consSelectFlag | selectFlag); //重置品种过滤选项 let oldInstType = $('#InstrumentType').val(); let filters = consInstrumentTypes.filter(x => instTypes.includes(x.value)); let html = filters.map(x => '').join(''); $('#InstrumentType').html(html); //非初始化时变更标的资产类型 if (!_init && instTypes[0] !== oldInstType) { $('#InstrumentType').val(instTypes[0]); changeInstrumentType(); } } //变更标的资产类型 function changeInstrumentType() { let instType = $('#InstrumentType').val(); $('#form1').toggleClass("not-futures", instType !== 'CommodityFutures' && instType !== 'StockIF'); _autoUnderlying.clearCache(); _autoUnderlying.selectFirst({ InstrumentTypes: instTypes }); } //变更标的品种 function changeVariety(yldata, flag) { let data = yldata || {}; $('#TradeUnit').val(data.TradeUnit || ''); $('#QuoteUnit').val(data.QuoteUnit || ''); $('#QuoteUnit2').text(data.QuoteUnit || ''); _autoUnderlying.setVarietyId(data.id); if (flag === 'select') { _autoUnderlying.selectFirst({ VarietyId: data.id }); } } //初始化 this.init = function () { //标的品种 _autoVariety = FastVue.autocomplete(document.getElementById('VarietyId'), { nameField: 'Name', valueField: 'id', searchField: ['Name', 'Code', 'PinYin'], lookup: ylotc.varieties, onSelect: function (data) { changeVariety(data, 'select'); } }); //场内期权 _autoExchangeOption = new tradeHelper.ExchangeOptionAutoComplete('OptionCode'); _autoExchangeOption.onSelect(function (data) { consView.ContractSize = (data.ContractSize || 1) / (data.CountRatio || 1); let tradeAmount = parseFloat($('#TradeAmount').val()) || 0; if (!_init) { $('#TradeLots').val((tradeAmount * lotAmountNum / consView.ContractSize).toFixed(2)); } $('#MaturityDate').val(data.MaturityDate || ''); }); //标的资产 _autoUnderlying = tradeHelper.UnderlyingAutoComplete('UnderlyingCode'); _autoUnderlying.onSelect(function (data) { consView.ContractSize = _init && page.ContractSize ? page.ContractSize : (data.ContractSize || 1) / (data.CountRatio || 1); let tradeAmount = parseFloat($('#TradeAmount').val()) || 0; if (!_init) { $('#TradeLots').val((tradeAmount * lotAmountNum / consView.ContractSize).toFixed(2)); } $('#MaturityDate').val(data.MaturityDate || ''); if ($('#TradeType').val() === '场内期权') { _autoExchangeOption.setSelect(data.Code, true); // (page.IsNew || !_init) } else if (page.IsNew || !_init) { $('#TradeSinglePrice').val(data.Price); } $('#InstrumentType').val(data.InstrumentType); let va = data.VarietyId ? ylotc.varieties.find(x => x.id === data.VarietyId) : null; _autoVariety.setData(va); changeVariety(va, 'underlying'); _init = false; }); //交易类型 $("#TradeType").val(page.Trade.TradeType); $('#TradeType').on('change', changeTradeType); changeTradeType(); //资产类型 $("#InstrumentType").val(page.Trade.InstrumentType); $('#InstrumentType').on('change', changeInstrumentType); //控件数据初始化 if (page.IsNew) { _autoUnderlying.selectFirst({ InstrumentTypes: instTypes }); } else { _autoUnderlying.selectByCode(page.Trade.UnderlyingCode); } }; }()); //右边下拉选择项管理 const rightPaneMgr = (new function () { var autoTrader, autoAssetUnit; this.init = function () { //交易员 if (page.CanSelectTrader) { autoTrader = FastVue.autocomplete(document.getElementById('TraderId'), { nameField: 'Name', valueField: 'id', searchField: ['Name', 'PinYin'], lookup: consTraders }); let traderId = page.Trade.TraderId; let trader = traderId ? consTraders.find(x => x.id === traderId) : null; autoTrader.setData(trader); } //簿记账户 autoAssetUnit = FastVue.autocomplete(document.getElementById('AssetBookId'), { nameField: 'Name', valueField: 'id', searchField: ['Name', 'PinYin'], lookup: consAssetUnits }); let asset = page.IsNew ? consAssetUnits[0] : consAssetUnits.find(x => x.id === page.Trade.AssetBookId); autoAssetUnit.setData(asset); //交易方向+有效性 $("#TradeSide").val(page.Trade.TradeSide); $("#IsValid").val(page.Trade.IsValid ? 'true' : 'false'); $("#ExchangeAccountId").val(page.Trade.ExchangeAccountId); main.setTradeDatePicker("", "#TradeDate", null, null, false); //成交手数 let lotsInput = FastVue.numberInput(document.getElementById('TradeLots')); $('#TradeLots').on('change', function () { let lots = lotsInput.getValue(); $('#TradeAmount').val((lots * amountNum * consView.ContractSize).toFixed(2)); }); //成交数量 let tradeAmountInput = FastVue.numberInput(document.getElementById('TradeAmount')); $('#TradeAmount').on('change', function () { let tradeAmount = tradeAmountInput.getValue(); $('#TradeLots').val((tradeAmount * lotAmountNum / consView.ContractSize).toFixed(2)); }); //成交价格 let priceInput = FastVue.numberInput(document.getElementById('TradeSinglePrice'), { precision: 6 }); //佣金计算 function changeCommissionType() { let checked = $(this).prop('checked'); $('#Commission').toggle(checked); $('#CommissionAuto').toggle(!checked); $('#CommissionType').val(checked ? consCommissionType.手动录入 : consCommissionType.系统计算); } $('#CommissionTypeCheck').on('change', changeCommissionType); changeCommissionType.call(document.getElementById('CommissionTypeCheck')); let commissionInput = FastVue.numberInput(document.getElementById('Commission')); }; //刷新成交价格 this.refreshPrice = function () { if ($('#TradeType').val() === '场内期权') { main.post("/pricing/AjaxGetExchangeOptionPrice", { optionCode: $('#OptionCode').val() }).done(function (resp) { $("#TradeSinglePrice").val(resp.obj); }); } else { main.post("/pricing/AjaxGetUnderlyingPrice", { underlyingCode: $('#UnderlyingCode').val() }).done(function (resp) { $("#TradeSinglePrice").val(resp.obj.price * lotAmountNum); }); } }; }()); //表单提交处理 const submitHandler = (new function () { function checkDate(value) { let date = new Date(value); return date.valueOf(); } function checkSubmitData(data) { if (!data.UnderlyingCode) { return "请设置标的信息"; } if (!data.AssetBookId) { return "请设置簿记账户"; } if (!data.TradeDate) { return '交易日期 必须填写'; } if (!checkDate(data.TradeDate)) { return '交易日期 填写错误'; } //if (data.TradeDate > page.ValueDate) { // return "交易日期不能是未来日期"; //} if (!data.TradeSide) { return '交易方向 必须填写'; } let lots = parseFloat(data.TradeLots) || 0; if (lots < 1e-5) { return '交易手数 必须大于0'; } let amount = parseFloat(data.TradeAmount) || 0; if (amount < 1e-5) { return '交易数量 必须大于0'; } } function savetrade() { let data = Object.assign({}, page.Trade, $('#form1').serializeObject()); let error = checkSubmitData(data); if (error) { main.message(error); return false; } $("#btn_save").prop('disabled', true); main.post("/ExchangeTrade/AjaxSaveTradeEdit", data).done(function (resp) { history.replaceState(null, '交易查看', '/ExchangeTrade/tradeView?reload=1&&enid=' + resp.obj.EncryptId); window.location.reload(); }).always(function () { $("#btn_save").prop('disabled', false); }); }; this.init = function () { $("#btn_save").on('click', savetrade); }; }()); $(function () { leftPaneMgr.init(); rightPaneMgr.init(); submitHandler.init(); }); //ESC $(document).keyup(function (event) { if (event.keyCode === 27) { window.parent.layer.closeAll(); } });