<?php include('../../../includes/config.php');
$title = "View Laboratory Reports";
isAdminLoggedIn();
ini_set('max_execution_time', 18000);
$department_id_fk = isset($_GET['department_id_fk'])?trim($_GET['department_id_fk']):'';
$opd_no = isset($_GET['opd_no'])?trim($_GET['opd_no']):'';
$opd_name = isset($_GET['opd_name'])?trim($_GET['opd_name']):'';
$lab_start_date = isset($_GET['lab_start_date'])?trim($_GET['lab_start_date']):'';
$lab_end_date = isset($_GET['lab_end_date'])?trim($_GET['lab_end_date']):'';
$leb_status = isset($_GET['leb_status'])?trim($_GET['leb_status']):'';
$reportview = isset($_GET['reportview'])?trim($_GET['reportview']):'';
$lab_procedure_value = isset($_GET["lab_procedure_value"])?$_GET["lab_procedure_value"]:'';
$onlyopdipd = isset($_GET["onlyopdipd"])?trim($_GET["onlyopdipd"]):'';
$select_all_standard_and_stream = 'select lab.lab_procedure,lab.lab_department,dept.department_name ,lab.lab_entrydate,lab.lab_opd_no,lab_ipd_no,lab_numberoftime_come,patient.opd_name as patientname,patient.opd_age as age,patient.opd_address as address,patient.opd_date as patientdate,patient.opd_gender   from hp_laboratorist as lab LEFT JOIN hp_opd as patient on lab.lab_opd_no=patient.opd_no LEFT JOIN hp_department as dept ON lab.lab_department=dept.department_id where lab.lab_is_delete=0 and lab.lab_procedure_value!="" ';
$where = '';
if($department_id_fk!=''){
    $where .= " and lab.lab_department='".$department_id_fk."'";
}
if($opd_no!=''){
    $where .= " and lab.lab_opd_no='".$opd_no."'";
}
if($opd_name!=''){
    $where .=" and  lab.lab_patient_name like '%".$opd_name."%'";
}
if($lab_start_date!='' and $lab_end_date==''){
   $where .= " and  lab.lab_entrydate='".$lab_start_date."'";
}
if($lab_start_date!='' and $lab_end_date!=''){
   $where .=" and   lab.lab_entrydate between'".$lab_start_date."' and '".$lab_end_date."'";
}
if($leb_status!=''){
    $where .=" and  lab.lab_status='".$leb_status."'";
}
if ($onlyopdipd != '' and $onlyopdipd=='opd' ){
    $where .=' and  lab.lab_opd_no !="" and lab_ipd_no ="0" ';
}
if ($onlyopdipd != '' and $onlyopdipd=='ipd' ){
    $where .=' and  lab.lab_ipd_no !="0" ';
}
$where1 =" group by lab_numberoftime_come,lab_patient_name order by lab.lab_entrydate ASC,lab.lab_opd_no ASC";
$row = $db->selectAll($select_all_standard_and_stream.$where.$where1); 

$report_title = "LABORATERY REGISTER";
if ($onlyopdipd != '' and $onlyopdipd=='opd' ){
    $report_title = "OPD LABORATERY REGISTER";
}
if ($onlyopdipd != '' and $onlyopdipd=='ipd' ){
    $report_title = "IPD LABORATERY 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','',7);
        $html = '<table  border="0"  align="center" width="100%">';
            $html .= '<tr>';
                $html .= '<td align="left"  style="font-weight: bold;">';
                        if($lab_start_date!='' and $lab_end_date!=''){
                            $html .= 'DATE :'.date('d-M-Y',strtotime($lab_start_date)).' To '.date('d-M-Y',strtotime($lab_end_date));
                            $html .= '<br>';
                        }
                        if($lab_start_date!='' and $lab_end_date==''){
                            $html .= 'DATE :'.date('d-M-Y',strtotime($lab_start_date));
                            $html .= '<br>';
                        }
                        if($department_id_fk!=''){
                            $html .= 'Department :'.getdepartmentname($department_id_fk);
                            $html .= '<br>';
                        }
                        if ($onlyopdipd != '' and $onlyopdipd=='opd' ){
                            $html .= 'Only OPD';
                            $html .= '<br>';
                        }
                        if ($onlyopdipd != '' and $onlyopdipd=='ipd' ){
                            $html .= 'Only IPD';
                            $html .= '<br>';
                        }
                       
                        if(!empty($row)){
                            $html .= 'Total  :'.count($row);
                          
                        }
                $html .='</td>';
                $html .='<td align="center" ';
                    if ($onlyopdipd != '' and $onlyopdipd!='' ){ $html .= "colspan='4'"; }else{  $html .="colspan='5'"; }
                 $html .= ' >';
                   $html .='<b>'.$report_title.'</b>';
                $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="55px">Date</th>';
                
                if ($onlyopdipd != '' and $onlyopdipd=='ipd' ){
                    $html .='<th width="25px">OPD NO </th>';
                    $html .='<th width="25px">IPD NO </th>';
                }
                if ($onlyopdipd != '' and $onlyopdipd=='opd' ){
                    
                    $html .='<th width="35px">OPD NO </th>';
                }
                if ($onlyopdipd == ''){
                    $html .='<th width="25px">OPD NO </th>';
                    $html .='<th width="25px">IPD NO </th>';
                }
                $html .='<th width="55px">Department</th>';
                $html .='<th width="150px">Name</th>';
                $html .='<th width="25px">Age</th>';
                $html .='<th width="40px">Gender</th>';
                $html .='<th width="75px">Address</th>';
                $html .='<th width="100px">Investigation</th>';
            $html .='</tr>';
            foreach ($row as $key => $student_info) {
                $html .='<tr align="center">';
                    $html .='<td width="55px">'.ucwords(date('d-M-Y',strtotime($student_info['patientdate']))).'</td>';
                    if ($onlyopdipd != '' and $onlyopdipd=='ipd' ){
                        $html .='<td width="25px" align="left">'.ucwords($student_info['lab_opd_no']).'</td>';
                        $html .='<td width="25px" align="left">'.ucwords($student_info['lab_ipd_no']).'</td>';
                    }
                    if ($onlyopdipd != '' and $onlyopdipd=='opd' ){
                        $html .='<td width="35px" align="left">'.ucwords($student_info['lab_opd_no']).'</td>';
                    }
                    if ($onlyopdipd == ''){
                        $html .='<td width="25px" align="left">'.ucwords($student_info['lab_opd_no']).'</td>';
                        $html .='<td width="25px" align="left">'.ucwords($student_info['lab_ipd_no']).'</td>';
                    }
                    $html .='<td width="55px" align="left">'.strtoupper($student_info['department_name']).'</td>';
                    $html .='<td width="150px" align="left">'.ucwords($student_info['patientname']).'</td>';
                    $html .='<td width="25px" align="right">'.$student_info['age'].'</td>';
                    $html .='<td width="40px" align="left">'.strtoupper($student_info['opd_gender']).'</td>';
                    $html .='<td width="75px" align="left">'.ucwords($student_info['address']).'</td>';
                    $html .='<td width="100px">';
                         $labwhere = '';
                         if ($onlyopdipd != '' and $onlyopdipd=='ipd' ){
                            $labwhere .= " and lab.lab_opd_no='".$student_info['lab_opd_no']."'";
                            $labwhere .= " and lab.lab_opd_no='".$student_info['lab_ipd_no']."'";
                        }
                        if ($onlyopdipd != '' and $onlyopdipd=='opd' ){
                            $labwhere .= " and lab.lab_opd_no='".$student_info['lab_opd_no']."'";
                            $labwhere .= " and lab.lab_ipd_no ='0' ";
                        }
                        if ($onlyopdipd == ''){
                            $labwhere .= " and lab.lab_opd_no='".$student_info['lab_opd_no']."'";
                            $labwhere .= " and lab.lab_opd_no='".$student_info['lab_ipd_no']."'";
                        }
                        $select_all_investigation = 'select lab.lab_title,lab_procedure,lab_procedure_value from hp_laboratorist as lab where lab.lab_is_delete=0 and lab.lab_procedure_value!="" and lab_numberoftime_come="'.$student_info['lab_numberoftime_come'].'"  '.$labwhere.'';
                       
                        $investigationrow = $db->selectAll($select_all_investigation);
                        foreach ($investigationrow as $investigationrowkey => $investigationrowvalue) {
                             $html .= $investigationrowvalue['lab_procedure'].' = '.$investigationrowvalue['lab_procedure_value']."<br>";
                        }
                    $html .='</td>';
                $html .='</tr>';
            }
        $html .='</table>';
        $pdf->writeHTML($html, false, 0, false, 0);
       
        $pdf->lastPage();
        $pdf->Output('labreport.pdf', 'I');
    }else{ 
        header("Content-type: application/vnd-ms-excel");
        header("Content-Disposition: attachment; filename=labreport.xls");
       $html = '<table  border="0"  align="center" width="100%">';
            $html .= '<tr>';
                $html .= '<td align="left"  style="font-weight: bold;">';
                        if($lab_start_date!='' and $lab_end_date!=''){
                            $html .= 'DATE :'.date('d-M-Y',strtotime($lab_start_date)).' To '.date('d-M-Y',strtotime($lab_end_date));
                            $html .= '<br>';
                        }
                        if($lab_start_date!='' and $lab_end_date==''){
                            $html .= 'DATE :'.date('d-M-Y',strtotime($lab_start_date));
                            $html .= '<br>';
                        }
                        if($department_id_fk!=''){
                            $html .= 'Department :'.getdepartmentname($department_id_fk);
                            $html .= '<br>';
                        }
                        if ($onlyopdipd != '' and $onlyopdipd=='opd' ){
                            $html .= 'Only OPD';
                            $html .= '<br>';
                        }
                        if ($onlyopdipd != '' and $onlyopdipd=='ipd' ){
                            $html .= 'Only IPD';
                            $html .= '<br>';
                        }
                       
                        if(!empty($row)){
                            $html .= 'Total  :'.count($row);
                          
                        }
                $html .='</td>';
                $html .='<td align="center" ';
                    if ($onlyopdipd != '' and $onlyopdipd!='' ){ $html .= "colspan='4'"; }else{  $html .="colspan='5'"; }
                 $html .= ' >';
                   $html .='<b>'.$report_title.'</b>';
                $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="55px">Date</th>';
                
                if ($onlyopdipd != '' and $onlyopdipd=='ipd' ){
                    $html .='<th width="25px">OPD NO </th>';
                    $html .='<th width="25px">IPD NO </th>';
                }
                if ($onlyopdipd != '' and $onlyopdipd=='opd' ){
                    
                    $html .='<th width="35px">OPD NO </th>';
                }
                if ($onlyopdipd == ''){
                    $html .='<th width="25px">OPD NO </th>';
                    $html .='<th width="25px">IPD NO </th>';
                }
                $html .='<th width="55px">Department</th>';
                $html .='<th width="150px">Name</th>';
                $html .='<th width="25px">Age</th>';
                $html .='<th width="40px">Gender</th>';
                $html .='<th width="75px">Address</th>';
                $html .='<th width="100px">Investigation</th>';
            $html .='</tr>';
            foreach ($row as $key => $student_info) {
                $html .='<tr align="center">';
                    $html .='<td width="55px">'.ucwords(date('d-M-Y',strtotime($student_info['patientdate']))).'</td>';
                    if ($onlyopdipd != '' and $onlyopdipd=='ipd' ){
                        $html .='<td width="25px" align="left">'.ucwords($student_info['lab_opd_no']).'</td>';
                        $html .='<td width="25px" align="left">'.ucwords($student_info['lab_ipd_no']).'</td>';
                    }
                    if ($onlyopdipd != '' and $onlyopdipd=='opd' ){
                        $html .='<td width="35px" align="left">'.ucwords($student_info['lab_opd_no']).'</td>';
                    }
                    if ($onlyopdipd == ''){
                        $html .='<td width="25px" align="left">'.ucwords($student_info['lab_opd_no']).'</td>';
                        $html .='<td width="25px" align="left">'.ucwords($student_info['lab_ipd_no']).'</td>';
                    }
                    $html .='<td width="55px" align="left">'.strtoupper($student_info['department_name']).'</td>';
                    $html .='<td width="150px" align="left">'.ucwords($student_info['patientname']).'</td>';
                    $html .='<td width="25px" align="right">'.$student_info['age'].'</td>';
                    $html .='<td width="40px" align="left">'.strtoupper($student_info['opd_gender']).'</td>';
                    $html .='<td width="75px" align="left">'.ucwords($student_info['address']).'</td>';
                    $html .='<td width="100px">';
                         $labwhere = '';
                         if ($onlyopdipd != '' and $onlyopdipd=='ipd' ){
                            $labwhere .= " and lab.lab_opd_no='".$student_info['lab_opd_no']."'";
                            $labwhere .= " and lab.lab_opd_no='".$student_info['lab_ipd_no']."'";
                        }
                        if ($onlyopdipd != '' and $onlyopdipd=='opd' ){
                            $labwhere .= " and lab.lab_opd_no='".$student_info['lab_opd_no']."'";
                            $labwhere .= " and lab.lab_ipd_no ='0' ";
                        }
                        if ($onlyopdipd == ''){
                            $labwhere .= " and lab.lab_opd_no='".$student_info['lab_opd_no']."'";
                            $labwhere .= " and lab.lab_opd_no='".$student_info['lab_ipd_no']."'";
                        }
                        $select_all_investigation = 'select lab.lab_title,lab_procedure,lab_procedure_value from hp_laboratorist as lab where lab.lab_is_delete=0 and lab.lab_procedure_value!="" and lab_numberoftime_come="'.$student_info['lab_numberoftime_come'].'"  '.$labwhere.'';
                       
                        $investigationrow = $db->selectAll($select_all_investigation);
                        foreach ($investigationrow as $investigationrowkey => $investigationrowvalue) {
                             $html .= $investigationrowvalue['lab_procedure'].' = '.$investigationrowvalue['lab_procedure_value']."<br>";
                        }
                    $html .='</td>';
                $html .='</tr>';
            }
        $html .='</table>';
        echo $html;
    }
}
?>
    
 
 