<?php
if (! $_REQUEST ['record'] || $_REQUEST ['record'] == '')
	die ( 'Brak rekordu' );
else
	$record = $_REQUEST ['record'];

include_once ("modules/EcmInvoiceOuts/PDFTemplate/helper.php");

$focus = new EcmInvoiceOut ();
$focus->retrieve ( $record );

$user = new User ();
$user->retrieve ( $focus->assigned_user_id );
$labels = return_module_language($focus->ecmlanguage, 'EcmInvoiceOuts');
include_once ("include/MPDF57/mpdf.php");
$p = new mPDF ( '', 'A4', null, 'helvetica', 10, 10, 30, 45, 5, 5 );
$mpdf->mirrorMargins = 1;

$db = $GLOBALS ['db'];
// document pdf
// document already exist?
$recipient_code=0;
foreach ($focus->getPositionList ( true ) as $k=>$v){
    if($v['recipient_code']!=''){
        $recipient_code=1;
    }
}
$recipient_code;
$res = $db->query ( "SELECT footer, content, header FROM ecminvoiceout_pdf WHERE id='$record'" );

// get prepaid invoices
/*
$result =  $GLOBALS ['db']->query ( "select SUM(inv_value) as total, ecmvat_name FROM ecmprepaymentinvoices WHERE ecmsale_id
='".$focus->so_id."' GROUP BY ecmvat_name" );
$prepaids = array();
while ( $row = $GLOBALS['db']->fetchByAssoc ( $result ) ) {
	$prepaids[$row['ecmvat_name']] = $row['total'];
}
*/

if ($res->num_rows == 0) {
	// create and save document
	$positions = formatPDFPositions ( $focus->getPositionList ( true ), $focus );
	
	foreach ($positions as &$v){
		if (strlen($v['product_ean']) > 0) {
			$v['product_code'] = $v['product_code'].', EAN '.$v['product_ean'];
		}
		if (!empty($focus->wz_id)) {
			$wzItem = $db->fetchByAssoc($db->query("SELECT part_no FROM ecmstockdocoutitems WHERE id='".$v['product_parent_doc_item_id']."'"));
			if (!empty($wzItem['part_no'])) {
				if ($focus->pdf_type=='K') {
					$v['product_code'] = $v['product_code'].'<br>Numer partii '.$wzItem['part_no'];
				} else {
					$v['product_code'] = $v['product_code'].'<br>Part no. '.$wzItem['part_no'];
				}
			}
		}
	}
	
	$header = '';
	$footer = '';
	$content = '';
	if($focus->pdf_type=='K'){
		include_once ("modules/EcmInvoiceOuts/PDFTemplate/tpl/header.php");
		if ($focus->type == 'normal')
			include_once ("modules/EcmInvoiceOuts/PDFTemplate/tpl/content_k.php");
		if ($focus->type == 'correct')
			include_once ("modules/EcmInvoiceOuts/PDFTemplate/tpl/content_k_correct.php");
		include_once ("modules/EcmInvoiceOuts/PDFTemplate/tpl/footer-pl_pl.php");
	} else {
	    include_once ("modules/EcmInvoiceOuts/PDFTemplate/tpl/header_en.php");
	    if ($focus->type == 'normal')
	        include_once ("modules/EcmInvoiceOuts/PDFTemplate/tpl/content_en.php");
	    if ($focus->type == 'correct')
	        include_once ("modules/EcmInvoiceOuts/PDFTemplate/tpl/content_en_correct.php");
	    include_once ("modules/EcmInvoiceOuts/PDFTemplate/tpl/footer-en_us.php");
	}
	global $current_user,$app_list_strings;
	$db->query ( "INSERT INTO ecminvoiceout_pdf VALUES ('$record','$current_user->id', NOW(),'" . urlencode ( $footer ) . "', '" . urlencode( $content ) . "', '" . urlencode( $header ) . "')" );
} else {
	$row = $db->fetchByAssoc ( $res );
	$footer = urldecode ( $row ['footer'] ); 
	$header = urldecode ( $row ['header'] ); 
	$content = urldecode ( $row ['content'] );
}

$p->SetHTMLHeader ( $header );
$p->SetHTMLFooter($footer);
$p->WriteHTML ( $content );
//echo $content;
//return;
// draw PDF
$filename = str_replace(" ","_", str_replace("/", "_", $focus->document_no));
$p->Output ($filename.'.pdf', 'I');

