CREATE PROCEDURE [dbo].[Create_Taraz_Over]  @AzTarikh Datetime, @TaTarikh Datetime, @AzCode Varchar(20), @TaCode Varchar(20), @ComputerName Varchar (20), @FlagSelect Char, @NuAftatah Float
AS

   Declare @P_Code Varchar (20)
   Declare @Acc_Code Varchar (20)
   Declare @Acc_Name NVarchar (60)
   Declare @ParentCode int
   Declare @Bedeh   Money
   Declare @Bestan   Money

   /*###########################################################################*/
   IF @FlagSelect = '1' 
   Begin
      Declare TarazOver Cursor Local For
      Select  Account.Parent_Code 
      from    HeadS inner JOIN Sanad on HeadS.ID_Sanad = Sanad.ID_Sanad inner join Account  on 
                       Sanad.Acc_Code = Account.Acc_Code
      where  (Convert(Varchar(10), HeadS.Tarikh_User, 111) >=  @AzTarikh)  
      AND    (Convert(Varchar(10), HeadS.Tarikh_User, 111) <= @TaTarikh)  
      And  (Sanad.Acc_Code >=  @AzCode)  AND (Sanad.Acc_Code <= @TaCode)
      Group by Account.Parent_Code
   End
   Else IF @FlagSelect = '2'
   Begin
      Declare TarazOver Cursor Local For
     Select  Account.Parent_Code
     From   HeadS inner JOIN Sanad on HeadS.ID_Sanad = Sanad.ID_Sanad inner join Account  on 
                       Sanad.Acc_Code = Account.Acc_Code
      where  (Convert(Varchar(10), HeadS.Tarikh_User, 111) >=  @AzTarikh)  
      AND    (Convert(Varchar(10), HeadS.Tarikh_User, 111) <= @TaTarikh)  
      Group by Account.Parent_Code
   End
   Else IF @FlagSelect = '3'
   Begin
      Declare TarazOver Cursor Local For
      Select  Account.Parent_Code 
      from    HeadS inner JOIN Sanad on HeadS.ID_Sanad = Sanad.ID_Sanad inner join Account  on 
                       Sanad.Acc_Code = Account.Acc_Code
      where  (Convert(Varchar(10), HeadS.Tarikh_User, 111) >=  @AzTarikh)  
      AND    (Convert(Varchar(10), HeadS.Tarikh_User, 111) <= @TaTarikh)  
      And  (Sanad.Acc_Code >=  @AzCode)  AND (Sanad.Acc_Code <= @TaCode)
      --And (HeadS.Nu_Manual <> @NuAftatah)
      Group by Account.Parent_Code
   End
   Else IF @FlagSelect = '4'
   Begin
       Declare TarazOver Cursor Local For
       Select  Account.Parent_Code
       From   HeadS inner JOIN Sanad on HeadS.ID_Sanad = Sanad.ID_Sanad inner join Account  on 
                       Sanad.Acc_Code = Account.Acc_Code
       where  (Convert(Varchar(10), HeadS.Tarikh_User, 111) >=  @AzTarikh)  
       AND    (Convert(Varchar(10), HeadS.Tarikh_User, 111) <= @TaTarikh)  
       --And (HeadS.Nu_Manual <> @NuAftatah)
       Group by Account.Parent_Code
   End
   /*###########################################################################*/
   Open  TarazOver
   Fetch Next From  TarazOver into @ParentCode
   
    While @@FETCH_STATUS = 0     
    Begin
       Select @Acc_Name = Acc_Name ,  @Acc_Code = Acc_Code  From Account Where AutoId = @ParentCode
       Set @P_Code = @Acc_Code
       
       While  @P_Code is Not Null   
       Begin
           Select @Bedeh =  Sum(Bedeh) From Taraz Where Parent_Code = @ParentCode  And ComputerName = @ComputerName
            Select @Bestan = Sum(Bestan) From Taraz Where Parent_Code = @ParentCode  And ComputerName = @ComputerName
            Select @Acc_Name = Acc_Name, @ParentCode = Parent_Code  From Account Where Acc_Code = @P_Code    
           IF (Select Count (Acc_Code) From dbo.Taraz   Where Acc_Code = @P_Code and ComputerName = @ComputerName) = 0 
           Begin
               Insert Into  dbo.Taraz (Acc_Name   , Acc_Code ,Bedeh   ,Bestan, ComputerName, Parent_Code)
                                      Values (@Acc_Name, @P_Code ,@Bedeh,@Bestan, @ComputerName, @ParentCode)
           End
           Else
           Begin
              Update dbo.Taraz set Bedeh =  @Bedeh , Bestan =  @Bestan
                        where Acc_Code = @P_Code And ComputerName  = @ComputerName
           End

           Select @P_Code = Dbo.GetParent (@P_Code)    
           Select @Acc_Name = Acc_Name  From Account Where Acc_Code = @P_Code    
 
      End
   /*###########################################################################*/
       Fetch Next From  TarazOver into @ParentCode
     End

    Close TarazOver
    Deallocate TarazOver
