DELIMITER $$

DROP TRIGGER IF EXISTS `Pit_11_8B_bi`$$
CREATE DEFINER=`root`@`localhost` TRIGGER `Pit_11_8B_bi` BEFORE INSERT ON `Pit_11_8B`
	FOR EACH ROW A: BEGIN
		IF _GetVer() = '5.1.30' THEN LEAVE A; END IF;
	--	wartosc @session.trigger_lock rowna TRUE blokuje wykonanie triggera
		IF IFNULL(@session.trigger_lock, FALSE) THEN LEAVE A; END IF;
		
		
		IF IFNULL(NEW.e_Doc_Def_ID, '') = '' THEN
			SELECT 
				ed.ID 
				INTO
				@NEW.e_Doc_Def_ID
			FROM `e_doc_def` ed
			WHERE 
				ed.doc_type = 'PIT-11' AND ed.doc_owner = 'gov.pl' AND YEAR(NEW.OkresDo) BETWEEN YEAR(ed.DateFrom) AND YEAR(ed.DateTo) ORDER BY ed.DateFrom DESC LIMIT 1
			;	
			SET NEW.e_Doc_Def_ID = @NEW.e_Doc_Def_ID;
		END IF;
		
    END;
$$

DROP TRIGGER IF EXISTS `Pit_11_8B_ai`$$
CREATE DEFINER = `root`@`localhost` TRIGGER `Pit_11_8B_ai` AFTER INSERT ON `Pit_11_8B`

    FOR EACH ROW A: BEGIN
		IF _GetVer() = '5.1.30' THEN LEAVE A; END IF;
	--	wartosc @session.trigger_lock rowna TRUE blokuje wykonanie triggera
		IF IFNULL(@session.trigger_lock, FALSE) THEN LEAVE A; END IF;
		
	/*
	*/
		IF IFNULL(NEW.GUID, '') != '' THEN

			INSERT IGNORE INTO `e_doc_ng`
				(e_Doc_Def_ID, Src_GUID, Src_TableName, Src_No, Src_OsobaGUID, Src_Date, Descr)
				VALUES
				(NEW.e_Doc_Def_ID, NEW.GUID, 'Pit_11_8B', NEW.NazwaPlatnika, NEW.pracGUID, NEW.OkresOd, 'Pit 11 8B')
			;	
		
			/*
			INSERT IGNORE INTO `dbobj_series`
				(GUIDDoc, TableName, NrDoc, OsobaGUID, DateLog, DateDoc, STATUS, Descr)
				VALUES
				(NEW.GUID, 'Pit_11_8B', NEW.NazwaPlatnika, NEW.pracGUID, NOW(), NEW.OkresOd, 0, 'Pit 11 8B')
			;	
			*/
		END IF;
		
    END;
$$

DROP TRIGGER IF EXISTS `Pit_11_8B_bu`$$
CREATE DEFINER=`root`@`localhost` TRIGGER `Pit_11_8B_bu` BEFORE UPDATE ON `Pit_11_8B`
	FOR EACH ROW A: BEGIN
		IF _GetVer() = '5.1.30' THEN LEAVE A; END IF;
	--	wartosc @session.trigger_lock rowna TRUE blokuje wykonanie triggera
		IF IFNULL(@session.trigger_lock, FALSE) THEN LEAVE A; END IF;
		-- 
		IF (NEW.OkresDo != OLD.OkresDo) THEN
			SELECT 
				ed.ID 
				INTO
				@NEW.e_Doc_Def_ID
			FROM `e_doc_def` ed
			WHERE 
				ed.doc_type = 'PIT-11' AND ed.doc_owner = 'gov.pl' AND NEW.OkresDo BETWEEN ed.DateFrom AND ed.DateTo ORDER BY ed.DateFrom DESC LIMIT 1
			;	
			SET NEW.e_Doc_Def_ID = @NEW.e_Doc_Def_ID;
		END IF;
    END;
$$

DROP TRIGGER IF EXISTS `Pit_11_8B_au`$$
CREATE DEFINER=`root`@`localhost` TRIGGER `Pit_11_8B_au` AFTER UPDATE ON `Pit_11_8B`
	FOR EACH ROW A: BEGIN
		IF _GetVer() = '5.1.30' THEN LEAVE A; END IF;
	--	wartosc @session.trigger_lock rowna TRUE blokuje wykonanie triggera
		IF IFNULL(@session.trigger_lock, FALSE) THEN LEAVE A; END IF;
		-- 
		IF (NEW.OkresDo != OLD.OkresDo) OR (NEW.e_Doc_Def_ID != OLD.e_Doc_Def_ID) THEN
			INSERT IGNORE INTO `e_doc_ng`
				(e_Doc_Def_ID, Src_GUID, Src_TableName, Src_No, Src_OsobaGUID, Src_Date, Descr)
				VALUES
				(NEW.e_Doc_Def_ID, NEW.GUID, 'Pit_11_8B', NEW.NazwaPlatnika, NEW.pracGUID, NEW.OkresOd, 'Pit 11 8B')
			ON DUPLICATE KEY
			UPDATE
				`e_Doc_Def_ID` = NEW.e_Doc_Def_ID
			;	
		END IF;
		
    END;
	
$$

DROP TRIGGER IF EXISTS `Pit_11_8B_ad`$$
CREATE DEFINER = `root`@`localhost` TRIGGER `Pit_11_8B_ad` AFTER DELETE ON `Pit_11_8B`
    FOR EACH ROW A: BEGIN
	# wartosc @session.trigger_lock rozna od NULL blokuje wykonanie triggera
	IF IFNULL(@session.trigger_lock, FALSE) THEN LEAVE A; END IF;
	
--	USUN
	DELETE FROM `e_doc_ng`
	WHERE	
		Src_GUID = OLD.GUID
		AND
		Src_TableName = 'Pit_11_8B'
	;

	/*
	DELETE FROM `dbobj_series`
	WHERE	
		GUIDDoc = OLD.GUID
		AND
		TableName = 'Pit_11_8B'
	;
	*/
END;
$$


DELIMITER ;

/*
====================================================================================================================================================================================
	NOTATNIK
====================================================================================================================================================================================
	



*/
