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

if(isset($_GET['expenses_details_id'])){
	$expenses_details_id=mysqli_real_escape_string($connation,$_GET['expenses_details_id']);
	if(mysqli_query($connation,"DELETE FROM expenses_details WHERE expenses_details_id='$expenses_details_id'")){
	echo "<script>window.location='expenses';</script>";
	}
}

if(isset($_POST['add_cat'])){
	$expenses_details_category=mysqli_real_escape_string($connation,$_POST['expenses_details_category']);
	$expenses_details_amount=mysqli_real_escape_string($connation,$_POST['expenses_details_amount']);
	$expenses_details_date=mysqli_real_escape_string($connation,$_POST['expenses_details_date']);
	mysqli_query($connation,"INSERT INTO expenses_details(expenses_details_category, expenses_details_amount, expenses_details_user, expenses_details_date) VALUES ('$expenses_details_category','$expenses_details_amount','$_COOKIE[user_id]','$expenses_details_date')");
}
?>
<!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>Supplier - Bill Summery</strong>
	
<?php require("sub_menu/supplier.php"); ?>

<div class="alert alert-danger mt-3">
<strong class="mb-2">Today Due Bill (<?php echo $current_date; ?>)</strong><br><br>
<div class="row">
<div class="table-responsive">
<table id="example" class="table list-table bg-white" style="width:100%; font-size: 14px; text-transform: capitalize;">
        <thead>
            <tr>
                <th style="width: 60px;">No.</th>
				<th>Bill Number</th>
				<th>Supplier</th>
				<th>Create Date</th>
				<th>Due date</th>
				<th>Bill Amount</th>
				<th>create User</th>
            </tr>
        </thead>
        <tbody>
			<?php
			$count_no=0;
			$total_amount=0;
					
			$list_qury=mysqli_query($connation,"SELECT * FROM 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 WHERE supplier_bill_details_close_status='0' and supplier_bill_details_due_date='$current_date' 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><?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>
            </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>
            </tr>
	</tfoot>
    </table>
</div>
</div>
	
	
<script>
$(document).ready(function() {
    $('#example').DataTable();
} );	
</script>
</div>
	
	
<div class="mt-3 alert alert-warning">
<?php $seven_day=date("Y-m-d",time()+60*60*24*7); ?>
<strong class="mb-2">Bills to be paid in the next 7 days (<?php echo $current_date; ?> to <?php echo $seven_day ?>)</strong><br><br>
<div class="row">
<div class="table-responsive">
<table id="example1" class="table list-table bg-white" style="width:100%; font-size: 14px; text-transform: capitalize;">
        <thead>
            <tr>
                <th style="width: 60px;">No.</th>
				<th>Bill Number</th>
				<th>Supplier</th>
				<th>Create Date</th>
				<th>Due date</th>
				<th>Bill Amount</th>
				<th>create User</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";
			
					
			$list_qury=mysqli_query($connation,"SELECT * FROM $join_str WHERE supplier_bill_details_close_status='0' and (supplier_bill_details_due_date BETWEEN '$current_date' and '$seven_day') 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><?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>
            </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>
            </tr>
	</tfoot>
    </table>
</div>
</div>
	
<script>
$(document).ready(function() {
    $('#example1').DataTable();
} );	
</script>
</div>
	
<div class="mt-3 alert alert-info">
<?php
	$month_start=date("Y-m-01",time());
	$month_end=date("Y-m-31",time());
?>
<strong class="mb-2">Current Month due bill (<?php echo date("F",time()); ?>)</strong><br><br>
<div class="row">
<div class="table-responsive">
<table id="example2" class="table list-table bg-white" style="width:100%; font-size: 14px; text-transform: capitalize;">
        <thead>
            <tr>
                <th style="width: 60px;">No.</th>
				<th>Bill Number</th>
				<th>Supplier</th>
				<th>Create Date</th>
				<th>Due date</th>
				<th>Bill Amount</th>
				<th>create User</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";
			
					
			$list_qury=mysqli_query($connation,"SELECT * FROM $join_str WHERE supplier_bill_details_close_status='0' and (supplier_bill_details_due_date BETWEEN '$month_start' and '$month_end') 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><?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>
            </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>
            </tr>
	</tfoot>
    </table>
</div>
</div>	
	
<script>
$(document).ready(function() {
    $('#example2').DataTable();
} );	
</script>
</div>
		
</div>	
	

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