<?php include('../../../includes/config.php');
$title = "View IPD Reports";
isAdminLoggedIn();
ini_set('max_execution_time', 180000);
$diet_opd_no = isset($_GET['diet_opd_no'])?trim($_GET['diet_opd_no']):'';
$diet_ipd_no = isset($_GET['diet_ipd_no'])?trim($_GET['diet_ipd_no']):'';
$diet_ipd_department = isset($_GET['department_id_fk'])?trim($_GET['department_id_fk']):'';
$diet_provied_morning = isset($_GET['diet_provied_morning'])?trim($_GET['diet_provied_morning']):'';
$diet_provied_evening = isset($_GET['diet_provied_evening'])?trim($_GET['diet_provied_evening']):'';
$diet_date = isset($_GET['diet_date'])?trim($_GET['diet_date']):'';
$diet_start_date = isset($_GET['diet_start_date'])?trim($_GET['diet_start_date']):'';
$diet_end_date = isset($_GET['diet_end_date'])?trim($_GET['diet_end_date']):'';
$reportview = isset($_GET['reportview'])?trim($_GET['reportview']):'';
$select_all_standard_and_stream = 'select 
diet.diet_ipd_no,
diet.diet_opd_no,
diet.diet_ipd_name,
diet.diet_provied_date,
diet.diet_provied_food_type,
diagnosis.diagnosis_name,
opd.opd_age,opd.opd_gender,dept.department_name from hp_diet as diet ';
$select_all_standard_and_stream .=' LEFT JOIN hp_ipd_other_details as otherD on diet.diet_ipd_fk=otherD.ipd_o_id';
$select_all_standard_and_stream .=' LEFT JOIN hp_diagnosis as diagnosis on otherD.ipd_diagnosis=diagnosis.diagnosis_id ';
$select_all_standard_and_stream .=' LEFT JOIN hp_opd as opd on diet.diet_opd_no=opd.opd_no';

$select_all_standard_and_stream .=' LEFT JOIN hp_department as dept on diet.diet_ipd_department=dept.department_id ';
$select_all_standard_and_stream .= " where diet.is_delete='0' ";
if($diet_opd_no!=''){
    $select_all_standard_and_stream .= " and diet.diet_opd_no='".$diet_opd_no."'";
}
if($diet_ipd_no!=''){
    $select_all_standard_and_stream .="  and diet.diet_ipd_no='".$diet_ipd_no."'";
}
if($diet_ipd_department!=''){
    $select_all_standard_and_stream .=" and diet.diet_ipd_department='".$diet_ipd_department."'";
}

if($diet_date!=''){
    $select_all_standard_and_stream .=" and diet.diet_provied_date='".$diet_date."'";
}
if($diet_provied_morning!=''){
    $select_all_standard_and_stream .= " and diet.diet_provied_morning ='".$diet_provied_morning."'";
}
if($diet_provied_evening!=''){
    $select_all_standard_and_stream .=" and diet.diet_provied_evening='".$diet_provied_evening."'";
}
if($diet_start_date!='' and $diet_end_date!=''){
    $select_all_standard_and_stream .=" and  diet.diet_provied_date between'".$diet_start_date."' and '".$diet_end_date."'";
}
$select_all_standard_and_stream .=" order by diet.diet_id_pk ASC,diet_ipd_no DESC";
$row = $db->selectAll($select_all_standard_and_stream); 

$report_title = "DIET 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" colspan="2" style="font-weight: bold;">';
                        if($diet_date!=''){
                             $html .= 'DATE :'.date('d-M-Y',strtotime($diet_date));
                             $html .= '<br>';
                        }
                        if($diet_start_date!='' and $diet_end_date!=''){
                             $html .= 'DATE :'.date('d-M-Y',strtotime($diet_start_date)).' To '.date('d-M-Y',strtotime($diet_end_date));
                             $html .= '<br>';
                        }
                        if($diet_start_date!='' and $diet_end_date==''){
                             $html .= 'DATE :'.date('d-M-Y',strtotime($diet_start_date));
                             $html .= '<br>';
                        }
                        if($diet_ipd_department!=''){
                             $html .= 'Department :'.getdepartmentname($diet_ipd_department);
                             $html .= '<br>';
                        }
                        if(!empty($row)){
                             $html .= 'Total  :'.count($row);
                        }
                $html .='</td>';
                $html .='<td colspan="8" 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="25px">IPD NO </th>';
                $html .='<th width="25px">OPD NO </th>';
                $html .='<th width="170px">Name</th>';
                $html .='<th width="20px">Age</th>';
                $html .='<th width="42px">Gender</th>';
                $html .='<th width="50px">Department</th>';
                $html .='<th width="75px">DIAGNOSIS</th>';
                $html .='<th width="55px">DATE</th>';
                $html .='<th width="65px">FOOD TYPE</th>';
            $html .='</tr>';
            $i = 1;
            $totalbeddays = 0;
            foreach ($row as $key => $diet_info) {
                
                $html .='<tr align="center">';
                    $html .='<td width="15px">'.($key+1).'</td>';
                    $html .='<td width="25px" align="right">'.ucwords($diet_info['diet_ipd_no']).'</td>';
                    $html .='<td width="25px" align="right">'.ucwords($diet_info['diet_opd_no']).'</td>';
                    $html .='<td width="170px" align="left">'.ucwords($diet_info['diet_ipd_name']).'</td>';
                    $html .='<td width="20px" align="right">'.$diet_info['opd_age'].'</td>';
                    $html .='<td width="42px" align="left">'.strtoupper($diet_info['opd_gender']).'</td>';
                    $html .='<td width="50px" align="left">'.strtoupper($diet_info['department_name']).'</td>';
                    $html .='<td width="75px" align="left">'.ucwords($diet_info['diagnosis_name']).'</td>';
                    $html .='<td width="55px">'.date('d-M-Y',strtotime($diet_info['diet_provied_date'])).'</td>';
                    $html .='<td width="65px" align="left">'.$diet_info['diet_provied_food_type'].'</td>';
                $html .='</tr>';
            }
        $html .='</table>';
        $pdf->writeHTML($html, false, 0, false, 0);
        
        
        $pdf->lastPage();
        $pdf->Output('diatreport.pdf', 'I');
    }else{ 
        header("Content-type: application/vnd-ms-excel");
        header("Content-Disposition: attachment; filename=diatreport.xls");
        $html = '<table  border="0"  align="center" width="100%">';
            $html .= '<tr>';
                $html .= '<td align="left" colspan="2" style="font-weight: bold;">';
                        if($diet_date!=''){
                             $html .= 'DATE :'.date('d-M-Y',strtotime($diet_date));
                             $html .= '<br>';
                        }
                        if($diet_start_date!='' and $diet_end_date!=''){
                             $html .= 'DATE :'.date('d-M-Y',strtotime($diet_start_date)).' To '.date('d-M-Y',strtotime($diet_end_date));
                             $html .= '<br>';
                        }
                        if($diet_start_date!='' and $diet_end_date==''){
                             $html .= 'DATE :'.date('d-M-Y',strtotime($diet_start_date));
                             $html .= '<br>';
                        }
                        if($diet_ipd_department!=''){
                             $html .= 'Department :'.getdepartmentname($diet_ipd_department);
                             $html .= '<br>';
                        }
                        if(!empty($row)){
                             $html .= 'Total  :'.count($row);
                        }
                $html .='</td>';
                $html .='<td colspan="7" 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="25px">IPD NO </th>';
                $html .='<th width="25px">OPD NO </th>';
                $html .='<th width="170px">Name</th>';
                $html .='<th width="20px">Age</th>';
                $html .='<th width="42px">Gender</th>';
                $html .='<th width="50px">Department</th>';
                $html .='<th width="75px">DIAGNOSIS</th>';
                $html .='<th width="55px">DATE</th>';
                $html .='<th width="65px">FOOD TYPE</th>';
            $html .='</tr>';
            $i = 1;
            $totalbeddays = 0;
            foreach ($row as $key => $diet_info) {
                
                $html .='<tr align="center">';
                    $html .='<td width="15px">'.($key+1).'</td>';
                    $html .='<td width="25px" align="right">'.ucwords($diet_info['diet_ipd_no']).'</td>';
                    $html .='<td width="25px" align="right">'.ucwords($diet_info['diet_opd_no']).'</td>';
                    $html .='<td width="170px" align="left">'.ucwords($diet_info['diet_ipd_name']).'</td>';
                    $html .='<td width="20px" align="right">'.$diet_info['opd_age'].'</td>';
                    $html .='<td width="42px" align="left">'.strtoupper($diet_info['opd_gender']).'</td>';
                    $html .='<td width="50px" align="left">'.strtoupper($diet_info['department_name']).'</td>';
                    $html .='<td width="75px" align="left">'.ucwords($diet_info['diagnosis_name']).'</td>';
                    $html .='<td width="55px">'.date('d-M-Y',strtotime($diet_info['diet_provied_date'])).'</td>';
                    $html .='<td width="65px" align="left">'.$diet_info['diet_provied_food_type'].'</td>';
                $html .='</tr>';
            }
        $html .='</table>';
        echo $html;
    }
}
?>  
<!--  <table border="1" cellpadding="5" cellspacing="1" align="center" width="100%" style="clear:both;font-size:10px;font-weight:normal;">
                            <tr>
                                <?php
                                    if(!empty($row)){
                                        echo '<th colspan="3">TOTAL</th>';
                                        echo '<th colspan="6">TOTAL TIME WISE</th>';
                                        echo '<th colspan="4">TOTAL DATE WISE</th>';
                                    }
                                ?>
                            </tr>
                            
                            <tbody>
                                <tr>
                                    <?php
                                        if(!empty($row)){
                                            echo '<th colspan="3" > '.count($row).'</th>';
                                            echo '<th colspan="6">';
                                                echo "<span style ='color:green;'>LUNCH </span>";
                                                $gendercounting = array_count_values(array_column($row, 'diet_provied_morning'));
                                                if(!empty($gendercounting)){
                                                    foreach ($gendercounting as $gendercountingkey => $gendercountingvalue) {
                                                        if($gendercountingkey=='Yes')
                                                            echo $gendercountingkey. "=". $gendercountingvalue."&nbsp;&nbsp;";
                                                    }
                                                }
                                                echo "<br>";
                                                echo "<span style ='color:green;'>DINNER </span>";
                                                $gendercounting = array_count_values(array_column($row, 'diet_provied_evening'));
                                                
                                                if(!empty($gendercounting)){
                                                    foreach ($gendercounting as $gendercountingkey => $gendercountingvalue) {
                                                       if($gendercountingkey=='Yes')
                                                            echo $gendercountingkey. "=". $gendercountingvalue."&nbsp;&nbsp;";
                                                    }
                                                }
                                                echo "<br>";
                                            echo '</th>';
                                            
                                            echo '<th colspan="4">';
                                                $oldnewcounting = array_count_values(array_column($row, 'diet_provied_date'));
                                                if(!empty($oldnewcounting)){
                                                    foreach ($oldnewcounting as $oldnewkey => $oldnewvalue) {
                                                        $lunch_counting = $db->selectone('select count(diet_provied_morning) as lunch from hp_diet where is_delete=0 and diet_provied_date="'.$oldnewkey.'" and diet_provied_morning="Yes" ');echo Date('d-m-Y',strtotime($oldnewkey))." = ";
                                                            echo '<span style="color:green;"> LUNCH  = </span>'.$lunch_counting['lunch'];
                                                            $dinner_counting = $db->selectone('select count(diet_provied_evening) as dinner from hp_diet where is_delete=0 and diet_provied_date="'.$oldnewkey.'" and diet_provied_evening="Yes" ');
                                                            echo " &nbsp;&nbsp;&nbsp; <span style='color:green;'> DINNER =</span>".$dinner_counting['dinner']."<br>";
                                                    }
                                                }
                                            echo '</th>';
                                        }
                                    ?>
                                    
                                    
                                </tr>
                            </tbody>
                        </table> -->
           

