 SET NAMES utf8 ;
DELIMITER $$

DROP PROCEDURE IF EXISTS `pit_firmowy_tools`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `pit_firmowy_tools`(
	IN _MODE VARCHAR(150),
	IN _GUID VARCHAR(38),
	IN _Rec_Content LONGBLOB,
	IN _Param_A VARCHAR(250),	
	IN _Param_B VARCHAR(250),	
	IN _Param_C TEXT,	
	IN _Param_D VARCHAR(250)	
)
SPROC: BEGIN
	/*
	_MODE
	*/
		
	DECLARE _SQLText TEXT;	
	DECLARE _S TEXT  DEFAULT '';	
	DECLARE cSL 	 INTEGER	DEFAULT 4;	-- SPACE(5*cSL)
	DECLARE cKorekta INTEGER	DEFAULT 2;
	DECLARE cDefTypySchema  	VARCHAR(200) DEFAULT 'xmlns:etd="http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2011/06/21/eD/DefinicjeTypy/">';
	DECLARE _e_Doc_Def_ID 				VARCHAR(200);
	DECLARE _e_doc_id, _e_doc_type, _kodPodatku      VARCHAR(50);
	DECLARE _form_ver, _doc_ver, _KodUS, _def_date_from VARCHAR(10);   
	DECLARE _CelZlozenia, _Rok, _ile, _IDN, _RodzajKorekty INTEGER;  
	DECLARE _US_GUID VARCHAR(38);  			 -- GUID urzedu skarbowego , do ktorego wysylana jest deklaracja
	DECLARE _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
			_kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12 	INTEGER;  -- TKwotaCNieujemna	-- DOUBLE;
	-- DECLARE _przyczyna_ORD_ZU VARCHAR(200);  
	-- DECLARE @xml.IC INTEGER;
	
/*------------------------------------------------------------------------------------------------------------------------------------------------------------------	
	Statusy
	-1		brak
	 0		nowy
	10		podpisany
	20		wyslany
	
==============================================================================================================================
Process					Opis														Status	gdy OK
==============================================================================================================================
	-					-															0	
CREATE_XML				kreowanie xml w blobie Content								CREATE	10
SAVE_ORIG_TO_FILE		zapisuje na dysku jako plik (pole FIleName)					b.z.	(a mo�e 11)
LOAD_SIGN_FROM_FILE		pobiera z dysku zawarto�� pliku i zapisuje w Content		SIGN 	20		je�li podpisany
SEND_TO					wysy�a na e-deklaracje gdy OK dodatkowo 
						wo�amy tu GET_RESPOND										SENT	30		wys�any
GET_RESPOND				pobiera urz.potwierdenie odbioru							CONFIRM	30		wys�any gdy istnieje UPO
dodatkowo
SIGN_DIR				podpisuje dokumenty - zawiera sekwencj� SAVE_TO_FILE + 
						sign by DLL (np.KIR) + 
						LOAD_FROM_FILE
	
------------------------------------------------------------------------------------------------------------------------------------------------------------------*/	





/*------------------------------------------------------------------------------------------------------------------------------------------------------------------	
	'pit4r.xml.create.201901'
		wygenerowanie XMLa na podstawie rekordu PIT-4R 
		Parametry - do zrewidowania
		_MODE		  Tryb pracy
		IN _GUID      GUID  rekordu		
		IN _Param_A   Rok, za ktory jest skladana deklaracja
				
		CALL pit_tools('pit4r.xml.create.201901', '', '', '', '', '', '');
------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
IF _MODE = 'pit4r.xml.create.202301' THEN
SELECT  
		d.ID, p.przyczyna_ORD_ZU, p.rok
		INTO _e_Doc_Def_ID, @_przyczyna_ORD_ZU, _Rok
	FROM 	
		pit_4r p, e_doc_def d 
	WHERE	
		p.GUID = _GUID AND  p.e_Doc_Def_ID = d.owner_doc_Id
	;

	SET @ePIT.xml = '';
	SET @xml.IC = 1;
		
	SET @ePIT.xml = '<?xml version="1.0" encoding="UTF-8" ?>\n';	
	SET @ePIT.xml = CONCAT(@ePIT.xml, '<tns:Deklaracja xmlns:tns="', _e_Doc_Def_ID, '" ');
	SET @ePIT.xml = CONCAT(@ePIT.xml, 'xmlns:etd="http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2022/09/13/eD/DefinicjeTypy/"', '\n');
	SET @ePIT.xml = CONCAT(@ePIT.xml, 'xmlns:kus="http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2022/01/05/eD/KodyUrzedowSkarbowych/"', '\n');
	SET @ePIT.xml = CONCAT(@ePIT.xml, 'xmlns:zzu="http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2022/09/13/eD/ORDZU/"', '\n');
	SET @ePIT.xml = CONCAT(@ePIT.xml, 'xmlns:xsd="http://www.w3.org/2001/XMLSchema">', '\n');
	SET @ePIT.xmlns = @ePIT.xml; 	
		
	
	CALL pit_tools('pit.xml.insert.header.2023', _GUID, @ePIT.xml, 'pit_4r', 'P', '', '');
	
	
		SET @ePIT.header = @ePIT.xml;
		
		CALL pit_tools('pit.xml.insert.podmiot1.2023', '', '', 'Płatnik', '', '', '');  
		
		SET @ePIT.P1 = @ePIT.xml;
		
		
		BEGIN
			DECLARE 
				_Lpodat01, _Lpodat02, _Lpodat03, _Lpodat04, _Lpodat05, _Lpodat06,
				_Lpodat07, _Lpodat08, _Lpodat09, _Lpodat10, _Lpodat11, _Lpodat12  INTEGER;
			        
			
		
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:PozycjeSzczegolowe>\n');
				SET @xml.IC = @xml.IC + 1;
				
			SELECT IFNULL(RodzajKorekty,0) INTO @RodzajKorekty FROM pit_4r WHERE GUID = _GUID;
				
				-- blokada na dziwny rodzaj korekty (duze liczby, nie wiadomo skad)
				IF @RodzajKorekty > 100 THEN
					SET @RodzajKorekty = 0;
				END IF;

				IF @RodzajKorekty > 0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_7>', _RodzajKorekty, '</tns:P_7>\n');
				END IF;
			
				SELECT
						SUM(IF(msc=1, Lpodat1, 0)) AS P_9, SUM(IF(msc=2, Lpodat1, 0)) AS P_10,
						SUM(IF(msc=3, Lpodat1, 0)) AS P_11, SUM(IF(msc=4, Lpodat1, 0)) AS P_12,
						SUM(IF(msc=5, Lpodat1, 0)) AS P_13, SUM(IF(msc=6, Lpodat1, 0)) AS P_14,						
						_varINT(SUM(IF(msc=1, Zalicz1+Zalicz2, 0))) AS P_15, _varINT(SUM(IF(msc=2, Zalicz1+Zalicz2, 0))) AS P_16,
						_varINT(SUM(IF(msc=3, Zalicz1+Zalicz2, 0))) AS P_17, _varINT(SUM(IF(msc=4, Zalicz1+Zalicz2, 0))) AS P_18,
						_varINT(SUM(IF(msc=5, Zalicz1+Zalicz2, 0))) AS P_19, _varINT(SUM(IF(msc=6, Zalicz1+Zalicz2, 0))) AS P_20,					
						SUM(IF(msc=7, Lpodat1, 0)) AS P_21, SUM(IF(msc=8, Lpodat1, 0)) AS P_22,
						SUM(IF(msc=9, Lpodat1, 0)) AS P_23, SUM(IF(msc=10, Lpodat1, 0)) AS P_24,
						SUM(IF(msc=11, Lpodat1, 0)) AS P_25, SUM(IF(msc=12, Lpodat1, 0)) AS P_26,
						SUM(IF(msc=7, Zalicz1+Zalicz2, 0))  AS P_27, SUM(IF(msc=8, Zalicz1+Zalicz2, 0)) AS P_28,
						SUM(IF(msc=9, Zalicz1+Zalicz2, 0))  AS P_29, SUM(IF(msc=10, Zalicz1+Zalicz2, 0)) AS P_30,
						SUM(IF(msc=11, Zalicz1+Zalicz2, 0)) AS P_31, SUM(IF(msc=12, Zalicz1+Zalicz2, 0)) AS P_32
					INTO _Lpodat01, _Lpodat02, _Lpodat03, _Lpodat04, _Lpodat05, _Lpodat06,
						 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _Lpodat07, _Lpodat08, _Lpodat09, _Lpodat10, _Lpodat11, _Lpodat12,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM 
						pit_4 WHERE rok = _Rok
					;
						 
					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_10>', _Lpodat01, '</tns:P_10>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_11>', _Lpodat02, '</tns:P_11>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_12>', _Lpodat03, '</tns:P_12>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_13>', _Lpodat04, '</tns:P_13>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_14>', _Lpodat05, '</tns:P_14>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_15>', _Lpodat06, '</tns:P_15>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_16>', _kwota01, '</tns:P_16>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_17>', _kwota02, '</tns:P_17>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_18>', _kwota03, '</tns:P_18>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_19>', _kwota04, '</tns:P_19>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_20>', _kwota05, '</tns:P_20>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_21>', _kwota06, '</tns:P_21>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_22>', _Lpodat07, '</tns:P_22>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_23>', _Lpodat08, '</tns:P_23>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_24>', _Lpodat09, '</tns:P_24>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_25>', _Lpodat10, '</tns:P_25>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_26>', _Lpodat11, '</tns:P_26>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_27>', _Lpodat12, '</tns:P_27>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_28>', _kwota07, '</tns:P_28>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_29>', _kwota08, '</tns:P_29>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_30>', _kwota09, '</tns:P_30>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_31>', _kwota10, '</tns:P_31>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_32>', _kwota11, '</tns:P_32>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_33>', _kwota12, '</tns:P_33>\n');													


					SET @kwota301=0, @kwota302=0, @kwota303=0, @kwota304=0, @kwota305=0, @kwota306=0,@kwota307=0, @kwota308=0, @kwota309=0, @kwota310=0, @kwota311=0, @kwota312=0;
					SELECT 	
					SUM(IF(msc=1, PodatekOdZlecen, 0)) AS P_109, SUM(IF(msc=2, PodatekOdZlecen, 0)),
					SUM(IF(msc=3, PodatekOdZlecen, 0)), SUM(IF(msc=4, PodatekOdZlecen, 0)),
					SUM(IF(msc=5, PodatekOdZlecen, 0)), SUM(IF(msc=6, PodatekOdZlecen, 0)),		
					SUM(IF(msc=7, PodatekOdZlecen, 0)), SUM(IF(msc=8, PodatekOdZlecen, 0)),
					SUM(IF(msc=9, PodatekOdZlecen, 0)), SUM(IF(msc=10, PodatekOdZlecen, 0)),
					SUM(IF(msc=11, PodatekOdZlecen, 0)), SUM(IF(msc=12, PodatekOdZlecen, 0)) AS P_120
					INTO
					 @kwota301, @kwota302, @kwota303, @kwota304, @kwota305, @kwota306,
					 @kwota307, @kwota308, @kwota309, @kwota310, @kwota311, @kwota312
				FROM 
						pit_4 WHERE rok = _Rok	
				;
														 					
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_46>', @kwota301, '</tns:P_46>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_47>', @kwota302, '</tns:P_47>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_48>', @kwota303, '</tns:P_48>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_49>', @kwota304, '</tns:P_49>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_50>', @kwota305, '</tns:P_50>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_51>', @kwota306, '</tns:P_51>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_52>', @kwota307, '</tns:P_52>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_53>', @kwota308, '</tns:P_53>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_54>', @kwota309, '</tns:P_54>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_55>', @kwota310, '</tns:P_55>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_56>', @kwota311, '</tns:P_56>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_57>', @kwota312, '</tns:P_57>\n');				
				
				SET @kwota401=0, @kwota402=0, @kwota403=0, @kwota404=0, @kwota405=0, @kwota406=0,@kwota407=0, @kwota408=0, @kwota409=0, @kwota410=0, @kwota411=0, @kwota412=0;
				SELECT 	
					SUM(IF(msc=1, Zalicz9, 0)) AS P_121, SUM(IF(msc=2, Zalicz9, 0)),
					SUM(IF(msc=3, Zalicz9, 0)), SUM(IF(msc=4, Zalicz9, 0)),
					SUM(IF(msc=5, Zalicz9, 0)), SUM(IF(msc=6, Zalicz9, 0)),		
					SUM(IF(msc=7, Zalicz9, 0)), SUM(IF(msc=8, Zalicz9, 0)),
					SUM(IF(msc=9, Zalicz9, 0)), SUM(IF(msc=10, Zalicz9, 0)),
					SUM(IF(msc=11, Zalicz9, 0)), SUM(IF(msc=12, Zalicz9, 0)) AS P_132
					INTO
					 @kwota401, @kwota402, @kwota403, @kwota404, @kwota405, @kwota406,
					 @kwota407, @kwota408, @kwota409, @kwota410, @kwota411, @kwota412
				FROM 
						pit_4 WHERE rok = _Rok	
				;
					
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_58>', @kwota401, '</tns:P_58>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_59>', @kwota402, '</tns:P_59>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_60>', @kwota403, '</tns:P_60>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_61>', @kwota404, '</tns:P_61>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_62>', @kwota405, '</tns:P_62>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_63>', @kwota406, '</tns:P_63>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_64>', @kwota407, '</tns:P_64>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_65>', @kwota408, '</tns:P_65>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_66>', @kwota409, '</tns:P_66>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_67>', @kwota410, '</tns:P_67>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_68>', @kwota411, '</tns:P_68>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_69>', @kwota412, '</tns:P_69>\n');
				
				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_70>', _kwota01+@kwota301+@kwota401, '</tns:P_70>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_71>', _kwota02+@kwota302+@kwota402, '</tns:P_71>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_72>', _kwota03+@kwota303+@kwota403, '</tns:P_72>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_73>', _kwota04+@kwota304+@kwota404, '</tns:P_73>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_74>', _kwota05+@kwota305+@kwota405, '</tns:P_74>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_75>', _kwota06+@kwota306+@kwota406, '</tns:P_75>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_76>', _kwota07+@kwota307+@kwota407, '</tns:P_76>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_77>', _kwota08+@kwota308+@kwota408, '</tns:P_77>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_78>', _kwota09+@kwota309+@kwota409, '</tns:P_78>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_79>', _kwota10+@kwota310+@kwota410, '</tns:P_79>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_80>', _kwota11+@kwota311+@kwota411, '</tns:P_80>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_81>', _kwota12+@kwota312+@kwota412, '</tns:P_81>\n');													
																
	
				SELECT 	
					SUM(IF(msc=1, Zalicz11, 0)) AS P_57, SUM(IF(msc=2, Zalicz11, 0)),
					SUM(IF(msc=3, Zalicz11, 0)), SUM(IF(msc=4, Zalicz11, 0)),
					SUM(IF(msc=5, Zalicz11, 0)), SUM(IF(msc=6, Zalicz11, 0)),		
					SUM(IF(msc=7, Zalicz11, 0)), SUM(IF(msc=8, Zalicz11, 0)),
					SUM(IF(msc=9, Zalicz11, 0)), SUM(IF(msc=10, Zalicz11, 0)),
					SUM(IF(msc=11, Zalicz11, 0)), SUM(IF(msc=12, Zalicz11, 0)) AS P_68
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
						pit_4 WHERE rok = _Rok	
				;

					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_82>', _kwota01, '</tns:P_82>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_83>', _kwota02, '</tns:P_83>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_84>', _kwota03, '</tns:P_84>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_85>', _kwota04, '</tns:P_85>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_86>', _kwota05, '</tns:P_86>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_87>', _kwota06, '</tns:P_87>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_88>', _kwota07, '</tns:P_88>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_89>', _kwota08, '</tns:P_89>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_90>', _kwota09, '</tns:P_90>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_91>', _kwota10, '</tns:P_91>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_92>', _kwota11, '</tns:P_92>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_93>', _kwota12, '</tns:P_93>\n');									
				
				
				SELECT 	
					SUM(IF(msc=1, Zalicz12, 0)) AS P_69, SUM(IF(msc=2, Zalicz12, 0)),
					SUM(IF(msc=3, Zalicz12, 0)), SUM(IF(msc=4, Zalicz12, 0)),
					SUM(IF(msc=5, Zalicz12, 0)), SUM(IF(msc=6, Zalicz12, 0)),		
					SUM(IF(msc=7, Zalicz12, 0)), SUM(IF(msc=8, Zalicz12, 0)),
					SUM(IF(msc=9, Zalicz12, 0)), SUM(IF(msc=10, Zalicz12, 0)),
					SUM(IF(msc=11, Zalicz12, 0)), SUM(IF(msc=12, Zalicz12, 0)) AS P_80
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
						pit_4 WHERE rok = _Rok	
				;
										 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_94>', _kwota01, '</tns:P_94>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_95>', _kwota02, '</tns:P_95>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_96>', _kwota03, '</tns:P_96>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_97>', _kwota04, '</tns:P_97>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_98>', _kwota05, '</tns:P_98>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_99>', _kwota06, '</tns:P_99>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_100>', _kwota07, '</tns:P_100>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_101>', _kwota08, '</tns:P_101>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_102>', _kwota09, '</tns:P_102>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_103>', _kwota10, '</tns:P_103>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_104>', _kwota11, '</tns:P_104>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_105>', _kwota12, '</tns:P_105>\n');									
				
				
				SELECT 	
					SUM(IF(msc=1, zalicz13, 0)) , SUM(IF(msc=2, zalicz13, 0)),
					SUM(IF(msc=3, zalicz13, 0)), SUM(IF(msc=4, zalicz13, 0))
				INTO
					 _kwota01, _kwota02, _kwota03, _kwota04
				FROM 
						pit_4 WHERE rok = _Rok	
				;
														 					
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_106>', _kwota01, '</tns:P_106>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_107>', _kwota02, '</tns:P_107>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_108>', _kwota03, '</tns:P_108>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_109>', _kwota04, '</tns:P_109>\n');		
				
			/*	
				SELECT 	
					SUM(IF(msc=1, Zalicz14, 0)), SUM(IF(msc=2, Zalicz14, 0)),
					SUM(IF(msc=3, Zalicz14, 0)), SUM(IF(msc=4, Zalicz14, 0)),
					SUM(IF(msc=5, Zalicz14, 0)), SUM(IF(msc=6, Zalicz14, 0)),		
					SUM(IF(msc=7, Zalicz14, 0)), SUM(IF(msc=8, Zalicz14, 0)),
					SUM(IF(msc=9, Zalicz14, 0)), SUM(IF(msc=10, Zalicz14, 0)),
					SUM(IF(msc=11, Zalicz14, 0)), SUM(IF(msc=12, Zalicz14, 0))
				FROM 
						pit_4 WHERE rok = _Rok	
				INTO 
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,						 
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12;	
					 
					 
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_110>', _kwota01, '</P_110>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_111>', _kwota02, '</P_111>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_112>', _kwota03, '</P_112>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_113>', _kwota04, '</P_113>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_114>', _kwota05, '</P_114>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_115>', _kwota06, '</P_115>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_116>', _kwota07, '</P_116>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_117>', _kwota08, '</P_117>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_118>', _kwota09, '</P_118>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_119>', _kwota10, '</P_119>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_120>', _kwota11, '</P_120>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_121>', _kwota12, '</P_121>\n');	
			*/
				SELECT 	
					SUM(IF(msc=1, PodatekPFRON, 0)) AS P_97, SUM(IF(msc=2, PodatekPFRON, 0)),
					SUM(IF(msc=3, PodatekPFRON, 0)), SUM(IF(msc=4, PodatekPFRON, 0)),
					SUM(IF(msc=5, PodatekPFRON, 0)), SUM(IF(msc=6, PodatekPFRON, 0)),		
					SUM(IF(msc=7, PodatekPFRON, 0)), SUM(IF(msc=8, PodatekPFRON, 0)),
					SUM(IF(msc=9, PodatekPFRON, 0)), SUM(IF(msc=10, PodatekPFRON, 0)),
					SUM(IF(msc=11, PodatekPFRON, 0)), SUM(IF(msc=12, PodatekPFRON, 0)) AS P_108
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
						pit_4 WHERE rok = _Rok	
				;
														 					
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_110>', _kwota01, '</tns:P_110>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_111>', _kwota02, '</tns:P_111>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_112>', _kwota03, '</tns:P_112>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_113>', _kwota04, '</tns:P_113>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_114>', _kwota05, '</tns:P_114>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_115>', _kwota06, '</tns:P_115>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_116>', _kwota07, '</tns:P_116>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_117>', _kwota08, '</tns:P_117>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_118>', _kwota09, '</tns:P_118>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_119>', _kwota10, '</tns:P_119>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_120>', _kwota11, '</tns:P_120>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_121>', _kwota12, '</tns:P_121>\n');									
					
			
				

		
		
				
				
				SELECT
					SUM(IF(msc=1, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_133, 
					SUM(IF(msc=2, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_134, 
					SUM(IF(msc=3, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_135, 
					SUM(IF(msc=4, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_136, 
					
					SUM(IF(msc=5, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ), 
					SUM(IF(msc=6, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=7, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=8, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) ,
					SUM(IF(msc=9, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=10, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=11, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=12, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_144
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
					pit_4 WHERE rok = _Rok	
				;

				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_122>', _kwota01, '</tns:P_122>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_123>', _kwota02, '</tns:P_123>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_124>', _kwota03, '</tns:P_124>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_125>', _kwota04, '</tns:P_125>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_126>', _kwota05, '</tns:P_126>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_127>', _kwota06, '</tns:P_127>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_128>', _kwota07, '</tns:P_128>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_129>', _kwota08, '</tns:P_129>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_130>', _kwota09, '</tns:P_130>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_131>', _kwota10, '</tns:P_131>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_132>', _kwota11, '</tns:P_132>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_133>', _kwota12, '</tns:P_133>\n');		 
												
		
				SELECT 	
					SUM(IF(msc=1, WynagrodzPlatnika, 0)) AS P_145, SUM(IF(msc=2, WynagrodzPlatnika, 0)),
					SUM(IF(msc=3, WynagrodzPlatnika, 0)), SUM(IF(msc=4, WynagrodzPlatnika, 0)),
					SUM(IF(msc=5, WynagrodzPlatnika, 0)), SUM(IF(msc=6, WynagrodzPlatnika, 0)),		
					SUM(IF(msc=7, WynagrodzPlatnika, 0)), SUM(IF(msc=8, WynagrodzPlatnika, 0)),
					SUM(IF(msc=9, WynagrodzPlatnika, 0)), SUM(IF(msc=10, WynagrodzPlatnika, 0)),
					SUM(IF(msc=11, WynagrodzPlatnika, 0)), SUM(IF(msc=12, WynagrodzPlatnika, 0)) AS P_156
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
						pit_4 WHERE rok = _Rok	
				;
					 
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_134>', _kwota01, '</tns:P_134>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_135>', _kwota02, '</tns:P_135>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_136>', _kwota03, '</tns:P_136>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_137>', _kwota04, '</tns:P_137>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_138>', _kwota05, '</tns:P_138>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_139>', _kwota06, '</tns:P_139>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_140>', _kwota07, '</tns:P_140>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_141>', _kwota08, '</tns:P_141>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_142>', _kwota09, '</tns:P_142>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_143>', _kwota10, '</tns:P_143>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_144>', _kwota11, '</tns:P_144>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_145>', _kwota12, '</tns:P_145>\n');		 
				
				
		
				SELECT
					SUM(IF(msc=1, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) AS P_157, 
					SUM(IF(msc=2, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ), 
					SUM(IF(msc=3, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ), 
					SUM(IF(msc=4, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ), 
					
					SUM(IF(msc=5, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ), 
					SUM(IF(msc=6, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=7, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=8, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) ,
					SUM(IF(msc=9, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=10, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=11, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=12, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) AS P_168
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
					pit_4 WHERE rok = _Rok	
				;


				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_146>', _kwota01, '</tns:P_146>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_147>', _kwota02, '</tns:P_147>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_148>', _kwota03, '</tns:P_148>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_149>', _kwota04, '</tns:P_149>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_150>', _kwota05, '</tns:P_150>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_151>', _kwota06, '</tns:P_151>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_152>', _kwota07, '</tns:P_152>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_153>', _kwota08, '</tns:P_153>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_154>', _kwota09, '</tns:P_154>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_155>', _kwota10, '</tns:P_155>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_156>', _kwota11, '</tns:P_156>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_157>', _kwota12, '</tns:P_157>\n');		 
--    nie mamy sekcji pomniejszających wiec na wynik przepisujemy to co  w 146-157  i w 158 zawsze NIE 

				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_158>', 2, '</tns:P_158>\n');	
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_171>', _kwota01, '</tns:P_171>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_172>', _kwota02, '</tns:P_172>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_173>', _kwota03, '</tns:P_173>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_174>', _kwota04, '</tns:P_174>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_175>', _kwota05, '</tns:P_175>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_176>', _kwota06, '</tns:P_176>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_177>', _kwota07, '</tns:P_177>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_178>', _kwota08, '</tns:P_178>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_179>', _kwota09, '</tns:P_179>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_180>', _kwota10, '</tns:P_180>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_181>', _kwota11, '</tns:P_181>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_182>', _kwota12, '</tns:P_182>\n');	

				SET @ePIT.poz = @ePIT.xml;	
												
				
				SET SESSION group_concat_max_len = 9000;
				
				SELECT GROUP_CONCAT(IF(IFNULL(uzasadnienie, '') <>"", CONCAT(msc,": ", IFNULL(uzasadnienie, '')),"") ORDER BY msc SEPARATOR "\n" ) AS uzasadnienie
				INTO @pit4.uzasadnienie
				FROM pit_4 WHERE rok= _Rok  AND uzasadnienie <>'' ORDER BY msc
				;
				
				
				IF LENGTH(IFNULL(@pit4.uzasadnienie, ''))>0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_183>', @pit4.uzasadnienie, '</tns:P_183>\n');	 
				END IF;
				
			/*	nieaktualna covidowa czesc nowej nie robimy
				
				SELECT IFNULL(valueB,0) FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_III_2020" INTO @covid_III_2020;
				SELECT IFNULL(valueB,0) FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_IV_2020" INTO @covid_IV_2020;
				SELECT IFNULL(valueB,0) FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_V_2020" INTO @covid_V_2020;
				SELECT IFNULL(valueB,0) FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_X_2020" INTO @covid_X_2020;
				SELECT IFNULL(valueB,0) FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_XI_2020" INTO @covid_XI_2020;
				SELECT IFNULL(valueB,0) FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_XII_2020" INTO @covid_XII_2020;
				
				IF @covid_III_2020>0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_159>', @covid_III_2020, '</P_159>\n');
				END IF;
					
				IF @covid_IV_2020>0 THEN								
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_160>', @covid_IV_2020, '</P_160>\n');				
				END IF;
									
				IF @covid_V_2020>0 THEN
				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_161>', @covid_V_2020, '</P_161>\n');
				END IF;
				
				IF @covid_X_2020>0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_162>', @covid_X_2020, '</P_162>\n');
				END IF;
				
				IF @covid_XI_2020>0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_163>', @covid_XI_2020, '</P_163>\n');
				END IF;
				
				IF @covid_XII_2020>0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_164>', @covid_XII_2020, '</P_164>\n');
				END IF;
				
				*/
				
				SET @ePIT.poz = @ePIT.xml;
			
				SET @xml.IC = @xml.IC - 1;	
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '</tns:PozycjeSzczegolowe>\n');
			
		END;
		
			
		  
		SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:Pouczenia>1</tns:Pouczenia>\n');
		
		IF (@ePIT.CelZlozenia = cKorekta) AND (LENGTH(@_przyczyna_ORD_ZU) > 0) THEN   
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:Zalaczniki>\n');									
			CALL pit_tools('pit.xml.insert.ORD_ZU', '', '', '', '', '', '10');						
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '</tns:Zalaczniki>\n');				
		END IF;
																	   						
	
		SET @ePIT.xml = CONCAT(_to_utf_xml(@ePIT.xml), '</tns:Deklaracja>');
		
		
		CALL e_doc_tools('e_doc_po.insert.or.edit', _Param_A, IFNULL(@ePIT.xml, ''), 'CREATE_XML', 'ORYG', '', '', '', '');
	END IF;

-- wersja 12 3-0E
IF _MODE = 'pit4r.xml.create.202212' THEN
SELECT
		d.ID, p.przyczyna_ORD_ZU, p.rok
		INTO _e_Doc_Def_ID, @_przyczyna_ORD_ZU, _Rok
	FROM
		pit_4r p, e_doc_def d
	WHERE
		p.GUID = _GUID AND  p.e_Doc_Def_ID = d.owner_doc_Id
	;

	SET @ePIT.xml = '';
	SET @xml.IC = 1;

	SET @ePIT.xml = '<?xml version="1.0" encoding="UTF-8" ?>\n';
	SET @ePIT.xml = CONCAT(@ePIT.xml, '<Deklaracja xmlns="', _e_Doc_Def_ID, '" ');
	SET @ePIT.xml = CONCAT(@ePIT.xml, 'xmlns:etd="http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2022/09/13/eD/DefinicjeTypy/">', '\n');
	SET @ePIT.xmlns = @ePIT.xml;


	CALL pit_tools('pit.xml.insert.header', _GUID, @ePIT.xml, 'pit_4r', 'P', '', '');


		SET @ePIT.header = @ePIT.xml;

		CALL pit_tools('pit.xml.insert.podmiot1', '', '', 'Płatnik', '', '', '');

		SET @ePIT.P1 = @ePIT.xml;


		BEGIN
			DECLARE
				_Lpodat01, _Lpodat02, _Lpodat03, _Lpodat04, _Lpodat05, _Lpodat06,
				_Lpodat07, _Lpodat08, _Lpodat09, _Lpodat10, _Lpodat11, _Lpodat12  INTEGER;



			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<PozycjeSzczegolowe>\n');
				SET @xml.IC = @xml.IC + 1;

			SELECT IFNULL(RodzajKorekty,0) INTO @RodzajKorekty FROM pit_4r WHERE GUID = _GUID;

				IF @RodzajKorekty > 0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_7>', _RodzajKorekty, '</P_7>\n');
				END IF;


				SELECT

						SUM(IF(msc=1, Lpodat1, 0)) AS P_9, SUM(IF(msc=2, Lpodat1, 0)) AS P_10,
						SUM(IF(msc=3, Lpodat1, 0)) AS P_11, SUM(IF(msc=4, Lpodat1, 0)) AS P_12,
						SUM(IF(msc=5, Lpodat1, 0)) AS P_13, SUM(IF(msc=6, Lpodat1, 0)) AS P_14,
						_varINT(SUM(IF(msc=1, Zalicz1+Zalicz2, 0))) AS P_15, _varINT(SUM(IF(msc=2, Zalicz1+Zalicz2, 0))) AS P_16,
						_varINT(SUM(IF(msc=3, Zalicz1+Zalicz2, 0))) AS P_17, _varINT(SUM(IF(msc=4, Zalicz1+Zalicz2, 0))) AS P_18,
						_varINT(SUM(IF(msc=5, Zalicz1+Zalicz2, 0))) AS P_19, _varINT(SUM(IF(msc=6, Zalicz1+Zalicz2, 0))) AS P_20,
						SUM(IF(msc=7, Lpodat1, 0)) AS P_21, SUM(IF(msc=8, Lpodat1, 0)) AS P_22,
						SUM(IF(msc=9, Lpodat1, 0)) AS P_23, SUM(IF(msc=10, Lpodat1, 0)) AS P_24,
						SUM(IF(msc=11, Lpodat1, 0)) AS P_25, SUM(IF(msc=12, Lpodat1, 0)) AS P_26,
						SUM(IF(msc=7, Zalicz1+Zalicz2, 0))  AS P_27, SUM(IF(msc=8, Zalicz1+Zalicz2, 0)) AS P_28,
						SUM(IF(msc=9, Zalicz1+Zalicz2, 0))  AS P_29, SUM(IF(msc=10, Zalicz1+Zalicz2, 0)) AS P_30,
						SUM(IF(msc=11, Zalicz1+Zalicz2, 0)) AS P_31, SUM(IF(msc=12, Zalicz1+Zalicz2, 0)) AS P_32
					INTO _Lpodat01, _Lpodat02, _Lpodat03, _Lpodat04, _Lpodat05, _Lpodat06,
						 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _Lpodat07, _Lpodat08, _Lpodat09, _Lpodat10, _Lpodat11, _Lpodat12,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM
						pit_4 WHERE rok = _Rok
					;

					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_10>', _Lpodat01, '</P_10>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_11>', _Lpodat02, '</P_11>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_12>', _Lpodat03, '</P_12>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_13>', _Lpodat04, '</P_13>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_14>', _Lpodat05, '</P_14>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_15>', _Lpodat06, '</P_15>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_16>', _kwota01, '</P_16>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_17>', _kwota02, '</P_17>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_18>', _kwota03, '</P_18>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_19>', _kwota04, '</P_19>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_20>', _kwota05, '</P_20>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_21>', _kwota06, '</P_21>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_22>', _Lpodat07, '</P_22>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_23>', _Lpodat08, '</P_23>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_24>', _Lpodat09, '</P_24>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_25>', _Lpodat10, '</P_25>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_26>', _Lpodat11, '</P_26>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_27>', _Lpodat12, '</P_27>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_28>', _kwota07, '</P_28>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_29>', _kwota08, '</P_29>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_30>', _kwota09, '</P_30>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_31>', _kwota10, '</P_31>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_32>', _kwota11, '</P_32>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_33>', _kwota12, '</P_33>\n');


					SET @kwota301=0, @kwota302=0, @kwota303=0, @kwota304=0, @kwota305=0, @kwota306=0,@kwota307=0, @kwota308=0, @kwota309=0, @kwota310=0, @kwota311=0, @kwota312=0;
					SELECT
					SUM(IF(msc=1, PodatekOdZlecen, 0)) AS P_109, SUM(IF(msc=2, PodatekOdZlecen, 0)),
					SUM(IF(msc=3, PodatekOdZlecen, 0)), SUM(IF(msc=4, PodatekOdZlecen, 0)),
					SUM(IF(msc=5, PodatekOdZlecen, 0)), SUM(IF(msc=6, PodatekOdZlecen, 0)),
					SUM(IF(msc=7, PodatekOdZlecen, 0)), SUM(IF(msc=8, PodatekOdZlecen, 0)),
					SUM(IF(msc=9, PodatekOdZlecen, 0)), SUM(IF(msc=10, PodatekOdZlecen, 0)),
					SUM(IF(msc=11, PodatekOdZlecen, 0)), SUM(IF(msc=12, PodatekOdZlecen, 0)) AS P_120
					INTO
					 @kwota301, @kwota302, @kwota303, @kwota304, @kwota305, @kwota306,
					 @kwota307, @kwota308, @kwota309, @kwota310, @kwota311, @kwota312
				FROM
						pit_4 WHERE rok = _Rok
				;

				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_46>', @kwota301, '</P_46>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_47>', @kwota302, '</P_47>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_48>', @kwota303, '</P_48>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_49>', @kwota304, '</P_49>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_50>', @kwota305, '</P_50>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_51>', @kwota306, '</P_51>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_52>', @kwota307, '</P_52>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_53>', @kwota308, '</P_53>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_54>', @kwota309, '</P_54>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_55>', @kwota310, '</P_55>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_56>', @kwota311, '</P_56>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_57>', @kwota312, '</P_57>\n');

				SET @kwota401=0, @kwota402=0, @kwota403=0, @kwota404=0, @kwota405=0, @kwota406=0,@kwota407=0, @kwota408=0, @kwota409=0, @kwota410=0, @kwota411=0, @kwota412=0;
				SELECT
					SUM(IF(msc=1, Zalicz9, 0)) AS P_121, SUM(IF(msc=2, Zalicz9, 0)),
					SUM(IF(msc=3, Zalicz9, 0)), SUM(IF(msc=4, Zalicz9, 0)),
					SUM(IF(msc=5, Zalicz9, 0)), SUM(IF(msc=6, Zalicz9, 0)),
					SUM(IF(msc=7, Zalicz9, 0)), SUM(IF(msc=8, Zalicz9, 0)),
					SUM(IF(msc=9, Zalicz9, 0)), SUM(IF(msc=10, Zalicz9, 0)),
					SUM(IF(msc=11, Zalicz9, 0)), SUM(IF(msc=12, Zalicz9, 0)) AS P_132
					INTO
					 @kwota401, @kwota402, @kwota403, @kwota404, @kwota405, @kwota406,
					 @kwota407, @kwota408, @kwota409, @kwota410, @kwota411, @kwota412
				FROM
						pit_4 WHERE rok = _Rok
				;

				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_58>', @kwota401, '</P_58>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_59>', @kwota402, '</P_59>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_60>', @kwota403, '</P_60>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_61>', @kwota404, '</P_61>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_62>', @kwota405, '</P_62>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_63>', @kwota406, '</P_63>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_64>', @kwota407, '</P_64>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_65>', @kwota408, '</P_65>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_66>', @kwota409, '</P_66>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_67>', @kwota410, '</P_67>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_68>', @kwota411, '</P_68>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_69>', @kwota412, '</P_69>\n');


					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_70>', _kwota01+@kwota301+@kwota401, '</P_70>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_71>', _kwota02+@kwota302+@kwota402, '</P_71>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_72>', _kwota03+@kwota303+@kwota403, '</P_72>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_73>', _kwota04+@kwota304+@kwota404, '</P_73>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_74>', _kwota05+@kwota305+@kwota405, '</P_74>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_75>', _kwota06+@kwota306+@kwota406, '</P_75>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_76>', _kwota07+@kwota307+@kwota407, '</P_76>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_77>', _kwota08+@kwota308+@kwota408, '</P_77>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_78>', _kwota09+@kwota309+@kwota409, '</P_78>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_79>', _kwota10+@kwota310+@kwota410, '</P_79>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_80>', _kwota11+@kwota311+@kwota411, '</P_80>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_81>', _kwota12+@kwota312+@kwota412, '</P_81>\n');


				SELECT
					SUM(IF(msc=1, Zalicz11, 0)) AS P_57, SUM(IF(msc=2, Zalicz11, 0)),
					SUM(IF(msc=3, Zalicz11, 0)), SUM(IF(msc=4, Zalicz11, 0)),
					SUM(IF(msc=5, Zalicz11, 0)), SUM(IF(msc=6, Zalicz11, 0)),
					SUM(IF(msc=7, Zalicz11, 0)), SUM(IF(msc=8, Zalicz11, 0)),
					SUM(IF(msc=9, Zalicz11, 0)), SUM(IF(msc=10, Zalicz11, 0)),
					SUM(IF(msc=11, Zalicz11, 0)), SUM(IF(msc=12, Zalicz11, 0)) AS P_68
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM
						pit_4 WHERE rok = _Rok
				;

					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_82>', _kwota01, '</P_82>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_83>', _kwota02, '</P_83>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_84>', _kwota03, '</P_84>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_85>', _kwota04, '</P_85>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_86>', _kwota05, '</P_86>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_87>', _kwota06, '</P_87>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_88>', _kwota07, '</P_88>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_89>', _kwota08, '</P_89>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_90>', _kwota09, '</P_90>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_91>', _kwota10, '</P_91>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_92>', _kwota11, '</P_92>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_93>', _kwota12, '</P_93>\n');


				SELECT
					SUM(IF(msc=1, Zalicz12, 0)) AS P_69, SUM(IF(msc=2, Zalicz12, 0)),
					SUM(IF(msc=3, Zalicz12, 0)), SUM(IF(msc=4, Zalicz12, 0)),
					SUM(IF(msc=5, Zalicz12, 0)), SUM(IF(msc=6, Zalicz12, 0)),
					SUM(IF(msc=7, Zalicz12, 0)), SUM(IF(msc=8, Zalicz12, 0)),
					SUM(IF(msc=9, Zalicz12, 0)), SUM(IF(msc=10, Zalicz12, 0)),
					SUM(IF(msc=11, Zalicz12, 0)), SUM(IF(msc=12, Zalicz12, 0)) AS P_80
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM
						pit_4 WHERE rok = _Rok
				;

					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_94>', _kwota01, '</P_94>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_95>', _kwota02, '</P_95>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_96>', _kwota03, '</P_96>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_97>', _kwota04, '</P_97>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_98>', _kwota05, '</P_98>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_99>', _kwota06, '</P_99>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_100>', _kwota07, '</P_100>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_101>', _kwota08, '</P_101>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_102>', _kwota09, '</P_102>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_103>', _kwota10, '</P_103>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_104>', _kwota11, '</P_104>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_105>', _kwota12, '</P_105>\n');


				SELECT
					SUM(IF(msc=1, zalicz13, 0)) , SUM(IF(msc=2, zalicz13, 0)),
					SUM(IF(msc=3, zalicz13, 0)), SUM(IF(msc=4, zalicz13, 0))
				INTO
					 _kwota01, _kwota02, _kwota03, _kwota04
				FROM
						pit_4 WHERE rok = _Rok
				;

				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_106>', _kwota01, '</P_106>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_107>', _kwota02, '</P_107>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_108>', _kwota03, '</P_108>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_109>', _kwota04, '</P_109>\n');

				SELECT
					SUM(IF(msc=1, PodatekPFRON, 0)) AS P_97, SUM(IF(msc=2, PodatekPFRON, 0)),
					SUM(IF(msc=3, PodatekPFRON, 0)), SUM(IF(msc=4, PodatekPFRON, 0)),
					SUM(IF(msc=5, PodatekPFRON, 0)), SUM(IF(msc=6, PodatekPFRON, 0)),
					SUM(IF(msc=7, PodatekPFRON, 0)), SUM(IF(msc=8, PodatekPFRON, 0)),
					SUM(IF(msc=9, PodatekPFRON, 0)), SUM(IF(msc=10, PodatekPFRON, 0)),
					SUM(IF(msc=11, PodatekPFRON, 0)), SUM(IF(msc=12, PodatekPFRON, 0)) AS P_108
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM
						pit_4 WHERE rok = _Rok
				;

				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_110>', _kwota01, '</P_110>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_111>', _kwota02, '</P_111>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_112>', _kwota03, '</P_112>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_113>', _kwota04, '</P_113>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_114>', _kwota05, '</P_114>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_115>', _kwota06, '</P_115>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_116>', _kwota07, '</P_116>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_117>', _kwota08, '</P_117>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_118>', _kwota09, '</P_118>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_119>', _kwota10, '</P_119>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_120>', _kwota11, '</P_120>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_121>', _kwota12, '</P_121>\n');


				SELECT
					SUM(IF(msc=1, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_133,
					SUM(IF(msc=2, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_134,
					SUM(IF(msc=3, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_135,
					SUM(IF(msc=4, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_136,

					SUM(IF(msc=5, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ),
					SUM(IF(msc=6, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) ,
					SUM(IF(msc=7, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) ,
					SUM(IF(msc=8, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) ,
					SUM(IF(msc=9, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) ,
					SUM(IF(msc=10, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) ,
					SUM(IF(msc=11, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) ,
					SUM(IF(msc=12, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_144
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM
					pit_4 WHERE rok = _Rok
				;

				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_122>', _kwota01, '</P_122>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_123>', _kwota02, '</P_123>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_124>', _kwota03, '</P_124>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_125>', _kwota04, '</P_125>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_126>', _kwota05, '</P_126>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_127>', _kwota06, '</P_127>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_128>', _kwota07, '</P_128>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_129>', _kwota08, '</P_129>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_130>', _kwota09, '</P_130>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_131>', _kwota10, '</P_131>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_132>', _kwota11, '</P_132>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_133>', _kwota12, '</P_133>\n');


				SELECT
					SUM(IF(msc=1, WynagrodzPlatnika, 0)) AS P_145, SUM(IF(msc=2, WynagrodzPlatnika, 0)),
					SUM(IF(msc=3, WynagrodzPlatnika, 0)), SUM(IF(msc=4, WynagrodzPlatnika, 0)),
					SUM(IF(msc=5, WynagrodzPlatnika, 0)), SUM(IF(msc=6, WynagrodzPlatnika, 0)),
					SUM(IF(msc=7, WynagrodzPlatnika, 0)), SUM(IF(msc=8, WynagrodzPlatnika, 0)),
					SUM(IF(msc=9, WynagrodzPlatnika, 0)), SUM(IF(msc=10, WynagrodzPlatnika, 0)),
					SUM(IF(msc=11, WynagrodzPlatnika, 0)), SUM(IF(msc=12, WynagrodzPlatnika, 0)) AS P_156
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM
						pit_4 WHERE rok = _Rok
				;

				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_134>', _kwota01, '</P_134>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_135>', _kwota02, '</P_135>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_136>', _kwota03, '</P_136>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_137>', _kwota04, '</P_137>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_138>', _kwota05, '</P_138>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_139>', _kwota06, '</P_139>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_140>', _kwota07, '</P_140>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_141>', _kwota08, '</P_141>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_142>', _kwota09, '</P_142>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_143>', _kwota10, '</P_143>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_144>', _kwota11, '</P_144>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_145>', _kwota12, '</P_145>\n');



				SELECT
					SUM(IF(msc=1, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) AS P_157,
					SUM(IF(msc=2, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ),
					SUM(IF(msc=3, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ),
					SUM(IF(msc=4, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ),

					SUM(IF(msc=5, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ),
					SUM(IF(msc=6, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) ,
					SUM(IF(msc=7, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) ,
					SUM(IF(msc=8, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) ,
					SUM(IF(msc=9, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) ,
					SUM(IF(msc=10, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) ,
					SUM(IF(msc=11, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) ,
					SUM(IF(msc=12, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) AS P_168
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM
					pit_4 WHERE rok = _Rok
				;

				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_146>', _kwota01, '</P_146>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_147>', _kwota02, '</P_147>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_148>', _kwota03, '</P_148>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_149>', _kwota04, '</P_149>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_150>', _kwota05, '</P_150>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_151>', _kwota06, '</P_151>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_152>', _kwota07, '</P_152>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_153>', _kwota08, '</P_153>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_154>', _kwota09, '</P_154>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_155>', _kwota10, '</P_155>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_156>', _kwota11, '</P_156>\n');
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_157>', _kwota12, '</P_157>\n');

				SET @ePIT.poz = @ePIT.xml;


				SET SESSION group_concat_max_len = 9000;

				SELECT GROUP_CONCAT(IF(IFNULL(uzasadnienie, '') <>"", CONCAT(msc,": ", IFNULL(uzasadnienie, '')),"") ORDER BY msc SEPARATOR "\n" ) AS uzasadnienie
				INTO @pit4.uzasadnienie
				FROM pit_4 WHERE rok= _Rok ORDER BY msc
				;


				IF LENGTH(IFNULL(@pit4.uzasadnienie, ''))>0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_158>', @pit4.uzasadnienie, '</P_158>\n');
				END IF;


				SET @ePIT.poz = @ePIT.xml;

				SET @xml.IC = @xml.IC - 1;
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '</PozycjeSzczegolowe>\n');

		END;



		SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Pouczenia>1</Pouczenia>\n');

		IF (@ePIT.CelZlozenia = cKorekta) AND (LENGTH(@_przyczyna_ORD_ZU) > 0) THEN
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Zalaczniki>\n');
			CALL pit_tools('pit.xml.insert.ORD_ZU', '', '', '', '', '', '10');
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '</Zalaczniki>\n');
		END IF;


		SET @ePIT.xml = CONCAT(_to_utf_xml(@ePIT.xml), '</Deklaracja>');


		CALL e_doc_tools('e_doc_po.insert.or.edit', _Param_A, IFNULL(@ePIT.xml, ''), 'CREATE_XML', 'ORYG', '', '', '', '');
	END IF;


-- wersja 12 1-0E
IF _MODE = 'pit4r.xml.create.202104' THEN
SELECT  
		d.ID, p.przyczyna_ORD_ZU, p.rok
		INTO _e_Doc_Def_ID, @_przyczyna_ORD_ZU, _Rok
	FROM 	
		pit_4r p, e_doc_def d 
	WHERE	
		p.GUID = _GUID AND  p.e_Doc_Def_ID = d.owner_doc_Id
	;

	SET @ePIT.xml = '';
	SET @xml.IC = 1;
		
	SET @ePIT.xml = '<?xml version="1.0" encoding="UTF-8" ?>\n';	
	SET @ePIT.xml = CONCAT(@ePIT.xml, '<Deklaracja xmlns="', _e_Doc_Def_ID, '" ');
	SET @ePIT.xml = CONCAT(@ePIT.xml, 'xmlns:etd="http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2020/03/11/eD/DefinicjeTypy/">', '\n');
	SET @ePIT.xmlns = @ePIT.xml; 	
		
	
	CALL pit_tools('pit.xml.insert.header', _GUID, @ePIT.xml, 'pit_4r', 'P', '', '');
	
	
		SET @ePIT.header = @ePIT.xml;
		
		CALL pit_tools('pit.xml.insert.podmiot1', '', '', 'Płatnik', '', '', '');  
		
		SET @ePIT.P1 = @ePIT.xml;
		
		
		BEGIN
			DECLARE 
				_Lpodat01, _Lpodat02, _Lpodat03, _Lpodat04, _Lpodat05, _Lpodat06,
				_Lpodat07, _Lpodat08, _Lpodat09, _Lpodat10, _Lpodat11, _Lpodat12  INTEGER;
			        
			
		
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<PozycjeSzczegolowe>\n');
				SET @xml.IC = @xml.IC + 1;
				
			SELECT IFNULL(RodzajKorekty,0) INTO @RodzajKorekty FROM pit_4r WHERE GUID = _GUID;
				
				IF @RodzajKorekty > 0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_7>', _RodzajKorekty, '</P_7>\n');
				END IF;
			
								
				SELECT 		
						
						SUM(IF(msc=1, Lpodat1, 0)) AS P_9, SUM(IF(msc=2, Lpodat1, 0)) AS P_10,
						SUM(IF(msc=3, Lpodat1, 0)) AS P_11, SUM(IF(msc=4, Lpodat1, 0)) AS P_12,
						SUM(IF(msc=5, Lpodat1, 0)) AS P_13, SUM(IF(msc=6, Lpodat1, 0)) AS P_14,						
						_varINT(SUM(IF(msc=1, Zalicz1+Zalicz2, 0))) AS P_15, _varINT(SUM(IF(msc=2, Zalicz1+Zalicz2, 0))) AS P_16,
						_varINT(SUM(IF(msc=3, Zalicz1+Zalicz2, 0))) AS P_17, _varINT(SUM(IF(msc=4, Zalicz1+Zalicz2, 0))) AS P_18,
						_varINT(SUM(IF(msc=5, Zalicz1+Zalicz2, 0))) AS P_19, _varINT(SUM(IF(msc=6, Zalicz1+Zalicz2, 0))) AS P_20,					
						SUM(IF(msc=7, Lpodat1, 0)) AS P_21, SUM(IF(msc=8, Lpodat1, 0)) AS P_22,
						SUM(IF(msc=9, Lpodat1, 0)) AS P_23, SUM(IF(msc=10, Lpodat1, 0)) AS P_24,
						SUM(IF(msc=11, Lpodat1, 0)) AS P_25, SUM(IF(msc=12, Lpodat1, 0)) AS P_26,
						SUM(IF(msc=7, Zalicz1+Zalicz2, 0))  AS P_27, SUM(IF(msc=8, Zalicz1+Zalicz2, 0)) AS P_28,
						SUM(IF(msc=9, Zalicz1+Zalicz2, 0))  AS P_29, SUM(IF(msc=10, Zalicz1+Zalicz2, 0)) AS P_30,
						SUM(IF(msc=11, Zalicz1+Zalicz2, 0)) AS P_31, SUM(IF(msc=12, Zalicz1+Zalicz2, 0)) AS P_32
					INTO _Lpodat01, _Lpodat02, _Lpodat03, _Lpodat04, _Lpodat05, _Lpodat06,
						 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _Lpodat07, _Lpodat08, _Lpodat09, _Lpodat10, _Lpodat11, _Lpodat12,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM 
						pit_4 WHERE rok = _Rok											
					;
					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_10>', _Lpodat01, '</P_10>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_11>', _Lpodat02, '</P_11>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_12>', _Lpodat03, '</P_12>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_13>', _Lpodat04, '</P_13>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_14>', _Lpodat05, '</P_14>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_15>', _Lpodat06, '</P_15>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_16>', _kwota01, '</P_16>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_17>', _kwota02, '</P_17>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_18>', _kwota03, '</P_18>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_19>', _kwota04, '</P_19>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_20>', _kwota05, '</P_20>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_21>', _kwota06, '</P_21>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_22>', _Lpodat07, '</P_22>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_23>', _Lpodat08, '</P_23>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_24>', _Lpodat09, '</P_24>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_25>', _Lpodat10, '</P_25>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_26>', _Lpodat11, '</P_26>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_27>', _Lpodat12, '</P_27>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_28>', _kwota07, '</P_28>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_29>', _kwota08, '</P_29>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_30>', _kwota09, '</P_30>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_31>', _kwota10, '</P_31>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_32>', _kwota11, '</P_32>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_33>', _kwota12, '</P_33>\n');													


					SET @kwota301=0, @kwota302=0, @kwota303=0, @kwota304=0, @kwota305=0, @kwota306=0,@kwota307=0, @kwota308=0, @kwota309=0, @kwota310=0, @kwota311=0, @kwota312=0;
					SELECT 	
					SUM(IF(msc=1, PodatekOdZlecen, 0)) AS P_109, SUM(IF(msc=2, PodatekOdZlecen, 0)),
					SUM(IF(msc=3, PodatekOdZlecen, 0)), SUM(IF(msc=4, PodatekOdZlecen, 0)),
					SUM(IF(msc=5, PodatekOdZlecen, 0)), SUM(IF(msc=6, PodatekOdZlecen, 0)),		
					SUM(IF(msc=7, PodatekOdZlecen, 0)), SUM(IF(msc=8, PodatekOdZlecen, 0)),
					SUM(IF(msc=9, PodatekOdZlecen, 0)), SUM(IF(msc=10, PodatekOdZlecen, 0)),
					SUM(IF(msc=11, PodatekOdZlecen, 0)), SUM(IF(msc=12, PodatekOdZlecen, 0)) AS P_120
					INTO
					 @kwota301, @kwota302, @kwota303, @kwota304, @kwota305, @kwota306,
					 @kwota307, @kwota308, @kwota309, @kwota310, @kwota311, @kwota312
				FROM 
						pit_4 WHERE rok = _Rok	
				;
														 					
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_46>', @kwota301, '</P_46>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_47>', @kwota302, '</P_47>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_48>', @kwota303, '</P_48>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_49>', @kwota304, '</P_49>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_50>', @kwota305, '</P_50>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_51>', @kwota306, '</P_51>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_52>', @kwota307, '</P_52>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_53>', @kwota308, '</P_53>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_54>', @kwota309, '</P_54>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_55>', @kwota310, '</P_55>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_56>', @kwota311, '</P_56>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_57>', @kwota312, '</P_57>\n');				
				
				SET @kwota401=0, @kwota402=0, @kwota403=0, @kwota404=0, @kwota405=0, @kwota406=0,@kwota407=0, @kwota408=0, @kwota409=0, @kwota410=0, @kwota411=0, @kwota412=0;
				SELECT 	
					SUM(IF(msc=1, Zalicz9, 0)) AS P_121, SUM(IF(msc=2, Zalicz9, 0)),
					SUM(IF(msc=3, Zalicz9, 0)), SUM(IF(msc=4, Zalicz9, 0)),
					SUM(IF(msc=5, Zalicz9, 0)), SUM(IF(msc=6, Zalicz9, 0)),		
					SUM(IF(msc=7, Zalicz9, 0)), SUM(IF(msc=8, Zalicz9, 0)),
					SUM(IF(msc=9, Zalicz9, 0)), SUM(IF(msc=10, Zalicz9, 0)),
					SUM(IF(msc=11, Zalicz9, 0)), SUM(IF(msc=12, Zalicz9, 0)) AS P_132
					INTO
					 @kwota401, @kwota402, @kwota403, @kwota404, @kwota405, @kwota406,
					 @kwota407, @kwota408, @kwota409, @kwota410, @kwota411, @kwota412
				FROM 
						pit_4 WHERE rok = _Rok	
				;
					
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_58>', @kwota401, '</P_58>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_59>', @kwota402, '</P_59>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_60>', @kwota403, '</P_60>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_61>', @kwota404, '</P_61>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_62>', @kwota405, '</P_62>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_63>', @kwota406, '</P_63>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_64>', @kwota407, '</P_64>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_65>', @kwota408, '</P_65>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_66>', @kwota409, '</P_66>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_67>', @kwota410, '</P_67>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_68>', @kwota411, '</P_68>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_69>', @kwota412, '</P_69>\n');
				
				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_70>', _kwota01+@kwota301+@kwota401, '</P_70>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_71>', _kwota02+@kwota302+@kwota402, '</P_71>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_72>', _kwota03+@kwota303+@kwota403, '</P_72>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_73>', _kwota04+@kwota304+@kwota404, '</P_73>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_74>', _kwota05+@kwota305+@kwota405, '</P_74>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_75>', _kwota06+@kwota306+@kwota406, '</P_75>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_76>', _kwota07+@kwota307+@kwota407, '</P_76>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_77>', _kwota08+@kwota308+@kwota408, '</P_77>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_78>', _kwota09+@kwota309+@kwota409, '</P_78>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_79>', _kwota10+@kwota310+@kwota410, '</P_79>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_80>', _kwota11+@kwota311+@kwota411, '</P_80>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_81>', _kwota12+@kwota312+@kwota412, '</P_81>\n');													
																
	
				SELECT 	
					SUM(IF(msc=1, Zalicz11, 0)) AS P_57, SUM(IF(msc=2, Zalicz11, 0)),
					SUM(IF(msc=3, Zalicz11, 0)), SUM(IF(msc=4, Zalicz11, 0)),
					SUM(IF(msc=5, Zalicz11, 0)), SUM(IF(msc=6, Zalicz11, 0)),		
					SUM(IF(msc=7, Zalicz11, 0)), SUM(IF(msc=8, Zalicz11, 0)),
					SUM(IF(msc=9, Zalicz11, 0)), SUM(IF(msc=10, Zalicz11, 0)),
					SUM(IF(msc=11, Zalicz11, 0)), SUM(IF(msc=12, Zalicz11, 0)) AS P_68
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
						pit_4 WHERE rok = _Rok	
				;
										 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_82>', _kwota01, '</P_82>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_83>', _kwota02, '</P_83>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_84>', _kwota03, '</P_84>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_85>', _kwota04, '</P_85>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_86>', _kwota05, '</P_86>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_87>', _kwota06, '</P_87>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_88>', _kwota07, '</P_88>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_89>', _kwota08, '</P_89>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_90>', _kwota09, '</P_90>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_91>', _kwota10, '</P_91>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_92>', _kwota11, '</P_92>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_93>', _kwota12, '</P_93>\n');									
				
				
				SELECT 	
					SUM(IF(msc=1, Zalicz12, 0)) AS P_69, SUM(IF(msc=2, Zalicz12, 0)),
					SUM(IF(msc=3, Zalicz12, 0)), SUM(IF(msc=4, Zalicz12, 0)),
					SUM(IF(msc=5, Zalicz12, 0)), SUM(IF(msc=6, Zalicz12, 0)),		
					SUM(IF(msc=7, Zalicz12, 0)), SUM(IF(msc=8, Zalicz12, 0)),
					SUM(IF(msc=9, Zalicz12, 0)), SUM(IF(msc=10, Zalicz12, 0)),
					SUM(IF(msc=11, Zalicz12, 0)), SUM(IF(msc=12, Zalicz12, 0)) AS P_80
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
						pit_4 WHERE rok = _Rok	
				;
										 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_94>', _kwota01, '</P_94>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_95>', _kwota02, '</P_95>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_96>', _kwota03, '</P_96>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_97>', _kwota04, '</P_97>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_98>', _kwota05, '</P_98>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_99>', _kwota06, '</P_99>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_100>', _kwota07, '</P_100>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_101>', _kwota08, '</P_101>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_102>', _kwota09, '</P_102>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_103>', _kwota10, '</P_103>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_104>', _kwota11, '</P_104>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_105>', _kwota12, '</P_105>\n');									
				
				
				SELECT 	
					SUM(IF(msc=1, zalicz13, 0)) , SUM(IF(msc=2, zalicz13, 0)),
					SUM(IF(msc=3, zalicz13, 0)), SUM(IF(msc=4, zalicz13, 0))
				INTO
					 _kwota01, _kwota02, _kwota03, _kwota04
				FROM 
						pit_4 WHERE rok = _Rok	
				;
														 					
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_106>', _kwota01, '</P_106>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_107>', _kwota02, '</P_107>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_108>', _kwota03, '</P_108>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_109>', _kwota04, '</P_109>\n');		
				
			/*	
				SELECT 	
					SUM(IF(msc=1, Zalicz14, 0)), SUM(IF(msc=2, Zalicz14, 0)),
					SUM(IF(msc=3, Zalicz14, 0)), SUM(IF(msc=4, Zalicz14, 0)),
					SUM(IF(msc=5, Zalicz14, 0)), SUM(IF(msc=6, Zalicz14, 0)),		
					SUM(IF(msc=7, Zalicz14, 0)), SUM(IF(msc=8, Zalicz14, 0)),
					SUM(IF(msc=9, Zalicz14, 0)), SUM(IF(msc=10, Zalicz14, 0)),
					SUM(IF(msc=11, Zalicz14, 0)), SUM(IF(msc=12, Zalicz14, 0))
				FROM 
						pit_4 WHERE rok = _Rok	
				INTO 
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,						 
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12;	
					 
					 
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_110>', _kwota01, '</P_110>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_111>', _kwota02, '</P_111>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_112>', _kwota03, '</P_112>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_113>', _kwota04, '</P_113>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_114>', _kwota05, '</P_114>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_115>', _kwota06, '</P_115>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_116>', _kwota07, '</P_116>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_117>', _kwota08, '</P_117>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_118>', _kwota09, '</P_118>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_119>', _kwota10, '</P_119>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_120>', _kwota11, '</P_120>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_121>', _kwota12, '</P_121>\n');	
			*/
				SELECT 	
					SUM(IF(msc=1, PodatekPFRON, 0)) AS P_97, SUM(IF(msc=2, PodatekPFRON, 0)),
					SUM(IF(msc=3, PodatekPFRON, 0)), SUM(IF(msc=4, PodatekPFRON, 0)),
					SUM(IF(msc=5, PodatekPFRON, 0)), SUM(IF(msc=6, PodatekPFRON, 0)),		
					SUM(IF(msc=7, PodatekPFRON, 0)), SUM(IF(msc=8, PodatekPFRON, 0)),
					SUM(IF(msc=9, PodatekPFRON, 0)), SUM(IF(msc=10, PodatekPFRON, 0)),
					SUM(IF(msc=11, PodatekPFRON, 0)), SUM(IF(msc=12, PodatekPFRON, 0)) AS P_108
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
						pit_4 WHERE rok = _Rok	
				;
														 					
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_110>', _kwota01, '</P_110>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_111>', _kwota02, '</P_111>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_112>', _kwota03, '</P_112>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_113>', _kwota04, '</P_113>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_114>', _kwota05, '</P_114>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_115>', _kwota06, '</P_115>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_116>', _kwota07, '</P_116>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_117>', _kwota08, '</P_117>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_118>', _kwota09, '</P_118>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_119>', _kwota10, '</P_119>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_120>', _kwota11, '</P_120>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_121>', _kwota12, '</P_121>\n');									
					
			
				

		
		
				
				
				SELECT
					SUM(IF(msc=1, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_133, 
					SUM(IF(msc=2, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_134, 
					SUM(IF(msc=3, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_135, 
					SUM(IF(msc=4, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_136, 
					
					SUM(IF(msc=5, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ), 
					SUM(IF(msc=6, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=7, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=8, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) ,
					SUM(IF(msc=9, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=10, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=11, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=12, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_144
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
					pit_4 WHERE rok = _Rok	
				;

				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_122>', _kwota01, '</P_122>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_123>', _kwota02, '</P_123>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_124>', _kwota03, '</P_124>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_125>', _kwota04, '</P_125>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_126>', _kwota05, '</P_126>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_127>', _kwota06, '</P_127>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_128>', _kwota07, '</P_128>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_129>', _kwota08, '</P_129>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_130>', _kwota09, '</P_130>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_131>', _kwota10, '</P_131>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_132>', _kwota11, '</P_132>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_133>', _kwota12, '</P_133>\n');		 
												
		
				SELECT 	
					SUM(IF(msc=1, WynagrodzPlatnika, 0)) AS P_145, SUM(IF(msc=2, WynagrodzPlatnika, 0)),
					SUM(IF(msc=3, WynagrodzPlatnika, 0)), SUM(IF(msc=4, WynagrodzPlatnika, 0)),
					SUM(IF(msc=5, WynagrodzPlatnika, 0)), SUM(IF(msc=6, WynagrodzPlatnika, 0)),		
					SUM(IF(msc=7, WynagrodzPlatnika, 0)), SUM(IF(msc=8, WynagrodzPlatnika, 0)),
					SUM(IF(msc=9, WynagrodzPlatnika, 0)), SUM(IF(msc=10, WynagrodzPlatnika, 0)),
					SUM(IF(msc=11, WynagrodzPlatnika, 0)), SUM(IF(msc=12, WynagrodzPlatnika, 0)) AS P_156
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
						pit_4 WHERE rok = _Rok	
				;
					 
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_134>', _kwota01, '</P_134>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_135>', _kwota02, '</P_135>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_136>', _kwota03, '</P_136>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_137>', _kwota04, '</P_137>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_138>', _kwota05, '</P_138>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_139>', _kwota06, '</P_139>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_140>', _kwota07, '</P_140>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_141>', _kwota08, '</P_141>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_142>', _kwota09, '</P_142>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_143>', _kwota10, '</P_143>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_144>', _kwota11, '</P_144>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_145>', _kwota12, '</P_145>\n');		 
				
				
		
				SELECT
					SUM(IF(msc=1, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) AS P_157, 
					SUM(IF(msc=2, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ), 
					SUM(IF(msc=3, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ), 
					SUM(IF(msc=4, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ), 
					
					SUM(IF(msc=5, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ), 
					SUM(IF(msc=6, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=7, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=8, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) ,
					SUM(IF(msc=9, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=10, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=11, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=12, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) AS P_168
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
					pit_4 WHERE rok = _Rok	
				;

				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_146>', _kwota01, '</P_146>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_147>', _kwota02, '</P_147>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_148>', _kwota03, '</P_148>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_149>', _kwota04, '</P_149>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_150>', _kwota05, '</P_150>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_151>', _kwota06, '</P_151>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_152>', _kwota07, '</P_152>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_153>', _kwota08, '</P_153>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_154>', _kwota09, '</P_154>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_155>', _kwota10, '</P_155>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_156>', _kwota11, '</P_156>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_157>', _kwota12, '</P_157>\n');	

				SET @ePIT.poz = @ePIT.xml;	
												
				
				SET SESSION group_concat_max_len = 9000;
				
				SELECT GROUP_CONCAT(IF(IFNULL(uzasadnienie, '') <>"", CONCAT(msc,": ", IFNULL(uzasadnienie, '')),"") ORDER BY msc SEPARATOR "\n" ) AS uzasadnienie
				INTO @pit4.uzasadnienie
				FROM pit_4 WHERE rok= _Rok ORDER BY msc
				;
				
				
				IF LENGTH(IFNULL(@pit4.uzasadnienie, ''))>0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_158>', @pit4.uzasadnienie, '</P_158>\n');	 
				END IF;
				
			/*	nieaktualna covidowa czesc nowej nie robimy
				
				SELECT IFNULL(valueB,0) FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_III_2020" INTO @covid_III_2020;
				SELECT IFNULL(valueB,0) FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_IV_2020" INTO @covid_IV_2020;
				SELECT IFNULL(valueB,0) FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_V_2020" INTO @covid_V_2020;
				SELECT IFNULL(valueB,0) FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_X_2020" INTO @covid_X_2020;
				SELECT IFNULL(valueB,0) FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_XI_2020" INTO @covid_XI_2020;
				SELECT IFNULL(valueB,0) FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_XII_2020" INTO @covid_XII_2020;
				
				IF @covid_III_2020>0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_159>', @covid_III_2020, '</P_159>\n');
				END IF;
					
				IF @covid_IV_2020>0 THEN								
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_160>', @covid_IV_2020, '</P_160>\n');				
				END IF;
									
				IF @covid_V_2020>0 THEN
				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_161>', @covid_V_2020, '</P_161>\n');
				END IF;
				
				IF @covid_X_2020>0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_162>', @covid_X_2020, '</P_162>\n');
				END IF;
				
				IF @covid_XI_2020>0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_163>', @covid_XI_2020, '</P_163>\n');
				END IF;
				
				IF @covid_XII_2020>0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_164>', @covid_XII_2020, '</P_164>\n');
				END IF;
				
				*/
				
				SET @ePIT.poz = @ePIT.xml;
			
				SET @xml.IC = @xml.IC - 1;	
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '</PozycjeSzczegolowe>\n');
			
		END;
		
			
		  
		SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Pouczenia>1</Pouczenia>\n');
		
		IF (@ePIT.CelZlozenia = cKorekta) AND (LENGTH(@_przyczyna_ORD_ZU) > 0) THEN   
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Zalaczniki>\n');									
			CALL pit_tools('pit.xml.insert.ORD_ZU', '', '', '', '', '', '4');						
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '</Zalaczniki>\n');				
		END IF;
																	   						
	
		SET @ePIT.xml = CONCAT(_to_utf_xml(@ePIT.xml), '</Deklaracja>');
		
		
		CALL e_doc_tools('e_doc_po.insert.or.edit', _Param_A, IFNULL(@ePIT.xml, ''), 'CREATE_XML', 'ORYG', '', '', '', '');
	END IF;


IF _MODE = 'pit4r.xml.create.WERSJA10BAD' THEN
SELECT  
		d.ID, p.przyczyna_ORD_ZU, p.rok
		INTO _e_Doc_Def_ID, @_przyczyna_ORD_ZU, _Rok
	FROM 	
		pit_4r p, e_doc_def d 
	WHERE	
		p.GUID = _GUID AND  p.e_Doc_Def_ID = d.owner_doc_Id
	;

	SET @ePIT.xml = '';
	SET @xml.IC = 1;
		
	SET @ePIT.xml = '<?xml version="1.0" encoding="UTF-8" ?>\n';	
	SET @ePIT.xml = CONCAT(@ePIT.xml, '<Deklaracja xmlns="', _e_Doc_Def_ID, '" ');
	SET @ePIT.xml = CONCAT(@ePIT.xml, 'xmlns:etd="http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2020/03/11/eD/DefinicjeTypy/">', '\n');
	SET @ePIT.xmlns = @ePIT.xml; 	
		
	
	CALL pit_tools('pit.xml.insert.header', _GUID, @ePIT.xml, 'pit_4r', 'P', '', '');
	
	
		SET @ePIT.header = @ePIT.xml;
		
		CALL pit_tools('pit.xml.insert.podmiot1', '', '', 'Płatnik', '', '', '');  
		
		SET @ePIT.P1 = @ePIT.xml;
		
		
		BEGIN
			DECLARE 
				_Lpodat01, _Lpodat02, _Lpodat03, _Lpodat04, _Lpodat05, _Lpodat06,
				_Lpodat07, _Lpodat08, _Lpodat09, _Lpodat10, _Lpodat11, _Lpodat12  INTEGER;
			        
			
		
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<PozycjeSzczegolowe>\n');
				SET @xml.IC = @xml.IC + 1;
				
			SELECT IFNULL(RodzajKorekty,0) INTO @RodzajKorekty FROM pit_4r WHERE GUID = _GUID;
				
				IF @RodzajKorekty > 0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_7>', _RodzajKorekty, '</P_7>\n');
				END IF;
			
								
				SELECT 		
						
						SUM(IF(msc=1, Lpodat1, 0)) AS P_9, SUM(IF(msc=2, Lpodat1, 0)) AS P_10,
						SUM(IF(msc=3, Lpodat1, 0)) AS P_11, SUM(IF(msc=4, Lpodat1, 0)) AS P_12,
						SUM(IF(msc=5, Lpodat1, 0)) AS P_13, SUM(IF(msc=6, Lpodat1, 0)) AS P_14,						
						_varINT(SUM(IF(msc=1, Zalicz1+Zalicz2, 0))) AS P_15, _varINT(SUM(IF(msc=2, Zalicz1+Zalicz2, 0))) AS P_16,
						_varINT(SUM(IF(msc=3, Zalicz1+Zalicz2, 0))) AS P_17, _varINT(SUM(IF(msc=4, Zalicz1+Zalicz2, 0))) AS P_18,
						_varINT(SUM(IF(msc=5, Zalicz1+Zalicz2, 0))) AS P_19, _varINT(SUM(IF(msc=6, Zalicz1+Zalicz2, 0))) AS P_20,					
						SUM(IF(msc=7, Lpodat1, 0)) AS P_21, SUM(IF(msc=8, Lpodat1, 0)) AS P_22,
						SUM(IF(msc=9, Lpodat1, 0)) AS P_23, SUM(IF(msc=10, Lpodat1, 0)) AS P_24,
						SUM(IF(msc=11, Lpodat1, 0)) AS P_25, SUM(IF(msc=12, Lpodat1, 0)) AS P_26,
						SUM(IF(msc=7, Zalicz1+Zalicz2, 0))  AS P_27, SUM(IF(msc=8, Zalicz1+Zalicz2, 0)) AS P_28,
						SUM(IF(msc=9, Zalicz1+Zalicz2, 0))  AS P_29, SUM(IF(msc=10, Zalicz1+Zalicz2, 0)) AS P_30,
						SUM(IF(msc=11, Zalicz1+Zalicz2, 0)) AS P_31, SUM(IF(msc=12, Zalicz1+Zalicz2, 0)) AS P_32
					INTO _Lpodat01, _Lpodat02, _Lpodat03, _Lpodat04, _Lpodat05, _Lpodat06,
						 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _Lpodat07, _Lpodat08, _Lpodat09, _Lpodat10, _Lpodat11, _Lpodat12,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM 
						pit_4 WHERE rok = _Rok											
					;
						 
					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL),  '<P_10>', _Lpodat01, '</P_10>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_11>', _Lpodat02, '</P_11>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_12>', _Lpodat03, '</P_12>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_13>', _Lpodat04, '</P_13>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_14>', _Lpodat05, '</P_14>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_15>', _Lpodat06, '</P_15>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_16>', _kwota01, '</P_16>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_17>', _kwota02, '</P_17>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_18>', _kwota03, '</P_18>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_19>', _kwota04, '</P_19>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_20>', _kwota05, '</P_20>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_21>', _kwota06, '</P_21>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_22>', _Lpodat07, '</P_22>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_23>', _Lpodat08, '</P_23>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_24>', _Lpodat09, '</P_24>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_25>', _Lpodat10, '</P_25>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_26>', _Lpodat11, '</P_26>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_27>', _Lpodat12, '</P_27>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_28>', _kwota07, '</P_28>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_29>', _kwota08, '</P_29>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_30>', _kwota09, '</P_30>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_31>', _kwota10, '</P_31>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_32>', _kwota11, '</P_32>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_33>', _kwota12, '</P_33>\n');													


					SET @kwota301=0, @kwota302=0, @kwota303=0, @kwota304=0, @kwota305=0, @kwota306=0,@kwota307=0, @kwota308=0, @kwota309=0, @kwota310=0, @kwota311=0, @kwota312=0;
					SELECT 	
					SUM(IF(msc=1, PodatekOdZlecen, 0)) AS P_109, SUM(IF(msc=2, PodatekOdZlecen, 0)),
					SUM(IF(msc=3, PodatekOdZlecen, 0)), SUM(IF(msc=4, PodatekOdZlecen, 0)),
					SUM(IF(msc=5, PodatekOdZlecen, 0)), SUM(IF(msc=6, PodatekOdZlecen, 0)),		
					SUM(IF(msc=7, PodatekOdZlecen, 0)), SUM(IF(msc=8, PodatekOdZlecen, 0)),
					SUM(IF(msc=9, PodatekOdZlecen, 0)), SUM(IF(msc=10, PodatekOdZlecen, 0)),
					SUM(IF(msc=11, PodatekOdZlecen, 0)), SUM(IF(msc=12, PodatekOdZlecen, 0)) AS P_120
					INTO
					 @kwota301, @kwota302, @kwota303, @kwota304, @kwota305, @kwota306,
					 @kwota307, @kwota308, @kwota309, @kwota310, @kwota311, @kwota312
				FROM 
						pit_4 WHERE rok = _Rok	
				;
														 					
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_46>', @kwota301, '</P_46>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_47>', @kwota302, '</P_47>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_48>', @kwota303, '</P_48>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_49>', @kwota304, '</P_49>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_50>', @kwota305, '</P_50>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_51>', @kwota306, '</P_51>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_52>', @kwota307, '</P_52>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_53>', @kwota308, '</P_53>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_54>', @kwota309, '</P_54>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_55>', @kwota310, '</P_55>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_56>', @kwota311, '</P_56>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_57>', @kwota312, '</P_57>\n');				
				
				SET @kwota401=0, @kwota402=0, @kwota403=0, @kwota404=0, @kwota405=0, @kwota406=0,@kwota407=0, @kwota408=0, @kwota409=0, @kwota410=0, @kwota411=0, @kwota412=0;
				SELECT 	
					SUM(IF(msc=1, Zalicz9, 0)) AS P_121, SUM(IF(msc=2, Zalicz9, 0)),
					SUM(IF(msc=3, Zalicz9, 0)), SUM(IF(msc=4, Zalicz9, 0)),
					SUM(IF(msc=5, Zalicz9, 0)), SUM(IF(msc=6, Zalicz9, 0)),		
					SUM(IF(msc=7, Zalicz9, 0)), SUM(IF(msc=8, Zalicz9, 0)),
					SUM(IF(msc=9, Zalicz9, 0)), SUM(IF(msc=10, Zalicz9, 0)),
					SUM(IF(msc=11, Zalicz9, 0)), SUM(IF(msc=12, Zalicz9, 0)) AS P_132
					INTO
					 @kwota401, @kwota402, @kwota403, @kwota404, @kwota405, @kwota406,
					 @kwota407, @kwota408, @kwota409, @kwota410, @kwota411, @kwota412
				FROM 
						pit_4 WHERE rok = _Rok	
				;
					
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_58>', @kwota401, '</P_58>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_59>', @kwota402, '</P_59>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_60>', @kwota403, '</P_60>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_61>', @kwota404, '</P_61>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_62>', @kwota405, '</P_62>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_63>', @kwota406, '</P_63>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_64>', @kwota407, '</P_64>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_65>', @kwota408, '</P_65>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_66>', @kwota409, '</P_66>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_67>', @kwota410, '</P_67>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_68>', @kwota411, '</P_68>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_69>', @kwota412, '</P_69>\n');
				
				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_70>', _kwota01+@kwota301+@kwota401, '</P_70>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_71>', _kwota02+@kwota302+@kwota402, '</P_71>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_72>', _kwota03+@kwota303+@kwota403, '</P_72>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_73>', _kwota04+@kwota304+@kwota404, '</P_73>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_74>', _kwota05+@kwota305+@kwota405, '</P_74>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_75>', _kwota06+@kwota306+@kwota406, '</P_75>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_76>', _kwota07+@kwota307+@kwota407, '</P_76>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_77>', _kwota08+@kwota308+@kwota408, '</P_77>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_78>', _kwota09+@kwota309+@kwota409, '</P_78>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_79>', _kwota10+@kwota310+@kwota410, '</P_79>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_80>', _kwota11+@kwota311+@kwota411, '</P_80>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_81>', _kwota12+@kwota312+@kwota412, '</P_81>\n');													
																
	
				SELECT 	
					SUM(IF(msc=1, Zalicz11, 0)) AS P_57, SUM(IF(msc=2, Zalicz11, 0)),
					SUM(IF(msc=3, Zalicz11, 0)), SUM(IF(msc=4, Zalicz11, 0)),
					SUM(IF(msc=5, Zalicz11, 0)), SUM(IF(msc=6, Zalicz11, 0)),		
					SUM(IF(msc=7, Zalicz11, 0)), SUM(IF(msc=8, Zalicz11, 0)),
					SUM(IF(msc=9, Zalicz11, 0)), SUM(IF(msc=10, Zalicz11, 0)),
					SUM(IF(msc=11, Zalicz11, 0)), SUM(IF(msc=12, Zalicz11, 0)) AS P_68
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
						pit_4 WHERE rok = _Rok	
				;
										 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_82>', _kwota01, '</P_82>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_83>', _kwota02, '</P_83>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_84>', _kwota03, '</P_84>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_85>', _kwota04, '</P_85>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_86>', _kwota05, '</P_86>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_87>', _kwota06, '</P_87>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_88>', _kwota07, '</P_88>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_89>', _kwota08, '</P_89>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_90>', _kwota09, '</P_90>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_91>', _kwota10, '</P_91>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_92>', _kwota11, '</P_92>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_93>', _kwota12, '</P_93>\n');									
				
				
				SELECT 	
					SUM(IF(msc=1, Zalicz12, 0)) AS P_69, SUM(IF(msc=2, Zalicz12, 0)),
					SUM(IF(msc=3, Zalicz12, 0)), SUM(IF(msc=4, Zalicz12, 0)),
					SUM(IF(msc=5, Zalicz12, 0)), SUM(IF(msc=6, Zalicz12, 0)),		
					SUM(IF(msc=7, Zalicz12, 0)), SUM(IF(msc=8, Zalicz12, 0)),
					SUM(IF(msc=9, Zalicz12, 0)), SUM(IF(msc=10, Zalicz12, 0)),
					SUM(IF(msc=11, Zalicz12, 0)), SUM(IF(msc=12, Zalicz12, 0)) AS P_80
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
						pit_4 WHERE rok = _Rok	
				;
										 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_94>', _kwota01, '</P_94>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_95>', _kwota02, '</P_95>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_96>', _kwota03, '</P_96>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_97>', _kwota04, '</P_97>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_98>', _kwota05, '</P_98>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_99>', _kwota06, '</P_99>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_100>', _kwota07, '</P_100>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_101>', _kwota08, '</P_101>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_102>', _kwota09, '</P_102>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_103>', _kwota10, '</P_103>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_104>', _kwota11, '</P_104>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_105>', _kwota12, '</P_105>\n');									
				
				
				SELECT 	
					SUM(IF(msc=1, zalicz13, 0)) , SUM(IF(msc=2, zalicz13, 0)),
					SUM(IF(msc=3, zalicz13, 0)), SUM(IF(msc=4, zalicz13, 0))
				INTO
					 _kwota01, _kwota02, _kwota03, _kwota04
				FROM 
						pit_4 WHERE rok = _Rok	
				;
														 					
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_106>', _kwota01, '</P_106>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_107>', _kwota02, '</P_107>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_108>', _kwota03, '</P_108>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_109>', _kwota04, '</P_109>\n');		
				
			/*	
				SELECT 	
					SUM(IF(msc=1, Zalicz14, 0)), SUM(IF(msc=2, Zalicz14, 0)),
					SUM(IF(msc=3, Zalicz14, 0)), SUM(IF(msc=4, Zalicz14, 0)),
					SUM(IF(msc=5, Zalicz14, 0)), SUM(IF(msc=6, Zalicz14, 0)),		
					SUM(IF(msc=7, Zalicz14, 0)), SUM(IF(msc=8, Zalicz14, 0)),
					SUM(IF(msc=9, Zalicz14, 0)), SUM(IF(msc=10, Zalicz14, 0)),
					SUM(IF(msc=11, Zalicz14, 0)), SUM(IF(msc=12, Zalicz14, 0))
				FROM 
						pit_4 WHERE rok = _Rok	
				INTO 
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,						 
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12;	
					 
					 
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_110>', _kwota01, '</P_110>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_111>', _kwota02, '</P_111>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_112>', _kwota03, '</P_112>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_113>', _kwota04, '</P_113>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_114>', _kwota05, '</P_114>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_115>', _kwota06, '</P_115>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_116>', _kwota07, '</P_116>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_117>', _kwota08, '</P_117>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_118>', _kwota09, '</P_118>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_119>', _kwota10, '</P_119>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_120>', _kwota11, '</P_120>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_121>', _kwota12, '</P_121>\n');	
			*/
				SELECT 	
					SUM(IF(msc=1, PodatekPFRON, 0)) AS P_97, SUM(IF(msc=2, PodatekPFRON, 0)),
					SUM(IF(msc=3, PodatekPFRON, 0)), SUM(IF(msc=4, PodatekPFRON, 0)),
					SUM(IF(msc=5, PodatekPFRON, 0)), SUM(IF(msc=6, PodatekPFRON, 0)),		
					SUM(IF(msc=7, PodatekPFRON, 0)), SUM(IF(msc=8, PodatekPFRON, 0)),
					SUM(IF(msc=9, PodatekPFRON, 0)), SUM(IF(msc=10, PodatekPFRON, 0)),
					SUM(IF(msc=11, PodatekPFRON, 0)), SUM(IF(msc=12, PodatekPFRON, 0)) AS P_108
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
						pit_4 WHERE rok = _Rok	
				;
														 					
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_110>', _kwota01, '</P_110>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_111>', _kwota02, '</P_111>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_112>', _kwota03, '</P_112>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_113>', _kwota04, '</P_113>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_114>', _kwota05, '</P_114>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_115>', _kwota06, '</P_115>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_116>', _kwota07, '</P_116>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_117>', _kwota08, '</P_117>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_118>', _kwota09, '</P_118>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_119>', _kwota10, '</P_119>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_120>', _kwota11, '</P_120>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_121>', _kwota12, '</P_121>\n');									
					
			
				

		
		
				
				
				SELECT
					SUM(IF(msc=1, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_133, 
					SUM(IF(msc=2, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_134, 
					SUM(IF(msc=3, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_135, 
					SUM(IF(msc=4, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_136, 
					
					SUM(IF(msc=5, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ), 
					SUM(IF(msc=6, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=7, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=8, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) ,
					SUM(IF(msc=9, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=10, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=11, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=12, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_144
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
					pit_4 WHERE rok = _Rok	
				;

				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_122>', _kwota01, '</P_122>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_123>', _kwota02, '</P_123>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_124>', _kwota03, '</P_124>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_125>', _kwota04, '</P_125>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_126>', _kwota05, '</P_126>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_127>', _kwota06, '</P_127>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_128>', _kwota07, '</P_128>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_129>', _kwota08, '</P_129>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_130>', _kwota09, '</P_130>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_131>', _kwota10, '</P_131>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_132>', _kwota11, '</P_132>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_133>', _kwota12, '</P_133>\n');		 
												
		
				SELECT 	
					SUM(IF(msc=1, WynagrodzPlatnika, 0)) AS P_145, SUM(IF(msc=2, WynagrodzPlatnika, 0)),
					SUM(IF(msc=3, WynagrodzPlatnika, 0)), SUM(IF(msc=4, WynagrodzPlatnika, 0)),
					SUM(IF(msc=5, WynagrodzPlatnika, 0)), SUM(IF(msc=6, WynagrodzPlatnika, 0)),		
					SUM(IF(msc=7, WynagrodzPlatnika, 0)), SUM(IF(msc=8, WynagrodzPlatnika, 0)),
					SUM(IF(msc=9, WynagrodzPlatnika, 0)), SUM(IF(msc=10, WynagrodzPlatnika, 0)),
					SUM(IF(msc=11, WynagrodzPlatnika, 0)), SUM(IF(msc=12, WynagrodzPlatnika, 0)) AS P_156
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
						pit_4 WHERE rok = _Rok	
				;
					 
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_134>', _kwota01, '</P_134>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_135>', _kwota02, '</P_135>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_136>', _kwota03, '</P_136>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_137>', _kwota04, '</P_137>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_138>', _kwota05, '</P_138>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_139>', _kwota06, '</P_139>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_140>', _kwota07, '</P_140>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_141>', _kwota08, '</P_141>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_142>', _kwota09, '</P_142>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_143>', _kwota10, '</P_143>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_144>', _kwota11, '</P_144>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_145>', _kwota12, '</P_145>\n');		 
				
				
		
				SELECT
					SUM(IF(msc=1, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) AS P_157, 
					SUM(IF(msc=2, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ), 
					SUM(IF(msc=3, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ), 
					SUM(IF(msc=4, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ), 
					
					SUM(IF(msc=5, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ), 
					SUM(IF(msc=6, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=7, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=8, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) ,
					SUM(IF(msc=9, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=10, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=11, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=12, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) AS P_168
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
					pit_4 WHERE rok = _Rok	
				;

				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_146>', _kwota01, '</P_146>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_147>', _kwota02, '</P_147>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_148>', _kwota03, '</P_148>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_149>', _kwota04, '</P_149>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_150>', _kwota05, '</P_150>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_151>', _kwota06, '</P_151>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_152>', _kwota07, '</P_152>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_153>', _kwota08, '</P_153>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_154>', _kwota09, '</P_154>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_155>', _kwota10, '</P_155>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_156>', _kwota11, '</P_156>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_157>', _kwota12, '</P_157>\n');	

				SET @ePIT.poz = @ePIT.xml;	
												
				
				SET SESSION group_concat_max_len = 9000;
				
				SELECT GROUP_CONCAT(IF(IFNULL(uzasadnienie, '') <>"", CONCAT(msc,": ", IFNULL(uzasadnienie, '')),"") ORDER BY msc SEPARATOR "\n" ) AS uzasadnienie
				INTO @pit4.uzasadnienie
				FROM pit_4 WHERE rok= _Rok ORDER BY msc
				;
				
				
				IF LENGTH(IFNULL(@pit4.uzasadnienie, ''))>0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_158>', @pit4.uzasadnienie, '</P_158>\n');	 
				END IF;
				
				
				
				SELECT IFNULL(valueB,0) INTO @covid_III_2020 FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_III_2020";
				SELECT IFNULL(valueB,0) INTO @covid_IV_2020 FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_IV_2020";
				SELECT IFNULL(valueB,0) INTO @covid_V_2020 FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_V_2020";
				
				IF @covid_III_2020>0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_159>', @covid_III_2020, '</P_159>\n');
				END IF;
					
				IF @covid_IV_2020>0 THEN								
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_160>', @covid_IV_2020, '</P_160>\n');				
				END IF;
									
				IF @covid_V_2020>0 THEN
				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_161>', @covid_V_2020, '</P_161>\n');
				END IF;
				
				SET @ePIT.poz = @ePIT.xml;
			
				SET @xml.IC = @xml.IC - 1;	
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '</PozycjeSzczegolowe>\n');
			
		END;
		
			
		  
		SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Pouczenia>1</Pouczenia>\n');
		
		IF (@ePIT.CelZlozenia = cKorekta) AND (LENGTH(@_przyczyna_ORD_ZU) > 0) THEN   
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Zalaczniki>\n');									
			CALL pit_tools('pit.xml.insert.ORD_ZU', '', '', '', '', '', '4');						
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '</Zalaczniki>\n');				
		END IF;
																	   						
	
		SET @ePIT.xml = CONCAT(_to_utf_xml(@ePIT.xml), '</Deklaracja>');
		
		
		CALL e_doc_tools('e_doc_po.insert.or.edit', _Param_A, IFNULL(@ePIT.xml, ''), 'CREATE_XML', 'ORYG', '', '', '', '');
	END IF;

IF _MODE = 'pit4r.xml.create.202001' THEN
SELECT  
		d.ID, p.przyczyna_ORD_ZU, p.rok
		INTO _e_Doc_Def_ID, @_przyczyna_ORD_ZU, _Rok
	FROM 	
		pit_4r p, e_doc_def d 
	WHERE	
		p.GUID = _GUID AND  p.e_Doc_Def_ID = d.owner_doc_Id
	;

	SET @ePIT.xml = '';
	SET @xml.IC = 1;
		
	SET @ePIT.xml = '<?xml version="1.0" encoding="UTF-8" ?>\n';	
	SET @ePIT.xml = CONCAT(@ePIT.xml, '<Deklaracja xmlns="', _e_Doc_Def_ID, '" ');
	SET @ePIT.xml = CONCAT(@ePIT.xml, 'xmlns:etd="http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2020/03/11/eD/DefinicjeTypy/">', '\n');
	SET @ePIT.xmlns = @ePIT.xml; 	
		
	
	CALL pit_tools('pit.xml.insert.header', _GUID, @ePIT.xml, 'pit_4r', 'P', '', '');
	
	
		SET @ePIT.header = @ePIT.xml;
		
		CALL pit_tools('pit.xml.insert.podmiot1', '', '', 'Płatnik', '', '', '');  
		
		SET @ePIT.P1 = @ePIT.xml;
		
		
		BEGIN
			DECLARE 
				_Lpodat01, _Lpodat02, _Lpodat03, _Lpodat04, _Lpodat05, _Lpodat06,
				_Lpodat07, _Lpodat08, _Lpodat09, _Lpodat10, _Lpodat11, _Lpodat12  INTEGER;
			        
			
		
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<PozycjeSzczegolowe>\n');
				SET @xml.IC = @xml.IC + 1;
				
			SELECT IFNULL(RodzajKorekty,0) INTO @RodzajKorekty FROM pit_4r WHERE GUID = _GUID;
				
				IF @RodzajKorekty > 0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_7>', _RodzajKorekty, '</P_7>\n');
				END IF;
			
								
				SELECT 		
						
						SUM(IF(msc=1, Lpodat1, 0)) AS P_9, SUM(IF(msc=2, Lpodat1, 0)) AS P_10,
						SUM(IF(msc=3, Lpodat1, 0)) AS P_11, SUM(IF(msc=4, Lpodat1, 0)) AS P_12,
						SUM(IF(msc=5, Lpodat1, 0)) AS P_13, SUM(IF(msc=6, Lpodat1, 0)) AS P_14,						
						_varINT(SUM(IF(msc=1, Zalicz1+Zalicz2, 0))) AS P_15, _varINT(SUM(IF(msc=2, Zalicz1+Zalicz2, 0))) AS P_16,
						_varINT(SUM(IF(msc=3, Zalicz1+Zalicz2, 0))) AS P_17, _varINT(SUM(IF(msc=4, Zalicz1+Zalicz2, 0))) AS P_18,
						_varINT(SUM(IF(msc=5, Zalicz1+Zalicz2, 0))) AS P_19, _varINT(SUM(IF(msc=6, Zalicz1+Zalicz2, 0))) AS P_20,					
						SUM(IF(msc=7, Lpodat1, 0)) AS P_21, SUM(IF(msc=8, Lpodat1, 0)) AS P_22,
						SUM(IF(msc=9, Lpodat1, 0)) AS P_23, SUM(IF(msc=10, Lpodat1, 0)) AS P_24,
						SUM(IF(msc=11, Lpodat1, 0)) AS P_25, SUM(IF(msc=12, Lpodat1, 0)) AS P_26,
						SUM(IF(msc=7, Zalicz1+Zalicz2, 0))  AS P_27, SUM(IF(msc=8, Zalicz1+Zalicz2, 0)) AS P_28,
						SUM(IF(msc=9, Zalicz1+Zalicz2, 0))  AS P_29, SUM(IF(msc=10, Zalicz1+Zalicz2, 0)) AS P_30,
						SUM(IF(msc=11, Zalicz1+Zalicz2, 0)) AS P_31, SUM(IF(msc=12, Zalicz1+Zalicz2, 0)) AS P_32
					INTO _Lpodat01, _Lpodat02, _Lpodat03, _Lpodat04, _Lpodat05, _Lpodat06,
						 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _Lpodat07, _Lpodat08, _Lpodat09, _Lpodat10, _Lpodat11, _Lpodat12,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM 
						pit_4 WHERE rok = _Rok
					;
						 
					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL),  '<P_10>', _Lpodat01, '</P_10>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_11>', _Lpodat02, '</P_11>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_12>', _Lpodat03, '</P_12>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_13>', _Lpodat04, '</P_13>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_14>', _Lpodat05, '</P_14>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_15>', _Lpodat06, '</P_15>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_16>', _kwota01, '</P_16>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_17>', _kwota02, '</P_17>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_18>', _kwota03, '</P_18>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_19>', _kwota04, '</P_19>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_20>', _kwota05, '</P_20>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_21>', _kwota06, '</P_21>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_22>', _Lpodat07, '</P_22>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_23>', _Lpodat08, '</P_23>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_24>', _Lpodat09, '</P_24>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_25>', _Lpodat10, '</P_25>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_26>', _Lpodat11, '</P_26>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_27>', _Lpodat12, '</P_27>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_28>', _kwota07, '</P_28>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_29>', _kwota08, '</P_29>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_30>', _kwota09, '</P_30>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_31>', _kwota10, '</P_31>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_32>', _kwota11, '</P_32>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_33>', _kwota12, '</P_33>\n');													


					SET @kwota301=0, @kwota302=0, @kwota303=0, @kwota304=0, @kwota305=0, @kwota306=0,@kwota307=0, @kwota308=0, @kwota309=0, @kwota310=0, @kwota311=0, @kwota312=0;
					SELECT 	
					SUM(IF(msc=1, PodatekOdZlecen, 0)) AS P_109, SUM(IF(msc=2, PodatekOdZlecen, 0)),
					SUM(IF(msc=3, PodatekOdZlecen, 0)), SUM(IF(msc=4, PodatekOdZlecen, 0)),
					SUM(IF(msc=5, PodatekOdZlecen, 0)), SUM(IF(msc=6, PodatekOdZlecen, 0)),		
					SUM(IF(msc=7, PodatekOdZlecen, 0)), SUM(IF(msc=8, PodatekOdZlecen, 0)),
					SUM(IF(msc=9, PodatekOdZlecen, 0)), SUM(IF(msc=10, PodatekOdZlecen, 0)),
					SUM(IF(msc=11, PodatekOdZlecen, 0)), SUM(IF(msc=12, PodatekOdZlecen, 0)) AS P_120
					INTO
					 @kwota301, @kwota302, @kwota303, @kwota304, @kwota305, @kwota306,
					 @kwota307, @kwota308, @kwota309, @kwota310, @kwota311, @kwota312
				FROM 
						pit_4 WHERE rok = _Rok	
				;
														 					
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_46>', @kwota301, '</P_46>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_47>', @kwota302, '</P_47>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_48>', @kwota303, '</P_48>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_49>', @kwota304, '</P_49>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_50>', @kwota305, '</P_50>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_51>', @kwota306, '</P_51>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_52>', @kwota307, '</P_52>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_53>', @kwota308, '</P_53>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_54>', @kwota309, '</P_54>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_55>', @kwota310, '</P_55>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_56>', @kwota311, '</P_56>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_57>', @kwota312, '</P_57>\n');				
				
				SET @kwota401=0, @kwota402=0, @kwota403=0, @kwota404=0, @kwota405=0, @kwota406=0,@kwota407=0, @kwota408=0, @kwota409=0, @kwota410=0, @kwota411=0, @kwota412=0;
				SELECT 	
					SUM(IF(msc=1, Zalicz9, 0)) AS P_121, SUM(IF(msc=2, Zalicz9, 0)),
					SUM(IF(msc=3, Zalicz9, 0)), SUM(IF(msc=4, Zalicz9, 0)),
					SUM(IF(msc=5, Zalicz9, 0)), SUM(IF(msc=6, Zalicz9, 0)),		
					SUM(IF(msc=7, Zalicz9, 0)), SUM(IF(msc=8, Zalicz9, 0)),
					SUM(IF(msc=9, Zalicz9, 0)), SUM(IF(msc=10, Zalicz9, 0)),
					SUM(IF(msc=11, Zalicz9, 0)), SUM(IF(msc=12, Zalicz9, 0)) AS P_132
					INTO
					 @kwota401, @kwota402, @kwota403, @kwota404, @kwota405, @kwota406,
					 @kwota407, @kwota408, @kwota409, @kwota410, @kwota411, @kwota412
				FROM 
						pit_4 WHERE rok = _Rok	
				;
					
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_58>', @kwota401, '</P_58>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_59>', @kwota402, '</P_59>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_60>', @kwota403, '</P_60>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_61>', @kwota404, '</P_61>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_62>', @kwota405, '</P_62>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_63>', @kwota406, '</P_63>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_64>', @kwota407, '</P_64>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_65>', @kwota408, '</P_65>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_66>', @kwota409, '</P_66>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_67>', @kwota410, '</P_67>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_68>', @kwota411, '</P_68>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_69>', @kwota412, '</P_69>\n');
				
				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_70>', _kwota01+@kwota301+@kwota401, '</P_70>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_71>', _kwota02+@kwota302+@kwota402, '</P_71>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_72>', _kwota03+@kwota303+@kwota403, '</P_72>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_73>', _kwota04+@kwota304+@kwota404, '</P_73>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_74>', _kwota05+@kwota305+@kwota405, '</P_74>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_75>', _kwota06+@kwota306+@kwota406, '</P_75>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_76>', _kwota07+@kwota307+@kwota407, '</P_76>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_77>', _kwota08+@kwota308+@kwota408, '</P_77>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_78>', _kwota09+@kwota309+@kwota409, '</P_78>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_79>', _kwota10+@kwota310+@kwota410, '</P_79>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_80>', _kwota11+@kwota311+@kwota411, '</P_80>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_81>', _kwota12+@kwota312+@kwota412, '</P_81>\n');													
																
	
				SELECT 	
					SUM(IF(msc=1, Zalicz11, 0)) AS P_57, SUM(IF(msc=2, Zalicz11, 0)),
					SUM(IF(msc=3, Zalicz11, 0)), SUM(IF(msc=4, Zalicz11, 0)),
					SUM(IF(msc=5, Zalicz11, 0)), SUM(IF(msc=6, Zalicz11, 0)),		
					SUM(IF(msc=7, Zalicz11, 0)), SUM(IF(msc=8, Zalicz11, 0)),
					SUM(IF(msc=9, Zalicz11, 0)), SUM(IF(msc=10, Zalicz11, 0)),
					SUM(IF(msc=11, Zalicz11, 0)), SUM(IF(msc=12, Zalicz11, 0)) AS P_68
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
						pit_4 WHERE rok = _Rok	
				;
										 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_82>', _kwota01, '</P_82>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_83>', _kwota02, '</P_83>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_84>', _kwota03, '</P_84>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_85>', _kwota04, '</P_85>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_86>', _kwota05, '</P_86>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_87>', _kwota06, '</P_87>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_88>', _kwota07, '</P_88>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_89>', _kwota08, '</P_89>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_90>', _kwota09, '</P_90>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_91>', _kwota10, '</P_91>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_92>', _kwota11, '</P_92>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_93>', _kwota12, '</P_93>\n');									
				
				
				SELECT 	
					SUM(IF(msc=1, Zalicz12, 0)) AS P_69, SUM(IF(msc=2, Zalicz12, 0)),
					SUM(IF(msc=3, Zalicz12, 0)), SUM(IF(msc=4, Zalicz12, 0)),
					SUM(IF(msc=5, Zalicz12, 0)), SUM(IF(msc=6, Zalicz12, 0)),		
					SUM(IF(msc=7, Zalicz12, 0)), SUM(IF(msc=8, Zalicz12, 0)),
					SUM(IF(msc=9, Zalicz12, 0)), SUM(IF(msc=10, Zalicz12, 0)),
					SUM(IF(msc=11, Zalicz12, 0)), SUM(IF(msc=12, Zalicz12, 0)) AS P_80
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
						pit_4 WHERE rok = _Rok	
				;
										 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_94>', _kwota01, '</P_94>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_95>', _kwota02, '</P_95>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_96>', _kwota03, '</P_96>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_97>', _kwota04, '</P_97>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_98>', _kwota05, '</P_98>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_99>', _kwota06, '</P_99>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_100>', _kwota07, '</P_100>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_101>', _kwota08, '</P_101>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_102>', _kwota09, '</P_102>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_103>', _kwota10, '</P_103>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_104>', _kwota11, '</P_104>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_105>', _kwota12, '</P_105>\n');									
				
				
				SELECT 	
					SUM(IF(msc=1, zalicz13, 0)) , SUM(IF(msc=2, zalicz13, 0)),
					SUM(IF(msc=3, zalicz13, 0)), SUM(IF(msc=4, zalicz13, 0))
				INTO
					 _kwota01, _kwota02, _kwota03, _kwota04
				FROM 
						pit_4 WHERE rok = _Rok	
				;
														 					
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_106>', _kwota01, '</P_106>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_107>', _kwota02, '</P_107>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_108>', _kwota03, '</P_108>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_109>', _kwota04, '</P_109>\n');		
				
			/*	
				SELECT 	
					SUM(IF(msc=1, Zalicz14, 0)), SUM(IF(msc=2, Zalicz14, 0)),
					SUM(IF(msc=3, Zalicz14, 0)), SUM(IF(msc=4, Zalicz14, 0)),
					SUM(IF(msc=5, Zalicz14, 0)), SUM(IF(msc=6, Zalicz14, 0)),		
					SUM(IF(msc=7, Zalicz14, 0)), SUM(IF(msc=8, Zalicz14, 0)),
					SUM(IF(msc=9, Zalicz14, 0)), SUM(IF(msc=10, Zalicz14, 0)),
					SUM(IF(msc=11, Zalicz14, 0)), SUM(IF(msc=12, Zalicz14, 0))
				FROM 
						pit_4 WHERE rok = _Rok	
				INTO 
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,						 
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12;	
					 
					 
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_110>', _kwota01, '</P_110>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_111>', _kwota02, '</P_111>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_112>', _kwota03, '</P_112>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_113>', _kwota04, '</P_113>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_114>', _kwota05, '</P_114>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_115>', _kwota06, '</P_115>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_116>', _kwota07, '</P_116>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_117>', _kwota08, '</P_117>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_118>', _kwota09, '</P_118>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_119>', _kwota10, '</P_119>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_120>', _kwota11, '</P_120>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_121>', _kwota12, '</P_121>\n');	
			*/
				SELECT 	
					SUM(IF(msc=1, PodatekPFRON, 0)) AS P_97, SUM(IF(msc=2, PodatekPFRON, 0)),
					SUM(IF(msc=3, PodatekPFRON, 0)), SUM(IF(msc=4, PodatekPFRON, 0)),
					SUM(IF(msc=5, PodatekPFRON, 0)), SUM(IF(msc=6, PodatekPFRON, 0)),		
					SUM(IF(msc=7, PodatekPFRON, 0)), SUM(IF(msc=8, PodatekPFRON, 0)),
					SUM(IF(msc=9, PodatekPFRON, 0)), SUM(IF(msc=10, PodatekPFRON, 0)),
					SUM(IF(msc=11, PodatekPFRON, 0)), SUM(IF(msc=12, PodatekPFRON, 0)) AS P_108
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
						pit_4 WHERE rok = _Rok	
				;
														 					
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_110>', _kwota01, '</P_110>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_111>', _kwota02, '</P_111>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_112>', _kwota03, '</P_112>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_113>', _kwota04, '</P_113>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_114>', _kwota05, '</P_114>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_115>', _kwota06, '</P_115>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_116>', _kwota07, '</P_116>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_117>', _kwota08, '</P_117>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_118>', _kwota09, '</P_118>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_119>', _kwota10, '</P_119>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_120>', _kwota11, '</P_120>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_121>', _kwota12, '</P_121>\n');									
					
			
				

		
		
				
				
				SELECT
					SUM(IF(msc=1, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_133, 
					SUM(IF(msc=2, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_134, 
					SUM(IF(msc=3, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_135, 
					SUM(IF(msc=4, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_136, 
					
					SUM(IF(msc=5, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ), 
					SUM(IF(msc=6, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=7, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=8, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) ,
					SUM(IF(msc=9, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=10, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=11, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=12, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_144
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
					pit_4 WHERE rok = _Rok	
				;

				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_122>', _kwota01, '</P_122>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_123>', _kwota02, '</P_123>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_124>', _kwota03, '</P_124>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_125>', _kwota04, '</P_125>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_126>', _kwota05, '</P_126>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_127>', _kwota06, '</P_127>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_128>', _kwota07, '</P_128>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_129>', _kwota08, '</P_129>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_130>', _kwota09, '</P_130>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_131>', _kwota10, '</P_131>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_132>', _kwota11, '</P_132>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_133>', _kwota12, '</P_133>\n');		 
												
		
				SELECT 	
					SUM(IF(msc=1, WynagrodzPlatnika, 0)) AS P_145, SUM(IF(msc=2, WynagrodzPlatnika, 0)),
					SUM(IF(msc=3, WynagrodzPlatnika, 0)), SUM(IF(msc=4, WynagrodzPlatnika, 0)),
					SUM(IF(msc=5, WynagrodzPlatnika, 0)), SUM(IF(msc=6, WynagrodzPlatnika, 0)),		
					SUM(IF(msc=7, WynagrodzPlatnika, 0)), SUM(IF(msc=8, WynagrodzPlatnika, 0)),
					SUM(IF(msc=9, WynagrodzPlatnika, 0)), SUM(IF(msc=10, WynagrodzPlatnika, 0)),
					SUM(IF(msc=11, WynagrodzPlatnika, 0)), SUM(IF(msc=12, WynagrodzPlatnika, 0)) AS P_156
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
						pit_4 WHERE rok = _Rok	
				;
					 
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_134>', _kwota01, '</P_134>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_135>', _kwota02, '</P_135>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_136>', _kwota03, '</P_136>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_137>', _kwota04, '</P_137>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_138>', _kwota05, '</P_138>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_139>', _kwota06, '</P_139>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_140>', _kwota07, '</P_140>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_141>', _kwota08, '</P_141>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_142>', _kwota09, '</P_142>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_143>', _kwota10, '</P_143>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_144>', _kwota11, '</P_144>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_145>', _kwota12, '</P_145>\n');		 
				
				
		
				SELECT
					SUM(IF(msc=1, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) AS P_157, 
					SUM(IF(msc=2, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ), 
					SUM(IF(msc=3, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ), 
					SUM(IF(msc=4, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ), 
					
					SUM(IF(msc=5, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ), 
					SUM(IF(msc=6, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=7, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=8, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) ,
					SUM(IF(msc=9, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=10, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=11, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=12, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) AS P_168
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
					pit_4 WHERE rok = _Rok	
				;

				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_146>', _kwota01, '</P_146>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_147>', _kwota02, '</P_147>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_148>', _kwota03, '</P_148>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_149>', _kwota04, '</P_149>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_150>', _kwota05, '</P_150>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_151>', _kwota06, '</P_151>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_152>', _kwota07, '</P_152>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_153>', _kwota08, '</P_153>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_154>', _kwota09, '</P_154>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_155>', _kwota10, '</P_155>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_156>', _kwota11, '</P_156>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_157>', _kwota12, '</P_157>\n');	

				SET @ePIT.poz = @ePIT.xml;	
												
				
				SET SESSION group_concat_max_len = 9000;
				
				SELECT GROUP_CONCAT(IF(IFNULL(uzasadnienie, '') <>"", CONCAT(msc,": ", IFNULL(uzasadnienie, '')),"") ORDER BY msc SEPARATOR "\n" ) AS uzasadnienie
				INTO @pit4.uzasadnienie
				FROM pit_4 WHERE rok= _Rok ORDER BY msc
				;
				
				
				IF LENGTH(IFNULL(@pit4.uzasadnienie, ''))>0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_158>', @pit4.uzasadnienie, '</P_158>\n');	 
				END IF;
				
				
				
				SELECT IFNULL(valueB,0) INTO @covid_III_2020 FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_III_2020";
				SELECT IFNULL(valueB,0) INTO @covid_IV_2020 FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_IV_2020";
				SELECT IFNULL(valueB,0) INTO @covid_V_2020 FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_V_2020";
				SELECT IFNULL(valueB,0) INTO @covid_X_2020 FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_X_2020";
				SELECT IFNULL(valueB,0) INTO @covid_XI_2020 FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_XI_2020";
				SELECT IFNULL(valueB,0) INTO @covid_XII_2020 FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_XII_2020";
				
				IF @covid_III_2020>0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_159>', @covid_III_2020, '</P_159>\n');
				END IF;
					
				IF @covid_IV_2020>0 THEN								
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_160>', @covid_IV_2020, '</P_160>\n');				
				END IF;
									
				IF @covid_V_2020>0 THEN
				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_161>', @covid_V_2020, '</P_161>\n');
				END IF;
				
				IF @covid_X_2020>0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_162>', @covid_X_2020, '</P_162>\n');
				END IF;
				
				IF @covid_XI_2020>0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_163>', @covid_XI_2020, '</P_163>\n');
				END IF;
				
				IF @covid_XII_2020>0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_164>', @covid_XII_2020, '</P_164>\n');
				END IF;
				
				SET @ePIT.poz = @ePIT.xml;
			
				SET @xml.IC = @xml.IC - 1;	
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '</PozycjeSzczegolowe>\n');
			
		END;
		
			
		  
		SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Pouczenia>1</Pouczenia>\n');
		
		IF (@ePIT.CelZlozenia = cKorekta) AND (LENGTH(@_przyczyna_ORD_ZU) > 0) THEN   
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Zalaczniki>\n');									
			CALL pit_tools('pit.xml.insert.ORD_ZU', '', '', '', '', '', '4');						
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '</Zalaczniki>\n');				
		END IF;
																	   						
	
		SET @ePIT.xml = CONCAT(_to_utf_xml(@ePIT.xml), '</Deklaracja>');
		
		
		CALL e_doc_tools('e_doc_po.insert.or.edit', _Param_A, IFNULL(@ePIT.xml, ''), 'CREATE_XML', 'ORYG', '', '', '', '');
	END IF;



IF _MODE = 'pit4r.xml.create.201901' THEN
SELECT  
		d.ID, p.przyczyna_ORD_ZU, p.rok
		INTO _e_Doc_Def_ID, @_przyczyna_ORD_ZU, _Rok
	FROM 	
		pit_4r p, e_doc_def d 
	WHERE	
		p.GUID = _GUID AND  p.e_Doc_Def_ID = d.owner_doc_Id
	;

	SET @ePIT.xml = '';
	SET @xml.IC = 1;
		
	SET @ePIT.xml = '<?xml version="1.0" encoding="UTF-8" ?>\n';	
	SET @ePIT.xml = CONCAT(@ePIT.xml, '<Deklaracja xmlns="', _e_Doc_Def_ID, '" ');
	SET @ePIT.xml = CONCAT(@ePIT.xml, 'xmlns:etd="http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2018/08/24/eD/DefinicjeTypy/">', '\n');
	SET @ePIT.xmlns = @ePIT.xml; 	
		
	
	CALL pit_tools('pit.xml.insert.header', _GUID, @ePIT.xml, 'pit_4r', 'P', '', '');
	
	
		SET @ePIT.header = @ePIT.xml;
		
		CALL pit_tools('pit.xml.insert.podmiot1', '', '', 'Płatnik', '', '', '');  -- OK
		
		SET @ePIT.P1 = @ePIT.xml;
		
		-- <PozycjeSzczegolowe> 
		BEGIN
			DECLARE 
				_Lpodat01, _Lpodat02, _Lpodat03, _Lpodat04, _Lpodat05, _Lpodat06,
				_Lpodat07, _Lpodat08, _Lpodat09, _Lpodat10, _Lpodat11, _Lpodat12  INTEGER;
			        
			
		
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<PozycjeSzczegolowe>\n');
				SET @xml.IC = @xml.IC + 1;
				
			SELECT IFNULL(RodzajKorekty,0) INTO @RodzajKorekty FROM pit_4r WHERE GUID = _GUID;
				
				IF @RodzajKorekty > 0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_7>', _RodzajKorekty, '</P_7>\n');
				END IF;
			
								
				SELECT 		
						-- wartosci do C.1 (i C.3)
						SUM(IF(msc=1, Lpodat1, 0)) AS P_9, SUM(IF(msc=2, Lpodat1, 0)) AS P_10,
						SUM(IF(msc=3, Lpodat1, 0)) AS P_11, SUM(IF(msc=4, Lpodat1, 0)) AS P_12,
						SUM(IF(msc=5, Lpodat1, 0)) AS P_13, SUM(IF(msc=6, Lpodat1, 0)) AS P_14,						
						_varINT(SUM(IF(msc=1, Zalicz1+Zalicz2, 0))) AS P_15, _varINT(SUM(IF(msc=2, Zalicz1+Zalicz2, 0))) AS P_16,
						_varINT(SUM(IF(msc=3, Zalicz1+Zalicz2, 0))) AS P_17, _varINT(SUM(IF(msc=4, Zalicz1+Zalicz2, 0))) AS P_18,
						_varINT(SUM(IF(msc=5, Zalicz1+Zalicz2, 0))) AS P_19, _varINT(SUM(IF(msc=6, Zalicz1+Zalicz2, 0))) AS P_20,					
						SUM(IF(msc=7, Lpodat1, 0)) AS P_21, SUM(IF(msc=8, Lpodat1, 0)) AS P_22,
						SUM(IF(msc=9, Lpodat1, 0)) AS P_23, SUM(IF(msc=10, Lpodat1, 0)) AS P_24,
						SUM(IF(msc=11, Lpodat1, 0)) AS P_25, SUM(IF(msc=12, Lpodat1, 0)) AS P_26,
						SUM(IF(msc=7, Zalicz1+Zalicz2, 0))  AS P_27, SUM(IF(msc=8, Zalicz1+Zalicz2, 0)) AS P_28,
						SUM(IF(msc=9, Zalicz1+Zalicz2, 0))  AS P_29, SUM(IF(msc=10, Zalicz1+Zalicz2, 0)) AS P_30,
						SUM(IF(msc=11, Zalicz1+Zalicz2, 0)) AS P_31, SUM(IF(msc=12, Zalicz1+Zalicz2, 0)) AS P_32
						INTO _Lpodat01, _Lpodat02, _Lpodat03, _Lpodat04, _Lpodat05, _Lpodat06,
						 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _Lpodat07, _Lpodat08, _Lpodat09, _Lpodat10, _Lpodat11, _Lpodat12,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					/*	-- C.4
						
						-- C.5
						  */
					FROM 
						pit_4 WHERE rok = _Rok											
					;
						 
					-- wstawiamy C.1 (p9...p32)
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL),  '<P_10>', _Lpodat01, '</P_10>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_11>', _Lpodat02, '</P_11>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_12>', _Lpodat03, '</P_12>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_13>', _Lpodat04, '</P_13>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_14>', _Lpodat05, '</P_14>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_15>', _Lpodat06, '</P_15>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_16>', _kwota01, '</P_16>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_17>', _kwota02, '</P_17>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_18>', _kwota03, '</P_18>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_19>', _kwota04, '</P_19>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_20>', _kwota05, '</P_20>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_21>', _kwota06, '</P_21>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_22>', _Lpodat07, '</P_22>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_23>', _Lpodat08, '</P_23>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_24>', _Lpodat09, '</P_24>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_25>', _Lpodat10, '</P_25>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_26>', _Lpodat11, '</P_26>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_27>', _Lpodat12, '</P_27>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_28>', _kwota07, '</P_28>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_29>', _kwota08, '</P_29>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_30>', _kwota09, '</P_30>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_31>', _kwota10, '</P_31>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_32>', _kwota11, '</P_32>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_33>', _kwota12, '</P_33>\n');													


					SET @kwota301=0, @kwota302=0, @kwota303=0, @kwota304=0, @kwota305=0, @kwota306=0,@kwota307=0, @kwota308=0, @kwota309=0, @kwota310=0, @kwota311=0, @kwota312=0;
					SELECT 	
					SUM(IF(msc=1, PodatekOdZlecen, 0)) AS P_109, SUM(IF(msc=2, PodatekOdZlecen, 0)),
					SUM(IF(msc=3, PodatekOdZlecen, 0)), SUM(IF(msc=4, PodatekOdZlecen, 0)),
					SUM(IF(msc=5, PodatekOdZlecen, 0)), SUM(IF(msc=6, PodatekOdZlecen, 0)),		
					SUM(IF(msc=7, PodatekOdZlecen, 0)), SUM(IF(msc=8, PodatekOdZlecen, 0)),
					SUM(IF(msc=9, PodatekOdZlecen, 0)), SUM(IF(msc=10, PodatekOdZlecen, 0)),
					SUM(IF(msc=11, PodatekOdZlecen, 0)), SUM(IF(msc=12, PodatekOdZlecen, 0)) AS P_120
					INTO
					 @kwota301, @kwota302, @kwota303, @kwota304, @kwota305, @kwota306,
					 @kwota307, @kwota308, @kwota309, @kwota310, @kwota311, @kwota312
				FROM 
						pit_4 WHERE rok = _Rok	
				;
														 					
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_46>', @kwota301, '</P_46>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_47>', @kwota302, '</P_47>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_48>', @kwota303, '</P_48>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_49>', @kwota304, '</P_49>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_50>', @kwota305, '</P_50>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_51>', @kwota306, '</P_51>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_52>', @kwota307, '</P_52>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_53>', @kwota308, '</P_53>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_54>', @kwota309, '</P_54>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_55>', @kwota310, '</P_55>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_56>', @kwota311, '</P_56>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_57>', @kwota312, '</P_57>\n');				
				
				SET @kwota401=0, @kwota402=0, @kwota403=0, @kwota404=0, @kwota405=0, @kwota406=0,@kwota407=0, @kwota408=0, @kwota409=0, @kwota410=0, @kwota411=0, @kwota412=0;
				SELECT 	
					SUM(IF(msc=1, Zalicz9, 0)) AS P_121, SUM(IF(msc=2, Zalicz9, 0)),
					SUM(IF(msc=3, Zalicz9, 0)), SUM(IF(msc=4, Zalicz9, 0)),
					SUM(IF(msc=5, Zalicz9, 0)), SUM(IF(msc=6, Zalicz9, 0)),		
					SUM(IF(msc=7, Zalicz9, 0)), SUM(IF(msc=8, Zalicz9, 0)),
					SUM(IF(msc=9, Zalicz9, 0)), SUM(IF(msc=10, Zalicz9, 0)),
					SUM(IF(msc=11, Zalicz9, 0)), SUM(IF(msc=12, Zalicz9, 0)) AS P_132
					INTO
					 @kwota401, @kwota402, @kwota403, @kwota404, @kwota405, @kwota406,
					 @kwota407, @kwota408, @kwota409, @kwota410, @kwota411, @kwota412
				FROM 
						pit_4 WHERE rok = _Rok	
				;
					
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_58>', @kwota401, '</P_58>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_59>', @kwota402, '</P_59>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_60>', @kwota403, '</P_60>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_61>', @kwota404, '</P_61>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_62>', @kwota405, '</P_62>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_63>', @kwota406, '</P_63>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_64>', @kwota407, '</P_64>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_65>', @kwota408, '</P_65>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_66>', @kwota409, '</P_66>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_67>', @kwota410, '</P_67>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_68>', @kwota411, '</P_68>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_69>', @kwota412, '</P_69>\n');
				
				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_70>', _kwota01+@kwota301+@kwota401, '</P_70>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_71>', _kwota02+@kwota302+@kwota402, '</P_71>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_72>', _kwota03+@kwota303+@kwota403, '</P_72>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_73>', _kwota04+@kwota304+@kwota404, '</P_73>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_74>', _kwota05+@kwota305+@kwota405, '</P_74>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_75>', _kwota06+@kwota306+@kwota406, '</P_75>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_76>', _kwota07+@kwota307+@kwota407, '</P_76>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_77>', _kwota08+@kwota308+@kwota408, '</P_77>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_78>', _kwota09+@kwota309+@kwota409, '</P_78>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_79>', _kwota10+@kwota310+@kwota410, '</P_79>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_80>', _kwota11+@kwota311+@kwota411, '</P_80>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_81>', _kwota12+@kwota312+@kwota412, '</P_81>\n');													
																
	
				SELECT 	
					SUM(IF(msc=1, Zalicz11, 0)) AS P_57, SUM(IF(msc=2, Zalicz11, 0)),
					SUM(IF(msc=3, Zalicz11, 0)), SUM(IF(msc=4, Zalicz11, 0)),
					SUM(IF(msc=5, Zalicz11, 0)), SUM(IF(msc=6, Zalicz11, 0)),		
					SUM(IF(msc=7, Zalicz11, 0)), SUM(IF(msc=8, Zalicz11, 0)),
					SUM(IF(msc=9, Zalicz11, 0)), SUM(IF(msc=10, Zalicz11, 0)),
					SUM(IF(msc=11, Zalicz11, 0)), SUM(IF(msc=12, Zalicz11, 0)) AS P_68
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
						pit_4 WHERE rok = _Rok	
				;
										 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_82>', _kwota01, '</P_82>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_83>', _kwota02, '</P_83>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_84>', _kwota03, '</P_84>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_85>', _kwota04, '</P_85>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_86>', _kwota05, '</P_86>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_87>', _kwota06, '</P_87>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_88>', _kwota07, '</P_88>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_89>', _kwota08, '</P_89>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_90>', _kwota09, '</P_90>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_91>', _kwota10, '</P_91>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_92>', _kwota11, '</P_92>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_93>', _kwota12, '</P_93>\n');									
				
				
				SELECT 	
					SUM(IF(msc=1, Zalicz12, 0)) AS P_69, SUM(IF(msc=2, Zalicz12, 0)),
					SUM(IF(msc=3, Zalicz12, 0)), SUM(IF(msc=4, Zalicz12, 0)),
					SUM(IF(msc=5, Zalicz12, 0)), SUM(IF(msc=6, Zalicz12, 0)),		
					SUM(IF(msc=7, Zalicz12, 0)), SUM(IF(msc=8, Zalicz12, 0)),
					SUM(IF(msc=9, Zalicz12, 0)), SUM(IF(msc=10, Zalicz12, 0)),
					SUM(IF(msc=11, Zalicz12, 0)), SUM(IF(msc=12, Zalicz12, 0)) AS P_80
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
						pit_4 WHERE rok = _Rok	
				;
										 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_94>', _kwota01, '</P_94>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_95>', _kwota02, '</P_95>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_96>', _kwota03, '</P_96>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_97>', _kwota04, '</P_97>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_98>', _kwota05, '</P_98>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_99>', _kwota06, '</P_99>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_100>', _kwota07, '</P_100>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_101>', _kwota08, '</P_101>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_102>', _kwota09, '</P_102>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_103>', _kwota10, '</P_103>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_104>', _kwota11, '</P_104>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_105>', _kwota12, '</P_105>\n');									
				-- TU  8 i 9 wyliczenia	
				
				SELECT 	
					SUM(IF(msc=1, zalicz13, 0)) , SUM(IF(msc=2, zalicz13, 0)),
					SUM(IF(msc=3, zalicz13, 0)), SUM(IF(msc=4, zalicz13, 0))
				INTO
					 _kwota01, _kwota02, _kwota03, _kwota04
				FROM 
						pit_4 WHERE rok = _Rok	
				;
														 					
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_106>', _kwota01, '</P_106>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_107>', _kwota02, '</P_107>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_108>', _kwota03, '</P_108>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_109>', _kwota04, '</P_109>\n');		
				
				
				SELECT 	
					SUM(IF(msc=1, Zalicz14, 0)), SUM(IF(msc=2, Zalicz14, 0)),
					SUM(IF(msc=3, Zalicz14, 0)), SUM(IF(msc=4, Zalicz14, 0)),
					SUM(IF(msc=5, Zalicz14, 0)), SUM(IF(msc=6, Zalicz14, 0)),		
					SUM(IF(msc=7, Zalicz14, 0)), SUM(IF(msc=8, Zalicz14, 0)),
					SUM(IF(msc=9, Zalicz14, 0)), SUM(IF(msc=10, Zalicz14, 0)),
					SUM(IF(msc=11, Zalicz14, 0)), SUM(IF(msc=12, Zalicz14, 0))
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
						pit_4 WHERE rok = _Rok	
				;
					 
					 
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_110>', _kwota01, '</P_110>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_111>', _kwota02, '</P_111>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_112>', _kwota03, '</P_112>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_113>', _kwota04, '</P_113>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_114>', _kwota05, '</P_114>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_115>', _kwota06, '</P_115>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_116>', _kwota07, '</P_116>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_117>', _kwota08, '</P_117>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_118>', _kwota09, '</P_118>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_119>', _kwota10, '</P_119>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_120>', _kwota11, '</P_120>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_121>', _kwota12, '</P_121>\n');	
			
				SELECT 	
					SUM(IF(msc=1, PodatekPFRON, 0)) AS P_97, SUM(IF(msc=2, PodatekPFRON, 0)),
					SUM(IF(msc=3, PodatekPFRON, 0)), SUM(IF(msc=4, PodatekPFRON, 0)),
					SUM(IF(msc=5, PodatekPFRON, 0)), SUM(IF(msc=6, PodatekPFRON, 0)),		
					SUM(IF(msc=7, PodatekPFRON, 0)), SUM(IF(msc=8, PodatekPFRON, 0)),
					SUM(IF(msc=9, PodatekPFRON, 0)), SUM(IF(msc=10, PodatekPFRON, 0)),
					SUM(IF(msc=11, PodatekPFRON, 0)), SUM(IF(msc=12, PodatekPFRON, 0)) AS P_108
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
						pit_4 WHERE rok = _Rok	
				;
														 					
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_122>', _kwota01, '</P_122>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_123>', _kwota02, '</P_123>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_124>', _kwota03, '</P_124>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_125>', _kwota04, '</P_125>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_126>', _kwota05, '</P_126>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_127>', _kwota06, '</P_127>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_128>', _kwota07, '</P_128>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_129>', _kwota08, '</P_129>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_130>', _kwota09, '</P_130>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_131>', _kwota10, '</P_131>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_132>', _kwota11, '</P_132>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_133>', _kwota12, '</P_133>\n');									
					
			--
				

		
		
				
				
				SELECT
					SUM(IF(msc=1, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_133, 
					SUM(IF(msc=2, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_134, 
					SUM(IF(msc=3, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_135, 
					SUM(IF(msc=4, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_136, 
					--
					SUM(IF(msc=5, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ), 
					SUM(IF(msc=6, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=7, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=8, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) ,
					SUM(IF(msc=9, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=10, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=11, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=12, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_144
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
					pit_4 WHERE rok = _Rok	
				;

				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_134>', _kwota01, '</P_134>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_135>', _kwota02, '</P_135>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_136>', _kwota03, '</P_136>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_137>', _kwota04, '</P_137>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_138>', _kwota05, '</P_138>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_139>', _kwota06, '</P_139>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_140>', _kwota07, '</P_140>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_141>', _kwota08, '</P_141>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_142>', _kwota09, '</P_142>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_143>', _kwota10, '</P_143>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_144>', _kwota11, '</P_144>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_145>', _kwota12, '</P_145>\n');		 
												
		
				SELECT 	
					SUM(IF(msc=1, WynagrodzPlatnika, 0)) AS P_145, SUM(IF(msc=2, WynagrodzPlatnika, 0)),
					SUM(IF(msc=3, WynagrodzPlatnika, 0)), SUM(IF(msc=4, WynagrodzPlatnika, 0)),
					SUM(IF(msc=5, WynagrodzPlatnika, 0)), SUM(IF(msc=6, WynagrodzPlatnika, 0)),		
					SUM(IF(msc=7, WynagrodzPlatnika, 0)), SUM(IF(msc=8, WynagrodzPlatnika, 0)),
					SUM(IF(msc=9, WynagrodzPlatnika, 0)), SUM(IF(msc=10, WynagrodzPlatnika, 0)),
					SUM(IF(msc=11, WynagrodzPlatnika, 0)), SUM(IF(msc=12, WynagrodzPlatnika, 0)) AS P_156
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
						pit_4 WHERE rok = _Rok	
				;
					 
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_146>', _kwota01, '</P_146>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_147>', _kwota02, '</P_147>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_148>', _kwota03, '</P_148>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_149>', _kwota04, '</P_149>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_150>', _kwota05, '</P_150>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_151>', _kwota06, '</P_151>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_152>', _kwota07, '</P_152>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_153>', _kwota08, '</P_153>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_154>', _kwota09, '</P_154>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_155>', _kwota10, '</P_155>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_156>', _kwota11, '</P_156>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_157>', _kwota12, '</P_157>\n');		 
				
				
		
				SELECT
					SUM(IF(msc=1, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) AS P_157, 
					SUM(IF(msc=2, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ), 
					SUM(IF(msc=3, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ), 
					SUM(IF(msc=4, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ), 
					--
					SUM(IF(msc=5, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ), 
					SUM(IF(msc=6, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=7, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=8, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) ,
					SUM(IF(msc=9, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=10, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=11, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=12, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) AS P_168
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
					pit_4 WHERE rok = _Rok	
				;

				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_158>', _kwota01, '</P_158>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_159>', _kwota02, '</P_159>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_160>', _kwota03, '</P_160>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_161>', _kwota04, '</P_161>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_162>', _kwota05, '</P_162>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_163>', _kwota06, '</P_163>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_164>', _kwota07, '</P_164>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_165>', _kwota08, '</P_165>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_166>', _kwota09, '</P_166>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_167>', _kwota10, '</P_167>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_168>', _kwota11, '</P_168>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_169>', _kwota12, '</P_169>\n');	

				SET @ePIT.poz = @ePIT.xml;	
												
				/*	<xsd:element name="P_169" type="etd:TTekstowy" minOccurs="0">  -- Typ znakowy ograniczony do 3500 znaków
					<xsd:annotation>
						<xsd:documentation>Wyjałnienie różnicy pomiędzy kwotą pobranego podatku a kwotą wpłaconego podatku</xsd:documentation>
					</xsd:annotation>
				    </xsd:element>		  
				select distinct left(Uzasadnienie, 1, 250) where ifnull(Uzasadnienie, '') <>''	
				*/
				SET SESSION group_concat_max_len = 9000;
				
				SELECT GROUP_CONCAT(IF(IFNULL(uzasadnienie, '') <>"", CONCAT(msc,": ", IFNULL(uzasadnienie, '')),"") ORDER BY msc SEPARATOR "\n" ) AS uzasadnienie
				INTO @pit4.uzasadnienie
				FROM pit_4 WHERE rok= _Rok ORDER BY msc
				;
				-- SELECT GROUP_CONCAT(DISTINCT uzasadnienie SEPARATOR "\n") FROM pit_4 WHERE rok=2014 ORDER BY msc ;
				
				IF LENGTH(IFNULL(@pit4.uzasadnienie, ''))>0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_170>', @pit4.uzasadnienie, '</P_170>\n');	 
				END IF;
			
				SET @xml.IC = @xml.IC - 1;	
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '</PozycjeSzczegolowe>\n');
			-- koniec sekcji pozycji szczegolowych	
		END;
		
			
		/* Pouczenie1 i Pouczenie2 (UTF8) OK */  
		SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Pouczenia>1</Pouczenia>\n');
		-- ewentualne zalaczniki
		IF (@ePIT.CelZlozenia = cKorekta) AND (LENGTH(@_przyczyna_ORD_ZU) > 0) THEN   
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Zalaczniki>\n');									
			CALL pit_tools('pit.xml.insert.ORD_ZU', '', '', '', '', '', '3');						
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '</Zalaczniki>\n');				
		END IF;
																	   						
	--	SET @ePIT.xml = CONCAT(@ePIT.xml, '</Deklaracja>');
		SET @ePIT.xml = CONCAT(_to_utf_xml(@ePIT.xml), '</Deklaracja>');
		-- wstawiamy wygenerowany XML do e_doc_po
		-- SET _IDN = _Param_A;
		CALL e_doc_tools('e_doc_po.insert.or.edit', _Param_A, IFNULL(@ePIT.xml, ''), 'CREATE_XML', 'ORYG', '', '', '', '');
	END IF;

/*------------------------------------------------------------------------------------------------------------------------------------------------------------------	
	'pit4r.xml.create.201812'
		wygenerowanie XMLa na podstawie rekordu PIT-4R 
		Parametry - do zrewidowania
		_MODE		  Tryb pracy
		IN _GUID      GUID  rekordu		
		IN _Param_A   Rok, za ktory jest skladana deklaracja
				
		CALL pit_tools('pit4r.xml.create.201812', '', '', '', '', '', '');
------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
IF _MODE = 'pit4r.xml.create.201812' THEN
SELECT  
		d.ID, p.przyczyna_ORD_ZU, p.rok
		INTO _e_Doc_Def_ID, @_przyczyna_ORD_ZU, _Rok
	FROM 	
		pit_4r p, e_doc_def d 
	WHERE	
		p.GUID = _GUID AND  p.e_Doc_Def_ID = d.owner_doc_Id
	;

	SET @ePIT.xml = '';
	SET @xml.IC = 1;
		
	SET @ePIT.xml = '<?xml version="1.0" encoding="UTF-8" ?>\n';	
	SET @ePIT.xml = CONCAT(@ePIT.xml, '<Deklaracja xmlns="', _e_Doc_Def_ID, '" ');
	SET @ePIT.xml = CONCAT(@ePIT.xml, 'xmlns:etd="http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2018/08/24/eD/DefinicjeTypy/">', '\n');
	SET @ePIT.xmlns = @ePIT.xml; 	
		

	
	CALL pit_tools('pit.xml.insert.header', _GUID, @ePIT.xml, 'pit_4r', 'P', '', '');
	
	
		SET @ePIT.header = @ePIT.xml;
		
		CALL pit_tools('pit.xml.insert.podmiot1', '', '', 'Płatnik', '', '', '1');  -- OK
		
		SET @ePIT.P1 = @ePIT.xml;
		
		-- <PozycjeSzczegolowe> 
		BEGIN
			DECLARE 
				_Lpodat01, _Lpodat02, _Lpodat03, _Lpodat04, _Lpodat05, _Lpodat06,
				_Lpodat07, _Lpodat08, _Lpodat09, _Lpodat10, _Lpodat11, _Lpodat12  INTEGER;
			        
			
		
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<PozycjeSzczegolowe>\n');
				SET @xml.IC = @xml.IC + 1;
				
			
								
				SELECT 		
						-- wartosci do C.1 (i C.3)
						SUM(IF(msc=1, Lpodat1, 0)) AS P_9, SUM(IF(msc=2, Lpodat1, 0)) AS P_10,
						SUM(IF(msc=3, Lpodat1, 0)) AS P_11, SUM(IF(msc=4, Lpodat1, 0)) AS P_12,
						SUM(IF(msc=5, Lpodat1, 0)) AS P_13, SUM(IF(msc=6, Lpodat1, 0)) AS P_14,						
						_varINT(SUM(IF(msc=1, Zalicz1+Zalicz2, 0))) AS P_15, _varINT(SUM(IF(msc=2, Zalicz1+Zalicz2, 0))) AS P_16,
						_varINT(SUM(IF(msc=3, Zalicz1+Zalicz2, 0))) AS P_17, _varINT(SUM(IF(msc=4, Zalicz1+Zalicz2, 0))) AS P_18,
						_varINT(SUM(IF(msc=5, Zalicz1+Zalicz2, 0))) AS P_19, _varINT(SUM(IF(msc=6, Zalicz1+Zalicz2, 0))) AS P_20,					
						SUM(IF(msc=7, Lpodat1, 0)) AS P_21, SUM(IF(msc=8, Lpodat1, 0)) AS P_22,
						SUM(IF(msc=9, Lpodat1, 0)) AS P_23, SUM(IF(msc=10, Lpodat1, 0)) AS P_24,
						SUM(IF(msc=11, Lpodat1, 0)) AS P_25, SUM(IF(msc=12, Lpodat1, 0)) AS P_26,
						SUM(IF(msc=7, Zalicz1+Zalicz2, 0))  AS P_27, SUM(IF(msc=8, Zalicz1+Zalicz2, 0)) AS P_28,
						SUM(IF(msc=9, Zalicz1+Zalicz2, 0))  AS P_29, SUM(IF(msc=10, Zalicz1+Zalicz2, 0)) AS P_30,
						SUM(IF(msc=11, Zalicz1+Zalicz2, 0)) AS P_31, SUM(IF(msc=12, Zalicz1+Zalicz2, 0)) AS P_32
						INTO _Lpodat01, _Lpodat02, _Lpodat03, _Lpodat04, _Lpodat05, _Lpodat06,
						 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _Lpodat07, _Lpodat08, _Lpodat09, _Lpodat10, _Lpodat11, _Lpodat12,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					/*	-- C.4
						
						-- C.5
						  */
					FROM 
						pit_4 WHERE rok = _Rok											
					;
						 
					-- wstawiamy C.1 (p9...p32)
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL),  '<P_9>', _Lpodat01, '</P_9>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_10>', _Lpodat02, '</P_10>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_11>', _Lpodat03, '</P_11>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_12>', _Lpodat04, '</P_12>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_13>', _Lpodat05, '</P_13>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_14>', _Lpodat06, '</P_14>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_15>', _kwota01, '</P_15>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_16>', _kwota02, '</P_16>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_17>', _kwota03, '</P_17>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_18>', _kwota04, '</P_18>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_19>', _kwota05, '</P_19>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_20>', _kwota06, '</P_20>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_21>', _Lpodat07, '</P_21>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_22>', _Lpodat08, '</P_22>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_23>', _Lpodat09, '</P_23>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_24>', _Lpodat10, '</P_24>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_25>', _Lpodat11, '</P_25>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_26>', _Lpodat12, '</P_26>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_27>', _kwota07, '</P_27>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_28>', _kwota08, '</P_28>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_29>', _kwota09, '</P_29>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_30>', _kwota10, '</P_30>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_31>', _kwota11, '</P_31>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_32>', _kwota12, '</P_32>\n');													


					SET @kwota301=0, @kwota302=0, @kwota303=0, @kwota304=0, @kwota305=0, @kwota306=0,@kwota307=0, @kwota308=0, @kwota309=0, @kwota310=0, @kwota311=0, @kwota312=0;
					SELECT 	
					SUM(IF(msc=1, PodatekOdZlecen, 0)) AS P_109, SUM(IF(msc=2, PodatekOdZlecen, 0)),
					SUM(IF(msc=3, PodatekOdZlecen, 0)), SUM(IF(msc=4, PodatekOdZlecen, 0)),
					SUM(IF(msc=5, PodatekOdZlecen, 0)), SUM(IF(msc=6, PodatekOdZlecen, 0)),		
					SUM(IF(msc=7, PodatekOdZlecen, 0)), SUM(IF(msc=8, PodatekOdZlecen, 0)),
					SUM(IF(msc=9, PodatekOdZlecen, 0)), SUM(IF(msc=10, PodatekOdZlecen, 0)),
					SUM(IF(msc=11, PodatekOdZlecen, 0)), SUM(IF(msc=12, PodatekOdZlecen, 0)) AS P_120
					INTO
					 @kwota301, @kwota302, @kwota303, @kwota304, @kwota305, @kwota306,
					 @kwota307, @kwota308, @kwota309, @kwota310, @kwota311, @kwota312
				FROM 
						pit_4 WHERE rok = _Rok	
				;
														 					
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_45>', @kwota301, '</P_45>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_46>', @kwota302, '</P_46>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_47>', @kwota303, '</P_47>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_48>', @kwota304, '</P_48>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_49>', @kwota305, '</P_49>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_50>', @kwota306, '</P_50>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_51>', @kwota307, '</P_51>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_52>', @kwota308, '</P_52>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_53>', @kwota309, '</P_53>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_54>', @kwota310, '</P_54>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_55>', @kwota311, '</P_55>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_56>', @kwota312, '</P_56>\n');				
				
				SET @kwota401=0, @kwota402=0, @kwota403=0, @kwota404=0, @kwota405=0, @kwota406=0,@kwota407=0, @kwota408=0, @kwota409=0, @kwota410=0, @kwota411=0, @kwota412=0;
				SELECT 	
					SUM(IF(msc=1, Zalicz9, 0)) AS P_121, SUM(IF(msc=2, Zalicz9, 0)),
					SUM(IF(msc=3, Zalicz9, 0)), SUM(IF(msc=4, Zalicz9, 0)),
					SUM(IF(msc=5, Zalicz9, 0)), SUM(IF(msc=6, Zalicz9, 0)),		
					SUM(IF(msc=7, Zalicz9, 0)), SUM(IF(msc=8, Zalicz9, 0)),
					SUM(IF(msc=9, Zalicz9, 0)), SUM(IF(msc=10, Zalicz9, 0)),
					SUM(IF(msc=11, Zalicz9, 0)), SUM(IF(msc=12, Zalicz9, 0)) AS P_132
					INTO
					 @kwota401, @kwota402, @kwota403, @kwota404, @kwota405, @kwota406,
					 @kwota407, @kwota408, @kwota409, @kwota410, @kwota411, @kwota412
				FROM 
						pit_4 WHERE rok = _Rok	
				;
					
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_57>', @kwota401, '</P_57>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_58>', @kwota402, '</P_58>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_59>', @kwota403, '</P_59>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_60>', @kwota404, '</P_60>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_61>', @kwota405, '</P_61>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_62>', @kwota406, '</P_62>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_63>', @kwota407, '</P_63>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_64>', @kwota408, '</P_64>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_65>', @kwota409, '</P_65>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_66>', @kwota410, '</P_66>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_67>', @kwota411, '</P_67>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_68>', @kwota412, '</P_68>\n');
				
				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_69>', _kwota01+@kwota301+@kwota401, '</P_69>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_70>', _kwota02+@kwota302+@kwota402, '</P_70>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_71>', _kwota03+@kwota303+@kwota403, '</P_71>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_72>', _kwota04+@kwota304+@kwota404, '</P_72>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_73>', _kwota05+@kwota305+@kwota405, '</P_73>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_74>', _kwota06+@kwota306+@kwota406, '</P_74>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_75>', _kwota07+@kwota307+@kwota407, '</P_75>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_76>', _kwota08+@kwota308+@kwota408, '</P_76>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_77>', _kwota09+@kwota309+@kwota409, '</P_77>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_78>', _kwota10+@kwota310+@kwota410, '</P_78>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_79>', _kwota11+@kwota311+@kwota411, '</P_79>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_80>', _kwota12+@kwota312+@kwota412, '</P_80>\n');													
																
	
				SELECT 	
					SUM(IF(msc=1, Zalicz11, 0)) AS P_57, SUM(IF(msc=2, Zalicz11, 0)),
					SUM(IF(msc=3, Zalicz11, 0)), SUM(IF(msc=4, Zalicz11, 0)),
					SUM(IF(msc=5, Zalicz11, 0)), SUM(IF(msc=6, Zalicz11, 0)),		
					SUM(IF(msc=7, Zalicz11, 0)), SUM(IF(msc=8, Zalicz11, 0)),
					SUM(IF(msc=9, Zalicz11, 0)), SUM(IF(msc=10, Zalicz11, 0)),
					SUM(IF(msc=11, Zalicz11, 0)), SUM(IF(msc=12, Zalicz11, 0)) AS P_68
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
						pit_4 WHERE rok = _Rok	
				;
										 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_81>', _kwota01, '</P_81>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_82>', _kwota02, '</P_82>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_83>', _kwota03, '</P_83>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_84>', _kwota04, '</P_84>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_85>', _kwota05, '</P_85>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_86>', _kwota06, '</P_86>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_87>', _kwota07, '</P_87>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_88>', _kwota08, '</P_88>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_89>', _kwota09, '</P_89>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_90>', _kwota10, '</P_90>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_91>', _kwota11, '</P_91>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_92>', _kwota12, '</P_92>\n');									
				
				
				SELECT 	
					SUM(IF(msc=1, Zalicz12, 0)) AS P_69, SUM(IF(msc=2, Zalicz12, 0)),
					SUM(IF(msc=3, Zalicz12, 0)), SUM(IF(msc=4, Zalicz12, 0)),
					SUM(IF(msc=5, Zalicz12, 0)), SUM(IF(msc=6, Zalicz12, 0)),		
					SUM(IF(msc=7, Zalicz12, 0)), SUM(IF(msc=8, Zalicz12, 0)),
					SUM(IF(msc=9, Zalicz12, 0)), SUM(IF(msc=10, Zalicz12, 0)),
					SUM(IF(msc=11, Zalicz12, 0)), SUM(IF(msc=12, Zalicz12, 0)) AS P_80
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
						pit_4 WHERE rok = _Rok	
				;
										 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_93>', _kwota01, '</P_93>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_94>', _kwota02, '</P_94>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_95>', _kwota03, '</P_95>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_96>', _kwota04, '</P_96>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_97>', _kwota05, '</P_97>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_98>', _kwota06, '</P_98>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_99>', _kwota07, '</P_99>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_100>', _kwota08, '</P_100>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_101>', _kwota09, '</P_101>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_102>', _kwota10, '</P_102>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_103>', _kwota11, '</P_103>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_104>', _kwota12, '</P_104>\n');									
				-- TU  8 i 9 wyliczenia	
				
				SELECT 	
					SUM(IF(msc=1, zalicz13, 0)) , SUM(IF(msc=2, zalicz13, 0)),
					SUM(IF(msc=3, zalicz13, 0)), SUM(IF(msc=4, zalicz13, 0))
				INTO
					 _kwota01, _kwota02, _kwota03, _kwota04
				FROM 
						pit_4 WHERE rok = _Rok	
				;
														 					
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_105>', _kwota01, '</P_105>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_106>', _kwota02, '</P_106>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_107>', _kwota03, '</P_107>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_108>', _kwota04, '</P_108>\n');		
				
				
				SELECT 	
					SUM(IF(msc=1, Zalicz14, 0)), SUM(IF(msc=2, Zalicz14, 0)),
					SUM(IF(msc=3, Zalicz14, 0)), SUM(IF(msc=4, Zalicz14, 0)),
					SUM(IF(msc=5, Zalicz14, 0)), SUM(IF(msc=6, Zalicz14, 0)),		
					SUM(IF(msc=7, Zalicz14, 0)), SUM(IF(msc=8, Zalicz14, 0)),
					SUM(IF(msc=9, Zalicz14, 0)), SUM(IF(msc=10, Zalicz14, 0)),
					SUM(IF(msc=11, Zalicz14, 0)), SUM(IF(msc=12, Zalicz14, 0))
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
						pit_4 WHERE rok = _Rok	
				;
					 
					 
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_109>', _kwota01, '</P_109>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_110>', _kwota02, '</P_110>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_111>', _kwota03, '</P_111>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_112>', _kwota04, '</P_112>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_113>', _kwota05, '</P_113>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_114>', _kwota06, '</P_114>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_115>', _kwota07, '</P_115>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_116>', _kwota08, '</P_116>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_117>', _kwota09, '</P_117>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_118>', _kwota10, '</P_118>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_119>', _kwota11, '</P_119>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_120>', _kwota12, '</P_120>\n');	
			
				SELECT 	
					SUM(IF(msc=1, PodatekPFRON, 0)) AS P_97, SUM(IF(msc=2, PodatekPFRON, 0)),
					SUM(IF(msc=3, PodatekPFRON, 0)), SUM(IF(msc=4, PodatekPFRON, 0)),
					SUM(IF(msc=5, PodatekPFRON, 0)), SUM(IF(msc=6, PodatekPFRON, 0)),		
					SUM(IF(msc=7, PodatekPFRON, 0)), SUM(IF(msc=8, PodatekPFRON, 0)),
					SUM(IF(msc=9, PodatekPFRON, 0)), SUM(IF(msc=10, PodatekPFRON, 0)),
					SUM(IF(msc=11, PodatekPFRON, 0)), SUM(IF(msc=12, PodatekPFRON, 0)) AS P_108
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
						pit_4 WHERE rok = _Rok	
				;
														 					
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_121>', _kwota01, '</P_121>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_122>', _kwota02, '</P_122>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_123>', _kwota03, '</P_123>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_124>', _kwota04, '</P_124>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_125>', _kwota05, '</P_125>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_126>', _kwota06, '</P_126>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_127>', _kwota07, '</P_127>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_128>', _kwota08, '</P_128>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_129>', _kwota09, '</P_129>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_130>', _kwota10, '</P_130>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_131>', _kwota11, '</P_131>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_132>', _kwota12, '</P_132>\n');									
					
			--
				

		
		
				
				
				SELECT
					SUM(IF(msc=1, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_133, 
					SUM(IF(msc=2, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_134, 
					SUM(IF(msc=3, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_135, 
					SUM(IF(msc=4, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_136, 
					--
					SUM(IF(msc=5, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ), 
					SUM(IF(msc=6, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=7, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=8, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) ,
					SUM(IF(msc=9, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=10, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=11, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=12, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_144
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
					pit_4 WHERE rok = _Rok	
				;

				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_133>', _kwota01, '</P_133>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_134>', _kwota02, '</P_134>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_135>', _kwota03, '</P_135>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_136>', _kwota04, '</P_136>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_137>', _kwota05, '</P_137>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_138>', _kwota06, '</P_138>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_139>', _kwota07, '</P_139>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_140>', _kwota08, '</P_140>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_141>', _kwota09, '</P_141>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_142>', _kwota10, '</P_142>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_143>', _kwota11, '</P_143>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_144>', _kwota12, '</P_144>\n');		 
												
		
				SELECT 	
					SUM(IF(msc=1, WynagrodzPlatnika, 0)) AS P_145, SUM(IF(msc=2, WynagrodzPlatnika, 0)),
					SUM(IF(msc=3, WynagrodzPlatnika, 0)), SUM(IF(msc=4, WynagrodzPlatnika, 0)),
					SUM(IF(msc=5, WynagrodzPlatnika, 0)), SUM(IF(msc=6, WynagrodzPlatnika, 0)),		
					SUM(IF(msc=7, WynagrodzPlatnika, 0)), SUM(IF(msc=8, WynagrodzPlatnika, 0)),
					SUM(IF(msc=9, WynagrodzPlatnika, 0)), SUM(IF(msc=10, WynagrodzPlatnika, 0)),
					SUM(IF(msc=11, WynagrodzPlatnika, 0)), SUM(IF(msc=12, WynagrodzPlatnika, 0)) AS P_156
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
						pit_4 WHERE rok = _Rok	
				;
					 
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_145>', _kwota01, '</P_145>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_146>', _kwota02, '</P_146>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_147>', _kwota03, '</P_147>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_148>', _kwota04, '</P_148>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_149>', _kwota05, '</P_149>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_150>', _kwota06, '</P_150>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_151>', _kwota07, '</P_151>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_152>', _kwota08, '</P_152>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_153>', _kwota09, '</P_153>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_154>', _kwota10, '</P_154>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_155>', _kwota11, '</P_155>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_156>', _kwota12, '</P_156>\n');		 
				
				
		
				SELECT
					SUM(IF(msc=1, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) AS P_157, 
					SUM(IF(msc=2, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ), 
					SUM(IF(msc=3, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ), 
					SUM(IF(msc=4, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ), 
					--
					SUM(IF(msc=5, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ), 
					SUM(IF(msc=6, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=7, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=8, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) ,
					SUM(IF(msc=9, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=10, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=11, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=12, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) AS P_168
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
					pit_4 WHERE rok = _Rok	
				;

				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_157>', _kwota01, '</P_157>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_158>', _kwota02, '</P_158>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_159>', _kwota03, '</P_159>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_160>', _kwota04, '</P_160>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_161>', _kwota05, '</P_161>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_162>', _kwota06, '</P_162>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_163>', _kwota07, '</P_163>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_164>', _kwota08, '</P_164>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_165>', _kwota09, '</P_165>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_166>', _kwota10, '</P_166>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_167>', _kwota11, '</P_167>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_168>', _kwota12, '</P_168>\n');	

				SET @ePIT.poz = @ePIT.xml;	
												
				/*	<xsd:element name="P_169" type="etd:TTekstowy" minOccurs="0">  -- Typ znakowy ograniczony do 3500 znaków
					<xsd:annotation>
						<xsd:documentation>Wyjaśnienie różnicy pomiędzy kwotą pobranego podatku a kwotą wpłaconego podatku</xsd:documentation>
					</xsd:annotation>
				    </xsd:element>		  
				select distinct left(Uzasadnienie, 1, 250) where ifnull(Uzasadnienie, '') <>''	
				*/
				SET SESSION group_concat_max_len = 9000;
				
				SELECT GROUP_CONCAT(IF(IFNULL(uzasadnienie, '') <>"", CONCAT(msc,": ", IFNULL(uzasadnienie, '')),"") ORDER BY msc SEPARATOR "\n" ) AS uzasadnienie
				INTO @pit4.uzasadnienie
				FROM pit_4 WHERE rok= _Rok ORDER BY msc
				;
				-- SELECT GROUP_CONCAT(DISTINCT uzasadnienie SEPARATOR "\n") FROM pit_4 WHERE rok=2014 ORDER BY msc ;
				
				IF LENGTH(IFNULL(@pit4.uzasadnienie, ''))>0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_169>', @pit4.uzasadnienie, '</P_169>\n');	 
				END IF;
			
				SET @xml.IC = @xml.IC - 1;	
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '</PozycjeSzczegolowe>\n');
			-- koniec sekcji pozycji szczegolowych	
		END;
		
			
		/* Pouczenie1 i Pouczenie2 (UTF8) OK */  
		SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Pouczenia>1</Pouczenia>\n');
		-- ewentualne zalaczniki
		IF (@ePIT.CelZlozenia = cKorekta) AND (LENGTH(@_przyczyna_ORD_ZU) > 0) THEN   
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Zalaczniki>\n');									
			CALL pit_tools('pit.xml.insert.ORD_ZU', '', '', '', '', '', '3');						
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '</Zalaczniki>\n');				
		END IF;
																	   						
		SET @ePIT.xml = CONCAT(@ePIT.xml, '</Deklaracja>');
		
		-- wstawiamy wygenerowany XML do e_doc_po
		-- SET _IDN = _Param_A;
		CALL e_doc_tools('e_doc_po.insert.or.edit', _Param_A, IFNULL(@ePIT.xml, ''), 'CREATE_XML', 'ORYG', '', '', '', '');
	END IF;
/*------------------------------------------------------------------------------------------------------------------------------------------------------------------	
	'pit4r.xml.create.201803'
		wygenerowanie XMLa na podstawie rekordu PIT-4R 
		Parametry - do zrewidowania
		_MODE		  Tryb pracy
		IN _GUID      GUID  rekordu		
		IN _Param_A   Rok, za ktory jest skladana deklaracja
				
		CALL pit_tools('pit4r.xml.create.201803', '', '', '', '', '', '');
------------------------------------------------------------------------------------------------------------------------------------------------------------------*/	
IF _MODE = 'pit4r.xml.create.201803' THEN
SELECT  
		d.ID, p.przyczyna_ORD_ZU, p.rok
		INTO _e_Doc_Def_ID, @_przyczyna_ORD_ZU, _Rok
	FROM 	
		pit_4r p, e_doc_def d 
	WHERE	
		p.GUID = _GUID AND  p.e_Doc_Def_ID = d.owner_doc_Id
	;

	SET @ePIT.xml = '';
	SET @xml.IC = 1;
		
	SET @ePIT.xml = '<?xml version="1.0" encoding="UTF-8" ?>\n';	
	SET @ePIT.xml = CONCAT(@ePIT.xml, '<Deklaracja xmlns="', _e_Doc_Def_ID, '" ');
	SET @ePIT.xml = CONCAT(@ePIT.xml, 'xmlns:etd="http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2016/01/25/eD/DefinicjeTypy/">', '\n');
	SET @ePIT.xmlns = @ePIT.xml; 	
		

	
	CALL pit_tools('pit.xml.insert.header', _GUID, @ePIT.xml, 'pit_4r', 'P', '', '');
	
	
		SET @ePIT.header = @ePIT.xml;
		
		CALL pit_tools('pit.xml.insert.podmiot1', '', '', 'Płatnik', '', '', '1');  -- OK
		
		SET @ePIT.P1 = @ePIT.xml;
		
		-- <PozycjeSzczegolowe> 
		BEGIN
			DECLARE 
				_Lpodat01, _Lpodat02, _Lpodat03, _Lpodat04, _Lpodat05, _Lpodat06,
				_Lpodat07, _Lpodat08, _Lpodat09, _Lpodat10, _Lpodat11, _Lpodat12  INTEGER;
			        
			
		
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<PozycjeSzczegolowe>\n');
				SET @xml.IC = @xml.IC + 1;
				
			
								
				SELECT 		
						-- wartosci do C.1 (i C.3)
						SUM(IF(msc=1, Lpodat1, 0)) AS P_9, SUM(IF(msc=2, Lpodat1, 0)) AS P_10,
						SUM(IF(msc=3, Lpodat1, 0)) AS P_11, SUM(IF(msc=4, Lpodat1, 0)) AS P_12,
						SUM(IF(msc=5, Lpodat1, 0)) AS P_13, SUM(IF(msc=6, Lpodat1, 0)) AS P_14,						
						_varINT(SUM(IF(msc=1, Zalicz1+Zalicz2, 0))) AS P_15, _varINT(SUM(IF(msc=2, Zalicz1+Zalicz2, 0))) AS P_16,
						_varINT(SUM(IF(msc=3, Zalicz1+Zalicz2, 0))) AS P_17, _varINT(SUM(IF(msc=4, Zalicz1+Zalicz2, 0))) AS P_18,
						_varINT(SUM(IF(msc=5, Zalicz1+Zalicz2, 0))) AS P_19, _varINT(SUM(IF(msc=6, Zalicz1+Zalicz2, 0))) AS P_20,					
						SUM(IF(msc=7, Lpodat1, 0)) AS P_21, SUM(IF(msc=8, Lpodat1, 0)) AS P_22,
						SUM(IF(msc=9, Lpodat1, 0)) AS P_23, SUM(IF(msc=10, Lpodat1, 0)) AS P_24,
						SUM(IF(msc=11, Lpodat1, 0)) AS P_25, SUM(IF(msc=12, Lpodat1, 0)) AS P_26,
						SUM(IF(msc=7, Zalicz1+Zalicz2, 0))  AS P_27, SUM(IF(msc=8, Zalicz1+Zalicz2, 0)) AS P_28,
						SUM(IF(msc=9, Zalicz1+Zalicz2, 0))  AS P_29, SUM(IF(msc=10, Zalicz1+Zalicz2, 0)) AS P_30,
						SUM(IF(msc=11, Zalicz1+Zalicz2, 0)) AS P_31, SUM(IF(msc=12, Zalicz1+Zalicz2, 0)) AS P_32
						INTO _Lpodat01, _Lpodat02, _Lpodat03, _Lpodat04, _Lpodat05, _Lpodat06,
						 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _Lpodat07, _Lpodat08, _Lpodat09, _Lpodat10, _Lpodat11, _Lpodat12,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					/*	-- C.4
						
						-- C.5
						  */
					FROM 
						pit_4 WHERE rok = _Rok											
					;
						 
					-- wstawiamy C.1 (p9...p32)
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL),  '<P_9>', _Lpodat01, '</P_9>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_10>', _Lpodat02, '</P_10>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_11>', _Lpodat03, '</P_11>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_12>', _Lpodat04, '</P_12>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_13>', _Lpodat05, '</P_13>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_14>', _Lpodat06, '</P_14>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_15>', _kwota01, '</P_15>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_16>', _kwota02, '</P_16>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_17>', _kwota03, '</P_17>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_18>', _kwota04, '</P_18>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_19>', _kwota05, '</P_19>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_20>', _kwota06, '</P_20>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_21>', _Lpodat07, '</P_21>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_22>', _Lpodat08, '</P_22>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_23>', _Lpodat09, '</P_23>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_24>', _Lpodat10, '</P_24>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_25>', _Lpodat11, '</P_25>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_26>', _Lpodat12, '</P_26>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_27>', _kwota07, '</P_27>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_28>', _kwota08, '</P_28>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_29>', _kwota09, '</P_29>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_30>', _kwota10, '</P_30>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_31>', _kwota11, '</P_31>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_32>', _kwota12, '</P_32>\n');													

				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_45>', _kwota01, '</P_45>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_46>', _kwota02, '</P_46>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_47>', _kwota03, '</P_47>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_48>', _kwota04, '</P_48>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_49>', _kwota05, '</P_49>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_50>', _kwota06, '</P_50>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_51>', _kwota07, '</P_51>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_52>', _kwota08, '</P_52>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_53>', _kwota09, '</P_53>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_54>', _kwota10, '</P_54>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_55>', _kwota11, '</P_55>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_56>', _kwota12, '</P_56>\n');													
																
	
				SELECT 	
					SUM(IF(msc=1, Zalicz11, 0)) AS P_57, SUM(IF(msc=2, Zalicz11, 0)),
					SUM(IF(msc=3, Zalicz11, 0)), SUM(IF(msc=4, Zalicz11, 0)),
					SUM(IF(msc=5, Zalicz11, 0)), SUM(IF(msc=6, Zalicz11, 0)),		
					SUM(IF(msc=7, Zalicz11, 0)), SUM(IF(msc=8, Zalicz11, 0)),
					SUM(IF(msc=9, Zalicz11, 0)), SUM(IF(msc=10, Zalicz11, 0)),
					SUM(IF(msc=11, Zalicz11, 0)), SUM(IF(msc=12, Zalicz11, 0)) AS P_68
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
						pit_4 WHERE rok = _Rok	
				;
										 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_57>', _kwota01, '</P_57>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_58>', _kwota02, '</P_58>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_59>', _kwota03, '</P_59>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_60>', _kwota04, '</P_60>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_61>', _kwota05, '</P_61>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_62>', _kwota06, '</P_62>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_63>', _kwota07, '</P_63>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_64>', _kwota08, '</P_64>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_65>', _kwota09, '</P_65>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_66>', _kwota10, '</P_66>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_67>', _kwota11, '</P_67>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_68>', _kwota12, '</P_68>\n');									
				
				
				SELECT 	
					SUM(IF(msc=1, Zalicz12, 0)) AS P_69, SUM(IF(msc=2, Zalicz12, 0)),
					SUM(IF(msc=3, Zalicz12, 0)), SUM(IF(msc=4, Zalicz12, 0)),
					SUM(IF(msc=5, Zalicz12, 0)), SUM(IF(msc=6, Zalicz12, 0)),		
					SUM(IF(msc=7, Zalicz12, 0)), SUM(IF(msc=8, Zalicz12, 0)),
					SUM(IF(msc=9, Zalicz12, 0)), SUM(IF(msc=10, Zalicz12, 0)),
					SUM(IF(msc=11, Zalicz12, 0)), SUM(IF(msc=12, Zalicz12, 0)) AS P_80
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
						pit_4 WHERE rok = _Rok	
				;
										 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_69>', _kwota01, '</P_69>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_70>', _kwota02, '</P_70>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_71>', _kwota03, '</P_71>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_72>', _kwota04, '</P_72>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_73>', _kwota05, '</P_73>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_74>', _kwota06, '</P_74>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_75>', _kwota07, '</P_75>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_76>', _kwota08, '</P_76>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_77>', _kwota09, '</P_77>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_78>', _kwota10, '</P_78>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_79>', _kwota11, '</P_79>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_80>', _kwota12, '</P_80>\n');									
					
			
				SELECT 	
					SUM(IF(msc=1, PodatekPFRON, 0)) AS P_97, SUM(IF(msc=2, PodatekPFRON, 0)),
					SUM(IF(msc=3, PodatekPFRON, 0)), SUM(IF(msc=4, PodatekPFRON, 0)),
					SUM(IF(msc=5, PodatekPFRON, 0)), SUM(IF(msc=6, PodatekPFRON, 0)),		
					SUM(IF(msc=7, PodatekPFRON, 0)), SUM(IF(msc=8, PodatekPFRON, 0)),
					SUM(IF(msc=9, PodatekPFRON, 0)), SUM(IF(msc=10, PodatekPFRON, 0)),
					SUM(IF(msc=11, PodatekPFRON, 0)), SUM(IF(msc=12, PodatekPFRON, 0)) AS P_108
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
						pit_4 WHERE rok = _Rok	
				;
														 					
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_81>', _kwota01, '</P_81>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_82>', _kwota02, '</P_82>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_83>', _kwota03, '</P_83>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_84>', _kwota04, '</P_84>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_85>', _kwota05, '</P_85>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_86>', _kwota06, '</P_86>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_87>', _kwota07, '</P_87>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_88>', _kwota08, '</P_88>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_89>', _kwota09, '</P_89>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_90>', _kwota10, '</P_90>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_91>', _kwota11, '</P_91>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_92>', _kwota12, '</P_92>\n');									
					
			
				SELECT 	
					SUM(IF(msc=1, PodatekOdZlecen, 0)) AS P_109, SUM(IF(msc=2, PodatekOdZlecen, 0)),
					SUM(IF(msc=3, PodatekOdZlecen, 0)), SUM(IF(msc=4, PodatekOdZlecen, 0)),
					SUM(IF(msc=5, PodatekOdZlecen, 0)), SUM(IF(msc=6, PodatekOdZlecen, 0)),		
					SUM(IF(msc=7, PodatekOdZlecen, 0)), SUM(IF(msc=8, PodatekOdZlecen, 0)),
					SUM(IF(msc=9, PodatekOdZlecen, 0)), SUM(IF(msc=10, PodatekOdZlecen, 0)),
					SUM(IF(msc=11, PodatekOdZlecen, 0)), SUM(IF(msc=12, PodatekOdZlecen, 0)) AS P_120
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
						pit_4 WHERE rok = _Rok	
				;
														 					
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_93>', _kwota01, '</P_93>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_94>', _kwota02, '</P_94>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_95>', _kwota03, '</P_95>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_96>', _kwota04, '</P_96>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_97>', _kwota05, '</P_97>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_98>', _kwota06, '</P_98>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_99>', _kwota07, '</P_99>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_100>', _kwota08, '</P_100>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_101>', _kwota09, '</P_101>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_102>', _kwota10, '</P_102>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_103>', _kwota11, '</P_103>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_104>', _kwota12, '</P_104>\n');

		
				SELECT 	
					SUM(IF(msc=1, Zalicz9, 0)) AS P_121, SUM(IF(msc=2, Zalicz9, 0)),
					SUM(IF(msc=3, Zalicz9, 0)), SUM(IF(msc=4, Zalicz9, 0)),
					SUM(IF(msc=5, Zalicz9, 0)), SUM(IF(msc=6, Zalicz9, 0)),		
					SUM(IF(msc=7, Zalicz9, 0)), SUM(IF(msc=8, Zalicz9, 0)),
					SUM(IF(msc=9, Zalicz9, 0)), SUM(IF(msc=10, Zalicz9, 0)),
					SUM(IF(msc=11, Zalicz9, 0)), SUM(IF(msc=12, Zalicz9, 0)) AS P_132
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
						pit_4 WHERE rok = _Rok	
				;
					
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_105>', _kwota01, '</P_105>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_106>', _kwota02, '</P_106>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_107>', _kwota03, '</P_107>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_108>', _kwota04, '</P_108>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_109>', _kwota05, '</P_109>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_110>', _kwota06, '</P_110>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_111>', _kwota07, '</P_111>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_112>', _kwota08, '</P_112>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_113>', _kwota09, '</P_113>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_114>', _kwota10, '</P_114>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_115>', _kwota11, '</P_115>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_116>', _kwota12, '</P_116>\n');	
				
				
				SELECT
					SUM(IF(msc=1, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_133, 
					SUM(IF(msc=2, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_134, 
					SUM(IF(msc=3, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_135, 
					SUM(IF(msc=4, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_136, 
					--
					SUM(IF(msc=5, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ), 
					SUM(IF(msc=6, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=7, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=8, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) ,
					SUM(IF(msc=9, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=10, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=11, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=12, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_144
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
					pit_4 WHERE rok = _Rok	
				;

				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_117>', _kwota01, '</P_117>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_118>', _kwota02, '</P_118>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_119>', _kwota03, '</P_119>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_120>', _kwota04, '</P_120>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_121>', _kwota05, '</P_121>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_122>', _kwota06, '</P_122>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_123>', _kwota07, '</P_123>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_124>', _kwota08, '</P_124>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_125>', _kwota09, '</P_125>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_126>', _kwota10, '</P_126>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_127>', _kwota11, '</P_127>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_128>', _kwota12, '</P_128>\n');		 
												
		
				SELECT 	
					SUM(IF(msc=1, WynagrodzPlatnika, 0)) AS P_145, SUM(IF(msc=2, WynagrodzPlatnika, 0)),
					SUM(IF(msc=3, WynagrodzPlatnika, 0)), SUM(IF(msc=4, WynagrodzPlatnika, 0)),
					SUM(IF(msc=5, WynagrodzPlatnika, 0)), SUM(IF(msc=6, WynagrodzPlatnika, 0)),		
					SUM(IF(msc=7, WynagrodzPlatnika, 0)), SUM(IF(msc=8, WynagrodzPlatnika, 0)),
					SUM(IF(msc=9, WynagrodzPlatnika, 0)), SUM(IF(msc=10, WynagrodzPlatnika, 0)),
					SUM(IF(msc=11, WynagrodzPlatnika, 0)), SUM(IF(msc=12, WynagrodzPlatnika, 0)) AS P_156
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
						pit_4 WHERE rok = _Rok	
				;
					 
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_129>', _kwota01, '</P_129>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_130>', _kwota02, '</P_130>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_131>', _kwota03, '</P_131>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_132>', _kwota04, '</P_132>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_133>', _kwota05, '</P_133>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_134>', _kwota06, '</P_134>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_135>', _kwota07, '</P_135>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_136>', _kwota08, '</P_136>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_137>', _kwota09, '</P_137>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_138>', _kwota10, '</P_138>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_139>', _kwota11, '</P_139>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_140>', _kwota12, '</P_140>\n');		 
				
				
		
				SELECT
					SUM(IF(msc=1, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) AS P_157, 
					SUM(IF(msc=2, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ), 
					SUM(IF(msc=3, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ), 
					SUM(IF(msc=4, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ), 
					--
					SUM(IF(msc=5, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ), 
					SUM(IF(msc=6, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=7, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=8, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) ,
					SUM(IF(msc=9, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=10, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=11, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=12, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) AS P_168
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
					pit_4 WHERE rok = _Rok	
				;

				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_141>', _kwota01, '</P_141>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_142>', _kwota02, '</P_142>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_143>', _kwota03, '</P_143>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_144>', _kwota04, '</P_144>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_145>', _kwota05, '</P_145>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_146>', _kwota06, '</P_146>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_147>', _kwota07, '</P_147>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_148>', _kwota08, '</P_148>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_149>', _kwota09, '</P_149>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_150>', _kwota10, '</P_150>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_151>', _kwota11, '</P_151>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_152>', _kwota12, '</P_152>\n');	

				SET @ePIT.poz = @ePIT.xml;	
												
				/*	<xsd:element name="P_169" type="etd:TTekstowy" minOccurs="0">  -- Typ znakowy ograniczony do 3500 znaków
					<xsd:annotation>
						<xsd:documentation>Wyjaśnienie różnicy pomiędzy kwotą pobranego podatku a kwotą wpłaconego podatku</xsd:documentation>
					</xsd:annotation>
				    </xsd:element>		  
				select distinct left(Uzasadnienie, 1, 250) where ifnull(Uzasadnienie, '') <>''	
				*/
				SET SESSION group_concat_max_len = 9000;
				
				SELECT GROUP_CONCAT(IF(IFNULL(uzasadnienie, '') <>"", CONCAT(msc,": ", IFNULL(uzasadnienie, '')),"") ORDER BY msc SEPARATOR "\n" ) AS uzasadnienie
				INTO @pit4.uzasadnienie
				FROM pit_4 WHERE rok= _Rok ORDER BY msc
				;
				-- SELECT GROUP_CONCAT(DISTINCT uzasadnienie SEPARATOR "\n") FROM pit_4 WHERE rok=2014 ORDER BY msc ;
				
				IF LENGTH(IFNULL(@pit4.uzasadnienie, ''))>0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_153>', @pit4.uzasadnienie, '</P_153>\n');	 
				END IF;
			
				SET @xml.IC = @xml.IC - 1;	
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '</PozycjeSzczegolowe>\n');
			-- koniec sekcji pozycji szczegolowych	
		END;
		
			
		/* Pouczenie1 i Pouczenie2 (UTF8) OK */  
		SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Pouczenia>1</Pouczenia>\n');
		-- ewentualne zalaczniki
		IF (@ePIT.CelZlozenia = cKorekta) AND (LENGTH(@_przyczyna_ORD_ZU) > 0) THEN   
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Zalaczniki>\n');									
			CALL pit_tools('pit.xml.insert.ORD_ZU', '', '', '', '', '', '');						
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '</Zalaczniki>\n');				
		END IF;
																	   						
		SET @ePIT.xml = CONCAT(@ePIT.xml, '</Deklaracja>');
		
		-- wstawiamy wygenerowany XML do e_doc_po
		-- SET _IDN = _Param_A;
		CALL e_doc_tools('e_doc_po.insert.or.edit', _Param_A, IFNULL(@ePIT.xml, ''), 'CREATE_XML', 'ORYG', '', '', '', '');
	END IF;


/*------------------------------------------------------------------------------------------------------------------------------------------------------------------	
	'pit4r.xml.create.201512'
		wygenerowanie XMLa na podstawie rekordu PIT-4R 
		Parametry - do zrewidowania
		_MODE		  Tryb pracy
		IN _GUID      GUID  rekordu		
		IN _Param_A   Rok, za ktory jest skladana deklaracja
				
		CALL pit_tools('pit4r.xml.create.201512', '', '', '', '', '', '');
------------------------------------------------------------------------------------------------------------------------------------------------------------------*/			
	IF _MODE = 'pit4r.xml.create.201512' THEN

	/* 		podstawowa struktura ident. jak w przypadku pit-8AR
		 - <Deklaracja> 
			<xsd:documentation>DEKLARACJA ROCZNA O POBRANYCH ZALICZKACH NA PODATEK DOCHODOWY</xsd:documentation>
			 + <Naglowek> 
			 + <Podmiot1> 			 
			 + <PozycjeSzczegolowe> 			 
			 <Pouczenie1>
			 <Pouczenie2>
			 <xsd:element name="Objasnienie">
			 <Zalaczniki minOccurs="0">  
		   </Deklaracja> 		
		*/	
		
/*	SELECT 
		CelZlozenia, rok, US_GUID, d.doc_id, _varSTR(d.form_ver), d.doc_type, d.doc_ver,					  									
			przyczyna_ORD_ZU, DATE_FORMAT(d.datefrom, '%Y/%m/%d') AS def_date_from
			
	FROM pit_4r p, e_doc_def d 
	WHERE 
			 p.GUID = _GUID AND  p.e_Doc_Def_ID = d.ID
	INTO 
		_CelZlozenia, _Rok, _US_GUID, _e_doc_id, _form_ver, _e_doc_type, _doc_ver,	@_przyczyna_ORD_ZU, _def_date_from;  */
		
	SELECT  
		d.ID, p.przyczyna_ORD_ZU, p.rok
		INTO _e_Doc_Def_ID, @_przyczyna_ORD_ZU, _Rok
	FROM 	
		pit_4r p, e_doc_def d 
	WHERE	
		p.GUID = _GUID AND  p.e_Doc_Def_ID = d.owner_doc_Id
	;

	SET @ePIT.xml = '';
	SET @xml.IC = 1;
		
	SET @ePIT.xml = '<?xml version="1.0" encoding="UTF-8" ?>\n';	
	SET @ePIT.xml = CONCAT(@ePIT.xml, '<Deklaracja xmlns="', _e_Doc_Def_ID, '" ');
	SET @ePIT.xml = CONCAT(@ePIT.xml, cDefTypySchema, '\n');
	SET @ePIT.xmlns = @ePIT.xml; 	
		
/*	SET @ePIT.xml = CONCAT(@ePIT.xml, '<Deklaracja xmlns="http://crd.gov.pl/wzor/', _def_date_from, '/1883', '/" ');  -- 2014/12/08/1883...  
	SET @ePIT.xml = CONCAT(@ePIT.xml, 'xmlns:etd="http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2011/06/21/eD/DefinicjeTypy/">\n');  */
	
	CALL pit_tools('pit.xml.insert.header', _GUID, @ePIT.xml, 'pit_4r', 'P', '', '');
	
	/*	zastapione przez CALL pit_tools('pit.xml.insert.header'
		-- <Naglowek> -- docelowo zrobic mode "insert_header(kod_podatku, _e_doc_id, _doc_ver, _e_doc_type, _form_ver, _CelZlozenia, _Rok)
		SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Naglowek>\n');				  			
			SET @xml.IC = @xml.IC + 1;	
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<KodFormularza kodPodatku="PIT" kodSystemowy="', _e_doc_id, '" rodzajZobowiazania="P" wersjaSchemy="', _doc_ver, '">', _e_doc_type, '</KodFormularza>\n');
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<WariantFormularza>', _form_ver, '</WariantFormularza>\n');     
			
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<CelZlozenia poz="P_6">');				
			SET @ePIT.xml = CONCAT(@ePIT.xml, _CelZlozenia);  
			SET @ePIT.xml = CONCAT(@ePIT.xml, '</CelZlozenia>\n');
			
			-- wszystkie PITy maja pole "rok", IFT ma zamiast tego <Od - Do>
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Rok>');	-- <xsd:minInclusive value="2014"/>			
			SET @ePIT.xml = CONCAT(@ePIT.xml, _Rok);  
			SET @ePIT.xml = CONCAT(@ePIT.xml, '</Rok>\n');
						
			SET _KodUS = '';		
			SELECT KOD_urzedu FROM `US`
			WHERE guid = _US_GUID
			into _KodUS;  
			
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<KodUrzedu>');				
			SET @ePIT.xml = CONCAT(@ePIT.xml, _KodUS);  
			SET @ePIT.xml = CONCAT(@ePIT.xml, '</KodUrzedu>\n');
		
			SET @xml.IC = @xml.IC - 1;		
		SET @ePIT.xml = CONCAT(@ePIT.xml,  SPACE(@xml.IC*cSL), '</Naglowek>\n');
		--  </Naglowek> koniec sekcji 				
		*/
		SET @ePIT.header = @ePIT.xml;
		
		CALL pit_tools('pit.xml.insert.podmiot1', '', '', 'Płatnik', '', '', '');  -- OK
		
		SET @ePIT.P1 = @ePIT.xml;
		
		-- <PozycjeSzczegolowe> 
		BEGIN
			DECLARE 
				_Lpodat01, _Lpodat02, _Lpodat03, _Lpodat04, _Lpodat05, _Lpodat06,
				_Lpodat07, _Lpodat08, _Lpodat09, _Lpodat10, _Lpodat11, _Lpodat12  INTEGER;
			        
			
			-- odpytujemy o poszczegolne wiersze - jesli jest rekord z danym wierszem, to go wstawiamy
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<PozycjeSzczegolowe>\n');
				SET @xml.IC = @xml.IC + 1;
				
				-- C.1. Zaliczki na podatek obliczone przez płatników, o których mowa w art. 31 i art. 42e ust. 1 ustawy
				-- SET _ile = 0; SET @poz.rodzaj = 5;
								
				SELECT 		
						-- wartosci do C.1 (i C.3)
						SUM(IF(msc=1, Lpodat1, 0)) AS P_9, SUM(IF(msc=2, Lpodat1, 0)) AS P_10,
						SUM(IF(msc=3, Lpodat1, 0)) AS P_11, SUM(IF(msc=4, Lpodat1, 0)) AS P_12,
						SUM(IF(msc=5, Lpodat1, 0)) AS P_13, SUM(IF(msc=6, Lpodat1, 0)) AS P_14,						
						_varINT(SUM(IF(msc=1, Zalicz1+Zalicz2, 0))) AS P_15, _varINT(SUM(IF(msc=2, Zalicz1+Zalicz2, 0))) AS P_16,
						_varINT(SUM(IF(msc=3, Zalicz1+Zalicz2, 0))) AS P_17, _varINT(SUM(IF(msc=4, Zalicz1+Zalicz2, 0))) AS P_18,
						_varINT(SUM(IF(msc=5, Zalicz1+Zalicz2, 0))) AS P_19, _varINT(SUM(IF(msc=6, Zalicz1+Zalicz2, 0))) AS P_20,					
						SUM(IF(msc=7, Lpodat1, 0)) AS P_21, SUM(IF(msc=8, Lpodat1, 0)) AS P_22,
						SUM(IF(msc=9, Lpodat1, 0)) AS P_23, SUM(IF(msc=10, Lpodat1, 0)) AS P_24,
						SUM(IF(msc=11, Lpodat1, 0)) AS P_25, SUM(IF(msc=12, Lpodat1, 0)) AS P_26,
						SUM(IF(msc=7, Zalicz1+Zalicz2, 0))  AS P_27, SUM(IF(msc=8, Zalicz1+Zalicz2, 0)) AS P_28,
						SUM(IF(msc=9, Zalicz1+Zalicz2, 0))  AS P_29, SUM(IF(msc=10, Zalicz1+Zalicz2, 0)) AS P_30,
						SUM(IF(msc=11, Zalicz1+Zalicz2, 0)) AS P_31, SUM(IF(msc=12, Zalicz1+Zalicz2, 0)) AS P_32
						INTO _Lpodat01, _Lpodat02, _Lpodat03, _Lpodat04, _Lpodat05, _Lpodat06,
						 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _Lpodat07, _Lpodat08, _Lpodat09, _Lpodat10, _Lpodat11, _Lpodat12,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					/*	-- C.4
						
						-- C.5
						  */
					from 
						pit_4 WHERE rok = _Rok											
					;
						 
					-- wstawiamy C.1 (p9...p32)
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL),  '<P_9>', _Lpodat01, '</P_9>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_10>', _Lpodat02, '</P_10>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_11>', _Lpodat03, '</P_11>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_12>', _Lpodat04, '</P_12>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_13>', _Lpodat05, '</P_13>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_14>', _Lpodat06, '</P_14>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_15>', _kwota01, '</P_15>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_16>', _kwota02, '</P_16>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_17>', _kwota03, '</P_17>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_18>', _kwota04, '</P_18>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_19>', _kwota05, '</P_19>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_20>', _kwota06, '</P_20>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_21>', _Lpodat07, '</P_21>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_22>', _Lpodat08, '</P_22>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_23>', _Lpodat09, '</P_23>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_24>', _Lpodat10, '</P_24>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_25>', _Lpodat11, '</P_25>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_26>', _Lpodat12, '</P_26>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_27>', _kwota07, '</P_27>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_28>', _kwota08, '</P_28>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_29>', _kwota09, '</P_29>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_30>', _kwota10, '</P_30>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_31>', _kwota11, '</P_31>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_32>', _kwota12, '</P_32>\n');													

					-- wstawiamy C.3 (p45...56)	3. Suma należnych zaliczek za poszczególne miesiące roku podatkowego wykazanych w wierszach 1 i 2	
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_45>', _kwota01, '</P_45>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_46>', _kwota02, '</P_46>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_47>', _kwota03, '</P_47>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_48>', _kwota04, '</P_48>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_49>', _kwota05, '</P_49>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_50>', _kwota06, '</P_50>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_51>', _kwota07, '</P_51>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_52>', _kwota08, '</P_52>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_53>', _kwota09, '</P_53>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_54>', _kwota10, '</P_54>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_55>', _kwota11, '</P_55>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_56>', _kwota12, '</P_56>\n');													
																
				-- C.4. =  4. Zaliczki na podatek, których pobór został ograniczony na podstawie art. 32 ust. 2 ustawy
				SELECT 	
					SUM(IF(msc=1, Zalicz11, 0)) AS P_57, SUM(IF(msc=2, Zalicz11, 0)),
					SUM(IF(msc=3, Zalicz11, 0)), SUM(IF(msc=4, Zalicz11, 0)),
					SUM(IF(msc=5, Zalicz11, 0)), SUM(IF(msc=6, Zalicz11, 0)),		
					SUM(IF(msc=7, Zalicz11, 0)), SUM(IF(msc=8, Zalicz11, 0)),
					SUM(IF(msc=9, Zalicz11, 0)), SUM(IF(msc=10, Zalicz11, 0)),
					SUM(IF(msc=11, Zalicz11, 0)), SUM(IF(msc=12, Zalicz11, 0)) AS P_68
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				from 
						pit_4 WHERE rok = _Rok	
				;
										 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_57>', _kwota01, '</P_57>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_58>', _kwota02, '</P_58>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_59>', _kwota03, '</P_59>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_60>', _kwota04, '</P_60>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_61>', _kwota05, '</P_61>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_62>', _kwota06, '</P_62>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_63>', _kwota07, '</P_63>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_64>', _kwota08, '</P_64>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_65>', _kwota09, '</P_65>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_66>', _kwota10, '</P_66>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_67>', _kwota11, '</P_67>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_68>', _kwota12, '</P_68>\n');									
				
				-- C.5. =  5. Zaliczki, które przypadały do pobrania w związku z ograniczeniem poboru zaliczek w poprzednich miesiącach
				SELECT 	
					SUM(IF(msc=1, Zalicz12, 0)) AS P_69, SUM(IF(msc=2, Zalicz12, 0)),
					SUM(IF(msc=3, Zalicz12, 0)), SUM(IF(msc=4, Zalicz12, 0)),
					SUM(IF(msc=5, Zalicz12, 0)), SUM(IF(msc=6, Zalicz12, 0)),		
					SUM(IF(msc=7, Zalicz12, 0)), SUM(IF(msc=8, Zalicz12, 0)),
					SUM(IF(msc=9, Zalicz12, 0)), SUM(IF(msc=10, Zalicz12, 0)),
					SUM(IF(msc=11, Zalicz12, 0)), SUM(IF(msc=12, Zalicz12, 0)) AS P_80
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
						pit_4 WHERE rok = _Rok	
				;
										 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_69>', _kwota01, '</P_69>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_70>', _kwota02, '</P_70>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_71>', _kwota03, '</P_71>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_72>', _kwota04, '</P_72>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_73>', _kwota05, '</P_73>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_74>', _kwota06, '</P_74>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_75>', _kwota07, '</P_75>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_76>', _kwota08, '</P_76>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_77>', _kwota09, '</P_77>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_78>', _kwota10, '</P_78>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_79>', _kwota11, '</P_79>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_80>', _kwota12, '</P_80>\n');									
					
				-- C.6. =  Dodatkowo pobrany podatek wynikający z rozliczenia za rok ubiegły
				SELECT 	
					SUM(IF(msc=1, Zalicz13, 0)) AS P_81, SUM(IF(msc=2, Zalicz13, 0)),
					SUM(IF(msc=3, Zalicz13, 0)), SUM(IF(msc=4, Zalicz13, 0))  AS P_84
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04
				FROM 
						pit_4 WHERE rok = _Rok	
				;
					 
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_81>', _kwota01, '</P_81>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_82>', _kwota02, '</P_82>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_83>', _kwota03, '</P_83>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_84>', _kwota04, '</P_84>\n');					 
				
				
				-- C.7. =  Nadpłaty wynikające z rozliczenia za rok ubiegły oraz zwrot nadpłat w gotówce, zaliczone na poczet należnej zaliczki
				SELECT
					SUM(IF(msc=1, Zalicz14+Zalicz15, 0)) AS P_85, SUM(IF(msc=2, Zalicz14+Zalicz15, 0)) ,
					SUM(IF(msc=3, Zalicz14+Zalicz15, 0)), SUM(IF(msc=4, Zalicz14+Zalicz15, 0)),
					SUM(IF(msc=5, Zalicz14+Zalicz15, 0)), SUM(IF(msc=6, Zalicz14+Zalicz15, 0)),	
					SUM(IF(msc=7, Zalicz14+Zalicz15, 0)), SUM(IF(msc=8, Zalicz14+Zalicz15, 0)),
					SUM(IF(msc=9, Zalicz14+Zalicz15, 0)), SUM(IF(msc=10, Zalicz14+Zalicz15, 0)),
					SUM(IF(msc=11, Zalicz14+Zalicz15, 0)), SUM(IF(msc=12, Zalicz14+Zalicz15, 0)) AS P_96
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
						pit_4 WHERE rok = _Rok	
				;
										 					
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_85>', _kwota01, '</P_85>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_86>', _kwota02, '</P_86>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_87>', _kwota03, '</P_87>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_88>', _kwota04, '</P_88>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_89>', _kwota05, '</P_89>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_90>', _kwota06, '</P_90>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_91>', _kwota07, '</P_91>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_92>', _kwota08, '</P_92>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_93>', _kwota09, '</P_93>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_94>', _kwota10, '</P_94>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_95>', _kwota11, '</P_95>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_96>', _kwota12, '</P_96>\n');									
				
								
				-- C.8 =  Pobrany podatek, przekazany na PFRON oraz zakładowy fundusz rehabilitacji osób niepełnosprawnych albo zakładowy fundusz aktywności
				SELECT 	
					SUM(IF(msc=1, PodatekPFRON, 0)) AS P_97, SUM(IF(msc=2, PodatekPFRON, 0)),
					SUM(IF(msc=3, PodatekPFRON, 0)), SUM(IF(msc=4, PodatekPFRON, 0)),
					SUM(IF(msc=5, PodatekPFRON, 0)), SUM(IF(msc=6, PodatekPFRON, 0)),		
					SUM(IF(msc=7, PodatekPFRON, 0)), SUM(IF(msc=8, PodatekPFRON, 0)),
					SUM(IF(msc=9, PodatekPFRON, 0)), SUM(IF(msc=10, PodatekPFRON, 0)),
					SUM(IF(msc=11, PodatekPFRON, 0)), SUM(IF(msc=12, PodatekPFRON, 0)) AS P_108
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
						pit_4 WHERE rok = _Rok	
				;
														 					
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_97>', _kwota01, '</P_97>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_98>', _kwota02, '</P_98>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_99>', _kwota03, '</P_99>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_100>', _kwota04, '</P_100>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_101>', _kwota05, '</P_101>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_102>', _kwota06, '</P_102>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_103>', _kwota07, '</P_103>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_104>', _kwota08, '</P_104>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_105>', _kwota09, '</P_105>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_106>', _kwota10, '</P_106>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_107>', _kwota11, '</P_107>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_108>', _kwota12, '</P_108>\n');									
					
				-- C.9 =  Zaliczki na podatek pobrane od świadczeń z tytułu działalności określonej w art. 13 pkt 2 i 4-9 oraz art. 18 ustawy
				SELECT 	
					SUM(IF(msc=1, PodatekOdZlecen, 0)) AS P_109, SUM(IF(msc=2, PodatekOdZlecen, 0)),
					SUM(IF(msc=3, PodatekOdZlecen, 0)), SUM(IF(msc=4, PodatekOdZlecen, 0)),
					SUM(IF(msc=5, PodatekOdZlecen, 0)), SUM(IF(msc=6, PodatekOdZlecen, 0)),		
					SUM(IF(msc=7, PodatekOdZlecen, 0)), SUM(IF(msc=8, PodatekOdZlecen, 0)),
					SUM(IF(msc=9, PodatekOdZlecen, 0)), SUM(IF(msc=10, PodatekOdZlecen, 0)),
					SUM(IF(msc=11, PodatekOdZlecen, 0)), SUM(IF(msc=12, PodatekOdZlecen, 0)) AS P_120
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
						pit_4 WHERE rok = _Rok	
				;
														 					
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_109>', _kwota01, '</P_109>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_110>', _kwota02, '</P_110>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_111>', _kwota03, '</P_111>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_112>', _kwota04, '</P_112>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_113>', _kwota05, '</P_113>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_114>', _kwota06, '</P_114>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_115>', _kwota07, '</P_115>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_116>', _kwota08, '</P_116>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_117>', _kwota09, '</P_117>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_118>', _kwota10, '</P_118>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_119>', _kwota11, '</P_119>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_120>', _kwota12, '</P_120>\n');

				-- C.10 Zaliczki na podatek pobrane od innych należności, w tym wynikających z umowy aktywizacyjnej	
				SELECT 	
					SUM(IF(msc=1, Zalicz9, 0)) AS P_121, SUM(IF(msc=2, Zalicz9, 0)),
					SUM(IF(msc=3, Zalicz9, 0)), SUM(IF(msc=4, Zalicz9, 0)),
					SUM(IF(msc=5, Zalicz9, 0)), SUM(IF(msc=6, Zalicz9, 0)),		
					SUM(IF(msc=7, Zalicz9, 0)), SUM(IF(msc=8, Zalicz9, 0)),
					SUM(IF(msc=9, Zalicz9, 0)), SUM(IF(msc=10, Zalicz9, 0)),
					SUM(IF(msc=11, Zalicz9, 0)), SUM(IF(msc=12, Zalicz9, 0)) AS P_132
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
						pit_4 WHERE rok = _Rok	
				;
					
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_121>', _kwota01, '</P_121>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_122>', _kwota02, '</P_122>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_123>', _kwota03, '</P_123>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_124>', _kwota04, '</P_124>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_125>', _kwota05, '</P_125>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_126>', _kwota06, '</P_126>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_127>', _kwota07, '</P_127>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_128>', _kwota08, '</P_128>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_129>', _kwota09, '</P_129>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_130>', _kwota10, '</P_130>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_131>', _kwota11, '</P_131>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_132>', _kwota12, '</P_132>\n');	
				
				/* C.11 Pobrany podatek do przekazania do urzędu skarbowego za poszczególne miesiące roku podatkowego
				   C.11 = (C.3 + C.5 + C.6 + C.9 + C.10) - (C.4 + C.7 + C.8);  				   
				   C.6 (Zalicz13) m-ce: 01..04  
				   ewent. wyliczać C.11 i C.13 w oddzielnym mode 'pit4r.calc'
				   */
				SELECT
					SUM(IF(msc=1, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_133, 
					SUM(IF(msc=2, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_134, 
					SUM(IF(msc=3, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_135, 
					SUM(IF(msc=4, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_136, 
					--
					SUM(IF(msc=5, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ), 
					SUM(IF(msc=6, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=7, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=8, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) ,
					SUM(IF(msc=9, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=10, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=11, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) , 
					SUM(IF(msc=12, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON), 0 ), 0) ) AS P_144
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
					pit_4 WHERE rok = _Rok	
				;

				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_133>', _kwota01, '</P_133>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_134>', _kwota02, '</P_134>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_135>', _kwota03, '</P_135>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_136>', _kwota04, '</P_136>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_137>', _kwota05, '</P_137>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_138>', _kwota06, '</P_138>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_139>', _kwota07, '</P_139>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_140>', _kwota08, '</P_140>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_141>', _kwota09, '</P_141>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_142>', _kwota10, '</P_142>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_143>', _kwota11, '</P_143>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_144>', _kwota12, '</P_144>\n');		 
												
				-- C.12  Wynagrodzenie z tytułu terminowego wpłacania podatku dochodowego za poszczególne miesiące, zgodnie z art. 28 Ordynacji podatkowej
				SELECT 	
					SUM(IF(msc=1, WynagrodzPlatnika, 0)) AS P_145, SUM(IF(msc=2, WynagrodzPlatnika, 0)),
					SUM(IF(msc=3, WynagrodzPlatnika, 0)), SUM(IF(msc=4, WynagrodzPlatnika, 0)),
					SUM(IF(msc=5, WynagrodzPlatnika, 0)), SUM(IF(msc=6, WynagrodzPlatnika, 0)),		
					SUM(IF(msc=7, WynagrodzPlatnika, 0)), SUM(IF(msc=8, WynagrodzPlatnika, 0)),
					SUM(IF(msc=9, WynagrodzPlatnika, 0)), SUM(IF(msc=10, WynagrodzPlatnika, 0)),
					SUM(IF(msc=11, WynagrodzPlatnika, 0)), SUM(IF(msc=12, WynagrodzPlatnika, 0)) AS P_156
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
						pit_4 WHERE rok = _Rok	
				;
					 
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_145>', _kwota01, '</P_145>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_146>', _kwota02, '</P_146>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_147>', _kwota03, '</P_147>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_148>', _kwota04, '</P_148>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_149>', _kwota05, '</P_149>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_150>', _kwota06, '</P_150>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_151>', _kwota07, '</P_151>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_152>', _kwota08, '</P_152>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_153>', _kwota09, '</P_153>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_154>', _kwota10, '</P_154>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_155>', _kwota11, '</P_155>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_156>', _kwota12, '</P_156>\n');		 
				
				
				/* C.13  Należne kwoty do wpłaty za poszczególne miesiące roku podatkowego
				   C.13 = C.11 - C.12 */
				SELECT
					SUM(IF(msc=1, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) AS P_157, 
					SUM(IF(msc=2, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ), 
					SUM(IF(msc=3, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ), 
					SUM(IF(msc=4, GREATEST( (Zalicz1+Zalicz2+Zalicz12+Zalicz13+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ), 
					--
					SUM(IF(msc=5, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ), 
					SUM(IF(msc=6, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=7, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=8, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) ,
					SUM(IF(msc=9, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=10, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=11, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) , 
					SUM(IF(msc=12, GREATEST( (Zalicz1+Zalicz2+Zalicz12+PodatekOdZlecen+Zalicz9) - (Zalicz11 + Zalicz14+Zalicz15 + PodatekPFRON) - WynagrodzPlatnika, 0 ), 0) ) AS P_168
					INTO
					 _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
				FROM 
					pit_4 WHERE rok = _Rok	
				;

				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_157>', _kwota01, '</P_157>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_158>', _kwota02, '</P_158>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_159>', _kwota03, '</P_159>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_160>', _kwota04, '</P_160>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_161>', _kwota05, '</P_161>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_162>', _kwota06, '</P_162>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_163>', _kwota07, '</P_163>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_164>', _kwota08, '</P_164>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_165>', _kwota09, '</P_165>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_166>', _kwota10, '</P_166>\n');									
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_167>', _kwota11, '</P_167>\n');				
				SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_168>', _kwota12, '</P_168>\n');	

				SET @ePIT.poz = @ePIT.xml;	
												
				/*	<xsd:element name="P_169" type="etd:TTekstowy" minOccurs="0">  -- Typ znakowy ograniczony do 3500 znaków
					<xsd:annotation>
						<xsd:documentation>Wyjaśnienie różnicy pomiędzy kwotą pobranego podatku a kwotą wpłaconego podatku</xsd:documentation>
					</xsd:annotation>
				    </xsd:element>		  
				select distinct left(Uzasadnienie, 1, 250) where ifnull(Uzasadnienie, '') <>''	
				*/
				SET SESSION group_concat_max_len = 9000;
				
				select GROUP_CONCAT(IF(IFNULL(uzasadnienie, '') <>"", CONCAT(msc,": ", IFNULL(uzasadnienie, '')),"") ORDER BY msc SEPARATOR "\n" ) AS uzasadnienie
				into @pit4.uzasadnienie
				FROM pit_4 WHERE rok= _Rok ORDER BY msc
				;
				-- SELECT GROUP_CONCAT(DISTINCT uzasadnienie SEPARATOR "\n") FROM pit_4 WHERE rok=2014 ORDER BY msc ;
				
				IF LENGTH(IFNULL(@pit4.uzasadnienie, ''))>0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_169>', @pit4.uzasadnienie, '</P_169>\n');	 
				END IF;
			
				SET @xml.IC = @xml.IC - 1;	
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '</PozycjeSzczegolowe>\n');
			-- koniec sekcji pozycji szczegolowych	
		END;
		
			
		/* Pouczenie1 i Pouczenie2 (UTF8) OK */  
		SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Pouczenie1>W przypadku niewpłacenia w obowiązującym terminie kwot z poz. od 157 do 168 lub wpłacenia ich w niepełnej wysokości niniejsza deklaracja stanowi podstawę do wystawienia tytułu wykonawczego, zgodnie z przepisami ustawy z dnia 17 czerwca 1966 r. o postępowaniu egzekucyjnym w administracji (Dz. U. z 2014 r. poz. 1619, z późn. zm.).</Pouczenie1>\n');
		SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Pouczenie2>Za uchybienie obowiązkom płatnika grozi odpowiedzialność przewidziana w Kodeksie karnym skarbowym.</Pouczenie2>\n');	
		SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Objasnienie>Kwoty podatku i wynagrodzenia przysługującego płatnikom z tytułu terminowego wpłacenia podatku dochodowego, zgodnie z art. 63 Ordynacji podatkowej, zaokrągla się do pełnych złotych.</Objasnienie>\n');	
		
		-- ewentualne zalaczniki
		IF (@ePIT.CelZlozenia = cKorekta) AND (LENGTH(@_przyczyna_ORD_ZU) > 0) THEN   
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Zalaczniki>\n');									
			CALL pit_tools('pit.xml.insert.ORD_ZU', '', '', '', '', '', '');						
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '</Zalaczniki>\n');				
		END IF;
																	   						
		SET @ePIT.xml = CONCAT(@ePIT.xml, '</Deklaracja>');
		
		-- wstawiamy wygenerowany XML do e_doc_po
		-- SET _IDN = _Param_A;
		CALL e_doc_tools('e_doc_po.insert.or.edit', _Param_A, IFNULL(@ePIT.xml, ''), 'CREATE_XML', 'ORYG', '', '', '', '');
						 
	END IF;  -- IF _MODE = 'pit4r.xml.create.201512'

	
/*------------------------------------------------------------------------------------------------------------------------------------------------------------------	
	'pit4r.rec.insert'
		utworzenie nowego rekordu PIT-4R
		Parametry 
		_MODE		  Tryb pracy
		IN _GUID      GUID  rekordu						
		IN _Param_A   Rok, za ktory jest skladana deklaracja
		IN _Param_B   Cel zlozenia: 1 - zgloszenie, 2 - korekta;  
		IN _Param_C   przyczyna ORD_ZU (da korekt)
		IN _Param_D   rodzaj korekty (dla korekt)
		
		CALL pit_tools('pit4r.rec.insert', @GUID, '2014', '1', '', '', '');
		CALL pit_tools('pit4r.rec.insert', @GUID, '2014', '2', 'blednie wypelnione pole', '', '');
------------------------------------------------------------------------------------------------------------------------------------------------------------------*/			
	IF _MODE = 'pit4r.rec.insert' THEN

		CALL _IReg_Get('DaneFirmy', 'Urzad_skarbowy', '0', 'STRING', @US_GUID, '(brak danych)');
	
		INSERT IGNORE INTO pit_4r (GUID, Rok, CelZlozenia, US_GUID, przyczyna_ORD_ZU, RodzajKorekty)
		SELECT 		 						  -- cel zlozenia: 1 - zgloszenie, 2 - korekta;  
			_GUID  AS GUID, _varINT(_Param_A), _varINT(_Param_B) AS CelZlozenia, @US_GUID,
			IF(_varINT(_Param_B) = 2, _Param_C, ''),  _varINT(_Param_D);
				 
	END IF;

	/*------------------------------------------------------------------------------------------------------------------------------------------------------------------	
	'pit8ar.xml.create.201801'
		utworzenie XML z deklaracja PIT-8AR(7) za 2018 rok
		Parametry
		_MODE		  Tryb pracy
		IN _GUID      GUID rekordu		cds_e_doc_Ng.FieldByName('Src_GUID').AsString
		IN _Param_A   					cds_e_doc_Ng.FieldByName('idn').asString
				
		CALL pit8ar_tools('pit8ar.xml.create.201801', '{2377D968-DBCE-4E92-8BA9-6754CA114C2C}', @_eDeklContent, 570, '', '', ''); 
------------------------------------------------------------------------------------------------------------------------------------------------------------------*/			

IF _MODE = 'pit8ar.xml.create.201801' THEN

	SELECT  
		d.ID, p.przyczyna_ORD_ZU
		INTO _e_Doc_Def_ID, @_przyczyna_ORD_ZU
	FROM 	
		pit_8ar p, e_doc_def d 
	WHERE	
		p.GUID = _GUID AND  p.e_Doc_Def_ID = d.owner_doc_Id
	;

	SET @ePIT.xml = '';
	SET @xml.IC = 1;
		
	SET @ePIT.xml = '<?xml version="1.0" encoding="UTF-8" ?>\n';	
								
	
	SET @ePIT.xml = CONCAT(@ePIT.xml, '<Deklaracja xmlns="', _e_Doc_Def_ID, '" ');
	SET @ePIT.xml = CONCAT(@ePIT.xml, 'xmlns:etd="http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2018/08/24/eD/DefinicjeTypy/">', '\n');
	SET @ePIT.xmlns = @ePIT.xml; 	
				
		
		
		
		CALL pit_tools('pit.xml.insert.header', _GUID, @ePIT.xml, 'pit_8ar', 'P', '', '');
				
		SET @ePIT.header = @ePIT.xml;
		
		CALL pit_tools('pit.xml.insert.podmiot1', '', '', 'Płatnik', '', '', '1');
		
		
		
			
			
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<PozycjeSzczegolowe>\n');
				SET @xml.IC = @xml.IC + 1;
				
				
				SET _ile = 0; SET @poz.rodzaj = 2;
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_9>', _kwota01, '</P_9>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_10>', _kwota02, '</P_10>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_11>', _kwota03, '</P_11>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_12>', _kwota04, '</P_12>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_13>', _kwota05, '</P_13>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_14>', _kwota06, '</P_14>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_15>', _kwota07, '</P_15>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_16>', _kwota08, '</P_16>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_17>', _kwota09, '</P_17>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_18>', _kwota10, '</P_18>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_19>', _kwota11, '</P_19>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_20>', _kwota12, '</P_20>\n');									
				END IF;  	
				
				
				SET _ile = 0; SET @poz.rodzaj = 5;
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 
					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL),  '<P_21>', _kwota01, '</P_21>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_22>', _kwota02, '</P_22>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_23>', _kwota03, '</P_23>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_24>', _kwota04, '</P_24>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_25>', _kwota05, '</P_25>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_26>', _kwota06, '</P_26>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_27>', _kwota07, '</P_27>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_28>', _kwota08, '</P_28>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_29>', _kwota09, '</P_29>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_30>', _kwota10, '</P_30>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_31>', _kwota11, '</P_31>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_32>', _kwota12, '</P_32>\n');									
				END IF;  
				
				
				
				SET _ile = 0; SET @poz.rodzaj = 4;
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_33>', _kwota01, '</P_33>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_34>', _kwota02, '</P_34>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_35>', _kwota03, '</P_35>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_36>', _kwota04, '</P_36>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_37>', _kwota05, '</P_37>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_38>', _kwota06, '</P_38>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_39>', _kwota07, '</P_39>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_40>', _kwota08, '</P_40>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_41>', _kwota09, '</P_41>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_42>', _kwota10, '</P_42>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_43>', _kwota11, '</P_43>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_44>', _kwota12, '</P_44>\n');									
				END IF;  
				
				
				
				
				
				
				
				SET _ile = 0; SET @poz.rodzaj = 777;  
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 		
					IF _kwota01 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_141>', _kwota01, '</P_141>\n');				
					END IF;	
					IF _kwota02 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_142>', _kwota02, '</P_142>\n');				
					END IF;	
					IF _kwota03 > 0 THEN							
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_143>', _kwota03, '</P_143>\n');							
					END IF;		
					IF _kwota04 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_144>', _kwota04, '</P_144>\n');				
					END IF;		
					IF _kwota05 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_145>', _kwota05, '</P_145>\n');									
					END IF;		
					IF _kwota06 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_146>', _kwota06, '</P_146>\n');				
					END IF;	
					IF _kwota07 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_147>', _kwota07, '</P_147>\n');									
					END IF;	
					IF _kwota08 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_148>', _kwota08, '</P_148>\n');				
					END IF;	
					IF _kwota09 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_149>', _kwota09, '</P_149>\n');									
					END IF;	
					IF _kwota10 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_150>', _kwota10, '</P_150>\n');				
					END IF;	
					IF _kwota11 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_151>', _kwota11, '</P_151>\n');									
					END IF;	
					IF _kwota12 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_152>', _kwota12, '</P_152>\n');									
					END IF;	
				END IF;  
				
				
				
				SET _ile = 0; SET @poz.rodzaj = 3;
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_177>', _kwota01, '</P_177>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_178>', _kwota02, '</P_178>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_179>', _kwota03, '</P_179>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_180>', _kwota04, '</P_180>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_181>', _kwota05, '</P_181>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_182>', _kwota06, '</P_182>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_183>', _kwota07, '</P_183>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_184>', _kwota08, '</P_184>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_185>', _kwota09, '</P_185>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_186>', _kwota10, '</P_186>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_187>', _kwota11, '</P_187>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_188>', _kwota12, '</P_188>\n');									
				END IF;  
				
				
				
				SET _ile = 0; SET @poz.rodzaj = 13;
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_309>', _kwota01, '</P_309>\n');				 
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_310>', _kwota02, '</P_310>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_311>', _kwota03, '</P_311>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_312>', _kwota04, '</P_312>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_313>', _kwota05, '</P_313>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_314>', _kwota06, '</P_314>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_315>', _kwota07, '</P_315>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_316>', _kwota08, '</P_316>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_317>', _kwota09, '</P_317>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_318>', _kwota10, '</P_318>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_319>', _kwota11, '</P_319>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_320>', _kwota12, '</P_320>\n');									
				END IF;  
				
				
				
				SET _ile = 0; SET @poz.rodzaj = 14;
				
				
				
				
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;

					SET @pit8ar.k01 = _kwota01;		
					SET @pit8ar.k02 = _kwota02;		
					SET @pit8ar.k03 = _kwota03;		
					SET @pit8ar.k04 = _kwota04;		
					SET @pit8ar.k05 = _kwota05;		
					SET @pit8ar.k06 = _kwota06;		
					SET @pit8ar.k07 = _kwota07;		
					SET @pit8ar.k08 = _kwota08;		
					SET @pit8ar.k09 = _kwota09;		
					SET @pit8ar.k10 = _kwota10;		
					SET @pit8ar.k11 = _kwota11;		
					SET @pit8ar.k12 = _kwota12;		
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_321>', _varDOUBLE_NOTNULL(_kwota01), '</P_321>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_322>', _varDOUBLE_NOTNULL(_kwota02), '</P_322>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_323>', _varDOUBLE_NOTNULL(_kwota03), '</P_323>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_324>', _varDOUBLE_NOTNULL(_kwota04), '</P_324>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_325>', _varDOUBLE_NOTNULL(_kwota05), '</P_325>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_326>', _varDOUBLE_NOTNULL(_kwota06), '</P_326>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_327>', _varDOUBLE_NOTNULL(_kwota07), '</P_327>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_328>', _varDOUBLE_NOTNULL(_kwota08), '</P_328>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_329>', _varDOUBLE_NOTNULL(_kwota09), '</P_329>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_330>', _varDOUBLE_NOTNULL(_kwota10), '</P_330>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_331>', _varDOUBLE_NOTNULL(_kwota11), '</P_331>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_332>', _varDOUBLE_NOTNULL(_kwota12), '</P_332>\n');									
				
				
				
				
				SET _ile = 0;				
				SELECT 
					IFNULL(wynagr01, 0) + IFNULL(wynagr02, 0) +  IFNULL(wynagr03, 0) + IFNULL(wynagr04, 0) + 
					IFNULL(wynagr05, 0) + IFNULL(wynagr06, 0) +  IFNULL(wynagr07, 0) + IFNULL(wynagr08, 0) + 	
					IFNULL(wynagr09, 0) + IFNULL(wynagr10, 0) +  IFNULL(wynagr11, 0) + IFNULL(wynagr12, 0) 
				AS ile
				INTO _ile
				FROM pit_8ar
				WHERE GUID = _GUID
				;
				
			
				
				IF _ile > 0 THEN
					SELECT 
						IFNULL(wynagr01, 0), IFNULL(wynagr02, 0), IFNULL(wynagr03, 0), IFNULL(wynagr04, 0),
						IFNULL(wynagr05, 0), IFNULL(wynagr06, 0), IFNULL(wynagr07, 0), IFNULL(wynagr08, 0),
						IFNULL(wynagr09, 0), IFNULL(wynagr10, 0), IFNULL(wynagr11, 0), IFNULL(wynagr12, 0)
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar
					WHERE GUID = _GUID  
					;
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_333>', _kwota01, '</P_333>\n');				 
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_334>', _kwota02, '</P_334>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_335>', _kwota03, '</P_335>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_336>', _kwota04, '</P_336>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_337>', _kwota05, '</P_337>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_338>', _kwota06, '</P_338>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_339>', _kwota07, '</P_339>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_340>', _kwota08, '</P_340>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_341>', _kwota09, '</P_341>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_342>', _kwota10, '</P_342>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_343>', _kwota11, '</P_343>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_344>', _kwota12, '</P_344>\n');

					SET @pit8ar.k01 = GREATEST(@pit8ar.k01 - _kwota01, 0);					
					SET @pit8ar.k02 = GREATEST(@pit8ar.k02 - _kwota02, 0);					
					SET @pit8ar.k03 = GREATEST(@pit8ar.k03 - _kwota03, 0);					
					SET @pit8ar.k04 = GREATEST(@pit8ar.k04 - _kwota04, 0);					
					SET @pit8ar.k05 = GREATEST(@pit8ar.k05 - _kwota05, 0);					
					SET @pit8ar.k06 = GREATEST(@pit8ar.k06 - _kwota06, 0);					
					SET @pit8ar.k07 = GREATEST(@pit8ar.k07 - _kwota07, 0);					
					SET @pit8ar.k08 = GREATEST(@pit8ar.k08 - _kwota08, 0);					
					SET @pit8ar.k09 = GREATEST(@pit8ar.k09 - _kwota09, 0);					
					SET @pit8ar.k10 = GREATEST(@pit8ar.k10 - _kwota10, 0);					
					SET @pit8ar.k11 = GREATEST(@pit8ar.k11 - _kwota11, 0);					
					SET @pit8ar.k12 = GREATEST(@pit8ar.k12 - _kwota12, 0);					
				END IF;  
				
				
				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_345>', _varDOUBLE_NOTNULL(@pit8ar.k01), '</P_345>\n');				 
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_346>', _varDOUBLE_NOTNULL(@pit8ar.k02), '</P_346>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_347>', _varDOUBLE_NOTNULL(@pit8ar.k03), '</P_347>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_348>', _varDOUBLE_NOTNULL(@pit8ar.k04), '</P_348>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_349>', _varDOUBLE_NOTNULL(@pit8ar.k05), '</P_349>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_350>', _varDOUBLE_NOTNULL(@pit8ar.k06), '</P_350>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_351>', _varDOUBLE_NOTNULL(@pit8ar.k07), '</P_351>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_352>', _varDOUBLE_NOTNULL(@pit8ar.k08), '</P_352>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_353>', _varDOUBLE_NOTNULL(@pit8ar.k09), '</P_353>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_354>', _varDOUBLE_NOTNULL(@pit8ar.k10), '</P_354>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_355>', _varDOUBLE_NOTNULL(@pit8ar.k11), '</P_355>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_356>', _varDOUBLE_NOTNULL(@pit8ar.k12), '</P_356>\n');									
			
				SET @xml.IC = @xml.IC - 1;	
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '</PozycjeSzczegolowe>\n');
			
		
		SET @ePIT.poz = @ePIT.xml;
			
		  
	
		SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Pouczenia>1</Pouczenia>\n');
		
		IF (@ePIT.CelZlozenia = cKorekta)  AND (LENGTH(@_przyczyna_ORD_ZU) > 0) THEN   
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Zalaczniki>\n');									
			CALL pit_tools('pit.xml.insert.ORD_ZU', '', '', '', '', '', '3');						
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '</Zalaczniki>\n');				
		END IF;
																	   						
		SET @ePIT.xml = CONCAT(@ePIT.xml, '</Deklaracja>');
		
		
		
		CALL e_doc_tools('e_doc_po.insert.or.edit', _Param_A, IFNULL(@ePIT.xml, ''), 'CREATE_XML', 'ORYG', '', '', '', '');
						 
END IF;  
/*------------------------------------------------------------------------------------------------------------------------------------------------------------------	
	'pit8ar.xml.create.201512'
		utworzenie XML z deklaracja PIT-8AR(5) za 2015 rok
		Parametry
		_MODE		  Tryb pracy
		IN _GUID      GUID rekordu		cds_e_doc_Ng.FieldByName('Src_GUID').AsString
		IN _Param_A   					cds_e_doc_Ng.FieldByName('idn').asString
				
		CALL pit8ar_tools('pit8ar.xml.create.201512', '{2377D968-DBCE-4E92-8BA9-6754CA114C2C}', @_eDeklContent, 570, '', '', ''); 
------------------------------------------------------------------------------------------------------------------------------------------------------------------*/			
IF _MODE = 'pit8ar.xml.create.201512' THEN

	/* 
		 - <Deklaracja> 
			<xsd:documentation>DEKLARACJA ROCZNA O ZRYCZAŁTOWANYM PODATKU DOCHODOWYM</xsd:documentation>
			 + <Naglowek> 
			 + <Podmiot1> 			 
			 + <PozycjeSzczegolowe> 			 
			 <Pouczenie1>
			 <Pouczenie2>
			 <Zalaczniki minOccurs="0">  
		   </Deklaracja> 		
		*/	
		
	SELECT  
		d.ID, p.przyczyna_ORD_ZU
	INTO _e_Doc_Def_ID, @_przyczyna_ORD_ZU
	FROM 	
		pit_8ar p, e_doc_def d 
	WHERE	
		p.GUID = _GUID AND  p.e_Doc_Def_ID = d.owner_doc_Id
	;

	SET @ePIT.xml = '';
	SET @xml.IC = 1;
		
	SET @ePIT.xml = '<?xml version="1.0" encoding="UTF-8" ?>\n';	
								
	-- SET @ePIT.xml = CONCAT(@ePIT.xml, '<Deklaracja xmlns="http://crd.gov.pl/wzor/', _def_date_from, '/1882', '/" ');  -- http://crd.gov.pl/wzor/2014/12/08/1882/
	SET @ePIT.xml = CONCAT(@ePIT.xml, '<Deklaracja xmlns="', _e_Doc_Def_ID, '" ');
	SET @ePIT.xml = CONCAT(@ePIT.xml, cDefTypySchema, '\n');
	SET @ePIT.xmlns = @ePIT.xml; 	
		
		-- <Naglowek> 
		-- CALL pit8ar_tools('pit8ar.xml.insert.header', _GUID, @ePIT.xml, '', '', '', '');  -- CALL pit_tools('pit.xml.insert.header', _GUID, @ePIT.xml, 'pit_8ar', '', '', '');
		CALL pit_tools('pit.xml.insert.header', _GUID, @ePIT.xml, 'pit_8ar', 'P', '', '');
				
		SET @ePIT.header = @ePIT.xml;
		
		CALL pit_tools('pit.xml.insert.podmiot1', '', '', 'Płatnik', '', '', '');
		
		-- <PozycjeSzczegolowe> 
		/* BEGIN
			 DECLARE _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					_kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12 DOUBLE;*/
			
			-- odpytujemy o poszczegolne wiersze - jesli jest rekord z danym wierszem, to go wstawiamy
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<PozycjeSzczegolowe>\n');
				SET @xml.IC = @xml.IC + 1;
				
				-- C.1. 1. Wygrane w konkursach, grach i zakładach wzajemnych oraz nagrody związane ze sprzedażą premiową (art. 30 ust. 1 pkt 2 
				SET _ile = 0; SET @poz.rodzaj = 5;
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 
					-- wstawiamy p9...p20
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL),  '<P_9>', _kwota01, '</P_9>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_10>', _kwota02, '</P_10>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_11>', _kwota03, '</P_11>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_12>', _kwota04, '</P_12>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_13>', _kwota05, '</P_13>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_14>', _kwota06, '</P_14>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_15>', _kwota07, '</P_15>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_16>', _kwota08, '</P_16>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_17>', _kwota09, '</P_17>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_18>', _kwota10, '</P_18>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_19>', _kwota11, '</P_19>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_20>', _kwota12, '</P_20>\n');									
				END IF;  
				-- koniec C.1
				
				-- C.2. =  2. Świadczenia otrzymane przez emerytów lub rencistów będących byłymi pracownikami (art. 30 ust. 1 pkt 4 ustawy)  
				SET _ile = 0; SET @poz.rodzaj = 4;
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_21>', _kwota01, '</P_21>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_22>', _kwota02, '</P_22>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_23>', _kwota03, '</P_23>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_24>', _kwota04, '</P_24>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_25>', _kwota05, '</P_25>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_26>', _kwota06, '</P_26>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_27>', _kwota07, '</P_27>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_28>', _kwota08, '</P_28>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_29>', _kwota09, '</P_29>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_30>', _kwota10, '</P_30>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_31>', _kwota11, '</P_31>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_32>', _kwota12, '</P_32>\n');									
				END IF;  
				-- koniec C.2
				
				-- C.6. =  6. Należności osób zagranicznych, wymienione w art. 29 ustawy
				SET _ile = 0; SET @poz.rodzaj = 2;
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_69>', _kwota01, '</P_69>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_70>', _kwota02, '</P_70>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_71>', _kwota03, '</P_71>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_72>', _kwota04, '</P_72>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_73>', _kwota05, '</P_73>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_74>', _kwota06, '</P_74>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_75>', _kwota07, '</P_75>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_76>', _kwota08, '</P_76>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_77>', _kwota09, '</P_77>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_78>', _kwota10, '</P_78>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_79>', _kwota11, '</P_79>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_80>', _kwota12, '</P_80>\n');									
				END IF;  
				-- koniec C.6
				
				-- C.7. =  7. Odsetki od pożyczek (z wyjątkiem gdy udzielanie pożyczek jest przedmiotem działalności gospodarczej), odsetki i dyskonto od papierów wartościowych (art. 30a ust. 1 pkt 1 i 2 ustawy)
				SET _ile = 0; SET @poz.rodzaj = 777;  -- tymczasowo, docelowo rozdzielic rodzaj=3 na dwa oddzielne rodzaje
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 		
					IF _kwota01 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_81>', _kwota01, '</P_81>\n');				
					END IF;	
					IF _kwota02 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_82>', _kwota02, '</P_82>\n');				
					END IF;	
					IF _kwota03 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_83>', _kwota03, '</P_83>\n');							
					END IF;		
					IF _kwota04 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_84>', _kwota04, '</P_84>\n');				
					END IF;		
					IF _kwota05 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_85>', _kwota05, '</P_85>\n');									
					END IF;		
					IF _kwota06 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_86>', _kwota06, '</P_86>\n');				
					END IF;	
					IF _kwota07 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_87>', _kwota07, '</P_87>\n');									
					END IF;	
					IF _kwota08 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_88>', _kwota08, '</P_88>\n');				
					END IF;	
					IF _kwota09 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_89>', _kwota09, '</P_89>\n');									
					END IF;	
					IF _kwota10 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_90>', _kwota10, '</P_90>\n');				
					END IF;	
					IF _kwota11 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_91>', _kwota11, '</P_91>\n');									
					END IF;	
					IF _kwota12 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_92>', _kwota12, '</P_92>\n');									
					END IF;	
				END IF;  
				-- koniec C.7
				
				-- C.9. =  9. Dywidendy i inne przychody z tytułu udziału w zyskach osób prawnych (art. 30a ust. 1 pkt 4 ustawy)
				SET _ile = 0; SET @poz.rodzaj = 3;
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_105>', _kwota01, '</P_105>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_106>', _kwota02, '</P_106>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_107>', _kwota03, '</P_107>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_108>', _kwota04, '</P_108>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_109>', _kwota05, '</P_109>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_110>', _kwota06, '</P_110>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_111>', _kwota07, '</P_111>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_112>', _kwota08, '</P_112>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_113>', _kwota09, '</P_113>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_114>', _kwota10, '</P_114>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_115>', _kwota11, '</P_115>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_116>', _kwota12, '</P_116>\n');									
				END IF;  
				-- koniec C.9
				
				-- C.14 =  14. Inne należności niewymienione w wierszach 1-13
				SET _ile = 0; SET @poz.rodzaj = 13;
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_165>', _kwota01, '</P_165>\n');				 
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_166>', _kwota02, '</P_166>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_167>', _kwota03, '</P_167>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_168>', _kwota04, '</P_168>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_169>', _kwota05, '</P_169>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_170>', _kwota06, '</P_170>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_171>', _kwota07, '</P_171>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_172>', _kwota08, '</P_172>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_173>', _kwota09, '</P_173>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_174>', _kwota10, '</P_174>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_175>', _kwota11, '</P_175>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_176>', _kwota12, '</P_176>\n');									
				END IF;  
				-- koniec C.14
				
				-- C.15 =  15. Suma pobranego zryczałtowanego podatku do przekazania do urzędu skarbowego za poszczególne miesiące z wierszy 1-14
				SET _ile = 0; SET @poz.rodzaj = 14;
				
				/* SELECT COUNT(*) AS ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				INTO _ile;  */
				
				-- IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;

					SET @pit8ar.k01 = _kwota01;		
					SET @pit8ar.k02 = _kwota02;		
					SET @pit8ar.k03 = _kwota03;		
					SET @pit8ar.k04 = _kwota04;		
					SET @pit8ar.k05 = _kwota05;		
					SET @pit8ar.k06 = _kwota06;		
					SET @pit8ar.k07 = _kwota07;		
					SET @pit8ar.k08 = _kwota08;		
					SET @pit8ar.k09 = _kwota09;		
					SET @pit8ar.k10 = _kwota10;		
					SET @pit8ar.k11 = _kwota11;		
					SET @pit8ar.k12 = _kwota12;		
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_177>', _varDOUBLE_NOTNULL(_kwota01), '</P_177>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_178>', _varDOUBLE_NOTNULL(_kwota02), '</P_178>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_179>', _varDOUBLE_NOTNULL(_kwota03), '</P_179>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_180>', _varDOUBLE_NOTNULL(_kwota04), '</P_180>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_181>', _varDOUBLE_NOTNULL(_kwota05), '</P_181>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_182>', _varDOUBLE_NOTNULL(_kwota06), '</P_182>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_183>', _varDOUBLE_NOTNULL(_kwota07), '</P_183>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_184>', _varDOUBLE_NOTNULL(_kwota08), '</P_184>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_185>', _varDOUBLE_NOTNULL(_kwota09), '</P_185>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_186>', _varDOUBLE_NOTNULL(_kwota10), '</P_186>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_187>', _varDOUBLE_NOTNULL(_kwota11), '</P_187>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_188>', _varDOUBLE_NOTNULL(_kwota12), '</P_188>\n');									
				-- END IF;  
				-- koniec C.15
				
				-- C.16 = 16. Wynagrodzenie z tytułu terminowego wpłacania podatku dochodowego za poszczególne miesiące
				SET _ile = 0;				--   
				select 
					ifnull(wynagr01, 0) + ifnull(wynagr02, 0) +  ifnull(wynagr03, 0) + ifnull(wynagr04, 0) + 
					ifnull(wynagr05, 0) + ifnull(wynagr06, 0) +  ifnull(wynagr07, 0) + ifnull(wynagr08, 0) + 	
					ifnull(wynagr09, 0) + ifnull(wynagr10, 0) +  ifnull(wynagr11, 0) + ifnull(wynagr12, 0) 
				AS ile
				INTO _ile
				FROM pit_8ar
				WHERE GUID = _GUID
				;
				
			/*	
				SET @poz.rodzaj = 16; 
				SELECT COUNT(*) AS ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				INTO _ile;  */
				
				IF _ile > 0 THEN
					SELECT 
						ifnull(wynagr01, 0), ifnull(wynagr02, 0), ifnull(wynagr03, 0), ifnull(wynagr04, 0),
						ifnull(wynagr05, 0), ifnull(wynagr06, 0), ifnull(wynagr07, 0), ifnull(wynagr08, 0),
						ifnull(wynagr09, 0), ifnull(wynagr10, 0), ifnull(wynagr11, 0), ifnull(wynagr12, 0)
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar
					WHERE GUID = _GUID  
					;
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_189>', _kwota01, '</P_189>\n');				 
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_190>', _kwota02, '</P_190>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_191>', _kwota03, '</P_191>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_192>', _kwota04, '</P_192>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_193>', _kwota05, '</P_193>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_194>', _kwota06, '</P_194>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_195>', _kwota07, '</P_195>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_196>', _kwota08, '</P_196>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_197>', _kwota09, '</P_197>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_198>', _kwota10, '</P_198>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_199>', _kwota11, '</P_199>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_200>', _kwota12, '</P_200>\n');

					SET @pit8ar.k01 = GREATEST(@pit8ar.k01 - _kwota01, 0);					
					SET @pit8ar.k02 = GREATEST(@pit8ar.k02 - _kwota02, 0);					
					SET @pit8ar.k03 = GREATEST(@pit8ar.k03 - _kwota03, 0);					
					SET @pit8ar.k04 = GREATEST(@pit8ar.k04 - _kwota04, 0);					
					SET @pit8ar.k05 = GREATEST(@pit8ar.k05 - _kwota05, 0);					
					SET @pit8ar.k06 = GREATEST(@pit8ar.k06 - _kwota06, 0);					
					SET @pit8ar.k07 = GREATEST(@pit8ar.k07 - _kwota07, 0);					
					SET @pit8ar.k08 = GREATEST(@pit8ar.k08 - _kwota08, 0);					
					SET @pit8ar.k09 = GREATEST(@pit8ar.k09 - _kwota09, 0);					
					SET @pit8ar.k10 = GREATEST(@pit8ar.k10 - _kwota10, 0);					
					SET @pit8ar.k11 = GREATEST(@pit8ar.k11 - _kwota11, 0);					
					SET @pit8ar.k12 = GREATEST(@pit8ar.k12 - _kwota12, 0);					
				END IF;  
				-- koniec C.16
				
				/* 17. Kwoty do wpłaty za poszczególne miesiące roku podatkowego
  				   C.17 = (C.15 - C.16)  ; */
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_201>', _varDOUBLE_NOTNULL(@pit8ar.k01), '</P_201>\n');				 
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_202>', _varDOUBLE_NOTNULL(@pit8ar.k02), '</P_202>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_203>', _varDOUBLE_NOTNULL(@pit8ar.k03), '</P_203>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_204>', _varDOUBLE_NOTNULL(@pit8ar.k04), '</P_204>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_205>', _varDOUBLE_NOTNULL(@pit8ar.k05), '</P_205>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_206>', _varDOUBLE_NOTNULL(@pit8ar.k06), '</P_206>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_207>', _varDOUBLE_NOTNULL(@pit8ar.k07), '</P_207>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_208>', _varDOUBLE_NOTNULL(@pit8ar.k08), '</P_208>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_209>', _varDOUBLE_NOTNULL(@pit8ar.k09), '</P_209>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_210>', _varDOUBLE_NOTNULL(@pit8ar.k10), '</P_210>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_211>', _varDOUBLE_NOTNULL(@pit8ar.k11), '</P_211>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_212>', _varDOUBLE_NOTNULL(@pit8ar.k12), '</P_212>\n');									
			
				SET @xml.IC = @xml.IC - 1;	
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '</PozycjeSzczegolowe>\n');
			-- koniec sekcji pozycji szczegolowych	
		-- END;
		SET @ePIT.poz = @ePIT.xml;
			
		/* Pouczenie1 i Pouczenie2 (UTF8)  */  
		SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Pouczenie1>W przypadku niewpłacenia w obowiązującym terminie kwot z poz. od 201 do 212 lub wpłacenia ich w niepełnej wysokości niniejsza deklaracja stanowi podstawę do wystawienia tytułu wykonawczego, zgodnie z przepisami ustawy z dnia 17 czerwca 1966 r. o postępowaniu egzekucyjnym w administracji (Dz. U. z 2014 r. poz. 1619, z późn. zm.).</Pouczenie1>\n');
		SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Pouczenie2>Za uchybienie obowiązkom płatnika grozi odpowiedzialność przewidziana w Kodeksie karnym skarbowym.</Pouczenie2>\n');	
		
		-- ewentualne zalaczniki
		IF (@ePIT.CelZlozenia = cKorekta)  AND (LENGTH(@_przyczyna_ORD_ZU) > 0) THEN   
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Zalaczniki>\n');									
			CALL pit_tools('pit.xml.insert.ORD_ZU', '', '', '', '', '', '');						
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '</Zalaczniki>\n');				
		END IF;
																	   						
		SET @ePIT.xml = CONCAT(@ePIT.xml, '</Deklaracja>');
		
		-- wstawiamy wygenerowany XML do e_doc_po
		-- SET _IDN = _Param_A;
		CALL e_doc_tools('e_doc_po.insert.or.edit', _Param_A, IFNULL(@ePIT.xml, ''), 'CREATE_XML', 'ORYG', '', '', '', '');
						 
END IF;  -- IF _MODE = 'pit8ar.xml.create.201512'


/*------------------------------------------------------------------------------------------------------------------------------------------------------------------	
	'pit8ar.xml.create.201412'
		utworzenie XML z deklaracja PIT-8AR(4) za 2014 rok
		Parametry 
		_MODE		  Tryb pracy
		IN _GUID      GUID  rekordu		
		IN _Param_A   
				
		CALL pit8ar_tools('pit8ar.xml.create.201412', '', '', '', '', '', '');
------------------------------------------------------------------------------------------------------------------------------------------------------------------*/			
IF _MODE = 'pit8ar.xml.create.201412' THEN

	/* 
		 - <Deklaracja> 
			<xsd:documentation>DEKLARACJA ROCZNA O ZRYCZAŁTOWANYM PODATKU DOCHODOWYM</xsd:documentation>
			 + <Naglowek> 
			 + <Podmiot1> 			 
			 + <PozycjeSzczegolowe> 			 
			 <Pouczenie1>
			 <Pouczenie2>
			 <Zalaczniki minOccurs="0">  
		   </Deklaracja> 		
		*/	
		
	SELECT 
		CelZlozenia, rok, US_GUID, d.doc_id, _varSTR(d.form_ver), d.doc_type, d.doc_ver,					  									
			przyczyna_ORD_ZU, DATE_FORMAT(d.datefrom, '%Y/%m/%d') AS def_date_from
	INTO
		_CelZlozenia, _Rok, _US_GUID, _e_doc_id, _form_ver, _e_doc_type, _doc_ver,	@_przyczyna_ORD_ZU, _def_date_from
	FROM pit_8ar p, e_doc_def d 
	WHERE 
			 p.GUID = _GUID AND  p.e_Doc_Def_ID = d.ID
	;

	SET @ePIT.xml = '';
	SET @xml.IC = 1;
		
	SET @ePIT.xml = '<?xml version="1.0" encoding="UTF-8" ?>\n';	
		
	SET @ePIT.xml = CONCAT(@ePIT.xml, '<Deklaracja xmlns="http://crd.gov.pl/wzor/', _def_date_from, '/1882', '/" ');  -- http://crd.gov.pl/wzor/2014/12/08/1882/
	SET @ePIT.xml = CONCAT(@ePIT.xml, 'xmlns:etd="http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2011/06/21/eD/DefinicjeTypy/">\n');
		
		-- <Naglowek> 
		SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Naglowek>\n');				  			
			SET @xml.IC = @xml.IC + 1;	
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<KodFormularza kodPodatku="PPR" kodSystemowy="', _e_doc_id, '" rodzajZobowiazania="P" wersjaSchemy="', _doc_ver, '">', _e_doc_type, '</KodFormularza>\n');
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<WariantFormularza>', _form_ver, '</WariantFormularza>\n');     
			
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<CelZlozenia poz="P_6">');				
			SET @ePIT.xml = CONCAT(@ePIT.xml, _CelZlozenia);  
			SET @ePIT.xml = CONCAT(@ePIT.xml, '</CelZlozenia>\n');
			
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Rok>');				
			SET @ePIT.xml = CONCAT(@ePIT.xml, _Rok);  
			SET @ePIT.xml = CONCAT(@ePIT.xml, '</Rok>\n');
						
			SET _KodUS = '';		
			SELECT KOD_urzedu
			into _KodUS
			FROM `US`
			WHERE guid = _US_GUID
			;
			
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<KodUrzedu>');				
			SET @ePIT.xml = CONCAT(@ePIT.xml, _KodUS);  
			SET @ePIT.xml = CONCAT(@ePIT.xml, '</KodUrzedu>\n');
		
			SET @xml.IC = @xml.IC - 1;		
		SET @ePIT.xml = CONCAT(@ePIT.xml,  SPACE(@xml.IC*cSL), '</Naglowek>\n');
		--  </Naglowek> koniec sekcji 
		
		SET @ePIT.header = @ePIT.xml;
		
		CALL pit_tools('pit.xml.insert.podmiot1', '', '', 'Płatnik', '', '', '');
		
		-- <PozycjeSzczegolowe> 
		/* BEGIN
			 DECLARE _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
					_kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12 DOUBLE;*/
			
			-- odpytujemy o poszczegolne wiersze - jesli jest rekord z danym wierszem, to go wstawiamy
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<PozycjeSzczegolowe>\n');
				SET @xml.IC = @xml.IC + 1;
				
				-- C.1. 1. Wygrane w konkursach, grach i zakładach wzajemnych oraz nagrody związane ze sprzedażą premiową (art. 30 ust. 1 pkt 2 
				SET _ile = 0; SET @poz.rodzaj = 5;
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 
					-- wstawiamy p9...p20
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL),  '<P_9>', _kwota01, '</P_9>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_10>', _kwota02, '</P_10>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_11>', _kwota03, '</P_11>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_12>', _kwota04, '</P_12>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_13>', _kwota05, '</P_13>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_14>', _kwota06, '</P_14>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_15>', _kwota07, '</P_15>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_16>', _kwota08, '</P_16>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_17>', _kwota09, '</P_17>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_18>', _kwota10, '</P_18>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_19>', _kwota11, '</P_19>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_20>', _kwota12, '</P_20>\n');									
				END IF;  
				-- koniec C.1
				
				-- C.2. =  2. Świadczenia otrzymane przez emerytów lub rencistów będących byłymi pracownikami (art. 30 ust. 1 pkt 4 ustawy)  
				SET _ile = 0; SET @poz.rodzaj = 4;
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_21>', _kwota01, '</P_21>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_22>', _kwota02, '</P_22>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_23>', _kwota03, '</P_23>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_24>', _kwota04, '</P_24>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_25>', _kwota05, '</P_25>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_26>', _kwota06, '</P_26>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_27>', _kwota07, '</P_27>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_28>', _kwota08, '</P_28>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_29>', _kwota09, '</P_29>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_30>', _kwota10, '</P_30>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_31>', _kwota11, '</P_31>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_32>', _kwota12, '</P_32>\n');									
				END IF;  
				-- koniec C.2
				
				-- C.5. =  5. Należności osób zagranicznych, wymienione w art. 29 ustawy
				SET _ile = 0; SET @poz.rodzaj = 2;
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_57>', _kwota01, '</P_57>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_58>', _kwota02, '</P_58>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_59>', _kwota03, '</P_59>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_60>', _kwota04, '</P_60>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_61>', _kwota05, '</P_61>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_62>', _kwota06, '</P_62>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_63>', _kwota07, '</P_63>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_64>', _kwota08, '</P_64>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_65>', _kwota09, '</P_65>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_66>', _kwota10, '</P_66>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_67>', _kwota11, '</P_67>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_68>', _kwota12, '</P_68>\n');									
				END IF;  
				-- koniec C.5
				
				-- C.8. =  8. Dywidendy i inne przychody z tytułu udziału w zyskach osób prawnych (art. 30a ust. 1 pkt 4 ustawy)
				SET _ile = 0; SET @poz.rodzaj = 3;
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_93>', _kwota01, '</P_93>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_94>', _kwota02, '</P_94>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_95>', _kwota03, '</P_95>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_96>', _kwota04, '</P_96>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_97>', _kwota05, '</P_97>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_98>', _kwota06, '</P_98>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_99>', _kwota07, '</P_99>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_100>', _kwota08, '</P_100>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_101>', _kwota09, '</P_101>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_102>', _kwota10, '</P_102>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_103>', _kwota11, '</P_103>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_104>', _kwota12, '</P_104>\n');									
				END IF;  
				-- koniec C.8
				
				-- C.13 =  13. Inne należności niewymienione w wierszach 1-12
				SET _ile = 0; SET @poz.rodzaj = 13;
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_153>', _kwota01, '</P_153>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_154>', _kwota02, '</P_154>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_155>', _kwota03, '</P_155>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_156>', _kwota04, '</P_156>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_157>', _kwota05, '</P_157>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_158>', _kwota06, '</P_158>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_159>', _kwota07, '</P_159>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_160>', _kwota08, '</P_160>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_161>', _kwota09, '</P_161>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_162>', _kwota10, '</P_162>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_163>', _kwota11, '</P_163>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_164>', _kwota12, '</P_164>\n');									
				END IF;  
				-- koniec C.13
				
				-- C.14 =  14. Suma pobranego zryczałtowanego podatku do przekazania do urzędu skarbowego za poszczególne miesiące z wierszy 1-13
				SET _ile = 0; SET @poz.rodzaj = 14;
				
				/* SELECT COUNT(*) AS ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				INTO _ile;  */
				
				-- IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_165>', _varDOUBLE_NOTNULL(_kwota01), '</P_165>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_166>', _varDOUBLE_NOTNULL(_kwota02), '</P_166>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_167>', _varDOUBLE_NOTNULL(_kwota03), '</P_167>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_168>', _varDOUBLE_NOTNULL(_kwota04), '</P_168>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_169>', _varDOUBLE_NOTNULL(_kwota05), '</P_169>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_170>', _varDOUBLE_NOTNULL(_kwota06), '</P_170>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_171>', _varDOUBLE_NOTNULL(_kwota07), '</P_171>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_172>', _varDOUBLE_NOTNULL(_kwota08), '</P_172>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_173>', _varDOUBLE_NOTNULL(_kwota09), '</P_173>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_174>', _varDOUBLE_NOTNULL(_kwota10), '</P_174>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_175>', _varDOUBLE_NOTNULL(_kwota11), '</P_175>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_176>', _varDOUBLE_NOTNULL(_kwota12), '</P_176>\n');									
				-- END IF;  
				-- koniec C.14
				
				/* 16. Kwoty do wpłaty za poszczególne miesiące roku podatkowego
  				   C.16 = (C.14 - C.15) czyli de facto C.16 = C.14 (bo nie naliczamy C.15); */
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_189>', _varDOUBLE_NOTNULL(_kwota01), '</P_189>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_190>', _varDOUBLE_NOTNULL(_kwota02), '</P_190>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_191>', _varDOUBLE_NOTNULL(_kwota03), '</P_191>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_192>', _varDOUBLE_NOTNULL(_kwota04), '</P_192>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_193>', _varDOUBLE_NOTNULL(_kwota05), '</P_193>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_194>', _varDOUBLE_NOTNULL(_kwota06), '</P_194>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_195>', _varDOUBLE_NOTNULL(_kwota07), '</P_195>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_196>', _varDOUBLE_NOTNULL(_kwota08), '</P_196>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_197>', _varDOUBLE_NOTNULL(_kwota09), '</P_197>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_198>', _varDOUBLE_NOTNULL(_kwota10), '</P_198>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_199>', _varDOUBLE_NOTNULL(_kwota11), '</P_199>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_200>', _varDOUBLE_NOTNULL(_kwota12), '</P_200>\n');									
			
				SET @xml.IC = @xml.IC - 1;	
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '</PozycjeSzczegolowe>\n');
			-- koniec sekcji pozycji szczegolowych	
		-- END;
		SET @ePIT.poz = @ePIT.xml;
			
		/* Pouczenie1 i Pouczenie2 (UTF8)  */  
		SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Pouczenie1>W przypadku niewpłacenia w obowiązującym terminie kwot z poz. od 189 do 200 lub wpłacenia ich w niepełnej wysokości niniejsza deklaracja stanowi podstawę do wystawienia tytułu wykonawczego, zgodnie z przepisami ustawy z dnia 17 czerwca 1966 r. o postępowaniu egzekucyjnym w administracji (Dz. U. z 2012 r. poz. 1015, z późn. zm.).</Pouczenie1>\n');
		SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Pouczenie2>Za uchybienie obowiązkom płatnika grozi odpowiedzialność przewidziana w Kodeksie karnym skarbowym.</Pouczenie2>\n');	
		
		-- ewentualne zalaczniki
		IF (_CelZlozenia = cKorekta)  THEN   
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Zalaczniki>\n');									
			CALL pit_tools('pit.xml.insert.ORD_ZU', '', '', '', '', '', '');						
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '</Zalaczniki>\n');				
		END IF;
																	   						
		SET @ePIT.xml = CONCAT(@ePIT.xml, '</Deklaracja>');
		
		-- wstawiamy wygenerowany XML do e_doc_po
		-- SET _IDN = _Param_A;
		CALL e_doc_tools('e_doc_po.insert.or.edit', _Param_A, IFNULL(@ePIT.xml, ''), 'CREATE_XML', 'ORYG', '', '', '', '');
						 
END IF;  -- IF _MODE = 'pit8ar.xml.create.201412'

/*------------------------------------------------------------------------------------------------------------------------------------------------------------------	
	'pit8ar.xml.create.201901'
	CALL pit_firmowy_tools('pit8ar.xml.create.201901', '{6E1A45EC-1183-4FED-825C-1660CE47A831}', @_eDeklContent, 570, '', '', ''); 
	Wersja nr 8, obowiazujaca od 201901, wystawiana w roku 2020
------------------------------------------------------------------------------------------------------------------------------------------------------------------*/			
IF _MODE = 'pit8ar.xml.create.201901' THEN
		
	SELECT  
		d.ID, p.przyczyna_ORD_ZU
		INTO _e_Doc_Def_ID, @_przyczyna_ORD_ZU
	FROM 	
		pit_8ar p, e_doc_def d 
	WHERE	
		p.GUID = _GUID AND  p.e_Doc_Def_ID = d.owner_doc_Id
	;

set @test1=1;

	SET @ePIT.xml = '';
	SET @xml.IC = 1;
		
	SET @ePIT.xml = '<?xml version="1.0" encoding="UTF-8" ?>\n';	
								
	SET @ePIT.xml = CONCAT(@ePIT.xml, '<Deklaracja xmlns="', _e_Doc_Def_ID, '" ');
	SET @ePIT.xml = CONCAT(@ePIT.xml, 'xmlns:etd="http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2018/08/24/eD/DefinicjeTypy/">', '\n');
	SET @ePIT.xmlns = @ePIT.xml; 	

		CALL pit_tools('pit.xml.insert.header', _GUID, @ePIT.xml, 'pit_8ar', 'P', '', '');
				
		SET @ePIT.header = @ePIT.xml;
		
		CALL pit_tools('pit.xml.insert.podmiot1', '', '', 'Płatnik', '', '', '');

			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<PozycjeSzczegolowe>\n');
				SET @xml.IC = @xml.IC + 1;

				SELECT RodzajKorekty
				INTO _RodzajKorekty
				FROM pit_8ar
				WHERE GUID = _GUID
				;
				
				IF _RodzajKorekty > 0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_7>', _RodzajKorekty, '</P_7>\n');
				END IF;
				
				SET _ile = 0; SET @poz.rodzaj = 2;
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_10>', _kwota01, '</P_10>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_11>', _kwota02, '</P_11>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_12>', _kwota03, '</P_12>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_13>', _kwota04, '</P_13>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_14>', _kwota05, '</P_14>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_15>', _kwota06, '</P_15>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_16>', _kwota07, '</P_16>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_17>', _kwota08, '</P_17>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_18>', _kwota09, '</P_18>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_19>', _kwota10, '</P_19>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_20>', _kwota11, '</P_20>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_21>', _kwota12, '</P_21>\n');				
				END IF;  	
				
				
				SET _ile = 0; SET @poz.rodzaj = 5;
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_22>', _kwota01, '</P_22>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_23>', _kwota02, '</P_23>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_24>', _kwota03, '</P_24>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_25>', _kwota04, '</P_25>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_26>', _kwota05, '</P_26>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_27>', _kwota06, '</P_27>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_28>', _kwota07, '</P_28>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_29>', _kwota08, '</P_29>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_30>', _kwota09, '</P_30>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_31>', _kwota10, '</P_31>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_32>', _kwota11, '</P_32>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_33>', _kwota12, '</P_33>\n');				
				END IF;  
				

-- sekcja C.3
				SET _ile = 0; SET @poz.rodzaj = 4;
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_34>', _kwota01, '</P_34>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_35>', _kwota02, '</P_35>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_36>', _kwota03, '</P_36>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_37>', _kwota04, '</P_37>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_38>', _kwota05, '</P_38>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_39>', _kwota06, '</P_39>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_40>', _kwota07, '</P_40>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_41>', _kwota08, '</P_41>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_42>', _kwota09, '</P_42>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_43>', _kwota10, '</P_43>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_44>', _kwota11, '</P_44>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_45>', _kwota12, '</P_45>\n');				
				END IF;  
				
-- sekcji od C.4 do C.11 nie mamy
-- sekcja C.12				
				SET _ile = 0; SET @poz.rodzaj = 777;  
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 		
					IF _kwota02 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_142>', _kwota01, '</P_142>\n');				
					END IF;	
					IF _kwota03 > 0 THEN							
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_143>', _kwota02, '</P_143>\n');							
					END IF;		
					IF _kwota04 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_144>', _kwota03, '</P_144>\n');				
					END IF;		
					IF _kwota05 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_145>', _kwota04, '</P_145>\n');									
					END IF;		
					IF _kwota06 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_146>', _kwota05, '</P_146>\n');				
					END IF;	
					IF _kwota07 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_147>', _kwota06, '</P_147>\n');									
					END IF;	
					IF _kwota08 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_148>', _kwota07, '</P_148>\n');				
					END IF;	
					IF _kwota09 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_149>', _kwota08, '</P_149>\n');									
					END IF;	
					IF _kwota10 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_150>', _kwota09, '</P_150>\n');				
					END IF;	
					IF _kwota11 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_151>', _kwota10, '</P_151>\n');									
					END IF;	
					IF _kwota12 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_152>', _kwota11, '</P_152>\n');									
					END IF;	
					IF _kwota01 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_153>', _kwota12, '</P_153>\n');				
					END IF;	
				END IF;  

-- sekcji C.13 i C.14 tez nie mamy
-- sekcja C.15
				SET _ile = 0; SET @poz.rodzaj = 3;
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_178>', _kwota01, '</P_178>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_179>', _kwota02, '</P_179>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_180>', _kwota03, '</P_180>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_181>', _kwota04, '</P_181>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_182>', _kwota05, '</P_182>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_183>', _kwota06, '</P_183>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_184>', _kwota07, '</P_184>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_185>', _kwota08, '</P_185>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_186>', _kwota09, '</P_186>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_187>', _kwota10, '</P_187>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_188>', _kwota11, '</P_188>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_189>', _kwota12, '</P_189>\n');				
				END IF;  

-- sekcji C.16 - C.25 tez nie mamy
-- sekcja C.26
				SET _ile = 0; SET @poz.rodzaj = 26;
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_310>', _kwota01, '</P_310>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_311>', _kwota02, '</P_311>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_312>', _kwota03, '</P_312>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_313>', _kwota04, '</P_313>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_314>', _kwota05, '</P_314>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_315>', _kwota06, '</P_315>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_316>', _kwota07, '</P_316>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_317>', _kwota08, '</P_317>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_318>', _kwota09, '</P_318>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_319>', _kwota10, '</P_319>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_320>', _kwota11, '</P_320>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_321>', _kwota12, '</P_321>\n'); 
				END IF;  
				
-- sekcja C.27 - sumowanie (rodzaj 14 jest juz suma, do tego rodzaj 26)
				SET _ile = 0;
				
					SELECT 
						SUM(ifnull(kwota01,0)), SUM(ifnull(kwota02,0)), SUM(ifnull(kwota03,0)), 
						SUM(ifnull(kwota04,0)), SUM(ifnull(kwota05,0)), SUM(ifnull(kwota06,0)),
						SUM(ifnull(kwota07,0)), SUM(ifnull(kwota08,0)), SUM(ifnull(kwota09,0)), 
						SUM(ifnull(kwota10,0)), SUM(ifnull(kwota11,0)), SUM(ifnull(kwota12,0))
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj IN (14, 26)
					;

					SET @pit8ar.k01 = _kwota01;		
					SET @pit8ar.k02 = _kwota02;		
					SET @pit8ar.k03 = _kwota03;		
					SET @pit8ar.k04 = _kwota04;		
					SET @pit8ar.k05 = _kwota05;		
					SET @pit8ar.k06 = _kwota06;		
					SET @pit8ar.k07 = _kwota07;		
					SET @pit8ar.k08 = _kwota08;		
					SET @pit8ar.k09 = _kwota09;		
					SET @pit8ar.k10 = _kwota10;		
					SET @pit8ar.k11 = _kwota11;		
					SET @pit8ar.k12 = _kwota12;		
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_322>', _varDOUBLE_NOTNULL(_kwota01), '</P_322>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_323>', _varDOUBLE_NOTNULL(_kwota02), '</P_323>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_324>', _varDOUBLE_NOTNULL(_kwota03), '</P_324>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_325>', _varDOUBLE_NOTNULL(_kwota04), '</P_325>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_326>', _varDOUBLE_NOTNULL(_kwota05), '</P_326>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_327>', _varDOUBLE_NOTNULL(_kwota06), '</P_327>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_328>', _varDOUBLE_NOTNULL(_kwota07), '</P_328>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_329>', _varDOUBLE_NOTNULL(_kwota08), '</P_329>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_330>', _varDOUBLE_NOTNULL(_kwota09), '</P_330>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_331>', _varDOUBLE_NOTNULL(_kwota10), '</P_331>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_332>', _varDOUBLE_NOTNULL(_kwota11), '</P_332>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_333>', _varDOUBLE_NOTNULL(_kwota12), '</P_333>\n');


-- sekcja C.28 
				SET _ile = 0;				
				SELECT 
					IFNULL(wynagr01, 0) + IFNULL(wynagr02, 0) +  IFNULL(wynagr03, 0) + IFNULL(wynagr04, 0) + 
					IFNULL(wynagr05, 0) + IFNULL(wynagr06, 0) +  IFNULL(wynagr07, 0) + IFNULL(wynagr08, 0) + 	
					IFNULL(wynagr09, 0) + IFNULL(wynagr10, 0) +  IFNULL(wynagr11, 0) + IFNULL(wynagr12, 0) 
				AS ile
				INTO _ile
				FROM pit_8ar
				WHERE GUID = _GUID
				;
				
				IF _ile > 0 THEN
					SELECT 
						IFNULL(wynagr01, 0), IFNULL(wynagr02, 0), IFNULL(wynagr03, 0), IFNULL(wynagr04, 0),
						IFNULL(wynagr05, 0), IFNULL(wynagr06, 0), IFNULL(wynagr07, 0), IFNULL(wynagr08, 0),
						IFNULL(wynagr09, 0), IFNULL(wynagr10, 0), IFNULL(wynagr11, 0), IFNULL(wynagr12, 0)
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar
					WHERE GUID = _GUID  
					;
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_334>', _kwota01, '</P_334>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_335>', _kwota02, '</P_335>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_336>', _kwota03, '</P_336>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_337>', _kwota04, '</P_337>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_338>', _kwota05, '</P_338>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_339>', _kwota06, '</P_339>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_340>', _kwota07, '</P_340>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_341>', _kwota08, '</P_341>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_342>', _kwota09, '</P_342>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_343>', _kwota10, '</P_343>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_344>', _kwota11, '</P_344>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_345>', _kwota12, '</P_345>\n');				 

					SET @pit8ar.k01 = GREATEST(@pit8ar.k01 - _kwota01, 0);					
					SET @pit8ar.k02 = GREATEST(@pit8ar.k02 - _kwota02, 0);					
					SET @pit8ar.k03 = GREATEST(@pit8ar.k03 - _kwota03, 0);					
					SET @pit8ar.k04 = GREATEST(@pit8ar.k04 - _kwota04, 0);					
					SET @pit8ar.k05 = GREATEST(@pit8ar.k05 - _kwota05, 0);					
					SET @pit8ar.k06 = GREATEST(@pit8ar.k06 - _kwota06, 0);					
					SET @pit8ar.k07 = GREATEST(@pit8ar.k07 - _kwota07, 0);					
					SET @pit8ar.k08 = GREATEST(@pit8ar.k08 - _kwota08, 0);					
					SET @pit8ar.k09 = GREATEST(@pit8ar.k09 - _kwota09, 0);					
					SET @pit8ar.k10 = GREATEST(@pit8ar.k10 - _kwota10, 0);					
					SET @pit8ar.k11 = GREATEST(@pit8ar.k11 - _kwota11, 0);					
					SET @pit8ar.k12 = GREATEST(@pit8ar.k12 - _kwota12, 0);					
				END IF;  
				
				
-- sekcja C.29
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_346>', _varDOUBLE_NOTNULL(@pit8ar.k01), '</P_346>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_347>', _varDOUBLE_NOTNULL(@pit8ar.k02), '</P_347>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_348>', _varDOUBLE_NOTNULL(@pit8ar.k03), '</P_348>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_349>', _varDOUBLE_NOTNULL(@pit8ar.k04), '</P_349>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_350>', _varDOUBLE_NOTNULL(@pit8ar.k05), '</P_350>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_351>', _varDOUBLE_NOTNULL(@pit8ar.k06), '</P_351>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_352>', _varDOUBLE_NOTNULL(@pit8ar.k07), '</P_352>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_353>', _varDOUBLE_NOTNULL(@pit8ar.k08), '</P_353>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_354>', _varDOUBLE_NOTNULL(@pit8ar.k09), '</P_354>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_355>', _varDOUBLE_NOTNULL(@pit8ar.k10), '</P_355>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_356>', _varDOUBLE_NOTNULL(@pit8ar.k11), '</P_356>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_357>', _varDOUBLE_NOTNULL(@pit8ar.k12), '</P_357>\n');				 
			
				SET @xml.IC = @xml.IC - 1;	
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '</PozycjeSzczegolowe>\n');
			
		SET @ePIT.poz = @ePIT.xml;
			
		SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Pouczenia>1</Pouczenia>\n');
		
		IF (@ePIT.CelZlozenia = cKorekta)  AND (LENGTH(@_przyczyna_ORD_ZU) > 0) THEN   
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Zalaczniki>\n');									
			CALL pit_tools('pit.xml.insert.ORD_ZU', '', '', '', '', '', '3');						
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '</Zalaczniki>\n');				
		END IF;
																	   						
		SET @ePIT.xml = CONCAT(@ePIT.xml, '</Deklaracja>');

		CALL e_doc_tools('e_doc_po.insert.or.edit', _Param_A, IFNULL(@ePIT.xml, ''), 'CREATE_XML', 'ORYG', '', '', '', '');
						 
END IF; 


IF _MODE = 'pit8ar.xml.create.WERSJA9_bad' THEN
		
	SELECT  
		d.ID, p.przyczyna_ORD_ZU
		INTO _e_Doc_Def_ID, @_przyczyna_ORD_ZU
	FROM 	
		pit_8ar p, e_doc_def d 
	WHERE	
		p.GUID = _GUID AND  p.e_Doc_Def_ID = d.owner_doc_Id
	;

SET @test1=1;

	SET @ePIT.xml = '';
	SET @xml.IC = 1;
		
	SET @ePIT.xml = '<?xml version="1.0" encoding="UTF-8" ?>\n';	
								
	SET @ePIT.xml = CONCAT(@ePIT.xml, '<Deklaracja xmlns="', _e_Doc_Def_ID, '" ');
	SET @ePIT.xml = CONCAT(@ePIT.xml, 'xmlns:etd="http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2020/03/11/eD/DefinicjeTypy/">', '\n');
	SET @ePIT.xmlns = @ePIT.xml; 	

		CALL pit_tools('pit.xml.insert.header', _GUID, @ePIT.xml, 'pit_8ar', 'P', '', '');
				
		SET @ePIT.header = @ePIT.xml;
		
		CALL pit_tools('pit.xml.insert.podmiot1', '', '', 'Płatnik', '', '', '');

			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<PozycjeSzczegolowe>\n');
				SET @xml.IC = @xml.IC + 1;

				SELECT RodzajKorekty
				INTO _RodzajKorekty
				FROM pit_8ar
				WHERE GUID = _GUID
				;
				
				IF _RodzajKorekty > 0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_7>', _RodzajKorekty, '</P_7>\n');
				END IF;
				
				SET _ile = 0; SET @poz.rodzaj = 2;
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_10>', _kwota01, '</P_10>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_11>', _kwota02, '</P_11>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_12>', _kwota03, '</P_12>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_13>', _kwota04, '</P_13>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_14>', _kwota05, '</P_14>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_15>', _kwota06, '</P_15>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_16>', _kwota07, '</P_16>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_17>', _kwota08, '</P_17>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_18>', _kwota09, '</P_18>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_19>', _kwota10, '</P_19>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_20>', _kwota11, '</P_20>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_21>', _kwota12, '</P_21>\n');				
				END IF;  	
				
				
				SET _ile = 0; SET @poz.rodzaj = 5;
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_22>', _kwota01, '</P_22>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_23>', _kwota02, '</P_23>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_24>', _kwota03, '</P_24>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_25>', _kwota04, '</P_25>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_26>', _kwota05, '</P_26>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_27>', _kwota06, '</P_27>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_28>', _kwota07, '</P_28>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_29>', _kwota08, '</P_29>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_30>', _kwota09, '</P_30>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_31>', _kwota10, '</P_31>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_32>', _kwota11, '</P_32>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_33>', _kwota12, '</P_33>\n');				
				END IF;  
				


				SET _ile = 0; SET @poz.rodzaj = 4;
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_34>', _kwota01, '</P_34>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_35>', _kwota02, '</P_35>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_36>', _kwota03, '</P_36>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_37>', _kwota04, '</P_37>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_38>', _kwota05, '</P_38>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_39>', _kwota06, '</P_39>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_40>', _kwota07, '</P_40>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_41>', _kwota08, '</P_41>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_42>', _kwota09, '</P_42>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_43>', _kwota10, '</P_43>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_44>', _kwota11, '</P_44>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_45>', _kwota12, '</P_45>\n');				
				END IF;  
				


				SET _ile = 0; SET @poz.rodzaj = 777;  
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
					INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 		
					IF _kwota02 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_142>', _kwota01, '</P_142>\n');				
					END IF;	
					IF _kwota03 > 0 THEN							
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_143>', _kwota02, '</P_143>\n');							
					END IF;		
					IF _kwota04 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_144>', _kwota03, '</P_144>\n');				
					END IF;		
					IF _kwota05 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_145>', _kwota04, '</P_145>\n');									
					END IF;		
					IF _kwota06 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_146>', _kwota05, '</P_146>\n');				
					END IF;	
					IF _kwota07 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_147>', _kwota06, '</P_147>\n');									
					END IF;	
					IF _kwota08 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_148>', _kwota07, '</P_148>\n');				
					END IF;	
					IF _kwota09 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_149>', _kwota08, '</P_149>\n');									
					END IF;	
					IF _kwota10 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_150>', _kwota09, '</P_150>\n');				
					END IF;	
					IF _kwota11 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_151>', _kwota10, '</P_151>\n');									
					END IF;	
					IF _kwota12 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_152>', _kwota11, '</P_152>\n');									
					END IF;	
					IF _kwota01 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_153>', _kwota12, '</P_153>\n');				
					END IF;	
				END IF;  



				SET _ile = 0; SET @poz.rodzaj = 3;
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;

					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_178>', _kwota01, '</P_178>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_179>', _kwota02, '</P_179>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_180>', _kwota03, '</P_180>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_181>', _kwota04, '</P_181>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_182>', _kwota05, '</P_182>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_183>', _kwota06, '</P_183>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_184>', _kwota07, '</P_184>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_185>', _kwota08, '</P_185>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_186>', _kwota09, '</P_186>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_187>', _kwota10, '</P_187>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_188>', _kwota11, '</P_188>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_189>', _kwota12, '</P_189>\n');				
				END IF;  



				SET _ile = 0; SET @poz.rodzaj = 26;
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_382>', _kwota01, '</P_382>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_383>', _kwota02, '</P_383>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_384>', _kwota03, '</P_384>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_385>', _kwota04, '</P_385>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_386>', _kwota05, '</P_386>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_387>', _kwota06, '</P_387>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_388>', _kwota07, '</P_388>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_389>', _kwota08, '</P_389>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_390>', _kwota09, '</P_390>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_391>', _kwota10, '</P_391>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_392>', _kwota11, '</P_392>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_393>', _kwota12, '</P_393>\n'); 
				END IF;  
				

				SET _ile = 0;
				
					SELECT 
						SUM(IFNULL(kwota01,0)), SUM(IFNULL(kwota02,0)), SUM(IFNULL(kwota03,0)), 
						SUM(IFNULL(kwota04,0)), SUM(IFNULL(kwota05,0)), SUM(IFNULL(kwota06,0)),
						SUM(IFNULL(kwota07,0)), SUM(IFNULL(kwota08,0)), SUM(IFNULL(kwota09,0)), 
						SUM(IFNULL(kwota10,0)), SUM(IFNULL(kwota11,0)), SUM(IFNULL(kwota12,0))
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj IN (14, 26)
					;

					SET @pit8ar.k01 = _kwota01;		
					SET @pit8ar.k02 = _kwota02;		
					SET @pit8ar.k03 = _kwota03;		
					SET @pit8ar.k04 = _kwota04;		
					SET @pit8ar.k05 = _kwota05;		
					SET @pit8ar.k06 = _kwota06;		
					SET @pit8ar.k07 = _kwota07;		
					SET @pit8ar.k08 = _kwota08;		
					SET @pit8ar.k09 = _kwota09;		
					SET @pit8ar.k10 = _kwota10;		
					SET @pit8ar.k11 = _kwota11;		
					SET @pit8ar.k12 = _kwota12;		
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_394>', _varDOUBLE_NOTNULL(_kwota01), '</P_394>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_395>', _varDOUBLE_NOTNULL(_kwota02), '</P_395>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_396>', _varDOUBLE_NOTNULL(_kwota03), '</P_396>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_397>', _varDOUBLE_NOTNULL(_kwota04), '</P_397>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_398>', _varDOUBLE_NOTNULL(_kwota05), '</P_398>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_399>', _varDOUBLE_NOTNULL(_kwota06), '</P_399>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_400>', _varDOUBLE_NOTNULL(_kwota07), '</P_400>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_401>', _varDOUBLE_NOTNULL(_kwota08), '</P_401>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_402>', _varDOUBLE_NOTNULL(_kwota09), '</P_402>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_403>', _varDOUBLE_NOTNULL(_kwota10), '</P_403>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_404>', _varDOUBLE_NOTNULL(_kwota11), '</P_404>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_405>', _varDOUBLE_NOTNULL(_kwota12), '</P_405>\n');



				SET _ile = 0;				
				SELECT 
					IFNULL(wynagr01, 0) + IFNULL(wynagr02, 0) +  IFNULL(wynagr03, 0) + IFNULL(wynagr04, 0) + 
					IFNULL(wynagr05, 0) + IFNULL(wynagr06, 0) +  IFNULL(wynagr07, 0) + IFNULL(wynagr08, 0) + 	
					IFNULL(wynagr09, 0) + IFNULL(wynagr10, 0) +  IFNULL(wynagr11, 0) + IFNULL(wynagr12, 0) 
				AS ile
				INTO _ile
				FROM pit_8ar
				WHERE GUID = _GUID
				;
				
				IF _ile > 0 THEN
					SELECT 
						IFNULL(wynagr01, 0), IFNULL(wynagr02, 0), IFNULL(wynagr03, 0), IFNULL(wynagr04, 0),
						IFNULL(wynagr05, 0), IFNULL(wynagr06, 0), IFNULL(wynagr07, 0), IFNULL(wynagr08, 0),
						IFNULL(wynagr09, 0), IFNULL(wynagr10, 0), IFNULL(wynagr11, 0), IFNULL(wynagr12, 0)
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar
					WHERE GUID = _GUID  
					;
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_406>', _kwota01, '</P_406>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_407>', _kwota02, '</P_407>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_408>', _kwota03, '</P_408>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_409>', _kwota04, '</P_409>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_410>', _kwota05, '</P_410>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_411>', _kwota06, '</P_411>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_412>', _kwota07, '</P_412>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_413>', _kwota08, '</P_413>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_414>', _kwota09, '</P_414>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_415>', _kwota10, '</P_415>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_416>', _kwota11, '</P_416>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_417>', _kwota12, '</P_417>\n');				 

					SET @pit8ar.k01 = GREATEST(@pit8ar.k01 - _kwota01, 0);					
					SET @pit8ar.k02 = GREATEST(@pit8ar.k02 - _kwota02, 0);					
					SET @pit8ar.k03 = GREATEST(@pit8ar.k03 - _kwota03, 0);					
					SET @pit8ar.k04 = GREATEST(@pit8ar.k04 - _kwota04, 0);					
					SET @pit8ar.k05 = GREATEST(@pit8ar.k05 - _kwota05, 0);					
					SET @pit8ar.k06 = GREATEST(@pit8ar.k06 - _kwota06, 0);					
					SET @pit8ar.k07 = GREATEST(@pit8ar.k07 - _kwota07, 0);					
					SET @pit8ar.k08 = GREATEST(@pit8ar.k08 - _kwota08, 0);					
					SET @pit8ar.k09 = GREATEST(@pit8ar.k09 - _kwota09, 0);					
					SET @pit8ar.k10 = GREATEST(@pit8ar.k10 - _kwota10, 0);					
					SET @pit8ar.k11 = GREATEST(@pit8ar.k11 - _kwota11, 0);					
					SET @pit8ar.k12 = GREATEST(@pit8ar.k12 - _kwota12, 0);					
				END IF;  
				
				

					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_418>', _varDOUBLE_NOTNULL(@pit8ar.k01), '</P_418>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_419>', _varDOUBLE_NOTNULL(@pit8ar.k02), '</P_419>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_420>', _varDOUBLE_NOTNULL(@pit8ar.k03), '</P_420>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_421>', _varDOUBLE_NOTNULL(@pit8ar.k04), '</P_421>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_422>', _varDOUBLE_NOTNULL(@pit8ar.k05), '</P_422>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_423>', _varDOUBLE_NOTNULL(@pit8ar.k06), '</P_423>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_424>', _varDOUBLE_NOTNULL(@pit8ar.k07), '</P_424>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_425>', _varDOUBLE_NOTNULL(@pit8ar.k08), '</P_425>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_426>', _varDOUBLE_NOTNULL(@pit8ar.k09), '</P_426>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_427>', _varDOUBLE_NOTNULL(@pit8ar.k10), '</P_427>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_428>', _varDOUBLE_NOTNULL(@pit8ar.k11), '</P_428>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_429>', _varDOUBLE_NOTNULL(@pit8ar.k12), '</P_429>\n');	
					
					
					
				SELECT IFNULL(valueB,0) INTO @covid_III_2020 FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_III_2020";
				SELECT IFNULL(valueB,0) INTO @covid_IV_2020 FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_IV_2020";
				SELECT IFNULL(valueB,0) INTO @covid_V_2020 FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_V_2020";
				
				IF @covid_III_2020>0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_430>', @covid_III_2020, '</P_430>\n');
				END IF;
					
				IF @covid_IV_2020>0 THEN								
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_431>', @covid_IV_2020, '</P_431>\n');				
				END IF;
									
				IF @covid_V_2020>0 THEN
				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_432>', @covid_V_2020, '</P_432>\n');
				END IF;
				
				SET @ePIT.poz = @ePIT.xml;			 
			
				SET @xml.IC = @xml.IC - 1;	
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '</PozycjeSzczegolowe>\n');
			
		SET @ePIT.poz = @ePIT.xml;
			
		SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Pouczenia>1</Pouczenia>\n');
		
		IF (@ePIT.CelZlozenia = cKorekta)  AND (LENGTH(@_przyczyna_ORD_ZU) > 0) THEN   
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Zalaczniki>\n');									
			CALL pit_tools('pit.xml.insert.ORD_ZU', '', '', '', '', '', '4');						
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '</Zalaczniki>\n');				
		END IF;
																	   						
		SET @ePIT.xml = CONCAT(@ePIT.xml, '</Deklaracja>');

		CALL e_doc_tools('e_doc_po.insert.or.edit', _Param_A, IFNULL(@ePIT.xml, ''), 'CREATE_XML', 'ORYG', '', '', '', '');
						 
END IF; 

IF _MODE = 'pit8ar.xml.create.202001' THEN
		
	SELECT  
		d.ID, p.przyczyna_ORD_ZU
		INTO _e_Doc_Def_ID, @_przyczyna_ORD_ZU
	FROM 	
		pit_8ar p, e_doc_def d 
	WHERE	
		p.GUID = _GUID AND  p.e_Doc_Def_ID = d.owner_doc_Id
	;

SET @test1=1;

	SET @ePIT.xml = '';
	SET @xml.IC = 1;
		
	SET @ePIT.xml = '<?xml version="1.0" encoding="UTF-8" ?>\n';	
								
	SET @ePIT.xml = CONCAT(@ePIT.xml, '<Deklaracja xmlns="', _e_Doc_Def_ID, '" ');
	SET @ePIT.xml = CONCAT(@ePIT.xml, 'xmlns:etd="http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2020/03/11/eD/DefinicjeTypy/">', '\n');
	SET @ePIT.xmlns = @ePIT.xml; 	

		CALL pit_tools('pit.xml.insert.header', _GUID, @ePIT.xml, 'pit_8ar', 'P', '', '');
				
		SET @ePIT.header = @ePIT.xml;
		
		CALL pit_tools('pit.xml.insert.podmiot1', '', '', 'Płatnik', '', '', '');

			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<PozycjeSzczegolowe>\n');
				SET @xml.IC = @xml.IC + 1;

				SELECT RodzajKorekty
				INTO _RodzajKorekty
				FROM pit_8ar
				WHERE GUID = _GUID
				;
				
				IF _RodzajKorekty > 0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_7>', _RodzajKorekty, '</P_7>\n');
				END IF;
				
				SET _ile = 0; SET @poz.rodzaj = 2;
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
					INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_10>', _kwota01, '</P_10>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_11>', _kwota02, '</P_11>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_12>', _kwota03, '</P_12>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_13>', _kwota04, '</P_13>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_14>', _kwota05, '</P_14>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_15>', _kwota06, '</P_15>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_16>', _kwota07, '</P_16>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_17>', _kwota08, '</P_17>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_18>', _kwota09, '</P_18>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_19>', _kwota10, '</P_19>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_20>', _kwota11, '</P_20>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_21>', _kwota12, '</P_21>\n');				
				END IF;  	
				
				
				SET _ile = 0; SET @poz.rodzaj = 5;
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_22>', _kwota01, '</P_22>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_23>', _kwota02, '</P_23>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_24>', _kwota03, '</P_24>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_25>', _kwota04, '</P_25>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_26>', _kwota05, '</P_26>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_27>', _kwota06, '</P_27>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_28>', _kwota07, '</P_28>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_29>', _kwota08, '</P_29>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_30>', _kwota09, '</P_30>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_31>', _kwota10, '</P_31>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_32>', _kwota11, '</P_32>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_33>', _kwota12, '</P_33>\n');				
				END IF;  
				


				SET _ile = 0; SET @poz.rodzaj = 4;
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
					INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_34>', _kwota01, '</P_34>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_35>', _kwota02, '</P_35>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_36>', _kwota03, '</P_36>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_37>', _kwota04, '</P_37>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_38>', _kwota05, '</P_38>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_39>', _kwota06, '</P_39>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_40>', _kwota07, '</P_40>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_41>', _kwota08, '</P_41>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_42>', _kwota09, '</P_42>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_43>', _kwota10, '</P_43>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_44>', _kwota11, '</P_44>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_45>', _kwota12, '</P_45>\n');				
				END IF;  
				


				SET _ile = 0; SET @poz.rodzaj = 777;  
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 		
					IF _kwota02 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_142>', _kwota01, '</P_142>\n');				
					END IF;	
					IF _kwota03 > 0 THEN							
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_143>', _kwota02, '</P_143>\n');							
					END IF;		
					IF _kwota04 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_144>', _kwota03, '</P_144>\n');				
					END IF;		
					IF _kwota05 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_145>', _kwota04, '</P_145>\n');									
					END IF;		
					IF _kwota06 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_146>', _kwota05, '</P_146>\n');				
					END IF;	
					IF _kwota07 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_147>', _kwota06, '</P_147>\n');									
					END IF;	
					IF _kwota08 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_148>', _kwota07, '</P_148>\n');				
					END IF;	
					IF _kwota09 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_149>', _kwota08, '</P_149>\n');									
					END IF;	
					IF _kwota10 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_150>', _kwota09, '</P_150>\n');				
					END IF;	
					IF _kwota11 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_151>', _kwota10, '</P_151>\n');									
					END IF;	
					IF _kwota12 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_152>', _kwota11, '</P_152>\n');									
					END IF;	
					IF _kwota01 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_153>', _kwota12, '</P_153>\n');				
					END IF;	
				END IF;  



				SET _ile = 0; SET @poz.rodzaj = 3;
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_178>', _kwota01, '</P_178>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_179>', _kwota02, '</P_179>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_180>', _kwota03, '</P_180>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_181>', _kwota04, '</P_181>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_182>', _kwota05, '</P_182>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_183>', _kwota06, '</P_183>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_184>', _kwota07, '</P_184>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_185>', _kwota08, '</P_185>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_186>', _kwota09, '</P_186>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_187>', _kwota10, '</P_187>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_188>', _kwota11, '</P_188>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_189>', _kwota12, '</P_189>\n');				
				END IF;  



				SET _ile = 0; SET @poz.rodzaj = 26;
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_382>', _kwota01, '</P_382>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_383>', _kwota02, '</P_383>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_384>', _kwota03, '</P_384>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_385>', _kwota04, '</P_385>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_386>', _kwota05, '</P_386>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_387>', _kwota06, '</P_387>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_388>', _kwota07, '</P_388>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_389>', _kwota08, '</P_389>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_390>', _kwota09, '</P_390>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_391>', _kwota10, '</P_391>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_392>', _kwota11, '</P_392>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_393>', _kwota12, '</P_393>\n'); 
				END IF;  
				

				SET _ile = 0;
				
					SELECT 
						SUM(IFNULL(kwota01,0)), SUM(IFNULL(kwota02,0)), SUM(IFNULL(kwota03,0)), 
						SUM(IFNULL(kwota04,0)), SUM(IFNULL(kwota05,0)), SUM(IFNULL(kwota06,0)),
						SUM(IFNULL(kwota07,0)), SUM(IFNULL(kwota08,0)), SUM(IFNULL(kwota09,0)), 
						SUM(IFNULL(kwota10,0)), SUM(IFNULL(kwota11,0)), SUM(IFNULL(kwota12,0))
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj IN (14)
					;

					SET @pit8ar.k01 = _kwota01;		
					SET @pit8ar.k02 = _kwota02;		
					SET @pit8ar.k03 = _kwota03;		
					SET @pit8ar.k04 = _kwota04;		
					SET @pit8ar.k05 = _kwota05;		
					SET @pit8ar.k06 = _kwota06;		
					SET @pit8ar.k07 = _kwota07;		
					SET @pit8ar.k08 = _kwota08;		
					SET @pit8ar.k09 = _kwota09;		
					SET @pit8ar.k10 = _kwota10;		
					SET @pit8ar.k11 = _kwota11;		
					SET @pit8ar.k12 = _kwota12;		
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_394>', _varDOUBLE_NOTNULL(_kwota01), '</P_394>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_395>', _varDOUBLE_NOTNULL(_kwota02), '</P_395>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_396>', _varDOUBLE_NOTNULL(_kwota03), '</P_396>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_397>', _varDOUBLE_NOTNULL(_kwota04), '</P_397>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_398>', _varDOUBLE_NOTNULL(_kwota05), '</P_398>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_399>', _varDOUBLE_NOTNULL(_kwota06), '</P_399>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_400>', _varDOUBLE_NOTNULL(_kwota07), '</P_400>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_401>', _varDOUBLE_NOTNULL(_kwota08), '</P_401>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_402>', _varDOUBLE_NOTNULL(_kwota09), '</P_402>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_403>', _varDOUBLE_NOTNULL(_kwota10), '</P_403>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_404>', _varDOUBLE_NOTNULL(_kwota11), '</P_404>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_405>', _varDOUBLE_NOTNULL(_kwota12), '</P_405>\n');



				SET _ile = 0;				
				SELECT 
					IFNULL(wynagr01, 0) + IFNULL(wynagr02, 0) +  IFNULL(wynagr03, 0) + IFNULL(wynagr04, 0) + 
					IFNULL(wynagr05, 0) + IFNULL(wynagr06, 0) +  IFNULL(wynagr07, 0) + IFNULL(wynagr08, 0) + 	
					IFNULL(wynagr09, 0) + IFNULL(wynagr10, 0) +  IFNULL(wynagr11, 0) + IFNULL(wynagr12, 0) 
				AS ile
				INTO _ile
				FROM pit_8ar
				WHERE GUID = _GUID
				;
				
				IF _ile > 0 THEN
					SELECT 
						IFNULL(wynagr01, 0), IFNULL(wynagr02, 0), IFNULL(wynagr03, 0), IFNULL(wynagr04, 0),
						IFNULL(wynagr05, 0), IFNULL(wynagr06, 0), IFNULL(wynagr07, 0), IFNULL(wynagr08, 0),
						IFNULL(wynagr09, 0), IFNULL(wynagr10, 0), IFNULL(wynagr11, 0), IFNULL(wynagr12, 0)
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar
					WHERE GUID = _GUID  
					;
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_406>', _kwota01, '</P_406>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_407>', _kwota02, '</P_407>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_408>', _kwota03, '</P_408>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_409>', _kwota04, '</P_409>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_410>', _kwota05, '</P_410>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_411>', _kwota06, '</P_411>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_412>', _kwota07, '</P_412>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_413>', _kwota08, '</P_413>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_414>', _kwota09, '</P_414>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_415>', _kwota10, '</P_415>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_416>', _kwota11, '</P_416>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_417>', _kwota12, '</P_417>\n');				 

					SET @pit8ar.k01 = GREATEST(@pit8ar.k01 - _kwota01, 0);					
					SET @pit8ar.k02 = GREATEST(@pit8ar.k02 - _kwota02, 0);					
					SET @pit8ar.k03 = GREATEST(@pit8ar.k03 - _kwota03, 0);					
					SET @pit8ar.k04 = GREATEST(@pit8ar.k04 - _kwota04, 0);					
					SET @pit8ar.k05 = GREATEST(@pit8ar.k05 - _kwota05, 0);					
					SET @pit8ar.k06 = GREATEST(@pit8ar.k06 - _kwota06, 0);					
					SET @pit8ar.k07 = GREATEST(@pit8ar.k07 - _kwota07, 0);					
					SET @pit8ar.k08 = GREATEST(@pit8ar.k08 - _kwota08, 0);					
					SET @pit8ar.k09 = GREATEST(@pit8ar.k09 - _kwota09, 0);					
					SET @pit8ar.k10 = GREATEST(@pit8ar.k10 - _kwota10, 0);					
					SET @pit8ar.k11 = GREATEST(@pit8ar.k11 - _kwota11, 0);					
					SET @pit8ar.k12 = GREATEST(@pit8ar.k12 - _kwota12, 0);					
				END IF;  
				
				

					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_418>', _varDOUBLE_NOTNULL(@pit8ar.k01), '</P_418>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_419>', _varDOUBLE_NOTNULL(@pit8ar.k02), '</P_419>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_420>', _varDOUBLE_NOTNULL(@pit8ar.k03), '</P_420>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_421>', _varDOUBLE_NOTNULL(@pit8ar.k04), '</P_421>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_422>', _varDOUBLE_NOTNULL(@pit8ar.k05), '</P_422>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_423>', _varDOUBLE_NOTNULL(@pit8ar.k06), '</P_423>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_424>', _varDOUBLE_NOTNULL(@pit8ar.k07), '</P_424>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_425>', _varDOUBLE_NOTNULL(@pit8ar.k08), '</P_425>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_426>', _varDOUBLE_NOTNULL(@pit8ar.k09), '</P_426>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_427>', _varDOUBLE_NOTNULL(@pit8ar.k10), '</P_427>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_428>', _varDOUBLE_NOTNULL(@pit8ar.k11), '</P_428>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_429>', _varDOUBLE_NOTNULL(@pit8ar.k12), '</P_429>\n');	
					
					
					
				SELECT IFNULL(valueB,0) INTO @covid_III_2020 FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_III_2020";
				SELECT IFNULL(valueB,0) INTO @covid_IV_2020 FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_IV_2020";
				SELECT IFNULL(valueB,0) INTO @covid_V_2020 FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_V_2020";
				SELECT IFNULL(valueB,0) INTO @covid_X_2020 FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_X_2020";
				SELECT IFNULL(valueB,0) INTO @covid_XI_2020 FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_XI_2020";
				SELECT IFNULL(valueB,0) INTO @covid_XII_2020 FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_XII_2020";
				
				IF @covid_III_2020>0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_430>', @covid_III_2020, '</P_430>\n');
				END IF;
					
				IF @covid_IV_2020>0 THEN								
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_431>', @covid_IV_2020, '</P_431>\n');				
				END IF;
									
				IF @covid_V_2020>0 THEN
				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_432>', @covid_V_2020, '</P_432>\n');
				END IF;
				
					IF @covid_X_2020>0 THEN
				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_433>', @covid_X_2020, '</P_433>\n');
				END IF;
				
					IF @covid_XI_2020>0 THEN
				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_434>', @covid_XI_2020, '</P_434>\n');
				END IF;
					IF @covid_XII_2020>0 THEN
				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_435>', @covid_XII_2020, '</P_435>\n');
				END IF;
				
				
				
				SET @ePIT.poz = @ePIT.xml;			 
			
				SET @xml.IC = @xml.IC - 1;	
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '</PozycjeSzczegolowe>\n');
			
		SET @ePIT.poz = @ePIT.xml;
			
		SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Pouczenia>1</Pouczenia>\n');
		
		IF (@ePIT.CelZlozenia = cKorekta)  AND (LENGTH(@_przyczyna_ORD_ZU) > 0) THEN   
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Zalaczniki>\n');									
			CALL pit_tools('pit.xml.insert.ORD_ZU', '', '', '', '', '', '4');						
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '</Zalaczniki>\n');				
		END IF;
																	   						
		SET @ePIT.xml = CONCAT(@ePIT.xml, '</Deklaracja>');

		CALL e_doc_tools('e_doc_po.insert.or.edit', _Param_A, IFNULL(@ePIT.xml, ''), 'CREATE_XML', 'ORYG', '', '', '', '');
						 
END IF;  -- pit8ar.xml.create.202001'

IF _MODE = 'pit8ar.xml.create.202104' THEN
		
	SELECT  
		d.ID, p.przyczyna_ORD_ZU
		INTO _e_Doc_Def_ID, @_przyczyna_ORD_ZU
	FROM 	
		pit_8ar p, e_doc_def d 
	WHERE	
		p.GUID = _GUID AND  p.e_Doc_Def_ID = d.owner_doc_Id
	;

SET @test1=1;

	SET @ePIT.xml = '';
	SET @xml.IC = 1;
		
	SET @ePIT.xml = '<?xml version="1.0" encoding="UTF-8" ?>\n';	
								
	SET @ePIT.xml = CONCAT(@ePIT.xml, '<Deklaracja xmlns="', _e_Doc_Def_ID, '" ');
	SET @ePIT.xml = CONCAT(@ePIT.xml, 'xmlns:etd="http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2020/03/11/eD/DefinicjeTypy/">', '\n');
	SET @ePIT.xmlns = @ePIT.xml; 	

		CALL pit_tools('pit.xml.insert.header', _GUID, @ePIT.xml, 'pit_8ar', 'P', '', '');
				
		SET @ePIT.header = @ePIT.xml;
		
		CALL pit_tools('pit.xml.insert.podmiot1', '', '', 'Płatnik', '', '', '');

			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<PozycjeSzczegolowe>\n');
				SET @xml.IC = @xml.IC + 1;

				SELECT RodzajKorekty
				INTO _RodzajKorekty
				FROM pit_8ar
				WHERE GUID = _GUID
				;
				
				IF _RodzajKorekty > 0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_7>', _RodzajKorekty, '</P_7>\n');
				END IF;
				
				SET _ile = 0; SET @poz.rodzaj = 2;
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
					INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_10>', _kwota01, '</P_10>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_11>', _kwota02, '</P_11>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_12>', _kwota03, '</P_12>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_13>', _kwota04, '</P_13>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_14>', _kwota05, '</P_14>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_15>', _kwota06, '</P_15>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_16>', _kwota07, '</P_16>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_17>', _kwota08, '</P_17>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_18>', _kwota09, '</P_18>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_19>', _kwota10, '</P_19>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_20>', _kwota11, '</P_20>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_21>', _kwota12, '</P_21>\n');				
				END IF;  	
				
				
				SET _ile = 0; SET @poz.rodzaj = 5;
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_22>', _kwota01, '</P_22>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_23>', _kwota02, '</P_23>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_24>', _kwota03, '</P_24>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_25>', _kwota04, '</P_25>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_26>', _kwota05, '</P_26>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_27>', _kwota06, '</P_27>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_28>', _kwota07, '</P_28>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_29>', _kwota08, '</P_29>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_30>', _kwota09, '</P_30>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_31>', _kwota10, '</P_31>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_32>', _kwota11, '</P_32>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_33>', _kwota12, '</P_33>\n');				
				END IF;  
				


				SET _ile = 0; SET @poz.rodzaj = 4;
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_34>', _kwota01, '</P_34>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_35>', _kwota02, '</P_35>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_36>', _kwota03, '</P_36>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_37>', _kwota04, '</P_37>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_38>', _kwota05, '</P_38>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_39>', _kwota06, '</P_39>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_40>', _kwota07, '</P_40>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_41>', _kwota08, '</P_41>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_42>', _kwota09, '</P_42>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_43>', _kwota10, '</P_43>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_44>', _kwota11, '</P_44>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_45>', _kwota12, '</P_45>\n');				
				END IF;  
				


				SET _ile = 0; SET @poz.rodzaj = 777;  
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 		
					IF _kwota02 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_142>', _kwota01, '</P_142>\n');				
					END IF;	
					IF _kwota03 > 0 THEN							
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_143>', _kwota02, '</P_143>\n');							
					END IF;		
					IF _kwota04 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_144>', _kwota03, '</P_144>\n');				
					END IF;		
					IF _kwota05 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_145>', _kwota04, '</P_145>\n');									
					END IF;		
					IF _kwota06 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_146>', _kwota05, '</P_146>\n');				
					END IF;	
					IF _kwota07 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_147>', _kwota06, '</P_147>\n');									
					END IF;	
					IF _kwota08 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_148>', _kwota07, '</P_148>\n');				
					END IF;	
					IF _kwota09 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_149>', _kwota08, '</P_149>\n');									
					END IF;	
					IF _kwota10 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_150>', _kwota09, '</P_150>\n');				
					END IF;	
					IF _kwota11 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_151>', _kwota10, '</P_151>\n');									
					END IF;	
					IF _kwota12 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_152>', _kwota11, '</P_152>\n');									
					END IF;	
					IF _kwota01 > 0 THEN								
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_153>', _kwota12, '</P_153>\n');				
					END IF;	
				END IF;  



				SET _ile = 0; SET @poz.rodzaj = 3;
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_178>', _kwota01, '</P_178>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_179>', _kwota02, '</P_179>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_180>', _kwota03, '</P_180>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_181>', _kwota04, '</P_181>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_182>', _kwota05, '</P_182>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_183>', _kwota06, '</P_183>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_184>', _kwota07, '</P_184>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_185>', _kwota08, '</P_185>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_186>', _kwota09, '</P_186>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_187>', _kwota10, '</P_187>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_188>', _kwota11, '</P_188>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_189>', _kwota12, '</P_189>\n');				
				END IF;  



				SET _ile = 0; SET @poz.rodzaj = 26;
				
				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;
				
				IF _ile > 0 THEN
					SELECT 
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
					INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj	
					;
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_382>', _kwota01, '</P_382>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_383>', _kwota02, '</P_383>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_384>', _kwota03, '</P_384>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_385>', _kwota04, '</P_385>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_386>', _kwota05, '</P_386>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_387>', _kwota06, '</P_387>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_388>', _kwota07, '</P_388>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_389>', _kwota08, '</P_389>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_390>', _kwota09, '</P_390>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_391>', _kwota10, '</P_391>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_392>', _kwota11, '</P_392>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_393>', _kwota12, '</P_393>\n'); 
				END IF;  
				

				SET _ile = 0;
				
					SELECT 
						SUM(IFNULL(kwota01,0)), SUM(IFNULL(kwota02,0)), SUM(IFNULL(kwota03,0)), 
						SUM(IFNULL(kwota04,0)), SUM(IFNULL(kwota05,0)), SUM(IFNULL(kwota06,0)),
						SUM(IFNULL(kwota07,0)), SUM(IFNULL(kwota08,0)), SUM(IFNULL(kwota09,0)), 
						SUM(IFNULL(kwota10,0)), SUM(IFNULL(kwota11,0)), SUM(IFNULL(kwota12,0))
					INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj IN (14)
					;

					SET @pit8ar.k01 = _kwota01;		
					SET @pit8ar.k02 = _kwota02;		
					SET @pit8ar.k03 = _kwota03;		
					SET @pit8ar.k04 = _kwota04;		
					SET @pit8ar.k05 = _kwota05;		
					SET @pit8ar.k06 = _kwota06;		
					SET @pit8ar.k07 = _kwota07;		
					SET @pit8ar.k08 = _kwota08;		
					SET @pit8ar.k09 = _kwota09;		
					SET @pit8ar.k10 = _kwota10;		
					SET @pit8ar.k11 = _kwota11;		
					SET @pit8ar.k12 = _kwota12;		
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_394>', _varDOUBLE_NOTNULL(_kwota01), '</P_394>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_395>', _varDOUBLE_NOTNULL(_kwota02), '</P_395>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_396>', _varDOUBLE_NOTNULL(_kwota03), '</P_396>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_397>', _varDOUBLE_NOTNULL(_kwota04), '</P_397>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_398>', _varDOUBLE_NOTNULL(_kwota05), '</P_398>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_399>', _varDOUBLE_NOTNULL(_kwota06), '</P_399>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_400>', _varDOUBLE_NOTNULL(_kwota07), '</P_400>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_401>', _varDOUBLE_NOTNULL(_kwota08), '</P_401>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_402>', _varDOUBLE_NOTNULL(_kwota09), '</P_402>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_403>', _varDOUBLE_NOTNULL(_kwota10), '</P_403>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_404>', _varDOUBLE_NOTNULL(_kwota11), '</P_404>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_405>', _varDOUBLE_NOTNULL(_kwota12), '</P_405>\n');



				SET _ile = 0;				
				SELECT 
					IFNULL(wynagr01, 0) + IFNULL(wynagr02, 0) +  IFNULL(wynagr03, 0) + IFNULL(wynagr04, 0) + 
					IFNULL(wynagr05, 0) + IFNULL(wynagr06, 0) +  IFNULL(wynagr07, 0) + IFNULL(wynagr08, 0) + 	
					IFNULL(wynagr09, 0) + IFNULL(wynagr10, 0) +  IFNULL(wynagr11, 0) + IFNULL(wynagr12, 0) 
				AS ile
				INTO _ile
				FROM pit_8ar
				WHERE GUID = _GUID
				;
				
				IF _ile > 0 THEN
					SELECT 
						IFNULL(wynagr01, 0), IFNULL(wynagr02, 0), IFNULL(wynagr03, 0), IFNULL(wynagr04, 0),
						IFNULL(wynagr05, 0), IFNULL(wynagr06, 0), IFNULL(wynagr07, 0), IFNULL(wynagr08, 0),
						IFNULL(wynagr09, 0), IFNULL(wynagr10, 0), IFNULL(wynagr11, 0), IFNULL(wynagr12, 0)
					INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar
					WHERE GUID = _GUID  
					;
						 					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_406>', _kwota01, '</P_406>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_407>', _kwota02, '</P_407>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_408>', _kwota03, '</P_408>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_409>', _kwota04, '</P_409>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_410>', _kwota05, '</P_410>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_411>', _kwota06, '</P_411>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_412>', _kwota07, '</P_412>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_413>', _kwota08, '</P_413>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_414>', _kwota09, '</P_414>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_415>', _kwota10, '</P_415>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_416>', _kwota11, '</P_416>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_417>', _kwota12, '</P_417>\n');				 

					SET @pit8ar.k01 = GREATEST(@pit8ar.k01 - _kwota01, 0);					
					SET @pit8ar.k02 = GREATEST(@pit8ar.k02 - _kwota02, 0);					
					SET @pit8ar.k03 = GREATEST(@pit8ar.k03 - _kwota03, 0);					
					SET @pit8ar.k04 = GREATEST(@pit8ar.k04 - _kwota04, 0);					
					SET @pit8ar.k05 = GREATEST(@pit8ar.k05 - _kwota05, 0);					
					SET @pit8ar.k06 = GREATEST(@pit8ar.k06 - _kwota06, 0);					
					SET @pit8ar.k07 = GREATEST(@pit8ar.k07 - _kwota07, 0);					
					SET @pit8ar.k08 = GREATEST(@pit8ar.k08 - _kwota08, 0);					
					SET @pit8ar.k09 = GREATEST(@pit8ar.k09 - _kwota09, 0);					
					SET @pit8ar.k10 = GREATEST(@pit8ar.k10 - _kwota10, 0);					
					SET @pit8ar.k11 = GREATEST(@pit8ar.k11 - _kwota11, 0);					
					SET @pit8ar.k12 = GREATEST(@pit8ar.k12 - _kwota12, 0);					
				END IF;  
				
				

					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_418>', _varDOUBLE_NOTNULL(@pit8ar.k01), '</P_418>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_419>', _varDOUBLE_NOTNULL(@pit8ar.k02), '</P_419>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_420>', _varDOUBLE_NOTNULL(@pit8ar.k03), '</P_420>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_421>', _varDOUBLE_NOTNULL(@pit8ar.k04), '</P_421>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_422>', _varDOUBLE_NOTNULL(@pit8ar.k05), '</P_422>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_423>', _varDOUBLE_NOTNULL(@pit8ar.k06), '</P_423>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_424>', _varDOUBLE_NOTNULL(@pit8ar.k07), '</P_424>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_425>', _varDOUBLE_NOTNULL(@pit8ar.k08), '</P_425>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_426>', _varDOUBLE_NOTNULL(@pit8ar.k09), '</P_426>\n');				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_427>', _varDOUBLE_NOTNULL(@pit8ar.k10), '</P_427>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_428>', _varDOUBLE_NOTNULL(@pit8ar.k11), '</P_428>\n');									
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_429>', _varDOUBLE_NOTNULL(@pit8ar.k12), '</P_429>\n');	
					
			/* nie obslugujemy nowej czesci		
					
				SELECT IFNULL(valueB,0) FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_III_2020" INTO @covid_III_2020;
				SELECT IFNULL(valueB,0) FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_IV_2020" INTO @covid_IV_2020;
				SELECT IFNULL(valueB,0) FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_V_2020" INTO @covid_V_2020;
				SELECT IFNULL(valueB,0) FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_X_2020" INTO @covid_X_2020;
				SELECT IFNULL(valueB,0) FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_XI_2020" INTO @covid_XI_2020;
				SELECT IFNULL(valueB,0) FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_XII_2020" INTO @covid_XII_2020;
				
				IF @covid_III_2020>0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_430>', @covid_III_2020, '</P_430>\n');
				END IF;
					
				IF @covid_IV_2020>0 THEN								
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_431>', @covid_IV_2020, '</P_431>\n');				
				END IF;
									
				IF @covid_V_2020>0 THEN
				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_432>', @covid_V_2020, '</P_432>\n');
				END IF;
				
					IF @covid_X_2020>0 THEN
				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_433>', @covid_X_2020, '</P_433>\n');
				END IF;
				
					IF @covid_XI_2020>0 THEN
				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_434>', @covid_XI_2020, '</P_434>\n');
				END IF;
					IF @covid_XII_2020>0 THEN
				
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_435>', @covid_XII_2020, '</P_435>\n');
				END IF;
				
				*/
				
				
				SET @ePIT.poz = @ePIT.xml;			 
			
				SET @xml.IC = @xml.IC - 1;	
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '</PozycjeSzczegolowe>\n');
			
		SET @ePIT.poz = @ePIT.xml;
			
		SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Pouczenia>1</Pouczenia>\n');
		
		IF (@ePIT.CelZlozenia = cKorekta)  AND (LENGTH(@_przyczyna_ORD_ZU) > 0) THEN   
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Zalaczniki>\n');									
			CALL pit_tools('pit.xml.insert.ORD_ZU', '', '', '', '', '', '4');						
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '</Zalaczniki>\n');				
		END IF;
																	   						
		SET @ePIT.xml = CONCAT(@ePIT.xml, '</Deklaracja>');

		CALL e_doc_tools('e_doc_po.insert.or.edit', _Param_A, IFNULL(@ePIT.xml, ''), 'CREATE_XML', 'ORYG', '', '', '', '');
						 
END IF;  -- pit8ar.xml.create.202104'


IF _MODE = 'pit8ar.xml.create.202204' THEN

	SELECT
		d.ID, p.przyczyna_ORD_ZU
		INTO _e_Doc_Def_ID, @_przyczyna_ORD_ZU
	FROM
		pit_8ar p, e_doc_def d
	WHERE
		p.GUID = _GUID AND  p.e_Doc_Def_ID = d.owner_doc_Id
	;

SET @test1=1;

	SET @ePIT.xml = '';
	SET @xml.IC = 1;

	SET @ePIT.xml = '<?xml version="1.0" encoding="UTF-8" ?>\n';

	SET @ePIT.xml = CONCAT(@ePIT.xml, '<Deklaracja xmlns="', _e_Doc_Def_ID, '" ');
	SET @ePIT.xml = CONCAT(@ePIT.xml, 'xmlns:etd="http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2022/01/05/eD/DefinicjeTypy/">', '\n');
	SET @ePIT.xmlns = @ePIT.xml;

		CALL pit_tools('pit.xml.insert.header', _GUID, @ePIT.xml, 'pit_8ar', 'P', '', '');

		SET @ePIT.header = @ePIT.xml;

		CALL pit_tools('pit.xml.insert.podmiot1', '', '', 'Płatnik', '', '', '');

			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<PozycjeSzczegolowe>\n');
				SET @xml.IC = @xml.IC + 1;

				SELECT RodzajKorekty
				INTO _RodzajKorekty
				FROM pit_8ar
				WHERE GUID = _GUID
				;

				IF _RodzajKorekty > 0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_7>', _RodzajKorekty, '</P_7>\n');
				END IF;

				SET _ile = 0; SET @poz.rodzaj = 2;

				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;

				IF _ile > 0 THEN
					SELECT
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
					;

					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_10>', _kwota01, '</P_10>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_11>', _kwota02, '</P_11>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_12>', _kwota03, '</P_12>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_13>', _kwota04, '</P_13>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_14>', _kwota05, '</P_14>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_15>', _kwota06, '</P_15>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_16>', _kwota07, '</P_16>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_17>', _kwota08, '</P_17>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_18>', _kwota09, '</P_18>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_19>', _kwota10, '</P_19>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_20>', _kwota11, '</P_20>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_21>', _kwota12, '</P_21>\n');
				END IF;


				SET _ile = 0; SET @poz.rodzaj = 5;

				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;

				IF _ile > 0 THEN
					SELECT
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
					;

					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_22>', _kwota01, '</P_22>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_23>', _kwota02, '</P_23>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_24>', _kwota03, '</P_24>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_25>', _kwota04, '</P_25>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_26>', _kwota05, '</P_26>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_27>', _kwota06, '</P_27>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_28>', _kwota07, '</P_28>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_29>', _kwota08, '</P_29>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_30>', _kwota09, '</P_30>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_31>', _kwota10, '</P_31>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_32>', _kwota11, '</P_32>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_33>', _kwota12, '</P_33>\n');
				END IF;



				SET _ile = 0; SET @poz.rodzaj = 4;

				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;

				IF _ile > 0 THEN
					SELECT
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
					;

					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_34>', _kwota01, '</P_34>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_35>', _kwota02, '</P_35>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_36>', _kwota03, '</P_36>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_37>', _kwota04, '</P_37>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_38>', _kwota05, '</P_38>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_39>', _kwota06, '</P_39>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_40>', _kwota07, '</P_40>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_41>', _kwota08, '</P_41>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_42>', _kwota09, '</P_42>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_43>', _kwota10, '</P_43>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_44>', _kwota11, '</P_44>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_45>', _kwota12, '</P_45>\n');
				END IF;



				SET _ile = 0; SET @poz.rodzaj = 777;

				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;

				IF _ile > 0 THEN
					SELECT
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
					;

					IF _kwota02 > 0 THEN
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_142>', _kwota01, '</P_142>\n');
					END IF;
					IF _kwota03 > 0 THEN
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_143>', _kwota02, '</P_143>\n');
					END IF;
					IF _kwota04 > 0 THEN
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_144>', _kwota03, '</P_144>\n');
					END IF;
					IF _kwota05 > 0 THEN
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_145>', _kwota04, '</P_145>\n');
					END IF;
					IF _kwota06 > 0 THEN
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_146>', _kwota05, '</P_146>\n');
					END IF;
					IF _kwota07 > 0 THEN
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_147>', _kwota06, '</P_147>\n');
					END IF;
					IF _kwota08 > 0 THEN
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_148>', _kwota07, '</P_148>\n');
					END IF;
					IF _kwota09 > 0 THEN
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_149>', _kwota08, '</P_149>\n');
					END IF;
					IF _kwota10 > 0 THEN
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_150>', _kwota09, '</P_150>\n');
					END IF;
					IF _kwota11 > 0 THEN
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_151>', _kwota10, '</P_151>\n');
					END IF;
					IF _kwota12 > 0 THEN
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_152>', _kwota11, '</P_152>\n');
					END IF;
					IF _kwota01 > 0 THEN
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_153>', _kwota12, '</P_153>\n');
					END IF;
				END IF;



				SET _ile = 0; SET @poz.rodzaj = 3;

				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;

				IF _ile > 0 THEN
					SELECT
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
					INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
					;

					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_178>', _kwota01, '</P_178>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_179>', _kwota02, '</P_179>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_180>', _kwota03, '</P_180>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_181>', _kwota04, '</P_181>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_182>', _kwota05, '</P_182>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_183>', _kwota06, '</P_183>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_184>', _kwota07, '</P_184>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_185>', _kwota08, '</P_185>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_186>', _kwota09, '</P_186>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_187>', _kwota10, '</P_187>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_188>', _kwota11, '</P_188>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_189>', _kwota12, '</P_189>\n');
				END IF;



				SET _ile = 0; SET @poz.rodzaj = 26;

				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;

				IF _ile > 0 THEN
					SELECT
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
					;
					-- rubryka 33, dawniej 32
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_394>', _kwota01, '</P_394>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_395>', _kwota02, '</P_395>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_396>', _kwota03, '</P_396>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_397>', _kwota04, '</P_397>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_398>', _kwota05, '</P_398>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_399>', _kwota06, '</P_399>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_400>', _kwota07, '</P_400>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_401>', _kwota08, '</P_401>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_402>', _kwota09, '</P_402>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_403>', _kwota10, '</P_403>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_404>', _kwota11, '</P_404>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_405>', _kwota12, '</P_405>\n');
				END IF;


				SET _ile = 0;

					SELECT
						SUM(IFNULL(kwota01,0)), SUM(IFNULL(kwota02,0)), SUM(IFNULL(kwota03,0)),
						SUM(IFNULL(kwota04,0)), SUM(IFNULL(kwota05,0)), SUM(IFNULL(kwota06,0)),
						SUM(IFNULL(kwota07,0)), SUM(IFNULL(kwota08,0)), SUM(IFNULL(kwota09,0)),
						SUM(IFNULL(kwota10,0)), SUM(IFNULL(kwota11,0)), SUM(IFNULL(kwota12,0))
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj IN (14)
					;

					SET @pit8ar.k01 = _kwota01;
					SET @pit8ar.k02 = _kwota02;
					SET @pit8ar.k03 = _kwota03;
					SET @pit8ar.k04 = _kwota04;
					SET @pit8ar.k05 = _kwota05;
					SET @pit8ar.k06 = _kwota06;
					SET @pit8ar.k07 = _kwota07;
					SET @pit8ar.k08 = _kwota08;
					SET @pit8ar.k09 = _kwota09;
					SET @pit8ar.k10 = _kwota10;
					SET @pit8ar.k11 = _kwota11;
					SET @pit8ar.k12 = _kwota12;
					-- sekcja 34, dawniej 33
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_406>', _varDOUBLE_NOTNULL(_kwota01), '</P_406>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_407>', _varDOUBLE_NOTNULL(_kwota02), '</P_407>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_408>', _varDOUBLE_NOTNULL(_kwota03), '</P_408>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_409>', _varDOUBLE_NOTNULL(_kwota04), '</P_409>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_410>', _varDOUBLE_NOTNULL(_kwota05), '</P_410>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_411>', _varDOUBLE_NOTNULL(_kwota06), '</P_411>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_412>', _varDOUBLE_NOTNULL(_kwota07), '</P_412>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_413>', _varDOUBLE_NOTNULL(_kwota08), '</P_413>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_414>', _varDOUBLE_NOTNULL(_kwota09), '</P_414>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_415>', _varDOUBLE_NOTNULL(_kwota10), '</P_415>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_416>', _varDOUBLE_NOTNULL(_kwota11), '</P_416>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_417>', _varDOUBLE_NOTNULL(_kwota12), '</P_417>\n');



				SET _ile = 0;
				SELECT
					IFNULL(wynagr01, 0) + IFNULL(wynagr02, 0) +  IFNULL(wynagr03, 0) + IFNULL(wynagr04, 0) +
					IFNULL(wynagr05, 0) + IFNULL(wynagr06, 0) +  IFNULL(wynagr07, 0) + IFNULL(wynagr08, 0) +
					IFNULL(wynagr09, 0) + IFNULL(wynagr10, 0) +  IFNULL(wynagr11, 0) + IFNULL(wynagr12, 0)
				AS ile
				INTO _ile
				FROM pit_8ar
				WHERE GUID = _GUID
				;

				IF _ile > 0 THEN
					SELECT
						IFNULL(wynagr01, 0), IFNULL(wynagr02, 0), IFNULL(wynagr03, 0), IFNULL(wynagr04, 0),
						IFNULL(wynagr05, 0), IFNULL(wynagr06, 0), IFNULL(wynagr07, 0), IFNULL(wynagr08, 0),
						IFNULL(wynagr09, 0), IFNULL(wynagr10, 0), IFNULL(wynagr11, 0), IFNULL(wynagr12, 0)
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar
					WHERE GUID = _GUID
					;
					-- sekcja 35, dawniej 34
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_418>', _kwota01, '</P_418>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_419>', _kwota02, '</P_419>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_420>', _kwota03, '</P_420>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_421>', _kwota04, '</P_421>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_422>', _kwota05, '</P_422>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_423>', _kwota06, '</P_423>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_424>', _kwota07, '</P_424>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_425>', _kwota08, '</P_425>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_426>', _kwota09, '</P_426>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_427>', _kwota10, '</P_427>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_428>', _kwota11, '</P_428>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_429>', _kwota12, '</P_429>\n');

					SET @pit8ar.k01 = GREATEST(@pit8ar.k01 - _kwota01, 0);
					SET @pit8ar.k02 = GREATEST(@pit8ar.k02 - _kwota02, 0);
					SET @pit8ar.k03 = GREATEST(@pit8ar.k03 - _kwota03, 0);
					SET @pit8ar.k04 = GREATEST(@pit8ar.k04 - _kwota04, 0);
					SET @pit8ar.k05 = GREATEST(@pit8ar.k05 - _kwota05, 0);
					SET @pit8ar.k06 = GREATEST(@pit8ar.k06 - _kwota06, 0);
					SET @pit8ar.k07 = GREATEST(@pit8ar.k07 - _kwota07, 0);
					SET @pit8ar.k08 = GREATEST(@pit8ar.k08 - _kwota08, 0);
					SET @pit8ar.k09 = GREATEST(@pit8ar.k09 - _kwota09, 0);
					SET @pit8ar.k10 = GREATEST(@pit8ar.k10 - _kwota10, 0);
					SET @pit8ar.k11 = GREATEST(@pit8ar.k11 - _kwota11, 0);
					SET @pit8ar.k12 = GREATEST(@pit8ar.k12 - _kwota12, 0);
				END IF;


					-- sekcja 36, dawniej 35, podatek do wplaty
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_430>', _varDOUBLE_NOTNULL(@pit8ar.k01), '</P_430>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_431>', _varDOUBLE_NOTNULL(@pit8ar.k02), '</P_431>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_432>', _varDOUBLE_NOTNULL(@pit8ar.k03), '</P_432>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_433>', _varDOUBLE_NOTNULL(@pit8ar.k04), '</P_433>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_434>', _varDOUBLE_NOTNULL(@pit8ar.k05), '</P_434>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_435>', _varDOUBLE_NOTNULL(@pit8ar.k06), '</P_435>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_436>', _varDOUBLE_NOTNULL(@pit8ar.k07), '</P_436>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_437>', _varDOUBLE_NOTNULL(@pit8ar.k08), '</P_437>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_438>', _varDOUBLE_NOTNULL(@pit8ar.k09), '</P_438>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_439>', _varDOUBLE_NOTNULL(@pit8ar.k10), '</P_439>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_440>', _varDOUBLE_NOTNULL(@pit8ar.k11), '</P_440>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_441>', _varDOUBLE_NOTNULL(@pit8ar.k12), '</P_441>\n');

			/* nie obslugujemy nowej czesci

				SELECT IFNULL(valueB,0) FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_III_2020" INTO @covid_III_2020;
				SELECT IFNULL(valueB,0) FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_IV_2020" INTO @covid_IV_2020;
				SELECT IFNULL(valueB,0) FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_V_2020" INTO @covid_V_2020;
				SELECT IFNULL(valueB,0) FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_X_2020" INTO @covid_X_2020;
				SELECT IFNULL(valueB,0) FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_XI_2020" INTO @covid_XI_2020;
				SELECT IFNULL(valueB,0) FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_XII_2020" INTO @covid_XII_2020;

				IF @covid_III_2020>0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_430>', @covid_III_2020, '</P_430>\n');
				END IF;

				IF @covid_IV_2020>0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_431>', @covid_IV_2020, '</P_431>\n');
				END IF;

				IF @covid_V_2020>0 THEN

					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_432>', @covid_V_2020, '</P_432>\n');
				END IF;

					IF @covid_X_2020>0 THEN

					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_433>', @covid_X_2020, '</P_433>\n');
				END IF;

					IF @covid_XI_2020>0 THEN

					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_434>', @covid_XI_2020, '</P_434>\n');
				END IF;
					IF @covid_XII_2020>0 THEN

					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<P_435>', @covid_XII_2020, '</P_435>\n');
				END IF;

				*/


				SET @ePIT.poz = @ePIT.xml;

				SET @xml.IC = @xml.IC - 1;
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '</PozycjeSzczegolowe>\n');

		SET @ePIT.poz = @ePIT.xml;

		SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Pouczenia>1</Pouczenia>\n');

		IF (@ePIT.CelZlozenia = cKorekta)  AND (LENGTH(@_przyczyna_ORD_ZU) > 0) THEN
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<Zalaczniki>\n');
			CALL pit_tools('pit.xml.insert.ORD_ZU', '', '', '', '', '', '4');
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '</Zalaczniki>\n');
		END IF;

		SET @ePIT.xml = CONCAT(@ePIT.xml, '</Deklaracja>');

		CALL e_doc_tools('e_doc_po.insert.or.edit', _Param_A, IFNULL(@ePIT.xml, ''), 'CREATE_XML', 'ORYG', '', '', '', '');

END IF;  -- pit8ar.xml.create.202204'

IF _MODE = 'pit8ar.xml.create.202301' THEN

	SELECT
		d.ID, p.przyczyna_ORD_ZU
		INTO _e_Doc_Def_ID, @_przyczyna_ORD_ZU
	FROM
		pit_8ar p, e_doc_def d
	WHERE
		p.GUID = _GUID AND  p.e_Doc_Def_ID = d.owner_doc_Id
	;

SET @test1=1;

	SET @ePIT.xml = '';
	SET @xml.IC = 1;

	SET @ePIT.xml = '<?xml version="1.0" encoding="UTF-8" ?>\n';

	SET @ePIT.xml = CONCAT(@ePIT.xml, '<tns:Deklaracja xmlns:tns="', _e_Doc_Def_ID, '" ');
	SET @ePIT.xml = CONCAT(@ePIT.xml, 'xmlns:etd="http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2022/09/13/eD/DefinicjeTypy/"', '\n');
	SET @ePIT.xml = CONCAT(@ePIT.xml, 'xmlns:kus="http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2022/01/05/eD/KodyUrzedowSkarbowych/"', '\n');
	SET @ePIT.xml = CONCAT(@ePIT.xml, 'xmlns:zzu="http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2022/09/13/eD/ORDZU/"', '\n');
	SET @ePIT.xml = CONCAT(@ePIT.xml, 'xmlns:xsd="http://www.w3.org/2001/XMLSchema">', '\n');
	SET @ePIT.xmlns = @ePIT.xml;

		CALL pit_tools('pit.xml.insert.header.2023', _GUID, @ePIT.xml, 'pit_8ar', 'P', '', '');

		SET @ePIT.header = @ePIT.xml;

		CALL pit_tools('pit.xml.insert.podmiot1.2023', '', '', 'Płatnik', '', '', '');

			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:PozycjeSzczegolowe>\n');
				SET @xml.IC = @xml.IC + 1;

				SELECT RodzajKorekty
				INTO _RodzajKorekty
				FROM pit_8ar
				WHERE GUID = _GUID
				;

				IF _RodzajKorekty > 0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_7>', _RodzajKorekty, '</tns:P_7>\n');
				END IF;

				SET _ile = 0; SET @poz.rodzaj = 2;

				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;

				IF _ile > 0 THEN
					SELECT
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
					;

					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_10>', _kwota01, '</tns:P_10>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_11>', _kwota02, '</tns:P_11>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_12>', _kwota03, '</tns:P_12>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_13>', _kwota04, '</tns:P_13>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_14>', _kwota05, '</tns:P_14>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_15>', _kwota06, '</tns:P_15>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_16>', _kwota07, '</tns:P_16>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_17>', _kwota08, '</tns:P_17>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_18>', _kwota09, '</tns:P_18>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_19>', _kwota10, '</tns:P_19>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_20>', _kwota11, '</tns:P_20>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_21>', _kwota12, '</tns:P_21>\n');
				END IF;


				SET _ile = 0; SET @poz.rodzaj = 5;

				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;

				IF _ile > 0 THEN
					SELECT
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
					;

					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_22>', _kwota01, '</tns:P_22>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_23>', _kwota02, '</tns:P_23>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_24>', _kwota03, '</tns:P_24>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_25>', _kwota04, '</tns:P_25>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_26>', _kwota05, '</tns:P_26>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_27>', _kwota06, '</tns:P_27>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_28>', _kwota07, '</tns:P_28>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_29>', _kwota08, '</tns:P_29>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_30>', _kwota09, '</tns:P_30>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_31>', _kwota10, '</tns:P_31>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_32>', _kwota11, '</tns:P_32>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_33>', _kwota12, '</tns:P_33>\n');
				END IF;



				SET _ile = 0; SET @poz.rodzaj = 4;

				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;

				IF _ile > 0 THEN
					SELECT
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
					;

					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_34>', _kwota01, '</tns:P_34>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_35>', _kwota02, '</tns:P_35>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_36>', _kwota03, '</tns:P_36>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_37>', _kwota04, '</tns:P_37>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_38>', _kwota05, '</tns:P_38>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_39>', _kwota06, '</tns:P_39>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_40>', _kwota07, '</tns:P_40>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_41>', _kwota08, '</tns:P_41>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_42>', _kwota09, '</tns:P_42>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_43>', _kwota10, '</tns:P_43>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_44>', _kwota11, '</tns:P_44>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_45>', _kwota12, '</tns:P_45>\n');
				END IF;



				SET _ile = 0; SET @poz.rodzaj = 777;

				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;

				IF _ile > 0 THEN
					SELECT
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
					;

					IF _kwota02 > 0 THEN
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_166>', _kwota01, '</tns:P_166>\n');
					END IF;
					IF _kwota03 > 0 THEN
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_167>', _kwota02, '</tns:P_167>\n');
					END IF;
					IF _kwota04 > 0 THEN
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_168>', _kwota03, '</tns:P_168>\n');
					END IF;
					IF _kwota05 > 0 THEN
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_169>', _kwota04, '</tns:P_169>\n');
					END IF;
					IF _kwota06 > 0 THEN
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_170>', _kwota05, '</tns:P_170>\n');
					END IF;
					IF _kwota07 > 0 THEN
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_171>', _kwota06, '</tns:P_171>\n');
					END IF;
					IF _kwota08 > 0 THEN
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_172>', _kwota07, '</tns:P_172>\n');
					END IF;
					IF _kwota09 > 0 THEN
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_173>', _kwota08, '</tns:P_173>\n');
					END IF;
					IF _kwota10 > 0 THEN
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_174>', _kwota09, '</tns:P_174>\n');
					END IF;
					IF _kwota11 > 0 THEN
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_175>', _kwota10, '</tns:P_175>\n');
					END IF;
					IF _kwota12 > 0 THEN
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_176>', _kwota11, '</tns:P_176>\n');
					END IF;
					IF _kwota01 > 0 THEN
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_177>', _kwota12, '</tns:P_177>\n');
					END IF;
				END IF;



				SET _ile = 0; SET @poz.rodzaj = 3;

				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;

				IF _ile > 0 THEN
					SELECT
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
					;

					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_202>', _kwota01, '</tns:P_202>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_203>', _kwota02, '</tns:P_203>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_204>', _kwota03, '</tns:P_204>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_205>', _kwota04, '</tns:P_205>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_206>', _kwota05, '</tns:P_206>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_207>', _kwota06, '</tns:P_207>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_208>', _kwota07, '</tns:P_208>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_209>', _kwota08, '</tns:P_209>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_210>', _kwota09, '</tns:P_210>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_211>', _kwota10, '</tns:P_211>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_212>', _kwota11, '</tns:P_212>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_213>', _kwota12, '</tns:P_213>\n');
				END IF;



				SET _ile = 0; SET @poz.rodzaj = 26;

				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;

				IF _ile > 0 THEN
					SELECT
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
					;
					-- rubryka 33, dawniej 32
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_418>', _kwota01, '</tns:P_418>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_419>', _kwota02, '</tns:P_419>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_420>', _kwota03, '</tns:P_420>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_421>', _kwota04, '</tns:P_421>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_422>', _kwota05, '</tns:P_422>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_423>', _kwota06, '</tns:P_423>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_424>', _kwota07, '</tns:P_424>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_425>', _kwota08, '</tns:P_425>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_426>', _kwota09, '</tns:P_426>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_427>', _kwota10, '</tns:P_427>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_428>', _kwota11, '</tns:P_428>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_429>', _kwota12, '</tns:P_429>\n');
				END IF;


				SET _ile = 0;

					SELECT
						SUM(IFNULL(kwota01,0)), SUM(IFNULL(kwota02,0)), SUM(IFNULL(kwota03,0)),
						SUM(IFNULL(kwota04,0)), SUM(IFNULL(kwota05,0)), SUM(IFNULL(kwota06,0)),
						SUM(IFNULL(kwota07,0)), SUM(IFNULL(kwota08,0)), SUM(IFNULL(kwota09,0)),
						SUM(IFNULL(kwota10,0)), SUM(IFNULL(kwota11,0)), SUM(IFNULL(kwota12,0))
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj IN (14)
					;

					SET @pit8ar.k01 = _kwota01;
					SET @pit8ar.k02 = _kwota02;
					SET @pit8ar.k03 = _kwota03;
					SET @pit8ar.k04 = _kwota04;
					SET @pit8ar.k05 = _kwota05;
					SET @pit8ar.k06 = _kwota06;
					SET @pit8ar.k07 = _kwota07;
					SET @pit8ar.k08 = _kwota08;
					SET @pit8ar.k09 = _kwota09;
					SET @pit8ar.k10 = _kwota10;
					SET @pit8ar.k11 = _kwota11;
					SET @pit8ar.k12 = _kwota12;
					-- sekcja 34, dawniej 33
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_430>', _varDOUBLE_NOTNULL(_kwota01), '</tns:P_430>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_431>', _varDOUBLE_NOTNULL(_kwota02), '</tns:P_431>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_432>', _varDOUBLE_NOTNULL(_kwota03), '</tns:P_432>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_433>', _varDOUBLE_NOTNULL(_kwota04), '</tns:P_433>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_434>', _varDOUBLE_NOTNULL(_kwota05), '</tns:P_434>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_435>', _varDOUBLE_NOTNULL(_kwota06), '</tns:P_435>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_436>', _varDOUBLE_NOTNULL(_kwota07), '</tns:P_436>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_437>', _varDOUBLE_NOTNULL(_kwota08), '</tns:P_437>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_438>', _varDOUBLE_NOTNULL(_kwota09), '</tns:P_438>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_439>', _varDOUBLE_NOTNULL(_kwota10), '</tns:P_439>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_440>', _varDOUBLE_NOTNULL(_kwota11), '</tns:P_440>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_441>', _varDOUBLE_NOTNULL(_kwota12), '</tns:P_441>\n');



				SET _ile = 0;
				SELECT
					IFNULL(wynagr01, 0) + IFNULL(wynagr02, 0) +  IFNULL(wynagr03, 0) + IFNULL(wynagr04, 0) +
					IFNULL(wynagr05, 0) + IFNULL(wynagr06, 0) +  IFNULL(wynagr07, 0) + IFNULL(wynagr08, 0) +
					IFNULL(wynagr09, 0) + IFNULL(wynagr10, 0) +  IFNULL(wynagr11, 0) + IFNULL(wynagr12, 0)
				AS ile
				INTO _ile
				FROM pit_8ar
				WHERE GUID = _GUID
				;

				IF _ile > 0 THEN
					SELECT
						IFNULL(wynagr01, 0), IFNULL(wynagr02, 0), IFNULL(wynagr03, 0), IFNULL(wynagr04, 0),
						IFNULL(wynagr05, 0), IFNULL(wynagr06, 0), IFNULL(wynagr07, 0), IFNULL(wynagr08, 0),
						IFNULL(wynagr09, 0), IFNULL(wynagr10, 0), IFNULL(wynagr11, 0), IFNULL(wynagr12, 0)
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar
					WHERE GUID = _GUID
					;
					-- sekcja 35, dawniej 34
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_442>', _kwota01, '</tns:P_442>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_443>', _kwota02, '</tns:P_443>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_444>', _kwota03, '</tns:P_444>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_445>', _kwota04, '</tns:P_445>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_446>', _kwota05, '</tns:P_446>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_447>', _kwota06, '</tns:P_447>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_448>', _kwota07, '</tns:P_448>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_449>', _kwota08, '</tns:P_449>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_450>', _kwota09, '</tns:P_450>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_451>', _kwota10, '</tns:P_451>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_452>', _kwota11, '</tns:P_452>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_453>', _kwota12, '</tns:P_453>\n');

					SET @pit8ar.k01 = GREATEST(@pit8ar.k01 - _kwota01, 0);
					SET @pit8ar.k02 = GREATEST(@pit8ar.k02 - _kwota02, 0);
					SET @pit8ar.k03 = GREATEST(@pit8ar.k03 - _kwota03, 0);
					SET @pit8ar.k04 = GREATEST(@pit8ar.k04 - _kwota04, 0);
					SET @pit8ar.k05 = GREATEST(@pit8ar.k05 - _kwota05, 0);
					SET @pit8ar.k06 = GREATEST(@pit8ar.k06 - _kwota06, 0);
					SET @pit8ar.k07 = GREATEST(@pit8ar.k07 - _kwota07, 0);
					SET @pit8ar.k08 = GREATEST(@pit8ar.k08 - _kwota08, 0);
					SET @pit8ar.k09 = GREATEST(@pit8ar.k09 - _kwota09, 0);
					SET @pit8ar.k10 = GREATEST(@pit8ar.k10 - _kwota10, 0);
					SET @pit8ar.k11 = GREATEST(@pit8ar.k11 - _kwota11, 0);
					SET @pit8ar.k12 = GREATEST(@pit8ar.k12 - _kwota12, 0);
				END IF;

					
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_454>', _varDOUBLE_NOTNULL(@pit8ar.k01), '</tns:P_454>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_455>', _varDOUBLE_NOTNULL(@pit8ar.k02), '</tns:P_455>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_456>', _varDOUBLE_NOTNULL(@pit8ar.k03), '</tns:P_456>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_457>', _varDOUBLE_NOTNULL(@pit8ar.k04), '</tns:P_457>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_458>', _varDOUBLE_NOTNULL(@pit8ar.k05), '</tns:P_458>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_459>', _varDOUBLE_NOTNULL(@pit8ar.k06), '</tns:P_459>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_460>', _varDOUBLE_NOTNULL(@pit8ar.k07), '</tns:P_460>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_461>', _varDOUBLE_NOTNULL(@pit8ar.k08), '</tns:P_461>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_462>', _varDOUBLE_NOTNULL(@pit8ar.k09), '</tns:P_462>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_463>', _varDOUBLE_NOTNULL(@pit8ar.k10), '</tns:P_463>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_464>', _varDOUBLE_NOTNULL(@pit8ar.k11), '</tns:P_464>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_465>', _varDOUBLE_NOTNULL(@pit8ar.k12), '</tns:P_465>\n');


					-- --    nie mamy sekcji pomniejszających wiec na wynik przepisujemy to co  w 454 - 465 i w 466 zawsze NIE 
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_466>', 2, '</tns:P_466>\n');

					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_479>', _varDOUBLE_NOTNULL(@pit8ar.k01), '</tns:P_479>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_480>', _varDOUBLE_NOTNULL(@pit8ar.k02), '</tns:P_480>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_481>', _varDOUBLE_NOTNULL(@pit8ar.k03), '</tns:P_481>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_482>', _varDOUBLE_NOTNULL(@pit8ar.k04), '</tns:P_482>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_483>', _varDOUBLE_NOTNULL(@pit8ar.k05), '</tns:P_483>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_484>', _varDOUBLE_NOTNULL(@pit8ar.k06), '</tns:P_484>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_485>', _varDOUBLE_NOTNULL(@pit8ar.k07), '</tns:P_485>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_486>', _varDOUBLE_NOTNULL(@pit8ar.k08), '</tns:P_486>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_487>', _varDOUBLE_NOTNULL(@pit8ar.k09), '</tns:P_487>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_488>', _varDOUBLE_NOTNULL(@pit8ar.k10), '</tns:P_488>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_489>', _varDOUBLE_NOTNULL(@pit8ar.k11), '</tns:P_489>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_490>', _varDOUBLE_NOTNULL(@pit8ar.k12), '</tns:P_490>\n');

			/* nie obslugujemy nowej czesci

				SELECT IFNULL(valueB,0) FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_III_2020" INTO @covid_III_2020;
				SELECT IFNULL(valueB,0) FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_IV_2020" INTO @covid_IV_2020;
				SELECT IFNULL(valueB,0) FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_V_2020" INTO @covid_V_2020;
				SELECT IFNULL(valueB,0) FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_X_2020" INTO @covid_X_2020;
				SELECT IFNULL(valueB,0) FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_XI_2020" INTO @covid_XI_2020;
				SELECT IFNULL(valueB,0) FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_XII_2020" INTO @covid_XII_2020;

				IF @covid_III_2020>0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_430>', @covid_III_2020, '</tns:P_430>\n');
				END IF;

				IF @covid_IV_2020>0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_431>', @covid_IV_2020, '</tns:P_431>\n');
				END IF;

				IF @covid_V_2020>0 THEN

					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_432>', @covid_V_2020, '</tns:P_432>\n');
				END IF;

					IF @covid_X_2020>0 THEN

					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_433>', @covid_X_2020, '</tns:P_433>\n');
				END IF;

					IF @covid_XI_2020>0 THEN

					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_434>', @covid_XI_2020, '</tns:P_434>\n');
				END IF;
					IF @covid_XII_2020>0 THEN

					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_435>', @covid_XII_2020, '</tns:P_435>\n');
				END IF;

				*/


				SET @ePIT.poz = @ePIT.xml;

				SET @xml.IC = @xml.IC - 1;
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '</tns:PozycjeSzczegolowe>\n');

		SET @ePIT.poz = @ePIT.xml;

		SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:Pouczenia>1</tns:Pouczenia>\n');

		IF (@ePIT.CelZlozenia = cKorekta)  AND (LENGTH(@_przyczyna_ORD_ZU) > 0) THEN
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:Zalaczniki>\n');
			CALL pit_tools('pit.xml.insert.ORD_ZU', '', '', '', '', '', '4');
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '</tns:Zalaczniki>\n');
		END IF;
	-- polskie znaki out	
		SET @ePIT.xml = CONCAT(_to_utf_xml(@ePIT.xml), '</tns:Deklaracja>');
	--	SET @ePIT.xml = CONCAT(@ePIT.xml, '</tns:Deklaracja>');

		CALL e_doc_tools('e_doc_po.insert.or.edit', _Param_A, IFNULL(@ePIT.xml, ''), 'CREATE_XML', 'ORYG', '', '', '', '');

END IF;  -- pit8ar.xml.create.202301'

IF _MODE = 'pit8ar.xml.create.202401' THEN

	SELECT
		d.ID, p.przyczyna_ORD_ZU
		INTO _e_Doc_Def_ID, @_przyczyna_ORD_ZU
	FROM
		pit_8ar p, e_doc_def d
	WHERE
		p.GUID = _GUID AND  p.e_Doc_Def_ID = d.owner_doc_Id
	;

SET @test1=1;

	SET @ePIT.xml = '';
	SET @xml.IC = 1;

	SET @ePIT.xml = '<?xml version="1.0" encoding="UTF-8" ?>\n';

	SET @ePIT.xml = CONCAT(@ePIT.xml, '<tns:Deklaracja xmlns:tns="', _e_Doc_Def_ID, '" ');
	SET @ePIT.xml = CONCAT(@ePIT.xml, 'xmlns:etd="http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2022/09/13/eD/DefinicjeTypy/"', '\n');
	SET @ePIT.xml = CONCAT(@ePIT.xml, 'xmlns:kus="http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2022/01/05/eD/KodyUrzedowSkarbowych/"', '\n');
	SET @ePIT.xml = CONCAT(@ePIT.xml, 'xmlns:zzu="http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2022/09/13/eD/ORDZU/"', '\n');
	SET @ePIT.xml = CONCAT(@ePIT.xml, 'xmlns:xsd="http://www.w3.org/2001/XMLSchema">', '\n');
	SET @ePIT.xmlns = @ePIT.xml;

		CALL pit_tools('pit.xml.insert.header.2023', _GUID, @ePIT.xml, 'pit_8ar', 'P', '', '');

		SET @ePIT.header = @ePIT.xml;

		CALL pit_tools('pit.xml.insert.podmiot1.2023', '', '', 'Płatnik', '', '', '');

			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:PozycjeSzczegolowe>\n');
				SET @xml.IC = @xml.IC + 1;

				SELECT RodzajKorekty
				INTO _RodzajKorekty
				FROM pit_8ar
				WHERE GUID = _GUID
				;

				IF _RodzajKorekty > 0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_7>', _RodzajKorekty, '</tns:P_7>\n');
				END IF;

				SET _ile = 0; SET @poz.rodzaj = 2;

				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;

				IF _ile > 0 THEN
					SELECT
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
					;

					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_10>', _kwota01, '</tns:P_10>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_11>', _kwota02, '</tns:P_11>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_12>', _kwota03, '</tns:P_12>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_13>', _kwota04, '</tns:P_13>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_14>', _kwota05, '</tns:P_14>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_15>', _kwota06, '</tns:P_15>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_16>', _kwota07, '</tns:P_16>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_17>', _kwota08, '</tns:P_17>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_18>', _kwota09, '</tns:P_18>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_19>', _kwota10, '</tns:P_19>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_20>', _kwota11, '</tns:P_20>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_21>', _kwota12, '</tns:P_21>\n');
				END IF;


				SET _ile = 0; SET @poz.rodzaj = 5;

				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;

				IF _ile > 0 THEN
					SELECT
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
					;

					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_22>', _kwota01, '</tns:P_22>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_23>', _kwota02, '</tns:P_23>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_24>', _kwota03, '</tns:P_24>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_25>', _kwota04, '</tns:P_25>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_26>', _kwota05, '</tns:P_26>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_27>', _kwota06, '</tns:P_27>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_28>', _kwota07, '</tns:P_28>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_29>', _kwota08, '</tns:P_29>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_30>', _kwota09, '</tns:P_30>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_31>', _kwota10, '</tns:P_31>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_32>', _kwota11, '</tns:P_32>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_33>', _kwota12, '</tns:P_33>\n');
				END IF;



				SET _ile = 0; SET @poz.rodzaj = 4;

				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;

				IF _ile > 0 THEN
					SELECT
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
					;

					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_34>', _kwota01, '</tns:P_34>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_35>', _kwota02, '</tns:P_35>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_36>', _kwota03, '</tns:P_36>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_37>', _kwota04, '</tns:P_37>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_38>', _kwota05, '</tns:P_38>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_39>', _kwota06, '</tns:P_39>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_40>', _kwota07, '</tns:P_40>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_41>', _kwota08, '</tns:P_41>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_42>', _kwota09, '</tns:P_42>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_43>', _kwota10, '</tns:P_43>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_44>', _kwota11, '</tns:P_44>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_45>', _kwota12, '</tns:P_45>\n');
				END IF;



				SET _ile = 0; SET @poz.rodzaj = 777;

				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;

				IF _ile > 0 THEN
					SELECT
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
					;

					IF _kwota02 > 0 THEN
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_166>', _kwota01, '</tns:P_166>\n');
					END IF;
					IF _kwota03 > 0 THEN
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_167>', _kwota02, '</tns:P_167>\n');
					END IF;
					IF _kwota04 > 0 THEN
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_168>', _kwota03, '</tns:P_168>\n');
					END IF;
					IF _kwota05 > 0 THEN
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_169>', _kwota04, '</tns:P_169>\n');
					END IF;
					IF _kwota06 > 0 THEN
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_170>', _kwota05, '</tns:P_170>\n');
					END IF;
					IF _kwota07 > 0 THEN
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_171>', _kwota06, '</tns:P_171>\n');
					END IF;
					IF _kwota08 > 0 THEN
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_172>', _kwota07, '</tns:P_172>\n');
					END IF;
					IF _kwota09 > 0 THEN
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_173>', _kwota08, '</tns:P_173>\n');
					END IF;
					IF _kwota10 > 0 THEN
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_174>', _kwota09, '</tns:P_174>\n');
					END IF;
					IF _kwota11 > 0 THEN
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_175>', _kwota10, '</tns:P_175>\n');
					END IF;
					IF _kwota12 > 0 THEN
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_176>', _kwota11, '</tns:P_176>\n');
					END IF;
					IF _kwota01 > 0 THEN
						SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_177>', _kwota12, '</tns:P_177>\n');
					END IF;
				END IF;



				SET _ile = 0; SET @poz.rodzaj = 3;

				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;

				IF _ile > 0 THEN
					SELECT
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
					;

					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_202>', _kwota01, '</tns:P_202>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_203>', _kwota02, '</tns:P_203>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_204>', _kwota03, '</tns:P_204>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_205>', _kwota04, '</tns:P_205>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_206>', _kwota05, '</tns:P_206>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_207>', _kwota06, '</tns:P_207>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_208>', _kwota07, '</tns:P_208>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_209>', _kwota08, '</tns:P_209>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_210>', _kwota09, '</tns:P_210>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_211>', _kwota10, '</tns:P_211>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_212>', _kwota11, '</tns:P_212>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_213>', _kwota12, '</tns:P_213>\n');
				END IF;



				SET _ile = 0; SET @poz.rodzaj = 26;

				SELECT COUNT(*) AS ile
				INTO _ile
				FROM pit_8ar_poz
				WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
				;

				IF _ile > 0 THEN
					SELECT
						kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
						kwota07, kwota08, kwota09, kwota10, kwota11, kwota12
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj = @poz.rodzaj
					;
					-- rubryka 33, dawniej 32
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_418>', _kwota01, '</tns:P_418>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_419>', _kwota02, '</tns:P_419>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_420>', _kwota03, '</tns:P_420>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_421>', _kwota04, '</tns:P_421>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_422>', _kwota05, '</tns:P_422>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_423>', _kwota06, '</tns:P_423>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_424>', _kwota07, '</tns:P_424>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_425>', _kwota08, '</tns:P_425>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_426>', _kwota09, '</tns:P_426>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_427>', _kwota10, '</tns:P_427>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_428>', _kwota11, '</tns:P_428>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_429>', _kwota12, '</tns:P_429>\n');
				END IF;


				SET _ile = 0;

					SELECT
						SUM(IFNULL(kwota01,0)), SUM(IFNULL(kwota02,0)), SUM(IFNULL(kwota03,0)),
						SUM(IFNULL(kwota04,0)), SUM(IFNULL(kwota05,0)), SUM(IFNULL(kwota06,0)),
						SUM(IFNULL(kwota07,0)), SUM(IFNULL(kwota08,0)), SUM(IFNULL(kwota09,0)),
						SUM(IFNULL(kwota10,0)), SUM(IFNULL(kwota11,0)), SUM(IFNULL(kwota12,0))
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar_poz
					WHERE deklGUID = _GUID  AND rodzaj IN (14)
					;

					SET @pit8ar.k01 = _kwota01;
					SET @pit8ar.k02 = _kwota02;
					SET @pit8ar.k03 = _kwota03;
					SET @pit8ar.k04 = _kwota04;
					SET @pit8ar.k05 = _kwota05;
					SET @pit8ar.k06 = _kwota06;
					SET @pit8ar.k07 = _kwota07;
					SET @pit8ar.k08 = _kwota08;
					SET @pit8ar.k09 = _kwota09;
					SET @pit8ar.k10 = _kwota10;
					SET @pit8ar.k11 = _kwota11;
					SET @pit8ar.k12 = _kwota12;
					-- sekcja 34, dawniej 33
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_430>', _varDOUBLE_NOTNULL(_kwota01), '</tns:P_430>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_431>', _varDOUBLE_NOTNULL(_kwota02), '</tns:P_431>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_432>', _varDOUBLE_NOTNULL(_kwota03), '</tns:P_432>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_433>', _varDOUBLE_NOTNULL(_kwota04), '</tns:P_433>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_434>', _varDOUBLE_NOTNULL(_kwota05), '</tns:P_434>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_435>', _varDOUBLE_NOTNULL(_kwota06), '</tns:P_435>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_436>', _varDOUBLE_NOTNULL(_kwota07), '</tns:P_436>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_437>', _varDOUBLE_NOTNULL(_kwota08), '</tns:P_437>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_438>', _varDOUBLE_NOTNULL(_kwota09), '</tns:P_438>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_439>', _varDOUBLE_NOTNULL(_kwota10), '</tns:P_439>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_440>', _varDOUBLE_NOTNULL(_kwota11), '</tns:P_440>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_441>', _varDOUBLE_NOTNULL(_kwota12), '</tns:P_441>\n');



				SET _ile = 0;
				SELECT
					IFNULL(wynagr01, 0) + IFNULL(wynagr02, 0) +  IFNULL(wynagr03, 0) + IFNULL(wynagr04, 0) +
					IFNULL(wynagr05, 0) + IFNULL(wynagr06, 0) +  IFNULL(wynagr07, 0) + IFNULL(wynagr08, 0) +
					IFNULL(wynagr09, 0) + IFNULL(wynagr10, 0) +  IFNULL(wynagr11, 0) + IFNULL(wynagr12, 0)
				AS ile
				INTO _ile
				FROM pit_8ar
				WHERE GUID = _GUID
				;

				IF _ile > 0 THEN
					SELECT
						IFNULL(wynagr01, 0), IFNULL(wynagr02, 0), IFNULL(wynagr03, 0), IFNULL(wynagr04, 0),
						IFNULL(wynagr05, 0), IFNULL(wynagr06, 0), IFNULL(wynagr07, 0), IFNULL(wynagr08, 0),
						IFNULL(wynagr09, 0), IFNULL(wynagr10, 0), IFNULL(wynagr11, 0), IFNULL(wynagr12, 0)
						INTO _kwota01, _kwota02, _kwota03, _kwota04, _kwota05, _kwota06,
						 _kwota07, _kwota08, _kwota09, _kwota10, _kwota11, _kwota12
					FROM pit_8ar
					WHERE GUID = _GUID
					;
					-- sekcja 35, dawniej 34
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_442>', _kwota01, '</tns:P_442>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_443>', _kwota02, '</tns:P_443>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_444>', _kwota03, '</tns:P_444>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_445>', _kwota04, '</tns:P_445>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_446>', _kwota05, '</tns:P_446>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_447>', _kwota06, '</tns:P_447>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_448>', _kwota07, '</tns:P_448>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_449>', _kwota08, '</tns:P_449>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_450>', _kwota09, '</tns:P_450>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_451>', _kwota10, '</tns:P_451>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_452>', _kwota11, '</tns:P_452>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_453>', _kwota12, '</tns:P_453>\n');

					SET @pit8ar.k01 = GREATEST(@pit8ar.k01 - _kwota01, 0);
					SET @pit8ar.k02 = GREATEST(@pit8ar.k02 - _kwota02, 0);
					SET @pit8ar.k03 = GREATEST(@pit8ar.k03 - _kwota03, 0);
					SET @pit8ar.k04 = GREATEST(@pit8ar.k04 - _kwota04, 0);
					SET @pit8ar.k05 = GREATEST(@pit8ar.k05 - _kwota05, 0);
					SET @pit8ar.k06 = GREATEST(@pit8ar.k06 - _kwota06, 0);
					SET @pit8ar.k07 = GREATEST(@pit8ar.k07 - _kwota07, 0);
					SET @pit8ar.k08 = GREATEST(@pit8ar.k08 - _kwota08, 0);
					SET @pit8ar.k09 = GREATEST(@pit8ar.k09 - _kwota09, 0);
					SET @pit8ar.k10 = GREATEST(@pit8ar.k10 - _kwota10, 0);
					SET @pit8ar.k11 = GREATEST(@pit8ar.k11 - _kwota11, 0);
					SET @pit8ar.k12 = GREATEST(@pit8ar.k12 - _kwota12, 0);
				END IF;


					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_454>', _varDOUBLE_NOTNULL(@pit8ar.k01), '</tns:P_454>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_455>', _varDOUBLE_NOTNULL(@pit8ar.k02), '</tns:P_455>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_456>', _varDOUBLE_NOTNULL(@pit8ar.k03), '</tns:P_456>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_457>', _varDOUBLE_NOTNULL(@pit8ar.k04), '</tns:P_457>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_458>', _varDOUBLE_NOTNULL(@pit8ar.k05), '</tns:P_458>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_459>', _varDOUBLE_NOTNULL(@pit8ar.k06), '</tns:P_459>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_460>', _varDOUBLE_NOTNULL(@pit8ar.k07), '</tns:P_460>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_461>', _varDOUBLE_NOTNULL(@pit8ar.k08), '</tns:P_461>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_462>', _varDOUBLE_NOTNULL(@pit8ar.k09), '</tns:P_462>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_463>', _varDOUBLE_NOTNULL(@pit8ar.k10), '</tns:P_463>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_464>', _varDOUBLE_NOTNULL(@pit8ar.k11), '</tns:P_464>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_465>', _varDOUBLE_NOTNULL(@pit8ar.k12), '</tns:P_465>\n');


					-- --    nie mamy sekcji pomniejszających wiec na wynik przepisujemy to co  w 454 - 465 i w 466 zawsze NIE
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_466>', 2, '</tns:P_466>\n');

					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_479>', _varDOUBLE_NOTNULL(@pit8ar.k01), '</tns:P_479>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_480>', _varDOUBLE_NOTNULL(@pit8ar.k02), '</tns:P_480>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_481>', _varDOUBLE_NOTNULL(@pit8ar.k03), '</tns:P_481>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_482>', _varDOUBLE_NOTNULL(@pit8ar.k04), '</tns:P_482>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_483>', _varDOUBLE_NOTNULL(@pit8ar.k05), '</tns:P_483>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_484>', _varDOUBLE_NOTNULL(@pit8ar.k06), '</tns:P_484>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_485>', _varDOUBLE_NOTNULL(@pit8ar.k07), '</tns:P_485>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_486>', _varDOUBLE_NOTNULL(@pit8ar.k08), '</tns:P_486>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_487>', _varDOUBLE_NOTNULL(@pit8ar.k09), '</tns:P_487>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_488>', _varDOUBLE_NOTNULL(@pit8ar.k10), '</tns:P_488>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_489>', _varDOUBLE_NOTNULL(@pit8ar.k11), '</tns:P_489>\n');
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_490>', _varDOUBLE_NOTNULL(@pit8ar.k12), '</tns:P_490>\n');

			/* nie obslugujemy nowej czesci

				SELECT IFNULL(valueB,0) FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_III_2020" INTO @covid_III_2020;
				SELECT IFNULL(valueB,0) FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_IV_2020" INTO @covid_IV_2020;
				SELECT IFNULL(valueB,0) FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_V_2020" INTO @covid_V_2020;
				SELECT IFNULL(valueB,0) FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_X_2020" INTO @covid_X_2020;
				SELECT IFNULL(valueB,0) FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_XI_2020" INTO @covid_XI_2020;
				SELECT IFNULL(valueB,0) FROM IReg  WHERE idkey="COVID_PIT_Przesuniecie_zaliczek_XII_2020" INTO @covid_XII_2020;

				IF @covid_III_2020>0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_430>', @covid_III_2020, '</tns:P_430>\n');
				END IF;

				IF @covid_IV_2020>0 THEN
					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_431>', @covid_IV_2020, '</tns:P_431>\n');
				END IF;

				IF @covid_V_2020>0 THEN

					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_432>', @covid_V_2020, '</tns:P_432>\n');
				END IF;

					IF @covid_X_2020>0 THEN

					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_433>', @covid_X_2020, '</tns:P_433>\n');
				END IF;

					IF @covid_XI_2020>0 THEN

					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_434>', @covid_XI_2020, '</tns:P_434>\n');
				END IF;
					IF @covid_XII_2020>0 THEN

					SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:P_435>', @covid_XII_2020, '</tns:P_435>\n');
				END IF;

				*/


				SET @ePIT.poz = @ePIT.xml;

				SET @xml.IC = @xml.IC - 1;
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '</tns:PozycjeSzczegolowe>\n');

		SET @ePIT.poz = @ePIT.xml;

		SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:Pouczenia>1</tns:Pouczenia>\n');

		IF (@ePIT.CelZlozenia = cKorekta)  AND (LENGTH(@_przyczyna_ORD_ZU) > 0) THEN
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '<tns:Zalaczniki>\n');
			CALL pit_tools('pit.xml.insert.ORD_ZU', '', '', '', '', '', '4');
			SET @ePIT.xml = CONCAT(@ePIT.xml, SPACE(@xml.IC*cSL), '</tns:Zalaczniki>\n');
		END IF;
	-- polskie znaki out
		SET @ePIT.xml = CONCAT(_to_utf_xml(@ePIT.xml), '</tns:Deklaracja>');
	--	SET @ePIT.xml = CONCAT(@ePIT.xml, '</tns:Deklaracja>');

		CALL e_doc_tools('e_doc_po.insert.or.edit', _Param_A, IFNULL(@ePIT.xml, ''), 'CREATE_XML', 'ORYG', '', '', '', '');

END IF;  -- pit8ar.xml.create.202401'


/*------------------------------------------------------------------------------------------------------------------------------------------------------------------	
	'pit8ar.rec.insert'
		utworzenie nowego rekordu PIT-8AR
		Parametry 
		_MODE		  Tryb pracy
		IN _GUID      GUID  rekordu						
		IN _Param_A   Rok, za ktory jest skladana deklaracja
		IN _Param_B   Cel zlozenia: 1 - zgloszenie, 2 - korekta;  
		param_c - powod korekty
		
		CALL pit_tools('pit8ar.rec.insert', @GUID, '2014', '1', '', 'powod', '');
------------------------------------------------------------------------------------------------------------------------------------------------------------------*/			
IF _MODE = 'pit8ar.rec.insert' THEN
BEGIN
	DECLARE	_suma DOUBLE;	

	CALL _IReg_Get('DaneFirmy', 'Urzad_skarbowy', '0', 'STRING', @US_GUID, '(brak danych)');
	
	INSERT IGNORE INTO pit_8ar (GUID, Rok, CelZlozenia, US_GUID, przyczyna_ORD_ZU)  
	SELECT 		 -- cel zlozenia: 1 - zgloszenie, 2 - korekta;  
				 _GUID  AS GUID, _varINT(_Param_A), _varINT(_Param_B) AS CelZlozenia, @US_GUID, IFNULL(_Param_C,'') as przyczyna_ORD_ZU; 

-- sekcja wstawiajaca zawsze pusty wiersz (nr 26 - inne naleznosi), nie na podstawie danych ale do ewentualnej korekty
 IF _varINT(_Param_A)<2020
 THEN
	INSERT IGNORE INTO pit_8ar_poz 
		  (GUID, deklGUID, rodzaj, 
		   kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
		   kwota07, kwota08, kwota09, kwota10, kwota11, kwota12)  
		SELECT
		   NEWGUID(), _GUID  AS deklGUID, 26,
			0,0,0,0,0,0,0,0,0,0,0,0;
END IF;
	
	-- zebranie danych do sekcji C.
	-- najpierw zliczamy sume (C.14 za 2014, C.15 za 2015) - jesli ona jest zerowa, to przerywamy dalsze zliczanie
	SET _suma = 0.0;  SET @pit8ar.RodzajRyczaltu = 14;  
	SELECT IFNULL(SUM(ZryczaltowanyPodatek), 0) AS kwota
	INTO _suma
	FROM prace_zlecone  
	WHERE YEAR(datawyplaty) = _varINT(_Param_A) AND (deleted = 0) AND  (RodzajRyczaltu BETWEEN 1 AND 6)
	;
	
	IF _suma > 0 THEN
		INSERT IGNORE INTO pit_8ar_poz 
		  (GUID, deklGUID, rodzaj, 
		   kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
		   kwota07, kwota08, kwota09, kwota10, kwota11, kwota12)  
		SELECT 	
		   NEWGUID(), _GUID  AS deklGUID, @pit8ar.RodzajRyczaltu,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 1, ZryczaltowanyPodatek, 0)), 0) AS kwota01,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 2, ZryczaltowanyPodatek, 0)), 0) AS kwota02, 		
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 3, ZryczaltowanyPodatek, 0)), 0) AS kwota03,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 4, ZryczaltowanyPodatek, 0)), 0) AS kwota04,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 5, ZryczaltowanyPodatek, 0)), 0) AS kwota05,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 6, ZryczaltowanyPodatek, 0)), 0) AS kwota06,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 7, ZryczaltowanyPodatek, 0)), 0) AS kwota07,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 8, ZryczaltowanyPodatek, 0)), 0) AS kwota08,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 9, ZryczaltowanyPodatek, 0)), 0) AS kwota09,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 10, ZryczaltowanyPodatek, 0)), 0) AS kwota10,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 11, ZryczaltowanyPodatek, 0)), 0) AS kwota11,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 12, ZryczaltowanyPodatek, 0)), 0) AS kwota12
		FROM 
			prace_zlecone 
		WHERE 
			YEAR(datawyplaty) = _varINT(_Param_A) AND (deleted = 0) AND (RodzajRyczaltu BETWEEN 1 AND 6);
			
		-- wstawiamy sume do naglowka
		update pit_8ar n, pit_8ar_poz p set
			do_wplaty01 = kwota01, do_wplaty02 = kwota02, do_wplaty03 = kwota03, do_wplaty04 = kwota04, do_wplaty05 = kwota05, do_wplaty06 = kwota06,
		    do_wplaty07 = kwota07, do_wplaty08 = kwota08, do_wplaty09 = kwota09, do_wplaty10 = kwota10, do_wplaty11 = kwota11, do_wplaty12 = kwota12
		where	p.deklGUID = n.guid and n.guid = _GUID and p.rodzaj = 14;
	ELSE
       LEAVE SPROC;	-- kwota pobranego zryczatlowanego podatku doch. jest zerowa
	END IF;
	
	
	-- C.1. (rodzaj ryczaltu = 5)	
	SET _suma = 0.0; SET @pit8ar.RodzajRyczaltu = 5;
	SELECT IFNULL(SUM(ZryczaltowanyPodatek), 0) AS kwota
	INTO _suma
	FROM prace_zlecone  
	WHERE YEAR(datawyplaty) = _varINT(_Param_A) AND deleted = 0 AND RodzajRyczaltu = @pit8ar.RodzajRyczaltu
	;
	
	IF _suma > 0 THEN
		INSERT IGNORE INTO pit_8ar_poz 
		  (GUID, deklGUID, rodzaj, 
		   kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
		   kwota07, kwota08, kwota09, kwota10, kwota11, kwota12)  
		SELECT 	
		   NEWGUID(), _GUID  AS deklGUID, @pit8ar.RodzajRyczaltu,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 1, ZryczaltowanyPodatek, 0)), 0) AS kwota01,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 2, ZryczaltowanyPodatek, 0)), 0) AS kwota02, 		
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 3, ZryczaltowanyPodatek, 0)), 0) AS kwota03,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 4, ZryczaltowanyPodatek, 0)), 0) AS kwota04,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 5, ZryczaltowanyPodatek, 0)), 0) AS kwota05,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 6, ZryczaltowanyPodatek, 0)), 0) AS kwota06,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 7, ZryczaltowanyPodatek, 0)), 0) AS kwota07,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 8, ZryczaltowanyPodatek, 0)), 0) AS kwota08,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 9, ZryczaltowanyPodatek, 0)), 0) AS kwota09,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 10, ZryczaltowanyPodatek, 0)), 0) AS kwota10,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 11, ZryczaltowanyPodatek, 0)), 0) AS kwota11,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 12, ZryczaltowanyPodatek, 0)), 0) AS kwota12
		FROM 
			prace_zlecone 
		WHERE 
			YEAR(datawyplaty) = _varINT(_Param_A) AND deleted = 0 AND RodzajRyczaltu = @pit8ar.RodzajRyczaltu;		
	END IF;
	
	-- C.2. (rodzaj ryczaltu = 4)	
	SET _suma = 0.0; SET @pit8ar.RodzajRyczaltu = 4;
	SELECT IFNULL(SUM(ZryczaltowanyPodatek), 0) AS kwota
	INTO _suma
	FROM prace_zlecone  
	WHERE YEAR(datawyplaty) = _varINT(_Param_A) AND deleted = 0 AND RodzajRyczaltu = @pit8ar.RodzajRyczaltu
	;
	
	IF _suma > 0 THEN
		INSERT IGNORE INTO pit_8ar_poz 
		  (GUID, deklGUID, rodzaj, 
		   kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
		   kwota07, kwota08, kwota09, kwota10, kwota11, kwota12)  
		SELECT 	
		   NEWGUID(), _GUID  AS deklGUID, @pit8ar.RodzajRyczaltu,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 1, ZryczaltowanyPodatek, 0)), 0) AS kwota01,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 2, ZryczaltowanyPodatek, 0)), 0) AS kwota02, 		
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 3, ZryczaltowanyPodatek, 0)), 0) AS kwota03,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 4, ZryczaltowanyPodatek, 0)), 0) AS kwota04,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 5, ZryczaltowanyPodatek, 0)), 0) AS kwota05,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 6, ZryczaltowanyPodatek, 0)), 0) AS kwota06,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 7, ZryczaltowanyPodatek, 0)), 0) AS kwota07,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 8, ZryczaltowanyPodatek, 0)), 0) AS kwota08,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 9, ZryczaltowanyPodatek, 0)), 0) AS kwota09,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 10, ZryczaltowanyPodatek, 0)), 0) AS kwota10,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 11, ZryczaltowanyPodatek, 0)), 0) AS kwota11,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 12, ZryczaltowanyPodatek, 0)), 0) AS kwota12
		FROM 
			prace_zlecone 
		WHERE 
			YEAR(datawyplaty) = _varINT(_Param_A) AND deleted = 0 AND RodzajRyczaltu = @pit8ar.RodzajRyczaltu;		
	END IF;
	
	-- "Należności osób zagranicznych, wymienione w art. 29 ustawy" (rodzaj ryczaltu = 2)	
	SET _suma = 0.0; SET @pit8ar.RodzajRyczaltu = 2;
	SELECT IFNULL(SUM(ZryczaltowanyPodatek), 0) AS kwota
	INTO _suma
	FROM prace_zlecone  
	WHERE YEAR(datawyplaty) = _varINT(_Param_A) AND deleted = 0 AND RodzajRyczaltu = @pit8ar.RodzajRyczaltu
	;
	
	IF _suma > 0 THEN
		INSERT IGNORE INTO pit_8ar_poz 
		  (GUID, deklGUID, rodzaj, 
		   kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
		   kwota07, kwota08, kwota09, kwota10, kwota11, kwota12)  
		SELECT 	
		   NEWGUID(), _GUID  AS deklGUID, @pit8ar.RodzajRyczaltu,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 1, ZryczaltowanyPodatek, 0)), 0) AS kwota01,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 2, ZryczaltowanyPodatek, 0)), 0) AS kwota02, 		
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 3, ZryczaltowanyPodatek, 0)), 0) AS kwota03,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 4, ZryczaltowanyPodatek, 0)), 0) AS kwota04,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 5, ZryczaltowanyPodatek, 0)), 0) AS kwota05,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 6, ZryczaltowanyPodatek, 0)), 0) AS kwota06,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 7, ZryczaltowanyPodatek, 0)), 0) AS kwota07,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 8, ZryczaltowanyPodatek, 0)), 0) AS kwota08,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 9, ZryczaltowanyPodatek, 0)), 0) AS kwota09,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 10, ZryczaltowanyPodatek, 0)), 0) AS kwota10,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 11, ZryczaltowanyPodatek, 0)), 0) AS kwota11,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 12, ZryczaltowanyPodatek, 0)), 0) AS kwota12
		FROM 
			prace_zlecone 
		WHERE 
			YEAR(datawyplaty) = _varINT(_Param_A) AND deleted = 0 AND RodzajRyczaltu = @pit8ar.RodzajRyczaltu;		
	END IF;
	
	-- "Dywidendy i inne przychody z tytułu udziału w zyskach osób prawnych (art. 30a ust. 1 pkt 4 ustawy)" (rodzaj ryczaltu = 3)	
	SET _suma = 0.0; SET @pit8ar.RodzajRyczaltu = 3;
	SELECT IFNULL(SUM(ZryczaltowanyPodatek), 0) AS kwota
	INTO _suma
	FROM prace_zlecone  
	WHERE YEAR(datawyplaty) = _varINT(_Param_A) AND deleted = 0 AND RodzajRyczaltu = @pit8ar.RodzajRyczaltu
	;
	
	IF _suma > 0 THEN
		INSERT IGNORE INTO pit_8ar_poz 
		  (GUID, deklGUID, rodzaj, 
		   kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
		   kwota07, kwota08, kwota09, kwota10, kwota11, kwota12)  
		SELECT 	
		   NEWGUID(), _GUID  AS deklGUID, @pit8ar.RodzajRyczaltu,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 1, ZryczaltowanyPodatek, 0)), 0) AS kwota01,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 2, ZryczaltowanyPodatek, 0)), 0) AS kwota02, 		
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 3, ZryczaltowanyPodatek, 0)), 0) AS kwota03,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 4, ZryczaltowanyPodatek, 0)), 0) AS kwota04,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 5, ZryczaltowanyPodatek, 0)), 0) AS kwota05,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 6, ZryczaltowanyPodatek, 0)), 0) AS kwota06,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 7, ZryczaltowanyPodatek, 0)), 0) AS kwota07,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 8, ZryczaltowanyPodatek, 0)), 0) AS kwota08,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 9, ZryczaltowanyPodatek, 0)), 0) AS kwota09,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 10, ZryczaltowanyPodatek, 0)), 0) AS kwota10,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 11, ZryczaltowanyPodatek, 0)), 0) AS kwota11,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 12, ZryczaltowanyPodatek, 0)), 0) AS kwota12
		FROM 
			prace_zlecone 
		WHERE 
			YEAR(datawyplaty) = _varINT(_Param_A) AND deleted = 0 AND RodzajRyczaltu = @pit8ar.RodzajRyczaltu;		
	END IF;
	
	-- C.13. (rodzaj ryczaltu = 1, 6)	(C.14 za 2015)
	SET _suma = 0.0;  SET @pit8ar.RodzajRyczaltu = IF(_varINT(_Param_A)>=2020,26,13);  -- wstawiamy nr wiersza z formularza (C.13 za 2014, w 2015 juz tego nie zmieniamy, zeby nie komplikowac)
	SELECT IFNULL(SUM(ZryczaltowanyPodatek), 0) AS kwota
	INTO _suma
	FROM prace_zlecone  
	WHERE YEAR(datawyplaty) = _varINT(_Param_A) AND deleted = 0 AND RodzajRyczaltu IN (1, 6)
	;
	
	IF _suma > 0 THEN
		INSERT IGNORE INTO pit_8ar_poz 
		  (GUID, deklGUID, rodzaj, 
		   kwota01, kwota02, kwota03, kwota04, kwota05, kwota06,
		   kwota07, kwota08, kwota09, kwota10, kwota11, kwota12)  
		SELECT 	
		   NEWGUID(), _GUID  AS deklGUID, @pit8ar.RodzajRyczaltu,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 1, ZryczaltowanyPodatek, 0)), 0) AS kwota01,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 2, ZryczaltowanyPodatek, 0)), 0) AS kwota02, 		
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 3, ZryczaltowanyPodatek, 0)), 0) AS kwota03,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 4, ZryczaltowanyPodatek, 0)), 0) AS kwota04,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 5, ZryczaltowanyPodatek, 0)), 0) AS kwota05,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 6, ZryczaltowanyPodatek, 0)), 0) AS kwota06,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 7, ZryczaltowanyPodatek, 0)), 0) AS kwota07,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 8, ZryczaltowanyPodatek, 0)), 0) AS kwota08,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 9, ZryczaltowanyPodatek, 0)), 0) AS kwota09,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 10, ZryczaltowanyPodatek, 0)), 0) AS kwota10,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 11, ZryczaltowanyPodatek, 0)), 0) AS kwota11,
		   IFNULL(SUM(IF(MONTH(datawyplaty) = 12, ZryczaltowanyPodatek, 0)), 0) AS kwota12
		FROM 
			prace_zlecone 
		WHERE 
			YEAR(datawyplaty) = _varINT(_Param_A) AND deleted = 0 AND RodzajRyczaltu IN (1, 6);		
	END IF;
						 
END;
END IF;  -- IF _MODE = 'pit8ar.rec.insert'

	
END$$


DELIMITER ;

/*
====================================================================================================================================================================================
	NOTATNIK
====================================================================================================================================================================================
STATUS-y
	0	brak content
	10	wygenerowany XML
	11	na dysku 
	20	podpisany
	30 	wys�any
	
			INSERT IGNORE INTO `dbobj_series`
				(GUIDDoc, TableName, NrDoc, OsobaGUID, DateLog, DateDoc, STATUS, Descr)
				SELECT
					NEW.GUID, 'Pit_11_8B', NEW.NazwaPlatnika, NEW.pracGUID, NOW(), NEW.OkresOd, 0, 'PIT-11 (18)'
				FROM `Pit_11_8B` NEW
			;	
		
Respond_No VARCHAR(150)
Respond_Status INT
Respond_Descr VARCHAR(150)		

																					Status	gdy OK
																					0	
CREATE_XML				kreowanie xml w blobie Content								10
SAVE_ORIG_TO_FILE		zapisuje na dysku jako plik (pole FIleName)					b.z.	(a mo�e 11)
LOAD_SIGN_FROM_FILE		pobiera z dysku zawarto�� pliku i zapisuje w Content		20		je�li podpisany
SEND_TO					wysy�a na e-deklaracje gdy OK dodatkowo 
						wo�amy tu GET_RESPOND										30		wys�any
GET_RESPOND				pobiera urz.potwierdenie odbioru							30		wys�any gdy istnieje UPO
dodatkowo
SIGN_DIR				podpisuje dokumenty - zawiera sekwencj� SAVE_TO_FILE + 
						sign by DLL (np.KIR) + 
						LOAD_FROM_FILE

						
						
						
						
						
A)	Przygotowanie procesu (w _Param_A podajemy id procesu)
	CALL pit_tools('Pit_11_8B.prepare.before.select', '', '', 'CREATE_XML', '', '', '');
	LUB
	CALL pit_tools('Pit_11_8B.prepare.before.select', '', '', 'SAVE_ORIG_TO_FILE', '', '', '');
	LUB ...
	
B)	cds.open
	SELECT * FROM `t_select_Pit_11_8B`;	
	
C)	cds.ApplyChanges +
	CALL pit_tools('Pit_11_8B.pity.zaznaczone.after.apply', '', '', '', '', '', '');		

D)	Przygotowujemy zawarto�� e-dokument�w (ng i po)  (w _Param_A podajemy id procesu)
	CALL pit_tools('Pit_11_8B.pity.zaznaczone.prepare.doc', '', '', 'CREATE_XML', '', '', '');
	LUB ...
	
E)	Teraz mo�na zrobi� otwarcie cds-�w z naglowkami i pozycjami (zawarto�ci plik�w)	
	cds_e_doc_Ng.Open
	SELECT * FROM `t_e_doc_ng`;
	cds_e_doc_po.Open
	SELECT * FROM `t_e_doc_po`;
	
A oto lista wszystkich proces�w
==============================================================================================================================
Proces					Opis														Status	gdy OK
==============================================================================================================================
	-					-															0	
CREATE_XML				kreowanie xml w blobie Content								CREATE	10
SAVE_ORIG_TO_FILE		zapisuje na dysku jako plik (pole FIleName)					b.z.	(a mo�e 11)
LOAD_SIGN_FROM_FILE		pobiera z dysku zawarto�� pliku i zapisuje w Content		SIGN 	20		je�li podpisany
SEND_TO					wysy�a na e-deklaracje gdy OK dodatkowo 
						wo�amy tu GET_RESPOND										SENT	30		wys�any
GET_RESPOND				pobiera urz.potwierdenie odbioru							CONFIRM	30		wys�any gdy istnieje UPO
dodatkowo
SIGN_DIR				podpisuje dokumenty - zawiera sekwencj� SAVE_TO_FILE + 
						sign by DLL (np.KIR) + 
						LOAD_FROM_FILE
						
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


Komunikat dla płatników wysyłających informację za nierezydentów

W związku z mogącymi powstać trudnościami przy przesyłaniu deklaracji podatkowych drogą elektroniczną przez płatników podatku PIT, 
w sytuacji gdy przesyłane informacje dotyczą osób niebędących rezydentami podatkowymi w Polsce i nie posiadających polskiego identyfikatora podatkowego, 
Minister Finansów  informuje, iż we wskazanych wyżej przypadkach w formularzach informacji płatnika PIT, 
w polu identyfikatora podatkowego NIP należy podać następujący ciąg cyfr: „9999999999". 
Tak podany zestaw danych będzie interpretowany przez systemy Administracji Podatkowej jako brak identyfikatora podatkowego dla osoby wskazanej w przekazywanej przez płatnika podatku PIT informacji.
Publikacja:  2014.12.10 11:11 Aktualizacja:  2014.12.10 11:11



*/

