<?php
/*echo '<link rel="stylesheet" href="includes/media/css/demo_page.css" type="text/css" />
<link rel="stylesheet" href="includes/media/css/demo_table.css" type="text/css" />
<link rel="stylesheet" href="includes/media/css/TableTools_JUI.css" type="text/css" />
<script type="text/javascript" language="javascript" src="includes/media/js/jquery.js"></script>
<script type="text/javascript" language="javascript" src="includes/media/js/jquery.dataTables.js"></script>
<script type="text/javascript" language="javascript" src="includes/media/js/ZeroClipboard.js"></script>
<script type="text/javascript" language="javascript" src="includes/media/js/TableTools.js"></script>
';*/
	/*echo '<script type="text/javascript" charset="utf-8">
		$(document).ready(function() {
			oTable1 = $('#example2').dataTable({
					"sScrollY": "450px",
					"bPaginate": false,
					"bJQueryUI": true,
					"sPaginationType": "full_numbers",
					"sDom": '<"H"Tfr>t<"F"ip>',
					"oTableTools": {
							"sSwfPath" : "includes/media/swf/copy_csv_xls_pdf.swf",
							"aButtons": [
									"csv", "pdf"
							]
					}
			});
		} );
	</script>';*/

	//echo PEAR.'/PHPExcel.php';
	
	error_reporting(E_ALL);
	ini_set('display_errors', 0);
	
	$part_id = isset($_REQUEST['part_id']) ? $_REQUEST['part_id'] : "";
    $part_id = isset($_REQUEST['partcpnt_id']) ? $_REQUEST['partcpnt_id'] : "";
	$prd_frm = isset($_REQUEST['prd_frm']) ? $_REQUEST['prd_frm'] : "";
    $prd_to = isset($_REQUEST['prd_to']) ? $_REQUEST['prd_to'] : "";
	
    $cupn_whr = "";
    $reg_whr = "";
    
    if($part_id != "")
    {
        $cupn_whr = " WHERE coupn_reg_id = '".$part_id."'";
        $reg_whr = " WHERE reg_id = '".$part_id."'";
		
        $cupn_whr = " AND coupn_reg_id = '".$part_id."'";
        $reg_whr = " AND reg_participant_id = '".$part_id."'";
    }
    
    $qry = "SELECT coupn_count FROM custom_coupon_master WHERE coupn_issue_date BETWEEN '".$common->date_format($prd_frm, DT_FMT, '')."' AND '".$common->date_format($prd_to, DT_FMT, '')."' $cupn_whr ORDER BY coupn_count DESC";
    $common->sql->query($qry, SQL_ALL, SQL_ASSOC);
    $rec = $common->sql->record;
    $cols = $rec[0]['coupn_count'];
    
    $qry = "SELECT * FROM custom_participant_registration
				INNER JOIN custom_survey_master ON (custom_survey_master.sur_code = custom_participant_registration.reg_survey_id)
				LEFT JOIN participant_finger_print ON (participant_finger_print.participant_id = custom_participant_registration.reg_participant_id)
			WHERE custom_participant_registration.reg_date_of_recurit <= '".$common->date_format($prd_to, DT_FMT, '')."'".$reg_whr." GROUP BY reg_participant_id";
    $common->sql->query($qry, SQL_ALL, SQL_ASSOC);
    $rec = $common->sql->record;
	
	if($attr == "excel")
	{
		require_once PEAR.'/PHPExcel.php';
		
		$styleThickBrownBorderOutline = array(
			'font'    => array(
				'bold'      => true
			)
		);
		
		$objPHPExcel = new PHPExcel();
		$objPHPExcel->setActiveSheetIndex(0);
		
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow(0, 1, "Participant Reference Number");
		$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(18);
		$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
		$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$objPHPExcel->getActiveSheet()->mergeCells('A1:H1');
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow(0, 2, "Report As On");
		$objPHPExcel->getActiveSheet()->getStyle('A2')->getFont()->setBold(true);
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow(1, 2, $prd_to);
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow(4, 2, "Report Printed On");
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow(5, 2, "=today()");
		
		$j = 0;
		$row = 4;
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, "Survey ID");
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($j, $row)->applyFromArray($styleThickBrownBorderOutline);
		$j++;
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, "Survey Title");
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($j, $row)->applyFromArray($styleThickBrownBorderOutline);
		$j++;
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, "Participant ID");
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($j, $row)->applyFromArray($styleThickBrownBorderOutline);
		$j++;
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, "Date of Registration");
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($j, $row)->applyFromArray($styleThickBrownBorderOutline);
		$j++;
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, "Reference Number");
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($j, $row)->applyFromArray($styleThickBrownBorderOutline);
		$j++;
		
		$hdr_col = $j;
		
		$row++;
		for($i=0; $i<count($rec); $i++)
		{
			$j = 0;
			$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, $rec[$i]['sur_code']);
			$j++;
			$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, $rec[$i]['sur_title']);
			$j++;
			$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, $rec[$i]['reg_participant_id']);
			$j++;
			$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, $common->date_format($rec[$i]['reg_date_of_recurit'], DT_FMT, true));
			$j++;
			$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, $rec[$i]['ref_id']);
			$j++;
			$row++;
		}
		// Rename worksheet
		$objPHPExcel->getActiveSheet()->setTitle('Participant Reference Number');
		$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);
		//$objPHPExcel->getActiveSheet()->protectCells('A12:ZZ12', 'PHPExcel');
		//$objPHPExcel->getActiveSheet()->getStyle('B10')->getProtection()->setLocked(PHPExcel_Style_Protection::PROTECTION_UNPROTECTED);

		// Set active sheet index to the first sheet, so Excel opens this as the first sheet
		$objPHPExcel->setActiveSheetIndex(0);


		// Redirect output to a client’s web browser (Excel5)
		header('Content-Type: application/vnd.ms-excel');
		header('Content-Disposition: attachment;filename="Participant Reference Number.xls"');
		header('Cache-Control: max-age=0');

		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
		$objWriter->save('php://output');
	}
	elseif($attr == "print")
	{
		echo '<table border="0" cellpadding="0" cellspacing="0" style="width: 100%; line-height: 40px;" align="center" class="display" id="example2">
			<thead>';
		echo '<tr>
				<td><strong>Survey ID</strong></td>
				<td><strong>Survey Title</strong></td>
				<td><strong>Participant ID</strong></td>
				<td><strong>Date of Registration</strong></td>
				<td><strong>Reference Number</strong></td>';
		echo '</tr>
			</thead>
			<tbody>';
		
		for($i=0; $i<count($rec); $i++)
		{
			echo '<tr>
					<td>'.$rec[$i]['sur_code'].'</td>
					<td>'.$rec[$i]['sur_title'].'</td>
					<td>'.$rec[$i]['reg_participant_id'].'</td>
					<td>'.$common->date_format($rec[$i]['reg_date_of_recurit'], DT_FMT, true).'</td>
					<td>'.$rec[$i]['ref_id'].'</td>';
			echo '</tr>';
		}
		echo '
			</tbody>
			</table>';
	}
	else
	{
		$disp_header = '
			<br/><br/>
			<table border="0" cellpadding="0" cellspacing="0" style="width: 100%; line-height: 10px;">
				<tr>
					<th align="center"><font size="13"><strong>Participant Reference Number</strong></font></th>
				</tr>
				<tr>
					<td><font size="10"><strong> Report as on : </strong> '.$prd_to.'</font></td>
				</tr>
			</table>
			<br><br>';
		$disp_table = '<table border="1" cellpadding="0" cellspacing="0" style="width: 100%; line-height: 10px;" align="center">
			<thead>';
		$disp_table .= '<tr>
				<td><strong>Survey ID</strong></td>
				<td><strong>Survey Title</strong></td>
				<td><strong>Participant ID</strong></td>
				<td><strong>Date of Registration</strong></td>
				<td><strong>Reference Number</strong></td>';
		$disp_table .= '</tr>
			</thead>
			<tbody>';
		
		for($i=0; $i<count($rec); $i++)
		{
			$disp_table .= '<tr>
					<td>'.$rec[$i]['sur_code'].'</td>
					<td>'.$rec[$i]['sur_title'].'</td>
					<td>'.$rec[$i]['reg_participant_id'].'</td>
					<td>'.$common->date_format($rec[$i]['reg_date_of_recurit'], DT_FMT, true).'</td>
					<td>'.$rec[$i]['ref_id'].'</td>';
			$disp_table .= '</tr>';
		}
		$disp_table .= '
			</tbody>
			</table>';
		
		$query = "SELECT * FROM custom_settings";
		$common->sql->query($query, SQL_ALL, SQL_ASSOC);
		$stngs_rec = $common->sql->record[0];
		
		$stngs_org_name = $stngs_rec['stngs_org_name'];
		$stngs_org_addrs = $stngs_rec['stngs_org_addrs'];
		$stngs_org_phone = $stngs_rec['stngs_org_phone'];
		$stngs_org_mail = $stngs_rec['stngs_org_mail'];
		$stngs_org_website = $stngs_rec['stngs_org_website'];
		
		$pdf = '';
		$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
		
		// set document information
		$pdf->SetCreator(PDF_CREATOR);
		$pdf->SetAuthor('YRG Care');
		$pdf->SetTitle('Expense Report');
		$pdf->SetSubject('TCPDF Tutorial');
		$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
		
		// set default header data
		$pdf->SetHeaderData(PDF_HEADER_LOGO, '30', $stngs_org_name, "<font size=\"9\">".$stngs_org_addrs."<br/><b>T : </b>".$stngs_org_phone."<br/><b>E : </b>".$stngs_org_mail."  <b>  Web : </b>".$stngs_org_website."</font>");
		// $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 001', PDF_HEADER_STRING);
		
		// set header and footer fonts
		/*$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', 20, 'L', true));
		$pdf->setHeaderStringFont(Array(PDF_FONT_NAME_MAIN, '', 10, 'L', true));
		$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));*/
		
		$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', 12, '', true));
		$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_RIGHT);
		$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
		$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
		
		//set auto page breaks
		$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
		
		//set image scale factor
		$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
		
		//set some language-dependent strings
		$pdf->setLanguageArray($l);
		
		// ---------------------------------------------------------
		
		// set font
		$pdf->SetFont('dejavusans', '', 8);
		
		// add a page
		$pdf->AddPage('L');
		
		$dt_fmt = str_replace("mm", "m", DT_FMT);
		$dt_fmt = str_replace("dd", "d", $dt_fmt);
		$dt_fmt = str_replace("yyyy", "Y", $dt_fmt);
		$dt = date($dt_fmt);
		
		$title_rght = "<br/><br/><br/><font size=\"9\"><b>Printed from :</b> ".APP_NAME."<br/>
		<b>Printed by : </b>".USRNME."<br/>
		<b>Report Printed on : </b>".$dt."</font>";
		
		// Print text using writeHTMLCell()
		$pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', $disp_header, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true);
		$pdf->writeHTML($disp_table, true, false, true, false, '');
		$pdf->MultiCell(0, 10, $title_rght, 0, 'R', 0, 1, '', 5, false, 0, true, true, 0, 'T', false);
		$pdf->lastPage();
		$pdf->Output("Participant Reference Number.pdf", "D");
	}
?>