<?php
include_once ("includes/setup.inc.php");

$current_year = date('Y');
$program_sc = fetch_postget_val('program_sc',$_POST, $_GET);
$daerah_sc = fetch_postget_val('daerah_sc',$_POST, $_GET);
$dun_sc = fetch_postget_val('dun_sc',$_POST, $_GET);
$tahun_sc_year = fetch_postget_val('tahun_sc_year',$_POST, $_GET);

$PDO_stmt = $PDO->prepare("SELECT * FROM i_daerah WHERE id_daerah = ?");
$PDO_stmt->bindValue(1, $daerah_sc, PDO::PARAM_INT);
$PDO_stmt->execute();
$daerah_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

$PDO_stmt = $PDO->prepare("SELECT * FROM i_dun WHERE id_dun = ?");
$PDO_stmt->bindValue(1, $dun_sc, PDO::PARAM_INT);
$PDO_stmt->execute();
$dun_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

$PDO_stmt = $PDO->prepare("SELECT * FROM i_program WHERE id_program = ?");
$PDO_stmt->bindValue(1, $program_sc, PDO::PARAM_INT);
$PDO_stmt->execute();
$program_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

$PDO_stmt = $PDO->prepare("SELECT * FROM i_kawalan LEFT JOIN i_bayaranfasa ON i_kawalan.id_bayaranfasa_tunai = i_bayaranfasa.id_bayaranfasa WHERE id_program = ?");
$PDO_stmt->bindValue(1, $program_sc, PDO::PARAM_INT);
$PDO_stmt->execute();
$kawalan_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);
$cutoff = $kawalan_List['tkh_cutoff_tunai'];
$cutoff_saheft = $kawalan_List['tkh_cutoff_sah_EFT'];
$cutoff_aktif = $kawalan_List['tkh_cutoff_aktifmohon'];

$bayaran_table = "m_bayaran_".$program_List['kod_program'];
/*
$query = "SELECT m_pemohon.*, i_poskod.desk_poskod, i_negeri.desk_negeri, i_dun.desk_dun, i_daerah.desk_daerah 
FROM m_pemohon
LEFT JOIN i_dun ON m_pemohon.id_dun = i_dun.id_dun 
LEFT JOIN i_daerah ON i_dun.id_daerah = i_daerah.id_daerah 
LEFT JOIN i_poskod ON m_pemohon.id_poskod = i_poskod.id_poskod 
LEFT JOIN i_negeri ON m_pemohon.id_negeri = i_negeri.id_negeri 
WHERE 
m_pemohon.id_program = ? 
AND m_pemohon.id_statusmohon = 4 
AND m_pemohon.id_statushidup = 1 
AND m_pemohon.id_aktifmohon = 1 
AND m_pemohon.id_dun = ? 
AND i_daerah.id_daerah = ? 
AND m_pemohon.id_pemohon NOT IN (SELECT id_pemohon FROM $bayaran_table WHERE tahun_bayar = ?)
AND m_pemohon.tkh_sah <= ?
AND (m_pemohon.tkh_saheft = ? OR m_pemohon.tkh_saheft >= ?)
ORDER BY nama_pemohon ";
*/

$tahun_bayar = $tahun_sc_year;
$select_column = "SELECT A.*, i_dun.desk_dun, i_daerah.desk_daerah  ";
$query = getNonBayarTunaiQuery($PDO, $select_column, $cutoff, $cutoff_saheft, $tahun_bayar, $cutoff_aktif);
//$query .= ' AND A.id_program = ? ';
if($tahun_bayar != $current_year)
{
	$query .= ' AND A.id_program_ori = ? ';
}
else
{
	$query .= ' AND A.id_program = ? ';
}
$query .= ' AND A.id_dun = ? ';
$query .= ' AND i_daerah.id_daerah = ? ';
$query .= ' ORDER BY nama_pemohon ';
// LIMIT 3

$PDO_stmt = $PDO->prepare($query);
$PDO_stmt->bindValue(1, $program_sc, PDO::PARAM_INT);
$PDO_stmt->bindValue(2, $dun_sc, PDO::PARAM_INT);
$PDO_stmt->bindValue(3, $daerah_sc, PDO::PARAM_INT);
$PDO_stmt->execute();
$pemohon_List_all = [];
while($result = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
{
	$pemohon_List_all[] = $result;
}

$TITLE = 'Daftar Buku Bank '.$program_List['desk_program'];
$AUTHOR = 'jj';
$FILE_NAME = str_replace(' ', '_', $TITLE);
//***********************************************************************************************************************
// PRINT PDF
//***********************************************************************************************************************

require_once('classes/tcpdf/config/tcpdf_config.php');
require_once('classes/tcpdf/tcpdf.php');
include_once ("classes/phpqrcode/qrlib.php");

//include_once ("inc/setup.inc.php");
error_reporting(0);

// Extend the TCPDF class to create custom Header and Footer
class MYPDF extends TCPDF {

	private $pageTitle = '';
	public function setHeaderTitle($title)
	{
		$this->pageTitle = $title;
	}
	
	//Page header
	public function Header() {
		$this->SetFont('helveticaB', '', 14); //CHANGE THIS FOR TITLE FONT
		$this->MultiCell(0, 0, $this->pageTitle, 0, 'C', 0, 1, '', '', true);
	}

	// Page footer
	public function Footer() {
		// Position at 15 mm from bottom
		$this->SetFont('helveticaB', '', 10);
		$this->MultiCell(0, 0, 'Muka Surat '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, 'C', 0, 1, '', '', true);
	}
}

$PDF_MARGIN_TOP = 20;

// -----------------------------------------------------------------------------
// create new PDF document
$pdf = new MYPDF("P", PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); //P for portrait, L for landscape
$pdf->setHeaderTitle($program_List['desk_program']);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor($AUTHOR);
$pdf->SetTitle($TITLE);
$pdf->SetSubject($TITLE);
$pdf->SetKeywords('TCPDF, PDF, $TITLE');

// 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(PDF_MARGIN_LEFT, PDF_MARGIN_TOP + $PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetMargins(10, $PDF_MARGIN_TOP, 10);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

//set auto page breaks
$pdf->SetAutoPageBreak(FALSE, PDF_MARGIN_BOTTOM);

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

//set some language-dependent strings
$pdf->setLanguageArray($l);

// -----------------------------------------------------------------------------
$HEADER_HTML = '<table border="1" style="padding:3px;text-align:center">';
	$HEADER_HTML .= '<tr style="height:50px;vertical-align: middle;">';
		$HEADER_HTML .= '<th style="vertical-align:bottom ;width:5%;height:50px">BIL.</th>';
		$HEADER_HTML .= '<th style="vertical-align:bottom ;width:20%">NAMA</th>';
		$HEADER_HTML .= '<th style="vertical-align:bottom ;width:20%">NO.KP BARU</th>';
		$HEADER_HTML .= '<th style="vertical-align:middle;width:25%">MOD BAYARAN</th>';
		$HEADER_HTML .= '<th style="vertical-align:middle;width:15%">T.T & TARIKH</th>';
		$HEADER_HTML .= '<th style="vertical-align: middle;width:15%">CAP JARI</th>';
	$HEADER_HTML .= '</tr>';
	$HEADER_HTML .= 'CONTENT_HERE';
$HEADER_HTML .= '<p style="text-align:right;">'.$dun_List['desk_dun'].'</p>';
$HEADER_HTML .= '</table>';

$pdf->SetFont('helvetica', '', 10);
$HTML = '';
for($num = 0 ; $num < count($pemohon_List_all); $num++)
{
	$pemohon_List = $pemohon_List_all[$num];
	if($num > 0 && $num % 10 == 0)
	{
		$pdf->AddPage();// add a page
		$pdf->writeHTMLCell(0, 0, '', '', str_replace('CONTENT_HERE', $HTML, $HEADER_HTML), 0, 1, 0, true, '', true);
		$HTML = '';
	}
	
	$HTML .= '<tr>';
		$HTML .= '<td style="height:80px">'.($num+1).'</td>';
		$HTML .= '<td>'.$pemohon_List['nama_pemohon'].'</td>';
		$HTML .= '<td >'.$pemohon_List['kpbaru_pemohon'].'</td>';
		$HTML .= '<td>';
			$HTML .= '<table style="padding:3px;">';
			$HTML .= '<tr>';
				$HTML .= '<td style="width:80%;height:25px">TIADA MASALAH</td>';
				$HTML .= '<td style="width:10%">(</td>';
				$HTML .= '<td style="width:10%">)</td>';
			$HTML .= '</tr>';
			$HTML .= '<tr>';
				$HTML .= '<td style="height:25px">PINDAAN</td>';
				$HTML .= '<td>(</td>';
				$HTML .= '<td>)</td>';
			$HTML .= '</tr>';
			$HTML .= '<tr>';
				$HTML .= '<td style="height:25px">MELALUI WAKIL</td>';
				$HTML .= '<td>(</td>';
				$HTML .= '<td>)</td>';
			$HTML .= '</tr>';
			$HTML .= '</table>';
		$HTML .= '</td>';
		$HTML .= '<td></td>';
		$HTML .= '<td></td>';
	$HTML .= '</tr>';
	
}
if(count($pemohon_List_all) % 10 != 0)
{
	$pdf->AddPage();// add a page
	$pdf->writeHTMLCell(0, 0, '', '', str_replace('CONTENT_HERE', $HTML, $HEADER_HTML), 0, 1, 0, true, '', true);
	$HTML = '';
}
//Close and output PDF document
ob_end_clean();
$pdf->Output($FILE_NAME.'.pdf', 'I');

//***********************************************************************************************************************
// END PRINT PDF
//***********************************************************************************************************************

?>

