<?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);
$baucer_tkh = fetch_postget_val('baucer_tkh',$_POST, $_GET);
$baucer_tkh_hingga = fetch_postget_val('baucer_tkh_hingga',$_POST, $_GET);
$baucer_from = fetch_postget_val('baucer_from',$_POST, $_GET);
$baucer_to = fetch_postget_val('baucer_to',$_POST, $_GET);
$baucer_tempat = fetch_postget_val('baucer_tempat',$_POST, $_GET);
$baucer_alamat = fetch_postget_val('baucer_alamat',$_POST, $_GET);

$baucer_from = str_replace('AM', 'PAGI', $baucer_from);
$baucer_from = str_replace('PM', 'PETANG', $baucer_from);
$baucer_to = str_replace('AM', 'PAGI', $baucer_to);
$baucer_to = str_replace('PM', 'PETANG', $baucer_to);

$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.*
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 
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.* ";
$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 = 'BAUCER TUNAI';
$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 {

	//Page header
	public function Header() {
		
	}

	// Page footer
	public function Footer() {
	}
}

$PDF_MARGIN_TOP = 30;
$HEADER_HTML = '';
$header = 1;
if($header == 1)
{
	$PDF_MARGIN_TOP = 5;
	$HEADER_HTML = '<table border="0" style="padding:3px">';
	
		$HEADER_HTML .= '<tr>';
			$HEADER_HTML .= '<td style="width:10%" rowspan="10"><img src="images/penang_logo.png" style="width:50px;height:80px"></td>';		
			$HEADER_HTML .= '<td style="width:40%"><br>'.$daerah_List['alamat'].'<br>Tel &nbsp;&nbsp;: '.$daerah_List['no_tel'].'<br>Web : http://isejahtera.penang.gov.my<br><br><br></td>';
			$HEADER_HTML .= '<td style="width:10%"></td>';
			$HEADER_HTML .= '<td style="width:40%;text-align:center;vertical-align:middle;color:red;font-size:30px" rowspan="3">'.$baucer_from.'<br></td>';
		$HEADER_HTML .= '</tr>';
		
		/*$HEADER_HTML .= '<tr>';
			$HEADER_HTML .= '<td style="width:10%" rowspan="6"><img src="images/penang_logo.png" style="width:50px;height:80px"></td>';		
			$HEADER_HTML .= '<td style="width:40%">PEJABAT DAERAH DAN TANAH</td>';
			$HEADER_HTML .= '<td style="width:12%"></td>';
			$HEADER_HTML .= '<td style="width:38%;text-align:center;vertical-align:middle;color:red;font-size:30px" rowspan="3">'.$baucer_from.'</td>';
		$HEADER_HTML .= '</tr>';
		$HEADER_HTML .= '<tr>';
			$HEADER_HTML .= '<td>'.$daerah_List['desk_daerah'].'</td>';
		$HEADER_HTML .= '</tr>';
		$HEADER_HTML .= '<tr>';
			$HEADER_HTML .= '<td>TINGKAT 50 & 51</td>';
		$HEADER_HTML .= '</tr>';
		$HEADER_HTML .= '<tr>';
			$HEADER_HTML .= '<td>KOMPLEKS TUN ABDUL RAZAK (KOMTAR)</td>';
			$HEADER_HTML .= '<td></td>';
			$HEADER_HTML .= '<td style="width:10%;text-align:right">Telefon</td>';
			$HEADER_HTML .= '<td style="text-align:center;width:3%">:</td>';
			$HEADER_HTML .= '<td style="width:25%">04-650-5511 / 5508</td>';
		$HEADER_HTML .= '</tr>';
		$HEADER_HTML .= '<tr>';
			$HEADER_HTML .= '<td>10000 PULAU PINANG</td>';
			$HEADER_HTML .= '<td></td>';
			$HEADER_HTML .= '<td></td>';
			$HEADER_HTML .= '<td style="text-align:center;">:</td>';
			$HEADER_HTML .= '<td>04-650-5538</td>';
		$HEADER_HTML .= '</tr>';
		$HEADER_HTML .= '<tr>';
			$HEADER_HTML .= '<td></td>';
			$HEADER_HTML .= '<td></td>';
			$HEADER_HTML .= '<td style="text-align:right;">Web</td>';
			$HEADER_HTML .= '<td style="text-align:center;">:</td>';
			$HEADER_HTML .= '<td>http://isejahtera.penang.gov.my</td>';
		$HEADER_HTML .= '</tr>';*/
	$HEADER_HTML .= '</table>';
}

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

// 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(PDF_MARGIN_LEFT, $PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$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);

// -----------------------------------------------------------------------------
$height = 4;

for($num = 0 ; $num < count($pemohon_List_all); $num++)
{
	$pemohon_List = $pemohon_List_all[$num];
	$pdf->AddPage();// add a page
	$pdf->SetFont('helvetica', '', 8);
	$pdf->writeHTMLCell(0, 0, '', '', $HEADER_HTML, 0, 1, 0, true, '', true);

	$errorCorrectionLevel ='L';
	$matrixPointSize = 1;
	$url = $pemohon_List['kpbaru_pemohon'];
	$filename = 'qrcodes/'.$pemohon_List['kpbaru_pemohon'].'.png';
	QRcode::png($url, $filename, $errorCorrectionLevel, $matrixPointSize, 1);
	
	$HTML = '<table border="0" style="padding:3px;border-top:1px solid black">';
		$HTML .= '<tr>';
			$HTML .= '<td rowspan="2" style="width:52%;font-size:20px"><b>'.$kawalan_List['desk_bayaranfasa'].' '.$kawalan_List['tahun_bayaran_tunai'].'</b></td>';
			$HTML .= '<td style="width:20%;text-align:right">Tarikh</td>';
			$HTML .= '<td style="text-align:center;width:3%">:</td>';
			$HTML .= '<td style="width:25%">'.date('d M Y').'</td>';
		$HTML .= '</tr>';
		$HTML .= '<tr>';
			$HTML .= '<td style="text-align:right">Keterangan Lanjut</td>';
			$HTML .= '<td style="text-align:center;">:</td>';
			$HTML .= '<td>'.$dun_List['nama_adun'].'</td>';
		$HTML .= '</tr>';
		$HTML .= '<tr>';
			$HTML .= '<td style="width:10%;"><b>No</b></td>';
			$HTML .= '<td style="width:3%;text-align:center;"><b>:</b></td>';
			$HTML .= '<td style="width:39%;font-size:20px"><b>('.($num + 1).') '.$dun_List['desk_dun'].'</b></td>';
			$HTML .= '<td style="width:23%;text-align:center"></td>';
			
			$telpej = '';
			if($dun_List['no_teladun'] != '')
			{				
				$telpej = $dun_List['no_teladun'].'/';
			}
			$HTML .= '<td style="width:25%;text-align:center">'.$telpej.$dun_List['no_telhpadun'].'</td>';
		$HTML .= '</tr>';
		$HTML .= '<tr style="font-weight:bold">';
			$HTML .= '<td style="width:10%;">Nama</td>';
			$HTML .= '<td style="width:3%;text-align:center;">:</td>';
			$HTML .= '<td>'.$pemohon_List['nama_pemohon'].'</td>';
			$HTML .= '<td style="width:20%;text-align:right">No KP</td>';
			$HTML .= '<td style="width:3%;text-align:center;">:</td>';
			$HTML .= '<td style="width:25%;">'.$pemohon_List['kpbaru_pemohon'].'</td>';
		$HTML .= '</tr>';
		$HTML .= '<tr style="font-weight:bold">';
			$HTML .= '<td>Alamat</td>';
			$HTML .= '<td style="text-align:center;">:</td>';
			$HTML .= '<td>'.$pemohon_List['alamat'].'</td>';
			$HTML .= '<td colspan="3" rowspan="3" style="text-align:center;"><img src="'.$filename.'" style="width:90px;height:90px"></td>';
		$HTML .= '</tr>';
		$HTML .= '<tr style="font-weight:bold">';
			$HTML .= '<td></td>';
			$HTML .= '<td></td>';
			$HTML .= '<td>'.$pemohon_List['alamat2'].' '.$pemohon_List['alamat3'].'</td>';
		$HTML .= '</tr>';
		$HTML .= '<tr style="font-weight:bold">';
			$HTML .= '<td></td>';
			$HTML .= '<td></td>';
			$HTML .= '<td>'.$pemohon_List['desk_poskod'].' '.$pemohon_List['desk_negeri'].'</td>';
		$HTML .= '</tr>';
		$HTML .= '<tr style="font-weight:bold">';
			$HTML .= '<td>No Tel</td>';
			$HTML .= '<td style="text-align:center;">:</td>';
			$HTML .= '<td>'.$pemohon_List['tel_hp'].'</td>';
			$HTML .= '<td colspan="3" style="text-align:center;font-size:10px">Mykad : '.$pemohon_List['kpbaru_pemohon'].'</td>';
		$HTML .= '</tr>';
	$HTML .= '</table>';
	$pdf->SetFont('helvetica', '', 10);
	$pdf->writeHTMLCell(0, 0, '', '', $HTML, 0, 1, 0, true, '', true);

	$border = 0;
	// MultiCell($w, $h, $txt, $border=0, $align='J', $fill=0, $ln=1, $x='', $y='', $reseth=true, $stretch=0, $ishtml=false, $autopadding=true, $maxh=0)
	$pdf->SetFont('helvetica', '', 10.5);
	
	$height = 3;
	$pdf->Ln($height);
	$pdf->SetFont('helvetica', '', 10.5);
	$pdf->MultiCell(0, $height, ($pemohon_List['id_jantina']==2?'Puan, ':'Tuan, '), $border, 'L', 0, 1, '', '', true);
	$pdf->Ln($height);
	$pdf->SetFont('helveticaB', 'BU', 11); 
	$pdf->MultiCell(0, $height, $kawalan_List['desk_bayaranfasa'].' '.$program_List['desk_program'].' BAGI YANG BELUM MENERIMA BAYARAN BAGI TAHUN '.$kawalan_List['tahun_bayaran_tunai'].'.', $border, 'J', 0, 1, '', '', true, 0, true);
	$pdf->Ln($height);
	$pdf->SetFont('helvetica', '', 10.5);
	$pdf->MultiCell(0, $height, 'Dengan segala hormatnya saya diarah merujuk perkara tersebut di atas.', $border, 'L', 0, 1, '', '', true);
	$gelaran = ($pemohon_List['id_jantina']==2?'puan':'tuan');
	$pdf->Ln($height);
	$pdf->MultiCell(0, $height, '2. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Adalah dimaklumkan Kerajaan Negeri Pulau Pinang dengan berbesar hati ingin menyampaikan insentif bagi penerima belum dibayar tahun '.$kawalan_List['tahun_bayaran_tunai'].' sebanyak RM'.$program_List['jum_bayar'].' kepada pemohon yang berdaftar di bawah program '.$program_List['desk_program'].' iSejahtera.', $border, 'J', 0, 1, '', '', true, 0, true);
	$pdf->Ln($height);
	$pdf->MultiCell(0, $height, '3. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sehubungan itu, '.$gelaran.' adalah dijemput hadir seperti ketetapan di bawah dengan membawa <b>kad pengenalan original berserta surat ini yang asal</b> sebagai bukti pengenalan diri:', $border, 'J', 0, 1, '', '', true, 0, true);
	
	$tkh_hingga = '';
	if($baucer_tkh_hingga != $baucer_tkh)
	{		
		$tkh_hingga = ' HINGGA '.$baucer_tkh_hingga;
	}
	
	$HTML = '<table border="0" style="padding:3px;font-weight:bold">';
		$HTML .= '<tr>';
			$HTML .= '<td style="width:5%"></td>';
			$HTML .= '<td style="width:10%">TARIKH</td>';
			$HTML .= '<td style="width:3%;text-align:center;">:</td>';
			$HTML .= '<td style="width:82%">'.$baucer_tkh.$tkh_hingga.' SAHAJA</td>';
		$HTML .= '</tr>';
		$HTML .= '<tr>';
			$HTML .= '<td></td>';
			$HTML .= '<td>MASA</td>';
			$HTML .= '<td style="text-align:center;">:</td>';
			$HTML .= '<td>'.$baucer_from.' HINGGA '.$baucer_to.'</td>';
		$HTML .= '</tr>';
		$HTML .= '<tr>';
			$HTML .= '<td></td>';
			$HTML .= '<td>TEMPAT</td>';
			$HTML .= '<td style="text-align:center;">:</td>';
			$HTML .= '<td>'.$baucer_alamat.'</td>';
		$HTML .= '</tr>';
	$HTML .= '</table>';
	$pdf->writeHTMLCell(0, 0, '', '', $HTML, 0, 1, 0, true, '', true);
	
	$pdf->Ln($height);
	$pdf->MultiCell(0, $height, '4. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Untuk makluman '.$gelaran.', pembayaran wang insentif TIDAK akan dibuat selepas tarikh yang dinyatakan di atas. Kerjasama '.$gelaran.' untuk menghadirkan diri di tempat dan dalam tempoh masa sepertimana yang telah ditetapkan amatlah dihargai.', $border, 'J', 0, 1, '', '', true, 0, true);
	
	
	$pdf->Ln($height);
	$pdf->MultiCell(0, $height, 'Sekian, terima kasih.', $border, 'L', 0, 1, '', '', true);
	
	$pdf->Ln($height);
	$pdf->SetFont('helveticaBI', '', 10);
	$pdf->MultiCell(0, $height, '- Surat ini adalah cetakan komputer dan tandatangan tidak diperlukan -', $border, 'C', 0, 1, '', '', true);
	
	$HTML = '<table border="0" style="padding:3px;font-weight:bold">';
		$HTML .= '<tr>';
			$HTML .= '<td style="width:20%">PERHATIAN:</td>';
		$HTML .= '</tr>';
		$HTML .= '<tr>';
			$HTML .= '<td style="width:5%">a.</td>';
			$HTML .= '<td style="width:95%">Untuk Tuntutan melalui WAKIL sila hubungi Pegawai Penyelaras KADUN di tempat pembayaran untuk mengisi Surat Pelepasan Tanggungjawab dan sila bawa bersama :</td>';
		$HTML .= '</tr>';
		$HTML .= '<tr>';
			$HTML .= '<td style="width:5%"></td>';
			$HTML .= '<td style="width:5%">i.</td>';
			$HTML .= '<td style="width:90%">Surat Panggilan ini (Asal)</td>';
		$HTML .= '</tr>';
		$HTML .= '<tr>';
			$HTML .= '<td style="width:5%"></td>';
			$HTML .= '<td style="width:5%">ii.</td>';
			$HTML .= '<td style="width:90%">Salinan Kad Pengenalan Penerima. Kad Pengenalan Wakil (Asal) dan PHOTOSTAT.</td>';
		$HTML .= '</tr>';
		$HTML .= '<tr>';
			$HTML .= '<td style="width:5%">b.</td>';
			$HTML .= '<td style="width:95%">Jika penama di atas telah meninggal dunia, pihak waris dikehendaki mengemukakan tuntutan sumbangan pengurusan jenazah/kematian di Pejabat Daerah berdekatan. Tuntutan hanya boleh dibuat dalam tempoh satu tahun mulai tarikh kematian.</td>';
		$HTML .= '</tr>';
		$HTML .= '<tr>';
			$HTML .= '<td>c.</td>';
			$HTML .= '<td>Bayaran hanya akan dibuat berdasarkan Nama penuh dan Nombor Kad Pengenalan Penerima sahaja. Sebarang kesalahan ejaan Nama atau Nombor Kad Pengenalan, sila kemukakan surat ini ke kaunter Pejabat Daerah di tempat bayaran untuk pindaan.</td>';
		$HTML .= '</tr>';
		$HTML .= '<tr>';
			$HTML .= '<td>d.</td>';
			$HTML .= '<td>Penama yang telah meninggal dunia tahun '.$kawalan_List['tahun_bayaran_tunai'].' tetap layak menerima bayaran RM'.$program_List['jum_bayar'].' dituntut oleh WAKIL (syarat wakil seperti di atas).</td>';
		$HTML .= '</tr>';
	$HTML .= '</table>';
	$pdf->Ln($height);
	$pdf->SetFont('helveticaB', '', 8);
	$pdf->writeHTMLCell(0, 0, '', '', $HTML, 0, 1, 0, true, '', true);
}
//Close and output PDF document
ob_end_clean();
$pdf->Output($FILE_NAME.'.pdf', 'I');

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

?>

