create procedure dbo.%PROC% @sid varchar(30) = null, .ARGLIST 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% .CHECK_USER .CHECK_INSERT .ARGCONVERT .INIT_VARS .TEST_UNIQUE if upper(.ITEM_STATE_CODE(@extract_id))='OP_END' .EXIT_MESSAGE('Добавить невозможно - отчет регистратора подтвержден') if upper(.ITEM_STATE_CODE(@extract_id))='ED' .EXIT_MESSAGE('Добавить невозможно - данные отчета регистратора редактируются') /* -- kashuba_av 20.01.06 begin Закоментировал, так как этот кусок запрещает добавления нескольких заявок в случае нескольких платежей if exists(select 1 from tp_extract2claim where tp_extract2claim.claim_id=@claim_id and tp_extract2claim.extract_id=@extract_id) .EXIT_MESSAGE('Данная заявка уже есть в этом отчете') -- kashuba_av 20.01.06 end */ if @claim_type_id=2 begin if (select pay_type_id from tp_pif_queries where id=@claim_id)=1 begin if isnull(@share_qty,0)<=0 .EXIT_MESSAGE('Введите количество паев') if @share_qty > (select stock_qty from tp_pif_queries where id=@claim_id) .EXIT_MESSAGE('Количество списываемых паев не должно превышать количество паев в выбранной заявке') end select @share_qty_c=@share_qty end if (select pif_pay_type_id from tp_pif_queries where id=@claim_id)=1 or @agent_dog_id is null begin select @agent_dog_id=agent_dog_id from tp_pif_queries where id=@claim_id select @pay_agent_id=agent_id from tp_pif_queries where id=@claim_id end else select @pay_agent_id=client_id from tb_contracts where id=@agent_dog_id declare @claim_fund_id numeric, @agent_fund_id numeric, @fund_id numeric select @claim_fund_id=owner_id from td_depo_docs where id=@claim_id select @agent_fund_id=owner_id from td_depo_docs where id=@agent_dog_id select @fund_id=owner_id from td_depo_docs where id=@extract_id if @claim_fund_id<>@fund_id .EXIT_MESSAGE('Выбрана заявка другого фонда') if @agent_dog_id is not null and @agent_fund_id<>@fund_id .EXIT_MESSAGE('Выбран агент для другого фонда') .INSERT_ITEMS .INSERT select @sid=convert(varchar(30),@id) if @claim_type_id=1 -- платежи для заявок на размещение begin if @get_paymt_type=2 begin .EXEC_ACTION(@sid,'PIF_PAYS2REPORT','GET_PAYMENTS',Y) end if @get_paymt_type=1 begin .ID_VAR(@paymt_id) select @paymt_id=p.id from t_paydocs p, t_items i, t_states s, t_types t where i.id = p.id and p.pif_pay_id = @claim_id and p.doc_sum = @sum_for_transfer and i.state_id = s.id and i.type_id = t.id and s.class_id =0 and upper(s.state_code) = 'PERF' and upper(t.type_code) in ('SD_PAYBANK_IN','SD_PAYDOCS_CASH_IN') and not exists (select 1 from tp_paymt2pays, .ACTUAL_TABLES where tp_paymt2pays.paymt_id=p.id .ACTUAL_WHERE(tp_paymt2pays.id)) if @paymt_id is not null and not exists(select 1 from tp_paymt2pays where tp_paymt2pays.paymt_id=@paymt_id) begin .TYPE_INIT('PIF_PAYMT2PAYS') .TYPE_ASSIGN(pif_pay_id,@id) .TYPE_ASSIGN(paymt_id,@paymt_id) .TYPE_INSERT('PIF_PAYMT2PAYS','CRT') end end if exists(select 1 from tp_paymt2pays pp, t_paydocs p where pp.pif_pay_id=@id and pp.paymt_id=p.id and p.pif_pay_id!=@claim_id and .ITEMS_EXISTS(pp.id)) .EXIT_MESSAGE('Заявка, указанная в выбранном платеже не соответствует данной заявке в отчете') select @paymt_str='' declare @paymt_date varchar(8), @paymt_no varchar(30) declare paydocs cursor for select isnull(convert(varchar(8),p.doc_date,3),''), isnull(rtrim(ltrim(p.doc_number)),'') from t_paydocs p, tp_paymt2pays pp where pp.paymt_id=p.id and pp.pif_pay_id = @id and .ITEMS_EXISTS(pp.id) open paydocs fetch paydocs into @paymt_date, @paymt_no while .CURSOR_STATE = 0 begin select @paymt_str=@paymt_str+'№'+@paymt_no+' от '+@paymt_date+':' fetch paydocs into @paymt_date, @paymt_no end close paydocs .DEALLOCATE paydocs update tp_extract2claim set paymt_str=@paymt_str where id=@id end .EXEC_ACTION(@sid,'PIF_PAYS2REPORT','CLAIM_CALC_PARTS',Y) .UPDATE_STATE .RELGEN .ACTION_HISTORY select sid = convert(varchar, @id) commit tran