<?php

//add mz 2014-12-30
$type = isset($_REQUEST['file']) ? 'FILE' : 'BROSWER';
if ($_REQUEST['action'] == 'createPDF') {
	createEcmQuotePdf($_REQUEST['record'], $type);
}

global $current_user;
//toEmail?
$session_key = $current_user->id . "_EcmQuotes_toEmail";
if (isset($_SESSION[$session_key])) {
	$_REQUEST['record'] = $_SESSION[$session_key];
	unset($_SESSION[$session_key]);
	$toEmail = true;
}
unset($session_key);
//toMultiPdf?
$session_key = $current_user->id . "_EcmQuotes_toMultiPdf";
if (isset($_SESSION[$session_key])) {
	$_REQUEST['record'] = $_SESSION[$session_key];
	unset($_SESSION[$session_key]);
	$toMultiPdf = true;
}
unset($session_key);

function createEcmQuotePdf($record, $outputtype) {
	if (!$record || $record == '') {
		die('Brak rekordu');
	}

	//pare options
	if ($_REQUEST['pdf_opt'] && $_REQUEST['pdf_opt']!="") {
		$opt = explode(",", $_REQUEST['pdf_opt']);
		foreach ($opt as $o)
			$_REQUEST[$o] = '1';
	}
	include_once("modules/EcmQuotes/PDFTemplate/helper.php");

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

	$positions = formatPDFPositions($focus->getPositionList(true), $focus);

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

	$b = $GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("SELECT document_bank_account as dba FROM accounts WHERE id='" . $focus->parent_id . "'"));
	$bank = $b['dba'];

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

//get languages
	$focus->ecmlanguage = $_REQUEST['pdf_language'];
	$labels = return_module_language($focus->ecmlanguage, 'EcmQuotes');
	$header = '';
	$footer = '';
	include_once('modules/EcmSysInfos/EcmSysInfo.php');
	$EcmSysInfo = new EcmSysInfo();
$header = $EcmSysInfo->getHeaderForModule('EcmQuotes');
		$footer = $EcmSysInfo->getFooterForModule('EcmQuotes');
	$p->SetHTMLHeader($header);
	$p->SetHTMLFooter($footer);

//get content
	$content = '';
	include_once("modules/EcmQuotes/PDFTemplate/content.php");
//$p->WriteHTML($style);
	$p->WriteHTML($content);

	switch ($outputtype) {
		case "BROSWER" :
			$p->Output('of_' . str_replace('/', '', (str_replace(' ', '', $focus->document_no))) . '.pdf', "I");
			//return true;
		case "FILE" :
			unlink( 'upload/O_' . $focus->id . '.pdf');
			$path = 'upload/O_' . $focus->id . '.pdf';
			break;
		case "EMAIL" :
			include_once 'include/ECM/EcmSendMail/EcmSendMail.inc';
			$path = EcmSendMail::TEMP_DIR . 'ZS_' . $focus->id . '.pdf';
			break;
		case "MULTIPDF" :
			include_once 'include/ECM/EcmMultiPdf/EcmMultiPdf.inc';
			$path = EcmMultiPdf::TEMP_DIR . 'EcmSales_' . $focus->id . '_' . create_guid() . '.pdf';
			break;
	};
	$p->Output($path, "F");
	return $path;
}