<?php

//add mz 2014-12-30
//check session variable
if($_REQUEST['show']=='yes' && $_REQUEST['record2']!='')createEcmPaymentStatePdf('null','BROSWER','null',$_REQUEST['record2']);

function generatenumber(){
    $db=$GLOBALS['db'];
    $num=$db->fetchByAssoc($db->query("select count(*) as n from ecmpaymentstate_pdf where YEAR(date_entered)='".date("Y")."'"));
    $num['n']++;
    $tmp = strval($num['n']);
    while (strlen($tmp) < 5)
        $tmp = '0'.$tmp;
    
    return $tmp.'/'.date("Y");
    
}
function calculatetotal($p){
    $total=0;

    foreach ($p as $k=>$v){
       
        $total+=$p[$k]['total'];
    }
    return $total;
}
function calculatetotal2($p){
    $total=0;

    foreach ($p as $k=>$v){
         
        $total+=$p[$k]['total']+$p[$k]['paid'];
    }
    return $total;
}
function calculatetotal3($p){
    $total=0;

    foreach ($p as $k=>$v){
         
        $total+=$p[$k]['paid'];
    }
    return $total;
}
function createEcmPaymentStatePdf($record, $outputtype,$positions,$id) {
  
    include_once ("modules/EcmPaymentStates/PDFTemplate/helper.php");
    
    $focus = new EcmTransaction ();
    $focus->retrieve ( $record );
    $a= new Account();
    $a->retrieve($focus->parent_id);
    $user = new User ();
    $user->retrieve ( $focus->assigned_user_id );
    $labels = return_module_language($focus->ecmlanguage, 'EcmPaymentStates');
    include_once ("include/MPDF57/mpdf.php");
    $p = new mPDF ( '', 'A4', null, 'helvetica', 10, 10, 30, 45, 5, 5 );
    $mpdf->mirrorMargins = 1;
    
    $db = $GLOBALS ['db'];
    // document pdf
    // document already exist?
    $focus->total=calculatetotal2($positions);
    $res = $db->query ( "SELECT footer, content FROM ecmpaymentstate_pdf WHERE id='$id'" );
    if ($res->num_rows == 0) {
    	// create and save document
    	$positions = formatPDFPositions ( $positions, $focus );
    	// get header
    	$header = '';
    	include ("modules/EcmPaymentStates/PDFTemplate/header.php");
    	$content = '';
    	include("modules/EcmPaymentStates/PDFTemplate/content.php");
    	
    	global $current_user,$app_list_strings;
    	
    	$db->query ( "INSERT INTO ecmpaymentstate_pdf VALUES ('$id','$current_user->id', NOW(),'" . urlencode ( $header ) . "', '" . urlencode( $content ) . "','')" );
    } else {
    	$row = $db->fetchByAssoc ( $res );
    	$header = urldecode ( $row ['footer'] ); // punk rock!
    	$content = urldecode ( $row ['content'] );
    }
    //var_dump($positions);
    $p->SetHTMLHeader ( $header );
    $footer='<hr><table style="font-size: 8pt; width: 100%;vertical-align:top;">
    		<tr><td width="25%">Saas SystemS Sp. z o.o.<br />
    		ul. Lipnowska 21-23<br />
    		87-100 Toruń<br />
    		NIP: 9562307719</td><td width="25%"></td><td width="20%">Bank: <br>Numer konta:</td><td>PKO BP<br> 53 1020 5011 0000 9002 0270 6323</td></tr></table>';
    $p->SetHTMLFooter($footer);
    $p->WriteHTML ( $content );
    //echo $content;
    // draw PDF

	switch ($outputtype) {
		case "BROSWER" :
			$p->Output ();
			return true;
		case "FILE" :
			$path = 'upload/fk_' . str_replace('/','',(str_replace(' ', '', $focus->document_no))) . '.pdf';
			break;
		case "EMAIL" :
			include_once 'include/ECM/EcmSendMail/EcmSendMail.inc';
			$path = EcmSendMail::TEMP_DIR . 'ZS_' . $focus->number . '.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;
}

function createEcmPaymentStatePdf2($record, $outputtype,$positions,$id) {

    include_once ("modules/EcmPaymentStates/PDFTemplate/helper.php");

    $focus = new EcmTransaction ();
    $focus->retrieve ( $record );
    $a= new Account();
    $a->retrieve($focus->parent_id);
    $user = new User ();
    $user->retrieve ( $focus->assigned_user_id );
    $labels = return_module_language($focus->ecmlanguage, 'EcmPaymentStates');
    include_once ("include/MPDF57/mpdf.php");
    $p = new mPDF ( '', 'A4', null, 'helvetica', 10, 10, 30, 45, 5, 5 );
    $mpdf->mirrorMargins = 1;

    $db = $GLOBALS ['db'];
    // document pdf
    // document already exist?
    $focus->total=calculatetotal($positions);
    $focus->interest=calculatetotal3($positions);

        // create and save document
        $positions = formatPDFPositions ( $positions, $focus );
        // get header
        $header = '';
        include ("modules/EcmPaymentStates/PDFTemplate/header.php");
        $content = '';
        include("modules/EcmPaymentStates/PDFTemplate/content2.php");
         
        global $current_user,$app_list_strings;

    //var_dump($positions);
    $p->SetHTMLHeader ( $header );
    $footer='<hr><table style="font-size: 8pt; width: 100%;vertical-align:top;">
    		<tr><td width="25%">Saas SystemS Sp. z o.o.<br />
    		ul. Lipnowska 21-23<br />
    		87-100 Toruń<br />
    		NIP: 9562307719</td><td width="25%"></td><td width="20%">Bank: <br>Numer konta:</td><td>PKO BP<br> 53 1020 5011 0000 9002 0270 6323</td></tr></table>';
    $p->SetHTMLFooter($footer);
    $p->WriteHTML ( $content );
    //echo $content;
    // draw PDF

    switch ($outputtype) {
        case "BROSWER" :
            $p->Output ();
            return true;
        case "FILE" :
            $path = 'upload/fk_' . str_replace('/','',(str_replace(' ', '', $focus->id))) . '.pdf';
            break;
        case "EMAIL" :
            include_once 'include/ECM/EcmSendMail/EcmSendMail.inc';
            $path = EcmSendMail::TEMP_DIR . 'ZS_' . $focus->number . '.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;
}