
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

begin tran

	DECLARE	@EntityId bigint =  3928
	DECLARE	@OwnerEntityId bigint =  2683
	DECLARE	@TransactionDate datetime =  '2020-01-15 14:44:50.000' 
	DECLARE	@BusinessDay datetime = '2020-01-15 00:00:00'  
	DECLARE	@PeriodStart datetime = '2020-01-13 00:00:00.000'
	DECLARE	@PeriodEnd datetime = '2020-01-19 00:00:00.000'  
	DECLARE	@FiscalYear varchar(10) =  '2015'
	DECLARE	@PeriodNo bigint =  3
	DECLARE	@ClerkId bigint =  1235521
	DECLARE	@ClerkName varchar(50) =  'Gutierrez, Cristobal'
	DECLARE	@Amount decimal = 290.00
	DECLARE @POSTransactionId bigint = -9223372032558572991

	DECLARE @DateAdded DATETIME = NULL
	DECLARE @Count FLOAT = 1
	DECLARE @ControlInfoId BIGINT = 150
	DECLARE @ControlInfo VARCHAR(150) = NULL
	DECLARE @ControlInfoSystemCode VARCHAR(50) = 'OPShift_Collected SaleAccountName' 

	DECLARE @OtherInfo1 VARCHAR(50) = 'OPShift_Collected SaleAccountName' 
	DECLARE @OtherInfo2 VARCHAR(50) = 'Evangel Christian Center'
	DECLARE @OtherInfo3 VARCHAR(50) = '1337373'
	DECLARE @RegisterNumber INT = 1
	DECLARE @CurrentShiftNumber BIGINT = 1

	-- For now, let's not let this run outside of JAT \ DEV \ Trainingx
	IF DB_NAME() NOT LIKE '%JAT%' AND DB_NAME() NOT LIKE '%CFADEV%' AND DB_NAME() NOT LIKE '%CFAQA%' AND DB_NAME() NOT LIKE '%Training%'
	BEGIN
		RAISERROR (N'Cannot run on this database.', 16, 1);
		RETURN;
	END
	
	DECLARE	@TransactionSummaryId bigint = (
		select SystemTransactionId 
		from tbTransactionControlInfo with (nolock) 
		where EntityId = @EntityId and BusinessDay = @BusinessDay and clerkName = @ClerkName 
		and ControlInfoAmount = @Amount and OtherInfo1 like 'OPShift_Collected SaleCheck')
		
	IF NOT(@TransactionSummaryId > 0) 
	BEGIN
		RAISERROR (N'Invalid TransactionSummaryID.', 16, 1);
		RETURN;
	END

	IF @DateAdded IS NULL SET @DateAdded = GETDATE()

	IF @ControlInfoId IS NULL
	BEGIN
		-- Deterimne the ID from the supplied name \ code
		IF @ControlInfoSystemCode IS NOT NULL
		BEGIN
			SELECT @ControlInfoId = ControlInfoID FROM tbControlINFO WITH(NOLOCK) WHERE ControlInfoSystemCode = @ControlInfoSystemCode
		END
		ELSE IF @ControlInfo IS NOT NULL
		BEGIN
			SELECT @ControlInfoId = ControlInfoID FROM tbControlINFO WITH(NOLOCK) WHERE ControlInfo = @ControlInfo
		END

		IF @ControlInfoId IS NULL
		BEGIN
			RAISERROR (N'Cannot determine the ContorlInfoID.', 16, 1);
		END
	END

	INSERT INTO tbTransactionControlInfo (
		EntityID, EntityTypeID, OwnerEntityID, SystemTransactionID, POSTransactionID, 
		TransactionDate, BusinessDay, StartDate, EndDate, FiscalYear, PeriodNo, 
		ClerkID, ClerkName, ControlInfoID, ControlInfoAmount, ControlInfoCount, DateAdded,
		OtherInfo1, OtherInfo2, OtherInfo3, RegisterNumber, CurrentShiftNumber, PolledDataID, CurrentDayNumber)
	SELECT
		@EntityId, 4, @OwnerEntityId, @TransactionSummaryId, @POSTransactionID, 
		@TransactionDate, @BusinessDay, @PeriodStart, @PeriodEnd, @FiscalYear, @PeriodNo, 
		@ClerkId, @ClerkName, @ControlInfoId, @Amount, @Count, @DateAdded, 
		@OtherInfo1, @OtherInfo2, @OtherInfo3, @RegisterNumber, @CurrentShiftNumber, 0, 0
	

	--commit
	--rollback
