 DELIMITER $$


DROP PROCEDURE IF EXISTS `xxng_vat_TOOLS`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `xxng_vat_TOOLS`(
	IN _MODE VARCHAR(250),
	IN _FROM VARCHAR(250),		--	'OP' lub 'ZM'
	IN _Swiat VARCHAR(250),		--	'' lub 'E'
	IN _Key VARCHAR(255),
	IN _Params MEDIUMTEXT,	-- np. "Clear_Fixed"
	IN _Param_A VARCHAR(255),
	IN _Param_B VARCHAR(255),
	IN _Param_C VARCHAR(255)
	-- IN _TableName VARCHAR(255),
	-- IN _Netto_do_Brutto BOOLEAN
)
/*
	Nowsza od 20217 roku
*/
S_xxng_vat_TOOLS: BEGIN 
	DECLARE _Clear_Fixed BOOLEAN DEFAULT FALSE;
	DECLARE _SQLText, _SQLText_Zwykly, _SQLText_Korekta MEDIUMTEXT;
	DECLARE _xxpo_TableName, _xxng_TableName, _xxng_vat_TableName VARCHAR(255) DEFAULT '';
	
											IF _MODE = 'CREATE TEMPORARY TABLE.`t_xxng_vat_INF`' THEN
											--	Tworzenie `t_xxng_vat_INF` ale !!! IF NOT EXISTS potem badam ....
												SET @.EXISTS.table.ROW_COUNT = ROW_COUNT();
												CREATE TEMPORARY TABLE IF NOT EXISTS `t_xxng_vat_INF` (
												  `_FROM` VARCHAR(100) NOT NULL DEFAULT '',
												  `_Swiat` VARCHAR(100) NOT NULL DEFAULT '',
												  `_Params` VARCHAR(255) NOT NULL DEFAULT '',
												  `GUID` VARCHAR(38) NOT NULL DEFAULT '',
												  `Rodzaj` INT(11) NOT NULL DEFAULT '0',
												  `rodzaj_typ` INT(11) DEFAULT NULL,
												  `Calc_Ver` INT(11) NOT NULL DEFAULT '0',
												  `Kurs` DOUBLE(16,4) NOT NULL DEFAULT '1.0000',
												  `koszt_nabycia_coeff` DOUBLE(16,4) NOT NULL DEFAULT '1.0000',
												  `blokada_przeliczenia_tab_vat` INT(11) DEFAULT '0',
												  `przeliczenie_B_N` INT(11) NOT NULL DEFAULT '0',
												  `Netto_do_Brutto` BOOLEAN NOT NULL DEFAULT '0',
												  `IS_Korekta` BOOLEAN NOT NULL DEFAULT '0',
												  `IS_Kancelaria` BOOLEAN NOT NULL DEFAULT '0',
												  `IS_PodlegaVAT` BOOLEAN NOT NULL DEFAULT '0',
												  `IS_PozycjeVisible` BOOLEAN NOT NULL DEFAULT '0',
												  `_Clear_Fixed` BOOLEAN NOT NULL DEFAULT '0',
												  -- `_ExtFlags` VARCHAR(250),
												  `_xxng_TableName` VARCHAR(200) NOT NULL DEFAULT '',
												  `_xxng_vat_TableName` VARCHAR(200) NOT NULL DEFAULT '',
												  `_xxpo_TableName` VARCHAR(200) NOT NULL DEFAULT '',
												  `_SQLText` MEDIUMTEXT
												)
												;
											--	wartosc RaOW_COUNT(); i gdy < 0 to byla tabela a gdy 0 to nie bylo jej 
											-- select ROW_COUNT();
												SET @.EXISTS.table.ROW_COUNT = ROW_COUNT();
												SET @.EXISTS.table.t_xxng_vat_INF = (@.EXISTS.table.ROW_COUNT < 0);
											END IF;
				
											IF _MODE = 'EXISTS.`t_xxng_vat_INF`' THEN
												CALL xxng_vat_TOOLS('CREATE TEMPORARY TABLE.`t_xxng_vat_INF`', '', '', '', '', '', '', ''); 
												SET @.EXISTS.t_xxng_vat_INF = NULL;
												SET _SQLText = 
												"
													SET @.EXISTS.t_xxng_vat_INF = 
													(
													SELECT 
														COUNT(*) 
													FROM `t_xxng_vat_INF` 
													WHERE 
														GUID = _Key
														AND _FROM = #_FROM#
														AND _Swiat = #_Swiat#
													) = 1
													;
												"
												;
								set @xxxx = 2.1;
												SET _SQLText = REPLACE(REPLACE(REPLACE(_SQLText, '_Key', QUOTE(_Key)), '#_FROM#', QUOTE(_FROM)), '#_Swiat#', QUOTE(_Swiat));
												SET @_SQLText = _SQLText;
								set @xxxx = 2.2;
												CALL _ExecuteCmd(_SQLText, @Error);
								set @xxxx = 2.3;
												SET @.EXISTS.t_xxng_vat_INF = IFNULL(@.EXISTS.t_xxng_vat_INF, FALSE);
								set @xxxx = 2.4;
											END IF;
				
				IF _MODE = 'PREPRATE.`t_xxng_vat_INF` IF NOT EXISTS OR INVALID' THEN
				--	UWAGA gdy _Param_A = "" to sprawdzamy ale gdy <> "" to uznajemy ze w `t_xxng_vat_INF` jest juz wlasciwy rekord
	set @xxxx = 1;
					IF IFNULL(_Param_A, '') = '' THEN
						SET @.EXISTS.t_xxng_vat_INF = NULL;
					ELSE
						SET @.EXISTS.t_xxng_vat_INF = TRUE;
						LEAVE S_xxng_vat_TOOLS;
					END IF;
	set @xxxx = 2;
					
					CALL xxng_vat_TOOLS('EXISTS.`t_xxng_vat_INF`', _FROM, _Swiat, _Key, _Params, _Param_A, _Param_B, _Param_C);
	set @xxxx = 3;
					IF NOT @.EXISTS.t_xxng_vat_INF THEN
	set @xxxx = 4;
						CALL xxng_vat_TOOLS('Preprae.`t_xxng_vat_INF`', _FROM, _Swiat, _Key, _Params, _Param_A, _Param_B, _Param_C); 
					END IF;
					
					
				END IF;
				
				IF _MODE = 'SQLTEXT.Execute' THEN
					IF IFNULL(_Params, '') != '' THEN
						UPDATE `t_xxng_vat_INF` n
						SET
							n._SQLText = _Params
						;
					END IF;
					
					UPDATE `t_xxng_vat_INF` n
					SET
						n._SQLText = 
							-- REPLACE(REPLACE(n._SQLText, '#_xxng_vat_TableName#', n._xxng_vat_TableName), '_Key', QUOTE(n.GUID))
							REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(n._SQLText, 
								'#_xxng_vat_TableName#', n._xxng_vat_TableName), 
								'#_xxpo_TableName#', n._xxpo_TableName), 
								'#_xxpo_TableName_b#', IF(n._Swiat = 'TE' AND n.IS_Korekta, 'TE_oppo_corr_b', n._xxpo_TableName)), 
								'#_xxng_TableName#', n._xxng_TableName), 
								'_Key', QUOTE(n.GUID)
							)
					;
					SELECT 
						n._SQLText
					INTO
						_SQLText
					FROM `t_xxng_vat_INF` n
					;
					SET @_SQLText = _SQLText;
					SET @_SQLTextX1 = _SQLText;
					CALL _ExecuteCmd(_SQLText, @Error);
				
				END IF;
				
				
				
	IF _MODE = 'Preprae.`t_xxng_vat_INF`' THEN
set @co.jest = 1;
set @.1._Key = _Key;
	set @xxxx = 10;
		IF IFNULL(_Key, '') = '' THEN
			LEAVE S_xxng_vat_TOOLS;
		END IF;

		SET @.xxng_vat.OP = (_FROM != 'ZM');
		IF (@.xxng_vat.OP) THEN
		--	OP
			SET 
				_xxng_TableName = IF(_Swiat = 'M', 'opng_m', IF(_Swiat LIKE '%TE%', 'TE_opng', IF(_Swiat LIKE '%E%', 'opng_e', 'opng'))),
				_xxng_vat_TableName = IF(_Swiat = 'M', 'opng_vat_m', IF(_Swiat LIKE '%TE%', 'TE_opng_vat', IF(_Swiat LIKE '%E%', 'opng_vat_e', 'opng_vat')))
			;
		ELSE
		--	ZM
			SET 
				_xxng_TableName = IF(_Swiat = 'M', 'zmng_m', IF(_Swiat LIKE '%TE%', 'TE_zmng', IF(_Swiat LIKE '%E%', 'zmng_e', 'zmng'))),
				_xxng_vat_TableName = IF(_Swiat = 'M', 'zmng_vat_m', IF(_Swiat LIKE '%TE%', 'TE_zmng_vat', IF(_Swiat LIKE '%E%', 'zmng_vat_e', 'zmng_vat')))
			;
		END IF;
set
@_xxng_TableName = _xxng_TableName,	
@_xxng_vat_TableName = _xxng_vat_TableName
;	
	set @xxxx = 10;

		SET _Params = IFNULL(_Params, '');
		SET @.xxng_vat.koszt_nabycia.coeff = IF(IFNULL(@session.HT.IR.PomniejszZTUEOKosztNabycia, FALSE), 1, 0);
		SET @.xxng_vat.koszt_nabycia.coeff = _VarDouble(IFNULL(@.xxng_vat.koszt_nabycia.coeff, 0.0));
		SET _Clear_Fixed = (_Params LIKE '%Clear_Fixed%');
		
		DROP TEMPORARY TABLE IF EXISTS `t_xxng_vat_INF`;
		CALL xxng_vat_TOOLS('CREATE TEMPORARY TABLE.`t_xxng_vat_INF`', '', '', '', '', '', '', ''); 
		/* zmaiast tego ...
		CREATE TEMPORARY TABLE `t_xxng_vat_INF` (
		  `_FROM` VARCHAR(100) NOT NULL DEFAULT '',
		  `_Swiat` VARCHAR(100) NOT NULL DEFAULT '',
		  `_Params` VARCHAR(255) NOT NULL DEFAULT '',
		  `GUID` VARCHAR(38) NOT NULL DEFAULT '',
		  `Rodzaj` INT(11) NOT NULL DEFAULT '0',
		  `rodzaj_typ` INT(11) DEFAULT NULL,
		  `Calc_Ver` INT(11) NOT NULL DEFAULT '0',
		  `Kurs` DOUBLE(16,4) NOT NULL DEFAULT '1.0000',
		  `koszt_nabycia_coeff` DOUBLE(16,4) NOT NULL DEFAULT '1.0000',
		  `blokada_przeliczenia_tab_vat` INT(11) DEFAULT '0',
		  `przeliczenie_B_N` INT(11) NOT NULL DEFAULT '0',
		  `Netto_do_Brutto` BOOLEAN NOT NULL DEFAULT '0',
		  `IS_Korekta` BOOLEAN NOT NULL DEFAULT '0',
		  `IS_Kancelaria` BOOLEAN NOT NULL DEFAULT '0',
		  `IS_PodlegaVAT` BOOLEAN NOT NULL DEFAULT '0',
		  `IS_PozycjeVisible` BOOLEAN NOT NULL DEFAULT '0',
		  `_Clear_Fixed` BOOLEAN NOT NULL DEFAULT '0',
		  `_xxng_TableName` VARCHAR(200) NOT NULL DEFAULT '',
		  `_xxng_vat_TableName` VARCHAR(200) NOT NULL DEFAULT '',
		  `_xxpo_TableName` VARCHAR(200) NOT NULL DEFAULT '',
		  `_SQLText` MEDIUMTEXT
		)
		;
		*/
		INSERT INTO `t_xxng_vat_INF`
		(_FROM, _Swiat, _Params, GUID, _xxng_TableName, _xxng_vat_TableName, _xxpo_TableName, koszt_nabycia_coeff, _Clear_Fixed)
		VALUES
		(_FROM, _Swiat, _Params, _Key, _xxng_TableName, _xxng_vat_TableName, _xxpo_TableName, @.xxng_vat.koszt_nabycia.coeff, _Clear_Fixed)
		;
		
		SET _SQLText = 
		"\n
		UPDATE `t_xxng_vat_INF` t, `#ng#` n
		SET
			t.Rodzaj = (@.xxng_vat.Rodzaj := n.Rodzaj), 
			t.rodzaj_typ = (@.xxng_vat.Rodzaj_typ := n.rodzaj_typ), 
			t.Calc_Ver = n.Calc_Ver,
			t.blokada_przeliczenia_tab_vat = #blokada_przeliczenia_tab_vat#, 
			t.przeliczenie_B_N = n.przeliczenie_B_N, 
			t.Netto_do_Brutto = (@.xxng_vat._Netto_do_Brutto := NOT n.przeliczenie_B_N),
			t.Kurs = n.Kurs,
			t.IS_Kancelaria = #IS_Kancelaria#,
			t.IS_Korekta = FALSE, 
			-- t._ExtFlags = n.ExtFlags
			t._Params = IFNULL(CONCAT_WS('&', NULLIF(t._Params, ''), NULLIF(n.ExtFlags, '')), '')
			-- FROM `#ng#` n 
			WHERE 
				n.GUID = _Key
		;
		";
		
		
		SET _SQLText = REPLACE(_SQLText, '#ng#', _xxng_TableName);
		SET _SQLText = REPLACE(_SQLText, '_Key', QUOTE(_Key));
		SET _SQLText = REPLACE(_SQLText, '#IS_Kancelaria#', IF(@.xxng_vat.OP, "(IFNULL(n.lp_ks_podawcza_str, '') != '')", FALSE));
		SET _SQLText = REPLACE(_SQLText, '#blokada_przeliczenia_tab_vat#', IF(@.xxng_vat.OP, "n.blokada_przeliczenia_tab_vat", FALSE));
		SET @_SQLText.UPD.1 = _SQLText;
		SET @_SQLText = _SQLText;
		CALL _ExecuteCmd(_SQLText, @Error);



		CALL ht_rodzaj_Recalc(@.xxng_vat.Rodzaj);
		
		IF (@.xxng_vat.OP) THEN
		--	OP
			SET @.xxng_vat.ZwyklyDoc = NOT(@ht.doc.IsKorekta AND (@.xxng_vat.Rodzaj_typ != 0));		--	nie jest to korekta nowego typu
			IF NOT @.xxng_vat.ZwyklyDoc THEN
			--	korekta nowego typu
				SET _xxpo_TableName = IF(_Swiat = 'M', 'oppo_corr_m', IF(_Swiat LIKE '%TE%', 'TE_oppo_corr', IF(_Swiat LIKE '%E%', 'oppo_corr_e', 'oppo_corr')));
			ELSE	
				SET _xxpo_TableName = IF(_Swiat = 'M', 'oppo_m', IF(_Swiat LIKE '%TE%', 'TE_oppo', IF(_Swiat LIKE '%E%', 'oppo_e', 'oppo')));
			END IF;
		ELSE
		--	ZM
			SET _xxpo_TableName = IF(_Swiat = 'M', 'zmpo_m', IF(_Swiat LIKE '%TE%', 'TE_zmpo', IF(_Swiat LIKE '%E%', 'zmpo_e', 'zmpo')));
		END IF;
		
		UPDATE `t_xxng_vat_INF` n
		SET
			n._xxpo_TableName = _xxpo_TableName,
			n.IS_Korekta = (@.xxng_vat.OP) AND NOT @.xxng_vat.ZwyklyDoc,
			n.IS_PodlegaVAT = @ht.doc.IsPodlegaVAT,
			n.IS_PozycjeVisible = @ht.doc.IsPozycjeVisible

		;
		
		
	END IF;

	IF _MODE IN ('`t_xxng_vat_z_poz` <- `xxpo`', 'Preprae.`t_xxng_vat_z_poz`') THEN
	/* ******************************************************************************************************************************
		Przygotowuje tabelke vat z pozycji wg `t_xxng_vat_INF`
		CALL xxng_vat_TOOLS('`t_xxng_vat_z_poz` <- `xxpo`', '', '', '', '', '', '', ''); -- zam
		
		CALL xxng_vat_TOOLS('Preprae.`t_xxng_vat_z_poz`', '', '', '', '', '', '', ''); -- zam
	*********************************************************************************************************************************/
	set @xxxx = 15;
	--	Z W Y K L Y  D O K U M E N T  -   N I E   K O R E K T A
		SET _SQLText_Zwykly = 
			'\n
			CREATE TEMPORARY TABLE `t_xxng_vat_z_poz_N` 
				SELECT
					_Key AS IdnOper,			
					p.idnvat AS IdVat,		
				--	Netto	// gdy włączone jest odejmowanie kosztów naby cia to @Session.vat.koszt_nabycia.coeff = 1 ?????? APRO się zastanowi
					_Rnd2(SUM(p.WartNet) - #koszt_nabycia#) AS Netto,		
				--	Vat
					_Rnd2(SUM(p.WartVat)) AS Vat,		
				--	Brutto
					_Rnd2(SUM(p.WartBrt)) AS Brutto,		
				--	NettoWal
					_Rnd2(SUM(p.WartNetWal)) AS NettoWal,		
				--	VatWal
					_Rnd2(SUM(p.WartBrtWal - p.WartNetWal)) AS VatWal,		
				--	BruttoWal
					_Rnd2(SUM(p.WartBrtWal)) AS BruttoWal,
				--	OperCount
					_VarInt(Count(*)) AS OperCount
				#FROM#	
				WHERE
					#IdnOper#
					AND 
					(p.Ilosc <> 0 OR p.WartVat <> 0)
				GROUP BY 
					p.idnvat	
				
			;
		';
set @_Swiat = _Swiat;	
	--	K O R E K T A
		IF _Swiat = 'TE' AND @.xxng_vat.OP THEN
			DROP TEMPORARY TABLE IF EXISTS `TE_oppo_corr_b`;
			CREATE TEMPORARY TABLE `TE_oppo_corr_b` SELECT p.* FROM `TE_oppo_corr` p WHERE p.IdnOper_corr = _Key;
		END IF;
		SET _SQLText_Korekta = 
			'\n
			CREATE TEMPORARY TABLE `t_xxng_vat_z_poz_N` 
			(
				SELECT
					_Key AS IdnOper,			--	IdnOper
					p.j_idnvat AS IdVat,		--	IdVat
				--	Netto	
					_Rnd2(SUM(p.j_WartNet)) AS Netto,		
				--	Vat
					_Rnd2(0) AS Vat,		
				--	Brutto
					_Rnd2(SUM(p.j_WartBrt)) AS Brutto,	
				--	NettoWal
					_Rnd2(SUM(p.j_WartNetWal)) AS NettoWal,		
				--	VatWal
					_Rnd2(SUM(p.j_WartBrtWal - p.j_WartNetWal)) AS VatWal,		
				--	BruttoWal
					_Rnd2(SUM(p.j_WartBrtWal)) AS BruttoWal,
				--	OperCount
					_VarInt(Count(*))  AS OperCount
				FROM `#_xxpo_TableName#` p
				WHERE
					p.IdnOper_corr = _Key
					AND p.Temporary = 0
					AND p.Fixed = 0
				GROUP BY 
					p.j_idnvat	
			)
			UNION 		--	HAHA
			(
				SELECT
					_Key AS IdnOper,			--	IdnOper
					p.b_idnvat AS IdVat,		--	IdVat
				--	Netto	
					_Rnd2(SUM(-p.b_WartNet)) AS Netto,		
				--	Vat
					_Rnd2(0) AS Vat,	
				--	Brutto
					_Rnd2(SUM(-p.b_WartBrt)) AS Brutto,	
				--	NettoWal
					_Rnd2(SUM(-p.b_WartNetWal)) AS NettoWal,		
				--	VatWal
					_Rnd2(SUM(-p.b_WartBrtWal + p.b_WartNetWal)) AS VatWal,		
				--	BruttoWal
					_Rnd2(SUM(-p.b_WartBrtWal)) AS BruttoWal,
				--	OperCount
					_VarInt(Count(*)) AS OperCount
				FROM `#_xxpo_TableName_b#` p
				WHERE
					p.IdnOper_corr = _Key
					AND p.Temporary = 0
					AND p.Fixed = 0
				GROUP BY 
					p.b_idnvat	
			)
			;
		';
		set @_SQLText_Korekta = _SQLText_Korekta;
		UPDATE `t_xxng_vat_INF` n
		SET
			n._SQLText = IF(n.IS_Korekta, _SQLText_Korekta, _SQLText_Zwykly)
		;
	--	dla NIE KOREKT (oper/zm)
		UPDATE `t_xxng_vat_INF` n
		SET
		--	Nie dla korekt (zwykle operacje + zamowienia)
			n._SQLText = 
				IF(n._FROM = 'OP',
				--	operacje	
					REPLACE(n._SQLText, '#FROM#', 'FROM `#_xxpo_TableName#` p'),
				--	zamowienia
					REPLACE(n._SQLText, '#FROM#', 'FROM `#_xxng_TableName#` n	\n						LEFT JOIN `#_xxpo_TableName#` p ON p.IdnNg = n.GUID AND p.Wersja = n.Wersja')
				)
		WHERE
			NOT n.IS_Korekta	-- Nie dla korekt (zwykle operacje + zamowienia)
		;
		UPDATE `t_xxng_vat_INF` n
		SET
		--	Nie dla korekt (zwykle operacje + zamowienia)

			n._SQLText = (@.n._SQLText.IS_Korekta :=
				IF(n._FROM = 'OP',
				--	operacje	
					REPLACE(REPLACE(REPLACE(n._SQLText, '#IdnOper#', '(p.IdnOper = _Key OR p.IdnOperFin = _Key)'), '#koszt_nabycia#', 'SUM(n.koszt_nabycia_coeff * p.koszt_nabycia)'), 'n.koszt_nabycia_coeff', n.koszt_nabycia_coeff), 
				--	zamowienia
					REPLACE(REPLACE(n._SQLText, '#IdnOper#', '(n.GUID = _Key)'), '#koszt_nabycia#', '0')
				)
			)
		WHERE
			NOT n.IS_Korekta	-- Nie dla korekt (zwykle operacje + zamowienia)
		;
		
		DROP TEMPORARY TABLE IF EXISTS `t_xxng_vat_z_poz_N`;
		CALL xxng_vat_TOOLS('SQLTEXT.Execute', '', '', '', NULL, '', '', ''); 
		SET @_SQLText_Korekta.fin = @_SQLTextX1;
		
		--	GDY Netto -> Brutto
			UPDATE `t_xxng_vat_z_poz_N` v, v_erp_vat ev, `t_xxng_vat_INF` n
			SET
				v.VatWal = _Rnd2(v.NettoWal*(ev.`value`)),
				v.BruttoWal = _Rnd2(v.NettoWal + _Rnd2(v.NettoWal*(ev.`value`))),
				v.Vat = _Rnd2(v.Netto*(ev.`value`)),
				v.Brutto = _Rnd2(v.Netto+ _Rnd2(v.Netto*(ev.`value`)))
				/*
				Wariant przy edycji tabelki w walucie czyli Calc_ver > 1
				do dalszych badań kwestie zaokrągleń gdyż PLN będą inaczej liczone 
									v.Vat = _Rnd2(v.VatWal*kurs?),
									v.Netto = _Rnd2(v.NettoWal*kurs?)),
									v.Brutto = _Rnd2(v.VatWal*kurs?) + _Rnd2(v.NettoWal*kurs?))
				*/
			WHERE
				v.IdnOper = _Key 
				AND n.Netto_do_Brutto	--	GDY Netto -> Brutto
				AND ev.id = v.IdVat 
			;
		--	GDY Brutto -> Netto
			UPDATE `t_xxng_vat_z_poz_N` v, v_erp_vat ev, `t_xxng_vat_INF` n
			SET
				v.NettoWal = _Rnd2(v.BruttoWal/(1 + ev.`value`)),
				v.VatWal = v.BruttoWal - _Rnd2(v.BruttoWal/(1 + ev.`value`)),
				v.Netto = _Rnd2(v.Brutto/(1 + ev.`value`)),
				v.Vat = v.Brutto - _Rnd2(v.Brutto/(1 + ev.`value`))
			WHERE
				v.IdnOper = _Key 
				AND NOT n.Netto_do_Brutto	--	GDY Brutto -> Netto
				AND ev.id = v.IdVat 
			;
			/*
				v.Vat = _Rnd2(v.VatWal*kurs?),
				v.Netto = _Rnd2(v.NettoWal*kurs?)),
				v.Brutto = _Rnd2(v.VatWal*kurs?) + _Rnd2(v.NettoWal*kurs?))
			*/

	--	UWAGA bywa np. dla korekt że mamy zmiany tylko w xxxWal a w xxx sa wartosci nie naliczone
	--	Dla Calc_Ver >= 3
	--	Tutaj także dwa wariany w zalezności od Netto_do_Brutto
		UPDATE `t_xxng_vat_z_poz_N` v, `t_xxng_vat_INF` n 
		SET 
			v.Netto = (@.v.Netto := IF(v.NettoWal = 0 AND v.Netto != 0, v.Netto, _Rnd2(v.NettoWal*n.Kurs))), 
			v.Vat = (@.v.Vat := IF(v.NettoWal = 0 AND v.Netto != 0, v.Vat, _Rnd2(v.VatWal*n.Kurs))),
			v.Brutto = (@.v.Netto + @.v.Vat)
		WHERE 
			TRUE
			-- and v.IdnOper = _Key
			AND n.Netto_do_Brutto	--	GDY Netto -> Brutto
			AND
			n.Calc_Ver >= 3
		;
		UPDATE `t_xxng_vat_z_poz_N` v, `t_xxng_vat_INF` n 
		SET 
			v.Brutto = (@.v.Brutto := IF(v.BruttoWal = 0 AND v.Brutto != 0, v.Brutto, _Rnd2(v.BruttoWal*n.Kurs))), 
			v.Vat = (@.v.Vat := IF(v.BruttoWal = 0 AND v.Brutto != 0, v.Vat, _Rnd2(v.VatWal*n.Kurs))),
			v.Netto = (@.v.Brutto - @.v.Vat)
		WHERE 
			TRUE
			-- and v.IdnOper = _Key
			AND NOT n.Netto_do_Brutto	--	GDY Brutto -> Netto
			AND
			n.Calc_Ver >= 3
		;
	
	--	Dla Calc_Ver < 3
		UPDATE `t_xxng_vat_z_poz_N` v, `t_xxng_vat_INF` n 
		SET 
			v.NettoWal = IF(v.Netto = 0 AND v.NettoWal != 0, v.NettoWal, _Rnd2(v.Netto/n.Kurs)), 
			v.BruttoWal = IF(v.Brutto = 0 AND v.BruttoWal != 0, v.BruttoWal, _Rnd2(v.Brutto/n.Kurs)), 
			v.VatWal = IF(v.Vat = 0 AND v.VatWal != 0, v.VatWal, _Rnd2(v.Brutto/n.Kurs) - _Rnd2(v.Netto/n.Kurs)) 
		WHERE 
			TRUE
			-- and v.IdnOper = _Key
			AND
			n.Calc_Ver < 3
		;
	
	--	Wstawianie do	
		DROP TEMPORARY TABLE IF EXISTS `t_xxng_vat_z_poz`;
		CREATE TEMPORARY TABLE `t_xxng_vat_z_poz` LIKE `opng_vat`;
		INSERT  INTO `t_xxng_vat_z_poz` 
			(IdnOper, IdVat, Netto, Vat, Brutto, NettoWal, VatWal, BruttoWal, OperCount)
		SELECT
			IdnOper, IdVat,
			SUM(Netto) AS Netto,
			SUM(Vat) AS Vat,
			SUM(Brutto) AS Brutto,
			SUM(NettoWal) AS NettoWal,
			SUM(VatWal) AS VatWal,
			-- SUM(BruttoWal) - SUM(NettoWal) AS VatWal,
			SUM(BruttoWal) AS BruttoWal,
			_VarInt(MAX(OperCount)) AS OperCount
		FROM `t_xxng_vat_z_poz_N` 
		GROUP BY IdVat
		HAVING Netto != 0 OR Vat != 0 OR NettoWal != 0 OR VatWal != 0
		;	
		
	END IF;
	
	IF _MODE IN ('`t_xxng_vat` <- `xxng_vat`', 'Preprae.`t_xxng_vat`') THEN
	/* ******************************************************************************************************************************
		Przygotowuje tabelke vat ze zrodlowej i `t_xxng_vat_z_poz`  pozycji wg `t_xxng_vat_INF`
		CALL xxng_vat_TOOLS('`t_xxng_vat` <- `xxng_vat`', '', '', '', '', '', '', ''); -- zam
	*********************************************************************************************************************************/
	set @xxxx = 20;
	--	UWAGA sa dok. np. zakup pozostaly ktory nie ma pozycji ma tabelke VAT ALE BEZ FIXED !!! A poniewaz nie maja pozycji to sa potem zerowane !!! I co wtedy ??? 
	-- 	Wtedy trzeba wstawic fixed i umozliwic zachowanie tych stawek
		CALL xxng_vat_TOOLS('`xxng_vat`.make.fixed.bezpozycyjne.niezerowe', _FROM, _Swiat, _Key, '', 'nie sprawdzaj', '', ''); 
	
		DROP TEMPORARY TABLE IF EXISTS `t_xxng_vat`;
		CREATE TEMPORARY TABLE `t_xxng_vat` LIKE `opng_vat`;

		SET
			_SQLText = 
			"
			INSERT IGNORE INTO `t_xxng_vat`
			SELECT
				v.* 
			FROM `#_xxng_vat_TableName#` v
			WHERE
				v.IdnOper = _Key
				AND
				v.Fixed != 0
			;			
			"
		;
		CALL xxng_vat_TOOLS('SQLTEXT.Execute', '', '', '', _SQLText, '', '', ''); 
		SET @.SQL.t_xxng_vat := @_SQLTextX1;
	
	/*
		Usun wszystkie czyli te nie fixed ORAZ fixed gdy tylko flaga _Clear_Fixed = TRUE
	*/
		DELETE v FROM `t_xxng_vat` v, `t_xxng_vat_INF` n
		WHERE 
			n._Clear_Fixed -- OR v.Fixed = 0
		;
	 /*	UWAGA
		z wyjatkiem Zakupu inwestycyjnego oraz pochodzacego z kancelarii ...
		GDY jest to dok. mający pozycje i z tych pozycji wynika że nie ma jakiejs stawki która w tabelce jest jako Fixed to oznacza
		że w `t_ng_vat_calc` nie ma danej stawki i należy ją usunąć z tabelki nawet jeśli jest Fixed!!!
	 */
		DELETE v FROM `t_xxng_vat` v, `t_xxng_vat_INF` n
		WHERE 
			NOT (v.IdVat IN (SELECT IdVat FROM `t_xxng_vat_z_poz`))
			AND n.IS_PodlegaVAT 
			AND n.IS_PozycjeVisible 
			AND (n.Rodzaj != 75) 
			AND (NOT n.IS_Kancelaria)
			AND NOT(n._Params LIKE '%<VAT.FIXED>%')
		;
		
	--	Dla Calc_Ver >= 3
		UPDATE `t_xxng_vat` v, `t_xxng_vat_INF` n 
		SET 
			v.Netto = IF(v.NettoWal = 0 AND v.Netto != 0, v.Netto, _Rnd2(v.NettoWal*n.Kurs)), 
			v.Brutto = IF(v.BruttoWal = 0 AND v.Brutto != 0, v.Brutto, _Rnd2(v.BruttoWal*n.Kurs)), 
			v.Vat = IF(v.BruttoWal = 0 AND v.Brutto != 0, v.Vat, _Rnd2(v.BruttoWal*n.Kurs) - _Rnd2(v.NettoWal*n.Kurs))
		WHERE 
			TRUE
			-- and v.IdnOper = _Key
			AND
			v.Fixed = 1		--	Gdy Fixed = 2 to mozemy sobie edytowac PLN też
			AND
			n.Calc_Ver >= 3
		;
	--	Dla Calc_Ver < 3
		UPDATE `t_xxng_vat` v, `t_xxng_vat_INF` n 
		SET 
			v.NettoWal = IF(v.Netto = 0 AND v.NettoWal != 0, v.NettoWal, _Rnd2(v.Netto/n.Kurs)), 
			v.BruttoWal = IF(v.Brutto = 0 AND v.BruttoWal != 0, v.BruttoWal, _Rnd2(v.Brutto/n.Kurs)), 
			v.VatWal = IF(v.Vat = 0 AND v.VatWal != 0, v.VatWal, _Rnd2(v.Brutto/n.Kurs) - _Rnd2(v.Netto/n.Kurs)) 
		WHERE 
			TRUE
			-- and v.IdnOper = _Key
			AND
			v.Fixed = 1		
			AND
			n.Calc_Ver < 3
		;
		
		
	END IF;

	IF _MODE = '`t_xxng_vat` <- `t_xxng_vat_z_poz`' THEN
	/* ******************************************************************************************************************************
		Wstaw do `t_xxng_vat` tabelke z pozycji `t_xxng_vat_z_poz`
		CALL xxng_vat_TOOLS('`t_xxng_vat` <- `t_xxng_vat_z_poz`', '', '', '', '', '', '', ''); 
	*********************************************************************************************************************************/
	set @xxxx = 40;
		INSERT IGNORE INTO `t_xxng_vat` SELECT * FROM `t_xxng_vat_z_poz`;
	END IF;
	
	
	IF _MODE IN ('`xxng_vat` <- `t_xxng_vat`', '`t_xxng_vat`.to.`xxng_vat`') THEN
	/* ******************************************************************************************************************************
		Wstaw nap do `zmng_vat_e` z `t_xxng_vat`
		CALL xxng_vat_TOOLS('`xxng_vat` <- `t_xxng_vat`', '', '', '', '', '', '', ''); 
	*********************************************************************************************************************************/
	set @xxxx = 50;
		SET
			_SQLText = 
			"
			DELETE FROM `#_xxng_vat_TableName#`
			WHERE
				IdnOper = _Key
			;			
			"
		;
		CALL xxng_vat_TOOLS('SQLTEXT.Execute', '', '', '', _SQLText, '', '', ''); 
		SET
			_SQLText = 
			"
			INSERT IGNORE INTO `#_xxng_vat_TableName#`
			SELECT * FROM `t_xxng_vat`
			;			
			"
		;
		CALL xxng_vat_TOOLS('SQLTEXT.Execute', '', '', '', _SQLText, '', '', ''); 

	END IF;
	
	IF _MODE = '`xxng` <- `xxng_vat`' THEN
	/* ******************************************************************************************************************************
		Przepisz zagregowane wartowsci tabelki VAT `xxng_vat` do naglowka `xxng`
		CALL xxng_vat_TOOLS('`xxng` <- `xxng_vat`', '', '', '', '', '', '', ''); 
	*********************************************************************************************************************************/
	--	Przepisz zagregowane wartowsci tabelki VAT do naglowka
	--	`xxng` <- `xxng_vat`
	set @xxxx = 70;
		CALL xxng_vat_TOOLS('PREPRATE.`t_xxng_vat_INF` IF NOT EXISTS OR INVALID', _FROM, _Swiat, _Key, _Params, _Param_A, _Param_B, _Param_C); 
		DROP TEMPORARY TABLE IF EXISTS `t_xxng_vat_AGG`;
		SET
			_SQLText = 
		"
		CREATE TEMPORARY TABLE `t_xxng_vat_AGG` (
			SELECT 
				SUM(IF(v.work_FieldName=1,o.netto,0)) AS tabwartnetto1,
				SUM(IF(v.work_FieldName=2,o.netto,0)) AS tabwartnetto2,
				SUM(IF(v.work_FieldName=3,o.netto,0)) AS tabwartnetto3,
				SUM(IF(v.work_FieldName=4,o.netto,0)) AS tabwartnetto4,
				SUM(IF(v.work_FieldName=5,o.netto,0)) AS tabwartnetto5,
				SUM(IF(v.work_FieldName=6,o.netto,0)) AS tabwartnetto6,
				SUM(IF(v.work_FieldName=7,o.netto,0)) AS tabwartnetto7,
				SUM(IF(v.work_FieldName=8,o.netto,0)) AS tabwartnetto8,
				SUM(IF(v.work_FieldName=9,o.netto,0)) AS tabwartnetto9,
				SUM(IF(v.work_FieldName=10,o.netto,0)) AS tabwartnetto10,
				SUM(IF(v.work_FieldName=11,o.netto,0)) AS tabwartnetto11,
				SUM(IF(v.work_FieldName=12,o.netto,0)) AS tabwartnetto12,
				SUM(IF(v.work_FieldName=13,o.netto,0)) AS tabwartnetto13,
				SUM(IF(v.work_FieldName=14,o.netto,0)) AS tabwartnetto14,

				SUM(IF(v.work_FieldName=1,o.vat,0)) AS tabwartvat1,
				SUM(IF(v.work_FieldName=2,o.vat,0)) AS tabwartvat2,
				SUM(IF(v.work_FieldName=3,o.vat,0)) AS tabwartvat3,
				SUM(IF(v.work_FieldName=4,o.vat,0)) AS tabwartvat4,
				SUM(IF(v.work_FieldName=5,o.vat,0)) AS tabwartvat5,
				SUM(IF(v.work_FieldName=6,o.vat,0)) AS tabwartvat6,
				SUM(IF(v.work_FieldName=7,o.vat,0)) AS tabwartvat7,
				SUM(IF(v.work_FieldName=8,o.vat,0)) AS tabwartvat8,
				SUM(IF(v.work_FieldName=9,o.vat,0)) AS tabwartvat9,
				SUM(IF(v.work_FieldName=10,o.vat,0)) AS tabwartvat10,
				SUM(IF(v.work_FieldName=11,o.vat,0)) AS tabwartvat11,
				SUM(IF(v.work_FieldName=12,o.vat,0)) AS tabwartvat12,
				SUM(IF(v.work_FieldName=13,o.vat,0)) AS tabwartvat13,
				SUM(IF(v.work_FieldName=14,o.vat,0)) AS tabwartvat14,

				SUM(IF(v.work_FieldName=1,o.brutto,0)) AS tabwartbrutto1,
				SUM(IF(v.work_FieldName=2,o.brutto,0)) AS tabwartbrutto2,
				SUM(IF(v.work_FieldName=3,o.brutto,0)) AS tabwartbrutto3,
				SUM(IF(v.work_FieldName=4,o.brutto,0)) AS tabwartbrutto4,
				SUM(IF(v.work_FieldName=5,o.brutto,0)) AS tabwartbrutto5,
				SUM(IF(v.work_FieldName=6,o.brutto,0)) AS tabwartbrutto6,
				SUM(IF(v.work_FieldName=7,o.brutto,0)) AS tabwartbrutto7,
				SUM(IF(v.work_FieldName=8,o.brutto,0)) AS tabwartbrutto8,
				SUM(IF(v.work_FieldName=9,o.brutto,0)) AS tabwartbrutto9,
				SUM(IF(v.work_FieldName=10,o.brutto,0)) AS tabwartbrutto10,
				SUM(IF(v.work_FieldName=11,o.brutto,0)) AS tabwartbrutto11,
				SUM(IF(v.work_FieldName=12,o.brutto,0)) AS tabwartbrutto12,
				SUM(IF(v.work_FieldName=13,o.brutto,0)) AS tabwartbrutto13,
				SUM(IF(v.work_FieldName=14,o.brutto,0)) AS tabwartbrutto14,
				
				SUM(o.Netto) AS Netto, 
				SUM(o.Vat) AS Vat,  
				SUM(o.Brutto) AS Brutto,
				SUM(o.NettoWal) AS NettoWal, 
				SUM(o.VatWal) AS VatWal,  
				SUM(o.BruttoWal) AS BruttoWal

			FROM `#_xxng_vat_TableName#` o
			INNER JOIN `v_erp_vat` v ON o.idvat = v.ID
			WHERE 
				o.IdnOper = _Key
			-- GROUP BY o.idnoper
		)	
		;
		"
		;   
		set @xxx = _SQLText;
		CALL xxng_vat_TOOLS('SQLTEXT.Execute', '', '', '', _SQLText, '', '', ''); 
		
		IF _FROM = 'OP' THEN
	--	Operacje	
			SET
				_SQLText = 
			"
			UPDATE `#_xxng_TableName#` n, `t_xxng_vat_AGG` tw
			SET
				n.tabwartnetto1 = IFNULL(tw.tabwartnetto1, 0),
				n.tabwartnetto2 = IFNULL(tw.tabwartnetto2, 0),
				n.tabwartnetto3 = IFNULL(tw.tabwartnetto3, 0),
				n.tabwartnetto4 = IFNULL(tw.tabwartnetto4, 0),
				n.tabwartnetto5 = IFNULL(tw.tabwartnetto5, 0),
				n.tabwartnetto6 = IFNULL(tw.tabwartnetto6, 0),
				n.tabwartnetto7 = IFNULL(tw.tabwartnetto7, 0),
				n.tabwartnetto8 = IFNULL(tw.tabwartnetto8, 0),
				n.tabwartnetto9 = IFNULL(tw.tabwartnetto9, 0),
				n.tabwartnetto10 = IFNULL(tw.tabwartnetto10, 0),
				n.tabwartnetto11 = IFNULL(tw.tabwartnetto11, 0),
				n.tabwartnetto12 = IFNULL(tw.tabwartnetto12, 0),
				n.tabwartnetto13 = IFNULL(tw.tabwartnetto13, 0),
				n.tabwartnetto14 = IFNULL(tw.tabwartnetto14, 0),
				
				n.tabwartvat1 = IFNULL(tw.tabwartvat1, 0),
				n.tabwartvat2 = IFNULL(tw.tabwartvat2, 0),
				n.tabwartvat3 = IFNULL(tw.tabwartvat3, 0),
				n.tabwartvat4 = IFNULL(tw.tabwartvat4, 0),
				n.tabwartvat7 = IFNULL(tw.tabwartvat7, 0),
				n.tabwartvat8 = IFNULL(tw.tabwartvat8, 0),
				n.tabwartvat9 = IFNULL(tw.tabwartvat9, 0),
				n.tabwartvat10 = IFNULL(tw.tabwartvat10, 0),
				n.tabwartvat11 = IFNULL(tw.tabwartvat11, 0),
				n.tabwartvat12 = IFNULL(tw.tabwartvat12, 0),
				
				n.tabwartBrutto1 = IFNULL(tw.tabwartBrutto1, 0),
				n.tabwartBrutto2 = IFNULL(tw.tabwartBrutto2, 0),
				n.tabwartBrutto3 = IFNULL(tw.tabwartBrutto3, 0),
				n.tabwartBrutto4 = IFNULL(tw.tabwartBrutto4, 0),
				n.tabwartBrutto7 = IFNULL(tw.tabwartBrutto7, 0),
				n.tabwartBrutto8 = IFNULL(tw.tabwartBrutto8, 0),
				n.tabwartBrutto9 = IFNULL(tw.tabwartBrutto9, 0),
				n.tabwartBrutto10 = IFNULL(tw.tabwartBrutto10, 0),
				n.tabwartBrutto11 = IFNULL(tw.tabwartBrutto11, 0),
				n.tabwartBrutto12 = IFNULL(tw.tabwartBrutto12, 0),
				
				n.Netto = IFNULL(tw.Netto, 0), 
				n.Vat = IFNULL(tw.Vat, 0),
				n.Brutto = IFNULL(tw.Brutto, 0),
				n.NettoWal = IFNULL(tw.NettoWal, 0), 	--	Gdy Rachunek zakup-rach uproszczony i kurs=1 a kurs_rozliczen <> 1
				-- n.VatWal = IFNULL(tw.VatWal, 0),
				n.BruttoWal = IFNULL(tw.BruttoWal, 0)
			WHERE 
				n.GUID = _Key	
			;
			";
		ELSE
	--	Zamowienia
			SET
				_SQLText = 
			"
			UPDATE `#_xxng_TableName#` n, `t_xxng_vat_AGG` tw
			SET
				n.Netto = IFNULL(tw.Netto, 0), 
				--	n.Vat = IFNULL(tw.Vat, 0),
				n.Brutto = IFNULL(tw.Brutto, 0),
				n.NettoWal = IFNULL(tw.NettoWal, 0), 	
				n.BruttoWal = IFNULL(tw.BruttoWal, 0)
			WHERE 
				n.GUID = _Key	
			;
			";
		END IF;
		CALL xxng_vat_TOOLS('SQLTEXT.Execute', '', '', '', _SQLText, '', '', ''); 
		SET @.t_xxng_vat_AGG.SQLText = @_SQLText;
		-- DROP TEMPORARY TABLE IF EXISTS `t_xxng_vat_AGG`;
	END IF;
	
	IF _MODE LIKE '`xxng_vat`.%' THEN
	/* ******************************************************************************************************************************
		np. wstaw brakujace stawki lub
		usun zerowe
	*********************************************************************************************************************************/
	set @xxxx = 80;
		CALL xxng_vat_TOOLS('PREPRATE.`t_xxng_vat_INF` IF NOT EXISTS OR INVALID', _FROM, _Swiat, _Key, _Params, _Param_A, _Param_B, _Param_C); 
		
		IF _MODE = '`xxng_vat`.fill.brakujace.stawki' THEN
		--	WSTAWIANIE Brakujących
			SET
				_SQLText = 
				"
				INSERT IGNORE INTO `#_xxng_vat_TableName#`
				(IdnOper, IdVat)
					SELECT _Key, ID FROM `erp_vat` ev WHERE ev.visible AND NOT ev.not_used
				;			
				"
			;
			CALL xxng_vat_TOOLS('SQLTEXT.Execute', '', '', '', _SQLText, '', '', ''); 
		END IF;
		
		IF _MODE = '`xxng_vat`.delete.zerowe' THEN
		--	USUWANIE zerowych
			SET
				_SQLText = 
				"
				DELETE FROM `#_xxng_vat_TableName#` WHERE IdnOper = _Key AND (Netto = 0) AND (Brutto = 0) AND (Vat = 0) AND (Fixed = 0);			
				"
			;
			CALL xxng_vat_TOOLS('SQLTEXT.Execute', '', '', '', _SQLText, '', '', ''); 
		END IF;
		
		IF _MODE = '`xxng_vat`.make.fixed.bezpozycyjne.niezerowe' THEN
		--	UWAGA sa dok. np. zakup pozostaly ktory nie ma pozycji ma tabelke VAT ALE BEZ FIXED !!! A poniewaz nie maja pozycji to sa potem zerowane !!! I co wtedy ??? 
		-- 	Wtedy trzeba wstawic fixed i umozliwic zachowanie tych stawek
			SET
				_SQLText = 
				"
				UPDATE `#_xxng_vat_TableName#` v, `t_xxng_vat_INF` n
				SET
					v.Fixed = GREATEST(v.Fixed, 1)
				WHERE 
					v.IdnOper = _Key 
					AND 
					NOT n.IS_PozycjeVisible
					AND 
					(v.Netto != 0 OR v.Brutto != 0)
				;
				"
			;
			CALL xxng_vat_TOOLS('SQLTEXT.Execute', '', '', '', _SQLText, '', '', ''); 
		END IF;
	END IF;
		
	
	IF _MODE LIKE 'UPDATE%' THEN
	/* ******************************************************************************************************************************
		CALL xxng_vat_TOOLS('UPDATE `xxng_vat` and `xxng`', _FROM, _Swiat, _Key, _Params, _Param_A, _Param_B, _Param_C); 
	*********************************************************************************************************************************/
		IF IFNULL(@SYSTEM.Client.Session.GUID, '') = '' OR @ht.CSpBR IS NULL THEN
			CALL _Born_Session('', '', '');		--	dla eventu
		END IF;
		
set @_vat_watch = NULL;
set @_vat_watch = concat_WS('/', @_vat_watch, '1.PREPRATE....');		
		CALL xxng_vat_TOOLS('PREPRATE.`t_xxng_vat_INF` IF NOT EXISTS OR INVALID', _FROM, _Swiat, _Key, _Params, _Param_A, _Param_B, _Param_C); 
set @_xxng_TableName = '?';		
		IF _MODE LIKE 'UPDATE `xxng_vat`%' THEN
		--	5 KROKOW - rozbilem dla testow (strata 0.003 s wiec warto) na pod procedury
		--	1. Przygotuj tabvat z pozycji
		--	`t_xxng_vat_z_poz` <- `xxpo`
set @_vat_watch = concat_WS('/', @_vat_watch, '2.`t_xxng_vat_z_poz` <- `xxpo`');		
			CALL xxng_vat_TOOLS('`t_xxng_vat_z_poz` <- `xxpo`', _FROM, _Swiat, _Key, '', 'nie sprawdzaj', '', '');
		--	2. Przygotuj tabvat `xxng_vat`
		--	`t_xxng_vat` <- `xxng_vat`
			CALL xxng_vat_TOOLS('`t_xxng_vat` <- `xxng_vat`', _FROM, _Swiat, _Key, '', 'nie sprawdzaj', '', ''); 
		--	3. Przepisz do `t_xxng_vat` tabelke z pozycji `t_xxng_vat_z_poz`
		--	`t_xxng_vat` <- `t_xxng_vat_z_poz`
			CALL xxng_vat_TOOLS('`t_xxng_vat` <- `t_xxng_vat_z_poz`', _FROM, _Swiat, _Key, '', 'nie sprawdzaj', '', ''); 
		--	4. Teraz mamy juz calkowicie naliczona tabelke vat w `t_xxng_vat`
		--	przepisz ja do docelowej `xxng_vat`
		--	`xxng_vat` <- `t_xxng_vat`
			CALL xxng_vat_TOOLS('`xxng_vat` <- `t_xxng_vat`', _FROM, _Swiat, _Key, '', 'nie sprawdzaj', '', ''); 
		--	5. Na koniec z tabelki do naglowka o ile mamy taki mode
			IF _MODE = 'UPDATE `xxng_vat` and `xxng`' THEN
			--	`xxng` <- `xxng_vat`
				CALL xxng_vat_TOOLS('`xxng` <- `xxng_vat`', _FROM, _Swiat, _Key, '', 'nie sprawdzaj', '', ''); 
			END IF;
		--	A Gdy jest to swiat E to jeszcze dodaj brakujace stawki
			IF _Swiat = 'E' THEN
			--	`xxng_vat` <- brakujace stawki
				CALL xxng_vat_TOOLS('`xxng_vat`.fill.brakujace.stawki', _FROM, _Swiat, _Key, _Params, 'nie sprawdzaj', _Param_B, _Param_C); 
			END IF;
		END IF;
	END IF;
	
	
			IF _MODE = 'Check.vat.nie_dziala' THEN
			-- PO CO ????????????????????
			-- nie ma _TableName	CALL t_ng_vat_calc_Build('', _Key, _TableName, _Netto_do_Brutto);
				SELECT
					t.*
				FROM t_ng_vat_calc t
				WHERE
					EXISTS((SELECT * FROM opng_vat v WHERE v.IdnOper = t.IdnOper AND t.IdVat = v.IdVat AND (t.Brutto != v.Brutto OR t.Netto != v.Netto OR t.BruttoWal != v.BruttoWal))
				)
				;
				
				-- LEAVE S_t_ng_vat_calc_Build;
			END IF;

			IF _MODE = 'From.`t_update_vat_for_rec`' THEN
			
			set @ROW_Curr = -1;
				BEGIN
				
					DECLARE done BOOL DEFAULT FALSE;
					DECLARE IdnOper_rec varchar(38) DEFAULT '';
				--	3.1 Kursor	
					DECLARE c_update_vat_for_rec CURSOR FOR
						SELECT
							t.GUID
						FROM `t_update_vat_for_rec` t
					;	
					DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
					
				--	3.2 Open Kursor	
					OPEN c_update_vat_for_rec;
					loop_c_update_vat_for_rec: LOOP
						SET done = FALSE;
						FETCH c_update_vat_for_rec INTO IdnOper_rec;
						IF done THEN
							LEAVE loop_c_update_vat_for_rec;
						END IF;
					--	Wykonaj 
						SET @ROW_Curr = @ROW_Curr + 1;
						
						CALL xxng_vat_TOOLS('UPDATE `xxng_vat` and `xxng`',  _FROM, _Swiat, IdnOper_rec, _Params, _Param_A, _Param_B, _Param_C);
					END LOOP;
					CLOSE c_update_vat_for_rec;
				
				END;
			
			END IF;
			
	
END$$



DELIMITER ;

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

CALL C_HT_VAT('', '', '', '');

Sprawdzenie zgodności kursu nagłówka z pozycjami
SELECT
	p.Rodzaj, od.Nazwa, n.Nrdokfin, n.NrdokMag, n.Datawystaw, CONCAT(w.Symbol, '(', n.Waluta, ')') AS Waluta, n.Kurs AS KursN, p.Kurs AS KursP, ROUND(ABS(n.Kurs - p.Kurs), 5) AS Delta, 
	n.Waluta, p.*
FROM `opng` n, `oppo` p, `operdoc` od, `waluty` w
WHERE
n.waluta <> 0
AND n.datawystaw > CURRENT_DATE() - INTERVAL 200 DAY
AND p.IdnOper = n.`GUID`
AND ABS(n.Kurs - p.Kurs) > 0.00001
AND od.Rodzaj = p.Rodzaj
AND w.Idn = n.waluta
ORDER BY n.Idn DESC
;

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
*/

