create procedure dbo.%PROC% @sid varchar(30) = null as .BEGIN('N') .CHECK_USER .CHECK_UPDATE begin tran .UPDATE_STATE .ACTION_HISTORY declare @broker_id numeric, @plan_secur_id numeric, @out_met_id int, @is_realiz int, @is_p2l_com int, @is_com_in int, @is_allow_short4stocks int, @is_allow_short4money int, @asset4pereoc_type_id int /*-------Проводки ----------*/ .INT_VAR(@pay_perpose) .NAME_VAR(@investor_subc_str) .NAME_VAR(@ext_subc_str) .NAME_VAR(@home_subc_str) .NAME_VAR(@client_subc_str) .ID_VAR(@home_place_class_id) .ID_VAR(@home_object_id) .ID_VAR(@home_bank_acc_id) .ID_VAR(@ext_object_id) --.ID_VAR(@client_contract_id) .ID_VAR(@contragent_id) .ID_VAR(@investor_id) .ID_VAR(@organisation_id) .MONEY_VAR(@exec_sum) .MONEY_VAR(@acc_exec_sum) .MONEY_VAR(@cred_acc_sum) .MONEY_VAR(@deb_acc_sum) .MONEY_VAR(@pereoc_sum) .MONEY_VAR(@acc_sum_exec_full) .DATE_VAR(@oper_date) .ID_VAR(@usd) .MONEY_VAR(@rate_value_cc) .DATE_VAR(@rate_date_cc) .MONEY_VAR(@rate2cur2rur) .ID_VAR(@plan_money_id) .ID_VAR(@plan_securs_id) .INT_VAR(@out_method_id) .INT_VAR(@fin_result) .INT_VAR(@fin_result2oper) .INT_VAR(@sum2prof2loss) .ID_VAR(@character_id) .NAME_VAR(@mes) declare @client_id numeric, @foundation_id_s varchar(30) declare @stock_id numeric, @rate_cur double precision, @rate_acc_cur double precision .BACK_BSPARMS_BEGIN ---*/*/*/*/ .GET_PROPS -- Если выбран портфель "только для ЦБ" переопределяем его на привязанный порфель "только для ДС" if (select section_type_id from tb_portfolio_section where id =@portfolio_sub_id)=1 begin select @portfolio_sub_id = (select money_section_id from tb_portfolio_section where id =@portfolio_sub_id) if @portfolio_sub_id is null .EXIT_MESSAGE('Не привязан портфель для денежных средств') end -- Окончание переопределения портфеля select @complete_date=isnull(@complete_date,@in_date) update t_paydocs set complete_date=@complete_date where id=@id ---.UPDATE select @doc_sum=round(@doc_sum,2) ----.BACK_GET_ORG_PARMS(@owner_id,@broker_id,@plan_money_id,@plan_secur_id,@out_met_id,@is_realiz,@is_p2l_com,@is_com_in,@is_allow_short4stocks,@is_allow_short4money,@asset4pereoc_type_id) if @purpose_id is null .EXIT_MESSAGE('Не задано целевое назначение') .ASSIGN(@pay_perpose,((select code from t_purposes where id=@purpose_id))) .ASSIGN(@ext_subc_str,'') .ASSIGN(@cred_acc_sum,null) .ASSIGN(@deb_acc_sum,null) declare @is_p2l_comis int, @curr_id numeric, @rur numeric .BACK_GET_ORG_PARMS(@owner_id,@organisation_id,@plan_money_id,@plan_securs_id,@out_method_id,@is_realiz,@is_p2l_comis,@is_com_in,@is_allow_short4stocks,@is_allow_short4money,@asset4pereoc_type_id) .ASSIGN(@oper_date,@in_date) .ASSIGN(@home_object_id,@to_object_id) .ASSIGN(@home_bank_acc_id,@to_partner_acc_id) .ASSIGN(@contragent_id,@from_partner_id) ---- вычисление класса объекта из места нахождения .BACK_PLASE_CLASS_FROM_OBJ(@home_object_id,@organisation_id,@home_place_class_id) /*=================== Логика по целевым назначениям========================== */ /* if (@pay_perpose = 10 and @foundation_id is not null and not exists (select 1 from td_depo_docs where id=@foundation_id and ( (.ITEMS_EXISTS_BY_TYPE_STATE(td_depo_docs.id,'DOG_DEALINGS_SALE_VBR','PODP')) or (.ITEMS_EXISTS_BY_TYPE_STATE(td_depo_docs.id,'DOG_DEALINGS_SALE_VBR','POST_END')) ) ) ) .EXIT_MESSAGE('Сделка, к которой привязан данный платеж, находится в неверном статусе') */ ---- вычисление суммы в валюте сделки if @pay_perpose in (10) /*------------------- Платеж по договору продажи ---------------------*/ begin /*--Расчеты по нестандартному курсу---*/ .ASSIGN(@usd,@cur_dealing_id) if @character_cur_price=1 begin select @oper_date=@cur_price_date select @deb_acc_sum=round(@sum_cur_dealing,2) select @rate2cur2rur=@cur_price end else begin select @oper_date=@in_date select @rate2cur2rur=null .TEST_RATE2CUR2DATE(@oper_date,@cur_id) .TEST_RATE2CUR2DATE(@oper_date,@cur_dealing_id) .CONVERT_SUM(@cur_id,@cur_dealing_id,@oper_date,0,@doc_sum,@deb_acc_sum,@rate_value_cc,@rate_date_cc) select @deb_acc_sum=round(@deb_acc_sum,2) end end declare @doc_sum_new money select @rur=.CUR2PLAN(@plan_money_id) if @pay_perpose in (10) /*------------------- Платеж по договору продажи ---------------------*/ begin declare @acc_cur_id numeric, @acc_sum money select @acc_cur_id=@cur_id, @acc_sum=@doc_sum if @rur<>@cur_id begin .CONVERT_SUM(@cur_dealing_id,@rur,@oper_date,0,@sum_cur_dealing,@doc_sum_new,@rate_value_cc,@rate_date_cc) select @cur_id=@rur select @doc_sum=round(@doc_sum_new,2) end end ---- вычисление суммы в валюте плана if @pay_perpose in (45, 47, 52) /*------------------- Платеж по групповой операции ---------------------*/ begin select @portfolio_id=null select @portfolio_sub_id=null if @rur<>@cur_id begin .CONVERT_SUM(@cur_id,@rur,@oper_date,0,@doc_sum,@doc_sum_new,@rate_value_cc,@rate_date_cc) select @cur_id=@rur select @doc_sum=round(@doc_sum_new,2) end end ---.EXIT_MESSAGE_PARM('@doc_sum',@doc_sum) /* if @character_cur_price=1 begin select @oper_date=@cur_price_date select @deb_acc_sum=round((@doc_sum/@cur_price),2) select @rate2cur2rur=@cur_price end else begin select @oper_date=@in_date select @rate2cur2rur=null if @cur_dealing_id=@cur_id select @curr_id=@cur_id else select @curr_id=@usd .TEST_RATE2CUR2DATE(@oper_date,@cur_id) .CONVERT_SUM(@cur_id,@curr_id,@oper_date,0,@doc_sum,@deb_acc_sum,@rate_value_cc,@rate_date_cc) select @deb_acc_sum=round(@deb_acc_sum,2) end */ select @cred_acc_sum=@deb_acc_sum if @pay_perpose in (10) /*------------------- Платеж по договору продажи ---------------------*/ begin /*-Присвоение-------------------*/ if not exists(select 1 from t_items,t_types where t_items.id=@foundation_id and t_types.id=t_items.type_id and t_types.type_code in ('DOG_DEALINGS_SALE_VBR','DOG_DEALINGS_SALE_VBR_VEKSEL','DEALINGS_VIA_BROKER_SALE')) .EXIT_MESSAGE('Задайте сделку продажи ЦБ') .BACK_BSPARMS_GET(@foundation_id) .ASSIGN(@client_contract_id,@bs1_client_contract_id) .ASSIGN(@contragent_id,@bs1_contragent_id) select @client_id=client_id from tb_contracts where id=@client_contract_id if @contragent_id is null .EXIT_MESSAGE('Не найден контрагент по договору продажи ЦБ') if @bs1_dog_character_id is null .EXIT_MESSAGE('Не задан характер сделки в договоре') if @bs1_dog_character_id = 2 and isnull(@client_contract_id,-1) = -1 .EXIT_MESSAGE('Не задан клиентский договор') if @bs1_dog_character_id = 1 begin .ASSIGN(@client_contract_id,null) select @client_id=null select @fin_result=1 end else begin select @fin_result=(select fin_result from tb_contracts where id=@client_contract_id) end end else if @pay_perpose in (37,79,89,91,64,98,105,122,126,130) /*----------------------Зачисление ДС в фонд клиента-------------*/ begin declare @k_acc_id numeric, @d_acc_id numeric, @subc_str varchar(255), @doc_sum_lim money, @all_sum money if @foundation_id is not null select @doc_sum_lim=doc_sum_lim from tb_directions where id=@foundation_id if @client_contract_id is null .EXIT_MESSAGE('Не задан клиентский договор') .SUBC_STR_ADD(@ext_subc_str,'INVESTORS',@client_contract_id) .SUBC_STR_ADD(@subc_str,'investors',@client_contract_id) ---.SUBC_STR_ADD(@subc_str,'stock_emis',@stock_id) ---.SUBC_STR_ADD(@subc_str,'ASSET_PLACES',@asset_place_id) .SUBC_STR_ADD(@subc_str,'portfolio',@portfolio_id) .SUBC_STR_ADD(@subc_str,'investor2portfolio',@portfolio_sub_id) ---.SUBC_STR_ADD(@subc_str,'depo_subacc',@depo_subacc_id) if @foundation_id is not null --- если задано поручение клиента по ДС, то погашаем обязательства begin .SUBC_STR_ADD(@subc_str,'INSTRUCTION',@foundation_id) end .ACCID_FROM_CONST2PLAN(@d_acc_id,'BACK_ACC_FUND_MONEY_CHNG',@plan_money_id,2,1,'Счет ожидаемого прихода ДС по клиентскому поручению',@mes) .ACCID_FROM_CONST2PLAN(@k_acc_id,'BACK_ACC_MONEY_MOVE_INSTR',@plan_money_id,2,1,'Счет ожидаемого прихода ДС по клиентскому поручению',@mes) select @mes='Снятие резервирование ДС к приходу' if @foundation_id is not null begin .OPERATION_SBC(@owner_id,@plan_money_id,@in_date,@d_acc_id,@k_acc_id,@doc_sum,@cur_id,@in_date,0,0, @mes,Y,@operation_id,@id,@action_id,@action_history_id,@doc_sum,@doc_sum,@subc_str,@subc_str,@cur_id,@cur_id) -- -- .SALDO -- declare @subconto_str VARCHAR(255) .SUBC_STR_ADD(@subconto_str, 'investors', @client_contract_id) .SALDO(@owner_id,@plan_money_id,@in_date,@d_acc_id,@subconto_str,Y,'K',@all_sum,N,N) if @all_sum < 0 .EXIT_MESSAGE('Превышено количество по поручению') if @all_sum=0 begin .ITEM_MOVE_STATE(@foundation_id,'COMPLIT') end end else begin if @instruction_on2off=1 and @pay_perpose =37 begin -------------- Создаем поручение автоматически-------- select @client_id=client_id from tb_contracts where id=@client_contract_id .TYPE_INIT('INSTRUCTION_DS_INOUT',5) .TYPE_ASSIGN(type_poruch_id,1,5) .BACK_GEN_NO(@in_no5_1,1,@in_date) .TYPE_ASSIGN(in_no,@in_no5_1,5) .TYPE_ASSIGN(in_date,@in_date,5) .TYPE_ASSIGN(depo_doc_type,1,5) .TYPE_ASSIGN(client_id,@client_id,5) .TYPE_ASSIGN(bank_account_id,@to_partner_acc_id,5) .TYPE_ASSIGN(asset_place_id,@to_object_id,5) .TYPE_ASSIGN(price_cur_id,@cur_id,5) .TYPE_ASSIGN(doc_sum_lim,@doc_sum,5) .TYPE_ASSIGN(owner_id,@owner_id,5) .TYPE_ASSIGN(exec_end_date,@in_date,5) .TYPE_ASSIGN(client_contract_id,@client_contract_id,5) .TYPE_ASSIGN(portfolio_id,@portfolio_id,5) .TYPE_ASSIGN(investor2portfolio_id,@portfolio_sub_id,5) .TYPE_INSERT('INSTRUCTION_DS_INOUT','COMPLIT',5) update t_paydocs set foundation_id=@id5_1 where id=@id update tb_directions set in_time = dateadd(minute, rand()*390, '09:30:00') where id=@id5_1 end end end else if @pay_perpose in (38,77,90,92,106,123,127,131,134) /*----------------------Зачисление ДС в фонд компании-------------*/ begin .ASSIGN(@contragent_id,@from_partner_id) end else if @pay_perpose=42 /*-------------------------------Взаимозачет--------------------*/ begin .ASSIGN(@contragent_id,@from_partner_id) if @contragent_id is null .EXIT_MESSAGE('Не задан контрагент') end else /*------------------------------------------- Прочие случаи----------------------- */ begin if @contragent_id is null .EXIT_MESSAGE('Не задан контрагент') end if @home_object_id is null .EXIT_MESSAGE('Не задано место нахождения ДС') if @home_place_class_id is null .EXIT_MESSAGE('Не задан класс места нахождения ДС') if @home_bank_acc_id is null .EXIT_MESSAGE('Не задан расчетный счет') /*============================Вычисление инвестора===========================*/ if @pay_perpose = 82 begin select @investor_id=(select tb_broker_change.id from tb_broker_change where tb_broker_change.partner_id = @owner_id and .ITEMS_EXISTS_BY_TYPE(tb_broker_change.id,'broker_info')) select @client_contract_id=null select @client_id = null end else begin if @client_contract_id is null begin select @investor_id=(select tb_broker_change.id from tb_broker_change where tb_broker_change.partner_id = @owner_id and .ITEMS_EXISTS_BY_TYPE(tb_broker_change.id,'broker_info')) if @pay_perpose=42 select @investor_id=investor_id from tb_portfolio_section where id=@portfolio_sub_id end else .ASSIGN(@investor_id,@client_contract_id) end if @investor_id is null .EXIT_MESSAGE('Инвестор не найден') /*============================Вычисление строк субконто===========================*/ if @pay_perpose in (10) begin .ASSIGN(@investor_subc_str,@bs1_investor_subc_str) end else begin .ASSIGN(@investor_subc_str,'') if @pay_perpose not in (45, 47, 52, 120) begin .SUBC_STR_ADD(@investor_subc_str,'investors',@investor_id) .SUBC_STR_ADD(@investor_subc_str,'investor2portfolio',@portfolio_sub_id) .SUBC_STR_ADD(@investor_subc_str,'portfolio',@portfolio_id) end end .ASSIGN(@home_subc_str,@investor_subc_str) .SUBC_STR_ADD(@home_subc_str,'ASSET_PLACES',@home_object_id) .SUBC_STR_ADD(@home_subc_str,'BANK_ACCOUNT',@home_bank_acc_id) if @pay_perpose in (38,19,82,77,90,92,106,123,127,131,134) /* Зачисление ДС из фонда компании */ begin .ASSIGN(@ext_subc_str,@investor_subc_str) end else if (@pay_perpose=42) /*Взаимозачет*/ or (@pay_perpose in (10)) /* По сделке покупки*/ begin .ASSIGN(@ext_subc_str,@investor_subc_str) .SUBC_STR_ADD(@ext_subc_str,'contragents',@contragent_id) if (@pay_perpose=42) begin .SUBC_STR_ADD(@ext_subc_str,'doc_sec',@foundation_id) end if not exists(select 1 from t_items,t_types --- если платеж по сделке вбр(рпс) where t_items.id=@foundation_id and t_types.id=t_items.type_id and upper(t_types.type_code) in ('DOG_DEALINGS_SALE_VBR_VEKSEL')) begin .SUBC_STR_ADD(@ext_subc_str,'doc_sec',@foundation_id) end else --- если платеж по сделке с неск. цб begin declare @found_id numeric, @cursor_sum money, @cursor_sum2unkd money, @rate_acc_sum money, @d_acc_sum money, @d_deal_sum money, @pay_summ money, @deal_sum money, @pay_perc double precision, @deal_perc double precision, @op_deal_sum money, @op_acc_sum money, @c_s_full int declare cur2deal cursor for ---- курсор по внутренним сделкам select tb_baysale_docitems.id,(tb_baysale_docitems.sum_in_paycur-isnull(tb_plan2fact_dates.payment_sum,0)), (tb_baysale_docitems.summ_doc_total2unkd-isnull(tb_baysale_docitems.covered_sum,0)) from td_depo_docs,tb_baysale_docitems,t_items,t_states,tb_plan2fact_dates where foundation_id=@foundation_id and tb_baysale_docitems.id=td_depo_docs.id and tb_plan2fact_dates.id=td_depo_docs.id and t_items.id=td_depo_docs.id and t_states.id=t_items.state_id and t_states.state_code in ('PODP','POST_END') ---for read only --- подсчет количества внутренних сделок select @c_s_full=(select count(*) from td_depo_docs,tb_baysale_docitems,t_items,t_states where foundation_id=@foundation_id and tb_baysale_docitems.id=td_depo_docs.id and t_items.id=td_depo_docs.id and t_states.id=t_items.state_id and t_states.state_code in ('PODP','POST_END')) --- and .ITEMS_EXISTS_BY_TYPE(td_depo_docs.id,'DOG_DEALINGS_SALE_VBR')) --- подсчет общей суммы select @pay_summ=sum(tb_baysale_docitems.sum_in_paycur), @deal_sum=sum(tb_baysale_docitems.summ_doc_total2unkd) from td_depo_docs,tb_baysale_docitems,t_items,t_states where foundation_id=@foundation_id and tb_baysale_docitems.id=td_depo_docs.id and t_items.id=td_depo_docs.id and t_states.id=t_items.state_id and t_states.state_code in ('PODP','POST_END') --- and .ITEMS_EXISTS_BY_TYPE(td_depo_docs.id,'DOG_DEALINGS_SALE_VBR') select @pay_perc=convert(double precision,(@deb_acc_sum/@pay_summ)) select @deal_perc=convert(double precision,(@acc_sum/@deal_sum)) end end /* if @pay_perpose=10 -- По сделке begin select @stock_id=(select stock_id from tb_baysale_docitems where id=@foundation_id) .SUBC_STR_ADD(@bs1_profit_subc_str,'STOCK_EMIS',@stock_id) end */ /*============================Проводка по списанию ДС===========================*/ if (@pay_perpose in (45, 47, 52)) select @portfolio_sub_id=null, @portfolio_id=null -- затычка для незаданного раздела if @portfolio_sub_id is null and (@pay_perpose not in (45, 47, 52)) select @portfolio_sub_id=(select max(ps.id) from tb_portfolio_section ps where ps.investor_id=@organisation_id and .ITEMS_EXISTS_BY_TYPE(ps.id,'PORTFOLIO_SECTION')) .BACK_OPERS_BEGIN(@owner_id,1,@purpose_id,@home_place_class_id,@home_object_id,@client_contract_id,@investor_subc_str,@portfolio_sub_id) if @pay_perpose in (10) begin if exists(select 1 from t_items,t_types where t_items.id=@foundation_id and t_types.id=t_items.type_id and upper(t_types.type_code) in ('DOG_DEALINGS_SALE_VBR','DEALINGS_VIA_BROKER_SALE')) begin select @stock_id=(select stock_id from tb_baysale_docitems where id=@foundation_id) .SUBC_STR_ADD(@bs1_profit_subc_str,'STOCK_EMIS',@stock_id) if @cur_id<>@bs1_cur_id begin ----.EXIT_MESSAGE('!!!') .BACK_CUROVR_VBR(2,@doc_sum,@deb_acc_sum,@in_date,@in_date,@cur_id,@bs1_profit_subc_str,@bs1_oblig_subc_str,@fin_result,@bs1_dog_character_id,@cur_dealing_id) ---.BACK_CLOSE_PROFIT_LOSS(1,2,@pereoc_sum,@in_date,@oper_date,@cur_id,@bs1_fund_subc_str,@bs1_profit_subc_str) end end end if @pay_perpose not in (45, 47, 52, 120) begin if not exists(select 1 from t_items,t_types where t_items.id=@foundation_id and t_types.id=t_items.type_id and upper(t_types.type_code) in ('DOG_DEALINGS_SALE_VBR_VEKSEL')) begin .BACK_MONEY_INOUT(@doc_sum,@in_date,@in_date,@cur_id,@home_subc_str,@ext_subc_str,@acc_sum,@sum_cur_dealing,@acc_cur_id,@cur_dealing_id) end end declare @doc_sum_new_s money, @c_s int select @doc_sum_new_s=@doc_sum select @c_s=0 if @pay_perpose in (10) -- По сделке begin ------------------ Задолженность клиента по ВБР if exists(select 1 from t_items,t_types where t_items.id=@foundation_id and t_types.id=t_items.type_id and upper(t_types.type_code) in ('DOG_DEALINGS_SALE_VBR_VEKSEL')) begin open cur2deal fetch cur2deal into @found_id,@cursor_sum,@cursor_sum2unkd ---select @d_s=@doc_sum select @d_deal_sum=@deb_acc_sum select @d_acc_sum=@acc_sum while .CURSOR_STATE=0 begin if @d_deal_sum=0 break select @c_s=@c_s+1 select @stock_id=(select stock_id from tb_baysale_docitems where id=@found_id) .SUBC_STR_ADD(@bs1_profit_subc_str,'STOCK_EMIS',@stock_id) .SUBC_STR_ADD(@ext_subc_str,'doc_sec',@found_id) .SUBC_STR_ADD(@bs1_profit_subc_str,'doc_sec',@found_id) .SUBC_STR_ADD(@bs1_oblig_subc_str,'doc_sec',@found_id) ---.EXIT_MESSAGE_PARM('@cursor_sum',@cursor_sum) /* if @character_cur_price=1 begin select @op_deal_sum=round(@cursor_sum2unkd*@deal_perc,2) end else begin .CONVERT_SUM(@cur_id,@cur_dealing_id,@oper_date,0,@cursor_sum2unkd,@op_deal_sum,@rate_value_cc,@rate_date_cc) select @op_deal_sum=round(@op_deal_sum,2) end */ ---select @character_cur_price,@cursor_sum2unkd,@cursor_sum,@op_deal_sum ---select @op_acc_sum=round(@cursor_sum*@pay_perc,2) ---select @d_deal_sum,@d_acc_sum,@cursor_sum,@cursor_sum2unkd,@op_deal_sum,@op_acc_sum ----select @d_deal_sum,@cursor_sum2unkd,@d_acc_sum,@op_deal_sum,@op_acc_sum if @d_deal_sum>@cursor_sum2unkd begin select @d_acc_sum=@d_acc_sum-@cursor_sum ---select @rate_acc_sum=round(@cursor_sum*@rate_cur,4) select @d_deal_sum=@d_deal_sum-@cursor_sum2unkd end else begin select @cursor_sum2unkd=@d_deal_sum ---select @rate_acc_sum=round(@cursor_sum*@rate_cur,4) select @cursor_sum=@d_acc_sum select @d_deal_sum=0 select @d_acc_sum=0 end update tb_baysale_docitems set covered_sum=isnull(covered_sum,0)+@cursor_sum2unkd where id=@found_id update tb_plan2fact_dates set payment_sum=isnull(payment_sum,0)+@cursor_sum where id=@found_id ---select @d_deal_sum,@cursor_sum2unkd,@d_acc_sum,@op_deal_sum,@op_acc_sum ---.EXIT_MESSAGE_PARM('!!!!',@cursor_sum2unkd) /*if @rur<>@cur_id begin*/ .CONVERT_SUM(@cur_dealing_id,@rur,@oper_date,0,@cursor_sum2unkd,@doc_sum_new,@rate_value_cc,@rate_date_cc) select @doc_sum_new=round(@doc_sum_new,2) ---end select @doc_sum_new_s=@doc_sum_new_s-@doc_sum_new --- if @c_s=6 ---- .EXIT_MESSAGE_PARM('',@doc_sum_new_s) ---select @doc_sum_new_s,@doc_sum_new,@c_s,@c_s_full if @doc_sum_new_s<>@doc_sum_new and @c_s=@c_s_full select @doc_sum_new=@doc_sum_new+@doc_sum_new_s ---select @doc_sum_new ---select @cursor_sum2unkd if @cur_id<>@bs1_cur_id begin ---select @doc_sum_new,@op_deal_sum,@in_date,@in_date,@cur_id,@bs1_profit_subc_str,@bs1_oblig_subc_str,@fin_result,@bs1_dog_character_id .BACK_CUROVR_VBR(2,@doc_sum_new,@cursor_sum2unkd,@in_date,@in_date,@cur_id,@bs1_profit_subc_str,@bs1_oblig_subc_str,@fin_result,@bs1_dog_character_id,@cur_dealing_id) end .BACK_MONEY_INOUT(@doc_sum_new,@in_date,@in_date,@cur_id,@home_subc_str,@ext_subc_str,@cursor_sum,@cursor_sum2unkd,@acc_cur_id,@cur_dealing_id) fetch cur2deal into @found_id,@cursor_sum,@cursor_sum2unkd end close cur2deal end ------------------ Задолженность клиента по ВБР if not(@client_contract_id is null) begin if exists(select 1 from t_items,t_types where t_items.id=@foundation_id and t_types.id=t_items.type_id and upper(t_types.type_code) in ('DOG_DEALINGS_SALE_VBR','DEALINGS_VIA_BROKER_SALE')) begin .BACK_CLIENT_OBLIG_VBR(0,0, @doc_sum , @in_date , @in_date , @cur_id , @deb_acc_sum , @deb_acc_sum , @bs1_client_subc_str ) end else begin open cur2deal fetch cur2deal into @found_id,@cursor_sum,@cursor_sum2unkd --select @d_s=@doc_sum select @d_deal_sum=@deb_acc_sum select @doc_sum_new_s=@doc_sum select @c_s=0 while .CURSOR_STATE=0 begin /* if @character_cur_price=1 begin select @op_deal_sum=round(@cursor_sum2unkd*@deal_perc,2) end else begin .CONVERT_SUM(@cur_id,@cur_dealing_id,@oper_date,0,@cursor_sum2unkd,@op_deal_sum,@rate_value_cc,@rate_date_cc) select @op_deal_sum=round(@op_deal_sum,2) end select @op_acc_sum=round(@cursor_sum*@pay_perc,2) */ select @c_s=@c_s+1 if @d_deal_sum>@cursor_sum2unkd begin select @d_acc_sum=@d_acc_sum-@cursor_sum ---select @rate_acc_sum=round(@cursor_sum*@rate_cur,4) select @d_deal_sum=@d_deal_sum-@cursor_sum2unkd end else begin select @cursor_sum2unkd=@d_deal_sum ---select @rate_acc_sum=round(@cursor_sum*@rate_cur,4) select @cursor_sum=@d_acc_sum end /*if @rur<>@cur_id begin*/ .CONVERT_SUM(@cur_dealing_id,@rur,@oper_date,0,@cursor_sum2unkd,@doc_sum_new,@rate_value_cc,@rate_date_cc) select @doc_sum_new=round(@doc_sum_new,2) ---end select @doc_sum_new_s=@doc_sum_new_s-@doc_sum_new if @doc_sum_new_s<>@doc_sum_new and @c_s=@c_s_full select @doc_sum_new=@doc_sum_new+@doc_sum_new_s .SUBC_STR_ADD(@bs1_client_subc_str,'doc_sec',@found_id) .BACK_CLIENT_OBLIG_VBR(0,0, @doc_sum_new , @in_date , @in_date , @cur_id ,@cursor_sum , @cursor_sum, @bs1_client_subc_str ) fetch cur2deal into @found_id,@cursor_sum,@cursor_sum2unkd end close cur2deal end end ------------------ Переоценка ---------------- declare @subc_str_stock varchar(255), @subc_str_money varchar(255) select @subc_str_money=@bs1_investor_subc_str .SUBC_STR_ADD(@subc_str_money,'contragents',@bs1_contragent_id,N) if exists(select 1 from t_items,t_types where t_items.id=@foundation_id and t_types.id=t_items.type_id and upper(t_types.type_code) in ('DOG_DEALINGS_SALE_VBR','DEALINGS_VIA_BROKER_SALE')) begin .SUBC_STR_ADD(@subc_str_money,'doc_sec',@foundation_id,N) select @subc_str_stock=@subc_str_money .SUBC_STR_ADD(@subc_str_stock,'stock_emis',@bs1_stock_id,N) .BACK_EXEC_OBLIGATION(@foundation_id,2,@subc_str_money,@subc_str_stock,@in_date,@owner_id,1) .BACK_SETUP_PROFIT_OR_LOSS(@foundation_id,@in_date,@owner_id) end else begin open cur2deal fetch cur2deal into @found_id,@cursor_sum,@cursor_sum2unkd while .CURSOR_STATE=0 begin .SUBC_STR_ADD(@subc_str_money,'doc_sec',@found_id,N) select @stock_id=(select stock_id from tb_baysale_docitems where id=@found_id) select @subc_str_stock=@subc_str_money .SUBC_STR_ADD(@subc_str_stock,'stock_emis',@stock_id,N) ----select @found_id .BACK_EXEC_OBLIGATION(@found_id,2,@subc_str_money,@subc_str_stock,@in_date,@owner_id,1) .BACK_SETUP_PROFIT_OR_LOSS(@found_id,@in_date,@owner_id) fetch cur2deal into @found_id,@cursor_sum,@cursor_sum2unkd end close cur2deal .DEALLOCATE cur2deal declare @count_deal int, @count_deal_opl int, @count_deal_compl int select @count_deal=(select count(td_depo_docs.id) from td_depo_docs where foundation_id=@foundation_id and .ITEMS_EXISTS_BY_TYPE(td_depo_docs.id,'DOG_DEALINGS_SALE_VBR')) select @count_deal_opl=(select count(td_depo_docs.id) from td_depo_docs where foundation_id=@foundation_id and .ITEMS_EXISTS_BY_TYPE_STATE(td_depo_docs.id,'DOG_DEALINGS_SALE_VBR','OPL_END')) select @count_deal_compl=(select count(td_depo_docs.id) from td_depo_docs where foundation_id=@foundation_id and .ITEMS_EXISTS_BY_TYPE_STATE(td_depo_docs.id,'DOG_DEALINGS_SALE_VBR','OBYAZ_END')) if @count_deal=@count_deal_opl begin .ITEM_MOVE_STATE(@foundation_id,'OPL_END') end else if @count_deal=@count_deal_compl begin .ITEM_MOVE_STATE(@foundation_id,'OBYAZ_END') end end end else if @pay_perpose=42 -- Окончание взаимозачета begin .TYPE_GET('SETOFF_FOR_DEALINGS',@foundation_id,1000) select @exec_sum = (select sum(t_paydocs.doc_sum) from t_paydocs where t_paydocs.foundation_id=@foundation_id and .ITEMS_EXISTS_BY_STATE(t_paydocs.id,@new_state_id)) if isnull(@exec_sum,0)=round(@sum_dc1000,2) begin .ITEM_MOVE_STATE(@foundation_id,'OP_END') end end ------------- По ПИФ #ifdef ALD_UKPIF if (.TEST_PIF_CLIENT(@client_contract_id)) and @pay_perpose not in (45, 47, 52, 120) begin exec ap_pif_exec_pay @sid, @action_history_id, @client_contract_id, 1, -- In_OR_OUT @purpose_id, @home_place_class_id, @home_object_id, @doc_sum, @in_date, @cur_id, @home_bank_acc_id, @contragent_id if @@error != 0 begin if @@trancount<>0 rollback tran return end if @pay_perpose in (10,15) begin .BACK_PIF_CLOSE90(@foundation_id,@action_id,@action_history_id) end end #endif if @pay_perpose=19 ---- снятие обязательств по счету begin declare @cred_acc_id numeric, @deb_acc_id numeric, @purp_name varchar(255) select @purp_name='Снятие обязательств - '+.PURPOSE_NAME2ID(@purpose_id) .TYPE_GET('BILL_OUT_COM_OPER',@foundation_id,100) .ACCID_FROM_CONST2PLAN(@cred_acc_id,'BACK_ACC_OBLIG_COMIS',@plan_money_id,3,1,'Счет кредиторской задолженности клиента',@mes) .ACCID_FROM_CONST2PLAN(@deb_acc_id,'BACK_ACC_OBLIG_COMIS',@plan_money_id,2,1,'Счет дебиторской задолженности перед клиентом',@mes) .OPERATION(@owner_id,@plan_money_id,@in_date,@cred_acc_id,@deb_acc_id,@doc_sum,@cur_id,@in_date,null,null,@purp_name,Y,@operation_id,@id,@action_id,@action_history_id,@doc_sum,@doc_sum,@cur_id,@cur_id) .UPDATE_SUBC(D,'PORTFOLIO',@portfolio_id100) .UPDATE_SUBC(D,'INVESTOR2PORTFOLIO',@portfolio_sub_id100) .UPDATE_SUBC(D,'INVESTORS',@contract_id100) .UPDATE_SUBC(D,'contragents',@contragent_id100) .UPDATE_SUBC(K,'PORTFOLIO',@portfolio_id) .UPDATE_SUBC(K,'INVESTOR2PORTFOLIO',@portfolio_sub_id) .UPDATE_SUBC(K,'INVESTORS',@organisation_id) .UPDATE_SUBC(K,'contragents',@from_partner_id) .SUBC_CHECK select @subc_str='' .SUBC_STR_ADD(@subc_str,'PORTFOLIO',@portfolio_id100,N) .SUBC_STR_ADD(@subc_str,'INVESTOR2PORTFOLIO',@portfolio_sub_id100,N) .SUBC_STR_ADD(@subc_str,'INVESTORS',@contract_id100,N) .SUBC_STR_ADD(@subc_str,'contragents',@contragent_id100,N) .SALDO(@owner_id,@plan_money_id,@in_date,@cred_acc_id,@subc_str,Y,-1,@all_sum,N,N) if @all_sum<0 .EXIT_MESSAGE('Превышена сумма по счету') if @all_sum=0 begin .ITEM_MOVE_STATE(@bill_id100,'PAYED') end end ---------------------------------------------------------------- -- погашение цб вбр, начисление дивидендов, погашение купона вбр ---------------------------------------------------------------- if @pay_perpose in (45, 47, 52,120) begin .ID_VAR(@oblig_acc_id) .ID_VAR(@purp_acc_id) .ID_VAR(@emmitent_id) .ID_VAR(@partner_id) .ID_VAR(@plan_cur_id) --! .ID_VAR(@cur_investor_id) .ID_VAR(@cur_portfolio_id) .ID_VAR(@cur_portfolio_sub_id) .ID_VAR(@to_acc_type_id) declare @sum_in_oper money declare @redemption_code varchar(50) --! /*счет обязательств*/ .ACCID_FROM_OBJ2PLAN(@oblig_acc_id,@purpose_id,@plan_money_id,1,1,'К расходу',@mes) /*счет ДС*/ .BACK_PLASE_CLASS_FROM_OBJ(@to_object_id,@organisation_id,@home_place_class_id) .ACCID_FROM_OBJ2PLAN(@purp_acc_id,@home_place_class_id,@plan_money_id,1,1,'К приходу',@mes) select @redemption_code=.ITEM_TYPE_CODE(@foundation_id) if @redemption_code not in ('STOCK_REDEMPTION','MONEY_REDEMPTION') .EXIT_MESSAGE('Задайте корпоративное действие') if upper(@redemption_code)='STOCK_REDEMPTION' begin /*ЦБ*/ select @stock_id = null select @stock_id = stock_id from ts_sd_stock_moves where ts_sd_stock_moves.id = @foundation_id if isnull(@stock_id, -1) = -1 begin .EXIT_MESSAGE('В корпоративном действии не задана ЦБ') end /*эммитент*/ select @emmitent_id = null select @emmitent_id = emmitent_id from td_stock_emis where td_stock_emis.id = @stock_id --- and .ITEMS_EXISTS_BY_TYPE(td_stock_emis.id,'TD_STOCK_EMIS') if isnull(@emmitent_id, -1) = -1 begin .EXIT_MESSAGE('У ЦБ не задан эммитент') end select @partner_id = null select @partner_id = partner_id from td_emitent_info where id = @emmitent_id if isnull(@partner_id, -1) = -1 begin .EXIT_MESSAGE('Зарегистрированное лицо эмитента не задано') end end else begin select @partner_id=@from_partner_id end select @oper_date = @in_date -- валюта плана select @plan_cur_id = .CUR2PLAN(@plan_money_id) select @plan_cur_id = cur_id from t_plans where t_plans.id = @plan_money_id and .ITEMS_EXISTS_BY_TYPE(t_plans.id,'PLANS') select @to_acc_type_id=acc_purpose_id from t_bank_accounts where id = @to_partner_acc_id if @to_partner_id=@owner_id and @to_acc_type_id=1 -- на наш собственный счет select @investor_id=@organisation_id else select @investor_id=null -- прочие случаи select @home_subc_str = '' if upper(@redemption_code)='STOCK_REDEMPTION' begin .SUBC_STR_ADD(@home_subc_str,'STOCK_EMIS',@stock_id,Y) end IF @pay_perpose NOT IN (120) BEGIN .SUBC_STR_ADD(@home_subc_str, 'DOC_SEC', @foundation_id,Y) END .SUBC_STR_ADD(@home_subc_str,'INVESTOR2PORTFOLIO',null,Y) .SUBC_STR_ADD(@home_subc_str,'PORTFOLIO',null,Y) .SUBC_STR_ADD(@home_subc_str,'CONTRAGENTS',null,Y) .SUBC_STR_ADD(@home_subc_str,'INVESTORS',null,Y) ---.SUBC_STR_ADD(@home_subc_str,'STOCK_EMIS',null,Y) ---- остатки по групповой операции .TOTALS_QUERY(@owner_id,@plan_money_id,@in_date,@in_date,@oblig_acc_id,@home_subc_str,L,N,Y,D,P,Y,S,N,N,N,L,N,Y) declare @ostatok money, @d2c_acc_sum money, @stock2oper_curr_id numeric, @full_oblig_sum money, @full_oblig_sum2accur money select @ostatok = sum(saldo_de) from #tmp_osv /* select * into t_test from #tmp_osv */ ---.EXIT_MESSAGE_PARM('',@to_acc_type_id) /*Блок удаляет все лишние строки из строк остатков, в зависимости от того, куда приходят ДС*/ -- на наш брокерский счет if @investor_id is null and @to_partner_id=@owner_id and @to_acc_type_id=4 and @pay_perpose<>52 --priymak 20.07.05 begin delete #tmp_osv where #tmp_osv.s1_id=@organisation_id or #tmp_osv.s1_id in (select c.id from tb_contracts c, td_depo_docs d where c.id=d.id and d.owner_id=@owner_id and c.dog_type_id=(select max(ct.id) from tb_contract_types ct where code='1' and .ITEMS_EXISTS_BY_TYPE(ct.id,'TYPE_CLIENT_CONTRACTS')) and .ITEMS_EXISTS_BY_TYPE(c.id,'CLIENT_CONTRACTS')) end -- на наш счет ДУ (или счет для ПИФ) if @investor_id is null and @to_partner_id=@owner_id and @to_acc_type_id in (2,3) and @pay_perpose<>52 --priymak 20.07.05 begin delete #tmp_osv where #tmp_osv.s1_id not in (select c.id from tb_contracts c, td_depo_docs d, t_bank_accounts b where c.id=d.id and d.owner_id=@owner_id and b.id = @to_partner_acc_id and c.client_id=b.fund_id and .ITEMS_EXISTS_BY_TYPE(c.id,'CLIENT_CONTRACTS')) end -- не для нас if @investor_id is null and @to_partner_id<>@owner_id begin delete #tmp_osv where #tmp_osv.s1_id not in (select c.id from tb_contracts c, td_depo_docs d where c.id=d.id and d.owner_id=@owner_id and c.client_id=@to_partner_id and .ITEMS_EXISTS_BY_TYPE(c.id,'CLIENT_CONTRACTS')) end -- ДЛЯ НАС -- @investor_id - ID учетной организации (BROKER_INFO) if @investor_id is not null and @to_partner_id=@owner_id and @to_acc_type_id in (1) begin delete #tmp_osv where #tmp_osv.s1_id in (select c.id from tb_contracts c, td_depo_docs d where c.id=d.id and d.owner_id=@owner_id and .ITEMS_EXISTS_BY_TYPE(c.id,'CLIENT_CONTRACTS')) end ------------------------priymak 20.07.05 if @investor_id is null and @to_partner_id=@owner_id and @to_acc_type_id in (2,3,4) and @pay_perpose=52 begin delete #tmp_osv where #tmp_osv.s1_id in (select c.id from tb_contracts c, td_depo_docs d where c.id=d.id and d.owner_id=@owner_id and .ITEMS_EXISTS_BY_TYPE(c.id,'CLIENT_CONTRACTS')) end ----------------------------- if @portfolio_sub_id is not null and @portfolio_sub_id<>(select max(ps.id) from tb_portfolio_section ps where ps.investor_id=@organisation_id and .ITEMS_EXISTS_BY_TYPE(ps.id,'PORTFOLIO_SECTION')) delete #tmp_osv where s3_id<>@portfolio_sub_id if @portfolio_id is not null delete #tmp_osv where s2_id<>@portfolio_id /*конец удалений*/ select @full_oblig_sum=sum(saldo_de) from #tmp_osv select @full_oblig_sum2accur=sum(ac_saldo_de) from #tmp_osv declare deb_rests cursor for select #tmp_osv.acc_id,#tmp_osv.s1_id, #tmp_osv.saldo_de,#tmp_osv.ac_saldo_de, #tmp_osv.s2_id, #tmp_osv.s3_id from #tmp_osv where #tmp_osv.saldo_de <> 0 for read only ---.EXIT_MESSAGE_PARM('@to_acc_type_id',@to_acc_type_id) open deb_rests fetch deb_rests into @oblig_acc_id, @cur_investor_id, @exec_sum,@acc_exec_sum, @cur_portfolio_id, @cur_portfolio_sub_id select @deb_acc_sum = @doc_sum select @emmitent_id = @partner_id select @acc_sum_exec_full = @sum_cur_dealing ---.EXIT_MESSAGE_PARM('@exec_sum',@exec_sum) while (.CURSOR_STATE = 0) begin ---.EXIT_MESSAGE_PARM('',@cur_portfolio_sub_id) select @home_subc_str = '' if upper(@redemption_code)='STOCK_REDEMPTION' begin .SUBC_STR_ADD(@home_subc_str,'STOCK_EMIS',@stock_id) end -- letsko_av -- полу-затычка. Тут в кредите счета 2.10.4 должна быть аналитика "Расчетный счет" и "Места нахождения активов" IF @pay_perpose IN (120) BEGIN .SUBC_STR_ADD(@home_subc_str, 'BANK_ACCOUNT', @to_partner_acc_id) .SUBC_STR_ADD(@home_subc_str, 'ASSET_PLACES', @home_object_id) END ELSE BEGIN .SUBC_STR_ADD(@home_subc_str,'DOC_SEC',@foundation_id) END .SUBC_STR_ADD(@home_subc_str,'INVESTOR2PORTFOLIO',@cur_portfolio_sub_id) .SUBC_STR_ADD(@home_subc_str,'PORTFOLIO',@cur_portfolio_id) .SUBC_STR_ADD(@home_subc_str,'CONTRAGENTS',@emmitent_id) .SUBC_STR_ADD(@home_subc_str,'INVESTORS',@cur_investor_id) select @ext_subc_str = '' .SUBC_STR_ADD(@ext_subc_str,'ASSET_PLACES',@to_object_id) .SUBC_STR_ADD(@ext_subc_str,'BANK_ACCOUNT',@to_partner_acc_id) .SUBC_STR_ADD(@ext_subc_str,'INVESTOR2PORTFOLIO',@cur_portfolio_sub_id) .SUBC_STR_ADD(@ext_subc_str,'PORTFOLIO',@cur_portfolio_id) .SUBC_STR_ADD(@ext_subc_str,'INVESTORS',@cur_investor_id) ---- .EXIT_MESSAGE_PARM('',@acc_exec_sum) ----- вычисление сумм проводки в валюте плана и валюте счета прапорционально суммам остатков if @pay_perpose in (47,120) begin select @acc_exec_sum=round(@sum_cur_dealing*round(@acc_exec_sum/@full_oblig_sum2accur,8),2) if @acc_sum_exec_full>@acc_exec_sum select @d2c_acc_sum=@acc_exec_sum, @sum_in_oper=round(((@acc_exec_sum*@cur_price)+(((@acc_exec_sum*@cur_price)/100.0)*@procent_plus)),2) ----@exec_sum else select @d2c_acc_sum=@acc_sum_exec_full, @sum_in_oper=@deb_acc_sum, @sum2prof2loss=1 end else if @pay_perpose in (45,52) begin select @exec_sum=round(@doc_sum*round(@exec_sum/@full_oblig_sum,8),2) if @deb_acc_sum > @exec_sum select @sum_in_oper=@exec_sum else select @sum_in_oper=@deb_acc_sum select @stock2oper_curr_id=.CUR_FROM_STOCK(@stock_id) --- .EXIT_MESSAGE('!!!!') .CONVERT_SUM(@plan_cur_id,@stock2oper_curr_id,@oper_date,0,@sum_in_oper,@d2c_acc_sum,@rate_value_cc,@rate_date_cc) select @d2c_acc_sum=round(@d2c_acc_sum,2) end if @sum_in_oper>0 ---- проводка по зачислению дс и списанию обязательства begin .OPERATION_SBC(@owner_id,@plan_money_id,@oper_date,@purp_acc_id,@oblig_acc_id,@sum_in_oper,@plan_cur_id,@oper_date,null,null,@mes,Y,@operation_id,@id,@action_id,@action_history_id,@d2c_acc_sum,@d2c_acc_sum,@ext_subc_str,@home_subc_str, @cur_dealing_id,@cur_dealing_id) -- проводки по ПИФ #ifdef ALD_UKPIF if .TEST_PIF_CLIENT(@cur_investor_id) begin exec ap_pif_exec_pay @sid, @action_history_id, @cur_investor_id, 1, -- In_OR_OUT @purpose_id, @home_place_class_id, @home_object_id, @sum_in_oper, @oper_date, @cur_id, @home_bank_acc_id, @emmitent_id if @@error != 0 begin if @@trancount<>0 rollback tran return end if @pay_perpose in (10,15) begin .BACK_PIF_CLOSE90(@foundation_id,@action_id,@action_history_id) end end #endif end select @deb_acc_sum = @deb_acc_sum - @sum_in_oper ,----@exec_sum @acc_sum_exec_full=@acc_sum_exec_full-@acc_exec_sum, @ostatok=isnull(@ostatok,0)-isnull(@sum_in_oper,0) if exists(select 1 from tb_contracts where id=@cur_investor_id) begin select @fin_result2oper=(select fin_result from tb_contracts where id=@cur_investor_id) select @character_id=2 end else begin select @fin_result2oper=1 select @character_id=1 end if @pay_perpose in (45,47,52) begin .SUBC_STR_ADD(@home_subc_str,'STOCK_EMIS',@stock_id) end /* if @sum2prof2loss=1 and @is_last_pay=1 begin .BACK_CLOSE_PROFIT_LOSS(@owner_id,@plan_money_id,@oper_date,@fin_result2oper,@character_id,@home_subc_str,4,@oblig_acc_id,@deb_acc_sum) end */ select @sum2prof2loss=0 fetch deb_rests into @oblig_acc_id, @cur_investor_id, @exec_sum,@acc_exec_sum, @cur_portfolio_id, @cur_portfolio_sub_id end close deb_rests .DEALLOCATE deb_rests IF (@pay_perpose <> 47) AND (@deb_acc_sum > 0) BEGIN .EXIT_MESSAGE_PARM('Не удалось найти задолженность на сумму ', @deb_acc_sum) END ELSE IF (@pay_perpose = 47) AND (@acc_sum_exec_full > 0) BEGIN .EXIT_MESSAGE_PARM('Не удалось найти задолженность на сумму ', @acc_sum_exec_full) END END #ifdef ALD_NPF ---------- Проводки НПФ if (.TEST_CLIENT_ACCOUNTING(@client_contract_id)) begin select @client_id=client_id from tb_contracts where id=@client_contract_id .EXEC_PROC((ap_real_payment_in_aliases @id),'Входящий платеж: формирование алиасов') .GENERATE_OPERATIONS(@client_id, 12,@id,@type_id,@action_id,@action_history_id,0,3,'Входящий платеж') end ------- Конец Проводки НПФ */ #endif ---------------------------------------------------------------- -- по групповым операциям и целевым назначениям перевод статуса групповой операции ---------------------------------------------------------------- ---.EXIT_MESSAGE_PARM('',@ostatok) ----- блок перевода статусов групповых операций if (@pay_perpose in (45, 47, 52,120)) and (@ostatok=0) begin if upper(@redemption_code) in ('STOCK_REDEMPTION','MONEY_REDEMPTION') begin declare @doc_state varchar(10) select @doc_state = t_states.state_code from t_items, t_states where t_items.id = @foundation_id and t_states.id = t_items.state_id if upper(@redemption_code)='STOCK_REDEMPTION' and @doc_state in ('ON_EXEC') begin .ITEM_MOVE_STATE(@foundation_id,'OP_END') end if upper(@redemption_code)='MONEY_REDEMPTION' and @doc_state in ('ISP') begin .ITEM_MOVE_STATE(@foundation_id,'CRT') end if @doc_state in ('OP_END','CRT') .EXIT_MESSAGE('Документ-основание платежа уже подтверждён') if @doc_state in ('ED','WAIT_ISP') .EXIT_MESSAGE('Документ-основание платежа находится в статусе "Редактируется" или "Ожидает исполнения", а должен в "На исполнении"') end end if @is_last_pay=1 ---- если в платеже стоит галочка что платеж последний begin declare @plan2partner_id numeric, @ostatok_qty money, @ostatok_acc money select @foundation_id_s=convert(varchar,@foundation_id) if (@pay_perpose in (45, 47, 52)) --- запуск процедуры принудительного закрытия begin --- .EXEC_ACTION(@foundation_id_s,'STOCK_REDEMPTION','STOCK_REDEMPTION_CLOSE') exec ap_stock_redemption_close @foundation_id_s,@id,@action_id,@action_history_id select @home_subc_str='' .SUBC_STR_ADD(@home_subc_str,'DOC_SEC',@foundation_id,Y) .SUBC_STR_ADD(@home_subc_str,'INVESTOR2PORTFOLIO',null,Y) .SUBC_STR_ADD(@home_subc_str,'PORTFOLIO',null,Y) .SUBC_STR_ADD(@home_subc_str,'CONTRAGENTS',null,Y) .SUBC_STR_ADD(@home_subc_str,'INVESTORS',null,Y) select @plan2partner_id=.PLAN2PARTNER_ID(@owner_id,@plan_money_id) select @ostatok=0 ---- проверка остатка .SALDO_NOW_SUM(@plan2partner_id,@oblig_acc_id,@home_subc_str,1,@ostatok,@ostatok_qty,@ostatok_acc) if @ostatok=0 begin .ITEM_MOVE_STATE(@foundation_id,'OP_END') end if @@error<>0 begin if @@trancount<>0 rollback tran return end end else if (@pay_perpose in (10,15)) begin --- занесение факт. даты платежа update tb_plan2fact_dates set fact_pay_date=(select max(d.in_date) from td_depo_docs d,t_paydocs p where p.foundation_id=@foundation_id and d.id=p.id and .ITEMS_EXISTS_BY_TYPE_STATE(p.id,'REAL_PAYMENT_IN','PERF')) where id=@foundation_id ---- принудительное закрытие сделки .EXEC_ACTION(@foundation_id_s,'DOG_DEALINGS_SALE_VBR','OBLIG_CLOSE_SALE_VBR') end end commit tran .END