Преглед изворни кода

Implementacja MSIG + testowe wylaczenie BiAuditGenerate z crona

Mariusz Muszyński пре 8 година
родитељ
комит
bcff244e39
1 измењених фајлова са 252 додато и 140 уклоњено
  1. 252 140
      SE/se-lib/Route/UrlAction/BiAuditGenerate.php

+ 252 - 140
SE/se-lib/Route/UrlAction/BiAuditGenerate.php

@@ -1183,19 +1183,55 @@ Router::getRoute("UrlAction_BiAuditGenerate")->' . $function . ';
 
 		Lib::loadClass('Teryt');
 
-		function reTeryt($table) {
-			$tableConf = [
-				'BI_audit_ENERGA_RUM_KONTRAHENCI' => [
-					'kodPocztowy' => 'Kod_pocztowy',
-					'miejscowosc' => 'Miejscowosc',
-					'ulica' => 'Ulica',
+		$tables = [
+			'BI_audit_CEIDG' => [
+				'fields' => [
+					'wojewodztwo' => 'wojewodztwo',
+					'powiat' => 'powiat',
+					'gmina' => 'gmina',
+					'miejscowosc' => 'miejscowosc',
+					'ulica' => 'ulica',
+					'kodPocztowy' => 'kodPocztowy',
+				],
+				'full' => false,
+			],
+			'BI_audit_CEIDG_pelnomocnicy' => [
+				'fields' => [
+					'wojewodztwo' => 'wojewodztwo',
+					'powiat' => 'powiat',
+					'gmina' => 'gmina',
+					'miejscowosc' => 'miejscowosc',
+					'ulica' => 'ulica',
+					'kodPocztowy' => 'kodPocztowy',
+				],
+				'full' => false,
+			],
+			'BI_audit_ENERGA_PRACOWNICY' => [
+				'fields' => [
+					'miejscowosc' => 'miejscowosc',
+					'kodPocztowy' => 'kodPocztowy',
+					'ulica' => 'ulica',
 				],
-				'BI_audit_ENERGA_PRACOWNICY_adresy' => [
+				'full' => true,
+			],
+			'BI_audit_ENERGA_PRACOWNICY_adresy' => [
+				'fields' => [
 					'kodPocztowy' => 'kodPocztowy',
 					'miejscowosc' => 'miejscowosc',
 					'ulica' => 'ulica',
+				],      
+				'full' => true,
+			],
+			'BI_audit_ENERGA_RUM_KONTRAHENCI' => [
+				'fields' => [
+					'kodPocztowy' => 'Kod_pocztowy',
+					'miejscowosc' => 'Miejscowosc',
+					'ulica' => 'Ulica',
 				],
-				'BI_audit_KRS' => [
+				'full' => true,
+			],
+			'BI_audit_KRS' => [ 
+				'fields' => [
 					'wojewodztwo' => 'S_wojewodztwo',
 					'powiat' => 'S_powiat',
 					'gmina' => 'S_gmina',
@@ -1203,24 +1239,40 @@ Router::getRoute("UrlAction_BiAuditGenerate")->' . $function . ';
 					'ulica' => 'A_ulica',
 					'kodPocztowy' => 'A_kod',
 				],
-				'BI_audit_CEIDG' => [
-					'wojewodztwo' => 'wojewodztwo',
-					'powiat' => 'powiat',
-					'gmina' => 'gmina',
-					'miejscowosc' => 'miejscowosc',
-					'ulica' => 'ulica',
-					'kodPocztowy' => 'kodPocztowy',
+				'full' => false,
+			],
+			'BI_audit_KRS_address' => [
+				'fields' => [
+					'wojewodztwo' => 'S_wojewodztwo',
+					'powiat' => 'S_powiat',
+					'gmina' => 'S_gmina',
+					'miejscowosc' => 'A_miejscowosc',
+					'ulica' => 'A_ulica',
+					'kodPocztowy' => 'A_kod',
 				],
-			];
-
-			$columns = ['kodPocztowy', 'miejscowosc', 'ulica'];
+				'full' => false,
+			],
+			'BI_audit_MSIG_address' => [
+				'fields' => [
+					'wojewodztwo' => 'S_wojewodztwo',
+					'powiat' => 'S_powiat',
+					'gmina' => 'S_gmina',
+					'miejscowosc' => 'A_miejscowosc',
+					'ulica' => 'A_ulica',
+					'kodPocztowy' => 'A_kod',
+				],
+				'full' => false,
+			],
+		];
 
-			$query = "select * from `{$table}` where TERYT_SYM is null";
+		foreach ($tables as $table => $tableConf) {
+			$where = ($full && $tableConf['full']) ? "" : "where `TERYT_SYM` is null and `cached` = 0";
+			$query = "select * from `{$table}` {$where}";
 			try {
-				$adresy = DB::getPDO()->fetchall($query);
-				foreach ($adresy as $adres) {
+				$adresy = DB::getPDO()->query($query);
+				while ($adres = $adresy->fetch()) {
 					$search = [];
-					foreach ($columns as $column) $search[$column] = $adres[$tableConf[$table][$column]];
+					foreach ($tableConf['fields'] as $field => $param) if (!empty($adres[$param])) $search[$field] = $adres[$param];
 					try {
 						$teryt = Teryt::search($search);
 						if ($teryt) {
@@ -1234,22 +1286,47 @@ Router::getRoute("UrlAction_BiAuditGenerate")->' . $function . ';
 			} catch (Exception $e) {
 			}
 		}
-
-		if ($full) {
-			reTeryt('BI_audit_KRS');
-			reTeryt('BI_audit_CEIDG');
-		}
-		reTeryt('BI_audit_ENERGA_RUM_KONTRAHENCI');
-		reTeryt('BI_audit_ENERGA_PRACOWNICY_adresy');
 	}
 
 	private function reloadCache_updateAll($full = false) {
-		$tables = [
+		$tablesConf = [
+			'BI_audit_CEIDG' => [
+				'fields' => [
+					'nip' => 'nip',
+					'regon' => 'regon',
+					'TERYT_SYM' => 'TERYT_SYM',
+					'TERYT_SYM_UL' => 'TERYT_SYM_UL',
+					'nrDomu' => 'budynek',
+					'nrLokalu' => 'lokal',
+				],
+				'base' => '1',
+			],
+			'BI_audit_CEIDG_pelnomocnicy' => [
+				'fields' => [
+					'nip' => 'nip',
+					'TERYT_SYM' => 'TERYT_SYM',
+					'TERYT_SYM_UL' => 'TERYT_SYM_UL',
+					'nrDomu' => 'budynek',
+					'nrLokalu' => 'lokal',
+				],
+				'base' => '1',
+			],
+			'BI_audit_CEIDG_powiazania' => [
+				'fields' => [
+					'nip' => 'nip',
+					'regon' => 'regon',
+				],
+				'base' => '1',
+			],
 			'BI_audit_ENERGA_PRACOWNICY' => [
 				'fields' => [
 					'nip' => 'nip',
 					'regon' => 'regon',
 					'pesel' => 'pesel',
+					'TERYT_SYM' => 'TERYT_SYM',
+					'TERYT_SYM_UL' => 'TERYT_SYM_UL',
+					'nrDomu' => 'nr',
+					'nrLokalu' => 'nrLokalu',
 				],
 				'base' => '0',
 				//'where' => '',
@@ -1264,6 +1341,7 @@ Router::getRoute("UrlAction_BiAuditGenerate")->' . $function . ';
 				'base' => '0',
 				//'where' => '',
 			],
+                        
 			'BI_audit_ENERGA_RUM_KONTRAHENCI' => [
 				'fields' => [
 					'krs' => 'KRS',
@@ -1278,52 +1356,50 @@ Router::getRoute("UrlAction_BiAuditGenerate")->' . $function . ';
 				'base' => '0',
 				'where' => "ownCompany != 'Y'",
 			],
-			'BI_audit_KW_requested_person' => [
-				'fields' => [
-					'pesel' => 'Seller_person_pesel',
-					'krs' => 'Seller_person_KRS',
-					'nip' => 'Seller_person_NIP',
-					'regon' => 'Seller_person_REGON',
-				],
-				'base' => '0',
-				//'where' => '',
-			],
-		];
-
-		$tables_full = [
-			'BI_audit_CEIDG' => [
+			'BI_audit_KRS' => [
 				'fields' => [
+					'krs' => 'krs',
 					'nip' => 'nip',
 					'regon' => 'regon',
 					'TERYT_SYM' => 'TERYT_SYM',
 					'TERYT_SYM_UL' => 'TERYT_SYM_UL',
-					'nrDomu' => 'budynek',
-					'nrLokalu' => 'lokal',
+					'nrDomu' => 'A_nrDomu',
+					'nrLokalu' => 'A_nrLokalu',
 				],
 				'base' => '1',
 			],
-			'BI_audit_CEIDG_pelnomocnicy' => [
+			'BI_audit_KRS_address' => [
 				'fields' => [
-					'nip' => 'nip',
 					'TERYT_SYM' => 'TERYT_SYM',
 					'TERYT_SYM_UL' => 'TERYT_SYM_UL',
-					'nrDomu' => 'budynek',
-					'nrLokalu' => 'lokal',
+					'nrDomu' => 'A_nrDomu',
+					'nrLokalu' => 'A_nrLokalu',
 				],
 				'base' => '1',
 			],
-			'BI_audit_CEIDG_powiazania' => [
+			'BI_audit_KRS_company' => [
 				'fields' => [
-					'nip' => 'nip',
 					'regon' => 'regon',
+					'krs' => 'krs',
 				],
 				'base' => '1',
 			],
-			'BI_audit_KRS' => [
+			'BI_audit_KRS_person' => [
+				'fields' => [
+					'pesel' => 'pesel',
+				],
+				'base' => '1',
+			],
+			'BI_audit_MSIG' => [
 				'fields' => [
 					'krs' => 'krs',
 					'nip' => 'nip',
 					'regon' => 'regon',
+				],
+				'base' => '1',
+			],
+			'BI_audit_MSIG_address' => [
+				'fields' => [
 					'TERYT_SYM' => 'TERYT_SYM',
 					'TERYT_SYM_UL' => 'TERYT_SYM_UL',
 					'nrDomu' => 'A_nrDomu',
@@ -1331,75 +1407,123 @@ Router::getRoute("UrlAction_BiAuditGenerate")->' . $function . ';
 				],
 				'base' => '1',
 			],
-			'BI_audit_KRS_company' => [
+			'BI_audit_MSIG_company' => [
 				'fields' => [
 					'regon' => 'regon',
 					'krs' => 'krs',
 				],
 				'base' => '1',
 			],
-			'BI_audit_KRS_person' => [
+			'BI_audit_MSIG_person' => [
 				'fields' => [
 					'pesel' => 'pesel',
 				],
 				'base' => '1',
 			],
+                        
+			'BI_audit_KW_requested_person' => [
+				'fields' => [
+					'pesel' => 'Seller_person_pesel',
+					'krs' => 'Seller_person_KRS',
+					'nip' => 'Seller_person_NIP',
+					'regon' => 'Seller_person_REGON',
+				],
+				'base' => '0',
+				//'where' => '',
+			],
+		];
+
+		$refTablesConf = [
+			'BI_audit_CEIDG' => [
+				'refTablesTo' => [
+					'BI_audit_CEIDG_pelnomocnicy',
+					'BI_audit_CEIDG_powiazania',
+				],
+				'base' => 1,
+			],
+			'BI_audit_ENERGA_PRACOWNICY' => [
+				'refTablesTo' => [
+					'BI_audit_ENERGA_PRACOWNICY_adresy',
+				],
+				'base' => 0,
+			],
+			'BI_audit_KRS' => [
+				'refTablesTo' => [
+					'BI_audit_KRS_address',
+					'BI_audit_KRS_company',
+					'BI_audit_KRS_person',
+				],
+				'base' => 1,
+			],
+			'BI_audit_MSIG' => [
+				'refTablesTo' => [
+					'BI_audit_MSIG_address',
+					'BI_audit_MSIG_company',
+					'BI_audit_MSIG_person',
+				],
+				'base' => 1,
+			],
 		];
 
 		if ($full) {
-			$tables = array_merge($tables_full, $tables);
-			$fullWhere = '1 = 1';
-		} else {
-			$fullWhere = 'BASE = 0';
+			foreach ($tablesConf as $table => $tableConf) if (!$tableConf['base']) $queries[] = "update `{$table}` set `cached` = 0";
+			$queries[] = "delete from BI_audit_ALL where BASE = 0";
+			$queries[] = "delete from BI_audit_ALL_ref where BASE = 0";
 		}
 
-		$queries[] = "delete from BI_audit_ALL where {$fullWhere}";
-		$queries[] = "delete from BI_audit_ALL_ref where {$fullWhere}";
-		//$queries[] = "delete from BI_audit_ALL_ref_RELATIONS where {$fullWhere}"; // niepotrzebne
-		foreach ($tables as $name => $table) {
-			if (isset($table['where']) && $table['where']) $where = "where {$table['where']}";
-			else $where = '';
-			$queries[] = "insert into BI_audit_ALL (REMOTE_TABLE, REMOTE_ID, BASE, " . implode(", ", array_keys($table['fields'])) . ") select '{$name}', ID, '{$table['base']}', " . implode(", ", $table['fields']) . " from {$name} {$where}";
+		foreach ($refTablesConf as $refTrableFrom => $refTableConf) {
+			if ($refTableConf['base']) continue;
+			foreach ($refTableConf['refTablesTo'] as $refTableTo) {
+				$refTable = ACL::getRefTable("default_db/{$refTrableFrom}/{$refTrableFrom}", "default_db__x3A__{$refTableTo}:{$refTableTo}");
+				$queries[] = "update `{$refTable}` set `A_STATUS` = 'WAITNG' where `A_STATUS` = 'NORMAL'";
+			}
 		}
-		$queries[] = "update BI_audit_ALL set nip = null where nip = 0";
-		$queries[] = "update BI_audit_ALL set regon = null where regon = 0";
-		$queries[] = "update BI_audit_ALL set krs = null where krs = 0";
-		$queries[] = "update BI_audit_ALL set pesel = null where pesel = 0";
-		$queries[] = "update BI_audit_ALL set TERYT_SYM = null where TERYT_SYM = 0";
-		$queries[] = "update BI_audit_ALL set TERYT_SYM_UL = null where TERYT_SYM_UL = 0";
-		$queries[] = "update BI_audit_ALL set nrDomu = null where trim(nrDomu) = ''";
-		$queries[] = "update BI_audit_ALL set nrLokalu = null where trim(nrLokalu) = ''";
-		$queries[] = "update BI_audit_ALL set nrDomu = substring(nrDomu, 1, position('/' in nrDomu) - 1), nrLokalu = substring(nrDomu, position('/' in nrDomu ) + 1) where nrDomu rlike '^[[:digit:]]+.*/[[:digit:]]+.*$' and nrLokalu is null and {$fullWhere}";
-
-		$queries[] = "set @var = (select round(coalesce(max(ID), '0.5'), 1) from BI_audit_ALL_ref_RELATIONS)";
-		$queries[] = "insert into BI_audit_ALL_ref_RELATIONS (ID, RELATION, BASE) select @var:=@var*2, a.RELATION, a.BASE from (select a.REMOTE_TABLE as RELATION, a.BASE as BASE from BI_audit_ALL a where a.REMOTE_TABLE not in ('BI_audit_ENERGA_RUM_KONTRAHENCI', 'BI_audit_KW_requested_person') group by a.REMOTE_TABLE order by a.BASE desc) as a left join BI_audit_ALL_ref_RELATIONS r on a.RELATION = r.RELATION where r.RELATION is null";
-
-		if ($full) {
-			$refCeidgToCeidgPelnomocnicy = ACL::getRefTable('default_db/BI_audit_CEIDG/BI_audit_CEIDG', "default_db__x3A__BI_audit_CEIDG_pelnomocnicy:BI_audit_CEIDG_pelnomocnicy");
-			$queries[] = "insert ignore into BI_audit_ALL_ref (ID1, ID2, RELATION_ID, BASE) select all1.ID, all2.ID, rel.ID, 1 from BI_audit_ALL as all1 join `{$refCeidgToCeidgPelnomocnicy}` as ref on all1.REMOTE_TABLE = 'BI_audit_CEIDG' and all1.REMOTE_ID = ref.PRIMARY_KEY join BI_audit_ALL as all2 on all2.REMOTE_TABLE = 'BI_audit_CEIDG_pelnomocnicy' and ref.REMOTE_PRIMARY_KEY = all2.REMOTE_ID join BI_audit_ALL_ref_RELATIONS rel on all1.REMOTE_TABLE = rel.RELATION";
-			$queries[] = "insert ignore into BI_audit_ALL_ref (ID1, ID2, RELATION_ID, BASE) select all2.ID, all1.ID, rel.ID, 1 from BI_audit_ALL as all1 join `{$refCeidgToCeidgPelnomocnicy}` as ref on all1.REMOTE_TABLE = 'BI_audit_CEIDG' and all1.REMOTE_ID = ref.PRIMARY_KEY join BI_audit_ALL as all2 on all2.REMOTE_TABLE = 'BI_audit_CEIDG_pelnomocnicy' and ref.REMOTE_PRIMARY_KEY = all2.REMOTE_ID join BI_audit_ALL_ref_RELATIONS rel on all2.REMOTE_TABLE = rel.RELATION";
 
-			$refCeidgToCeidgPowiazania = ACL::getRefTable('default_db/BI_audit_CEIDG/BI_audit_CEIDG', "default_db__x3A__BI_audit_CEIDG_powiazania:BI_audit_CEIDG_powiazania");
-			$queries[] = "insert ignore into BI_audit_ALL_ref (ID1, ID2, RELATION_ID, BASE) select all1.ID, all2.ID, rel.ID, 1 from BI_audit_ALL as all1 join `{$refCeidgToCeidgPowiazania}` as ref on all1.REMOTE_TABLE = 'BI_audit_CEIDG' and all1.REMOTE_ID = ref.PRIMARY_KEY join BI_audit_ALL as all2 on all2.REMOTE_TABLE = 'BI_audit_CEIDG_powiazania' and ref.REMOTE_PRIMARY_KEY = all2.REMOTE_ID join BI_audit_ALL_ref_RELATIONS rel on all1.REMOTE_TABLE = rel.RELATION";
-			$queries[] = "insert ignore into BI_audit_ALL_ref (ID1, ID2, RELATION_ID, BASE) select all2.ID, all1.ID, rel.ID, 1 from BI_audit_ALL as all1 join `{$refCeidgToCeidgPowiazania}` as ref on all1.REMOTE_TABLE = 'BI_audit_CEIDG' and all1.REMOTE_ID = ref.PRIMARY_KEY join BI_audit_ALL as all2 on all2.REMOTE_TABLE = 'BI_audit_CEIDG_powiazania' and ref.REMOTE_PRIMARY_KEY = all2.REMOTE_ID join BI_audit_ALL_ref_RELATIONS rel on all2.REMOTE_TABLE = rel.RELATION";
-
-			$refKrsToKrsCompany = ACL::getRefTable('default_db/BI_audit_KRS/BI_audit_KRS', "default_db__x3A__BI_audit_KRS_company:BI_audit_KRS_company");
-			$queries[] = "insert ignore into BI_audit_ALL_ref (ID1, ID2, RELATION_ID, BASE) select all1.ID, all2.ID, rel.ID, 1 from BI_audit_ALL as all1 join `{$refKrsToKrsCompany}` as ref on all1.REMOTE_TABLE = 'BI_audit_KRS' and all1.REMOTE_ID = ref.PRIMARY_KEY join BI_audit_ALL as all2 on all2.REMOTE_TABLE = 'BI_audit_KRS_company' and ref.REMOTE_PRIMARY_KEY = all2.REMOTE_ID join BI_audit_ALL_ref_RELATIONS rel on all1.REMOTE_TABLE = rel.RELATION";
-			$queries[] = "insert ignore into BI_audit_ALL_ref (ID1, ID2, RELATION_ID, BASE) select all2.ID, all1.ID, rel.ID, 1 from BI_audit_ALL as all1 join `{$refKrsToKrsCompany}` as ref on all1.REMOTE_TABLE = 'BI_audit_KRS' and all1.REMOTE_ID = ref.PRIMARY_KEY join BI_audit_ALL as all2 on all2.REMOTE_TABLE = 'BI_audit_KRS_company' and ref.REMOTE_PRIMARY_KEY = all2.REMOTE_ID join BI_audit_ALL_ref_RELATIONS rel on all2.REMOTE_TABLE = rel.RELATION";
-
-			$refKrsToKrsPerson = ACL::getRefTable('default_db/BI_audit_KRS/BI_audit_KRS', "default_db__x3A__BI_audit_KRS_person:BI_audit_KRS_person");
-			$queries[] = "insert ignore into BI_audit_ALL_ref (ID1, ID2, RELATION_ID, BASE) select all1.ID, all2.ID, rel.ID, 1 from BI_audit_ALL as all1 join `{$refKrsToKrsPerson}` as ref on all1.REMOTE_TABLE = 'BI_audit_KRS' and all1.REMOTE_ID = ref.PRIMARY_KEY join BI_audit_ALL as all2 on all2.REMOTE_TABLE = 'BI_audit_KRS_person' and ref.REMOTE_PRIMARY_KEY = all2.REMOTE_ID join BI_audit_ALL_ref_RELATIONS rel on all1.REMOTE_TABLE = rel.RELATION";
-			$queries[] = "insert ignore into BI_audit_ALL_ref (ID1, ID2, RELATION_ID, BASE) select all2.ID, all1.ID, rel.ID, 1 from BI_audit_ALL as all1 join `{$refKrsToKrsPerson}` as ref on all1.REMOTE_TABLE = 'BI_audit_KRS' and all1.REMOTE_ID = ref.PRIMARY_KEY join BI_audit_ALL as all2 on all2.REMOTE_TABLE = 'BI_audit_KRS_person' and ref.REMOTE_PRIMARY_KEY = all2.REMOTE_ID join BI_audit_ALL_ref_RELATIONS rel on all2.REMOTE_TABLE = rel.RELATION";
+		foreach ($tablesConf as $table => $tableConf) {
+			$where = "where `cached` = 0";
+			if (!empty($tableConf['where'])) $where .= " {$tableConf['where']}";
+			$queries[] = "lock tables `{$table}` write";
+			$queries[] = "insert into `BI_audit_ALL` (`REMOTE_TABLE`, `REMOTE_ID`, `BASE`, " . implode(", ", array_keys($tableConf['fields'])) .
+				") select '{$table}', ID, '{$tableConf['base']}', " . implode(", ", $tableConf['fields']) . " from `{$table}` {$where}";
+			$queries[] = "update `{$table}` set `cached` = 1 {$where}";
+			$queries[] = "unlock tables";
 		}
 
-		$refPracownicyToPracownicyAdresy = ACL::getRefTable('default_db/BI_audit_ENERGA_PRACOWNICY/BI_audit_ENERGA_PRACOWNICY', "default_db__x3A__BI_audit_ENERGA_PRACOWNICY_adresy:BI_audit_ENERGA_PRACOWNICY_adresy");
-		$queries[] = "insert into BI_audit_ALL_ref (ID1, ID2, RELATION_ID, BASE) select all1.ID, all2.ID, rel.ID, 0 from BI_audit_ALL as all1 join `{$refPracownicyToPracownicyAdresy}` as ref on all1.REMOTE_TABLE = 'BI_audit_ENERGA_PRACOWNICY' and all1.REMOTE_ID = ref.PRIMARY_KEY join BI_audit_ALL as all2 on all2.REMOTE_TABLE = 'BI_audit_ENERGA_PRACOWNICY_adresy' and ref.REMOTE_PRIMARY_KEY = all2.REMOTE_ID join BI_audit_ALL_ref_RELATIONS rel on all1.REMOTE_TABLE = rel.RELATION";
+		$queries[] = "update `BI_audit_ALL` set `nip` = null where `nip` = 0";
+		$queries[] = "update `BI_audit_ALL` set `regon` = null where `regon` = 0";
+		$queries[] = "update `BI_audit_ALL` set `krs` = null where `krs` = 0";
+		$queries[] = "update `BI_audit_ALL` set `pesel` = null where `pesel` = 0";
+		$queries[] = "update `BI_audit_ALL` set `TERYT_SYM` = null where `TERYT_SYM` = 0";
+		$queries[] = "update `BI_audit_ALL` set `TERYT_SYM_UL` = null where `TERYT_SYM_UL` = 0";
+		$queries[] = "update `BI_audit_ALL` set `nrDomu` = null where trim(`nrDomu`) = ''";
+		$queries[] = "update `BI_audit_ALL` set `nrLokalu` = null where trim(`nrLokalu`) = ''";
+
+		$queries[] = "update `BI_audit_ALL` set `nrDomu` = substring(`nrDomu`, 1, position('/' in `nrDomu`) - 1), `nrLokalu` = substring(`nrDomu`, position('/' in `nrDomu`) + 1) where `reffed` = 0 and `nrDomu` rlike '^[[:digit:]]+.*/[[:digit:]]+.*$' and `nrLokalu` is null";
+
+		$queries[] = "set @var = (select round(coalesce(max(`ID`), '0.5'), 1) from `BI_audit_ALL_ref_RELATIONS`)";
+		$queries[] = "insert into `BI_audit_ALL_ref_RELATIONS` (`ID`, `RELATION`, `BASE`) select @var:=@var*2, `a`.`RELATION`, `a`.`BASE` from (select `a`.`REMOTE_TABLE` as `RELATION`, `a`.`BASE` as `BASE` from `BI_audit_ALL` `a` where `a`.`REMOTE_TABLE` not in ('BI_audit_ENERGA_RUM_KONTRAHENCI', 'BI_audit_KW_requested_person') group by `a`.`REMOTE_TABLE` order by `a`.`BASE` desc) as `a` left join `BI_audit_ALL_ref_RELATIONS` `r` on `a`.`RELATION` = `r`.`RELATION` where `r`.`RELATION` is null";
+
+		foreach ($refTablesConf as $refTrableFrom => $refTableConf) {
+			foreach ($refTableConf['refTablesTo'] as $refTableTo) {
+				$refTable = ACL::getRefTable("default_db/{$refTrableFrom}/{$refTrableFrom}", "default_db__x3A__{$refTableTo}:{$refTableTo}");
+				$queries[] = "lock tables `{$refTable}` write";
+				$joins = "`BI_audit_ALL` as `all1` join `{$refTable}` as `ref` on `all1`.`REMOTE_TABLE` = '{$refTableFrom}' and `all1`.`REMOTE_ID` = `ref`.`PRIMARY_KEY` and `ref`.`A_STATUS` = 'WAITING' join `BI_audit_ALL` as `all2` on `all2`.`REMOTE_TABLE` = '{$refTableTo}' and `ref`.`REMOTE_PRIMARY_KEY` = `all2`.`REMOTE_ID` join `BI_audit_ALL_ref_RELATIONS` `rel` on `all1`.`REMOTE_TABLE` = `rel`.`RELATION`";
+
+				$queries[] = "insert ignore into `BI_audit_ALL_ref` (`ID1`, `ID2`, `RELATION_ID`, `BASE`) select `all1`.`ID`, `all2`.`ID`, `rel`.`ID`, {$refTableConf['base']} from {$joins}";
+				$queries[] = "insert ignore into `BI_audit_ALL_ref` (`ID1`, `ID2`, `RELATION_ID`, `BASE`) select `all2`.`ID`, `all1`.`ID`, `rel`.`ID`, {$refTableConf['base']} from {$joins}";
+				$queries[] = "update `ref` using {$joins} set `ref`.`A_STATUS` = 'NORMAL'";
+				$queries[] = "unlock tables";
+			}
+		}
 
 		foreach ($queries as $query) DB::getPDO()->query($query);
+
 		$BiAuditRelations = new BiAuditRelations($full);
-		$query = "select ID from BI_audit_ALL where {$fullWhere} order by ID";
-		$result = DB::query($query);
-		while ($row = DB::fetch($result)) $BiAuditRelations->findRelations($row->ID);
+		DB::getPDO()->query("locktables `BI_audit_ALL` write");
+		$result = DB::getPDO()->query("select `ID` from `BI_audit_ALL` where `reffed` = 0 order by `ID`");
+		while ($row = $result->fetch()) $BiAuditRelations->findRelations($row['ID']);
+		DB::getPDO()->query("update `BI_audit_ALL` set `reffed` = 1");
+		DB::getPDO()->query("unlock tables");
 	}
 
 	public function doReloadCache($full = false) {
@@ -1445,31 +1569,6 @@ Router::getRoute("UrlAction_BiAuditGenerate")->' . $function . ';
 			file_put_contents($xmlFile, $BiAuditPowiazania->asXml());
 			$BiAuditPowiazania->saveToDb();
 			$BiAuditPowiazania->generatePdfAndHtml($tasksDirLocation);
-/*			$powiazania = $BiAuditPowiazania->asArray('BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA');
-			if (!$powiazania) return false;
-			DB::getPDO()->query($query);
-			foreach ($powiazania['BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row'] as $row) {
-				$powiazaniaRowSqlArr = [];
-				$string_concat_path = [];
-				foreach ($row['object'] as $i => $object) {
-					$powiazaniaRowObjectSqlArr = [];
-					$table = array_keys($object)[0];
-					if (!isset($tablesConf[$table])) throw new Exception("Błąd danych konfiguracyjnych");
-					$concat = "table='{$table}'";
-					if (isset($object['relation_from'])) $concat .= ";relation_from='{$object['relation_from']}'";
-					foreach ($tablesConf[$table] as $field) {
-						if ($object[$table][$field]) $concat .= ";{$field}='{$object[$table][$field]}'";
-					}
-					if ($i == 0) $powiazaniaRowSqlArr['string_concat_from'] = $concat;
-					elseif ($i == count($row['object']) - 1) $powiazaniaRowSqlArr['string_concat_to'] = $concat;
-					else $string_concat_path[] = $concat;
-				}
-				$powiazaniaRowSqlArr['string_concat_path'] = implode(' / ', $string_concat_path);
-				$id_row = DB::getPDO()->insert("BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row", $powiazaniaRowSqlArr);
-				if (!$id_row) throw new Exception("Błąd bazy danych");
-				DB::getPDO()->insert('CRM__#REF_TABLE__25', ["PRIMARY_KEY" => $ID, "REMOTE_PRIMARY_KEY" => $id_row]);
-			}
-*/
 			file_put_contents($resultFile, "ok");
 		} catch (Exception $e) {
 			file_put_contents($resultFile, $e->getMessage());
@@ -1536,8 +1635,7 @@ class BiAuditRelations {
 	private $destTables = ['BI_audit_ENERGA_RUM_KONTRAHENCI', 'BI_audit_KW_requested_person'];
 	private $sourceTables = ['BI_audit_ENERGA_PRACOWNICY', 'BI_audit_ENERGA_PRACOWNICY_adresy'];
 
-	public function __construct($full = false) {
-		$this->full = $full;
+	public function __construct() {
 		$query = "select * from BI_audit_ALL_ref_RELATIONS";
 		$result = DB::getPDO()->fetchAll($query);
 		foreach ($result as $row) $this->RELATIONS_ID[$row['RELATION']] = $row['ID'];
@@ -1576,10 +1674,8 @@ class BiAuditRelations {
 		}
 		if (!$joins) return false;
 
-		if ($this->full) $where = "where t1.ID = {$ID} and t2.ID > {$ID}";
-		else $where = "where t1.ID = {$ID} and (t2.BASE = 1 or t2.ID > {$ID})";
 		$query = "select t2.ID as ID, t1.REMOTE_TABLE as REMOTE_TABLE_1, t2.REMOTE_TABLE as REMOTE_TABLE_2 {$items}, t1.BASE * t2.BASE as BASE " .
-			"from BI_audit_ALL t1 join BI_audit_ALL t2 on " . implode(" or ", $joins) . " {$where}";
+			"from BI_audit_ALL t1 join BI_audit_ALL t2 on " . implode(" or ", $joins) . " where t1.ID = {$ID} and (t2.reffed = 1 or t2.ID > {$ID})";
 		$result = DB::getPDO()->fetchAll($query);
 		foreach ($result as $row) {
 			$relationID = 0;
@@ -1615,6 +1711,19 @@ class BiAuditPowiazania {
 	private $items_results = [];
 	private $relations = [];
 	private $destTables = ['BI_audit_ENERGA_RUM_KONTRAHENCI', 'BI_audit_KW_requested_person'];
+	private $refTables = [];
+
+	private function getRefTable($tableFrom, $tableTo, $returnException = true) {
+		if (!isset($this->refTables[$tableFrom][$tableTo])) {
+			try {
+				$this->refTables[$tableFrom][$tableTo] = ACL::getRefTable("default_db/{$tableFrom}/{$tableFrom}", "default_db__x3A__{$tableTo}:{$tableTo}");
+			} catch (Exception $e) {
+				if ($returnException) throw new Exception($e->getMessage());
+				$this->refTables[$tableFrom][$tableTo] = false;
+			}
+		}
+		return $this->refTables[$tableFrom][$tableTo];
+	}
 
 	public function __construct($ID = 0) {
 		if (!$ID) throw new Exception("Wrong ID parameter");
@@ -1631,7 +1740,7 @@ class BiAuditPowiazania {
 
 		$subQueries = [];
 		foreach ($this->destTables as $destTable) {
-			$refTable = ACL::getRefTable('default_db/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA', "default_db__x3A__{$destTable}:{$destTable}");
+			$refTable = $this->getRefTable('BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA', $destTable);
 			$subQueries[] = "select `all`.ID from BI_audit_ALL `all` join `{$refTable}` ref on `all`.REMOTE_TABLE = '{$destTable}' and `all`.REMOTE_ID = ref.REMOTE_PRIMARY_KEY and ref.PRIMARY_KEY = '{$ID}'";
 		}
 		$query = implode(" union ", $subQueries);
@@ -1639,8 +1748,7 @@ class BiAuditPowiazania {
 		if (!$result) throw new Exception("Błąd danych - nie zdefinowano żadnego końcowego obiektu");
 		$this->endNodes = array_map('reset', $result);
 
-		$refPowiazaniaToPracownicy = ACL::getRefTable('default_db/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA',
-			'default_db__x3A__BI_audit_ENERGA_PRACOWNICY:BI_audit_ENERGA_PRACOWNICY');
+		$refPowiazaniaToPracownicy = $this->getRefTable('BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA', 'BI_audit_ENERGA_PRACOWNICY');
 		$query = "select `all`.ID as ID from BI_audit_ALL `all` join `{$refPowiazaniaToPracownicy}` ref
 			on `all`.REMOTE_TABLE = 'BI_audit_ENERGA_PRACOWNICY' and `all`.REMOTE_ID = ref.REMOTE_PRIMARY_KEY and ref.PRIMARY_KEY = '{$ID}'";
 		$result = DB::getPDO()->fetchAll($query);
@@ -1695,8 +1803,8 @@ class BiAuditPowiazania {
 		$kontrahenci = [];
 		$kw_person = [];
 
-		$refUmowyToKontrahenci = ACL::getRefTable('default_db/BI_audit_ENERGA_RUM_UMOWY/BI_audit_ENERGA_RUM_UMOWY', 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI:BI_audit_ENERGA_RUM_KONTRAHENCI');
-		$refKwRequestedToKwRequestedPerson = ACL::getRefTable('default_db/BI_audit_KW_requested/BI_audit_KW_requested', 'default_db__x3A__BI_audit_KW_requested_person:BI_audit_KW_requested_person');
+		$refUmowyToKontrahenci = $this->getRefTable('BI_audit_ENERGA_RUM_UMOWY', 'BI_audit_ENERGA_RUM_KONTRAHENCI');
+		$refKwRequestedToKwRequestedPerson = $this->getRefTable('/BI_audit_KW_requested', 'BI_audit_KW_requested_person');
 
 		foreach ($this->results as $result) {
 			$item_results = [];
@@ -1706,8 +1814,15 @@ class BiAuditPowiazania {
 				$query = "select * from `{$row['REMOTE_TABLE']}` where ID = '{$row['REMOTE_ID']}'";
 				$obj = DB::fetch(DB::query($query));
 				$item_result = [$row['REMOTE_TABLE'] => (array) $obj];
+
+				if ($refTable = $this->getRefTable('BI_audit_operational_raport_note', $row['REMOTE_TABLE'], false)) {
+					$query = "select `note`.* from `BI_audit_operational_raport_note` `note` join `{$refTable}` `ref` on `note`.`ID` = `ref`.`PRIMARY_KEY` where `ref`.`REMOTE_PRIMARY_KEY` = '{$row['REMOTE_ID']}'";
+					if ($notes = DB::getPDO()->fetchAll($query)) $item_result[$row['REMOTE_TABLE']]['operational_raport_note'] = $notes;
+				}
+
 				if ($rel) $item_result['relation_from'] = $this->relationName($rel);
 				$item_results['object'][] = $item_result;
+
 				if ($row['REMOTE_TABLE'] == "BI_audit_ENERGA_RUM_KONTRAHENCI" && (!in_array($row['REMOTE_ID'], $kontrahenci))) {
 					$kontrahenci[] = $row['REMOTE_ID'];
 					$query = "select umowy.* from BI_audit_ENERGA_RUM_UMOWY umowy join `{$refUmowyToKontrahenci}` ref on umowy.ID = ref.PRIMARY_KEY where ref.REMOTE_PRIMARY_KEY = '{$row['REMOTE_ID']}'";
@@ -1740,10 +1855,8 @@ class BiAuditPowiazania {
 
 	public function saveToDb() {
 		if (!$this->results) return null;
-		$refPowiazaniaToPowiazaniaRow = ACL::getRefTable('default_db/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA',
-			'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row');
-		$refPowiazaniaRowToPowiazaniaRowObject = ACL::getRefTable('default_db/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row',
-			'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object');
+		$refPowiazaniaToPowiazaniaRow = $this->getRefTable('BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA', 'BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row');
+		$refPowiazaniaRowToPowiazaniaRowObject = $this->getRefTable('BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row', 'BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object');
 		foreach ($this->results as $result) {
 			$IdPowiazaniaRow = DB::getPDO()->insert('BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row', []);
 			DB::getPDO()->insert($refPowiazaniaToPowiazaniaRow, ['PRIMARY_KEY' => $this->ID, 'REMOTE_PRIMARY_KEY' => $IdPowiazaniaRow]);
@@ -1752,8 +1865,7 @@ class BiAuditPowiazania {
 				$object = DB::getPDO()->fetchFirst($query);
 				$idPowiazaniaRowObject = DB::getPDO()->insert('BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object', ['relation_from' => $this->relations[$rel]]);
 				DB::getPDO()->insert($refPowiazaniaRowToPowiazaniaRowObject, ['PRIMARY_KEY' => $IdPowiazaniaRow, 'REMOTE_PRIMARY_KEY' => $idPowiazaniaRowObject]);
-				$ref = ACL::getRefTable('default_db/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object',
-					"default_db__x3A__{$object['REMOTE_TABLE']}:{$object['REMOTE_TABLE']}");
+				$ref = $this->getRefTable('BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object', $object['REMOTE_TABLE']);
 				DB::getPDO()->insert($ref, ['PRIMARY_KEY' => $idPowiazaniaRowObject, 'REMOTE_PRIMARY_KEY' => $object['REMOTE_ID']]);
 			}
 		}