USE `inventory`;
DROP TRIGGER IF EXISTS `transaksi_stok_ruangan_after_insert`;
SET @OLDTMP_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_ENGINE_SUBSTITUTION';
DELIMITER //
CREATE TRIGGER `transaksi_stok_ruangan_after_insert` AFTER INSERT ON `transaksi_stok_ruangan` FOR EACH ROW BEGIN
	DECLARE VID CHAR(23);
	
	SELECT ID INTO VID
	  FROM inventory.transaksi_stok_ruangan
	 WHERE BARANG_RUANGAN = NEW.BARANG_RUANGAN
		AND ID > NEW.ID
	 ORDER BY ID ASC, TANGGAL ASC LIMIT 1;
	 
	IF FOUND_ROWS() > 0 THEN
		INSERT INTO inventory.recalculation(BARANG_RUANGAN, ID) VALUES(NEW.BARANG_RUANGAN, VID);
	END IF;
	
	IF NEW.JENIS = 21 OR NEW.JENIS = 24 THEN
		INSERT INTO inventory.log_status_mutasi (JENIS,REF) VALUES (NEW.JENIS, NEW.REF);
	END IF;
	
END//
DELIMITER ;
SET SQL_MODE=@OLDTMP_SQL_MODE;