USE inventory;

DROP TRIGGER IF EXISTS inventory.harga_barang_after_update;

DELIMITER //

CREATE DEFINER=`root`@`localhost` TRIGGER `harga_barang_after_update` AFTER UPDATE ON `harga_barang` FOR EACH ROW BEGIN
	IF NEW.HARGA_JUAL != OLD.HARGA_JUAL THEN
		UPDATE inventory.barang
		   SET HARGA_JUAL = NEW.HARGA_JUAL
		 WHERE ID = OLD.BARANG;
	END IF;
	
	IF NEW.REF_PENERIMAAN IS NOT NULL OR NEW.REF_PENERIMAAN != 0 AND NEW.IS_UPDATE_BY_CORE = 0 THEN
		UPDATE inventory.harga_barang_log_penerimaan SET STATUS = 99 WHERE REF_PENERIMAAN = NEW.REF_PENERIMAAN AND STATUS = 1;
	END IF;
END//
DELIMITER ;