
Create PROCEDURE [dbo].[Create_Sanad_Ghest] @msg_num BigInt  OUTPUT, @Act  Smallint ,  @Guy_Code  VarChar(20)  ,@Price Money,@Id BigInt,@PNote NvarChar(100),@Nu_Havaleh Bigint, @Nu_Ghest Bigint, @Nu_Sanad  Bigint, @AutoGhest Numeric(18,0),@UserId smallint
AS

  Declare  @Acc_Code           VarChar(20)
  Declare  @Acc_Code_Equal     VarChar(20)
  Declare  @Parent_Code        VarChar(20)
  Declare  @Parent_Code_Equal  VarChar(20)
  Declare  @Type               Bit  
  Declare  @ActNote            NvarChar(100)
  Declare  @Auto               Char  
  Declare  @Fiexd              Char  
  DECLARE  @Mobile             CHAR(11)
  DECLARE  @ServiceId          TINYINT
  DECLARE Cursor_Ghest CURSOR FOR
  SELECT Type , Note_In_Sanad , Parent_Code,Parent_Code_Equal FROM dbo.Act Where (Act_Code=@Act )
  OPEN Cursor_Ghest
    
  FETCH NEXT FROM Cursor_Ghest INTO  @Type,@ActNote , @Parent_Code,@Parent_Code_Equal

  Set @msg_Num= 0

  IF @@FETCH_STATUS = 0  -- يافتن و يا ايجاد شماره سند
  Begin 
    IF (@Nu_Sanad Is Null) Or (@Nu_Sanad = '')
      SELECT @Nu_Sanad = Id_Sanad,@Auto = Auto,@Fiexd=Fiexd FROM dbo.Heads
      Where (DateDiff(day,Tarikh_User,getdate() ) =0 ) And (Auto=1)
  
    IF (@Nu_Sanad Is not Null) And (@Auto = '0') and (@Nu_Sanad  > 0) 
    Begin
      --Update Dbo.Heads Set Auto='1'   Where Nu_Manual=dbo.samsi_Date(GetDate())  
      Update Dbo.Heads Set Auto='1'   Where Id_Sanad  = @Nu_Sanad
      IF @@Error > 0  
      Begin
        Set  @msg_num = -5  
        CLOSE Cursor_Ghest
        DEALLOCATE Cursor_Ghest
        RETURN   @msg_num 
      End
    End 
  	
    IF (@Nu_Sanad Is Null) Or (@Nu_Sanad=0)  -- اگر قبلا با اين شماره در سندها وجود نداشته باشد
    Begin 
      Insert Into Dbo.Heads(Auto,Fiexd,Nu_Manual,User_Id) Values(1,0,dbo.samsi_Date(GetDate()),@UserId )
      Select @Nu_Sanad=@@Identity --   به دست آوردن شماره آخرين سند 
      IF @@Error > 0  
      Begin
        Set  @msg_num = -5  
        CLOSE Cursor_Ghest
        DEALLOCATE Cursor_Ghest
        RETURN   @Msg_Num 
      End
    End 
    Select @msg_Num = @Nu_Sanad
  End --  If @@FETCH_STATUS = 0  -- يافتن و يا ايجاد شماره سند

  WHILE @@FETCH_STATUS = 0
  BEGIN 
    Set @Acc_Code       = '0'
    Set @Acc_Code_Equal = '0' 
  
    IF (Select Has_Child from Dbo.Account where Acc_Code=@Parent_Code)='0' 
      Set @Acc_Code=@Parent_Code -- اگر مستقيما کد حسابداري وجود داشت
    Else
      IF (Select Has_Child from Dbo.Account where Acc_Code = @Parent_Code + @Guy_Code)='0' 
         Set  @Acc_Code = @Parent_Code + @Guy_Code -- از روي كد والد و  كد شخص كد حسابداري را بدست مي اورد
      Else
      Begin
        Set  @msg_num = -6  --msg-num = -6 -- كد حسابداري وجود ندارد
        CLOSE Cursor_Ghest
        DEALLOCATE Cursor_Ghest
        RETURN   @msg_num 
      End    
                      
      IF @Type = 0   
      Begin
        Insert Into Dbo.Sanad (Id_Sanad  , Acc_Code  , Note ,                      Bedeh  , Auto_Create  , Edit_Flag ,Type ,Nu_Havaleh ,Nu_Ghest, Auto_ghest,User_ID)  
                     Values   (@Nu_Sanad , @Acc_Code , @ActNote + ' ' + @PNote  ,  @Price ,    1         ,    1      ,@Type,@Nu_Havaleh , @Nu_Ghest, @AutoGhest,@UserId)
        IF @@Error > 0  
        Begin
          Set  @msg_num = -5  
          CLOSE Cursor_Ghest
          DEALLOCATE Cursor_Ghest
          RETURN   @msg_num 
        End
        Select @msg_Num=@@Identity --   به دست آورن اخرين شماره خوکار رديف سند
      End  -- If @Type = 0   
      Else 
      Begin
        Insert Into Dbo.Sanad (Id_Sanad  , Acc_Code  , Note                   , Bestan , Auto_Create  , Edit_Flag , Type ,Nu_Havaleh, Nu_Ghest,Auto_ghest,User_ID )  
                       Values (@Nu_Sanad , @Acc_Code , @ActNote + ' ' +@PNote , @Price ,       1      ,        1  , @Type,@Nu_Havaleh ,@Nu_Ghest,@AutoGhest,@UserId)
        IF @@Error > 0  
        Begin
          Set  @msg_num = -5  
          CLOSE Cursor_Ghest
          DEALLOCATE Cursor_Ghest
          RETURN   @msg_num 
        End
        Select @msg_Num=@@Identity --   به دست آورن اخرين شماره خوکار رديف سند
      End
    FETCH NEXT FROM Cursor_Ghest INTO  @Type,@ActNote , @Parent_Code,@Parent_Code_Equal 
  END -- WHILE @@FETCH_STATUS = 0
  IF @Act=41
  begin
    SELECT @Mobile = Mobile FROM dbo.Guy WHERE Guy_Code = @Guy_Code
    IF @Mobile NOT in ( SELECT  mobile FROM tblReceivedSms WHERE  Mobile=@Mobile  
    AND CAST(Message AS NVARCHAR(1))='5'  AND (DATEDIFF(minute,regdate,GETDATE()) < 2) 
    )
    BEGIN
	  SELECT @ServiceId = ServiceId FROM dbo.SMS_AutoSendingSettings WHERE ServiceId=5 AND IsActive = 1
	  IF (ISNULL(@ServiceId,0) <> 0 AND ISNULL(@Mobile,'') <> '')
	  BEGIN
	    INSERT INTO dbo.tblReceivedSms(Mobile,[Message])  
	    VALUES(@Mobile, CAST(@ServiceId AS VARCHAR(10)))
	  END 
    END
  End
  CLOSE Cursor_Ghest
  DEALLOCATE Cursor_Ghest

  RETURN @msg_Num
