<?php

require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');

$formname = "Print";
$maindb = 'consultmain';
$trandb = 'consultsymp';
$file = 'consultent_pdf.php';
$zcomp = $_SESSION["comp_" . $dbname];

$zdb = $_SESSION["zdbyear_" . $dbname];
$zroot = $_SESSION["zdir"];
mysqli_select_db($conn, $zdb);

$mht = 'height:20px;';
$admin = $_SESSION["admin_" . $dbname];
$vouchno = $_REQUEST["vouchno"];
$p = $_REQUEST["ppara"];
$mainqry = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') AS vouchdt
                                          FROM `$maindb` WHERE `vouchno`='$vouchno' ORDER BY `vouchno` ") or die(mysqli_error($conn));
$getdata = mysqli_fetch_array($mainqry);

$getpat = mysqli_query($conn, "SELECT * FROM `accmast` WHERE accode='$getdata[accode]'") or die(mysqli_error($conn));
$fetpat = mysqli_fetch_array($getpat);

if ($fetpat['gender'] == 'M') {
    $gval = "MALE";
} elseif ($fetpat['gender'] == 'F') {
    $gval = "FEMALE";
} else {
    $gval = "";
}

$cmpname = mysqli_query($conn, "SELECT * From $zroot.`company` where code ='$zcomp'") or die(mysqli_error($conn));
$getcmpname = mysqli_fetch_array($cmpname);

require("library/fpdf/fpdf.php");

class pdf extends fpdf {

    function Footer() {

        //Position at 1.5 cm from bottom
        $this->SetY(-15);

        //Arial italic 8
        $this->SetFont('Arial', 'I', 8);
        //Page number
        //        $this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C');
    }
}

$ht = 7;

$pdf = new pdf('P', 'mm', 'A4');

$pdf->SetTitle('EVA');

$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Arial', 'B', 18);
$pdf->SetTextColor(0, 0, 0);
$pdf->Ln(27);

// SIDE IMAGE OF EVA
$pdf->Image('images/evapdf.jpg', 3, 1, 193, 295);

$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(50, $ht, "", 0, 0, 'R');
$pdf->Cell(23, $ht, "Patient ID     :", 0, 0, 'L');
$pdf->SetFont('Arial', '', 10);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(27, $ht, "$fetpat[accode]", 0, 0, 'L');
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(70, $ht, "Date :", 0, 0, 'R');
$pdf->SetFont('Arial', '', 10);
$pdf->Cell(20, $ht, "$getdata[vouchdt]", 0, 0, 'L');
$pdf->Ln(6);

if ($fetpat['name'] != '') {
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell(50, $ht, "", 0, 0, 'L');
    $pdf->Cell(23, $ht, "Name           :", 0, 0, 'L');
    $pdf->SetFont('Arial', '', 10);
    $name = strtoupper($fetpat['name']);
    $pdf->Cell(77, $ht, "$name", 0, 0, 'L');
    $pdf->Ln(6);
}
if ($fetpat['age'] or $fetpat['gender'] != '') {
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell(50, $ht, "", 0, 0, 'L');
    //$pdf->Cell(20, $ht, "Gender : ", 0, 0, 'R');
    //$pdf->SetFont('Arial', '', 10);
    //$pdf->Cell(10, $ht, "$gval", 0, 0, 'L');
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell(23, $ht, "Age              :", 0, 0, 'L');
    $pdf->SetFont('Arial', '', 10);
    $pdf->Cell(17, $ht, "$fetpat[age] / $fetpat[gender]", 0, 0, 'L');
    $pdf->Ln(5);
}

// $pdf->SetFont('Arial', 'B', 10);
// $pdf->Cell(60, $ht, "", 0, 0, 'L');
// $pdf->Cell(20, $ht, "Purpose : ", 0, 0, 'R');
// $pdf->SetFont('Arial', '', 10);
// $pdf->Cell(80, $ht, "$getdata[purpvisit]", 0, 0, 'L');
// $pdf->Ln(10);
// $pdf->SetFont('Arial', 'B', 10);
// $pdf->Cell(60, $ht, "", 0, 0, 'L');
// $pdf->Cell(100, $ht, "CONSULTATION ", 0, 0, 'C');
// $pdf->Cell(100, $ht, "", 0, 0, 'C');
// $pdf->Ln(2);
if ($getdata['concern1'] != '') {
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell(50, $ht, "", 0, 0, 'L');
    $pdf->Cell(23, $ht, "Concerns    : ", 0, 0, 'L');
    $pdf->SetFont('Arial', '', 10);
    $concern1 = strtoupper($getdata['concern1']);
    $pdf->Cell(77, $ht, "$concern1", 0, 0, 'L');
    $pdf->Ln(5);
}
// if ($p != 'p') {
// $sctr = 0;
// $gettran = mysqli_query($conn, "SELECT * FROM $trandb WHERE vouchno = '$getdata[vouchno]' order by srno ") or die(mysqli_error($conn));
// $pdf->SetFont('Arial', 'B', 10);
// $pdf->Cell(53, $ht, "", 0, 0, 'L');
// $pdf->Cell(20, $ht, "Symptoms  :", 0, 0, 'R');
// $pdf->SetFont('Arial', '', 10);
// while ($rowsmt = mysqli_fetch_array($gettran)) {
//     $symptoms = strtoupper($rowsmt['symptoms']);
//     $pdf->Cell(150, $ht, "$symptoms", 0, 0, 'L');
// }
// $pdf->Ln();
// }
if ($getdata['diag1'] || $getdata['diag2'] != '') {
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell(50, $ht, "", 0, 0, 'L');
    $pdf->Cell(23, $ht, "Diagnosis   : ", 0, 0, 'L');
    $pdf->SetFont('Arial', '', 10);
    $diagnosis = strtoupper($getdata['diag1'] . $getdata['diag2']);
    $pdf->Cell(77, $ht, "$diagnosis", 0, 0, 'L');
    $pdf->Ln(5);
}
if ($getdata['invs'] || $getdata['invs1'] != '') {
    $pdf->SetFont('Arial', 'B', 9);
    $pdf->Cell(53, $ht, "", 0, 0, 'L');
    $pdf->Cell(20, $ht, "Investigation:", 0, 0, 'R');
    $pdf->SetFont('Arial', '', 10);
    $investigation = strtoupper($getdata['invs']);
    $investigation1 = strtoupper($getdata['invs1']);
    $pdf->Cell(80, $ht, "$investigation $investigation1", 0, 0, 'L');
    $pdf->Ln();
}
// $pdf->SetFont('Arial', 'B', 10);
// $pdf->Cell(66, $ht, "", 0, 0, 'L');
// $pdf->Cell(20, $ht, "", 0, 0, 'R');
// $pdf->SetFont('Arial', '', 10);
// $investigation1 = strtoupper($getdata['invs1']);
// $pdf->Cell(80, $ht, "$investigation1", 0, 0, 'L');
// $pdf->Ln();

$gettreatment = mysqli_query($conn, "SELECT a.*,b.name FROM `consulttreat` a LEFT JOIN treatment b
                                     ON a.treatment = b.code where a.vouchno='$getdata[vouchno]' order by a.srno");
if (mysqli_num_rows($gettreatment) > 0) {

$pdf->SetDrawColor(255, 255, 255); // RGB color for the border

$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(60, $ht, "", 0, 0, 'L');
$pdf->Cell(100, $ht, "TREATMENTS", 0, 0, 'C');
$pdf->Ln(6);

// $pdf->SetTextColor(255, 255, 255);
$pdf->SetFont('Arial', '', 9);
$pdf->Cell(51, $ht, "", 0, 0, 'L');

$pdf->SetFillColor(238, 238, 238);
// $pdf->SetFillColor(61, 59, 60);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(10, $ht, "SR", 1, 0, 'C', 1);

$pdf->Cell(80, $ht, "PROCEDURE", 1, 0, 'L', 1);

$pdf->Cell(20, $ht, "INTERVAL", 1, 0, 'C', 1);
$pdf->Cell(30, $ht, "NO OF SESSION", 1, 0, 'C', 1);
$pdf->ln();


//$pdf->SetFont('Arial', 'B', 10);
//$pdf->Cell(60, $ht, "", 0, 0, 'L');
//$pdf->Cell(10, $ht, "Sr.", 0, 0, 'L');
//$pdf->Cell(90, $ht, "Treatment", 0, 0, 'L');
//$pdf->Cell(15, $ht, "Nos.", 0, 0, 'R');
//$pdf->Cell(15, $ht, "Days", 0, 0, 'R');
//$pdf->Ln(5);

$trtctr = 0;

    while ($rowtrt = mysqli_fetch_array($gettreatment)) {
        $lb = 'SESSION';

        if ($rowtrt['qty'] > 1) {
            $lb = 'SESSIONS';
        }

        $trtctr++;
        // $pdf->SetFont('Arial', '', 10);
        // $pdf->Cell(60, $ht, "", 0, 0, 'L');
        // $pdf->Cell(90, $ht, "$rowtrt[name]", 0, 0, 'L');
        // $pdf->Cell(15, $ht, "$rowtrt[qty] - $lb", 0, 0, 'R');
        // $pdf->Ln(5);
        // $pdf->SetTextColor(255, 255, 255);
        $pdf->SetTextColor(0, 0, 0);
        $pdf->SetFont('Arial', '', 9);
        $pdf->Cell(51, $ht, "", 0, 0, 'L');

        $pdf->SetFillColor(238, 238, 238);
        // $pdf->SetFillColor(61, 59, 60);
        $pdf->Cell(10, $ht, "$trtctr", 1, 0, 'C', 1);

        $treatment_medicine = strtoupper($rowtrt['name']);
        $pdf->Cell(80, $ht, "$treatment_medicine", 1, 0, 'L', 1);

        $pdf->Cell(20, $ht, "$rowtrt[duration]", 1, 0, 'C', 1);
        $pdf->Cell(30, $ht, "$rowtrt[qty] - $lb", 1, 0, 'C', 1);
        $pdf->ln();
    }
}

$pdf->Ln(2);

$pdf->SetFont('Arial', 'B', 10);
$y = $pdf->getY();
$pdf->Image('images/px.jpg', 62, $y, 7);
$pdf->Cell(60, $ht, "", 0, 0, 'L');
// $pdf->Cell(100, $ht, "PRESCRIPTION ", 0, 0, 'C');
//$pdf->Cell(100, $ht, " ", 0, 0, 'C');
$pdf->Ln(7);

// $gtmdtran = mysqli_query($conn, "SELECT a.*,b.name FROM `consultmed` a LEFT JOIN product b
//                                               ON a.item = b.code WHERE a.vouchno='$getdata[vouchno]'  order by a.srno");
// $pdf->SetFont('Arial', 'B', 10);
// $pdf->Cell(60, $ht, "", 0, 0, 'L');
// $pdf->Cell(10, $ht, "Sr.", 0, 0, 'L');
// $pdf->Cell(90, $ht, "Medicine", 0, 0, 'L');
// $pdf->Cell(15, $ht, "No. Days", 0, 0, 'R');
// $pdf->Ln(7);
// $mdctr = 0;
// while ($rowmd = mysqli_fetch_array($gtmdtran)) {
//  $mdctr++;
//  $pdf->SetFont('Arial', '', 10);
//  $pdf->Cell(60, $ht, "", 0, 0, 'L');
//  $pdf->Cell(10, $ht, "$mdctr", 0, 0, 'L');
//  $pdf->Cell(90, $ht, "$rowmd[name]", 0, 0, 'L');
//  $pdf->Cell(15, $ht, "$rowmd[duration]", 0, 0, 'R');
//  $pdf->Ln();
// }
// $pdf->Ln();
//$gtdsdays = mysqli_query($conn, "SELECT a.*,b.name FROM `consultmed` a LEFT JOIN product b
//                                               ON a.item = b.code WHERE a.vouchno='$getdata[vouchno]'  AND a.daytake = '1' order by a.srno");
// $pdf->SetFont('Arial', 'B', 10);
// $pdf->Cell(60, $ht, "", 0, 0, 'L');
// $pdf->Cell(15, $ht, "Morning :", 0, 0, 'R');
// $dsdaysctr = 0;
// $dosage = "";
// while ($rowdsdays = mysqli_fetch_array($gtdsdays)) {
//     $dsdaysctr++;
//     if ($dsdaysctr > 1) {
//         $pdf->Cell(60, $ht, "", 0, 0, 'L');
//         $pdf->Cell(15, $ht, "", 0, 0, 'L');
//     }
//     if ($rowdsdays['dosage'] != "") {
//         $dosage = "( $rowdsdays[dosage] )";
//     }
//     if ($rowdsdays['duration'] > 0) {
//         $dayduration = "$rowdsdays[duration] Days";
//     }
//     $pdf->SetFont('Arial', '', 10);
//     $pdf->Cell(90, $ht, "$rowdsdays[name] $dosage $dayduration", 0, 0, 'L');
//     $pdf->Ln(7);
// }
// $pdf->Ln();
// $gtdsnights = mysqli_query($conn, "SELECT a.*,b.name FROM `consultmed` a LEFT JOIN product b
//                                                ON a.item = b.code WHERE a.vouchno='$getdata[vouchno]'  AND a.ngttake = '1' order by a.srno");
// $pdf->SetFont('Arial', 'B', 10);
// $pdf->Cell(60, $ht, "", 0, 0, 'L');
// $pdf->Cell(15, $ht, "Night :", 0, 0, 'R');
// $dsngtctr = 0;
// $dosage = "";
// while ($rowdsnights = mysqli_fetch_array($gtdsnights)) {
//     $dsngtctr++;
//     if ($dsngtctr > 1) {
//         $pdf->Cell(60, $ht, "", 0, 0, 'L');
//         $pdf->Cell(15, $ht, "", 0, 0, 'L');
//     }
//     if ($rowdsnights['dosage'] != "") {
//         $dosage = "( $rowdsnights[dosage] )";
//     }
//     if ($rowdsnights['duration'] > 0) {
//         $ngtduration = "$rowdsnights[duration] Days";
//     }
//     $pdf->SetFont('Arial', '', 10);
//     $pdf->Cell(90, $ht, "$rowdsnights[name] $dosage $ngtduration", 0, 0, 'L');
//     $pdf->Ln(7);
// }
// 
// Morning

$th = 6;
$ft = 10;
//$gtdsdays = mysqli_query($conn, "SELECT a.*,b.name FROM `consultmed` a LEFT JOIN product b
//                                               ON a.item = b.code WHERE a.vouchno='$getdata[vouchno]' and a.medfor = 'SKIN'  AND a.daytake = '1' order by a.srno");
//$gtdsdays = mysqli_query($conn, "SELECT * FROM `consultmed` WHERE vouchno='$getdata[vouchno]' and medfor = 'SKIN'  AND daytake = '1' order by srno");
$gtdsdays = mysqli_query($conn, "SELECT * FROM `consultmed` WHERE vouchno='$getdata[vouchno]' and medskin = '1'  AND daytake = '1' order by srno");
if (mysqli_num_rows($gtdsdays) > 0) {


    $pdf->SetTextColor(0, 0, 0);
    $pdf->SetDrawColor(255, 255, 255); // RGB color for the border

    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell(118, $th, "SKIN - MORNING", 0, 0, 'R');
    $pdf->ln();

    // $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFont('Arial', '', 9);
    $pdf->Cell(51, $th, "", 0, 0, 'L');
    $pdf->SetFillColor(238, 238, 238);
    // $pdf->SetFillColor(61, 59, 60);
    $pdf->Cell(10, $th, "SR", 1, 0, 'C', 1);
    $pdf->SetTextColor(0, 0, 0);
    $pdf->Cell(85, $th, "MEDICINE", 1, 0, 'L', 1);
    // $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFillColor(238, 238, 238);
    $pdf->Cell(35, $th, "DOSAGE", 1, 0, 'L', 1);
    $pdf->Cell(10, $th, "DAYS", 1, 0, 'R', 1);
    $pdf->ln();

    $dsdaysctr = 0;
    $dosage = "";
    while ($rowdsdays = mysqli_fetch_array($gtdsdays)) {

        $dsdaysctr++;
        // if ($dsdaysctr > 1) {
        //     $pdf->Cell(61, $th, "", "R", 0, 'L');
        //     $pdf->Cell(60, $th, "", 0, 0, 'L');
        // }
        if ($rowdsdays['dosage'] != "") {
            $dosage = " $rowdsdays[dosage] ";
        }
        if ($rowdsdays['duration'] > 0) {
            $dayduration = "$rowdsdays[duration]";
        } else {
            $dayduration = "";
        }
        $pdf->SetFont('Arial', '', 9);
        $pdf->Cell(51, $th, "", 0, 0);
        $pdf->SetFillColor(238, 238, 238);
        // $pdf->SetFillColor(61, 59, 60);
        // $pdf->SetTextColor(255, 255, 255);
        $pdf->Cell(10, $th, "$dsdaysctr", 1, 0, 'C', 1);
        $pdf->SetTextColor(0, 0, 0);
        $morning_medicine = strtoupper($rowdsdays['itemname']);
        $pdf->Cell(85, $th, "$morning_medicine", 1, 0, 'L', 1);
        // $pdf->SetTextColor(255, 255, 255);
        $pdf->SetFillColor(238, 238, 238);
        $pdf->Cell(35, $th, "$dosage", 1, 0, 'L', 1);
        $pdf->Cell(10, $th, "$dayduration", 1, 0, 'R', 1);
        $pdf->Ln($th);
    }
    // $pdf->Ln();
    // NIGHT
}

//$gtdsnights = mysqli_query($conn, "SELECT a.*,b.name FROM `consultmed` a LEFT JOIN product b
//                                               ON a.item = b.code WHERE a.vouchno='$getdata[vouchno]' AND a.medfor = 'SKIN'  AND a.ngttake = '1' order by a.srno");
$gtdsnights = mysqli_query($conn, "SELECT * FROM `consultmed` WHERE vouchno='$getdata[vouchno]' and medskin = '1'  AND ngttake = '1' order by srno");

if (mysqli_num_rows($gtdsnights) > 0) {

    $pdf->SetTextColor(0, 0, 0);
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell(115, $th, "SKIN - NIGHT", 0, 0, 'R');
    $pdf->ln();

    $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFont('Arial', '', 9);
    $pdf->Cell(51, $th, "", 0, 0, 'L');
    $pdf->SetFillColor(238, 238, 238);
    // $pdf->SetFillColor(61, 59, 60);
    $pdf->SetTextColor(0, 0, 0);
    // $pdf->SetTextColor(255, 255, 255);
    $pdf->Cell(10, $th, "SR", 1, 0, 'C', 1);
    $pdf->Cell(85, $th, "MEDICINE", 1, 0, 'L', 1);

    // $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFillColor(238, 238, 238);
    $pdf->Cell(35, $th, "DOSAGE", 1, 0, 'L', 1);
    $pdf->Cell(10, $th, "DAYS", 1, 0, 'R', 1);
    $pdf->ln();

    $dsngtctr = 0;
    $dosage = "";
    while ($rowdsnights = mysqli_fetch_array($gtdsnights)) {

        $dsngtctr++;
        // if ($dsdaysctr > 1) {
        //     $pdf->Cell(61, $th, "", "R", 0, 'L');
        //     $pdf->Cell(60, $th, "", 0, 0, 'L');
        // }
        if ($rowdsnights['dosage'] != "") {
            $dosage = " $rowdsnights[dosage] ";
        }

        if ($rowdsnights['duration'] > 0) {
            $ngtduration = "$rowdsnights[duration]";
        } else {
            $ngtduration = " ";
        }
        $pdf->SetFont('Arial', '', 9);
        $pdf->Cell(51, $th, "", 0, 0);
        $pdf->SetFillColor(238, 238, 238);
        // $pdf->SetFillColor(61, 59, 60);
        // $pdf->SetTextColor(255, 255, 255);
        $pdf->Cell(10, $th, "$dsngtctr", 1, 0, 'C', 1);
        $pdf->SetTextColor(0, 0, 0);
        $night_medicine = strtoupper($rowdsnights['itemname']);
        $pdf->Cell(85, $th, "$night_medicine", 1, 0, 'L', 1);
        // $pdf->SetTextColor(255, 255, 255);
        $pdf->SetFillColor(238, 238, 238);
        $pdf->Cell(35, $th, "$dosage", 1, 0, 'L', 1);
        $pdf->Cell(10, $th, "$ngtduration", 1, 0, 'R', 1);
        $pdf->Ln($th);
    }
    $pdf->Ln();
}

//$gtreg = mysqli_query($conn, "SELECT a.*,b.name FROM `consultmed` a LEFT JOIN product b
//                                               ON a.item = b.code WHERE a.vouchno='$getdata[vouchno]' AND a.medfor = 'SKIN' AND a.ngttake = '' and a.daytake = '' order by a.srno");
$gtreg = mysqli_query($conn, "SELECT * FROM `consultmed` WHERE vouchno='$getdata[vouchno]' and medskin = '1'  AND ngttake = '' and daytake = '' order by srno");

if (mysqli_num_rows($gtreg) > 0) {

    $pdf->SetTextColor(0, 0, 0);
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell(115, $th, "SKIN", 0, 0, 'R');
    $pdf->ln();

    $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFont('Arial', '', 9);
    $pdf->Cell(51, $th, "", 0, 0, 'L');
    $pdf->SetFillColor(238, 238, 238);
    // $pdf->SetFillColor(61, 59, 60);
    $pdf->SetTextColor(0, 0, 0);
    // $pdf->SetTextColor(255, 255, 255);
    $pdf->Cell(10, $th, "SR", 1, 0, 'C', 1);
    $pdf->Cell(85, $th, "MEDICINE", 1, 0, 'L', 1);

    // $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFillColor(238, 238, 238);
    $pdf->Cell(35, $th, "DOSAGE", 1, 0, 'L', 1);
    $pdf->Cell(10, $th, "DAYS", 1, 0, 'R', 1);
    $pdf->ln();

    $dsngtctr = 0;
    $dosage = "";
    while ($rowreg = mysqli_fetch_array($gtreg)) {

        $dsngtctr++;
        // if ($dsdaysctr > 1) {
        //     $pdf->Cell(61, $th, "", "R", 0, 'L');
        //     $pdf->Cell(60, $th, "", 0, 0, 'L');
        // }
        if ($rowreg['dosage'] != "") {
            $dosage = " $rowreg[dosage] ";
        }

        if ($rowreg['duration'] > 0) {
            $ngtduration = "$rowreg[duration]";
        } else {
            $ngtduration = " ";
        }
        $pdf->SetFont('Arial', '', 8);
        $pdf->Cell(51, $th, "", 0, 0);
        $pdf->SetFillColor(238, 238, 238);
        // $pdf->SetFillColor(61, 59, 60);
        // $pdf->SetTextColor(255, 255, 255);
        $pdf->Cell(10, $th, "$dsngtctr", 1, 0, 'C', 1);
        $pdf->SetTextColor(0, 0, 0);
        $night_medicine = strtoupper($rowreg['itemname']);
        $pdf->Cell(85, $th, "$night_medicine", 1, 0, 'L', 1);
        // $pdf->SetTextColor(255, 255, 255);
        $pdf->SetFillColor(238, 238, 238);
        $pdf->Cell(35, $th, "$dosage", 1, 0, 'L', 1);
        $pdf->Cell(10, $th, "$ngtduration", 1, 0, 'R', 1);
        $pdf->Ln($th);
    }
    $pdf->Ln();
}

//HAIR

$th = 6;
$ft = 10;
//$gtdsdays = mysqli_query($conn, "SELECT a.*,b.name FROM `consultmed` a LEFT JOIN product b
//                                               ON a.item = b.code WHERE a.vouchno='$getdata[vouchno]' and a.medfor = 'HAIR'  AND a.daytake = '1' order by a.srno");
$gtdsdays = mysqli_query($conn, "SELECT * FROM `consultmed` WHERE vouchno='$getdata[vouchno]' and medhair = '1'  AND daytake = '1' order by srno");
if (mysqli_num_rows($gtdsdays) > 0) {


    $pdf->SetTextColor(0, 0, 0);
    $pdf->SetDrawColor(255, 255, 255); // RGB color for the border

    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell(118, $th, "HAIR - MORNING", 0, 0, 'R');
    $pdf->ln();

    // $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFont('Arial', '', 9);
    $pdf->Cell(51, $th, "", 0, 0, 'L');
    $pdf->SetFillColor(238, 238, 238);
    // $pdf->SetFillColor(61, 59, 60);
    $pdf->Cell(10, $th, "SR", 1, 0, 'C', 1);
    $pdf->SetTextColor(0, 0, 0);
    $pdf->Cell(85, $th, "MEDICINE", 1, 0, 'L', 1);
    // $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFillColor(238, 238, 238);
    $pdf->Cell(35, $th, "DOSAGE", 1, 0, 'L', 1);
    $pdf->Cell(10, $th, "DAYS", 1, 0, 'R', 1);
    $pdf->ln();

    $dsdaysctr = 0;
    $dosage = "";
    while ($rowdsdays = mysqli_fetch_array($gtdsdays)) {

        $dsdaysctr++;
        // if ($dsdaysctr > 1) {
        //     $pdf->Cell(61, $th, "", "R", 0, 'L');
        //     $pdf->Cell(60, $th, "", 0, 0, 'L');
        // }
        if ($rowdsdays['dosage'] != "") {
            $dosage = " $rowdsdays[dosage] ";
        }
        if ($rowdsdays['duration'] > 0) {
            $dayduration = "$rowdsdays[duration]";
        } else {
            $dayduration = "";
        }
        $pdf->SetFont('Arial', '', 9);
        $pdf->Cell(51, $th, "", 0, 0);
        $pdf->SetFillColor(238, 238, 238);
        // $pdf->SetFillColor(61, 59, 60);
        // $pdf->SetTextColor(255, 255, 255);
        $pdf->Cell(10, $th, "$dsdaysctr", 1, 0, 'C', 1);
        $pdf->SetTextColor(0, 0, 0);
        $morning_medicine = strtoupper($rowdsdays['itemname']);
        $pdf->Cell(85, $th, "$morning_medicine", 1, 0, 'L', 1);
        // $pdf->SetTextColor(255, 255, 255);
        $pdf->SetFillColor(238, 238, 238);
        $pdf->Cell(35, $th, "$dosage", 1, 0, 'L', 1);
        $pdf->Cell(10, $th, "$dayduration", 1, 0, 'R', 1);
        $pdf->Ln($th);
    }
    // $pdf->Ln();
    // NIGHT
}

//$gtdsnights = mysqli_query($conn, "SELECT a.*,b.name FROM `consultmed` a LEFT JOIN product b
//                                               ON a.item = b.code WHERE a.vouchno='$getdata[vouchno]' AND a.medfor = 'HAIR'  AND a.ngttake = '1' order by a.srno");
$gtdsnights = mysqli_query($conn, "SELECT * FROM `consultmed` WHERE vouchno='$getdata[vouchno]' and medhair = '1' AND ngttake = '1' order by srno");

if (mysqli_num_rows($gtdsnights) > 0) {

    $pdf->SetTextColor(0, 0, 0);
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell(115, $th, "HAIR - NIGHT", 0, 0, 'R');
    $pdf->ln();

    $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFont('Arial', '', 9);
    $pdf->Cell(51, $th, "", 0, 0, 'L');
    $pdf->SetFillColor(238, 238, 238);
    // $pdf->SetFillColor(61, 59, 60);
    $pdf->SetTextColor(0, 0, 0);
    // $pdf->SetTextColor(255, 255, 255);
    $pdf->Cell(10, $th, "SR", 1, 0, 'C', 1);
    $pdf->Cell(85, $th, "MEDICINE", 1, 0, 'L', 1);

    // $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFillColor(238, 238, 238);
    $pdf->Cell(35, $th, "DOSAGE", 1, 0, 'L', 1);
    $pdf->Cell(10, $th, "DAYS", 1, 0, 'R', 1);
    $pdf->ln();

    $dsngtctr = 0;
    $dosage = "";
    while ($rowdsnights = mysqli_fetch_array($gtdsnights)) {

        $dsngtctr++;
        // if ($dsdaysctr > 1) {
        //     $pdf->Cell(61, $th, "", "R", 0, 'L');
        //     $pdf->Cell(60, $th, "", 0, 0, 'L');
        // }
        if ($rowdsnights['dosage'] != "") {
            $dosage = " $rowdsnights[dosage] ";
        }

        if ($rowdsnights['duration'] > 0) {
            $ngtduration = "$rowdsnights[duration]";
        } else {
            $ngtduration = " ";
        }
        $pdf->SetFont('Arial', '', 9);
        $pdf->Cell(51, $th, "", 0, 0);
        $pdf->SetFillColor(238, 238, 238);
        // $pdf->SetFillColor(61, 59, 60);
        // $pdf->SetTextColor(255, 255, 255);
        $pdf->Cell(10, $th, "$dsngtctr", 1, 0, 'C', 1);
        $pdf->SetTextColor(0, 0, 0);
        $night_medicine = strtoupper($rowdsnights['itemname']);
        $pdf->Cell(85, $th, "$night_medicine", 1, 0, 'L', 1);
        // $pdf->SetTextColor(255, 255, 255);
        $pdf->SetFillColor(238, 238, 238);
        $pdf->Cell(35, $th, "$dosage", 1, 0, 'L', 1);
        $pdf->Cell(10, $th, "$ngtduration", 1, 0, 'R', 1);
        $pdf->Ln($th);
    }
    $pdf->Ln();
}

//$gtreg = mysqli_query($conn, "SELECT a.*,b.name FROM `consultmed` a LEFT JOIN product b
//                                               ON a.item = b.code WHERE a.vouchno='$getdata[vouchno]' AND a.medfor = 'HAIR' AND a.ngttake = '' and a.daytake = '' order by a.srno");
$gtreg = mysqli_query($conn, "SELECT * FROM `consultmed` WHERE vouchno='$getdata[vouchno]' and medhair = '1' AND ngttake = '' and daytake ='' order by srno");

if (mysqli_num_rows($gtreg) > 0) {

    $pdf->SetTextColor(0, 0, 0);
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell(115, $th, "HAIR", 0, 0, 'R');
    $pdf->ln();

    $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFont('Arial', '', 9);
    $pdf->Cell(51, $th, "", 0, 0, 'L');
    $pdf->SetFillColor(238, 238, 238);
    // $pdf->SetFillColor(61, 59, 60);
    $pdf->SetTextColor(0, 0, 0);
    // $pdf->SetTextColor(255, 255, 255);
    $pdf->Cell(10, $th, "SR", 1, 0, 'C', 1);
    $pdf->Cell(85, $th, "MEDICINE", 1, 0, 'L', 1);

    // $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFillColor(238, 238, 238);
    $pdf->Cell(35, $th, "DOSAGE", 1, 0, 'L', 1);
    $pdf->Cell(10, $th, "DAYS", 1, 0, 'R', 1);
    $pdf->ln();

    $dsngtctr = 0;
    $dosage = "";
    while ($rowreg = mysqli_fetch_array($gtreg)) {

        $dsngtctr++;
        // if ($dsdaysctr > 1) {
        //     $pdf->Cell(61, $th, "", "R", 0, 'L');
        //     $pdf->Cell(60, $th, "", 0, 0, 'L');
        // }
        if ($rowreg['dosage'] != "") {
            $dosage = " $rowreg[dosage] ";
        }

        if ($rowreg['duration'] > 0) {
            $ngtduration = "$rowreg[duration]";
        } else {
            $ngtduration = " ";
        }
        $pdf->SetFont('Arial', '', 8);
        $pdf->Cell(51, $th, "", 0, 0);
        $pdf->SetFillColor(238, 238, 238);
        // $pdf->SetFillColor(61, 59, 60);
        // $pdf->SetTextColor(255, 255, 255);
        $pdf->Cell(10, $th, "$dsngtctr", 1, 0, 'C', 1);
        $pdf->SetTextColor(0, 0, 0);
        $night_medicine = strtoupper($rowreg['itemname']);
        $pdf->Cell(85, $th, "$night_medicine", 1, 0, 'L', 1);
        // $pdf->SetTextColor(255, 255, 255);
        $pdf->SetFillColor(238, 238, 238);
        $pdf->Cell(35, $th, "$dosage", 1, 0, 'L', 1);
        $pdf->Cell(10, $th, "$ngtduration", 1, 0, 'R', 1);
        $pdf->Ln($th);
    }
    $pdf->Ln();
}



//NORMAL


$th = 6;
$ft = 10;
//$gtdsdays = mysqli_query($conn, "SELECT a.*,b.name FROM `consultmed` a LEFT JOIN product b
//                                               ON a.item = b.code WHERE a.vouchno='$getdata[vouchno]' and a.medfor = ''  AND a.daytake = '1' order by a.srno");
$gtdsdays = mysqli_query($conn, "SELECT * FROM `consultmed` WHERE vouchno='$getdata[vouchno]' and medhair = '' and medskin = ''  AND daytake = '1' order by srno");

if (mysqli_num_rows($gtdsdays) > 0) {


    $pdf->SetTextColor(0, 0, 0);
    $pdf->SetDrawColor(255, 255, 255); // RGB color for the border

    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell(118, $th, "MORNING", 0, 0, 'R');
    $pdf->ln();

    // $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFont('Arial', '', 9);
    $pdf->Cell(51, $th, "", 0, 0, 'L');
    $pdf->SetFillColor(238, 238, 238);
    // $pdf->SetFillColor(61, 59, 60);
    $pdf->Cell(10, $th, "SR", 1, 0, 'C', 1);
    $pdf->SetTextColor(0, 0, 0);
    $pdf->Cell(85, $th, "MEDICINE", 1, 0, 'L', 1);
    // $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFillColor(238, 238, 238);
    $pdf->Cell(35, $th, "DOSAGE", 1, 0, 'L', 1);
    $pdf->Cell(10, $th, "DAYS", 1, 0, 'R', 1);
    $pdf->ln();

    $dsdaysctr = 0;
    $dosage = "";
    while ($rowdsdays = mysqli_fetch_array($gtdsdays)) {

        $dsdaysctr++;
        // if ($dsdaysctr > 1) {
        //     $pdf->Cell(61, $th, "", "R", 0, 'L');
        //     $pdf->Cell(60, $th, "", 0, 0, 'L');
        // }
        if ($rowdsdays['dosage'] != "") {
            $dosage = " $rowdsdays[dosage] ";
        }
        if ($rowdsdays['duration'] > 0) {
            $dayduration = "$rowdsdays[duration]";
        } else {
            $dayduration = "";
        }
        $pdf->SetFont('Arial', '', 9);
        $pdf->Cell(51, $th, "", 0, 0);
        $pdf->SetFillColor(238, 238, 238);
        // $pdf->SetFillColor(61, 59, 60);
        // $pdf->SetTextColor(255, 255, 255);
        $pdf->Cell(10, $th, "$dsdaysctr", 1, 0, 'C', 1);
        $pdf->SetTextColor(0, 0, 0);
        $morning_medicine = strtoupper($rowdsdays['itemname']);
        $pdf->Cell(85, $th, "$morning_medicine", 1, 0, 'L', 1);
        // $pdf->SetTextColor(255, 255, 255);
        $pdf->SetFillColor(238, 238, 238);
        $pdf->Cell(35, $th, "$dosage", 1, 0, 'L', 1);
        $pdf->Cell(10, $th, "$dayduration", 1, 0, 'R', 1);
        $pdf->Ln($th);
    }
    // $pdf->Ln();
    // NIGHT
}

//$gtdsnights = mysqli_query($conn, "SELECT a.*,b.name FROM `consultmed` a LEFT JOIN product b
//                                               ON a.item = b.code WHERE a.vouchno='$getdata[vouchno]' AND a.medfor = ''  AND a.ngttake = '1' order by a.srno");
$gtdsnights = mysqli_query($conn, "SELECT * FROM `consultmed` WHERE vouchno='$getdata[vouchno]' and medhair = '' and medskin = ''  AND ngttake = '1' order by srno");

if (mysqli_num_rows($gtdsnights) > 0) {

    $pdf->SetTextColor(0, 0, 0);
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell(115, $th, "NIGHT", 0, 0, 'R');
    $pdf->ln();

    $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFont('Arial', '', 9);
    $pdf->Cell(51, $th, "", 0, 0, 'L');
    $pdf->SetFillColor(238, 238, 238);
    // $pdf->SetFillColor(61, 59, 60);
    $pdf->SetTextColor(0, 0, 0);
    // $pdf->SetTextColor(255, 255, 255);
    $pdf->Cell(10, $th, "SR", 1, 0, 'C', 1);
    $pdf->Cell(85, $th, "MEDICINE", 1, 0, 'L', 1);

    // $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFillColor(238, 238, 238);
    $pdf->Cell(35, $th, "DOSAGE", 1, 0, 'L', 1);
    $pdf->Cell(10, $th, "DAYS", 1, 0, 'R', 1);
    $pdf->ln();

    $dsngtctr = 0;
    $dosage = "";
    while ($rowdsnights = mysqli_fetch_array($gtdsnights)) {

        $dsngtctr++;
        // if ($dsdaysctr > 1) {
        //     $pdf->Cell(61, $th, "", "R", 0, 'L');
        //     $pdf->Cell(60, $th, "", 0, 0, 'L');
        // }
        if ($rowdsnights['dosage'] != "") {
            $dosage = " $rowdsnights[dosage] ";
        }

        if ($rowdsnights['duration'] > 0) {
            $ngtduration = "$rowdsnights[duration]";
        } else {
            $ngtduration = " ";
        }
        $pdf->SetFont('Arial', '', 9);
        $pdf->Cell(51, $th, "", 0, 0);
        $pdf->SetFillColor(238, 238, 238);
        // $pdf->SetFillColor(61, 59, 60);
        // $pdf->SetTextColor(255, 255, 255);
        $pdf->Cell(10, $th, "$dsngtctr", 1, 0, 'C', 1);
        $pdf->SetTextColor(0, 0, 0);
        $night_medicine = strtoupper($rowdsnights['itemname']);
        $pdf->Cell(85, $th, "$night_medicine", 1, 0, 'L', 1);
        // $pdf->SetTextColor(255, 255, 255);
        $pdf->SetFillColor(238, 238, 238);
        $pdf->Cell(35, $th, "$dosage", 1, 0, 'L', 1);
        $pdf->Cell(10, $th, "$ngtduration", 1, 0, 'R', 1);
        $pdf->Ln($th);
    }
    $pdf->Ln();
}

//$gtreg = mysqli_query($conn, "SELECT a.*,b.name FROM `consultmed` a LEFT JOIN product b
//                                               ON a.item = b.code WHERE a.vouchno='$getdata[vouchno]' AND a.medfor = '' AND a.ngttake = '' and a.daytake = '' order by a.srno");
$gtreg = mysqli_query($conn, "SELECT * FROM `consultmed` WHERE vouchno='$getdata[vouchno]' and medhair = '' and medskin = ''  AND ngttake = '' and daytake ='' order by srno");

if (mysqli_num_rows($gtreg) > 0) {

    $pdf->SetTextColor(0, 0, 0);
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell(115, $th, "", 0, 0, 'R');
    $pdf->ln();

    $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFont('Arial', '', 9);
    $pdf->Cell(51, $th, "", 0, 0, 'L');
    $pdf->SetFillColor(238, 238, 238);
    // $pdf->SetFillColor(61, 59, 60);
    $pdf->SetTextColor(0, 0, 0);
    // $pdf->SetTextColor(255, 255, 255);
    $pdf->Cell(10, $th, "SR", 1, 0, 'C', 1);
    $pdf->Cell(85, $th, "MEDICINE", 1, 0, 'L', 1);

    // $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFillColor(238, 238, 238);
    $pdf->Cell(35, $th, "DOSAGE", 1, 0, 'L', 1);
    $pdf->Cell(10, $th, "DAYS", 1, 0, 'R', 1);
    $pdf->ln();

    $dsngtctr = 0;
    $dosage = "";
    while ($rowreg = mysqli_fetch_array($gtreg)) {

        $dsngtctr++;
        // if ($dsdaysctr > 1) {
        //     $pdf->Cell(61, $th, "", "R", 0, 'L');
        //     $pdf->Cell(60, $th, "", 0, 0, 'L');
        // }
        if ($rowreg['dosage'] != "") {
            $dosage = " $rowreg[dosage] ";
        }

        if ($rowreg['duration'] > 0) {
            $ngtduration = "$rowreg[duration]";
        } else {
            $ngtduration = " ";
        }
        $pdf->SetFont('Arial', '', 8);
        $pdf->Cell(51, $th, "", 0, 0);
        $pdf->SetFillColor(238, 238, 238);
        // $pdf->SetFillColor(61, 59, 60);
        // $pdf->SetTextColor(255, 255, 255);
        $pdf->Cell(10, $th, "$dsngtctr", 1, 0, 'C', 1);
        $pdf->SetTextColor(0, 0, 0);
        $night_medicine = strtoupper($rowreg['itemname']);
        $pdf->Cell(85, $th, "$night_medicine", 1, 0, 'L', 1);
        // $pdf->SetTextColor(255, 255, 255);
        $pdf->SetFillColor(238, 238, 238);
        $pdf->Cell(35, $th, "$dosage", 1, 0, 'L', 1);
        $pdf->Cell(10, $th, "$ngtduration", 1, 0, 'R', 1);
        $pdf->Ln($th);
    }
    $pdf->Ln();
}

if ($getdata['advice'] != '') {
    $pdf->Ln();
    $advice = strtoupper($getdata['advice']);
    if ($advice != '') {
        $pdf->SetFont('Arial', 'B', 10);
        $pdf->Cell(50, $ht, "", 0, 0, 'L');
        $pdf->Cell(20, $ht, "Advice : ", 0, 0, 'L');
        $pdf->SetFont('Arial', '', 10);
        $pdf->Cell(80, $ht, "$advice", 0, 0, 'L');
        $pdf->Ln(5);
    }
}
ob_end_clean();
$pdf->Output("$formname - $fetpat[name]-" . $getdata['vouchno'] . ".pdf", "I");
