audit_krs_con_digger.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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']['ref'][19]='BI_audit_CEIDG_pelnomocnicy';
  36. $tables['Z']['BI_audit_CEIDG']['ref'][18]='BI_audit_CEIDG_powiazania';
  37. $tables['Z']['BI_audit_CEIDG_pelnomocnicy']['rel']['nip']='nip';
  38. $tables['Z']['BI_audit_CEIDG_pelnomocnicy']['rel_and']['adres']['miejscowosc']='miejscowosc';
  39. $tables['Z']['BI_audit_CEIDG_pelnomocnicy']['rel_and']['adres']['ulica']='ulica';
  40. $tables['Z']['BI_audit_CEIDG_pelnomocnicy']['rel_and']['adres']['kodPocztowy']='kodPocztowy';
  41. //$tables['Z']['BI_audit_CEIDG_pelnomocnicy']['ref'][]='BI_audit_CEIDG';
  42. $tables['Z']['BI_audit_CEIDG_powiazania']['rel']['nip']='nip';
  43. $tables['Z']['BI_audit_CEIDG_powiazania']['rel']['regon']='nip';
  44. //$tables['Z']['BI_audit_CEIDG_powiazania']['ref'][]='BI_audit_CEIDG';
  45. $tables['Z']['BI_audit_KRS']['rel']['nip']='nip';
  46. $tables['Z']['BI_audit_KRS']['rel']['regon']='regon';
  47. $tables['Z']['BI_audit_KRS']['rel']['krs']='krs';
  48. $tables['Z']['BI_audit_KRS']['rel_and']['adres']['miejscowosc']='A_miejscowosc';
  49. $tables['Z']['BI_audit_KRS']['rel_and']['adres']['ulica']='A_ulica';
  50. $tables['Z']['BI_audit_KRS']['rel_and']['adres']['kodPocztowy']='A_kod';
  51. $tables['Z']['BI_audit_KRS']['ref'][12]='BI_audit_KRS_person';
  52. $tables['Z']['BI_audit_KRS']['ref'][11]='BI_audit_KRS_company';
  53. $tables['Z']['BI_audit_KRS_company']['rel']['regon']='regon';
  54. $tables['Z']['BI_audit_KRS_company']['rel']['krs']='krs';
  55. function start_dig($tables) {
  56. global $db;
  57. foreach($tables['Y'] as $Y=>$t) {
  58. $sql='select * from '.$Y.' limit 100' ;
  59. $res=$db->query($sql);
  60. while($h=$db->fetch($res)) {
  61. $hist_loop[$Y][]=$h;
  62. dig_next($hist_loop,$t,$h,1);
  63. }
  64. }
  65. }
  66. function dig_next($hist_loop,$prev_tbl,$prev_dig,$loop) {
  67. global $db,$tables;
  68. $loop++;
  69. echo "\n<hr>###LOOP ".$loop;
  70. if($loop>10) die(' loop '.$loop);
  71. //detect next ref tbls
  72. $sql = array();
  73. foreach($tables['Z'] as $Z=>$z) {
  74. //rels in tbls
  75. foreach($z['rel'] as $z_rel_from=>$z_rel_to) {
  76. // echo "110 test z_rel_fr ".$z_rel_from." to z_rel_to ".$z_rel_to ;
  77. if( isset($prev_tbl['rel'][$z_rel_from])) {
  78. if( strlen($prev_dig->$prev_tbl['rel'][$z_rel_from]) > 1 ) {
  79. $sql[$Z]['rel'][$z_rel_to]= $prev_dig->$prev_tbl['rel'][$z_rel_from];
  80. }
  81. }
  82. /*foreach($prev_dig as $col=>$val) {
  83. //transl ref to ref
  84. if( $prev_tbl['rel'][$z_rel_from] ) {
  85. $sql[$Z]['rel'][$z_rel_to]=$val;
  86. }
  87. }
  88. */
  89. }
  90. }
  91. //X test
  92. //echo " <br> 129 prev <pre>";
  93. // print_r($prev_dig);
  94. // echo "<br> prevtbl";
  95. // print_r($prev_tbl);
  96. // echo "</pre>";
  97. foreach($tables['X'] as $X=>$x) {
  98. //rels in tbls
  99. foreach($x['rel'] as $x_rel_from=>$x_rel_to) {
  100. //echo "\n<br> #133test tbl ".$X." z_rel_fr ".$x_rel_from." to z_rel_to ".$x_rel_to." prev tbl " ;
  101. if( isset($prev_tbl['rel'][$x_rel_from]) ) { //isset($prev_dig->$prev_tbl['rel'][$x_rel_to])
  102. // 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].")
  103. if( isset($prev_dig->$prev_tbl['rel'][$x_rel_from]) and strlen($prev_dig->$prev_tbl['rel'][$x_rel_from]) > 1 ) { //
  104. $sql_X[$X]['rel'][$x_rel_to]= $prev_dig->$prev_tbl['rel'][$x_rel_from];
  105. }
  106. }
  107. }
  108. }
  109. flush();
  110. // echo "\n<br>sql_X<pre>";
  111. // print_r($sql_X);
  112. // echo "</pre>";
  113. foreach($sql_X as $T => $rel) {
  114. $sql_x[$T]=" select * from ".$T." where ( ";
  115. foreach($rel['rel'] as $col=>$val) {
  116. if( isset($sql_x_str[$T])) $sql_x_str[$T].=" or `".$col."` = '".$val."' " ;
  117. else $sql_x_str[$T]=" `".$col."` = '".$val."' " ;
  118. }
  119. $sql_x[$T].=$sql_x_str[$T]." ) ";
  120. //not loop
  121. if( isset($hist_loop[$T])) {
  122. foreach($hist_loop[$T] as $hist_rec) {
  123. $sql_t[$T].=" and ID!='".$hist_rec->ID."' ";
  124. }
  125. }
  126. }
  127. // echo "<br> sql_x" ;
  128. //print_r($sql_x);
  129. if( isset($sql_x)) {
  130. foreach($sql_x as $X => $sql_ext) {
  131. echo "\n<br>174 Querying :".$sql_ext ;
  132. $res=$db->query($sql_ext) or die('175 blad');
  133. while($h=$db->fetch($res)) {
  134. echo "\n<hr> #177 POWIAZANIE ZNALEZIONE DLA <pre>";
  135. print_r($h);
  136. print_r($hist_loop);
  137. echo "#177 eof </pre>";
  138. }
  139. }
  140. }
  141. //X test eof
  142. foreach($sql as $T => $rel) {
  143. $sql_t[$T]=" select * from ".$T." where ( ";
  144. foreach($rel['rel'] as $col=>$val) {
  145. if( isset($sql_t_str[$T])) $sql_t_str[$T].=" or `".$col."` = '".$val."' " ;
  146. else $sql_t_str[$T]=" `".$col."` = '".$val."' " ;
  147. }
  148. $sql_t[$T].=$sql_t_str[$T]." ) ";
  149. //not loop
  150. if( isset($hist_loop[$T])) {
  151. echo "<br> hist loop cnt: ". count($hist_loop[$T]);
  152. foreach($hist_loop[$T] as $hist_rec) {
  153. $sql_t[$T].=" and ID!='".$hist_rec->ID."' ";
  154. }
  155. }
  156. echo "<br> 202 hist loop cnt: <pre> ". count($hist_loop);
  157. print_r($hist_loop);
  158. echo "</pre>";
  159. }
  160. echo "<pre>sql";
  161. //print_r($sql);
  162. // echo "<br>sql_t ";
  163. print_r($sql_t);
  164. echo "</pre>";
  165. if( isset($sql_t)) {
  166. foreach($sql_t as $T => $sql_ext) {
  167. echo "\n<br>Querying :".$sql_ext ;
  168. $res=$db->query($sql_ext) or die('211 blad '.$sql_ext);
  169. while($h=$db->fetch($res)) {
  170. echo "\n<br> #149 found "; print_r($h);
  171. $hist_loop[$tables['Z'][$T]][]=$h;
  172. // print_r($hist_loop);
  173. dig_next($hist_loop, $tables['Z'][$T],$h,$loop);
  174. //rel dzieci
  175. foreach($tables[$T]['ref'] as $ref_id => $ref_table) {
  176. $sql_ref=" select REMOTE_PRIMARY_KEY from `CRM__#REF_TABLE__".$ref_id."` where `PRIMARY_KEY`=".$h->ID." ";
  177. $res_rel=$db->query($sql_ref) or die('211 blad '.$sql_ref);
  178. while($h_rel=$db->fetch($res_rel)){
  179. $sql_rel_rec="select * from `".$ref_table."` where ID=".$h_rel->REMOTE_PRIMARY_KEY." ";
  180. $res_rel_rec=$db->query($sql_rel_rec) or die('211 blad '.$sql_rel_rec);
  181. echo "<br>#159 ref tbl ".$sql_rel_rec;
  182. while($h_rel_rec=$db->fetch($res_rel_rec)){
  183. dig_next($hist_loop,$tables['Z'][$ref_table],$h_rel_rec,$loop);
  184. }
  185. }
  186. }
  187. }
  188. }
  189. }
  190. //todo look in Y and die()
  191. }
  192. start_dig($tables);