 SET NAMES utf8;
DELIMITER $$
/* ************************************************************************************************************************************************************************
	Narzędzia 
************************************************************************************************************************************************************************ */	

DROP PROCEDURE IF EXISTS `geo_int_adr_link_AddToLog`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `geo_int_adr_link_AddToLog`(
	IN _MODE VARCHAR(150),
	IN _Key VARCHAR(38),
	IN _ObjTable VARCHAR(38),
	IN _Czynnosc VARCHAR(38),
	IN _OldValue VARCHAR(250),
	IN _NewValue VARCHAR(250)
)
/*
	CALL geo_int_adr_link_AddToLog('INSERT', _Key, _Czynnosc, _OldValue, _NewValue);
*/
S_geo_int_adr_link_AddToLog: BEGIN
	IF _MODE = 'INSERT' THEN
		CALL log_Insert(
			'INSERT', @Log.LAST_INSERT_ID,
			@session.Registration.UserLogin, 
			CASE _ObjTable WHEN 'kontrah' THEN '1'  WHEN 'osoby' THEN '5055' END, 
			_Czynnosc, _Key, @session.Registration.IP, 
			'zmiana adresu', -- Info
			''
		);		
		INSERT INTO `logpo`
			(idnng, fld_name, `old`, `new`) 
		VALUES
			(@Log.LAST_INSERT_ID, 'Adres', QUOTE(IFNULL(_OldValue, '')), QUOTE(IFNULL(_NewValue, '')) )
		;

	END IF;
	
END;
$$

DROP PROCEDURE IF EXISTS `geo_int_adr_link_NED`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `geo_int_adr_link_NED`(
	IN _MODE VARCHAR(150),
	IN _IN_GUID VARCHAR(38),
	IN _IN_ObjGUID VARCHAR(38),
	IN _IN_ObjTable VARCHAR(38),
	IN _IN_AdrTyp VARCHAR(100),
	IN _GUID VARCHAR(38),
	IN _ObjGUID VARCHAR(38),
	IN _ObjTable VARCHAR(38),
	IN _AdrTyp VARCHAR(100),
	IN _Param_A VARCHAR(250),
	IN _Param_B VARCHAR(250)
)
S_geo_int_adr_link_NED: BEGIN
	DECLARE _Exists BOOLEAN DEFAULT FALSE;
	DECLARE _KontrGUID, _AreaGUID VARCHAR(38) DEFAULT NULL;
	DECLARE _IN_ObjTable_1, _IN_ObjTable_2 VARCHAR(255) DEFAULT NULL;
						IF _MODE LIKE 'TEST.%' THEN
							IF LOCATE(',', _IN_ObjTable) > 0 THEN
								SET _IN_ObjTable_1 = _Mid_Sep(_IN_ObjTable, ',', 1);
								SET _IN_ObjTable_2 = _Mid_Sep(_IN_ObjTable, ',', 2);
							ELSE
								SET _IN_ObjTable_1 = _IN_ObjTable;
							END IF;
						
							
							DROP TEMPORARY TABLE IF EXISTS `t_clone_geo_int_adr_link`;
							CREATE TEMPORARY TABLE `t_clone_geo_int_adr_link` LIKE `geo_int_adr_link`;
							/*
							IF _MODE = 'TEST.2' THEN
								ALTER TABLE `t_clone_geo_int_adr_link` DROP KEY `PRIMARY`;
							END IF;
							*/
							IF TRUE THEN
							--	np. klonowanie z zamowien do wz - i moze byc ich KILKA niestety np. koilka adresow kontrahenta a my musimy miec tyljko 1
								INSERT IGNORE INTO `t_clone_geo_int_adr_link` 
									SELECT * FROM `geo_int_adr_link` 
									WHERE 
										ObjGUID = _IN_ObjGUID
										-- AND IF(_IN_ObjTable IN ('', '*'), TRUE, ObjTable = _IN_ObjTable)
										AND IF(_IN_ObjTable IN ('', '*'), TRUE, ObjTable IN (_IN_ObjTable_1, _IN_ObjTable_2))
										AND IF(_IN_AdrTyp IN ('', '*'), TRUE, AdrTyp = _IN_AdrTyp)
									GROUP BY AdrTyp, ObjTable
									-- LIMIT 1
								;
							END IF;
							
							UPDATE `t_clone_geo_int_adr_link`  t
							SET
								t.GUID = 
									IF(IFNULL(_GUID, '') != '',
										_GUID,
										_VarStr(CONCAT('ADR', MD5(CONCAT(_ObjGUID, IF(_ObjTable IN('', '*'), t.ObjTable, _ObjTable), IF(_AdrTyp IN('', '*'), t.AdrTyp, _AdrTyp)))))
									),
								t.ObjGUID = _ObjGUID,		--	nowy GUID	lub dla tras WyjazdGUID
								t.ObjTable = IF(_ObjTable IN('', '*'), t.ObjTable, _ObjTable),		--	nowa tabela
								t.AdrTyp = IF(_AdrTyp IN('', '*'), t.AdrTyp, _AdrTyp),
								t.Status = 0	--	wazne bo jest to czysty nowy rekord gdy Status = 1 to zablokowany 
							;
							-- SELECT * FROM `t_clone_geo_int_adr_link`  t;
							-- INSERT IGNORE INTO `geo_int_adr_link` SELECT * FROM `t_clone_geo_int_adr_link`;
						END IF;
	
			IF _MODE = 'GET.GUID.For.objguid.objTable.AdrTyp.and.save' THEN
			/*
				CALL geo_int_adr_link_NED('GET.GUID.For.objguid.objTable.AdrTyp.and.save', '', _IN_ObjGUID, _IN_objTable, _IN_AdrTyp, '', '', '', '', '', '');
			*/
				SET @_OUT_GUID = NULL;
				SELECT
					al.GUID
                INTO
                    @.geo_int_adr_link_NED.GUID
				FROM `geo_int_adr_link` al
				WHERE
					al.objguid = _IN_ObjGUID 
					AND al.objTable = _IN_objTable 
					AND IF(_IN_AdrTyp = '', TRUE, al.AdrTyp = _IN_AdrTyp)
				LIMIT 1
				;
			END IF;

	IF _MODE LIKE 'REC.CLONE%' THEN
	/*
		CALL geo_int_adr_link_NED('REC.CLONE', '', _IN_ObjGUID, _IN_objTable, _IN_AdrTyp, '', '', '', '', '', '');
		CALL geo_int_adr_link_NED('REC.CLONE', 'DEFAULT', '', '', '', '', 'XYZ', 'zmng', 'Dost', '', '');
		CALL geo_int_adr_link_NED('REC.CLONE', '', 'Z', 'zmng', '*', '', 'Y', 'zmng', '*', '', '');  -- klonowanie adresow zam X do zam. Y (klonowanie)
		CALL geo_int_adr_link_NED('REC.CLONE', '', 'Z', 'zmng', '*', '', 'Z', 'opng', '*', '', '');  -- klonowanie adresow zam X do dokumentumentu Z (realizacja)
		
	*/
		IF LOCATE(',', _IN_ObjTable) > 0 THEN
			SET _IN_ObjTable_1 = _Mid_Sep(_IN_ObjTable, ',', 1);
			SET _IN_ObjTable_2 = _Mid_Sep(_IN_ObjTable, ',', 2);
		ELSE
			SET _IN_ObjTable_1 = _IN_ObjTable;
		END IF;
	
		DROP TEMPORARY TABLE IF EXISTS `t_clone_geo_int_adr_link`;
		/*
		bylo
		CREATE TEMPORARY TABLE `t_clone_geo_int_adr_link` LIKE `geo_int_adr_link`;
		ALTER TABLE `t_clone_geo_int_adr_link` DROP KEY `PRIMARY`;
		*/
		CREATE TEMPORARY TABLE `t_clone_geo_int_adr_link` SELECT * FROM `geo_int_adr_link` WHERE FALSE;
		
		set @.Jestem = '1';
		set @.Jestem = concat_ws('/', @.Jestem, _IN_GUID, _GUID);
		IF IFNULL(_IN_GUID, '') = 'DEFAULT' THEN
		--	Domyslny i pusty o nowym skomponowanym GUID-ie
                    set @.Jestem = concat_ws('/', @.Jestem, '1');
			SET
				_GUID = 
					IF(IFNULL(_GUID, '') != '',
						_GUID,
						CONCAT('ADR', MD5(CONCAT(_ObjGUID, _ObjTable, _AdrTyp)))
					)
			;
			IF EXISTS(SELECT * FROM `geo_int_adr_link` WHERE GUID = _GUID) THEN
				LEAVE S_geo_int_adr_link_NED;
			END IF;
			/*
			INSERT IGNORE INTO `t_clone_geo_int_adr_link` 
				SELECT * FROM `geo_int_adr_link` 
				WHERE 
					GUID = _GUID
			;
			*/
			INSERT IGNORE INTO `t_clone_geo_int_adr_link` 
			SET
				GUID = _GUID
			;
		ELSEIF IFNULL(_IN_GUID, '') = 'kontrah.OWNER.GUID' THEN
		--	Z kontrahenta z db_globals do specjalnego rekordu tylko do adresów własciciela pakietu
			INSERT IGNORE INTO `t_clone_geo_int_adr_link` 
				SELECT 
					al.* 
				FROM `geo_int_adr_link` al, `kontrah` k
				WHERE
					k.CreatorGUID = 'OWNER'
					AND
					al.ObjGUID = k.GUID
					AND
					al.objTable = 'kontrah'
					AND
					al.AdrTyp LIKE '%F;%'
				ORDER BY IF(al.AdrTyp LIKE '%KontrPlatDost%', '', al.AdrTyp)
				LIMIT 1
			;
		ELSEIF IFNULL(_IN_GUID, '') != '' THEN
		set @.Jestem = concat_ws('/', @.Jestem, '2');
		--	Nie jest to domyslny ale o konkretnym GUID-ie czyli jest TYLKO 1
			INSERT IGNORE INTO `t_clone_geo_int_adr_link` 
				SELECT * FROM `geo_int_adr_link` 
				WHERE 
					GUID = _IN_GUID
			;
		ELSE
		set @.Jestem = concat_ws('/', @.Jestem, '3');
		--	np. klonowanie z zamowien do wz - i moze byc ich KILKA niestety np. koilka adresow kontrahenta a my musimy miec tyljko 1
			INSERT IGNORE INTO `t_clone_geo_int_adr_link` 
				SELECT * FROM `geo_int_adr_link` 
				WHERE 
					ObjGUID = _IN_ObjGUID
					-- AND IF(_IN_ObjTable IN ('', '*'), TRUE, ObjTable = _IN_ObjTable)
					AND IF(_IN_ObjTable IN ('', '*'), TRUE, ObjTable IN (_IN_ObjTable_1, _IN_ObjTable_2))
					AND IF(_IN_AdrTyp IN ('', '*'), TRUE, AdrTyp = _IN_AdrTyp)
				GROUP BY AdrTyp, ObjTable
				-- LIMIT 1
			;
		END IF;
		
		set @.Jestem = concat_ws('/', @.Jestem, _GUID);
		UPDATE `t_clone_geo_int_adr_link`  t
		SET
			t.GUID = 
				IF(IFNULL(_GUID, '') != '',
					_GUID,
					_VarStr(CONCAT('ADR', MD5(CONCAT(_ObjGUID, IF(_ObjTable IN('', '*'), t.ObjTable, _ObjTable), IF(_AdrTyp IN('', '*'), t.AdrTyp, _AdrTyp)))))
				),
			t.ObjGUID = _ObjGUID,		--	nowy GUID	lub dla tras WyjazdGUID
			t.ObjTable = IF(_ObjTable IN('', '*'), t.ObjTable, _ObjTable),		--	nowa tabela
			t.AdrTyp = IF(_AdrTyp IN('', '*'), t.AdrTyp, _AdrTyp),
			t.Status = 0	--	wazne bo jest to czysty nowy rekord gdy Status = 1 to zablokowany 
		;
	--	Maly wyjatek dla areanodes
		IF _IN_ObjTable = 'erp_area_nodes' AND _IN_ObjGUID != '' THEN
			UPDATE `t_clone_geo_int_adr_link`  t
			SET
				t.AdrDescr = IFNULL((SELECT an.Nazwa FROM `erp_area_nodes` an WHERE an.GUID = _IN_ObjGUID), '')
			WHERE
				IFNULL(t.AdrDescr, '') = ''
			;
		END IF;
		
	--	Usun	
		DELETE al FROM `geo_int_adr_link` al, `t_clone_geo_int_adr_link`  t
		WHERE
			al.GUID = t.GUID 
			AND IF(_Param_A = '', TRUE, al.Status = 0) 
		;
	--	Wstaw	
		-- SELECT * FROM `t_clone_geo_int_adr_link`  t;
		INSERT IGNORE INTO `geo_int_adr_link` SELECT * FROM `t_clone_geo_int_adr_link`;
		
	END IF;
			
	IF _MODE = 'REC.DELETE' THEN
	/*
		CALL geo_int_adr_link_NED('REC.DELETE', _IN_GUID, '', '', '', '', '', '', '', '', '');
		CALL geo_int_adr_link_NED('REC.DELETE', '', '_IN_ObjGUID', 'zmng', '*', '', '', '', '', '', '');
		CALL geo_int_adr_link_NED('REC.DELETE', '', '_IN_ObjGUID', '*', '*', '', '', '', '', '', '');
	*/
		IF IFNULL(_IN_GUID, '') != '' THEN
			DELETE al FROM `geo_int_adr_link` al
			WHERE
				al.guid = _IN_GUID
				AND IF(_Param_A = '', TRUE, al.Status = 0) 
			;
		ELSE
			DELETE al FROM `geo_int_adr_link` al
			WHERE
				al.ObjGUID = _IN_ObjGUID
				AND IF(_IN_ObjTable IN ('', '*'), TRUE, al.ObjTable = _IN_ObjTable)
				AND IF(_IN_AdrTyp IN ('', '*'), TRUE, al.AdrTyp = _IN_AdrTyp)
				AND IF(_Param_A = '', TRUE, al.Status = 0) 
			;
		END IF;
	END IF;

	IF _MODE = 'REC.CANCEL.DELETE' THEN
	/*
		Gdy anulujemy educje a rekord byl nowy to w `geo_int_adr_link` mógł powstac > 0 rekordów - 
		musimy je usunac jesli nie istnie w realnym świecie doku,ent (operacja.zamówienie)
		UWAGA !!!!!!
		tutaj w _IN_ObjTable mamy 'OP' lub 'ZM' a nie 'zmng' 'opng' 'kontrah' i inne !!!!!!
	*/
		SET 
			@.DELETE.geo_int_adr_link.SQL = 
			"
						DELETE al FROM `geo_int_adr_link` al
						WHERE
							al.objGUID = #_IdnOper#
							AND al.ObjTable = '#R.xxng#'
							AND NOT EXISTS(SELECT n.GUID FROM `#R.xxng#` n WHERE n.GUID = #_IdnOper#)
						;	
			"
		;
		-- CALL _Cmd_xxNgPo_Tools('EXE.SQL', IF(_IN_ObjTable = 'opng', 'OP', 'ZM'), '', '', _IN_ObjGUID, '', @_Param_A, @_Param_B, @_Param_C, @_Param_D, @.DELETE.geo_int_adr_link.SQL);
		CALL _Cmd_xxNgPo_Tools('EXE.SQL', _IN_ObjTable, '', '', _IN_ObjGUID, '', @_Param_A, @_Param_B, @_Param_C, @_Param_D, @.DELETE.geo_int_adr_link.SQL);
	/*
		IF _IN_ObjTable = 'opng' THEN
			DELETE al FROM `geo_int_adr_link` al
			WHERE
				al.objGUID = _IN_ObjGUID
				AND al.ObjTable = _IN_ObjTable
				AND NOT EXISTS(SELECT n.GUID FROM `opng` n WHERE n.GUID = _IN_ObjGUID)
			;	
		ELSEIF _IN_ObjTable = 'zmng' THEN
			DELETE al FROM `geo_int_adr_link` al
			WHERE
				al.objGUID = _IN_ObjGUID
				AND al.ObjTable = _IN_ObjTable
				AND NOT EXISTS(SELECT n.GUID FROM `zmng` n WHERE n.GUID = _IN_ObjGUID)
			;	
		END IF;
	*/
		
	END IF;
	
	-- IF _MODE = 'SPEC.`kontrah`<-`geo_int_adr_link`' THEN
	IF _MODE IN ('SPEC.`kontrah`<-`geo_int_adr_link`', 'SPEC.kontrah z geo_int_adr_link') THEN
	/*
		CALL geo_int_adr_link_NED('SPEC.`kontrah`<-`geo_int_adr_link`', '', KontrGUID, '', '', '', '', '', '', '', '');
	*/
		UPDATE `kontrah` k, `geo_int_adr_link` al
		SET
			k.Ulica = _f_AdrLink_Select_Mask_from_adr_MASKS('<UM N>', al.Adr_Masks, '', '', ''),		-- Ulica z numerem
			k.NazwaMiasta = _f_AdrLink_Select_Mask_from_adr_MASKS('<PoM>', al.Adr_Masks, '', '', ''),	-- Poczta gdy <> "" i <> Miasta lub Miasto
			k.zipcode = al.zipcode,
			k.wojewodztwo = 
			IFNULL(
				(SELECT w.idn FROM `wojewodztwa` w WHERE w.nazwa = _f_AdrLink_Select_Mask_from_adr_MASKS('<WOJ>', al.Adr_Masks, '', '', ''))
			, 0)
		WHERE
			k.GUID = _IN_ObjGUID
			AND 
			al.ObjGUID = k.GUID
			AND
			al.objTable = 'kontrah'
			AND
			al.AdrTyp LIKE '%F;%'
		;
		-- select ROW_COUNT() AS XX;
	END IF;


	IF _MODE LIKE 'OWNER.ADR.%' THEN
	/*
		CALL geo_int_adr_link_NED('OWNER.ADR.INS', '', '', '', '', '', '', '', '', '', '');
		CALL geo_int_adr_link_NED('OWNER.ADR.DEL', '', '', '', '', '', '', '', '', '', '');
		CALL geo_int_adr_link_NED('OWNER.ADR.IReg->Adr', '', '', '', '', '', '', '', '', '', '');
		CALL geo_int_adr_link_NED('OWNER.ADR.IReg<-Adr', '', '', '', '', '', '', '', '', '', '');  
		CALL geo_int_adr_link_NED('OWNER.ADR.Kontrah->Adr', '', '', '', '', '', '', '', '', '', '');
	*/
		IF _MODE LIKE 'OWNER.ADR.INS' THEN
			INSERT IGNORE INTO `geo_int_adr_link`
			SET
				GUID = 'OWNER.ADR', 
				ObjGUID = 'OWNER',
				objTable = 'kontrah',
				AdrTyp = ';F;K;'
			;
		END IF;
		
		IF _MODE LIKE 'OWNER.ADR.DEL' THEN
			DELETE FROM `geo_int_adr_link` WHERE GUID = 'OWNER.ADR';
		END IF;

		IF _MODE LIKE 'OWNER.ADR.Kontrah->Adr' THEN
		-- klonowanie z adresu Kontrahenta czyli takiego który ma w k.CreatorGUID = 'OWNER'
			CALL geo_int_adr_link_NED('REC.CLONE', 'kontrah.OWNER.GUID', '', '', '', 'OWNER.ADR', 'OWNER', 'kontrah', ';F;K;', '', '');  -- klonowanie z adresu Kontrahenta
		END IF;

		IF _MODE LIKE 'OWNER.ADR.IReg->Adr' THEN
			SELECT
				(SELECT valuea FROM IReg WHERE idkey='Ulica' AND idpath LIKE '%MOTLAWA_DaneFirmy' LIMIT 1) AS ulica_not_GUS,
				(SELECT valuea FROM IReg WHERE idkey='Nr_domu' AND idpath LIKE '%DaneFirmy' LIMIT 1) AS NrPos,
				(SELECT valuea FROM IReg WHERE idkey='Nr_lokalu' AND idpath LIKE '%DaneFirmy' LIMIT 1) AS NrLoc,
				(SELECT valuea FROM IReg WHERE idkey='Kod_poczt' AND idpath LIKE '%DaneFirmy' LIMIT 1) AS ZipCode,
				(SELECT valuea FROM IReg WHERE idkey='Wojewodztwo' AND idpath LIKE '%MOTLAWA_DaneFirmy' LIMIT 1) AS WojStr,
				(SELECT valuea FROM IReg WHERE idkey='POWIAT' AND idpath LIKE '%MOTLAWA_DaneFirmy' LIMIT 1) AS PowStr,
				(SELECT valuea FROM IReg WHERE idkey='Gmina' AND idpath LIKE '%MOTLAWA_DaneFirmy' LIMIT 1) AS GmiStr,
				(SELECT NazwaMiasta AS miasto FROM Miasta WHERE idn=(SELECT valuei FROM IReg WHERE idkey='Miasto' AND idpath LIKE '%DaneFirmy' LIMIT 1  )) AS MiastoStr,
				'' AS xx
			INTO
				@.Owner.New.Adr.ulica_not_GUS,
				@.Owner.New.Adr.NrPos,
				@.Owner.New.Adr.NrLoc,
				@.Owner.New.Adr.ZipCode,
				@.Owner.New.Adr.WojStr,
				@.Owner.New.Adr.PowStr,
				@.Owner.New.Adr.GmiStr,
				@.Owner.New.Adr.MiastoStr,
				@.Owner.New.Adr.XXX
			;
			/*
			SET
				@.Owner.New.Adr.ulica_not_GUS = REPLACE(REPLACE(REPLACE(@.Owner.New.Adr.ulica_not_GUS,
					'ul. ', 'ul.'), 'al. ', 'al.'), 'pl. ', 'pl.' )
			;
			*/
			SET
				@.Owner.New.Adr.ulica_not_GUS.Srch = REPLACE(@.Owner.New.Adr.ulica_not_GUS,
					' ', '%' )
			;
			/*	do testów select...
			SELECT 
				al.GUID,
				al.IDCountry, 
				
				-- vWoj.ID,
				-- vPow.ID,vPow.Nazwa, @.Owner.New.Adr.PowStr,
				-- vGmi.ID,
				-- IFNULL(vGmi.Nazwa, vGmi2.Nazwa),
				-- IFNULL(vGmi.ID_Pow, vGmi2.ID_Pow),
				m.Nazwa, 
				IFNULL(m.ID, al.geo_int_miejsca_ID) AS geo_int_miejsca_ID,
				IFNULL(a.ID, al.geo_int_adresy_ID) AS geo_int_adresy_ID,
				IFNULL(NULLIF(@.Owner.New.Adr.NrPos, ''), al.NrPos) AS NrPos,
				IFNULL(NULLIF(@.Owner.New.Adr.NrLoc, ''), al.NrLoc) AS NrLoc,
				IFNULL(NULLIF(@.Owner.New.Adr.ZipCode, ''), al.ZipCode) AS ZipCode,
				IFNULL(NULLIF(@.Owner.New.Adr.ulica_not_GUS, ''), al.ulica_not_GUS) AS ulica_not_GUS,
				a.Srch_Cecha_N1_N2,
				''
			FROM `geo_int_adr_link` al
			LEFT JOIN `v_geo_int_adm_woj` vWoj ON vWoj.Nazwa = @.Owner.New.Adr.WojStr
			LEFT JOIN `v_geo_int_adm_pow` vPow ON vPow.Nazwa = @.Owner.New.Adr.PowStr AND vPow.ID_WOJ = vWoj.ID
			LEFT JOIN `v_geo_int_adm_gmi` vGmi ON vGmi.Nazwa = @.Owner.New.Adr.GmiStr AND vGmi.ID_Pow = vPow.ID
			LEFT JOIN `v_geo_int_adm_gmi` vGmi2 ON vGmi2.Nazwa = @.Owner.New.Adr.GmiStr AND vGmi2.ID_WOJ = vWoj.ID
			LEFT JOIN `v_geo_int_miejsca` m ON m.Nazwa = @.Owner.New.Adr.MiastoStr AND m.ID_GMI = IFNULL(vGmi.ID, vGmi2.ID)
			LEFT JOIN `v_geo_int_adresy` a ON a.`Srch_Cecha_N1_N2` LIKE @.Owner.New.Adr.ulica_not_GUS.Srch AND a.`ID_Miejsca` = m.ID
			-- LEFT JOIN `v_geo_int_adm_woj` vwoj ON vwoj.`Nazwa` = @.Owner.New.Adr.WojStr
			WHERE
				al.GUID = 'OWNER.ADR'
			;			
			*/
			CALL geo_int_adr_link_NED('OWNER.ADR.INS', '', '', '', '', '', '', '', '', '', '');
			UPDATE `geo_int_adr_link` al
			LEFT JOIN `v_geo_int_adm_woj` vWoj ON vWoj.Nazwa = @.Owner.New.Adr.WojStr
			LEFT JOIN `v_geo_int_adm_pow` vPow ON vPow.Nazwa = @.Owner.New.Adr.PowStr AND vPow.ID_WOJ = vWoj.ID
			LEFT JOIN `v_geo_int_adm_gmi` vGmi ON vGmi.Nazwa = @.Owner.New.Adr.GmiStr AND vGmi.ID_Pow = vPow.ID
			LEFT JOIN `v_geo_int_adm_gmi` vGmi2 ON vGmi2.Nazwa = @.Owner.New.Adr.GmiStr AND vGmi2.ID_WOJ = vWoj.ID
			LEFT JOIN `v_geo_int_miejsca` m ON m.Nazwa = @.Owner.New.Adr.MiastoStr AND m.ID_GMI = IFNULL(vGmi.ID, vGmi2.ID)
			LEFT JOIN `v_geo_int_adresy` a ON a.`Srch_Cecha_N1_N2` LIKE @.Owner.New.Adr.ulica_not_GUS.Srch AND a.`ID_Miejsca` = m.ID
			SET
				al.ObjGUID = 'OWNER',
				al.objTable = 'kontrah',
				al.AdrTyp = ';F;K;',
				al.IDCountry = al.IDCountry,
				al.geo_int_miejsca_ID = IFNULL(m.ID, al.geo_int_miejsca_ID),
				al.geo_int_adresy_ID = IFNULL(a.ID, al.geo_int_adresy_ID),
				al.NrPos = IFNULL(NULLIF(@.Owner.New.Adr.NrPos, ''), al.NrPos),
				al.NrLoc = IFNULL(NULLIF(@.Owner.New.Adr.NrLoc, ''), al.NrLoc),
				al.ZipCode = IFNULL(NULLIF(@.Owner.New.Adr.ZipCode, ''), al.ZipCode),
				al.ulica_not_GUS = IF(a.ID IS NULL, NULLIF(@.Owner.New.Adr.ulica_not_GUS, ''), NULL)
			WHERE
				al.GUID = 'OWNER.ADR'
			;			
		END IF;
		
		IF _MODE LIKE 'OWNER.ADR.IReg<-Adr' THEN
			SELECT
				-- 'Adres firmowy' AS Inf,
				IF(IFNULL(al.AdrDescr, '') = '', 
					al.Adres,
					IF(LENGTH(al.AdrDescr) > @.Max.Len.AdrDescr,
						CONCAT(SUBSTR(al.AdrDescr, 1, @.Max.Len.AdrDescr), '..., ', al.Adres),
						CONCAT(al.AdrDescr, ', ', al.Adres)
					)
				) AS Adres_text,
				_f_AdrLink_Select_Mask_from_adr_MASKS('<U>', al.Adr_Masks, '', '', '') AS UlicaStr,
				_f_AdrLink_Select_Mask_from_adr_MASKS('<U>', al.Adr_Masks, '', '', '') AS Ulica,
				al.NrPos,
				al.NrLoc,
				al.ZipCode,
				-- _f_AdrLink_Select_Mask_from_adr_MASKS('<P>', al.Adr_Masks, '', '', '') AS PocztaStr,
				_f_AdrLink_Select_Mask_from_adr_MASKS('<WOJ>', al.Adr_Masks, '', '', '') AS WojStr,
				_f_AdrLink_Select_Mask_from_adr_MASKS('<POW>', al.Adr_Masks, '', '', '') AS PowStr,
				_f_AdrLink_Select_Mask_from_adr_MASKS('<GMI>', al.Adr_Masks, '', '', '') AS GmiStr,
				_f_AdrLink_Select_Mask_from_adr_MASKS('<M>', al.Adr_Masks, '', '', '') AS MiastoStr,
				_f_AdrLink_Select_Mask_from_adr_MASKS('<ADR_P>', al.Adr_Masks, '', '', '') AS AdrPocztowyStr,
				_f_AdrLink_Select_Mask_from_adr_MASKS('<CC>', al.Adr_Masks, '', '', '') AS CountryStr
				-- _ConcatSTR(al.AdrDescr, al.Adres, ' ') AS Adres_text,
				-- al.*
			INTO
				@.Owner.New.Adr.Adres_text,
				@.Owner.New.Adr.ulica_not_GUS,
				@.Owner.New.Adr.ulica,
				@.Owner.New.Adr.NrPos,
				@.Owner.New.Adr.NrLoc,
				@.Owner.New.Adr.ZipCode,
				@.Owner.New.Adr.WojStr,
				@.Owner.New.Adr.PowStr,
				@.Owner.New.Adr.GmiStr,
				@.Owner.New.Adr.MiastoStr,
				@.Owner.New.Adr.Adres_Info,
				@.Owner.New.Adr.Kraj
			FROM `geo_int_adr_link` al
			INNER JOIN `geo_int_adr_link` al2 ON al2.ObjGUID = al.ObjGUID
			WHERE
				al.GUID = 'OWNER.ADR'
			;
			/* testowy sql
			SELECT
				@.Owner.New.Adr.Adres_text,
				@.Owner.New.Adr.ulica_not_GUS,
				@.Owner.New.Adr.ulica,
				@.Owner.New.Adr.NrPos,
				@.Owner.New.Adr.NrLoc,
				@.Owner.New.Adr.ZipCode,
				@.Owner.New.Adr.WojStr,
				@.Owner.New.Adr.PowStr,
				@.Owner.New.Adr.GmiStr,
				@.Owner.New.Adr.MiastoStr,
				@.Owner.New.Adr.Adres_text,
				@.Owner.New.Adr.Kraj
			;
			*/
			DO _IReg_Set_Value('EDIT', '', 'DaneFirmy', 'Wojewodztwo', 0, 'STRING', '', @.Owner.New.Adr.WojStr);
			DO _IReg_Set_Value('EDIT', '', 'DaneFirmy', 'Powiat', 0, 'STRING', '', @.Owner.New.Adr.PowStr);
			DO _IReg_Set_Value('EDIT', '', 'DaneFirmy', 'Gmina', 0, 'STRING', '', @.Owner.New.Adr.GmiStr);
			DO _IReg_Set_Value('EDIT', '', 'DaneFirmy', 'Ulica', 0, 'STRING', '', @.Owner.New.Adr.ulica);
			DO _IReg_Set_Value('EDIT', '', 'DaneFirmy', 'Kod_poczt', 0, 'STRING', '', @.Owner.New.Adr.ZipCode);
			DO _IReg_Set_Value('EDIT', '', 'DaneFirmy', 'Miasto', 0, 'STRING', '', @.Owner.New.Adr.MiastoStr);
			DO _IReg_Set_Value('EDIT', '', 'DaneFirmy', 'Nr_domu', 0, 'STRING', '', @.Owner.New.Adr.NrPos);
			DO _IReg_Set_Value('EDIT', '', 'DaneFirmy', 'Nr_lokalu', 0, 'STRING', '', @.Owner.New.Adr.NrLoc);
			
		END IF;
		
	END IF;

	
END;
$$

DROP PROCEDURE IF EXISTS `geo_int_adr_link_NED_SELECT`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `geo_int_adr_link_NED_SELECT`(
	IN _MODE VARCHAR(150),
	IN _ObjGUID VARCHAR(38),
	IN _ObjTable VARCHAR(38),
	IN _AdrTyp VARCHAR(100),
	IN _Param_A VARCHAR(250),
	IN _Param_B VARCHAR(250),
	IN _Param_C VARCHAR(250),
	IN _Param_D VARCHAR(250)
)
S_geo_int_adr_link_NED_SELECT: BEGIN

	DECLARE _GUID, _KontrGUID, _AreaGUID VARCHAR(38) DEFAULT NULL;
	DECLARE _SQLText MEDIUMTEXT;
	SET @.Max.Len.AdrDescr = 20;
	
	IF _MODE LIKE 'SELECT.Kontrah.ADR.K%' THEN
		SELECT
			'Adres firmowy' AS Inf,
			IF(IFNULL(al.AdrDescr, '') = '', 
				al.Adres,
				IF(LENGTH(al.AdrDescr) > @.Max.Len.AdrDescr,
					CONCAT(SUBSTR(al.AdrDescr, 1, @.Max.Len.AdrDescr), '..., ', al.Adres),
					CONCAT(al.AdrDescr, ', ', al.Adres)
				)
			) AS Adres_text,
			_f_AdrLink_Select_Mask_from_adr_MASKS('<U>', al.Adr_Masks, '', '', '') AS UlicaStr,
			_f_AdrLink_Select_Mask_from_adr_MASKS('<M>', al.Adr_Masks, '', '', '') AS MiastoStr,
			_f_AdrLink_Select_Mask_from_adr_MASKS('<P>', al.Adr_Masks, '', '', '') AS PocztaStr,
			_f_AdrLink_Select_Mask_from_adr_MASKS('<ADR_P>', al.Adr_Masks, '', '', '') AS AdrPocztowyStr,
			_f_AdrLink_Select_Mask_from_adr_MASKS('<GMI>', al.Adr_Masks, '', '', '') AS GmiStr,
			_f_AdrLink_Select_Mask_from_adr_MASKS('<POW>', al.Adr_Masks, '', '', '') AS PowStr,
			_f_AdrLink_Select_Mask_from_adr_MASKS('<WOJ>', al.Adr_Masks, '', '', '') AS WojStr,
			_f_AdrLink_Select_Mask_from_adr_MASKS('<CC>', al.Adr_Masks, '', '', '') AS CountryStr,
			-- _ConcatSTR(al.AdrDescr, al.Adres, ' ') AS Adres_text,
			al.*
		FROM `geo_int_adr_link` al
		INNER JOIN `geo_int_adr_link` al2 ON al2.ObjGUID = al.ObjGUID
		WHERE
			al.ObjGUID = _ObjGUID
			AND
			al.objTable = 'kontrah'
			AND
			al.AdrTyp LIKE '%F;%'
		GROUP BY al.GUID
		ORDER BY IF(al.AdrTyp LIKE '%KontrPlatDost%', '', al.AdrTyp)
		LIMIT 1
		;
	END IF;
	
	IF _MODE LIKE 'SELECT.ObjGUID.ADR.List%' THEN
		/*
			CALL geo_int_adr_link_NED_SELECT('SELECT.ObjGUID.ADR.List', _ObjGUID, '', '', '', '', '', '');
			
		*/
		SELECT
		-- al.AdrTyp ,
		-- @WasPlat := (SUM(al2.AdrTyp = 'Plat') > 0) AS WasPlat,
		-- @WasDost := (SUM(al2.AdrTyp = 'Dost') > 0) AS WasDost,
		-- @AdrTypy := GROUP_CONCAT(al2.AdrTyp) AS AdrTypy,
			IF(al.objTable IN ('kontrah'),
			--	Kontrahent
				IF(al.AdrTyp LIKE '%F%',	--	IF(al.AdrTyp LIKE '%F;K;W%',
					'Adres firmowy',
					IF(al.AdrTyp LIKE '%<MEMO>%',	--	IF(al.AdrTyp LIKE '%F;K;W%',
						'Adres - z notatki',
						IF(al.AdrTyp LIKE '%K%',
							'Adres korespondencyjny',
							IF(al.AdrTyp LIKE '%W%',
								'Adres wysyłkowy lub dostawy',
								'Adres inny'
							)
						)
					)
				),
			--	nie konrahenta - opng zmng etc 
				IF(al.AdrTyp LIKE '%KontrPlatDost%',
					CONCAT(
						'Adres kontrahenta', 
						IF(NOT (SUM(al2.AdrTyp = 'Plat') > 0), ', płatnika', ''), 
						IF(NOT (SUM(al2.AdrTyp = 'Dost') > 0), ', dostawy', ''),
						IF(NOT (SUM(al2.AdrTyp = 'Odbior') > 0), ', odbioru', '')
					),
					IF(al.AdrTyp LIKE '%Plat%',
						'Adres płatnika',
						IF(al.AdrTyp LIKE '%Dost%',
							'Adres dostawy',
							IF(al.AdrTyp LIKE '%Odbior%',
								'Adres odbioru',
								'Adres inny'
							)
						)
					)
				)
			) AS Inf,
			IF(IFNULL(al.AdrDescr, '') = '', 
				al.Adres,
				IF(LENGTH(al.AdrDescr) > @.Max.Len.AdrDescr,
					CONCAT(SUBSTR(al.AdrDescr, 1, @.Max.Len.AdrDescr), '..., ', al.Adres),
					CONCAT(al.AdrDescr, ', ', al.Adres)
				)
			) AS Adres_text,
			_f_AdrLink_Select_Mask_from_adr_MASKS('<U>', al.Adr_Masks, '', '', '') AS UlicaStr,
			_f_AdrLink_Select_Mask_from_adr_MASKS('<M>', al.Adr_Masks, '', '', '') AS MiastoStr,
			_f_AdrLink_Select_Mask_from_adr_MASKS('<P>', al.Adr_Masks, '', '', '') AS PocztaStr,
			_f_AdrLink_Select_Mask_from_adr_MASKS('<ADR_P>', al.Adr_Masks, '', '', '') AS AdrPocztowyStr,
			_f_AdrLink_Select_Mask_from_adr_MASKS('<GMI>', al.Adr_Masks, '', '', '') AS GmiStr,
			_f_AdrLink_Select_Mask_from_adr_MASKS('<POW>', al.Adr_Masks, '', '', '') AS PowStr,
			_f_AdrLink_Select_Mask_from_adr_MASKS('<WOJ>', al.Adr_Masks, '', '', '') AS WojStr,
			
			-- _ConcatSTR(al.AdrDescr, al.Adres, ' ') AS Adres_text,
			al.*
		FROM `geo_int_adr_link` al
		INNER JOIN `geo_int_adr_link` al2 ON al2.ObjGUID = al.ObjGUID
		WHERE
			al.ObjGUID = _ObjGUID
			AND
			IF(_ObjTable <> '', al.ObjTable = _ObjTable, TRUE)
			AND
			IF(_AdrTyp <> '', al.AdrTyp = _AdrTyp, TRUE)
		GROUP BY al.GUID
		ORDER BY IF(al.AdrTyp LIKE '%KontrPlatDost%', '', al.AdrTyp)
		;
	END IF;
			
	IF 
		_MODE LIKE 'SELECT.adr_dostawy_for_kontrah%'
		OR
		_MODE LIKE 'SELECT.adr_odbioru_for_kontrah%'
	THEN
	-- było IF _MODE IN ('SELECT.adr_dostawy_for_kontrah', 'SELECT.adr_odbioru_for_kontrah') THEN
	-- było IF _MODE LIKE 'SELECT.adr_dostawy_for_kontrah%' THEN
		/*
			CALL geo_int_adr_link_NED_SELECT('SELECT.adr_dostawy_for_kontrah', KontrGUID, '', '', '', '', '', '');
			CALL geo_int_adr_link_NED_SELECT('SELECT.adr_odbioru_for_kontrah', KontrGUID, '', '', '', '', '', '');
		*/
		SET
			_KontrGUID = _ObjGUID,
			@.AdresDostOdb = IF(_MODE LIKE 'SELECT.adr_dostawy_for_kontrah%', 'Adres dostawy', 'Adres odbioru'),
			@.AdresDostOdb_2 = IF(_MODE LIKE 'SELECT.adr_dostawy_for_kontrah%', 'Adresy dostawy z ost. zam.', 'Adresy odbioru z ost. zam.'),
			@.LIKE.al.AdrTyp = IF(_MODE LIKE 'SELECT.adr_dostawy_for_kontrah%', '%Dost%', '%Odbior%')
		;
		DROP TEMPORARY TABLE IF EXISTS `t_get_adr_dostawy_for_kontrah`;
		CREATE TEMPORARY TABLE `t_get_adr_dostawy_for_kontrah` (
		  `GUID` VARCHAR(38) NOT NULL DEFAULT '',
		  `Inf` VARCHAR(100) CHARACTER SET utf8 NOT NULL DEFAULT '',
		  `Adres` VARCHAR(250) DEFAULT NULL COMMENT 'pe³en skonkatenowany adres - np. ul. PoMorska 23b/34 80-555 Gdañsk',
		  `AdrDescr` VARCHAR(250) DEFAULT NULL COMMENT 'opis dodatkowy',
		  `NazwaAreaNodes` VARCHAR(250) DEFAULT NULL COMMENT 'nazwa lokalizacji',
		  `ObjGUID` VARCHAR(38) NOT NULL DEFAULT '' COMMENT 'kontrahent, osoba, zamówienie',
		  `ObjTable` VARCHAR(38) NOT NULL DEFAULT '' COMMENT 'kontrah, osoby, zmng, crm_commisions, etc.',
		  `AdrTyp` VARCHAR(100) NOT NULL DEFAULT '',
		  `LP` VARCHAR(100) NOT NULL DEFAULT '',
		  PRIMARY KEY GUID(GUID),
		  UNIQUE KEY Adres(Adres, NazwaAreaNodes)
		)
		;
		INSERT INTO `t_get_adr_dostawy_for_kontrah`
		SET
			GUID = '',
			Inf = 'P u s t y - edycja niestandardowa',
			LP = ''
		;
		IF _MODE IN ('SELECT.adr_dostawy_for_kontrah', 'SELECT.adr_odbioru_for_kontrah') THEN
			INSERT INTO `t_get_adr_dostawy_for_kontrah`
			SET
				GUID = 'INNY_KONTRAHENT',
				Inf = 'I m p o r t  adresu innego kontrahenta',
				LP = ' 0'
			;
		END IF;
		IF @session.globals.Owner_kontrguid IS NULL THEN
			CALL _Globals_Get('Owner_kontrguid', @session.globals.Owner_kontrguid, NULL);
		END IF;
		IF _MODE LIKE 'SELECT.adr_odbioru_for_kontrah' AND IFNULL(@session.globals.Owner_kontrguid, '') != '' THEN		
			INSERT IGNORE INTO `t_get_adr_dostawy_for_kontrah`
				SELECT 
					al.GUID, 
					IF(al.ObjTable = 'kontrah', 
						'Nasz adres',
						'Adresy wlasne'	-- 'Adres dostawy'/'Adres odbioru' dla 'kontrah_wys'
					) AS Inf,
					al.Adres, 
					al.AdrDescr,
					'' AS NazwaAreaNodes,
					al.ObjGUID, al.ObjTable, al.AdrTyp,
					IF(al.ObjTable = 'kontrah', 
						'00',
						'01'
					) AS LP
				FROM `geo_int_adr_link` al
				WHERE
					al.ObjGUID = @session.globals.Owner_kontrguid
					AND al.ObjTable LIKE 'kontrah%' -- , al.AdrType 
			;
		END IF;
		
		INSERT IGNORE INTO `t_get_adr_dostawy_for_kontrah`
			SELECT 
				al.GUID, 
				IF(al.ObjTable = 'kontrah', 
					'Adres kontrahenta',
					@.AdresDostOdb	-- 'Adres dostawy'/'Adres odbioru' dla 'kontrah_wys'
				) AS Inf,
				al.Adres, 
				al.AdrDescr,
				'' AS NazwaAreaNodes,
				al.ObjGUID, al.ObjTable, al.AdrTyp,
				IF(al.ObjTable = 'kontrah', 
					'10',
					'11'
				) AS LP
			FROM `geo_int_adr_link` al
			WHERE
				al.ObjGUID = _KontrGUID 
				AND al.ObjTable LIKE 'kontrah%' -- , al.AdrType 
		;
		IF _MODE IN ('SELECT.adr_dostawy_for_kontrah', 'SELECT.adr_odbioru_for_kontrah') OR TRUE THEN
			SET 
				@.AreaNode.KontrGUID = 
					IF(_MODE LIKE '%.Inny',
						'NIEISTNIEJE',
						@session.globals.Owner_kontrguid
					)
			;
			INSERT IGNORE INTO `t_get_adr_dostawy_for_kontrah`
				SELECT 
					al.GUID, 
					_ConcatStr('Adres lokalizacji',	IFNULL(an.Symbol, ''), ' - ') AS Inf,
					IFNULL(al.Adres, '') AS Adres, 
					al.AdrDescr,
					an.Nazwa AS NazwaAreaNodes,
					al.ObjGUID, al.ObjTable, al.AdrTyp,
					'13' AS LP
				FROM `erp_area_nodes` an, `geo_int_adr_link` al
				WHERE
					an.KontrGUID IN (_KontrGUID, @.AreaNode.KontrGUID) 
					AND al.ObjGUID = an.GUID 		-- '{6A49CB6A-F17F-4C40-97CA-F682D2F6CB11}' 
					AND al.ObjTable = 'erp_area_nodes' -- , al.AdrType 

			;
		END IF;
		
		IF _MODE IN ('SELECT.adr_dostawy_for_kontrah', 'SELECT.adr_odbioru_for_kontrah') THEN
			SET @.t_get_adr_dostawy_for_kontrah.LP = 0;
			INSERT IGNORE INTO `t_get_adr_dostawy_for_kontrah`
				SELECT 
					al.GUID, 
					@.AdresDostOdb_2 AS Inf,
					al.Adres, 
					al.AdrDescr,
					'' AS NazwaAreaNodes,
					al.ObjGUID, al.ObjTable, al.AdrTyp,
					CONCAT('12', _VarStr(@.t_get_adr_dostawy_for_kontrah.LP := @.t_get_adr_dostawy_for_kontrah.LP + 1)) AS LP 
				FROM `zmng` zn, `geo_int_adr_link` al
				WHERE
					zn.KontrGUID = _KontrGUID 
					AND al.ObjGUID = zn.GUID
					AND al.ObjTable = 'zmng' 
					AND al.AdrTyp LIKE @.LIKE.al.AdrTyp		-- '%Dost%' '%Odbior%'
				ORDER BY zn.IDN DESC
				LIMIT 10
			;
			SET @.t_get_adr_dostawy_for_kontrah.LP = 0;
		--	Raz jeszzce zam ale starsze	
			INSERT IGNORE INTO `t_get_adr_dostawy_for_kontrah`
				SELECT 
					al.GUID, 
					'Adresy dostawy ze starszych zam.' AS Inf,
					al.Adres, 
					al.AdrDescr,
					'' AS NazwaAreaNodes,
					al.ObjGUID, al.ObjTable, al.AdrTyp,
					CONCAT('14', _VarStr(@.t_get_adr_dostawy_for_kontrah.LP := @.t_get_adr_dostawy_for_kontrah.LP + 1)) AS LP 
				FROM `zmng` zn, `geo_int_adr_link` al
				WHERE
					zn.KontrGUID = _KontrGUID 
					AND al.ObjGUID = zn.GUID
					AND al.ObjTable = 'zmng' 
					AND al.AdrTyp LIKE @.LIKE.al.AdrTyp		-- '%Dost%' '%Odbior%'
				ORDER BY zn.IDN DESC
				LIMIT 30
			;
		END IF;
		
		UPDATE `t_get_adr_dostawy_for_kontrah` al
		SET
			al.AdrDescr = al.NazwaAreaNodes
		WHERE	
			IFNULL(al.AdrDescr, '') = ''  
			AND 
			IFNULL(al.NazwaAreaNodes, '') != ''
		;
		UPDATE `t_get_adr_dostawy_for_kontrah` al
		SET
			al.Adres = 
				IF(IFNULL(al.AdrDescr, '') = '', 
					al.Adres,
					IF(LENGTH(al.AdrDescr) > @.Max.Len.AdrDescr,
						CONCAT(SUBSTR(al.AdrDescr, 1, @.Max.Len.AdrDescr), '..., ', al.Adres),
						CONCAT(al.AdrDescr, ', ', al.Adres)
					)
				)
		;
		IF @session.globals.Owner = 'TRANSBUD' THEN
		/*
		W nowej procedurze dodaj mi proszę na ifdef do transbudu  :
		do adresów dostawy :
		kontrah.platnikguid='{7A2F429C-F68C-4881-B286-26A2CC3F3730}' AND kontrah.guid<>'{7A2F429C-F68C-4881-B286-26A2CC3F3730}'
		do adresów odbioeu:
		kontrah.extraA='TERMINAL'
		*/
			IF _MODE LIKE 'SELECT.adr_odbioru_for_kontrah' AND IFNULL(@session.globals.Owner_kontrguid, '') != '' THEN
				INSERT IGNORE INTO `t_get_adr_dostawy_for_kontrah`
				SELECT
				al.GUID,
				IF(al.ObjTable = 'kontrah',
				'Nasz adres',
				'Adresy wlasne'
				) AS Inf,
				al.Adres,
				al.AdrDescr,
				'' AS NazwaAreaNodes,
				al.ObjGUID, al.ObjTable, al.AdrTyp,
				IF(al.ObjTable = 'kontrah',
				'00',
				'01'
				) AS LP
				FROM `geo_int_adr_link` al
				WHERE
				al.ObjGUID = @session.globals.Owner_kontrguid
				AND al.ObjTable LIKE 'kontrah%'
				;
				INSERT IGNORE INTO `t_get_adr_dostawy_for_kontrah`
				SELECT
				al.GUID,
				CONCAT(k.nazwa) AS Inf,
				IFNULL(al.Adres, '') AS Adres,
				al.AdrDescr,
				an.Nazwa AS NazwaAreaNodes,
				al.ObjGUID, al.ObjTable, al.AdrTyp,
				'13' AS LP
				FROM `erp_area_nodes` an, `geo_int_adr_link` al,kontrah k
				WHERE

				k.extraA='TERMINAL'
				AND al.ObjGUID = an.GUID
				AND k.guid=an.kontrguid

				AND al.ObjTable = 'erp_area_nodes' ;



			END IF;
		
			IF _MODE LIKE 'SELECT.adr_dostawy_for_kontrah' AND IFNULL(@session.globals.Owner_kontrguid, '') != '' THEN
				INSERT IGNORE INTO `t_get_adr_dostawy_for_kontrah`

				SELECT
				al.GUID,
				CONCAT(k.nazwa,' -  ',k.symbol) AS Inf,
				IFNULL(al.Adres, '') AS Adres,
				al.AdrDescr,
				an.Nazwa AS NazwaAreaNodes,
				al.ObjGUID, al.ObjTable, al.AdrTyp,
				'13' AS LP
				FROM `erp_area_nodes` an, `geo_int_adr_link` al,kontrah k
				WHERE

				k.platnikguid='{7A2F429C-F68C-4881-B286-26A2CC3F3730}' AND k.guid<>'{7A2F429C-F68C-4881-B286-26A2CC3F3730}'
				AND al.ObjGUID = an.GUID
				AND k.guid=an.kontrguid

				AND al.ObjTable = 'erp_area_nodes'
				;		
			END IF;
		END IF;
		
		SELECT t.* FROM `t_get_adr_dostawy_for_kontrah` t ORDER BY LP;
	END IF;
	
	/*
	IF _MODE LIKE 'SELECT.adr_dostawy_for_kontrah%' THEN
	END IF;
	*/
	
	IF _MODE LIKE 'DOC.SELECT.%' THEN
			IF _MODE = 'DOC.SELECT.Prepare.SQL.UNION' THEN
				/*
					CALL geo_int_adr_link_NED_SELECT('DOC.SELECT.Prepare.SQL.UNION', _ObjGUID, _ObjTable, _AdrTyp, '', '', '', '');
					IN _ObjTable VARCHAR(38),
					IN _AdrTyp VARCHAR(100),
				*/
				SET _SQLText = 
				"
					INSERT IGNORE INTO `t_doc_adr_1`		
					(AdrTypFin, GUID, AdrTyp, A, Z)
					SELECT 
						'#AdrTyp#' AS AdrTypFin,
						al.GUID, 
						al.AdrTyp,
						al.Adres,
						al.ZipCode
						-- , al.*
					FROM `geo_int_adr_link` al
					WHERE
						objGUID = '#ObjGUID#'
						AND
						objTable = '#objTable#'
						AND
						AdrTyp LIKE '%#AdrTyp#%'
					ORDER BY LENGTH(al.AdrTyp) 
					LIMIT 1
					;
				"
			;
				SET _SQLText = REPLACE(_SQLText, '#ObjGUID#', _ObjGUID);
				SET _SQLText = REPLACE(_SQLText, '#objTable#', _objTable);
				SET _SQLText = REPLACE(_SQLText, '#AdrTyp#', _AdrTyp);
				SET @_SQLText = _SQLText;
				CALL _ExecuteCmd(_SQLText, @Error);
			END IF;
	--	Wywal ten MODE lada chwila !!!!!!!!!!!!!!!!!!!!!!!!!!!
		IF _MODE = 'DOC.SELECT.Prepare' THEN
		/*
			CALL geo_int_adr_link_NED_SELECT('DOC.SELECT.Prepare', _ObjGUID, _ObjTable, _AdrTyp, '', '', '', '');
			IN _ObjTable VARCHAR(38),
			IN _AdrTyp VARCHAR(100),
		*/
			DROP TEMPORARY TABLE IF EXISTS `t_doc_adr_1`;
			CREATE TEMPORARY TABLE `t_doc_adr_1` (
			  `AdrTypFin` VARCHAR(50) CHARACTER SET utf8 NOT NULL DEFAULT '',
			  `GUID` VARCHAR(38) NOT NULL DEFAULT '',
			  `AdrTyp` VARCHAR(100) NOT NULL DEFAULT '',
			  `A` VARCHAR(250) NOT NULL DEFAULT '',
			  `U_N` VARCHAR(250) DEFAULT '',
			  `Z` VARCHAR(250) DEFAULT '',
			  `Z_M` VARCHAR(250) DEFAULT '',
			  `M` VARCHAR(250) NOT NULL DEFAULT '',
			  `WOJ` VARCHAR(250) NOT NULL DEFAULT '',
			  `POW` VARCHAR(250) NOT NULL DEFAULT '',
			  `GMI` VARCHAR(250) NOT NULL DEFAULT '',
			  `C` VARCHAR(250) NOT NULL DEFAULT '',
			  `CC` VARCHAR(250) NOT NULL DEFAULT '',
			  `CC2` VARCHAR(250) NOT NULL DEFAULT '',
			  `DESCR` VARCHAR(250) NOT NULL DEFAULT '',
			  PRIMARY KEY AdrTypFin(AdrTypFin),
			  KEY GUID(GUID)
			)
			;		
			IF @session.globals.DOC_with_AdrLink IS NULL THEN
				CALL _Globals_Get('DOC_with_AdrLink', @session.globals.DOC_with_AdrLink, FALSE);
			END IF;
			
			IF @session.globals.DOC_with_AdrLink 
			-- AND @session.globals.Owner != 'IZOHAN' --	CZASOWO!!!!!!!!!!!!!!!!!!!
			THEN
				CALL geo_int_adr_link_NED_SELECT('DOC.SELECT.Prepare.SQL.UNION', _ObjGUID, _ObjTable, 'Dost', '', '', '', '');
				CALL geo_int_adr_link_NED_SELECT('DOC.SELECT.Prepare.SQL.UNION', _ObjGUID, _ObjTable, 'Plat', '', '', '', '');
				CALL geo_int_adr_link_NED_SELECT('DOC.SELECT.Prepare.SQL.UNION', _ObjGUID, _ObjTable, 'Kontr', '', '', '', '');
				IF @session.globals.DOC_with_AdrLink_Odbior THEN
					CALL geo_int_adr_link_NED_SELECT('DOC.SELECT.Prepare.SQL.UNION', _ObjGUID, _ObjTable, 'Odbior', '', '', '', '');
				END IF;
				
				-- by adr_MASKS
				-- _f_AdrLink_Select_Mask_from_adr_MASKS('<D> <M/Gmi>', al.adr_MASKS, '', '', ''), 
				UPDATE `t_doc_adr_1` t
				LEFT JOIN `geo_int_adr_link` al ON al.GUID = t.GUID
				SET
					t.U_N = _f_AdrLink_Select_Mask_from_adr_MASKS('<U> <N>', al.adr_MASKS, '', '', ''), 
					t.Z_M = _f_AdrLink_Select_Mask_from_adr_MASKS('<Z> <MaP>', al.adr_MASKS, '', '', ''), 
					-- bylo t.M = _f_AdrLink_Select_Mask_from_adr_MASKS('<MaP>', al.adr_MASKS, '', '', ''), 
					t.M = _f_AdrLink_Select_Mask_from_adr_MASKS('<M>', al.adr_MASKS, '', '', ''), 
					t.WOJ = _f_AdrLink_Select_Mask_from_adr_MASKS('<WOJ>', al.adr_MASKS, '', '', ''), 
					t.POW = _f_AdrLink_Select_Mask_from_adr_MASKS('<POW>', al.adr_MASKS, '', '', ''), 
					t.GMI = _f_AdrLink_Select_Mask_from_adr_MASKS('<GMI>', al.adr_MASKS, '', '', ''), 
					t.C = _f_AdrLink_Select_Mask_from_adr_MASKS('<C>', al.adr_MASKS, '', '', ''), 
					t.CC = _f_AdrLink_Select_Mask_from_adr_MASKS('<CC>', al.adr_MASKS, '', '', ''), 
					t.CC2 = _f_AdrLink_Select_Mask_from_adr_MASKS('<CC2>', al.adr_MASKS, '', '', ''), 
					t.DESCR = _f_AdrLink_Select_Mask_from_adr_MASKS('<D>', al.adr_MASKS, '', '', '') 
				WHERE
					IFNULL(al.adr_MASKS, '') != ''
				;
				UPDATE `t_doc_adr_1` t
				LEFT JOIN `geo_int_adr_link` al ON al.GUID = t.GUID
				SET
					t.U_N = _f_AdrLink_MASK_for_ID('<U> <N>', al.geo_int_miejsca_ID, al.geo_int_adresy_ID, al.ZipCode, al.NrPos, al.NrLoc, al.AdrDescr, al.Post_ID, al.ulica_not_GUS, '', '', ''),
					t.Z_M = _f_AdrLink_MASK_for_ID('<Z> <MaP>@', '', '', '', '', '', '', '', '', '', '', ''),
					t.M = _f_AdrLink_MASK_for_ID('<MaP>@', '', '', '', '', '', '', '', '', '', '', ''),
					t.WOJ = _f_AdrLink_MASK_for_ID('<WOJ>@', '', '', '', '', '', '', '', '', '', '', ''), 
					t.POW = _f_AdrLink_MASK_for_ID('<POW>@', '', '', '', '', '', '', '', '', '', '', ''),
					t.GMI = _f_AdrLink_MASK_for_ID('<GMI>@', '', '', '', '', '', '', '', '', '', '', ''),
					t.C = _f_AdrLink_MASK_for_ID('<C>@', '', '', '', '', '', '', '', '', '', '', ''),
					t.CC = _f_AdrLink_MASK_for_ID('<CC>@', '', '', '', '', '', '', '', '', '', '', ''), 
					t.CC2 = _f_AdrLink_MASK_for_ID('<CC2>@', '', '', '', '', '', '', '', '', '', '', ''),
					t.DESCR = _f_AdrLink_MASK_for_ID('<D>@', '', '', '', '', '', '', '', '', '', '', '') 
				WHERE
					t.U_N = ''
				;
			END IF;
		--	Teraz klasyczne adresy GDY ICH NIE MA !!!!	
		--	Kontrahent
			IF NOT EXISTS (SELECT GUID FROM `t_doc_adr_1` t WHERE AdrTypFin = 'Kontr') THEN
				IF _ObjTable = 'opng' THEN
					INSERT IGNORE INTO `t_doc_adr_1`		
					(AdrTypFin, GUID, AdrTyp, A, U_N, Z, Z_M, M, C, CC, CC2)
					-- ;
					SELECT 
						'Kontr' AS AdrTypFin,
						'' AS GUID, 
						'Kontr' AS AdrTyp,
						IF(n.kontradres != '', kontradres, CONCAT(k.ulica, ', ', k.ZipCode, ' ', k.NazwaMiasta)) AS A,
						k.ulica AS U_N,
						k.ZipCode AS Z,
						CONCAT(k.ZipCode, ' ', k.NazwaMiasta) AS Z_M,
						k.NazwaMiasta AS M,
						kr.Idn_Kraju AS C,
						kr.Nazwa AS CC,
						kr.Nazwa_2 AS CC2
						-- kr.*,
						-- k.ulica,
						-- k.NazwaMiasta
						-- , al.*
					FROM `opng` n 
					INNER JOIN kontrah k ON k.GUID = n.kontrGUID  
					LEFT JOIN Miasta mi ON mi.NazwaMiasta = k.NazwaMiasta  
					LEFT JOIN Kraje kr ON kr.idn_kraju = mi.IDNKraju   
					WHERE
						n.GUID = _ObjGUID
					;
				END IF;
				IF _ObjTable = 'zmng' THEN
					INSERT IGNORE INTO `t_doc_adr_1`		
					(AdrTypFin, GUID, AdrTyp, A, U_N, Z, Z_M, M, C, CC, CC2)
					-- ;
					SELECT 
						'Kontr' AS AdrTypFin,
						'' AS GUID, 
						'Kontr' AS AdrTyp,
						CONCAT(k.ulica, ', ', k.ZipCode, ' ', k.NazwaMiasta) AS A,
						k.ulica AS U_N,
						k.ZipCode AS Z,
						CONCAT(k.ZipCode, ' ', k.NazwaMiasta) AS Z_M,
						k.NazwaMiasta AS M,
						kr.Idn_Kraju AS C,
						kr.Nazwa AS CC,
						kr.Nazwa_2 AS CC2
						-- kr.*,
						-- k.ulica,
						-- k.NazwaMiasta
						-- , al.*
					FROM `zmng` n 
					INNER JOIN kontrah k ON k.GUID = n.kontrGUID  
					LEFT JOIN Miasta mi ON mi.NazwaMiasta = k.NazwaMiasta  
					LEFT JOIN Kraje kr ON kr.idn_kraju = mi.IDNKraju   
					WHERE
						n.GUID = _ObjGUID
					;
				END IF;
			END IF;
		--	Platnik
			IF NOT EXISTS (SELECT GUID FROM `t_doc_adr_1` t WHERE AdrTypFin = 'Plat') THEN
				IF _ObjTable = 'opng' THEN
					
					INSERT IGNORE INTO `t_doc_adr_1`		
					(AdrTypFin, GUID, AdrTyp, A, U_N, Z, Z_M, M, CC, CC2)
					-- ;
					SELECT 
						'Plat' AS AdrTypFin,
						'' AS GUID, 
						'Plat' AS AdrTyp,
						CONCAT(k_p.ulica, ', ', k_p.ZipCode, ' ', k_p.NazwaMiasta) AS A,
						k_p.ulica AS U_N,
						k_p.ZipCode AS Z,
						CONCAT(k_p.ZipCode, ' ', k_p.NazwaMiasta) AS Z_M,
						k_p.NazwaMiasta AS M,
						krp.Nazwa AS CC,
						krp.Nazwa_2 AS CC2
						-- kr.*,
						-- k.ulica,
						-- k.NazwaMiasta
						-- , al.*
					FROM `opng` n 
					-- INNER JOIN kontrah k ON k.GUID = n.kontrGUID  
					LEFT JOIN kontrah k_p ON k_p.GUID = n.platnikGUID  
					LEFT JOIN Miasta mip ON mip.NazwaMiasta = k_p.NazwaMiasta  
					LEFT JOIN Kraje krp ON krp.idn_kraju = mip.IDNKraju   
					WHERE
						n.GUID = _ObjGUID
					;
				END IF;
				IF _ObjTable = 'zmng' THEN
					
					INSERT IGNORE INTO `t_doc_adr_1`		
					(AdrTypFin, GUID, AdrTyp, A, U_N, Z, Z_M, M, CC, CC2)
					-- ;
					SELECT 
						'Plat' AS AdrTypFin,
						'' AS GUID, 
						'Plat' AS AdrTyp,
						CONCAT(k_p.ulica, ', ', k_p.ZipCode, ' ', k_p.NazwaMiasta) AS A,
						k_p.ulica AS U_N,
						k_p.ZipCode AS Z,
						CONCAT(k_p.ZipCode, ' ', k_p.NazwaMiasta) AS Z_M,
						k_p.NazwaMiasta AS M,
						krp.Nazwa AS CC,
						krp.Nazwa_2 AS CC2
						-- kr.*,
						-- k.ulica,
						-- k.NazwaMiasta
						-- , al.*
					FROM `zmng` n 
					-- INNER JOIN kontrah k ON k.GUID = n.kontrGUID  
					LEFT JOIN kontrah k_p ON k_p.GUID = n.platnikGUID  
					LEFT JOIN Miasta mip ON mip.NazwaMiasta = k_p.NazwaMiasta  
					LEFT JOIN Kraje krp ON krp.idn_kraju = mip.IDNKraju   
					WHERE
						n.GUID = _ObjGUID
					;
				END IF;
			END IF;

			--	Dostawa 1
			IF NOT EXISTS (SELECT GUID FROM `t_doc_adr_1` t WHERE AdrTypFin = 'Dost') THEN
				IF _ObjTable = 'opng' THEN
					INSERT IGNORE INTO `t_doc_adr_1`		
					(AdrTypFin, GUID, AdrTyp, A, U_N, Z, Z_M, M, C, CC, CC2)
					SELECT 
						'Dost' AS AdrTypFin,
						'' AS GUID, 
						'Dost' AS AdrTyp,
						al.Adres AS A,
						_f_AdrLink_MASK_for_ID('<U> <N>', al.geo_int_miejsca_ID, al.geo_int_adresy_ID, al.ZipCode, al.NrPos, al.NrLoc, al.AdrDescr, al.Post_ID, al.ulica_not_GUS, '', '', '') AS U_N,
						al.ZipCode AS Z,
						_f_AdrLink_MASK_for_ID('<Z> <MaP>@', '', '', '', '', '', '', '', '', '', '', '') AS Z_M,
						_f_AdrLink_MASK_for_ID('<MaP>@', '', '', '', '', '', '', '', '', '', '', '') AS M,
						_f_AdrLink_MASK_for_ID('<C>@', '', '', '', '', '', '', '', '', '', '', '') AS C,
						_f_AdrLink_MASK_for_ID('<CC>@', '', '', '', '', '', '', '', '', '', '', '') AS CC,
						_f_AdrLink_MASK_for_ID('<CC2>@', '', '', '', '', '', '', '', '', '', '', '') AS CC2
						-- kr.*,
						-- k.ulica,
						-- k.NazwaMiasta
						-- , al.*
					FROM `opng` n 
					-- INNER JOIN `erp_area_nodes` an ON an.GUID = n.AreaGUID  
					INNER JOIN `geo_int_adr_link` al ON al.ObjGUID = n.AreaGUID
					WHERE
						n.GUID = _ObjGUID
					;
				END IF;
				
				IF _ObjTable = 'zmng' THEN
					
					INSERT IGNORE INTO `t_doc_adr_1`		
					(AdrTypFin, GUID, AdrTyp, A, U_N, Z, Z_M, M, C, CC, CC2)
					SELECT 
						'Dost' AS AdrTypFin,
						'' AS GUID, 
						'Dost' AS AdrTyp,
						al.Adres AS A,
						_f_AdrLink_MASK_for_ID('<U> <N>', al.geo_int_miejsca_ID, al.geo_int_adresy_ID, al.ZipCode, al.NrPos, al.NrLoc, al.AdrDescr, al.Post_ID, al.ulica_not_GUS, '', '', '') AS U_N,
						al.ZipCode AS Z,
						_f_AdrLink_MASK_for_ID('<Z> <MaP>@', '', '', '', '', '', '', '', '', '', '', '') AS Z_M,
						_f_AdrLink_MASK_for_ID('<MaP>@', '', '', '', '', '', '', '', '', '', '', '') AS M,
						_f_AdrLink_MASK_for_ID('<C>@', '', '', '', '', '', '', '', '', '', '', '') AS C,
						_f_AdrLink_MASK_for_ID('<CC>@', '', '', '', '', '', '', '', '', '', '', '') AS CC,
						_f_AdrLink_MASK_for_ID('<CC2>@', '', '', '', '', '', '', '', '', '', '', '') AS CC2
						-- kr.*,
						-- k.ulica,
						-- k.NazwaMiasta
						-- , al.*
					FROM `zmng` n 
					-- INNER JOIN `erp_area_nodes` an ON an.GUID = n.AreaGUID  
					INNER JOIN `geo_int_adr_link` al ON al.ObjGUID = n.AreaGUID
					WHERE
						n.GUID = _ObjGUID
					;
				END IF;
				
			END IF;
		--	Dostawa 2
			IF NOT EXISTS (SELECT GUID FROM `t_doc_adr_1` t WHERE AdrTypFin = 'Dost') THEN
				IF _ObjTable = 'opng' THEN
					
					INSERT IGNORE INTO `t_doc_adr_1`		
					(AdrTypFin, GUID, AdrTyp, A, U_N, Z, Z_M, M, C, CC, CC2)
					-- ;
					SELECT 
						'Dost' AS AdrTypFin,
						'' AS GUID, 
						'Dost' AS AdrTyp,
						IF(k.W_ulica != '', CONCAT(k.W_ulica, ', ', k.W_ZipCode, ' ', k.W_NazwaMiasta), '') AS A,
						k.W_ulica AS U_N,
						k.W_ZipCode AS Z,
						CONCAT(k.W_ZipCode, ' ', k.W_NazwaMiasta) AS Z_M,
						k.W_NazwaMiasta AS M,
						kr.Idn_Kraju AS C,
						kr.Nazwa AS CC,
						kr.Nazwa_2 AS CC2
						-- kr.*,
						-- k.ulica,
						-- k.NazwaMiasta
						-- , al.*
					FROM `opng` n 
					INNER JOIN kontrah k ON k.GUID = n.kontrGUID  
					LEFT JOIN Miasta m ON m.NazwaMiasta = k.W_NazwaMiasta  
					LEFT JOIN Kraje kr ON kr.idn_kraju = m.IDNKraju   
					WHERE
						n.GUID = _ObjGUID
						AND
						k.W_NazwaMiasta != ''
					;
				END IF;
				
				IF _ObjTable = 'zmng' THEN
					
					INSERT IGNORE INTO `t_doc_adr_1`		
					(AdrTypFin, GUID, AdrTyp, A, U_N, Z, Z_M, M, C, CC, CC2)
					-- ;
					SELECT 
						'Dost' AS AdrTypFin,
						'' AS GUID, 
						'Dost' AS AdrTyp,
						IF(k.W_ulica != '', CONCAT(k.W_ulica, ', ', k.W_ZipCode, ' ', k.W_NazwaMiasta), '') AS A,
						k.W_ulica AS U_N,
						k.W_ZipCode AS Z,
						CONCAT(k.W_ZipCode, ' ', k.W_NazwaMiasta) AS Z_M,
						k.W_NazwaMiasta AS M,
						kr.Idn_Kraju AS C,
						kr.Nazwa AS CC,
						kr.Nazwa_2 AS CC2
						-- kr.*,
						-- k.ulica,
						-- k.NazwaMiasta
						-- , al.*
					FROM `zmng` n 
					INNER JOIN kontrah k ON k.GUID = n.kontrGUID  
					LEFT JOIN Miasta m ON m.NazwaMiasta = k.W_NazwaMiasta  
					LEFT JOIN Kraje kr ON kr.idn_kraju = m.IDNKraju   
					WHERE
						n.GUID = _ObjGUID
						AND
						k.W_NazwaMiasta != ''
					;
				END IF;
				
				IF NOT EXISTS (SELECT GUID FROM `t_doc_adr_1` t WHERE AdrTypFin = 'Dost') THEN
				--	Gdy brak osobnego adresu dostawy to sklonuj z 'kontr'
					DROP TEMPORARY TABLE IF EXISTS `t_doc_adr_1_clone`;
					CREATE TEMPORARY TABLE `t_doc_adr_1_clone` SELECT * FROM `t_doc_adr_1` WHERE AdrTypFin = 'Kontr';
					UPDATE `t_doc_adr_1_clone` SET AdrTypFin = 'Dost';
					INSERT IGNORE INTO `t_doc_adr_1` SELECT * FROM `t_doc_adr_1_clone`;
					DROP TEMPORARY TABLE IF EXISTS `t_doc_adr_1_clone`;
				END IF;
			END IF;
		--	I jeszcze tabelka 1 rekordowa z wszystkim adresami
			DROP TEMPORARY TABLE IF EXISTS `t_doc_adr_1_one`;
			CREATE TEMPORARY TABLE `t_doc_adr_1_one` 
				SELECT
					GROUP_CONCAT(IF(AdrTypFin = 'Kontr', t.A, NULL)) AS k_A,
					GROUP_CONCAT(IF(AdrTypFin = 'Kontr', t.U_N, NULL)) AS k_U_N,
					GROUP_CONCAT(IF(AdrTypFin = 'Kontr', t.Z, NULL)) AS k_Z,
					GROUP_CONCAT(IF(AdrTypFin = 'Kontr', t.Z_M, NULL)) AS k_Z_M,
					GROUP_CONCAT(IF(AdrTypFin = 'Kontr', t.M, NULL)) AS k_M,
					GROUP_CONCAT(IF(AdrTypFin = 'Kontr', t.CC, NULL)) AS k_CC,
					GROUP_CONCAT(IF(AdrTypFin = 'Kontr', t.CC2, NULL)) AS k_CC2,
					GROUP_CONCAT(IF(AdrTypFin = 'Kontr', t.C, NULL)) AS k_C,
					GROUP_CONCAT(IF(AdrTypFin = 'Kontr', t.WOJ, NULL)) AS k_WOJ,
					GROUP_CONCAT(IF(AdrTypFin = 'Kontr', t.POW, NULL)) AS k_POW,
					GROUP_CONCAT(IF(AdrTypFin = 'Kontr', t.GMI, NULL)) AS k_GMI,
					GROUP_CONCAT(IF(AdrTypFin = 'Kontr', t.DESCR, NULL)) AS k_DESCR,

					GROUP_CONCAT(IF(AdrTypFin = 'Plat', t.A, NULL)) AS p_A,
					GROUP_CONCAT(IF(AdrTypFin = 'Plat', t.U_N, NULL)) AS p_U_N,
					GROUP_CONCAT(IF(AdrTypFin = 'Plat', t.Z, NULL)) AS p_Z,
					GROUP_CONCAT(IF(AdrTypFin = 'Plat', t.Z_M, NULL)) AS p_Z_M,
					GROUP_CONCAT(IF(AdrTypFin = 'Plat', t.M, NULL)) AS p_M,
					GROUP_CONCAT(IF(AdrTypFin = 'Plat', t.CC, NULL)) AS p_CC,
					GROUP_CONCAT(IF(AdrTypFin = 'Plat', t.CC2, NULL)) AS p_CC2,
					GROUP_CONCAT(IF(AdrTypFin = 'Plat', t.C, NULL)) AS p_C,
					GROUP_CONCAT(IF(AdrTypFin = 'Plat', t.WOJ, NULL)) AS p_WOJ,
					GROUP_CONCAT(IF(AdrTypFin = 'Plat', t.POW, NULL)) AS p_POW,
					GROUP_CONCAT(IF(AdrTypFin = 'Plat', t.GMI, NULL)) AS p_GMI,
					GROUP_CONCAT(IF(AdrTypFin = 'Plat', t.DESCR, NULL)) AS p_DESCR,

					GROUP_CONCAT(IF(AdrTypFin = 'Dost', t.A, NULL)) AS d_A,
					GROUP_CONCAT(IF(AdrTypFin = 'Dost', t.U_N, NULL)) AS d_U_N,
					GROUP_CONCAT(IF(AdrTypFin = 'Dost', t.Z, NULL)) AS d_Z,
					GROUP_CONCAT(IF(AdrTypFin = 'Dost', t.Z_M, NULL)) AS d_Z_M,
					GROUP_CONCAT(IF(AdrTypFin = 'Dost', t.M, NULL)) AS d_M,
					GROUP_CONCAT(IF(AdrTypFin = 'Dost', t.CC, NULL)) AS d_CC,
					GROUP_CONCAT(IF(AdrTypFin = 'Dost', t.CC2, NULL)) AS d_CC2,
					GROUP_CONCAT(IF(AdrTypFin = 'Dost', t.C, NULL)) AS d_C,
					GROUP_CONCAT(IF(AdrTypFin = 'Dost', t.WOJ, NULL)) AS d_WOJ,
					GROUP_CONCAT(IF(AdrTypFin = 'Dost', t.POW, NULL)) AS d_POW,
					GROUP_CONCAT(IF(AdrTypFin = 'Dost', t.GMI, NULL)) AS d_GMI,
					GROUP_CONCAT(IF(AdrTypFin = 'Dost', t.DESCR, NULL)) AS d_DESCR,
					
					GROUP_CONCAT(IF(AdrTypFin = 'Odbior', t.A, NULL)) AS O_A,
					GROUP_CONCAT(IF(AdrTypFin = 'Odbior', t.U_N, NULL)) AS O_U_N,
					GROUP_CONCAT(IF(AdrTypFin = 'Odbior', t.Z, NULL)) AS O_Z,
					GROUP_CONCAT(IF(AdrTypFin = 'Odbior', t.Z_M, NULL)) AS O_Z_M,
					GROUP_CONCAT(IF(AdrTypFin = 'Odbior', t.M, NULL)) AS O_M,
					GROUP_CONCAT(IF(AdrTypFin = 'Odbior', t.CC, NULL)) AS O_CC,
					GROUP_CONCAT(IF(AdrTypFin = 'Odbior', t.CC2, NULL)) AS O_CC2,
					GROUP_CONCAT(IF(AdrTypFin = 'Odbior', t.C, NULL)) AS O_C,
					GROUP_CONCAT(IF(AdrTypFin = 'Odbior', t.WOJ, NULL)) AS O_WOJ,
					GROUP_CONCAT(IF(AdrTypFin = 'Odbior', t.POW, NULL)) AS O_POW,
					GROUP_CONCAT(IF(AdrTypFin = 'Odbior', t.GMI, NULL)) AS O_GMI,
					GROUP_CONCAT(IF(AdrTypFin = 'Odbior', t.DESCR, NULL)) AS O_DESCR


				FROM `t_doc_adr_1` t
			;
		
			
		END IF;
	
		IF _MODE LIKE 'DOC.SELECT.DOC.INIT%' THEN
			DROP TEMPORARY TABLE IF EXISTS `t_doc_adr_INIT_1`;
			CREATE TEMPORARY TABLE `t_doc_adr_INIT_1` (
			  `AdrTyp` VARCHAR(50) CHARACTER SET utf8 NOT NULL DEFAULT '',
			  `al_GUID` VARCHAR(38) NOT NULL DEFAULT '',
			  `al_AdrTyp` VARCHAR(100) NOT NULL DEFAULT '',
			  `A` VARCHAR(250) NOT NULL DEFAULT '',
			  `U_N` VARCHAR(250) NOT NULL DEFAULT '',
			  `Z` VARCHAR(250) NOT NULL DEFAULT '',
			  `Z_M` VARCHAR(250) NOT NULL DEFAULT '',
			  `M` VARCHAR(250) NOT NULL DEFAULT '',
			  `WOJ` VARCHAR(250) NOT NULL DEFAULT '',
			  `POW` VARCHAR(250) NOT NULL DEFAULT '',
			  `GMI` VARCHAR(250) NOT NULL DEFAULT '',
			  `C` VARCHAR(250) NOT NULL DEFAULT '',
			  `CC` VARCHAR(250) NOT NULL DEFAULT '',
			  `CC2` VARCHAR(250) NOT NULL DEFAULT '',
			  `DESCR` VARCHAR(250) NOT NULL DEFAULT '',
			  `objGUID` VARCHAR(38) NOT NULL DEFAULT '',
			  `objTable` VARCHAR(250) NOT NULL DEFAULT '',
			  `Done` BOOLEAN DEFAULT FALSE,
			  PRIMARY KEY AdrTyp(AdrTyp),
			  KEY al_GUID(al_GUID),
			  KEY objGUID(objGUID),
			  KEY objTable(objTable)
			)
			;		
			TRUNCATE `t_doc_adr_INIT_1`;
			INSERT INTO `t_doc_adr_INIT_1`
			(AdrTyp, objGUID, objTable)
			VALUES
			('Kontr', _ObjGUID, _objTable), 
			('Plat', _ObjGUID, _objTable), 
			('Dost', _ObjGUID, _objTable) 
			;
			
		--	Szukaj dla kotr,platn, dost linka w geo	
			UPDATE `t_doc_adr_INIT_1` t
			SET
				t.al_GUID = 
				(
					SELECT 
						al.GUID 
					FROM `geo_int_adr_link` al
					WHERE
						al.objGUID = t.ObjGUID
						AND
						al.objTable = t.objTable
						AND
						al.AdrTyp LIKE CONCAT('%', t.AdrTyp, '%')
					ORDER BY LENGTH(al.AdrTyp) 
					LIMIT 1
				)
			;
			
		--	gdy dla dostawy nie znalazl to Szukaj dla dost linka w geo by areanode	
			UPDATE `t_doc_adr_INIT_1` t
			INNER JOIN `opng` n ON n.GUID = t.ObjGUID
			SET
				t.al_GUID = 
				IFNULL(
				IF(t.objTable = 'opng',
					
					(
						SELECT 
							al.GUID 
						FROM `opng` n, `geo_int_adr_link` al
						WHERE
							n.GUID = t.ObjGUID
							AND 
							n.AreaGUID != ''
							AND 
							al.objGUID = n.AreaGUID
							AND 
							IFNULL(al.Adres, '') != ''
						LIMIT 1
					),
					(
						SELECT 
							al.GUID 
						FROM `zmng` n, `geo_int_adr_link` al
						WHERE
							n.GUID = t.ObjGUID
							AND 
							n.AreaGUID != ''
							AND 
							al.objGUID = n.AreaGUID
							AND 
							IFNULL(al.Adres, '') != ''
						LIMIT 1
					)
				), t.al_GUID
				)
			WHERE
				NOT t.Done
				AND t.AdrTyp = 'Dost'
			;
			
		--	SELECT * FROM `t_doc_adr_INIT_1`;
						
				UPDATE `t_doc_adr_INIT_1` t
				LEFT JOIN `geo_int_adr_link` al ON al.GUID = t.al_GUID
				SET
					t.Done = TRUE,
					t.al_AdrTyp = al.AdrTyp,
					t.A = al.Adres,
					t.Z = al.ZipCode,
					t.U_N = _f_AdrLink_Select_Mask_from_adr_MASKS('<U> <N>', al.adr_MASKS, '', '', ''), 
					t.Z_M = _f_AdrLink_Select_Mask_from_adr_MASKS('<Z> <MaP>', al.adr_MASKS, '', '', ''), 
					-- bylo t.M = _f_AdrLink_Select_Mask_from_adr_MASKS('<MaP>', al.adr_MASKS, '', '', ''), 
					t.M = _f_AdrLink_Select_Mask_from_adr_MASKS('<M>', al.adr_MASKS, '', '', ''), 
					t.WOJ = _f_AdrLink_Select_Mask_from_adr_MASKS('<WOJ>', al.adr_MASKS, '', '', ''), 
					t.POW = _f_AdrLink_Select_Mask_from_adr_MASKS('<POW>', al.adr_MASKS, '', '', ''), 
					t.GMI = _f_AdrLink_Select_Mask_from_adr_MASKS('<GMI>', al.adr_MASKS, '', '', ''), 
					t.C = _f_AdrLink_Select_Mask_from_adr_MASKS('<C>', al.adr_MASKS, '', '', ''), 
					t.CC = _f_AdrLink_Select_Mask_from_adr_MASKS('<CC>', al.adr_MASKS, '', '', ''), 
					t.CC2 = _f_AdrLink_Select_Mask_from_adr_MASKS('<CC2>', al.adr_MASKS, '', '', ''), 
					t.DESCR = _f_AdrLink_Select_Mask_from_adr_MASKS('<D>', al.adr_MASKS, '', '', '') 
				WHERE
					t.al_GUID <> ''
					AND IFNULL(al.adr_MASKS, '') != ''
				;
				UPDATE `t_doc_adr_INIT_1` t
				LEFT JOIN `geo_int_adr_link` al ON al.GUID = t.al_GUID
				SET
					t.Done = TRUE,
					t.al_AdrTyp = al.AdrTyp,
					t.A = al.Adres,
					t.Z = al.ZipCode,
					t.U_N = _f_AdrLink_MASK_for_ID('<U> <N>', al.geo_int_miejsca_ID, al.geo_int_adresy_ID, al.ZipCode, al.NrPos, al.NrLoc, al.AdrDescr, al.Post_ID, al.ulica_not_GUS, '', '', ''),
					t.Z_M = _f_AdrLink_MASK_for_ID('<Z> <MaP>@', '', '', '', '', '', '', '', '', '', '', ''),
					t.M = _f_AdrLink_MASK_for_ID('<MaP>@', '', '', '', '', '', '', '', '', '', '', ''),
					t.WOJ = _f_AdrLink_MASK_for_ID('<WOJ>@', '', '', '', '', '', '', '', '', '', '', ''), 
					t.POW = _f_AdrLink_MASK_for_ID('<POW>@', '', '', '', '', '', '', '', '', '', '', ''),
					t.GMI = _f_AdrLink_MASK_for_ID('<GMI>@', '', '', '', '', '', '', '', '', '', '', ''),
					t.C = _f_AdrLink_MASK_for_ID('<C>@', '', '', '', '', '', '', '', '', '', '', ''),
					t.CC = _f_AdrLink_MASK_for_ID('<CC>@', '', '', '', '', '', '', '', '', '', '', ''), 
					t.CC2 = _f_AdrLink_MASK_for_ID('<CC2>@', '', '', '', '', '', '', '', '', '', '', ''),
					t.DESCR = _f_AdrLink_MASK_for_ID('<D>@', '', '', '', '', '', '', '', '', '', '', '') 
				WHERE
					t.al_GUID <> ''
					AND t.U_N = ''
				;
		--	SELECT * FROM `t_doc_adr_INIT_1`;
		--	Kontrahent
			--	opng
				UPDATE `t_doc_adr_INIT_1` t
				INNER JOIN `opng` n ON n.GUID = t.ObjGUID
				INNER JOIN kontrah k ON k.GUID = n.kontrGUID  
				LEFT JOIN Miasta mi ON mi.NazwaMiasta = k.NazwaMiasta  
				LEFT JOIN Kraje kr ON kr.idn_kraju = mi.IDNKraju   
				SET
					t.Done = TRUE,
					t.A = IF(n.kontradres != '', kontradres, CONCAT(k.ulica, ', ', k.ZipCode, ' ', k.NazwaMiasta)),
					t.Z = k.ZipCode,
					t.U_N = k.ulica,
					t.Z_M = CONCAT(k.ZipCode, ' ', k.NazwaMiasta),
					t.M = k.NazwaMiasta,
					t.C = kr.Idn_Kraju,
					t.CC = kr.Nazwa, 
					t.CC2 = kr.Nazwa_2
				WHERE
					NOT t.Done
					AND t.AdrTyp = 'Kontr'
					AND t.objTable = 'opng'
					
				;
			--	zmng
				UPDATE `t_doc_adr_INIT_1` t
				INNER JOIN `zmng` n ON n.GUID = t.ObjGUID
				INNER JOIN kontrah k ON k.GUID = n.kontrGUID  
				LEFT JOIN Miasta mi ON mi.NazwaMiasta = k.NazwaMiasta  
				LEFT JOIN Kraje kr ON kr.idn_kraju = mi.IDNKraju   
				SET
					t.Done = TRUE,
					t.A = CONCAT(k.ulica, ', ', k.ZipCode, ' ', k.NazwaMiasta),
					t.Z = k.ZipCode,
					t.U_N = k.ulica,
					t.Z_M = CONCAT(k.ZipCode, ' ', k.NazwaMiasta),
					t.M = k.NazwaMiasta,
					t.C = kr.Idn_Kraju,
					t.CC = kr.Nazwa, 
					t.CC2 = kr.Nazwa_2
				WHERE
					NOT t.Done
					AND t.AdrTyp = 'Kontr'
					AND t.objTable = 'zmng'
				;
		--	Platnik
			--	opng
				UPDATE `t_doc_adr_INIT_1` t
				INNER JOIN `opng` n ON n.GUID = t.ObjGUID
				LEFT JOIN kontrah k_p ON k_p.GUID = n.platnikGUID  
				LEFT JOIN Miasta mip ON mip.NazwaMiasta = k_p.NazwaMiasta  
				LEFT JOIN Kraje krp ON krp.idn_kraju = mip.IDNKraju   
				SET
					t.Done = TRUE,
					t.A = CONCAT(k_p.ulica, ', ', k_p.ZipCode, ' ', k_p.NazwaMiasta),
					t.Z = k_p.ZipCode,
					t.U_N = k_p.ulica,
					t.Z_M = CONCAT(k_p.ZipCode, ' ', k_p.NazwaMiasta),
					t.M = k_p.NazwaMiasta,
					t.C = krp.Idn_Kraju,
					t.CC = krp.Nazwa, 
					t.CC2 = krp.Nazwa_2
				WHERE
					NOT t.Done
					AND t.AdrTyp = 'Plat'
					AND t.objTable = 'opng'
					
				;
			--	zmng
				UPDATE `t_doc_adr_INIT_1` t
				INNER JOIN `zmng` n ON n.GUID = t.ObjGUID
				LEFT JOIN kontrah k_p ON k_p.GUID = n.platnikGUID  
				LEFT JOIN Miasta mip ON mip.NazwaMiasta = k_p.NazwaMiasta  
				LEFT JOIN Kraje krp ON krp.idn_kraju = mip.IDNKraju   
				SET
					t.Done = TRUE,
					t.A = CONCAT(k_p.ulica, ', ', k_p.ZipCode, ' ', k_p.NazwaMiasta),
					t.Z = k_p.ZipCode,
					t.U_N = k_p.ulica,
					t.Z_M = CONCAT(k_p.ZipCode, ' ', k_p.NazwaMiasta),
					t.M = k_p.NazwaMiasta,
					t.C = krp.Idn_Kraju,
					t.CC = krp.Nazwa, 
					t.CC2 = krp.Nazwa_2
				WHERE
					NOT t.Done
					AND t.AdrTyp = 'Plat'
					AND t.objTable = 'zmng'
				;
		--	Dostawa 
			--	opng
				UPDATE `t_doc_adr_INIT_1` t
				INNER JOIN `opng` n ON n.GUID = t.ObjGUID
				INNER JOIN kontrah k ON k.GUID = n.kontrGUID AND k.W_NazwaMiasta != '' -- !!! WAZNY warunek
				LEFT JOIN Miasta m ON m.NazwaMiasta = k.W_NazwaMiasta  
				LEFT JOIN Kraje kr ON kr.idn_kraju = m.IDNKraju   
				SET
					t.Done = TRUE,
					t.A = IF(k.W_ulica != '', CONCAT(k.W_ulica, ', ', k.W_ZipCode, ' ', k.W_NazwaMiasta), ''),
					t.Z = k.W_ZipCode,
					t.U_N = k.W_ulica,
					t.Z_M = CONCAT(k.W_ZipCode, ' ', k.W_NazwaMiasta),
					t.M = k.W_NazwaMiasta,
					t.C = kr.Idn_Kraju,
					t.CC = kr.Nazwa, 
					t.CC2 = kr.Nazwa_2
				WHERE
					NOT t.Done
					AND t.AdrTyp = 'Dost'
					AND t.objTable = 'opng'
					
				;
			--	zmng
				UPDATE `t_doc_adr_INIT_1` t
				INNER JOIN `zmng` n ON n.GUID = t.ObjGUID
				INNER JOIN kontrah k ON k.GUID = n.kontrGUID AND k.W_NazwaMiasta != '' -- !!! WAZNY warunek
				LEFT JOIN Miasta m ON m.NazwaMiasta = k.W_NazwaMiasta  
				LEFT JOIN Kraje kr ON kr.idn_kraju = m.IDNKraju   
				SET
					t.Done = TRUE,
					t.A = IF(k.W_ulica != '', CONCAT(k.W_ulica, ', ', k.W_ZipCode, ' ', k.W_NazwaMiasta), ''),
					t.Z = k.W_ZipCode,
					t.U_N = k.W_ulica,
					t.Z_M = CONCAT(k.W_ZipCode, ' ', k.W_NazwaMiasta),
					t.M = k.W_NazwaMiasta,
					t.C = kr.Idn_Kraju,
					t.CC = kr.Nazwa, 
					t.CC2 = kr.Nazwa_2
				WHERE
					NOT t.Done
					AND t.AdrTyp = 'Dost'
					AND t.objTable = 'zmng'
				;
				
				
		--	SELECT * FROM `t_doc_adr_INIT_1`;
			
		
			DROP TEMPORARY TABLE IF EXISTS `t_doc_adr_FIN_1`;
			CREATE TEMPORARY TABLE `t_doc_adr_FIN_1` LIKE t_doc_adr_INIT_1;
			INSERT IGNORE INTO `t_doc_adr_FIN_1` SELECT t.* FROM `t_doc_adr_INIT_1` t WHERE t.Done;
			
			IF NOT EXISTS (SELECT t.AdrTyp FROM `t_doc_adr_INIT_1` t WHERE t.AdrTyp = 'Dost' AND t.Done) THEN
			--	Gdy brak osobnego adresu dostawy to sklonuj z 'kontr'
				DROP TEMPORARY TABLE IF EXISTS `t_doc_adr_FIN_1_clone`;
				CREATE TEMPORARY TABLE `t_doc_adr_FIN_1_clone` SELECT t.* FROM `t_doc_adr_INIT_1` t WHERE t.AdrTyp = 'Kontr';
				UPDATE `t_doc_adr_FIN_1_clone` SET AdrTyp = 'Dost';
				INSERT IGNORE INTO `t_doc_adr_FIN_1` SELECT t.* FROM `t_doc_adr_FIN_1_clone` t WHERE t.Done;
				DROP TEMPORARY TABLE IF EXISTS `t_doc_adr_FIN_1_clone`;
			END IF;
		END IF;
		
		IF _MODE LIKE 'DOC.SELECT.ALL.%.Prepare' THEN
		--	ponize wolam tylko aby zdobyc tabele `t_doc_adr_1_one`
			CALL geo_int_adr_link_NED_SELECT('DOC.SELECT.Prepare', 'nie ma takiego', 'opng', '', '', '', '', '');
			DROP TEMPORARY TABLE IF EXISTS `t_multi_doc_adr_1_one`;
			CREATE TEMPORARY TABLE `t_multi_doc_adr_1_one` LIKE `t_doc_adr_1_one`;
			ALTER TABLE `t_multi_doc_adr_1_one` ADD COLUMN `Dokument_Numer` VARCHAR(50) NOT NULL FIRST;
			ALTER TABLE `t_multi_doc_adr_1_one` ADD COLUMN `GUID` VARCHAR(38) NOT NULL FIRST, ADD PRIMARY KEY GUID(GUID);

			DROP TEMPORARY TABLE IF EXISTS `t_adr_all_doc`;
			CREATE TEMPORARY TABLE `t_adr_all_doc` (
				`IdnOper` VARCHAR(38) NOT NULL DEFAULT '',
				PRIMARY KEY `IdnOper`(IdnOper)
			)
			;
			IF _MODE = 'DOC.SELECT.ALL.WZ.From.Fv.Prepare' THEN
				INSERT IGNORE INTO `t_adr_all_doc`  
					SELECT p.IdnOper FROM `oppo` p WHERE p.IdnOperFin = _ObjGUID
				;
				/*
				INSERT IGNORE INTO `t_adr_all_doc` set IdnOper = '{076BA019-C152-4A77-8069-C53EF6D494ED}'; 
				INSERT IGNORE INTO `t_adr_all_doc` set IdnOper = '{B23B82BE-B52E-47C0-9AF3-C08DF5605922}'; 
				INSERT IGNORE INTO `t_adr_all_doc` set IdnOper = '{BF24CC86-1E70-4EFC-A8F3-C311A04CD583}'; 
				*/
			END IF;
			
			IF _MODE = 'DOC.SELECT.ALL.FV.From.SP.Prepare' THEN
				INSERT IGNORE INTO `t_adr_all_doc`  
					SELECT n.GUID FROM opng n WHERE n.wyjazdGUID = _ObjGUID
				;
			END IF;
			
			BEGIN
				DECLARE _GUID VARCHAR(38);
				DECLARE _error BOOL DEFAULT FALSE;
				DECLARE done BOOL DEFAULT FALSE;
				DECLARE c_t_adr_all_doc CURSOR FOR
					SELECT 	
						t.IdnOper AS GUID
					FROM `t_adr_all_doc` t
				;
				DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
				OPEN c_t_adr_all_doc;
				Loop__t_adr_all_doc: LOOP
					FETCH c_t_adr_all_doc INTO 
						_GUID
					;
					IF done THEN
						LEAVE Loop__t_adr_all_doc;
					END IF;
				--	Wykonaj zapis w 
					CALL geo_int_adr_link_NED_SELECT('DOC.SELECT.Prepare', _GUID, 'opng', '', '', '', '', '');
					INSERT IGNORE INTO `t_multi_doc_adr_1_one` 
					SELECT
						_GUID AS GUID,
						CONCAT(od.Nazwa, ' ', IF(od.Grupa = 2, n.NrdokMag, n.NrDokFin)) AS Dokument_Numer,
						t.*
					FROM `t_doc_adr_1_one` t, `opng` n, `operdoc` od
					where
						n.GUID = _GUID
						AND od.Rodzaj = n.Rodzaj
					;
				END LOOP;
				CLOSE c_t_adr_all_doc;
				
			END;
			
			-- SELECT * FROM `t_adr_all_doc`;
			-- SELECT * FROM `t_multi_doc_adr_1_one` ;
			
			
		END IF;
	
/*

		IF _MODE = 'DOC.SELECT.ALL.FV.From.SP.Prepare' THEN
		--	ponize wolam tylko aby zdobyc tabele `t_doc_adr_1_one`
			CALL geo_int_adr_link_NED_SELECT('DOC.SELECT.Prepare', 'nie ma takiego', 'opng', '', '', '', '', '');
			DROP TEMPORARY TABLE IF EXISTS `t_multi_doc_adr_1_one`;
			CREATE TEMPORARY TABLE `t_multi_doc_adr_1_one` LIKE `t_doc_adr_1_one`;
			ALTER TABLE `t_multi_doc_adr_1_one` ADD COLUMN `Dokument_Numer` VARCHAR(50) NOT NULL FIRST;
			ALTER TABLE `t_multi_doc_adr_1_one` ADD COLUMN `GUID` VARCHAR(38) NOT NULL FIRST, ADD PRIMARY KEY GUID(GUID);

			DROP TEMPORARY TABLE IF EXISTS `t_adr_all_fv_from_sp`;
			CREATE TEMPORARY TABLE `t_adr_all_fv_from_sp` (
				IdnOper VARCHAR(38) NOT NULL DEFAULT '',
				PRIMARY KEY IdnOper (IdnOper)
			)
			;
			INSERT IGNORE INTO `t_adr_all_fv_from_sp`  
				SELECT n.GUID FROM opng n WHERE n.wyjazdGUID = _ObjGUID
			;
			
			BEGIN
				DECLARE _GUID VARCHAR(38);
				DECLARE _error BOOL DEFAULT FALSE;
				DECLARE done BOOL DEFAULT FALSE;
				DECLARE c_t_adr_all_fv_from_sp CURSOR FOR
					SELECT 	
						t.IdnOper AS GUID
					FROM `t_adr_all_fv_from_sp` t
				;
				DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
				OPEN c_t_adr_all_fv_from_sp;
				Loop__t_adr_all_fv_from_sp: LOOP
					FETCH c_t_adr_all_fv_from_sp INTO 
						_GUID
					;
					IF done THEN
						LEAVE Loop__t_adr_all_fv_from_sp;
					END IF;
				--	Wykonaj zapis w 
					CALL geo_int_adr_link_NED_SELECT('DOC.SELECT.Prepare', _GUID, 'opng', '', '', '', '', '');
					INSERT IGNORE INTO `t_multi_doc_adr_1_one` 
					SELECT
						_GUID AS GUID,
						CONCAT(od.Nazwa, ' ', IF(od.Grupa = 2, n.NrdokMag, n.NrDokFin)) AS Dokument_Numer,
						t.*
					FROM `t_doc_adr_1_one` t, `opng` n, `operdoc` od
					WHERE
						n.GUID = _GUID
						AND od.Rodzaj = n.Rodzaj
					;
				END LOOP;
				CLOSE c_t_adr_all_fv_from_sp;
				
			END;
			
			-- SELECT * FROM `t_adr_all_wz_from_fv`;
			-- SELECT * FROM `t_multi_doc_adr_1_one` ;
			
			
		END IF;
	
*/
	
	END IF;
	
END$$

DROP PROCEDURE IF EXISTS `_p_AdrLink_Adr_PARTS_Tools`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `_p_AdrLink_Adr_PARTS_Tools`(
	IN _MODE VARCHAR(250),
	IN _MASK VARCHAR(150),
	IN _ID_Miejsca VARCHAR(200),	
	IN _ID_Adresu VARCHAR(200),	
	IN _ZipCode VARCHAR(255),
	IN _NrPos VARCHAR(255),
	IN _NrLoc VARCHAR(255),
	IN _AdrDescr VARCHAR(255),
	IN _Post_ID VARCHAR(200),	
	IN _ulica_not_GUS VARCHAR(255),
	IN _Param_A VARCHAR(255),
	IN _Param_B VARCHAR(255),
	IN _Param_C MEDIUMTEXT,
	OUT _Result MEDIUMTEXT
)
S_p_AdrLink_Adr_PARTS_Tools: BEGIN 

	DECLARE _ID_Miejsca_from_adres VARCHAR(255);
	
	IF _MODE IN ('VAR.Prepare', 'GET `adr_MASKS`') THEN
    SET @.Adr.Z.jmam = _ZipCode;
		SET
			@.Adr.Z = _ZipCode,
			@.Adr.N = _ConcatStr(_NrPos, _NrLoc, IF(LOCATE('/', _NrPos) > 0, '', '/')),
			@.Adr.D = _AdrDescr,
			
			@.Adr.U = '',
			@.Adr.Z = '',
			@.Adr.I = '',
			@.Adr.M = '',
			@.Adr.Woj = '',
			@.Adr.Pow = '',
			@.Adr.Gmi = '',
			@.Adr.RM = '',
			@.Adr.C = '',
			@.Adr.CC = '',
			@.Adr.CC2 = '',
			@.Adr.CPL = '',
			@.Adr.P = '',
			
			@.Adr.UM = '',
			@.Adr.MaP = '',
			@.Adr.PoM = '',
			@.Adr.GmiWoj_C = '',
			@.Adr.M_Gmi = '',
			@.Adr.M_Gmi2 = '',
			
			@_Param_A = _Param_A
			
		;
	END IF;

	IF _MODE IN ('VAR.GEO_DB_2_Var', 'GET `adr_MASKS`') THEN
		IF IFNULL(_ID_Adresu, '') = '' AND IFNULL(_ID_Miejsca, '') = '' AND IFNULL(_ulica_not_GUS, '') = '' THEN
			SET
				@.Adr.M = NULL,
				@.Adr.U = NULL
			;
			set @.moj.x = '1';
			LEAVE S_p_AdrLink_Adr_PARTS_Tools;
		END IF;
		
		IF _ID_Adresu != '' THEN
			SELECT
				a.ID_Miejsca,
				CONCAT_WS('', TRIM(c.DisplayValue), _ConcatStr(a.Nazwa_2, a.Nazwa_1, ' ')) AS Ulica,
				IF(@_geo_int_adr.ZipCode != '', @_geo_int_adr.ZipCode, IFNULL(a.ZipCode, '')) AS ZipCode
			INTO
				_ID_Miejsca_from_adres,
				@.Adr.U,
				@.Adr.Z
			FROM `geo_int_adresy` a 
			LEFT JOIN `geo_int_adresy_cechy` c ON c.Cecha = a.GUS_CECHA
			WHERE
				a.ID  = _ID_Adresu
			;
			
			-- /* bedzie potrzebne w wyzszej procedurze
			IF _ID_Miejsca = '' THEN
				SET 
					@_ID_Miejsca_from_adres = _ID_Miejsca_from_adres,
					_ID_Miejsca = _ID_Miejsca_from_adres
				;
			END IF;
			-- */
		END IF;
		
		IF _ID_Miejsca != '' THEN
			IF _ID_Adresu = '' THEN
				SET
					@.Adr.U = '',
					@.Adr.Z = ''
				;	
			END IF;
			
			SELECT
				_VarStr(
					IF(w.Nazwa IS NULL,
						m.Nazwa,
						CONCAT(
							m.Nazwa, ' ',
							IF(m.ID = m.ParentID, 
								'', 
								CONCAT(
									' (', _ConcatSepSTR(IF(rm.IsPartOf, rm.Nazwa, ''), ' '), a1.Nazwa, ')'
								)
							),
							' (', IF(NOT rm.IsPartOf, _ConcatSepSTR(rm.Nazwa, ' - '), ''), 'woj.', w.Nazwa, ', pow.', p.Nazwa, ', gm.', g.Nazwa, ')'
						)
					)
				) AS Info,
				
				
				_VarStr(
					IF(m.ID = m.ParentID, 
						_ConcatSTR(IF(rm.IsPartOf, '', IF(rm.rm IN ('01', '96'), '', rm.Nazwa)), m.Nazwa, ' '), 
						CONCAT(_ConcatSTR(IF(rm.IsPartOf, '', IF(rm.rm IN ('01', '96'), '', rm.Nazwa)), m.Nazwa, ' '), ' (', a1.Nazwa, ')')
					)
				) AS Nazwa_Miejsca,
				IFNULL(w.Nazwa, '') AS Woj_Str,  
				IFNULL(p.Nazwa, '') AS Pow_Str, 
				IFNULL(g.Nazwa, '') AS Gmi_Str, 
				-- rm.Nazwa AS Rodz_Miejsca,
				IFNULL(rm.Nazwa, '') AS Rodz_Miejsca,
				c.Code AS C,
				c.Name AS CC,
				IF(c.NameEng != '', c.NameEng, IF(c.NameC != '', c.NameC, c.Name)) AS CC2
			INTO
				@.Adr.I,
				@.Adr.M,
				@.Adr.Woj,
				@.Adr.Pow,
				@.Adr.Gmi,
				@.Adr.RM,
				@.Adr.C,
				@.Adr.CC,
				@.Adr.CC2
			FROM `geo_int_miejsca` m 
			LEFT JOIN `geo_int_rodz_miejsc` rm ON rm.rm = m.GUS_RM
			INNER JOIN `geo_int_miejsca` a1 ON a1.ID = m.ParentID
			LEFT JOIN `geo_int_adm` g ON g.ID = m.ID_Adm AND g.Typ = 1
			LEFT JOIN `geo_int_adm` w ON w.ID = CONCAT('PLW', g.GUS_WOJ)
			LEFT JOIN `geo_int_adm` p ON p.ID = CONCAT('PLW', g.GUS_WOJ, 'P', g.GUS_POW)
			INNER JOIN `geo_country` c ON c.Code = m.IDCountry
			WHERE
				m.ID  = _ID_Miejsca
			;
		END IF;
		
		IF IFNULL(_Post_ID, '') != '' THEN
			SELECT
				m.Nazwa
			INTO
				@.Adr.P
			FROM `geo_int_miejsca` m 
			WHERE
				m.ID  = _Post_ID
			;
		ELSE	
			SET @.Adr.P = '';
		END IF;
		
		SET
			@.Adr.U = IF(IFNULL(_ulica_not_GUS, '') != '', _ulica_not_GUS, @.Adr.U)
		;	
	END IF;
		
	IF _MODE IN ('VAR.Final', 'GET `adr_MASKS`') THEN
		IF IFNULL(@.Adr.Z, '') = '' THEN
			SET @.Adr.Z := IFNULL(_ZipCode, '');
		END IF;
		SET @.Adr.Z := IF(LOCATE('-', @.Adr.Z) > 0, @.Adr.Z, INSERT(@.Adr.Z, 3, 0, '-'));

	--	'<UM>'		
		SET @.Adr.UM = 
			IF(@.Adr.U != '', @.Adr.U, @.Adr.M)
		;
	--	'<MaP>'		gdy jest ulica to "Lubelec poczta Pelplin" lub "Pelplin" gdy Poczta pusta lub = miasto gdy ulica pusta to "Lubelec" lub "" czyli Poczta gdy Poczta <> Miasta lub pusto
		SET @.Adr.MaP = 
			IF(@.Adr.U != '',
			--	normalny adres z ulica
				_ConcatStr(
					@.Adr.M,
					IF(@.Adr.P IN ('', @.Adr.M), '', @.Adr.P),
					', poczta '
				),
			--	bez ulicy - zamiast ulicy bedzie miasto zatem tutaj zawsze poczta
				IF(@.Adr.P IN ('', @.Adr.M), '', @.Adr.P)
			)	
		;
		-- SET @.Adr.MaP = IF(@.Adr.MaP = '', @.Adr.M, @.Adr.MaP);
	--	'<PoM>'		Poczta gdy <> "" i <> Miasta lub Miasto
		SET @.Adr.PoM = 
			IF(@.Adr.P = '', @.Adr.M, @.Adr.P)
		;
	--	'<CPL>'		
		SET @.Adr.CPL = 
			IF(@.Adr.C = 'PL', '', @.Adr.C)
		;
	--	'<GMIWOJ/C>'		
		SET @.Adr.GmiWoj_C = 
			CONCAT(
				'(', 
				IF(@.Adr.C = 'PL',
					_ConcatStr(IF((LOCATE(CONCAT('(', @.Adr.Gmi, ')'), @.Adr.M) > 0) OR (@.Adr.Gmi = @.Adr.M) OR (@.Adr.Gmi = @.Adr.P), '', @.Adr.Gmi), @.Adr.Woj, ', '),
					@.Adr.CC
				),
				')'
			)	
		;
	--	'<M/Gmi>'
		SET @.Adr.M_Gmi =
			_ConcatStr(@.Adr.M, IF((LOCATE(CONCAT('(', @.Adr.Gmi, ')'), @.Adr.M) > 0) OR (@.Adr.Gmi = @.Adr.M) OR (@.Adr.Gmi = @.Adr.P), '', @.Adr.Gmi), ', ')
		;
	--	'<M|Gmi>'
		SET @.Adr.M_Gmi2 =
			IF((LOCATE(CONCAT('(', @.Adr.Gmi, ')'), @.Adr.M) > 0) OR (@.Adr.Gmi = @.Adr.M) OR (@.Adr.Gmi = @.Adr.P), @.Adr.M, @.Adr.Gmi)
		;
		
		
	END IF;

	IF _MODE IN ('VAR.2.Params', 'GET `adr_MASKS`') THEN
		IF IFNULL(_Param_C, '') = '' THEN
			SET _Param_C = '<M>=#@.Adr.M#&<Z>=#@.Adr.Z#&<U>=#@.Adr.U#&<N>=#@.Adr.N#&<WOJ>=#@.Adr.WOJ#&<POW>=#@.Adr.POW#&<GMI>=#@.Adr.GMI#&<C>=#@.Adr.C#&<CC>=#@.Adr.CC#&<CC2>=#@.Adr.CC2#&<CPL>=#@.Adr.CPL#&<P>=#@.Adr.P#&<UM>=#@.Adr.UM#&<MaP>=#@.Adr.MaP#&<PoM>=#@.Adr.PoM#&<GMIWOJ/C>=#@.Adr.GmiWoj_C#&<M/Gmi>=#@.Adr.M_Gmi#&<M|Gmi>=#@.Adr.M_Gmi2#&<D>=#@.Adr.D#&<I>=#@.Adr.I#&<RM>=#@.Adr.RM#&<ADR_P>=#@.Adr.UM# #@.Adr.N#, #@.Adr.Z# #@.Adr.MaP#&<UM N>=#@.Adr.UM# #@.Adr.N#&';
		END IF;

		SET _Result = 
			REPLACE(
			REPLACE(
			REPLACE(
			REPLACE(
			REPLACE(
			REPLACE(
			REPLACE(
			REPLACE(
			REPLACE(
			REPLACE(
			REPLACE(
			REPLACE(
			REPLACE(
			REPLACE(
			REPLACE(
			REPLACE(
			REPLACE(
			REPLACE(
			REPLACE(
			REPLACE(
			REPLACE(
			_Param_C,
			'#@.Adr.M#', @.Adr.M),
			'#@.Adr.Z#', @.Adr.Z),
			'#@.Adr.U#', @.Adr.U),
			'#@.Adr.N#', @.Adr.N),
			'#@.Adr.WOJ#', @.Adr.WOJ),
			'#@.Adr.POW#', @.Adr.POW),
			'#@.Adr.GMI#', @.Adr.GMI),
			'#@.Adr.C#', @.Adr.C),
			'#@.Adr.CC#', @.Adr.CC),
			'#@.Adr.CC2#', @.Adr.CC2),
			'#@.Adr.CPL#', @.Adr.CPL),
			'#@.Adr.P#', @.Adr.P),
			'#@.Adr.UM#', @.Adr.UM),
			'#@.Adr.MaP#', @.Adr.MaP),
			'#@.Adr.PoM#', @.Adr.PoM),
			'#@.Adr.GmiWoj_C#', @.Adr.GmiWoj_C),
			'#@.Adr.M_Gmi#', @.Adr.M_Gmi),
			'#@.Adr.M_Gmi2#', @.Adr.M_Gmi2),
			'#@.Adr.D#', @.Adr.D),
			'#@.Adr.I#', @.Adr.I),
			'#@.Adr.RM#', @.Adr.RM)
		;	
			
			
	
	END IF;


	
	IF _MODE = 'MASK.Get.from.params' THEN
		IF IFNULL(_Param_C, '') = '' THEN
			SET _Result = '';
		ELSE
			SET _MASK = REPLACE(_MASK, '@', '');
			SET _Result = _FetchParams_Quick('', _Param_C, _MASK, '');
			--	 czasem jest tak ze maska juz istnbieje w memo wtedy nie robi juz niczego a jesli maska zawiera wiele kombinacji to ...
			IF _Result = '' THEN
				
				SET _Result = _MASK;
				
				SET @.Metoda.1 = FALSE;
				
				IF @.Metoda.1 THEN
					SET @._REPLACE_Brackets.Exists := TRUE;
					IF @._REPLACE_Brackets.Exists THEN
						SET _Result = _REPLACE_Brackets(_Result, '<Z>', _FetchParams('', _Param_C, '<Z>', ''));
					END IF;
					IF @._REPLACE_Brackets.Exists THEN
						SET _Result = _REPLACE_Brackets(_Result, '<N>', _FetchParams('', _Param_C, '<N>', ''));
					END IF;
					IF @._REPLACE_Brackets.Exists THEN
						SET _Result = _REPLACE_Brackets(_Result, '<D>', _FetchParams('', _Param_C, '<D>', ''));
					END IF;
					IF @._REPLACE_Brackets.Exists THEN
						SET _Result = _REPLACE_Brackets(_Result, '<U>', _FetchParams('', _Param_C, '<U>', ''));
					END IF;
					IF @._REPLACE_Brackets.Exists THEN
						SET _Result = _REPLACE_Brackets(_Result, '<I>', _FetchParams('', _Param_C, '<I>', ''));
					END IF;
					IF @._REPLACE_Brackets.Exists THEN
						SET _Result = _REPLACE_Brackets(_Result, '<M>', _FetchParams('', _Param_C, '<M>', ''));
					END IF;
					IF @._REPLACE_Brackets.Exists THEN
						SET _Result = _REPLACE_Brackets(_Result, '<WOJ>', _FetchParams('', _Param_C, '<WOJ>', ''));
					END IF;
					IF @._REPLACE_Brackets.Exists THEN
						SET _Result = _REPLACE_Brackets(_Result, '<POW>', _FetchParams('', _Param_C, '<POW>', ''));
					END IF;
					IF @._REPLACE_Brackets.Exists THEN
						SET _Result = _REPLACE_Brackets(_Result, '<GMI>', _FetchParams('', _Param_C, '<GMI>', ''));
					END IF;
					IF @._REPLACE_Brackets.Exists THEN
						SET _Result = _REPLACE_Brackets(_Result, '<RM>', _FetchParams('', _Param_C, '<RM>', ''));
					END IF;
					IF @._REPLACE_Brackets.Exists THEN
						SET _Result = _REPLACE_Brackets(_Result, '<C>', _FetchParams('', _Param_C, '<C>', ''));
					END IF;
					IF @._REPLACE_Brackets.Exists THEN
						SET _Result = _REPLACE_Brackets(_Result, '<CC>', _FetchParams('', _Param_C, '<CC>', ''));
					END IF;
					IF @._REPLACE_Brackets.Exists THEN
						SET _Result = _REPLACE_Brackets(_Result, '<CC2>', _FetchParams('', _Param_C, '<CC2>', ''));
					END IF;
					IF @._REPLACE_Brackets.Exists THEN
						SET _Result = _REPLACE_Brackets(_Result, '<CPL>', _FetchParams('', _Param_C, '<CPL>', ''));
					END IF;
					IF @._REPLACE_Brackets.Exists THEN
						SET _Result = _REPLACE_Brackets(_Result, '<P>', _FetchParams('', _Param_C, '<P>', ''));
					END IF;
					IF @._REPLACE_Brackets.Exists THEN
						SET _Result = _REPLACE_Brackets(_Result, '<UM>', _FetchParams('', _Param_C, '<UM>', ''));
					END IF;
					IF @._REPLACE_Brackets.Exists THEN
						SET _Result = _REPLACE_Brackets(_Result, '<MaP>', _FetchParams('', _Param_C, '<MaP>', ''));
					END IF;
					IF @._REPLACE_Brackets.Exists THEN
						SET _Result = _REPLACE_Brackets(_Result, '<PoM>', _FetchParams('', _Param_C, '<PoM>', ''));
					END IF;
					IF @._REPLACE_Brackets.Exists THEN
						SET _Result = _REPLACE_Brackets(_Result, '<GMIWOJ/C>', _FetchParams('', _Param_C, '<GMIWOJ/C>', ''));
					END IF;
					IF @._REPLACE_Brackets.Exists THEN
						SET _Result = _REPLACE_Brackets(_Result, '<M/Gmi>', _FetchParams('', _Param_C, '<M/Gmi>', ''));
					END IF;
					IF @._REPLACE_Brackets.Exists THEN
						SET _Result = _REPLACE_Brackets(_Result, '<M|Gmi>', _FetchParams('', _Param_C, '<M|Gmi>', ''));
					END IF;
					IF @._REPLACE_Brackets.Exists THEN
						SET _Result = _REPLACE_Brackets(_Result, '<ADR>', _FetchParams('', _Param_C, '<ADR>', ''));
					END IF;
				
				END IF;

				IF not @.Metoda.1 THEN
					SET _Result = 
						REPLACE(
						REPLACE(
						REPLACE(
						REPLACE(
						REPLACE(
						REPLACE(
						REPLACE(
						REPLACE(
						REPLACE(
						REPLACE(
						REPLACE(
						REPLACE(
						REPLACE(
						REPLACE(
						REPLACE(
						REPLACE(
						REPLACE(
						REPLACE(
						REPLACE(
						REPLACE(
						REPLACE(
						REPLACE(
						REPLACE(
						_Result,
						'<Z>', _FetchParams('', _Param_C, '<Z>', '')),
						'<N>', _FetchParams('', _Param_C, '<N>', '')),
						'<D>', _FetchParams('', _Param_C, '<D>', '')),
						'<U>', _FetchParams('', _Param_C, '<U>', '')),
						'<I>', _FetchParams('', _Param_C, '<I>', '')),
						'<M>', _FetchParams('', _Param_C, '<M>', '')),
						'<WOJ>', _FetchParams('', _Param_C, '<WOJ>', '')),
						'<POW>', _FetchParams('', _Param_C, '<POW>', '')),
						'<GMI>', _FetchParams('', _Param_C, '<GMI>', '')),
						'<RM>', _FetchParams('', _Param_C, '<RM>', '')),
						'<C>', _FetchParams('', _Param_C, '<C>', '')),
						'<CC>', _FetchParams('', _Param_C, '<CC>', '')),
						'<CC2>', _FetchParams('', _Param_C, '<CC2>', '')),
						'<CPL>', _FetchParams('', _Param_C, '<CPL>', '')),
						'<P>', _FetchParams('', _Param_C, '<P>', '')),
						'<UM>', _FetchParams('', _Param_C, '<UM>', '')),
						'<MaP>', _FetchParams('', _Param_C, '<MaP>', '')),
						'<PoM>', _FetchParams('', _Param_C, '<PoM>', '')),
						'<GMIWOJ/C>', _FetchParams('', _Param_C, '<GMIWOJ/C>', '')),
						'<M/Gmi>', _FetchParams('', _Param_C, '<M/Gmi>', '')),
						'<M|Gmi>', _FetchParams('', _Param_C, '<M|Gmi>', '')),
						'<UM N>', _FetchParams('', _Param_C, '<UM N>', '')),
						'<ADR>', _FetchParams('', _Param_C, '<ADR>', ''))
					;	
						
				END IF;
			END IF;
			
			
		END IF;
	
	END IF;
	
	
	IF _MODE IN ('Simple.VAR.Prepare') THEN
	/*
	IN _ID_Miejsca VARCHAR(38),	
	IN _ID_Adresu VARCHAR(38),	
	IN _ZipCode VARCHAR(255),
	IN _NrPos VARCHAR(255),
	IN _NrLoc VARCHAR(255),
	IN _AdrDescr VARCHAR(255),
	IN _Post_ID VARCHAR(38),	
	IN _ulica_not_GUS VARCHAR(255),
	IN _Param_A VARCHAR(255),
	IN _Param_B VARCHAR(255),
	IN _Param_C MEDIUMTEXT,
	OUT _Result MEDIUMTEXT
	*/
		SET
			@.Adr.N = _ConcatStr(_NrPos, _NrLoc, IF(LOCATE('/', _NrPos) > 0, '', '/')),
			@.Adr.D = _AdrDescr,
			
			@.Adr.U = _FetchParams('', _Param_C, '<U>', ''),	--	jako napis !!!!
			@.Adr.M = _FetchParams('', _Param_C, '<M>', ''),	--	jako napis !!!!
			@.Adr.P = _FetchParams('', _Param_C, '<P>', ''),	--	jako napis !!!!
			@.Adr.Z = IFNULL(_ZipCode, ''),
			@.Adr.I = '',
			@.Adr.Woj = _FetchParams('', _Param_C, '<WOJ>', ''),	--	jako napis !!!!
			@.Adr.Pow = _FetchParams('', _Param_C, '<POW>', ''),	--	jako napis !!!!
			@.Adr.Gmi = _FetchParams('', _Param_C, '<GMI>', ''),	--	jako napis !!!!
			@.Adr.RM = '',
			@.Adr.C = _FetchParams('', _Param_C, '<C>', ''),	--	jako napis !!!!
			@.Adr.CC = _FetchParams('', _Param_C, '<CC>', ''),	--	jako napis !!!!
			@.Adr.CC2 = '',
			@.Adr.CPL = '',
			
			@.Adr.UM = '',
			@.Adr.MaP = '',
			@.Adr.PoM = '',
			@.Adr.GmiWoj_C = '',
			@.Adr.M_Gmi = '',
			@.Adr.M_Gmi2 = '',
			
			@_Param_A = _Param_A
			
		;
	END IF;
	
END$$

DROP FUNCTION IF EXISTS `_f_AdrLink_Select_Mask_from_adr_MASKS`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_f_AdrLink_Select_Mask_from_adr_MASKS`(
	_MASK VARCHAR(255),
	I_adr_MASKS VARCHAR(1000),	
	_Param_A VARCHAR(255),
	_Param_B VARCHAR(255),
	_Param_C VARCHAR(255)
) RETURNS VARCHAR(250)
    NO SQL
    DETERMINISTIC
S_f_AdrLink_Select_Mask_from_adr_MASKS: BEGIN
	DECLARE _Result VARCHAR(255);
	
	IF IFNULL(I_adr_MASKS, '')  = '' THEN
	--	RETURN '';
		RETURN IF(_PARAM_C='required','Error', '');
	ELSE
		SET _MASK = REPLACE(_MASK, '@', '');
		-- SET _Result = _FetchParams('', I_adr_MASKS, _MASK, '');
		SET _Result = _FetchParams_Quick('', I_adr_MASKS, _MASK, '');
		SET @_Result_F = _Result;
		--	 czasem jest tak ze maska juz istnbieje w memo wtedy nie robi juz niczego a jesli maska zawiera wiele kombinacji to ...
		IF _Result = '' THEN
			
			SET _Result = _MASK;
			
			SET @.Metoda.1 = FALSE;
			
			IF @.Metoda.1 THEN
				SET @._REPLACE_Brackets.Exists := TRUE;
				IF @._REPLACE_Brackets.Exists THEN
					SET _Result = _REPLACE_Brackets(_Result, '<Z>', _FetchParams('', I_adr_MASKS, '<Z>', ''));
				END IF;
				IF @._REPLACE_Brackets.Exists THEN
					SET _Result = _REPLACE_Brackets(_Result, '<N>', _FetchParams('', I_adr_MASKS, '<N>', ''));
				END IF;
				IF @._REPLACE_Brackets.Exists THEN
					SET _Result = _REPLACE_Brackets(_Result, '<D>', _FetchParams('', I_adr_MASKS, '<D>', ''));
				END IF;
				IF @._REPLACE_Brackets.Exists THEN
					SET _Result = _REPLACE_Brackets(_Result, '<U>', _FetchParams('', I_adr_MASKS, '<U>', ''));
				END IF;
				IF @._REPLACE_Brackets.Exists THEN
					SET _Result = _REPLACE_Brackets(_Result, '<I>', _FetchParams('', I_adr_MASKS, '<I>', ''));
				END IF;
				IF @._REPLACE_Brackets.Exists THEN
					SET _Result = _REPLACE_Brackets(_Result, '<M>', _FetchParams('', I_adr_MASKS, '<M>', ''));
				END IF;
				IF @._REPLACE_Brackets.Exists THEN
					SET _Result = _REPLACE_Brackets(_Result, '<WOJ>', _FetchParams('', I_adr_MASKS, '<WOJ>', ''));
				END IF;
				IF @._REPLACE_Brackets.Exists THEN
					SET _Result = _REPLACE_Brackets(_Result, '<POW>', _FetchParams('', I_adr_MASKS, '<POW>', ''));
				END IF;
				IF @._REPLACE_Brackets.Exists THEN
					SET _Result = _REPLACE_Brackets(_Result, '<GMI>', _FetchParams('', I_adr_MASKS, '<GMI>', ''));
				END IF;
				IF @._REPLACE_Brackets.Exists THEN
					SET _Result = _REPLACE_Brackets(_Result, '<RM>', _FetchParams('', I_adr_MASKS, '<RM>', ''));
				END IF;
				IF @._REPLACE_Brackets.Exists THEN
					SET _Result = _REPLACE_Brackets(_Result, '<C>', _FetchParams('', I_adr_MASKS, '<C>', ''));
				END IF;
				IF @._REPLACE_Brackets.Exists THEN
					SET _Result = _REPLACE_Brackets(_Result, '<CC>', _FetchParams('', I_adr_MASKS, '<CC>', ''));
				END IF;
				IF @._REPLACE_Brackets.Exists THEN
					SET _Result = _REPLACE_Brackets(_Result, '<CC2>', _FetchParams('', I_adr_MASKS, '<CC2>', ''));
				END IF;
				IF @._REPLACE_Brackets.Exists THEN
					SET _Result = _REPLACE_Brackets(_Result, '<CPL>', _FetchParams('', I_adr_MASKS, '<CPL>', ''));
				END IF;
				IF @._REPLACE_Brackets.Exists THEN
					SET _Result = _REPLACE_Brackets(_Result, '<P>', _FetchParams('', I_adr_MASKS, '<P>', ''));
				END IF;
				IF @._REPLACE_Brackets.Exists THEN
					SET _Result = _REPLACE_Brackets(_Result, '<UM>', _FetchParams('', I_adr_MASKS, '<UM>', ''));
				END IF;
				IF @._REPLACE_Brackets.Exists THEN
					SET _Result = _REPLACE_Brackets(_Result, '<MaP>', _FetchParams('', I_adr_MASKS, '<MaP>', ''));
				END IF;
				IF @._REPLACE_Brackets.Exists THEN
					SET _Result = _REPLACE_Brackets(_Result, '<PoM>', _FetchParams('', I_adr_MASKS, '<PoM>', ''));
				END IF;
				IF @._REPLACE_Brackets.Exists THEN
					SET _Result = _REPLACE_Brackets(_Result, '<GMIWOJ/C>', _FetchParams('', I_adr_MASKS, '<GMIWOJ/C>', ''));
				END IF;
				IF @._REPLACE_Brackets.Exists THEN
					SET _Result = _REPLACE_Brackets(_Result, '<M/Gmi>', _FetchParams('', I_adr_MASKS, '<M/Gmi>', ''));
				END IF;
				IF @._REPLACE_Brackets.Exists THEN
					SET _Result = _REPLACE_Brackets(_Result, '<M|Gmi>', _FetchParams('', I_adr_MASKS, '<M|Gmi>', ''));
				END IF;
				IF @._REPLACE_Brackets.Exists THEN
					SET _Result = _REPLACE_Brackets(_Result, '<ADR>', _FetchParams('', I_adr_MASKS, '<ADR>', ''));
				END IF;
			
			END IF;

			IF not @.Metoda.1 THEN
				SET _Result = 
					REPLACE(
					REPLACE(
					REPLACE(
					REPLACE(
					REPLACE(
					REPLACE(
					REPLACE(
					REPLACE(
					REPLACE(
					REPLACE(
					REPLACE(
					REPLACE(
					REPLACE(
					REPLACE(
					REPLACE(
					REPLACE(
					REPLACE(
					REPLACE(
					REPLACE(
					REPLACE(
					REPLACE(
					REPLACE(
					REPLACE(
					_Result,
					'<Z>', _FetchParams('', I_adr_MASKS, '<Z>', '')),
					'<N>', _FetchParams('', I_adr_MASKS, '<N>', '')),
					'<D>', _FetchParams('', I_adr_MASKS, '<D>', '')),
					'<U>', _FetchParams('', I_adr_MASKS, '<U>', '')),
					'<I>', _FetchParams('', I_adr_MASKS, '<I>', '')),
					'<M>', _FetchParams('', I_adr_MASKS, '<M>', '')),
					'<WOJ>', _FetchParams('', I_adr_MASKS, '<WOJ>', '')),
					'<POW>', _FetchParams('', I_adr_MASKS, '<POW>', '')),
					'<GMI>', _FetchParams('', I_adr_MASKS, '<GMI>', '')),
					'<RM>', _FetchParams('', I_adr_MASKS, '<RM>', '')),
					'<C>', _FetchParams('', I_adr_MASKS, '<C>', '')),
					'<CC>', _FetchParams('', I_adr_MASKS, '<CC>', '')),
					'<CC2>', _FetchParams('', I_adr_MASKS, '<CC2>', '')),
					'<CPL>', _FetchParams('', I_adr_MASKS, '<CPL>', '')),
					'<P>', _FetchParams('', I_adr_MASKS, '<P>', '')),
					'<UM>', _FetchParams('', I_adr_MASKS, '<UM>', '')),
					'<MaP>', _FetchParams('', I_adr_MASKS, '<MaP>', '')),
					'<PoM>', _FetchParams('', I_adr_MASKS, '<PoM>', '')),
					'<GMIWOJ/C>', _FetchParams('', I_adr_MASKS, '<GMIWOJ/C>', '')),
					'<M/Gmi>', _FetchParams('', I_adr_MASKS, '<M/Gmi>', '')),
					'<M|Gmi>', _FetchParams('', I_adr_MASKS, '<M|Gmi>', '')),
					'<UM N>', _FetchParams('', I_adr_MASKS, '<UM N>', '')),
					'<ADR>', _FetchParams('', I_adr_MASKS, '<ADR>', ''))
				;	
					
			END IF;
		END IF;
		
		-- RETURN TRIM(_Result);
		IF _Param_C='required' THEN
			RETURN IF(LENGTH(TRIM(_Result))=0,'Error',TRIM(_Result));
		ELSE 
			RETURN TRIM(_Result);
		END IF;
	END IF;
END;
$$



DROP PROCEDURE IF EXISTS `_Adr_MU_Parts_2_Var`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `_Adr_MU_Parts_2_Var`(
	IN _MODE VARCHAR(150),
	IN _ID_Miejsca VARCHAR(38),	
	IN _ID_Adresu VARCHAR(38),	
	IN _Post_ID VARCHAR(38),	
	IN _ulica_not_GUS VARCHAR(255),
	IN _Param_A VARCHAR(255),
	IN _Param_B VARCHAR(255),
	IN _Param_C VARCHAR(255)
)
S_Adr_MU_Parts_2_Var: BEGIN 


	DECLARE _ID_Miejsca_from_adres VARCHAR(255);
	
	SET
		@.Adr.U = '',
		@.Adr.Z = '',
		@.Adr.I = '',
		@.Adr.M = '',
		@.Adr.Woj = '',
		@.Adr.Pow = '',
		@.Adr.Gmi = '',
		@.Adr.RM = '',
		@.Adr.C = '',
		@.Adr.CC = '',
		@.Adr.CC2 = '',
		@.Adr.P = ''
	;
		set @.moj.x = '0';
	
	IF IFNULL(_ID_Adresu, '') = '' AND IFNULL(_ID_Miejsca, '') = '' AND IFNULL(_ulica_not_GUS, '') = '' THEN
		SET
			@.Adr.M = NULL,
			@.Adr.U = NULL
		;
		set @.moj.x = '1';
		LEAVE S_Adr_MU_Parts_2_Var;
	END IF;
	
	IF _ID_Adresu != '' THEN
		SELECT
			a.ID_Miejsca,
			CONCAT(TRIM(c.DisplayValue), _ConcatStr(a.Nazwa_2, a.Nazwa_1, ' ')) AS Ulica,
			IF(@_geo_int_adr.ZipCode != '', @_geo_int_adr.ZipCode, IFNULL(a.ZipCode, '')) AS ZipCode
		INTO
			_ID_Miejsca_from_adres,
			@.Adr.U,
			@.Adr.Z
		FROM `geo_int_adresy` a 
		LEFT JOIN `geo_int_adresy_cechy` c ON c.Cecha = a.GUS_CECHA
		WHERE
			a.ID  = _ID_Adresu
		;
		
		-- /* bedzie potrzebne w wyzszej procedurze
		IF _ID_Miejsca = '' THEN
			SET 
				@_ID_Miejsca_from_adres = _ID_Miejsca_from_adres,
				_ID_Miejsca = _ID_Miejsca_from_adres
			;
		END IF;
		-- */
	END IF;
	
	IF _ID_Miejsca != '' THEN
		IF _ID_Adresu = '' THEN
			SET
				@.Adr.U = '',
				@.Adr.Z = ''
			;	
		END IF;
		
		SELECT
			_VarStr(
				CONCAT(
					m.Nazwa, ' ',
					IF(m.ID = m.ParentID, 
						'', 
						CONCAT(
							' (', _ConcatSepSTR(IF(rm.IsPartOf, rm.Nazwa, ''), ' '), a1.Nazwa, ')'
						)
					),
					' (', IF(NOT rm.IsPartOf, _ConcatSepSTR(rm.Nazwa, ' - '), ''), 'woj.', w.Nazwa, ', pow.', p.Nazwa, ', gm.', g.Nazwa, ')'
				)
			) AS Info,
			
			
			_VarStr(
				IF(m.ID = m.ParentID, 
					_ConcatSTR(IF(rm.IsPartOf, '', IF(rm.rm IN ('01', '96'), '', rm.Nazwa)), m.Nazwa, ' '), 
					CONCAT(_ConcatSTR(IF(rm.IsPartOf, '', IF(rm.rm IN ('01', '96'), '', rm.Nazwa)), m.Nazwa, ' '), ' (', a1.Nazwa, ')')
				)
			) AS Nazwa_Miejsca,
			w.Nazwa AS Woj_Str,  
			p.Nazwa AS Pow_Str, 
			g.Nazwa AS Gmi_Str, 
			rm.Nazwa AS Rodz_Miejsca,
			c.Code AS C,
			c.Name AS CC,
			IF(c.NameEng = '', c.Name, c.NameEng) AS CC2
		INTO
			@.Adr.I,
			@.Adr.M,
			@.Adr.Woj,
			@.Adr.Pow,
			@.Adr.Gmi,
			@.Adr.RM,
			@.Adr.C,
			@.Adr.CC,
			@.Adr.CC2
		FROM `geo_int_miejsca` m 
		INNER JOIN `geo_int_rodz_miejsc` rm ON rm.rm = m.GUS_RM
		INNER JOIN `geo_int_miejsca` a1 ON a1.ID = m.ParentID
		LEFT JOIN `geo_int_adm` g ON g.ID = m.ID_Adm AND g.Typ = 1
		INNER JOIN `geo_int_adm` w ON w.ID = CONCAT('PLW', g.GUS_WOJ)
		INNER JOIN `geo_int_adm` p ON p.ID = CONCAT('PLW', g.GUS_WOJ, 'P', g.GUS_POW)
		LEFT JOIN `geo_country` c ON c.Code = m.IDCountry
		WHERE
			m.ID  = _ID_Miejsca
		;
	END IF;
	
	IF IFNULL(_Post_ID, '') != '' THEN
		SELECT
			m.Nazwa
		INTO
			@.Adr.P
		FROM `geo_int_miejsca` m 
		WHERE
			m.ID  = _Post_ID
		;
	ELSE	
		SET @.Adr.P = '';
	END IF;
	
	SET
		@.Adr.U = IF(IFNULL(_ulica_not_GUS, '') != '', _ulica_not_GUS, @.Adr.U)
	;	
	
END$$

DROP FUNCTION IF EXISTS `_f_AdrLink_MASK_for_ID`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_f_AdrLink_MASK_for_ID`(
	_MASK VARCHAR(150),
	_ID_Miejsca VARCHAR(38),	
	_ID_Adresu VARCHAR(38),	
	_ZipCode VARCHAR(255),
	_NrPos VARCHAR(255),
	_NrLoc VARCHAR(255),
	_AdrDescr VARCHAR(255),
	_Post_ID VARCHAR(38),	
	_ulica_not_GUS VARCHAR(255),
	_Param_A VARCHAR(255),
	_Param_B VARCHAR(255),
	_Param_C VARCHAR(255)
) RETURNS VARCHAR(250)
    NO SQL
    DETERMINISTIC
S_f_AdrLink_MASK_for_ID: BEGIN
	DECLARE _Result VARCHAR(255);
	
	SET @.adr.Params_from.Variables = (LOCATE('@', _MASK) > 0);
	SET _MASK = REPLACE(_MASK, '@', '');

	IF NOT @.adr.Params_from.Variables THEN
		SET
			@.Adr.Z = _ZipCode,
			@.Adr.N = _ConcatStr(_NrPos, _NrLoc, IF(LOCATE('/', _NrPos) > 0, '', '/')),
			@.Adr.D = _AdrDescr,
			@_Param_A = _Param_A,
			
			@_Woj = '',
			@_Powiat = '',
			@_Gmina = '',
			@_Miejsce = '',
			@_Ulica = '',
			@.Adr.I = ''
		;
		CALL _Adr_MU_Parts_2_Var('', _ID_Miejsca, _ID_Adresu, _Post_ID, _ulica_not_GUS, '', '', '');		
		IF IFNULL(@.Adr.Z, '') = '' THEN
			SET @.Adr.Z = IFNULL(_ZipCode, '');
		END IF;
	END IF;
	
	
	SET _Result = _f_AdrLink_MASK(_MASK, '', '', '');

	
	RETURN TRIM(_Result);
END;
$$

--	Tylko tutaj lokalnie wolane (w _f_AdrLink_MASK_for_ID())
DROP FUNCTION IF EXISTS `_f_AdrLink_MASK`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_f_AdrLink_MASK`(
	_MASK VARCHAR(150),
	_Param_A VARCHAR(255),
	_Param_B VARCHAR(255),
	_Param_C VARCHAR(255)
) RETURNS VARCHAR(250)
    NO SQL
    DETERMINISTIC
S_f_AdrLink_MASK: BEGIN
--	DECLARE _Result, _adr_part_Numery, _adr_part_Numery_ZipCode, _gmina_woj, _UM VARCHAR(255);
	DECLARE _Result VARCHAR(255);
	
	IF _MASK = '' THEN SET _MASK = '<ADR>'; END IF;
	SET _MASK = REPLACE(_MASK, '<ADR>', '<UM> <N>, <Z> <MaP> <GMIWOJ/C>'); 
	SET _Result = _MASK;

	SET 
		@.adr.MASK.UM = (LOCATE('<UM>', _MASK) > 0),
		@.adr.MASK.U = (LOCATE('<U>', _MASK) > 0),
		@.adr.MASK.M = (LOCATE('<M>', _MASK) > 0),
		@.adr.MASK.MaP = (LOCATE('<MaP>', _MASK) > 0),
		@.adr.MASK.PoM = (LOCATE('<PoM>', _MASK) > 0),
		@.adr.MASK.P = (LOCATE('<P>', _MASK) > 0),
		@.adr.MASK.N = (LOCATE('<N>', _MASK) > 0),
		@.adr.MASK.Z = (LOCATE('<Z>', _MASK) > 0),
		@.adr.MASK.WOJ = (LOCATE('<WOJ>', _MASK) > 0),	--	woj
		@.adr.MASK.POW = (LOCATE('<POW>', _MASK) > 0),	--	pow
		@.adr.MASK.GMI = (LOCATE('<GMI>', _MASK) > 0),	--	gmi
		@.adr.MASK.C = (LOCATE('<C>', _MASK) > 0),
		@.adr.MASK.CPL = (LOCATE('<CPL>', _MASK) > 0),
		@.adr.MASK.CC = (LOCATE('<CC>', _MASK) > 0),
		@.adr.MASK.CC2 = (LOCATE('<CC2>', _MASK) > 0),
		@.adr.MASK.GmiWoj_C = (LOCATE('<GMIWOJ/C>', _MASK) > 0),
		@.adr.MASK.M_Gmi = (LOCATE('<M/Gmi>', _MASK) > 0),
		@.adr.MASK.M_Gmi2 = (LOCATE('<M|Gmi>', _MASK) > 0),
		
		@.adr.MASK.D = (LOCATE('<D>', _MASK) > 0),
		@.adr.MASK.I = (LOCATE('<I>', _MASK) > 0),
		@.adr.MASK.RM = (LOCATE('<RM>', _MASK) > 0)
	;
	SET @._REPLACE_Brackets.Exists := TRUE;
	
	IF @.adr.MASK.UM AND @._REPLACE_Brackets.Exists THEN
		SET _Result = _REPLACE_Brackets(_Result, '<UM>', IF(@.Adr.U != '', @.Adr.U, @.Adr.M));
	END IF;
	IF @.adr.MASK.U AND @._REPLACE_Brackets.Exists THEN
		SET _Result = _REPLACE_Brackets(_Result, '<U>', @.Adr.U);
	END IF;
	IF @.adr.MASK.N AND @._REPLACE_Brackets.Exists THEN
		SET _Result = _REPLACE_Brackets(_Result, '<N>', @.Adr.N);
	END IF;
	IF @.adr.MASK.Z AND @._REPLACE_Brackets.Exists THEN
		SET _Result = _REPLACE_Brackets(_Result, '<Z>', @.Adr.Z);
	END IF;
	
	IF @.adr.MASK.MaP AND @._REPLACE_Brackets.Exists THEN
		SET _Result = 
			_REPLACE_Brackets(_Result, '<MaP>', 
				IF(@.Adr.U != '',
				--	normalny adres z ulica
					_ConcatStr(
						@.Adr.M,
						IF(@.Adr.P IN ('', @.Adr.M), '', @.Adr.P),
						', poczta '
					),
				--	bez ulicy - zamiast ulicy bedzie miasto zatem tutaj zawsze poczta
					IF(@.Adr.P IN ('', @.Adr.M), '', @.Adr.P)
				)	
		);
	END IF;
	IF @.adr.MASK.PoM AND @._REPLACE_Brackets.Exists THEN
		SET _Result = _REPLACE_Brackets(_Result, '<PoM>', IF(@.Adr.P = '', @.Adr.M, @.Adr.P));
	END IF;
	IF @.adr.MASK.M AND @._REPLACE_Brackets.Exists THEN
		SET _Result = _REPLACE_Brackets(_Result, '<M>', @.Adr.M);
	END IF;
	IF @.adr.MASK.P AND @._REPLACE_Brackets.Exists THEN
		SET _Result = _REPLACE_Brackets(_Result, '<P>', @.Adr.P);
	END IF;

	IF @.adr.MASK.WOJ AND @._REPLACE_Brackets.Exists THEN
		SET _Result = _REPLACE_Brackets(_Result, '<WOJ>', @.Adr.Woj);
	END IF;
	IF @.adr.MASK.POW AND @._REPLACE_Brackets.Exists THEN
		SET _Result = _REPLACE_Brackets(_Result, '<POW>', @.Adr.Pow);
	END IF;
	IF @.adr.MASK.GMI AND @._REPLACE_Brackets.Exists THEN
		SET _Result = _REPLACE_Brackets(_Result, '<GMI>', @.Adr.Gmi);
	END IF;
	
	IF @.adr.MASK.C AND @._REPLACE_Brackets.Exists THEN
		SET _Result = _REPLACE_Brackets(_Result, '<C>', @.Adr.C);
	END IF;
	IF @.adr.MASK.CPL AND @._REPLACE_Brackets.Exists THEN
		SET _Result = _REPLACE_Brackets(_Result, '<CPL>', IF(@.Adr.C = 'PL', '', @.Adr.C));
	END IF;
	IF @.adr.MASK.CC AND @._REPLACE_Brackets.Exists THEN
		SET _Result = _REPLACE_Brackets(_Result, '<CC>', @.Adr.CC);
	END IF;
	IF @.adr.MASK.CC2 AND @._REPLACE_Brackets.Exists THEN
		SET _Result = _REPLACE_Brackets(_Result, '<CC2>', @.Adr.CC2);
	END IF;
	IF @.adr.MASK.RM AND @._REPLACE_Brackets.Exists THEN
		SET _Result = _REPLACE_Brackets(_Result, '<RM>', @.Adr.RM);
	END IF;
	
	IF @.adr.MASK.GmiWoj_C AND @._REPLACE_Brackets.Exists THEN
		SET _Result = _REPLACE_Brackets(_Result, '<GMIWOJ/C>', 
				CONCAT(
					'(', 
					IF(@.Adr.C = 'PL',
						_ConcatStr(IF((LOCATE(CONCAT('(', @.Adr.Gmi, ')'), @.Adr.M) > 0) OR (@.Adr.Gmi = @.Adr.M) OR (@.Adr.Gmi = @.Adr.P), '', @.Adr.Gmi), @.Adr.Woj, ', '),
						@.Adr.CC
					),
					')'
				)	
		);
	END IF;
	
	IF @.adr.MASK.M_Gmi AND @._REPLACE_Brackets.Exists THEN
		SET _Result = _REPLACE_Brackets(_Result, '<M/Gmi>', 
				_ConcatStr(@.Adr.M, IF((LOCATE(CONCAT('(', @.Adr.Gmi, ')'), @.Adr.M) > 0) OR (@.Adr.Gmi = @.Adr.M) OR (@.Adr.Gmi = @.Adr.P), '', @.Adr.Gmi), ', ')
		);
	END IF;
	
	IF @.adr.MASK.M_Gmi2 AND @._REPLACE_Brackets.Exists THEN
		SET _Result = _REPLACE_Brackets(_Result, '<M|Gmi>', 
				IF((LOCATE(CONCAT('(', @.Adr.Gmi, ')'), @.Adr.M) > 0) OR (@.Adr.Gmi = @.Adr.M) OR (@.Adr.Gmi = @.Adr.P), @.Adr.M, @.Adr.Gmi)
		);
	END IF;
	
	IF @.adr.MASK.D AND @._REPLACE_Brackets.Exists THEN
		SET _Result = _REPLACE_Brackets(_Result, '<D>', @.Adr.D);
	END IF;
	IF @.adr.MASK.I AND @._REPLACE_Brackets.Exists THEN
		SET _Result = _REPLACE_Brackets(_Result, '<I>', @.Adr.I);
	END IF;
	
	SET _Result = REPLACE(REPLACE(_Result, '  ', ' '), '()', '');
	
	RETURN _Result;
END;
$$

DROP FUNCTION IF EXISTS `_f_AdrLink_SELECT`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `_f_AdrLink_SELECT`(
	_IN_GUID VARCHAR(38),
	_IN_ObjGUID VARCHAR(38),
	_IN_ObjTable VARCHAR(38),
	_IN_AdrTyp VARCHAR(100),
	_MASK VARCHAR(150),
	_Param_A VARCHAR(255),
	_Param_B VARCHAR(255),
	_Param_C VARCHAR(255)
) RETURNS VARCHAR(250)
    NO SQL
    DETERMINISTIC
S_f_AdrLink_SELECT: BEGIN
	/*
	select _f_AdrLink_SELECT('', 'guid', 'zmng', 'Kontr', '<UM> <N>, <Z> <MaP>', '', '', '');
	*/
	DECLARE _Result VARCHAR(255);
	
	IF (_IN_ObjTable IN ('opng', 'zmng')) AND NOT IFNULL(@session.globals.DOC_with_AdrLink, FALSE) THEN
		RETURN NULL;
		LEAVE S_f_AdrLink_SELECT;
	END IF;
	/*
	SET
		@_IN_ObjGUID = _IN_ObjGUID,
		@_IN_ObjTable = _IN_ObjTable,
		@_IN_AdrTyp = _IN_AdrTyp
	;
	*/
	SET @.adr.Params_from.Variables = (LOCATE('@', _MASK) > 0);
	
	IF @.adr.Params_from.Variables THEN
	--	Z @ -> nie musimy robic selecta 
		IF @._f_AdrLink_SELECT.not.found  THEN
			RETURN NULL; -- bylo RETURN '';
		ELSE
			SET
				_Result = 
					_f_AdrLink_MASK_for_ID(_MASK, '', '', '', '', '', '', '', '', '', '', '')
			;
			RETURN IFNULL(_Result, '');
		END IF;
	ELSE
		SET
			_Result = 
			(
			SELECT 
				_f_AdrLink_MASK_for_ID(_MASK, al.geo_int_miejsca_ID, al.geo_int_adresy_ID, al.ZipCode, al.NrPos, al.NrLoc, al.AdrDescr, al.Post_ID, al.ulica_not_GUS, '', '', '')
			FROM `geo_int_adr_link` al 
			WHERE 
				al.ObjGUID = _IN_ObjGUID 
				AND al.ObjTable = _IN_ObjTable
				-- AND al.AdrTyp LIKE CONCAT('%', _IN_AdrTyp, '%') 
				AND IF(_IN_AdrTyp IN ('', '*'), TRUE, al.AdrTyp LIKE CONCAT('%', _IN_AdrTyp, '%'))
			ORDER BY LENGTH(al.AdrTyp) 
			LIMIT 1
			)
		;
		SET @._f_AdrLink_SELECT.not.found = (_Result IS NULL);
		RETURN _Result;
		-- RETURN IFNULL(_Result, '');
	END IF;
END;
$$


DELIMITER ;

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

_f_AdrLink_MASK_for_ID('<ADR>', al.geo_int_miejsca_ID, al.geo_int_adresy_ID, al.ZipCode, al.NrPos, al.NrLoc, al.AdrDescr, al.Post_ID, al.ulica_not_GUS, '', '', '') AS Ad1,
_f_AdrLink_MASK_for_ID('<ADR>@', '', '', '', '', '', '', '', '', '', '', '') AS Ad2,

  'if(@session.globals.DOC_with_AdrLink, IFNULL( _f_AdrLink_SELECT('''', O.GUID, ''opng'', ''Dost'', ''<UM> <N>, <Z> <MaP>'', '''', '''', ''''), ''''), _VarStr('''')) AS Adres_D, '+
  
  'if(@session.globals.DOC_with_AdrLink, IFNULL( _f_AdrLink_SELECT('''', O.GUID, ''opng'', ''Odbior'', ''<UM> <N>, <Z> <MaP>'', '''', '''', ''''), ''''), _VarStr('''')) AS Adres_OD, '+
  'if(@session.globals.DOC_with_AdrLink, IFNULL( _f_AdrLink_SELECT('''', O.GUID, ''zmng'', ''Odbior'', ''<UM> <N>, <Z> <MaP>'', '''', '''', ''''), ''''), _VarStr('''')) AS Adres_OD, '+
 

ALTER TABLE `geo_int_adr_link`   
	ADD COLUMN `adr_MASKS` MEDIUMTEXT NULL COMMENT 'Zawiera czastkowe wartosci adresu, nazwy miast, gmin etc'
;
 SET @Masks = '<M>=Pozna�&<Z>=61-240&<U>=os.Rusa&<N>=121&<WOJ>=WIELKOPOLSKIE&<POW>=Pozna�&
 <GMI>=Pozna�-Nowe Miasto&<C>=PL&<CC>=Polska&<CC2>=Polska&<CPL>=&<P>=Sopot&<UM>=os.Rusa&
 <MaP>=Pozna�, poczta Sopot&<PoM>=Sopot&<GMIWOJ/C>=(Pozna�-Nowe Miasto, 
 WIELKOPOLSKIE)&<M/Gmi>=Pozna�, Pozna�-Nowe Miasto&
 <M|Gmi>=Pozna�-Nowe Miasto&<D>=&
 <I>=Pozna�  (woj.WIELKOPOLSKIE, pow.Pozna�, gm.Pozna�-Nowe Miasto)&<RM>=delegatura&
 <ADR_P>=os.Rusa 121, 61-240 Pozna�, poczta Sopot&<UM N>=os.Rusa 121&';
*/
 
/*
$Log$ 
*/
