<?php

require_once "../auth.php";
//require_once "../init.php";

$pairs = array(
        'MM' => '10',
        'VÖH' => '20',
        'KRU' => '30',
        'ULM' => '40',
        'LL' => '50',
		'GZ' => '55',
		'AAM' => '60',
        'LEH' => '70',
        'WTB' => '80'		
    );

function format_date($entry) {
	if ($entry == "0000-00-00") {
		return "";
	}
	$d = explode("-", $entry);
	return $d[2] . "." . $d[1] . "." . $d[0];
}	

function format_amount($entry) {
	return number_format($entry, 2, ",", ".");
}
	
/* Function to extract headline...
 * First the actual php-file is opened, then it is parsed and searched for $reportName=title.
 * The title will be extracted.
 * This title is used to perform a Database SELECT on qb_report.
 * The column readonly contains the reports column names. unified with the column hidden,
 * the output will be the actual column names, that just need to be translated using __().
 */
$part = substr($_GET['report'], strpos($_GET['report'], "/")+1);
$strDoc = '';
$strDoc = file_get_contents('..' . substr($part, strpos($part, "/")));
$match = substr($strDoc, strpos($strDoc, 'reportName')+14);
$match = substr($match, 0, strpos($match, ';')-1);
//echo $match;exit; //--> $match now contains the report's name' 

$sql = $adodb->Execute("SELECT * FROM qb_report WHERE name='" . $match . "'");
while ( !$sql->EOF )
{
  $query = $sql->fields['sql'];
  $id = $sql->fields['id'];
  $head_raw = $sql->fields['readonly'];
  $head_filter = $sql->fields['hidden'];
  $sql->moveNext();
}

$head_filter = explode(',', $head_filter);
$header = preg_replace('@' . $head_filter[0] . '@', '', $head_raw);
foreach ( $head_filter as $filter )
{
  //echo $filter. "<br>";
  $header = preg_replace('@' . $filter . '@', '', $header);
}

while ( strpos($query, '[userid]') != FALSE) $query = preg_replace('/\[userid\]/i', "'" . $_SESSION['userid'] . "'", $query);

//echo strpos($query, 'order');

/*
 * Hier den Filter hin
 * Beginn
 * Wird aus der Datei '/query2.txt' geladen.
 * Diese Datei wird von der phpMyEdit3.class.php geschrieben.
 */
 
  if ( $_GET['Betrieb'] )
{
  $query = substr($query, 0, strpos($query, 'order')-1) . ' AND FFIRM="' . strtr($_GET['Betrieb'], $pairs) . '" ' . substr($query, strpos($query, 'order'));	
}

if ( $_GET['Stufe2'] )
{
  $query = substr($query, 0, strpos($query, 'order')-1) . ' AND Stufe2="' . $_GET['Stufe2'] . '" ' . substr($query, strpos($query, 'order'));	
}

if ( $_GET['Stufe5'] )
{
  $query = substr($query, 0, strpos($query, 'order')-1) . ' AND Stufe5="' . $_GET['Stufe5'] . '" ' . substr($query, strpos($query, 'order'));	
}

if ( $_GET['Mahnstufe'] )
{
  $query = substr($query, 0, strpos($query, 'order')-1) . ' AND O500_MAHNSTUFE="' . $_GET['Mahnstufe'] . '" ' . substr($query, strpos($query, 'order'));	
}

if ( $_GET['from'] )
{
  $query = substr($query, 0, strpos($query, 'order')-1) . ' AND FLDAT >= STR_TO_DATE("' . $_GET['from'] . '", GET_FORMAT(DATE,"EUR")) ' . substr($query, strpos($query, 'order'));	
}

if ( $_GET['until'] )
{
  $query = substr($query, 0, strpos($query, 'order')-1) . ' AND FLDAT <= STR_TO_DATE("' . $_GET['until'] . '", GET_FORMAT(DATE,"EUR")) ' . substr($query, strpos($query, 'order'));	
}

if ( $_GET['wv'] )
{
  $query = substr($query, 0, strpos($query, 'order')-1) . ' AND Wiedervorlage = STR_TO_DATE("' . $_GET['wv'] . '", GET_FORMAT(DATE,"EUR")) ' . substr($query, strpos($query, 'order'));	
}

if ( $_GET['Kunde'] )
{
  if ( substr($_GET['Kunde'], 0, 1) != '-' ) $query = substr($query, 0, strpos($query, 'order')-1) . ' AND KNDNR="' . substr(substr($_GET['Kunde'], 0, strpos($_GET['Kunde'], ' - ')), 0, 10) . '" ' . substr($query, strpos($query, 'order'));
  else	$query = substr($query, 0, strpos($query, 'order')-1) . ' AND KNDNR="" ' . substr($query, strpos($query, 'order'));
}

if ( $_GET['Wochen'] )
{
  //1 Woche, 2 Wochen, 3 Wochen, 4 Wochen, > 4 Wochen
  if ( $_GET['Wochen'] == '0-2 Wochen' ) {
    $query = substr($query, 0, strpos($query, 'order')-1) . ' AND round((unix_timestamp(current_date())-unix_timestamp(FLDAT))/86400) < 14 ' . substr($query, strpos($query, 'order'));	
  }
  if ( $_GET['Wochen'] == '2-4 Wochen' ) {
    $query = substr($query, 0, strpos($query, 'order')-1) . ' AND round((unix_timestamp(current_date())-unix_timestamp(FLDAT))/86400) between 14 and 27 ' . substr($query, strpos($query, 'order'));	
  }
  if ( $_GET['Wochen'] == '4-6 Wochen' ) {
    $query = substr($query, 0, strpos($query, 'order')-1) . ' AND round((unix_timestamp(current_date())-unix_timestamp(FLDAT))/86400) between 28 and 41 ' . substr($query, strpos($query, 'order'));	
  }
  if ( $_GET['Wochen'] == '> 6 Wochen' ) {
    $query = substr($query, 0, strpos($query, 'order')-1) . ' AND round((unix_timestamp(current_date())-unix_timestamp(FLDAT))/86400) >= 42 ' . substr($query, strpos($query, 'order'));	
  }
}

if ( $_GET['Reklamation'] )
{
  $query = substr($query, 0, strpos($query, 'order')-1) . ' AND Reklamation="' . $_GET['Reklamation'] . '" ' . substr($query, strpos($query, 'order'));	
}

if ( $_GET['Versicherung'] )
{
  $query = substr($query, 0, strpos($query, 'order')-1) . ' AND Versicherung="' . $_GET['Versicherung'] . '" ' . substr($query, strpos($query, 'order'));	
}
if ( $_GET['Mahnen'] )
{
  $query = substr($query, 0, strpos($query, 'order')-1) . ' AND Mahnen="' . $_GET['Mahnen'] . '" ' . substr($query, strpos($query, 'order'));	
} 
if ( $_GET['Rechtsanwalt'] )
{
  $query = substr($query, 0, strpos($query, 'order')-1) . ' AND Rechtsanwalt="' . $_GET['Rechtsanwalt'] . '" ' . substr($query, strpos($query, 'order'));	
} 
if ( $_GET['Reserve_3'] )
{
  $query = substr($query, 0, strpos($query, 'order')-1) . ' AND Reserve_3="' . $_GET['Reserve_3'] . '" ' . substr($query, strpos($query, 'order'));	
} 
// DEBUG
// echo $query;
/*
 * Filter
 * Ende
 */
 
//echo $query;exit;
$insdoc = '';

$sql = $adodb->Execute($query);

$insdoc .= '\plain \fs30\ql \b Forderungsmanagement';
$insdoc .= '\tab \tab \tab \tab \tab \tab ';
if (in_array($id, array('1', '2', '3', '4'))) {
	$insdoc .= '\tab \tab \tab  ';
}
$insdoc .= __('TITLE', $match, "qb_report_{$id}");
$insdoc .= ' \tab \tab \b0\qr0\fs0\par \fs30\qr \b ________________________________________________________________________________\tab \tab \b0\qr0 \par \par ';

if ( $match == 'NAGA' || $match == 'FM_NaGa' || $match == 'fm_ahr_naga' || $match == 'fm_aha_naga' || $match == 'fm_rem_naga')
{
  $colwidth = array(615, 1020, 1636, 2495, 4328, 5771, 6648, 7410, 8237, 9275, 10019, 11300, 12290, 13461, 13661, 13851, 14810);
  $colwidth = array(570, 430, 720, 800, 1830, 1440, 840, 820, 850, 1040, 850, 1280, 840, 1170, 840, 340, 340);
  $colname = array("Fil.", "Abt", "Fzg-Nr", "Typ", "Kunde", "VK", "Rg.Dat.", "Rg.Betr.", "fällig", "Mahnst.", "offen", "Bemerkung FIBU", "WV", "Bemerkung Abt.", "Lieferdat.", "Bearb.", "Mahnen?");
	
  $insdoc .= '\trowd \trgaph70\trleft-70 \trbrdrt\brdrs\brdrw10 ' .
		'\trpaddl10\trpaddr10\trpaddfl3\trpaddfr3\clbrdrt\brdrw10\brdrs\clbrdrb\brdrw10\brdrs \clvertalc ';
  $colsum = 0;
  foreach ($colwidth as $w) {
    $colsum += $w;
    $insdoc .= '\cellx' . $colsum .'\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clvertalc ';

  }
  $insdoc .= '\intbl\sl2\slmult0\qj\f0\fs12 ';
		
  $insdoc .= '\b \qc \fs12 \trhdr ';
  $insdoc .= implode("\cell ", $colname);
  $insdoc .= '\qc0\b0\cell \row ';		

  $index = 0;
  $sum = 0;

  while ( !$sql->EOF )
  {
	$index += 1;
	$bem = $sql->fields['Bemerkung'];
	$bem_2 = $sql->fields['Bemerkung_2'];
	//if ( strlen($sql->fields['Bemerkung']) > 28 ) { $bem = substr($sql->fields['Bemerkung'], 0, 25) . '...'; }
	//if ( strlen($sql->fields['Bemerkung_2']) > 28 ) { $bem_2 = substr($sql->fields['Bemerkung'], 0, 25) . '...'; }
	if ( $index % 2 == 0 ) {
		$insdoc .= ' \trowd \trgaph70' . '\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clcbpat3\clvertalc ';
		$colsum = 0;
		foreach ($colwidth as $w) {
			$colsum += $w;
			$insdoc .= '\cellx' . $colsum . '\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clcbpat3\clvertalc ';
		}
		$insdoc .= '\intbl\sl2\slmult0\qj\f0\fs12 \plain \fs12\clcbpat3';
	} else {
		$insdoc .= ' \trowd \trgaph70\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clvertalc ';
		$colsum = 0;
		foreach ($colwidth as $w) {
			$colsum += $w;
			$insdoc .= '\cellx' . $colsum . '\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clvertalc ';
		}
		$insdoc .= 	'\intbl\sl2\slmult0\qj\f0\fs12 \plain \fs12\clcbpat2';
	}
	$insdoc .= ' \pard \intbl \fs12\ql {' . $sql->fields['Betrieb'] . 
		'}\cell \pard \intbl \fs12\ql {' . $sql->fields['Stufe2'] . 
		'}\cell \pard \intbl \fs10\qc {' . $sql->fields['FAGNR'] . 
		'}\cell \pard \intbl \fs10\qc {' . $sql->fields['FTYPE'] . 
		'}\cell \pard \intbl \fs10\ql {' . $sql->fields['KNAME'] . 
		'}\cell \pard \intbl \fs10\ql {' . $sql->fields['Stufe5'] . 
		'}\cell \pard \intbl \fs10\qc {' . format_date($sql->fields['REDAT']) . 
		'}\cell \pard \intbl \fs12\qr {' . format_amount($sql->fields['O500_HW2BUCHGBETR']) . 
		'}\cell \pard \intbl \fs10\qc {' . format_date($sql->fields['FLDAT']) . 
		'}\cell \pard \intbl \fs10\qc {' . $sql->fields['O500_MAHNSTUFE'] . 
		'}\cell \pard \intbl \fs12\qr {' . format_amount($sql->fields['O500_HW2BETR_OFFEN']) . 
		'}\cell \pard \intbl \fs10\ql {' . $bem . 
		'}\cell \pard \intbl \fs10\qc {' . format_date($sql->fields['Wiedervorlage']) . 
		'}\cell \pard \intbl \fs10\ql {' . $bem_2 . 
		'}\cell \pard \intbl \fs10\qc {' . format_date($sql->fields['shipment_date']) . 
		'}\cell \pard \intbl \fs12\qc {' . $sql->fields['Bearbeitet'] . 
		'}\cell \pard \intbl \fs12\qc {' . $sql->fields['Mahnen'] . 
		'}\qc0\cell \row ';
	// $sql->fields['Betrieb'];
			 
    $bem = '';
    $bem_2 = '';
    $sum += (float) $sql->fields['O500_HW2BETR_OFFEN'];
    $sql->MoveNext();
  }
    //$insdoc = substr($insdoc, 0, -4);
    $sum = number_format($sum, 2, ',', '.');
	$insdoc .= ' \pard \par \par \plain \fs12\qr \b \tab \tab \tab {Gesamtsumme: ' . $sum . '} \tab \tab \b0\qr0';
    $doc = file_get_contents('Uebersicht_' . $match . '_vorl.rtf');
    $doc = preg_replace('@<header>@', $insdoc, $doc);
    $fd = fopen('temp/Uebersicht.rtf', 'w');
    rewind($fd);
    fwrite($fd, $doc);
    fclose($fd);
    
} elseif ( $match == 'SC-TuZ'  || $match == 'FM_SC-TuZ' || $match == 'fm_ahr_sc' || $match == 'fm_aha_sc' || $match == 'fm_rem_sc' )
{
  $insdoc .= '\trowd \trgaph70\trleft-70 \trbrdrt\brdrs\brdrw10 ' .
		'\trpaddl10\trpaddr10\trpaddfl3\trpaddfr3\clbrdrt\brdrw10\brdrs\clbrdrb\brdrw10\brdrs \clvertalc ' .
		'\cellx615\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clvertalc ' .
		'\cellx1020\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clvertalc ' .
		'\cellx1636\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clvertalc ' .
		'\cellx2495\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clvertalc ' .
		'\cellx4328\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clvertalc ' .
		'\cellx5771\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clvertalc ' .
		'\cellx6648\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clvertalc ' .
		'\cellx7410\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clvertalc ' .
		'\cellx8237\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clvertalc ' .
		'\cellx9275\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clvertalc ' .
		'\cellx10019\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clvertalc ' .
		'\cellx11300\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clvertalc ' .
		'\cellx12290\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clvertalc ' .
		'\cellx13461\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clvertalc ' .
		'\cellx13851\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clvertalc ' .
		'\cellx14810\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clvertalc \intbl\sl2\slmult0\qj\f0\fs12 ';
		
  $insdoc .= '\b \qc \fs12 ' . __('Betrieb', 'Betrieb', 'qb_report_' . $id) . '\cell ' . __('Stufe2', 'Stufe2', 'qb_report_' . $id) . '\cell ' . 
		   __('FAGNR', 'FAGNR', 'qb_report_' . $id) . '\cell ' . __('FTYPE', 'FTYPE', 'qb_report_' . $id) . '\cell ' .
		   __('KNAME', 'KNAME', 'qb_report_' . $id) . '\cell ' . __('Stufe5', 'Stufe5', 'qb_report_' . $id) . '\cell ' .
		   __('REDAT', 'REDAT', 'qb_report_' . $id) . '\cell ' . __('O500_HW2BUCHGBETR', 'O500_HW2BUCHGBETR', 'qb_report_' . $id) . '\cell ' .
		   __('FLDAT', 'FLDAT', 'qb_report_' . $id) . '\cell ' . __('O500_MAHNSTUFE', 'O500_MAHNSTUFE', 'qb_report_' . $id) . '\cell ' .
		   __('O500_HW2BETR_OFFEN', 'O500_HW2BETR_OFFEN', 'qb_report_' . $id) . '\cell ' . __('Bemerkung', 'Bemerkung', 'qb_report_' . $id) . '\cell ' .
		   __('Wiedervorlage', 'Wiedervorlage', 'qb_report_' . $id) . '\cell ' . __('Bemerkung_2', 'Bemerkung_2', 'qb_report_' . $id) . '\cell ' .
		   __('Bearbeitet', 'Bearbeitet', 'qb_report_' . $id) . '\cell ' . __('Mahnen', 'Mahnen', 'qb_report_' . $id) . '\qc0\b0\cell\row ';		

  $index = 0;

  while ( !$sql->EOF )
  {
	$index += 1;
	$bem = $sql->fields['Bemerkung'];
	$bem_2 = $sql->fields['Bemerkung_2'];
	//if ( strlen($sql->fields['Bemerkung']) > 28 ) { $bem = substr($sql->fields['Bemerkung'], 0, 25) . '...'; }
	//if ( strlen($sql->fields['Bemerkung_2']) > 28 ) { $bem_2 = substr($sql->fields['Bemerkung_2'], 0, 25) . '...'; }
	if ( $index % 2 == 0 ) $insdoc .= ' \trowd \trgaph70\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clcbpat3\clvertalc ' .
		'\cellx615\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clcbpat3\clvertalc ' .
		'\cellx1020\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clcbpat3\clvertalc ' .
		'\cellx1636\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clcbpat3\clvertalc ' .
		'\cellx2495\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clcbpat3\clvertalc ' .
		'\cellx4328\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clcbpat3\clvertalc ' .
		'\cellx5771\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clcbpat3\clvertalc ' .
		'\cellx6648\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clcbpat3\clvertalc ' .
		'\cellx7410\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clcbpat3\clvertalc ' .
		'\cellx8300\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clcbpat3\clvertalc ' .
		'\cellx9275\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clcbpat3\clvertalc ' .
		'\cellx10019\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clcbpat3\clvertalc ' .
		'\cellx11300\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clcbpat3\clvertalc ' .
		'\cellx12290\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clcbpat3\clvertalc ' .
		'\cellx13461\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clcbpat3\clvertalc ' .
		'\cellx13851\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clcbpat3\clvertalc ' .
		'\cellx14810\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clcbpat3\clvertalc \intbl\sl2\slmult0\qj\f0\fs12 ' .
		'\plain \fs12\clcbpat3';
	else $insdoc .= ' \trowd \trgaph70\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clvertalc ' .
		'\cellx615\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clvertalc ' .
		'\cellx1020\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clvertalc ' .
		'\cellx1636\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clvertalc ' .
		'\cellx2495\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clvertalc ' .
		'\cellx4328\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clvertalc ' .
		'\cellx5771\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clvertalc ' .
		'\cellx6648\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clvertalc ' .
		'\cellx7410\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clvertalc ' .
		'\cellx8300\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clvertalc ' .
		'\cellx9275\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clvertalc ' .
		'\cellx10019\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clvertalc ' .
		'\cellx11300\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clvertalc ' .
		'\cellx12290\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clvertalc ' .
		'\cellx13461\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clvertalc ' .
		'\cellx13851\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clvertalc ' .
		'\cellx14810\clbrdrt\brdrs\brdrw1\brdrcf1\clbrdrb\brdrs\brdrw1\brdrcf1\clvertalc \intbl\sl2\slmult0\qj\f0\fs12 ' .
		'\plain \fs12\clcbpat2';
	$insdoc .= ' \pard \intbl \fs12\qc {' . $sql->fields['Betrieb'] . '}\cell \pard \intbl \fs12\qc {' . $sql->fields['Stufe2'] . '}\cell \pard \intbl \fs12\qc {' . 
  			 $sql->fields['FAGNR'] . '}\cell \pard \intbl \fs12\qc{ ' . $sql->fields['FTYPE'] . '}\cell \pard \intbl \fs12\qc {' . 
  			 $sql->fields['KNAME'] . '}\cell \pard \intbl \fs12\qc {' . $sql->fields['Stufe5'] . '}\cell \pard \intbl \fs12\qc {' . 
			 $sql->fields['REDAT'] . '}\cell \pard \intbl \fs12\qc {' . $sql->fields['O500_HW2BUCHGBETR'] . '}\cell \pard \intbl \fs12\qc {' . 
			 $sql->fields['FLDAT'] . '}\cell \pard \intbl \fs12\qc {' . $sql->fields['O500_MAHNSTUFE'] . '}\cell \pard \intbl \fs12\qc {' 
			 . $sql->fields['O500_HW2BETR_OFFEN'] . '}\cell \pard \intbl \fs12\qc {' . $bem . '}\cell \pard \intbl \fs12\qc {' .
			 $sql->fields['Wiedervorlage'] . '}\cell \pard \intbl \fs12\qc {' . $bem_2 . '} \cell \pard \intbl \fs12\qc {' . 
			 $sql->fields['Bearbeitet'] . '}\cell \pard \intbl \fs12\qc {' . $sql->fields['Mahnen'] . '}\qc0\cell \row ';
	$bem = '';
    $bem_2 = '';
    $sum += (float) $sql->fields['O500_HW2BETR_OFFEN'];

    $sql->MoveNext();
  }
    //$insdoc = substr($insdoc, 0, -4);
    $sum = number_format($sum, 2, ',', '.');
	$insdoc .= ' \pard \par \par \plain \fs12\qr \b \tab \tab \tab {Gesamtsumme: ' . $sum . '} \tab \tab \b0\qr0';
    $doc = file_get_contents('Uebersicht_' . $match . '_vorl.rtf');
    $doc = preg_replace('@<header>@', $insdoc, $doc);
    $fd = fopen('temp/Uebersicht.rtf', 'w');
    fwrite($fd, $doc);
    fclose($fd);
}
/*\trowd\trgaph70\trleft-70\trbrdrl\brdrs\brdrw10 \trbrdrt\brdrs\brdrw10 \trbrdrr\brdrs\brdrw10 \trbrdrb\brdrs\brdrw10 \trpaddl70\trpaddr70\trpaddfl3\trpaddfr3\clbrdrt\brdrw10\brdrs\clbrdrr\brdrw10\brdrs\clbrdrb\brdrw10\brdrs \cellx1418\clbrdrt\brdrw10\brdrs\clbrdrr\brdrw10\brdrs\clbrdrb\brdrw10\brdrs \cellx2835\clbrdrt\brdrw10\brdrs\clbrdrr\brdrw10\brdrs\clbrdrb\brdrw10\brdrs \cellx4636\clbrdrt\brdrw10\brdrs\clbrdrr\brdrw10\brdrs\clbrdrb\brdrw10\brdrs \cellx6096\clbrdrt\brdrw10\brdrs\clbrdrr\brdrw10\brdrs\clbrdrb\brdrw10\brdrs \cellx8222\clbrdrt\brdrw10\brdrs\clbrdrr\brdrw10\brdrs\clbrdrb\brdrw10\brdrs \cellx10134\pard\intbl\sl360\slmult0\qj\f0\fs16*/

  echo '
<html>
<head>
<title></title>
<meta http-equiv="refresh" content="0; URL=temp/Uebersicht.rtf">
</head>
<body>
</body>
</html>';
?>
