Преглед изворни кода

Poprawki + dodanie refów dla Benforda

Mariusz Muszyński пре 7 година
родитељ
комит
cf9c8fdab9
1 измењених фајлова са 110 додато и 71 уклоњено
  1. 110 71
      SE/se-lib/Route/UrlAction/BiAuditGenerate.php

+ 110 - 71
SE/se-lib/Route/UrlAction/BiAuditGenerate.php

@@ -1307,7 +1307,7 @@ function validateCompany(source) {
 td {font-size: 12px;}
 #benford ::selection {background: transparent;}
 #details {display:none; position: fixed; left: 0; top: 0; width: 100%; height: 100%; text-align: center; z-index: 1000; background-color: rgba(0,0,0,0.5);}
-#details div {width: 500px; height: auto; margin: 100px auto; background: #fff; padding: 10px; text-align: center; overflow: hidden; border-radius: 10px; box-shadow: 10px 10px rgba(0,0,0,0.25);}
+#details div {width: 500px; height: auto; margin: 100px auto; background: #fff; padding: 10px; text-align: center; overflow: hidden; border-radius: 5px; box-shadow: 0 0 7px 7px rgba(0,0,0,0.25);}
 -->
 </style>
 <script language="JavaScript">
@@ -1417,84 +1417,123 @@ function toggleCheckSecond(check) {
 	}
 
 	private function benford_generate() {
-		$benford = @unserialize(@gzuncompress(V::get('benford', '', $_SESSION)[$this->SOURCE['TABLE']]['data']));
-		unset($_SESSION['benford'][$this->SOURCE['TABLE']]);
-		if (!$benford) {
-			$this->benford_form(true);
-			return;
-		}
+		$namespace = function($table, $field = null) {
+			if ($field === null) return "default_db__x3A__{$table}";
+			return "default_db__x3A__{$table}:{$field}";
+		};
+		$arrayWithNamespace = function($table, $array) use ($namespace) {
+			$keys = array_map(function($key) use ($table, $namespace) {
+				return $namespace($table, $key);
+			}, array_keys($array));
+			return array_combine($keys, $array);
+		};
 
-		$root = 'RelatedFeatureRoot';
-		$namespace = 'BI_audit_BENFORD';
-		$array = [];
-		$array['@attributes'] = ['xmlns:system_cache__dita' => 'http://biuro.biall-net.pl/xmlschema_procesy5/default_db_xml_cache/dita.xsd'];
-		$array[$namespace] = [
-			'@attributes' => ['fid' => "{$namespace}.1"],
-			'ID' => 1,
-			'L_APPOITMENT_INFO' => V::get('L_APPOITMENT_INFO', '', $_POST),
-			'step' => V::get('step', '', $_POST),
-			'table' => $this->SOURCE['TABLE'],
-			'valueField' => $benford['conf']['valueField'],
-			'groupField' => $benford['conf']['groupField'],
-			'count' => $benford['conf']['count'],
-		];
-		$checked = [
-			'firstStep' => V::get('checkFirstStep', [], $_POST),
-			'secondStep' => V::get('checkSecondStep', [], $_POST),
-		];
+		$benfordId = null;
+		try {
+			$benford = @unserialize(@gzuncompress(V::get('benford', '', $_SESSION)[$this->SOURCE['TABLE']]['data']));
+			unset($_SESSION['benford'][$this->SOURCE['TABLE']]);
+			if (!$benford) {
+				$this->benford_form(true);
+				return;
+			}
+
+			$mainTable = 'BI_audit_BENFORD';
 
-		$firstStep = [];
-		for ($ka = 1; $ka <= 9; $ka++) {
-			if (!isset($benford['firstStep'][$ka])) $va = ['count' => 0, 'value' => 0, 'correct' => true];
-			else $va = $benford['firstStep'][$ka];
-			$_firstStep = [
-				'@attributes' => ['leadingNumber' => $ka],
-				'count' => $va['count'],
-				'value' => (double) round($va['value'], 5),
-				'norm' => (double) round($va['norm'], 5),
-				'deviation' => (double) round($va['value'] / $va['norm'] - 1, 5),
-				'correct' => $va['correct'] ? 'true' : 'false',
+			if (!($refTable = BiAuditRefTables::getRefTable($mainTable, $this->SOURCE['TABLE'], false))) throw new Exception("Nie znaleziono relacji do obiektu \"{$this->SOURCE['TABLE']}\" w konfiguracji obiektu \"{$mainTable}\" - nie można wygenerować raportu.");
+
+
+			$sqlArr = [
+				'step' => V::get('step', '', $_POST),
+				'table' => $this->SOURCE['TABLE'],
+				'valueField' => $benford['conf']['valueField'],
+				'groupField' => $benford['conf']['groupField'],
+				'count' => $benford['conf']['count'],
+				'L_APPOITMENT_INFO' => V::get('L_APPOITMENT_INFO', '', $_POST),
+				'A_RECORD_CREATE_AUTHOR' => User::getName(),
+				'A_RECORD_CREATE_DATE' => date('Y-m-d-H:i'),
 			];
-			if ($array[$namespace]['step'] == 'second' && (!$va['correct'])) {
-				$secondStep = [];
-				for ($kb = 0; $kb <= 9; $kb++) {
-					if (!isset($benford['secondStep'][$ka][$kb])) $vb = ['count' => 0, 'value' => 0, 'correct' => true];
-					else $vb = $benford['secondStep'][$ka][$kb];
-					$_secondStep = [
-						'@attributes' => ['leadingNumber' => $ka.$kb],
-						'count' => $vb['count'],
-						'value' => (double) round($vb['value'], 5),
-						'norm' => (double) round($vb['norm'], 5),
-						'deviation' => (double) round($vb['value'] / $vb['norm'] - 1, 5),
-						'correct' => $vb['correct'] ? 'true' : 'false',
-					];
-					if ((!$vb['correct']) && in_array($ka.$kb, $checked['secondStep'])) {
-						$objects = [];
-						foreach ($benford['secondStep'][$ka][$kb]['keys'] as $id) {
-							$object = DB::getPDO()->fetchFirst("select * from `{$this->SOURCE['TABLE']}` where `ID` = " . DB::getPDO()->quote($id));
-							if (!$object) continue;
-							$objects[] = array_merge(['@attributes' => ['fid' => "{$this->SOURCE['TABLE']}.{$id}"]], $object);
+			if (!($benfordId = DB::getPDO()->insert($mainTable, $sqlArr))) throw new Exception("Nie wygenerowano raportu - Wystąpił nieznany błąd podczas zapisywania raportu do bazy");
+
+			$dir = self::getDirectory($mainTable, $benfordId);
+			if (!file_exists($dir)) mkdir($dir, 0770, true);
+			if (!is_dir($dir)) throw new Exception("Nie wygenerowano raportu - wystąpił błąd podczas tworzenia katalogu dla rekordu");
+
+			$root = 'RelatedFeatureRoot';
+			$array = [];
+			$array['@attributes'] = [
+				'xmlns' => 'http://www.opengis.net/wfs',
+				'xmlns:system_cache__dita' => 'http://biuro.biall-net.pl/xmlschema_procesy5/default_db_xml_cache/dita.xsd',
+				'xmlns:'.$namespace($mainTable) => 'https://biuro.biall-net.pl/wfs',
+				'xmlns:'.$namespace($this->SOURCE['TABLE']) => 'https://biuro.biall-net.pl/wfs'
+			];
+			$array[$namespace($mainTable, $mainTable)] = array_merge(['@attributes' => ['fid' => "{$mainTable}.{$benfordId}", 'p5:primaryKey' => $benfordId]], $arrayWithNamespace($mainTable, array_merge(['ID' => $benfordId], $sqlArr)));
+echo "<pre>";
+print_r($array);
+echo "</pre>";
+			$checked = [
+				'firstStep' => V::get('checkFirstStep', [], $_POST),
+				'secondStep' => V::get('checkSecondStep', [], $_POST),
+			];
+
+			$firstStep = [];
+			for ($ka = 1; $ka <= 9; $ka++) {
+				if (!isset($benford['firstStep'][$ka])) $va = ['count' => 0, 'value' => 0, 'correct' => true];
+				else $va = $benford['firstStep'][$ka];
+				$_firstStep = [
+					'@attributes' => ['leadingNumber' => $ka],
+					'count' => $va['count'],
+					'value' => (double) round($va['value'], 5),
+					'norm' => (double) round($va['norm'], 5),
+					'deviation' => (double) round($va['value'] / $va['norm'] - 1, 5),
+					'correct' => $va['correct'] ? 'true' : 'false',
+				];
+				if ($array[$namespace($mainTable, $mainTable)]['step'] == 'second' && (!$va['correct'])) {
+					$secondStep = [];
+					for ($kb = 0; $kb <= 9; $kb++) {
+						if (!isset($benford['secondStep'][$ka][$kb])) $vb = ['count' => 0, 'value' => 0, 'correct' => true];
+						else $vb = $benford['secondStep'][$ka][$kb];
+						$_secondStep = [
+							'@attributes' => ['leadingNumber' => $ka.$kb],
+							'count' => $vb['count'],
+							'value' => (double) round($vb['value'], 5),
+							'norm' => (double) round($vb['norm'], 5),
+							'deviation' => (double) round($vb['value'] / $vb['norm'] - 1, 5),
+							'correct' => $vb['correct'] ? 'true' : 'false',
+						];
+						if ((!$vb['correct']) && in_array($ka.$kb, $checked['secondStep'])) {
+							$objects = [];
+							foreach ($benford['secondStep'][$ka][$kb]['keys'] as $id) {
+								DB::getPDO()->insert($refTable, ['PRIMARY_KEY' => $benfordId, 'REMOTE_PRIMARY_KEY' => $id]);
+								$object = DB::getPDO()->fetchFirst("select * from `{$this->SOURCE['TABLE']}` where `ID` = " . DB::getPDO()->quote($id));
+								if (!$object) continue;
+								$objects[] = array_merge(['@attributes' => ['fid' => "{$this->SOURCE['TABLE']}.{$id}", 'p5:primaryKey' => $id]], $arrayWithNamespace($this->SOURCE['TABLE'], $object));
+							}
+							$_secondStep[$this->SOURCE['TABLE']][] = $objects;
 						}
-						$_secondStep[$this->SOURCE['TABLE']][] = $objects;
+						$secondStep[] = $_secondStep;
 					}
-					$secondStep[] = $_secondStep;
-				}
-				$_firstStep['secondStep'] = $secondStep;
-			} elseif ((!$va['correct']) && in_array($ka, $checked['firstStep'])) {
-				$objects = [];
-				foreach ($benford['firstStep'][$ka]['keys'] as $id) {
-					$object = DB::getPDO()->fetchFirst("select * from `{$this->SOURCE['TABLE']}` where `ID` = " . DB::getPDO()->quote($id));
-					if (!$object) continue;
-					$objects[] = array_merge(['@attributes' => ['fid' => "{$this->SOURCE['TABLE']}.{$id}"]], $object);
+					$_firstStep['secondStep'] = $secondStep;
+				} elseif ((!$va['correct']) && in_array($ka, $checked['firstStep'])) {
+					$objects = [];
+					foreach ($benford['firstStep'][$ka]['keys'] as $id) {
+						DB::getPDO()->insert($refTable, ['PRIMARY_KEY' => $benfordId, 'REMOTE_PRIMARY_KEY' => $id]);
+						$object = DB::getPDO()->fetchFirst("select * from `{$this->SOURCE['TABLE']}` where `ID` = " . DB::getPDO()->quote($id));
+						if (!$object) continue;
+						$objects[] = array_merge(['@attributes' => ['fid' => "{$this->SOURCE['TABLE']}.{$id}", 'p5:primaryKey' => $id]], $arrayWithNamespace($this->SOURCE['TABLE'], $object));
+					}
+					$_firstStep[$namespace($this->SOURCE['TABLE'], $this->SOURCE['TABLE'])][] = $objects;
 				}
-				$_firstStep[$this->SOURCE['TABLE']][] = $objects;
+				$firstStep[] = $_firstStep;
 			}
-			$firstStep[] = $_firstStep;
-		}
-		$array[$namespace]['firstStep'] = $firstStep;
+			$array[$namespace($mainTable, $mainTable)]['firstStep'] = $firstStep;
 
-		$xml = V::arrayToXML($array, true, $root);
-		file_put_contents("/tmp/benford_{$array[$namespace]['step']}Step.xml", $xml);
+			$xml = V::arrayToXML($array, true, $root);
+			file_put_contents($dir . DIRECTORY_SEPARATOR . "{$mainTable}_{$benfordId}.xml", $xml);
+			DB::getPDO()->update($mainTable, 'ID', $benfordId, ['A_STATUS' => 'NORMAL', 'A_STATUS_INFO' => 'Poprawnie wygenerowano raport']);
+		} catch (Exception $e) {
+			if ($benfordId) DB::getPDO()->update($mainTable, 'ID', $benfordId, ['A_STATUS' => 'DELETED', 'A_STATUS_INFO' => $e->getMessage()]);
+			SE_Layout::alert('danger', $e->getMessage());
+		}
 ?>
 <pre><?//=htmlspecialchars($xml)?></pre>
 <br><br>