
BEGIN
	DECLARE @EntityId BIGINT = 123
	DECLARE @BusinessDay DATETIME = '2020-02-03'
	DECLARE @PeriodStart DATETIME
		,@PeriodEnd DATETIME
		,@FiscalYear VARCHAR(12)
		,@MonthName VARCHAR(12)
		,@PeriodNo INT
		,@OwnerEntityId BIGINT
		,@PollPOSID INT
		,@Amount MONEY = 10.00
		,@ClerkId INT = 123
		,@ClerkName VARCHAR(50) = 'LoadTest Cashier'
		,@POSTransactionId BIGINT  
		,@TransactionDate DATETIME = DATEADD(day, DATEDIFF(day, 0, @BusinessDay), '03:30:00')
		,@TransactionSummaryId BIGINT
		,@TransactionControlInfoId BIGINT
		,@ControlInfoId BIGINT
		,@POSTransactionId2 BIGINT 
		
		SELECT @POSTransactionId = convert(numeric(8,0),rand() * 89999999) + 10000000
		SELECT @POSTransactionId2 = convert(numeric(8,0),rand() * 89999999) + 10000000

	SELECT @FiscalYear = sFiscalYear
		,@MonthName = [MonthName]
		,@PeriodStart = iPeriodBegin_dt
		,@PeriodEnd = iPeriodEnd_dt
		,@PeriodNo = PeriodNo
	FROM tbPeriodDetail pd WITH (NOLOCK)
	WHERE @BusinessDay BETWEEN iPeriodBegin_dt
			AND iPeriodEnd_dt

	SELECT TOP 1 @PollPOSID = PollPOSID
	FROM tbPollPOS WITH (NOLOCK)
	WHERE Title = 'RADIANT'

	

		IF NOT EXISTS (
				SELECT *
				FROM tbTransactionSummary WITH (NOLOCK)
				WHERE EntityID = @EntityId
					AND BusinessDay = @BusinessDay
					AND POSTransactionID IN (
						@POSTransactionId
						,@POSTransactionId2
						)
				)
		BEGIN
			DECLARE @CustomerID BIGINT = 63911614
			DECLARE @CustomerAccountName VARCHAR(50) = 'Wire Shark, LLC.'

			SELECT @CustomerID = CustomerID
			FROM CustomerInformation
			WHERE EntityID = @EntityId
				AND CustomerName = @CustomerAccountName

		

			-- Insert into tbTransactionsummary and tbTransactionPayment
			SELECT @OwnerEntityId = OwnerEntityID
			FROM tbEntity WITH (NOLOCK)
			WHERE EntityID = @EntityId

			INSERT INTO tbTransactionSummary (
				EntityID
				,EntityTypeID
				,OwnerEntityID
				,PollPOSID
				,POSTransactionID
				,TransactionDate
				,BusinessDay
				,StartDate
				,EndDate
				,FiscalYear
				,[MonthName]
				,PeriodNo
				,ClerkID
				,ClerkIDName
				,InOutDel
				,Sales_Incl_GST_INCL
				,Sales_Incl_GST_EXCL
				,Sales_Excl_GST_INCL
				,Sales_Excl_GST_EXCL
				,SalesItemCount
				,GSTAmount
				,Discount
				,Compliment
				,Free
				,Waste
				,Rounding
				,NonSales
				,CountAsTransaction
				,RegisterNumber
				,CurrentShiftNumber
				,DateAdded
				,TransactionVersion
				,RewardsSchemeID
				,ProcessComplete
				,NoOfCovers
				,CurrentDayNumber
				,CustomerInformationID
				)
			VALUES (
				@EntityId
				,4
				,@OwnerEntityId
				,@PollPOSID
				,@POSTransactionId
				,@TransactionDate
				,@BusinessDay
				,@PeriodStart
				,@PeriodEnd
				,@FiscalYear
				,@MonthName
				,@PeriodNo
				,@ClerkId
				,@ClerkName
				,5
				,@Amount
				,@Amount
				,@Amount
				,@Amount
				,1
				,0
				,0
				,0
				,0
				,0
				,0
				,0
				,1
				,1
				,1
				,GETDATE()
				,0
				,0
				,1
				,0
				,0
				,@CustomerID
				)

			SELECT @TransactionSummaryId = SCOPE_IDENTITY()

			DECLARE @PaymentTypeID INT

			SELECT @PaymentTypeID = PaymentTypeId
			FROM tbPaymentType WITH (NOLOCK)
			WHERE Code = 'ToBeCollected'

			-- Insert Payment
			INSERT INTO tbTransactionPayment (
				Amount
				,PaymentTypeID
				,Quantity
				,TransactionSummaryID
				,SequenceNo
				)
			VALUES (
				@Amount
				,@PaymentTypeId
				,1
				,@TransactionSummaryId
				,1
				)

			-- Insert Transaction Control Info for Sale
			SELECT @ControlInfoId = ControlInfoID
			FROM tbControlINFO WITH (NOLOCK)
			WHERE ControlInfoSystemCode = 'Checksum'

			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
				,1
				,GETDATE()
				,''
				,''
				,''
				,1
				,1
				,0
				,0

			-- Insert Transaction Control Info for Payment
			SELECT @ControlInfoId = ControlInfoID
			FROM tbControlINFO WITH (NOLOCK)
			WHERE ControlInfoSystemCode = 'Undefined Payments'

			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
				,1
				,GETDATE()
				,''
				,''
				,''
				,1
				,1
				,0
				,0

			-- Insert onAccountSale record
			INSERT INTO tbOnAccountSale (
				TransactionSummaryID
				,EntityId
				,CustomerId
				,InvoiceNumber
				,Paid
				,[Type]
				,UncollectedFlag
				,PaidInFullTimestamp
				,ReversalFlag
				)
			SELECT DISTINCT ts.TransactionSummaryID
				,ts.EntityID
				,@CustomerID
				,'Load test TBC'
				,0
				,1
				,0
				,NULL
				,0
			FROM tbTransactionSummary ts WITH (NOLOCK)
			WHERE ts.TransactionSummaryID = @TransactionSummaryId
END


END
go 1000