<?php
ob_start();
include("../db/cn.php");
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

if (isset($_SESSION['user_name'], $_SESSION['user_role'], $_SESSION['outlet_name'], $_SESSION['outlet_address'])) {
    $userName = $_SESSION['user_name'];
    $userRole = $_SESSION['user_role'];
    $outlet_name = $_SESSION['outlet_name'];
    $outlet_address = $_SESSION['outlet_address'];

    if ($_SERVER['REQUEST_METHOD'] === "POST" && isset($_POST['expence_add'])) {
        $name_detail = $_POST['desc'] ?? '';
        $amount = $_POST['amount'] ?? 0;
        $date_time122 = date("Y-m-d");

        mysqli_query($connection, "
            INSERT INTO incash(name_detail, amount, login_user, user_role, outlet_name, outlet_address, date_time)
            VALUES ('$name_detail', '$amount', '$userName', '$userRole', '$outlet_name', '$outlet_address', '$date_time122')
        ");
    }




    $date_time = date("Y-m-d");
    $row_amount = mysqli_fetch_array(mysqli_query($connection, "
        SELECT * FROM opening_amount where date_time = '$date_time'
    "));
    @$open_cash = (int)$row_amount['open_cash'];

    $total_sales = 0;
    $rec10 = mysqli_query($connection, "
        SELECT order_id, discount_amount FROM log_user_sales
        WHERE login_user = '$userName' AND user_role = '$userRole'
        AND date_time = '$date_time' AND paid_by = 'cash'
        GROUP BY order_id
    ");
    while ($row3 = mysqli_fetch_array($rec10)) {
        $total_sales += $row3['discount_amount'];
    }

    $re_total_sales = 0;
    $rec11 = mysqli_query($connection, "SELECT * FROM log_user_sales");
    while ($rowre = mysqli_fetch_array($rec11)) {
        $re_total_sales += $rowre['discount_amount'];
    }

    $cash_purchase_return = 0;
    $rec12 = mysqli_query($connection, "
        SELECT * FROM purchase
        WHERE login_user = '$userName' AND user_role = '$userRole'
        AND date_time = '$date_time' AND paid_by = 'cash' AND status = 'return'
        GROUP BY r_inv_id
    ");
    while ($row32 = mysqli_fetch_array($rec12)) {
        $cash_purchase_return += $row32['discount_amount'];
    }

    $cash_purchase_m = 0;
    $rec12m = mysqli_query($connection, "
        SELECT * FROM m_order
        WHERE login_user = '$userName' AND user_role = '$userRole'
        AND date_time = '$date_time' AND paid_by = 'cash'
        GROUP BY order_id
    ");
    while ($row32m = mysqli_fetch_array($rec12m)) {
        $cash_purchase_m += $row32m['total_amount'];
    }

    $totalAmount_cashexpense = 0;
    $cash_out = mysqli_query($connection, "
        SELECT SUM(amount) AS total_amount FROM incash
        WHERE date_time = '$date_time' AND login_user = '$userName'
        AND user_role = '$userRole' AND paid_by = 'cash'
    ");
    $cash_outdata = mysqli_fetch_array($cash_out);
    $totalAmount_cashexpense = $cash_outdata['total_amount'] ?? 0;

    $credit_cashrecievable = 0;
    $credit_get = mysqli_query($connection, "
        SELECT SUM(amount) AS total_amounts FROM credit_paid
        WHERE date_time = '$date_time' AND user_name = '$userName'
        AND user_role = '$userRole' AND paid_by = 'cash'
    ");
    $credit_data = mysqli_fetch_array($credit_get);
    $credit_cashrecievable = $credit_data['total_amounts'] ?? 0;

    $credit_done = 0;
    $cash_paid = mysqli_query($connection, "
        SELECT SUM(supplier_amount) AS total_amounts FROM supplier_paid
        WHERE date_time = '$date_time' AND status = 'cash'
    ");
    $cash_paid_data = mysqli_fetch_array($cash_paid);
    $credit_done = $cash_paid_data['total_amounts'] ?? 0;

    $credit_cash_purchase1 = 0;
    $credit_purchase = mysqli_query($connection, "
        SELECT * FROM purchase
        WHERE date_time = '$date_time' AND paid_by = 'cash'
        GROUP BY inv_id
    ");
    while ($row32m = mysqli_fetch_array($credit_purchase)) {
        $credit_cash_purchase1 += $row32m['discount_amount'];
    }

    $total_sales_return = 0;
    while ($row31 = mysqli_fetch_array($rec11)) {
        $total_sales_return += $row31['sale_return_cash'] ?? 0;
    }

    $draw = 0;
    $with = mysqli_query($connection, "SELECT * FROM withdraw WHERE date_time = '$date_time'");
    while ($rowdraw = mysqli_fetch_array($with)) {
        $draw += $rowdraw['amount'];
    }

    // Final calculations
    $amount_addtion = $total_sales + $credit_cashrecievable;
    $amount_sub = $credit_cash_purchase1;

    $new_final_end = (int)$open_cash + $amount_addtion - $amount_sub - ($cash_from_i ?? 0);
    $new_final_end1 = $new_final_end - $credit_done + $cash_purchase_m + ($cash_to_i ?? 0);
    $new_final_end2 = $new_final_end1 - $totalAmount_cashexpense;
    $new_f = $new_final_end2 - $draw ;
    $NewF = $new_f - $total_sales_return ;
    
    
}
//-----------------------------------------------------------------------------------------
//opening_amount
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['open_submit'])){
	
$date_time2122 = date('Y-m-d'); 
	$open_amount = $_POST["open_amount"];
	mysqli_query($connection,"insert into opening_amount(open_cash,login_user,outlet_name,outlet_address,date_time,role)
	values('".$open_amount."','".$userName."','".$outlet_name."','".$outlet_address."','".$date_time2122."','".$userRole."')");

// Redirect to logout.php
echo '<script type="text/javascript">window.location.href="cashinout.php";</script>';
    //header("Location: cashinout.php");
    exit();
}

//----------------------------------------------------------------------------------------

// //opening_amount
// if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['open_cash_amount'])){
	
// $date_time2122 = date('Y-m-d'); 
// 	$open_amount1 = $_POST["open_amount1"];
// 	mysqli_query($connection,"insert into cash_opening_amount(open_cash,login_user,outlet_name,outlet_address,date_time,role)
// 	values('".$open_amount1."','".$userName."','".$outlet_name."','".$outlet_address."','".$date_time2122."','".$userRole."')");

// // Redirect to logout.php
// echo '<script type="text/javascript">window.location.href="cashinout.php";</script>';
//     //header("Location: cashinout.php");
//     exit();
// }



//closing Drawer
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['closedrawer'])){
    
    $open_cash_f = $_POST["open_cash_f"];
    $sale_cash_f = $_POST["sale_cash_f"];
    $sale_cash_r = $_POST["sale_cash_r"];
    $cash_purchase_f = $_POST["cash_purchase_f"];
    $cash_purchase_r = $_POST["cash_purchase_r"];
    $cash_recieved_f = $_POST["cash_recieved_f"];
    $cash_paid_f = $_POST["cash_paid_f"];
    $cash_expence_f = $_POST["cash_expence_f"];
    $cash_full_f = $_POST["cash_full_f"];
	 $withdraw = $_POST["withdraw"];
    $drawer_amount = $_POST["drawer_amount"];
    $remarks = $_POST["remarks"];
    $user = $userName;
    $role = $userRole;
    $outlet = $outlet_name;
    $address = $outlet_address;
    $date_time = date('Y-m-d');

    $query = "INSERT INTO closing_amount (
        open_cash,
        cash_sales,
        cash_sales_return,
        cash_purchase,
        cash_purchase_return,
        cash_recieved,
        cash_paid,
        cash_expence,
        cash_close,
		cash_draw,
        drawer_amount,
        login_user,
        outlet_name,
        outlet_address,
        role,
        date_time,
        remarks
    ) VALUES (
        '$open_cash_f',
        '$sale_cash_f',
        '$sale_cash_r',
        '$cash_purchase_f',
        '$cash_purchase_r',
        '$cash_recieved_f',
        '$cash_paid_f',
        '$cash_expence_f',
        '$cash_full_f',
		'$withdraw',
        '$drawer_amount',
        '$user',
        '$outlet',
        '$address',
        '$role',
        '$date_time',
        '$remarks'
    )";

    mysqli_query($connection, $query);

    // Redirect to logout.php
    echo '<script type="text/javascript">window.location.href="logout.php";</script>';
    //header("Location: logout.php");
    exit(); // Ensure script execution stops after redirection

}
//-----------------------------------------------------------------------------------------
ob_end_flush();
?>
<!DOCTYPE html>
<html lang="en">

<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Cash ~ IN & OUT ~ </title>

    <!-- Custom fonts for this template-->
    <link href="../vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
   <link href="https://fonts.googleapis.com/css2?family=Quicksand:wght@200;300;400;500;600;700&display=swap" rel="stylesheet">

    <!-- Custom styles for this template-->
    <link href="../css/sb-admin-2.min.css" rel="stylesheet">
    <link href="../css/sb-admin-2.css" rel="stylesheet">
    
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<style>
body {
    font-family: 'Quicksand', sans-serif;
    
}
		    .chart-container {
      position: relative;
      background: rgba(255, 255, 255, 0.9);
      border-radius: 10px;
      padding: 15px;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
    }

    canvas {
      max-width: 100%;
      height: auto;
    }
.bg-gradient-primary{
    
	background:#0A4657;
	color:#fff;	
	
	}

.btn-primary{
	background:#0A4657;
	color:#fff;
	 border:1px #0A4657 solid;
	
	}  
.btn-primary:hover{
  
  background:#fff;
  border:1px #0A4657 solid;
	color:#0A4657;
	
	}	 
 /* Basic styling */
    .tab {
        display: none;
        padding: 20px;
    }
    .active {
        display: block;
    }
    .tab-buttons {
        display: flex;
    }
    .tab-button {
        cursor: pointer;
        padding: 10px 10px;
        background-color: #ffff;
        border: 1px solid #0A5064;
        border-radius: 5px 5px 0 0;
        margin-right: 5px;
		color:#212529;
		font-size:13px;
    }
    .tab-button.active {
        background-color: #212529;
		color:#fff;
    }

    /* Background colors for tab content */
    #tab1 {
        background-color: #fff;
		color:#212529;
		padding:20px;
    }
    #tab2 {
        color:#212529;
		background-color: #;
    }
    #tab3 {
		color:#212529;
        background-color: #;
    }
	.fa-download:hover{
		color:#953E39;
		}
		
h4{ 
    font-weight:700;
	  	color:#060606;
		}
		tr{
		    font-weight:700;
		}
		.table{
		    background:#fff;
		    color:#000;
		}
  </style>

<body id="page-top">

    <!-- Page Wrapper -->
    <div id="wrapper">

        <!-- Sidebar -->
        <?php
        include("common/sd.php");
		?>
        <!-- End of Sidebar -->

        <!-- Content Wrapper -->
        <div id="content-wrapper" class="d-flex flex-column">

            <!-- Main Content -->
            <div id="content">

                <!-- Topbar -->
                <nav class="navbar navbar-expand navbar-light bg-white topbar mb-4 static-top shadow">

                    <!-- Sidebar Toggle (Topbar) -->
                    <button id="sidebarToggleTop" class="btn btn-link d-md-none rounded-circle mr-3">
                        <i class="fa fa-bars"></i>
                    </button>

                    <!-- Topbar Search -->
                    
  <?php include('common/log.php');  ?>

                    <!-- Topbar Navbar -->
                    
                </nav>
                <!-- End of Topbar -->

                <!-- Begin Page Content -->

<div class="">
<div class="container-fluid">
  <div class="row">
    <div class="col-sm-6 text-capitalize">
      <h3 style="color:#212529; font-weight:700;">Cash Drawer</h3>
    </div>
    <div class="col-5">
          <button type="button" style="float:right;font-weight:700;margin-top:15px;" class="btn btn-dark btn-lg" data-toggle="modal" data-target="#addOpenModal">Add Opening Amount</button>
    </div>
    <div class="col-sm-1">
      <br />
    
      <span style="float:right; margin-top:-7px; padding:11px; background:#0A4657; box-shadow:0px 0px 1px 1px #0A4657; border-radius:5px; color:#fff; text-transform:capitalize; font-size:14px; font-weight:700;">
        <?php echo $userName; ?>
      </span>
    </div>
  </div>
</div>
<br />

<div class="container-fluid">
  <div class="tab-buttons" style="color:#060606; font-weight:600;">
    <div class="tab-button active" onclick="openTab(event, 'tab1')">Overview</div>
    <div class="tab-button" onclick="openTab(event, 'tab3')">Cash Sales</div>
    <div class="tab-button" onclick="openTab(event, 'tab6')">Cash Purchases</div>
    <div class="tab-button" onclick="openTab(event, 'tab4')">Cash Recieved</div>
    <div class="tab-button" onclick="openTab(event, 'tab5')">Cash Paid</div>
    <div class="tab-button" onclick="openTab(event, 'tab2')">Cash Expense</div>
    <div class="tab-button" onclick="openTab(event, 'tab10')">Cash Withdraw</div>
  </div>

  <div class="tab-container">
    <div id="tab1" class="tab active">
      <h4>Overview</h4>
      <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        <div class="container-fluid">
          <div class="row" style="padding:20px; background:#fff; box-shadow:0px 0px 5px 2px #CCC; border-radius:10px;">
            <div class="col-sm-12" style="padding:20px;">
              <table class="table table-hover">
                <tr style="color:#8A1218;">
                  <td style="font-weight:600;">Opening Cash</td>
                  <td style="text-align:right; font-size:19px; font-weight:600;">
                    <?php echo isset($open_cash) ? "Rs $open_cash" : ""; ?>
                    <input type="hidden" name="open_cash_f" value="<?php echo $open_cash; ?>">
                  </td>
                </tr>
                <tr>
                  <td style="font-weight:600;">Cash Sales</td>
                  <td style="text-align:right; font-size:19px; font-weight:600;">
                    <?php echo isset($total_sales) ? "Rs $total_sales" : ""; ?>
                    <input type="hidden" name="sale_cash_f" value="<?php echo $total_sales; ?>">
                  </td>
                </tr>
                <tr>
                  <td style="font-weight:600;">Cash Purchase</td>
                  <td style="text-align:right; font-size:19px; font-weight:600;">
                    <?php echo isset($credit_cash_purchase1) ? "Rs $credit_cash_purchase1" : ""; ?>
                    <input type="hidden" name="cash_purchase_f" value="<?php echo $credit_cash_purchase1; ?>">
                  </td>
                </tr>
                <tr>
                  <td style="font-weight:600;">Cash Recieved</td>
                  <td style="text-align:right; font-size:19px; font-weight:600;">
                    <?php echo isset($credit_cashrecievable) ? "Rs $credit_cashrecievable" : ""; ?>
                    <input type="hidden" name="cash_recieved_f" value="<?php echo $credit_cashrecievable; ?>">
                  </td>
                </tr>
                <tr>
                  <td style="font-weight:600;">Cash Paid</td>
                  <td style="text-align:right; font-size:19px; font-weight:600;">
                    <?php echo isset($credit_done) ? "Rs $credit_done" : ""; ?>
                    <input type="hidden" name="cash_paid_f" value="<?php echo $credit_done; ?>">
                  </td>
                </tr>
                <tr>
                  <td style="font-weight:600;">Cash Expense</td>
                  <td style="text-align:right; font-size:19px; font-weight:600;">
                    <?php echo isset($totalAmount_cashexpense) ? "Rs $totalAmount_cashexpense" : ""; ?>
                    <input type="hidden" name="cash_expence_f" value="<?php echo $totalAmount_cashexpense; ?>">
                  </td>
                </tr>
                <tr>
                  <td style="font-weight:600;">Cash Withdraw</td>
                  <td style="text-align:right; font-size:19px; font-weight:600;">
                    <?php echo isset($draw) ? "Rs $draw" : ""; ?>
                    <input type="hidden" name="withdraw" value="<?php echo $draw; ?>">
                  </td>
                </tr>
                <tr>
                  <td style="font-weight:600;">Transfer Out</td>
                  <td style="text-align:right; font-size:19px; font-weight:600;">
                    <?php echo isset($cash_from_i) ? "Rs $cash_from_i" : ""; ?>
                    <input type="hidden" name="cash_from_f" value="<?php echo $cash_from_i; ?>">
                  </td>
                </tr>
                <tr>
                  <td style="font-weight:600;">Transfer In</td>
                  <td style="text-align:right; font-size:19px; font-weight:600;">
                    <?php echo isset($cash_to_i) ? "Rs $cash_to_i" : ""; ?>
                    <input type="hidden" name="cash_to_f" value="<?php echo $cash_to_i; ?>">
                  </td>
                </tr>
                <tr>
                  <td style="font-weight:600;">Closing Cash</td>
                  <td style="text-align:right; font-size:19px; font-weight:600;">
                    <?php echo isset($NewF) ? "Rs $NewF" : ""; ?>
                    <input type="hidden" name="cash_full_f" value="<?php echo $NewF; ?>">
                  </td>
                </tr>
                <tr>
                  <td style="font-weight:600;">Cash Drawer Amount</td>
                  <td style="text-align:right; font-size:19px; font-weight:600;">
                    <input type="text" name="drawer_amount" placeholder="Rs" style="width:220px; height:35px; border:1px solid #999; box-shadow:0px 0px 4px 1px #CCC; border-radius:5px;">
                  </td>
                </tr>
              </table>
            </div>
          </div>

          <br />
          <div class="row">
            <div class="col-sm-12">
              <textarea placeholder="Remarks:" name="remarks" class="form-control" style="height:150px;"></textarea>
              <br />
              <button class="btn btn-dark" type="submit" name="closedrawer">
                <i class="fa fa-cash-register"></i>&nbsp;Close Cash Drawer
              </button>
            </div>
          </div>
        </div>
      </form>
    </div>
  </div>
</div>


    <?php
$date_today = date("Y-m-d");
?>

<div id="tab2" class="tab">
  <h4>Cash Expense</h4>
  <div class="container-fluid" id="table-container-wrapper">
    <div class="row"></div>
    <br />
    <div class="col-sm-12">
      <table class="table table-striped text-center">
        <tr style="background:#212529; color:#fff;">
          <th>Date</th>
          <th>Name</th>
          <th>Amount (Rs)</th>
          <th>Paid By</th>
        </tr>
        <?php
        $rec2 = mysqli_query($connection, "SELECT * FROM incash WHERE date_time = '$date_today' AND paid_by = 'cash'");
        while ($row2 = mysqli_fetch_array($rec2)) {
        ?>
        <tr>
          <td><?php echo $row2['date_time']; ?></td>
          <td class="text-capitalize" style="font-weight:600;"><?php echo $row2['name_detail']; ?></td>
          <td><?php echo $row2['amount']; ?></td>
          <td class="text-capitalize"><?php echo $row2['login_user']; ?></td>
        </tr>
        <?php } ?>
      </table>
    </div>
  </div>
</div>

<div id="tab10" class="tab">
  <h4>Cash Withdraw</h4>
  <div class="container-fluid" id="table-container-wrapper">
    <div class="row"></div>
    <br />
    <div class="col-sm-12">
      <table class="table table-striped text-center">
        <tr style="background:#212529; color:#fff;">
          <th>Date</th>
          <th>Name</th>
          <th>Amount (Rs)</th>
          <th>Paid By</th>
        </tr>
        <?php
        $recw = mysqli_query($connection, "SELECT * FROM withdraw WHERE date_time = '$date_today' ");
        while ($roww = mysqli_fetch_array($recw)) {
        ?>
        <tr>
          <td><?php echo $roww['date_time']; ?></td>
          <td class="text-capitalize" style="font-weight:600;"><?php echo $roww['name_detail']; ?></td>
          <td><?php echo $roww['amount']; ?></td>
          <td class="text-capitalize"><?php echo $roww['login_user']; ?></td>
        </tr>
        <?php } ?>
      </table>
    </div>
  </div>
</div>

<div id="tab3" class="tab">
  <h4>Cash Sales</h4>
  <div class="container-fluid" id="table-container-wrapper">
    <div class="row"></div>
    <br />
    <div class="col-sm-12">
      <table class="table table-striped text-center">
        <tr style="background:#212529; color:#fff;">
          <th>Date</th>
          <th>Order ID</th>
          <th>Sub Total (Rs)</th>
          <th>Discount (Rs)</th>
          <th>Amount (Rs)</th>
        </tr>
        <?php
        $rec = mysqli_query($connection, "
          SELECT SUM(net_amount) AS NetAmouts, order_id, total_amount, discounted, discount_amount, date_time
          FROM log_user_sales
          WHERE date_time = '$date_today' AND paid_by = 'cash'
          GROUP BY order_id
        ");
        while ($row = mysqli_fetch_array($rec)) {
        ?>
        <tr>
          <td><?php echo $row['date_time']; ?></td>
          <td style="font-weight:600;"><?php echo $row['order_id']; ?></td>
          <td><?php echo $row['NetAmouts']; ?></td>
          <td><?php echo $row['discounted']; ?></td>
          <td><?php echo $row['discount_amount']; ?></td>
        </tr>
        <?php } ?>
      </table>
    </div>
  </div>
</div>

<div id="tab4" class="tab">
  <h4 style="color:#212529;">Cash Received</h4>
  <div class="container-fluid" id="table-container-wrapper">
    <div class="row"></div>
    <br />
    <div class="col-sm-12">
      <table class="table table-striped text-center">
        <tr style="background:#212529; color:#fff;">
          <th>Date</th>
          <th>Name</th>
          <th>Amount (Rs)</th>
          <th>Status</th>
          <th>By</th>
        </tr>
        <?php
        $rec5 = mysqli_query($connection, "SELECT * FROM credit_paid WHERE date_time = '$date_today' AND paid_by = 'cash'");
        while ($row6 = mysqli_fetch_array($rec5)) {
        ?>
        <tr>
          <td><?php echo $row6['date_time']; ?></td>
          <td class="text-capitalize" style="font-weight:600;"><?php echo $row6['cr_name']; ?></td>
          <td><?php echo $row6['amount']; ?></td>
          <td class="text-capitalize"><?php echo $row6['paid_by']; ?></td>
          <td class="text-capitalize"><?php echo $row6['user_name']; ?></td>
        </tr>
        <?php } ?>
      </table>
    </div>
  </div>
</div>

<div id="tab5" class="tab">
  <h4 style="color:#212529;">Cash Paid</h4>
  <div class="container-fluid" id="table-container-wrapper">
    <div class="row"></div>
    <br />
    <div class="col-sm-12">
      <table class="table table-striped text-center">
        <tr style="background:#212529; color:#fff;">
          <th>Date</th>
          <th>Name</th>
          <th>Amount (Rs)</th>
          <th>Status</th>
          <th>By</th>
        </tr>
        <?php
        $rec51 = mysqli_query($connection, "SELECT * FROM supplier_paid WHERE date_time = '$date_today' AND status = 'cash'");
        while ($row69 = mysqli_fetch_array($rec51)) {
        ?>
        <tr>
          <td><?php echo $row69['date_time']; ?></td>
          <td class="text-capitalize" style="font-weight:600;"><?php echo $row69['supplier_name']; ?></td>
          <td><?php echo $row69['supplier_amount']; ?></td>
          <td class="text-capitalize"><?php echo $row69['status']; ?></td>
          <td class="text-capitalize"><?php echo $row69['user_name']; ?></td>
        </tr>
        <?php } ?>
      </table>
    </div>
  </div>
</div>

<div id="tab6" class="tab">
  <h4 style="color:#212529;">Cash Purchases</h4>
  <div class="container-fluid" id="table-container-wrapper">
    <div class="row"></div>
    <br />
    <div class="col-sm-12">
      <table class="table table-striped text-center">
        <tr style="background:#212529; color:#fff;">
          <th>Date</th>
          <th>Invoice ID</th>
          <th>Sub Total (Rs)</th>
          <th>Discount (Rs)</th>
          <th>Amount (Rs)</th>
        </tr>
        <?php
        $rec5 = mysqli_query($connection, "
          SELECT *, SUM(net_amount) AS netAmounts FROM purchase
          WHERE date_time = '$date_today' AND paid_by = 'cash' AND r_inv_id = ''
          GROUP BY inv_id
        ");
        while ($row6 = mysqli_fetch_array($rec5)) {
        ?>
        <tr>
          <td><?php echo $row6['date_time']; ?></td>
          <td style="font-weight:600;"><?php echo $row6['inv_id']; ?></td>
          <td><?php echo $row6['netAmounts']; ?></td>
          <td><?php echo $row6['discounted']; ?></td>
          <td><?php echo $row6['discount_amount']; ?></td>
        </tr>
        <?php } ?>
      </table>
    </div>
  </div>
</div>

    
   </div> 
    
    
    
</div>
 <!-- Modal HTML -->
        <!-- Modal for adding purchases -->
            <div class="modal fade" id="addPurchaseModal" tabindex="-1" role="dialog" 
            aria-labelledby="addPurchaseModalLabel" aria-hidden="true" style="margin-top:80px;">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title text-capitalize" id="addPurchaseModalLabel">Add Cash Expense</h5>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>
                        <div class="modal-body">
                            <!-- Form for adding purchase -->
                            <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
                            
                                <input type="text" name="desc" placeholder="Description / Name" class="form-control" />
                                <br />
                                <input type="text" name="amount" placeholder="Amount" class="form-control" />
                                <br />
                                <button type="submit"  class="btn btn-dark" name="expence_add">Enter</button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
            
            
            <div class="modal fade" id="addOpenModal" tabindex="-1" role="dialog" 
            aria-labelledby="addOpenModalLabel" aria-hidden="true" style="margin-top:80px;">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title text-capitalize" id="addOpenModalLabel">Add Opening Amount</h5>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>
                        <div class="modal-body">
                            <!-- Form for adding purchase -->
                            <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
                                
                                <input type="text" name="open_amount" placeholder="Amount" class="form-control" />
                                <br />
                                <button type="submit" name="open_submit"  class="btn btn-dark">Enter</button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>

</div>
</div>
</div>
           
            <!-- End of Main Content -->

            <!-- Footer -->
            <?php include("common/main_ft.php");  ?>
            <!-- End of Footer -->

        </div>
        <!-- End of Content Wrapper -->

    </div>
    <!-- End of Page Wrapper -->

    <!-- Scroll to Top Button-->
    <a class="scroll-to-top rounded" href="#page-top">
        <i class="fas fa-angle-up"></i>
    </a>

    <!-- Logout Modal-->
    <div class="modal fade" id="logoutModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
        aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">Ready to Leave?</h5>
                    <button class="close" type="button" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                </div>
                <div class="modal-body">Select "Logout" below if you are ready to end your current session.</div>
                <div class="modal-footer">
                    <button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
                    <a class="btn btn-primary" href="">Logout</a>
                </div>
            </div>
        </div>
    </div>
<script>
        // Function to switch between tabs
    function openTab(evt, tabName) {
        // Get all elements with class="tab" and hide them
        var tabs = document.getElementsByClassName("tab");
        for (var i = 0; i < tabs.length; i++) {
            tabs[i].style.display = "none";
        }

        // Get all elements with class="tab-button" and remove the class "active"
        var tabButtons = document.getElementsByClassName("tab-button");
        for (var i = 0; i < tabButtons.length; i++) {
            tabButtons[i].classList.remove("active");
        }

        // Show the current tab, and add an "active" class to the button that opened the tab
        document.getElementById(tabName).style.display = "block";
        evt.currentTarget.classList.add("active");
    }
</script>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script> 
   <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.10.2/dist/umd/popper.min.js"></script>
    <!-- Bootstrap core JavaScript-->
    <script src="../vendor/jquery/jquery.min.js"></script>
    <script src="../vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

    <!-- Core plugin JavaScript-->
    <script src="../vendor/jquery-easing/jquery.easing.min.js"></script>

    <!-- Custom scripts for all pages-->
    <script src="../js/sb-admin-2.min.js"></script>

</body>

</html>