-- --------------------------------------------------------
-- 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;
	
END//
DELIMITER ;

