
  
<?php
session_start();
include("dbConfig.php");

// Check if session is set for username and name
if (isset($_SESSION['username']) && isset($_SESSION['name'])) {
    $username = $_SESSION['username'];
    $name = $_SESSION['name'];
} else {
    // If session is not set, redirect to logout page
    echo '<META http-equiv="refresh" content="0;logout.php">';
    exit;
}

// Check if Billno is passed in the URL
if (!isset($_GET['Billno']) || empty($_GET['Billno'])) {
    die("Invalid Bill Number.");
}

// Sanitize the Billno input to prevent SQL injection
$billNo = mysqli_real_escape_string($db, $_GET['Billno']);

// Fetch examination and patient details
$query = "
    SELECT 
        e.`BillNo`, e.`PatId`, e.`Particulars`, e.`Consultant`, 
        e.`Quantity`, e.`UnitPrice`, e.`Amount`, e.`created_at`,
        p.`Name` AS PatientName, p.`Contact` AS PatientContact, 
        p.`Age`, p.`Gender`, p.`Address`
    FROM `examination` e
    LEFT JOIN `patientdetails` p ON e.`PatId` = p.`PatId`
    WHERE LOWER(e.`BillNo`) = LOWER('$billNo')
";

$result = mysqli_query($db, $query);

// Check if query failed
if (!$result) {
    die("Query failed: " . mysqli_error($db));
}

// Check if results are returned
if (mysqli_num_rows($result) === 0) {
    die("No details found for Bill Number: " . htmlspecialchars($billNo));
}

// Fetch patient and examination details
$row = mysqli_fetch_assoc($result);
?>

<!DOCTYPE html>
<html>
<head>
  <title>
    
  </title>
  <meta charset="UTF-8">
  <link href="assets/img/favicon.png" rel="icon">
  <link href="assets/img/apple-touch-icon.png" rel="apple-touch-icon">
  <script src='https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>
<link href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.0.3/css/font-awesome.css'>
<link href='https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css'>
  <!-- Google Fonts -->
  <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,700,700i|Raleway:300,400,500,700,800|Montserrat:300,400,700" rel="stylesheet">
<link href='https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css'>
<link href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.0.3/css/font-awesome.css'>

  <!-- Vendor CSS Files -->
  <link href="assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
  <link href="assets/vendor/ionicons/css/ionicons.min.css" rel="stylesheet">
  <link href="assets/vendor/animate.css/animate.min.css" rel="stylesheet">
  <link href="assets/vendor/font-awesome/css/font-awesome.min.css" rel="stylesheet">
  <link href="assets/vendor/venobox/venobox.css" rel="stylesheet">
  <link href="assets/vendor/owl.carousel/assets/owl.carousel.min.css" rel="stylesheet">
  <link href="assets/vendor/boxicons/css/boxicons.min.css" rel="stylesheet">
  <script src="https://code.jquery.com/jquery.min.js"></script>
  <script src="jquery.dependent.fields.js"></script>
  <!-- Template Main CSS File -->
  <link href="assets/css/style.css" rel="stylesheet">
  <script src="https://code.jquery.com/jquery.min.js"></script>
<script src="jquery.dependent.fields.js"></script>
<style type="text/css">
    .nav-menu a {
        padding: 3px 3px;
    }
    
    .patientde{
        border:1px solid black;
        }
        
        .patientamt{
            border:2px solid black;
        }
        
        .patientde tr td{
            border:1px solid black;
        }
        
        .patientamt tr td{
            border:2px solid black;
        }
        
        
        .patientde tr td{
            border:1px solid black;
        }
        
        .patientde thead th {
            vertical-align: bottom !important;
            border: 1px solid black !important;
        }
    
    
    .footer {
       position: fixed;
       left: 0;
       bottom: 0;
       width: 100%;
       background-color: #efe8e8;
       color: black;
       text-align: center;
       padding:4px;
       font-size:12px;
    }.footer a{
      font-weight: bold;
      color:#0c0c67;
    }
      .hide {
      display: none;
    }
    h4{
      padding-top: 5px;
      padding-bottom: 0px;
      color:#1abfbf;
      font-weight: bold;
    }
    hr{
      background-color:#1abfbf;
    }
     @media print {
  #printPageButton {
    display: none;
  }
}
.spanfont{
    font-size:25px;
}
</style>

</head>
<body>
<div class="row" id="printPageButton" style="margin:5%;">
  <button  type="button" class="btn btn-primary" onclick="window.print()" >Print</button> &nbsp;&nbsp;&nbsp;
    <a href="index.php"><button type="button" class="btn btn-primary">Home</button></a>
</div>
  <section>
  <div class="row">
    <div class="col-md-1"></div>
    <div class="col-md-10">
     

  
    <div class="row">
      <div class="col-md-6 col-sm-6">
        <img src="assets/img/logo.png">
        <span class="spanfont"><b>Vidhyaranya Hospital PVT LTD.</b></span>
      </div>
     
      <div class="col-md-6 col-sm-6">
      <table>
      <tr>
        <td valign="top">Address</td>
          <td>:2876, 4th Cross, NS Road, Chamundipuram, Mysuru, Karnataka 570004</td>
      </tr>
 
      <tr>
        <td valign="top">Contact</td>
        <td>:+91-821 233 1655, +91-821 233 0555, +91 9035933963</td>
      </tr>
      <tr>
        <td>Unit</td>
        <td>:Mysore</td>
      </tr>

    </table>
      </div>
    </div>
    <div class="row mt-5">
        <div class="col-md-12 text-center">
          <label style="font-size:15pt"><b>PATIENT EXAMINATION BILL</b></label>'
        </div>
    </div>
<hr>
<div class="row d-flex justify-content-between">
  <div class="col-md-6 col-sm-6 row1 text-left">
    <table>
    <tr>
      <td valign="top">Patient Id: </td>
      <td valign="top">: <?php echo htmlspecialchars($row['PatId']); ?></td>
    </tr>
  <tr>
            <th>Name: </th>
            <td><?php echo htmlspecialchars($row['PatientName']); ?></td>
        </tr>
        <tr>
            <th>Contact: </th>
            <td><?php echo htmlspecialchars($row['PatientContact']); ?></td>
        </tr>
        <tr>
            <th>Age: </th>
            <td><?php echo htmlspecialchars($row['Age']); ?></td>
        </tr>
        <tr>
            <th>Gender: </th>
            <td><?php echo htmlspecialchars($row['Gender']); ?></td>
        </tr>
        <tr>
            <th>Address: </th>
            <td><?php echo htmlspecialchars($row['Address']); ?></td>
        </tr>
</table>
  </div>
  <div class="col-md-6 col-sm-6 row2 text-right">
    <table>   
 
       
</table>
  </div>
</div>
<br>

<br>

    <h2>Bill Details for Bill Number: <?php echo htmlspecialchars($row['BillNo']); ?></h2>

    <!-- Table to display examination details -->
    <table class="table table-hover patientde">
    <thead>
        <tr>
            <th scope="col">Sl No.</th>
            <th scope="col">Particulars</th>
            <th scope="col">Consultant</th>
            <th scope="col">Quantity</th>
            <th scope="col">Unit Price</th>
            <th scope="col">Amount</th>
        </tr>
    </thead>
    <tbody>
        <?php 
        $j = 1; // Serial Number Counter

        // Query to fetch examination details
        $queryDetails = "
            SELECT 
                e.`Particulars`, e.`Consultant`, 
                e.`Quantity`, e.`UnitPrice`, e.`Amount`
            FROM `examination` e
            WHERE LOWER(e.`BillNo`) = LOWER('$billNo')
        ";

        $resultDetails = mysqli_query($db, $queryDetails);

        // Loop through the results and display in the table
        if ($resultDetails && mysqli_num_rows($resultDetails) > 0) {
            while ($rowDetails = mysqli_fetch_assoc($resultDetails)) {
                echo "<tr>";
                echo "<td>" . $j++ . "</td>";
                echo "<td>" . htmlspecialchars($rowDetails['Particulars']) . "</td>";
                echo "<td>" . htmlspecialchars($rowDetails['Consultant']) . "</td>";
                echo "<td>" . htmlspecialchars($rowDetails['Quantity']) . "</td>";
                echo "<td>" . number_format($rowDetails['UnitPrice'], 2) . "</td>";
                echo "<td>" . number_format($rowDetails['Amount'], 2) . "</td>";
                echo "</tr>";
            }
        } else {
            echo "<tr><td colspan='6'>No details found for this bill.</td></tr>";
        }
        ?>
        <!-- Display Total Amount Row -->
        <?php 
        // Query to calculate total amount
        $totalAmountQuery = "
            SELECT SUM(`Amount`) as TotalAmount 
            FROM `examination` 
            WHERE LOWER(`BillNo`) = LOWER('$billNo')
        ";

        $totalResult = mysqli_query($db, $totalAmountQuery);
        if ($totalResult && $totalRow = mysqli_fetch_assoc($totalResult)) {
            echo "<tr>";
            echo "<td colspan='5' style='text-align: right;'><strong>Total Amount:</strong></td>";
            echo "<td><strong>" . number_format($totalRow['TotalAmount'], 2) . "</strong></td>";
            echo "</tr>";
        }
        ?>
    </tbody>
</table>



<br><br><br>
<!--<p>Prepared by <?echo $name?></p><br><br>-->


<div class="row d-flex justify-content-between">
  <div class="col-md-6 text-left"></div>
  <div class="col-md-6 text-right">
   <h6>Authorized Signatory</h6> <br>
  <h6> Prepared by <?echo $name?><h6>
  </div>
  <!--<div class="col-md-4">-->

    
  <!--</div>-->
</div>
<br><br><br><br>

    
    <div class="col-md-1"></div>
    
  </div>
</div>
    
    


</section>

 <script src='https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js'></script>
  <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>
  <script src="assets/vendor/jquery/jquery.min.js"></script>
  <script src="assets/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
  <script src="assets/vendor/jquery.easing/jquery.easing.min.js"></script>
  <script src="assets/vendor/php-email-form/validate.js"></script>
  <script src="assets/vendor/wow/wow.min.js"></script>
  <script src="assets/vendor/venobox/venobox.min.js"></script>
  <script src="assets/vendor/owl.carousel/owl.carousel.min.js"></script>
  <script src="assets/vendor/jquery-sticky/jquery.sticky.js"></script>
  <script src="assets/vendor/superfish/superfish.min.js"></script>
  <script src="assets/vendor/hoverIntent/hoverIntent.js"></script>
  <script src="assets/vendor/isotope-layout/isotope.pkgd.min.js"></script>
  <script src="assets/js/main.js"></script>
</body>
</html>
<?


?>