audit_krs_con_digger.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. <?php
  2. $_SERVER['SERVER_NAME'] = 'biuro.biall-net.pl';
  3. //when used in /Library/Server/Web/Data/Sites/bzyk-biale-plamy-php-temp/SE/dev on biuro.biall-net.pl
  4. if( file_exists ( "../../se-lib/bootstrap.php"))
  5. require("../../se-lib/bootstrap.php");
  6. //when used in in native SE
  7. else require("../../../../SE-production-git/SE/se-lib/bootstrap.php");
  8. Lib::loadClass("Vendor_Geophp");
  9. $db = DB::getDB();
  10. // DB::getPDO()->query("truncate table Rozdzielcza_test_bzyk_Cables_to_PE");
  11. //unset($ID_Way);
  12. /*foreach($cables_from_joins_to_PE_arr as $id_j=>$Path_Point_val) {
  13. $sql_Path_Point_val="insert into Rozdzielcza_test_bzyk_Cables_to_PE (the_geom,ID_Way,ID_Point,len,PE_ID_Join)
  14. values (ST_GeomFromText('".$Path_Point_val['LINESTRING']."'),'".$Path_Point_val['ID_Way']."',
  15. '".$Path_Point_val['ID_Point']."' ,'".$Path_Point_val['len']."','".$Path_Point_val['PE_ID_Join']."' ) ";
  16. echo "#216Query path ins:: ".$sql_Path_Point_val." ";
  17. //if(strlen($edge_joins_filled_paths[$ID_Way]['asText'])>2)
  18. if(strlen($Path_Point_val['LINESTRING'])>1)
  19. DB::getPDO()->query($sql_Path_Point_val);
  20. else echo " ERROR blad danych!!! dla ID_Point:".$Path_Point_val['ID_Point'];
  21. //else echo "#296 path nie wygenerowalo sie !! dla ".$ID_Way." i pkt ".$pkt_A." , ".$pkt_B." \n";
  22. }
  23. */
  24. $tables['Y']['BI_audit_ENERGA_PRACOWNICY']['rel']['pesel']='pesel';
  25. $tables['Y']['BI_audit_ENERGA_PRACOWNICY']['rel']['nip']='nip';
  26. $tables['Y']['BI_audit_ENERGA_PRACOWNICY']['rel']['regon']='regon';
  27. $tables['Y']['BI_audit_ENERGA_PRACOWNICY']['rel']['ID_BI_audit_ENERGA_PRACOWNICY']='ID';
  28. //$tables['X']['BI_audit_KW_requested']['rel_search']['pesel']='Seller_Person';
  29. //$tables['X']['BI_audit_KW_requested']['rel']['pesel']['search']='like';
  30. $tables['Z']['BI_audit_ENERGA_PRACOWNICY_adresy']['rel']['ID_BI_audit_ENERGA_PRACOWNICY']='ID_BI_audit_ENERGA_PRACOWNICY';
  31. $tables['Z']['BI_audit_ENERGA_PRACOWNICY_adresy']['rel_and']['TERYT']['TERYT_SYM']='TERYT_SYM';
  32. $tables['Z']['BI_audit_ENERGA_PRACOWNICY_adresy']['rel_and']['TERYT']['TERYT_SYM_UL']='TERYT_SYM_UL';
  33. $tables['Z']['BI_audit_ENERGA_PRACOWNICY_adresy']['rel_and']['TERYT']['budynek']='nrBudynku';
  34. //$tables['Z']['BI_audit_ENERGA_PRACOWNICY_adresy']['rel_and']['TERYT']['nrLokalu']='nrLokalu';
  35. $tables['X']['BI_audit_ENERGA_RUM_KONTRAHENCI']['rel']['nip']='NIP';
  36. $tables['X']['BI_audit_ENERGA_RUM_KONTRAHENCI']['rel']['regon']='REGON';
  37. $tables['X']['BI_audit_ENERGA_RUM_KONTRAHENCI']['rel']['pesel']='PESEL';
  38. //$tables['X']['BI_audit_ENERGA_RUM_KONTRAHENCI']['rel']['teryt']=1;
  39. //$tables['X']['BI_audit_ENERGA_RUM_UMOWY']['rel_like']['nip']='Strony_umowy';
  40. $tables['Z']['BI_audit_CEIDG']['rel']['nip']='nip';
  41. $tables['Z']['BI_audit_CEIDG']['rel']['regon']='regon';
  42. $tables['Z']['BI_audit_CEIDG']['rel_and']['adres']['miejscowosc']='miejscowosc';
  43. $tables['Z']['BI_audit_CEIDG']['rel_and']['adres']['ulica']='ulica';
  44. $tables['Z']['BI_audit_CEIDG']['rel_and']['adres']['kodPocztowy']='kodPocztowy';
  45. $tables['Z']['BI_audit_CEIDG']['rel_and']['adres']['budynek']='budynek';
  46. $tables['Z']['BI_audit_CEIDG']['rel_and']['TERYT']['TERYT_SYM']='TERYT_SYM';
  47. $tables['Z']['BI_audit_CEIDG']['rel_and']['TERYT']['TERYT_SYM_UL']='TERYT_SYM_UL';
  48. //$tables['Z']['BI_audit_CEIDG']['rel_and']['TERYT']['nrLokalu']='lokal';
  49. $tables['Z']['BI_audit_CEIDG']['rel_and']['TERYT']['budynek']='budynek';
  50. $tables['Z']['BI_audit_CEIDG']['ref'][19]='BI_audit_CEIDG_pelnomocnicy';
  51. $tables['Z']['BI_audit_CEIDG']['ref'][18]='BI_audit_CEIDG_powiazania';
  52. $tables['Z']['BI_audit_CEIDG_pelnomocnicy']['rel']['nip']='nip';
  53. $tables['Z']['BI_audit_CEIDG_pelnomocnicy']['rel_and']['adres']['miejscowosc']='miejscowosc';
  54. $tables['Z']['BI_audit_CEIDG_pelnomocnicy']['rel_and']['adres']['ulica']='ulica';
  55. $tables['Z']['BI_audit_CEIDG_pelnomocnicy']['rel_and']['adres']['kodPocztowy']='kodPocztowy';
  56. $tables['Z']['BI_audit_CEIDG_pelnomocnicy']['rel_and']['TERYT']['TERYT_SYM']='TERYT_SYM';
  57. $tables['Z']['BI_audit_CEIDG_pelnomocnicy']['rel_and']['TERYT']['TERYT_SYM_UL']='TERYT_SYM_UL';
  58. //$tables['Z']['BI_audit_CEIDG_pelnomocnicy']['rel_and']['TERYT']['nrLokalu']='lokal';
  59. $tables['Z']['BI_audit_CEIDG_pelnomocnicy']['rel_and']['TERYT']['budynek']='budynek';
  60. $tables['Z']['BI_audit_CEIDG_pelnomocnicy']['backref'][19]='BI_audit_CEIDG';
  61. $tables['Z']['BI_audit_CEIDG_powiazania']['rel']['nip']='nip';
  62. $tables['Z']['BI_audit_CEIDG_powiazania']['rel']['regon']='regon';
  63. $tables['Z']['BI_audit_CEIDG_powiazania']['backref'][18]='BI_audit_CEIDG';
  64. $tables['Z']['BI_audit_KRS']['rel']['nip']='nip';
  65. $tables['Z']['BI_audit_KRS']['rel']['regon']='regon';
  66. $tables['Z']['BI_audit_KRS']['rel']['krs']='krs';
  67. $tables['Z']['BI_audit_KRS']['rel_and']['adres']['miejscowosc']='A_miejscowosc';
  68. $tables['Z']['BI_audit_KRS']['rel_and']['adres']['ulica']='A_ulica';
  69. $tables['Z']['BI_audit_KRS']['rel_and']['adres']['kodPocztowy']='A_kod';
  70. $tables['Z']['BI_audit_KRS']['rel_and']['adres']['budynek']='A_nrDomu';
  71. $tables['Z']['BI_audit_KRS']['rel_and']['TERYT']['TERYT_SYM']='TERYT_SYM';
  72. $tables['Z']['BI_audit_KRS']['rel_and']['TERYT']['TERYT_SYM_UL']='TERYT_SYM_UL';
  73. //$tables['Z']['BI_audit_KRS']['rel_and']['TERYT']['nrLokalu']='A_nrLokalu';
  74. $tables['Z']['BI_audit_KRS']['rel_and']['TERYT']['budynek']='A_nrDomu';
  75. $tables['Z']['BI_audit_KRS']['ref'][12]='BI_audit_KRS_person';
  76. $tables['Z']['BI_audit_KRS']['ref'][11]='BI_audit_KRS_company';
  77. $tables['Z']['BI_audit_KRS_company']['rel']['regon']='regon';
  78. $tables['Z']['BI_audit_KRS_company']['rel']['krs']='krs';
  79. $tables['Z']['BI_audit_KRS_company']['backref'][11]='BI_audit_KRS';
  80. $tables['Z']['BI_audit_KRS_person']['rel']['pesel']='pesel';
  81. $tables['Z']['BI_audit_KRS_person']['backref'][12]='BI_audit_KRS';
  82. function start_dig($tables) {
  83. global $db;
  84. foreach($tables['Y'] as $Y=>$t) {
  85. $sql='select * from '.$Y.' where ID=12 or ID=19 or 1=12 limit 100' ;
  86. $res=$db->query($sql);
  87. while($h=$db->fetch($res)) {
  88. $hist_loop[$Y][]=$h;
  89. $hist_time[][$Y]=$h;
  90. dig_next($hist_loop,$t,$h,1,$hist_time,$Y,add_path($path,$Y));
  91. unset($hist_time);
  92. }
  93. }
  94. }
  95. function add_path($path='/',$last_tbl) {
  96. $path.="/".$last_tbl;
  97. }
  98. function dig_next($hist_loop,$prev_tbl,$prev_dig,$loop,$hist_time,$last_tbl='n/s',$path='/') {
  99. global $db,$tables;
  100. $loop++;
  101. echo "\n<hr>###LOOP ".$loop." path:".$path;
  102. if($loop<12) {
  103. //detect next ref tbls
  104. $sql = array();
  105. $sql_RELAND = array();
  106. foreach($tables['Z'] as $Z=>$z) {
  107. //rels in tbls
  108. foreach($z['rel'] as $z_rel_from=>$z_rel_to) {
  109. // echo "110 test z_rel_fr ".$z_rel_from." to z_rel_to ".$z_rel_to ;
  110. if( isset($prev_tbl['rel'][$z_rel_from])) {
  111. if( strlen($prev_dig->$prev_tbl['rel'][$z_rel_from]) > 0 ) {
  112. $sql[$Z]['rel'][$z_rel_to] = $prev_dig->$prev_tbl['rel'][$z_rel_from];
  113. }
  114. }
  115. }
  116. //search addr
  117. if(isset($z['rel_and'])) {
  118. foreach($z['rel_and'] as $z_rel_and=>$z_rel_and_name) {
  119. $rel_and_ok=1;
  120. foreach($z_rel_and_name as $z_rel_and_name_cur=>$z_rel_and_name_col_to) {
  121. if(isset($prev_dig->$z_rel_and_name_col_to) and ( strlen($prev_dig->$z_rel_and_name_col_to)) > 0 ) {//and ( strlen($prev_dig->$z_rel_and_name_cur) > 2)
  122. if($rel_and_ok<>0) $rel_and_ok=2;
  123. // echo "\n<br>121 rel_and from:".$last_tbl." OK for tbl ".$Z." z_rel_and_name_col_to: ".$z_rel_and_name_col_to." value:".$prev_dig->$z_rel_and_name_cur." rel_and_ok=".$rel_and_ok;
  124. } else {
  125. // echo "<br> #127 UNSET ".$prev_dig->$z_rel_and_name_cur." / ".$z_rel_and_name_col_to." ";
  126. $rel_and_ok=0;
  127. }
  128. }
  129. if($rel_and_ok==2) {
  130. // echo "\n<br>#133 Z=".$Z." ok rel_and_ok=".$rel_and_ok;
  131. foreach($z_rel_and_name as $z_rel_and_name_cur=>$z_rel_and_name_col_to) {
  132. // echo "\n<br> 135... ok. Z=".$Z."/ z_rel_and_name=".$z_rel_and_name_cur."/ z_rel_and_name_col_to=".$z_rel_and_name_col_to."/ prev_dig-z_rel_and_name_cur=".$prev_dig->$z_rel_and_name_cur;
  133. $sql_rel_and[$Z][$z_rel_and][$z_rel_and_name_col_to]=$prev_dig->$z_rel_and_name_col_to;
  134. }
  135. } else {
  136. // echo "<br>#137 error rel_and_ok=".$rel_and_ok;
  137. }
  138. }
  139. }
  140. }
  141. // echo "<br>135:sql_rel_and <pre>";
  142. // print_r($sql_rel_and);
  143. // echo " .EOF</pre>";
  144. if( isset($sql_rel_and)) {
  145. foreach($sql_rel_and as $T=>$z_rel_and_arr) {
  146. foreach($z_rel_and_arr as $z_rel_and_name=>$z_rel_and_name_col) {
  147. //echo "<br>156 z_rel_and_name=".$z_rel_and_name." ";
  148. $sql_rel_and_sql[$T][$z_rel_and_name]="select * from ".$T." where ( ";
  149. $and=0;
  150. foreach($z_rel_and_name_col as $z_rel_and_name_cur=>$z_rel_and_name_col_to) {
  151. if( $and==1) $sql_rel_and_sql[$T][$z_rel_and_name].=" and `".$z_rel_and_name_cur."` = '".$z_rel_and_name_col_to."' and `".$z_rel_and_name_cur."` not like '%--%' and `".$z_rel_and_name_cur."` not like '' " ;
  152. else $sql_rel_and_sql[$T][$z_rel_and_name].=" `".$z_rel_and_name_cur."` like '".$z_rel_and_name_col_to."' and `".$z_rel_and_name_cur."` not like '%--%' and `".$z_rel_and_name_cur."` not like '' " ;
  153. // if($z_rel_and_name_cur=='miejscowosc') $sql_rel_and_sql[$T][$z_rel_and_name].=" and `miejscowosc` not like '' " ;
  154. // if($z_rel_and_name_cur=='A_ulica') $sql_rel_and_sql[$T][$z_rel_and_name].=" and `A_ulica` not like '' " ;
  155. // if($z_rel_and_name_cur=='ulica') $sql_rel_and_sql[$T][$z_rel_and_name].=" and `ulica` not like '' " ;
  156. // if($z_rel_and_name_cur=='kodPocztowy') $sql_rel_and_sql[$T][$z_rel_and_name].=" and `kodPocztowy` not like '' " ;
  157. // if($z_rel_and_name_cur=='budynek') $sql_rel_and_sql[$T][$z_rel_and_name].=" and `budynek` like '".$z_rel_and_name_col_to."' " ;
  158. // if($z_rel_and_name_cur=='A_nrDomu') $sql_rel_and_sql[$T][$z_rel_and_name].=" and `A_nrDomu` like '".$z_rel_and_name_col_to."' " ;
  159. // if($z_rel_and_name_cur=='kodPocztowy') $sql_rel_and_sql[$T][$z_rel_and_name].=" and `kodPocztowy` like '".$z_rel_and_name_col_to."' " ;
  160. $and=1;
  161. }
  162. $sql_rel_and_sql[$T][$z_rel_and_name].=" ) " ;
  163. if( isset($hist_loop[$T])) {
  164. foreach($hist_loop[$T] as $hist_rec) {
  165. $sql_rel_and_sql[$T][$z_rel_and_name].=" and ID!='".$hist_rec->ID."' ";
  166. }
  167. }
  168. $sql_rel_and_sql[$T][$z_rel_and_name].=" ; " ;
  169. dig_arr_cur($hist_loop,$T,$sql_rel_and_sql[$T][$z_rel_and_name],$hist_time,$loop,$last_tbl,$path);
  170. }
  171. }
  172. }
  173. //echo "<br>164:sql_rel_and_sql <pre>";
  174. // print_r($sql_rel_and_sql);
  175. // echo " .EOF</pre>";
  176. //X test
  177. //echo " <br> 129 prev <pre>";
  178. // print_r($prev_dig);
  179. // echo "<br> prevtbl";
  180. // print_r($prev_tbl);
  181. // echo "</pre>";
  182. foreach($tables['X'] as $X=>$x) {
  183. //rels in tbls
  184. if( isset($x['rel']) ) {
  185. foreach($x['rel'] as $x_rel_from=>$x_rel_to) {
  186. //echo "\n<br> #133test tbl ".$X." z_rel_fr ".$x_rel_from." to z_rel_to ".$x_rel_to." prev tbl " ;
  187. if( isset($prev_tbl['rel'][$x_rel_from]) ) { //isset($prev_dig->$prev_tbl['rel'][$x_rel_to])
  188. // echo "<br>141 rel from ".$x_rel_from." exists .. testing field ".$prev_tbl['rel'][$x_rel_from]." <br>";//strlen(".$prev_dig->$prev_tbl['rel'][$x_rel_to].")
  189. if( isset($prev_dig->$prev_tbl['rel'][$x_rel_from]) and strlen($prev_dig->$prev_tbl['rel'][$x_rel_from]) > 1 ) { //
  190. $sql_X[$X]['rel'][$x_rel_to]= $prev_dig->$prev_tbl['rel'][$x_rel_from];
  191. }
  192. }
  193. }
  194. }
  195. //rel_like
  196. if( isset($x['rel_like'])) {
  197. foreach($x['rel_like'] as $x_rel_from=>$x_rel_to) {
  198. //echo "\n<br> #133test tbl ".$X." z_rel_fr ".$x_rel_from." to z_rel_to ".$x_rel_to." prev tbl " ;
  199. if( isset($prev_tbl['rel'][$x_rel_from]) ) { //isset($prev_dig->$prev_tbl['rel'][$x_rel_to])
  200. // echo "<br>141 rel from ".$x_rel_from." exists .. testing field ".$prev_tbl['rel'][$x_rel_from]." <br>";//strlen(".$prev_dig->$prev_tbl['rel'][$x_rel_to].")
  201. if( isset($prev_dig->$prev_tbl['rel'][$x_rel_from]) and strlen($prev_dig->$prev_tbl['rel'][$x_rel_from]) > 1 ) { //
  202. $sql_X1[$X]['rel_like'][$x_rel_to]= $prev_dig->$prev_tbl['rel'][$x_rel_from];
  203. }
  204. }
  205. }
  206. }
  207. }
  208. flush();
  209. // echo "\n<br>sql_X<pre>";
  210. // print_r($sql_X);
  211. // echo "</pre>";
  212. if( isset($sql_X)) {
  213. foreach($sql_X as $T => $rel) {
  214. $sql_x[$T]=" select * from ".$T." where ( ";
  215. foreach($rel['rel'] as $col=>$val) {
  216. if( isset($sql_x_str[$T])) $sql_x_str[$T].=" or `".$col."` = '".$val."' " ;
  217. else $sql_x_str[$T]=" `".$col."` = '".$val."' " ;
  218. }
  219. $sql_x[$T].=$sql_x_str[$T]." ) ";
  220. //not loop
  221. if( isset($hist_loop[$T])) {
  222. foreach($hist_loop[$T] as $hist_rec) {
  223. $sql_t[$T].=" and ID!='".$hist_rec->ID."' ";
  224. }
  225. }
  226. }
  227. }
  228. // echo "<br> sql_x" ;
  229. //print_r($sql_x);
  230. if( isset($sql_X1)) {
  231. foreach($sql_X1 as $T => $rel) {
  232. $sql_x1[$T]=" select * from ".$T." where ( ";
  233. foreach($rel['rel_like'] as $col=>$val) {
  234. if( isset($sql_x1_str[$T])) $sql_x1_str[$T].=" or `".$col."` like '%".$val."'%" ;
  235. else $sql_x1_str[$T]=" `".$col."` like '%".$val."%' " ;
  236. }
  237. $sql_x1[$T].=$sql_x1_str[$T]." ) ";
  238. //not loop
  239. if( isset($hist_loop[$T])) {
  240. foreach($hist_loop[$T] as $hist_rec) {
  241. $sql_t[$T].=" and ID!='".$hist_rec->ID."' ";
  242. }
  243. }
  244. }
  245. }
  246. if( isset($sql_x)) {
  247. foreach($sql_x as $X => $sql_ext) {
  248. echo "\n<br>174 Querying :".$sql_ext ;
  249. $res=$db->query($sql_ext) or die('175 blad');
  250. while($h=$db->fetch($res)) {
  251. echo "\n<hr> #177 POWIAZANIE ZNALEZIONE DLA X <pre>";
  252. print_r($h);
  253. echo "<br> Historia sledzen:";
  254. print_r($hist_time);
  255. echo "#177 eof </pre>";
  256. }
  257. }
  258. }
  259. if( isset($sql_x1)) {
  260. foreach($sql_x1 as $X => $sql_ext) {
  261. echo "\n<br>306 Querying :".$sql_ext ;
  262. $res=$db->query($sql_ext) or die('307 blad');
  263. while($h=$db->fetch($res)) {
  264. echo "\n<hr> #309 POWIAZANIE ZNALEZIONE DLA X2 <pre>";
  265. print_r($h);
  266. echo "<br> Historia sledzen:";
  267. print_r($hist_time);
  268. echo "<br>relacje:";
  269. echo "#177 eof </pre>";
  270. }
  271. }
  272. }
  273. //X test eof
  274. foreach($sql as $T => $rel) {
  275. $sql_t[$T]=" select * from ".$T." where ( ";
  276. foreach($rel['rel'] as $col=>$val) {
  277. if( isset($sql_t_str[$T])) $sql_t_str[$T].=" or `".$col."` = '".$val."' " ;
  278. else $sql_t_str[$T]=" `".$col."` = '".$val."' " ;
  279. }
  280. $sql_t[$T].=$sql_t_str[$T]." ) ";
  281. //not loop
  282. if( isset($hist_loop[$T])) {
  283. echo "<br> hist loop cnt: ". count($hist_loop[$T]);
  284. foreach($hist_loop[$T] as $hist_rec) {
  285. $sql_t[$T].=" and ID!='".$hist_rec->ID."' ";
  286. }
  287. }
  288. }
  289. // echo "<br> 202 hist loop cnt: <pre> ". count($hist_loop);
  290. // print_r($hist_loop);
  291. // echo "</pre>";
  292. /* echo "<pre>sql_t";
  293. print_r($sql);
  294. echo "<br>sql_t ";
  295. print_r($sql_t);
  296. echo "</pre>";
  297. */
  298. if( isset($sql_t)) {
  299. foreach($sql_t as $T => $sql_ext) {
  300. dig_arr_cur($hist_loop,$T,$sql_ext,$hist_time,$loop,$last_tbl,$path);
  301. }
  302. }
  303. //todo look in Y and die()
  304. } else {
  305. echo "\n<hr> #377 Przekroczony limit zagnieżdzeń dla wątku: path=".$path."; <pre>\n";
  306. //print_r($hist_time);
  307. echo "</pre>";
  308. }
  309. echo "<br>#243 glebokosc:".$loop;
  310. }
  311. function dig_arr_cur($hist_loop,$T,$sql_ext,$hist_time,$loop,$last_tbl,$path) {
  312. global $db,$tables;
  313. echo "\n<br>216: from:".$last_tbl." Querying :T ".$T." ext: ".$sql_ext ;//$sql_ext
  314. // echo "\n<br> las fou"
  315. flush();
  316. $res=$db->query($sql_ext) or die('211 blad '.$sql_ext);
  317. while($h=$db->fetch($res)) {
  318. echo "\n<br> #149 found ".$h->ID;
  319. // echo "<pre>"; print_r($h); echo "</pre>";
  320. $hist_loop[$T][]=$h;
  321. $hist_time[][$T]=$sql_ext;
  322. $hist_time[][$T]=$h;
  323. // print_r($hist_loop);
  324. dig_next($hist_loop, $tables['Z'][$T],$h,$loop,$hist_time,$last_tbl,add_path($path,$T));
  325. //rel dzieci $tables['Z']['BI_audit_CEIDG']['ref'][19]='BI_audit_CEIDG_pelnomocnicy';
  326. if(isset($tables['Z'][$T]['ref'])) {
  327. // echo "<br>#296 try to ref tables for T ".$T."<pre>";
  328. // print_r($tables['Z'][$T]['ref']);
  329. // echo "</pre>";
  330. foreach($tables['Z'][$T]['ref'] as $ref_id => $ref_table) {
  331. $sql_ref=" select REMOTE_PRIMARY_KEY from `CRM__#REF_TABLE__".$ref_id."` where `PRIMARY_KEY`=".$h->ID." ";
  332. $res_rel=$db->query($sql_ref) or die('211 blad '.$sql_ref);
  333. echo "<br>#297 ".$sql_ref;
  334. while($h_rel=$db->fetch($res_rel)){
  335. $sql_rel_rec="select * from `".$ref_table."` where ID=".$h_rel->REMOTE_PRIMARY_KEY." ";
  336. $res_rel_rec=$db->query($sql_rel_rec) or die('211 blad '.$sql_rel_rec);
  337. echo "<br>#159 ref tbl ".$sql_rel_rec;
  338. while($h_rel_rec=$db->fetch($res_rel_rec)){
  339. echo "#315 fnd:".$h_rel_rec->ID;
  340. $hist_loop[$ref_table][]=$h_rel_rec;
  341. $hist_time[][$ref_table]=$sql_rel_rec;
  342. $hist_time[][$ref_table]=$h_rel_rec;
  343. dig_next($hist_loop,$tables['Z'][$ref_table],$h_rel_rec,$loop,$hist_time,$ref_table,add_path($path,$ref_table));
  344. }
  345. }
  346. }
  347. } else echo "#322 not ref to ".$T;
  348. if(isset($tables['Z'][$T]['backref'])) {
  349. //echo "<br>#296 try to backref tables T ".$T."<pre>";
  350. // print_r($tables['Z'][$T]['backref']);
  351. //echo "</pre>";
  352. foreach($tables['Z'][$T]['backref'] as $ref_id => $ref_table) {
  353. $sql_ref=" select PRIMARY_KEY from `CRM__#REF_TABLE__".$ref_id."` where `REMOTE_PRIMARY_KEY`=".$h->ID." ";
  354. $res_rel=$db->query($sql_ref) or die('318 blad '.$sql_ref);
  355. echo "<br>#319 backref ".$sql_ref;
  356. while($h_rel=$db->fetch($res_rel)){
  357. $sql_rel_rec="select * from `".$ref_table."` where ID=".$h_rel->PRIMARY_KEY." ";
  358. $res_rel_rec=$db->query($sql_rel_rec) or die('211 blad '.$sql_rel_rec);
  359. echo "<br>#324 ref tbl ".$sql_rel_rec;
  360. while($h_rel_rec=$db->fetch($res_rel_rec)){
  361. echo "#315 fnd:".$h_rel_rec->ID;
  362. $hist_loop[$ref_table][]=$h_rel_rec;
  363. $hist_time[][$ref_table]=$sql_rel_rec;
  364. $hist_time[][$ref_table]=$h_rel_rec;
  365. dig_next($hist_loop,$tables['Z'][$ref_table],$h_rel_rec,$loop,$hist_time,$ref_table,add_path($path,$ref_table));
  366. }
  367. }
  368. }
  369. } else echo "#322 not backref to ".$T;
  370. }
  371. }
  372. start_dig($tables);
  373. echo "<hr>end of raport ";