<?php
session_start();
require("php_con.php");
require("check_user.php");
require("function.php");

if(isset($_GET['remove'])){
	$remove=mysqli_real_escape_string($connation,$_GET['remove']);
	mysqli_query($connation,"DELETE FROM sale_list WHERE sale_billid='$remove'");
	mysqli_query($connation,"DELETE FROM stock_details WHERE stock_bill_id='$remove'");
	mysqli_query($connation,"DELETE FROM sale_details WHERE sale_details_billid='$remove'");	
	echo "<script>window.location='sales?removed';</script>";
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="favicon.ico">
<title><?php echo $system_name; ?></title>
<link rel="stylesheet" href="main_css.css">
	
<script src="data_table/js/jquery-3.5.1.js"></script>
<script src="data_table/js/jquery.dataTables.min.js"></script>
<script src="data_table/js/dataTables.bootstrap4.min.js"></script>
<link rel="stylesheet" href="data_table/css/bootstrap.css">
<link rel="stylesheet" href="data_table/css/dataTables.bootstrap4.min.css">
	
<link rel="stylesheet" href="icon/css/font-awesome.css">
<link rel="stylesheet" href="icon/css/font-awesome.min.css">
	
<link rel="stylesheet" href="alert/sweetalert2.min.css">
<script src="alert/sweetalert2.min.js"></script>
</head>

<body>
<script>
window.addEventListener("load",function(){
	var load_screen=document.getElementById('load_screen');
	document.body.removeChild(load_screen);
});
</script>

<div id="load_screen"><div id="loading"><img src="image/loading.gif" width="80px;"></div></div>
	
<?php require("header.php"); ?>
	
<div class="contaner">

	
<strong>Sale Details</strong>
	
<?php if(!isset($_GET['sale_details_customer'])){ ?>
<table class="mb-2">
<tbody>
<tr>
<form method="post">
<td>From Date<input name="from_date" type="date" class="form-control form-control-sm" id="from_date" value="<?php if(isset($_POST['from_date'])){echo $_POST['from_date'];}else{echo $current_date;} ?>" required max="<?php echo $current_date; ?>"></td>
<td>To Date<input name="to_date" type="date" class="form-control form-control-sm" id="to_date" value="<?php if(isset($_POST['to_date'])){echo $_POST['to_date'];}else{echo $current_date;} ?>" required max="<?php echo $current_date; ?>"></td>
<td><br><button type="submit" name="filter" class="btn btn-dark btn-sm ml-2"><i class="fa fa-filter"></i> Filter</button></td>
</form>
	
<form method="post" autocomplete="off">
<td class="pl-4">Invoice Number<input name="invoice_search" type="text" required="required" class="form-control form-control-sm" id="invoice_search"></td>
<td><br><button type="submit" name="filter_invoice" class="btn btn-dark btn-sm ml-2"><i class="fa fa-search"></i> Search</button></td>
</form>
</tr>
</tbody>
</table>
<?php } ?>
	
<div class="row">
<div class="table-responsive">
<table id="example" class="table list-table" style="width:100%; font-size: 14px; text-transform: capitalize;">
        <thead>
            <tr>
                <th>No.</th>
				<th>&nbsp;</th>
                <th>Bill ID</th>
                <th>Bill Amount</th>
                <th>Cash</th>
				<th>Card</th>
				<th>Discount</th>
				<th>User</th>
				<th>Customer</th>
				<th>Sale Time</th>
            </tr>
        </thead>
        <tbody>
			<?php
			$count_no=0;
			$total_amount=0;
			if(isset($_POST['filter'])){
			$qury=mysqli_query($connation,"SELECT sd.*,CONCAT(ud.user_fname,' ',ud.user_lname) AS uname
			FROM sale_details sd INNER JOIN user_details ud ON sd.sale_details_user=ud.user_id
			WHERE sd.sale_details_date BETWEEN '$_POST[from_date]' AND '$_POST[to_date]'
			ORDER BY sd.sale_details_id DESC");
			}
			elseif(isset($_GET['sale_details_customer'])){
			$sale_details_customer=(int)mysqli_real_escape_string($connation,$_GET['sale_details_customer']);
			$qury=mysqli_query($connation,"SELECT sd.*,CONCAT(ud.user_fname,' ',ud.user_lname) AS uname
			FROM sale_details sd INNER JOIN user_details ud ON sd.sale_details_user=ud.user_id
			WHERE sd.sale_details_customer='$sale_details_customer'
			ORDER BY sd.sale_details_id DESC");
			}
			elseif(isset($_POST['filter_invoice'])){
			$qury=mysqli_query($connation,"SELECT sd.*,CONCAT(ud.user_fname,' ',ud.user_lname) AS uname
			FROM sale_details sd INNER JOIN user_details ud ON sd.sale_details_user=ud.user_id
			WHERE sd.sale_details_billid LIKE '%$_POST[invoice_search]%'
			ORDER BY sd.sale_details_id DESC");
			}
			else{
			$qury=mysqli_query($connation,"SELECT sd.*,CONCAT(ud.user_fname,' ',ud.user_lname) AS uname
			FROM sale_details sd INNER JOIN user_details ud ON sd.sale_details_user=ud.user_id
			WHERE sd.sale_details_date='$current_date'
			ORDER BY sd.sale_details_id DESC");
			}
			while($resalt=mysqli_fetch_assoc($qury)){
			$count_no++;
			$total_amount+=bill_amount($resalt['sale_details_billid']);
				
			$cus_qury=mysqli_query($connation,"SELECT cs_name FROM customer_details WHERE cs_mobile='$resalt[sale_details_customer]'");
			if(mysqli_num_rows($cus_qury)>0){
			$cus_resalt=mysqli_fetch_assoc($cus_qury);
			$customer=$cus_resalt['cs_name'];
			}
			else{
			$customer="Unknown";
			}
			?>
            <tr>
                <td><?php echo number_format($count_no,0); ?></td>
				<td style="white-space: nowrap; text-align: center">
				<a href="poin-of-sale?bill_id=<?php echo $resalt['sale_details_billid']; ?>" class="badge badge-dark" title="View Bill"><i class="fa fa-edit"></i></a>
				<a href="view-sale?sale_billid=<?php echo $resalt['sale_details_billid']; ?>" target="_blank" class="badge badge-primary" title="View Bill"><i class="fa fa-eye"></i></a>
				<a href="bill_print.php?print=<?php echo $resalt['sale_details_billid']; ?>" target="re_print_bill" class="badge badge-info" title="Re-print Bill"><i class="fa fa-print"></i></a>
				<a href="sales?remove=<?php echo $resalt['sale_details_billid']; ?>" class="badge badge-danger" title="Remove Sale" onClick="return confirm('Are you sure to remove this sale?');"><i class="fa fa-trash"></i></a>
				</td>
                <td><?php echo $resalt['sale_details_billid']; ?></td>
                <td style="text-align: left;"><?php echo number_format((float)bill_amount($resalt['sale_details_billid']),2); ?></td>
                <td style="text-align: left;"><?php echo number_format((float)$resalt['sale_details_cash_payment'],2); ?></td>
				<td style="text-align: left;"><?php echo number_format((float)$resalt['sale_details_card_payment'],2); ?></td>
				<td style="text-align: left;"><?php echo number_format((float)$resalt['sale_details_discount'],2); ?></td>
				<td class="text-capitalize"><?php echo $resalt['uname']; ?></td>
				<td class="text-capitalize"><?php echo $customer; ?></td>
				<td><?php echo date("y-m-d h:i:s A",strtotime($resalt['sale_details_date']." ".$resalt['sale_details_time'])); ?></td>
            </tr>
            <?php
			}
			?>
        </tbody>
	<tfoot>
	        <tr style="font-weight: bold; background-color: #DDDDDD;">
                <td colspan="3" align="right">Total Amount</td>
				<td align="left"><?php echo number_format($total_amount,2); ?></td>
				<td colspan="6"></td>
            </tr>
	</tfoot>
    </table>
</div>
	
	
<script>
$(document).ready(function() {
    $('#example').DataTable();
} );	
</script>
		
</div>
</div>	
	
<iframe name="re_print_bill" style="display: none;"></iframe>
	
</body>
</html>
<?php @mysqli_close($connation); ?>