<?php
$db = $GLOBALS ['db'];
ini_set('display_errors',1);
if (! $_REQUEST ['record'] || $_REQUEST ['record'] == '')
	die ( 'Brak rekordu' );
else
	$record = $_REQUEST ['record'];

include_once ("modules/EcmKpkw/PDFTemplate/helper.php");
include_once ("modules/EcmKpkw/PDFTemplate/kwota.php");

$focus = new EcmKpkw ();
$focus->retrieve ( $record );
$kwota=new KwotaSlownie();

$user = new User ();
$user->retrieve ( $focus->created_by );

$acc = new Account();
$acc->retrieve ( $focus->parent_id );

$cur = New Currency();
$cur->retrieve($focus->currency_id);
$r=$db->query ("select name,footer_address,footer_nip from ecmdocumenttemplates where id='75997203-f430-7f64-10e0-4b0a912673fa'");
$comp=$db->fetchByAssoc($r);
$addr = explode(",", $comp['footer_address']);


include_once ("include/MPDF57/mpdf.php");
$p = new mPDF ( '', 'A4', null, 'helvetica', 10, 10, 30, 45, 5, 5 );
$mpdf->mirrorMargins = 1;


// document pdf
// document already exist?
$res = $db->query ( "SELECT footer, content FROM ecmstockdocin_pdf WHERE id='$record'" );

if ($res->num_rows == 0) {
	// create and save document
	//$positions = formatPDFPositions ( $focus->getPositionList ( true ), $focus );
	// get header
	$header = '';
	include_once ("modules/EcmKpkw/PDFTemplate/header.php");
	$content = '';
	include_once ("modules/EcmKpkw/PDFTemplate/content.php");
	
	global $current_user;
	$db->query ( "INSERT INTO ecmstockdocin_pdf VALUES ('$record','$current_user->id', NOW(),'" . mysql_real_escape_string ( $header ) . "', '" . mysql_real_escape_string ( $content ) . "')" );

} else {
	//$positions = formatPDFPositions ( $focus->getPositionList ( true ), $focus );
	$row = $db->fetchByAssoc ( $res );
	$header = htmlspecialchars_decode ( $row ['footer'] ); // punk rock!
	$content = htmlspecialchars_decode ( $row ['content'] );
		$header = '';
	include_once ("modules/EcmKpkw/PDFTemplate/header.php");
	$content = '';
	include_once ("modules/EcmKpkw/PDFTemplate/content.php");
	
}
$p->SetHTMLHeader ( $header );
$p->WriteHTML ( $content );

// draw PDF
$p->Output ();

