SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_ProcessEntryLog]') and xtype = N'P')
drop procedure [dbo].[sp_ProcessEntryLog]
GO

-- =============================================
-- Author:		Marat Zigangaraev
-- Create date: 2011/06/28
-- Description:	Обработка ENTRY_LOG
-- =============================================
CREATE PROCEDURE [dbo].[sp_ProcessEntryLog]
	@Action int -- 1-insert, 2-update, 3-delete
	,@EntryID uniqueidentifier
	,@OwnerID uniqueidentifier = NULL
	,@ResourceID int = NULL
	,@WorkDate datetime = NULL
	,@EntryKind int = NULL
	,@channel int = NULL
	,@PayType int = NULL
As
Begin

	If @Action = 1
	 Begin
		INSERT INTO [AKUZ].[T_ENTRY_LOG]([EntryLogID],[WorkDate],[OwnerID],[Resource],[EntryKind],[Channel],[PayType])
		Values (@EntryID, @WorkDate, @OwnerID, @ResourceID, @EntryKind, @channel, @PayType)
	 End
	Else If @Action = 2
	 Begin
	  if @OwnerID is not null
	    begin
		 -- смена идентификатора
		 Update [AKUZ].[T_ENTRY_LOG] Set [EntryLogID] = @OwnerID Where [EntryLogID] = @EntryID 
		end
	  else
	    begin
		 -- перезапись
		 Update [AKUZ].[T_ENTRY_LOG] 
		 Set [WorkDate] = @WorkDate,
			[Resource] = @ResourceID
		 Where [EntryLogID] = @EntryID 
		end
	 End
	Else If @Action = 3
	 Begin
		Delete From [AKUZ].[T_ENTRY_LOG] Where EntryLogID = @EntryID
	 End
End
GO