CREATE PROCEDURE [dbo].[ChangeAccCode] @IdSanad Bigint, @OldAccCode Varchar(20), 
                 @NewAccCode Varchar(20), @NuHavaleh Bigint, @NuVajh Bigint,
                 @GuidSanad Varchar(90)
AS

Declare @Note        NVarchar(120)
Declare @Code        Varchar(5)
Declare @SendRecive  Nvarchar(35)
Declare @KindFlag    Char(1)
Declare @KindHeadc   Char(1)
Declare @TarikhCheqe SmallDateTime
Declare @NameBank    NVarchar(50)
Declare @ChBank      Smallint
Declare @ChNo        Nvarchar(15)
Declare @FormatDate  Varchar(10)
Declare @NameGuy     Nvarchar(90)
Declare @IdHavaleh   Bigint

---------------------------------------------------------------------------------------
Select @Code = Acc_link.Code  FROM classify INNER JOIN
               Acc_link ON classify.AutoId = Acc_link.Class INNER JOIN
               Account ON Acc_link.Acc_Code = Account.Acc_Code INNER JOIN
               Guy ON Acc_link.Code = Guy.Guy_Code
               WHERE (classify.AutoId IN (20, 21)) AND (Acc_link.Acc_Code = @NewAccCode)
IF (@Code Is Not Null And @Code <> '') 
Begin
  Select @NameGuy = Name From Guy Where Guy_Code = @Code
  IF (@NuHavaleh <> '' And @NuHavaleh <> 0) 
  Begin
    SELECT  @NuHavaleh = Sanad.Nu_Havaleh, @NuVajh = HeadC.Id_Cheqe
    FROM  Sanad INNER JOIN headH ON Sanad.Nu_Havaleh = headH.Id_Havaleh 
    LEFT OUTER JOIN HeadC ON headH.Id_Havaleh = HeadC.Id_Havaleh
    WHERE     (Sanad.Acc_Code = @OldAccCode) AND (Sanad.Nu_Havaleh = @NuHavaleh)
    GROUP BY Sanad.Nu_Vajeh, Sanad.Nu_Havaleh, HeadC.Id_Cheqe
  End
  Else IF (@Nuvajh <> '' And @Nuvajh <> 0)
  Begin
    Select @IdHavaleh = Id_Havaleh From HeadC Where Id_Cheqe = @NuVajh
    IF (@IdHavaleh  = 0 Or @IdHavaleh Is Null)
      Set @NuHavaleh = ''
    Else
      Set @NuHavaleh = @IdHavaleh
  End
  ----------------------------------------------------------------------------------------
  IF (@NuHavaleh  Is Not Null And @NuHavaleh <> '') 
  Begin
    Select @KindFlag = HeadH.Kind_Flag From HeadH Inner Join Sanad
                       On(HeadH.Id_Havaleh = Sanad.nu_Havaleh)                 
                       Where Sanad.Nu_Havaleh = @NuHavaleh
                       Group By HeadH.Kind_Flag 
    IF @KindFlag in('3', '6', '0', '8','2')
    Begin
      Update HeadH Set Trans_Name = Left(@NameGuy,35) Where Id_Havaleh = @NuHavaleh
      Update Havaleh Set Sender = @Code Where Id_Havaleh = @NuHavaleh
    End
    Else IF @KindFlag in('4', '5', '9', '7')
    Begin
      Update HeadH Set Recive_Name = Left(@NameGuy,35) Where Id_Havaleh = @NuHavaleh
      Update Havaleh Set Reciver = @Code Where Id_Havaleh = @NuHavaleh
    End
    Update Over_Under_Factor Set Guy_Code = @Code Where Id_Havaleh = @NuHavaleh
    Update Sanad Set Acc_Code = @NewAccCode Where Nu_Havaleh = @NuHavaleh 
    And Acc_Code = @OldAccCode
  End --IF (@NuHavaleh  Is Not Null Or @NuHavaleh <> '') 

  /******************************************************************************************/

  IF (@NuVajh Is Not Null And @NuVajh <> '') 
  Begin
    Select @KindHeadc   = Kind_flag From HeadC Where Id_Cheqe = @NuVajh
    IF @KindHeadC = '0'   
      Set @Note = 'دريافت از'
    Else
      Set @Note = 'پرداخت به'
   
    Update HeadC Set Note = @Note + ' ' + @NameGuy , 
    Guy_Code = @Code, AccCode = @NewAccCode, Guy_Bank = @Code
    Where Id_Cheqe = @NuVajh
  
    Declare Cursor_Update Cursor Local For
    Select Guid_Sanad From Sanad Where Nu_Vajeh = @NuVajh And Acc_Code = @OldAccCode
    Open Cursor_Update
    Fetch Next From Cursor_Update Into @GuidSanad
    While @@Fetch_Status = 0 
    Begin
      Select @ChNo = Cheqe.Ch_No From Sanad INNER JOIN
                     Acc_Cheqe_Link ON Sanad.Guid_Sanad = Acc_Cheqe_Link.GuId_Sanad 
                     INNER JOIN Cheqe ON Acc_Cheqe_Link.Guid_Cheqe = Cheqe.Guid_Cheqe
                     Where Sanad.Guid_Sanad = @GuidSanad

      IF (@ChNo Is Null or @ChNo = '')
      Begin
        Update Cheqe Set Ch_Note = Left(@Note + ' ' + @NameGuy,90)
        Where Id_Cheqe = @NuVajh And Ch_No Is Null
    
        Update Sanad Set Sanad.Note = Cheqe.Ch_Note, Sanad.Acc_code = @NewAccCode 
        From Sanad INNER JOIN Acc_Cheqe_Link ON Sanad.Guid_Sanad = Acc_Cheqe_Link.GuId_Sanad 
        INNER JOIN Cheqe ON Acc_Cheqe_Link.Guid_Cheqe = Cheqe.Guid_Cheqe
        Where Sanad.Guid_Sanad = @GuidSanad
      End --IF (@ChNo Is Null And @ChNo = '')  
    
      Else IF (@ChNo Is Not Null And @ChNo <> '')
      Begin
        Select @TarikhCheqe = Ch_Tarikh,  @ChBank = Ch_Bank From Cheqe Where Ch_No = @ChNo And Id_Cheqe = @NuVajh
        Select @NameBank = Name From Bank Where AutoId = @ChBank
        Select @FormatDate  = dbo.samsi_Date(@TarikhCheqe)
        Set @FormatDate = '13' + Substring(@FormatDate,1,2) + '/' + Substring(@FormatDate,3,2) + '/' + Substring(@FormatDate,5,2)
    
        Update Cheqe Set Ch_Note = Left('چ.ش' + ' ' + @ChNo + 'سررسيد ' + ' ' +
        @FormatDate + ' ' + @NameBank + ' ' + @Note + ' ' + @NameGuy,90) 
        Where Id_Cheqe = @NuVajh And Ch_No = @ChNo
    
        Update Sanad Set Sanad.Note = Cheqe.Ch_Note, Sanad.Acc_code = @NewAccCode 
        From Sanad INNER JOIN Acc_Cheqe_Link ON Sanad.Guid_Sanad = Acc_Cheqe_Link.GuId_Sanad 
        INNER JOIN Cheqe ON Acc_Cheqe_Link.Guid_Cheqe = Cheqe.Guid_Cheqe
        Where Sanad.Guid_Sanad = @GuidSanad
      End --Else IF (@ChNo Is Not Null And @ChNo <> '')
    
      Fetch Next From Cursor_Update Into @GuidSanad
    
    End --While @@Fetch_Status = 0 
    Close Cursor_Update
    Deallocate Cursor_Update

    Update Sanad Set Sanad.Note = Cheqe.Ch_Note From Sanad 
    INNER JOIN Acc_Cheqe_Link ON Sanad.Guid_Sanad = Acc_Cheqe_Link.GuId_Sanad 
    INNER JOIN Cheqe ON Acc_Cheqe_Link.Guid_Cheqe = Cheqe.Guid_Cheqe
    Where Sanad.Acc_Code <> @NewAccCode And Nu_Vajeh = @NuVajh
  End --IF (@NuVajh Is Not Null And @NuVajh <> '') 
  IF (@NuHavaleh Is Null Or @NuHavaleh  = '' Or @NuHavaleh = '0') And 
     (@NuVajh Is Null Or @NuVajh  = '' Or @NuVajh = '0')      
    Update Sanad Set Acc_code = @NewAccCode Where Guid_Sanad = @GuidSanad

End --IF (@Code Is Not Null And @Code <> '') 
Else
  Update Sanad Set Acc_code = @NewAccCode Where Guid_Sanad = @GuidSanad
