 DELIMITER $$

/* ************************************************************************************************************************************************************************
	BI
************************************************************************************************************************************************************************ */	
DROP TRIGGER IF EXISTS `ht_zwroty_p_agg_bi`$$
CREATE DEFINER=`root`@`localhost` TRIGGER `ht_zwroty_p_agg_bi` BEFORE INSERT ON `ht_zwroty_p_agg` FOR EACH ROW 
A: BEGIN
	IF _TriggerDisabled() THEN LEAVE A; END IF;

	
	
	SET NEW.ID = CONCAT('ZW_p_', MD5(CONCAT(NEW.Ng_ID, NEW.IdnTowr)));
	IF NEW.IsOferta	THEN
    --  obow. oferta np. ABRAVA (POLOMARKET)
        IF NEW.limit_zwrotu = 0 THEN
            SET NEW.ZW_Ilosc_Platna = 0;
            SET NEW.ZW_WartBrt_Platna = 0;
            SET NEW.ZW_WartNet_Platna = 0;
        ELSE
            SET NEW.ZW_Ilosc_Platna = LEAST(NEW.ZW_Ilosc, NEW.SP_Ilosc * NEW.limit_zwrotu/100);
            SET NEW.ZW_WartBrt_Platna = LEAST(NEW.ZW_WartBrt, NEW.SP_WartBrt * NEW.limit_zwrotu/100);
            SET NEW.ZW_WartNet_Platna = LEAST(NEW.ZW_WartNet, NEW.SP_WartNet * NEW.limit_zwrotu/100);
        END IF;
    ELSE
		SET NEW.ZW_Ilosc_Platna = NEW.ZW_Ilosc;
		SET NEW.ZW_WartBrt_Platna = NEW.ZW_WartBrt;
		SET NEW.ZW_WartNet_Platna = NEW.ZW_WartNet;
    END IF;
    /*
	IF	(NEW.limit_zwrotu = 0) OR (NEW.limit_zwrotu = 100) 
	THEN
		SET NEW.ZW_Ilosc_Platna = NEW.ZW_Ilosc;
		SET NEW.ZW_WartBrt_Platna = NEW.ZW_WartBrt;
		SET NEW.ZW_WartNet_Platna = NEW.ZW_WartNet;
	ELSEIF	(NEW.limit_zwrotu > 0) 
	THEN
		SET NEW.ZW_Ilosc_Platna = LEAST(NEW.ZW_Ilosc, NEW.SP_Ilosc * NEW.limit_zwrotu/100);
		SET NEW.ZW_WartBrt_Platna = LEAST(NEW.ZW_WartBrt, NEW.SP_WartBrt * NEW.limit_zwrotu/100);
		SET NEW.ZW_WartNet_Platna = LEAST(NEW.ZW_WartNet, NEW.SP_WartNet * NEW.limit_zwrotu/100);
	END IF;
    */
	
	SET NEW.Update_status = '';	--	zawsze
	
END;
$$

/* ************************************************************************************************************************************************************************
	AI
************************************************************************************************************************************************************************ */	
DROP TRIGGER IF EXISTS `ht_zwroty_p_agg_ai`$$
CREATE DEFINER=`root`@`localhost` TRIGGER `ht_zwroty_p_agg_ai` AFTER INSERT ON `ht_zwroty_p_agg` FOR EACH ROW 
A: BEGIN
	IF _TriggerDisabled() THEN LEAVE A; END IF;
END;
$$

/* ************************************************************************************************************************************************************************
	BU
************************************************************************************************************************************************************************ */	
DROP TRIGGER IF EXISTS `ht_zwroty_p_agg_bu`$$
CREATE DEFINER=`root`@`localhost` TRIGGER `ht_zwroty_p_agg_bu` BEFORE UPDATE ON `ht_zwroty_p_agg` FOR EACH ROW 
A: BEGIN
	IF _TriggerDisabled() THEN LEAVE A; END IF;
	
	IF	(!(NEW.limit_zwrotu <=> OLD.limit_zwrotu) OR !(NEW.ZW_Ilosc <=> OLD.ZW_Ilosc) OR !(NEW.SP_Ilosc <=> OLD.SP_Ilosc))
	THEN
        IF NEW.IsOferta	THEN
        --  obow. oferta np. ABRAVA (POLOMARKET)
            IF NEW.limit_zwrotu = 0 THEN
                SET NEW.ZW_Ilosc_Platna = 0;
                SET NEW.ZW_WartBrt_Platna = 0;
                SET NEW.ZW_WartNet_Platna = 0;
            ELSE
                SET NEW.ZW_Ilosc_Platna = LEAST(NEW.ZW_Ilosc, NEW.SP_Ilosc * NEW.limit_zwrotu/100);
                SET NEW.ZW_WartBrt_Platna = LEAST(NEW.ZW_WartBrt, NEW.SP_WartBrt * NEW.limit_zwrotu/100);
                SET NEW.ZW_WartNet_Platna = LEAST(NEW.ZW_WartNet, NEW.SP_WartNet * NEW.limit_zwrotu/100);
            END IF;
        ELSE
            SET NEW.ZW_Ilosc_Platna = NEW.ZW_Ilosc;
            SET NEW.ZW_WartBrt_Platna = NEW.ZW_WartBrt;
            SET NEW.ZW_WartNet_Platna = NEW.ZW_WartNet;
        END IF;
        /*
		IF	(NEW.limit_zwrotu = 0) OR (NEW.limit_zwrotu = 100) 
		THEN
			SET NEW.ZW_Ilosc_Platna = NEW.ZW_Ilosc;
			SET NEW.ZW_WartBrt_Platna = NEW.ZW_WartBrt;
			SET NEW.ZW_WartNet_Platna = NEW.ZW_WartNet;
		ELSEIF	(NEW.limit_zwrotu > 0) 
		THEN
			SET NEW.ZW_Ilosc_Platna = LEAST(NEW.ZW_Ilosc, NEW.SP_Ilosc * NEW.limit_zwrotu/100);
			SET NEW.ZW_WartBrt_Platna = LEAST(NEW.ZW_WartBrt, NEW.SP_WartBrt * NEW.limit_zwrotu/100);
			SET NEW.ZW_WartNet_Platna = LEAST(NEW.ZW_WartNet, NEW.SP_WartNet * NEW.limit_zwrotu/100);
		END IF;
        */
	END IF;
	
	SET NEW.Update_status = '';	--	zawsze
END;
$$

/* ************************************************************************************************************************************************************************
	AU
************************************************************************************************************************************************************************ */	
DROP TRIGGER IF EXISTS `ht_zwroty_p_agg_au`$$
CREATE DEFINER=`root`@`localhost` TRIGGER `ht_zwroty_p_agg_au` AFTER UPDATE ON `ht_zwroty_p_agg` FOR EACH ROW 
A: BEGIN
	IF _TriggerDisabled() THEN LEAVE A; END IF;
END;
$$

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


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

	UPDATE `oppo` p
	SET
		p.GUID_zwrotu = '',
		p.Rodzaj_typ = 0
	WHERE
		p.GUID_zwrotu = OLD.ID
	;


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


/* ************************************************************************************************************************************************************************
	Narzędzia 
************************************************************************************************************************************************************************ */	
DELIMITER ;

/*
====================================================================================================================================================================================
	NOTATNIK
====================================================================================================================================================================================
IF War1 THEN
	sql 1
ELSEIF War2 THEN
	sql 2
ELSEIF War3 THEN
	sql 3
ELSEIF War4 THEN
	sql 4
ELSE
	sql 2

END IF;

*/

/*  Zmiany CVS
$Log: ht_zwroty_p_agg.sql,v $
Revision 1.6  2026/01/23 10:35:19  jmam
Zgrywanie  stan na 23 I 2026 cz. II

Revision 1.5  2026/01/23 10:33:55  jmam
Zgrywanie  stan na 23 I 2026

*/
