Jelajahi Sumber

merge z xmlschema + prace nad dodaniem BI_audit analizy jako modul WPS

a.binder 8 tahun lalu
induk
melakukan
d469fb7e4a
21 mengubah file dengan 1496 tambahan dan 32 penghapusan
  1. 3 0
      SE/build_SE.xml
  2. 58 0
      SE/schema/WPS_Functions/BI_audit/1_import_kw_person_and_optimize_db.php
  3. 149 0
      SE/schema/WPS_Functions/BI_audit/1_rum_create_ref.php
  4. 64 0
      SE/schema/WPS_Functions/BI_audit/2_verify_kontrahenci.php
  5. 108 0
      SE/schema/WPS_Functions/BI_audit/3_create_all.php
  6. 93 0
      SE/schema/WPS_Functions/BI_audit/4_find_corelations.php
  7. 93 0
      SE/schema/WPS_Functions/BI_audit/4_find_corelations_local.php
  8. 97 0
      SE/schema/WPS_Functions/BI_audit/4_find_corelations_local_global.php
  9. 155 0
      SE/schema/WPS_Functions/BI_audit/4_find_corelations_local_memory.php
  10. 89 0
      SE/schema/WPS_Functions/BI_audit/5_analyse.php
  11. 95 0
      SE/schema/WPS_Functions/BI_audit/5_generate_xml.php
  12. 94 0
      SE/schema/WPS_Functions/BI_audit/build_BI_audit.xml
  13. 130 0
      SE/schema/WPS_Functions/BI_audit/cache_relations.php
  14. 126 0
      SE/schema/WPS_Functions/BI_audit/find_corelations.php
  15. 14 0
      SE/schema/WPS_Functions/build_WPS_Functions.xml
  16. 9 0
      SE/schema/WPS_Functions/default_db/CRM_PROCES_tree/build_CRM_PROCES_tree.xml
  17. 55 20
      SE/schema/WPS_Functions/default_db/recurse_ant/build_recurse_ant.xml
  18. 23 2
      SE/schema/WPS_Functions/get_wfs/build_get_wfs.xml
  19. 1 1
      SE/schema/WPS_Functions/mapserver_gml_to_png/build_mapserver_gml_to_png.xml
  20. 33 9
      SE/schema/WPS_Functions/mapserver_gml_to_png/map/genpng2.sh
  21. 7 0
      SE/schema/WPS_Functions/object_modeling_by_default_db_xml_schema/build_object_modeling_by_default_db_xml_schema.xml

+ 3 - 0
SE/build_SE.xml

@@ -24,6 +24,9 @@
     <property name="procesy5_config.folders.properties.xml" location="${build_SE_basedir}/procesy5_config.folders.properties.xml"/>
   
   
+    <!-- TODO config/licence/to handle params to SYNC with xmlschema and WPS_Functions -->
+    <property name="bootstrap.php" location="${basedir}/se-lib/bootstrap.php"/> <!-- used in php scripts to execute  as root require("/Library/Server/Web/Data/Sites/SE-producition-git/SE/se-lib/bootstrap.php"); -->
+  
    <!-- task to maintain properties -->
   
     <target name="save_build_properties" depends="build_SE.install_dirs,build_SE_read_procesy5_file_properties">

+ 58 - 0
SE/schema/WPS_Functions/BI_audit/1_import_kw_person_and_optimize_db.php

@@ -0,0 +1,58 @@
+#!/usr/bin/env php
+<?php
+$_SERVER['SERVER_NAME'] = "biuro.biall-net.pl";
+require("/Library/Server/Web/Data/Sites/SE-production-git/SE/se-lib/bootstrap.php");
+date_default_timezone_set('Europe/Warsaw');// PHP 5 >= 5.1.0 required by date functions
+
+
+$queries[] = "create temporary table BI_audit_KW_requested_person_temp select * from BI_audit_KW_requested_person_copy";
+$queries[] = "truncate table `CRM__#REF_TABLE__24`";
+$queries[] = "insert into `CRM__#REF_TABLE__24` (PRIMARY_KEY, REMOTE_PRIMARY_KEY) select Id_BI_audit_KW_requested, ID from BI_audit_KW_requested_person_temp";
+$queries[] = "update BI_audit_KW_requested_person_temp set Seller_person = trim(Seller_person)";
+$queries[] = "update BI_audit_KW_requested_person_temp set Seller_person_pesel = null where Seller_person_pesel = 0";
+$queries[] = "update BI_audit_KW_requested_person_temp set Seller_person_KRS = null where Seller_person_KRS = 0";
+$queries[] = "update BI_audit_KW_requested_person_temp set Seller_person_NIP = null where Seller_person_NIP = 0";
+$queries[] = "update BI_audit_KW_requested_person_temp set Seller_person_REGON = null where Seller_person_REGON = 0";
+
+foreach ($queries as $query) DB::getPDO()->query($query);
+
+$query = "select Seller_person, Seller_person_pesel, Seller_person_KRS, Seller_person_NIP, Seller_person_REGON, count(*) as c from BI_audit_KW_requested_person_temp group by Seller_person, Seller_person_pesel, Seller_person_KRS, Seller_person_NIP, Seller_person_REGON having c>1";
+$duplicates = DB::getPDO()->fetchall($query);
+
+$itemsCount = 0; $companiesCount = 0;
+try{
+	foreach ($duplicates as $duplicate) {
+		$query = "select ID from BI_audit_KW_requested_person_temp where Seller_person='" . addslashes($duplicate['Seller_person']) . "'" .
+			" and Seller_person_pesel" . (($duplicate['Seller_person_pesel'] === null) ? " is null" : "='" . addslashes($duplicate['Seller_person_pesel']) . "'") .
+			" and Seller_person_KRS" . (($duplicate['Seller_person_KRS'] === null) ? " is null" : "='" . addslashes($duplicate['Seller_person_KRS']) . "'") .
+			" and Seller_person_NIP" . (($duplicate['Seller_person_NIP'] === null) ? " is null" : "='" . addslashes($duplicate['Seller_person_NIP']) . "'") .
+			" and Seller_person_REGON" . (($duplicate['Seller_person_REGON'] === null) ? " is null" : "='" . addslashes($duplicate['Seller_person_REGON']) . "'") .
+			" order by ID";
+		$items = DB::getPDO()->fetchall($query);
+		$id = $items[0]['ID'];
+		for ($i = 1; $i < count($items); $i++) {
+			DB::getPDO()->query("update `CRM__#REF_TABLE__24` set REMOTE_PRIMARY_KEY={$id} where REMOTE_PRIMARY_KEY={$items[$i]['ID']}");
+			DB::getPDO()->query("delete from BI_audit_KW_requested_person_temp where ID={$items[$i]['ID']}");
+			$itemsCount++;
+		}
+		if ($items) $companiesCount++;
+	}
+} catch (Exception $e) {
+	echo "{$e->getMessage()}\n{$query}\n";
+}
+$i = 1;
+$results = DB::getPDO()->fetchall("select ID from BI_audit_KW_requested_person_temp order by ID");
+foreach ($results as $result) {
+	if ($result["ID"] != $i) {
+		DB::getPDO()->query("update BI_audit_KW_requested_person_temp set ID={$i} where ID={$result["ID"]}");
+		DB::getPDO()->query("update `CRM__#REF_TABLE__24` set REMOTE_PRIMARY_KEY={$i} where REMOTE_PRIMARY_KEY={$result["ID"]}");
+	}
+	$i++;
+}
+DB::getPDO()->query("ALTER TABLE `BI_audit_KW_requested_person_temp` ORDER BY `ID`");
+DB::getPDO()->query("ALTER TABLE `BI_audit_KW_requested_person_temp` auto_increment = {$i} ROW_FORMAT = DYNAMIC");
+DB::getPDO()->query("ALTER TABLE `BI_audit_KW_requested_person_temp` DROP COLUMN Id_BI_audit_KW_requested");
+DB::getPDO()->query("truncate table `BI_audit_KW_requested_person`");
+DB::getPDO()->query("insert into `BI_audit_KW_requested_person` select * from `BI_audit_KW_requested_person_temp`");
+
+echo "Reduced {$itemsCount} entries for {$companiesCount} of " . count($duplicates) . " entries.\n";

+ 149 - 0
SE/schema/WPS_Functions/BI_audit/1_rum_create_ref.php

@@ -0,0 +1,149 @@
+#!/usr/bin/env php
+<?php
+$_SERVER['SERVER_NAME'] = "biuro.biall-net.pl";
+require("/Library/Server/Web/Data/Sites/Default/dev-bzyk/se-lib/bootstrap.php");
+#require("/Library/Server/Web/Data/Sites/SE-producition-git/SE/se-lib/bootstrap.php");
+date_default_timezone_set('Europe/Warsaw');// PHP 5 >= 5.1.0 required by date functions
+
+
+function is_nip($nip) {
+	$waga = [6, 5, 7, 2, 3, 4, 5, 6, 7];
+	$c = 0;
+	for ($i = 0; $i < 9; $i++) $c += $nip[$i] * $waga[$i];
+	$c = ($c % 11) % 10;
+	if ($nip[9] == $c) return true;
+	else return false;
+}
+
+function is_regon($regon) {
+	$waga = [8, 9, 2, 3, 4, 5, 6, 7];
+	$c = 0;
+	for ($i = 0; $i < 8; $i++) $c += $regon[$i] * $waga[$i];
+	$c = ($c % 11) % 10;
+	if ($regon[8] == $c) return true;
+	else return false;
+}
+
+function parseNipRegon($string) {
+	preg_match('/^(.*[^[:digit:]])?([[:digit:]]{10})([^[:digit:]].*)?$/', str_replace('-', '', $string), $matches);
+	if ($matches && is_nip($matches[2])) $nip = $matches[2];
+	else $nip = null;
+
+	preg_match('/^(.*[^[:digit:]])?([[:digit:]]{9})([^[:digit:]].*)?$/', str_replace('-', '', $string), $matches);
+	if ($matches && is_regon($matches[2])) $regon = $matches[2];
+	else $regon = null;
+
+	return [$nip, $regon];
+}
+
+                                        
+$fields['BI_audit_ENERGA_RUM_KONTRAHENCI'] = [
+	'Tytul_dokumentu',
+	'Pelna_nazwa_kontrahenta',
+	'NIP',
+	'REGON',
+	'KRS',
+	'Kraj',
+	'Kod_pocztowy',
+	'Miejscowosc',
+	'Ulica',
+	'Numer_budynku',
+	'Numer_mieszkania_lokalu',
+	'TERYT_SYM',
+	'TERYT_SYM_UL',
+];
+
+$fields['BI_audit_KRS'] = [
+	'"Dodano z BI_audit_KRS"',
+	'nazwa',
+	'nip',
+	'regon',
+	'krs',
+	'A_kraj',
+	'A_kod',
+	'A_miejscowosc',
+	'A_ulica',
+	'A_nrDomu',
+	'A_nrLokalu',
+	'TERYT_SYM',
+	'TERYT_SYM_UL',
+];
+
+$fields['BI_audit_CEIDG'] = [
+	'"Dodano z BI_audit_CEIDG"',
+	'firma',
+	'nip',
+	'regon',
+	'null',
+	'kraj',
+	'kodPocztowy',
+	'miejscowosc',
+	'ulica',
+	'budynek',
+	'lokal',
+	'TERYT_SYM',
+	'TERYT_SYM_UL',
+];
+
+DB::getPDO()->query("truncate table `CRM__#REF_TABLE__23`");
+$query = "select ID, Strony_umowy from `BI_audit_ENERGA_RUM_UMOWY`";
+$result = DB::query($query);
+while ($row = DB::fetch($result)) {
+	$ID = $row->ID;
+	$strony = array_map('trim', explode("|", $row->Strony_umowy));
+	$K_IDs = [];
+	foreach ($strony as $item) {
+		$strona = array_map('trim', explode(";", $item));
+		$nr = null;
+		if (count($strona) == 3) $i = 1;//$nr = $strona[1];
+		elseif (count($strona) == 4) $i = 2;//$nr = $strona[2];
+		elseif (count($strona) >= 5 && count($strona) <= 6) $i = 3;//$nr = $strona[3];
+		else $i = null;
+		if ($i) $nr = $strona[$i];
+		$notFound = false;
+		if (is_numeric($nr)) {
+			$query = "select ID from BI_audit_ENERGA_RUM_KONTRAHENCI where Numer_kontrahenta = '{$nr}'";
+			if ($K_ID = DB::getPDO()->fetchValue($query)) {
+				$K_IDs[] = $K_ID;
+			} else {
+				$nazwa = $strona[$i+1];
+				$query = "select ID from BI_audit_ENERGA_RUM_KONTRAHENCI where Pelna_nazwa_kontrahenta = '" . addslashes($nazwa) . "'";
+				if ($result2 = DB::getPDO()->fetchall($query)) {
+					foreach ($result2 as $item) $K_IDs[] = $item['ID'];
+				} else $notFound = true;
+			}
+		} else $notFound = true;
+		if ($notFound) {
+			list($nip, $regon) = parseNipRegon($item);
+			if ($nip || $regon) {
+				$query = "select ID from BI_audit_ENERGA_RUM_KONTRAHENCI where NIP = '{$nip}' or REGON = '{$regon}'";
+				if ($result2 = DB::getPDO()->fetchall($query)) {
+					foreach ($result2 as $item) $K_IDs[] = $item['ID'];
+				} else {
+					$query = "select ID from BI_audit_KRS where nip = '{$nip}' or regon = '{$regon}'";
+					if ($result2 = DB::getPDO()->fetchall($query)) {
+						foreach ($result2 as $item) {
+							$query = "insert into BI_audit_ENERGA_RUM_KONTRAHENCI (" . implode(', ', $fields['BI_audit_ENERGA_RUM_KONTRAHENCI']) . ")" .
+								" select " . implode(', ', $fields['BI_audit_KRS']) . " from BI_audit_KRS where ID = '{$item['ID']}'";
+							if (DB::getPDO()->query($query)) $K_IDs[] = DB::getPDO()->lastInsertId();
+						}
+					} else {
+						$query = "select ID from BI_audit_CEIDG where nip = '{$nip}' or regon = '{$regon}'";
+						if ($result2 = DB::getPDO()->fetchall($query)) {
+							foreach ($result2 as $item) {
+								$query = "insert into BI_audit_ENERGA_RUM_KONTRAHENCI (" . implode(', ', $fields['BI_audit_ENERGA_RUM_KONTRAHENCI']) . ")" .
+									" select " . implode(', ', $fields['BI_audit_CEIDG']) . " from BI_audit_CEIDG where ID = '{$item['ID']}'";
+								if (DB::getPDO()->query($query)) $K_IDs[] = DB::getPDO()->lastInsertId();
+							}
+						}
+					}
+				}
+			}
+		}
+	}
+	foreach ($K_IDs as $K_ID) {
+		$query = "insert into `CRM__#REF_TABLE__23` (PRIMARY_KEY, REMOTE_PRIMARY_KEY) values ('{$ID}', '{$K_ID}')";
+		DB::getPDO()->query($query);
+	}
+}
+

+ 64 - 0
SE/schema/WPS_Functions/BI_audit/2_verify_kontrahenci.php

@@ -0,0 +1,64 @@
+#!/usr/bin/env php
+<?php
+$_SERVER['SERVER_NAME'] = "biuro.biall-net.pl";
+require("/Library/Server/Web/Data/Sites/Default/dev-bzyk/se-lib/bootstrap.php");
+#require("/Library/Server/Web/Data/Sites/SE-producition-git/SE/se-lib/bootstrap.php");
+date_default_timezone_set('Europe/Warsaw');// PHP 5 >= 5.1.0 required by date functions
+
+$query = "select ID, NIP, REGON, KRS from BI_audit_ENERGA_RUM_KONTRAHENCI where (NIP is null or REGON is null or KRS is null) and not (NIP is null and REGON is null and KRS is null)";
+$result = DB::query($query);
+while ($row = DB::fetch($result)) {
+	$query = "select nip, regon, krs from BI_audit_KRS where nip = '{$row->NIP}' or regon = '{$row->REGON}' or krs = '{$row->KRS}'";
+	if ($items = DB::getPDO()->fetchAll($query)) {
+		list($nip, $regon, $krs) = [$items[0]["nip"], $items[0]["regon"], $items[0]["krs"]];
+		if (count($items) > 1) {
+			for ($i = 1; $i < count($items); $i++) {
+				if ($nip != $items[$i]["nip"]) $nip = null;
+				if ($regon != $items[$i]["regon"]) $regon = null;
+				if ($krs != $items[$i]["krs"]) $krs = null;
+			}
+		}
+		if (!($nip || $regon || $krs)) {
+			echo "ERROR!\n";
+			echo $query . "\n";
+		} else {
+			$set = [];
+			if ($nip && $nip != $row->NIP) $set[] = "NIP = '{$nip}'";
+			if ($regon && $regon != $row->REGON) $set[] = "REGON = '{$regon}'";
+			if ($krs && $krs != $row->KRS) $set[] = "KRS = '{$krs}'";
+			if ($set) {
+				$query = "update BI_audit_ENERGA_RUM_KONTRAHENCI set " . implode(', ', $set) . " where ID = '{$row->ID}'";
+				DB::query($query);
+				echo $query . "\n";
+			}
+		}
+	}
+}
+
+$query = "select ID, NIP, REGON from BI_audit_ENERGA_RUM_KONTRAHENCI where (NIP is null or REGON is null) and not (NIP is null and REGON is null)";
+$result = DB::query($query);
+while ($row = DB::fetch($result)) {
+	$query = "select nip, regon from BI_audit_CEIDG where nip = '{$row->NIP}' or regon = '{$row->REGON}'";
+	if ($items = DB::getPDO()->fetchAll($query)) {
+		list($nip, $regon) = [$items[0]["nip"], $items[0]["regon"]];
+		if (count($items) > 1) {
+			for ($i = 1; $i < count($items); $i++) {
+				if ($nip != $items[$i]["nip"]) $nip = null;
+				if ($regon != $items[$i]["regon"]) $regon = null;
+			}
+		}
+		if (!($nip || $regon)) {
+			echo "ERROR!\n";
+			echo $query . "\n";
+		} else {
+			$set = [];
+			if ($nip && $nip != $row->NIP) $set[] = "NIP = '{$nip}'";
+			if ($regon && $regon != $row->REGON) $set[] = "REGON = '{$regon}'";
+			if ($set) {
+				$query = "update BI_audit_ENERGA_RUM_KONTRAHENCI set " . implode(', ', $set) . " where ID = '{$row->ID}'";
+				DB::query($query);
+				echo $query . "\n";
+			}
+		}
+	}
+}

+ 108 - 0
SE/schema/WPS_Functions/BI_audit/3_create_all.php

@@ -0,0 +1,108 @@
+#!/usr/bin/env php
+<?php
+$_SERVER['SERVER_NAME'] = "biuro.biall-net.pl";
+require("/Library/Server/Web/Data/Sites/Default/dev-bzyk/se-lib/bootstrap.php");
+#require("/Library/Server/Web/Data/Sites/SE-producition-git/SE/se-lib/bootstrap.php");
+date_default_timezone_set('Europe/Warsaw');// PHP 5 >= 5.1.0 required by date functions
+
+$tables = [
+	'BI_audit_CEIDG' => [
+		'nip' => 'nip',
+		'regon' => 'regon',
+		'TERYT_SYM' => 'TERYT_SYM',
+		'TERYT_SYM_UL' => 'TERYT_SYM_UL',
+		'nrDomu' => 'budynek',
+		'nrLokalu' => 'lokal',
+	],
+	'BI_audit_CEIDG_pelnomocnicy' => [
+		'nip' => 'nip',
+		'TERYT_SYM' => 'TERYT_SYM',
+		'TERYT_SYM_UL' => 'TERYT_SYM_UL',
+		'nrDomu' => 'budynek',
+		'nrLokalu' => 'lokal',
+	],
+	'BI_audit_CEIDG_powiazania' => [
+		'nip' => 'nip',
+		'regon' => 'regon',
+	],
+	'BI_audit_ENERGA_PRACOWNICY' => [
+		'nip' => 'nip',
+		'regon' => 'regon',
+		'pesel' => 'pesel',
+	],
+	'BI_audit_ENERGA_PRACOWNICY_adresy' => [
+		'TERYT_SYM' => 'TERYT_SYM',
+		'TERYT_SYM_UL' => 'TERYT_SYM_UL',
+		'nrDomu' => 'nrBudynku',
+		'nrLokalu' => 'nrLokalu',
+	],
+	'BI_audit_ENERGA_RUM_KONTRAHENCI' => [
+		'nip' => 'NIP',
+		'regon' => 'REGON',
+		'pesel' => 'PESEL',
+		'TERYT_SYM' => 'TERYT_SYM',
+		'TERYT_SYM_UL' => 'TERYT_SYM_UL',
+		'nrDomu' => 'Numer_budynku',
+		'nrLokalu' => 'Numer_mieszkania_lokalu',
+	],
+	'BI_audit_KRS' => [
+		'krs' => 'krs',
+		'nip' => 'nip',
+		'regon' => 'regon',
+		'TERYT_SYM' => 'TERYT_SYM',
+		'TERYT_SYM_UL' => 'TERYT_SYM_UL',
+		'nrDomu' => 'A_nrDomu',
+		'nrLokalu' => 'A_nrLokalu',
+	],
+	'BI_audit_KRS_company' => [
+		'regon' => 'regon',
+		'krs' => 'krs',
+	],
+	'BI_audit_KRS_person' => [
+		'pesel' => 'pesel',
+	],
+//	'BI_audit_KW_requested' => [
+//	],
+	'BI_audit_KW_requested_person' => [
+		'pesel' => 'Seller_person_pesel',
+		'krs' => 'Seller_person_KRS',
+		'nip' => 'Seller_person_NIP',
+		'regon' => 'Seller_person_REGON',
+	],
+];
+
+$query[] = "truncate table BI_audit_ALL";
+foreach ($tables as $name => $table) {
+	if ($table) $query[] = "insert into BI_audit_ALL (REMOTE_TABLE, REMOTE_ID, " . implode(", ", array_keys($table)) . ") select '{$name}', ID, " . implode(", ", $table) . " from {$name}";
+	else $query[] = "insert into BI_audit_ALL (REMOTE_TABLE, REMOTE_ID) select '{$name}', ID from {$name}";
+}
+$query[] = "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";
+
+$query = [];
+$query[] = "truncate table BI_audit_ALL_ref";
+
+$query[] = "insert ignore into BI_audit_ALL_ref (ID1, ID2, RELATION) select all1.ID, all2.ID, all1.REMOTE_TABLE from BI_audit_ALL as all1 join `CRM__#REF_TABLE__19` 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";
+$query[] = "insert ignore into BI_audit_ALL_ref (ID1, ID2, RELATION) select all2.ID, all1.ID, all2.REMOTE_TABLE from BI_audit_ALL as all1 join `CRM__#REF_TABLE__19` 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";
+
+$query[] = "insert ignore into BI_audit_ALL_ref (ID1, ID2, RELATION) select all1.ID, all2.ID, all1.REMOTE_TABLE from BI_audit_ALL as all1 join `CRM__#REF_TABLE__18` 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";
+$query[] = "insert ignore into BI_audit_ALL_ref (ID1, ID2, RELATION) select all2.ID, all1.ID, all2.REMOTE_TABLE from BI_audit_ALL as all1 join `CRM__#REF_TABLE__18` 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";
+
+$query[] = "insert ignore into BI_audit_ALL_ref (ID1, ID2, RELATION) select all1.ID, all2.ID, all1.REMOTE_TABLE from BI_audit_ALL as all1 join `CRM__#REF_TABLE__11` 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";
+$query[] = "insert ignore into BI_audit_ALL_ref (ID1, ID2, RELATION) select all2.ID, all1.ID, all2.REMOTE_TABLE from BI_audit_ALL as all1 join `CRM__#REF_TABLE__11` 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";
+
+$query[] = "insert ignore into BI_audit_ALL_ref (ID1, ID2, RELATION) select all1.ID, all2.ID, all1.REMOTE_TABLE from BI_audit_ALL as all1 join `CRM__#REF_TABLE__12` 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";
+$query[] = "insert ignore into BI_audit_ALL_ref (ID1, ID2, RELATION) select all2.ID, all1.ID, all2.REMOTE_TABLE from BI_audit_ALL as all1 join `CRM__#REF_TABLE__12` 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";
+
+$query[] = "insert ignore into BI_audit_ALL_ref (ID1, ID2, RELATION) select all1.ID, all2.ID, all1.REMOTE_TABLE from BI_audit_ALL as all1 join `BI_audit_ENERGA_PRACOWNICY` as pracownicy on all1.REMOTE_TABLE = 'BI_audit_ENERGA_PRACOWNICY' and all1.REMOTE_ID = pracownicy.ID join BI_audit_ENERGA_PRACOWNICY_adresy as adresy on pracownicy.ID = adresy.ID_BI_audit_ENERGA_PRACOWNICY join BI_audit_ALL as all2 on all2.REMOTE_TABLE = 'BI_audit_ENERGA_PRACOWNICY_adresy' and adresy.ID = all2.REMOTE_ID";
+$query[] = "insert ignore into BI_audit_ALL_ref (ID1, ID2, RELATION) select all2.ID, all1.ID, all2.REMOTE_TABLE from BI_audit_ALL as all1 join `BI_audit_ENERGA_PRACOWNICY` as pracownicy on all1.REMOTE_TABLE = 'BI_audit_ENERGA_PRACOWNICY' and all1.REMOTE_ID = pracownicy.ID join BI_audit_ENERGA_PRACOWNICY_adresy as adresy on pracownicy.ID = adresy.ID_BI_audit_ENERGA_PRACOWNICY join BI_audit_ALL as all2 on all2.REMOTE_TABLE = 'BI_audit_ENERGA_PRACOWNICY_adresy' and adresy.ID = all2.REMOTE_ID";
+
+//$query[] = "replace into BI_audit_ALL_ref (ID1, ID2, RELATION) select all1.ID, all2.ID, all1.REMOTE_TABLE from BI_audit_ALL as all1 join `CRM__#REF_TABLE__24` as ref on all1.REMOTE_TABLE = 'BI_audit_KW_requested' and all1.REMOTE_ID = ref.PRIMARY_KEY join BI_audit_ALL as all2 on all2.REMOTE_TABLE = 'BI_audit_KW_requested_person' and ref.REMOTE_PRIMARY_KEY = all2.REMOTE_ID";
+//$query[] = "replace into BI_audit_ALL_ref (ID1, ID2, RELATION) select all2.ID, all1.ID, all2.REMOTE_TABLE from BI_audit_ALL as all1 join `CRM__#REF_TABLE__24` as ref on all1.REMOTE_TABLE = 'BI_audit_KW_requested' and all1.REMOTE_ID = ref.PRIMARY_KEY join BI_audit_ALL as all2 on all2.REMOTE_TABLE = 'BI_audit_KW_requested_person' and ref.REMOTE_PRIMARY_KEY = all2.REMOTE_ID";
+
+
+//$query[] = "replace into BI_audit_ALL_ref (ID1, ID2) select ID2, ID1 from BI_audit_ALL_ref";
+
+foreach ($query as $sql) {
+	echo $sql . "\n";
+	DB::getPDO()->query($sql);
+}

+ 93 - 0
SE/schema/WPS_Functions/BI_audit/4_find_corelations.php

@@ -0,0 +1,93 @@
+#!/usr/bin/env php
+<?php
+$_SERVER['SERVER_NAME'] = "biuro.biall-net.pl";
+require("/Library/Server/Web/Data/Sites/Default/dev-bzyk/se-lib/bootstrap.php");
+#require("/Library/Server/Web/Data/Sites/SE-producition-git/SE/se-lib/bootstrap.php");
+date_default_timezone_set('Europe/Warsaw');// PHP 5 >= 5.1.0 required by date functions
+
+class FindCorelations {
+
+	private $TABLE, $TABLE_REF, $TABLE_DEST, $DEPTH;
+	public $results = [];
+
+	private $corelations = [
+		'nip' => ['nip'],
+		'regon' => ['regon'],
+		'krs' => ['krs'],
+		'pesel' => ['pesel'],
+		'adres' => ['TERYT_SYM', 'TERYT_SYM_UL', 'nrDomu', 'nrLokalu'],
+	];
+
+	function __construct($TABLE, $TABLE_REF, $TABLE_DEST, $DEPTH, $ID) {
+		$this->TABLE = $TABLE;
+		$this->TABLE_REF = $TABLE_REF;
+		$this->TABLE_DEST = $TABLE_DEST;
+		$this->DEPTH = $DEPTH;
+		$this->findNodes($ID);
+	}
+
+	function findNodes($ID, $corelation = "", $path = []) {
+		if (isset($path[$ID])) return null;
+		$path[$ID] = $corelation;
+		$query = "select * from {$this->TABLE} where ID = '{$ID}'";
+		if (!($result = DB::query($query))) return null;
+		$row = DB::fetch($result);
+		if (in_array($row->REMOTE_TABLE, $this->TABLE_DEST)) {
+			if ($row->REMOTE_TABLE == "BI_audit_KW_requested") implode("->", array_keys($path)) . "\n";
+			$this->results[] = $path;
+			return null;
+		}
+
+		if (count($path) > $this->DEPTH) return null;
+		$nodes = [];
+		foreach ($this->corelations as $name => $columns) {
+			$ok = 0;
+			$join = [];
+			foreach ($columns as $column) {
+				$join[] = "t1.{$column} = t2.{$column}";
+				if ($row->$column) {
+					$ok++;
+				}
+			}
+			if ($ok / count($columns) > 0.5) {
+				$query = "select t2.ID as ID from {$this->TABLE} t1 join {$this->TABLE} t2 on " . implode(" and ", $join) . " where t1.ID = '{$ID}' and t2.ID != '{$ID}'";
+				if ($result = DB::query($query)) {
+					while ($item = DB::fetch($result)) {
+						$nodes[$item->ID] = $name;
+					}
+				}
+			}
+		}
+
+		$query = "select ID2 from {$this->TABLE_REF} where ID1 = '{$ID}'";
+		if ($result = DB::query($query)) {
+			while ($item = DB::fetch($result)) {
+				$nodes[$item->ID2] = $row->REMOTE_TABLE;
+			}
+		}
+
+		if ($nodes) {
+			ksort($nodes);
+			foreach ($nodes as $node => $corel) $this->findNodes($node, $corel, $path);
+		}
+	}
+}
+
+
+
+$TABLE = "BI_audit_ALL";
+$TABLE_REF = "BI_audit_ALL_ref";
+$TABLE_DEST = ["BI_audit_ENERGA_RUM_KONTRAHENCI", "BI_audit_KW_requested_person"];
+
+$query = "select ID from {$TABLE} where REMOTE_TABLE = 'BI_audit_ENERGA_PRACOWNICY'";// and ID not in (5131123, 5131126, 5131129, 5131131, 5131132, 5131133, 5131134, 5131135)";
+$result = DB::query($query);
+$results = [];
+while ($row = DB::fetch($result)) {
+	$test = new FindCorelations($TABLE, $TABLE_REF, $TABLE_DEST, 7, $row->ID);
+	if ($test->results) {
+		$results = array_merge($results, $test->results);
+	}
+}
+
+$data = base64_encode(gzcompress(json_encode($results)));
+echo $data;

+ 93 - 0
SE/schema/WPS_Functions/BI_audit/4_find_corelations_local.php

@@ -0,0 +1,93 @@
+#!/usr/bin/env php
+<?php
+
+mysql_connect('192.168.61.253', 'root', 'kontem11on');
+mysql_select_db('SES_USERS2');
+mysql_query("set names utf8");
+
+class FindCorelations {
+
+	private $TABLE_REF, $DEPTH;
+	public $results = array();
+	private $nodesCache = array();
+
+	function __construct($TABLE_REF, $DEPTH) {
+		$this->TABLE_REF = $TABLE_REF;
+		$this->DEPTH = $DEPTH;
+	}
+
+	public function findNodes($ID, $path = array(), $relation = "", $end = 0) {
+		if (!$path) $this->firstNodes[] = $ID;
+		if (isset($path[$ID])) {
+			return false;
+		}
+
+		$path[$ID] = $relation;
+		if (!$relation) $relation = 0;
+
+		if ($end) {
+			$this->results[] = $path;
+			return true;
+		}
+
+		if (count($path) > $this->DEPTH) {
+			return false;
+		}
+
+		$nodes = array();
+		if (isset($this->nodesCache[$ID])) {
+			$nodes = $this->nodesCache[$ID];
+		} else {
+			$nodes = array();
+//			$query = "select ref.ID2, RELATION, END from {$this->TABLE_REF} ref join BI_audit_ALL_usable usable on ref.ID2 = usable.ID_BI_audit_ALL where ref.ID1 = '{$ID}' and ref.RELATION != '{$relation}'";
+//			$query = "select ref.ID2, RELATION, END from {$this->TABLE_REF} ref join BI_audit_ALL_usable usable on ref.ID2 = usable.ID_BI_audit_ALL where ref.ID1 = '{$ID}'";
+//			$query = "select ref.ID2, RELATION, END from {$this->TABLE_REF} ref where ref.ID1 = '{$ID}'";
+//			$where = "";
+//			if ($relation) $where = "and ({$relation} & ref.RELATION_ID) != {$relation}";
+//			else $where = "";
+			$query = "select ref.ID2, RELATION_ID, END from {$this->TABLE_REF} ref where ref.ID1 = '{$ID}'";// {$where}";
+//echo $query."\n";
+			//$query = "select ref.ID2, RELATION, RELATION_ID, END from {$this->TABLE_REF} ref where ref.ID1 = '{$ID}' and ref.RELATION != '{$relation}'";
+			if ($result = mysql_query($query)) {
+				while ($row = mysql_fetch_object($result)) {
+					//if (!in_array($row->ID2, $nodes)) 
+			//		$nodes[$row->ID2] = array('relation' => explode(", ", $row->RELATION), 'end' => $row->END);
+					$nodes[$row->ID2] = array('relation' => (int) $row->RELATION_ID, 'end' => $row->END);
+				}
+			}
+			if ($nodes) {
+				ksort($nodes);
+				$this->nodesCache[$ID] = $nodes;
+			}
+		}
+
+		if ($nodes) {
+			foreach ($nodes as $node => $data) {
+				//if (!$relation ||array_diff(explode(", ", $relation), explode(", ", $data['relation'])))
+//echo $relation . " : " . $data['relation'] . " : " . (($relation & $data['relation'])) . "\n";
+				if (!($relation && (($relation & $data['relation']) == $relation))) {
+//					echo $relation . " : " . $data['relation'] . " : " . (($relation & $data['relation'])) . "\n";
+					$this->findNodes($node, $path, $data['relation'], $data['end']);
+//				} else {
+//					echo $relation . " : " . $data['relation'] . " : " . ((int) $relation & (int) $data['relation']) . "\n";
+				}
+			}
+		}
+	}
+}
+
+
+
+$TABLE = "BI_audit_ALL";
+$TABLE_REF = "BI_audit_ALL_ref";
+
+$query = "select t.ID from {$TABLE} as t join BI_audit_ENERGA_PRACOWNICY pracownicy on t.REMOTE_ID = pracownicy.ID where REMOTE_TABLE = 'BI_audit_ENERGA_PRACOWNICY'";
+//$query = "select t.ID from {$TABLE} as t join BI_audit_ENERGA_PRACOWNICY pracownicy on t.REMOTE_ID = pracownicy.ID where pracownicy.source = 'ENERGA' and REMOTE_TABLE = 'BI_audit_ENERGA_PRACOWNICY'";
+
+$result = mysql_query($query);
+$test = new FindCorelations($TABLE_REF, 8);
+while ($row = mysql_fetch_object($result)) $test->findNodes($row->ID);
+$results = $test->results;
+
+$data = base64_encode(gzcompress(json_encode($results)));
+echo $data;

+ 97 - 0
SE/schema/WPS_Functions/BI_audit/4_find_corelations_local_global.php

@@ -0,0 +1,97 @@
+#!/usr/bin/env php
+<?php
+
+mysql_connect('192.168.61.253', 'root', 'kontem11on');
+mysql_select_db('SES_USERS2');
+mysql_query("set names utf8");
+
+class FindCorelations {
+
+	private $TABLE_REF, $DEPTH;
+	public $results = array();
+	private $nodesCache = array();
+	private $path = array();
+
+	function __construct($TABLE_REF, $DEPTH) {
+		$this->TABLE_REF = $TABLE_REF;
+		$this->DEPTH = $DEPTH;
+	}
+
+	public function findNodes($ID, $relation = "", $end = 0) {
+		if (!$this->path) $this->firstNodes[] = $ID;
+		if (isset($this->path[$ID])) {
+			return false;
+		}
+
+		$this->path[$ID] = $relation;
+		if (!$relation) $relation = 0;
+
+		if ($end) {
+			$this->results[] = $this->path;
+			array_pop($this->path);
+			return true;
+		}
+
+		if (count($this->path) > $this->DEPTH) {
+			array_pop($this->path);
+			return false;
+		}
+
+		$nodes = array();
+		if (isset($this->nodesCache[$ID])) {
+			$nodes = $this->nodesCache[$ID];
+		} else {
+			$nodes = array();
+//			$query = "select ref.ID2, RELATION, END from {$this->TABLE_REF} ref join BI_audit_ALL_usable usable on ref.ID2 = usable.ID_BI_audit_ALL where ref.ID1 = '{$ID}' and ref.RELATION != '{$relation}'";
+//			$query = "select ref.ID2, RELATION, END from {$this->TABLE_REF} ref join BI_audit_ALL_usable usable on ref.ID2 = usable.ID_BI_audit_ALL where ref.ID1 = '{$ID}'";
+//			$query = "select ref.ID2, RELATION, END from {$this->TABLE_REF} ref where ref.ID1 = '{$ID}'";
+//			$where = "";
+//			if ($relation) $where = "and ({$relation} & ref.RELATION_ID) != {$relation}";
+//			else $where = "";
+			$query = "select ref.ID2, RELATION_ID, END from {$this->TABLE_REF} ref where ref.ID1 = '{$ID}'";// {$where}";
+//echo $query."\n";
+			//$query = "select ref.ID2, RELATION, RELATION_ID, END from {$this->TABLE_REF} ref where ref.ID1 = '{$ID}' and ref.RELATION != '{$relation}'";
+			if ($result = mysql_query($query)) {
+				while ($row = mysql_fetch_object($result)) {
+					//if (!in_array($row->ID2, $nodes)) 
+			//		$nodes[$row->ID2] = array('relation' => explode(", ", $row->RELATION), 'end' => $row->END);
+					$nodes[$row->ID2] = array('relation' => (int) $row->RELATION_ID, 'end' => $row->END);
+				}
+			}
+			if ($nodes) {
+				ksort($nodes);
+				$this->nodesCache[$ID] = $nodes;
+			}
+		}
+
+		if ($nodes) {
+			foreach ($nodes as $node => $data) {
+				//if (!$relation ||array_diff(explode(", ", $relation), explode(", ", $data['relation'])))
+//echo $relation . " : " . $data['relation'] . " : " . (($relation & $data['relation'])) . "\n";
+				if (!($relation && (($relation & $data['relation']) == $relation))) {
+//					echo $relation . " : " . $data['relation'] . " : " . (($relation & $data['relation'])) . "\n";
+					$this->findNodes($node, $data['relation'], $data['end']);
+//				} else {
+//					echo $relation . " : " . $data['relation'] . " : " . ((int) $relation & (int) $data['relation']) . "\n";
+				}
+			}
+		}
+		array_pop($this->path);
+	}
+}
+
+
+
+$TABLE = "BI_audit_ALL";
+$TABLE_REF = "BI_audit_ALL_ref";
+
+$query = "select t.ID from {$TABLE} as t join BI_audit_ENERGA_PRACOWNICY pracownicy on t.REMOTE_ID = pracownicy.ID where REMOTE_TABLE = 'BI_audit_ENERGA_PRACOWNICY'";
+//$query = "select t.ID from {$TABLE} as t join BI_audit_ENERGA_PRACOWNICY pracownicy on t.REMOTE_ID = pracownicy.ID where pracownicy.source = 'ENERGA' and REMOTE_TABLE = 'BI_audit_ENERGA_PRACOWNICY'";
+
+$result = mysql_query($query);
+$test = new FindCorelations($TABLE_REF, 8);
+while ($row = mysql_fetch_object($result)) $test->findNodes($row->ID);
+$results = $test->results;
+
+$data = base64_encode(gzcompress(json_encode($results)));
+echo $data;

+ 155 - 0
SE/schema/WPS_Functions/BI_audit/4_find_corelations_local_memory.php

@@ -0,0 +1,155 @@
+#!/usr/bin/env php
+<?php
+
+mysql_connect('192.168.61.253', 'root', 'kontem11on');
+mysql_select_db('SES_USERS2');
+mysql_query("set names utf8");
+
+class FindCorelations {
+
+	private $TABLE_REF, $DEPTH;
+	public $results = array();
+	private $nodesCache = array();
+	public $cache = array();
+	public $firstNodes = array();
+	private $ends = array();
+	private $relations = array();
+
+	function __construct($TABLE_REF, $DEPTH) {
+		$this->TABLE_REF = $TABLE_REF;
+		$this->DEPTH = $DEPTH;
+	}
+
+	private function cachePath($path) {
+		$last = 0;
+		foreach ($path as $node => $rel) {
+			if ($last) $this->cache[$last][$node] = $rel;
+			$last = $node;
+		}
+	}
+
+	public function loadCache() {
+		$query = "select ID1, ID2, RELATION, END from {$this->TABLE_REF}";
+		$result = mysql_query($query);
+		while ($row = mysql_fetch_object($result)) {
+			$relations = explode(", ", $row->RELATION);
+			$rel = 0;
+			foreach ($relations as $relation) {
+				if (!isset($this->relations[$relation])) {
+					if (!$this->relations) $this->relations[$relation] = 1;
+					else $this->relations[$relation] = 2 * max($this->relations);
+				}
+				$rel += $this->relations[$relation];
+			}
+			$this->nodesCache[$row->ID1][$row->ID2] = $rel;
+			if ($row->END) $this->ends[$row->ID2] = true;
+		}
+	}
+
+	public function findNodes($ID, $path = array(), $relation = "") {
+		if (!$path) $this->firstNodes[] = $ID;
+		if (isset($path[$ID])) {
+			return false;
+		}
+
+		$path[$ID] = $relation;
+
+		if (isset($this->ends[$ID])) {
+//			$this->cachePath($path);
+			$this->results[] = $path;
+			return true;
+		}
+
+//		if (isset($this->cache[$ID])) {
+//			$this->cachePath($path);
+//			return true;
+//		}
+
+		if (count($path) > $this->DEPTH) {
+			return false;
+		}
+
+		$nodes = array();
+		if (isset($this->nodesCache[$ID])) {
+			$nodes = $this->nodesCache[$ID];
+//		} else {
+//			$nodes = array();
+//			$query = "select ref.ID2, RELATION, END from {$this->TABLE_REF} ref join BI_audit_ALL_usable usable on ref.ID2 = usable.ID_BI_audit_ALL where ref.ID1 = '{$ID}' and ref.RELATION != '{$relation}'";
+//			$query = "select ref.ID2, RELATION, END from {$this->TABLE_REF} ref join BI_audit_ALL_usable usable on ref.ID2 = usable.ID_BI_audit_ALL where ref.ID1 = '{$ID}'";
+//			$query = "select ref.ID2, RELATION, END from {$this->TABLE_REF} ref where ref.ID1 = '{$ID}'";
+//			$query = "select ref.ID2, RELATION, END from {$this->TABLE_REF} ref where ref.ID1 = '{$ID}' and ref.RELATION != '{$relation}'";
+//			if ($result = mysql_query($query)) {
+//				while ($row = mysql_fetch_object($result)) {
+					//if (!in_array($row->ID2, $nodes)) 
+			//		$nodes[$row->ID2] = array('relation' => explode(", ", $row->RELATION), 'end' => $row->END);
+//					$nodes[$row->ID2] = $row->RELATION;
+//					if ($row->END && (!in_array($row->ID2, $this->ends))) $this->ends[] = $row->ID2;
+//				}
+//			}
+//			if ($nodes) {
+//				ksort($nodes);
+//				$this->nodesCache[$ID] = $nodes;
+//			}
+//		}
+
+//		if ($nodes) {
+			foreach ($nodes as $node => $rel) {
+				if (!($relation && ($relation & $rel)))
+					$this->findNodes($node, $path, $rel);
+			}
+		}
+	}
+}
+
+
+
+$TABLE = "BI_audit_ALL";
+$TABLE_REF = "BI_audit_ALL_ref";
+
+$query = "select t.ID from {$TABLE} as t join BI_audit_ENERGA_PRACOWNICY pracownicy on t.REMOTE_ID = pracownicy.ID where REMOTE_TABLE = 'BI_audit_ENERGA_PRACOWNICY'";
+//$query = "select t.ID from {$TABLE} as t join BI_audit_ENERGA_PRACOWNICY pracownicy on t.REMOTE_ID = pracownicy.ID where pracownicy.source = 'ENERGA' and REMOTE_TABLE = 'BI_audit_ENERGA_PRACOWNICY'";
+
+$result = mysql_query($query);
+$test = new FindCorelations($TABLE_REF, 13);
+echo "Loading cache...";
+$test->loadCache();
+echo " done.\n";
+//$test->generateReverseCache();
+//print_r($test->cache);
+//die();
+while ($row = mysql_fetch_object($result)) $test->findNodes($row->ID);
+//$result = mysql_query($query);
+//while ($row = mysql_fetch_object($result)) $test->findNodes($row->ID);
+
+//print_r($test->cache);
+//die();
+
+$results = $test->results;
+
+print_r($results);
+/*
+echo "Przykladowe powiazanie:\n";
+$result = $results[2];
+print_r($result);
+foreach ($result as $node => $rel) {
+	if (isset($test->cache[$node])) {
+		echo "\nCache dla {$node}:\n";
+		print_r($test->cache[$node]);
+	}
+}
+die();
+*/
+
+/*
+$max = 0;
+foreach ($results as $result) if (($c = count($result)) > $max) {
+	$max = $c;
+	$test = $result;
+}
+$results = array($test);
+*/
+
+//print_r($test->cache);
+//die();
+$data = base64_encode(gzcompress(json_encode($results)));
+//echo $data;

+ 89 - 0
SE/schema/WPS_Functions/BI_audit/5_analyse.php

@@ -0,0 +1,89 @@
+#!/usr/bin/env php
+<?php
+$_SERVER['SERVER_NAME'] = "biuro.biall-net.pl";
+require("/Library/Server/Web/Data/Sites/Default/dev-bzyk/se-lib/bootstrap.php");
+#require("/Library/Server/Web/Data/Sites/SE-producition-git/SE/se-lib/bootstrap.php");
+date_default_timezone_set('Europe/Warsaw');// PHP 5 >= 5.1.0 required by date functions
+Lib::loadClass('V');
+
+if (!isset($argv[1])) die("Missing parameter\n");
+if ($argv[1] == "-") $data = stream_get_contents(STDIN);
+else {
+	if (!is_file($argv[1])) die("File {$argv[1]} not found\n");
+	$data = @file_get_contents($argv[1]);
+	if (!$data) die("Error loading file\n");
+}
+//preg_match('/.*\.([[:alpha:]]*)$/', $argv[1], $match);
+//switch ($match[1]) {
+//	case "data":
+		$results = @json_decode(gzuncompress(base64_decode($data)), true);
+//		break;
+//	case "txt":
+//		$results = unserialize($data);
+//		break;
+//	default:
+//		die("Unknown file type\n");
+//}
+
+
+if (!$results) die("Error loading data\n");
+
+//echo "Liczba wyników: " . count($results) . "\n";
+foreach ($results as $result) echo implode("->", array_keys($result)) . "\n";
+//print_r($results);
+die();
+
+
+$TABLE = "BI_audit_ALL";
+
+//$query = "select `DATA` from BI_audit_ALL_results where ID=4";
+//$data = DB::getPDO()->fetchvalue($query);
+//$results = json_decode(gzuncompress(base64_decode($data)), true);
+
+$xmlRoot = "RelatedFeatureRoot";
+$xmlRoot2 = "BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA";
+$xmlElements = "BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row";
+
+$items_results = [];
+foreach ($results as $result) {
+	$item_results = [];
+	foreach ($result as $ID => $rel) {
+		$query = "select REMOTE_TABLE, REMOTE_ID from {$TABLE} where ID = '{$ID}'";
+		$row = DB::getPDO()->fetchFirst($query);
+		$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 ($rel) $item_result['relation_from'] = $rel;
+		$item_results['object'][] = $item_result;
+		if ($row['REMOTE_TABLE'] == "BI_audit_ENERGA_RUM_KONTRAHENCI") {
+			$query = "select umowy.* from BI_audit_ENERGA_RUM_UMOWY umowy join `CRM__#REF_TABLE__23` ref on umowy.ID = ref.PRIMARY_KEY where ref.REMOTE_PRIMARY_KEY = '{$row['REMOTE_ID']}'";
+			$res = DB::query($query);
+			$umowy = [];
+			$i = 0;
+			while ($obj = DB::fetch($res)) {
+				if (++$i > 50) {
+					//$umowy[] = "Przekroczono limit 50 rekordów (pominięto " . (mysql_num_rows($res) - 50) . " z " . mysql_num_rows($res) . ")";
+					break;
+				}
+				$umowy[] = (array) $obj;
+			}
+			if ($umowy) {
+				$item_results['deal'] = $umowy;
+			}
+		} elseif ($row['REMOTE_TABLE'] == "BI_audit_KW_requested_person") {
+			$query = "select kw.* from BI_audit_KW_requested kw join `CRM__#REF_TABLE__24` ref on kw.ID = ref.PRIMARY_KEY where ref.REMOTE_PRIMARY_KEY = '{$row['REMOTE_ID']}'";
+			$res = DB::query($query);
+			$kw = [];
+			while ($obj = DB::fetch($res)) {
+				$kw[] = (array) $obj;
+			}
+			if ($kw) {
+				$item_results['kw'] = $kw;
+			}
+		}
+	}
+	$items_results[$xmlRoot2][$xmlElements][] = $item_results;
+}
+//print_r($items_results);
+$test = V::arrayToXML($items_results, true, $xmlRoot);
+echo $test;

+ 95 - 0
SE/schema/WPS_Functions/BI_audit/5_generate_xml.php

@@ -0,0 +1,95 @@
+#!/usr/bin/env php
+<?php
+$_SERVER['SERVER_NAME'] = "biuro.biall-net.pl";
+require("/Library/Server/Web/Data/Sites/Default/dev-bzyk/se-lib/bootstrap.php");
+#require("/Library/Server/Web/Data/Sites/SE-producition-git/SE/se-lib/bootstrap.php");
+date_default_timezone_set('Europe/Warsaw');// PHP 5 >= 5.1.0 required by date functions
+Lib::loadClass('V');
+
+if (!isset($argv[1])) die("Missing parameter\n");
+if ($argv[1] == "-") $data = stream_get_contents(STDIN);
+else {
+	if (!is_file($argv[1])) die("File {$argv[1]} not found\n");
+	$data = @file_get_contents($argv[1]);
+	if (!$data) die("Error loading file\n");
+}
+//preg_match('/.*\.([[:alpha:]]*)$/', $argv[1], $match);
+//switch ($match[1]) {
+//	case "data":
+		$results = @json_decode(gzuncompress(base64_decode($data)), true);
+//		break;
+//	case "txt":
+//		$results = unserialize($data);
+//		break;
+//	default:
+//		die("Unknown file type\n");
+//}
+
+
+if (!$results) die("Error loading data\n");
+$TABLE = "BI_audit_ALL";
+
+$query = "select * from BI_audit_ALL_ref_RELATIONS";
+$result = DB::getPDO()->fetchAll($query);
+foreach ($result as $row) $relations[$row['ID']] = $row['RELATION'];
+
+
+//$query = "select `DATA` from BI_audit_ALL_results where ID=4";
+//$data = DB::getPDO()->fetchvalue($query);
+//$results = json_decode(gzuncompress(base64_decode($data)), true);
+
+$xmlRoot = "RelatedFeatureRoot";
+$xmlRoot2 = "BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA";
+$xmlElements = "BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row";
+
+$items_results = [];
+$items_kontrahenci = [];
+$items_kw_person = [];
+$kontrahenci = [];
+$kw_person = [];
+foreach ($results as $result) {
+	$item_results = [];
+	foreach ($result as $ID => $rel) {
+		$query = "select REMOTE_TABLE, REMOTE_ID from {$TABLE} where ID = '{$ID}'";
+		$row = DB::getPDO()->fetchFirst($query);
+		$query = "select * from `{$row['REMOTE_TABLE']}` where ID = '{$row['REMOTE_ID']}'";
+		$obj = DB::fetch(DB::query($query));
+		//if ($row['REMOTE_TABLE'] == 'BI_audit_ENERGA_PRACOWNICY') unset($obj->source);
+		$item_result = [$row['REMOTE_TABLE'] => (array) $obj];
+		if (is_array($rel)) $rel = implode(", ", $rel);
+		elseif (is_numeric($rel)) $rel = $relations[$rel];
+		if ($rel) $item_result['relation_from'] = $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 `CRM__#REF_TABLE__23` ref on umowy.ID = ref.PRIMARY_KEY where ref.REMOTE_PRIMARY_KEY = '{$row['REMOTE_ID']}'";
+			$res = DB::query($query);
+			if (mysql_num_rows($res)) {
+				$umowy = [];
+				while ($obj = DB::fetch($res)) $umowy[] = (array) $obj;
+				$item_kontrahenci['ID'] = $row['REMOTE_ID'];
+				$item_kontrahenci['BI_audit_ENERGA_RUM_UMOWY'] = $umowy;
+				$items_kontrahenci[] = $item_kontrahenci;
+			}
+		} elseif ($row['REMOTE_TABLE'] == "BI_audit_KW_requested_person" && (!in_array($row['REMOTE_ID'], $kw_person))) {
+			$kw_person[] = $row['REMOTE_ID'];
+			$query = "select kw.* from BI_audit_KW_requested kw join `CRM__#REF_TABLE__24` ref on kw.ID = ref.PRIMARY_KEY where ref.REMOTE_PRIMARY_KEY = '{$row['REMOTE_ID']}'";
+			$res = DB::query($query);
+			if (mysql_num_rows($res)) {
+				$kw = [];
+				while ($obj = DB::fetch($res)) $kw[] = (array) $obj;
+				$item_kw_person['ID'] = $row['REMOTE_ID'];
+				$item_kw_person['BI_audit_KW_requested'] = $kw;
+				$items_kw_person[] = $item_kw_person;
+			}
+		}
+	}
+	$items_results[$xmlRoot2][$xmlElements][] = $item_results;
+}
+$items_results[$xmlRoot2]['BI_audit_ENERGA_RUM_KONTRAHENCI'] = $items_kontrahenci;
+$items_results[$xmlRoot2]['BI_audit_KW_requested_person'] = $items_kw_person;
+
+//print_r($items_results[$xmlRoot2]['BI_audit_KW_requested_person']);
+//die();
+$test = V::arrayToXML($items_results, true, $xmlRoot);
+echo $test;

+ 94 - 0
SE/schema/WPS_Functions/BI_audit/build_BI_audit.xml

@@ -0,0 +1,94 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project basedir="../../." name="BI_audit" default="BI_audit">
+    <description>to execute Business inteligence corelations analysis TODO</description>
+    
+    <!-- modules WPS_Functions - to be copied to any module needed -->
+    <property name="WPS_Functions" location="${basedir}/WPS_Functions"/>
+    <property name="build_WPS_Functions.xml" location="${WPS_Functions}/build_WPS_Functions.xml"/> <!-- call it to get properties -->
+    <property name="build_WPS_Functions.xml.properties" location="${build_WPS_Functions.xml}.properties"/>
+    <!-- eof modules WPS_Functions  -->
+    
+    
+    <!-- local shared module individual properties should contain ${project.name} basedir ant its .temp -->
+    <property name="BI_audit" location="${basedir}/WPS_Functions/graph_gnuplot/build_graph_gnuplot.xml"/>
+    <property name="BI_audit_basedir" location="${basedir}/WPS_Functions/BI_audit"  />                         
+    <property name="BI_audit_basedir.temp" location="${BI_audit_basedir}/temp"  />
+    <!-- eof shared local module individual properties -->
+    
+    <!-- local individual module properties -->
+    <property name="BI_audit:config_inc.php" value="${BI_audit_basedir}/config_inc.php"/>
+    
+    <property name="BI_audit:1_import_kw_person_and_optimize_db.php" value="${BI_audit_basedir}/1_import_kw_person_and_optimize_db.php"/>
+    <property name="BI_audit:1_rum_create_ref.php" value="${BI_audit_basedir}/1_rum_create_ref.php"/>
+    <property name="BI_audit:2_verify_kontrahenci.php" value="${BI_audit_basedir}/2_verify_kontrahenci.php"/>
+    <property name="BI_audit:3_create_all.php" value="${BI_audit_basedir}/3_create_all.php"/>
+    <property name="BI_audit:4_find_corelations.php" value="${BI_audit_basedir}/4_find_corelations.php"/>
+    <property name="BI_audit:4_find_corelations_local.php" value="${BI_audit_basedir}/4_find_corelations_local.php"/>
+    
+    <property name="BI_audit:4_find_corelations_local_global.php" value="${BI_audit_basedir}/4_find_corelations_local_global.php"/>
+    <property name="BI_audit:4_find_corelations_local_memory.php" value="${BI_audit_basedir}/4_find_corelations_local_memory.php"/>
+    <property name="BI_audit:5_analyse.php" value="${BI_audit_basedir}/5_analyse.php"/>
+    <property name="BI_audit:4_find_corelations_local.php" value="${BI_audit_basedir}/4_find_corelations_local.php"/>
+    <property name="BI_audit:5_generate_xml.php" value="${BI_audit_basedir}/5_generate_xml.php"/>
+    
+    <property name="BI_audit:cache_relations.php" value="${BI_audit_basedir}/cache_relations.php"/>
+    <property name="BI_audit:find_corelations.php" value="${BI_audit_basedir}/find_corelations.php"/>
+    
+    
+    
+    <!--<property name="PATH_ADD" value="/opt/local/bin:/usr/bin:/bin"/>-->
+    
+    
+    <target name="BI_audit:create_tempdir">
+        <mkdir dir="${BI_audit_basedir.temp}"/>
+        <!--<mkdir dir="${BI_audit_basedir.temp}/BI_audit/${uuid}"/>-->
+    </target>
+    
+    <target name="read_build_properties" depends="BI_audit:create_tempdir">
+        <ant antfile="${build_WPS_Functions.xml}" target="save_build_properties"/>
+        <loadproperties srcfile="${build_WPS_Functions.xml.properties}"/>
+    </target>
+    
+    
+    <target name="BI_audit:create_php_localized_config" depends="read_build_properties" description="To create localized include config for php scripts with bootstrap.php etc">
+        <echo file="${BI_audit:config_inc.php}">
+#!/usr/bin/env php
+<![CDATA[<?php]]>
+$_SERVER['SERVER_NAME'] = "${SERVER_NAME}";
+require("${bootstrap.php}");
+<![CDATA[?>]]>
+        </echo>
+        
+    </target>
+    
+    
+    <target name="BI_audit" depends="BI_audit:create_tempdir" description="To generate raport If called from CRM_PROCES_tree (to be renamed to XML_tree_to_dita ) framework ">
+        <property name="srcfile" value="${srcfile}"/>
+        <property name="dstfile" value="${dstfile}"/>
+        <property name="fileindex" value="${fileindex}"/> <!-- to uniq name -->
+        <property name="ditadir" value="${ditadir}"/>
+        <property name="uuid" value="${uuid}"/>
+        <!--<property name="images.xml" value="${BI_audit_basedir.temp}/BI_audit/${uuid}/${fileindex}_images.xml"/>--> 
+        
+        
+      <!--  <property name="barchart.gnuplot" value="${graph_gnuplot_basedir.temp}/graph_to_image/${uuid}/barchart.${fileindex}.gnuplot"/>
+        <property name="population.dat" value="${graph_gnuplot_basedir.temp}/graph_to_image/${uuid}/population.${fileindex}.dat"/>
+        <property name="barchart.png" value="${graph_gnuplot_basedir.temp}/graph_to_image/${uuid}/barchart.${fileindex}.png"/>-->
+       
+        <echo>
+            BI_audit  
+        </echo>
+        
+        
+        
+        <!--<exec executable="${gnuplot}" dir="${graph_gnuplot_basedir.temp}/graph_to_image/${uuid}">-->
+        <exec executable="php" dir="${BI_audit_basedir.temp}">
+            <arg line="${BI_audit:1_import_kw_person_and_optimize_db.php}"/>
+            
+        </exec>
+        
+        
+    </target>    
+    
+    
+</project>

+ 130 - 0
SE/schema/WPS_Functions/BI_audit/cache_relations.php

@@ -0,0 +1,130 @@
+#!/usr/bin/env php
+<?php
+
+mysql_connect('localhost', 'root', 'kontem11on');
+mysql_select_db('SES_USERS2');
+mysql_query("set names utf8");
+
+class FindCorelations {
+
+	private $TABLE, $TABLE_REF, $TABLE_DEST, $DEPTH;
+	public $results = array();
+	private $nodesCache = array();
+
+	private $corelations = array(
+		'nip' => array('nip'),
+		'regon' => array('regon'),
+		'krs' => array('krs'),
+		'pesel' => array('pesel'),
+		'adres' => array('TERYT_SYM', 'TERYT_SYM_UL', 'nrDomu', 'nrLokalu'),
+	);
+
+	function __construct($TABLE, $TABLE_REF, $TABLE_DEST, $DEPTH) {
+		$this->TABLE = $TABLE;
+		$this->TABLE_REF = $TABLE_REF;
+		$this->TABLE_DEST = $TABLE_DEST;
+		$this->DEPTH = $DEPTH;
+	}
+
+	public function findNodes($ID, $corelation = "", $path = array()) {
+		if (isset($path[$ID])) {
+//			echo $ID . "\n";
+			return false;
+		}
+		$path[$ID] = $corelation;
+		$query = "select * from {$this->TABLE} where ID = '{$ID}'";
+		if (!($result = mysql_query($query))) return false;
+		$row = mysql_fetch_object($result);
+		if (in_array($row->REMOTE_TABLE, $this->TABLE_DEST)) {
+			$this->results[] = $path;
+			return true;
+		}
+
+		if (count($path) > $this->DEPTH) {
+			return false;
+		}
+
+		$dupaNodes = array();
+		if (isset($this->nodesCache[$ID])) {
+			$nodes = $this->nodesCache[$ID];
+		} else {
+			$nodes = array();
+			$joins = array();
+			$items = "";
+			foreach ($this->corelations as $name => $columns) {
+				$ok = 0;
+				$join = array();
+				foreach ($columns as $column) {
+					$join[] = "t1.{$column} = t2.{$column}";
+					if ($row->$column) {
+						$ok++;
+					}
+				}
+				if ($ok / count($columns) > 0.5) {
+					$joins[] = "(" . implode(" and ", $join) . ")";
+					$items .= ", if(" . implode(" and ", $join) . ", 1, 0) as {$name}";
+				}
+			}
+			if (!$joins) return false;
+			$query = "select t2.ID as ID {$items} from {$this->TABLE} t1 join {$this->TABLE} t2 on " . implode(" or ", $joins) . " join BI_audit_ALL_usable usable on t2.ID = usable.ID_BI_audit_ALL" . 
+//				" where t1.ID = '{$ID}' and t2.ID not in (" . implode(",", array_keys($path)) . ")";
+				" where t1.ID = '{$ID}' and t2.ID not in ({$ID})";
+//			echo $query."\n";
+			if ($result = mysql_query($query)) {
+				while ($item = mysql_fetch_object($result)) {
+					$names = array();
+					foreach ($this->corelations as $name => $v) {
+						if (isset($item->$name) && $item->$name) $names[] = $name;
+					}
+//					if (isset($path[$item->ID])) echo $item->ID . " :: " . implode("->", array_keys($path)) . "\n";
+//if (!isset($path[$item->ID])) {
+//	$dupaNodes[$item->ID] = $name;
+//}
+					$nodes[$item->ID] = implode(', ', $names);
+				}
+			}
+
+//			$query = "select ref.ID2 from {$this->TABLE_REF} ref join BI_audit_ALL_usable usable on ref.ID2 = usable.ID_BI_audit_ALL where ref.ID1 = '{$ID}' and ref.ID2 not in (" . implode(",", array_keys($path)) . ")";
+			$query = "select ref.ID2 from {$this->TABLE_REF} ref join BI_audit_ALL_usable usable on ref.ID2 = usable.ID_BI_audit_ALL where ref.ID1 = '{$ID}'";
+			if ($result = mysql_query($query)) {
+				while ($item = mysql_fetch_object($result)) {
+//					if (!isset($path[$item->ID2])) 
+//if (!array_key_exists($item->ID2, $path)) $dupaNodes[$item->ID2] = $row->REMOTE_TABLE;
+$nodes[$item->ID2] = $row->REMOTE_TABLE;
+				}
+			}
+			if ($nodes) {
+				ksort($nodes);
+				$this->nodesCache[$ID] = $nodes;
+			}
+		}
+
+		if ($nodes) {
+			foreach ($nodes as $node => $corel) {
+//				if (!isset($path[$node]))
+$this->findNodes($node, $corel, $path);
+			}
+		}
+//		ksort($dupaNodes);
+//		foreach ($dupaNodes as $node => $corel) echo "{$node}\n";
+	}
+}
+
+
+
+$TABLE = "BI_audit_ALL";
+$TABLE_REF = "BI_audit_ALL_ref";
+$TABLE_DEST = array("BI_audit_ENERGA_RUM_KONTRAHENCI", "BI_audit_KW_requested_person");
+
+$query = "select ID from {$TABLE} where REMOTE_TABLE = 'BI_audit_ENERGA_PRACOWNICY'";
+//$query = "select ref.ID2 as ID from {$TABLE} `all` join BI_audit_ALL_ref ref on `all`.ID = ref.ID1 where all.krs='0000457722';";
+$result = mysql_query($query);
+$results = array();
+$test = new FindCorelations($TABLE, $TABLE_REF, $TABLE_DEST, 7);
+while ($row = mysql_fetch_object($result)) $test->findNodes($row->ID);
+$results = $test->results;
+//print_r($test->badNodes);
+
+$data = base64_encode(gzcompress(json_encode($results)));
+echo $data;
+

+ 126 - 0
SE/schema/WPS_Functions/BI_audit/find_corelations.php

@@ -0,0 +1,126 @@
+#!/usr/bin/env php
+<?php
+$_SERVER['SERVER_NAME'] = "biuro.biall-net.pl";
+require("/Library/Server/Web/Data/Sites/Default/dev-bzyk/se-lib/bootstrap.php");
+#require("/Library/Server/Web/Data/Sites/SE-producition-git/SE/se-lib/bootstrap.php");
+date_default_timezone_set('Europe/Warsaw');// PHP 5 >= 5.1.0 required by date functions
+
+class FindCorelations {
+
+	private $TABLE, $TABLE_REF, $TABLE_DEST, $DEPTH;
+	public $results = [];
+
+	private $corelations = [
+		'nip' => ['nip'],
+		'regon' => ['regon'],
+		'krs' => ['krs'],
+		'pesel' => ['pesel'],
+		'adres' => ['TERYT_SYM', 'TERYT_SYM_UL', 'nrDomu', 'nrLokalu'],
+	];
+
+	function __construct($TABLE, $TABLE_REF, $TABLE_DEST, $DEPTH, $ID) {
+		$this->TABLE = $TABLE;
+		$this->TABLE_REF = $TABLE_REF;
+		$this->TABLE_DEST = $TABLE_DEST;
+		$this->DEPTH = $DEPTH;
+		$this->findNodes($ID);
+	}
+
+	function findNodes($ID, $corelation = "", $path = []) {
+//		echo "START: {$ID}\n";
+		if (isset($path[$ID])) return null;
+		$path[$ID] = $corelation;
+//		echo implode("->", array_keys($path)) . "\n";
+//		echo count($path) . ":";
+		$query = "select * from {$this->TABLE} where ID = '{$ID}'";
+		if (!($result = DB::query($query))) return null;
+		$row = DB::fetch($result);
+		if (in_array($row->REMOTE_TABLE, $this->TABLE_DEST)) {
+			if ($row->REMOTE_TABLE == "BI_audit_KW_requested") implode("->", array_keys($path)) . "\n";
+			$this->results[] = $path;
+			return null;
+		}
+
+		if (count($path) > $this->DEPTH) return null;
+		$nodes = [];
+		foreach ($this->corelations as $name => $columns) {
+			$ok = 0;
+			$join = [];
+			foreach ($columns as $column) {
+				$join[] = "t1.{$column} = t2.{$column}";
+				if ($row->$column) {
+					$ok++;
+				}
+			}
+			if ($ok / count($columns) > 0.5) {
+				$query = "select t2.ID as ID from {$this->TABLE} t1 join {$this->TABLE} t2 on " . implode(" and ", $join) . " where t1.ID = '{$ID}' and t2.ID != '{$ID}'";
+//				echo $name . ":" . $query . "\n";
+				if ($result = DB::query($query)) {
+					while ($item = DB::fetch($result)) {
+						$nodes[$item->ID] = $name;
+					}
+				}
+			}
+		}
+
+		$query = "select ID2 from {$this->TABLE_REF} where ID1 = '{$ID}'";
+//		echo "REF:" . $query . "\n";
+		if ($result = DB::query($query)) {
+			while ($item = DB::fetch($result)) {
+				$nodes[$item->ID2] = $row->REMOTE_TABLE;
+			}
+		}
+
+		if ($nodes) {
+			ksort($nodes);
+			foreach ($nodes as $node => $corel) $this->findNodes($node, $corel, $path);
+		}
+//		print_r($nodes);
+	}
+}
+
+$TABLE = "BI_audit_ALL";
+$TABLE_REF = "BI_audit_ALL_ref";
+$TABLE_DEST = ["BI_audit_ENERGA_RUM_KONTRAHENCI", "BI_audit_KW_requested"];
+
+$query = "select ID from {$TABLE} where REMOTE_TABLE = 'BI_audit_ENERGA_PRACOWNICY'";// and ID not in (5131123, 5131126, 5131129, 5131131, 5131132, 5131133, 5131134, 5131135)";
+$result = DB::query($query);
+$results = [];
+while ($row = DB::fetch($result)) {
+//	echo $row->ID . "\n";
+	$test = new FindCorelations($TABLE, $TABLE_REF, $TABLE_DEST, 5, $row->ID);
+	if ($test->results) {
+		$results = array_merge($results, $test->results);
+//		echo "\n==========\nGOT IT!\n";
+//		print_r($test->results);
+//		echo "==========\n";
+	}
+}
+//echo "\n\n============ RESULTS =======================\n";
+
+$items_results = [];
+foreach ($results as $result) {
+	$item_results = [];
+	foreach ($result as $ID => $rel) {
+		$query = "select REMOTE_TABLE, REMOTE_ID from {$TABLE} where ID = '{$ID}'";
+		$row = DB::getPDO()->fetchFirst($query);
+		$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 ($rel) $item_result['relation_from'] = $rel;
+		$item_results[] = $item_result;
+		if ($row['REMOTE_TABLE'] == "BI_audit_ENERGA_RUM_KONTRAHENCI") {
+			$query = "select umowy.* from BI_audit_ENERGA_RUM_UMOWY umowy join `CRM__#REF_TABLE__23` ref on umowy.ID = ref.PRIMARY_KEY where ref.REMOTE_PRIMARY_KEY = '{$row['REMOTE_ID']}'";
+			$res = DB::query($query);
+			$umowy = [];
+			while ($obj = DB::fetch($res)) {
+				$umowy[] = (array) $obj;
+			}
+			if ($umowy) {
+				$item_results['umowy'] = $umowy;
+			}
+		}
+	}
+	$items_results[] = $item_results;
+}
+print_r($items_results);

+ 14 - 0
SE/schema/WPS_Functions/build_WPS_Functions.xml

@@ -2,6 +2,10 @@
 <project basedir="../." name="build_WPS_Functions">
   
   
+  
+  <!-- TODO config/licence/to handle params  - in procesy5 there is SE/schema/WPS_Functions folder - so we need to tell it is in one more root folder ../-->
+    <property name="bootstrap.php" location="${basedir}/../se-lib/bootstrap.php"/>
+  
     <!-- parent properties -->
     <property name="build_root.xml" location="${basedir}/build.xml"/>
     <property name="build_properties" location="${basedir}/build_properties" />
@@ -19,11 +23,16 @@
     <property name="object_modeling_new_basedir" location="${basedir}/WPS_Functions/object_modeling_new"  />
     <property name="object_modeling_by_default_db_xml_schema_basedir" location="${basedir}/WPS_Functions/object_modeling_by_default_db_xml_schema"  />
     
+    <property name="PATH_ADD" value="/opt/local/bin:/usr/bin:/bin"/> <!-- to use in mapserver_gml_to_png ant kw -->
+    
     <property name="default_db:recurse_ant_basedir" location="${basedir}/WPS_Functions/default_db/recurse_ant"/>
     <!--<property name="default_db:recurse_ant_basedir.temp" location="${default_db:recurse_ant_basedir}/temp/${uuid}"/>-->
     <property name="default_db:recurse_ant_basedir.out" location="${default_db:recurse_ant_basedir}/out"/>
+    <property name="default_db:recurse_ant_basedir.recurse_cache" location="${default_db:recurse_ant_basedir}/recurse_cache"/><!-- @2017-06-22 cache plan -->
     <property name="default_db:recurse_ant" location="${default_db:recurse_ant_basedir}/build_recurse_ant.xml"/>
     
+    <property name="build_recurse_ant_cache_control.xsl" value="${basedir}/default_db_xml_cache.public/build_recurse_ant_cache_control.xsl"/>
+    
     <property name="get_wfs_basedir" location="${basedir}/WPS_Functions/get_wfs"/><!-- to ordinary retrieve wfs data  -->
     <property name="get_wfs_basedir.temp" location="${get_wfs_basedir}/temp"/>
     <property name="get_wfs" location="${get_wfs_basedir}/build_get_wfs.xml"/><!-- to ordinary retrieve wfs data  -->
@@ -40,6 +49,11 @@
     <property name="mapserver_gml_to_png_basedir" location="${basedir}/WPS_Functions/mapserver_gml_to_png"  />                         
     <property name="mapserver_gml_to_png_basedir.temp" location="${mapserver_gml_to_png_basedir}/temp"  />
     
+    <property name="graph_gnuplot" location="${basedir}/WPS_Functions/graph_gnuplot/build_graph_gnuplot.xml"/>
+    <property name="graph_gnuplot_basedir" location="${basedir}/WPS_Functions/graph_gnuplot"  />                         
+    <property name="graph_gnuplot_basedir.temp" location="${graph_gnuplot_basedir}/temp"  />
+        
+    <property name="BI_audit" location="${WPS_Functions}/build_BI_audit.xml"/>
         
     <property name="postprocess.xsl" location="${basedir}/default_db_xml_cache.public/system_cache__appinfo/postprocess.xsl"/>
     

+ 9 - 0
SE/schema/WPS_Functions/default_db/CRM_PROCES_tree/build_CRM_PROCES_tree.xml

@@ -237,6 +237,14 @@
                         <property name="ditadir" value="@ditadir"/>
                     </ant>
                 </gml_to_image>
+                <graph_to_image>
+                    <ant antfile="${graph_gnuplot}" > <!-- inheritall="false" -->
+                        <property name="srcfile" value="@srcfile"/>
+                        <property name="dstfile" value="@dstfile"/>
+                        <property name="fileindex" value="@fileindex"/>
+                        <property name="ditadir" value="@ditadir"/>
+                    </ant>
+                </graph_to_image>
             </postprocess_modules>
         </echoxml>
         
@@ -278,6 +286,7 @@
             <param name="temp" expression="${CRM_PROCES_tree_basedir.temp}"/>
             <param name="uuid" expression="${uuid}"/>
             <param name="preprocess" expression="${preprocess}"/> <!-- to use mainly for merge images -->
+            <param name="TargetNamespaceBindings" expression="${TargetNamespaceBindings}"/> <!-- to use for referenced process to URL https://biuro.biall-net.pl/SE/version-git/procesy5.php?task=PROCES_VIEW_LIST&HIDE_PANEL=0&show_big_img=1&group_stanowiska=1&id_proces=5609 -->
         </xslt>
         
 

+ 55 - 20
SE/schema/WPS_Functions/default_db/recurse_ant/build_recurse_ant.xml

@@ -38,7 +38,9 @@
     <property name="default_db:recurse_ant_basedir" location="."/>
     <property name="default_db:recurse_ant_basedir.temp" location="${default_db:recurse_ant_basedir}/temp/${uuid}"/> <!-- /${uuid} -->
     <property name="default_db:recurse_ant" value="${default_db:recurse_ant_basedir}/build_recurse_ant.xml"/>
+    <property name="default_db:recurse_ant_basedir.recurse_cache" location="${default_db:recurse_ant_basedir}/recurse_cache"/>
      
+    
     <property name="out" value="out"/> <!-- output ready files like pdf -->
     <property name="p5_tr_map_dir" location="${basedir}/../../../default_db.instance.xml/p5_tr_map" />
     <property name="simpleSchema_dir" location="${basedir}/../../../default_db.instance.xml/simpleSchema" />
@@ -68,6 +70,7 @@
     <property name="temp" value="${default_db:recurse_ant_basedir.temp}${loop_path_recurs}" /> <!-- temp files-->
     <property name="xpath_query.xsl" value="${default_db:recurse_ant_basedir.temp}/${uuid}.xpath_query.xsl"/>
     <property name="build_recurse_ant_xpath_query.xsl" value="${default_db:recurse_ant_basedir.temp}/build_recurse_ant_xpath_query.xsl"/>
+    
     <property name="process_locks" value="${default_db:recurse_ant_basedir.temp}/.locks"/>
     <property name="empty.xml" value="${temp}/${uuid}.empty.xml"/>
     <!-- variables to first transform of doc for then get related features -->
@@ -95,6 +98,8 @@
     
     <property name="dita.dir" location="/Library/Server/Web/Data/Sites/Default/SE/stuff/dita-ot-2.3.3/bin/dita"/>
     
+    <property name="input_obj_fid_forced" value="${input_obj_fid_forced}"/>
+    
     <!--<echo>#93 $typeName= ${typeName};  $xpath= ${xpath};  $xpath_value= ${xpath_value}  </echo>-->
 
     <!--<echo> $basedir=${basedir}; 
@@ -106,7 +111,9 @@
     <property name="CRM_PROCES_tree_basedir" location="${default_db:recurse_ant_basedir}/../CRM_PROCES_tree"  />                         
     <property name="CRM_PROCES_tree_basedir.temp" location="${CRM_PROCES_tree_basedir}/temp"  />
     
-    <property name="dita.dir" location="/Library/Server/Web/Data/Sites/Default/SE/stuff/dita-ot-2.3.3/bin/dita"/><target name="uuid_cond">
+    <property name="dita.dir" location="/Library/Server/Web/Data/Sites/Default/SE/stuff/dita-ot-2.3.3/bin/dita"/>
+    
+    <target name="uuid_cond">
         <condition property="uuid_set" taskname="uuid_check">
             <or>
                 <equals arg1="${uuid}" arg2=""/>
@@ -226,8 +233,10 @@
     <target name="install_folders">
         <mkdir dir="${temp}"/>
         <mkdir dir="${out}"/>
+        <mkdir dir="${default_db:recurse_ant_basedir.recurse_cache}"/>
         <chmod  dir="${temp}" perm="ugo+rwx"/>
         <chmod  dir="${out}" perm="ugo+rwx"/>
+        <chmod  dir="${default_db:recurse_ant_basedir.recurse_cache}" perm="ugo+rwx"/>
     </target>
 
 
@@ -323,7 +332,7 @@
             <mkdir dir="${process_locks}"/>
         </target>
     
-    <target name="xml_recursed_from_tr_map_with_RelatedFeatureLoop" depends="mkdir_process_locks,build_recurse_ant_xsl_xpath_filter.xsl,xml_recursed_from_tr_map" description="URL_TASK Pobierz XML obiektu">
+    <target name="xml_recursed_from_tr_map_with_RelatedFeatureLoop" depends="mkdir_process_locks,build_recurse_ant_xsl_xpath_filter.xsl,xml_recursed_from_tr_map,xml_recursed_from_tr_map_with_RelatedFeatureLoop_noout" description="URL_TASK Pobierz XML obiektu">
                         
                         <!-- TODO proba wlaczenia rekursywnosci zapisanie wezla do sciezki w folderze-->
                         <!--<copy file="${out_task.dita.final}" tofile="${temp}/${loop_path_recurs}/${uuid_tested}.loop_path.xml" />-->
@@ -333,29 +342,45 @@
                         <!--<echoxml file="${out_task.dita.final}.xml">
                             <RelatedFeatureRoot>${out_task.dita.final_no_RelatedFeatureRoot}</RelatedFeatureRoot>
                         </echoxml>-->
-                    <delete file="${out_task.dita.final_with_RelatedFeatureLoop}.xml"/>   
-                    <xslt basedir="${default_db:recurse_ant_basedir}" style="${build_recurse_ant_merge_dita_from_temp.xsl}" in="${main_build_recurse_ant.dita_with_id}"  destdir="./" out="${out_task.dita.final_with_RelatedFeatureLoop}.xml" >
-                            <factory name="net.sf.saxon.TransformerFactoryImpl">    
-                                <attribute name="http://saxon.sf.net/feature/xinclude-aware" value="true"/>
-                                <attribute name="http://saxon.sf.net/feature/initialMode" value="RelatedFeatureRoot"/>
-                            </factory>
-                            <classpath location="/opt/local/share/java/saxon9he.jar"/>
-                            <!--<param name="webRootUrl" expression="${webRootUrl}" />--> 
-                            <!--<param name="main_build_recurse_ant.dita" expression="${main_build_recurse_ant.dita}"/>-->
-                            <param name="uuid" expression="${uuid_tested}"/>
-                            <!--<param name="basedir" expression="${basedir}"/>-->
-                            <param name="temp" expression="${temp}"/>
-                            <param name="with_RelatedFeatureLoop" expression="1"/>
-                            <param name="simpleSchema_dir" expression="${simpleSchema_dir}"/>
-                        </xslt>
+                   
                         <echo message="OUTPUT__START"/>
                         <echo>
                             &lt;br&gt;Wygenerowano wyjsciowy plik XML - sciagnij go &lt;a href=&quot;${webRootUrl}/${out_task.dita.final_with_RelatedFeatureLoop_no_path}.xml&quot; target=&quot;_blank&quot;&gt; &quot;${webRootUrl}/${out_task.dita.final_with_RelatedFeatureLoop_no_path}.xml&lt;/a&gt;&lt;/br&gt; 
                         </echo>
                         <echo message="OUTPUT__END"/>
          </target>
+    
+    
+    <target name="xml_recursed_from_tr_map_with_RelatedFeatureLoop_noout" depends="mkdir_process_locks,build_recurse_ant_xsl_xpath_filter.xsl,xml_recursed_from_tr_map" description="URL_TASK Pobierz XML obiektu">
+        
+        <!-- TODO proba wlaczenia rekursywnosci zapisanie wezla do sciezki w folderze-->
+        <!--<copy file="${out_task.dita.final}" tofile="${temp}/${loop_path_recurs}/${uuid_tested}.loop_path.xml" />-->
+        
+        <!--<loadfile property="out_task.dita.final_no_RelatedFeatureRoot" srcfile="${out_task.dita.final}"/>-->
+        <!--<move file="${out_task.dita.final}" tofile="${out_task.dita.final}.xml"/>-->
+        <!--<echoxml file="${out_task.dita.final}.xml">
+                            <RelatedFeatureRoot>${out_task.dita.final_no_RelatedFeatureRoot}</RelatedFeatureRoot>
+                        </echoxml>-->
+        <delete file="${out_task.dita.final_with_RelatedFeatureLoop}.xml"/>   
+        <xslt basedir="${default_db:recurse_ant_basedir}" style="${build_recurse_ant_merge_dita_from_temp.xsl}" in="${main_build_recurse_ant.dita_with_id}"  destdir="./" out="${out_task.dita.final_with_RelatedFeatureLoop}.xml" >
+            <factory name="net.sf.saxon.TransformerFactoryImpl">    
+                <attribute name="http://saxon.sf.net/feature/xinclude-aware" value="true"/>
+                <attribute name="http://saxon.sf.net/feature/initialMode" value="RelatedFeatureRoot"/>
+            </factory>
+            <classpath location="/opt/local/share/java/saxon9he.jar"/>
+            <!--<param name="webRootUrl" expression="${webRootUrl}" />--> 
+            <!--<param name="main_build_recurse_ant.dita" expression="${main_build_recurse_ant.dita}"/>-->
+            <param name="uuid" expression="${uuid_tested}"/>
+            <!--<param name="basedir" expression="${basedir}"/>-->
+            <param name="temp" expression="${temp}"/>
+            <param name="with_RelatedFeatureLoop" expression="1"/>
+            <param name="simpleSchema_dir" expression="${simpleSchema_dir}"/>
+        </xslt>
+       
+    </target>
+    
    
-    <target name="xml_recursed_from_tr_map_with_RelatedFeatureLoop_tree_to_dita" depends="xml_recursed_from_tr_map_with_RelatedFeatureLoop" description="URL_TASK Wygeneruj druk z obiektu uzywajac styli systemowych">
+    <target name="xml_recursed_from_tr_map_with_RelatedFeatureLoop_tree_to_dita" depends="xml_recursed_from_tr_map_with_RelatedFeatureLoop_noout" description="URL_TASK Wygeneruj druk z obiektu uzywajac styli systemowych">
         
         <ant antfile="${build_CRM_PROCES_tree}" target="default_db:PROCES_INIT:tree:dita" inheritall="false">
             <property name="uuid" value="${uuid}"/>
@@ -374,8 +399,11 @@
         </exec>
         
         <echo message="OUTPUT__START"/>
-        <echo>
+        <!--<echo>
             &lt;br&gt;Wygenerowano wyjsciowy plik XML - sciagnij go &lt;a href=&quot;${webRootUrl}/${out_task.dita.final_with_RelatedFeatureLoop_no_path}.xml&quot; target=&quot;_blank&quot;&gt; &quot;${webRootUrl}/${out_task.dita.final_with_RelatedFeatureLoop_no_path}.xml&lt;/a&gt;&lt;/br&gt; 
+        </echo>-->
+        <echo>
+            &lt;br&gt;# Wygenerowano wyjsciowy plik XML - sciagnij go &lt;a href=&quot;${webRootUrl}/${out}/${uuid}.pdf&quot; target=&quot;_blank&quot;&gt; &quot;${webRootUrl}/${out}/${uuid}.pdf&lt;/a&gt;&lt;/br&gt; 
         </echo>
         <echo message="OUTPUT__END"/>
     </target>
@@ -430,7 +458,8 @@
         <property name="xpath_query_check_create_xsl.xsl" value="${default_db_xml_cache.public}/xpath_query_check_create_xsl.xsl"/>
         <property name="build_recurse_ant_xsl_xpath_filter.xsl" value="${default_db_xml_cache.public}/build_recurse_ant_xsl_xpath_filter.xsl"/>
         <property name="recurse_ant_to_html.xsl" value="${default_db_xml_cache.public}/recurse_ant_to_html.xsl"/>
-        <property name="recurse_ant_to_html.xsl" value="${default_db_xml_cache.public}/recurse_ant_to_html.xsl"/>
+        <property name="build_recurse_ant_cache_control.xsl" value="${default_db_xml_cache.public}/build_recurse_ant_cache_control.xsl"/>
+        
     </target>
     
     <target name="check_default_db_xml_cache.public_default" depends="set_default_db_xml_cache.public_local" unless="${set_default_db_xml_cache.public_local}">
@@ -442,6 +471,7 @@
         <property name="xpath_query_check_create_xsl.xsl" value="${default_db_xml_cache.public}/xpath_query_check_create_xsl.xsl"/>
         <property name="build_recurse_ant_xsl_xpath_filter.xsl" value="${default_db_xml_cache.public}/build_recurse_ant_xsl_xpath_filter.xsl"/>
         <property name="recurse_ant_to_html.xsl" value="${default_db_xml_cache.public}/recurse_ant_to_html.xsl"/>
+        <property name="build_recurse_ant_cache_control.xsl" value="${default_db_xml_cache.public}/build_recurse_ant_cache_control.xsl"/>
     </target>
     
     
@@ -628,6 +658,10 @@
             <param name="parallel_pos" expression="${parallel_pos}"/>
             <param name="process_locks" expression="${process_locks}"/>
             <param name="get_wfs" expression="${get_wfs}"/>
+            <param name="recurse_ant_basedir.recurse_cache" expression="${default_db:recurse_ant_basedir.recurse_cache}"/>
+            <param name="build_recurse_ant_cache_control.xsl" expression="${build_recurse_ant_cache_control.xsl}"/>
+            <param name="typeNameRoot" expression="${typeName_tested}"/> <!-- to cache purpose - to allow child elements cache created to be named from this typeNameRoot -->
+            <param name="input_obj_fid_forced" expression="${input_obj_fid_forced}"/>
         </xslt>
         <!--<import file="${build_recurse_ant.xml}"/>-->
         <!--<record action="stop" name="ant.log"/>-->
@@ -668,6 +702,7 @@
             <!--<param name="basedir" expression="${basedir}"/>-->
             <param name="temp" expression="${temp}"/>
             <param name="simpleSchema_dir" expression="${simpleSchema_dir}"/>
+            <param name="recurse_ant_basedir.recurse_cache" expression="${default_db:recurse_ant_basedir.recurse_cache}"/>
         </xslt>
         <echo> *************************************************************************
                * #573 FIHISHED NODE-LOOP in SAVED TO BE MERGED BY MERGE                     

+ 23 - 2
SE/schema/WPS_Functions/get_wfs/build_get_wfs.xml

@@ -35,6 +35,8 @@
     <property name="wfs_error.xml" location="${get_wfs_basedir.temp}/${uuid}.wfs_error.xml"/>
     <property name="wfs_timestamp.xml" location="${get_wfs_basedir.temp}/${uuid}.wfs_timestamp.xml"/>
     
+    <!-- for sending xml - param inputs -->
+    <property name="wfs_input.xml" location="${get_wfs_basedir.temp}/${uuid}.wfs_input.xml"/> 
     <!-- eof module local properties -->
 
     
@@ -143,6 +145,25 @@
         <echoxml file="${wfs_timestamp.xml}"><wfs_timestamp>${wfs.timestamp}</wfs_timestamp></echoxml>
     </target>
     
+    <target name="PostXmlTransaction" description="TODO to post transaction based on build__for_post_to_wfs and curl_post_wfst">
+        <exec executable="$curl" outputproperty="wfs_output" errorproperty="wfs_error" resultproperty="wfs_result" failonerror="true">
+            <arg value="-H 'Authorization: Basic ${passwordBase64Basic_tested}'"/>
+            <arg value="-X"/>
+            <arg value="POST"/>
+            <arg value="-d"/>
+            <arg value="${wfs_input.xml}"/>
+            <arg value="${api_url}"/>
+            <!--<arg value="-\-user {$Types__x3A__wfs:username}:{$Types__x3A__wfs:username-pass} "/>-->
+            <arg value="-v"/>
+            <arg value="--insecure"/>
+            <!--<arg value="-O"/>
+                <arg value="{@Target_file}"/>-->
+        </exec>
+        <echo file="${wfs_output.xml}" message="${wfs_output}"/>
+        <echo file="${wfs_result.xml}" message="${wfs_result}"/>
+        <echo file="${wfs_error.xml}" message="${wfs_error}"/>
+    </target>
+    
     <target name="GetFeatureByLastMod" description="URL_TASK Target GetFeatureByLastMod - should do incremental retrievement" depends="read_build_properties,Filter_check"/>
        
        
@@ -184,7 +205,7 @@
     
     <target name="GetXmlByUrlVerifyRetry.validate_length.failed" if="${output.length.failed}">
         <echo>GetXmlByUrlVerifyRetry.validate_length.failed #182 - first retry ! </echo>
-        <delete file="${output}"/>
+        <delete file="${output}" quiet="true"/>
         <exec executable="curl" output="${output}" errorproperty="wfs_error"> 
             <arg line="${cookie_curl_option} &quot;${url}&quot;"/>
             <arg line="--connect-timeout 5"/>
@@ -197,7 +218,7 @@
     </target>
     <target name="GetXmlByUrlVerifyRetry.validate_length.failed.0" if="${output.length.failed.0}">
         <echo>GetXmlByUrlVerifyRetry.validate_length.failed #198 - first retry - zero length ! </echo>
-        <delete file="${output}"/>
+        <delete file="${output}" quiet="true"/>
         <exec executable="curl" output="${output}" errorproperty="wfs_error"> 
             <arg line="${cookie_curl_option} &quot;${url}&quot;"/>
             <arg line="--connect-timeout 5"/>

+ 1 - 1
SE/schema/WPS_Functions/mapserver_gml_to_png/build_mapserver_gml_to_png.xml

@@ -18,7 +18,7 @@
     <!-- local individual module properties -->
     <property name="genpng2.sh" location="${mapserver_gml_to_png_basedir}/map/genpng2.sh"/>
     <property name="shp2img" value="/opt/local/bin/shp2img"/>
-    <property name="PATH_ADD" value="/opt/local/bin:/usr/bin:/bin"/>
+    <!--<property name="PATH_ADD" value="/opt/local/bin:/usr/bin:/bin"/>-->
     
     <property name="mapserver_gml_to_png:raster.NAME" location="Otomin"/><!-- to be dynamic -->
     <property name="mapserver_gml_to_png:raster.TYPE" location="RASTER"/>

+ 33 - 9
SE/schema/WPS_Functions/mapserver_gml_to_png/map/genpng2.sh

@@ -6,10 +6,11 @@
 SIZE=800 # szerokosc i wysokosc obrazka
 DPI=120 # rozdzielczosc w DITA - nie ruszac
 SCALE=500 # domyslna skala
-BORDER=5 # liczba metrow, o ktora nalezy powiekszyc zasieg z kazdej strony wzgledem danych wejsciowych
-BORDER_CUT="5"
+BORDER=$[SCALE/100] # liczba metrow, o ktora nalezy powiekszyc zasieg z kazdej strony wzgledem danych wejsciowych
+BORDER_CUT=$[SCALE/100]
 UUID=${RANDOM}
 RETRIES=3 # liczba prob wygenerowania obrazka
+PROCESSES=12 # maksymalna liczba procesow potomnych
 
 MAP_SCHEMA_DIR="./map_schema"
 MAP_SCHEMA_HEAD="${MAP_SCHEMA_DIR}/head.map.inc"
@@ -32,9 +33,10 @@ Usage: ${0} -g|--gml FILE[s] [OPTION]...
 Options:
  -s, --scale SCALE		Scale, defult ${SCALE} (means 1:${SCALE})
  -b, --border METERS		Size of border in meters that will be added
-				  around GML data (default ${BORDER})
+				  around GML data (default SCALE/100 = ${BORDER})
  -cb, --cut-border METERS	Size of additional border in meters
-				  while splitting image (default ${BORDER_CUT})
+				  while splitting image (default SCALE/100 = ${BORDER})
+ -p, --processes		shp2img processes count limit (default ${PROCESSES}
  -r, --retries			Number of retries to generate signle image (default ${RETRIES})
  -u, --uuid			UUID for generating process, defaut - random
  -w, --wms WMS_LAYER[s]		URLs and layer names (as following arguments)
@@ -65,6 +67,9 @@ while [ "${1}" ]; do
 		"-g"|"--gml")
 			PARAM="gml"
 			;;
+		"-p"|"--processes")
+			PARAM="processes"
+			;;
 		"-r"|"--retries")
 			PARAM="retries"
 			;;
@@ -88,6 +93,9 @@ while [ "${1}" ]; do
 				"gml")
 					FILES+=("${1}")
 					;;
+				"processes")
+					PROCESSES=${1}
+					;;
 				"retries")
 					RETRIES=${1}
 					;;
@@ -116,8 +124,18 @@ FONT_DIR="${PWD}/fonts"
 TMP=`mktemp -d`
 SHAPEPATH="${TMP}/2177"
 
-size=`echo "${SIZE}/${DPI}*2.54*${SCALE}/100" | bc -l`
+#size=`echo "${SIZE}/${DPI}*2.54*${SCALE}/100" | bc -l`
+#size_cut=`echo "${size} - ${BORDER_CUT}" | bc -l`
+#correction_tmp=`echo "12 - l(${SCALE}) / l(2)" | bc -l`
+#correction_tmp=`echo "10 ^ (${correction_tmp} / 1)" | bc`
+#correction=`echo "1 / ${correction_tmp}" | bc -l`
+#correction=`echo "scale=20;a=6+l(125/${SCALE})/l(2)-0.5;scale=0;b=a/1;scale=20;1/10^b" | bc -l`
+correction=`echo "e(-(6+l(142/${SCALE})/l(2)-0.3)*l(8))" | bc -l`
+#correction=0
+size=`echo "${SIZE}/${DPI}*2.54*(${SCALE})/100 - ${correction}" | bc -l`
 size_cut=`echo "${size} - ${BORDER_CUT}" | bc -l`
+correction=0
+
 
 for i in `seq 0 $[${#FILES[@]}-1]`; do
 	FILE=${FILES[i]}
@@ -200,11 +218,16 @@ echo '<?xml version="1.0" encoding="UTF-8"?>' > ${XML_FILE}
 echo "<images uuid=\"${UUID}\">" >> ${XML_FILE}
 for y in `seq 1 ${ly}`; do
 	for x in `seq 1 ${lx}`; do
+		jobs=`jobs -p | wc -l`
+		while [ ${jobs} -ge ${PROCESSES} ]; do
+			sleep 1
+			jobs=`jobs -p | wc -l`
+		done
 		printf "Generating ${x}x${y} "
-		lxmin=`echo "${xmin} + ${size_cut} * (${x} -1)" | bc -l`
-		lymin=`echo "${ymin} + ${size_cut} * (${y} -1)" | bc -l`
-		lxmax=`echo "${lxmin} + ${size}" | bc -l`
-		lymax=`echo "${lymin} + ${size}" | bc -l`
+		lxmin=`echo "${xmin} + ${size_cut} * (${x} -1) + ${correction}" | bc -l`
+		lymin=`echo "${ymin} + ${size_cut} * (${y} -1) + ${correction}" | bc -l`
+		lxmax=`echo "${lxmin} + ${size} - 2 * ${correction}" | bc -l`
+		lymax=`echo "${lymin} + ${size} - 2 * ${correction}" | bc -l`
 		printf "(BBOX: ${lxmin} ${lymin} ${lxmax} ${lymax})...\n"
 		file="${IMAGE_FILE}-${y}-${x}.png"
 
@@ -221,6 +244,7 @@ for y in `seq 1 ${ly}`; do
 			echo "    <ymax>${lymax}</ymax>" >> ${XML_FILE}
 			echo "    <file>${file}</file>" >> ${XML_FILE}
 			echo "  </image>" >> ${XML_FILE}
+			echo "\tshp2img -m \"${MAP}\" -o ${file} -e ${lxmin} ${lymin} ${lxmax} ${lymax}"
 			{
 				retry=0
 				while [ ${retry} -le ${RETRIES} ]; do

+ 7 - 0
SE/schema/WPS_Functions/object_modeling_by_default_db_xml_schema/build_object_modeling_by_default_db_xml_schema.xml

@@ -39,6 +39,7 @@
         <antcall target="system_cache__appinfo:default_db_get_xml_schema"/>
     </target>
     
+    
     <target name="system_cache__appinfo:default_db_get_xml_schema" depends="read_build_properties">
         <echo>We try to refresh ${default_db.xml} </echo>
         <delete file="${default_db.xml}"/>
@@ -47,4 +48,10 @@
             <arg line=" -H 'Authorization: Basic ${passwordBase64Basic}' &#34;${default_db.xml.get_url}&#34;"/> <!-- ${TargetNamespaceBindings}/SE/version-git/api.php/xml/xsd/default_db  -->
         </exec>
     </target>
+    
+    
+    
+    
+    
+    
 </project>