UFN_DISBURSEMENTPROCESS_RANGESGRID_DONOTALLOWSTARTGREATERTHANEND

Do not allow the start range to be greater than the end range.

Return

Return Type
nvarchar(10)

Parameters

Parameter Parameter Type Mode Description
@DISBURSEMENTPROCESSID uniqueidentifier IN
@RANGESGRID xml IN

Definition

Copy


create function dbo.UFN_DISBURSEMENTPROCESS_RANGESGRID_DONOTALLOWSTARTGREATERTHANEND(@DISBURSEMENTPROCESSID uniqueidentifier, @RANGESGRID xml)
returns nvarchar(10)
with execute as caller
as begin
    declare @ReturnValue bit;
    set @ReturnValue=0;
    declare @TempTable table
    (
        [START] int
    ,[END] int
    ,[COUNT] int
    );
  declare @TOTALDISBURSEMENTS int

  select    
    @TOTALDISBURSEMENTS=COUNT(*)
  from
    dbo.DISBURSEMENTPROCESSDISBURSEMENT as DPD
    inner join dbo.FINANCIALTRANSACTION as FTD on DPD.ID = FTD.ID
  where
    DPD.DISBURSEMENTPROCESSID = @DISBURSEMENTPROCESSID
  group by
    DPD.DISBURSEMENTPROCESSID;


    insert into @TempTable
        select 
            [RANGESTART]
      ,[RANGEEND]
      ,case when [RANGEEND]<>0
         then
           [RANGEEND]-[RANGESTART]+1 
         else
           0
         end
       as [RANGECOUNT]      
        from dbo.UFN_DISBURSEMENTPROCESSFORMNUMBERRANGE_FROMITEMLISTXML(@RANGESGRID);

  update @TempTable
  set [COUNT] = @TOTALDISBURSEMENTS - (select SUM([COUNT]) from @TempTable)
  where [COUNT] = 0;

    if exists
    (
        select * from @TempTable where [START] > case when [END]<>0 then [END] else [START]+[COUNT]-1 end
    )    
    set @ReturnValue = 1;

    return @ReturnValue;
end