create procedure dbo.%PROC% @code varchar(255), @r_no varchar(255), @q_no varchar(255), @q_date varchar(255), @qty varchar(255), @pay_date_s varchar(255), @sk_proc varchar(255), @agent_sum varchar(255), @pay_price varchar(255), @q_sum varchar(255), @round varchar(255), @acc_no varchar(255), @agent varchar(255), @agent_up varchar(255), @payer varchar(255), @pay_price_op varchar(255), @as_of varchar(255), @a_fond varchar(255), @a_acc_no varchar(255), @a_payer varchar(255), @q_type varchar(255), @status varchar(255), @op_date varchar(255), @reason varchar(255), @p_no varchar(255), @p_date varchar(255), @b_sum varchar(255), @bank varchar(255), @b_rec_no varchar(255), @b_date varchar(255)--, --@report_id_s varchar(30) 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 select @action_id = %ACTION_ID% .ID_VAR(@report_id) .ID_VAR(@q_type_id) .INT_VAR(@is_cancel) declare @q_date_d datetime declare @p_date_d datetime declare @q_sum_m money declare @qty_m .PIF_PAY_TYPE declare @agent_sum_m money declare @reg_date datetime declare @pay_date datetime declare @owner_id numeric(18,0) declare @owner_imp_id numeric(18,0) declare @op_date_d datetime declare @pay_date_d datetime declare @sk_proc_m money declare @pay_price_m money declare @round_m money declare @pay_price_op_m money declare @as_of_m money declare @indoc_type_id numeric, @document_form_id numeric, @indoc_comment varchar(255), @corr_name varchar(255), @corr_id numeric, @indoc_id numeric, @in_time datetime, @indoc_id_s varchar(30), @days int, @out_date datetime, @indate datetime, @out_no varchar(50), @indocs_id numeric select @in_time=.OPERDAY select @indate=.OPERDAY declare @fund_id numeric .NRC_CUT_FIELD(@code) .NRC_CUT_FIELD(@r_no) .NRC_CUT_FIELD(@q_no) .NRC_CUT_FIELD(@qty) .NRC_CUT_FIELD(@sk_proc) .NRC_CUT_FIELD(@agent_sum) .NRC_CUT_FIELD(@pay_price) .NRC_CUT_FIELD(@q_sum) .NRC_CUT_FIELD(@round) .NRC_CUT_FIELD(@acc_no) .NRC_CUT_FIELD(@agent) .NRC_CUT_FIELD(@agent_up) .NRC_CUT_FIELD(@payer) .NRC_CUT_FIELD(@pay_price_op) .NRC_CUT_FIELD(@as_of) .NRC_CUT_FIELD(@a_fond) .NRC_CUT_FIELD(@a_acc_no) .NRC_CUT_FIELD(@a_payer) .NRC_CUT_FIELD(@status) .NRC_CUT_FIELD(@op_date) .NRC_CUT_FIELD(@reason) .NRC_CUT_FIELD(@p_no) .NRC_CUT_FIELD(@b_sum) .NRC_CUT_FIELD(@bank) .NRC_CUT_FIELD(@b_rec_no) .NRC_CUT_FIELD(@q_date) .NRC_CUT_FIELD(@pay_date_s) .NRC_CUT_FIELD(@q_type) .NRC_CUT_FIELD(@p_date) .NRC_CUT_FIELD(@b_date) .ID_VAR(@folder_id) select @folder_id = .DEPO_FOLDER_DEFAULT select @q_sum = replace(@q_sum,',','.') select @b_sum = replace(@b_sum,',','.') select @qty = replace(@qty,',','.') select @sk_proc= replace(@sk_proc,',','.') select @pay_price= replace(@pay_price,',','.') select @round = replace(@round,',','.') select @pay_price_op = replace(@pay_price_op,',','.') select @as_of= replace(@as_of,',','.') select @agent_sum = replace(@agent_sum,',','.') if @q_type not in ('4') begin select @op_date_d = convert(datetime,@op_date) select @pay_date_d = convert(datetime,@pay_date_s) select @owner_imp_id = td_depo_docs.owner_id from td_depo_docs, t_item2class, t_classvalues, t_classes where td_depo_docs.owner_id = t_item2class.item_id and t_classvalues.id=t_item2class.value_id and t_classvalues.code='FUND_IMPORT_CODE' and t_classes.id=t_classvalues.class_id and t_classes.code='FUND_IMPORT_CODE' and t_item2class.code = @code if @status = '1' select @status = '0' if @status = '2' select @status = '1' --** Отчет регистратора if @status = 0 begin select @report_id = ts_reg_report.id from ts_reg_report, td_depo_docs where ts_reg_report.reg_date = @op_date_d and ts_reg_report.pay_date = @pay_date_d and td_depo_docs.owner_id = @owner_imp_id and td_depo_docs.id = ts_reg_report.id and .ITEMS_EXISTS_BY_TYPE_STATE(ts_reg_report.id,'PIF_PAY_REPORT','WAIT_CHECK') and .ITEMS_EXISTS_BY_TYPE(ts_reg_report.id,'PIF_PAY_REPORT') if @report_id is null ----- вставка отчета ----- begin .TYPE_INIT('PIF_PAY_REPORT',1) .TYPE_ASSIGN(owner_id,@owner_imp_id,1) .TYPE_ASSIGN(reg_date,@op_date_d,1) .TYPE_ASSIGN(in_date,@op_date_d,1) .TYPE_ASSIGN(folder_id,@folder_id,1) .TYPE_ASSIGN(pay_date,@pay_date_d,1) .TYPE_INSERT('PIF_PAY_REPORT','ED_CLAIM',1) select @id = @id1_1 select @report_id = @id1_1 .DEPO_GEN_NO(@in_no1_1,1) .TYPE_UPDATE(@id1_1,'PIF_PAY_REPORT',1) ------- генерация вход. документа---------- -----проверка существования вх. документа с типом "отчета регистратора" ,с данным фондом и датой if not exists(select 1 from t_sd_indocs where in_date=.OPERDAY and owner_id=@owner_imp_id and indoc_type_id=(select id FROM t_sd_indoc_types WHERE indoc_name = 'Отчет регистратора') and .ITEMS_EXISTS_BY_TYPE(t_sd_indocs.id,'SD_INDOC')) begin select @document_form_id=1 select @corr_id=spec_reg_id from td_pay_fond where id=@owner_imp_id select @corr_name=partner_name from t_partners where id = @corr_id select @indoc_comment='Фонды УК "ДВС Инвестмент"' select @indoc_type_id=id FROM t_sd_indoc_types WHERE indoc_name = 'Отчет регистратора' --время регистрации if not exists(select 1 from t_sd_indocs where in_date=.OPERDAY and .ITEMS_EXISTS_BY_TYPE(t_sd_indocs.id,'SD_INDOC')) select @in_time=dateadd(Hour,11,@in_time) else begin --select @in_time=dateadd(Hour,11,@in_time) select @in_time=dateadd(mi,1,max(t.in_time)) from t_sd_indocs t where in_date=.OPERDAY and .ITEMS_EXISTS_BY_TYPE(t.id,'SD_INDOC') end exec ap_calc_end_period @date_beg = @indate, @days_beg = -1, @date_end = @out_date out, @days =@days select @out_no =convert(varchar(10), @out_date,3) select @out_no=replace(@out_no,'/','') .TYPE_INIT('SD_INDOC',4) .TYPE_ASSIGN(out_date,@out_date,4) .TYPE_ASSIGN(in_date,.OPERDAY,4) .TYPE_ASSIGN(out_no,@out_no,4) .TYPE_ASSIGN(in_time,@in_time,4) .TYPE_ASSIGN(indoc_type_id,@indoc_type_id,4) .TYPE_ASSIGN(corr_name,@corr_name,4) .TYPE_ASSIGN(document_form_id,@document_form_id,4) .TYPE_ASSIGN(folder_id,.DEPO_FOLDER_DEFAULT,4) .TYPE_ASSIGN(owner_id,@owner_imp_id,4) .TYPE_ASSIGN(comment,@indoc_comment,4) .TYPE_ASSIGN(send_date,.OPERDAY,4) .TYPE_INSERT('SD_INDOC','ED',4) select @indoc_id=.TYPE_FIELD(id,4) select @indoc_id_s=convert(varchar(30),@indoc_id) exec ap_sd_indoc_fix @indoc_id_s if @@error<>0 begin if @@trancount>0 rollback tran return end .TYPE_INIT('SD_INDOC2DOC',5) .TYPE_ASSIGN(indoc_id,@id4_1,5) .TYPE_ASSIGN(doc_id,@id1_1,5) .TYPE_INSERT('SD_INDOC2DOC','CRT',5) --курсор declare curs1 cursor for select id from td_pay_fond where ctrl_comp_id=(select ctrl_comp_id from td_pay_fond where td_pay_fond.id=@owner_imp_id) and .ITEMS_EXISTS_BY_TYPE(td_pay_fond.id,'PIF_FUND') and not exists(select 1 from t_sd_indocs where t_sd_indocs.in_date=.OPERDAY and t_sd_indocs.owner_id=td_pay_fond.id and t_sd_indocs.indoc_type_id=(select id FROM t_sd_indoc_types WHERE indoc_name = 'Отчет регистратора') and .ITEMS_EXISTS_BY_TYPE(t_sd_indocs.id,'SD_INDOC')) open curs1 fetch curs1 into @fund_id while .CURSOR_STATE=0 begin select @corr_id = null, @corr_name = null select @document_form_id=1 select @corr_id=spec_reg_id from td_pay_fond where id=@fund_id select @corr_name=partner_name from t_partners where id =@corr_id select @indoc_comment='Фонды УК "ДВС Инвестмент"' select @indoc_type_id=id FROM t_sd_indoc_types WHERE indoc_name = 'Отчет регистратора' --время регистрации if not exists(select 1 from t_sd_indocs where in_date=.OPERDAY and .ITEMS_EXISTS_BY_TYPE(t_sd_indocs.id,'SD_INDOC')) select @in_time=dateadd(Hour,11,@in_time) else begin --select @in_time=dateadd(Hour,11,@in_time) select @in_time=dateadd(mi,1,max(t.in_time)) from t_sd_indocs t where in_date=.OPERDAY and .ITEMS_EXISTS_BY_TYPE(t.id,'SD_INDOC') end exec ap_calc_end_period @date_beg = @indate, @days_beg = -1, @date_end = @out_date out, @days =@days select @out_no =convert(varchar(10), @out_date,3) select @out_no=replace(@out_no,'/','') .TYPE_INIT('SD_INDOC',10) .TYPE_ASSIGN(out_date,@out_date,10) .TYPE_ASSIGN(in_date,.OPERDAY,10) .TYPE_ASSIGN(out_no,@out_no,10) .TYPE_ASSIGN(in_time,@in_time,10) .TYPE_ASSIGN(indoc_type_id,@indoc_type_id,10) .TYPE_ASSIGN(corr_name,@corr_name,10) .TYPE_ASSIGN(document_form_id,@document_form_id,10) .TYPE_ASSIGN(folder_id,.DEPO_FOLDER_DEFAULT,10) .TYPE_ASSIGN(owner_id,@fund_id,10) .TYPE_ASSIGN(comment,@indoc_comment,10) .TYPE_ASSIGN(send_date,.OPERDAY,10) .TYPE_INSERT('SD_INDOC','ED',10) select @indoc_id=.TYPE_FIELD(id,10) select @indoc_id_s=convert(varchar(30),@indoc_id) select @corr_id = null, @corr_name = null --test exec ap_sd_indoc_fix @indoc_id_s if @@error<>0 begin if @@trancount>0 rollback tran return end fetch curs1 into @fund_id end close curs1 deallocate curs1 end else begin --прикрепляем к существующему вход. документу select @indocs_id=id from t_sd_indocs where in_date=.OPERDAY and owner_id=@owner_imp_id and indoc_type_id=(select id FROM t_sd_indoc_types WHERE indoc_name = 'Отчет регистратора') and .ITEMS_EXISTS_BY_TYPE(t_sd_indocs.id,'SD_INDOC') .TYPE_INIT('SD_INDOC2DOC',6) .TYPE_ASSIGN(indoc_id,@indocs_id,6) .TYPE_ASSIGN(doc_id,@id1_1,6) .TYPE_INSERT('SD_INDOC2DOC','CRT',6) end -------конец генерации вход. документа---------- end end --** if @status = 1 begin select @report_id = ts_reg_report.id from ts_reg_report, td_depo_docs where ts_reg_report.reg_date = @op_date_d and ts_reg_report.pay_date = @pay_date_d and td_depo_docs.owner_id = @owner_imp_id and td_depo_docs.id = ts_reg_report.id and .ITEMS_EXISTS_BY_TYPE_STATE(ts_reg_report.id,'PIF_PAY_CANCEL','WAIT_CHECK') and .ITEMS_EXISTS_BY_TYPE(ts_reg_report.id,'PIF_PAY_CANCEL') if @report_id is null ----- вставка отчета ----- begin .TYPE_INIT('PIF_PAY_CANCEL',2) .TYPE_ASSIGN(owner_id,@owner_imp_id,2) .TYPE_ASSIGN(reg_date,@op_date_d,2) .TYPE_ASSIGN(in_date,@op_date_d,2) .TYPE_ASSIGN(folder_id,@folder_id,2) .TYPE_ASSIGN(pay_date,@pay_date_d,2) .TYPE_INSERT('PIF_PAY_CANCEL','ED_CLAIM',2) select @id = @id2_1 select @report_id = @id2_1 .DEPO_GEN_NO(@in_no2_1,2) .TYPE_UPDATE(@id2_1,'PIF_PAY_CANCEL',2) ------- генерация вход. документа---------- -----проверка существования вх. документа с типом "отчета регистратора" ,с данным фондом и датой if not exists(select 1 from t_sd_indocs where in_date=.OPERDAY and owner_id=@owner_imp_id and indoc_type_id=(select id FROM t_sd_indoc_types WHERE indoc_name = 'Отчет регистратора') and .ITEMS_EXISTS_BY_TYPE(t_sd_indocs.id,'SD_INDOC')) begin select @document_form_id=1 select @corr_id=spec_reg_id from td_pay_fond where id=@owner_imp_id select @corr_name=partner_name from t_partners where id = @corr_id select @indoc_comment='Фонды УК "ДВС Инвестмент"' select @indoc_type_id=id FROM t_sd_indoc_types WHERE indoc_name = 'Отчет регистратора' --время регистрации if not exists(select 1 from t_sd_indocs where in_date=.OPERDAY and .ITEMS_EXISTS_BY_TYPE(t_sd_indocs.id,'SD_INDOC')) select @in_time=dateadd(Hour,11,@in_time) else begin --select @in_time=dateadd(Hour,11,@in_time) select @in_time=dateadd(mi,1,max(t.in_time)) from t_sd_indocs t where in_date=.OPERDAY and .ITEMS_EXISTS_BY_TYPE(t.id,'SD_INDOC') end exec ap_calc_end_period @date_beg = @indate, @days_beg = -1, @date_end = @out_date out, @days =@days select @out_no =convert(varchar(10), @out_date,3) select @out_no=replace(@out_no,'/','') .TYPE_INIT('SD_INDOC',7) .TYPE_ASSIGN(out_date,@out_date,7) .TYPE_ASSIGN(in_date,.OPERDAY,7) .TYPE_ASSIGN(out_no,@out_no,7) .TYPE_ASSIGN(in_time,@in_time,7) .TYPE_ASSIGN(indoc_type_id,@indoc_type_id,7) .TYPE_ASSIGN(corr_name,@corr_name,7) .TYPE_ASSIGN(document_form_id,@document_form_id,7) .TYPE_ASSIGN(folder_id,.DEPO_FOLDER_DEFAULT,7) .TYPE_ASSIGN(owner_id,@owner_imp_id,7) .TYPE_ASSIGN(comment,@indoc_comment,7) .TYPE_ASSIGN(send_date,.OPERDAY,7) .TYPE_INSERT('SD_INDOC','ED',7) select @indoc_id=.TYPE_FIELD(id,7) select @indoc_id_s=convert(varchar(30),@indoc_id) exec ap_sd_indoc_fix @indoc_id_s if @@error<>0 begin if @@trancount>0 rollback tran return end .TYPE_INIT('SD_INDOC2DOC',8) .TYPE_ASSIGN(indoc_id,@id7_1,8) .TYPE_ASSIGN(doc_id,@id2_1,8) .TYPE_INSERT('SD_INDOC2DOC','CRT',8) declare curs1 cursor for select id from td_pay_fond where ctrl_comp_id=(select ctrl_comp_id from td_pay_fond where td_pay_fond.id=@owner_imp_id) and .ITEMS_EXISTS_BY_TYPE(td_pay_fond.id,'PIF_FUND') and not exists(select 1 from t_sd_indocs where t_sd_indocs.in_date=.OPERDAY and t_sd_indocs.owner_id=td_pay_fond.id and t_sd_indocs.indoc_type_id=(select id FROM t_sd_indoc_types WHERE indoc_name = 'Отчет регистратора') and .ITEMS_EXISTS_BY_TYPE(t_sd_indocs.id,'SD_INDOC')) open curs1 fetch curs1 into @fund_id while .CURSOR_STATE=0 begin select @corr_id = null, @corr_name = null select @document_form_id=1 select @corr_id=spec_reg_id from td_pay_fond where id=@fund_id select @corr_name=partner_name from t_partners where id =@corr_id select @indoc_comment='Фонды УК "ДВС Инвестмент"' select @indoc_type_id=id FROM t_sd_indoc_types WHERE indoc_name = 'Отчет регистратора' --время регистрации if not exists(select 1 from t_sd_indocs where in_date=.OPERDAY and .ITEMS_EXISTS_BY_TYPE(t_sd_indocs.id,'SD_INDOC')) select @in_time=dateadd(Hour,11,@in_time) else begin --select @in_time=dateadd(Hour,11,@in_time) select @in_time=dateadd(mi,1,max(t.in_time)) from t_sd_indocs t where in_date=.OPERDAY and .ITEMS_EXISTS_BY_TYPE(t.id,'SD_INDOC') end exec ap_calc_end_period @date_beg = @indate, @days_beg = -1, @date_end = @out_date out, @days =@days select @out_no =convert(varchar(10), @out_date,3) select @out_no=replace(@out_no,'/','') .TYPE_INIT('SD_INDOC',11) .TYPE_ASSIGN(out_date,@out_date,11) .TYPE_ASSIGN(in_date,.OPERDAY,11) .TYPE_ASSIGN(out_no,@out_no,11) .TYPE_ASSIGN(in_time,@in_time,11) .TYPE_ASSIGN(indoc_type_id,@indoc_type_id,11) .TYPE_ASSIGN(corr_name,@corr_name,11) .TYPE_ASSIGN(document_form_id,@document_form_id,11) .TYPE_ASSIGN(folder_id,.DEPO_FOLDER_DEFAULT,11) .TYPE_ASSIGN(owner_id,@fund_id,11) .TYPE_ASSIGN(comment,@indoc_comment,11) .TYPE_ASSIGN(send_date,.OPERDAY,11) .TYPE_INSERT('SD_INDOC','ED',11) select @indoc_id=.TYPE_FIELD(id,11) select @indoc_id_s=convert(varchar(30),@indoc_id) select @corr_id = null, @corr_name = null --test exec ap_sd_indoc_fix @indoc_id_s if @@error<>0 begin if @@trancount>0 rollback tran return end fetch curs1 into @fund_id end close curs1 deallocate curs1 end else begin --прикрепляем к существующему вход. документу select @indocs_id=id from t_sd_indocs where in_date=.OPERDAY and owner_id=@owner_imp_id and indoc_type_id=(select id FROM t_sd_indoc_types WHERE indoc_name = 'Отчет регистратора') and .ITEMS_EXISTS_BY_TYPE(t_sd_indocs.id,'SD_INDOC') .TYPE_INIT('SD_INDOC2DOC',9) .TYPE_ASSIGN(indoc_id,@indocs_id,9) .TYPE_ASSIGN(doc_id,@id2_1,9) .TYPE_INSERT('SD_INDOC2DOC','CRT',9) end -------конец генерации вход. документа---------- end end select @q_type=.STR_FOR_IF(@q_type) if @q_type='1' select @q_type_id=.TYPE_ID_FROM_CODE_FUNC('PIF_PAY_ALLOCATE') if @q_type='2' select @q_type_id=.TYPE_ID_FROM_CODE_FUNC('PIF_PAY_ALLOCATE') if @q_type='3' select @q_type_id=.TYPE_ID_FROM_CODE_FUNC('PIF_PAY_BUY') if @q_type='4' select @q_type_id=.TYPE_ID_FROM_CODE_FUNC('TP_PIF_PAYS_MOVES') if @q_type_id is null .EXIT_MESSAGE('Не указан тип заявки') if @q_sum='' select @q_sum=null if @qty='' select @qty=null if @agent_sum='' select @agent_sum=null if @sk_proc='' select @sk_proc=null if @pay_price='' select @pay_price=null if @round='' select @round=null if @pay_price_op='' select @pay_price_op=null if @as_of='' select @as_of=null select @q_sum_m=convert(money,@q_sum) --select @q_sum_m=convert(money,@b_sum) select @qty_m=convert(.PIF_PAY_TYPE,@qty) select @sk_proc_m=convert(money,@sk_proc) select @pay_price_m=convert(money,@pay_price) select @round_m=convert(money,@round) select @pay_price_op_m=convert(money,@pay_price_op) select @as_of_m=convert(money,@as_of) if @status=0 and (@q_sum_m is null or @qty_m is null) .EXIT_MESSAGE('Не указана сумма или кол-во паев') select @agent_sum_m=convert(money,@agent_sum) --.EXIT_MESSAGE_PARM('1',@q_date) if @q_date <> '' select @q_date_d=convert(datetime,@q_date) if @p_date <> '' select @p_date_d=convert(datetime,@p_date) if @q_date_d is null or @q_no is null .EXIT_MESSAGE('Не указана дата или номер заявки') if @q_type='1' and (@p_date_d is null or @p_no is null) and @status=0 .EXIT_MESSAGE('Не указана дата или номер платежа для заявки на выдачу') if @q_type='2' and (@p_date_d is null or @p_no is null) and @status=0 .EXIT_MESSAGE('Не указана дата или номер платежа для заявки на выдачу') if exists(select 1 from tp_pay2rep_imp where q_no=@q_no and q_date=@q_date_d and q_type_id=@q_type_id and ((@q_type='1' and p_no=@p_no and p_date=@p_date_d and @status=0) or (@q_type='1' and p_no=@p_no and p_date=@p_date_d and @status=0) /* or @q_type='3' or @q_type='4'*/ or @status=1)) .EXIT_MESSAGE('Заявка уже была импортирована') begin tran .TYPE_INIT('PIF_PAY2REP_IMP',3) .TYPE_ASSIGN(extract_id,@report_id,3) .TYPE_ASSIGN(agent,@agent,3) .TYPE_ASSIGN(payer,@payer,3) .TYPE_ASSIGN(acc_no,@acc_no,3) .TYPE_ASSIGN(q_type_id,@q_type_id,3) .TYPE_ASSIGN(q_date,@q_date_d,3) .TYPE_ASSIGN(q_no,@q_no,3) .TYPE_ASSIGN(p_date,@p_date_d,3) .TYPE_ASSIGN(p_no,@p_no,3) .TYPE_ASSIGN(q_sum,@q_sum_m,3) .TYPE_ASSIGN(q_qty,@qty_m,3) .TYPE_ASSIGN(agent_sum,@agent_sum_m,3) .TYPE_ASSIGN(sk_proc,@sk_proc_m,3) .TYPE_ASSIGN(pay_price,@pay_price_m,3) .TYPE_ASSIGN(round,@round_m,3) .TYPE_ASSIGN(pay_price_op,@pay_price_op_m,3) .TYPE_ASSIGN(as_of,@as_of_m,3) .TYPE_ASSIGN(a_fond,@a_fond,3) .TYPE_ASSIGN(a_acc_no,@a_acc_no,3) .TYPE_ASSIGN(a_payer,@a_payer,3) .TYPE_ASSIGN(reason,@reason,3) .TYPE_INSERT('PIF_PAY2REP_IMP','WAIT',3) if upper(.ITEM_STATE_CODE(@report_id))='ED' .ITEM_MOVE_STATE(@report_id,'WAIT_CHECK') commit tran end select sid = convert(varchar, @id) end