<?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 != "")
    {
        $reg_whr = " AND loctr_reg_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 $table_name WHERE create_time <= '".$common->date_format($prd_to, DT_FMT, '')."'".$reg_whr;
    $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, "Locator 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');
		
		$j = 0;
		$row = 3;
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, "Participant ID");
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($j, $row)->applyFromArray($styleThickBrownBorderOutline);
		$j++;
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, "Name");
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($j, $row)->applyFromArray($styleThickBrownBorderOutline);
		$j++;
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, "Address");
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($j, $row)->applyFromArray($styleThickBrownBorderOutline);
		$j++;
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, "City");
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($j, $row)->applyFromArray($styleThickBrownBorderOutline);
		$j++;
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, "Pincode");
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($j, $row)->applyFromArray($styleThickBrownBorderOutline);
		$j++;
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, "Mobile");
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($j, $row)->applyFromArray($styleThickBrownBorderOutline);
		$j++;
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, "Alternate Contact No");
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($j, $row)->applyFromArray($styleThickBrownBorderOutline);
		$j++;
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, "Date");
		$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]['loctr_reg_id']);
			$j++;
			$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, $rec[$i]['loctr_name']);
			$j++;
			$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, $rec[$i]['loctr_address']);
			$j++;
			$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, $rec[$i]['loctr_city']);
			$j++;
			$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, $rec[$i]['loctr_pincode']);
			$j++;
			$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, $rec[$i]['loctr_mobile']);
			$j++;
			$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, $rec[$i]['loctr_contno']);
			$j++;
			$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, $rec[$i]['create_time']);
			// $objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, $common->date_format($rec[$i]['reg_date_of_recurit'], DT_FMT, ''));
			$j++;
			$row++;
		}
		// Rename worksheet
		$objPHPExcel->getActiveSheet()->setTitle('Locator 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="Locator 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>Participant ID</strong></td>
				<td><strong>Name</strong></td>
				<td><strong>Address</strong></td>
				<td><strong>City</strong></td>
				<td><strong>Pincode</strong></td>
				<td><strong>Mobile</strong></td>
				<td><strong>Alternate Contact No</strong></td>
				<td><strong>Date</strong></td>';
		echo '</tr>
			</thead>
			<tbody>';
		
		for($i=0; $i<count($rec); $i++)
		{
			echo '<tr>
					<td>'.$rec[$i]['loctr_reg_id'].'</td>
					<td>'.$rec[$i]['loctr_name'].'</td>
					<td>'.$rec[$i]['loctr_address'].'</td>
					<td>'.$rec[$i]['loctr_city'].'</td>
					<td>'.$rec[$i]['loctr_pincode'].'</td>
					<td>'.$rec[$i]['loctr_mobile'].'</td>
					<td>'.$rec[$i]['loctr_contno'].'</td>
					<td>'.$rec[$i]['create_time'].'</td>';
					// '<td>'.$common->date_format($rec[$i]['create_time'], DT_FMT, '').'</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>Locator Report</strong></font></th>
				</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>Participant ID</strong></td>
				<td><strong>Name</strong></td>
				<td><strong>Address</strong></td>
				<td><strong>City</strong></td>
				<td><strong>Pincode</strong></td>
				<td><strong>Mobile</strong></td>
				<td><strong>Alternate Contact No</strong></td>
				<td><strong>Date</strong></td>';
		$disp_table .= '</tr>
			</thead>
			<tbody>';
		
		for($i=0; $i<count($rec); $i++)
		{
			$disp_table .= '<tr>
					<td>'.$rec[$i]['loctr_reg_id'].'</td>
					<td>'.$rec[$i]['loctr_name'].'</td>
					<td>'.$rec[$i]['loctr_address'].'</td>
					<td>'.$rec[$i]['loctr_city'].'</td>
					<td>'.$rec[$i]['loctr_pincode'].'</td>
					<td>'.$rec[$i]['loctr_mobile'].'</td>
					<td>'.$rec[$i]['loctr_contno'].'</td>
					<td>'.$rec[$i]['create_time'].'</td>';
					// '<td>'.$common->date_format($rec[$i]['create_time'], DT_FMT, '').'</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("Locator Report.pdf", "D");
	}
?>