create procedure dbo.%PROC% @sid varchar(30) = null, @report_sid varchar(30) = null, @report_state int = 0 out as declare @action_id numeric, @id numeric, @user_id numeric, @state_id numeric, @new_state_id numeric, @type_id numeric begin tran select @action_id = %ACTION_ID% select @id = convert(numeric, @sid) .CHECK_USER .CHECK_UPDATE .GET_PROPS declare @old_state_code varchar(30) select @old_state_code = upper(state_code) from t_items, t_states where t_items.id=@id and t_items.state_id = t_states.id if @pay_perpose in (4014, 4020) and @old_state_code='PERF'/* Выплата цены выкупа пая */ begin if upper(.ITEM_STATE_CODE(@pif_pay_id)) not in ('CRT','CRT_NEXT') .EXIT_MESSAGE('Заявка должна быть исполнена') if (select count(*) from t_paydocs, td_depo_docs, t_items, t_types, t_states where t_paydocs.pif_pay_id = @pif_pay_id and t_paydocs.id = td_depo_docs.id and td_depo_docs.owner_id = @owner_id and t_paydocs.id = t_items.id and t_paydocs.complete_date > @complete_date and t_items.type_id = t_types.id and upper(t_types.type_code) in ('SD_PAYBANK_IN', 'SD_PAYBANK_OUT') and t_items.state_id = t_states.id and upper(t_states.state_code) in ('PERF')) >0 .EXIT_MESSAGE('По заявке есть платежи с датой больше текущей') if upper(.ITEM_STATE_CODE(@pif_pay_id)) = 'CRT' begin .ITEM_MOVE_STATE(@pif_pay_id,'WAIT_CRT') end else begin .ITEM_MOVE_STATE(@pif_pay_id,'CRT_RET') end update t_paydocs set covered_sum=null where id=@id end .UPDATE_STATE .ACTION_HISTORY .PAYS_COMPLETE_STATE(@id,0) .OPERS_ROLL(ALL) commit tran