<?php include('../../../includes/config.php');
$title = "View OPD Reports";
isAdminLoggedIn();
ini_set('max_execution_time', 1800000);
$medicine_name = isset($_REQUEST['medicine_name_fk'])?trim($_REQUEST['medicine_name_fk']):'';
$medicine_start_date =isset($_REQUEST['medicine_start_date'])?trim($_REQUEST['medicine_start_date']):'';
$medicine_end_date =isset($_REQUEST['medicine_end_date'])?trim($_REQUEST['medicine_end_date']):'';
$medicine_department =isset($_REQUEST['medicine_department'])?trim($_REQUEST['medicine_department']):'';
$reportview = isset($_GET['reportview'])?trim($_GET['reportview']):'';
if($medicine_department==''){
    echo  "<script type='text/javascript'>";
    echo "window.close();";
    echo "</script>";
}
if($medicine_department=='opd'){
    //opd
    $select_all_opd_medicine = "SELECT pom.pom_opd_no as opdno,pom.pom_visitnumberoftime as coming,pom.pom_medicine_date as pomdate,opd.opd_name as name,opd.opd_address as address,opd.opd_age as age,opd.opd_gender as sex,department.department_name as department_name,opd.opd_old_new as oldnew FROM hp_provied_opd_patient_medicine as pom
    LEFT JOIN hp_opd as opd ON opd.opd_no = pom.pom_opd_no
    LEFT JOIN hp_department as department ON department.department_id = opd.opd_reffer_department
    WHERE pom.pom_is_delete='0' and pom.pom_visitnumberoftime=opd.opd_visitnumberoftime";
    if ($medicine_name != '') 
        $select_all_opd_medicine .=' and pom.pom_medicine_name = "' . $medicine_name . '"';
    if($medicine_start_date!='' and $medicine_end_date=='')
        $select_all_opd_medicine .=" and pom.pom_medicine_date ='".$medicine_start_date."'";
    if($medicine_start_date!='' and $medicine_end_date!='')
        $select_all_opd_medicine .=" and pom.pom_medicine_date between'".$medicine_start_date."' and '".$medicine_end_date."'";
        $select_all_opd_medicine .=" Group by pom.pom_opd_no  order by pom_id ASC ";
    $row = $db->selectAll($select_all_opd_medicine);

    //Medicine stock
    $select_opd_stock = "SELECT mn.medicine_name_details_name as mname,stock.m_name as mid,stock.m_current_stock as currentstock,unit.medicine_unit_name unitname FROM hp_medicine_stock  as stock LEFT JOIN hp_medicine_name  as mn ON mn.medicine_name_details_id_pk = stock.m_name
    LEFT JOIN hp_medicine_unit  as unit ON unit.medicine_unit_id_pk = stock.m_unit WHERE stock.m_department='opd'";
    if ($medicine_name != '') 
        $select_opd_stock .=' and stock.m_name = "' . $medicine_name . '"';
    $stockopdrow = $db->selectAll($select_opd_stock);
    $report_title = "OPD MEDICINE DISPENCHING REGISTER";
}
if($medicine_department=='ipd'){
    //ipd
     $select_all_ipd_medicine = "SELECT pim.pim_ipd_no as ipdno,pim.pim_visitnumberoftime as coming,pim.pim_medicine_date as pimdate,opd.opd_name as name,opd.opd_age as age,opd.opd_gender as sex,department.department_name as department_name,opd.opd_old_new as oldnew  FROM hp_provied_ipd_patient_medicine as pim
    LEFT JOIN hp_ipd as ipd ON ipd.ipd_no = pim.pim_ipd_no
    LEFT JOIN hp_opd as opd ON opd.opd_id = ipd.opd_id_fk
    LEFT JOIN hp_department as department ON department.department_id = opd.opd_reffer_department
    WHERE pim.pim_is_delete='0' and pim.pim_visitnumberoftime=opd.opd_visitnumberoftime";
    if ($medicine_name != '') 
        $select_all_ipd_medicine .=' and pim.pim_medicine_name = "' . $medicine_name . '"';
    if($medicine_start_date!='' and $medicine_end_date=='')
        $select_all_ipd_medicine .=" and pim.pim_medicine_date ='".$medicine_start_date."'";
    if($medicine_start_date!='' and $medicine_end_date!='')
        $select_all_ipd_medicine .=" and pim.pim_medicine_date between'".$medicine_start_date."' and '".$medicine_end_date."'";
        $select_all_ipd_medicine .=" Group by pim.pim_ipd_no order by pim_id ASC ";
    $row = $db->selectAll($select_all_ipd_medicine);
    //Medicine stock
    $select_opd_stock = "SELECT mn.medicine_name_details_name as mname,stock.m_name as mid,stock.m_current_stock as currentstock,unit.medicine_unit_name unitname FROM hp_medicine_stock  as stock LEFT JOIN hp_medicine_name  as mn ON mn.medicine_name_details_id_pk = stock.m_name
    LEFT JOIN hp_medicine_unit  as unit ON unit.medicine_unit_id_pk = stock.m_unit WHERE stock.m_department='ipd'";
    if ($medicine_name != '') 
        $select_opd_stock .=' and stock.m_name = "' . $medicine_name . '"';
    $stockipdrow = $db->selectAll($select_opd_stock);
    $report_title = "IPD MEDICINE DISPENCHING REGISTER";
}
if($medicine_department=='all'){
    //opd
    $select_all_opd_medicine = "SELECT pom.pom_opd_no as opdno,pom.pom_visitnumberoftime as coming,pom.pom_medicine_date as pomdate,opd.opd_name as name,opd.opd_address as address,opd.opd_age as age,opd.opd_gender as sex,department.department_name as department_name,opd.opd_old_new as oldnew FROM hp_provied_opd_patient_medicine as pom
    LEFT JOIN hp_opd as opd ON opd.opd_no = pom.pom_opd_no
     LEFT JOIN hp_department as department ON department.department_id = opd.opd_reffer_department
    WHERE pom.pom_is_delete='0' and pom.pom_visitnumberoftime=opd.opd_visitnumberoftime";
    if ($medicine_name != '') 
        $select_all_opd_medicine .=' and pom.pom_medicine_name = "' . $medicine_name . '"';
    if($medicine_start_date!='' and $medicine_end_date=='')
        $select_all_opd_medicine .=" and pom.pom_medicine_date ='".$medicine_start_date."'";
    if($medicine_start_date!='' and $medicine_end_date!='')
        $select_all_opd_medicine .=" and pom.pom_medicine_date between'".$medicine_start_date."' and '".$medicine_end_date."'";
        $select_all_opd_medicine .=" Group by pom.pom_opd_no  order by pom_id ASC ";
    $rowopd = $db->selectAll($select_all_opd_medicine);
    //Medicine stock
    $select_opd_stock = "SELECT mn.medicine_name_details_name as mname,stock.m_name as mid,stock.m_current_stock as currentstock,unit.medicine_unit_name unitname FROM hp_medicine_stock  as stock LEFT JOIN hp_medicine_name  as mn ON mn.medicine_name_details_id_pk = stock.m_name
    LEFT JOIN hp_medicine_unit  as unit ON unit.medicine_unit_id_pk = stock.m_unit WHERE stock.m_department='opd'";
    if ($medicine_name != '') 
        $select_opd_stock .=' and stock.m_name = "' . $medicine_name . '"';
    $stockopdrow = $db->selectAll($select_opd_stock);
    //ipd
    $select_all_ipd_medicine = "SELECT pim.pim_ipd_no as ipdno,pim.pim_visitnumberoftime as coming,pim.pim_medicine_date as pimdate,opd.opd_name as name,opd.opd_age as age,opd.opd_gender as sex,department.department_name as department_name,opd.opd_old_new as oldnew FROM hp_provied_ipd_patient_medicine as pim
    LEFT JOIN hp_ipd as ipd ON ipd.ipd_no = pim.pim_ipd_no
    LEFT JOIN hp_opd as opd ON opd.opd_id = ipd.opd_id_fk
    LEFT JOIN hp_department as department ON department.department_id = opd.opd_reffer_department
    WHERE pim.pim_is_delete='0' and pim.pim_visitnumberoftime=opd.opd_visitnumberoftime";
    if ($medicine_name != '') 
        $select_all_ipd_medicine .=' and pim.pim_medicine_name = "' . $medicine_name . '"';
    if($medicine_start_date!='' and $medicine_end_date=='')
        $select_all_ipd_medicine .=" and pim.pim_medicine_date ='".$medicine_start_date."'";
    if($medicine_start_date!='' and $medicine_end_date!='')
        $select_all_ipd_medicine .=" and pim.pim_medicine_date between'".$medicine_start_date."' and '".$medicine_end_date."'";
        $select_all_ipd_medicine .=" Group by pim.pim_ipd_no order by pim_id ASC ";
    $rowipd = $db->selectAll($select_all_ipd_medicine);
    $row = array_merge($rowopd,$rowipd); 
    //Medicine stock
    $select_opd_stock = "SELECT mn.medicine_name_details_name as mname,stock.m_name as mid,stock.m_current_stock as currentstock,unit.medicine_unit_name unitname FROM hp_medicine_stock  as stock LEFT JOIN hp_medicine_name  as mn ON mn.medicine_name_details_id_pk = stock.m_name
    LEFT JOIN hp_medicine_unit  as unit ON unit.medicine_unit_id_pk = stock.m_unit WHERE stock.m_department='ipd'";
    if ($medicine_name != '') 
        $select_opd_stock .=' and stock.m_name = "' . $medicine_name . '"';
    $stockipdrow = $db->selectAll($select_opd_stock);
    $report_title = "VIEW ALL MEDICINE REPORT";
}
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($medicine_start_date!='' and $medicine_end_date!=''){
                            $html .= 'DATE :'.date('d-M-Y',strtotime($medicine_start_date)).' To '.date('d-M-Y',strtotime($medicine_end_date));
                            $html .= '<br>';
                        }
                        if($medicine_start_date!='' and $medicine_end_date==''){
                            $html .= 'DATE :'.date('d-M-Y',strtotime($medicine_start_date));
                            $html .= '<br>';
                        }
                        if($medicine_name!=''){
                            $html .= 'MEDICINE NAME :'.getmedicinename($medicine_name);
                            $html .= '<br>';
                        }
                        if(!empty($row)){
                            $html .= 'Total  :'.count($row);
                           
                        }
                $html .='</td>';
                $html .='<td colspan="5" 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>';
        if($medicine_department=='opd'){
            $i = 0;
            $html .= '<table  border="1" cellpadding="2" align="center" width="100%">';
            if(!empty($row)){
                $html .='<tr align="center" style="font-weight:bold;">';
                     $html .='<th width="25">SR. NO.</th>';
                     $html .='<th width="45">DATE</th>';
                     $html .='<th width="35">OPD NO.</th>';
                     $html .='<th width="150">NAME OF PATIENT</th>';
                     $html .='<th width="25">AGE</th>';
                     $html .='<th width="35">GENDER</th>';
                     $html .='<th width="55">DEPARTMENT</th>';
                     $html .='<th width="30">Type</th>';
                     $html .='<th width="155">MEDICINE</th>';
               $html .='</tr>';
                foreach ($row as $rowopdkey => $rowopdvalue) {
                    $html .='<tr align="center">';
                        $html .='<td width="25">'.($i+1).'</td>';
                        $html .='<td width="45">'.ucwords(date('d-M-Y',strtotime($rowopdvalue['pomdate']))).'</td>';
                        if(isset($rowopdvalue['opdno'])){
                            $html .='<td width="35">'.ucwords($rowopdvalue['opdno']).'</td>';
                        }else{  $html .='<td width="35"> --- </td>'; }
                        $html .='<td width="150" align="left">'.ucwords($rowopdvalue['name']).'</td>';
                        $html .='<td width="25">'.ucwords($rowopdvalue['age']).'</td>';
                        $html .='<td width="35" align="left">'.ucwords($rowopdvalue['sex']).'</td>';
                        $html .='<td width="55" align="left">'.ucwords($rowopdvalue['department_name']).'</td>';
                         $html .='<td width="30">'.ucwords($rowopdvalue['oldnew']).'</td>';
                            // medicine list
                            $select_opd_medicine = "SELECT
                            mn.medicine_name_details_name as mname,
                            pom.pom_medicine_stock as mstock,
                            pom.pom_unit as munit FROM hp_provied_opd_patient_medicine  as pom
                            LEFT JOIN hp_medicine_name  as mn ON mn.medicine_name_details_id_pk = pom.pom_medicine_name 
                            WHERE pom.pom_is_delete='0' and pom.pom_opd_no='".$rowopdvalue['opdno']."' and  pom_visitnumberoftime='".$rowopdvalue['coming']."'";
                            if ($medicine_name != '') 
                                $select_opd_medicine .=' and pom.pom_medicine_name = "' . $medicine_name . '"';
                            if($medicine_start_date!='' and $medicine_end_date=='')
                                $select_opd_medicine .=" and pom.pom_medicine_date ='".$medicine_start_date."'";
                            if($medicine_start_date!='' and $medicine_end_date!='')
                                $select_opd_medicine .=" and pom.pom_medicine_date between'".$medicine_start_date."' and '".$medicine_end_date."'";
                            $allopdmedicine = $db->selectAll($select_opd_medicine);
                        $html .='<td align="left">';
                                if(!empty($allopdmedicine)){
                                    foreach ($allopdmedicine as $allopdmedicinekey => $allopdmedicinevalue) {
                                        $html .= $allopdmedicinevalue['mname'].' - '.ucwords($allopdmedicinevalue['mstock']).' - '.$allopdmedicinevalue['munit']."<br>";
                                    }
                                }
                        $html .='</td>';
                    $html .='</tr>';
                    $i++;
                }
            }
            $html .= '</table>';
            
        }
        if($medicine_department=='ipd'){
            $i = 0;
            $html .= '<table  border="1" cellpadding="2" align="center" width="100%">';
            if(!empty($row)){
                $html .='<tr align="center" style="font-weight:bold;">';
                     $html .='<th width="25">SR. NO.</th>';
                     $html .='<th width="45">DATE</th>';
                     $html .='<th width="35">IPD NO.</th>';
                     $html .='<th width="150">NAME OF PATIENT</th>';
                     $html .='<th width="25">AGE</th>';
                     $html .='<th width="35">GENDER</th>';
                     $html .='<th width="55">DEPARTMENT</th>';
                     $html .='<th width="30">TYPE</th>';
                     $html .='<th width="155">MEDICINE</th>';
               $html .='</tr>';
                foreach ($row as $rowopdkey => $rowipdvalue) {
                    $html .='<tr align="center">';
                        $html .='<td width="25">'.($i+1).'</td>';
                        $html .='<td width="45">'.ucwords(date('d-M-Y',strtotime($rowipdvalue['pimdate']))).'</td>';
                        if(isset($rowipdvalue['ipdno'])){
                            $html .='<td width="35">'.ucwords($rowipdvalue['ipdno']).'</td>';
                        }else{  $html .='<td width="35"> --- </td>'; }
                        $html .='<td width="150" align="left">'.ucwords($rowipdvalue['name']).'</td>';
                        $html .='<td width="25">'.ucwords($rowipdvalue['age']).'</td>';
                        $html .='<td width="35" align="left">'.ucwords($rowipdvalue['sex']).'</td>';
                        $html .='<td width="55" align="left">'.ucwords($rowipdvalue['department_name']).'</td>';
                         $html .='<td width="30">'.ucwords($rowipdvalue['oldnew']).'</td>';
                            // medicine list
                        $select_ipd_medicine = "SELECT mn.medicine_name_details_name as mname,
                            pim.pim_medicine_stock as mstock,
                            pim.pim_unit as munit
                            FROM hp_provied_ipd_patient_medicine  as pim
                            LEFT JOIN hp_medicine_name  as mn ON mn.medicine_name_details_id_pk = pim.pim_medicine_name 
                            WHERE pim.pim_is_delete='0' and pim.pim_ipd_no='".$rowipdvalue['ipdno']."' and  pim_visitnumberoftime='".$rowipdvalue['coming']."'";
                        if ($medicine_name != '') 
                             $select_ipd_medicine .=' and pim.pim_medicine_name = "' . $medicine_name . '"';
                        if($medicine_start_date!='' and $medicine_end_date=='')
                             $select_ipd_medicine .=" and pim.pim_medicine_date ='".$medicine_start_date."'";
                        if($medicine_start_date!='' and $medicine_end_date!='')
                             $select_ipd_medicine .=" and pim.pim_medicine_date between'".$medicine_start_date."' and '".$medicine_end_date."'";
                        $allipdmedicine = $db->selectAll($select_ipd_medicine);
                        $html .='<td align="left">';
                                if(!empty($allipdmedicine)){
                                    foreach ($allipdmedicine as $allipdmedicinekey => $allipdmedicinevalue) {
                                        $html .= $allipdmedicinevalue['mname'].' - '.ucwords($allipdmedicinevalue['mstock']).' - '.$allipdmedicinevalue['munit']."<br>";
                                    }
                                }
                        $html .='</td>';
                    $html .='</tr>';
                    $i++;
                }
            }
            $html .= '</table>';
            
        }
        if($medicine_department=='all'){
            $i = 0;

            $html .= '<table  border="1" cellpadding="2" align="center" width="100%">';
            if(!empty($rowopd)){
                $html .='<tr align="center" style="font-weight:bold;">';
                     $html .='<th width="25">SR. NO.</th>';
                     $html .='<th width="45">DATE</th>';
                     $html .='<th width="35">OPD NO.</th>';
                     $html .='<th width="150">NAME OF PATIENT</th>';
                     $html .='<th width="25">AGE</th>';
                     $html .='<th width="35">GENDER</th>';
                     $html .='<th width="55">DEPARTMENT</th>';
                     $html .='<th width="30">Type</th>';
                     $html .='<th width="155">MEDICINE</th>';
               $html .='</tr>';
                foreach ($rowopd as $rowopdkey => $rowopdvalue) {
                    $html .='<tr align="center">';
                        $html .='<td width="25">'.($i+1).'</td>';
                        $html .='<td width="45">'.ucwords(date('d-M-Y',strtotime($rowopdvalue['pomdate']))).'</td>';
                        if(isset($rowopdvalue['opdno'])){
                            $html .='<td width="35">'.ucwords($rowopdvalue['opdno']).'</td>';
                        }else{  $html .='<td width="35"> --- </td>'; }
                        $html .='<td width="150" align="left">'.ucwords($rowopdvalue['name']).'</td>';
                        $html .='<td width="25">'.ucwords($rowopdvalue['age']).'</td>';
                        $html .='<td width="35" align="left">'.ucwords($rowopdvalue['sex']).'</td>';
                        $html .='<td width="55" align="left">'.ucwords($rowopdvalue['department_name']).'</td>';
                         $html .='<td width="30">'.ucwords($rowopdvalue['oldnew']).'</td>';
                            // medicine list
                            $select_opd_medicine = "SELECT
                            mn.medicine_name_details_name as mname,
                            pom.pom_medicine_stock as mstock,
                            pom.pom_unit as munit FROM hp_provied_opd_patient_medicine  as pom
                            LEFT JOIN hp_medicine_name  as mn ON mn.medicine_name_details_id_pk = pom.pom_medicine_name 
                            WHERE pom.pom_is_delete='0' and pom.pom_opd_no='".$rowopdvalue['opdno']."' and  pom_visitnumberoftime='".$rowopdvalue['coming']."'";
                            if ($medicine_name != '') 
                                $select_opd_medicine .=' and pom.pom_medicine_name = "' . $medicine_name . '"';
                            if($medicine_start_date!='' and $medicine_end_date=='')
                                $select_opd_medicine .=" and pom.pom_medicine_date ='".$medicine_start_date."'";
                            if($medicine_start_date!='' and $medicine_end_date!='')
                                $select_opd_medicine .=" and pom.pom_medicine_date between'".$medicine_start_date."' and '".$medicine_end_date."'";
                            $allopdmedicine = $db->selectAll($select_opd_medicine);
                        $html .='<td align="left">';
                                if(!empty($allopdmedicine)){
                                    foreach ($allopdmedicine as $allopdmedicinekey => $allopdmedicinevalue) {
                                        $html .= $allopdmedicinevalue['mname'].' - '.ucwords($allopdmedicinevalue['mstock']).' - '.$allopdmedicinevalue['munit']."<br>";
                                    }
                                }
                        $html .='</td>';
                    $html .='</tr>';
                    $i++;
                }
            }
            $html .= '</table>';
            $html .= '<table  border="1" cellpadding="2" align="center" width="100%">';
            if(!empty($rowipd)){
                $html .='<tr align="center" style="font-weight:bold;">';
                     $html .='<th width="25">SR. NO.</th>';
                     $html .='<th width="45">DATE</th>';
                     $html .='<th width="35">IPD NO.</th>';
                     $html .='<th width="150">NAME OF PATIENT</th>';
                     $html .='<th width="25">AGE</th>';
                     $html .='<th width="35">GENDER</th>';
                     $html .='<th width="55">DEPARTMENT</th>';
                     $html .='<th width="30">TYPE</th>';
                     $html .='<th width="155">MEDICINE</th>';
               $html .='</tr>';
                foreach ($rowipd as $rowopdkey => $rowipdvalue) {
                    $html .='<tr align="center">';
                        $html .='<td width="25">'.($i+1).'</td>';
                        $html .='<td width="45">'.ucwords(date('d-M-Y',strtotime($rowipdvalue['pimdate']))).'</td>';
                        if(isset($rowipdvalue['ipdno'])){
                            $html .='<td width="35">'.ucwords($rowipdvalue['ipdno']).'</td>';
                        }else{  $html .='<td width="35"> --- </td>'; }
                        $html .='<td width="150" align="left">'.ucwords($rowipdvalue['name']).'</td>';
                        $html .='<td width="25">'.ucwords($rowipdvalue['age']).'</td>';
                        $html .='<td width="35" align="left">'.ucwords($rowipdvalue['sex']).'</td>';
                        $html .='<td width="55" align="left">'.ucwords($rowipdvalue['department_name']).'</td>';
                         $html .='<td width="30">'.ucwords($rowipdvalue['oldnew']).'</td>';
                            // medicine list
                        $select_ipd_medicine = "SELECT mn.medicine_name_details_name as mname,
                            pim.pim_medicine_stock as mstock,
                            pim.pim_unit as munit
                            FROM hp_provied_ipd_patient_medicine  as pim
                            LEFT JOIN hp_medicine_name  as mn ON mn.medicine_name_details_id_pk = pim.pim_medicine_name 
                            WHERE pim.pim_is_delete='0' and pim.pim_ipd_no='".$rowipdvalue['ipdno']."' and  pim_visitnumberoftime='".$rowipdvalue['coming']."'";
                        if ($medicine_name != '') 
                             $select_ipd_medicine .=' and pim.pim_medicine_name = "' . $medicine_name . '"';
                        if($medicine_start_date!='' and $medicine_end_date=='')
                             $select_ipd_medicine .=" and pim.pim_medicine_date ='".$medicine_start_date."'";
                        if($medicine_start_date!='' and $medicine_end_date!='')
                             $select_ipd_medicine .=" and pim.pim_medicine_date between'".$medicine_start_date."' and '".$medicine_end_date."'";
                        $allipdmedicine = $db->selectAll($select_ipd_medicine);
                        $html .='<td align="left">';
                                if(!empty($allipdmedicine)){
                                    foreach ($allipdmedicine as $allipdmedicinekey => $allipdmedicinevalue) {
                                        $html .= $allipdmedicinevalue['mname'].' - '.ucwords($allipdmedicinevalue['mstock']).' - '.$allipdmedicinevalue['munit']."<br>";
                                    }
                                }
                        $html .='</td>';
                    $html .='</tr>';
                    $i++;
                }
            }
            $html .= '</table>';
            
        }
        $pdf->writeHTML($html, false, 0, false, 0);
        $pdf->lastPage();
        $pdf->Output('medicinereport.pdf', 'I');
    }else{ 
        header("Content-type: application/vnd-ms-excel");
        header("Content-Disposition: attachment; filename=medicinereport.xls");
        
    }
}
?>