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

if(isset($_GET['sp_supplier_id'])){
	if($_GET['sp_supplier_id']==""){
		echo "<script>window.location='supplier-payment';</script>";
	}
	
$supplier_id=mysqli_real_escape_string($connation,$_GET['sp_supplier_id']);
$sup_qury=mysqli_query($connation,"SELECT * FROM supplier_details WHERE supplier_id='$supplier_id'");
$sup_resalt=mysqli_fetch_assoc($sup_qury);
}

if(isset($_GET['remove'])){
	$sp_id=mysqli_real_escape_string($connation,$_GET['remove']);
	if(mysqli_query($connation,"DELETE FROM supplier_payment WHERE sp_id='$sp_id'")){
		echo "<script>window.location='supplier-payment?removed';</script>";
	}
}


$f_date=date("Y-m-d",strtotime($current_date." -10 day"));
$t_date=date("Y-m-t");

if(isset($_POST['pay_btn'])){
	$sp_supplier_id=mysqli_real_escape_string($connation,$_POST['sp_supplier_id']);
	$sp_payment_info=mysqli_real_escape_string($connation,$_POST['sp_payment_info']);
	$sp_amount=mysqli_real_escape_string($connation,$_POST['sp_amount']);
	if(mysqli_query($connation,"INSERT INTO
	supplier_payment (sp_id, sp_supplier_id, sp_pay_user, sp_payment_info, sp_amount, sp_add_date, sp_add_time)
	VALUES (NULL, '$sp_supplier_id', '$_COOKIE[user_id]', '$sp_payment_info', '$sp_amount', '$current_date', '$current_time')")){
		echo "<script>window.location='supplier-payment?sp_supplier_id=$sp_supplier_id';</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">
	
<link rel="stylesheet" href="select2/core.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="icon/css/font-awesome.css">
<link rel="stylesheet" href="icon/css/font-awesome.min.css">
</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 - Supplier Payment</strong>
	
<?php require("sub_menu/supplier.php"); ?>
	

	
<table class="table table-sm table-borderless alert-success text-dark" style="font-size: 14px;">
  <tbody>
    <tr>
      <td colspan="3" class="alert-dark">
<form method="post" autocomplete="off">	
<div class="form-control form-control-sm p-0 m-0">
<select name="sp_supplier_id" required="required" id="sp_supplier_id" class="custom-select2 form-control form-control-sm" style="border: none;" onChange="window.location='supplier-payment?sp_supplier_id='+this.value;">
<option value="" hidden="yes">Select 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 <?php if(isset($_GET['sp_supplier_id'])){ if($_GET['sp_supplier_id']==$supplier_resalt['supplier_id']){echo "selected";} } ?> value="<?php echo $supplier_resalt['supplier_id']; ?>"><?php echo $supplier_resalt['supplier_name']; ?> <?php echo "0".(int)$supplier_resalt['supplier_mobile']; ?></option>
<?php } ?>
</select>
</div>
</form>
		  
		</td>
      </tr>
    <tr>
      <td class="w-25"><b>Supplier Details</b>
		
<table class="table-sm text-capitalize">
  <tbody>
    <tr>
      <td>Name</td>
      <td><?php if(isset($_GET['sp_supplier_id'])){ echo $sup_resalt['supplier_name']; }else{ echo "-"; } ?></td>
    </tr>
    <tr>
      <td>Mobile</td>
      <td><?php if(isset($_GET['sp_supplier_id'])){ echo "0".(int)$sup_resalt['supplier_mobile']; }else{ echo "-"; } ?></td>
    </tr>
    <tr>
      <td>Company</td>
      <td><?php if(isset($_GET['sp_supplier_id'])){ echo $sup_resalt['supplier_company']; }else{ echo "-"; } ?></td>
    </tr>
    <tr>
      <td>Address</td>
      <td style="white-space: pre-line;"><?php if(isset($_GET['sp_supplier_id'])){ echo $sup_resalt['supplier_address']; }else{ echo "-"; } ?></td>
    </tr>
  </tbody>
</table>

		  
		</td>
      <td class="w-25"><b>Payment Summery</b>
        <table class="table-sm table-bordered">
          <tbody>
            <tr>
              <td>Total Invoicce</td>
              <td align="right" class="bg-info text-white"><?php if(isset($_GET['sp_supplier_id'])){ echo number_format((float)supplier_total_invoice_amount($sup_resalt['supplier_id']),2); }else{ echo "0.00"; } ?></td>
            </tr>
            <tr>
              <td>Total Payment</td>
              <td align="right" class="bg-success text-white"><?php if(isset($_GET['sp_supplier_id'])){ echo number_format((float)supplier_total_payment($sup_resalt['supplier_id']),2); }else{ echo "0.00"; } ?></td>
            </tr>
            <tr>
              <td>Due Balance</td>
              <td align="right" style="font-weight: bold;" class="bg-danger text-white"><?php if(isset($_GET['sp_supplier_id'])){ echo number_format((float)supplier_total_invoice_amount($sup_resalt['supplier_id'])-supplier_total_payment($sup_resalt['supplier_id']),2); }else{ echo "0.00"; } ?></td>
            </tr>
            </tbody>
        </table></td>
      <td class="w-50"><b>Last 5 Payment</b>
		
<table class="table table-sm table-bordered table-hover">
  <tbody>	  
<?php
if(isset($_GET['sp_supplier_id'])){
$lp_qury=mysqli_query($connation,"SELECT sp.*,CONCAT(ud.user_fname,' ',ud.user_lname) AS u_name
FROM supplier_payment sp INNER JOIN user_details ud ON sp.sp_pay_user=ud.user_id
WHERE sp.sp_supplier_id='$sup_resalt[supplier_id]'
ORDER BY sp.sp_id DESC
LIMIT 5");
while($lp_resalt=mysqli_fetch_assoc($lp_qury)){
?>
    <tr>
      <td><?php echo date("Y-m-d h:i:s A",strtotime($lp_resalt['sp_add_date']." ".$lp_resalt['sp_add_time'])); ?></td>
      <td><?php echo $lp_resalt['u_name']; ?></td>
      <td align="right"><?php echo number_format((float)$lp_resalt['sp_amount'],2); ?></td>
    </tr>
<?php } } ?>
  </tbody>
</table>

		  
		</td>
    </tr>
	  
<?php if(isset($_GET['sp_supplier_id'])){ ?>
    <tr>
      <td colspan="3" class="w-25 alert-primary text-dark" align="right">
<form method="post" autocomplete="off">
<input name="sp_supplier_id" type="hidden" id="sp_supplier_id" value="<?php echo $sup_resalt['supplier_id']; ?>">
	<table>
		<tbody>
			<tr>
			  <td>Pay Amount</td>
			  <td><input name="sp_amount" type="number" required="required" class="form-control form-control-sm" id="sp_amount" min="1" step="0.01"></td>
			  </tr>
			<tr>
			  <td>Remark</td>
			  <td><textarea name="sp_payment_info" rows="4" class="form-control form-control-sm" id="sp_payment_info"><?php echo $current_date; ?> payment</textarea></td>
				</tr>
			<tr>
			  <td colspan="2" align="right">
				  <a href="supplier-payment" class="btn btn-sm btn-secondary">Clear</a>
				  <button name="pay_btn" type="submit" class="btn btn-sm btn-success">Payment</button></td>
			  </tr>
		</tbody>
	</table>
</form>
		  
		</td>
      </tr>
<?php } ?>
	  
  </tbody>
</table>
	
<hr>
	
<form method="post" autocomplete="off" class="mb-2">
<table>
  <tbody>
    <tr>
      <td>From<input name="f_date" type="date" required="required" class="form-control form-control-sm" id="f_date" value="<?php if(isset($_POST['f_date'])){ echo $_POST['f_date']; }else{ echo $f_date; } ?>"></td>
      <td>To<input name="t_date" type="date" required="required" class="form-control form-control-sm" id="t_date" value="<?php if(isset($_POST['t_date'])){ echo $_POST['t_date']; }else{ echo $t_date; } ?>"></td>
      <td class="pl-2"><br><button type="submit" class="btn btn-sm btn-dark">Filter</button></td>
    </tr>
  </tbody>
</table>
</form>
	
<table class="table table-sm" style="font-size: 14px;">
<tbody>
<tr class="table-active">
<th align="center"><i class="fa fa-trash"></i></th>
<td>Supplier</td>
<td>Payment Info</td>
<td>Payed By</td>
<td>Pay Time</td>
<td>Amount</td>
</tr>
<?php
if(isset($_POST['f_date'])&&isset($_POST['t_date'])){
$f_date=$_POST['f_date'];
$t_date=$_POST['t_date'];
$payment_qury=mysqli_query($connation,"SELECT sp.*,CONCAT(ud.user_fname,' ',ud.user_lname) AS u_name,spd.supplier_name
FROM supplier_payment sp INNER JOIN user_details ud ON sp.sp_pay_user=ud.user_id
LEFT JOIN supplier_details spd ON sp.sp_supplier_id=spd.supplier_id
WHERE sp.sp_add_date BETWEEN '$f_date' AND '$t_date'
ORDER BY sp.sp_id DESC");
}
else{
$payment_qury=mysqli_query($connation,"SELECT sp.*,CONCAT(ud.user_fname,' ',ud.user_lname) AS u_name,spd.supplier_name
FROM supplier_payment sp INNER JOIN user_details ud ON sp.sp_pay_user=ud.user_id
LEFT JOIN supplier_details spd ON sp.sp_supplier_id=spd.supplier_id
WHERE sp.sp_add_date BETWEEN '$f_date' AND '$t_date'
ORDER BY sp.sp_id DESC");
}
while($payment_resalt=mysqli_fetch_array($payment_qury)){
?>
<tr>
<td style="white-space: nowrap; width: 50px;">
<a href="supplier-payment?remove=<?php echo $payment_resalt['sp_id']; ?>" class="badge badge-danger" onClick="JavaScript:return confirm('Are your sure remove this supplier?');"><i class="fa fa-trash"></i></a>
</td>
<td><?php echo $payment_resalt['supplier_name']; ?></td>
<td><?php echo $payment_resalt['sp_payment_info']; ?></td>
<td><?php echo $payment_resalt['u_name']; ?></td>
<td><?php echo date("Y-m-d h:i:s A",strtotime($payment_resalt['sp_add_date']." ".$payment_resalt['sp_add_time'])); ?></td>
<td align="right"><?php echo number_format((float)$payment_resalt['sp_amount'],2); ?></td>
</tr>
<?php
}
?>
</tbody>
</table>

		
</div>	
	
<script src="select2/core.js"></script>
<script src="select2/script.min.js"></script>
	
</body>
</html>
<?php @mysqli_close($connation); ?>