<?php
require_once(dirname(__FILE__) . "/../auth.php");
require_once(dirname(__FILE__) . "/../init.php");
  
function getReportName () 
{  
	$part = substr($_GET['report'], strpos($_GET['report'], "/")+1);
	$strDoc = '';
	$strDoc = file_get_contents('..' . substr($part, strpos($part, "/")));
	$match = substr($strDoc, strpos($strDoc, 'reportName')+14);
	return substr($match, 0, strpos($match, ';')-1);
}
  
$doc = file_get_contents('Beitreibungsauftrag_' . getReportName() . '.rtf');

foreach ($_GET as $key => $value)
{
	$value = utf8_decode($value);
	switch ($value) {
		case "":
			$value = '-';
			break;
		case "Mahnst. 0: Kontoauszug vor Mahnung":
			$value = '{\b\cf11 ' . $value . '}';
			break;
		case "Mahnst. 1: Zahlungserinnerung":
			$value = '{\b\cf7 ' . $value . '}';
			break;
		case "Mahnst. 2: RA-Androhung":
			$value = '{\b\cf6 ' . $value . '}';
			break;
	}
	$doc = preg_replace('/\<' . strtolower($key) . '\>/i', $value, $doc);
}
$doc = preg_replace('/\<datum\>/i', date("d.m.Y"), $doc);

if (true) {
	$fd = fopen('Beitreibungsauftrag.rtf', 'w');
	fwrite($fd, $doc);
	fclose($fd);
    echo '<html><head><title></title><meta http-equiv="refresh" content="0; URL=Beitreibungsauftrag.rtf"></head></html>';
} else {
	$length = strlen($doc);
	$filename = "Beitreibungsauftrag_" . $_GET['KNDNR'] . ".rtf";
	
	header('Pragma: public');
	header('Expires: 0'); 
	header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
	header('Cache-Control: private', false);
	header('Content-Description: File Transfer');
	header('Content-Type: application/rtf');
	header('Content-Length: "' . $length . '"');
	header('Content-Disposition: attachment; filename="' . $filename . '"');

	echo $doc;
}

/*

$_GET
(
    [report] => /qbuilder/phpME_report_1183144753.php
    [KNAME] => 843487 - SCHAUB GmbH
    [KNDNR] => 843487
    [Kstra] => Postfach 101 552
    [Kdplz] => 86885
    [Kdort] => Landsberg
    [Ktelf] => 
    [BelNr] => VGGF135000115
    [ReDat] => 18.06.2013
    [Verka] => 155 - Eray Cakirogullari
    [Betri] => LL - NA
    [OffBe] => -145,00
    [RechBe] => -145,00
    [Mahnst] => 
    [NextMahnst] => 
)

*/
?>
