CREATE TRIGGER [dbo].[TAuto_Sanad] ON [dbo].[Havaleh]
AFTER INSERT
AS 
Begin
     Declare @Price  Money
     Declare @Code  Varchar(20)
     Declare @Id       BigInt
     Declare @Status Char
     Declare @Kind   Char
     Declare @Msg   Bigint
     Declare @Note NvarChar(60)
     Declare @KindCooperative  Smallint
     Declare @PriceCooperative Money
     Declare @CooperativeFlag  Char(1)

     SELECT   @Kind=dbo.headH.Kind_Flag,@Note=Cast(Headh.Note as Nvarchar(150)),
              @Price= 
                    Case 
                      When headH.Kind_Flag in(2,3,4,8) then Tot_Price_KHarid
                      When headH.Kind_Flag in(5,7,9,6,0) then Tot_Price_Forosh
                    End, 
              @Code=
                    Case 
                      When headH.Kind_Flag in(2,3,6,8,0) then i.Sender
                      When headH.Kind_Flag in(5,7,9,4)   then i.Reciver
                    End, 
              @Id=dbo.headH.Id_Havaleh,@Status = I.Status,
              @CooperativeFlag = i.Cooperative_Flag,
              @KindCooperative = Kind_Cooperative, 
              @PriceCooperative = Tot_Price_Cooperative
              FROM  dbo.headH INNER JOIN
              Inserted I ON dbo.headH.Id_Havaleh = I.Id_Havaleh
     GROUP BY dbo.headH.Kind_Flag, I.Sender, i.Reciver, dbo.headH.Id_Havaleh, i.Status,i.Cooperative_Flag,
              HeadH.Tot_Price_Kharid, HeadH.Tot_Price_forosh, Headh.Tot_Price_Cooperative,Kind_Cooperative, Cast(Headh.Note as Nvarchar(150))
     
	 IF (@Price > 0 ) And (Len(@Code) > 3 ) 
     Begin
       IF (@Status Is Null Or @Status = '')  
       Begin
		 IF @KindCooperative = 0
		   Exec @Msg= Create_Sanad_Auto   @Msg  ,  @Kind     ,  @Code    ,@Price    ,@Id,@Note,null
		 Else
		 Begin
		   IF Exists(Select Parent_code From Act Where Act_Code = @KindCooperative)
		   Begin
		     Set @Price = @Price - @PriceCooperative
		     IF @Price > 0 
		 	   Exec @Msg= Create_Sanad_Auto   @Msg  ,  @Kind     ,  @Code    ,@Price     ,@Id,@Note,null
		     Exec @Msg= Create_Sanad_Auto   @Msg  ,  @KindCooperative     ,  @Code    ,@PriceCooperative    ,@Id,@Note,null
		   End
		   Else
		     Exec @Msg= Create_Sanad_Auto   @Msg  ,  @Kind     ,  @Code    ,@Price    ,@Id,@Note,null
		 End
       End
       Else
       Begin
         IF @CooperativeFlag = 1 
           Set @KindCooperative = Cast(@Kind as Smallint) + 900
         Else
           Set @KindCooperative = Cast(@Kind as Smallint)
         IF Not Exists(Select Id_Sanad From Sanad Where Nu_Havaleh = @Id And Nu_Cooperative = @KindCooperative)
         Begin
           IF @CooperativeFlag = 0 
           begin
             Set @Price = @Price - @PriceCooperative 
             Exec @Msg= Create_Sanad_Auto   @Msg  ,  @Kind     ,  @Code    , @Price  ,@Id,@Note,null
           end  
           Else 
		   Begin
		     IF Exists(Select Parent_code From Act Where Act_Code = @KindCooperative)
		       Exec @Msg= Create_Sanad_Auto   @Msg  ,  @KindCooperative     ,  @Code    ,@PriceCooperative    ,@Id,@Note,null
		   End
         End
       End
     End
End
