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

// Check if the username session variable is set
if (!isset($_SESSION['username'])) {
    // Redirect to logout.php if the session is not set
    header("Location: logout.php");
    exit();  // Stop further script execution after redirect
}

// You can safely use the username here
$username = $_SESSION['username'];
// Initialize query variables
$query1 = $query2 = $query3 = $query4 = null;

// Process form submission
if (isset($_POST['datefrom']) && isset($_POST['dateto'])) {
    $from = $_POST['datefrom'];
    $to = $_POST['dateto'];

    // Validate date input
    if ($from > $to) {
        echo "<h3>Invalid date range. 'From' date cannot be greater than 'To' date.</h3>";
    } else {
        // Fetch records based on date range
        $query1 = mysqli_query($db, "SELECT a.*, p.Name AS PatientName, GROUP_CONCAT(b.optionlab SEPARATOR ', ') AS OptionLabs 
                                    FROM advance a
                                    LEFT JOIN patientdetails p ON a.PatId = p.PatId
                                    LEFT JOIN billparticulars b ON a.BillNo = b.BillNo
                                    WHERE a.AdvanceDate >= '$from' AND a.AdvanceDate <= '$to'
                                    GROUP BY a.PatId, a.BillNo");

        $query2 = mysqli_query($db, "SELECT f.*, p.Name AS PatientName, GROUP_CONCAT(b.optionlab SEPARATOR ', ') AS OptionLabs 
                                    FROM finalbill f
                                    LEFT JOIN patientdetails p ON f.PatientId = p.PatId
                                    LEFT JOIN billparticulars b ON f.FinalBillNo = b.BillNo
                                    WHERE f.DischargeDate >= '$from' AND f.DischargeDate <= '$to'
                                    GROUP BY f.PatientId, f.FinalBillNo");

        $query3 = mysqli_query($db, "SELECT i.*, p.Name AS PatientName, GROUP_CONCAT(b.optionlab SEPARATOR ', ') AS OptionLabs 
                                    FROM inbill i
                                    LEFT JOIN patientdetails p ON i.PatientId = p.PatId
                                    LEFT JOIN billparticulars b ON i.BillNo = b.BillNo
                                    WHERE i.BDate >= '$from' AND i.BDate <= '$to'
                                    GROUP BY i.PatientId, i.BillNo");

        $query4 = mysqli_query($db, "SELECT b.*, p.Name AS PatientName, GROUP_CONCAT(bp.optionlab SEPARATOR ', ') AS OptionLabs 
                                    FROM bill b
                                    LEFT JOIN patientdetails p ON b.PatientId = p.PatId
                                    LEFT JOIN billparticulars bp ON b.BillNo = bp.BillNo
                                    WHERE b.BDate >= '$from' AND b.BDate <= '$to'
                                    GROUP BY b.PatientId, b.BillNo");

        // Check if queries are successful
        if (!$query1 || !$query2 || !$query3 || !$query4) {
            echo "Error executing queries: " . mysqli_error($db);
            exit();
        }
    }
}
?>









    <!DOCTYPE html>
    <html lang="en">
    <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
    <head>
    <title>NPH-Amdin</title>

    <link rel="icon" href="images/logo.png" type="image/x-icon" class="abc">
    
    <link rel="icon" type="image/icon" href="images/logo.png" />
    
    <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700,800" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Quicksand:500,700" rel="stylesheet">
    
    <link rel="stylesheet" type="text/css" href="../files/bower_components/bootstrap/css/bootstrap.min.css">
    
    <link rel="stylesheet" href="../files/assets/pages/waves/css/waves.min.css" type="text/css" media="all">
    
    <link rel="stylesheet" type="text/css" href="../files/assets/icon/feather/css/feather.css">
    
    <link rel="stylesheet" type="text/css" href="../files/assets/css/font-awesome-n.min.css">
    
    <link rel="stylesheet" href="../files/bower_components/chartist/css/chartist.css" type="text/css" media="all">
    
    <link rel="stylesheet" type="text/css" href="../files/assets/css/style.css">
    <link rel="stylesheet" type="text/css" href="../files/assets/css/widget.css">
    
    <style>
        .img-fluid {
    max-width: 66%;
    height: auto;
    margin-left: -47px;
}
.pcoded li.pcoded-hasmenu[dropdown-icon=style1]>a:after {
    content: none;
}
.pcoded[theme-layout=vertical] .pcoded-navbar .pcoded-item[item-border=true][item-border-style=solid] li>a {
    border-bottom-style: solid;
    height: 53px;
}
div#c1 {
    background-color: cadetblue;
}
div#c2 {
    background-color: slategrey;
}
div#c3 {
    background-color: salmon;
}
div#c4 {
    background-color: #a549a5;
}
#dom-jqry{
    font-size:12px;
    
}
.dataTable.table td, .dataTable.table th {
    padding: 1px;
}
    </style>

    </head>
    <body>
    
    <div class="loader-bg">
    <div class="loader-bar"></div>
    </div>
    
    <div id="pcoded" class="pcoded">
    <div class="pcoded-overlay-box"></div>
    <div class="pcoded-container navbar-wrapper">
    
    <nav class="navbar header-navbar pcoded-header">
    <div class="navbar-wrapper">
    <div class="navbar-logo">
    <a href="index.php">
     <h5>Admin</h5>
    </a>
    <a class="mobile-menu" id="mobile-collapse" href="#!">
    <i class="feather icon-menu icon-toggle-right"></i>
    </a>
    <a class="mobile-options waves-effect waves-light">
    <i class="feather icon-more-horizontal"></i>
    </a>
    </div>
    <div class="navbar-container container-fluid">
    <ul class="nav-left">
    <li class="header-search">
    <div class="main-search morphsearch-search">
    <div class="input-group">

    <input type="text" class="form-control" placeholder="Enter Keyword">

    </div>
    </div>
    </li>
    
    </a>
    </li>
    </ul>
    <ul class="nav-right">
    <li class="header-notification">
    <div class="dropdown-primary dropdown">
   
    <ul class="show-notification notification-view dropdown-menu" data-dropdown-in="fadeIn" data-dropdown-out="fadeOut">
    
    <li>
   
    </li>
    <li>
    
    </li>
    <li>
   
    </li>
    </ul>
    </div>
    </li>
   
    <li class="user-profile header-notification">
    <div class="dropdown-primary dropdown">
    <div class="dropdown-toggle" data-toggle="dropdown">
    <img src="../files/assets/images/avatar-4.jpg" class="img-radius" alt="User-Profile-Image">
    <span>Admin</span>
    <i class="feather icon-chevron-down"></i>
    </div>
    <ul class="show-notification profile-notification dropdown-menu" data-dropdown-in="fadeIn" data-dropdown-out="fadeOut">
   
    <!--<li>-->
    <!--<a href="#">-->
    <!--<i class="feather icon-user"></i> Profile-->
    <!--</a>-->
    <!--</li>-->
    <li>
    <a href="logout.php">
    <i class="feather icon-log-out"></i> Logout
    </a>
    </li>
    </ul>
    </div>
    </li>
    </ul>
    </div>
    </div>
    </nav>
    
    
    
  
   
    <div class="pcoded-main-container">
    <div class="pcoded-wrapper">
    
    <nav class="pcoded-navbar">
    <div class="nav-list">
    <div class="pcoded-inner-navbar main-menu">
    <div class="pcoded-navigation-label">Navigation</div>
    <ul class="pcoded-item pcoded-left-item">
        
    <li class="pcoded-hasmenu active pcoded-trigger">
        <a href="index.php" class="waves-effect waves-dark">
            <span class="pcoded-micon"><i class="feather icon-home"></i></span>
            <span class="pcoded-mtext">Dashboard</span>
        </a>
    </li>
    
    <li class="pcoded-hasmenu">
        <a href="billlist.php" class="waves-effect waves-dark">
            <span class="pcoded-micon"><i class="fa fa-registered"></i></span>
            <span class="pcoded-mtext">View Bill</span>
        </a>
    </li>
        <li class="pcoded-hasmenu">
        <a href="daily.php" class="waves-effect waves-dark">
            <span class="pcoded-micon"><i class="fa fa-registered"></i></span>
            <span class="pcoded-mtext">Daily Transaction</span>
        </a>
    </li>
            </ul> 
    </div>
    </div>
    </nav>

    <div class="pcoded-content">

    <div class="page-header card">
    <div class="row align-items-end">
    <div class="col-lg-8">
    <div class="page-header-title">
    <i class="feather icon-inbox bg-c-blue"></i>
    <div class="d-inline">
    <h5>Search Daily Transaction Details</h5>
    </div>
    </div>
    </div>
    <div class="col-lg-4">
    <div class="page-header-breadcrumb">
    <ul class=" breadcrumb breadcrumb-title">
    <li class="breadcrumb-item">
    <a href="index.html"><i class="feather icon-home"></i></a>
    </li>
    <li class="breadcrumb-item"><a href="#!">Home</a>
    </li>
    <li class="breadcrumb-item"><a href="#!">Daily Transaction Details</a>
    </li>
    
    
    
    
    </ul>
    </div>
    </div>
    </div>
    </div>

    <div class="pcoded-inner-content">

    <div class="main-body">
    <div class="page-wrapper">

    <div class="page-body">

    <div class="card">
    <div class="card-header">
    <h5>Daily Transaction Details</h5>
    </div>
    
    <div class="card-block">
     

<!-- HTML Form and Table -->
<form method="POST" action=""><h5>Enter  Dates</h5>
<div class="row">
  <div class="form-group col">
  <label for="datefrom">From</label>
  <input  class="form-control" type="date" name="datefrom" id="datefrom" required>
</div>
 <div class="form-group col">
  <label for="dateto">To</label>
  <input  class="form-control" type="date" name="dateto" id="dateto" required>
</div>
 <div class="col">
  
     <button type="submit" class="btn btn-primary" id="viewbtn">View</button>
 </div>
</div>
 


</form>
     
     


<?php if (isset($query1, $query2, $query3, $query4) && 
    (mysqli_num_rows($query1) != 0 || mysqli_num_rows($query2) != 0 || mysqli_num_rows($query3) != 0 || mysqli_num_rows($query4) != 0)) : ?>
    <button type="button" class="btn btn-primary" onclick="exportToExcel('tblexportData')">Export To Excel File</button><br><br>
    <div  style="overflow-x: auto; margin: 20px;">
    <table border="1" id="tblexportData" class="table table-hover">
        <thead>
            <tr>
                <th>Sl.No.</th>
                <th>Patient Id</th>
                <th>Patient Name</th>
                <th>Bill Type</th>
                <th>Mode Of Payment</th>
                <th>OptionLab</th>
                <th>Bill No</th>
                <th>Bill Date/Time</th>
                <th>Payable amount</th>
            </tr>
        </thead>
        <tbody>
            <?php
            $i = 1;
            $total = 0;

            if (mysqli_num_rows($query1) != 0) {
                while ($row = $query1->fetch_assoc()) {
                    $total += $row['Amount'];
                    ?>
                    <tr>
                        <td><?php echo $i; ?></td>
                        <td><?php echo $row['PatId']; ?></td>
                        <td><?php echo $row['PatientName']; ?></td>
                        <td>Advance</td>
                        <td><?php echo $row['PaymentMode']; ?></td>
                       <td><?php echo !empty($row['OptionLabs']) ? $row['OptionLabs'] : 'NA'; ?></td>

                        <td><a href="ViewTransBill1.php?Billno=<?php echo $row['BillNo']; ?>&PatId=<?php echo $row['PatId']; ?>&type=Advance" target="_blank"><?php echo $row['BillNo']; ?></a></td>
                        <td><?php echo $row['AdvanceDate']; ?></td>
                        <td><?php echo $row['Amount']; ?></td>
                    </tr>
                    <?php
                    $i++;
                }
            }

            if (mysqli_num_rows($query2) != 0) {
                while ($row = $query2->fetch_assoc()) {
                    $final = ($row['PaymentMode'] == "Insurance") ? $row['TotalAmount'] : $row['PayableAmount'];
                    $total += $final;
                    ?>
                    <tr>
                        <td><?php echo $i; ?></td>
                        <td><?php echo $row['PatientId']; ?></td>
                        <td><?php echo $row['PatientName']; ?></td>
                        <td>Discharge</td>
                        <td><?php echo $row['PaymentMode']; ?></td>
                        <td><?php echo !empty($row['OptionLabs']) ? $row['OptionLabs'] : 'NA'; ?></td>

                        <td><?php echo $row['FinalBillNo']; ?></td>
                        <td><?php echo $row['DischargeDate'] . ' ,' . $row['DischargeTime']; ?></td>
                        <td><?php echo $final; ?></td>
                    </tr>
                    <?php
                    $i++;
                }
            }

            if (mysqli_num_rows($query3) != 0) {
                while ($row = $query3->fetch_assoc()) {
                    $total += $row['PayableAmount'];
                    ?>
                    <tr>
                        <td><?php echo $i; ?></td>
                        <td><?php echo $row['PatientId']; ?></td>
                        <td><?php echo $row['PatientName']; ?></td>
                        <td>Investigation Charges</td>
                        <td><?php echo $row['PaymentMode']; ?></td>
                       <td><?php echo !empty($row['OptionLabs']) ? $row['OptionLabs'] : 'NA'; ?></td>

                        <td><?php echo $row['BillNo']; ?></td>
                        <td><?php echo $row['BDate']; ?></td>
                        <td><?php echo $row['PayableAmount']; ?></td>
                    </tr>
                    <?php
                    $i++;
                }
            }

            if (mysqli_num_rows($query4) != 0) {
                while ($row = $query4->fetch_assoc()) {
                    if ($row['PatientType'] == "OPD") {
                        $total += $row['PayableAmount'];
                        ?>
                        <tr>
                            <td><?php echo $i; ?></td>
                            <td><?php echo $row['PatientId']; ?></td>
                            <td><?php echo $row['PatientName']; ?></td>
                            <td><?php echo $row['PatientType']; ?></td>
                            <td><?php echo $row['PaymentMode']; ?></td>
                           <td><?php echo !empty($row['OptionLabs']) ? $row['OptionLabs'] : 'NA'; ?></td>

                            
                                <td><<?php echo $row['BillNo']; ?></td>
                            <td><?php echo $row['BDate']; ?></td>
                            <td><?php echo $row['PayableAmount']; ?></td>
                        </tr>
                        <?php
                        $i++;
                    }
                }
            }
            ?>
            <tr>
                <td colspan="8">Total Transaction</td>
                <td><?php echo $total; ?></td>
            </tr>
        </tbody>
    </table></div>
<?php else : ?>
    <h3>No records found.</h3>
<?php endif; ?>


    <br><br><!--
            <form method="post" action="export.php">
                <input type="submit" name="export" class="btn btn-danger" value="Export Individual Plan Data To Excel" />
            </form>
    -->
    </div>
    </div>


    

    </div>

    </div>
    </div>

    <div id="styleSelector">
    </div>
    </div>
    </div>
    </div>
    </div>
    </div>
    </div>


    
    <script type="a0935012553127c718007911-text/javascript" src="../files/bower_components/jquery/js/jquery.min.js"></script>
    <script type="a0935012553127c718007911-text/javascript" src="../files/bower_components/jquery-ui/js/jquery-ui.min.js"></script>
    <script type="a0935012553127c718007911-text/javascript" src="../files/bower_components/popper.js/js/popper.min.js"></script>
    <script type="a0935012553127c718007911-text/javascript" src="../files/bower_components/bootstrap/js/bootstrap.min.js"></script>

    <script src="../files/assets/pages/waves/js/waves.min.js" type="a0935012553127c718007911-text/javascript"></script>

    <script type="a0935012553127c718007911-text/javascript" src="../files/bower_components/jquery-slimscroll/js/jquery.slimscroll.js"></script>

    <script type="a0935012553127c718007911-text/javascript" src="../files/bower_components/modernizr/js/modernizr.js"></script>
    <script type="a0935012553127c718007911-text/javascript" src="../files/bower_components/modernizr/js/css-scrollbars.js"></script>

    <script src="../files/assets/pages/waves/js/waves.min.js" type="a0935012553127c718007911-text/javascript"></script>

    <script src="../files/bower_components/datatables.net/js/jquery.dataTables.min.js" type="a0935012553127c718007911-text/javascript"></script>
    <script src="../files/bower_components/datatables.net-buttons/js/dataTables.buttons.min.js" type="a0935012553127c718007911-text/javascript"></script>
    <script src="../files/assets/pages/data-table/js/jszip.min.js" type="a0935012553127c718007911-text/javascript"></script>
    <script src="../files/assets/pages/data-table/js/pdfmake.min.js" type="a0935012553127c718007911-text/javascript"></script>
    <script src="../files/assets/pages/data-table/js/vfs_fonts.js" type="a0935012553127c718007911-text/javascript"></script>
    <script src="../files/bower_components/datatables.net-buttons/js/buttons.print.min.js" type="a0935012553127c718007911-text/javascript"></script>
    <script src="../files/bower_components/datatables.net-buttons/js/buttons.html5.min.js" type="a0935012553127c718007911-text/javascript"></script>
    <script src="../files/bower_components/datatables.net-bs4/js/dataTables.bootstrap4.min.js" type="a0935012553127c718007911-text/javascript"></script>
    <script src="../files/bower_components/datatables.net-responsive/js/dataTables.responsive.min.js" type="a0935012553127c718007911-text/javascript"></script>
    <script src="../files/bower_components/datatables.net-responsive-bs4/js/responsive.bootstrap4.min.js" type="a0935012553127c718007911-text/javascript"></script>

    <script src="../files/assets/pages/data-table/js/data-table-custom.js" type="a0935012553127c718007911-text/javascript"></script>
    <script src="../files/assets/js/pcoded.min.js" type="a0935012553127c718007911-text/javascript"></script>
    <script src="../files/assets/js/vertical/vertical-layout.min.js" type="a0935012553127c718007911-text/javascript"></script>
    <script src="../files/assets/js/jquery.mCustomScrollbar.concat.min.js" type="a0935012553127c718007911-text/javascript"></script>
    <script type="a0935012553127c718007911-text/javascript" src="../files/assets/js/script.js"></script>

    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-23581568-13" type="a0935012553127c718007911-text/javascript"></script>
    <script type="a0935012553127c718007911-text/javascript">
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());

      gtag('config', 'UA-23581568-13');
    </script>
    <script src="rocket-loader.min.js" data-cf-settings="a0935012553127c718007911-|49" defer=""></script></body>

    </html>
