IF EXISTS (SELECT *	FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[qryMMSGetCustomerIdForOutsideSale]')
			AND type IN (N'P',N'PC'))
	DROP PROCEDURE qryMMSGetCustomerIdForOutsideSale
GO

CREATE PROCEDURE qryMMSGetCustomerIdForOutsideSale 
    @EntityID BIGINT
	,@TransactionSummaryId BIGINT
	,@CustomerId BIGINT OUTPUT
AS
DECLARE @TBCAmount FLOAT

SELECT @TBCAmount = ISNULL(SUM(Amount),0)
FROM tbTransactionPayment tp WITH (NOLOCK)
JOIN tbPaymentType pt WITH (NOLOCK) ON tp.PaymentTypeID = pt.PaymentTypeID
WHERE TransactionSummaryID = @TransactionSummaryId
	AND pt.Description = 'ToBeCollected'

IF @TBCAmount <> 0
BEGIN
	SET @customerId = 0
END
ELSE
BEGIN
SELECT @CustomerId = customerid
	FROM CustomerInformation ci WITH (NOLOCK)
	WHERE entityid = @EntityID
	    AND customername = 'Systematic Cust - Outside Sls'	
END
