create procedure dbo.%PROC% @sid varchar(30) = null, .ARGLIST as .BEGIN('N') declare @sid_n varchar(30) select @sid_n=@sid ---.EXIT_MESSAGE('!!!!') .CHECK_USER ---.CHECK_UPDATE .ARGCONVERT ----.GET_PROPS .TEST_UNIQUE .TYPE_GET('DOG_DEALINGS_BAY_VBR',@id) declare @new_deal_state_id numeric, @current_new_state_id numeric select @new_deal_state_id=.STATE_ID_FROM_CODE(@type_id,'NEW') select @current_new_state_id=a.src_id from t_actions_history a where a.doc_id=@id and a.dst_id is not null and a.id=(select max(aa.id) from t_actions_history aa where aa.doc_id=@id and aa.dst_id is not null) declare @purpose_code int, @coupon_account_sum_n money, @coupon_cur_id_n numeric, @coupon_account_sum_new money, @r_v money, @r_d datetime, @stock_price_input decimal(18,8), @summ_doc_total money, @summ_doc_total2unkd money select @purpose_code=.PURPOSE_CODE(@repo_type_id) if @purpose_code in (56,57) select @repo_rel_init_type_id=null, @marg_cred_doc_id=null, @repo_discount_sum=null, @repo_percent_year=null, @repo_days_year=null, @repo_corresp_deal_sum=null, @corr_exec_type_id=null, @corr_exec_place_id=null, @corr_exec_date_money=null, @corr_exec_date_sec=null, @is_zalog_id=null, @date_2part=null, @margin_deal=null, @disput_deal=null, @days_for_exec=null, @repo_2part=null, @regime_deal=null begin tran .TRANSACTION_SAVE ---.UPDATE_ITEMS ----.EXIT_MESSAGE_PARM('@type_id',@act_state_id) if @repo_rel_init_type_id=2 begin if @repo_doc_id is null begin if @current_new_state_id=@new_deal_state_id begin update t_items set state_id=@new_deal_state_id where id=@id /* delete t_actions_history where doc_id=@id and id=(select max(aa.id) from t_actions_history aa where aa.doc_id=@id) */ end .EXIT_MESSAGE('Укажите сделку') end end ---select @date_2part=dateadd(dd,@days_for_exec,@in_date1) .CALC_EXT_DATE(@in_date1,@days_for_exec,@date_2part) /* update tb_plan2fact_dates set plan_reg_date=@date_2part, plan_pay_date=@date_2part where id=@id */ if @purpose_code in (54,55) begin ---select @sale_place_id1=4 if @corr_exec_place_id is null select @corr_exec_place_id=@sale_place_id1 if @corr_exec_date_sec is null select @corr_exec_date_sec=@date_2part if @corr_exec_date_money is null select @corr_exec_date_money=@corr_exec_date_sec if @is_zalog_id is null begin if @current_new_state_id=@new_deal_state_id begin update t_items set state_id=@new_deal_state_id where id=@id .EXIT_MESSAGE('Не задан параметр отслеживания залога') end end if @repo_type_id is null begin if @current_new_state_id=@new_deal_state_id begin update t_items set state_id=@new_deal_state_id where id=@id .EXIT_MESSAGE('Целевое назначение не задано') end end declare @repo_type_new_id numeric, @summ money if @ticket_type_id1=1 select @repo_type_new_id=(select id from t_purposes where code=56) else select @repo_type_new_id=(select id from t_purposes where code=57) if isnull(@repo_2part,0)=0 begin if @current_new_state_id=@new_deal_state_id begin update t_items set state_id=@new_deal_state_id where id=@id .EXIT_MESSAGE('Не задана цена второй части!') end end select @repo_corresp_deal_sum=isnull(@repo_2part,0)*@stock_qty1 if @ticket_type_id1=1 select @ticket_type_id1=2 else select @ticket_type_id1=1 if @is_entry_cd1=1 begin /* if @ticket_type_id1=1 begin select @repo_corresp_deal_sum=@summ_doc_total2unkd1+round(@summ_doc_total2unkd1*(isnull(@repo_discount_sum,0)/100),2)+round(((@summ_doc_total2unkd1*(@repo_percent_year/100))/@repo_days_year)*datediff(day,@in_date1,@corr_exec_date_sec),2) select @ticket_type_id1=2 end else begin select @repo_corresp_deal_sum=@summ_doc_total2unkd1-round(@summ_doc_total2unkd1*(isnull(@repo_discount_sum,0)/100),2)-round(((@summ_doc_total2unkd1*(@repo_percent_year/100))/@repo_days_year)*datediff(day,@in_date1,@corr_exec_date_sec),2) select @ticket_type_id1=1 end */ select @summ_doc_total2unkd=@repo_corresp_deal_sum end else begin /* if @ticket_type_id1=1 begin select @repo_corresp_deal_sum=@summ_doc_total1+round(@summ_doc_total1*(isnull(@repo_discount_sum,0)/100),2)+round(((@summ_doc_total1*(@repo_percent_year/100))/@repo_days_year)*datediff(day,@in_date1,@corr_exec_date_sec),2) select @ticket_type_id1=2 end else begin select @repo_corresp_deal_sum=@summ_doc_total1-round(@summ_doc_total1*(isnull(@repo_discount_sum,0)/100),2)-round(((@summ_doc_total1*(@repo_percent_year/100))/@repo_days_year)*datediff(day,@in_date1,@corr_exec_date_sec),2) select @ticket_type_id1=1 end */ select @summ_doc_total=@repo_corresp_deal_sum end --.EXIT_MESSAGE_PARM('@repo_corresp_deal_sum',@repo_corresp_deal_sum) --.EXIT_MESSAGE_PARM('@summ_doc_total1',@summ_doc_total1) -- Результат @repo_corresp_deal_sum=null, @summ_doc_total1 имеет конкретное значение .BACK_CALC_COUPON(@stock_id1,@stock_qty1,@date_2part,1,@coupon_account_sum_n,@coupon_cur_id_n) if isnull(@coupon_account_sum_n,0)<>0 begin .CONVERT_SUM(@coupon_cur_id_n,@cur_id1,@date_2part,0,@coupon_account_sum_n,@coupon_account_sum_new,@r_v,@r_d) end else begin select @coupon_account_sum_n=null end if @is_entry_cd1=1 begin select @summ_doc_total=@repo_corresp_deal_sum-isnull(isnull(@coupon_account_sum_new,0),0) end else begin select @summ_doc_total2unkd=@repo_corresp_deal_sum+isnull(isnull(@coupon_account_sum_new,0),0) end declare @repo_rel_init_type_old_id numeric select @repo_rel_init_type_old_id=(select repo_rel_init_type_id from tb_repo_params where id=@id) if @repo_rel_init_type_old_id=1 and @repo_rel_init_type_old_id=@repo_rel_init_type_id and @repo_doc_id is not null begin if (select upper(state_code) from t_items,t_states where t_items.id=@repo_doc_id and t_states.id=t_items.state_id) in ('EDIT','EDIT_TRADE','ED','NEW','DEL') begin delete t_items where id=@repo_doc_id delete tb_baysale_docitems where id not in (select id from t_items) delete tb_plan2fact_dates where id not in (select id from t_items) delete td_depo_docs where id not in (select id from t_items) select @repo_doc_id=null end else .EXIT_MESSAGE('Невозможно отредактировать сделку , поскольку вторая части РЕПО подтверждена') end declare @rate_value_test money declare @broker_id numeric, @plan_money_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 .BACK_GET_ORG_PARMS(@owner_id1,@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 @pay_cur_id1 is null select @pay_cur_id1=.CUR2PLAN(@plan_money_id) if @pay_cur_id1<>@cur_id1 begin if @character_cur_price1 = 1 begin select @rate_value_test=.RATE_DATE_TEST(@rate_date1,@cur_id1) if @cur_price2pay_rate1 is null select @cur_price2pay_rate1=@rate_value_test select @sum_in_paycur1=(@cur_price2pay_rate1*@summ_doc_total2unkd)+(@cur_price2pay_rate1*@summ_doc_total2unkd)*(isnull(@procent_plus1,0)/100) end else begin .TEST_RATE2CUR2DATE(@in_date1,@cur_id1) .CONVERT_SUM(@cur_id1,@pay_cur_id1,@in_date1,0,@summ_doc_total2unkd,@sum_in_paycur1,@r_v,@r_d) end end else begin select @sum_in_paycur1=@summ_doc_total2unkd end if @repo_rel_init_type_id=1 and @repo_doc_id is null begin declare @in_no_new varchar(100), @type_new_id numeric, @state_new_id numeric .BACK_GEN_NO(@in_no_new,1,@date_2part) .TYPE_INIT('DOG_DEALINGS_BAY_VBR',2) .TYPE_ASSIGN(in_date,@date_2part,2) ---.TYPE_ASSIGN(in_date,@in_date1,2) .TYPE_ASSIGN(in_no,@in_no_new,2) .TYPE_ASSIGN(asset_place_id,@asset_place_id1,2) .TYPE_ASSIGN(folder_id,@folder_id1,2) .TYPE_ASSIGN(initiator_id,@initiator_id1,2) .TYPE_ASSIGN(instruction_on2off,@instruction_on2off1,2) .TYPE_ASSIGN(is_entry_cd,@is_entry_cd1,2) .TYPE_ASSIGN(movedate_from_id,@movedate_from_id1,2) --.TYPE_ASSIGN(nom_holder_id,@nom_holder_id1,2) .TYPE_ASSIGN(organisation_id,@organisation_id1,2) .TYPE_ASSIGN(owner_id,@owner_id1,2) .TYPE_ASSIGN(pay_cur_id,@pay_cur_id1,2) .TYPE_ASSIGN(paydate_from_id,@paydate_from_id1,2) .TYPE_ASSIGN(payer_id,@payer_id1,2) .TYPE_ASSIGN(payment_cur_id,@payment_cur_id1,2) .TYPE_ASSIGN(plan_dog_date,@plan_dog_date1,2) .TYPE_ASSIGN(plan_pay_date,@corr_exec_date_money,2) .TYPE_ASSIGN(plan_reg_date,@corr_exec_date_sec,2) .TYPE_ASSIGN(portfolio_id,@portfolio_id1,2) .TYPE_ASSIGN(portfolio_sub_id,@portfolio_sub_id1,2) .TYPE_ASSIGN(procent_plus,@procent_plus1,2) ---.TYPE_ASSIGN(properter_id,@properter_id1,2) .TYPE_ASSIGN(props_id,@props_id1,2) .TYPE_ASSIGN(rate_date,@rate_date1,2) .TYPE_ASSIGN(reg_place_id,@corr_exec_place_id,2) ---.TYPE_ASSIGN(resp_exec_tel,@resp_exec_tel1,2) ---.TYPE_ASSIGN(resp_executor,@resp_executor1,2) .TYPE_ASSIGN(sale_place_id,@corr_exec_place_id,2) .TYPE_ASSIGN(stock_id,@stock_id1,2) .TYPE_ASSIGN(stock_price,(@repo_corresp_deal_sum/@stock_qty1),2) .TYPE_ASSIGN(stock_qty,@stock_qty1,2) .TYPE_ASSIGN(sum_cur_id,@sum_cur_id1,2) .TYPE_ASSIGN(sum_in_paycur,@sum_in_paycur1,2) .TYPE_ASSIGN(summ_doc_total,@summ_doc_total,2) .TYPE_ASSIGN(summ_doc_total2unkd,@summ_doc_total2unkd,2) .TYPE_ASSIGN(ticket_type_id,@ticket_type_id1,2) .TYPE_ASSIGN(bank_account_id,@bank_account_id1,2) .TYPE_ASSIGN(character_cur_price,@character_cur_price1,2) .TYPE_ASSIGN(character_id,@character_id1,2) .TYPE_ASSIGN(client_contract_id,@client_contract_id1,2) .TYPE_ASSIGN(client_id,@client_id1,2) ---.TYPE_ASSIGN(cnt_nom_holder_id,@cnt_nom_holder_id,2) ---.TYPE_ASSIGN(cnt_properter_id,@cnt_properter_id,2) ---.TYPE_ASSIGN(cnt_resp_exec_tel,@cnt_resp_exec_tel,2) ---.TYPE_ASSIGN(cnt_resp_executor,@cnt_resp_executor,2) ---.TYPE_ASSIGN(comm_who_pays,@comm_who_pays,2) .TYPE_ASSIGN(contract_date,@in_date1,2) .TYPE_ASSIGN(contragent_id,@contragent_id1,2) .TYPE_ASSIGN(contrdate_from_id,@contrdate_from_id1,2) .TYPE_ASSIGN(correspondent_id,@correspondent_id1,2) --.TYPE_ASSIGN(coupon_account_sum,@coupon_account_sum_n,2) .TYPE_ASSIGN(coupon_account_sum, @coupon_account_sum1, 2) --.TYPE_ASSIGN(coupon_cur_id,@coupon_cur_id_n,2) .TYPE_ASSIGN(coupon_cur_id, @coupon_cur_id1, 2) --.TYPE_ASSIGN(coupon_deal_sum,@coupon_account_sum_new,2) .TYPE_ASSIGN(coupon_deal_sum, @coupon_deal_sum1, 2) .TYPE_ASSIGN(cur_id,@cur_id1,2) .TYPE_ASSIGN(cur_price2pay_rate,@cur_price2pay_rate1,2) .TYPE_ASSIGN(days_for_regs,@days_for_regs1,2) .TYPE_ASSIGN(days_for_sign,@days_for_sign1,2) .TYPE_ASSIGN(days_to_pay,@days_to_pay1,2) .TYPE_ASSIGN(dealing_look,@dealing_look1,2) .TYPE_ASSIGN(depo_doc_type,@depo_doc_type1,2) .TYPE_ASSIGN(depo_subacc_id,@depo_subacc_id1,2) .TYPE_ASSIGN(document_form_id,@document_form_id1,2) .TYPE_ASSIGN(dog_calc_order,@dog_calc_order1,2) .TYPE_ASSIGN(external_broker_id,@external_broker_id1,2) .TYPE_ASSIGN(sale_type_id,@sale_type_id1,2) .TYPE_ASSIGN(stock_price_input,(@repo_corresp_deal_sum/@stock_qty1),2) .TYPE_ASSIGN(stock_qty_input,@stock_qty_input1,2) .TYPE_ASSIGN(deal_units,1,2) .TYPE_INSERT('DOG_DEALINGS_BAY_VBR','EDIT',2) select @repo_doc_id=@id2_1 select @sid=convert(varchar(30),@id2_1) if @corr_exec_type_id=2 begin if @ticket_type_id1=2 begin .TYPE_ID_FROM_CODE('DEALINGS_VIA_BROKER_SALE',@type_new_id) select @state_new_id=.STATE_ID_FROM_CODE(@type_new_id,'ED') end else if @ticket_type_id1=1 begin .TYPE_ID_FROM_CODE('DEALINGS_VIA_BROKER',@type_new_id) select @state_new_id=.STATE_ID_FROM_CODE(@type_new_id,'ED') end end else if @corr_exec_type_id=1 begin if @ticket_type_id1=2 begin .TYPE_ID_FROM_CODE('DOG_DEALINGS_SALE_VBR',@type_new_id) select @state_new_id=.STATE_ID_FROM_CODE(@type_new_id,'EDIT') end else if @ticket_type_id1=1 begin .TYPE_ID_FROM_CODE('DOG_DEALINGS_BAY_VBR',@type_new_id) select @state_new_id=.STATE_ID_FROM_CODE(@type_new_id,'EDIT') end end update t_items set type_id=@type_new_id, state_id=@state_new_id where id=@repo_doc_id if @corr_exec_type_id=2 begin if @ticket_type_id1=2 begin .EXEC_ACTION(@sid,'DEALINGS_VIA_BROKER_SALE','DATA2IMPORT_SALE') end else if @ticket_type_id1=1 begin .EXEC_ACTION(@sid,'dealings_via_broker','DATA2IMPORT_BAY') end end end if @repo_doc_id is not null begin if not exists (select 1 from tb_repo_params where id=@repo_doc_id) begin insert tb_repo_params(id,repo_type_id,repo_rel_init_type_id,repo_doc_id,repo_2part) values(@repo_doc_id,@repo_type_new_id,@repo_rel_init_type_id,@id,@repo_corresp_deal_sum/@stock_qty1) if @@rowcount =0 begin if @current_new_state_id=@new_deal_state_id begin update t_items set state_id=@new_deal_state_id where id=@id rollback tran raiserror 40000 'Вставка в таблицу документа tb_repo_params не прошла' return end end end else begin update tb_repo_params set repo_type_id=@repo_type_new_id, repo_rel_init_type_id=@repo_rel_init_type_id, repo_doc_id=@id, repo_2part=@repo_corresp_deal_sum/@stock_qty1 where id=@repo_doc_id end end update td_depo_docs set oper_date=@date_2part where id=@repo_doc_id end -- Апдэйтить только если сделка не ВБР declare @doc_type_code varchar(150) select @doc_type_code = t_types.type_code from t_types where id=(select t_items.type_id from t_items where id=@id) if upper(rtrim(ltrim(@doc_type_code))) not in ('DOG_DEALINGS_SALE_VBR','DOG_DEALINGS_BAY_VBR', 'DOG_DEALINGS_BAY_VBR_VEKSEL','DOG_DEALINGS_SALE_VBR_VEKSEL') begin update tb_plan2fact_dates set plan_reg_date=@date_2part, plan_pay_date=@date_2part where id=@id end .UPDATE .RELGEN ---.ACTION_HISTORY .TRANSACTION_RESTORE exec ap_calculate_comis @sid_n commit tran .END