Create TRIGGER [dbo].[TAuto_Sanad] ON [dbo].[Havaleh]
FOR INSERT
AS 
Begin
     Declare @Price  Money
     Declare @Code  Varchar(20)
     Declare @Id       BigInt
     Declare @Kind   Char
     Declare @Status Char
     Declare @Msg   Bigint
     Declare @Note NvarChar(60)
     Declare @Tot_Price_Kharid Money 
     Declare @Tot_Price_Omdeh  Money 
     Declare @Tot_Price_Forosh Money 
     Declare @Tot_Price        Money 
	 Declare @_Price           Money
     Declare @Nu               Bigint 


-------------------------- فاکتور خريد و برگشت از خريد
     SELECT   @Kind=dbo.headH.Kind_Flag,@Price= HeadH.Tot_Price_Kharid,@_Price = HeadH.Tot_Price_forosh, @Code=I.Sender,
                     @Id=dbo.headH.Id_Havaleh, @Status = I.Status,@Nu=i.Id_Havaleh
     FROM        dbo.headH INNER JOIN
                       Inserted I ON dbo.headH.Id_Havaleh = I.Id_Havaleh
     GROUP BY dbo.headH.Kind_Flag, I.Sender, dbo.headH.Id_Havaleh, I.Status,I.Id_Havaleh, HeadH.Tot_Price_Kharid, HeadH.Tot_Price_forosh

     Select @Note=Note From HeadH Where Id_Havaleh = @Id

     IF @Kind in('0','6')
	   Set @Price = @_Price

	 IF (@Price > 0 ) And (Len(@Code) > 3 ) 
     Begin
        IF (@Status Is Null Or @Status = '')  
           Exec @Msg= Create_Sanad_Auto   @Msg  ,  @Kind     ,  @Code    ,@Price    ,@Id,@Note,null
        Else
        Begin
            Select @Tot_Price_Forosh=Sum(Round(K_Qty1*Price_Forosh,0)),  
                      @Tot_Price_Kharid=Sum(Round(K_Qty1*Price_Kharid,0)),
                      @Tot_Price_Omdeh=Sum(Round(K_Qty1*Price_Omdeh,0))
                      From Havaleh where Id_Havaleh=@Nu

          Update HeadH Set Tot_Price_kharid  =@Tot_Price_Kharid,  Tot_Price_Forosh =@Tot_Price_Forosh,  Tot_Price_Omdeh =@Tot_Price_Omdeh
               From Inserted i  Where  HeadH.Id_havaleh=@id

            Set @Tot_Price = 0 
            IF  @Kind= '0'  Set @Tot_Price=@Tot_Price_Forosh 
            IF  @Kind= '2'  Set @Tot_Price=@Tot_Price_Kharid 
            IF  @Kind= '3'  Set @Tot_Price=@Tot_Price_Kharid 
            IF  @Kind= '4'  Set @Tot_Price=@Tot_Price_Kharid 
            IF  @Kind= '5'  Set @Tot_Price=@Tot_Price_Forosh 
            IF  @Kind= '6'  Set @Tot_Price=@Tot_Price_Forosh 
            IF  @Kind= '7'  Set @Tot_Price=@Tot_Price_Forosh 
            IF  @Kind= '8'  Set @Tot_Price=@Tot_Price_Kharid 
            IF  @Kind= '9'  Set @Tot_Price=@Tot_Price_Forosh 
            IF @Tot_Price <>0 
            Begin  
                Update   dbo.sanad set Bedeh=@Tot_Price  Where (Nu_Havaleh=@Nu) and (Bestan=0) and ( Nu_Over_under is Null )  
                Update dbo.sanad set Bestan=@Tot_Price  Where (Nu_Havaleh=@Nu) and (Bedeh=0) and ( Nu_Over_under is Null ) 
           End 
         End
     End
------------------------- فاکتور فروش و برگشت از فروش
    Else 
    Begin
       SELECT @Kind=dbo.headH.Kind_Flag,@Price = HeadH.Tot_Price_Forosh,@_Price = HeadH.Tot_Price_Kharid,@Code=I.Reciver,@Id=dbo.headH.Id_Havaleh
       FROM dbo.headH INNER JOIN Inserted I ON dbo.headH.Id_Havaleh = I.Id_Havaleh
       GROUP BY dbo.headH.Kind_Flag,  I.Reciver, dbo.headH.Id_Havaleh, HeadH.Tot_Price_Forosh,HeadH.Tot_Price_Kharid

      Select @Note=Note From HeadH Where Id_Havaleh = @Id

       IF @Kind in('4')
	     Set @Price = @_Price

       IF (@Price > 0 ) And (Len(@Code) >  3 ) 
       Begin
          IF (@Status Is Null Or @Status = '')  
             Exec   Create_Sanad_Auto             @Msg     ,   @Kind     ,  @Code   ,@Price   ,@Id,@Note,null
          Else
          Begin
            Select @Tot_Price_Forosh=Sum(Round(K_Qty1*Price_Forosh,0)),  
                      @Tot_Price_Kharid=Sum(Round(K_Qty1*Price_Kharid,0)),
                      @Tot_Price_Omdeh=Sum(Round(K_Qty1*Price_Omdeh,0))
                      From Havaleh where Id_Havaleh=@Nu
 
          Update HeadH Set Tot_Price_kharid  =@Tot_Price_Kharid,  Tot_Price_Forosh =@Tot_Price_Forosh,  Tot_Price_Omdeh =@Tot_Price_Omdeh
               From Inserted i  Where  HeadH.Id_havaleh=@id

              Set @Tot_Price = 0 
              IF  @Kind= '0'  Set @Tot_Price=@Tot_Price_Forosh 
              IF  @Kind= '2'  Set @Tot_Price=@Tot_Price_Kharid 
              IF  @Kind= '3'  Set @Tot_Price=@Tot_Price_Kharid 
              IF  @Kind= '4'  Set @Tot_Price=@Tot_Price_Kharid 
              IF  @Kind= '5'  Set @Tot_Price=@Tot_Price_Forosh 
              IF  @Kind= '6'  Set @Tot_Price=@Tot_Price_Forosh 
              IF  @Kind= '7'  Set @Tot_Price=@Tot_Price_Forosh 
              IF  @Kind= '8'  Set @Tot_Price=@Tot_Price_Kharid 
              IF  @Kind= '9'  Set @Tot_Price=@Tot_Price_Forosh 
              IF @Tot_Price <>0 
              Begin  
                Update dbo.sanad set Bedeh=@Tot_Price  Where (Nu_Havaleh=@Nu) and (Bestan=0) and ( Nu_Over_under is Null )  
                Update dbo.sanad set Bestan=@Tot_Price  Where (Nu_Havaleh=@Nu) and (Bedeh=0) and ( Nu_Over_under is Null ) 
              End 
           End
       End
    End
End
