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

if(isset($_GET['remove'])){
	$supplier_bill_details_system_id=mysqli_real_escape_string($connation,$_GET['remove']);
	
	if(mysqli_query($connation,"DELETE FROM supplier_bill_details WHERE supplier_bill_details_system_id='$supplier_bill_details_system_id'")){
	
	$stock_qury=mysqli_query($connation,"SELECT * FROM supp_bill_list WHERE supp_bill_system_id='$supplier_bill_details_system_id'");
	while($stock_resalt=mysqli_fetch_array($stock_qury)){
		mysqli_query($connation,"INSERT INTO stock_details(stock_systemid, stock_qty, stock_type, stock_info, stock_addtime) VALUES ('$stock_resalt[supp_bill_product_id]','$stock_resalt[supp_bill_qty]','0','Bill remove and stock reverse','$current_date $current_time')");
	}
		
	}	
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<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>Supplier - Current Bill Details</strong>
	
<?php require("sub_menu/supplier.php"); ?>
	
<form method="post">
	
<hr>
	
<table class="mb-2">
<tbody>
<tr>
<td><label>Supplier</label></td>
<td><label>From Create Date</label></td>
<td><label>To Create Date</label></td>
<td></td>
</tr>
<tr>
<td>
<select name="supplier_bill_details_supplier" required class="form-control form-control-sm" id="supplier_bill_details_supplier">
<option value="All" <?php if(isset($_POST['supplier_bill_details_supplier'])){if($_POST['supplier_bill_details_supplier']=="All"){echo "selected";}} ?>>All Supplier</option>
<?php 
$supplier_qury=mysqli_query($connation,"SELECT * FROM supplier_details ORDER BY supplier_name");
while($supplier_resalt=mysqli_fetch_array($supplier_qury)){	
?>
<option value="<?php echo $supplier_resalt['supplier_id']; ?>" <?php if(isset($_POST['supplier_bill_details_supplier'])){if($_POST['supplier_bill_details_supplier']==$supplier_resalt['supplier_id']){echo "selected";}} ?>><?php echo $supplier_resalt['supplier_name']." | 0".(int)$supplier_resalt['supplier_mobile']." | ".$supplier_resalt['supplier_company']; ?></option>
<?php 
}
?>
</select>
</td>
<td><input name="supplier_bill_details_create_fromdate" type="date" required class="form-control form-control-sm" id="supplier_bill_details_create_fromdate" value="<?php if(isset($_POST['supplier_bill_details_create_fromdate'])){echo $_POST['supplier_bill_details_create_fromdate'];}else{echo $current_date;} ?>"></td>
<td><input name="supplier_bill_details_create_todate" type="date" required class="form-control form-control-sm" id="supplier_bill_details_create_todate" value="<?php if(isset($_POST['supplier_bill_details_create_todate'])){echo $_POST['supplier_bill_details_create_todate'];}else{echo $current_date;} ?>"></td>
<td><button type="submit" name="filter" class="btn btn-dark btn-sm ml-2"><i class="fa fa-filter"></i> Filter</button></td>
</tr>
</tbody>
</table>

</form>
<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 style="width: 60px;">No.</th>
				<th style="width: 50px;">&nbsp;</th>
				<th>Bill Number</th>
				<th>Supplier</th>
				<th>Create Date</th>
				<th>Due date</th>
				<th>Bill Amount</th>
				<th>create User</th>
				<th>Status</th>
            </tr>
        </thead>
        <tbody>
			<?php
			$count_no=0;
			$total_amount=0;
			
			$join_str="supplier_bill_details INNER JOIN supplier_details ON supplier_bill_details.supplier_bill_details_supplier=supplier_details.supplier_id
			INNER JOIN user_details ON supplier_bill_details.supplier_bill_details_create_user=user_details.user_id";
			
			if(isset($_POST['supplier_bill_details_supplier'])&&isset($_POST['supplier_bill_details_create_fromdate'])&&isset($_POST['supplier_bill_details_create_todate'])){
				
				if($_POST['supplier_bill_details_supplier']=="All"){
					$list_qury=mysqli_query($connation,"SELECT * FROM $join_str WHERE supplier_bill_details_create_date BETWEEN '$_POST[supplier_bill_details_create_fromdate]' and '$_POST[supplier_bill_details_create_todate]' ORDER BY supplier_bill_details_id DESC");
				}
				else{
					$list_qury=mysqli_query($connation,"SELECT * FROM $join_str WHERE supplier_bill_details_supplier='$_POST[supplier_bill_details_supplier]' and (supplier_bill_details_create_date BETWEEN '$_POST[supplier_bill_details_create_fromdate]' and '$_POST[supplier_bill_details_create_todate]') ORDER BY supplier_bill_details_id DESC");
				}
				
			}
			else{			
			$list_qury=mysqli_query($connation,"SELECT * FROM $join_str ORDER BY supplier_bill_details_id DESC");
			}
			
			while($list_resalt=mysqli_fetch_array($list_qury)){
				
			$amount_qury=mysqli_query($connation,"SELECT SUM(supp_bill_buy_price*supp_bill_qty) AS amount FROM supp_bill_list WHERE supp_bill_system_id='$list_resalt[supplier_bill_details_system_id]'");
			$amount_resalt=mysqli_fetch_array($amount_qury);
				
			$total_amount=$total_amount+$amount_resalt['amount'];
				
			$count_no++;
			?>
            <tr>
                <td><?php echo number_format($count_no,0); ?></td>
				<td style="width: 50px;" align="center">
				<a href="view-bill?supplier_bill_details_id=<?php echo $list_resalt['supplier_bill_details_id']; ?>" class="text-dark"><i class="fa fa-edit"></i></a>
				<a href="supplier?remove=<?php echo $list_resalt['supplier_bill_details_system_id']; ?>" class="text-danger" onClick="JavaScript:return confirm('Are your sure delete this bill?');"><i class="fa fa-trash"></i></a>
				</td>
				<td><?php echo $list_resalt['supplier_bill_details_billnumber']; ?></td>
				<td title="<?php echo $list_resalt['supplier_company']." | ".$list_resalt['supplier_name']." | ".$list_resalt['supplier_mobile']; ?>"><?php echo $list_resalt['supplier_company']; ?></td>
				<td><?php echo date_format(date_create($list_resalt['supplier_bill_details_create_date']),"M d, Y") ?></td>
				<td><?php echo date_format(date_create($list_resalt['supplier_bill_details_due_date']),"M d, Y") ?></td>
				<td align="right"><?php echo number_format($amount_resalt['amount'],2); ?></td>
				<td title="<?php echo $list_resalt['user_fname']." ".$list_resalt['user_lname']; ?>"><?php echo $list_resalt['user_fname']; ?></td>
				<td>
				<?php if($list_resalt['supplier_bill_details_close_status']==0){ ?>
					<span class="badge badge-warning" style="font-weight: normal;">Open Bill</span>
				<?php } ?>
					
				<?php if($list_resalt['supplier_bill_details_close_status']==1){ ?>
					<span class="badge badge-success" style="font-weight: normal;">Close Bill</span>
				<?php } ?>
				</td>
            </tr>
            <?php
			}
			?>
        </tbody>
	<tfoot>
	        <tr style="font-weight: bold; background-color: #DDDDDD;">
                <td colspan="6" align="right">Total Amount</td>
				<td align="right"><?php echo number_format($total_amount,2); ?></td>
				<td colspan="2"></td>
            </tr>
	</tfoot>
    </table>
</div>	
	
<script>
$(document).ready(function() {
    $('#example').DataTable();
} );	
</script>
			
</div>
</div>	
	

	
</body>
</html>
<?php @mysqli_close($connation); ?>