<?php 
ini_set("display_errors",1);
error_reporting(E_ALL);
ini_set ( 'max_execution_time', 9999999999 );
$db=$GLOBALS['db'];

include 'PDFMerger.php';

$pdf = new PDFMerger ();

$query="select id from ecmstockdocouts where delivery_date='2017-10-04' and deleted=0";

$res = $db->query($query);
while($dane = $db->fetchByAssoc($res)){
	$file = createEcmStockDocOutPdf($dane['id']);
	$pdf->addPDF ($file);
	$pdf->addPDF ($file);
	
}

$query="select id from ecminvoiceouts where register_date='2017-10-02' and deleted=0";

$res = $db->query($query);
while($dane = $db->fetchByAssoc($res)){
	$file = createEcmInvoiceOutPdf($dane['id']);
	$pdf->addPDF ($file);

	
}

try {
	$pdf->merge ( "file", 'mojpdf.pdf' );
} catch (Exception $e) {

	
}
echo "skończyłem!";




function createEcmStockDocOutPdf($record) {
	include_once ("modules/EcmStockDocOuts/PDFTemplate/helper.php");
	
	$focus = new EcmStockDocOut ();
	$focus->retrieve ( $record );
	
	$user = new User ();
	$user->retrieve ( $focus->assigned_user_id );
	
	include_once ("include/MPDF57/mpdf.php");
	$p = new mPDF ( '', 'A4', null, 'helvetica', 10, 10, 30, 45, 5, 5 );
	$labels = return_module_language ( $focus->ecmlanguage, 'EcmStockDocOuts' );
	$mpdf->mirrorMargins = 1;
	$db = $GLOBALS ['db'];
	$positions = formatPDFPositions22 ( $focus->getPositionList ( true ), $focus );
	$quantity = getQuantity22 ( $focus->getPositionList ( true ), $focus );
	$content = '';
	
	include ("modules/EcmStockDocOuts/PDFTemplate/content.php");
	
	$ec = new EcmSysInfo ();
	
	$footer = $ec->getFooterForModule ( 'EcmStockDocOuts' );
	$header = $ec->getHeaderForModule ( 'EcmStockDocOuts' );
	$p->SetHTMLHeader ( $header );
	$p->SetHTMLFooter ( $footer );
	$p->WriteHTML ( $content );
	
	$EcmSysInfo = new EcmSysInfo ();
	$dir = 'upload/' . $EcmSysInfo->getDatabaseName () . '/pdf/EcmStockDocOuts/';
	if (! is_dir ( $dir )) {
		mkdir ( $dir, '755', true );
	}
	$file_name = create_guid () . '.pdf';
	$p->Output ( $dir . $file_name, "F" );
	
	return $dir . $file_name;
}
function createEcmInvoiceOutPdf($record, $outputtype) {
	$EcmInvoiceOut = new EcmInvoiceOut ();
	$EcmInvoiceOut->retrieve ( $record );
	$file_location = $EcmInvoiceOut->get_PDF_file_path ( '0' );
	$EcmSysInfo = new EcmSysInfo ();
	if ($EcmSysInfo->getDatabaseName () == 'preDb_60b08fe051546309b61d2714d4a0438d') {
		$file_location = newPDFCreator ( $record, $type, true );
	}
	// return;
	switch ($outputtype) {
		case "BROSWER" :
			header ( "Location: " . $file_location );
			return true;
		case "FILE" :
			$path = $file_location;
			break;
		case "EMAIL" :
			include_once 'include/ECM/EcmSendMail/EcmSendMail.inc';
			$path = EcmSendMail::TEMP_DIR . 'ZS_' . $EcmInvoiceOut->number . '.pdf';
			copy ( $file_location, $path );
			
			break;
		case "MULTIPDF" :
			include_once 'include/ECM/EcmMultiPdf/EcmMultiPdf.inc';
			$path = EcmMultiPdf::TEMP_DIR . 'EcmSales_' . $focus->id . '_' . create_guid () . '.pdf';
			copy ( $file_location, $path );
			break;
	}
	;
	return $file_location;
}
?>