USE `inventory`;
-- --------------------------------------------------------
-- Versi server:                 8.0.11 - MySQL Community Server - GPL
-- OS Server:                    Win64
-- HeidiSQL Versi:               9.2.0.4947
-- --------------------------------------------------------


-- Dumping structure for trigger inventory.penerimaan_barang_after_update
DROP TRIGGER IF EXISTS `penerimaan_barang_after_update`;
SET @OLDTMP_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_ENGINE_SUBSTITUTION';
DELIMITER //
CREATE TRIGGER `penerimaan_barang_after_update` AFTER UPDATE ON `penerimaan_barang` FOR EACH ROW BEGIN
	IF OLD.STATUS != NEW.STATUS AND NEW.STATUS = 2 THEN
		UPDATE inventory.penerimaan_barang_detil
		   SET STATUS = 2, FINAL = (FINAL+1)
		 WHERE PENERIMAAN = NEW.ID
		   AND STATUS = 1;
	END IF;
	
	
	IF OLD.STATUS != NEW.STATUS AND OLD.STATUS = 2 AND NEW.STATUS = 1 THEN 
			UPDATE inventory.penerimaan_barang_detil
			   SET STATUS = 1, BATAL = (BATAL+1)
			 WHERE PENERIMAAN = OLD.ID
			   AND STATUS = 2;
	END IF;
	
	
	IF OLD.STATUS != NEW.STATUS AND OLD.STATUS = 1 AND NEW.STATUS = 0 THEN
		UPDATE inventory.penerimaan_barang_detil
		   SET STATUS = 0, BATAL = (BATAL+1)
		 WHERE PENERIMAAN = NEW.ID
		   AND STATUS = 1;
	END IF;
END//
DELIMITER ;
