<?php 
	require_once("config/connect.php");
	require_once("global_functions.php");
	require_once('include/fpdf.php');
	
	function compare_cb($a,$b){
					#$c = $a['titel'] - $b['titel'];
					$c = strtotime($a['datum']) - strtotime($b['datum']);
					$c .= $b['titel'] - $a['titel'];
					return $c;
				}
	
	function printCashbookA4($cashbookId){
				# das muss dann noch übergeben werden
			$kassenabschlStartId = 260;
			$kassenabschlEndeId = 265;
			$kassabuchUebertrag = "1222.0000777";
			$xDate=date('Y-m-d 12:05:00');
			$dDate = strtotime($xDate);
			$erstelltDatum = date( 'j.n.Y H:i', $dDate );
			$erstelltTitel = "Eingegebener Name, event Monat od so";
			
			$cbWidth=210;
			$cbHeight=280;
			$marginLR=10;
			$marginTop=20;
			
			$pdf=new FPDF('P','mm', array($cbWidth, $cbHeight));
			$pdf->SetMargins($marginLR, $marginTop, 0);			// margin right (optional) set to 0, rest of pager width	
			$pdf->AddPage(); 
			
			$fontConfig=SCHRIFT_AUSDRUCK_PDF;
			
			$printer=mysql_fetch_array(mysql_query("SELECT win_drucker_name, drucker_margin_pdf, druckbreite, zoom_factor 
											FROM drucker WHERE LOWER(ist_a4) = 'ja' "));
		
			$drucker=$printer['win_drucker_name'];

			if ( strpos(strtolower($fontConfig), 'arial') !== false  || strpos(strtolower($fontConfig), 'helvetica') !== false || 
						strpos(strtolower($fontConfig), 'sans serif') !== false ){ $pdfFont="Arial"; $z="Arial"; }
						
			else if ( strpos(strtolower($fontConfig), 'times') !== false  || strpos(strtolower($fontConfig), 'roman') !== false ||
						(strpos(strtolower($fontConfig), 'serif') !== false &&  strpos(strtolower($fontConfig), 'sans') == false)) { $pdfFont="Times";$z="Times";  }
			else if (strpos(strtolower($fontConfig), 'courier') !== false ) { $pdfFont="Courier"; $z="Courir";   }
			else {$pdfFont="Arial"; $z="Default";   }
			
			preg_match('/\d+px | \d+pt/', $fontConfig, $m);
			preg_match('/\d+/', $m[0], $match);
			$pdfFontSize=$match[0];		
			
			$lineHeight=$pdfFontSize/2.5;
			
			$pdf->SetFont($pdfFont, '', $pdfFontSize); 
			$kZeile1=""; $kZeile2=""; $kZeile3=""; $kZeile4=""; $kZeile5=""; $kZeile6=""; $kZeile7=""; $kZeile8="";
			$zz='kZeile';
			$headArray=kopfzeile_ausdrucken('', 'printPdf');
				for($i=0; $i < sizeof($headArray); $i++){
					$zNo=$i+1;
						# so wird der variablenname concated ergibt dann $kZeile1 - $kZeileX je nachdem wie viele kopfzeilen befüllt sind 
					${$zz.$zNo} = $headArray[$i];
			}
			
			$pdf->Cell(($cbWidth/2)-$marginLR, $lineHeight, "Kassabuch, erstellt: ".$erstelltDatum, 0, 0, 'L'); 
			$pdf->Cell(($cbWidth/2)-$marginLR, $lineHeight, iconv('UTF-8', 'windows-1252', strip_tags($kZeile1)), 0, 1, 'R');
			$pdf->Cell(($cbWidth/2)-$marginLR, $lineHeight, "Periode: ".$erstelltTitel, 0, 0, 'L'); 
			$pdf->Cell(($cbWidth/2)-$marginLR, $lineHeight, iconv('UTF-8', 'windows-1252', strip_tags($kZeile2)), 0, 1, 'R'); 
			$pdf->Cell(($cbWidth/2)-$marginLR, $lineHeight, iconv('UTF-8', 'windows-1252', "Kassaabschlüsse: ".$kassenabschlStartId." - ".$kassenabschlEndeId), 0, 0, 'L'); 
			$pdf->Cell(($cbWidth/2)-$marginLR, $lineHeight, iconv('UTF-8', 'windows-1252', strip_tags($kZeile3)), 0, 1, 'R'); 
			$pdf->Cell(($cbWidth/2)-$marginLR, $lineHeight, iconv('UTF-8', 'windows-1252', "Übertrag: ".$kassabuchUebertrag), 0, 0, 'L'); 
			$pdf->Cell(($cbWidth/2)-$marginLR, $lineHeight, iconv('UTF-8', 'windows-1252', strip_tags($kZeile4)), 0, 1, 'R'); 
			$pdf->Cell(($cbWidth/2)-$marginLR, $lineHeight, iconv('UTF-8', 'windows-1252', ""), 0, 0, 'L'); 
			$pdf->Cell(($cbWidth/2)-$marginLR, $lineHeight, iconv('UTF-8', 'windows-1252', strip_tags($kZeile5)), 0, 1, 'R'); 
		
			$pdf->Cell($cbWidth-($marginLR*2), 2, "", 'B', 1, 'C'); 	#  trennlinie unterhalb von Fa kopf
			
			$cashbookResultArray= calculateCasbook($kassenabschlStartId, $kassenabschlEndeId);
			
			usort($cashbookResultArray, "compare_cb");
				
				$sumBetrag=0;
				$sumUst=0; 
				$sumVorst=0; 
	for($z=0; $z< 10; $z++){			
				for($i=0; $i < count($cashbookResultArray); $i++){
						$dDate = strtotime($cashbookResultArray[$i]['datum']);
						$formDate = date( 'j.n.Y', $dDate );
						$bBetrag=number_format($cashbookResultArray[$i]['betrag'], 2, ",", ".");
						$ustBetrag=number_format($cashbookResultArray[$i]['ust'], 2, ",", ".");
						$vorstBetrag=number_format($cashbookResultArray[$i]['vorst'], 2, ",", ".");
							$pdf->Cell(($cbWidth-($marginLR*2))* .11, $lineHeight, $formDate, 0, 0, 'C');
							$pdf->Cell(($cbWidth-($marginLR*2))* .11, $lineHeight, $bBetrag, 0, 0, 'R');							
							$pdf->Cell(($cbWidth-($marginLR*2))* .11, $lineHeight, $ustBetrag, 0, 0, 'R');
							$pdf->Cell(($cbWidth-($marginLR*2))* .11, $lineHeight, $vorstBetrag, 0, 0, 'R');
							$pdf->Cell(($cbWidth-($marginLR*2))* .11, $lineHeight, iconv('UTF-8', 'windows-1252', $cashbookResultArray[$i]['zahlart']), 0, 0, 'L');
							$pdf->Cell(($cbWidth-($marginLR*2))* .15, $lineHeight, iconv('UTF-8', 'windows-1252', $cashbookResultArray[$i]['titel']), 0, 0, 'L');
							if($pdf->getstringwidth(iconv('UTF-8', 'windows-1252', $cashbookResultArray[$i]['bemerkung'])) > 20){
								$posY=$pdf->GetY();
								$posX=120;
								$pdf->MultiCell(($cbWidth-($marginLR*2))* .30, $lineHeight, iconv('UTF-8', 'windows-1252', $cashbookResultArray[$i]['bemerkung']), 0, 'L', 0); 
							}
							else $pdf->Cell(($cbWidth-($marginLR*2))* .30, $lineHeight, iconv('UTF-8', 'windows-1252', $cashbookResultArray[$i]['bemerkung']), "B", 1, 'L');
	

						$sumBetrag+=$cashbookResultArray[$i]['betrag'];
						$sumUst+=$cashbookResultArray[$i]['ust'];
						$sumVorst+=$cashbookResultArray[$i]['vorst'];
				}
					# Summenzeile
				$pdf->Cell(20, $lineHeight, "Summen", 0, 0, 'L');
				$pdf->Cell(20, $lineHeight, number_format($sumBetrag, 2, ",", "."), 0, 0, 'R');							
				$pdf->Cell(20, $lineHeight, number_format($sumUst, 2, ",", "."), 0, 0, 'R');
				$pdf->Cell(20, $lineHeight, number_format($sumVorst, 2, ",", "."), 0, 0, 'R');

				$pdf->Cell(20, $lineHeight, iconv('UTF-8', 'windows-1252', ''), 0, 1, 'L');
				$kassastand=$sumBetrag+$kassabuchUebertrag;
				$pdf->Cell(80, $lineHeight, "Kassastand: ", 0, 0, 'L');
				$pdf->Cell(20, $lineHeight, number_format($kassastand, 2, ",", "."), 0, 1, 'R');
				$pdf->Cell(20, $lineHeight*4, "", "B", 1, 'L');
	}
			
			$docNa='kassenbuch';
			
			$outputTarget=__DIR__ ."\docs\sIT_".$docNa."_".$cashbookId."_".date('d_m_H_i').".pdf";
			$command="C:\sitpos\pdfxchange\App\PDFV_Portable\PDFXCview.exe /print:printer=\"$drucker\" \"$outputTarget\" ";
				
			$writeFile=$pdf->Output($outputTarget, "F");
			return "docs/sIT_".$docNa."_".$cashbookId."_".date('d_m_H_i').".pdf";

	
	
	}




























































?>