<?php

if (count ( $_REQUEST ['mass'] )) {
	
	
	$db = $GLOBALS ['db'];
	$zap = $db->query ( "select id,document_no from ecminvoiceouts where id in ('" . implode ( "','", $_REQUEST ['mass'] ) . "') order by register_date asc" );
	$n = new EcmInvoiceOut ();
	$files = [ ];
	
	while ( $dane = $db->fetchByAssoc ( $zap ) ) {
		
		$files [] = createEcmInvoiceOutPdf ( $dane ['id'], 'FILE' );
	}
	

	
	include 'PDFMerger.php';
	
	
	if (count ( $files ) > 0) {
		
		$pdf = new PDFMerger ();
		foreach ( $files as $file ) {
			$pdf->addPDF ( $file );
		}
		try {
			$pdf->merge ( "file", 'faktury.pdf' );
		} catch ( Exception $e ) {
		
		}
		header ( "Location: faktury.pdf" );
	}
}


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;
}
?>