<?php

//Include the main TCPDF library (search for installation path).
$this->mypdf_class->tcpdf();

global $diamond_data_pdf ;


$diamond_data_pdf = $data['diamond_data'];
// echo "<pre>"; print_r($diamond_data_pdf); exit;

// Extend the TCPDF class to create custom Header and Footer
class MYPDF extends TCPDF 
{
    //Page header
    public function Header() 
    {
    	global $diamond_data_pdf;
    	
    	$stock_id 	= empty($_GET['search_value_stock_id']) ? 'ALL' : $diamond_data_pdf[0]['dm_code'];
    	$customer 	= empty($_GET['search_value_party']) ? 'ALL' : $diamond_data_pdf[0]['crm_customer'];
    	$broker  	= empty($_GET['search_value_broker']) ? 'ALL' : $diamond_data_pdf[0]['crm_broker'];
    	$start_date = empty($_GET['search_value_start_date']) ? '' : $_GET['search_value_start_date'];
    	$end_date 	= empty($_GET['search_value_end_date']) ? '' : $_GET['search_value_end_date'];
    	$entry_no 	= empty($_GET['search_value_entry_no']) ? '' : $_GET['search_value_entry_no'];
    	$cert 		= 'ALL';

    	if(!empty($_GET['search_value_cert']))
    	{
    		if($_GET['search_value_cert'] == 1)
	    	{
	    		$cert 		= 'YES';
	    	}
	    	else
	    	{
	    		$cert 		= 'NO';
	    	}
    	}
    		
    	$this->SetFont('Helvetica', 'B', 8);
		
		$tbl_header = "";
		$tbl_header .= <<<EOD
		<table border="0" cellpadding="3">
			<tr>
				<td colspan="2" align="center">
					<b>SOLD REPORT FROM $start_date TO $end_date<br/></b>
				</td>
			</tr>
		</table>

		<table border="1">
			<tr colspan="5">
				<td align="center">
					<b>SEARCH CRITERIA</b>
				</td>
			</tr>
		</table>
		<table border="1">
			<tr>
				<td align="center">
					<b>STOCK ID : $stock_id</b>
				</td>

				<td align="center">
					<b>PARTY : $customer</b>
				</td>
				<td align="center">
					<b>BROKER : $broker</b>
				</td>
				<td align="center">
					<b>ENTRY NO : $entry_no</b>
				</td>
				<td align="center">
					<b>CERT : $cert</b>
				</td>
			</tr>
		</table>
		<table border="0">
			<tr>
				<td align="center">
					
				</td>
				<td align="center">
					
				</td>
				<td align="center">
					
				</td>
				<td align="center">
					
				</td>
			</tr>			
		</table>
EOD;
		$tbl_header .= <<<EOD
		<table cellpadding="6" border="1">
			<tr>
				<td width="5%" align="center"><b>SR NO</b></td>
				<td width="6%" align="center"><b>STOCK ID</b></td>
				<td width="4%" align="center"><b>SH</b></td>
				<td width="4%" align="center"><b>WT</b></td>
				<td width="4%" align="center"><b>COL</b></td>
				<td width="4%" align="center"><b>CLA</b></td>
				<td width="4%" align="center"><b>CUT</b></td>
				<td width="4%" align="center"><b>POL</b></td>
				<td width="4%" align="center"><b>SYM</b></td>
				<td width="4%" align="center"><b>FL</b></td>
				<td width="4%" align="center"><b>LAB</b></td>
				<td width="4%" align="center"><b>DISC</b></td>
				<td width="6%" align="center"><b>RAP</b></td>
				<td width="12%" align="center"><b>PARTY</b></td>
				<td width="12%" align="center"><b>BROKER</b></td>
				<td width="8%" align="center"><b>ENTRY DATE</b></td>
				<td width="7%" align="center"><b>ENTRY NO</b></td>
				<td width="4%" align="center"><b>CERT</b></td>
			</tr>
		</table>
EOD;

		$this->writeHTML($tbl_header, true, false, false, false, '');
		$yy = $this->GetY();
		$yy = $yy - 10;
		
		$this->SetTopMargin($yy + 3);
    }

    // Page footer
    public function Footer() 
    {
        // Set font
        $this->SetFont('helvetica', 'I', 8);
        // Page number
        $this->Cell(0, 0, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
    }
}

// create new PDF document
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Imran Khan');
$pdf->SetTitle('Issue Report Pdf');
$pdf->SetSubject('Issue Report Pdf');
// $pdf->SetKeywords('TCPDF, PDF, example, test, guide');

// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
$pdf->SetMargins(5, PDF_MARGIN_TOP, 5);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(10);

// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, 10);

// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
    require_once(dirname(__FILE__).'/lang/eng.php');
    $pdf->setLanguageArray($l);
}

// ---------------------------------------------------------

// set font
// $pdf->SetFont('times', 'BI', 12);

$pdf->SetFont('Helvetica', '', 8);
$row_cnt = count($diamond_data_pdf);
// echo $row_cnt;exit;

$tbl = "";
$pdf->AddPage('L');
	$tbl .= <<<EOD
		<table  border="1"  cellpadding="3" >
EOD;
	if ($row_cnt > 0) 
	{
	    $sr_no = 0;
        
		foreach ($diamond_data_pdf as $key => $value) 
		{
			$sr_no 			+= 1;
			$dm_code 		= $value['dm_code'];
			$dm_sh 			= $value['dm_sh'];
			$dm_wt 			= $value['dm_wt'];
			$dm_col 		= $value['dm_col'];
			$dm_cla 		= $value['dm_cla'];
			$dm_cut 		= $value['dm_cut'];
			$dm_pol 		= $value['dm_pol'];
			$dm_sym 		= $value['dm_sym'];
			$dm_fl 			= $value['dm_fl'];
			$dm_lab 		= $value['dm_lab'];
			$dm_disc 		= $value['dm_disc'];
			$dm_rap 		= $value['dm_rap'];
			$crm_customer	= $value['crm_customer'];
			$crm_broker		= $value['crm_broker'];
			$crm_entry_date	= $value['crm_entry_date'];
			$crm_entry_no	= $value['crm_entry_no'];
			$crt_dm_cert	= $value['crt_dm_cert'] == 1 ? 'YES' : 'NO';

			
			
			$tbl .= <<<EOD
					<tr>
						<td width="5%" align="center">$sr_no</td>
						<td width="6%" align="center">$dm_code</td>
						<td width="4%" align="center">$dm_sh</td>
						<td width="4%" align="center">$dm_wt</td>
						<td width="4%" align="center">$dm_col</td>
						<td width="4%" align="center">$dm_cla</td>
						<td width="4%" align="center">$dm_cut</td>
						<td width="4%" align="center">$dm_pol</td>
						<td width="4%" align="center">$dm_sym</td>
						<td width="4%" align="center">$dm_fl</td>
						<td width="4%" align="center">$dm_lab</td>
						<td width="4%" align="center">$dm_disc</td>
						<td width="6%" align="center">$dm_rap</td>
						<td width="12%" align="center">$crm_customer</td>
						<td width="12%" align="center">$crm_broker</td>
						<td width="8%" align="center">$crm_entry_date</td>
						<td width="7%" align="center">$crm_entry_no</td>
						<td width="4%" align="center">$crt_dm_cert</td>
					</tr>
EOD;
		}
		$tbl .= <<<EOD
				</table>
EOD;
	}
	else
	{
		$tbl .= <<<EOD
				<table  border="1"  cellpadding="3" >
					<tr>
						<td width="100%" align="center"><b>No Record Found</b></td>
					</tr>
				</table>
EOD;
	}
		
$pdf->writeHTML($tbl, true, false, false, false, '');
$pdf->IncludeJS("print();");
// ---------------------------------------------------------

//Close and output PDF document
ob_end_clean();
$pdf->Output('Sold_report.pdf', 'I');

//============================================================+
// END OF FILE
//============================================================+