<?php
set_time_limit(9999);
if($_REQUEST['uid']){
	$_SESSION['cids_files']=array();
	$_SESSION['cids']=explode(",",$_REQUEST['uid']);
}
if(!$_REQUEST['no'])$_REQUEST['no']=0;

include_once("modules/EcmInvoiceOutOlds/EcmInvoiceOutOld.php");
$r=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select document_no,id from ecminvoiceoutolds where id='".$_SESSION['cids'][$_REQUEST['no']]."'"));
$in=new EcmInvoiceOutOld();
$in->retrieve($r['id']);
$ff="modules/EcmInvoiceOutOlds/files/".str_replace(" ","",str_replace("/","",$r['document_no'])).".pdf";
$in->getPDF($r['id'],"F",$ff);
$_SESSION['cids_files'][]=$ff;
chmod($ff,0777);
chown($ff,"e5crm.more7.com");

if(count($_SESSION['cids'])==$_REQUEST['no']-1){
	/*include_once("include/fpdi/fpdi.php");
	class Mao_FPDI extends FPDI { 
    	var $files = array ();
    	function setFiles($files) {
        	$this->files = $files;
		}
		function concat() {
       	 	$this->setPrintHeader(false);
        	$this->setPrintFooter(false);
        	foreach ( $this->files as $file ) {
            	$pagecount = $this->setSourceFile ( $file );
           		for($i = 1; $i <= $pagecount; $i ++) {
                	$tplidx = $this->ImportPage ( $i );
               		$s = $this->getTemplatesize ( $tplidx );
               		$this->AddPage ( 'P', array ($s ['w'], $s ['h'] ) );
               		$this->useTemplate ( $tplidx );
            	}
       		}
    	}
	}*/
	require_once('include/pclzip/pclzip.lib.php');
	$f='modules/EcmInvoiceOutOlds/files/archive'.date("YmdHis").'.zip';
	$archive = new PclZip($f);
	foreach($_SESSION['cids_files'] as $a)$archive->add($a,PCLZIP_OPT_REMOVE_PATH,"modules/EcmInvoiceOutOlds/files/");
	$_SESSION['cids_files']=array();
	chmod($f,0777);
	header("Location: ".$f);
	/*$pdf = new Mao_FPDF ( );
	$pdf->setFiles ($_SESSION['cids_files']);
	$pdf->concat (); 
	$pdf->Output ( 'modules/EcmInvoiceOutOlds/files/archive'.date("YmdHis").'.pdf', 'D' );*/
}
else {
	header("Location: index.php?module=EcmInvoiceOutOlds&action=printAll&to_pdf=1&no=".($_REQUEST['no']+1));
}
?>
