-- --------------------------------------------------------
-- Server OS:                    Linux
-- HeidiSQL Version:             11.0.0.5919
-- --------------------------------------------------------


USE `cssd`;

-- Dumping structure for trigger cssd.penerimaan_alat_kotor_after_update
SET @OLDTMP_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_ENGINE_SUBSTITUTION';
DELIMITER //
CREATE TRIGGER `penerimaan_alat_kotor_after_update` AFTER UPDATE ON `penerimaan_alat_kotor` FOR EACH ROW BEGIN

	DECLARE VNOMOR CHAR;

	IF NEW.STATUS = 0 THEN
		/*SELECT COUNT(*) INTO VNOMOR FROM cssd.pengembalian_alat pa
		WHERE pa.PENERIMAAN_ALAT_KOTOR = OLD.NOMOR;
		
		IF VNOMOR != 0 THEN
			UPDATE cssd.pengembalian_alat pa
				SET pa.STATUS = 0
			WHERE pa.PENERIMAAN_ALAT_KOTOR = OLD.NOMOR;
		END IF;*/
	
		UPDATE cssd.penerimaan_alat_kotor_detil pakd 
		   SET pakd.STATUS = 0
		 WHERE pakd.PENERIMAAN_ALAT_KOTOR = OLD.NOMOR;
		 
		UPDATE cssd.penerimaan_alat_kotor_detil_barangruangan pakdbr
		   SET pakdbr.STATUS = 0
		 WHERE pakdbr.PENERIMAAN_ALAT_KOTOR = OLD.NOMOR;
	END IF;

	IF NEW.STATUS = 2 THEN
		UPDATE cssd.penerimaan_alat_kotor_detil pakd 
		   SET pakd.STATUS = 2
		 WHERE pakd.PENERIMAAN_ALAT_KOTOR = OLD.NOMOR;
		 
		UPDATE cssd.penerimaan_alat_kotor_detil_barangruangan pakdbr
		   SET pakdbr.STATUS = 2
		 WHERE pakdbr.PENERIMAAN_ALAT_KOTOR = OLD.NOMOR;
	END IF;
	
	IF NEW.STATUS = 1 AND NEW.STATUS != OLD.STATUS THEN
			UPDATE cssd.penerimaan_alat_kotor_detil pakd 
		   SET pakd.STATUS = 1
		 WHERE pakd.PENERIMAAN_ALAT_KOTOR = OLD.NOMOR;
		 
		UPDATE cssd.penerimaan_alat_kotor_detil_barangruangan pakdbr
		   SET pakdbr.STATUS = 1
		 WHERE pakdbr.PENERIMAAN_ALAT_KOTOR = OLD.NOMOR;
	END IF;
	
END//
DELIMITER ;

-- Dumping structure for trigger cssd.pengembalian_alat_after_update
SET @OLDTMP_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_ENGINE_SUBSTITUTION';
DELIMITER //
CREATE TRIGGER `pengembalian_alat_after_update` AFTER UPDATE ON `pengembalian_alat` FOR EACH ROW BEGIN

	IF NEW.STATUS = 0 THEN
		UPDATE cssd.pengembalian_alat_detil pad 
		   SET pad.STATUS = 0
		 WHERE pad.PENGEMBALIAN_ALAT = OLD.NOMOR;
		 
		UPDATE cssd.pengembalian_alat_detil_barangruangan padbr
		   SET padbr.STATUS = 0
		 WHERE padbr.PENGEMBALIAN_ALAT = OLD.NOMOR;
	END IF;

	IF NEW.STATUS = 2 THEN
		UPDATE cssd.pengembalian_alat_detil pad 
		   SET pad.STATUS = 2
		 WHERE pad.PENGEMBALIAN_ALAT = OLD.NOMOR;
		 
		UPDATE cssd.pengembalian_alat_detil_barangruangan padbr
		   SET padbr.STATUS = 2
		 WHERE padbr.PENGEMBALIAN_ALAT = OLD.NOMOR;
		 
		/*UPDATE cssd.penerimaan_alat_kotor pak
		   SET pak.STATUS = 2, pak.KEMBALI = 2
		 WHERE pak.NOMOR = OLD.PENERIMAAN_ALAT_KOTOR;*/
	END IF;
	
	IF NEW.STATUS = 1 AND NEW.STATUS != OLD.STATUS THEN
		UPDATE cssd.pengembalian_alat_detil pad 
		   SET pad.STATUS = 1
		 WHERE pad.PENGEMBALIAN_ALAT = OLD.NOMOR;
		 
		UPDATE cssd.pengembalian_alat_detil_barangruangan padbr
		   SET padbr.STATUS = 1
		 WHERE padbr.PENGEMBALIAN_ALAT = OLD.NOMOR;
	END IF;
	
END//
DELIMITER ;

