CREATE PROCEDURE [dbo].[UpdateSanadStandard]  
AS
  
  Declare @Acc_Code Varchar(20)
  Declare @Bedeh Money
  Declare @Bestan Money
  Declare @Note nVarchar(130)
  Declare @Acc_Level Smallint
  Declare @Radif Smallint
  Declare @AutoId Numeric(9) 
  DECLARE @DocId BIGINT 


DECLARE CursoSort CURSOR FOR
SELECT Id_Sanad FROM dbo.SanadStandard
GROUP BY Id_Sanad
OPEN CursoSort
FETCH NEXT FROM CursoSort INTO @DocId
WHILE @@FETCH_STATUS = 0 
BEGIN 
----------------------------------------------------------------------------------------
DECLARE Update_Cursor Cursor LOCAL FOR
Select Autoid, acc_code, bedeh, bestan,  acc_level, note  From sanadStandard 
WHERE Id_Sanad = @DocId
order by acc_code, radif

OPEN Update_Cursor
  
FETCH NEXT FROM Update_Cursor INTO @AutoId,@Acc_Code,@Bedeh,@Bestan,@Acc_Level,@Note
Set @Radif = 1

  WHILE @@FETCH_STATUS = 0     
  BEGIN 
    if @Bedeh > 0 
    begin  
      if @Acc_Level = 3  
        Update SanadStandard Set Moein = @Bedeh , Bedeh = 0 Where Type = '0' And Acc_Code = @Acc_Code  And AutoId = @AutoId

      if @Acc_Level = 4  
        Update SanadStandard Set Tafzil = @Bedeh ,Bedeh = 0 Where Type = '0' And Acc_Code = @Acc_Code And AutoId = @AutoId
    end

    if @Bestan > 0 
    begin  
      if @Acc_Level = 3  
        Update SanadStandard Set Moein = @Bestan , Bestan = 0 Where Type = '1' And Acc_Code = @Acc_Code  And AutoId = @AutoId

      if @Acc_Level = 4  
       Update SanadStandard Set Tafzil = @Bestan , Bestan = 0 Where Type = '1' And Acc_Code = @Acc_Code And   AutoId = @AutoId
    end
     
     Update SanadStandard Set Radif = @Radif Where AutoId = @AutoId
      
     FETCH NEXT FROM  Update_Cursor INTO @AutoId,@Acc_Code,@bedeh,@bestan,@Acc_Level,@Note
     Set @Radif = @Radif + 1
 End

Close Update_Cursor
Deallocate Update_Cursor
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Declare Update_Cursor Cursor LOCAL FOR
SELECT  Acc_Code,AutoId,Acc_Level,Note From SanadStandard
 Where  Acc_Level > 2 AND Id_Sanad = @DocId     
 ORDER By Type,Acc_Code
 OPEN Update_Cursor
 FETCH NEXT FROM Update_Cursor INTO @Acc_Code,@Autoid,@Acc_Level,@Note
 WHILE @@FETCH_STATUS = 0     
 BEGIN 
    if  @Acc_Level = 3
    Update SanadStandard Set Acc_Code =   '        '+ SubString(@Acc_Code ,Len(Dbo.GetParent(@Acc_Code))+ 1,10)
      Where AutoId = @AutoId

    if  @Acc_Level = 4
    Update SanadStandard Set Acc_Code = '              ' +   SubString(@Acc_Code ,Len(Dbo.GetParent(@Acc_Code))+ 1,10)
      Where AutoId = @AutoId

      FETCH NEXT FROM  Update_Cursor INTO @Acc_Code,@AutoId,@Acc_Level,@Note
 End
Close Update_Cursor
Deallocate Update_Cursor
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
FETCH NEXT FROM CursoSort INTO @DocId
END 
Close CursoSort
Deallocate CursoSort