audit_krs_con_digger.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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['X']['BI_audit_KW_requested']['rel_search']['pesel']='Seller_Person';
  28. //$tables['X']['BI_audit_KW_requested']['rel']['pesel']['search']='like';
  29. $tables['X']['BI_audit_ENERGA_RUM_KONTRAHENCI']['rel']['nip']='NIP';
  30. $tables['X']['BI_audit_ENERGA_RUM_KONTRAHENCI']['rel']['regon']='REGON';
  31. $tables['X']['BI_audit_ENERGA_RUM_KONTRAHENCI']['rel']['pesel']='PESEL';
  32. //$tables['X']['BI_audit_ENERGA_RUM_KONTRAHENCI']['rel']['teryt']=1;
  33. $tables['Z']['BI_audit_CEIDG']['rel']['nip']='nip';
  34. $tables['Z']['BI_audit_CEIDG']['rel']['regon']='regon';
  35. $tables['Z']['BI_audit_CEIDG']['rel_and']['adres']['miejscowosc']='miejscowosc';
  36. $tables['Z']['BI_audit_CEIDG']['rel_and']['adres']['ulica']='ulica';
  37. $tables['Z']['BI_audit_CEIDG']['rel_and']['adres']['kodPocztowy']='kodPocztowy';
  38. $tables['Z']['BI_audit_CEIDG']['ref'][19]='BI_audit_CEIDG_pelnomocnicy';
  39. $tables['Z']['BI_audit_CEIDG']['ref'][18]='BI_audit_CEIDG_powiazania';
  40. $tables['Z']['BI_audit_CEIDG_pelnomocnicy']['rel']['nip']='nip';
  41. $tables['Z']['BI_audit_CEIDG_pelnomocnicy']['rel_and']['adres']['miejscowosc']='miejscowosc';
  42. $tables['Z']['BI_audit_CEIDG_pelnomocnicy']['rel_and']['adres']['ulica']='ulica';
  43. $tables['Z']['BI_audit_CEIDG_pelnomocnicy']['rel_and']['adres']['kodPocztowy']='kodPocztowy';
  44. $tables['Z']['BI_audit_CEIDG_pelnomocnicy']['backref'][19]='BI_audit_CEIDG';
  45. $tables['Z']['BI_audit_CEIDG_powiazania']['rel']['nip']='nip';
  46. $tables['Z']['BI_audit_CEIDG_powiazania']['rel']['regon']='nip';
  47. $tables['Z']['BI_audit_CEIDG_powiazania']['backref'][18]='BI_audit_CEIDG';
  48. $tables['Z']['BI_audit_KRS']['rel']['nip']='nip';
  49. $tables['Z']['BI_audit_KRS']['rel']['regon']='regon';
  50. $tables['Z']['BI_audit_KRS']['rel']['krs']='krs';
  51. $tables['Z']['BI_audit_KRS']['rel_and']['adres']['miejscowosc']='A_miejscowosc';
  52. $tables['Z']['BI_audit_KRS']['rel_and']['adres']['ulica']='A_ulica';
  53. $tables['Z']['BI_audit_KRS']['rel_and']['adres']['kodPocztowy']='A_kod';
  54. $tables['Z']['BI_audit_KRS']['ref'][12]='BI_audit_KRS_person';
  55. $tables['Z']['BI_audit_KRS']['ref'][11]='BI_audit_KRS_company';
  56. $tables['Z']['BI_audit_KRS_company']['rel']['regon']='regon';
  57. $tables['Z']['BI_audit_KRS_company']['rel']['krs']='krs';
  58. $tables['Z']['BI_audit_KRS_company']['backref'][11]='BI_audit_KRS';
  59. function start_dig($tables) {
  60. global $db;
  61. foreach($tables['Y'] as $Y=>$t) {
  62. $sql='select * from '.$Y.' where ID=12 or ID=19 or 1=12 limit 100' ;
  63. $res=$db->query($sql);
  64. while($h=$db->fetch($res)) {
  65. $hist_loop[$Y][]=$h;
  66. $hist_time[][$Y]=$h;
  67. dig_next($hist_loop,$t,$h,1,$hist_time);
  68. unset($hist_time);
  69. }
  70. }
  71. }
  72. function dig_next($hist_loop,$prev_tbl,$prev_dig,$loop,$hist_time) {
  73. global $db,$tables;
  74. $loop++;
  75. echo "\n<hr>###LOOP ".$loop;
  76. if($loop<5) {
  77. //detect next ref tbls
  78. $sql = array();
  79. $sql_RELAND = array();
  80. foreach($tables['Z'] as $Z=>$z) {
  81. //rels in tbls
  82. foreach($z['rel'] as $z_rel_from=>$z_rel_to) {
  83. // echo "110 test z_rel_fr ".$z_rel_from." to z_rel_to ".$z_rel_to ;
  84. if( isset($prev_tbl['rel'][$z_rel_from])) {
  85. if( strlen($prev_dig->$prev_tbl['rel'][$z_rel_from]) > 1 ) {
  86. $sql[$Z]['rel'][$z_rel_to]= $prev_dig->$prev_tbl['rel'][$z_rel_from];
  87. }
  88. }
  89. }
  90. //search addr
  91. if(isset($z['rel_and'])) {
  92. foreach($z['rel_and'] as $z_rel_and=>$z_rel_and_name) {
  93. $rel_and_ok=1;
  94. foreach($z_rel_and_name as $z_rel_and_name_cur=>$z_rel_and_name_col_to) {
  95. if(isset($prev_dig->$z_rel_and_name_cur) and strlen($prev_dig->$z_rel_and_name_cur) > 2) {
  96. $rel_and_ok=2;
  97. // echo "<br>121 rel_and 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;
  98. }
  99. else {
  100. // echo "<br> #127 UNSET ".$prev_dig->$z_rel_and_name_cur." / ".$z_rel_and_name_col_to." ";
  101. $rel_and_ok=1;
  102. }
  103. }
  104. if($rel_and_ok==2) {
  105. //echo "<br>#133 Z=".$Z."/z=".$z." ok rel_and_ok=".$rel_and_ok;
  106. foreach($z_rel_and_name as $z_rel_and_name_cur=>$z_rel_and_name_col_to) {
  107. // echo "<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;
  108. $sql_rel_and[$Z][$z_rel_and][$z_rel_and_name_col_to]=$prev_dig->$z_rel_and_name_cur;
  109. }
  110. } else {
  111. // echo "<br>#137 error rel_and_ok=".$rel_and_ok;
  112. }
  113. }
  114. }
  115. }
  116. // echo "<br>135:sql_rel_and <pre>";
  117. // print_r($sql_rel_and);
  118. // echo " .EOF</pre>";
  119. if( isset($sql_rel_and)) {
  120. foreach($sql_rel_and as $T=>$z_rel_and_arr) {
  121. foreach($z_rel_and_arr as $z_rel_and_name=>$z_rel_and_name_col) {
  122. //echo "<br>156 z_rel_and_name=".$z_rel_and_name." ";
  123. $sql_rel_and_sql[$T][$z_rel_and_name]="select * from ".$T." where ( ";
  124. $and=0;
  125. foreach($z_rel_and_name_col as $z_rel_and_name_cur=>$z_rel_and_name_col_to) {
  126. if( $and==1) $sql_rel_and_sql[$T][$z_rel_and_name].=" and SOUNDEX(`".$z_rel_and_name_cur."`) = SOUNDEX('.".$z_rel_and_name_col_to."') and `".$z_rel_and_name_cur."` not like '%--%' " ;
  127. else $sql_rel_and_sql[$T][$z_rel_and_name].=" SOUNDEX(`".$z_rel_and_name_cur."`) = SOUNDEX('".$z_rel_and_name_col_to."') and `".$z_rel_and_name_cur."` not like '%--%' " ;
  128. $and=1;
  129. }
  130. $sql_rel_and_sql[$T][$z_rel_and_name].=" ) " ;
  131. if( isset($hist_loop[$T])) {
  132. foreach($hist_loop[$T] as $hist_rec) {
  133. $sql_rel_and_sql[$T][$z_rel_and_name].=" and ID!='".$hist_rec->ID."' ";
  134. }
  135. }
  136. $sql_rel_and_sql[$T][$z_rel_and_name].=" ; " ;
  137. dig_arr_cur($hist_loop,$T,$sql_rel_and_sql[$T][$z_rel_and_name],$hist_time,$loop);
  138. }
  139. }
  140. }
  141. //echo "<br>164:sql_rel_and_sql <pre>";
  142. // print_r($sql_rel_and_sql);
  143. // echo " .EOF</pre>";
  144. //X test
  145. //echo " <br> 129 prev <pre>";
  146. // print_r($prev_dig);
  147. // echo "<br> prevtbl";
  148. // print_r($prev_tbl);
  149. // echo "</pre>";
  150. foreach($tables['X'] as $X=>$x) {
  151. //rels in tbls
  152. foreach($x['rel'] as $x_rel_from=>$x_rel_to) {
  153. //echo "\n<br> #133test tbl ".$X." z_rel_fr ".$x_rel_from." to z_rel_to ".$x_rel_to." prev tbl " ;
  154. if( isset($prev_tbl['rel'][$x_rel_from]) ) { //isset($prev_dig->$prev_tbl['rel'][$x_rel_to])
  155. // 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].")
  156. if( isset($prev_dig->$prev_tbl['rel'][$x_rel_from]) and strlen($prev_dig->$prev_tbl['rel'][$x_rel_from]) > 1 ) { //
  157. $sql_X[$X]['rel'][$x_rel_to]= $prev_dig->$prev_tbl['rel'][$x_rel_from];
  158. }
  159. }
  160. }
  161. }
  162. flush();
  163. // echo "\n<br>sql_X<pre>";
  164. // print_r($sql_X);
  165. // echo "</pre>";
  166. if( isset($sql_X)) {
  167. foreach($sql_X as $T => $rel) {
  168. $sql_x[$T]=" select * from ".$T." where ( ";
  169. foreach($rel['rel'] as $col=>$val) {
  170. if( isset($sql_x_str[$T])) $sql_x_str[$T].=" or `".$col."` = '".$val."' " ;
  171. else $sql_x_str[$T]=" `".$col."` = '".$val."' " ;
  172. }
  173. $sql_x[$T].=$sql_x_str[$T]." ) ";
  174. //not loop
  175. if( isset($hist_loop[$T])) {
  176. foreach($hist_loop[$T] as $hist_rec) {
  177. $sql_t[$T].=" and ID!='".$hist_rec->ID."' ";
  178. }
  179. }
  180. }
  181. }
  182. // echo "<br> sql_x" ;
  183. //print_r($sql_x);
  184. if( isset($sql_x)) {
  185. foreach($sql_x as $X => $sql_ext) {
  186. echo "\n<br>174 Querying :".$sql_ext ;
  187. $res=$db->query($sql_ext) or die('175 blad');
  188. while($h=$db->fetch($res)) {
  189. echo "\n<hr> #177 POWIAZANIE ZNALEZIONE DLA X <pre>";
  190. print_r($h);
  191. echo "<br> Historia sledzen:";
  192. print_r($hist_time);
  193. echo "#177 eof </pre>";
  194. }
  195. }
  196. }
  197. //X test eof
  198. foreach($sql as $T => $rel) {
  199. $sql_t[$T]=" select * from ".$T." where ( ";
  200. foreach($rel['rel'] as $col=>$val) {
  201. if( isset($sql_t_str[$T])) $sql_t_str[$T].=" or `".$col."` = '".$val."' " ;
  202. else $sql_t_str[$T]=" `".$col."` = '".$val."' " ;
  203. }
  204. $sql_t[$T].=$sql_t_str[$T]." ) ";
  205. //not loop
  206. if( isset($hist_loop[$T])) {
  207. echo "<br> hist loop cnt: ". count($hist_loop[$T]);
  208. foreach($hist_loop[$T] as $hist_rec) {
  209. $sql_t[$T].=" and ID!='".$hist_rec->ID."' ";
  210. }
  211. }
  212. }
  213. // echo "<br> 202 hist loop cnt: <pre> ". count($hist_loop);
  214. // print_r($hist_loop);
  215. // echo "</pre>";
  216. echo "<pre>sql_t";
  217. print_r($sql);
  218. echo "<br>sql_t ";
  219. print_r($sql_t);
  220. echo "</pre>";
  221. if( isset($sql_t)) {
  222. foreach($sql_t as $T => $sql_ext) {
  223. dig_arr_cur($hist_loop,$T,$sql_ext,$hist_time,$loop);
  224. }
  225. }
  226. //todo look in Y and die()
  227. }
  228. echo "<br>#243 glebokosc:".$loop;
  229. }
  230. function dig_arr_cur($hist_loop,$T,$sql_ext,$hist_time,$loop) {
  231. global $db,$tables;
  232. echo "\n<br>216: Querying :T ".$T." ext: ".$sql_ext ;//$sql_ext
  233. $res=$db->query($sql_ext) or die('211 blad '.$sql_ext);
  234. while($h=$db->fetch($res)) {
  235. echo "\n<br> #149 found ".$h->ID;// print_r($h);
  236. $hist_loop[$T][]=$h;
  237. $hist_time[][$T]=$sql_ext;
  238. $hist_time[][$T]=$h;
  239. // print_r($hist_loop);
  240. dig_next($hist_loop, $tables['Z'][$T],$h,$loop,$hist_time);
  241. //rel dzieci $tables['Z']['BI_audit_CEIDG']['ref'][19]='BI_audit_CEIDG_pelnomocnicy';
  242. if(isset($tables['Z'][$T]['ref'])) {
  243. echo "<br>#296 try to ref tables for T ".$T."<pre>";
  244. print_r($tables['Z'][$T]['ref']);
  245. echo "</pre>";
  246. foreach($tables['Z'][$T]['ref'] as $ref_id => $ref_table) {
  247. $sql_ref=" select REMOTE_PRIMARY_KEY from `CRM__#REF_TABLE__".$ref_id."` where `PRIMARY_KEY`=".$h->ID." ";
  248. $res_rel=$db->query($sql_ref) or die('211 blad '.$sql_ref);
  249. echo "<br>#297 ".$sql_ref;
  250. while($h_rel=$db->fetch($res_rel)){
  251. $sql_rel_rec="select * from `".$ref_table."` where ID=".$h_rel->REMOTE_PRIMARY_KEY." ";
  252. $res_rel_rec=$db->query($sql_rel_rec) or die('211 blad '.$sql_rel_rec);
  253. echo "<br>#159 ref tbl ".$sql_rel_rec;
  254. while($h_rel_rec=$db->fetch($res_rel_rec)){
  255. echo "#315 fnd:".$h_rel_rec->ID;
  256. $hist_loop[$ref_table][]=$h_rel_rec;
  257. $hist_time[][$ref_table]=$sql_rel_rec;
  258. $hist_time[][$ref_table]=$h_rel_rec;
  259. dig_next($hist_loop,$tables['Z'][$ref_table],$h_rel_rec,$loop,$hist_time);
  260. }
  261. }
  262. }
  263. } else echo "#322 not ref to ".$T;
  264. if(isset($tables['Z'][$T]['backref'])) {
  265. echo "<br>#296 try to backref tables T ".$T."<pre>";
  266. print_r($tables['Z'][$T]['backref']);
  267. echo "</pre>";
  268. foreach($tables['Z'][$T]['backref'] as $ref_id => $ref_table) {
  269. $sql_ref=" select PRIMARY_KEY from `CRM__#REF_TABLE__".$ref_id."` where `REMOTE_PRIMARY_KEY`=".$h->ID." ";
  270. $res_rel=$db->query($sql_ref) or die('318 blad '.$sql_ref);
  271. echo "<br>#319 backref ".$sql_ref;
  272. while($h_rel=$db->fetch($res_rel)){
  273. $sql_rel_rec="select * from `".$ref_table."` where ID=".$h_rel->PRIMARY_KEY." ";
  274. $res_rel_rec=$db->query($sql_rel_rec) or die('211 blad '.$sql_rel_rec);
  275. echo "<br>#324 ref tbl ".$sql_rel_rec;
  276. while($h_rel_rec=$db->fetch($res_rel_rec)){
  277. echo "#315 fnd:".$h_rel_rec->ID;
  278. $hist_loop[$ref_table][]=$h_rel_rec;
  279. $hist_time[][$ref_table]=$sql_rel_rec;
  280. $hist_time[][$ref_table]=$h_rel_rec;
  281. dig_next($hist_loop,$tables['Z'][$ref_table],$h_rel_rec,$loop,$hist_time);
  282. }
  283. }
  284. }
  285. } else echo "#322 not backref to ".$T;
  286. }
  287. }
  288. start_dig($tables);
  289. echo "<hr>end of raport ";