<?php

class StructController
{
	private $dir;
	private $config;

	private $pattern;
	private $result;

	public static function Validate ($action, $filename, $logfile = "") {
		if (!file_exists($filename)) return;
		$struct = new StructController($filename);
		$result = $struct->ValidateFinance();

		if ($logfile == "") {
			echo "FEHLER:";
			foreach($result['error'] as $type => $list) {
				echo "\r\n\r\n" . $type . ": \r\n";
				echo implode("\r\n", $list);
			}
		} else {
			file_put_contents($logfile, json_encode($result));
		}
	}
	
	public function __construct ($configFile) {
		$this->dir = dirname(dirname($configFile));
		$xml = simplexml_load_file($configFile);
		$this->config = $this->xmlToArray($xml);
	}

	public function ExportSKR51 ($csvFile) {

	}

	public function ValidateFinance () {
		$this->pattern = array(
			'error' => array(
				'Neues Konto' => '^[^;]*;[^;]*;[^;]*$',
				'Konto_Art' => '^([^;]*;){2};',
				'Kostenstelle' => '^([^;]*;){2}[02];;',
				'Konto_1' => '^([^;]*;){5};'
			),
			'info' => array(
				'FB' => '^([^;]*;){10};',
				'TEK' => '^([^;]*;){20};',
				'GuV' => '^([^;]*;){2}[02];([^;]*;){27};',
				'Bilanz' => '^([^;]*;){2}1;([^;]*;){37};',
				'GuV_extern' => '^([^;]*;){2}[02];([^;]*;){47};'
			)
		);
		$csvFile = $this->dir . "\\" . $this->config['Dateipfade']['KontenrahmenPfad'] . $this->config['Dateipfade']['KontenrahmenCsv'];

		$this->result = array('error' => array(), 'info' => array());

		foreach(file($csvFile) as $i => $row) {
			if ($i == 0) continue;
			$this->CheckRow(mb_convert_encoding($row, "ISO-8859-1", "UTF-8"));
		}
		return $this->result;
	}

	public function CheckRow ($row) {
		$info = "";
		if (!preg_match("/^([^;]*);([^;]*)/", $row, $info)) {
			return;
		}

		foreach ($this->pattern as $level => $list) {
			foreach ($list as $type => $p) {
				if (preg_match("/". $p . "/", $row)) {
					$this->result[$level][$type][] = mb_convert_encoding($info[1] . " - " . $info[2], "UTF-8", "Windows-1252");
				}
			}
		}
	}


	public function xmlToArray ($xml) {
		$s = array();
		$s['Kunde'] = (string)$xml->Kunde->Firma;
		$s['Dateipfade'] = (array)$xml->Dateipfade;

		$sd = array();
		for ($i=0; $i < count($xml->Strukturdefinitionen->Struktur); $i++) {
			$str = $xml->Strukturdefinitionen->Struktur[$i];
			$attr = $xml->Strukturdefinitionen->Struktur[$i]->attributes();

			$sd[] = array(
				'KontenInStruktur' => (string)$attr->KontenInStruktur,
				'Name' => (string)$str->Name,
				'Beschreibung' => (string)$str->Beschreibung,
				'Kontenfilter' => (string)$str->Kontenfilter,
				'ExportStk' => (string)$str->ExportStk,
				'ExportUebersetzung' => (string)$str->ExportUebersetzung,
				'ExportUebersetzungStk' => (string)$str->ExportUebersetzungStk,
				'ExportAdjazenz' => (string)$str->ExportAdjazenz,
				'ExportHerstellerKontenrahmen' => (string)$str->ExportHerstellerKontenrahmen
			);
		}
		$s['Strukturdefinitionen'] = $sd;

		$sf = array();
		for ($i=0; $i < count($xml->Suchfilter->Filter); $i++) {
			$attr = $xml->Suchfilter->Filter[$i]->attributes();
			$sf[] = array(
				'Name' => (string)$attr->Bezeichnung,
				'Kontenfilter' => (string)$attr->Regex
			);
		}
		$s['Suchfilter'] = $sf;

		$kdf = array();
		for ($i=0; $i < count($xml->KontoDatenfelder->Feld); $i++) {
			$str = $xml->KontoDatenfelder->Feld[$i];
			$attr = $xml->KontoDatenfelder->Feld[$i]->attributes();

			$opt = array();
			for ($j = 0; $j < count($str->Optionen->Option); $j++) {
				$optAttr = $str->Optionen->Option[$j]->attributes();
				$opt[] = array(
					'Name' => (string)$optAttr->Name,
					'Wert' => (string)$optAttr->Wert
				);
			}

			$kdf[] = array(
				'id' => (string)$attr->id,
				'Aktiv' => (string)$str->Aktiv,
				'Optionen' => $opt,
				'Name' => (string)$str->Name,
				'Beschreibung' => (string)$str->Beschreibung,
				'Default' => (string)$str->Default,
				'Darstellung' => (string)$str->Darstellung
			);
		}
		$s['KontoDatenfelder'] = $kdf;
		return $s;
	}
}