SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

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

-- =============================================
-- Author:		Marat Zigangaraev
-- Create date: 2011/06/28
-- Description:	Обработка ENTRY_LOG
-- =============================================
CREATE PROCEDURE [dbo].[sp_ProcessEntryLog2]
	 @Action int -- 1-insert, 2-update, 3-delete
	,@EntryID uniqueidentifier
	,@PerformerMO uniqueidentifier = NULL
	,@PerformerResource int = NULL
	,@PerformerDivision uniqueidentifier = NULL
	,@PerformerSpeciality uniqueidentifier = NULL
	,@PerformerBranch uniqueidentifier = NULL
	,@EntryPlace int = NULL
	,@EntryKind int = NULL
	,@Channel int = NULL
	,@PayType int = NULL
	,@WorkDate datetime = NULL
	,@Year int = NULL
	,@Month int = NULL
	,@Week int = NULL
	,@Day int = NULL
	,@Kv int = NULL
	,@DirectorRemoteUser uniqueidentifier = NULL
	,@DirectorPost int = NULL
	,@DirectorDivision uniqueidentifier = NULL
	,@DirectorLpuMain uniqueidentifier = NULL
	,@DirectorSpeciality uniqueidentifier = NULL
	,@DirectorGroup uniqueidentifier = NULL
	,@UserID uniqueidentifier = NULL
As
Begin

	If @Action = 1
	 Begin
		INSERT INTO [AKUZ].[T_ENTRY_LOG2]([EntryLogID],[RuleID],[QuotaKey],[EntryID],[PerformerMO],[PerformerResource],[PerformerDivision],[PerformerSpeciality],[PerformerBranch]
			,[Year],[Month],[Week],[Day],[Kv],[DirectorRemoteUser],[DirectorPost],[DirectorDivision],[DirectorLpuMain],[DirectorSpeciality]
			,EntryPlace,[EntryKind],[Channel],[PayType],[WorkDate],[DirectorGroup],[UserID])
		VALUES(NEWID(),null,null,@EntryID,@PerformerMO,@PerformerResource,@PerformerDivision,@PerformerSpeciality,@PerformerBranch
			,@Year,@Month,@Week,@Day,@Kv,@DirectorRemoteUser,@DirectorPost,@DirectorDivision,@DirectorLpuMain,@DirectorSpeciality
			,@EntryPlace,@EntryKind,@Channel,@PayType,@WorkDate,@DirectorGroup,@UserID)
	 End
	Else If @Action = 2
	 Begin
	  if @DirectorRemoteUser is not null
	    begin
		 -- смена идентификатора
		Update [AKUZ].[T_ENTRY_LOG2] Set [EntryID] = @DirectorRemoteUser Where [EntryID] = @EntryID 
		end
	  else
	    begin
		 -- перезапись
		 Update [AKUZ].[T_ENTRY_LOG2] 
		 Set [WorkDate] = @WorkDate,
			[PerformerMO] = @PerformerMO,
			[PerformerResource] = @PerformerResource,
			[PerformerDivision] = @PerformerDivision,
			[PerformerSpeciality] = @PerformerSpeciality,
			[PerformerBranch] = @PerformerBranch,
			[EntryPlace] = @EntryPlace,
			[Year] = @Year,
			[Month] = @Month,
			[Week] = @Week,
			[Day] = @Day,
			[Kv] = @Kv
		 Where [EntryID] = @EntryID 
		end
	 End
	Else If @Action = 3
	 Begin
		Delete From [AKUZ].[T_ENTRY_LOG2] Where [EntryID] = @EntryID
	 End
End
GO