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

if(isset($_GET['remove'])){
	$supplier_id=mysqli_real_escape_string($connation,$_GET['remove']);
	if(mysqli_query($connation,"DELETE FROM supplier_details WHERE supplier_id='$supplier_id' AND supplier_id!='1'")){
	echo "<script>window.location='supplier-manage';</script>";
	}
}

if(isset($_POST['abb_bt'])){
	$supplier_code=mysqli_real_escape_string($connation,$_POST['supplier_code']);
	$supplier_company=mysqli_real_escape_string($connation,$_POST['supplier_company']);
	$supplier_name=mysqli_real_escape_string($connation,$_POST['supplier_name']);
	$supplier_contact_name=mysqli_real_escape_string($connation,$_POST['supplier_contact_name']);
	$supplier_mobile=(int)mysqli_real_escape_string($connation,$_POST['supplier_mobile']);
	$supplier_address1=mysqli_real_escape_string($connation,$_POST['supplier_address1']);
	$supplier_address2=mysqli_real_escape_string($connation,$_POST['supplier_address2']);
	$supplier_address3=mysqli_real_escape_string($connation,$_POST['supplier_address3']);
	$supplier_phone1=(int)mysqli_real_escape_string($connation,$_POST['supplier_phone1']);
	$supplier_phone2=(int)mysqli_real_escape_string($connation,$_POST['supplier_phone2']);
	$supplier_phone3=(int)mysqli_real_escape_string($connation,$_POST['supplier_phone3']);
	$supplier_fax=(int)mysqli_real_escape_string($connation,$_POST['supplier_fax']);
	$supplier_email=mysqli_real_escape_string($connation,$_POST['supplier_email']);
	$supplier_web=mysqli_real_escape_string($connation,$_POST['supplier_web']);
	$supplier_account=mysqli_real_escape_string($connation,$_POST['supplier_account']);
	$supplier_branch=mysqli_real_escape_string($connation,$_POST['supplier_branch']);
	if(mysqli_query($connation,"INSERT INTO
	supplier_details (supplier_id, supplier_code, supplier_company, supplier_name, supplier_contact_name, supplier_mobile, supplier_address1, supplier_address2, supplier_address3, supplier_phone1, supplier_phone2, supplier_phone3, supplier_fax, supplier_email, supplier_web, supplier_account, supplier_branch, supplier_add_user, supplier_add_date, supplier_add_time)
	VALUES (NULL, '$supplier_code', '$supplier_company', '$supplier_name', '$supplier_contact_name', '$supplier_mobile', '$supplier_address1', '$supplier_address2', '$supplier_address3', '$supplier_phone1', '$supplier_phone2', '$supplier_phone3', '$supplier_fax', '$supplier_email', '$supplier_web', '$supplier_account', '$supplier_branch', '$_COOKIE[user_id]', '$current_date', '$current_time')")){
		echo "<script>window.location='supplier-manage?add_success';</script>";
	}
	else{
		echo "<script>window.location='supplier-manage?add_fail';</script>";
	}
}

if(isset($_POST['update_bt'])){
	$supplier_id=mysqli_real_escape_string($connation,$_GET['supplier_id']);
	$supplier_code=mysqli_real_escape_string($connation,$_POST['supplier_code']);
	$supplier_company=mysqli_real_escape_string($connation,$_POST['supplier_company']);
	$supplier_name=mysqli_real_escape_string($connation,$_POST['supplier_name']);
	$supplier_contact_name=mysqli_real_escape_string($connation,$_POST['supplier_contact_name']);
	$supplier_mobile=(int)mysqli_real_escape_string($connation,$_POST['supplier_mobile']);
	$supplier_address1=mysqli_real_escape_string($connation,$_POST['supplier_address1']);
	$supplier_address2=mysqli_real_escape_string($connation,$_POST['supplier_address2']);
	$supplier_address3=mysqli_real_escape_string($connation,$_POST['supplier_address3']);
	$supplier_phone1=(int)mysqli_real_escape_string($connation,$_POST['supplier_phone1']);
	$supplier_phone2=(int)mysqli_real_escape_string($connation,$_POST['supplier_phone2']);
	$supplier_phone3=(int)mysqli_real_escape_string($connation,$_POST['supplier_phone3']);
	$supplier_fax=(int)mysqli_real_escape_string($connation,$_POST['supplier_fax']);
	$supplier_email=mysqli_real_escape_string($connation,$_POST['supplier_email']);
	$supplier_web=mysqli_real_escape_string($connation,$_POST['supplier_web']);
	$supplier_account=mysqli_real_escape_string($connation,$_POST['supplier_account']);
	$supplier_branch=mysqli_real_escape_string($connation,$_POST['supplier_branch']);
	if(mysqli_query($connation,"UPDATE supplier_details SET supplier_code='$supplier_code',supplier_company='$supplier_company',supplier_name='$supplier_name',supplier_contact_name='$supplier_contact_name',supplier_mobile='$supplier_mobile',supplier_address1='$supplier_address1',supplier_address2='$supplier_address2',supplier_address3='$supplier_address3',supplier_phone1='$supplier_phone1',supplier_phone2='$supplier_phone2',supplier_phone3='$supplier_phone3',supplier_fax='$supplier_fax',supplier_email='$supplier_email',supplier_web='$supplier_web',supplier_account='$supplier_account',supplier_branch='$supplier_branch' WHERE supplier_id='$supplier_id'")){
		echo "<script>window.location='supplier-manage?update_success';</script>";
	}
	else{
		echo "<script>window.location='supplier-manage?update_fail';</script>";
	}
}

if(isset($_GET['supplier_id'])){
$supplier_id=mysqli_real_escape_string($connation,$_GET['supplier_id']);
$edit_qury=mysqli_query($connation,"SELECT * FROM supplier_details WHERE supplier_id='$supplier_id'");
$edit_resalt=mysqli_fetch_array($edit_qury);
}
?>
<!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 Manage</strong>
	
<?php require("sub_menu/supplier.php"); ?>
	

<form method="post" autocomplete="off">
<table class="table-sm" style="margin: auto;">
  <tbody>
<tr>
<td colspan="2"><h4>Add New Supplier</h4></td>	  
</tr>
    <tr>
      <td style="width: 150px;">Supplier Code</td>
      <td>		
<table style="width: 100%;">
  <tbody>
    <tr>
      <td class="pl-0"><input name="supplier_code" type="text" required="required" class="form-control form-control-sm" id="supplier_code" style="width: 140px;" value="<?php if(isset($_GET['supplier_id'])){echo $edit_resalt['supplier_code'];}else{echo time();} ?>"></td>
	<td align="right" style="padding-left: 20px;">Company</td>
      <td class="pr-0"><input name="supplier_company" type="text" class="form-control form-control-sm" id="supplier_company" value="<?php if(isset($_GET['supplier_id'])){echo $edit_resalt['supplier_company'];} ?>"></td>
    </tr>
  </tbody>
</table>		  
		</td>
    </tr>
<tr>
<td>Supplier Name</td>
<td><input name="supplier_name" type="text" required="required" class="form-control form-control-sm" id="supplier_name" value="<?php if(isset($_GET['supplier_id'])){echo $edit_resalt['supplier_name'];} ?>"></td>
</tr>
<tr>
<td>Contact Name</td>
<td><input name="supplier_contact_name" type="text" required="required" class="form-control form-control-sm" id="supplier_contact_name" value="<?php if(isset($_GET['supplier_id'])){echo $edit_resalt['supplier_contact_name'];} ?>"></td>
</tr>
	  
<tr>
<td>Phone Number</td>
<td><input name="supplier_mobile" type="tel" required="required" class="form-control form-control-sm" id="supplier_mobile" style="width: 140px;" value="<?php if(isset($_GET['supplier_id'])){echo "0".(int)$edit_resalt['supplier_mobile'];} ?>"></td>
</tr>
	  
<tr>
<td>
<table style="width: 100%;">
  <tbody>
    <tr>
      <td class="pl-0">Address</td>
      <td align="right" style="padding-right: 0px;">1</td>
    </tr>
  </tbody>
</table>	
</td>
<td><input name="supplier_address1" type="text" class="form-control form-control-sm" id="supplier_address1" value="<?php if(isset($_GET['supplier_id'])){echo $edit_resalt['supplier_address1'];} ?>"></td>
</tr>
	  
<tr>
<td align="right">2</td>
<td><input name="supplier_address2" type="text" class="form-control form-control-sm" id="supplier_address2" value="<?php if(isset($_GET['supplier_id'])){echo $edit_resalt['supplier_address2'];} ?>"></td>
</tr>
	  
<tr>
<td align="right">3</td>
<td><input name="supplier_address3" type="text" class="form-control form-control-sm" id="supplier_address3" value="<?php if(isset($_GET['supplier_id'])){echo $edit_resalt['supplier_address3'];} ?>"></td>
</tr>
	  
<tr>
<td>
<table style="width: 100%;">
  <tbody>
    <tr>
      <td class="pl-0">Other Phone</td>
      <td align="right" style="padding-right: 0px;">1</td>
    </tr>
  </tbody>
</table>	
</td>
<td>
	
<table>
  <tbody>
    <tr>
      <td class="pl-0"><input name="supplier_phone1" type="tel" class="form-control form-control-sm" id="supplier_phone1" style="width: 140px; display: inline-block;" value="<?php if(isset($_GET['supplier_id'])){echo "0".(int)$edit_resalt['supplier_phone1'];} ?>"></td>
      <td>2<input name="supplier_phone2" type="tel" class="form-control form-control-sm ml-1" id="supplier_phone2" style="width: 140px; display: inline-block;" value="<?php if(isset($_GET['supplier_id'])){echo "0".(int)$edit_resalt['supplier_phone2'];} ?>"></td>
      <td class="pr-0">3<input name="supplier_phone3" type="tel" class="form-control form-control-sm ml-1" id="supplier_phone3" style="width: 140px; display: inline-block;" value="<?php if(isset($_GET['supplier_id'])){echo "0".(int)$edit_resalt['supplier_phone3'];} ?>"></td>
    </tr>
  </tbody>
</table>
	</td>
</tr>
	  
<tr>
<td>Fax Number</td>
<td><input name="supplier_fax" type="tel" class="form-control form-control-sm" id="supplier_fax" style="width: 140px;" value="<?php if(isset($_GET['supplier_id'])){echo "0".(int)$edit_resalt['supplier_fax'];} ?>"></td>
</tr>
	  
<tr>
<td>Email Address</td>
<td><input name="supplier_email" type="email" class="form-control form-control-sm" id="supplier_email" value="<?php if(isset($_GET['supplier_id'])){echo $edit_resalt['supplier_email'];} ?>"></td>
</tr>
	  
<tr>
<td>Web Address</td>
<td><input name="supplier_web" type="url" class="form-control form-control-sm" id="supplier_web" value="<?php if(isset($_GET['supplier_id'])){echo $edit_resalt['supplier_web'];} ?>"></td>
</tr>
	  
<tr>
<td>Account Number</td>
<td><input name="supplier_account" type="text" class="form-control form-control-sm" id="supplier_account" value="<?php if(isset($_GET['supplier_id'])){echo $edit_resalt['supplier_account'];} ?>"></td>
</tr>
	  
<tr>
<td>Branch</td>
<td><input name="supplier_branch" type="text" class="form-control form-control-sm" id="supplier_branch" value="<?php if(isset($_GET['supplier_id'])){echo $edit_resalt['supplier_branch'];} ?>"></td>
</tr>
<tr>
<td colspan="2" align="right"><button name="<?php if(isset($_GET['supplier_id'])){echo "update_bt";}else{echo "abb_bt";} ?>" type="submit" class="btn btn-sm btn-success mr-1 mt-2"><?php if(isset($_GET['supplier_id'])){echo "Update Supplier";}else{echo "Add Supplier";} ?></button></td>
</tr>
  </tbody>
</table>

</form>
	
<hr>

<p style="margin-bottom: 10px;"><strong>Current Supplier List</strong></p>
	
<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>
<td>Name</td>
<td>Mobile</td>
<td>Company</td>
<td>Total Invoice</td>
<td>Total Payment</td>
<td>Due Balance</td>
</tr>
</thead>
<tbody>
<?php
$count_no=0;
$net_due=0;
$net_invoice=0;
$net_payment=0;
$supplier_qury=mysqli_query($connation,"SELECT * FROM supplier_details ORDER BY supplier_name");
while($supplier_resalt=mysqli_fetch_array($supplier_qury)){
$count_no++;
$net_due+=supplier_total_invoice_amount($supplier_resalt['supplier_id'])-supplier_total_payment($supplier_resalt['supplier_id']);
$net_invoice+=supplier_total_invoice_amount($supplier_resalt['supplier_id']);
$net_payment+=supplier_total_payment($supplier_resalt['supplier_id']);
?>
<tr>
<td style="width: 100px;"><?php echo number_format($count_no,0); ?></td>
<td style="white-space: nowrap; width: 50px;">
<a href="supplier-manage?supplier_id=<?php echo $supplier_resalt['supplier_id']; ?>" class="badge badge-info"><i class="fa fa-edit"></i></a>
<a href="supplier-manage?remove=<?php echo $supplier_resalt['supplier_id']; ?>" class="badge badge-danger" onClick="JavaScript:return confirm('Are your sure remove this supplier?');"><i class="fa fa-trash"></i></a>
</td>
<td style="white-space: pre-line;"><?php echo $supplier_resalt['supplier_name']; ?></td>
<td><?php echo "0".(int)$supplier_resalt['supplier_mobile']; ?></td>
<td style="white-space: pre-line;"><?php echo $supplier_resalt['supplier_company']; ?></td>
<td align="right"><?php echo number_format((float)supplier_total_invoice_amount($supplier_resalt['supplier_id']),2); ?></td>
<td align="right"><?php echo number_format((float)supplier_total_payment($supplier_resalt['supplier_id']),2); ?></td>
<td align="right"><?php echo number_format((float)supplier_total_invoice_amount($supplier_resalt['supplier_id'])-supplier_total_payment($supplier_resalt['supplier_id']),2); ?></td>
</tr>
<?php
}
?>
</tbody>
<tfoot>
	<tr class="table-active">
	<td colspan="5">Net Due Amount</td>
		<td align="right"><?php echo number_format((float)$net_invoice,2) ?></td>
		<td align="right"><?php echo number_format((float)$net_payment,2) ?></td>
		<td align="right"><?php echo number_format((float)$net_due,2) ?></td>
	</tr>
</tfoot>
</table>
</div>
	
<script>
$(document).ready(function() {
    $('#example').DataTable();
} );	
</script>
		
</div>	
	

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