1_rum_create_ref.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #!/usr/bin/env php
  2. <?php
  3. $_SERVER['SERVER_NAME'] = "biuro.biall-net.pl";
  4. require("/Library/Server/Web/Data/Sites/Default/dev-bzyk/se-lib/bootstrap.php");
  5. #require("/Library/Server/Web/Data/Sites/SE-producition-git/SE/se-lib/bootstrap.php");
  6. date_default_timezone_set('Europe/Warsaw');// PHP 5 >= 5.1.0 required by date functions
  7. function is_nip($nip) {
  8. $waga = [6, 5, 7, 2, 3, 4, 5, 6, 7];
  9. $c = 0;
  10. for ($i = 0; $i < 9; $i++) $c += $nip[$i] * $waga[$i];
  11. $c = ($c % 11) % 10;
  12. if ($nip[9] == $c) return true;
  13. else return false;
  14. }
  15. function is_regon($regon) {
  16. $waga = [8, 9, 2, 3, 4, 5, 6, 7];
  17. $c = 0;
  18. for ($i = 0; $i < 8; $i++) $c += $regon[$i] * $waga[$i];
  19. $c = ($c % 11) % 10;
  20. if ($regon[8] == $c) return true;
  21. else return false;
  22. }
  23. function parseNipRegon($string) {
  24. preg_match('/^(.*[^[:digit:]])?([[:digit:]]{10})([^[:digit:]].*)?$/', str_replace('-', '', $string), $matches);
  25. if ($matches && is_nip($matches[2])) $nip = $matches[2];
  26. else $nip = null;
  27. preg_match('/^(.*[^[:digit:]])?([[:digit:]]{9})([^[:digit:]].*)?$/', str_replace('-', '', $string), $matches);
  28. if ($matches && is_regon($matches[2])) $regon = $matches[2];
  29. else $regon = null;
  30. return [$nip, $regon];
  31. }
  32. $fields['BI_audit_ENERGA_RUM_KONTRAHENCI'] = [
  33. 'Tytul_dokumentu',
  34. 'Pelna_nazwa_kontrahenta',
  35. 'NIP',
  36. 'REGON',
  37. 'KRS',
  38. 'Kraj',
  39. 'Kod_pocztowy',
  40. 'Miejscowosc',
  41. 'Ulica',
  42. 'Numer_budynku',
  43. 'Numer_mieszkania_lokalu',
  44. 'TERYT_SYM',
  45. 'TERYT_SYM_UL',
  46. ];
  47. $fields['BI_audit_KRS'] = [
  48. '"Dodano z BI_audit_KRS"',
  49. 'nazwa',
  50. 'nip',
  51. 'regon',
  52. 'krs',
  53. 'A_kraj',
  54. 'A_kod',
  55. 'A_miejscowosc',
  56. 'A_ulica',
  57. 'A_nrDomu',
  58. 'A_nrLokalu',
  59. 'TERYT_SYM',
  60. 'TERYT_SYM_UL',
  61. ];
  62. $fields['BI_audit_CEIDG'] = [
  63. '"Dodano z BI_audit_CEIDG"',
  64. 'firma',
  65. 'nip',
  66. 'regon',
  67. 'null',
  68. 'kraj',
  69. 'kodPocztowy',
  70. 'miejscowosc',
  71. 'ulica',
  72. 'budynek',
  73. 'lokal',
  74. 'TERYT_SYM',
  75. 'TERYT_SYM_UL',
  76. ];
  77. DB::getPDO()->query("truncate table `CRM__#REF_TABLE__23`");
  78. $query = "select ID, Strony_umowy from `BI_audit_ENERGA_RUM_UMOWY`";
  79. $result = DB::query($query);
  80. while ($row = DB::fetch($result)) {
  81. $ID = $row->ID;
  82. $strony = array_map('trim', explode("|", $row->Strony_umowy));
  83. $K_IDs = [];
  84. foreach ($strony as $item) {
  85. $strona = array_map('trim', explode(";", $item));
  86. $nr = null;
  87. if (count($strona) == 3) $i = 1;//$nr = $strona[1];
  88. elseif (count($strona) == 4) $i = 2;//$nr = $strona[2];
  89. elseif (count($strona) >= 5 && count($strona) <= 6) $i = 3;//$nr = $strona[3];
  90. else $i = null;
  91. if ($i) $nr = $strona[$i];
  92. $notFound = false;
  93. if (is_numeric($nr)) {
  94. $query = "select ID from BI_audit_ENERGA_RUM_KONTRAHENCI where Numer_kontrahenta = '{$nr}'";
  95. if ($K_ID = DB::getPDO()->fetchValue($query)) {
  96. $K_IDs[] = $K_ID;
  97. } else {
  98. $nazwa = $strona[$i+1];
  99. $query = "select ID from BI_audit_ENERGA_RUM_KONTRAHENCI where Pelna_nazwa_kontrahenta = '" . addslashes($nazwa) . "'";
  100. if ($result2 = DB::getPDO()->fetchall($query)) {
  101. foreach ($result2 as $item) $K_IDs[] = $item['ID'];
  102. } else $notFound = true;
  103. }
  104. } else $notFound = true;
  105. if ($notFound) {
  106. list($nip, $regon) = parseNipRegon($item);
  107. if ($nip || $regon) {
  108. $query = "select ID from BI_audit_ENERGA_RUM_KONTRAHENCI where NIP = '{$nip}' or REGON = '{$regon}'";
  109. if ($result2 = DB::getPDO()->fetchall($query)) {
  110. foreach ($result2 as $item) $K_IDs[] = $item['ID'];
  111. } else {
  112. $query = "select ID from BI_audit_KRS where nip = '{$nip}' or regon = '{$regon}'";
  113. if ($result2 = DB::getPDO()->fetchall($query)) {
  114. foreach ($result2 as $item) {
  115. $query = "insert into BI_audit_ENERGA_RUM_KONTRAHENCI (" . implode(', ', $fields['BI_audit_ENERGA_RUM_KONTRAHENCI']) . ")" .
  116. " select " . implode(', ', $fields['BI_audit_KRS']) . " from BI_audit_KRS where ID = '{$item['ID']}'";
  117. if (DB::getPDO()->query($query)) $K_IDs[] = DB::getPDO()->lastInsertId();
  118. }
  119. } else {
  120. $query = "select ID from BI_audit_CEIDG where nip = '{$nip}' or regon = '{$regon}'";
  121. if ($result2 = DB::getPDO()->fetchall($query)) {
  122. foreach ($result2 as $item) {
  123. $query = "insert into BI_audit_ENERGA_RUM_KONTRAHENCI (" . implode(', ', $fields['BI_audit_ENERGA_RUM_KONTRAHENCI']) . ")" .
  124. " select " . implode(', ', $fields['BI_audit_CEIDG']) . " from BI_audit_CEIDG where ID = '{$item['ID']}'";
  125. if (DB::getPDO()->query($query)) $K_IDs[] = DB::getPDO()->lastInsertId();
  126. }
  127. }
  128. }
  129. }
  130. }
  131. }
  132. }
  133. foreach ($K_IDs as $K_ID) {
  134. $query = "insert into `CRM__#REF_TABLE__23` (PRIMARY_KEY, REMOTE_PRIMARY_KEY) values ('{$ID}', '{$K_ID}')";
  135. DB::getPDO()->query($query);
  136. }
  137. }