Przeglądaj źródła

Merge branch 'master' of bn.git:plabudda/se

Piotr Labudda 8 lat temu
rodzic
commit
710f76660e

+ 197 - 38
SE/se-lib/Route/UrlAction/BiAuditGenerate.php

@@ -92,14 +92,28 @@ class Route_UrlAction_BiAuditGenerate extends RouteBase {
 <?php
 		}
 ?>
-            <tr>
-              <td><input type="checkbox" name="reloadCache"/></td>
-              <td colspan=6>Odśwież cache (wymagane w przypadku, gdy dokonano dowolnych zmian w tabelach pracowników/kontrahentów)</td>
-            </tr>
           </tbody>
         </table>
       </div>
     </div>
+    <div class="form-group">
+      <label class="col-sm-2 control-label">
+         Odśwież cache
+      </label>
+      <div class="col-sm-2">
+        <select name="reloadCache" class="form-control">
+          <option value="No" selected>Nie</option>
+          <option value="Part">Częściowe</option>
+          <option value="Full">Pełne</option>
+        </select>
+      </div>
+    </div>
+    <div class="form-group">
+       <div class="col-sm-offset-2 col-sm-10">
+         Częściowe odświeżenie wymagane w przypadku, gdy dokonano dowolnych zmian w tabelach pracowników/kontrahentów.<br/>
+         Pełne odświeżenie wymagane w przypadku modyfikacji baz KRS lub CEiDG (UWAGA - trwa to wiele godzin)
+       </div>
+    </div>
     <div class="form-group">
       <div class="col-sm-12">
         <div class="containter" style="text-align:center">
@@ -144,14 +158,15 @@ function toggle(source) {
 
 		$sqlUpdate = ['FILE_STATUS' => 'TO_GENERATE'];
 
-		$reloadCache = V::get('reloadCache', 'off', $_POST);
-		if ($reloadCache == 'on') $sqlUpdate['BI_analiza_reloadCache'] = 'Y';
-		else $sqlUpdate['BI_analiza_reloadCache'] = 'N';
+		$reloadCache = V::get('reloadCache', 'No', $_POST);
+		$reloadCacheAvailable = ['Full', 'Part', 'No'];
+		if (!in_array($reloadCache, $reloadCacheAvailable)) throw new Exception("Błąd formularza!");
+		$sqlUpdate['BI_analiza_reloadCache'] = $reloadCache;
 
-		$query = "select count(*) from BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA where BI_analiza_reloadCache = 'Y'";
+		$query = "select count(*) from BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA where BI_analiza_reloadCache in ('Full', 'Part')";
 		$result = DB::getPDO()->fetchValue($query);
 		if ($result) $sqlUpdate['FILE_STATUS_info'] = 'Oczekuje na odświeżenie cache (wymagane dla innego rekordu)';
-		elseif ($reloadCache == 'on') $sqlUpdate['FILE_STATUS_info'] = 'Oczekuje na odświeżenie cache';
+		elseif ($reloadCache != 'No') $sqlUpdate['FILE_STATUS_info'] = 'Oczekuje na odświeżenie cache';
 		else $sqlUpdate['FILE_STATUS_info'] = "Oczekuje na wygenerowanie powiązań";
 
 		if ($this->powiazanie) { // wyzwolone z poziomu tabeli BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA - aktualizujemy istniejący rekord
@@ -315,14 +330,24 @@ where `REMOTE_PRIMARY_KEY` = '{$this->SOURCE['ID']}'";
       </div>
     </div>
     <div class="form-group">
-      <label class="col-sm-4 control-label">Odśwież cache (wymagane w przypadku, gdy dokonano dowolnych zmian w tabelach pracowników/kontrahentów)</label>
-      <div class="col-sm-1">
+      <label class="col-sm-4 control-label">
+         Odśwież cache
+      </label>
+      <div class="col-sm-2">
         <select name="reloadCache" class="form-control">
-          <option value="on">Tak</option>
-          <option value="off" selected>Nie</option>
+          <option value="No" selected>Nie</option>
+          <option value="Part">Częściowe</option>
+          <option value="Full">Pełne</option>
         </select>
       </div>
     </div>
+    <div class="form-group">
+       <div class="col-sm-offset-4 col-sm-8">
+         Częściowe odświeżenie wymagane w przypadku, gdy dokonano dowolnych zmian w tabelach pracowników/kontrahentów.<br/>
+         Pełne odświeżenie wymagane w przypadku modyfikacji baz KRS lub CEiDG (UWAGA - trwa to wiele godzin)
+       </div>
+    </div>
+    </div>
     <div class="form-group">
       <div class="col-sm-12">
         <div class="containter" style="text-align:center">
@@ -502,7 +527,7 @@ Router::getRoute("UrlAction_BiAuditGenerate")->' . $function . ';
 
 			### Sprawdzenie czy któryś rekord wymaga odświeżenia cache
 			$doGenerate = true;
-			$query = "select ID from BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA where BI_analiza_reloadCache = 'Y' and FILE_STATUS = 'TO_GENERATE'";
+			$query = "select ID from BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA where BI_analiza_reloadCache in ('Full', 'Part') and FILE_STATUS = 'TO_GENERATE'";
 			$result = DB::getPDO()->fetchAll($query);
 			if ($result) {
 				$doGenerate = false;
@@ -514,7 +539,7 @@ Router::getRoute("UrlAction_BiAuditGenerate")->' . $function . ';
 						if ($reloadCacheResult == "ok") {
 							$sqlArr = [
 								'FILE_STATUS_info' => 'Odświeżono cache, oczekiwanie na wygenerowanie powiązań',
-								'BI_analiza_reloadCache' => 'N',
+								'BI_analiza_reloadCache' => 'No',
 							];
 							$doGenerate = true;
 						} else {
@@ -557,7 +582,10 @@ Router::getRoute("UrlAction_BiAuditGenerate")->' . $function . ';
 						$sqlArr = [
 							'FILE_STATUS_info' => 'W trakcie odświeżania cache',
 						];
-						file_put_contents($reloadCachePhpFile, generatePhpScript('doReloadCache()'));
+
+						$query = "select count(*) from BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA where BI_analiza_reloadCache = 'Full' and FILE_STATUS = 'TO_GENERATE'";
+						$fullReloadCache = (DB::getPDO()->fetchValue($query) > 0);
+						file_put_contents($reloadCachePhpFile, generatePhpScript("doReloadCache({$fullReloadCache})"));
 						shell_exec('su - root -c "php ' . $reloadCachePhpFile . ' > ' . $reloadCacheLogFile. ' 2>&1 &"');
 					}
 				}
@@ -655,7 +683,7 @@ Router::getRoute("UrlAction_BiAuditGenerate")->' . $function . ';
 		}
 	}
 
-	private function reloadCache_reTeryt() {
+	private function reloadCache_reTeryt($full = false) {
 
 		Lib::loadClass('Teryt');
 
@@ -671,6 +699,22 @@ Router::getRoute("UrlAction_BiAuditGenerate")->' . $function . ';
 					'miejscowosc' => 'miejscowosc',
 					'ulica' => 'ulica',
 				],
+				'BI_audit_KRS' => [
+					'wojewodztwo' => 'S_wojewodztwo',
+					'powiat' => 'S_powiat',
+					'gmina' => 'S_gmina',
+					'miejscowosc' => 'A_miejscowosc',
+					'ulica' => 'A_ulica',
+					'kodPocztowy' => 'A_kod',
+				],
+				'BI_audit_CEIDG' => [
+					'wojewodztwo' => 'wojewodztwo',
+					'powiat' => 'powiat',
+					'gmina' => 'gmina',
+					'miejscowosc' => 'miejscowosc',
+					'ulica' => 'ulica',
+					'kodPocztowy' => 'kodPocztowy',
+				],
 			];
 
 			$columns = ['kodPocztowy', 'miejscowosc', 'ulica'];
@@ -695,11 +739,15 @@ Router::getRoute("UrlAction_BiAuditGenerate")->' . $function . ';
 			}
 		}
 
+		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() {
+	private function reloadCache_updateAll($full = false) {
 		$tables = [
 			'BI_audit_ENERGA_PRACOWNICY' => [
 				'fields' => [
@@ -707,6 +755,7 @@ Router::getRoute("UrlAction_BiAuditGenerate")->' . $function . ';
 					'regon' => 'regon',
 					'pesel' => 'pesel',
 				],
+				'base' => '0',
 				//'where' => '',
 			],
 			'BI_audit_ENERGA_PRACOWNICY_adresy' => [
@@ -716,10 +765,12 @@ Router::getRoute("UrlAction_BiAuditGenerate")->' . $function . ';
 					'nrDomu' => 'nrBudynku',
 					'nrLokalu' => 'nrLokalu',
 				],
+				'base' => '0',
 				//'where' => '',
 			],
 			'BI_audit_ENERGA_RUM_KONTRAHENCI' => [
 				'fields' => [
+					'krs' => 'KRS',
 					'nip' => 'NIP',
 					'regon' => 'REGON',
 					'pesel' => 'PESEL',
@@ -728,6 +779,7 @@ Router::getRoute("UrlAction_BiAuditGenerate")->' . $function . ';
 					'nrDomu' => 'Numer_budynku',
 					'nrLokalu' => 'Numer_mieszkania_lokalu',
 				],
+				'base' => '0',
 				'where' => "ownCompany != 'Y'",
 			],
 			'BI_audit_KW_requested_person' => [
@@ -737,39 +789,132 @@ Router::getRoute("UrlAction_BiAuditGenerate")->' . $function . ';
 					'nip' => 'Seller_person_NIP',
 					'regon' => 'Seller_person_REGON',
 				],
+				'base' => '0',
 				//'where' => '',
 			],
 		];
-		$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_ref_RELATIONS where BASE = 0";
+
+		$tables_full = [
+			'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_KRS' => [
+				'fields' => [
+					'krs' => 'krs',
+					'nip' => 'nip',
+					'regon' => 'regon',
+					'TERYT_SYM' => 'TERYT_SYM',
+					'TERYT_SYM_UL' => 'TERYT_SYM_UL',
+					'nrDomu' => 'A_nrDomu',
+					'nrLokalu' => 'A_nrLokalu',
+				],
+				'base' => '1',
+			],
+			'BI_audit_KRS_company' => [
+				'fields' => [
+					'regon' => 'regon',
+					'krs' => 'krs',
+				],
+				'base' => '1',
+			],
+			'BI_audit_KRS_person' => [
+				'fields' => [
+					'pesel' => 'pesel',
+				],
+				'base' => '1',
+			],
+		];
+
+		if ($full) {
+			$tables = array_merge($tables_full, $tables);
+			$fullWhere = '1 = 1';
+		} else {
+			$fullWhere = '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, " . implode(", ", array_keys($table['fields'])) . ") select '{$name}', ID, " . implode(", ", $table['fields']) . " from {$name} {$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}";
 		}
-		$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 BASE = 0";
-		$queries[] = "set @var = (select round(coalesce(max(ID), '0.5')) from BI_audit_ALL_ref_RELATIONS)";
-		$queries[] = "insert into BI_audit_ALL_ref_RELATIONS (ID, RELATION) select @var:=@var*2, a.RELATION from (select a.REMOTE_TABLE as RELATION 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) as a left join BI_audit_ALL_ref_RELATIONS r on a.RELATION = r.RELATION where r.RELATION is null";
+		$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";
+		}
+
 		$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) select all1.ID, all2.ID, rel.ID 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[] = "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";
 
 		foreach ($queries as $query) DB::getPDO()->query($query);
-		$BiAuditRelations = new BiAuditRelations();
-		$query = "select ID from BI_audit_ALL where BASE = 0";
+		$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);
 	}
 
-	public function doReloadCache() {
+	public function doReloadCache($full = false) {
 		$powiazaniaDirLocation = $this->getMainDirectory();
 		$tasksDirLocation = $powiazaniaDirLocation . "/.tasks";
 		$reloadCachePidFile = $tasksDirLocation . "/reloadCache.pid";
 		$reloadCacheResultFile = $tasksDirLocation . "/reloadCache.result";
 		file_put_contents($reloadCachePidFile, getmypid());
 		try {
-			$this->reloadCache_reTeryt();
-			$this->reloadCache_updateAll();
+			$this->reloadCache_reTeryt($full);
+			$this->reloadCache_updateAll($full);
 			file_put_contents($reloadCacheResultFile, "ok");
 		} catch (Exception $e) {
 			file_put_contents($reloadCacheResultFile, $e->getMessage());
@@ -844,6 +989,16 @@ Router::getRoute("UrlAction_BiAuditGenerate")->' . $function . ';
 		$sqlList['ActivateBiAuditGenerate'] = <<<SQL
 REPLACE INTO `CRM_CONFIG` (`CONF_KEY`,`CONF_VAL`) VALUES ('module_BiAudit','on');
 SQL;
+		$sqlList['ChangeStructure1'] = <<<SQL
+ALTER TABLE `BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA` CHANGE `BI_analiza_reloadCache` `BI_analiza_reloadCache` ENUM('Y','N','Full','Part','No') NOT NULL DEFAULT 'No';
+SQL;
+		$sqlList['ChangeStructure2'] = <<<SQL
+UPDATE `BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA` SET `BI_analiza_reloadCache` = IF (`BI_analiza_reloadCache` = 'Y', 'Part', 'No');
+SQL;
+		$sqlList['ChangeStructure3'] = <<<SQL
+ALTER TABLE `BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA` CHANGE `BI_analiza_reloadCache` `BI_analiza_reloadCache` ENUM('Full','Part','No') NOT NULL DEFAULT 'No';
+SQL;
+
 
 		$db = DB::getDB();
 		if ($db->has_errors()) {
@@ -861,6 +1016,7 @@ SQL;
 
 class BiAuditRelations {
 	private $RELATIONS_ID = [];
+	private $full;
 
 	private $relations = [
 		'nip' => ['nip'],
@@ -871,9 +1027,10 @@ class BiAuditRelations {
 	];
 
 	private $destTables = ['BI_audit_ENERGA_RUM_KONTRAHENCI', 'BI_audit_KW_requested_person'];
-	private $sourceTable = ['BI_audit_ENERGA_PRACOWNICY', 'BI_audit_ENERGA_PRACOWNICY_adresy'];
+	private $sourceTables = ['BI_audit_ENERGA_PRACOWNICY', 'BI_audit_ENERGA_PRACOWNICY_adresy'];
 
-	public function __construct() {
+	public function __construct($full = false) {
+		$this->full = $full;
 		$query = "select * from BI_audit_ALL_ref_RELATIONS";
 		$result = DB::getPDO()->fetchAll($query);
 		foreach ($result as $row) $this->RELATIONS_ID[$row['RELATION']] = $row['ID'];
@@ -912,8 +1069,10 @@ class BiAuditRelations {
 		}
 		if (!$joins) return false;
 
-		$query = "select t2.ID as ID, t1.REMOTE_TABLE as REMOTE_TABLE_1, t2.REMOTE_TABLE as REMOTE_TABLE_2 {$items} " .
-			"from BI_audit_ALL t1 join BI_audit_ALL t2 on " . implode(" or ", $joins) . " where t1.ID = {$ID} and (t2.BASE = 1 or t2.ID > {$ID})";
+		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}";
 		$result = DB::getPDO()->fetchAll($query);
 		foreach ($result as $row) {
 			$relationID = 0;
@@ -925,11 +1084,11 @@ class BiAuditRelations {
 			$end1 = (in_array($row['REMOTE_TABLE_1'], $this->destTables) ? 1 : 0);
 			$end2 = (in_array($row['REMOTE_TABLE_2'], $this->destTables) ? 1 : 0);
 			if (!($start2 || $end1)) {
-				$query = "insert into BI_audit_ALL_ref (ID1, ID2, RELATION_ID, END) values ({$ID}, {$row['ID']}, '{$relationID}', {$end2})";
+				$query = "insert into BI_audit_ALL_ref (ID1, ID2, RELATION_ID, END, BASE) values ({$ID}, {$row['ID']}, '{$relationID}', {$end2}, {$row['BASE']})";
 				DB::getPDO()->query($query);
 			}
 			if (!($start1 || $end2)) {
-				$query = "insert into BI_audit_ALL_ref (ID2, ID1, RELATION_ID, END) values ({$ID}, {$row['ID']}, '{$relationID}', {$end1})";
+				$query = "insert into BI_audit_ALL_ref (ID2, ID1, RELATION_ID, END, BASE) values ({$ID}, {$row['ID']}, '{$relationID}', {$end1}, {$row['BASE']})";
 				DB::getPDO()->query($query);
 			}
 		}
@@ -950,7 +1109,7 @@ class BiAuditPowiazania {
 
 	public function __construct($ID = 0) {
 		if (!$ID) throw new Exception("Wrong ID parameter");
-		$query = "select BI_analiza_depth from BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA where ID = '{$ID}' and FILE_STATUS = 'IN_PROGRESS' and BI_analiza_reloadCache != 'Y'";
+		$query = "select BI_analiza_depth from BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA where ID = '{$ID}' and FILE_STATUS = 'IN_PROGRESS' and BI_analiza_reloadCache not in ('Full', 'Part')";
 		$result = DB::getPDO()->fetchAll($query);
 		if (!$result) throw new Exception("Błąd danych");
 		$this->DEPTH = (int) $result[0]['BI_analiza_depth'];

+ 1 - 1
SE/superedit-SQIX_STRUCTURE_DB_SYNC.php

@@ -294,7 +294,7 @@ $TABLES_STRUCTURE_arr['MAIN'] = array('QUALITY_NOTICES','QUALITY_NOTICES_HIST','
 'Rozdzielcza_Przeciski_110mm_HIST','Rozdzielcza_Wykop_przedmiar_na_mikrorurki','Rozdzielcza_Wykop_przedmiar_na_mikrorurki_HIST','Rozdzielcza_Zabruki','Rozdzielcza_Zabruki_HIST','Rozdzielcza_Kabel_Swiatlowodowy_wsg84','Rozdzielcza_Kabel_Swiatlowodowy_wsg84_HIST','Rozdzielcza_koszty_dodatkowe_wsg84',
 'Rozdzielcza_koszty_dodatkowe_wsg84_HIST','Rozdzielcza_wezly','Rozdzielcza_wezly_HIST','Rozdzielcza_Mikrokanalizacja_do_klienta','Rozdzielcza_Mikrokanalizacja_do_klienta_HIST','Rozdzielcza_PionyKablowe','Rozdzielcza_PionyKablowe_HIST','opt_kable','opt_kable_HIST','opt_lokalizacje','opt_lokalizacje_HIST','opt_spawy','opt_spawy_HIST','opt_przelacznice_mufy','opt_przelacznice_mufy_HIST','opt_porty','opt_porty_HIST','opt_wlokna','opt_wlokna_HIST','opt_tuby','opt_tuby_HIST','opt_tacki','opt_tacki_HIST','Rozdzielcza_rurociag_wsg84','Rozdzielcza_rurociag_wsg84_HIST','MK_BIALE_PLAMY','MK_BIALE_PLAMY_HIST','CRM_CONFIG','MK_BIALE_PLAMY_lokale','MK_Rewiry_POPC_sum','MK_Rewiry','MK_Rewiry_HIST','Rozdzielcza_Wezly_POPC','Rozdzielcza_Wezly_POPC_HIST','HOLIDAYS_REQUESTS','HOLIDAYS_REQUESTS_HIST','BILLING_OWNER','BILLING_OWNER_HIST','CRM_REF_CONFIG','WORKER_GPS_LOG','WORKER_GPS_LOG_HIST','P5Automation1OrangeRaportSimple','P5Automation1OrangeRaportSimple_HIST');
 
-$TABLES_STRUCTURE_DATA_arr['MAIN'] = array('CRM_PRZYPADEK','CRM_REF_CONFIG');
+$TABLES_STRUCTURE_DATA_arr['MAIN'] = array('CRM_PRZYPADEK');
 
  
 $TABLES_STRUCTURE_arr['BiAudit'] = array('BI_audit_ALL','BI_audit_ALL_ref','BI_audit_ALL_ref_RELATIONS','BI_audit_CEIDG','BI_audit_CEIDG_pelnomocnicy','BI_audit_CEIDG_powiazania','BI_audit_ENERGA_PRACOWNICY','BI_audit_ENERGA_PRACOWNICY_adresy','BI_audit_ENERGA_PRACOWNICY_HIST','BI_audit_ENERGA_RUM_KONTRAHENCI','BI_audit_ENERGA_RUM_KONTRAHENCI_HIST','BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA','BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_HIST','BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row','BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_HIST','BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object','BI_audit_ENERGA_RUM_UMOWY','BI_audit_ENERGA_RUM_UMOWY_HIST','BI_audit_KRS','BI_audit_KRS_company','BI_audit_KRS_person','BI_audit_KW_requested','BI_audit_KW_requested_HIST','BI_audit_KW_requested_person');