DELIMITER $$

/* ************************************************************************************************************************************************************************
	BI
************************************************************************************************************************************************************************ */	
DROP TRIGGER IF EXISTS `zmng_bi`$$
CREATE DEFINER=`root`@`localhost` TRIGGER `zmng_bi` BEFORE INSERT ON `zmng` FOR EACH ROW 
A: BEGIN
	IF _TriggerDisabled() THEN LEAVE A; END IF;
	
	IF NEW.terminrealizacji_plan IS NULL THEN
		SET NEW.terminrealizacji_plan = NEW.terminrealizacji;
	END IF;
	
END;
$$

/* ************************************************************************************************************************************************************************
	AI
************************************************************************************************************************************************************************ */	
DROP TRIGGER IF EXISTS `zmng_ai`$$
CREATE DEFINER=`root`@`localhost` TRIGGER `zmng_ai` AFTER INSERT ON `zmng` FOR EACH ROW 
A: BEGIN
	IF _TriggerDisabled() THEN LEAVE A; END IF;
--	INIT Lock trigger zmienna @session.trigger.xxx
	--	IF IFNULL(@session.trigger.xxxx, FALSE) THEN LEAVE A; END IF;
	--	SET @session.trigger.xxxx = TRUE;


	CALL _modify_ht_kontrah_info('refresh.for.zmng', NEW.KontrGUID, '', '', '', '');		
	
--	FINAL UnLock trigger zmienna @session.trigger.xxx
	--	SET @session.trigger.xxxx = FALSE;
END;
$$

/* ************************************************************************************************************************************************************************
	BU
************************************************************************************************************************************************************************ */	
DROP TRIGGER IF EXISTS `zmng_bu`$$
CREATE DEFINER=`root`@`localhost` TRIGGER `zmng_bu` BEFORE UPDATE ON `zmng` FOR EACH ROW 
A: BEGIN
	IF _TriggerDisabled() THEN LEAVE A; END IF;

	IF @session.globals.Owner IN ('CDG','MPGK') THEN	
	/********************************************************************************** 
		Gazownicy
	**********************************************************************************/
		IF ABS(DATEDIFF(NEW.TerminRealizacji, NEW.TerminRealizacji_Plan)) > 3 THEN
			SET NEW.terminrealizacji_plan = NEW.terminrealizacji;
		END IF;
	END IF;
	
	
END;

$$

/* ************************************************************************************************************************************************************************
	AU
************************************************************************************************************************************************************************ */	
DROP TRIGGER IF EXISTS `zmng_au`$$
CREATE DEFINER=`root`@`localhost` TRIGGER `zmng_au` AFTER UPDATE ON `zmng` FOR EACH ROW 
A: BEGIN
	IF _TriggerDisabled() THEN LEAVE A; END IF;
--	INIT Lock trigger zmienna @session.trigger.xxx
	--	IF IFNULL(@session.trigger.xxxx, FALSE) THEN LEAVE A; END IF;
	--	SET @session.trigger.xxxx = TRUE;

	IF (!(NEW.ilepozzreal <=> OLD.ilepozzreal) OR !(NEW.KontrGUID <=> OLD.KontrGUID) OR !(NEW.terminrealizacji_plan <=> OLD.terminrealizacji_plan))
		AND NEW.rodzaj in (27, 52) AND (@session.globals.Owner in ('CDG', 'MPGK')) THEN
	-- IF ((NEW.ilepozzreal != OLD.ilepozzreal) OR (NEW.KontrGUID != OLD.KontrGUID)) and NEW.rodzaj in (27,52) THEN
	--	update `ht_kontrah_info`
		IF OLD.KontrGUID IS NOT NULL THEN
			-- bylo CALL _modify_ht_kontrah_info('refresh.zam.kontrah.zam_prev_date', OLD.KontrGUID, '', '', '', '');		
			CALL _modify_ht_kontrah_info('refresh.for.zmng', OLD.KontrGUID, '', '', '', '');		
		END IF;
		IF NEW.KontrGUID IS NOT NULL THEN
			-- bylo CALL _modify_ht_kontrah_info('refresh.zam.kontrah.zam_prev_date', NEW.KontrGUID, '', '', '', '');		
			CALL _modify_ht_kontrah_info('refresh.for.zmng', NEW.KontrGUID, '', '', '', '');		
		END IF;
	END IF;

	IF (!(NEW.IlepozZreal <=> OLD.IlepozZreal) OR !(NEW.ilepoz <=> OLD.ilepoz)) 
		AND NEW.rodzaj in (73) 
		AND (@session.globals.Owner in ('IZOHAN', 'IZOHAN')) 
	THEN
		UPDATE `planprodpo` pp, `zmpo` p
		SET
			pp.Status_Realizacji = -1
		WHERE
			p.IdnNg = NEW.GUID
			AND IFNULL(pp.GuidZleceniaPo, '') != ''
			AND pp.GuidZleceniaPo = p.GUID
		;
	END IF;


	IF @session.globals.Owner = 'IZOHAN' THEN	
	/********************************************************************************** 
		IZOHAN - EBI
	**********************************************************************************/
		IF !(NEW.Status <=> OLD.Status) AND NEW.GUID LIKE 'ZM.ebi%'
		THEN
			INSERT INTO ebi.`C_INT_MESSAGE_HEAD_TAB` (MSG_TYPE, SRC_ID) SELECT 'INFO', NEW.GUID;
		END IF;
	END IF;

	
--	FINAL UnLock trigger zmienna @session.trigger.xxx
	--	SET @session.trigger.xxxx = FALSE;
	
END;
$$

/* ************************************************************************************************************************************************************************
	BD
************************************************************************************************************************************************************************ */	


/* ************************************************************************************************************************************************************************
	AD
************************************************************************************************************************************************************************ */	
DROP TRIGGER IF EXISTS `zmng_ad`$$
CREATE DEFINER = `root`@`localhost` TRIGGER `zmng_ad` AFTER DELETE ON `zmng` FOR EACH ROW 
A: BEGIN
	IF _TriggerDisabled() THEN LEAVE A; END IF;
--	INIT Lock trigger zmienna @session.trigger.xxx
	--	IF IFNULL(@session.trigger.xxxx, FALSE) THEN LEAVE A; END IF;
	--	SET @session.trigger.xxxx = TRUE;



--	FINAL UnLock trigger zmienna @session.trigger.xxx
	--	SET @session.trigger.xxxx = FALSE;
END;
$$


/* ************************************************************************************************************************************************************************
	Narzędzia 
************************************************************************************************************************************************************************ */	


DELIMITER ;


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

*/

