create procedure dbo.%PROC% @sid varchar(30) = null, .ARGLIST(ALL, 2) as declare @action_id numeric, @id numeric, @user_id numeric, @state_id numeric, @type_id numeric, @item_name varchar(100), @new_state_id numeric begin tran select @action_id = %ACTION_ID% select @id = convert(numeric, @sid) .CHECK_USER .CHECK_UPDATE .ARGCONVERT(ALL, 2) .TEST_UNIQUE(ALL, 2) .GET_PROPS(ALL,1) .GET_PROPS(ALL,3) .UPDATE_ITEMS if @contragent_id is null begin .EXIT_MESSAGE('Не задан контрагент') end if @stock_id is null begin .EXIT_MESSAGE('Не задана ЦБ') end if @owner_id is null begin .EXIT_MESSAGE('Не задана компания') end if (@stock_qty_input is null or @stock_qty_input=0) begin .EXIT_MESSAGE('Не задано количество бумаг') end if @cur_id is null begin .EXIT_MESSAGE('Не задана валюта') end if @character_id =2 /*Брокерская*/ begin if @client_contract_id is null .EXIT_MESSAGE('Не задан брокерский договор') select @client_id = (select client_id from tb_contracts where id = @client_contract_id) end /*Выставление флага и договора с брокером, если сделка через брокера*/ declare @dog_type_ext_id numeric, @ext_br_id numeric select @dog_type_ext_id=dog_type_id, @ext_br_id=tb_contracts.id from tb_contracts,tb_broker2exchange where tb_broker2exchange.id=@asset_place_id and tb_broker2exchange.ext_broker_deal=tb_contracts.id if @dog_type_ext_id in (1,3) select @external_broker_id=@ext_br_id if(isnull(@external_broker_id,-1) = -1) select @dealing_look = 1 else select @dealing_look = 2 if ((@stock_qty_input is null) or (@stock_qty_input=0)) .EXIT_MESSAGE('Не задано количество бумаг') /* if ((@stock_price_input is null) or (@stock_price_input=0)) .EXIT_MESSAGE('Не задана цена ЦБ') */ declare @com money, @com_ts money /* if @character_id=2 begin .CALC_AGENT_COMIS(@summ_doc_total,@com,@client_contract_id,@cur_id,@in_date,@owner_id,1) if @sum_comis is null select @sum_comis=@com end */ /* .CALC_TS_COMIS(@summ_doc_total,@com_ts,@sale_place_id,@cur_id,@in_date,@owner_id,1) if @comis_ts is null select @comis_ts=@com_ts */ if @contract_date!=@in_date .EXIT_MESSAGE('Входящая дата не совпадает с датой сделки') if @dog_calc_order=3 begin if @movedate_from_id!=@paydate_from_id .EXIT_MESSAGE('Оплата и перерегистрация должна считаться от одной даты') if @days_for_regs!=@days_to_pay .EXIT_MESSAGE('Количество дней на оплату и перерегистрацию должны быть одинаковыми') end /* if(@pay_cur_id <> isnull((select bank_acc_cur_id from t_bank_accounts where id = @bank_account_id),-1)) begin .EXIT_MESSAGE('Валюта платежа не совпадает с валютой счета ДС, указанной в нем, или не указан счет ДС.') end */ if @fact_dog_date is null select @fact_dog_date = @plan_dog_date declare @stock_price_lim money declare @doc_sum_lim money declare @price_cur_id numeric(18, 0) declare @covered_stock_qty numeric(18, 0) declare @covered_stock_averprice numeric(18, 6) declare @stock_qty_plan money declare @character_old_id numeric, @direction_old_id numeric, @summ_doc_total_old money, @stock_qty_old money, @stock_cur_old numeric, @cur_por numeric, @stock_sum_new money, @instr_on2off int, @cl_contr_old_id numeric, @in_date_old datetime, @is_entry_cd_old int, @coupon_deal_sum_old money, @rate_value money, @rate_date_dval datetime declare @nominal money, @nominal_cur_id numeric, @nominal_new money, @stock_coupon_id numeric, @broker_id numeric, @plan_money numeric, @plan_securs numeric, @out_method numeric, @is_realiz int, @is_p2l_comis int, @plan_money_cur_id numeric, @is_com_in int, @is_allow_short4stocks int, @is_allow_short4money int, @asset4pereoc_type_id numeric .BACK_GET_ORG_PARMS(@owner_id,@broker_id,@plan_money,@plan_securs,@out_method,@is_realiz,@is_p2l_comis,@is_com_in,@is_allow_short4stocks,@is_allow_short4money,@asset4pereoc_type_id) select @plan_money_cur_id=cur_id from t_plans where id=@plan_money declare @for_calc money .BACK_CALC_DEAL_PARMS(@deal_units,@stock_id,@stock_qty_input,@stock_price_input,@cur_id,@coupon_cur_id,@stock_qty,@stock_price,@cur_id,@coupon_cur_id) -- .BACK_CALC_DEAL_PARMS(@stock_id,@stock_qty_input,@stock_price_input,@for_calc,@deal_units,@deal_units,@stock_qty,@stock_price,@for_calc,@cur_id) .TYPE_GET('client_contracts',@client_contract_id,2) .TYPE_GET('type_client_contracts',@dog_type_id2,20) if (@contract_class_id20=1 and @direction_id is null) select @instruction_on2off=1 if @direction_id is not null begin select @stock_price_lim = tb_directions.stock_price_lim, @doc_sum_lim = tb_directions.doc_sum_lim, @price_cur_id = tb_directions.price_cur_id, @covered_stock_qty = tb_directions.covered_stock_qty, @covered_stock_averprice = tb_directions.covered_stock_averprice, @stock_qty_plan=tb_directions.stock_qty from t_items, td_depo_docs, tb_directions where t_items.id=@direction_id and td_depo_docs.id=@direction_id and tb_directions.id=@direction_id end select @character_old_id=character_id, @direction_old_id=direction_id, @summ_doc_total_old=summ_doc_total, @stock_qty_old=stock_qty, @stock_cur_old=cur_id, @instr_on2off=instruction_on2off, @cl_contr_old_id=client_contract_id, @in_date_old=contract_date, @is_entry_cd_old=is_entry_cd, @coupon_deal_sum_old=coupon_deal_sum from tb_baysale_docitems where id=@id .TYPE_GET('client_contracts',@cl_contr_old_id,2) .TYPE_GET('type_client_contracts',@dog_type_id2,20) if @character_old_id=2 begin if (@instr_on2off=0) begin if @code20!='1' begin select @cur_por=(select price_cur_id from tb_directions where id=@direction_old_id) .TEST_RATE2CUR2DATE(@in_date_old,@cur_por) .TEST_RATE2CUR2DATE(@in_date_old,@stock_cur_old) if @is_entry_cd_old=0 begin select @summ_doc_total_old=@summ_doc_total_old+isnull(@coupon_deal_sum_old,0) end .CONVERT_SUM(@stock_cur_old,@cur_por,@in_date_old,0,@summ_doc_total_old,@stock_sum_new,@rate_value,@rate_date_dval) ---- if @character_id=1 begin declare @qty_full money select @qty_full=(select (covered_stock_qty-@stock_qty_old) from tb_directions where id=@direction_old_id) if @qty_full=0 select @qty_full=1 update tb_directions set covered_stock_averprice=(((covered_stock_averprice*covered_stock_qty)-@stock_sum_new)/@qty_full), covered_stock_qty=(covered_stock_qty-@stock_qty_old) where id=@direction_old_id ---- end end end end if @character_old_id=2 begin if (@instr_on2off=0) begin if @code20!='1' begin if @direction_old_id=@direction_id begin declare @old_qty money select @old_qty=isnull((select stock_qty from tb_baysale_docitems where id=@id),0) select @covered_stock_qty=isnull((@covered_stock_qty-@old_qty),0) end if @covered_stock_qty is null begin select @covered_stock_qty=0 end if @stock_qty_plan is null begin select @stock_qty_plan=0 end declare @kol_stock_qty money, @kol_stock_qty_s varchar(30), @message_1 varchar(255) select @kol_stock_qty=isnull((@stock_qty_plan-@covered_stock_qty),0) select @kol_stock_qty_s=convert(varchar,@kol_stock_qty) if @kol_stock_qty=0 .EXIT_MESSAGE('По введенному поручению нельзя формировать сделку,так как количество бумаг уже покрыто') /*.EXIT_MESSAGE_PARM('',@stock_qty)*/ select @message_1='Введенное количество бумаг превышает возможное,количество которое можно ввести: '+@kol_stock_qty_s+'шт' if @stock_qty>@kol_stock_qty .EXIT_MESSAGE(@message_1) end end end /*Подсчет копонного дохода и суммы*/ if isnull(@coupon_account_sum,-1)=-1 begin /*Если сделка РЕПО 2 часть, то расчет на дату исполнения*/ declare @deal_purp_code int , @date_for_coupon datetime select @deal_purp_code=(select p.code from tb_repo_params r, t_purposes p where r.id=@id and r.repo_type_id = p.id) if @deal_purp_code in (56,57) select @date_for_coupon=oper_date from td_depo_docs where id=@id else select @date_for_coupon=@in_date .BACK_CALC_COUPON(@stock_id,@stock_qty,@date_for_coupon,1,@coupon_account_sum,@coupon_cur_id) -- .BACK_CALC_COUPON(@stock_id,@plan_money,@in_date,@coupon_account_sum_forone,@coupon_cur_id) end -- if isnull(@coupon_account_sum,-1)=-1 -- begin -- select @coupon_account_sum=round(@coupon_account_sum_forone*@stock_qty,2) -- end if isnull(@coupon_account_sum,0)<>0 begin if @coupon_cur_id is null .EXIT_MESSAGE('Не задана валюта купонного дохода') .TEST_RATE2CUR2DATE(@in_date,@coupon_cur_id) .TEST_RATE2CUR2DATE(@in_date,@cur_id) .CONVERT_SUM(@coupon_cur_id,@cur_id,@in_date,0,@coupon_account_sum,@coupon_deal_sum,@rate_value,@rate_date_dval) end else select @coupon_deal_sum=0 if @ckd_class_type_id <> 4 begin if @is_entry_cd=0 begin select @summ_doc_total=round(isnull((@stock_price*@stock_qty*.STOCK_QTY_MULT(@stock_id)),0),4) select @summ_doc_total2unkd=round(isnull((@stock_price*@stock_qty*.STOCK_QTY_MULT(@stock_id)),0),4)+isnull(@coupon_deal_sum,0) end else begin select @summ_doc_total=round(isnull((@stock_price*@stock_qty*.STOCK_QTY_MULT(@stock_id)),0),4)-isnull(@coupon_deal_sum,0) select @summ_doc_total2unkd=round(isnull((@stock_price*@stock_qty*.STOCK_QTY_MULT(@stock_id)),0),4) end end else begin if @is_entry_cd=0 begin -- select @summ_doc_total=round(isnull((@stock_price*@stock_qty*.STOCK_QTY_MULT(@stock_id)),0),4) select @stock_price = round(isnull((@summ_doc_total/@stock_qty/.STOCK_QTY_MULT(@stock_id)),0),4) select @summ_doc_total2unkd=@summ_doc_total+isnull(@coupon_deal_sum,0) end else begin select @stock_price = round(isnull((@summ_doc_total/@stock_qty/.STOCK_QTY_MULT(@stock_id)),0),4) select @summ_doc_total=@summ_doc_total-isnull(@coupon_deal_sum,0) end end --select @summ_doc_total=round((@summ_doc_total*.STOCK_QTY_MULT(@stock_id)),2) --select @summ_doc_total2unkd=@summ_doc_total+isnull(@coupon_account_sum,0) .UPDATE(ALL,2) ---exec ap_calculate_comis @sid .RELGEN .ACTION_HISTORY /* Богаковский 10/12/2004 закомментарил шаблон нумератора сделок, созданный специально для Prosperity */ /* Богаковский 09/02/2004 специально для Prosperity сделал нумератор по клиентским договорам, у нас закомментарил */ --.BACK_GEN_DEALS_OUT_NO(@client_contract_id) ----.BACK_GEN_DEALS_OUT_NO(@portfolio_sub_id) commit tran