 DELIMITER $$

/* ************************************************************************************************************************************************************************
	BI
************************************************************************************************************************************************************************ */	
DROP TRIGGER IF EXISTS `planprodpo_bi`$$
CREATE DEFINER=`root`@`localhost` TRIGGER `planprodpo_bi` BEFORE INSERT ON `planprodpo` FOR EACH ROW 
A: BEGIN

	IF _TriggerDisabled() THEN LEAVE A; END IF;
	IF @session.globals.Owner IN ('IZOHAN', 'POLAM') THEN
	/********************************************************************************** 
		IZOHAN - receptury
	**********************************************************************************/
		SET 
			@planprodpo_bu.NEW.IdnTowr = NULL,
			@planprodpo_bu.NEW.TowrSymb = '',
			@planprodpo_bu.NEW.TowrNazwa = '',
			@planprodpo_bu.NEW.TowrNazwa2 = ''
		;
		IF !(NEW.RecepturaGUID <=> '') AND NEW.Update_Status = 'EDIT.receptura' THEN
			SELECT 
				IFNULL(IdnTowr, ''), rn.Nazwa
			FROM `rcpo` rp 
			INNER JOIN `rcng` rn ON rn.GUID = rp.idnng 
			WHERE 
				rp.idnng = NEW.RecepturaGUID 
				AND rp.przychod = 1
				AND rp.priorytet = -1
				AND rn.widoczna_w_zam
			INTO
				@planprodpo_bu.NEW.IdnTowr,
				@planprodpo_bu.NEW.TowrNazwa2
			;
			SET NEW.IdnTowr = IFNULL(@planprodpo_bu.NEW.IdnTowr, '');
		END IF;
		
		IF !(NEW.IdnTowr <=> '') AND (NEW.Update_Status IN ('EDIT.receptura', 'EDIT.IdnTowr')) THEN
			SELECT 
				t.Symbol, t.Nazwa
			FROM `TowrNg` t
			WHERE 
				t.GUID = NEW.IdnTowr
			INTO
				@planprodpo_bu.NEW.TowrSymb,
				@planprodpo_bu.NEW.TowrNazwa
			;
			SET 
				NEW.TowrSymb = IFNULL(@planprodpo_bu.NEW.TowrSymb, ''),
				NEW.TowrNazwa = IFNULL(@planprodpo_bu.NEW.TowrNazwa, '')
			;
			IF NEW.Update_Status = 'EDIT.receptura' THEN
				SET NEW.TowrNazwa = 
					IF(REPLACE(TRIM(UCASE(@planprodpo_bu.NEW.TowrNazwa)), ' ', '') != REPLACE(TRIM(UCASE(@planprodpo_bu.NEW.TowrNazwa2)), ' ', ''),
						CONCAT(NEW.TowrNazwa, '(', IFNULL(@planprodpo_bu.NEW.TowrNazwa2, ''), ')'),
						NEW.TowrNazwa
					);
			END IF;
		END IF;
		
	END IF;
	
--	ZAWSZE	
	SET NEW.Update_Status = '';
	
END;
$$

/* ************************************************************************************************************************************************************************
	AI
************************************************************************************************************************************************************************ */	
DROP TRIGGER IF EXISTS `planprodpo_ai`$$
CREATE DEFINER=`root`@`localhost` TRIGGER `planprodpo_ai` AFTER INSERT ON `planprodpo` FOR EACH ROW 
A: BEGIN
	IF _TriggerDisabled() THEN LEAVE A; END IF;
	/*
	IF IFNULL(@session.trigger.planprodpo, FALSE) THEN 
		LEAVE A; 
	END IF;
	SET @session.trigger.planprodpo = TRUE;
	*/

	
	
	/*
	SET @session.trigger.planprodpo = FALSE;
	*/
END;
$$

/* ************************************************************************************************************************************************************************
	BU
************************************************************************************************************************************************************************ */	
DROP TRIGGER IF EXISTS `planprodpo_bu`$$
CREATE DEFINER=`root`@`localhost` TRIGGER `planprodpo_bu` BEFORE UPDATE ON `planprodpo` FOR EACH ROW 
A: BEGIN
	IF _TriggerDisabled() THEN LEAVE A; END IF;
	/*
	IF IFNULL(@session.trigger.planprodpo, FALSE) THEN 
		LEAVE A; 
	END IF;
	SET @session.trigger.planprodpo = TRUE;
	*/
SET @xcd = concat('!!!', NEW.Update_Status);
SET @xcd = '????';
	
	IF @session.globals.Owner IN ('IZOHAN', 'POLAM') THEN
	/********************************************************************************** 
		IZOHAN - receptury
	**********************************************************************************/
		SET 
			@planprodpo_bu.NEW.IdnTowr = NULL,
			@planprodpo_bu.NEW.TowrSymb = '',
			@planprodpo_bu.NEW.TowrNazwa = '',
			@planprodpo_bu.NEW.TowrNazwa2 = ''
		;
		
	--	Zmiana ilosci lub Ilosc_1_N
		IF NEW.Update_Status LIKE 'Set.Ilosc%' AND (IFNULL(NEW.RecepturaGUID, '') != '') THEN
			SET @Calc.Masa.ilosc_w_mieszalniku = 0, @Calc.Prod.Ilosc = 0;
			SELECT 
				@p.Ilosc := IFNULL(rp.Ilosc, 0) AS Ilosc, 
				@p.ilosc_w_mieszalniku := IFNULL(rp.ilosc_w_mieszalniku, 0) AS ilosc_w_mieszalniku, 
				@p.ilosc_w_mieszalniku * NEW.Ilosc_1_N/@p.Ilosc AS Factor 
			FROM `planprodng` pn, `rcpo` rp 
			WHERE 
				pn.GUID = NEW.IdnNg
				AND rp.IdnNg = NEW.RecepturaGUID
				AND rp.Res_Type = '1..N' 
				AND IFNULL(rp.AreaGUID, '') IN ('', pn.AreaGUID)
			ORDER BY (IFNULL(rp.AreaGUID, '') = pn.AreaGUID) DESC
			LIMIT 1
			INTO 
				@Calc.Masa.Ilosc, @Calc.Masa.ilosc_w_mieszalniku, @Calc.Masa.Factor
			;
			SELECT 
				rp.Ilosc
				-- , rn.GUID, rn.Nazwa, rn.data_receptury, rn.IDN
			-- ,rp.*
			FROM `rcpo` rp 
			INNER JOIN `rcng` rn ON rn.GUID = rp.idnng 
			WHERE 
				rp.IdnNg = NEW.RecepturaGUID
				AND rp.IdnTowr = NEW.IdnTowr
				AND rp.przychod = 1
				AND rp.priorytet = -1
				AND rn.widoczna_w_zam
			INTO
				@Calc.Prod.Ilosc
			;
			IF @Calc.Masa.ilosc_w_mieszalniku > 0 AND @Calc.Prod.Ilosc > 0 THEN
			--	Jest mieszalnik zatem oblicz Ilosc_1_N lub Ilosc
				IF NEW.Update_Status = 'Set.Ilosc' THEN
					SET @Calc.Prod.FinalIlosc = NEW.Ilosc;
					SET NEW.Ilosc_1_N = ROUND(@Calc.Masa.Ilosc*(ROUND(@Calc.Prod.FinalIlosc, 0)/@Calc.Prod.Ilosc)/@Calc.Masa.ilosc_w_mieszalniku, 0);
				ELSEIF NEW.Update_Status = 'Set.Ilosc_1_N' THEN
					SET @Calc.Prod.FinalIlosc = @Calc.Prod.Ilosc * @Calc.Masa.Factor;
					SET NEW.Ilosc = @Calc.Prod.FinalIlosc;
				END IF;	
			END IF;
		END IF;
		
		SET @.NEW.Update.planprodpo_rcpo = '';
		IF !(NEW.RecepturaGUID <=> OLD.RecepturaGUID) AND NEW.Update_Status = 'EDIT.receptura' THEN
			SELECT 
				IFNULL(IdnTowr, ''), rn.Nazwa
			FROM `rcpo` rp 
			INNER JOIN `rcng` rn ON rn.GUID = rp.idnng 
			WHERE 
				rp.idnng = NEW.RecepturaGUID 
				AND rp.przychod = 1
				AND rp.priorytet = -1
				AND rn.widoczna_w_zam
			LIMIT 1	
			INTO
				@planprodpo_bu.NEW.IdnTowr,
				@planprodpo_bu.NEW.TowrNazwa2
			;
			SET NEW.IdnTowr = IFNULL(@planprodpo_bu.NEW.IdnTowr, '');
			
			SET @.NEW.Update.planprodpo_rcpo = 'from.rcpo';
		END IF;
		
		IF !(NEW.IdnTowr <=> OLD.IdnTowr) AND (NEW.Update_Status IN ('EDIT.receptura', 'EDIT.IdnTowr')) THEN
			SELECT 
				t.Symbol, t.Nazwa
			FROM `TowrNg` t
			WHERE 
				t.GUID = NEW.IdnTowr
			INTO
				@planprodpo_bu.NEW.TowrSymb,
				@planprodpo_bu.NEW.TowrNazwa
			;
			SET 
				NEW.TowrSymb = IFNULL(@planprodpo_bu.NEW.TowrSymb, ''),
				NEW.TowrNazwa = IFNULL(@planprodpo_bu.NEW.TowrNazwa, '')
			;
			IF NEW.Update_Status = 'EDIT.receptura' THEN
				SET NEW.TowrNazwa = 
					IF(REPLACE(TRIM(UCASE(@planprodpo_bu.NEW.TowrNazwa)), ' ', '') != REPLACE(TRIM(UCASE(@planprodpo_bu.NEW.TowrNazwa2)), ' ', ''),
						CONCAT(NEW.TowrNazwa, '(', IFNULL(@planprodpo_bu.NEW.TowrNazwa2, ''), ')'),
						NEW.TowrNazwa
					);
			END IF;
			SET @.NEW.Update.planprodpo_rcpo = 'from.rcpo';
		END IF;

		IF NEW.Update_Status = 'Update.planprodpo_rcpo.from.rcpo' THEN
			SET @.NEW.Update.planprodpo_rcpo = 'from.rcpo';
		END IF;

		IF NEW.Update_Status = 'Update.planprodpo_rcpo.from.prev.planprodpo' THEN
			SET @.NEW.Update.planprodpo_rcpo = 'from.prev.planprodpo';
		END IF;
		
		-- IF !(NEW.Plan_EndTime <=> OLD.Plan_EndTime) AND 
		IF NEW.Update_Status = 'Set.TIME' THEN
			SET
				@Time = 
				IFNULL((
					SELECT SUM(IFNULL(r.Ilosc, 0)) FROM `planprodpo_res` r WHERE r.pGUID = NEW.GUID AND r.Res_Type = 'Time'
				), 0)
			;
			SET
				NEW.Plan_EndTime = DATE_ADD(NEW.Plan_StartTime, INTERVAL @Time MINUTE)
			;
			CALL planprodpo_time_line_create_from_planprodpo('', NEW.GUID, '', '', '');
		END IF;
		
		SET NEW.GuidZleceniaPo = IFNULL(NEW.GuidZleceniaPo, '');
		
		IF NEW.Update_Status = 'Set.Status_Realizacji' THEN
			IF NEW.GuidZleceniaPo = '' THEN
				SET
					NEW.Status_Realizacji = 0
				;
			ELSE
				SET @session.trigger.planprodpo.n.GUID = IFNULL((SELECT p.IdnNg FROM `zmpo` p WHERE p.GUID = NEW.GuidZleceniaPo), '');
				IF @session.trigger.planprodpo.n.GUID != '' THEN
				/*
				Moze kiedys tak zrobimy ale....
					CALL _zmng_Update_zrealizowane('Ustaw.@_zmng_Realizuj.IlePozDoRealizacji', @session.trigger.planprodpo.n.GUID, '', '');				
				--	teraz w zmiennych  @_zmng_Realizuj.n.ilepoz, @_zmng_Realizuj.n.IlepozZreal, @_zmng_Realizuj.IlePozDoRealizacji są wartości
					SET
						@session.trigger.planprodpo.n.Count = @_zmng_Realizuj.n.ilepoz,
						@session.trigger.planprodpo.n.zrealizowane = @_zmng_Realizuj.n.IlepozZreal
					;
				Na razie zostawiamy tak...
				*/
					SELECT
						-- _VarInt(COUNT(*)) AS pCount,
						_VarInt(IFNULL(SUM(IFNULL(p.ilosc > 0, 0)), 0)) AS pCount,
						_VarInt(IFNULL(SUM(IFNULL(p.zrealizowano = p.ilosc AND p.ilosc > 0, 0)), 0)) AS pzrealizowane
					FROM `zmpo` p 
					WHERE 
						p.IdnNg = @session.trigger.planprodpo.n.GUID
					INTO
						@session.trigger.planprodpo.n.Count,
						@session.trigger.planprodpo.n.zrealizowane
					;
					SET
						NEW.Status_Realizacji = 
							IF(@session.trigger.planprodpo.n.Count = @session.trigger.planprodpo.n.zrealizowane,
								1,	--	100%
								IF((@session.trigger.planprodpo.n.Count = 0) OR (@session.trigger.planprodpo.n.zrealizowane = 0),
									0,
									@session.trigger.planprodpo.n.zrealizowane/@session.trigger.planprodpo.n.Count
								)
							)
					;
				END IF;
			END IF;
			
		END IF;
		
	END IF;
	
--	ZAWSZE	
	SET NEW.Update_Status = '';
	
	/*
	SET @session.trigger.planprodpo = FALSE;
	*/
END;
$$

/* ************************************************************************************************************************************************************************
	AU
************************************************************************************************************************************************************************ */	
DROP TRIGGER IF EXISTS `planprodpo_au`$$
CREATE DEFINER=`root`@`localhost` TRIGGER `planprodpo_au` AFTER UPDATE ON `planprodpo` FOR EACH ROW 
A: BEGIN
	IF _TriggerDisabled() THEN LEAVE A; END IF;
	/*
	IF IFNULL(@session.trigger.planprodpo, FALSE) THEN 
		LEAVE A; 
	END IF;
	SET @session.trigger.planprodpo = TRUE;
	*/
	
	IF @session.globals.Owner IN ('IZOHAN', 'POLAM') THEN
	/********************************************************************************** 
		IZOHAN - receptury
	**********************************************************************************/
		set @xcd = '0';
		IF @.NEW.Update.planprodpo_rcpo LIKE 'from.%' THEN
		set @xcd = '1';
			IF @.NEW.Update.planprodpo_rcpo = 'from.rcpo' THEN
				DELETE FROM `planprodpo_rcpo` WHERE pGUID = NEW.GUID;
				INSERT IGNORE INTO `planprodpo_rcpo`
					(pGUID, rcng_GUID_merge, Time_Res, rcpoGUID, ilosc, IloscIsConst)
					SELECT
						NEW.GUID, p.idnng_merge AS rcng_GUID_merge, p.Time_Res, p.GUID AS rcpoGUID, p.ilosc, p.IloscIsConst
					FROM `planprodng` pn, `rcpo` p
					WHERE
						p.idnng = NEW.RecepturaGUID
						AND 
						pn.GUID = NEW.IdnNg
						AND
						IFNULL(p.Time_Res, '') IN ('BEGIN','DURING','END') 
						AND 
						p.AreaGUID IN ('', pn.AreaGUID)
				;
			END IF;

			IF @.NEW.Update.planprodpo_rcpo = 'from.prev.planprodpo' THEN
				SET @.planprodpo.GUID = NULL;
				SELECT 
					pp.GUID 
				FROM `planprodpo` pp
				WHERE 
					pp.IDN < NEW.IDN 
					AND 
					pp.RecepturaGUID = NEW.RecepturaGUID
				ORDER BY pp.IDN DESC
				LIMIT 1
				INTO 
					@.planprodpo.GUID
				;
				IF @.planprodpo.GUID IS NOT NULL THEN
					SET @.planprodpo.GUID.Jestem = 999;
					DROP TEMPORARY TABLE IF EXISTS `t_planprodpo_rcpo_ins`;
					CREATE TEMPORARY TABLE `t_planprodpo_rcpo_ins` 
						SELECT 
							p.pGUID, p.rcng_GUID_merge, p.Time_Res, p.rcpoGUID, p.ilosc, p.IloscIsConst
						FROM `planprodpo_rcpo` p
						WHERE
							p.pGUID = @.planprodpo.GUID
					;
					SET @.planprodpo.GUID.Jestem = 2;
					DELETE FROM `planprodpo_rcpo` WHERE pGUID = NEW.GUID;
					SET @.planprodpo.GUID.Jestem = 3;
					INSERT IGNORE INTO `planprodpo_rcpo`
						(pGUID, rcng_GUID_merge, Time_Res, rcpoGUID, ilosc, IloscIsConst)
						SELECT
							NEW.GUID, t.rcng_GUID_merge, t.Time_Res, t.rcpoGUID, t.ilosc, t.IloscIsConst
						FROM `t_planprodpo_rcpo_ins` t
					;
					SET @.planprodpo.GUID.Jestem = 4;
					-- DROP TEMPORARY TABLE IF EXISTS `t_planprodpo_rcpo_ins`;
				END IF;
			END IF;
			
			INSERT IGNORE INTO `planprodpo_rcpo`
				(pGUID, rcng_GUID_merge, Time_Res, rcpoGUID, ilosc)
				VALUES
				(NEW.GUID, NULL, 'BEGIN', NULL, 0)
			;
			INSERT IGNORE INTO `planprodpo_rcpo`
				(pGUID, rcng_GUID_merge, Time_Res, rcpoGUID, ilosc)
				VALUES
				(NEW.GUID, NULL, 'DURING', NULL, 0)
			;
			INSERT IGNORE INTO `planprodpo_rcpo`
				(pGUID, rcng_GUID_merge, Time_Res, rcpoGUID, ilosc)
				VALUES
				(NEW.GUID, NULL, 'END', NULL, 0)
			;
			
		END IF;
		
		SET @.NEW.Update.planprodpo_rcpo1 = @.NEW.Update.planprodpo_rcpo;
		SET @.NEW.Update.planprodpo_rcpo = '';
		
	END IF;

	
	/*
	SET @session.trigger.planprodpo = FALSE;
	*/
END;
$$

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


/* ************************************************************************************************************************************************************************
	AD
************************************************************************************************************************************************************************ */	
DROP TRIGGER IF EXISTS `planprodpo_ad`$$
CREATE DEFINER = `root`@`localhost` TRIGGER `planprodpo_ad` AFTER DELETE ON `planprodpo` FOR EACH ROW 
A: BEGIN
	IF _TriggerDisabled() THEN LEAVE A; END IF;
	
	IF NOT IFNULL(@.planprodpo.Clear.Old, FALSE) THEN
	--	czysc pr_krpo_GUID w zmpo ALE!!! nie dla czyszcenia starych arch. rekordów - w takim przypadku nie czyść
		UPDATE `zmpo` p
		SET
			p.pr_krpo_GUID = ''
		WHERE
			p.pr_krpo_GUID = OLD.GUID
		;
	END IF;
	DELETE FROM `planprodpo_res`
	WHERE
		pGUID = OLD.GUID
	;
	DELETE FROM `planprodpo_time_line`
	WHERE
		pGUID = OLD.GUID
	;
	DELETE FROM `planprodpo_rcpo`
	WHERE
		pGUID = OLD.GUID
	;
END;
$$


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

/*
DROP PROCEDURE IF EXISTS `planprodpo_tools`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `planprodpo_tools`(
	IN _MODE VARCHAR(150),
	IN _pGUID VARCHAR(38),	
	IN _Param_A VARCHAR(250),	
	IN _Param_B VARCHAR(250),	
	IN _Param_C VARCHAR(250)	
	-- IN _Param_D VARCHAR(250)	
)
S_planprodpo_tools: BEGIN
	
	IF _MODE LIKE 'Update.planprodpo_rcpo.from.%' THEN
	/-* *************************************************************************************************************************************************************
		Zwraca dla okienka pozycji planu liste czasochlonnych pozycji receptur
		CALL planprodpo_tools('Update.planprodpo_rcpo.from.rcpo', 'pGUID', '', '', '');			
		CALL planprodpo_tools('Update.planprodpo_rcpo.from.prev.planprodpo', 'pGUID', '', '', '');			
	**************************************************************************************************************************************************************-/
		UPDATE `planprodpo` 
		SET
			Update_Status = _MODE
		WHERE
			GUID = _pGUID
		;
	END IF;
	
END$$
*/

		DROP PROCEDURE IF EXISTS `planprodpo_time_line_create_from_planprodpo`$$
		CREATE DEFINER=`root`@`localhost` PROCEDURE `planprodpo_time_line_create_from_planprodpo`(
			IN _MODE VARCHAR(150),
			IN _pGUID VARCHAR(38),	
			IN _Param_A VARCHAR(250),	
			IN _Param_B VARCHAR(250),	
			IN _Param_C VARCHAR(250)	
			-- IN _Param_D VARCHAR(250)	
		)
		S_planprodpo_time_line_create_from_planprodpo: BEGIN
		
		-- 	Naliczanie slotow godzinnych
			SET
				@_StartTime =
				IFNULL((
					SELECT Plan_StartTime FROM `planprodpo` p WHERE p.GUID = _pGUID
				), 0),
				@TimeM = ROUND(
				IFNULL((
					SELECT SUM(IFNULL(r.Ilosc, 0)) FROM `planprodpo_res` r WHERE r.pGUID = _pGUID AND r.Res_Type = 'Time' AND r.Time_Res = 'DURING'
				), 0)),
				@TimeM_BEGIN = ROUND(
				IFNULL((
					SELECT SUM(IFNULL(r.Ilosc, 0)) FROM `planprodpo_res` r WHERE r.pGUID = _pGUID AND r.Res_Type = 'Time' AND r.Time_Res = 'BEGIN'
				), 0)),
				@TimeM_END = 
				IFNULL((
					SELECT SUM(IFNULL(r.Ilosc, 0)) FROM `planprodpo_res` r WHERE r.pGUID = _pGUID AND r.Res_Type = 'Time' AND r.Time_Res = 'END'
				), 0),
				@Electr = ROUND(
				IFNULL((
					SELECT SUM(IFNULL(r.Ilosc, 0)) FROM `planprodpo_res` r WHERE r.pGUID = _pGUID AND r.Res_Type = 'Electr' AND r.Time_Res = 'DURING'
				), 0)),
				@Electr_BEGIN = 
				IFNULL((
					SELECT SUM(IFNULL(r.Ilosc, 0)) FROM `planprodpo_res` r WHERE r.pGUID = _pGUID AND r.Res_Type = 'Electr' AND r.Time_Res = 'BEGIN'
				), 0),
				@Electr_END = ROUND(
				IFNULL((
					SELECT SUM(IFNULL(r.Ilosc, 0)) FROM `planprodpo_res` r WHERE r.pGUID = _pGUID AND r.Res_Type = 'Electr' AND r.Time_Res = 'END'
				), 0)),
				@Gas = ROUND(
				IFNULL((
					SELECT SUM(IFNULL(r.Ilosc, 0)) FROM `planprodpo_res` r WHERE r.pGUID = _pGUID AND r.Res_Type = 'Gas' AND r.Time_Res = 'DURING'
				), 0)),
				@Gas_BEGIN = ROUND(
				IFNULL((
					SELECT SUM(IFNULL(r.Ilosc, 0)) FROM `planprodpo_res` r WHERE r.pGUID = _pGUID AND r.Res_Type = 'Gas' AND r.Time_Res = 'BEGIN'
				), 0)),
				@Gas_END = ROUND(
				IFNULL((
					SELECT SUM(IFNULL(r.Ilosc, 0)) FROM `planprodpo_res` r WHERE r.pGUID = _pGUID AND r.Res_Type = 'Gas' AND r.Time_Res = 'END'
				), 0))
			;
			SET 
				@Electr_per_min = @Electr/@TimeM,
				@Electr_BEGIN_per_min = @Electr_BEGIN/@TimeM_BEGIN,
				@Electr_END_per_min = @Electr_END/@TimeM_END,
				@Gas_per_min = @Gas/@TimeM,
				@Gas_BEGIN_per_min = @Gas_BEGIN/@TimeM_BEGIN,
				@Gas_END_per_min = @Gas_END/@TimeM_END
			;
			
			DROP TEMPORARY TABLE IF EXISTS `t_planprodpo_timeline_min`;
			CREATE TEMPORARY TABLE `t_planprodpo_timeline_min` (
			  `pGUID` VARCHAR(200) NOT NULL DEFAULT '' COMMENT 'Klucz glowny',
			  `_min` INTEGER NOT NULL DEFAULT 0,
			  `_StartTime` DATETIME NOT NULL DEFAULT '0000-00-00 00:00',
			  `_hour` INTEGER NOT NULL DEFAULT 0,
			  `_is_Const` INTEGER NOT NULL DEFAULT 0,
			  `electr` DECIMAL(20,4) NOT NULL DEFAULT 0,
			  `gas` DECIMAL(20,4) NOT NULL DEFAULT 0,
			  PRIMARY KEY (`_min`),
			  KEY `_hour` (`_hour`),
			  KEY `_StartTime` (`_StartTime`)
			)
			;
			
			SET 
				@I := @TimeM_BEGIN,
				@_Min_C := 0
			;
			WHILE @I > 0 DO
				INSERT IGNORE INTO `t_planprodpo_timeline_min`
				SET
					pGUID = _pGUID,
					_StartTime = DATE_ADD(@_StartTime, INTERVAL @_Min_C MINUTE),
					_Min = @_Min_C,
					electr = @Electr_BEGIN_per_min,
					gas = @Gas_BEGIN_per_min,
					_is_Const = 1
				;
				SET 
					@I := @I - 1,
					@_Min_C := @_Min_C + 1
				;
			END WHILE;
			
			SET 
				@I := @TimeM
			;
			
			WHILE @I > 0 DO
				INSERT IGNORE INTO `t_planprodpo_timeline_min`
				SET
					pGUID = _pGUID,
					_StartTime = DATE_ADD(@_StartTime, INTERVAL @_Min_C MINUTE),
					_Min = @_Min_C,
					electr = @Electr_per_min,
					gas = @gas_per_min,
					_is_Const = 0
				;
				SET 
					@I := @I - 1,
					@_Min_C := @_Min_C + 1
				;
			END WHILE;
			
			UPDATE `t_planprodpo_timeline_min` t
			SET
				t._hour = HOUR(t._StartTime)
			;
			
			SET 
				@I := @TimeM_END
				-- @_Min_C := @TimeM,
				-- @_StartTime = DATE_ADD(@_StartTime, INTERVAL @TimeM MINUTE)
			;
			WHILE @I > 0 DO
				INSERT IGNORE INTO `t_planprodpo_timeline_min`
				SET
					pGUID = _pGUID,
					_StartTime = DATE_ADD(@_StartTime, INTERVAL @_Min_C MINUTE),
					_Min = @_Min_C,
					electr = @Electr_END_per_min,
					gas = @Gas_END_per_min,
					_is_Const = 1
				;
				SET 
					@I := @I - 1,
					@_Min_C := @_Min_C + 1
				;
			END WHILE;
			
			UPDATE `t_planprodpo_timeline_min` t
			SET
				t._hour = HOUR(t._StartTime)
			WHERE
				_is_Const = 1
			;
			DELETE FROM `planprodpo_time_line` WHERE pGUID = _pGUID;
			SET @H = 0;
			INSERT IGNORE INTO `planprodpo_time_line`
			(`pGUID`, `StartTime`, `hour_lp`, `hour`, `electr`, `gas`, `Minutes_total`, `Minutes_Norm`, `Minutes_Const`)
				SELECT 
					t.pGUID,
					_VarDATETIME(CONCAT(DATE(_StartTime), ' ', _Hour,':00:00')) AS StartTime,
					(@H := @H + 1) AS hour_lp,
					_hour AS `hour`,
					SUM(electr) AS electr,
					SUM(gas) AS gas,
					COUNT(*) AS Minutes_total,
					SUM(_is_Const = 0) AS Minutes_Norm,
					SUM(_is_Const = 1) AS Minutes_Const
					-- t.* 
				FROM `t_planprodpo_timeline_min` t
				GROUP BY StartTime
			;			
		
			DROP TEMPORARY TABLE IF EXISTS `t_planprodpo_timeline_min`;
		END$$


DELIMITER ;


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


*/
