1_rum_create_ref.php 4.6 KB

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