
ALTER PROCEDURE [sCanteen].[pCanteenMasterAdd]
	 
        @rId int,
		@rNumber  int,
		@rCode nvarchar(20),
		@rTitle nvarchar(100),
		@rIsObsolete bit,
		@rOrgGroupId smallint,
		@rCreatedUserId bigint,
		@rUpdatedUserId bigint,
		@rOrderIndex int,
		@rDepositHeadId bigint,
		@rDepositExpenseTypeId bigint
		
		
		
	
WITH EXECUTE AS CALLER
AS
SET NOCOUNT ON
BEGIN
	BEGIN TRY
	
	IF(@rId = 0)	
	BEGIN  
		
		If Exists(select 1 from sCanteen.tCanteenMaster  where Code=@rCode AND OrgGroupId=@rOrgGroupId )
		Begin
			select -102 as Result
			RETURN
		End
		If Exists(select 1 from sCanteen.tCanteenMaster where Title=@rTitle AND OrgGroupId=@rOrgGroupId)
			BEGIN
			select -103 as Result
			RETURN
		END 
		--mantis Start - 0033603
		If Exists(select 1 from sCanteen.tCanteenMaster where Number=@rNumber AND OrgGroupId=@rOrgGroupId)
			BEGIN
			select -104 as Result
			RETURN
		END 
		If Exists(select 1 from sCanteen.tCanteenMaster where OrderIndex=@rOrderIndex AND OrgGroupId=@rOrgGroupId)
			BEGIN
			select -105 as Result
			RETURN
		END 
		--mantis End - 0033603
	END
	ELSE
	BEGIN
	--mantis Start - 0033603
		If Exists(select 1 from sCanteen.tCanteenMaster  where Code=@rCode AND OrgGroupId=@rOrgGroupId AND Id<>@rId)
		Begin
			select -102 as Result
			RETURN
		End
		--mantis End - 0033603
		If  Exists(select Id from sCanteen.tCanteenMaster where Title=@rTitle AND OrgGroupId=@rOrgGroupId AND Id<>@rId)
		Begin
			select -103 as Result
			RETURN
		End
		--mantis Start - 0033603
		If Exists(select 1 from sCanteen.tCanteenMaster where Number=@rNumber AND OrgGroupId=@rOrgGroupId AND Id<>@rId)
			BEGIN
			select -104 as Result
			RETURN
		END 
		If Exists(select 1 from sCanteen.tCanteenMaster where OrderIndex=@rOrderIndex AND OrgGroupId=@rOrgGroupId AND Id<>@rId)
			BEGIN
			select -105 as Result
			RETURN
		END 
		--mantis End - 0033603

	END
	
	 BEGIN TRANSACTION	
	 DECLARE @lNextNumber int

	IF(@rId = 0)	
	BEGIN
	EXECUTE sCommon.pTableRowIdGet @rOrgGroupId, 'sCanteen.tCanteenMaster', 0, 1, 1,@outNextNumber = @lNextNumber output
	
	INSERT INTO sCanteen.tCanteenMaster
		(
			Id,
			Title,
			Number,
			Code,
			IsObsolete,
			CreatedUserId,
			CreatedDateTime,
			UpdatedUserId,
			UpdatedDateTime,
			OrgGroupId ,
			OrderIndex,
			DepositHeadId,
			DepositExpenseTypeId
		)
	VALUES
		(
			@lNextNumber,
			@rTitle,
			@rNumber,
			@rCode,
			@rIsObsolete,
			@rCreatedUserId,
			sCommon.fGetDate(), 
			@rUpdatedUserId,
			sCommon.fGetDate(),
			@rOrgGroupId,
			@rOrderIndex,
			@rDepositHeadId,
			@rDepositExpenseTypeId
		)
		
		  INSERT INTO sSecurity.tUserAccess
           (
           SourceTypeId
           ,SourceId
           ,[UserId]
           
           )
		   SELECT 
		   2026
		   ,@lNextNumber
		   ,@rCreatedUserId
		   
		    SELECT @lNextNumber AS Result
		   
	END	 

ELSE
	 BEGIN
	 SET @lNextNumber = @rId
	    UPDATE  sCanteen.tCanteenMaster
	    SET
			Title=@rTitle,
			Number=@rNumber,
			Code=@rCode,
			IsObsolete=@rIsObsolete,
			UpdatedUserId=@rCreatedUserId,
			UpdatedDateTime=sCommon.fGetDate(),
			OrderIndex=@rOrderIndex,
			DepositHeadId=@rDepositHeadId,
			DepositExpenseTypeId=@rDepositExpenseTypeId
			
	      
	      WHERE Id = @rId
	      SELECT @lNextNumber AS Result
	 END  	

	COMMIT TRANSACTION		
	END TRY
	
	BEGIN CATCH 
		ROLLBACK TRANSACTION
		SELECT -1 AS Result
	END CATCH	
		
END
