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

DROP PROCEDURE IF EXISTS `ht_pr_STCorr`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `ht_pr_STCorr` (
	IN _MODE VARCHAR(50),		
	IN _GUID VARCHAR(38),
	IN _Param_A VARCHAR(250),	
	IN _Param_B VARCHAR(250),	
	IN _Param_C VARCHAR(250),
	IN _Param_D VARCHAR(250)
)
SPROC: BEGIN
DECLARE _rok INTEGER;
DECLARE _SQLText, _SQLPrefix TEXT;
DECLARE _order TEXT;
DECLARE _przeliczenie_B_N TINYINT;

-- CALL ht_pr_STCorr('GetCorrPoz', @guid, 'KSEF', '_Param_B', '_Param_C', '_Param_D');

SET _rok = ifnull(@session.FK.AktRokObrach, year(current_date));
	
IF _MODE = 'GetCorrPoz' THEN
	
	CALL _IReg_Get('OPNGINFOPATH', 'Dok. drukować grupując towary', 0, 'INTEGER', @session.HT.IR.PozycjeGrupowaneNaWydruku, 2);
	-- CALL _IReg_Get('Wydruki', 'Pokaz_wszystkie_pozycje_korekty', 0, 'INTEGER', @session.HT.IR.Pokaz_wszystkie_pozycje_korekty, 2);
	SELECT valueB INTO @session.HT.IR.Pokaz_wszystkie_pozycje_korekty FROM IReg WHERE idkey='Pokaz_wszystkie_pozycje_korekty';
	SET @session.HT.IR.Pokaz_wszystkie_pozycje_korekty = IFNULL(@session.HT.IR.Pokaz_wszystkie_pozycje_korekty,0);

	IF (select count(*) from oppo_corr where idnoper_corr = _GUID and j_grp_GUID <> '') > 0 THEN
		CALL ht_pr_STCorr('GRUPOWANIE_INTERNALNE', _GUID, _Param_A, _Param_B, _Param_C, _Param_D);
		LEAVE SPROC;
	END IF;
	
	IF @session.HT.IR.PozycjeGrupowaneNaWydruku > 0 THEN
		CALL ht_pr_STCorr('SimpleCorrGrp', _GUID, _Param_A, _Param_B, _Param_C, _Param_D);	
		LEAVE SPROC;
	ELSE

	set @test1 = 'SimpleCorr';

		CALL ht_pr_STCorr('SimpleCorr', _GUID, _Param_A, _Param_B, _Param_C, _Param_D);
		LEAVE SPROC;
	END IF;
END IF;
	

IF _MODE = 'SimpleCorr' THEN
	DROP TEMPORARY TABLE IF EXISTS t_korpoz;

	SET _SQLText = '
	CREATE TEMPORARY TABLE t_korpoz
	select
		"ht_pr_STCorr_SimpleCorr" AS ht_pr_STCorr_SimpleCorr,
		"#scenario#" AS scenariusz,
		C.b_towrsymb,
		C.j_towrsymb,
		IF(N.lang_id<>"pl",IF(Tb.nazwa=C.b_towrnazwa, 
		IFNULL(( SELECT tostr from lang_translations where to_lang=N.lang_id and object_name="TowrNg" and object_key=b_idnTowr LIMIT 1), 
		C.b_towrnazwa), C.b_towrnazwa),C.b_towrnazwa) as b_towrnazwa,
		IF(N.lang_id<>"pl",IF(Tj.nazwa=C.j_towrnazwa, 
		IFNULL(( SELECT tostr from lang_translations where to_lang=N.lang_id and object_name="TowrNg" and object_key=j_idnTowr LIMIT 1), 
		C.j_towrnazwa),C.j_towrnazwa),C.j_towrnazwa) as j_towrnazwa,
		
		IFNULL(C.b_towrnazwa2,"") as b_towrnazwa2,
		IFNULL(C.j_towrnazwa2,"") as j_towrnazwa2,
		C.b_cenanet,
		C.j_cenanet,
		if(C.b_ilosc=0, _Rnd2(C.b_cenanet/C.b_kurs),_Rnd2(C.b_wartnetwal/C.b_ilosc)) AS b_cenanetwal,
		if(C.j_ilosc=0,0,_Rnd2(C.j_wartnetwal/C.j_ilosc)) AS j_cenanetwal,

		C.b_rabat,
		C.j_rabat,
		C.b_ilosc,
		C.j_ilosc,
		C.b_ilosc - C.j_ilosc AS ilosc_diff,
		C.b_Jednostka,
		C.j_Jednostka,

		case when N.lang_id <> "pl" then IF((JPb.Nazwa = "" or isnull(JPb.Nazwa)), JPb.Nazwa, JPb.Nazwa1)
		WHEN @session.globals.Owner = "TRANSBUD" AND P.kat9=1 THEN "kg"			
		else JPb.Nazwa END As b_JednostkaPom, 
		case when N.lang_id <> "pl" then IF((JPj.Nazwa = "" or isnull(JPj.Nazwa)), JPj.Nazwa, JPj.Nazwa1) 
		WHEN @session.globals.Owner = "TRANSBUD" AND P.kat9=1 THEN "kg"
		else JPj.Nazwa END As j_JednostkaPom, 
		JPb.precyzja as b_JednostkaPomPrecyzja,
		JPj.precyzja as j_JednostkaPomPrecyzja,
		IF(@session.globals.Owner = "TRANSBUD" AND P.kat9=1, _varDOUBLE(_Rnd(C.b_ilosc * 1000 / 1, JPb.precyzja)),
		_varDOUBLE(_Rnd(C.b_ilosc * Tb.przelicznik_l / Tb.przelicznik_m, JPb.precyzja))) as b_iloscPom,
			IF(@session.globals.Owner = "TRANSBUD" AND P.kat9=1, _varDOUBLE(_Rnd(C.b_ilosc * 1000 / 1, JPb.precyzja)),
		_varDOUBLE(_Rnd(C.j_ilosc * Tb.przelicznik_l / Tj.przelicznik_m, JPj.precyzja))) as j_iloscPom,
		
		IF(SUM(C.b_ilosc)=0,0,_varDOUBLE(_Rnd2(SUM(C.b_wartnetwal)/(_Rnd(SUM(C.b_ilosc * Tb.przelicznik_l / Tb.przelicznik_m), JPb.precyzja)))) ) AS b_cenaPom,
		IF(SUM(C.j_ilosc)=0,0,_varDOUBLE(_Rnd2(SUM(C.j_wartnetwal)/(_Rnd(SUM(C.j_ilosc * Tj.przelicznik_l / Tj.przelicznik_m), JPj.precyzja))))) AS j_cenaPom,
		-- _varDOUBLE(_Rnd2(C.b_wartnetwal/(_Rnd(C.b_ilosc * Tb.przelicznik_l / Tb.przelicznik_m, JPb.precyzja)))) as b_cenaPom,
		-- _varDOUBLE(_Rnd2(C.j_wartnetwal/(_Rnd(C.j_ilosc * Tj.przelicznik_l / Tj.przelicznik_m, JPj.precyzja)))) as j_cenaPom,

		b_vattab.shortname as b_idnVatStr,
		j_vattab.shortname as j_idnVatStr,
		C.b_wartvat,
		C.b_kurs,
		C.b_wartnet,
		C.j_wartnet,
			-- C.b_wartbrt,
		IF(N.Calc_Ver>2  and C.b_wartbrtWal<>0,C.b_wartBrtWal,C.b_wartbrt) as b_wartbrt,
		C.b_cenabrt,
		Tj.symbol as symbol,
		C.b_pkwiu,
		C.j_pkwiu,
		Tj.sww as sww,
		Tj.usluga as usluga,
		W.symbol as WalutaStr,
		Tj.treenode,
		Tj.GUID as towrGUID,
		Tj.kod_kreskowy,
		Tb.kod_kreskowy as b_kod_kreskowy,
		Tj.kod_kreskowy as j_kod_kreskowy,
		Tj.pcn,
		Tj.NazwaUProducenta,
		Tj.symboludost,
		C.b_uwagi,
		C.b_idnvat,
		C.j_idnvat,
		C.b_uwagi2,
		C.j_uwagi2,
		C.j_uwagi,
		Tj.oplata_produktowa,
		C.b_wartnetWal,
		C.j_wartnetWal,
		-- C.j_wartbrt,
		IF(N.Calc_Ver>2,C.j_wartBrtWal,C.j_wartbrt) as j_wartbrt,
		C.j_wartvat,
		if(N.przeliczenie_B_N=0,_Rnd2(C.b_wartnetWal*b_vattab.value), b_wartbrtWal - _Rnd2(b_wartbrtWal/(1 + b_vattab.value)))  as b_wartvatWal, 
		if(N.przeliczenie_B_N=0,_Rnd2(C.j_wartnetWal*j_vattab.value), j_wartbrtWal - _Rnd2(j_wartbrtWal/(1 + j_vattab.value)))  as j_wartvatWal,
			-- sekcja do wyrzucenia w momencie, gdy wartosci bruttowal beda w pelni i zawsze naliczane
		case when C.b_wartBrtWal <> 0 then C.b_wartBrtWal
		else C.b_wartnetWal  + _Rnd2(C.b_wartnetWal*b_vattab.value)						
		end as b_wartBrtWal,
		case when C.j_wartBrtWal <> 0 then C.j_wartBrtWal
		else C.j_wartnetWal  + _Rnd2(C.j_wartnetWal*j_vattab.value)						
		end as j_wartBrtWal,
		
		N.waluta,
		_Rnd2(C.b_cenanet - (C.b_cenanet*C.b_rabat)/100)  as CenaNetWalzRab,
		_Rnd2(C.j_cenanet - (C.j_cenanet*C.j_rabat)/100)  as CenaNetWalzRab_jest,
		
		-- sekcja dla Eneva (Relacom) - rabaty kwotowe
		_Rnd2(C.j_cenanet * C.j_ilosc) - C.j_wartnet AS j_wartrabnetto,
		_Rnd2(C.j_cenanet * C.j_ilosc) AS j_wart_przed_rabatem_netto,
		_Rnd2(C.j_cenanet * C.j_ilosc * (1+j_vattab.value)) AS j_wart_przed_rabatem_brutto,
		
		P. grupuj,
		Jb.nazwa as b_jednostkaStr,
		Jj.nazwa as j_jednostkaStr,
		P.wspolplatnosc,
		P.GUIDKorPo,
		C.j_kurs,
		C.j_kurs as kurs_jest, 
		P.LP,
		-- P.kat0, P.kat1, P.kat2, P.kat3, P.kat4, P.kat5, P.kat6, P.kat7, P.kat8, P.kat9,
		IF(P.kat0 > 0, (select kc.valuex from v_kkn0 kc where kc.klucz=P.kat0 and rok in (0, #rok#) ), "" ) as katcol0Str,
		IF(P.kat1 > 0, (select kc.valuex from v_kkn1 kc where kc.klucz=P.kat1 and rok in (0, #rok#) ), "" ) as katcol1Str,
		IF(P.kat2 > 0, (select kc.valuex from v_kkn2 kc where kc.klucz=P.kat2 and rok in (0, #rok#) ), "" ) as katcol2Str,
		IF(P.kat3 > 0, (select kc.valuex from v_kkn3 kc where kc.klucz=P.kat3 and rok in (0, #rok#) ), "" ) as katcol3Str,
		IF(P.kat4 > 0, (select kc.valuex from v_kkn4 kc where kc.klucz=P.kat4 and rok in (0, #rok#) ), "" ) as katcol4Str,
		IF(P.kat5 > 0, (select kc.valuex from v_ktw0 kc where kc.klucz=P.kat5 and rok in (0, #rok#) ), "" ) as katcol5Str,
		IF(P.kat6 > 0, (select kc.valuex from v_ktw1 kc where kc.klucz=P.kat6 and rok in (0, #rok#) ), "" ) as katcol6Str,
		IF(P.kat7 > 0, (select kc.valuex from v_ktw2 kc where kc.klucz=P.kat7 and rok in (0, #rok#) ), "" ) as katcol7Str,
		IF(P.kat8 > 0, (select kc.valuex from v_ktw3 kc where kc.klucz=P.kat8 and rok in (0, #rok#) ), "" ) as katcol8Str,
		IF(P.kat9 > 0, (select kc.valuex from v_ktw4 kc where kc.klucz=P.kat9 and rok in (0, #rok#) ), "" ) as katcol9Str,
		Norig.datawystaw as DataWystawDocKor, Norig.rodzaj as RodzajDocKor,
		_VarStr(nd.symbol) AS AreaSymbol,
		_VarStr(nd.Nazwa) AS AreaNazwa,
		_VarStr(CONCAT("korekta dokumentu: ", IFNULL(od.Opis, ""), "  Nr ", Norig.NrDOkFin, " wyst. ", Norig.DataWystaw)) AS V_KorektaDotyczy,P.nrseryjny,
		"." as NrdokfinDocKor,
		Norig.tabela_kursow AS b_tabela_kursow,
		Norig.datawaluty AS b_datawaluty,
		zn.nrzamw, zn.nrzamwkontr,Tb.wyroznik,
		Tb.jpkgtu
	FROM 
		oppo_corr C
	LEFT JOIN opng N On N.GUID = C.IdnOper_corr  							-- naglowek korekty
	LEFT JOIN oppo P On P.GUID  = C.GUID_orig  								-- pozycja oryginalna
	LEFT JOIN opng Norig ON Norig.GUID = P.IdnOperFin					-- naglowek oryginalu
	LEFT JOIN operdoc od ON od.Rodzaj = Norig.Rodzaj
	LEFT JOIN v_erp_vat b_vattab ON  b_vattab.id = C.b_idnvat
	LEFT JOIN v_erp_vat j_vattab ON  j_vattab.id = C.j_idnvat
	LEFT JOIN TowrNg Tb ON Tb.GUID = C.b_idnTowr
	LEFT JOIN TowrNg Tj ON Tj.GUID = C.j_idnTowr 
	LEFT JOIN Jednostki Jb On Jb.idn = C.b_jednostka
	LEFT JOIN Jednostki Jj On Jj.idn = C.j_jednostka
	LEFT JOIN Jednostki JPb ON JPb.idn = Tb.jednostka_pom
	LEFT JOIN Jednostki JPj ON JPj.idn = Tj.jednostka_pom
	LEFT JOIN waluty W on W.idn = N.waluta
	LEFT JOIN erp_area_nodes nd ON nd.GUID = P.areaGUID AND nd.deleted = 0
	LEFT JOIN zmpo zp ON zp.GUID = P.GUIDzm
	LEFT JOIN zmng zn ON zn.GUID = zp.IdnNG
	WHERE
		IF(@session.HT.IR.Pokaz_wszystkie_pozycje_korekty=0, C.Changed > 0 ,C.Changed >= 0)
		AND C.IdnOper_Corr = "#GUID#"
		group by C.GUID
		#ORDER#
	';

	SET @_SQLText2 =	_SQLText;

	SET _order = 
		CASE WHEN @session.globals.Owner IN ('POLASZEK') AND _Param_A IN ('STZTMGZM_KOREKTA2') THEN 'ORDER BY zn.nrzamw, P.lp '
			-- ELSE (SELECT CONCAT('ORDER BY ',valuea) FROM IReg WHERE idkey = 'Sortowanie pozycji dokumentu FV' LIMIT 1)
				ELSE 'ORDER BY P.lp '
		END;
		
	SET _SQLText =	REPLACE(_SQLText, '#GUID#', _GUID);
	SET _SQLText =	REPLACE(_SQLText, '#scenario#', IFNULL(_Param_A,'')); 
	SET _SQLText =	REPLACE(_SQLText, '#ORDER#', _order);
	SET _SQLText =	REPLACE(_SQLText, '#rok#', _rok);

	SET @_SQLText =	_SQLText;
	CALL _ExecuteCmd(_SQLText, @Error);

	IF _Param_A <> 'KSEF' THEN -- dla ksef bez selecta
			SELECT * FROM t_korpoz;
	END IF;

END IF; 	-- _MODE = 'SimpleCorr' 
	
	
IF _MODE = 'SimpleCorrGrp' THEN
	DROP TEMPORARY TABLE IF EXISTS t_korpoz;

	CREATE TEMPORARY TABLE t_korpoz
	SELECT
		'ht_pr_STCorr_SimpleCorrGrp' AS ht_pr_STCorr_SimpleCorrGrp,
		C.b_towrsymb,
	--	ifnull(C.b_towrnazwa2,C.b_towrnazwa) as b_towrnazwa,
		C.j_towrsymb,
	--	ifnull(C.j_towrnazwa2,C.j_towrnazwa) as j_towrnazwa,
		IFNULL(C.b_towrnazwa2,(IF(N.lang_id<>"pl",IF(Tb.nazwa=C.b_towrnazwa, 
		IFNULL(( SELECT tostr FROM lang_translations WHERE to_lang=N.lang_id AND object_name="TowrNg" AND object_key=b_idnTowr LIMIT 1), 
		C.b_towrnazwa), C.b_towrnazwa),C.b_towrnazwa))) AS b_towrnazwa,
		-- C.j_towrsymb,
		IFNULL(C.j_towrnazwa2,(IF(N.lang_id<>"pl",IF(Tj.nazwa=C.j_towrnazwa, 
		IFNULL(( SELECT tostr FROM lang_translations WHERE to_lang=N.lang_id AND object_name="TowrNg" AND object_key=j_idnTowr LIMIT 1), 
		C.j_towrnazwa),C.j_towrnazwa),C.j_towrnazwa)) ) AS j_towrnazwa,
		
		
		IFNULL(C.b_towrnazwa2,'') as b_towrnazwa2,
		IFNULL(C.j_towrnazwa2,'') as j_towrnazwa2,
		-- jmam (C.b_cenanet) as b_cenanet,
		(C.b_cenanet - (C.b_cenanet*C.b_rabat)/100)  AS b_cenanet,

		if(SUM(C.b_ilosc)=0, _Rnd2(C.b_cenanet/C.b_kurs) ,_Rnd2(SUM(C.b_wartnetwal)/SUM(C.b_ilosc))) AS b_cenanetwal,
		if(SUM(C.j_ilosc)=0,0,_Rnd2(SUM(C.j_wartnetwal)/SUM(C.j_ilosc))) AS j_cenanetwal,

		C.b_rabat,
		SUM(C.b_ilosc) AS b_ilosc,
		SUM(C.b_ilosc - C.j_ilosc) AS ilosc_diff,
		C.b_Jednostka,
		C.j_Jednostka,

		case when N.lang_id <> "pl" then IF((JPb.Nazwa = "" or isnull(JPb.Nazwa)), JPb.Nazwa, JPb.Nazwa1) 
		WHEN @session.globals.Owner = "TRANSBUD" AND P.kat9=1 THEN "kg"
		else JPb.Nazwa END As b_JednostkaPom, 
		case when N.lang_id <> "pl" then IF((JPj.Nazwa = "" or isnull(JPj.Nazwa)), JPj.Nazwa, JPj.Nazwa1) 
		WHEN @session.globals.Owner = "TRANSBUD" AND P.kat9=1 THEN "kg"
		else JPj.Nazwa END As j_JednostkaPom, 
		JPb.precyzja as b_JednostkaPomPrecyzja,
		JPj.precyzja as j_JednostkaPomPrecyzja,
	--	_varDOUBLE(_Rnd(SUM(C.b_ilosc * Tb.przelicznik_l / Tb.przelicznik_m), JPb.precyzja)) as b_iloscPom,
	--	_varDOUBLE(_Rnd(SUM(C.j_ilosc * Tj.przelicznik_l / Tj.przelicznik_m), JPj.precyzja)) as j_iloscPom,
			IF(@session.globals.Owner = "TRANSBUD" AND P.kat9=1, _varDOUBLE(_Rnd(SUM(C.b_ilosc * 1000 / 1), JPb.precyzja)) ,
		_varDOUBLE(_Rnd(SUM(C.b_ilosc * Tb.przelicznik_l / Tb.przelicznik_m), JPb.precyzja))) AS b_iloscPom,	
			IF(@session.globals.Owner = "TRANSBUD" AND P.kat9=1, _varDOUBLE(_Rnd(SUM(C.j_ilosc * 1000 / 1), JPj.precyzja)) ,
		_varDOUBLE(_Rnd(SUM(C.j_ilosc * Tj.przelicznik_l / Tj.przelicznik_m), JPj.precyzja))) AS j_iloscPom,
		_varDOUBLE(_Rnd2(SUM(C.b_wartnetwal)/(_Rnd(SUM(C.b_ilosc * Tb.przelicznik_l / Tb.przelicznik_m), JPb.precyzja)))) as b_cenaPom,
		_varDOUBLE(_Rnd2(SUM(C.j_wartnetwal)/(_Rnd(SUM(C.j_ilosc * Tj.przelicznik_l / Tj.przelicznik_m), JPj.precyzja)))) as j_cenaPom,

		b_vattab.shortname AS b_idnVatStr,
		j_vattab.shortname AS j_idnVatStr,
		SUM(C.b_wartvat) AS b_wartvat,
		IF(N.przeliczenie_B_N=0,_Rnd2(SUM(C.b_wartnetWal)*b_vattab.value), SUM(b_wartbrtWal) - _Rnd2(SUM(b_wartbrtWal)/(1 + b_vattab.value)))  AS b_wartvatWal, 
		IF(N.przeliczenie_B_N=0,_Rnd2(SUM(C.j_wartnetWal)*j_vattab.value), SUM(j_wartbrtWal) - _Rnd2(SUM(j_wartbrtWal)/(1 + j_vattab.value)))  AS j_wartvatWal, 
		-- sekcja do wyrzucenia w momencie, gdy wartosci bruttowal beda w pelni i zawsze naliczane
		CASE WHEN SUM(C.b_wartBrtWal) <> 0 THEN SUM(C.b_wartBrtWal)
		ELSE SUM(C.b_wartnetWal  + _Rnd2(C.b_wartnetWal*b_vattab.value))
		END AS b_wartBrtWal,
		-- CASE WHEN SUM(C.j_wartBrtWal) <> 0 THEN SUM(C.j_wartBrtWal)
		CASE WHEN SUM(IF(C.Temporary AND !C.Changed, C.b_wartBrtWal, C.j_wartBrtWal)) <> 0 THEN SUM(IF(C.Temporary AND !C.Changed, C.b_wartBrtWal, C.j_wartBrtWal))	--	##
		ELSE SUM(C.j_wartnetWal  + _Rnd2(C.j_wartnetWal*j_vattab.value))
		END AS j_wartBrtWal,

		C.b_kurs,
		SUM(C.b_wartnet) AS b_wartnet,
		-- SUM(C.b_wartbrt) as b_wartbrt,
		SUM(IF(N.Calc_Ver>2 AND C.b_wartbrtWal<>0,C.b_wartbrtWal,C.b_wartbrt)) AS b_wartbrt,
		(C.b_cenabrt) AS b_cenabrt,
		Tj.symbol AS symbol,
		C.b_pkwiu,
		C.j_pkwiu,
		Tj.sww AS sww,
		Tj.usluga AS usluga,
		W.symbol AS WalutaStr,
		Tj.treenode,
		Tj.GUID AS towrGUID,
		Tj.kod_kreskowy,
		Tb.kod_kreskowy as b_kod_kreskowy,
		Tj.kod_kreskowy as j_kod_kreskowy,
		Tj.pcn,
		Tj.NazwaUProducenta,
		Tj.symboludost,
		C.b_uwagi,
		C.b_idnvat,
		C.j_idnvat,
		C.b_uwagi2,
		C.j_uwagi2,
		C.j_uwagi,
		Tj.oplata_produktowa,
		SUM(C.b_wartnetWal) AS b_wartnetWal,
		SUM(IF(C.Temporary AND !C.Changed, C.b_wartnetWal, C.j_wartnetWal)) AS j_wartnetWal,
		-- SUM(C.j_wartnetWal) AS j_wartnetWal,
		-- C.j_towrnazwa,
		-- jmam (C.j_cenanet) as j_cenanet,
		(C.j_cenanet - (C.j_cenanet*C.j_rabat)/100)  AS j_cenanet,
		C.j_rabat,
		SUM(C.j_ilosc) AS j_ilosc,
		SUM(IF(C.Temporary AND !C.Changed, C.b_wartnet, C.j_wartnet)) AS j_wartnet,
		--			SUM(C.j_wartbrt) as j_wartbrt,
		SUM(IF(N.Calc_Ver>2, C.j_wartbrtWal,C.j_wartbrt)) AS j_wartbrt,
		SUM(C.j_wartvat) AS j_wartvat,
		N.waluta,
		_Rnd2(C.b_cenanet - (C.b_cenanet*C.b_rabat)/100)  AS CenaNetWalzRab,					-- uch, nie mozna konsekwentnie?
		_Rnd2(C.j_cenanet - (C.j_cenanet*C.j_rabat)/100)  AS CenaNetWalzRab_jest,		-- jw.
		
		-- sekcja dla Eneva (Relacom) - rabaty kwotowe
		SUM(_Rnd2(C.j_cenanet * C.j_ilosc) - C.j_wartnet) AS j_wartrabnetto,
		SUM(_Rnd2(C.j_cenanet * C.j_ilosc)) AS j_wart_przed_rabatem_netto,
		SUM(_Rnd2(C.j_cenanet * C.j_ilosc * (1+j_vattab.value))) AS j_wart_przed_rabatem_brutto,
		
		P.grupuj,
		P.IdnOperFin,
		Jb.nazwa AS b_jednostkaStr,
		Jj.nazwa AS j_jednostkaStr,
		P.wspolplatnosc,
		P.GUIDKorPo,
		C.j_kurs,
		C.j_kurs AS kurs_jest, -- jw
		P.LP,
		-- P.kat0, P.kat1, P.kat2, P.kat3, P.kat4, P.kat5, P.kat6, P.kat7, P.kat8, P.kat9,
/*
		IF(P.kat0 > 0, (select kc.valuex from v_kkn0 kc where kc.klucz=P.kat0 and rok in (0, _rok) ), "" ) as katcol0Str,
		IF(P.kat1 > 0, (select kc.valuex from v_kkn1 kc where kc.klucz=P.kat1 and rok in (0, _rok) ), "" ) as katcol1Str,
		IF(P.kat2 > 0, (select kc.valuex from v_kkn2 kc where kc.klucz=P.kat2 and rok in (0, _rok) ), "" ) as katcol2Str,
		IF(P.kat3 > 0, (select kc.valuex from v_kkn3 kc where kc.klucz=P.kat3 and rok in (0, _rok) ), "" ) as katcol3Str,
		IF(P.kat4 > 0, (select kc.valuex from v_kkn4 kc where kc.klucz=P.kat4 and rok in (0, _rok) ), "" ) as katcol4Str,
		IF(P.kat5 > 0, (select kc.valuex from v_ktw0 kc where kc.klucz=P.kat5 and rok in (0, _rok) ), "" ) as katcol5Str,
		IF(P.kat6 > 0, (select kc.valuex from v_ktw1 kc where kc.klucz=P.kat6 and rok in (0, _rok) ), "" ) as katcol6Str,
		IF(P.kat7 > 0, (select kc.valuex from v_ktw2 kc where kc.klucz=P.kat7 and rok in (0, _rok) ), "" ) as katcol7Str,
		IF(P.kat8 > 0, (select kc.valuex from v_ktw3 kc where kc.klucz=P.kat8 and rok in (0, _rok) ), "" ) as katcol8Str,
		IF(P.kat9 > 0, (select kc.valuex from v_ktw4 kc where kc.klucz=P.kat9 and rok in (0, _rok) ), "" ) as katcol9Str,
*/
		IF(max(P.kat0) > 0, (select kc.valuex from v_kkn0 kc, oppo P2 where kc.klucz=P2.kat0 and rok in (0, _rok) and P2.GUID=P.GUID), "" ) as katcol0Str,
		IF(max(P.kat1) > 0, (select kc.valuex from v_kkn1 kc, oppo P2 where kc.klucz=P2.kat1 and rok in (0, _rok) and P2.GUID=P.GUID), "" ) as katcol1Str,
		IF(max(P.kat2) > 0, (select kc.valuex from v_kkn2 kc, oppo P2 where kc.klucz=P2.kat2 and rok in (0, _rok) and P2.GUID=P.GUID), "" ) as katcol2Str,
		IF(max(P.kat3) > 0, (select kc.valuex from v_kkn3 kc, oppo P2 where kc.klucz=P2.kat3 and rok in (0, _rok) and P2.GUID=P.GUID), "" ) as katcol3Str,
		IF(max(P.kat4) > 0, (select kc.valuex from v_kkn4 kc, oppo P2 where kc.klucz=P2.kat4 and rok in (0, _rok) and P2.GUID=P.GUID), "" ) as katcol4Str,
		IF(max(P.kat5) > 0, (select kc.valuex from v_ktw0 kc, oppo P2 where kc.klucz=P2.kat5 and rok in (0, _rok) and P2.GUID=P.GUID), "" ) as katcol5Str,
		IF(max(P.kat6) > 0, (select kc.valuex from v_ktw1 kc, oppo P2 where kc.klucz=P2.kat6 and rok in (0, _rok) and P2.GUID=P.GUID), "" ) as katcol6Str,
		IF(max(P.kat7) > 0, (select kc.valuex from v_ktw2 kc, oppo P2 where kc.klucz=P2.kat7 and rok in (0, _rok) and P2.GUID=P.GUID), "" ) as katcol7Str,
		IF(max(P.kat8) > 0, (select kc.valuex from v_ktw3 kc, oppo P2 where kc.klucz=P2.kat8 and rok in (0, _rok) and P2.GUID=P.GUID), "" ) as katcol8Str,
		IF(max(P.kat9) > 0, (select kc.valuex from v_ktw4 kc, oppo P2 where kc.klucz=P2.kat9 and rok in (0, _rok) and P2.GUID=P.GUID), "" ) as katcol9Str,

		Norig.datawystaw as DataWystawDocKor, Norig.rodzaj as RodzajDocKor,
		_VarStr(nd.symbol) AS AreaSymbol,
		_VarStr(nd.Nazwa) AS AreaNazwa,
		_VarStr(CONCAT('korekta dokumentu: ', IFNULL(od.Opis, ''), '  Nr ', Norig.NrDOkFin, ' wyst. ', Norig.DataWystaw)) AS V_KorektaDotyczy,
		"." AS NrdokfinDocKor,
		Norig.tabela_kursow AS b_tabela_kursow,
		Norig.datawaluty AS b_datawaluty,
		zn.nrzamw, zn.nrzamwkontr,
		P.nrseryjny,
		SUM(C.Changed) AS CHANGED,Tb.wyroznik,
		Tb.jpkgtu
	FROM 
		oppo_corr C
	LEFT JOIN opng N On N.GUID = C.IdnOper_corr  
	LEFT JOIN oppo P On P.GUID  = C.GUID_orig  
	LEFT JOIN opng Norig ON Norig.GUID = P.IdnOperFin
	LEFT JOIN operdoc od ON od.Rodzaj = Norig.Rodzaj
	LEFT JOIN v_erp_vat b_vattab ON  b_vattab.id = C.b_idnvat
	LEFT JOIN v_erp_vat j_vattab ON  j_vattab.id = C.j_idnvat
	LEFT JOIN TowrNg Tb ON Tb.GUID = C.b_idnTowr
	LEFT JOIN TowrNg Tj ON Tj.GUID = C.j_idnTowr 
	LEFT JOIN Jednostki Jb ON Jb.idn = C.b_jednostka
	LEFT JOIN Jednostki Jj ON Jj.idn = C.j_jednostka
	LEFT JOIN Jednostki JPb ON JPb.idn = Tb.jednostka_pom
	LEFT JOIN Jednostki JPj ON JPj.idn = Tj.jednostka_pom
	LEFT JOIN waluty W ON W.idn = N.waluta
	LEFT JOIN erp_area_nodes nd ON nd.GUID = P.areaGUID AND nd.deleted = 0
	LEFT JOIN zmpo zp ON zp.GUID = P.GUIDzm
	LEFT JOIN zmng zn ON zn.GUID = zp.IdnNG
	WHERE
		C.IdnOper_Corr = _GUID
	GROUP BY
		j_idntowr, C.j_cenanet, C.b_cenanet, j_rabat, b_rabat, Norig.GUID
	HAVING IF(@session.HT.IR.Pokaz_wszystkie_pozycje_korekty=0, CHANGED > 0, CHANGED >= 0)
	ORDER BY j_towrsymb
	;

	IF _Param_A <> 'KSEF' THEN -- dla ksef bez selecta
			SELECT * FROM t_korpoz;
	END IF;

END IF; 	-- _MODE = 'SimpleCorrGrp' 
	
	
IF _MODE = 'GRUPOWANIE_INTERNALNE' THEN
	DROP TEMPORARY TABLE IF EXISTS t_korpoz;
	
	-- CALL ht_pr_STCorr('GetCorrPoz','{EB646477-0212-4A5F-8474-7599FA9DEFE3}','','','','');
	-- CALL ht_pr_STCorr('GetCorrPoz','{A954FAD7-6E1E-47B9-9910-F223B96D04FF}','','','',''); przypadek w Sevenet, pozycje grupowane i nie

	-- CALL ht_pr_STCorr('GetCorrPoz','{67FFCF62-BE7D-491E-9EAA-5C2379AEE26F}','','','',''); nowszy przypadek w Sevenet, pozycje grupowane i nie

	-- taka kombinacja, zeby wymusic odpowiednie sortowanie pozycji tak, zeby towar grupowany byl na poczatku, co jest, ale zgodnie z sugestia apro trzeba to przerobic na tabele tymczasowe, gdzie mam najpierw towary glowne i agregaty (oraz towary bez grupowania) i z niej robic wydruk

	SET _przeliczenie_B_N = (SELECT n.przeliczenie_B_N FROM opng n WHERE n.guid= _GUID LIMIT 1);

	drop table if EXISTS tt_oppo_corr;
	CREATE TEMPORARY table tt_oppo_corr
	SELECT * FROM oppo_corr
	WHERE IdnOper_Corr = _GUID AND j_grp_GUID <> ''
	order by j_grp_GUID, if(j_grp_Towrsymb = '' , 1, 0);

	drop table if EXISTS tt_wyn_grp;
	CREATE TEMPORARY table tt_wyn_grp

	select
			'ht_pr_STCorr_GRUPOWANIE_INTERNALNE' AS mode_int_simple,
			max(C.b_grp_TowrSymb) as b_towrsymb,
			max(C.b_grp_TowrNazwa) as b_towrnazwa,
			max(C.j_grp_TowrSymb) as j_towrsymb,
			max(C.j_grp_TowrNazwa) as j_towrnazwa,
			max(IFNULL(C.b_towrnazwa2,'')) as b_towrnazwa2,
			max(IFNULL(C.j_towrnazwa2,'')) as j_towrnazwa2,
			-- (C.b_cenanet - (C.b_cenanet*C.b_rabat)/100)  AS b_cenanet,
			if(SUM(C.b_grp_ilosc)=0,0,SUM(C.b_wartnet)/SUM(C.b_grp_ilosc)) AS b_cenanet,
			-- (C.j_cenanet - (C.j_cenanet*C.j_rabat)/100)  AS j_cenanet,
			if(SUM(C.j_grp_ilosc)=0,0,SUM(C.j_wartnet)/SUM(C.j_grp_ilosc)) AS j_cenanet,
			
			if(SUM(C.b_grp_ilosc)=0,0,SUM(C.b_wartnetwal)/SUM(C.b_grp_ilosc)) AS b_cenanetwal,
			if(SUM(C.j_grp_ilosc)=0,0,SUM(C.j_wartnetwal)/SUM(C.j_grp_ilosc)) AS j_cenanetwal,
	-- max(IF(warunek na poz. glowne, pole, null))
			C.b_rabat,
			C.j_rabat,
			SUM(C.b_grp_ilosc) as b_ilosc,
			SUM(C.j_grp_ilosc) as j_ilosc,
			SUM(C.b_grp_ilosc - C.j_grp_ilosc) as ilosc_diff,
			max(C.b_grp_Jednostka) as b_Jednostka,
			max(C.j_grp_Jednostka) as j_Jednostka,

			case when N.lang_id <> "pl" then IF((JPb.Nazwa = "" or isnull(JPb.Nazwa)), JPb.Nazwa, JPb.Nazwa1) 
			WHEN @session.globals.Owner = "TRANSBUD" AND P.kat9=1 THEN "kg"
			else JPb.Nazwa END As b_JednostkaPom, 
			case when N.lang_id <> "pl" then IF((JPj.Nazwa = "" or isnull(JPj.Nazwa)), JPj.Nazwa, JPj.Nazwa1) 
			WHEN @session.globals.Owner = "TRANSBUD" AND P.kat9=1 THEN "kg"
			else JPj.Nazwa END As j_JednostkaPom, 
			JPb.precyzja as b_JednostkaPomPrecyzja,
			JPj.precyzja as j_JednostkaPomPrecyzja,


			IF(@session.globals.Owner = "TRANSBUD" AND P.kat9=1 , _varDOUBLE(_Rnd(SUM(C.b_grp_ilosc * 1000 / 1), JPb.precyzja)) ,
			_varDOUBLE(_Rnd(SUM(C.b_grp_ilosc * Tb.przelicznik_l / Tb.przelicznik_m), JPb.precyzja))) AS b_iloscPom,

			 IF(@session.globals.Owner = "TRANSBUD" AND P.kat9=1, _varDOUBLE(_Rnd(SUM(C.j_grp_ilosc * 1000 / 1), JPj.precyzja)) ,
			_varDOUBLE(_Rnd(SUM(C.j_grp_ilosc * Tj.przelicznik_l / Tj.przelicznik_m), JPj.precyzja))) AS j_iloscPom,
			_varDOUBLE(_Rnd2(SUM(C.b_wartnetwal)/(_Rnd(SUM(C.b_ilosc * Tb.przelicznik_l / Tb.przelicznik_m), JPb.precyzja)))) as b_cenaPom,
			_varDOUBLE(_Rnd2(SUM(C.j_wartnetwal)/(_Rnd(SUM(C.j_ilosc * Tj.przelicznik_l / Tj.przelicznik_m), JPj.precyzja)))) as j_cenaPom,

			b_vattab.shortname as b_idnVatStr,
			j_vattab.shortname as j_idnVatStr,
			SUM(C.b_wartvat) as b_wartvat,
			SUM(C.j_wartvat) as j_wartvat,
			if(N.przeliczenie_B_N=0,_Rnd2(sum(C.b_wartnetWal)*b_vattab.value), sum(b_wartbrtWal) - _Rnd2(sum(b_wartbrtWal)/(1 + b_vattab.value)))  as b_wartvatWal, 
			if(N.przeliczenie_B_N=0,_Rnd2(sum(C.j_wartnetWal)*j_vattab.value), sum(j_wartbrtWal) - _Rnd2(sum(j_wartbrtWal)/(1 + j_vattab.value)))  as j_wartvatWal, 
			-- sekcja do wyrzucenia w momencie, gdy wartosci bruttowal beda w pelni i zawsze naliczane
			case when sum(C.b_wartBrtWal) <> 0 then sum(C.b_wartBrtWal)
			else sum(C.b_wartnetWal  + _Rnd2(C.b_wartnetWal*b_vattab.value))
			end as b_wartBrtWal,
			case when sum(C.j_wartBrtWal) <> 0 then sum(C.j_wartBrtWal)
			else sum(C.j_wartnetWal  + _Rnd2(C.j_wartnetWal*j_vattab.value))
			end as j_wartBrtWal,

			C.b_kurs,
			SUM(C.b_wartnet) as b_wartnet,
			SUM(C.j_wartnet) as j_wartnet,
			-- SUM(C.b_wartbrt) as b_wartbrt,
			SUM(IF(N.Calc_Ver>2  and C.b_wartbrtWal<>0, C.b_wartbrtWal,C.b_wartbrt)) AS b_wartbrt,
			SUM(IF(N.Calc_Ver>2  and C.j_wartbrtWal<>0, C.j_wartbrtWal,C.j_wartbrt)) AS j_wartbrt,
			
			if(SUM(C.b_grp_ilosc)=0,0,SUM(IF(N.Calc_Ver>2, C.b_wartbrtWal,C.b_wartbrt))/SUM(C.b_grp_ilosc)) as b_cenabrt,
			if(SUM(C.j_grp_ilosc)=0,0,SUM(IF(N.Calc_Ver>2, C.j_wartbrtWal,C.j_wartbrt))/SUM(C.j_grp_ilosc)) as j_cenabrt,
			
			Tj.symbol as symbol,
			C.b_pkwiu,
			C.j_pkwiu,
			Tj.sww as sww,
			Tj.usluga as usluga,
			W.symbol as WalutaStr,
			Tj.treenode,
			Tj.GUID as towrGUID,
			Tj.kod_kreskowy,
			Tb.kod_kreskowy as b_kod_kreskowy,
			Tj.kod_kreskowy as j_kod_kreskowy,
			Tj.pcn,
			Tj.NazwaUProducenta,
			Tj.symboludost,
			GROUP_CONCAT(C.b_uwagi SEPARATOR ' ' ) AS b_uwagi,
			-- C.b_uwagi,
			C.b_idnvat,
			C.j_idnvat,
			C.b_uwagi2,
			C.j_uwagi2,
			GROUP_CONCAT(C.j_uwagi SEPARATOR ' ' ) AS j_uwagi,
			-- C.j_uwagi,
			Tj.oplata_produktowa,
			SUM(C.b_wartnetWal) as b_wartnetWal,
			SUM(C.j_wartnetWal) as j_wartnetWal,
			-- C.j_towrnazwa,
			
			N.waluta,
			_Rnd2(C.b_cenanet - (C.b_cenanet*C.b_rabat)/100)  as CenaNetWalzRab,					-- uch, nie mozna konsekwentnie?
			_Rnd2(C.j_cenanet - (C.j_cenanet*C.j_rabat)/100)  as CenaNetWalzRab_jest,		-- jw.
			
			-- sekcja dla Eneva (Relacom) - rabaty kwotowe
			SUM(_Rnd2(C.j_cenanet * C.j_ilosc) - C.j_wartnet) AS j_wartrabnetto,
			SUM(_Rnd2(C.j_cenanet * C.j_ilosc)) AS j_wart_przed_rabatem_netto,
			SUM(_Rnd2(C.j_cenanet * C.j_ilosc * (1+j_vattab.value))) AS j_wart_przed_rabatem_brutto,
			
			P.grupuj,
			P.IdnOperFin,
			max(ifnull(Jb.nazwa,'')) as b_jednostkaStr,
			max(ifnull(Jj.nazwa,'')) as j_jednostkaStr,
			
			P.wspolplatnosc,
			P.GUIDKorPo,
			C.j_kurs,
			C.j_kurs as kurs_jest, -- jw
			P.LP,
			-- P.kat0, P.kat1, P.kat2, P.kat3, P.kat4, P.kat5, P.kat6, P.kat7, P.kat8, P.kat9,
			IF(max(P.kat0) > 0, (select kc.valuex from v_kkn0 kc, oppo P2 where kc.klucz=P2.kat0 and rok in (0, _rok) and P2.GUID=P.grp_GUID), "" ) as katcol0Str,
			IF(max(P.kat1) > 0, (select kc.valuex from v_kkn1 kc, oppo P2 where kc.klucz=P2.kat1 and rok in (0, _rok) and P2.GUID=P.grp_GUID), "" ) as katcol1Str,
			IF(max(P.kat2) > 0, (select kc.valuex from v_kkn2 kc, oppo P2 where kc.klucz=P2.kat2 and rok in (0, _rok) and P2.GUID=P.grp_GUID), "" ) as katcol2Str,
			IF(max(P.kat3) > 0, (select kc.valuex from v_kkn3 kc, oppo P2 where kc.klucz=P2.kat3 and rok in (0, _rok) and P2.GUID=P.grp_GUID), "" ) as katcol3Str,
			IF(max(P.kat4) > 0, (select kc.valuex from v_kkn4 kc, oppo P2 where kc.klucz=P2.kat4 and rok in (0, _rok) and P2.GUID=P.grp_GUID), "" ) as katcol4Str,
			IF(max(P.kat5) > 0, (select kc.valuex from v_ktw0 kc, oppo P2 where kc.klucz=P2.kat5 and rok in (0, _rok) and P2.GUID=P.grp_GUID), "" ) as katcol5Str,
			IF(max(P.kat6) > 0, (select kc.valuex from v_ktw1 kc, oppo P2 where kc.klucz=P2.kat6 and rok in (0, _rok) and P2.GUID=P.grp_GUID), "" ) as katcol6Str,
			IF(max(P.kat7) > 0, (select kc.valuex from v_ktw2 kc, oppo P2 where kc.klucz=P2.kat7 and rok in (0, _rok) and P2.GUID=P.grp_GUID), "" ) as katcol7Str,
			IF(max(P.kat8) > 0, (select kc.valuex from v_ktw3 kc, oppo P2 where kc.klucz=P2.kat8 and rok in (0, _rok) and P2.GUID=P.grp_GUID), "" ) as katcol8Str,
			IF(max(P.kat9) > 0, (select kc.valuex from v_ktw4 kc, oppo P2 where kc.klucz=P2.kat9 and rok in (0, _rok) and P2.GUID=P.grp_GUID), "" ) as katcol9Str,
			Norig.datawystaw as DataWystawDocKor, Norig.rodzaj as RodzajDocKor,
			_VarStr(nd.symbol) AS AreaSymbol,
			_VarStr(nd.Nazwa) AS AreaNazwa,
			_VarStr(CONCAT('korekta dokumentu: ', IFNULL(od.Opis, ''), '  Nr ', Norig.NrDOkFin, ' wyst. ', Norig.DataWystaw)) AS V_KorektaDotyczy,
			"." as NrdokfinDocKor,
			Norig.tabela_kursow AS b_tabela_kursow,
			Norig.datawaluty AS b_datawaluty,
			zn.nrzamw, zn.nrzamwkontr, 
			P.nrseryjny,
			SUM(C.Changed) AS `Changed`,
			MAX(IF(j_grp_GUID=P.guid,Tb.wyroznik,'')) AS wyroznik,
			Tb.jpkgtu
		from 
			tt_oppo_corr C
		LEFT JOIN opng N On N.GUID = C.IdnOper_corr  
		LEFT JOIN oppo P On P.GUID  = C.GUID_orig  
		LEFT JOIN opng Norig ON Norig.GUID = P.IdnOperFin 
		LEFT JOIN operdoc od ON od.Rodzaj = Norig.Rodzaj
		LEFT JOIN v_erp_vat b_vattab ON  b_vattab.id = C.b_idnvat
		LEFT JOIN v_erp_vat j_vattab ON  j_vattab.id = C.j_idnvat
		LEFT JOIN TowrNg Tb ON Tb.GUID = C.b_idnTowr
		LEFT JOIN TowrNg Tj ON Tj.GUID = C.j_idnTowr 
		LEFT JOIN Jednostki Jb On Jb.idn = C.b_grp_jednostka AND C.b_grp_jednostka
		LEFT JOIN Jednostki Jj On Jj.idn = C.j_grp_jednostka AND C.j_grp_jednostka
		LEFT JOIN Jednostki JPb ON JPb.idn = Tb.jednostka_pom
		LEFT JOIN Jednostki JPj ON JPj.idn = Tj.jednostka_pom
		LEFT JOIN waluty W on W.idn = N.waluta
		LEFT JOIN erp_area_nodes nd ON nd.GUID = P.areaGUID AND nd.deleted = 0
		LEFT JOIN zmpo zp ON zp.GUID = P.GUIDzm
		LEFT JOIN zmng zn ON zn.GUID = zp.IdnNG
		WHERE
			C.IdnOper_Corr = _GUID AND C.j_grp_GUID <> ''
		GROUP BY
			C.j_grp_GUID
		HAVING IF(@session.HT.IR.Pokaz_wszystkie_pozycje_korekty=0, `Changed`>0,`Changed`>=0)
		order by j_grp_GUID, if(j_grp_Towrsymb = '' , 1, 0)
		;

		-- przeliczenie kwot brutto i vat dla roznych sposob przeliczania netto-brutto i dla towarow glownych w grupie
		UPDATE `tt_wyn_grp` p SET
		b_WartVat = IF(_przeliczenie_B_N = 0, (b_WartNet)*(SELECT v.value FROM v_erp_vat v WHERE  v.Id=p.b_idnvat LIMIT 1),(b_WartVat) ),

		b_WartBrt = IF(_przeliczenie_B_N = 0, (b_WartNet)+((b_WartNet)*(SELECT v.value FROM v_erp_vat v WHERE  v.Id=p.b_idnvat LIMIT 1)),(b_WartBrt) ),

		b_WartBrtWal = IF(_przeliczenie_B_N = 0 ,(b_WartNetWal)+((b_WartNetWal)*(SELECT v.value FROM v_erp_vat v WHERE  v.Id=p.b_idnvat LIMIT 1)),(b_WartBrtWal)  )
		;

		UPDATE `tt_wyn_grp` p SET
		j_WartVat = IF(_przeliczenie_B_N = 0, (j_WartNet)*(SELECT v.value FROM v_erp_vat v WHERE  v.Id=p.j_idnvat LIMIT 1),(j_WartVat) ),

		j_WartBrt = IF(_przeliczenie_B_N = 0 ,(j_WartNet)+((j_WartNet)*(SELECT v.value FROM v_erp_vat v WHERE  v.Id=p.j_idnvat LIMIT 1)),(j_WartBrt) ),

		j_WartBrtWal = IF(_przeliczenie_B_N = 0 ,(j_WartNetWal)+((j_WartNetWal)*(SELECT v.value FROM v_erp_vat v WHERE  v.Id=p.j_idnvat LIMIT 1)),(j_WartBrtWal)  )
		;

		CREATE TEMPORARY TABLE t_korpoz
		SELECT * FROM tt_wyn_grp
		UNION ALL
		select
			'ht_pr_STCorr_SimpleCorr_INTERBAL' AS mode_int_simple,
			C.b_towrsymb,
			C.j_towrsymb,
			IF(N.lang_id<>"pl",IF(Tb.nazwa=C.b_towrnazwa, 
			IFNULL(( SELECT tostr FROM lang_translations WHERE to_lang=N.lang_id AND object_name="TowrNg" AND object_key=b_idnTowr LIMIT 1), 
			C.b_towrnazwa), C.b_towrnazwa),C.b_towrnazwa) AS b_towrnazwa,
			IF(N.lang_id<>"pl",IF(Tj.nazwa=C.j_towrnazwa, 
			IFNULL(( SELECT tostr FROM lang_translations WHERE to_lang=N.lang_id AND object_name="TowrNg" AND object_key=j_idnTowr LIMIT 1), 
			C.j_towrnazwa),C.j_towrnazwa),C.j_towrnazwa) AS j_towrnazwa,
			C.b_towrnazwa2,
			C.j_towrnazwa2,
			C.b_cenanet,
			C.j_cenanet,
			
			if(C.b_ilosc=0,0,_Rnd2(C.b_wartnetwal/C.b_ilosc)) AS b_cenanetwal,
			if(C.j_ilosc=0,0,_Rnd2(C.j_wartnetwal/C.j_ilosc)) AS j_cenanetwal,

			C.b_rabat,
			C.j_rabat,
			C.b_ilosc,
			C.j_ilosc,
			C.b_ilosc - C.j_ilosc as ilosc_diff,
			C.b_Jednostka,
			C.j_Jednostka,

			case when N.lang_id <> "pl" then IF((JPb.Nazwa = "" or isnull(JPb.Nazwa)), JPb.Nazwa, JPb.Nazwa1) 
			WHEN @session.globals.Owner = "TRANSBUD" AND P.kat9=1 THEN "kg"
			else JPb.Nazwa END As b_JednostkaPom, 
			case when N.lang_id <> "pl" then IF((JPj.Nazwa = "" or isnull(JPj.Nazwa)), JPj.Nazwa, JPj.Nazwa1) 
			WHEN @session.globals.Owner = "TRANSBUD" AND P.kat9=1 THEN "kg"
			else JPj.Nazwa END As j_JednostkaPom, 
			JPb.precyzja as b_JednostkaPomPrecyzja,
			JPj.precyzja as j_JednostkaPomPrecyzja,
			-- _varDOUBLE(_Rnd(SUM(C.b_ilosc * Tb.przelicznik_l / Tb.przelicznik_m), JPb.precyzja)) as b_iloscPom,
			-- _varDOUBLE(_Rnd(SUM(C.j_ilosc * Tj.przelicznik_l / Tj.przelicznik_m), JPj.precyzja)) as j_iloscPom,
			IF(@session.globals.Owner = "TRANSBUD" AND P.kat9=1, _varDOUBLE(_Rnd(SUM(C.b_ilosc * 1000 / 1), JPb.precyzja)) ,
			_varDOUBLE(_Rnd(SUM(C.b_ilosc * Tb.przelicznik_l / Tb.przelicznik_m), JPb.precyzja))) AS b_iloscPom,
			
			IF(@session.globals.Owner = "TRANSBUD" AND P.kat9=1, _varDOUBLE(_Rnd(SUM(C.j_ilosc * 1000 / 1), JPj.precyzja)) ,
			_varDOUBLE(_Rnd(SUM(C.j_ilosc * Tj.przelicznik_l / Tj.przelicznik_m), JPj.precyzja))) AS j_iloscPom,
			_varDOUBLE(_Rnd2(SUM(C.b_wartnetwal)/(_Rnd(SUM(C.b_ilosc * Tb.przelicznik_l / Tb.przelicznik_m), JPb.precyzja)))) as b_cenaPom,
			_varDOUBLE(_Rnd2(SUM(C.j_wartnetwal)/(_Rnd(SUM(C.j_ilosc * Tj.przelicznik_l / Tj.przelicznik_m), JPj.precyzja)))) as j_cenaPom,

			b_vattab.shortname as b_idnVatStr,
			j_vattab.shortname as j_idnVatStr,
			C.b_wartvat,
			C.j_wartvat,
			if(N.przeliczenie_B_N=0,_Rnd2(C.b_wartnetWal*b_vattab.value), b_wartbrtWal - _Rnd2(b_wartbrtWal/(1 + b_vattab.value)))  as b_wartvatWal, 
			if(N.przeliczenie_B_N=0,_Rnd2(C.j_wartnetWal*j_vattab.value), j_wartbrtWal - _Rnd2(j_wartbrtWal/(1 + j_vattab.value)))  as j_wartvatWal,
				-- sekcja do wyrzucenia w momencie, gdy wartosci bruttowal beda w pelni i zawsze naliczane
			case when C.b_wartBrtWal <> 0 then C.b_wartBrtWal
			else C.b_wartnetWal  + _Rnd2(C.b_wartnetWal*b_vattab.value)						
			end as b_wartBrtWal,
			case when C.j_wartBrtWal <> 0 then C.j_wartBrtWal
			else C.j_wartnetWal  + _Rnd2(C.j_wartnetWal*j_vattab.value)						
			end as j_wartBrtWal,

			C.b_kurs,
			C.b_wartnet,
			C.j_wartnet,
			 -- C.b_wartbrt,
			IF(N.Calc_Ver>2  and C.b_wartbrtWal<>0,C.b_wartBrtWal,C.b_wartbrt) as b_wartbrt,
			IF(N.Calc_Ver>2  and C.j_wartbrtWal<>0,C.j_wartBrtWal,C.j_wartbrt) as j_wartbrt,

			C.b_cenabrt,
			C.j_cenabrt,

			Tj.symbol as symbol,
			C.b_pkwiu,
			C.j_pkwiu,
			Tj.sww as sww,
			Tj.usluga as usluga,
			W.symbol as WalutaStr,
			Tj.treenode,
			Tj.GUID as towrGUID,
			Tj.kod_kreskowy,
			Tb.kod_kreskowy as b_kod_kreskowy,
			Tj.kod_kreskowy as j_kod_kreskowy,
			Tj.pcn,
			Tj.NazwaUProducenta,
			Tj.symboludost,
			C.b_uwagi,
			C.b_idnvat,
			C.j_idnvat,
			C.b_uwagi2,
			C.j_uwagi2,
			C.j_uwagi,
			Tj.oplata_produktowa,
			C.b_wartnetWal,
			C.j_wartnetWal,
			-- C.j_towrnazwa,
			
			N.waluta,
			_Rnd2(C.b_cenanet - (C.b_cenanet*C.b_rabat)/100)  as CenaNetWalzRab,
			_Rnd2(C.j_cenanet - (C.j_cenanet*C.j_rabat)/100)  as CenaNetWalzRab_jest,
			
			-- sekcja dla Eneva (Relacom) - rabaty kwotowe
			_Rnd2(C.j_cenanet * C.j_ilosc) - C.j_wartnet AS j_wartrabnetto,
			_Rnd2(C.j_cenanet * C.j_ilosc) AS j_wart_przed_rabatem_netto,
			_Rnd2(C.j_cenanet * C.j_ilosc * (1+j_vattab.value)) AS j_wart_przed_rabatem_brutto,
			
			P. grupuj,
			P.IdnOperFin,
			Jb.nazwa as b_jednostkaStr,
			Jj.nazwa as j_jednostkaStr,
			P.wspolplatnosc,
			P.GUIDKorPo,
			C.j_kurs,
			C.j_kurs as kurs_jest, 
			P.LP,
			-- P.kat0, P.kat1, P.kat2, P.kat3, P.kat4, P.kat5, P.kat6, P.kat7, P.kat8, P.kat9,
			IF(P.kat0 > 0, (select kc.valuex from v_kkn0 kc where kc.klucz=P.kat0 and rok in (0, _rok) ), "" ) as katcol0Str,
			IF(P.kat1 > 0, (select kc.valuex from v_kkn1 kc where kc.klucz=P.kat1 and rok in (0, _rok) ), "" ) as katcol1Str,
			IF(P.kat2 > 0, (select kc.valuex from v_kkn2 kc where kc.klucz=P.kat2 and rok in (0, _rok) ), "" ) as katcol2Str,
			IF(P.kat3 > 0, (select kc.valuex from v_kkn3 kc where kc.klucz=P.kat3 and rok in (0, _rok) ), "" ) as katcol3Str,
			IF(P.kat4 > 0, (select kc.valuex from v_kkn4 kc where kc.klucz=P.kat4 and rok in (0, _rok) ), "" ) as katcol4Str,
			IF(P.kat5 > 0, (select kc.valuex from v_ktw0 kc where kc.klucz=P.kat5 and rok in (0, _rok) ), "" ) as katcol5Str,
			IF(P.kat6 > 0, (select kc.valuex from v_ktw1 kc where kc.klucz=P.kat6 and rok in (0, _rok) ), "" ) as katcol6Str,
			IF(P.kat7 > 0, (select kc.valuex from v_ktw2 kc where kc.klucz=P.kat7 and rok in (0, _rok) ), "" ) as katcol7Str,
			IF(P.kat8 > 0, (select kc.valuex from v_ktw3 kc where kc.klucz=P.kat8 and rok in (0, _rok) ), "" ) as katcol8Str,
			IF(P.kat9 > 0, (select kc.valuex from v_ktw4 kc where kc.klucz=P.kat9 and rok in (0, _rok) ), "" ) as katcol9Str,
			Norig.datawystaw as DataWystawDocKor, Norig.rodzaj as RodzajDocKor,
			_VarStr(nd.symbol) AS AreaSymbol,
			_VarStr(nd.Nazwa) AS AreaNazwa,
			_VarStr(CONCAT('korekta dokumentu: ', IFNULL(od.Opis, ''), '  Nr ', Norig.NrDOkFin, ' wyst. ', Norig.DataWystaw)) AS V_KorektaDotyczy,
			"." as NrdokfinDocKor,
			Norig.tabela_kursow AS b_tabela_kursow,
			Norig.datawaluty AS b_datawaluty,
			zn.nrzamw, zn.nrzamwkontr,
			P.nrseryjny,
			C.Changed,
			Tb.wyroznik,
			Tb.jpkgtu
		FROM 
			oppo_corr C
		LEFT JOIN opng N On N.GUID = C.IdnOper_corr  						-- naglowek korekty
		LEFT JOIN oppo P On P.GUID  = C.GUID_orig  							-- pozycja oryginalna
		LEFT JOIN opng Norig ON Norig.GUID = P.IdnOperFin					-- naglowek oryginalu
		LEFT JOIN operdoc od ON od.Rodzaj = Norig.Rodzaj
		LEFT JOIN v_erp_vat b_vattab ON  b_vattab.id = C.b_idnvat
		LEFT JOIN v_erp_vat j_vattab ON  j_vattab.id = C.j_idnvat
		LEFT JOIN TowrNg Tb ON Tb.GUID = C.b_idnTowr
		LEFT JOIN TowrNg Tj ON Tj.GUID = C.j_idnTowr 
		LEFT JOIN Jednostki Jb On Jb.idn = C.b_jednostka
		LEFT JOIN Jednostki Jj On Jj.idn = C.j_jednostka
		LEFT JOIN Jednostki JPb ON JPb.idn = Tb.jednostka_pom
		LEFT JOIN Jednostki JPj ON JPj.idn = Tj.jednostka_pom
		LEFT JOIN waluty W on W.idn = N.waluta
		LEFT JOIN erp_area_nodes nd ON nd.GUID = P.areaGUID AND nd.deleted = 0
		LEFT JOIN zmpo zp ON zp.GUID = P.GUIDzm
		LEFT JOIN zmng zn ON zn.GUID = zp.IdnNG
		WHERE
			IF(@session.HT.IR.Pokaz_wszystkie_pozycje_korekty=0, C.Changed>0,C.Changed>=0)
			AND C.IdnOper_Corr = _GUID AND C.j_grp_GUID = ''
			GROUP BY
			C.GUID
		ORDER BY j_towrsymb
		;

		IF _Param_A <> 'KSEF' THEN -- dla ksef bez selecta
				SELECT * FROM t_korpoz;
		END IF;

END IF; -- GRUPOWANIE_INTERNALNE
	

END$$

DELIMITER ;

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

SELECT GUID FROM kontrah WHERE Symbol = '2410 D';
SELECT
GUID
FROM opng n
WHERE
rodzaj = 3
AND kontrGUID = '{8A61D1A6-6C56-434C-94B8-08E06564FABD}'
AND datawystaw > '2012-01-10'
AND nrDokFin = '00535/2012/KOR'

;
SET @IdnOper = '{9DD22730-4745-11E1-8DD0-002481F9239A}';

CALL ht_pr_STCorr('SimpleCorrGrp', @IdnOper, '', '', '', '');

SELECT
GUID
FROM opng n
WHERE
rodzaj = 3
AND datawystaw > '2012-01-15'
AND nrDokFin = '00568/2012/KOR'

;
SET @IdnOper = '{E4DC0788-AE20-4AAA-A130-89ED3EFC0F30}';
SELECT
n.rodzaj, n.rodzaj_typ, p.*
FROM opng n
LEFT JOIN oppo p ON p.IdnOper = n.GUID
LEFT JOIN oppo_corr C ON C.IdnOper_Corr = n.GUID 
WHERE
n.GUID = @IdnOper

;
SELECT
GUID
FROM opng n
WHERE
rodzaj = 1
-- AND kontrGUID = '{8A61D1A6-6C56-434C-94B8-08E06564FABD}'
AND datawystaw > '2012-01-15'
AND nrDokFin = '002359/2012/P'

;

SELECT
p.GUID,
p.TowrSymb,
p.Ilosc,
p.CenaNet,
p.WartNet
FROM oppo p
WHERE
p.IdnOperFin = '{C3F2360D-11DA-4FD7-9788-9D79A14464CD}'
;



SELECT
n.rodzaj, n.rodzaj_typ, n.*
FROM opng n
WHERE
rodzaj = 3
-- AND datawystaw > '2012-01-15'
-- AND nrDokFin = '00568/2012/KOR'
ORDER BY IDN DESC LIMIT 10
;
	
CALL ht_pr_STCorr('SimpleCorrGrp', '{A594C07A-4BED-11E1-8DD0-002481F9239A}', '', '', '', '');
	
	
*/
	
