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

$bayaran_dari = fetch_postget_val('bayaran_dari',$_POST, $_GET);
//$bayaran_hingga = fetch_postget_val('bayaran_hingga',$_POST, $_GET);
$program = fetch_postget_val('program',$_POST, $_GET);
$all_program = explode(',', $program);

$PDO_stmt = $PDO->prepare("SELECT * FROM i_daerah WHERE aktif = 1 ORDER BY susunan");
$PDO_stmt->bindValue(1, $daerah_sc, PDO::PARAM_INT);
$PDO_stmt->execute();
$daerah_List = [];
while($result = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
{
	$daerah_List[$result['id_daerah']] = $result;
}

$PDO_stmt = $PDO->prepare("SELECT * FROM i_dun WHERE id_dun > 1 AND aktif = 1 ORDER BY susunan");
$PDO_stmt->bindValue(1, $dun_sc, PDO::PARAM_INT);
$PDO_stmt->execute();
$dun_List = [];
while($result = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
{
	$dun_List[$result['id_dun']] = $result;
}

$PDO_stmt = $PDO->prepare("SELECT * FROM i_program WHERE aktif = 1 ORDER BY susunan");
$PDO_stmt->bindValue(1, $dun_sc, PDO::PARAM_INT);
$PDO_stmt->execute();
$program_List = [];
while($result = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
{
	$program_List[$result['id_program']] = $result;
}

//*********************************************************************************************************
//(FOR EFT)
//*********************************************************************************************************
//to check inside all program 
$pemohon_List_program = [];
$program_jumlah = [];
$PDO_stmt = $PDO->prepare("
	SELECT *
	FROM i_program
	WHERE aktif = 1
	AND id_program IN (1,2,3,4,5,7)
");
$PDO_stmt->bindValue(1, $id_program, PDO::PARAM_INT);
$PDO_stmt->execute();
$p_list = [];
while($res = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
{
	$mbayar_table = "m_bayaran_".$res['kod_program'];
	$query = "SELECT tahun_bayar, count(*) AS total_pemohon, SUM(jum_bayaran) AS jumlah_bayaran 
		FROM $mbayar_table
		WHERE id_bayaranjenis = 1 AND tahun_bayar = ?
		GROUP BY tahun_bayar;
	";
	$PDO_stmt2 = $PDO->prepare($query);
	$PDO_stmt2->bindValue(1, $bayaran_dari, PDO::PARAM_INT);
	//$PDO_stmt2->bindValue(2, $bayaran_hingga, PDO::PARAM_INT);
	$PDO_stmt2->execute();
	while($result = $PDO_stmt2->fetch(PDO::FETCH_ASSOC))
	{
		$pemohon_List_program[$result['tahun_bayar']][$res['id_program']]['total_pemohon'] = $result['total_pemohon'];
		$pemohon_List_program[$result['tahun_bayar']][$res['id_program']]['jumlah_bayaran'] = $result['jumlah_bayaran'];
		$program_jumlah[$result['tahun_bayar']]['total_pemohon'] += $result['total_pemohon'];
		$program_jumlah[$result['tahun_bayar']]['jumlah_bayaran'] += $result['jumlah_bayaran'];
	}
}


//*********************************************************************************************************
//END (FOR TUNAI)
//*********************************************************************************************************

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

$TITLE = 'JUMLAH PENERIMA PROGRAM I-SEJAHTERA MENGIKUT TAHUN DAN PROGRAM '.$bayaran_dari;
$AUTHOR = 'jj';
$FILE_NAME = str_replace(' ', '_', $TITLE);

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', '', 10); //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('helvetica', '', 7);
		$this->MultiCell(0, 0, 'Dijana pada '.date('d-m-Y | H:i:s').' - Tarikh Sah Sehingga - 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($TITLE);

// 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);

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

$counter = 0;
$bayaran_hingga = $bayaran_dari;
for ($year = $bayaran_dari ; $year <= $bayaran_hingga; $year++)
{
	if($counter % 8 == 0)
	{
		if($counter > 0)
		{
			$pdf->SetFont('helvetica', '', 11); //CHANGE THIS FOR TITLE FONT
			$pdf->writeHTMLCell(0, 0, '', '', $HTML, 0, 1, 0, true, '', true);
		}
		$pdf->AddPage();// add a page
		$HTML = '<table border="1" style="padding:3px;text-align:center">';
	}
	
	if($counter % 2 == 0)
	{
		$HTML .= '<tr>';
	}
	$HTML .= '<td style="width:48%">';
		$HTML .= '<table border="1" style="padding:3px;text-align:center">';
			$HTML .= '<tr style="vertical-align: middle;background-color:#C5E0FC;font-weight:bold">';
				$HTML .= '<th style="width:25%;">'.$year.'</th>';
				$HTML .= '<th style="width:30%;">Bilangan</th>';
				$HTML .= '<th style="width:45%;">Peruntukan</th>';
			$HTML .= '</tr>';
			
			$jumlah = [];
			foreach ($all_program as $pItem)
			{
				$total_pemohon = $pemohon_List_program[$year][$pItem]['total_pemohon'];
				$jumlah_bayaran = $pemohon_List_program[$year][$pItem]['jumlah_bayaran'];
				
				$HTML .= '<tr>';
					$HTML .= '<td>'.$program_List[$pItem]['kod_program'].'</td>';
					$HTML .= '<td style="text-align:right">'.number_format(($total_pemohon > 0?$total_pemohon : 0), 0).'</td>';
					$HTML .= '<td style="text-align:right">RM'.number_format(($jumlah_bayaran > 0?$jumlah_bayaran : 0), 2).'</td>';
				$HTML .= '</tr>';
			}
			
			$HTML .= '<tr style="font-weight:bold">';
				$HTML .= '<td></td>';
				$HTML .= '<td style="text-align:right">'.number_format(($program_jumlah[$year]['total_pemohon'] > 0?$program_jumlah[$year]['total_pemohon'] : 0), 0).'</td>';
				$HTML .= '<td style="text-align:right">RM'.number_format(($program_jumlah[$year]['jumlah_bayaran'] > 0?$program_jumlah[$year]['jumlah_bayaran'] : 0), 2).'</td>';
			$HTML .= '</tr>';
		$HTML .= '</table>';
	$HTML .= '</td>';
	
	if($counter % 2 != 0)
	{
		$HTML .= '</tr>';
		$HTML .= '<tr>';
			$HTML .= '<td>&nbsp;</td>';
		$HTML .= '</tr>';
	}
	else
	{
		$HTML .= '<td style="width:4%"></td>';
	}
	$counter++;
}
if($counter % 8 != 0)
{
	$pdf->SetFont('helvetica', '', 11); //CHANGE THIS FOR TITLE FONT
	$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
//***********************************************************************************************************************

?>

