Kostenerstattungsantrag über $sum EUR
	Projekt: $_POST[project]
	
	
	 
	
	Name: $_POST[realname]
	Telefon: $_POST[phone]
	E-Mail: $_POST[email]
	Ansprechpartner: $_POST[wmdecontact]
	
	
	
	
	Auszahlungsweg: $aw
	$aw_details
	
	
	$thtml
	
	
EOD;
#var_dump($thtml);
#die;
// WMDE Ansprechpartner: $_POST[wmdecontact]
// Telefon: $_POST[phone]
	// Print text using writeHTMLCell()
	$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
	// ---------------------------------------------------------
	$tn = tempnam("/tmp","kaform");
	// Close and output PDF document
	// This method has several options, check the source code 
	// documentation for more information.
	$pdf->Output($tn, 'F');
	return $tn;
}
function mkSnippedPdf($key){
	$pageLayout = array(210,70);
	
	// create new PDF document
#	$pdf = new kaformPDF(
#		PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, 
#		true, 'UTF-8', false);
	$pdf = new kaformPDF(
		PDF_PAGE_ORIENTATION, PDF_UNIT, $pageLayout /*PDF_PAGE_FORMAT*/, 
		true, 'UTF-8', false);
	$pdf->AddPage('L');
	$pos = array_search($key,array_keys($_POST['desc']))+1;
//	echo "
Make Snipped for $key -> $pos
";
	#	$mydesk = $_POST[desc][$key];
	#
	$amount = $_POST['amount'][$key];
	$desc = $_POST['desc'][$key];
	$am = floatval(str_replace(",",".",$amount));
	$am =  str_replace(".",",",sprintf("%.2f",$am));
	$html = 
<<Position $pos
	$desc
	$am EUR
EOD;
	
	// Print text using writeHTMLCell()
	$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
	$tn = tempnam("/tmp","kaform");
	// Close and output PDF document
	// This method has several options, check the source code 
	// documentation for more information.
	$pdf->Output("$tn", 'F');
	return $tn;
}
//var_dump($_POST);
function convert($row) {
	$r = array();
	foreach ($_SESSION['files'] as $filename => $file){
		if ($file['row'] != $row)
			continue;
		$tn = tempnam("/tmp","kaform");
		file_put_contents("$tn",$file['content']);
		array_push($r,"$tn");
	}
	return $r;
}
function calcSum(){
	$sum =0;
	if (!isset($_POST['amount'])) {
		echo "wrong parameters";
		die;
	}
	foreach ($_POST['amount'] as $val){
//		var_dump($val);
		$sum += floatval(str_replace(",",".",$val));
		
	}
	$sum -= get_adv();
	return str_replace(".",",",sprintf("%0.2f",$sum));
//	return sprintf("%.2f",$sum);
}
function mkTable(){
	$html = "
 
";
	$pos = 1;
	$sum =0;
	if (!isset($_POST['amount'])) {
		echo "wrong parameters";
		die;
	}
	foreach ($_POST['amount'] as $key=>$val){
		$html .='';
		
		$html .= "| $pos";
		$html .= " | ".$_POST['date'][$key]."";
		$html .= " | ".$_POST['desc'][$key]."";
		$am = floatval(str_replace(",",".",$val));
		$am =  str_replace(".",",",sprintf("%.2f",$am));
		$html .= ' | '.$am." ".$_POST['currency'][$key]. "";
		
		$html .=" | 
";
		$sum += floatval(str_replace(",",".",$val));
		$pos ++;
	}
	$adv = get_adv();
	if ($adv) {
		$html .= "";
		$html .= '| ';
		$html .= "Bereits erhaltener Vorschuss:";
		$html .= '';
		$html .= ' | ';
		$html .= sprintf("-%0.2f",$adv)." EUR";
		$html .= '';
		$html .= " | 
";
	}
	$html .= '';
	$html .= '| ';
	$html .= "Summe:";
	$html .= '';
	$html .= ' | ';
	$html .= "".calcSum()." EUR";
	$html .= '';
	$html .= ' | 
';
	$html .= "
";
#	$html .="
 
";
#	$html .="Summe: ".calcSum()."";
	return $html;
//	return str_replace(".",",",sprintf("%0.2f",$sum));
//	return sprintf("%.2f",$sum);
}
$pdfs = array();
// Applicatoin starts here
//
$p = mkFrontPDF();
$file = $p;
#header('Content-Disposition: attachment; filename="'. basename($file) . '"');
#header('Content-Length: ' . filesize($file));
#readfile($file);
#var_dump($p);
#die();
#echo "Making Fromt PDF
";
#echo "Did Making Fromt PDF
";
array_push($pdfs,$p);
//var_dump($pdfs);
foreach ($_POST['desc'] as $key => $val) {
//	echo "DREESK KEY VAL $key --> $val
";
	$p = mkSnippedPDF($key);
	array_push($pdfs,$p);
	$pr = convert($key);
	$pdfs = array_merge($pdfs,$pr);
}
//var_dump($pdfs);
//
$resultnam = tempnam("/tmp","karesult");
$cmd = "$CONVERT_CMD";
foreach ($pdfs as $filename){
	$cmd .= " $filename";
}
$cmd .= " cat output $resultnam.pdf";
exec ($cmd);
#var_dump($_POST);
$file = "$resultnam.pdf";
if ($_POST['sendmail']=='yes'){
	$subj = "Kostenerstattungsantrag ueber".calcSum()." EUR";	
		$mail = new PHPMailer\PHPMailer\PHPMailer();
		$mail->SMTPOptions = array(
		        'ssl' => array(
			        'verify_peer' => false,
		       		'verify_peer_name' => false,
			        'allow_self_signed' => true
	   		 )
		);
		$mail->IsSMTP();	
		$mail->setFrom($mailFrom);
		$mail->Subject = $subj;
		$mail->Body = "Kostenerstattungsantrag ...";
		$mail->AddAddress($mailDest);
		$mail->AddAttachment( $file , 'antrag.pdf' );
		$mail->Host = $mailHost;                 // Specify main and backup server
		$mail->Port = $mailPort;                                     // Set the SMTP port
		$mail->SMTPAuth = $mailSMTPAuth;
		$mail->Username = $mailUsername;
		$mail->Password = $mailPassword;
		$mail->SMTPSecure = $mailSMTPSecure;                            // Enable encryption, 'ssl' also accepted
		$rc = 	$mail->send();
		if (!$rc) {
			echo 'Mailer Error: ' . $mail->ErrorInfo;
		}
		echo "Dein Antrag wurde versendet.
";
		echo '
		Weiteren Antrag auf Kostenerstattung stellen
';
	die;
}
$file = "$resultnam.pdf";
header('Content-Disposition: attachment; filename="'. basename($file) . '"');
header('Content-Length: ' . filesize($file));
readfile($file);