ALTER TRIGGER [dbo].[TRIG_MATCH_ALTER_AR_RECEIVABLEENTRY] ON [dbo].[T_AR_RECEIVABLEENTRY]
AFTER INSERT
AS
BEGIN
	DECLARE @FID INT,@FENTRYID INT
    SELECT @FID = FID,@FENTRYID = FENTRYID FROM inserted

	DECLARE @FISWRITEOFF CHAR(1)
	SELECT @FISWRITEOFF = t0.FISWRITEOFF FROM T_AR_RECEIVABLE t0 WHERE t0.FID = @FID

	IF(@FISWRITEOFF = '1')
	BEGIN
		UPDATE t1e SET t1e.FNOTAXAMOUNTFOR = t1e.FALLAMOUNTFOR - CASE WHEN t1e.FALLAMOUNTFOR < 0 THEN 0 - t1e.FTAXAMOUNTFOR ELSE t1e.FTAXAMOUNTFOR END 
			,t1e.FNOTAXAMOUNT = t1e.FALLAMOUNT - CASE WHEN t1e.FALLAMOUNT < 0 THEN 0 - t1e.FTAXAMOUNT ELSE t1e.FTAXAMOUNT END 
			,t1e.FTAXAMOUNTFOR = CASE WHEN t1e.FALLAMOUNTFOR < 0 THEN 0 - t1e.FTAXAMOUNTFOR ELSE t1e.FTAXAMOUNTFOR END 
			,t1e.FTAXAMOUNT = CASE WHEN t1e.FALLAMOUNT < 0 THEN 0 - t1e.FTAXAMOUNT ELSE t1e.FTAXAMOUNT END
		FROM T_AR_RECEIVABLEENTRY t1e
		WHERE t1e.FENTRYID = @FENTRYID
	END
END
