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.pengiriman_after_update
DROP TRIGGER IF EXISTS `pengiriman_after_update`;
SET @OLDTMP_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_ENGINE_SUBSTITUTION';
DELIMITER //
CREATE TRIGGER `pengiriman_after_update` AFTER UPDATE ON `pengiriman` FOR EACH ROW BEGIN
	IF OLD.STATUS != NEW.STATUS AND NEW.STATUS = 2 THEN
		UPDATE inventory.pengiriman_detil
		   SET STATUS = 2
		 WHERE PENGIRIMAN = OLD.NOMOR
		   AND STATUS = 1;
	END IF;
	IF OLD.STATUS != NEW.STATUS AND OLD.STATUS = 2 AND NEW.STATUS = 1 THEN
		UPDATE inventory.pengiriman_detil
		   SET STATUS = 1
		 WHERE PENGIRIMAN = OLD.NOMOR
		   AND STATUS = 2;
	END IF;
	/* Batal Kirim */
	IF OLD.STATUS != NEW.STATUS AND OLD.STATUS = 1 AND NEW.STATUS = 0 THEN
		UPDATE inventory.pengiriman_detil
		   SET STATUS = 0
		 WHERE PENGIRIMAN = OLD.NOMOR
		   AND STATUS = 1;
		UPDATE inventory.penerimaan SET STATUS = 0 WHERE REF = NEW.NOMOR AND JENIS = 1;
		UPDATE inventory.permintaan SET STATUS = 1 WHERE NOMOR = NEW.PERMINTAAN AND STATUS = 2;
	END IF;
	
	
	IF OLD.TANGGAL != NEW.TANGGAL THEN
		UPDATE inventory.pengiriman_detil d
		   SET d.FLAG_UBAH_TANGGAL = (d.FLAG_UBAH_TANGGAL + 1)
		 WHERE PENGIRIMAN = OLD.NOMOR;
	END IF;
	
END//
DELIMITER ;
