<?php

include_once 'modules/EcmStockDocOuts/createPDF.php';
global $current_user;
$db=$GLOBALS['db'];
$query=$_SESSION[$current_user->id.'_pdf_ecmstockdocouts'];

$result = $db->query($query);

$id=create_guid();
$files=[];
$focus = new EcmStockDocOut ();
while($dane=$db->fetchByAssoc($result)){


	$focus->retrieve($dane['id']);
	$filename= str_replace(" ","_", str_replace("/", "_", $focus->document_no)).'.pdf';

	createPDF($focus->id,'F','pdfkopie/'.$filename);
	$db->query("insert into files set name='".$filename."', executed=0, zlecenie_id='$id', email='".$current_user->email1."'");

}
echo "Generacja PDF została zlecona, za chwilę otrzymasz email z adresem do pobrania paczki.";

/*
include './PDFMerger.php';
//require_once('fpdi/fpdi.php');
require_once('fpdf2/fpdf2.php');
require_once('fpdi/fpdi.php');
foreach($files as $key=>$val){

	$merge = new PDFMerger(false);

	foreach ($files[$key] as $name){
	
		if(is_file('pdfkopie/'.$name)){
			echo './pdfkopie/'.$name.'<br>';
			$merge->addPDF('pdfkopie/'.$name);
		}
		
	}
	echo './pdfkopie/'.$key.'.pdf<br>';
	 $merge->merge("file",'pdfkopie/'.$key.'.pdf');


	
}



/*
	$zip = new ZipArchive();
	$filename = "pdfkopie/paczka.zip";
	unlink($filename);
	if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
		exit("cannot open <$filename>\n");
	}
*/
?>