ソースを参照

Dodanie do BiAuditGenerate parsowanie umów Energa + bug fixy

Mariusz Muszyński 8 年 前
コミット
cea3ed0dfa
1 ファイル変更186 行追加4 行削除
  1. 186 4
      SE/se-lib/Route/UrlAction/BiAuditGenerate.php

+ 186 - 4
SE/se-lib/Route/UrlAction/BiAuditGenerate.php

@@ -57,6 +57,20 @@ class Route_UrlAction_BiAuditGenerate extends RouteBase {
 		return $folderConf['mount_point'];
 	}
 
+	private static function parseNipRegon($string) {
+		$nip = null; $regon = null;
+
+		preg_match('/^(.*[^[:digit:]])?([[:digit:]]{10})([^[:digit:]].*)?$/', str_replace('-', '', $string), $matches);
+		if (!empty($matches[2])) $nip = trim($matches[2]);
+		if ($nip && !V::isNip($nip)) $nip = null;
+
+		preg_match('/^(.*[^[:digit:]])?([[:digit:]]{9})([^[:digit:]].*)?$/', str_replace('-', '', $string), $matches);
+		if (!empty($matches[2])) $regon = trim($matches[2]);
+		if ($regon && !V::isRegon($regon)) $regon = null;
+
+		return [$nip, $regon];
+	}
+
 	private function initializePowiazaniaForm() {
 ?>
 <div class="container" style="margin-top:20px">
@@ -1199,6 +1213,169 @@ Router::getRoute("UrlAction_BiAuditGenerate")->' . $function . ';
 		}
 	}
 
+	private static function reloadCache_parseUmowy($full = false) {
+		$fields = [
+			'BI_audit_ENERGA_RUM_KONTRAHENCI' => [
+				'Tytul_dokumentu',
+				'Pelna_nazwa_kontrahenta',
+				'NIP',
+				'REGON',
+				'KRS',
+				'Kraj',
+				'Kod_pocztowy',
+				'Miejscowosc',
+				'Ulica',
+				'Numer_budynku',
+				'Numer_mieszkania_lokalu',
+				'TERYT_SYM',
+				'TERYT_SYM_UL',
+			],
+			'BI_audit_KRS' => [
+				'"Zaimportowanoz KRS na podstawie umowy"',
+				'nazwa',
+				'nip',
+				'regon',
+				'krs',
+				'A_kraj',
+				'A_kod',
+				'A_miejscowosc',
+				'A_ulica',
+				'A_nrDomu',
+				'A_nrLokalu',
+				'TERYT_SYM',
+				'TERYT_SYM_UL',
+			],
+			'BI_audit_CEIDG' => [
+				'"Zaimportowano z CEiDG na podstawie umowy"',
+				'firma',
+				'nip',
+				'regon',
+				'null',
+				'kraj',
+				'kodPocztowy',
+				'miejscowosc',
+				'ulica',
+				'budynek',
+				'lokal',
+				'TERYT_SYM',
+				'TERYT_SYM_UL',
+			],
+		];
+
+		$refTable = ACL::getRefTable('default_db/BI_audit_ENERGA_RUM_UMOWY/BI_audit_ENERGA_RUM_UMOWY', 'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI:BI_audit_ENERGA_RUM_KONTRAHENCI');
+		if ($full) {
+			DB::getPDO()->query("truncate table `{$refTable}`");
+			DB::getPDO()->query("update `BI_audit_ENERGA_RUM_UMOWY` set `A_STATUS` = 'WAITING' where `A_STATUS` = 'NORMAL'");
+		}
+
+		$query = "select `ID`, `Strony_umowy` from `BI_audit_ENERGA_RUM_UMOWY` where `A_STATUS` = 'WAITING'";
+		$result = DB::getPDO()->fetchAll($query);
+		foreach ($result as $row) {
+			$ID = $row['ID'];
+			$strony = array_map('trim', explode("|", $row['Strony_umowy']));
+			$K_IDs = [];
+
+			foreach ($strony as $item) {
+				$strona = array_map('trim', explode(";", $item));
+				$nr = null;
+				if (count($strona) == 3) $i = 1;//$nr = $strona[1];
+				elseif (count($strona) == 4) $i = 2;//$nr = $strona[2];
+				elseif (count($strona) >= 5 && count($strona) <= 6) $i = 3;//$nr = $strona[3];
+				else $i = null;
+				if ($i) $nr = $strona[$i];
+				$notFound = false;
+				if (is_numeric($nr)) {
+					$query = "select ID from BI_audit_ENERGA_RUM_KONTRAHENCI where Numer_kontrahenta = '{$nr}'";
+					if ($K_ID = DB::getPDO()->fetchValue($query)) {
+						$K_IDs[] = $K_ID;
+					} else {
+						$nazwa = $strona[$i+1];
+						$query = "select ID from BI_audit_ENERGA_RUM_KONTRAHENCI where Pelna_nazwa_kontrahenta = '" . addslashes($nazwa) . "'";
+						if ($result2 = DB::getPDO()->fetchAll($query)) {
+							foreach ($result2 as $item) $K_IDs[] = $item['ID'];
+						} else $notFound = true;
+					}
+				} else $notFound = true;
+				if ($notFound) {
+					list($nip, $regon) = self::parseNipRegon($item);
+					if ($nip || $regon) {
+						$query = "select ID from BI_audit_ENERGA_RUM_KONTRAHENCI where NIP = '{$nip}' or REGON = '{$regon}'";
+						if ($result2 = DB::getPDO()->fetchall($query)) {
+							foreach ($result2 as $item) $K_IDs[] = $item['ID'];
+						} else {
+							$query = "select ID from BI_audit_KRS where nip = '{$nip}' or regon = '{$regon}'";
+							if ($result2 = DB::getPDO()->fetchall($query)) {
+								foreach ($result2 as $item) {
+									$query = "insert into BI_audit_ENERGA_RUM_KONTRAHENCI (" . implode(', ', $fields['BI_audit_ENERGA_RUM_KONTRAHENCI']) . ")" .
+										" select " . implode(', ', $fields['BI_audit_KRS']) . " from BI_audit_KRS where ID = '{$item['ID']}'";
+									if (DB::getPDO()->query($query)) $K_IDs[] = DB::getPDO()->lastInsertId();
+								}
+							} else {
+								$query = "select ID from BI_audit_CEIDG where nip = '{$nip}' or regon = '{$regon}'";
+								if ($result2 = DB::getPDO()->fetchall($query)) {
+									foreach ($result2 as $item) {
+										$query = "insert into BI_audit_ENERGA_RUM_KONTRAHENCI (" . implode(', ', $fields['BI_audit_ENERGA_RUM_KONTRAHENCI']) . ")" .
+											" select " . implode(', ', $fields['BI_audit_CEIDG']) .
+											" from BI_audit_CEIDG where ID = '{$item['ID']}'";
+										if (DB::getPDO()->query($query)) $K_IDs[] = DB::getPDO()->lastInsertId();
+									}
+								}
+							}
+						}
+					}
+				}
+			}
+
+			foreach ($K_IDs as $K_ID) {
+				if (!(DB::getPDO()->fetchValue("select count(*) from `{$refTable}` where `PRIMARY_KEY` = {$ID} and `REMOTE_PRIMARY_KEY` = {$K_ID}"))) {
+					DB::getPDO()->query("insert into `{$refTable}` (PRIMARY_KEY, REMOTE_PRIMARY_KEY) values ({$ID}, {$K_ID})");
+				}
+			}
+			DB::getPDO()->update('BI_audit_ENERGA_RUM_UMOWY', "ID", $ID, ['A_STATUS' => 'NORMAL']);
+		}
+
+		$query = "select ID, NIP, REGON, KRS from BI_audit_ENERGA_RUM_KONTRAHENCI where (NIP is null or REGON is null or KRS is null) and not (NIP is null and REGON is null and KRS is null) and `cached` = 0";
+		$result = DB::getPDO()->fetchAll($query);
+		foreach ($result as $row) {
+			$query = "select nip, regon, krs from BI_audit_KRS where nip = '{$row['NIP']}' or regon = '{$row['REGON']}' or krs = '{$row['KRS']}'";
+			if ($items = DB::getPDO()->fetchAll($query)) {
+				list($nip, $regon, $krs) = [$items[0]["nip"], $items[0]["regon"], $items[0]["krs"]];
+				if (count($items) > 1) {
+					for ($i = 1; $i < count($items); $i++) {
+						if ($nip != $items[$i]["nip"]) $nip = null;
+						if ($regon != $items[$i]["regon"]) $regon = null;
+						if ($krs != $items[$i]["krs"]) $krs = null;
+					}
+				}
+
+				$set = [];
+				if ($nip && $nip != $row['NIP']) $set[] = "NIP = '{$nip}'";
+				if ($regon && $regon != $row['REGON']) $set[] = "REGON = '{$regon}'";
+				if ($krs && $krs != $row['KRS']) $set[] = "KRS = '{$krs}'";
+				if ($set) DB::getPDO()->query("update BI_audit_ENERGA_RUM_KONTRAHENCI set " . implode(', ', $set) . " where ID = '{$row['ID']}'");
+			}
+		}
+
+		$query = "select ID, NIP, REGON from BI_audit_ENERGA_RUM_KONTRAHENCI where (NIP is null or REGON is null) and not (NIP is null and REGON is null) and `cached` = 0";
+		$result = DB::getPDO()->fetchAll($query);
+		foreach ($result as $row) {
+			$query = "select nip, regon from BI_audit_CEIDG where nip = '{$row['NIP']}' or regon = '{$row['REGON']}'";
+			if ($items = DB::getPDO()->fetchAll($query)) {
+				list($nip, $regon) = [$items[0]["nip"], $items[0]["regon"]];
+				if (count($items) > 1) {
+					for ($i = 1; $i < count($items); $i++) {
+						if ($nip != $items[$i]["nip"]) $nip = null;
+						if ($regon != $items[$i]["regon"]) $regon = null;
+					}
+				}
+				$set = [];
+				if ($nip && $nip != $row['NIP']) $set[] = "NIP = '{$nip}'";
+				if ($regon && $regon != $row['REGON']) $set[] = "REGON = '{$regon}'";
+				if ($set) DB::getPDO()->query("update BI_audit_ENERGA_RUM_KONTRAHENCI set " . implode(', ', $set) . " where ID = '{$row->ID}'");
+			}
+		}
+	}
+
 	private function reloadCache_reTeryt($full = false) {
 
 		Lib::loadClass('Teryt');
@@ -1308,7 +1485,7 @@ Router::getRoute("UrlAction_BiAuditGenerate")->' . $function . ';
 		}
 	}
 
-	private function reloadCache_updateAll($full, $onlyBase) {
+	private static function reloadCache_updateAll($full, $onlyBase) {
 		$tablesConf = [
 			'BI_audit_CEIDG' => [
 				'fields' => [
@@ -1552,22 +1729,27 @@ Router::getRoute("UrlAction_BiAuditGenerate")->' . $function . ';
 	}
 
 	public function doReloadCache($full = false) {
-		if ($full == 'base') {
+		if ($full === 'base') {
 			$onlyBase = true;
 			$full = false;
 		} else {
 			$onlyBase = false;
 		}
+
 		$powiazaniaDirLocation = $this->getMainDirectory();
 		$tasksDirLocation = $powiazaniaDirLocation . "/.tasks";
 		$reloadCachePidFile = $tasksDirLocation . "/reloadCache.pid";
 		$reloadCacheResultFile = $tasksDirLocation . "/reloadCache.result";
 		file_put_contents($reloadCachePidFile, getmypid());
 		try {
-			$this->reloadCache_reTeryt($full);
-			$this->reloadCache_updateAll($full, $onlyBase);
+//			self::reloadCache_reTeryt($full);
+			self::reloadCache_parseUmowy($full);
+			self::reloadCache_updateAll($full, $onlyBase);
+//			echo "updateall ok\n";
 			file_put_contents($reloadCacheResultFile, "ok");
 		} catch (Exception $e) {
+			echo "\n{$e->getMessage()}\n";
+			var_dump($e);
 			file_put_contents($reloadCacheResultFile, $e->getMessage());
 		}
 	}