<?php

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

global $diamond_data_pdf ;
global $history_data_pdf ;


$diamond_data_pdf = isset($data['diamond_data']) ? $data['diamond_data'] : [];
$history_data_pdf = isset($data['history_data']) ? $data['history_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']) ? '' : $diamond_data_pdf[0]['dm_code'];
    	$dm_sh 		= empty($_GET['search_value_stock_id']) ? '' : $diamond_data_pdf[0]['dm_sh'];
    	$dm_wt 		= empty($_GET['search_value_stock_id']) ? '' : $diamond_data_pdf[0]['dm_wt'];
    	$dm_col 	= empty($_GET['search_value_stock_id']) ? '' : $diamond_data_pdf[0]['dm_col'];
    	$dm_cla 	= empty($_GET['search_value_stock_id']) ? '' : $diamond_data_pdf[0]['dm_cla'];
    	$dm_cut 	= empty($_GET['search_value_stock_id']) ? '' : $diamond_data_pdf[0]['dm_cut'];
    	$dm_pol 	= empty($_GET['search_value_stock_id']) ? '' : $diamond_data_pdf[0]['dm_pol'];
    	$dm_sym 	= empty($_GET['search_value_stock_id']) ? '' : $diamond_data_pdf[0]['dm_sym'];
    	$dm_fl 		= empty($_GET['search_value_stock_id']) ? '' : $diamond_data_pdf[0]['dm_fl'];
    	$dm_lab 	= empty($_GET['search_value_stock_id']) ? '' : $diamond_data_pdf[0]['dm_lab'];
    	$dm_disc 	= empty($_GET['search_value_stock_id']) ? '' : $diamond_data_pdf[0]['dm_disc'];
    	$dm_rap 	= empty($_GET['search_value_stock_id']) ? '' : $diamond_data_pdf[0]['dm_rap'];
    		
    	$this->SetFont('Helvetica', 'B', 8);
		
		$tbl_header = "";
		$tbl_header .= <<<EOD
		<table border="0" cellpadding="3">
			<tr>
				<td colspan="2" align="center">
					<b>HISTORY REPORT<br/></b>
				</td>
			</tr>
		</table>

		<table border="1">
			<tr colspan="1">
				<td align="center">
					<b>SEARCH CRITERIA</b>
				</td>
			</tr>
		</table>
		<table border="1">
			<tr>
				<td align="center">
					<b>STOCK ID</b>
				</td>
				<td align="center">
					<b>SHAPE</b>
				</td>
				<td align="center">
					<b>WEIGHT</b>
				</td>
				<td align="center">
					<b>COLOR</b>
				</td>
				<td align="center">
					<b>CLARITY</b>
				</td>
				<td align="center">
					<b>CUT</b>
				</td>
				<td align="center">
					<b>POLISH</b>
				</td>
				<td align="center">
					<b>SYMETRY</b>
				</td>
				<td align="center">
					<b>FLOUROSCENT</b>
				</td>
				<td align="center">
					<b>LAB</b>
				</td>
				<td align="center">
					<b>DISCOUNT</b>
				</td>
				<td align="center">
					<b>RAPNET</b>
				</td>
			</tr>
			<tr>
				<td align="center">
					$stock_id
				</td>
				<td align="center">
					$dm_sh
				</td>
				<td align="center">
					$dm_wt
				</td>
				<td align="center">
					$dm_col
				</td>
				<td align="center">
					$dm_cla
				</td>
				<td align="center">
					$dm_cut
				</td>
				<td align="center">
					$dm_pol
				</td>
				<td align="center">
					$dm_sym
				</td>
				<td align="center">
					$dm_fl
				</td>
				<td align="center">
					$dm_lab
				</td>
				<td align="center">
					$dm_disc
				</td>
				<td align="center">
					$dm_rap
				</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 align="center"><b>STATUS</b></td>
				<td align="center"><b>NO</b></td>
				<td align="center"><b>DATE</b></td>
				<td align="center"><b>PARTY</b></td>
				<td align="center"><b>BROKER</b></td>
				<td align="center"><b>USER</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($history_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 ($history_data_pdf as $key => $value) 
		{
			$status = '';
            if (!empty($value['crt_dm_status'])) 
            {
                if($value['crt_dm_status'] == 2)
                {
                    $status = "Hold";
                }
                else if($value['crt_dm_status'] == 3)
                {
                    $status = "Sold";
                }
                else
                {
                    $status = "Return";
                }
            }

            $crm_entry_no 	= empty($value['crm_entry_no']) ? '' : $value['crm_entry_no'] ;
            $crm_entry_date = empty($value['crm_entry_date']) ? '' : date('d-m-Y', strtotime($value['crm_entry_date'])) ;
            $crm_customer 	= empty($value['crm_cust_id']) ? '' : $value['crm_customer'] ;
            $crm_broker 	= empty($value['crm_broker_id']) ? '' : $value['crm_broker'] ;
            $crm_user 		= empty($value['crm_user']) ? '' : $value['crm_user'] ;

			$ct_status 		= empty($value['ct_status']) ? '' : 'Issue' ;
			$cm_entry_no 	= empty($value['cm_entry_no']) ? '' : $value['cm_entry_no'] ;
			$cm_entry_date 	= empty($value['cm_entry_date']) ? '' : date('d-m-Y', strtotime($value['cm_entry_date'])) ;
			$cm_customer 	= empty($value['cm_cust_id']) ? '' : $value['cm_customer'] ;
			$cm_broker 		= empty($value['cm_broker_id']) ? '' : $value['cm_broker'] ;
			$cm_user 		= empty($value['cm_user']) ? '' : $value['cm_user'] ;
			
			$tbl .= <<<EOD
			
				<tr>
                    <td align="center">$status</td>
                    <td align="center">$crm_entry_no</td>
                    <td align="center">$crm_entry_date</td>
                    <td align="center">$crm_customer</td>
                    <td align="center">$crm_broker</td>
                    <td align="center">$crm_user</td>
            	</tr>
            	<tr>
                    <td align="center">$ct_status</td>
                    <td align="center">$cm_entry_no</td>
                    <td align="center">$cm_entry_date</td>
                    <td align="center">$cm_customer</td>
                    <td align="center">$cm_broker</td>
                    <td align="center">$cm_user</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
//============================================================+