--msg-num = -1  برخي از کالاها ها موجودي ندارند
--msg-num = -2 موردي براي صدور وجود ندارد
--msg-num = -3 سريال کالايي که براي فروش انتخاب شده قبلا به فروش رسيده است
--msg-num = -4 انقضا کالاهائي که براي فروش انتخاب شده قبلا بع فروش رسيده است
--msg-num = -5 خطاي سيستم
--msg-num = -6 برخي از کالاها قيمت ندارند
--@Msg_Num=-20 با اين شماره موردي براي صدور وجود ندارد
--@Msg_Num=-21
--@msg-num = -25 خطاي سيستم
-------------------------------------------------------------------------
-- مربوطه به وجه
--@Msg_Num=-10 با اين شماره موردي براي صدور وجود ندارد
--@Msg_Num=-11
--@msg-num = -5 خطاي سيستم
------------------------------------------------------------
        

CREATE PROCEDURE [dbo].[Sodor_Factor] @msg_num Numeric(18,0) OUTPUT,@TmpId  Numeric(18,0) AS

 Declare @Nu_sanad Bigint
 Declare @idSanad Bigint

 set @msg_num=0
 IF (Select Count(TmpHeadh.Id_Havaleh) From Dbo.TmpHeadH 
     Inner Join TmpHavaleh on TmpHavaleh.Id_Havaleh = Tmpheadh.Id_Havaleh
     Where Tmpheadh.Id_Havaleh = @TmpId Group By TmpHeadh.Id_Havaleh) > 0  
 Begin
   -- كنترل باز بودن سند
   Select @IdSanad = Sanad From TmpHeadH Where Id_Havaleh = @TmpId
   IF (@IdSanad is null) Or (@IdSanad = '')
     SELECT @Nu_Sanad=Nu_Manual FROM dbo.heads where (DateDiff(day,Tarikh_User,getdate() ) =0 ) And (Auto=1)  and (Fiexd=1 )

   
   SELECT @Nu_Sanad= Nu_Manual FROM dbo.heads where Id_Sanad = @IdSanad  And (Fiexd=1 )
   IF ( @Nu_Sanad ) > 0 
   Begin
     Set  @msg_num= cast ('-99'+Ltrim(str(@Nu_Sanad)) as int)
     RETURN @msg_num  --msg-num = -10 سند مربوطه بسته است
   End
   ------------------------------------------------------------------------------------------------
   -- کنترل موجودي
   IF (Select Count(*) from LESS_Mojodi(@TmpId))  > 0 Begin
     Set  @msg_num=-1  --msg-num = -1  برخي از کالاها ها موجودي ندارند
     RETURN @msg_num
   End
   
   ------------------------------------------------------------------------------------------------
   -- کنترل سريال
   Else IF (Select Count(*) from LESS_Serial(@TmpId))  > 0 
   Begin
     Set  @msg_num=-3 --msg-num = -3 سريال کالايي که براي فروش انتخاب شده قبلا به فروش رسيده است
     RETURN @msg_num
   End
   ------------------------------------------------------------------------------------------------
   -- کنترل تارخ انقضا
   Else IF (Select Count(*) from LESS_Exp(@TmpId))  > 0 
   Begin
     Set  @msg_num=-4 --msg-num = -4 انقضا کالاهائي که براي فروش انتخاب شده قبلا به فروش رسيده است
     RETURN @msg_num
   End
   
   ------------------------------------------------------------------------------------------------
   Else 
   Begin   
     Exec Dbo.Sp_CalcTotPriceFactor @TmpId
     Begin Tran 
     Declare @Last_Id Numeric(18,0)
     Insert Into Dbo.HeadH(Kind_Flag,Nu_Havaleh,Nu_Resid,Nu_Forosh,Nu_Kharid,Nu_Bargasht_Forosh,Nu_Bargasht_kharid,Items,Recive_Name,Trans_Name,Tot_Price_kharid,Tot_Price_Forosh,Tot_Price_Omdeh,Tot_Price_Cooperative, Note,Tot_kahandeh,Tot_Afzayandeh,User_Id,Tarikh_Faktor, Tarikh_Pardakht, CheckTarikh_Pardakht, Number_Bar, Sanad,Store_Code, Kind_Cooperative)
                    Select Kind_Flag,Nu_Havaleh,Nu_Resid,Nu_Forosh,Nu_Kharid,Nu_Bargasht_Forosh,Nu_Bargasht_kharid,Items,Recive_Name,Trans_Name,Tot_Price_kharid,Tot_Price_Forosh,Tot_Price_Omdeh,Tot_Price_Cooperative, Note,Tot_kahandeh,Tot_Afzayandeh,User_Id,Tarikh_Faktor,Tarikh_Pardakht, CheckTarikh_Pardakht ,Number_Bar, Sanad, Store_code,Kind_Cooperative  From TmpHeadH
                    Where Id_Havaleh=@TmpId	
     IF @@Error > 0  
     Begin
       Set  @msg_num=-5  
       ROLLBACK TRANSACTION 
       RETURN @msg_num 
     End    
   
     Select @Last_Id=Scope_Identity() 
   
     Insert Into Dbo.Havaleh(Id_Havaleh,AutoId,Sender,Reciver,Radif,K_Code,K_Qty1,K_Qty2,R_Unit1_2,Price_Kharid,Price_Forosh,Price_Omdeh,Status,Takhfif_Flag,Cooperative_Flag, Point_Flag,Serial_Flag,Hamrah_Flag,EditQty_Flag,CtrMojodi,Link_Record,Exp_Flag,Make_Flag,Note,DayOfKala, Price_Consumer)
                     Select @Last_Id ,AutoId,Sender,Reciver,Radif,K_Code,K_Qty1,K_Qty2,R_Unit1_2,Price_Kharid,Price_Forosh,Price_Omdeh,Status,Takhfif_Flag,Cooperative_Flag, Point_Flag,Serial_Flag,Hamrah_Flag,EditQty_Flag,CtrMojodi,Link_Record,Exp_Flag,Make_Flag,Note,DayOfKala,Price_Consumer
                     From TmpHavaleh Where Id_Havaleh=@TmpId  -- رکوردهاي جدول تمپ را به جدول اصلي حواله اضافه ميکند
   
     IF @@Error > 0  
     Begin
       Set  @msg_num=-5  
       ROLLBACK TRANSACTION
       RETURN @msg_num 
     End    
     ------------------------------------------------------------------------------------------------    -- صدور تاريخ انقضا
     IF ( Select Count(*) From Dbo.TmpHavaleh WHERE (Id_Havaleh = @TmpId) AND (Exp_Flag = 1) ) > 0
        Exec Sodor_Exp @TmpId  
     IF @@Error > 0  
     Begin
       Set  @msg_num=-5  
       ROLLBACK TRANSACTION
       RETURN @msg_num 
     End    
     ------------------------------------------------------------------------------------------------
     IF (Select Count(*)  FROM  dbo.TmpSerial WHERE  AutoId In (Select  AutoId From dbo.TmpHavaleh Where Id_Havaleh = @TmpId)) > 0 
       exec  Sodor_Serial  @TmpId  
     IF @@Error > 0  
     Begin
       Set  @msg_num=-5  
       ROLLBACK TRANSACTION
       RETURN @msg_num 
     End    
     ------------------------------------------------------------------------------------------------
     Delete from dbo.Tmp_Over_Under_Factor Where  (Id_Havaleh=@TmpId) And (Price <=0 Or Price is null )
     IF (SELECT     Count(*) FROM dbo.Tmp_Over_Under_Factor   WHERE     (Id_Havaleh = @TmpId)) > 0 
     Begin
       Exec  @Msg_Num=Sodor_Over_Under  @Msg_num   ,@TmpId  ,@last_id
       If (@@Error > 0) Or (@Msg_Num < 0)  
       Begin
         Set  @msg_num=-5  
         ROLLBACK TRANSACTION
         RETURN @msg_num 
       End    
     End
     
     ------------------------------------------------------------------------------------------------
     Insert Into Dbo.Over_Under_Row (AutoId,name,Code_Act,Type,Price,[Percent],Code_Over_Under)
     SELECT  Tmp_Over_Under_Row.AutoId, Tmp_Over_Under_Row.name, Tmp_Over_Under_Row.Code_Act, 
                   Tmp_Over_Under_Row.Type, Tmp_Over_Under_Row.Price, Tmp_Over_Under_Row.[Percent], 
                   Tmp_Over_Under_Row.Code_Over_Under FROM  TmpHavaleh INNER JOIN
                   Tmp_Over_Under_Row ON TmpHavaleh.AutoId = Tmp_Over_Under_Row.AutoId
                    WHERE  TmpHavaleh.Id_Havaleh = @TmpId And Price > 0 
     IF @@Error > 0  
     Begin
       Set  @Msg_Num= -5  
       ROLLBACK TRANSACTION
       RETURN @Msg_Num 
     End
      ---------------------------------------------------------------------------------------------------------------------------------------
     Declare @Id_Cheqe BigInt      
     SELECT  @Id_cheqe= dbo.TmpHeadC.Id_Cheqe
     FROM dbo.TmpHeadC INNER JOIN dbo.TmpCheqe ON dbo.TmpHeadC.Id_Cheqe = dbo.TmpCheqe.Id_Cheqe
     WHERE (dbo.TmpHeadc.Id_Havaleh = @TmpId)And (dbo.TmpCheqe.CH_Price > 0)
  
     IF  (@Id_Cheqe <> Null) Or (@Id_Cheqe <> 0) 
       Exec  @Msg_Num=Sodor_Vajeh  @Msg_num,@Id_Cheqe,@TmpId,@last_id,@IdSanad
     IF (@@Error > 0) Or (@Msg_Num < 0)  
     Begin
       Set  @msg_num=-5  
       ROLLBACK TRANSACTION
       RETURN @msg_num 
     End    
     ------------------------------------------------------------------------------------------------
     Delete From dbo.TmpHeadH  Where  Id_Havaleh=@TmpId 
     ------------------------------------------------------------------------------------------------
     print 'فاکتور صادر شد' 
  End 
   
  Commit Tran
  
  Select @msg_num=@Last_Id
  RETURN @msg_num
 End -- If (Select Count(*)  From Dbo.TmpHeadH Where Id_Havaleh = @TmpId  ) > 0   And  (Select Count(*)  From Dbo.TmpHavaleh  Where Id_Havaleh = @TmpId) > 0
 Else 
 Begin
   Set  @msg_num=-2 
   RETURN @msg_num
 Print 'موردي براي صدور وجود ندارد'
End
