<!DOCTYPE html>
<?php 

session_start();
if (!isset($_SESSION['username'])) {
  header("location:login/main_login.php");
}

require_once("setting.php");
	
    require_once(connection);?>

<html lang="en">
<head>
  <title>Bill Generator</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="css/bootstrap.min.css">
  <script src="js/jquery.min.js"></script>
  <script src="js/bootstrap.min.js"></script>
</head>
<body>
<div class="container"><div class="row justify-content-md-center">
<?php include_once(menu_penal);
?>
</div>
<div class="row justify-content-md-center">
    <div class="col-5">
  <h2>Bills Generated App</h2></div></div>
  <form class="form-horizontal" action="" method="post">
    <div class="form-group">
		 	  	  <div class="row justify-content-md-center">
<!--
  <label class="col-sm-3 control-label" for="sel1">Select Month:</label>
      <div class="col-sm-3">

  <select class="form-control" id="sel1" name="bill_month">
<?php

//for ($m=1; $m<=12; $m++) {
 //    $month = date('F', mktime(0,0,0,$m, 1, date('Y')));
  //   echo $month. '<br>';
//echo '<option value="'.$m.'">'.$month.'</option>';

     //}  
   ?>    
  </select>
</div> -->
  <label class="col-sm-1 control-label"></label>
	  <div class="col-sm-3">
   <input class="btn btn-primary" type="submit" name="submit" value="Submit" />
 </div></div></div></form>		 	  	  <div class="form-row justify-content-md-center">
<div class="col-sm-6 col-md-6" >
<table class="table table-sm"><tbody><tr><td class="border-0">Name#:</td><td class="border-0"></td><td class="border-0">Amount</td></tr>
<?php
$paid=0;
if(isset($_POST['submit'])){
//$bill_month =$_POST['bill_month'];
$bill_month = date('m');
$sql="SELECT * FROM customer";
if ($result=mysqli_query($con,$sql)){ 
while($row = $result->fetch_assoc()) { 
$cust_id = $row['c_id'];
$cust_name = $row['c_name'];
$due_date=$row['conn_date'];
$status=$row['status'];
$pkg_id=$row['c_pkg'];

$sq1="SELECT * FROM `pkg` WHERE `pkg_id` = $pkg_id";
if ($result1=mysqli_query($con,$sq1)){ 
while($row1 = $result1->fetch_assoc()) { 

$pkg_name=$row1['pkg_name'];
$pkg_detail=$row1['pkg_detail'];
$bill_amount=$row1['pkg_price'];
}}
$sqli="SELECT SUM(bill_amount - paid) as `arear1` FROM `c_bill` where c_id=$cust_id";
if ($resulti=mysqli_query($con,$sqli)){ while($rowi = $resulti->fetch_assoc()) { $arear = $rowi['arear1'];  }}
	if (empty($arear)) { $arear=0; }
	$sqli="SELECT * FROM `c_bill` where c_id=$cust_id ORDER BY c_id DESC LIMIT 1";
if ($resulti=mysqli_query($con,$sqli)){ while($rowi = $resulti->fetch_assoc()) { $due_date =$rowi['due_date'];  }}

$date = date_create($due_date);
date_add($date, date_interval_create_from_date_string('30 days'));
$due_date = date_format($date, 'Y-m-d');
$total_amount=$arear+$bill_amount;



$sql122 = "SELECT bill_month FROM c_bill WHERE bill_month = $bill_month AND c_id= $cust_id";
       $result122 = $con->query($sql122);
       if($result122->num_rows > 0) {
         // echo $cust_name." &nbsp;bill Already Generated for this month </br>";
      
echo '<tr><td>'.$cust_name.'</td><td class="border-0"></td><td>Already Generated</td></tr>';


	  } else { 
	   
	   
$cbill = "INSERT INTO c_bill(c_id,pkg_id,arear,bill_amount,total_amount,paid,due_date,bill_month) 
VALUES ('$cust_id','$pkg_id','$arear','$bill_amount','$total_amount','$paid','$due_date','$bill_month')";
if ($con->query($cbill) === TRUE) {
	        //echo '<script type="text/javascript">alert("New record:::' . $cust_name . ':::created successfully"); </script>';

			
echo '<tr><td>'.$cust_name.'</td><td class="border-0">'.$total_amount.'</td><td>Successfully</td></tr>';


			
			
			} else {
    echo "Error: " . $cbill . "<br>" . $con->error;
}	   
	   
	   
	   
	   
	   }

}}

$con->close();
}
?>
</tbody></table></div></div>
</body>
</html>
