 /*!40101 SET NAMES utf8 */;
DELIMITER $$

DROP PROCEDURE IF EXISTS `fr_ht_zp_rec`$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `fr_ht_zp_rec`(
	IN _Symbol VARCHAR(38),
	IN _GUID VARCHAR(38),
	IN _Param_A VARCHAR(50),
	IN _Param_B VARCHAR(50),
	IN _Param_C VARCHAR(50)
)
BEGIN
	# REC1 - procedura podajaca na wydruk ZP zalecenia technologiczne z receptury (Izohan)
	# REC2 - procedura podajaca na wydruk WZ pozycje materialowe z receptury (lista pakowa - Simex)
	# ZP1 - podaje na wydruk ZP zgrupowane i posortowane towary (wg symbolu) z podsumowaniem ilosci,
	#       chodzi o to, zeby w samym dokumencie zostawic kolejnosc wprowadzania pozycji dla roznych zlecen
	#       (Polaszek)
	# ZP2 - precyzyjne wyliczenie skladnikow (Amicus)
	# REC_N, REC... - wydruk receptury (naglowek i pozycje w kilku zestawach)

	
	SET @Symbol = UPPER(_Symbol);
	SET @GUID = _GUID;
	
	SET @Param_A = _Param_A;
	SET @Param_B = _Param_B;
	SET @Param_C = _Param_C;
	
/* *******************************************************************************
	REC1
	CALL fr_ht_zp_rec('REC1','zmng395dde01ec04e09dcc64c2e6d2fd3a1d','1','','');
******************************************************************************* */
	IF _Symbol = 'REC1' THEN

	drop table if exists t2;
	create temporary table t2
	(`zalecenia_techn` varchar(255) NOT NULL DEFAULT '') 
	ENGINE=MyISAM DEFAULT CHARSET=cp1250;
	
	
	IF (@Param_A IN ('0','')) THEN
		SET @Iter = 1;
		WHILE @Iter<21 DO
			SET @Iter = @Iter + 1;
			insert into t2
			select replace(substring_index(substring_index(Zalecenia_techn,char(13),@Iter),char(13),-1),char(10),'') as linia
			from rcng 
			where guid in (select recepturaGUID from zmpo where idnNG = _GUID and transza_zamow= @Param_A and recepturaGUID<>''  AND magtyp=0)
			;
		END WHILE;
	ELSE
		SET @Iter = 1;
		WHILE @Iter<21 DO
			SET @Iter = @Iter + 1;
			insert into t2
			select replace(substring_index(substring_index(Zalecenia_techn,char(13),@Iter),char(13),-1),char(10),'') as linia
			from rcng 
			where guid in (select recepturaGUID from zmpo where idnNG = _GUID  and recepturaGUID<>''  AND magtyp= 2)
			;
		END WHILE;
	END IF;
	
	(select distinct
	substring_index(Zalecenia_techn,';',1) as par1,
	substring_index(substring_index(Zalecenia_techn,';',2),';',-1) as par2,
	substring_index(Zalecenia_techn,';',-1) as par3
	from t2 where zalecenia_techn<>' ; ; ' 
	)
	union
	(
	select "" as par1, "" as par2, "" as par3
	)
	;

	drop table if exists t2;

	END IF;
/* *******************************************************************************
	REC2
******************************************************************************* */

	IF _Symbol = 'REC2' THEN
	
	drop table if exists t3;
	create temporary table t3
	select distinct
	p.idntowr, 
	t.symbol,t.nazwa,
	rr.ilosc*p.ilosc/rp.ilosc as p_ilosc,
	j.nazwa as p_jednostka
	from oppo p
	inner join rcpo rp on rp.idntowr=p.idntowr and rp.przychod
	inner join rcpo rr on rr.idnng=rp.idnng and not rr.przychod
	inner join TowrNg t on t.guid=rr.idntowr and t.wyroznik='D'
	inner join Jednostki j on rr.jednostka=j.idn
	where p.idnoper=@GUID;
	
	select 
	p.GUID,
	p.idntowr,
	p.towrsymb, p.towrnazwa, p.ilosc, j.nazwa as jednostka,
	p.cenanet, p.wartnet,
	t.symbol as p_symbol,
	t.nazwa as p_nazwa,
	t.p_ilosc, t.p_jednostka
	from oppo p
	inner join Jednostki j on j.idn=p.jednostka
	left join t3 t on t.idntowr=p.idntowr
	where p.idnoper = _GUID
	order by p.towrsymb, t.nazwa;

	drop table if exists t3;

	END IF;


/* *******************************************************************************
	ZP1
******************************************************************************* */

	IF _Symbol = 'ZP1' THEN
			
	select 
	p.towrsymb, p.towrnazwa, sum(p.ilosc) as ilosc, j.nazwa as jednostka
	from zmpo p
	inner join Jednostki j on j.idn=p.jednostka
	where p.idnNG = _GUID and p.magtyp=2
	group by p.towrsymb
	order by p.towrsymb;

	END IF;	



/* *******************************************************************************
	ZP2
******************************************************************************* */

	IF _Symbol = 'ZP2' THEN
		BEGIN
			DECLARE _jedn_wyrobu VARCHAR(5);
			DECLARE _waga_j_wyrobu DOUBLE(16,4);
			DECLARE _waga_wyrobu DOUBLE(16,4);
			DECLARE _ilosc_wyrobu DOUBLE(18,6);
			DECLARE _symbol_wyrobu VARCHAR(70);
			DECLARE _strona VARCHAR(70);
			
			select j.nazwa as jednostka, t.waga, p.waga, p.ilosc, p.towrsymb,t.info2
			INTO _jedn_wyrobu, _waga_j_wyrobu, _waga_wyrobu, _ilosc_wyrobu, _symbol_wyrobu, _strona
			from zmpo p
			inner join Jednostki j on j.idn=p.jednostka
			inner join TowrNg t on t.GUID=p.idntowr
			where p.idnNG = _GUID and p.magtyp=0;
		
	
		IF _jedn_wyrobu <> 'LT' THEN
			select 
			_symbol_wyrobu as symbol_wyrobu, _ilosc_wyrobu as ilosc_wyrobu, _jedn_wyrobu as jedn_wyrobu,
			 p.towrsymb, p.towrnazwa, 
			#round(p.waga*1000,2) as ilosc_gram, 
			#round(p.waga*1000/(select sum(waga*1000) from zmpo where idnNG=@GUID and magtyp=2 group by idnNg)*100,6) as ilosc_proc, 
			#round(p.ilosc*_waga_j_wyrobu*_ilosc_wyrobu*1000,2) as ilosc_gram, 
			#round(p.ilosc*_waga_j_wyrobu*_ilosc_wyrobu*1000/(_waga_wyrobu*1000)*100,6) as ilosc_proc, 
			p.ilosc,
			round(p.ilosc*_waga_j_wyrobu*1000,2) as ilosc_gram, 
			round(p.ilosc*_waga_j_wyrobu*1000/(_waga_wyrobu*1000)*100,6) as ilosc_proc, 			
			j.nazwa as jednostka, _strona AS stron
			from zmpo p
			inner join Jednostki j on j.idn=p.jednostka
			where p.idnNG = _GUID and p.magtyp=2
			order by ilosc_gram;
		ELSE
			#select _jedn_wyrobu, _waga_j_wyrobu;
			
			select 
			_symbol_wyrobu as symbol_wyrobu, _ilosc_wyrobu as ilosc_wyrobu, _jedn_wyrobu as jedn_wyrobu,
			p.towrsymb, p.towrnazwa,p.ilosc, 
			round(if(0=0,p.ilosc*_waga_j_wyrobu,p.waga)*1000,2) as ilosc_gram, -- p.waga=0
			round(if(0=0,p.ilosc*_waga_j_wyrobu,p.waga)*1000/(_waga_wyrobu*1000)*100,6) as ilosc_proc, 
			j.nazwa as jednostka, _strona AS stron
			from zmpo p
			inner join Jednostki j on j.idn=p.jednostka
			inner join TowrNg t on t.GUID=p.idntowr
			where p.idnNG = _GUID and p.magtyp=2
			order by ilosc_gram;
			
			
		END IF;
		END;
	END IF;	


/********************************************************************************
	REC_N - wydruk receptury
	CALL fr_ht_zp_rec('REC_N', 'A01-3', 'CALL_RECEPTURY', '0;1;1;0','');
	CALL fr_ht_zp_rec('REC_P2', 'A01-3', 'CALL_RECEPTURY', '0;1;1;0','');
	prcedura pr_build_t_zlecenia_prod_atom jest w pliku pl_zlecprod_tools, ehhh
********************************************************************************/

	IF _Symbol = 'REC_N' THEN

		
		set @ukryj := _VarInt(substring_index(_Param_B,';',1));
		set @ilosc1 := _VarDouble(substring_index(substring_index(_Param_B,';',2),';',-1));
		set @ilosc2 := _VarDouble(substring_index(substring_index(_Param_B,';',3),';',-1));
		set @drukuj_opakowania := _VarDouble(substring_index(_Param_B,';',-1));
		
		
		CALL pr_build_t_zlecenia_prod_atom('Prepar.t_zlecenia_prod_atom.to.print',
										0, --	_Level
										'',	-- _Parent_ID	
										'',	--	_Parent_LP_Str
										_GUID, --	_RcNg_GUID
										'', --	_IdnTowr 'IM1461190850004004004'
										@ilosc2, --	_Ilosc
										0,	-- _Ilosc_1_N, moze byc 0 
										'',
										'',
										FALSE, -- _Use_Pomin,
										'',	-- jezeli puste, to nie robi selectu na tabeli koncowej
										''	
										);


		drop table if exists t_zlecenia2;
		create temporary table t_zlecenia2
		select guid, idnng, _Ilosc, przychod, typ_zasobu, jednostka from t_zlecenia_prod_atom;
		alter table t_zlecenia2 add index guid(guid);
	
		CALL pr_build_t_zlecenia_prod_atom('Prepar.t_zlecenia_prod_atom.to.print',0,'','',_GUID,'',@ilosc1,0,'','',FALSE,'','');

		SET @a1 := (SELECT _Rnd2(SUM(t._ilosc)) FROM t_zlecenia_prod_atom t WHERE t.idnng= _GUID AND t.przychod=0 AND t.typ_zasobu=0 AND t.jednostka IN (2,14));
		SET @a2 := (SELECT _Rnd2(SUM(t._ilosc)) FROM t_zlecenia2 t WHERE t.idnng= _GUID AND t.przychod=0 AND t.typ_zasobu=0 AND t.jednostka IN (2,14));
		
		select 
		'REC_N' as REC_N,
		_Param_B AS _Param_B,
		IF(@drukuj_opakowania=1,'TAK','NIE') as opakowania,
		n.*, 
		p.*,
		t.nazwa as wyrob_gotowy,
		t.photo as zdjecie,
		_VarDouble(@ilosc1) as ilosc1,
		_VarDouble(@ilosc2) as ilosc2,
		_VarDouble(@a1) as ilosc_ciasta,
		_Rnd2(@a1 / @ilosc1) AS nawazka,
		_VarDouble(@a2) as ilosc_ciasta2,
		_Rnd2(@a2 / @ilosc2) AS nawazka2
		
		from rcng n
		inner join rcpo p ON n.guid = p.idnng
		inner join TowrNg t ON t.guid = p.idntowr
		where n.GUID = _GUID and p.przychod=1
		;
		
		-- drop table if exists t_zlecenia2;

	END IF;	

	
	IF _Symbol = 'REC_P3' THEN -- pozycje przychodowe, na przyszlosc
	
		select 'REC_P3' as REC_P3, p.* from rcpo p where p.idnng = _GUID and p.przychod=1;
	
	END IF;	

	
	IF _Symbol = 'REC_P2' THEN

		DROP TABLE IF EXISTS t_rec1;
		CREATE TEMPORARY TABLE t_rec1
		SELECT p.*
		FROM t_zlecenia_prod_atom p
		INNER JOIN rcng n2 ON p.idnng = n2.guid
		WHERE p.przychod=0 
		-- AND p.typ_zasobu = 0 -- tylko towar/material
		AND CASE WHEN @session.globals.Owner = 'KONKOL' THEN p.typ_zasobu = 0 -- tylko towar/material
				ELSE TRUE END
		AND IF(@ukryj = 0, TRUE, n2.ugrpguid = 'ALL')

		-- AND IF(@drukuj_opakowania = 1, TRUE, p.wydzial <> 6)
		AND CASE WHEN @session.globals.Owner = 'AXIS' THEN TRUE -- dla axisu NIE wylaczamy wydzialu 6 bo to nie sa opakowania
				ELSE IF(@drukuj_opakowania = 1, TRUE, p.wydzial <> 6) END

		ORDER BY p.typ_zasobu, p._LP_Str
		;

		ALTER table t_rec1 add column nlp varchar(10);
	
		SET @poziom_pop := 0; 
		SET @nlp_pop := ''; 
		SET @n := 0;
		SET @nlp := '';
		
		BEGIN -- petla, ktora numeruje pozycje i pozycje zagniezdzone
			DECLARE wykonuj BOOL DEFAULT FALSE;
			DECLARE _LOCAL_GUID VARCHAR(38);
			DECLARE _LOCAL_level INT;
			DECLARE c_proc CURSOR FOR
				SELECT guid, _level FROM t_rec1 ORDER BY typ_zasobu, _LP_Str;
			

			DECLARE CONTINUE HANDLER FOR NOT FOUND SET wykonuj = TRUE;
			OPEN c_proc;
			loop1: LOOP
				FETCH c_proc INTO _LOCAL_GUID, _LOCAL_level;
				IF wykonuj THEN
					LEAVE loop1;
				END IF;
				
				
				IF @poziom_pop = _LOCAL_level then
						SET @nlp_pop := substring_index(@nlp,'.',_LOCAL_level+1);
						SET @nlp := concat(@nlp_pop,'.',@n + 1);
						SET @n := @n +1;
				END IF;
				
				IF @poziom_pop < _LOCAL_level then
						SET @poziom_pop = _LOCAL_level;
						SET @nlp_pop := @nlp;
						SET @n := 0;
						SET @nlp := concat(@nlp_pop,'.',@n + 1);
						SET @n := @n +1;
				END IF;
				
				IF @poziom_pop > _LOCAL_level then
						SET @poziom_pop = _LOCAL_level;
						SET @nlp_pop := substring_index(@nlp,'.',_LOCAL_level+1);
						SET @n := substring_index(substring_index(@nlp,'.',_LOCAL_level+2),'.',-1);
						SET @nlp := concat(@nlp_pop,'.',@n + 1);
						SET @n := @n +1;
				END IF;
				
				update t_rec1 set nlp = substring_index(@nlp,'.',-(_LOCAL_level+1)) where guid = _LOCAL_GUID;
				
			END LOOP;
			CLOSE c_proc;
			END;
	
  IF @session.globals.Owner IN ('AXIS') THEN
   DROP TEMPORARY TABLE IF EXISTS rec_druk;
   CREATE TEMPORARY TABLE rec_druk
   SELECT 'REC_P2' AS REC_P2,
   _Param_B AS _Param_B,
   n.numer_wewn AS numer_wewn,p.idntowr,
   p.nlp,p.lp, p.ID, p._LP_Str, p._ilosc, p.IS_Produkt, p.PRN_Grp_ID, p.PRN_Grp_Text, p._level,
   CONCAT(SPACE(p._level*5),p.towrsymb,IF(n.ugrpguid<>'ALL',' - rec. poufna',''),
    IF(n.numer_wewn IS NULL,'',CONCAT(' - ',n.numer_wewn))) AS towrsymb,
   p.opis,

   j.nazwa AS jm,
   p._ilosc AS ilosc_b,
   p2._ilosc AS ilosc_c,
   p._ilosc * p.cena_kalulacyjna AS wartosc_b,
   p2._ilosc * p.cena_kalulacyjna AS wartosc_c,
   p.typ_zasobu, p.idnng_merge,
   CASE WHEN p.typ_zasobu = 0 THEN 'towar/materiał'
      WHEN p.typ_zasobu = 6 THEN 'robocizna'
      WHEN p.typ_zasobu = 1 THEN 'usluga'
      ELSE 'NN'
   END AS zasob
   FROM t_rec1 p
   INNER JOIN t_zlecenia2 p2 ON p2.guid = p.guid
   INNER JOIN Jednostki j ON p.jednostka = j.idn
   LEFT JOIN rcng n ON p.recepturaGUID = n.guid
   INNER JOIN rcng n2 ON p.idnng = n2.guid
   ORDER BY LPAD(p.lp,3,0);

   SELECT r.*,t.nazwa FROM rec_druk r LEFT JOIN TowrNg t ON r.idntowr=t.guid   WHERE _level=0;

  ELSE

   select 'REC_P2' as REC_P2,
   _Param_B AS _Param_B,
   n.numer_wewn as numer_wewn,
   p.nlp, p.ID, p._LP_Str, p._ilosc, p.IS_Produkt, p.PRN_Grp_ID, p.PRN_Grp_Text, p._level,
   concat(space(p._level*5),p.towrsymb,if(n.ugrpguid<>'ALL',' - rec. poufna',''),
    IF(n.numer_wewn is null,'',concat(' - ',n.numer_wewn))) as towrsymb,
   p.opis,
   j.nazwa as jm,
   p._ilosc as ilosc_b,
   p2._ilosc as ilosc_c,
   p._ilosc * p.cena_kalulacyjna as wartosc_b,
   p2._ilosc * p.cena_kalulacyjna as wartosc_c,
   p.typ_zasobu,
   CASE WHEN p.typ_zasobu = 0 THEN 'towar/materiał'
      WHEN p.typ_zasobu = 6 THEN 'robocizna'
      ELSE 'NN'
   END AS zasob
   from t_rec1 p
   inner join t_zlecenia2 p2 ON p2.guid = p.guid
   inner join Jednostki j ON p.jednostka = j.idn
   left join rcng n ON p.recepturaGUID = n.guid
   inner join rcng n2 ON p.idnng = n2.guid
   order by p.typ_zasobu, p.nlp; -- p._LP_Str;

  END IF; -- @session.globals.Owner IN ('AXIS')

	END IF; -- _Symbol = 'REC_P2'

	/*
	SET @session.globals.Owner = 'AXIS';
CALL fr_ht_zp_rec('REC_N', '{C503EB8A-3041-4458-A3FB-D309FB8DCAA7}', 'CALL_RECEPTURY', '0;1;1;0','');
CALL fr_ht_zp_rec('REC_P2', '{C503EB8A-3041-4458-A3FB-D309FB8DCAA7}', 'CALL_RECEPTURY', '0;1;1;0','');
CALL fr_ht_zp_rec('REC_P', '{C503EB8A-3041-4458-A3FB-D309FB8DCAA7}', 'CALL_RECEPTURY', '0;1;1;0','');
	*/
	-- frxDBUniversalTwo
	IF _Symbol = 'REC_P' THEN
  IF @session.globals.Owner IN ('AXIS') THEN
 
  DROP TABLE IF EXISTS t_rec1;
		CREATE TEMPORARY TABLE t_rec1
		SELECT p.*
		FROM t_zlecenia_prod_atom p
		INNER JOIN rcng n2 ON p.idnng = n2.guid
		WHERE p.przychod=0 
		
		AND IF(@ukryj = 0, TRUE, n2.ugrpguid = 'ALL')
		-- AND IF(@drukuj_opakowania = 1, TRUE, p.wydzial <> 6) wylaczenie dla axisu, bo wydzial 6 nie jest od opakowan
		ORDER BY p.typ_zasobu, p._LP_Str
		;

		ALTER TABLE t_rec1 ADD COLUMN nlp VARCHAR(10);
	
		SET @poziom_pop := 0; 
		SET @nlp_pop := ''; 
		SET @n := 0;
		SET @nlp := '';
		
		BEGIN 
			DECLARE wykonuj BOOL DEFAULT FALSE;
			DECLARE _LOCAL_GUID VARCHAR(38);
			DECLARE _LOCAL_level INT;
			DECLARE c_proc CURSOR FOR
				SELECT guid, _level FROM t_rec1 ORDER BY typ_zasobu, _LP_Str;
			

			DECLARE CONTINUE HANDLER FOR NOT FOUND SET wykonuj = TRUE;
			OPEN c_proc;
			loop1: LOOP
				FETCH c_proc INTO _LOCAL_GUID, _LOCAL_level;
				IF wykonuj THEN
					LEAVE loop1;
				END IF;
				
				
				IF @poziom_pop = _LOCAL_level THEN
						SET @nlp_pop := SUBSTRING_INDEX(@nlp,'.',_LOCAL_level+1);
						SET @nlp := CONCAT(@nlp_pop,'.',@n + 1);
						SET @n := @n +1;
				END IF;
				
				IF @poziom_pop < _LOCAL_level THEN
						SET @poziom_pop = _LOCAL_level;
						SET @nlp_pop := @nlp;
						SET @n := 0;
						SET @nlp := CONCAT(@nlp_pop,'.',@n + 1);
						SET @n := @n +1;
				END IF;
				
				IF @poziom_pop > _LOCAL_level THEN
						SET @poziom_pop = _LOCAL_level;
						SET @nlp_pop := SUBSTRING_INDEX(@nlp,'.',_LOCAL_level+1);
						SET @n := SUBSTRING_INDEX(SUBSTRING_INDEX(@nlp,'.',_LOCAL_level+2),'.',-1);
						SET @nlp := CONCAT(@nlp_pop,'.',@n + 1);
						SET @n := @n +1;
				END IF;
				
				UPDATE t_rec1 SET nlp = SUBSTRING_INDEX(@nlp,'.',-(_LOCAL_level+1)) WHERE guid = _LOCAL_GUID;
				
			END LOOP;
			CLOSE c_proc;
			END;
	
		DROP TEMPORARY TABLE IF EXISTS rec_druk;
		CREATE TEMPORARY TABLE rec_druk
		SELECT 'REC_P2' AS REC_P2,
		_Param_B AS _Param_B,
		n.numer_wewn AS numer_wewn,p.idntowr,
		p.nlp,p.lp, p.ID, p._LP_Str, p._ilosc, p.IS_Produkt, p.PRN_Grp_ID, p.PRN_Grp_Text, p._level,
		CONCAT(SPACE(p._level*5),p.towrsymb,IF(n.ugrpguid<>'ALL',' - rec. poufna',''),
			IF(n.numer_wewn IS NULL,'',CONCAT(' - ',n.numer_wewn))) AS towrsymb, 
		p.opis, 
		
		j.nazwa AS jm,
		p._ilosc AS ilosc_b,
		p2._ilosc AS ilosc_c,
		p._ilosc * p.cena_kalulacyjna AS wartosc_b,
		p2._ilosc * p.cena_kalulacyjna AS wartosc_c,
		p.typ_zasobu, p.idnng_merge,
		CASE WHEN p.typ_zasobu = 0 THEN 'towar/materiał'
				 WHEN p.typ_zasobu = 6 THEN 'robocizna'
				 WHEN p.typ_zasobu = 1 THEN 'usluga'
				 ELSE 'NN'
		END AS zasob
		FROM t_rec1 p
		INNER JOIN t_zlecenia2 p2 ON p2.guid = p.guid
		INNER JOIN Jednostki j ON p.jednostka = j.idn
		LEFT JOIN rcng n ON p.recepturaGUID = n.guid
		INNER JOIN rcng n2 ON p.idnng = n2.guid
		ORDER BY   LPAD(p.lp,3,0); 
	
		DROP TEMPORARY TABLE IF EXISTS rec_grp2;
		CREATE TEMPORARY TABLE rec_grp2
		SELECT 	
		SUM(ilosc_b) AS ilosc_b,
		SUM(ilosc_c) AS ilosc_c,
		SUM(wartosc_b) AS wartosc_b,
		SUM(wartosc_c) AS wartosc_c,typ_zasobu,zasob
		
		-- FROM rec_druk WHERE IFNULL(idnng_merge,'')='' GROUP BY typ_zasobu;
		FROM rec_druk WHERE _level = 0 GROUP BY typ_zasobu;

		ALTER TABLE rec_grp2 ADD UNIQUE INDEX (typ_zasobu); 
		INSERT IGNORE INTO rec_grp2 SELECT 0,0,0,0,0,'towar/materiał';
		INSERT IGNORE INTO rec_grp2 SELECT 0,0,0,0,6,'robocizna';
		INSERT IGNORE INTO rec_grp2 SELECT 0,0,0,0,1,'usługa';
		
		
		
		SELECT * FROM rec_grp2 ORDER BY typ_zasobu;
		
		
  ELSE
   CALL fr_ht_zp_rec('REC_P_ZALECENIA',_GUID,'','','');
  END IF; -- @session.globals.Owner IN ('AXIS')

 END IF; -- _Symbol = 'REC_P'
	
	
	IF _Symbol = 'REC_P_ZALECENIA' THEN

		DROP TABLE IF EXISTS t2;
		CREATE TEMPORARY TABLE t2
		(`zalecenia_techn` VARCHAR(255) NOT NULL DEFAULT '');
		
		
		SET @Iter = 1;
		WHILE @Iter<21 DO
			SET @Iter = @Iter + 1;
			INSERT INTO t2
			SELECT REPLACE(SUBSTRING_INDEX(SUBSTRING_INDEX(Zalecenia_techn,CHAR(13),@Iter),CHAR(13),-1),CHAR(10),'') AS linia
			FROM rcng 
			WHERE guid = _GUID;
		END WHILE;
			
		SELECT 
		SUBSTRING_INDEX(Zalecenia_techn,';',1) AS par1,
		SUBSTRING_INDEX(SUBSTRING_INDEX(Zalecenia_techn,';',2),';',-1) AS par2,
		SUBSTRING_INDEX(Zalecenia_techn,';',-1) AS par3
		FROM t2 WHERE zalecenia_techn NOT IN (' ; ; ','');

	END IF;
	
	-- CALL fr_ht_zp_rec('REC_N', 'C09-1', 'CALL_RECEPTURY', '0;30;400','');
	

END$$

DELIMITER ;
