<?php include('../../../includes/config.php');
$title = "View OPD Reports";
isAdminLoggedIn();
ini_set('max_execution_time', 18000);
$department_id_fk = isset($_GET['department_id_fk'])?trim($_GET['department_id_fk']):'';
$opd_gender = isset($_GET['opd_gender'])?trim($_GET['opd_gender']):'';
$opd_case = isset($_GET['opd_case'])?trim($_GET['opd_case']):'';
$opd_start_date = isset($_GET['opd_start_date'])?trim($_GET['opd_start_date']):'';
$opd_end_date = isset($_GET['opd_end_date'])?trim($_GET['opd_end_date']):'';
$reportview = isset($_GET['reportview'])?trim($_GET['reportview']):'';
$select_all_standard_and_stream = 'select opd.*,dept.department_name from hp_opd as opd LEFT JOIN hp_department as dept ON opd.opd_reffer_department=dept.department_id  where opd.opd_status="Active"';
if($department_id_fk!=''){
    $select_all_standard_and_stream .= " and opd.opd_reffer_department='".$department_id_fk."'";
}
if($opd_case!=''){
    $select_all_standard_and_stream .= " and opd.opd_old_new='".$opd_case."'";
}
if($opd_gender!=''){
    $select_all_standard_and_stream .=" and opd.opd_gender='".$opd_gender."'";
}
if($opd_start_date!='' and $opd_end_date!=''){
   $select_all_standard_and_stream .=" and opd.opd_date between'".$opd_start_date."' and '".$opd_end_date."'";
}
if($opd_start_date!='' and $opd_end_date!='' and $opd_no=='' and $department_id_fk=='' and $opd_gender==''){
  $select_all_standard_and_stream .=" and opd.opd_reffer_by_department ='OPD'";
}
if($opd_start_date!='' and $opd_end_date!='' and $department_id_fk!=''){
  $select_all_standard_and_stream .=" Group by opd.opd_no ";
}
$select_all_standard_and_stream .=" order by opd.opd_id ASC,opd.opd_no ASC";
$row = $db->selectAll($select_all_standard_and_stream); 
$report_title = "CENTRAL OPD REGISTER";
if($department_id_fk!='' ){
   $report_title =" ".getdepartmentname($department_id_fk)." OPD REGISTER";
}
if(!empty($row)) {
    if($reportview=="View Report"){
        require_once('tcpdf_include.php');
        class MYPDF extends TCPDF {

            //Page header
            public function Header() {
               
            }

            // Page footer
            public function Footer() {
                // Position at 15 mm from bottom
                $this->SetY(-15);
                // Set font
                $this->SetFont('helvetica', '', 8);
                // Page number infinitylogo.png
                
                
                $this->Cell(0, 10,'  Print On:   '.date('d-M-y h:i:s A').'', 0, false, 'L', 0, '', 0, false, 'T', 'M');
                $this->Cell(0, 10, 'Page '.$this->getAliasNumPage().' of '.$this->getAliasNbPages(), 0, false, 'R', 0, '', 0, false, 'T', 'M');
            }
            public function ColoredTable($header,$data) {
                // Colors, line width and bold font
                $this->SetFillColor(128,128,128);
                $this->SetTextColor(255);
                
                $this->SetLineWidth(0.3);
                $this->SetFont('', 'B');
                // Header
                $w = array(40, 35, 40, 45);
                $num_headers = count($header);
                for($i = 0; $i < $num_headers; ++$i) {
                    $this->Cell($w[$i], 7, $header[$i], 1, 0, 'C', 1);
                }
                $this->Ln();
                // Color and font restoration
                $this->SetFillColor(224, 235, 255);
                $this->SetTextColor(0);
                $this->SetFont('');
                // Data
                $fill = 0;
                foreach($data as $row) {
                    $this->Cell($w[0], 6, $row[0], 'LR', 0, 'L', $fill);
                    $this->Cell($w[1], 6, $row[1], 'LR', 0, 'L', $fill);
                    $this->Cell($w[2], 6, $row[2], 'LR', 0, 'R', $fill);
                    $this->Cell($w[3], 6, $row[3], 'LR', 0, 'R', $fill);
                    $this->Ln();
                    $fill=!$fill;
                }
                $this->Cell(array_sum($w), 0, '', 'T');
            }
            function plot_table($widths, $lineheight, $table, $border=1, $aligns=array(), $fills=array(), $links=array()){
                $func = function($text, $c_width){
                    $len=strlen($text);
                    $twidth = $this->GetStringWidth($text);
                    $split = floor($c_width * $len / $twidth);
                    $w_text = explode( "\n", wordwrap( $text, $split, "\n", true));
                    return $w_text;
                };
                foreach ($table as $line){
                    $line = array_map($func, $line, $widths);
                    $maxlines = max(array_map("count", $line));
                    foreach ($line as $key => $cell){
                        $x_axis = $this->getx();
                        $height = $lineheight * $maxlines / count($cell);
                        $len = count($line);
                        $width = (isset($widths[$key]) === TRUE ? $widths[$key] : $widths / count($line));
                        $align = (isset($aligns[$key]) === TRUE ? $aligns[$key] : '');
                        $fill = (isset($fills[$key]) === TRUE ? $fills[$key] : false);
                        $link = (isset($links[$key]) === TRUE ? $links[$key] : '');
                        foreach ($cell as $textline){
                            $this->cell($widths[$key],$height,$textline,0,0,$align,$fill,$link);
                            $height += 2 * $lineheight * $maxlines / count($cell);
                            $this->SetX($x_axis);
                        }
                        if($key == $len - 1){
                            $lbreak=1;
                        }
                        else{
                            $lbreak = 0;
                        }
                        $this->cell($widths[$key],$lineheight * $maxlines, '',$border,$lbreak);
                    }
                }
            }
        }
        ob_clean();
        $pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
        // add a page
        $pdf->AddPage();
        // Title
        $pdf->SetFont('times','',8);
        $html = '<table  border="0"  align="center" width="100%">';
            $html .= '<tr>';
                $html .= '<td align="left" colspan="2" style="font-weight: bold;">';
                        if($opd_start_date!='' and $opd_end_date!=''){
                             $html .= 'DATE :'.date('d-M-Y',strtotime($opd_start_date)).' To '.date('d-M-Y',strtotime($opd_end_date));
                             $html .= '<br>';
                        }
                        if($opd_start_date!='' and $opd_end_date==''){
                            $html .= 'DATE :'.date('d-M-Y',strtotime($opd_start_date));
                            $html .= '<br>';
                        }
                        if($opd_gender!=''){
                            $html .= 'Gender :'.strtoupper($opd_gender);
                            $html .= '<br>';
                        }
                        if($department_id_fk!=''){
                            $html .= 'Department :'.getdepartmentname($department_id_fk);
                            $html .= '<br>';
                        }
                        if(!empty($row)){
                            $html .= 'Total  :'.count($row);
                           
                        }
                $html .='</td>';
                $html .='<td colspan="6" align="center">';
                   $html .='<h3 class="panel-title">'.$report_title.'</h3>';
                $html .='</td>';
                $html .='<td align="right" height="50">';
                   $html .='<img src="'.SITE.'/img/logo.jpg" height="50">';
                $html .='</td>';
            $html .='</tr>';
         $html .= '</table>';
        $html .= '<table  border="1" cellpadding="1" cellspacing="0" width="100%">';
           
            $html .='<tr align="center" style="font-weight:bold;">';
                $html .='<th width="15px">Sr.No</th>';
                $html .='<th width="55px">Date</th>';
                $html .='<th width="35px">OPD NO </th>';
                $html .='<th width="200px">Name</th>';
                $html .='<th width="25px">Age</th>';
                $html .='<th width="40px">Gender</th>';
                $html .='<th width="55px">Department</th>';
                $html .='<th width="75px">Address</th>';
                $html .='<th width="45px">Type</th>';
            $html .='</tr>';
            foreach ($row as $key => $student_info) {
                $html .='<tr align="center">';
                    $html .='<td width="15px">'.($key+1).'</td>';
                    $html .='<td width="55px">'.ucwords(date('d-M-Y',strtotime($student_info['opd_date']))).'</td>';
                    $html .='<td width="35px" align="right">'.ucwords($student_info['opd_no']).'</td>';
                    $html .='<td width="200px" align="left">'.ucwords($student_info['opd_name']).'</td>';
                    $html .='<td width="25px" align="right">'.$student_info['opd_age'].'</td>';
                    $html .='<td width="40px" align="left">'.strtoupper($student_info['opd_gender']).'</td>';
                    $html .='<td width="55px" align="left">'.strtoupper($student_info['department_name']).'</td>';
                    $html .='<td width="75px" align="left">'.ucwords($student_info['opd_address']).'</td>';
                    $html .='<td width="45px">'.$student_info['opd_old_new'].'</td>';
                $html .='</tr>';
            }
        $html .='</table>';
        $pdf->writeHTML($html, false, 0, false, 0);
        $html ='<table border="1" cellpadding="5" cellspacing="1" align="center" width="100%" >';
            $html .='<tr>';
                if(!empty($row)){
                    $html .='<th colspan="2">Total Case</th>';
                    $html .='<th colspan="2">Total Gender Wise</th>';
                    $html .='<th colspan="3">Total Department Wise</th>';
                    $html .='<th colspan="2">Total New / Old</th>';
                }
            $html .='</tr>';
            $html .='<tr>';
                if(!empty($row)){
                    $html .='<th colspan="2"> '.count($row).'</th>';
                    $html .='<th colspan="2">';
                        $gendercounting = array_count_values(array_column($row, 'opd_gender'));
                        if(!empty($gendercounting)){
                            foreach ($gendercounting as $gendercountingkey => $gendercountingvalue) {
                                $html .=$gendercountingkey. "=". $gendercountingvalue."<br>";
                            }
                        }
                    $html .='</th>';
                    $html .='<th colspan="3">';
                        $departmentcounting = array_count_values(array_column($row, 'opd_reffer_department'));
                        if(!empty($departmentcounting)){
                            foreach ($departmentcounting as $departmentkey => $departmentvalue) {
                                $html .=getdepartmentname($departmentkey). "=". $departmentvalue."<br>";
                            }
                        }
                        
                        
                    $html .='</th>';
                    $html .='<th colspan="2">';
                        $oldnewcounting = array_count_values(array_column($row, 'opd_old_new'));
                        if(!empty($oldnewcounting)){
                            foreach ($oldnewcounting as $oldnewkey => $oldnewvalue) {
                                $html .=$oldnewkey. "=". $oldnewvalue."<br>";
                            }
                        }
                    $html .='</th>';
                }
            $html .='</tr>';
        $html .='</table>';
        $pdf->writeHTML($html, false, 0, false, 0);
        $pdf->lastPage();
        $pdf->Output('opdreport.pdf', 'I');
    }else{ 
        header("Content-type: application/vnd-ms-excel");
        header("Content-Disposition: attachment; filename=opdreport.xls");
        $html = '<table  border="0"  align="center" width="100%">';
            $html .= '<tr>';
                $html .= '<td align="left" colspan="2" style="font-weight: bold;">';
                        if($opd_start_date!='' and $opd_end_date!=''){
                             $html .= 'DATE :'.date('d-M-Y',strtotime($opd_start_date)).' To '.date('d-M-Y',strtotime($opd_end_date));
                             $html .= '<br>';
                        }
                        if($opd_start_date!='' and $opd_end_date==''){
                            $html .= 'DATE :'.date('d-M-Y',strtotime($opd_start_date));
                            $html .= '<br>';
                        }
                        if($opd_gender!=''){
                            $html .= 'Gender :'.strtoupper($opd_gender);
                            $html .= '<br>';
                        }
                        if($department_id_fk!=''){
                            $html .= 'Department :'.getdepartmentname($department_id_fk);
                            $html .= '<br>';
                        }
                        if(!empty($row)){
                            $html .= 'Total  :'.count($row);
                           
                        }
                $html .='</td>';
                $html .='<td colspan="6" align="center">';
                   $html .='<h3 class="panel-title">'.$report_title.'</h3>';
                $html .='</td>';
                $html .='<td align="right" height="50">';
                   $html .='<img src="'.SITE.'/img/logo.jpg" height="50">';
                $html .='</td>';
            $html .='</tr>';
         $html .= '</table>';
        $html .= '<table  border="1" cellpadding="1" cellspacing="0" width="100%">';
            $html .='<tr align="center" style="font-weight:bold;">';
                $html .='<th width="15px">Sr.No</th>';
                $html .='<th width="55px">Date</th>';
                $html .='<th width="35px">OPD NO </th>';
                $html .='<th width="200px">Name</th>';
                $html .='<th width="25px">Age</th>';
                $html .='<th width="40px">Gender</th>';
                $html .='<th width="55px">Department</th>';
                $html .='<th width="75px">Address</th>';
                $html .='<th width="45px">Type</th>';
            $html .='</tr>';
            foreach ($row as $key => $student_info) {
                $html .='<tr align="center">';
                    $html .='<td width="15px">'.($key+1).'</td>';
                    $html .='<td width="55px">'.ucwords(date('d-M-Y',strtotime($student_info['opd_date']))).'</td>';
                    $html .='<td width="35px" align="right">'.ucwords($student_info['opd_no']).'</td>';
                    $html .='<td width="200px" align="left">'.ucwords($student_info['opd_name']).'</td>';
                    $html .='<td width="25px" align="right">'.$student_info['opd_age'].'</td>';
                    $html .='<td width="40px" align="left">'.strtoupper($student_info['opd_gender']).'</td>';
                    $html .='<td width="55px" align="left">'.strtoupper($student_info['department_name']).'</td>';
                    $html .='<td width="75px" align="left">'.ucwords($student_info['opd_address']).'</td>';
                    $html .='<td width="45px">'.$student_info['opd_old_new'].'</td>';
                $html .='</tr>';
            }
        $html .='</table>';
        $html .='<table border="1" cellpadding="5" cellspacing="1" align="center" width="100%" >';
            $html .='<tr>';
                if(!empty($row)){
                    $html .='<th colspan="2">Total Case</th>';
                    $html .='<th colspan="2">Total Gender Wise</th>';
                    $html .='<th colspan="3">Total Department Wise</th>';
                    $html .='<th colspan="2">Total New / Old</th>';
                }
            $html .='</tr>';
            $html .='<tr>';
                if(!empty($row)){
                    $html .='<th colspan="2"> '.count($row).'</th>';
                    $html .='<th colspan="2">';
                        $gendercounting = array_count_values(array_column($row, 'opd_gender'));
                        if(!empty($gendercounting)){
                            foreach ($gendercounting as $gendercountingkey => $gendercountingvalue) {
                                $html .=$gendercountingkey. "=". $gendercountingvalue."<br>";
                            }
                        }
                    $html .='</th>';
                    $html .='<th colspan="3">';
                        $departmentcounting = array_count_values(array_column($row, 'opd_reffer_department'));
                        if(!empty($departmentcounting)){
                            foreach ($departmentcounting as $departmentkey => $departmentvalue) {
                                $html .=getdepartmentname($departmentkey). "=". $departmentvalue."<br>";
                            }
                        }
                        
                        
                    $html .='</th>';
                    $html .='<th colspan="2">';
                        $oldnewcounting = array_count_values(array_column($row, 'opd_old_new'));
                        if(!empty($oldnewcounting)){
                            foreach ($oldnewcounting as $oldnewkey => $oldnewvalue) {
                                $html .=$oldnewkey. "=". $oldnewvalue."<br>";
                            }
                        }
                    $html .='</th>';
                }
            $html .='</tr>';
        $html .='</table>';
        echo $html;
    }
}
?>