
 IF NOT EXISTS(select * from tbFinancialGroup where [Description] = 'External Credit (On Demand)')
 BEGIN
  DECLARE @Financial int 
  DECLARE @Banking int
  INSERT INTO tbFinancialGroup(Description) VALUES('External Credit (On Demand)')
  SET @Financial = SCOPE_IDENTITY()
  INSERT INTO tbFinancialGroup(Description) VALUES('External Credit (On Demand)')
  SET @Banking = SCOPE_IDENTITY()
  
  INSERT INTO tbFinancialTypeMember(FinancialTypeID,FinancialGroupID,OperationEnum,SortOrder,Active,Status,FunctionEnum)
  VALUES(1,@Financial,0,215,1,2,0)
  
  INSERT INTO tbFinancialTypeMember(FinancialTypeID,FinancialGroupID,OperationEnum,SortOrder,Active,Status,FunctionEnum)
  VALUES(2,@Banking,0,225,1,2,0)
  
  DECLARE @ControlInfoID bigint
  select @ControlInfoID = ControlInfoID from tbControlInfo where ControlInfo = 'OPShift Payment OnDemand'
  
  INSERT INTO tbFinancialGroupMember(FinancialGroupID,CalculationOptionEnum,ControlInfoID,CashDepositFinancialTypeID)
  VALUES(@Financial,0,@ControlInfoID,NULL)
  
  INSERT INTO tbFinancialGroupMember(FinancialGroupID,CalculationOptionEnum,ControlInfoID,CashDepositFinancialTypeID)
  VALUES(@Banking,0,NULL,@Financial)
  
 END

declare
   @EntityIdString VARCHAR(max)= '3928'
DECLARE @EntityId bigint
DECLARE cur CURSOR FOR SELECT value from dbo.fn_Split(@EntityIdString,',')
OPEN cur

FETCH NEXT FROM cur INTO @EntityId

WHILE @@FETCH_STATUS = 0

BEGIN

if exists (select * from tbEntity with (nolock) where EntityID = @EntityId)

BEGIN

declare @transactioncashdepositId1 as bigint
declare @transactioncashdepositId2 as bigint

select top 1 @transactioncashdepositId1 = TransactionCashDepositID from tbTransactionCashDeposit with (nolock)
 where EntityID =@EntityId and CashDepositDate = '2020-1-15' and ClerkName = 'Shapiro, Yehoshua' and RegisterSales <> 0


declare @BankingGroupId as int
declare @CashupGroupId as int

select @BankingGroupId = fg.FinancialGroupID from tbFinancialTypeMember ftm with (nolock)
join tbFinancialGroup fg with (nolock) on ftm.FinancialGroupID = fg.FinancialGroupID
 where ftm.FinancialTypeID = 2 and Description = 'External Credit (On Demand)'
 
 select @CashupGroupId = fg.FinancialGroupID from tbFinancialTypeMember ftm with (nolock)
join tbFinancialGroup fg with (nolock) on ftm.FinancialGroupID = fg.FinancialGroupID
 where ftm.FinancialTypeID = 1 and Description = 'External Credit (On Demand)'
 
 DECLARE @CreditCard int
 SELECT @CreditCard = FinancialGroupID  from tbFinancialGroup WHERE Description = 'Credit Card'
 
 UPDATE tbTransactionCashDepositFinancial SET Amount = Amount - 20, AmountPolled = Amount - 20 
 WHERE TransactionCashDepositID = @transactioncashdepositId1 and FinancialGroupID = @CreditCard

insert into tbTransactionCashDepositFinancial(TransactionCashDepositID, FinancialGroupID, Amount, Quantity, AmountPolled, QuantityPolled)
values(@transactioncashdepositId1, @CashupGroupId, 20.00, 1, 20.00,1)

insert into tbTransactionCashDepositFinancial(TransactionCashDepositID, FinancialGroupID, Amount, Quantity, AmountPolled, QuantityPolled)
values(@transactioncashdepositId1, @BankingGroupId, 20.00, 1, 20.00,1)

END

FETCH NEXT FROM cur INTO @EntityId
END


CLOSE cur
DEALLOCATE cur