--@Msg_Num=-10 با اين شماره موردي براي صدور وجود ندارد
--@msg-num = -5 خطاي سيستم
--msg-num = Left(NuSanad,3) = -99  سند مربوطه بسته است

CREATE PROCEDURE [dbo].[Sodor_Vajeh]  @Msg_num BigInt Output ,@Id_Cheqe BigInt,@Tmp_Id numeric(18,0),@Main_Id_Havaleh BigInt ,@Main_Id_Sanad bigint
As
DECLARE @Nu_Sanad     Bigint
-- كنترل باز بودن سند
SELECT  @Nu_Sanad = 
CASE
  WHEN @Main_Id_Sanad = 0 THEN (SELECT Nu_Manual FROM dbo.heads where DateDiff(day,Tarikh_User,getdate()) =0 And Auto=1 and Fiexd=1 )
ELSE
  (SELECT Nu_Manual FROM dbo.heads where Fiexd=1 And Id_Sanad = @Main_Id_Sanad)
End
  
IF ( @Nu_Sanad ) > 0 
begin
   Set  @msg_num= cast ('-99'+Ltrim(str(@Nu_Sanad)) as int)
   RETURN   @msg_num  
 End
ELSE
 ------------------------------------------------------------------------------------------------------------------------
 DECLARE @TmpHeadc as Table ( Id_Cheqe  BIGINT, Id_Havaleh  bigint , Id_Sanad bigint , Id_Manual VARCHAR(10), Kind_Flag CHAR(1), Items smallint,
	                          TarikhBargeh SMALLDATETIME, TotPrice MONEY, Note NTEXT,Guy_Code VARCHAR(5),Guy_Bank VARCHAR(5),
	                          AccCode VARCHAR(20), Tarikh SMALLDATETIME, User_Id SMALLINT, Store_Code VARCHAR(3)
                              ) 
 INSERT INTO @TmpHeadc ( Id_Cheqe, Id_Havaleh , Id_Sanad , Id_Manual , Kind_Flag ,
                          Items ,  TarikhBargeh , TotPrice ,  Note ,
                          Guy_Code ,  Guy_Bank ,  AccCode ,   Tarikh ,
                          User_Id , Store_Code)
 SELECT Id_Cheqe, Id_Havaleh , Id_Sanad , Id_Manual , Kind_Flag ,
                          Items ,  TarikhBargeh , TotPrice ,  Note ,
                          Guy_Code ,  Guy_Bank ,  AccCode ,   Tarikh ,
                          User_Id , Store_Code FROM TmpHeadc WHERE Id_Cheqe=@Id_Cheqe
                          
 DECLARE @TmpCheqe as Table ( Id_Cheqe  BIGINT , Ch_Note NVARCHAR(90),Ch_no NVARCHAR(15), Ch_Tarikh SMALLDATETIME,Ch_Price MONEY,
                              Ch_Pass VARCHAR(2), Ch_Desc NVARCHAR(70), Acc_Code VARCHAR(20),Act_Code SMALLINT, Ch_Bank SMALLINT,
                              Tmp_Main_Auto_Sanad CHAR(1) ,Guid_Cheqe UNIQUEIDENTIFIER, Nu_Hesab  VARCHAR(15)
                              ) 
 INSERT INTO @TmpCheqe (Id_Cheqe  , Ch_Note ,Ch_no , Ch_Tarikh ,Ch_Price ,
                        Ch_Pass , Ch_Desc , Acc_Code ,Act_Code , Ch_Bank ,
                        Tmp_Main_Auto_Sanad  ,Guid_Cheqe , Nu_Hesab)
 SELECT Id_Cheqe  , Ch_Note ,Ch_no , Ch_Tarikh ,Ch_Price ,
                        Ch_Pass , Ch_Desc , Acc_Code ,Act_Code , Ch_Bank ,
                        Tmp_Main_Auto_Sanad  ,Guid_Cheqe , Nu_Hesab FROM TmpCheqe 
                        WHERE Id_Cheqe = @Id_Cheqe And CH_Price > 0 
 ------------------------------------------------------------------------------------------------------------------------
 
 IF (SELECT TOP 1 Id_Cheqe FROM @TmpCheqe) > 0 
 BEGIN
    BEGIN Tran SodorVajh 
    SET @msg_num=0
    DECLARE @LastId_Cheqe Bigint
    
    INSERT Into dbo.Headc(Id_Havaleh , Id_Sanad  ,Id_Manual,Kind_Flag,Items,TarikhBargeh,TotPrice,Note,Guy_Code,Guy_Bank,AccCode,Tarikh,User_Id, Store_Code)
                Select @Main_Id_Havaleh,@Main_Id_Sanad,Id_Manual,Kind_Flag,Items,TarikhBargeh,TotPrice,Note,Guy_Code,Guy_Bank,AccCode,Tarikh,User_Id, Store_code
                From @TmpHeadc Where Id_Cheqe=@Id_Cheqe
    IF @@Error > 0        
    Begin 
       Set  @msg_num=-5  
       ROLLBACK TRANSACTION 
       RETURN @msg_num 
    End

    Select @LastId_Cheqe= SCOPE_IDENTITY()  
    Insert Into Dbo.Cheqe (Id_Cheqe , Ch_Note,Ch_no,Ch_Tarikh,Ch_Price,Ch_Pass,ch_pass_New,Ch_Desc,Acc_Code,Act_Code,Ch_Bank,Tmp_Main_Auto_Sanad,Guid_Cheqe, Nu_Hesab  )
    SELECT                 @LastId_Cheqe, Ch_Note,Ch_no,Ch_Tarikh,Ch_Price,Ch_Pass,Ch_Pass,Ch_Desc,Acc_Code,Act_Code,Ch_Bank  ,Tmp_Main_Auto_Sanad,Guid_Cheqe, Nu_Hesab
    FROM  @TmpCheqe  WHERE (Id_Cheqe = @Id_Cheqe) And (CH_Price > 0)

    IF @@Error > 0        
    Begin
      Set  @msg_num=-5  
      ROLLBACK TRANSACTION 
      RETURN @msg_num 
    END 

    Insert Into Dbo.Acc_Cheqe_Link(Guid_Cheqe,Guid_Sanad )
    SELECT  dbo.TmpAcc_Cheqe_link.Guid_Cheqe, dbo.TmpAcc_Cheqe_link.Guid_Sanad
    FROM  dbo.TmpAcc_Cheqe_link INNER JOIN @TmpCheqe TmpCheqe 
    ON dbo.TmpAcc_Cheqe_link.Guid_Cheqe = TmpCheqe.Guid_Cheqe
    WHERE TmpCheqe.Id_Cheqe =@Id_Cheqe

    IF @@Error > 0       
    BEGIN
      Set  @msg_num=-5  
      ROLLBACK TRANSACTION 
      RETURN @msg_num 
    END
    Delete from dbo.TmpHeadc Where Id_Cheqe=@id_Cheqe
    Commit Tran SodorVajh
    SET @Msg_Num=@LastId_Cheqe
    RETURN @msg_num
 End 
 Else Begin
   Set @Msg_Num=-10
   Return @Msg_Num
 End 


