USP_UPDATE1099DISTRIBUTION

Updates the 1099 Distribution for a Financial Transaction

Parameters

Parameter Parameter Type Mode Description
@ID uniqueidentifier IN
@NEWAMOUNT money IN

Definition

Copy


create procedure dbo.USP_UPDATE1099DISTRIBUTION (
  @ID uniqueidentifier
  ,@NEWAMOUNT money
)
as
begin

declare @TotalAmount as money;
declare @RunningAmount as money;
declare @RunningPercent as decimal(28,23);
declare @TotalPercent as decimal(28,23);
declare @Amount as money;
declare @Percent as decimal(28,23);
declare @RowID as uniqueidentifier;

set @TotalAmount = @NEWAMOUNT;
select 
  @TotalPercent = sum([PERCENT]) 
  from dbo.FINANCIALTRANSACTION1099DISTRIBUTION
  where FINANCIALTRANSACTIONID = @ID;

set @TotalAmount = @TotalAmount * @TotalPercent / 100;
set @RunningAmount = @TotalAmount;
set @RunningPercent = @TotalPercent;

declare DISTRIBUTION cursor forward_only read_only for select [PERCENT], ID
      from dbo.FINANCIALTRANSACTION1099DISTRIBUTION
      where FINANCIALTRANSACTIONID = @ID
      order by SEQUENCE asc;


open DISTRIBUTION;
fetch next from DISTRIBUTION into @Percent, @RowID;

while @@fetch_status = 0
begin
      set @Amount = ROUND(@RunningAmount * ( @Percent / @RunningPercent ),2); 
      set @RunningPercent = @RunningPercent - @Percent;
      set @RunningAmount = @RunningAmount - @Amount;
      update dbo.FINANCIALTRANSACTION1099DISTRIBUTION 
      set AMOUNT = @Amount 
      where ID = @RowID;
      fetch next from DISTRIBUTION into @Percent, @RowID;
end;
close DISTRIBUTION;
deallocate DISTRIBUTION;

end