CREATE TRIGGER [dbo].[TD_HeadH]  ON [dbo].[headH]
INSTEAD OF DELETE
AS
  Declare @Id_Havaleh  BIGINT
  DECLARE @DeletedUser SMALLINT
  Select  @Id_Havaleh=id_Havaleh from Deleted 
  SELECT  @DeletedUser = Manager.dbo.SessionUser(@@Spid,0)
  INSERT INTO dbo.History_Detail_Factors
          ( Id_Havaleh ,
            AutoId ,
            Sender ,
            Reciver ,
            Radif ,
            K_Code ,
            K_Qty1 ,
            K_Qty2 ,
            R_Unit1_2 ,
            Price_KHarid ,
            Price_Omdeh ,
            Price_Forosh ,
            Status ,
            Takhfif_Flag ,
            Exp_Flag ,
            Point_Flag ,
            Serial_Flag ,
            Make_Flag ,
            Hamrah_Flag ,
            EditQty_Flag ,
            CtrMojodi ,
            Link_Record ,
            Note, UserIdForSelete 
          )
  SELECT Id_Havaleh ,
            AutoId ,
            Sender ,
            Reciver ,
            Radif ,
            K_Code ,
            K_Qty1 ,
            K_Qty2 ,
            R_Unit1_2 ,
            Price_KHarid ,
            Price_Omdeh ,
            Price_Forosh ,
            Status ,
            Takhfif_Flag ,
            Exp_Flag ,
            Point_Flag ,
            Serial_Flag ,
            Make_Flag ,
            Hamrah_Flag ,
            EditQty_Flag ,
            CtrMojodi ,
            Link_Record ,
            Note,@DeletedUser  FROM dbo.Havaleh 
			WHERE Id_Havaleh = @Id_Havaleh
  DELETE FROM Headh WHERE Id_Havaleh=@Id_Havaleh
  DELETE FROM Sanad WHERE Nu_Havaleh=@Id_Havaleh
  DELETE FROM HeadC WHERE Id_Havaleh=@Id_Havaleh

