<?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)
				INNER JOIN custom_coupon_master ON (custom_coupon_master.coupn_reg_id = custom_participant_registration.reg_participant_id)
			WHERE custom_coupon_master.coupn_issue_date <= '".$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, "Recruit / Seed Wise Report");
		$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, "Protocol");
		$objPHPExcel->getActiveSheet()->getStyle('A2')->getFont()->setBold(true);
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow(1, 2, $rec[0]['sur_title']);
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow(4, 2, "Report Printed On");
		// $objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow(5, 2, "=today()");
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow(5, 2, $common->date_format(date("Y-m-d"), DT_FMT, ''));
		$objPHPExcel->getActiveSheet()->getStyle('E2')->getFont()->setBold(true);
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow(0, 3, "Report As On");
		$objPHPExcel->getActiveSheet()->getStyle('A3')->getFont()->setBold(true);
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow(1, 3, $prd_to);
		
		$j = 0;
		$row = 5;
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, "Recruit ID");
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($j, $row)->applyFromArray($styleThickBrownBorderOutline);
		$j++;
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, "Coupon ID");
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($j, $row)->applyFromArray($styleThickBrownBorderOutline);
		$j++;
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, "Issue Date");
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($j, $row)->applyFromArray($styleThickBrownBorderOutline);
		$j++;
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, "Status");
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($j, $row)->applyFromArray($styleThickBrownBorderOutline);
		$j++;
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, "Voucher Number");
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($j, $row)->applyFromArray($styleThickBrownBorderOutline);
		$j++;
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, "Voucher Date");
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($j, $row)->applyFromArray($styleThickBrownBorderOutline);
		$j++;
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, "Amount");
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($j, $row)->applyFromArray($styleThickBrownBorderOutline);
		$j++;
		
		for($k=0; $k<count($rec); $k++)
		{
			$qry = "SELECT * FROM custom_coupon_master WHERE coupn_reg_id = '".$rec[$k]['reg_participant_id']."'";
			$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
			$cupn_rec = $common->sql->record;
			$cnt = count($cupn_rec);
			
			for($i=0; $i<count($cupn_rec); $i++)
			{
				// $query = "SELECT * FROM custom_participant_registration WHERE reg_coupon_code = '".$cupn_rec[$i]['coupn_participant_id']."' AND reg_biometric_reference != ''";
				$query = "SELECT * FROM custom_participant_registration WHERE reg_coupon_code = '".$cupn_rec[$i]['coupn_participant_id']."'";
				$common->sql->query($query, SQL_ALL, SQL_ASSOC);
				$reg = $common->sql->record;
				$partid = $reg[0]['reg_id'];
				$partid = $reg[0]['reg_participant_id'];
				$part_id = $reg[0]['reg_participant_id'];
				$survey_id = $reg[0]['reg_survey_id'];
				
				if(count($reg) == 0)
				{
					$sts = "Registration Pending";
				}
				else
				{
					$query = "SELECT * FROM custom_survey_master WHERE sur_code = '".$survey_id."'";
					$common->sql->query($query, SQL_ALL, SQL_ASSOC);
					$sur_mstr_rec = $common->sql->record;
					$db_sur_code = $sur_mstr_rec[0]['sur_code'];
					$db_sur_id = $sur_mstr_rec[0]['sur_onlne_id'];
					
					$db_server = $sur_mstr_rec[0]['sur_pri_db_server'];
					$db_name = $sur_mstr_rec[0]['sur_pri_db_name'];
					$db_usrnme = $sur_mstr_rec[0]['sur_pri_db_usrnme'];
					$db_paswrd = $sur_mstr_rec[0]['sur_pri_db_paswrd'];
					
					if($db_sur_code == "YRGBS1")
						$p_id = "23634X14X898";
					elseif($db_sur_code == "YRGBS2")
						$p_id = "23634X16X486";
					elseif($db_sur_code == "IDURDSII")
						$p_id = "166474X19X1162";
					else
						$p_id = "";
					
					$sur_conn = array('host' => $db_server, 'dbname' => $db_name, 'dbuser' => $db_usrnme, 'dbpass' => $db_paswrd);
					$survey = new Survey_Functions($sur_conn);
					
					$query = "SELECT * FROM lime_survey_".$db_sur_id." WHERE ".$p_id." = '".$part_id."' AND submitdate IS NOT NULL";
					$survey->sql->query($query, SQL_ALL, SQL_ASSOC);
					$sur_mstr = $survey->sql->record;
					
					if(count($sur_mstr) == 0)
					{
						$sts = "Survey Pending";
						$sts_val = 2;
					}
					else
					{
						$query = "SELECT * FROM custom_lab_mapping WHERE lab_mapng_sur_id = '".$survey_id."'";
						$common->sql->query($query, SQL_ALL, SQL_ASSOC);
						$lab_mpng = $common->sql->record;
						
						$query = "SELECT * FROM custom_lab_mapping 
										INNER JOIN custom_lab_master ON (custom_lab_master.lab_id = custom_lab_mapping.lab_mapng_mstr_id)
										INNER JOIN custom_lab_spceimen_master ON (custom_lab_spceimen_master.lab_spcmn_id = custom_lab_mapping.lab_mapng_spcmn_id)
										INNER JOIN custom_lab_samples 
											ON (custom_lab_samples.lab_smple_sur_id = custom_lab_mapping.lab_mapng_sur_id 
												AND custom_lab_samples.lab_smple_tst_id = custom_lab_master.lab_id 
												AND custom_lab_samples.lab_smple_spcmn_id = custom_lab_spceimen_master.lab_spcmn_id 
												AND lab_smple_part_id = '".$partid."' AND lab_smple_spcmn_clctd = 1)
									WHERE lab_mapng_sur_id = '".$survey_id."' ORDER BY lab_id";
						$common->sql->query($query, SQL_ALL, SQL_ASSOC);
						$lab_smples = $common->sql->record;
						
						if(count($lab_smples) != count($lab_mpng))
						{
							$sts = "Lab Samples Pending";
						}
						else
						{
							$sts = "Completed";
						}
					}
				}
				
				$row++;
				if($sts == "Completed")
				{
					$qry = "SELECT * FROM custom_incentive WHERE incentive_particpnt_id = '".$rec[$k]['reg_participant_id']."' AND incentive_survy_id = '".$survey_id."' AND incentive_cupn_id = '".$cupn_rec[$i]['coupn_participant_id']."'";
					$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
					$inc_rec = $common->sql->record;
					
					if(count($inc_rec) != 0)
					{
						$vch_no = $inc_rec[0]['incentive_vchr_no'];
						$vch_dt = $inc_rec[0]['incentive_vchr_dt'];
						$vch_amt = $inc_rec[0]['incentive_amt'];
					}
				}
				
				if(count($inc_rec) == 0 || $sts != "Completed")
				{
					$vch_no = '--';
					$vch_dt = '--';
					$vch_amt = '--';
				}
				
				$coupn_issue_date = $cupn_rec[$i]['coupn_issue_date'];
				$j = 0;
				$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, $rec[$k]['reg_participant_id']);
				$j++;
				$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, $cupn_rec[$i]['coupn_participant_id']);
				$j++;
				$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, $common->date_format($coupn_issue_date, DT_FMT, true));
				$j++;
				$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, $sts);
				$j++;
				$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, $vch_no);
				$j++;
				// $objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, $vch_dt);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, $common->date_format($vch_dt, DT_FMT, true));
				$j++;
				$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, $vch_amt);
				$j++;
			}
		}
		
		// Rename worksheet
		$objPHPExcel->getActiveSheet()->setTitle('Recruit Seed Wise Report');
		$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);

		// 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="Recruit Seed Wise Report.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>Recruit ID</strong></td>
				<td><strong>Coupon ID</strong></td>
				<td><strong>Issue Date</strong></td>
				<td><strong>Status</strong></td>
				<td><strong>Voucher Number</strong></td>
				<td><strong>Voucher Date</strong></td>
				<td><strong>Amount</strong></td>';
		echo '</tr>
			</thead>
			<tbody>';
		
		for($k=0; $k<count($rec); $k++)
		{
			$qry = "SELECT * FROM custom_coupon_master WHERE coupn_reg_id = '".$rec[$k]['reg_participant_id']."'";
			$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
			$cupn_rec = $common->sql->record;
			$cnt = count($cupn_rec);
			
			for($i=0; $i<count($cupn_rec); $i++)
			{
				// $query = "SELECT * FROM custom_participant_registration WHERE reg_coupon_code = '".$cupn_rec[$i]['coupn_participant_id']."' AND reg_biometric_reference != ''";
				$query = "SELECT * FROM custom_participant_registration WHERE reg_coupon_code = '".$cupn_rec[$i]['coupn_participant_id']."'";
				$common->sql->query($query, SQL_ALL, SQL_ASSOC);
				$reg = $common->sql->record;
				$partid = $reg[0]['reg_id'];
				$partid = $reg[0]['reg_participant_id'];
				$part_id = $reg[0]['reg_participant_id'];
				$survey_id = $reg[0]['reg_survey_id'];
				
				if(count($reg) == 0)
				{
					$sts = "Registration Pending";
				}
				else
				{
					$query = "SELECT * FROM custom_survey_master WHERE sur_code = '".$survey_id."'";
					$common->sql->query($query, SQL_ALL, SQL_ASSOC);
					$sur_mstr_rec = $common->sql->record;
					$db_sur_code = $sur_mstr_rec[0]['sur_code'];
					$db_sur_id = $sur_mstr_rec[0]['sur_onlne_id'];
					
					$db_server = $sur_mstr_rec[0]['sur_pri_db_server'];
					$db_name = $sur_mstr_rec[0]['sur_pri_db_name'];
					$db_usrnme = $sur_mstr_rec[0]['sur_pri_db_usrnme'];
					$db_paswrd = $sur_mstr_rec[0]['sur_pri_db_paswrd'];
					
					if($db_sur_code == "YRGBS1")
						$p_id = "23634X14X898";
					elseif($db_sur_code == "YRGBS2")
						$p_id = "23634X16X486";
					elseif($db_sur_code == "IDURDSII")
						$p_id = "166474X19X1162";
					else
						$p_id = "";
					
					$sur_conn = array('host' => $db_server, 'dbname' => $db_name, 'dbuser' => $db_usrnme, 'dbpass' => $db_paswrd);
					$survey = new Survey_Functions($sur_conn);
					
					$query = "SELECT * FROM lime_survey_".$db_sur_id." WHERE ".$p_id." = '".$part_id."' AND submitdate IS NOT NULL";
					$survey->sql->query($query, SQL_ALL, SQL_ASSOC);
					$sur_mstr = $survey->sql->record;
					
					if(count($sur_mstr) == 0)
					{
						$sts = "Survey Pending";
						$sts_val = 2;
					}
					else
					{
						$query = "SELECT * FROM custom_lab_mapping WHERE lab_mapng_sur_id = '".$survey_id."'";
						$common->sql->query($query, SQL_ALL, SQL_ASSOC);
						$lab_mpng = $common->sql->record;
						
						$query = "SELECT * FROM custom_lab_mapping 
										INNER JOIN custom_lab_master ON (custom_lab_master.lab_id = custom_lab_mapping.lab_mapng_mstr_id)
										INNER JOIN custom_lab_spceimen_master ON (custom_lab_spceimen_master.lab_spcmn_id = custom_lab_mapping.lab_mapng_spcmn_id)
										INNER JOIN custom_lab_samples 
											ON (custom_lab_samples.lab_smple_sur_id = custom_lab_mapping.lab_mapng_sur_id 
												AND custom_lab_samples.lab_smple_tst_id = custom_lab_master.lab_id 
												AND custom_lab_samples.lab_smple_spcmn_id = custom_lab_spceimen_master.lab_spcmn_id 
												AND lab_smple_part_id = '".$partid."' AND lab_smple_spcmn_clctd = 1)
									WHERE lab_mapng_sur_id = '".$survey_id."' ORDER BY lab_id";
						$common->sql->query($query, SQL_ALL, SQL_ASSOC);
						$lab_smples = $common->sql->record;
						
						if(count($lab_smples) != count($lab_mpng))
						{
							$sts = "Lab Samples Pending";
						}
						else
						{
							$sts = "Completed";
						}
					}
				}
				
				if($sts == "Completed")
				{
					$qry = "SELECT * FROM custom_incentive WHERE incentive_particpnt_id = '".$rec[$k]['reg_participant_id']."' AND incentive_survy_id = '".$survey_id."' AND incentive_cupn_id = '".$cupn_rec[$i]['coupn_participant_id']."'";
					$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
					$inc_rec = $common->sql->record;
					
					if(count($inc_rec) != 0)
					{
						$vch_no = $inc_rec[0]['incentive_vchr_no'];
						$vch_dt = $inc_rec[0]['incentive_vchr_dt'];
						$vch_amt = $inc_rec[0]['incentive_amt'];
					}
				}
				
				if(count($inc_rec) == 0 || $sts != "Completed")
				{
					$vch_no = '&nbsp; -- &nbsp;';
					$vch_dt = '&nbsp; -- &nbsp;';
					$vch_amt = '&nbsp; -- &nbsp;';
				}
				
				echo '<tr>
						<td>'.$rec[$k]['reg_participant_id'].'</td>';
				$coupn_issue_date = $cupn_rec[$i]['coupn_issue_date'];
				echo '<td>'.$cupn_rec[$i]['coupn_participant_id'].'</td>';
				echo '<td>'.$common->date_format($coupn_issue_date, DT_FMT, true).'</td>';
				echo '<td>'.$sts.'</td>';
				echo '<td>'.$vch_no.'</td>';
				echo '<td>'.$common->date_format($vch_dt, DT_FMT, true).'</td>';
				echo '<td>'.$vch_amt.'</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>Recruit / Seed Wise Report</strong></font></th>
				</tr>
				<tr>
					<td><font size="10"><strong> Survey Title : </strong> '.$rec[0]['sur_title'].'</font></td>
				</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>Recruit ID</strong></td>
				<td><strong>Coupon ID</strong></td>
				<td><strong>Issue Date</strong></td>
				<td><strong>Status</strong></td>
				<td><strong>Voucher Number</strong></td>
				<td><strong>Voucher Date</strong></td>
				<td><strong>Amount</strong></td>
			  </tr>
			</thead>
			<tbody>';
		
		for($k=0; $k<count($rec); $k++)
		{
			$qry = "SELECT * FROM custom_coupon_master WHERE coupn_reg_id = '".$rec[$k]['reg_participant_id']."'";
			$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
			$cupn_rec = $common->sql->record;
			$cnt = count($cupn_rec);
			
			for($i=0; $i<count($cupn_rec); $i++)
			{
				// $query = "SELECT * FROM custom_participant_registration WHERE reg_coupon_code = '".$cupn_rec[$i]['coupn_participant_id']."' AND reg_biometric_reference != ''";
				$query = "SELECT * FROM custom_participant_registration WHERE reg_coupon_code = '".$cupn_rec[$i]['coupn_participant_id']."'";
				$common->sql->query($query, SQL_ALL, SQL_ASSOC);
				$reg = $common->sql->record;
				$partid = $reg[0]['reg_id'];
				$partid = $reg[0]['reg_participant_id'];
				$part_id = $reg[0]['reg_participant_id'];
				$survey_id = $reg[0]['reg_survey_id'];
				
				if(count($reg) == 0)
				{
					$sts = "Registration Pending";
				}
				else
				{
					$query = "SELECT * FROM custom_survey_master WHERE sur_code = '".$survey_id."'";
					$common->sql->query($query, SQL_ALL, SQL_ASSOC);
					$sur_mstr_rec = $common->sql->record;
					$db_sur_code = $sur_mstr_rec[0]['sur_code'];
					$db_sur_id = $sur_mstr_rec[0]['sur_onlne_id'];
					
					$db_server = $sur_mstr_rec[0]['sur_pri_db_server'];
					$db_name = $sur_mstr_rec[0]['sur_pri_db_name'];
					$db_usrnme = $sur_mstr_rec[0]['sur_pri_db_usrnme'];
					$db_paswrd = $sur_mstr_rec[0]['sur_pri_db_paswrd'];
					
					if($db_sur_code == "YRGBS1")
						$p_id = "23634X14X898";
					elseif($db_sur_code == "YRGBS2")
						$p_id = "23634X16X486";
					elseif($db_sur_code == "IDURDSII")
						$p_id = "166474X19X1162";
					else
						$p_id = "";
					
					$sur_conn = array('host' => $db_server, 'dbname' => $db_name, 'dbuser' => $db_usrnme, 'dbpass' => $db_paswrd);
					$survey = new Survey_Functions($sur_conn);
					
					$query = "SELECT * FROM lime_survey_".$db_sur_id." WHERE ".$p_id." = '".$part_id."' AND submitdate IS NOT NULL";
					$survey->sql->query($query, SQL_ALL, SQL_ASSOC);
					$sur_mstr = $survey->sql->record;
					
					if(count($sur_mstr) == 0)
					{
						$sts = "Survey Pending";
						$sts_val = 2;
					}
					else
					{
						$query = "SELECT * FROM custom_lab_mapping WHERE lab_mapng_sur_id = '".$survey_id."'";
						$common->sql->query($query, SQL_ALL, SQL_ASSOC);
						$lab_mpng = $common->sql->record;
						
						$query = "SELECT * FROM custom_lab_mapping 
										INNER JOIN custom_lab_master ON (custom_lab_master.lab_id = custom_lab_mapping.lab_mapng_mstr_id)
										INNER JOIN custom_lab_spceimen_master ON (custom_lab_spceimen_master.lab_spcmn_id = custom_lab_mapping.lab_mapng_spcmn_id)
										INNER JOIN custom_lab_samples 
											ON (custom_lab_samples.lab_smple_sur_id = custom_lab_mapping.lab_mapng_sur_id 
												AND custom_lab_samples.lab_smple_tst_id = custom_lab_master.lab_id 
												AND custom_lab_samples.lab_smple_spcmn_id = custom_lab_spceimen_master.lab_spcmn_id 
												AND lab_smple_part_id = '".$partid."' AND lab_smple_spcmn_clctd = 1)
									WHERE lab_mapng_sur_id = '".$survey_id."' ORDER BY lab_id";
						$common->sql->query($query, SQL_ALL, SQL_ASSOC);
						$lab_smples = $common->sql->record;
						
						if(count($lab_smples) != count($lab_mpng))
						{
							$sts = "Lab Samples Pending";
						}
						else
						{
							$sts = "Completed";
						}
					}
				}
				
				if($sts == "Completed")
				{
					$qry = "SELECT * FROM custom_incentive WHERE incentive_particpnt_id = '".$rec[$k]['reg_participant_id']."' AND incentive_survy_id = '".$survey_id."' AND incentive_cupn_id = '".$cupn_rec[$i]['coupn_participant_id']."'";
					$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
					$inc_rec = $common->sql->record;
					
					if(count($inc_rec) != 0)
					{
						$vch_no = $inc_rec[0]['incentive_vchr_no'];
						$vch_dt = $inc_rec[0]['incentive_vchr_dt'];
						$vch_amt = $inc_rec[0]['incentive_amt'];
					}
				}
				
				if(count($inc_rec) == 0 || $sts != "Completed")
				{
					$vch_no = '&nbsp; -- &nbsp;';
					$vch_dt = '&nbsp; -- &nbsp;';
					$vch_amt = '&nbsp; -- &nbsp;';
				}
				
				$disp_table .= '<tr>
						<td>'.$rec[$k]['reg_participant_id'].'</td>';
				$coupn_issue_date = $cupn_rec[$i]['coupn_issue_date'];
				$disp_table .= '<td>'.$cupn_rec[$i]['coupn_participant_id'].'</td>';
				$disp_table .= '<td>'.$common->date_format($coupn_issue_date, DT_FMT, true).'</td>';
				$disp_table .= '<td>'.$sts.'</td>';
				$disp_table .= '<td>'.$vch_no.'</td>';
				// $disp_table .= '<td>'.$vch_dt.'</td>';
				$disp_table .= '<td>'.$common->date_format($vch_dt, DT_FMT, true).'</td>';
				$disp_table .= '<td>'.$vch_amt.'</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("Recruit Seed Wise Report.pdf", "I");
	}
?>