<?php
ob_start();
include("../db/cn.php");

 // Check if the user is logged in
if(isset($_SESSION['user_name']) && isset($_SESSION['user_role']) && isset($_SESSION['outlet_name'])
 && isset($_SESSION['outlet_address'])) {
    $userName = $_SESSION['user_name'];
    $userRole = $_SESSION['user_role'];
	$outlet_address = $_SESSION['outlet_address'];
	$outlet_name = $_SESSION['outlet_name'];

        // Display the user's name and role
if($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST['expence_add'])){
	$userName1 = $_SESSION['user_name'];
     $userRole1 = $_SESSION['user_role'];
	@$name_detail = $_POST['desc'];
	@$amount = $_POST["amount"];

	$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."','".$userName1."','".$userRole1."','".$outlet_name."','".$outlet_address."','".$date_time122."')");
	
	}
	
//-----------------------------------------------------------------------------------------
//opening Amount
$date_time = date("Y-m-d");
$record_opening = mysqli_query($connection,"select * from opening_amount where login_user = '$userName' && role = '$userRole' && date_time = '$date_time'");
 $row_amount = mysqli_fetch_array($record_opening);
 @$open_cash = $row_amount["open_cash"];
 //---------------------------------------------------------------
 // sales Sum
$date_time1 = date("Y-m-d");
$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_time1' && paid_by ='cash' GROUP BY order_id");

if ($rec10) {
    $total_sales = 0;
    while ($row3 = mysqli_fetch_array($rec10)) {
        @$total_sales += $row3['discount_amount'];
    }
    echo "";
    if (mysqli_num_rows($rec10) == 0) {
        echo "";
    }
} else {
    echo "Error executing query: " . mysqli_error($connection);
}



$date1= date("Y-m-d");
$rec11 = mysqli_query($connection, "SELECT r_order_id, discount_amount FROM log_user_sales WHERE 
 paid_by = 'cash' && status ='refund' AND date_time = '$date1' GROUP BY r_order_id");

if ($rec11) {
    $re_total_sales = 0;
    while ($rowre = mysqli_fetch_array($rec11)) {
        @$re_total_sales += $rowre['discount_amount'];
    }
    echo "";
    if (mysqli_num_rows($rec11) == 0) {
        echo "";
    }
} else {
    echo "Error executing query: " . mysqli_error($connection);
}
//--------------------------------------------------------------------------------



//--------------------------------------------------------------------------------
$date_time12 = date("Y-m-d");
$rec12 = mysqli_query($connection, "SELECT * FROM purchase WHERE login_user = '$userName' AND user_role = '$userRole' AND date_time = '$date_time12' && paid_by ='cash' AND status ='return' GROUP BY r_inv_id ");

if ($rec12) {
    $cash_purchase_return = 0;
    while ($row32 = mysqli_fetch_array($rec12)) {
        @$cash_purchase_return += $row32['discount_amount'];
    }
    echo "";
    if (mysqli_num_rows($rec12) == 0) {
        echo "";
    }
} else {
    echo "Error executing query: " . mysqli_error($connection);
}
//--------------------------------------------------------------------------------

$date_time1211 = date("Y-m-d");
$rec12m = mysqli_query($connection, "SELECT * FROM m_order WHERE login_user = '$userName' AND user_role = '$userRole' AND date_time = '$date_time1211' && paid_by ='cash' GROUP BY order_id ");

if ($rec12m) {
    $cash_purchase_m = 0;
    while ($row32m = mysqli_fetch_array($rec12m)) {
        @$cash_purchase_m += $row32m['total_amount'];
    }
    echo "";
    if (mysqli_num_rows($rec12m) == 0) {
        echo "";
    }
} else {
    echo "Error executing query: " . mysqli_error($connection);
}



//--------------------------------------------------------------------------------
// incash Sum cash expense
$date_time21 = date("Y-m-d");
$cash_out = mysqli_query($connection, "SELECT SUM(amount) AS total_amount FROM incash where date_time = '$date_time21' && login_user = '$userName' && user_role = '$userRole' && date_time = '$date_time21' && paid_by = 'cash' ");
if ($cash_out) {
    $cash_outdata = mysqli_fetch_array($cash_out);
    if ($cash_outdata && isset($cash_outdata['total_amount'])) {
        $totalAmount_cashexpense = $cash_outdata['total_amount'];
        echo "";
    } else {
        echo "";
    }
} else {
    echo "Error executing query: " . mysqli_error($connection);
}
//-----------------------------------------------------------------------------------------
//credit get cash recieved
		$date_time212 = date("Y-m-d");
$credit_get = mysqli_query($connection, "SELECT SUM(amount) AS total_amounts FROM credit_paid where date_time = '$date_time212' && user_name = '$userName' && user_role = '$userRole' && paid_by = 'cash'");
if ($credit_get) {
    $credit_data = mysqli_fetch_array($credit_get);
    if ($credit_data && isset($credit_data['total_amounts'])) {
        $credit_cashrecievable = $credit_data['total_amounts'];
        echo "";
    } else {
        echo "";
    }
} else {
    echo "Error executing query: " . mysqli_error($connection);
}
//-----------------------------------------------------------------------------------------

//cash paid // cash paid to supplier
		$date_time2121 = date("Y-m-d");
$cash_paid = mysqli_query($connection, "SELECT SUM(supplier_amount) AS total_amounts FROM supplier_paid  where date_time = '$date_time2121' && status = 'cash'");
if ($credit_get) {
    $cash_paid_data = mysqli_fetch_array($cash_paid);
    if ($cash_paid_data && isset($cash_paid_data['total_amounts'])) {
        $credit_done = $cash_paid_data['total_amounts'];
        echo "";
    } else {
        echo "";
    }
} else {
    echo "Error executing query: " . mysqli_error($connection);
}
//-----------------------------------------------------------------------------------------

//cash purchase
		
/*$credit_purchase = mysqli_query($connection, "SELECT SUM(total_amount) AS total_amounts FROM purchase where date_time = '$date_time2122' && user_name = '$userName' && user_role = '$userRole' && paid_by = 'cash'" );
if ($credit_purchase) {
    $credit_purchase_data = mysqli_fetch_array($credit_purchase);
    if ($credit_purchase_data && isset($credit_purchase_data['total_amounts'])) {
        $credit_cash_purchase1 = $credit_purchase_data['total_amounts'];
        echo "";
    } else {
        echo "";
    }
} else {
    echo "Error executing query: " . mysqli_error($connection);
}*/
$date_time2122 = date("Y-m-d");
$credit_purchase = mysqli_query($connection, "SELECT * FROM purchase WHERE  date_time = '$date_time2122' && paid_by ='cash' GROUP BY inv_id ");

if ($credit_purchase) {
    $credit_cash_purchase1 = 0;
    while ($row32m = mysqli_fetch_array($credit_purchase)) {
        @$credit_cash_purchase1 += $row32m['discount_amount'];
    }
    echo "";
    if (mysqli_num_rows($credit_purchase) == 0) {
        echo "";
    }
} else {
    echo "Error executing query: " . mysqli_error($connection);
}

//---------------------------------------------------------------------------------------- if ($rec11) {
    $total_sales_return = 0;
    while ($row31 = mysqli_fetch_array($rec11)) {
        $total_sales_return += $row31['sale_return_cash'];
    }
    
    echo  "";
    
    if (mysqli_num_rows($rec11) == 0) {
        echo "";
    }
} else {
    echo "Error executing query: " . mysqli_error($connection);
} 



$date = date("Y-m-d");
$with = mysqli_query($connection, "SELECT * FROM withdraw WHERE  date_time = '$date' AND paid_by = 'cash'  ");

if ($with) {
    $draw = 0;
    while ($rowdraw = mysqli_fetch_array($with)) {
        @$draw += $rowdraw['amount'];
    }
    echo "";
    if (mysqli_num_rows($with) == 0) {
        echo "";
    }
} else {
    echo "Error executing query: " . mysqli_error($connection);
}

//-------------------------------------
// POS cash credit received
$date_21 = date("Y-m-d");

// Prepare and execute the query
$query = "SELECT order_id, cash_amount AS totalamounts 
          FROM log_user_sales 
          WHERE paid_by = 'credit' 
            AND status = '' 
            AND r_order_id = '' 
            AND date_time = ? 
          GROUP BY order_id";
$stmt = $connection->prepare($query);
$stmt->bind_param("s", $date_21);
$stmt->execute();
$result = $stmt->get_result();

$pos_credit = 0;
if ($result) {
    while ($row = $result->fetch_assoc()) {
        if (isset($row['totalamounts'])) {
            $pos_credit += $row['totalamounts'];
        }
    }
    // Output the total or handle it as needed
    echo " ";
} else {
//    echo "Error executing query: " . $stmt->error;
}

$stmt->close();
//----------------------------------------
// incash Sum cash form_i
$date_time21a = date("Y-m-d");
$cash_from = mysqli_query($connection, "SELECT SUM(amount_i) AS total_amountq FROM transfer where  from_i = 'cash'  && date_time = '$date_time21a' ");
if ($cash_from) {
    $cash_fromdata = mysqli_fetch_array($cash_from);
    if ($cash_fromdata && isset($cash_fromdata['total_amountq'])) {
        $cash_from_i = $cash_fromdata['total_amountq'];
        echo "";
    } else {
        echo "";
    }
} else {
    echo "Error executing query: " . mysqli_error($connection);
}

// incash Sum cash to_i
$date_time21d = date("Y-m-d");
$cash_to = mysqli_query($connection, "SELECT SUM(amount_i) AS total_amounts FROM transfer where  to_i = 'cash' && date_time = '$date_time21d' ");
if ($cash_to) {
    $cash_todata = mysqli_fetch_array($cash_to);
    if ($cash_todata && isset($cash_todata['total_amounts'])) {
        $cash_to_i = $cash_todata['total_amounts'];
        echo "";
    } else {
        echo "";
    }
} else {
    echo "Error executing query: " . mysqli_error($connection);
}
//-----------------------------------------------------------------------------------------
// Ensure all variables are numeric and default to 0 if not set or empty
@$total_sales = intval($total_sales);
@$credit_cashrecievable = intval($credit_cashrecievable);
@$cash_purchase_return = intval($cash_purchase_return);
@$credit_cash_purchase1 = intval($credit_cash_purchase1);
@$open_cash = intval($open_cash);
@$credit_done = intval($credit_done);
@$cash_purchase_m = intval($cash_purchase_m);
@$totalAmount_cashexpense = intval($totalAmount_cashexpense);
@$draw = intval($draw);
@$total_sales_return = intval($total_sales_return);

// Perform calculations
@$amount_addtion = $total_sales + $credit_cashrecievable ;
@$amount_sub = $credit_cash_purchase1;

$new_final_end = $open_cash + $amount_addtion - $amount_sub - @$cash_from_i;
$new_final_end1 = $new_final_end - $credit_done + $cash_purchase_m + @$cash_to_i ;
$new_final_end2 = $new_final_end1 - $totalAmount_cashexpense  ;
$new_f = $new_final_end2 - $draw - $re_total_sales  ;
$NewF = $new_f - $total_sales_return + (int)$cash_purchase_return;



//-----------------------------------------------------------------------------------------
//opening_amount
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['open_submit'])){
	

	$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'])){
	

	$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-sm-6" >
<br />
<button type="button" class="btn btn-dark btn-sm font-weight-bold" data-toggle="modal" data-target="#addOpenModal" >Add Opening Amount</button>&nbsp;&nbsp;  
&nbsp;&nbsp;


   
<!--<a href="pos.php"><button type="button" class="btn btn-dark" ><i class="fa fa-arrow-left"></i></button></a>-->

<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>
<?php  
?>
<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, 'tab7')">Cash Sales Return</div>
    <!--<div class="tab-button" onclick="openTab(event, 'tab9')">Cash Sales Adj</div>-->
    <div class="tab-button" onclick="openTab(event, 'tab6')">Cash Purchases</div>
    <div class="tab-button" onclick="openTab(event, 'tab8')">Cash Purchases Return</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;padding:20px;">
            <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>
            <tr>
            <td style="font-weight:600;">Cash Sales Return</td>
            <td style="text-align:right; font-size:19px; font-weight:600;"><?php echo isset($re_total_sales) ? "Rs ".$re_total_sales."" : ""; ?>
            <input type="hidden" name="sale_cash_r" value="<?php echo $re_total_sales; ?>">
            </td>
            </tr>
             <tr>
            <!--<tr>
            <td style="font-weight:600;">Cash Sales Adj</td>
            <td style="text-align:right; font-size:19px; font-weight:600;"><?php echo isset($cash_purchase_m) ? "Rs ".$cash_purchase_m."" : ""; ?></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."" : ""; ?></td>
            <input type="hidden" name="cash_purchase_f" value="<?php echo $credit_cash_purchase1; ?>">
            </tr>
             <tr>
            <td style="font-weight:600;">Cash Purchase Return</td>
            <td style="text-align:right; font-size:19px; font-weight:600;"><?php echo isset($cash_purchase_return) ? "Rs ".$cash_purchase_return."" : ""; ?> 
            <input type="hidden" name="cash_purchase_r" value="<?php echo $cash_purchase_return ?>">
            </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;">POS Cash Recieved</td>-->
            <!--<td style="text-align:right; font-size:19px; font-weight:600;"><?php echo isset($pos_credit) ? "Rs ".$pos_credit."" : ""; ?>-->
            <!--<input type="hidden" name="cash_pos_f" value="<?php echo $pos_credit; ?>">-->
            <!--</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_from_f" value="<?php echo $cash_to_i ;?>">
            </td>
            
             
            <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>
            <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>
    <p></p>
</div>

    <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-stripped text-center">
                <tr style="background:#212529; color:#fff;">
                <td>Date</td>
                
                <th>Name</th>
                
                <td>Amount(Rs)</td>
                <td> Paid By</td>
                </tr>
               
               <?php
			   $date_time2111 = date("Y-m-d");
                        $rec2 = mysqli_query($connection,"select * from incash where date_time = '$date_time2111' && 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 style="text-transform: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-stripped text-center">
                <tr style="background:#212529; color:#fff;">
                <td>Date</td>
                
                <th>Name</th>
                
                <td>Amount(Rs)</td>
                <td> Paid By</td>
                </tr>
               
               <?php
			   $dateTime = date("Y-m-d");
                        $recw = mysqli_query($connection,"select * from withdraw where date_time = '$dateTime' AND paid_by = 'cash'");    
			     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 style="text-transform:capitalize;"><?php echo $roww["login_user"]."";?></td>
               </tr>
              <?php } ?>
               </table>
             </div>
           </div>
          </div>
          
          <div id="tab7" class="tab">
        <h4>Cash Sales Return</h4>
        <div class="container-fluid" id="table-container-wrapper">
           <div class="row">
            
      </div>
           
           <br />
             <div class="col-sm-12">
               <table class="table table-stripped text-center bg-white" >
                <tr style="background:#212529; color:#fff;" >
               <!-- <td>ID</td>-->
                <td>Date</td>
                <td>Order ID</td>
                <td>Sub Total(Rs)</td>
                
                <td>Discount(Rs)</td>
                <td>Amount(Rs)</td>
                </tr>
              <?php
$daTe = date("Y-m-d");
$rec = mysqli_query($connection, "SELECT *, SUM(net_amount) AS NE_AMOUNT
                                   FROM log_user_sales 
                                   WHERE status = 'refund' &&  paid_by = 'cash' AND date_time = '$daTe'
                                   GROUP BY  r_order_id");
// Check if any records were found
if (mysqli_num_rows($rec) > 0) {

    // Loop through results and display them
    while ($row = mysqli_fetch_array($rec)) { ?>
        <tr style="color:#000;">
        <td><?php echo $row["date_time"];?></td>
            <td style="font-weight:600;"><?php echo $row["order_id"]; ?></td>
            <td> <?php echo $row["NE_AMOUNT"]; ?>.00</td>
            
            <td> <?php echo $row["discounted"]; ?>
</td>            <td> <?php echo $row["discount_amount"]; ?></td>
        </tr>
    <?php }
} else {
    echo "";
}
?>
              </table>
             </div>
           </div>
          </div>
          
          <!--<div id="tab9" class="tab">
        <h4>Cash Sales Adjustment</h4>
        <div class="container-fluid" id="table-container-wrapper">
           <div class="row">
            
      </div>
           
           <br />
             <div class="col-sm-12">
               <table class="table table-stripped text-center" >
                <tr style="background:#212529; color:#fff;" >
               <!-- <td>ID</td>
                <td>Date</td>
                <td>Order ID</td>
                <td>Sub Total</td>
                
                <td>Discount</td>
                <td>Amount</td>
                </tr>
              <?php
$date_time = date("Y-m-d");
$rec = mysqli_query($connection, "SELECT *, SUM(total_amount) AS total_product
                                   FROM m_order
                                   WHERE date_time = '$date_time' 
                                   GROUP BY order_id");
// Check if any records were found
if (mysqli_num_rows($rec) > 0) {
    // Loop through results and display them
    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>Rs <?php echo $row["discount_amount"]; ?>.00</td>
            
            <td>Rs <?php echo $row["discounted"]; ?>
</td>            <td>Rs <?php echo $row["total_amount"]; ?></td>
        </tr>
    <?php }
} else {
    echo "No records found.";
}
?>
              </table>
             </div>
           </div>
          </div>-->
          
          <div id="tab8" class="tab">
        <h4>Cash Purchase Return</h4>
        <div class="container-fluid" id="table-container-wrapper">
           <div class="row">
            
      </div>
           <br />
           <table class="table table-stripped text-center">
                <tr style="background:#212529; color:#fff;">
                
                <td>Date</td>
                <th> Invoice ID </th>
                <td>Sub Total(Rs)</td>
                <th> Discount(Rs)</th>
                
               
                <td>Amount(Rs)</td>
      
                </tr>
               
               <?php
			   $date_time11 = date("Y-m-d");
                   $rec5 = mysqli_query($connection,
				   "select *, SUM(net_amount) AS Ne from purchase where  date_time = '$date_time11' && paid_by = 'cash' AND status = 'return' GROUP BY r_inv_id");        
			     while($row6 = mysqli_fetch_array($rec5)){
			   ?>
              <tr>
              
               <td><?php echo $row6["date_time"]; ?></td>
               <td><?php echo $row6['r_inv_id'];?></td>
               
              
               
               <td class="text-capitalize"><?php echo $row6["Ne"]; ?></td>
               <td><?php echo $row6["discounted"]; ?></td> 
               
               <td> <?php echo $row6["discount_amount"]; ?></td>
              
             
               </tr>
              <?php } ?>
               </table>
           <br />
             <div class="col-sm-12">
               
             </div>
           </div>
          </div>
          
    </div>
    <div id="tab3" class="tab">
        <h4> Cash Sales</h2>
        <div class="container-fluid" id="table-container-wrapper">
           <div class="row">
        
            </div>
           
           <br />
             <div class="col-sm-12">
               <table class="table table-stripped text-center" >
                <tr style="background:#212529; color:#fff;" >
               <!-- <td>ID</td>-->
                <td>Date</td>
                <td>Order ID</td>
                <td>Sub Total(Rs)</td>
                
                <td>Discount(Rs)</td>
                <td>Amount(Rs)</td>
                </tr>
              <?php
$date_time = date("Y-m-d");
$rec = mysqli_query($connection, "SELECT SUM(net_amount) AS NetAmouts,order_id,total_amount,discounted,discount_amount,date_time, SUM(net_amount) AS total_product
                                   FROM log_user_sales 
                                   WHERE  date_time = '$date_time' && paid_by = 'cash' &&  r_order_id = ''
                                   GROUP BY order_id");
// Check if any records were found
if (mysqli_num_rows($rec) > 0) {
    // Loop through results and display them
    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 }
} else {
    echo "";
}
?>
              </table>
             </div>
            
             
           </div>
          </div>
          
          <div id="tab4" class="tab">
        <h4 style="color:#212529;">Cash Recieved</h4>
        <div class="container-fluid" id="table-container-wrapper">
           <div class="row">
    
           </div>
           <br />
             <div class="col-sm-12">
               <table class="table table-stripped text-center">
                <tr style="background:#212529; color:#fff;">
            
                <td>Date</td>
                
                <th>Name</th>
              
                <td>Amount(Rs)</td>
                <td>Status</td>
                <td> By</td>
                </tr>
               
               <?php
			   $date_time11 = date("Y-m-d");
                   $rec5 = mysqli_query($connection,
				   "select * from credit_paid where date_time = '$date_time11' && 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-stripped text-center">
                <tr style="background:#212529; color:#fff;">
             
                <td>Date</td>
                
                <th>Name</th>
              
                <td>Amount(Rs)</td>
                <td>Status</td>
                <td>By</td>
                </tr>
               
               <?php
			   $date_time11 = date("Y-m-d");
                   $rec51 = mysqli_query($connection,
				   "select * from supplier_paid where date_time = '$date_time11' 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-stripped text-center">
                <tr style="background:#212529; color:#fff;">
                
                 <td>Date</td>
                <td>Invoice ID</td>
                <td>Sub Total(Rs)</td>
                
                <td>Discount(Rs)</td>
                <td>Amount(Rs)</td>
      
                </tr>
               
               <?php
			   $date_time11 = date("Y-m-d");
                   $rec5 = mysqli_query($connection,
				   "select *, SUM(net_amount) AS netAmounts from purchase where date_time = '$date_time11' && paid_by = 'cash' && 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> 
    
    
    
</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" style="background:#0A4657;">
                            <h5 class="modal-title text-capitalize font-weight-bold text-white" 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>
           
            <!-- 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>