SELECT BankingSettingsID, EntityID, * FROM tbEntityBankingSettings
SELECT SelectTransactionsOnLoad, SettingID, * FROM tbBankingSettings

declare	@EntityID as bigint = 271
declare	@BusinessDay as datetime = '2023-01-27'

DECLARE @Completed as bit
DECLARE @SelectTransactionsOnLoad AS bit
DECLARE @MoneyCollected as decimal(16,4)
DECLARE @RebankMoneyCollected as decimal(16,4)	
DECLARE @BankingID bigint
DECLARE @TotalDeposit  as decimal(16,4)

SELECT Top 1 @SelectTransactionsOnLoad = SelectTransactionsOnLoad   
                                FROM vwBankingSettings with(nolock)
                                WHERE (EntityID = @EntityID) OR (EntityID = 0)   
                                ORDER BY EntityID DESC
								

SELECT @Completed=Completed, @BankingID = BankingId FROM tbBanking with(nolock) WHERE EntityId = @EntityID and CreateDate = @BusinessDay

SELECT @MoneyCollected = case when @SelectTransactionsOnLoad = 'true' then 
			sum(isnull(ExpectedDeposit, 0))
	   else
			sum(isnull(ActualDeposit, 0))
	   end
FROM tbBankingTransaction with(nolock)
WHERE (BankingID = @BankingID OR (BankingID = 0 AND @Completed=0))
AND EntityID = @EntityID
AND BusinessDay = @BusinessDay	

SELECT @RebankMoneyCollected = case when @SelectTransactionsOnLoad = 'true' then 
			sum(isnull(ExpectedDeposit, 0))
	   else
			sum(isnull(ActualDeposit, 0))
	   end
FROM tbRebankTransaction with(nolock)
WHERE (BankingID = @BankingID OR (BankingID = 0 AND @Completed=0))
AND EntityID = @EntityID
AND BusinessDay = @BusinessDay	

SELECT    
     @TotalDeposit =  isnull(SUM(isnull(AmountDeposit, 0)), 0) 
  
                         FROM tbBankingdeposit  with(nolock)  
                         WHERE BankingID = @BankingID
                         AND ExcludeFromBanking != 1
SELECT @MoneyCollected as TotalCashierDeposit,
     @RebankMoneyCollected as ChangeFundDeposit, 
	 (@MoneyCollected + @RebankMoneyCollected - @TotalDeposit) as MoniesToBeDeposited, 
     @TotalDeposit as TotalDeposit,
	AutoBillAmount as BillsAcceptedBySafe,
     ManualCashAmount as ManualCashDrop,
     ManualCheckAmount as ManualCheckDrop,
     AchAmount as ThirdPartyACH,
     AutoBillAmount +  ManualCashAmount +  ManualCheckAmount + AchAmount as [Total Bills]
FROM tbBankingdeposit  with(nolock)  
 WHERE BankingID = @BankingID
               AND ExcludeFromBanking != 1
