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

if(isset($_GET['supplier_bill_details_id'])){
$supplier_bill_details_id=mysqli_real_escape_string($connation,$_GET['supplier_bill_details_id']);
$view_qury=mysqli_query($connation,"SELECT * FROM supplier_bill_details WHERE supplier_bill_details_id='$supplier_bill_details_id'");
$view_resalt=mysqli_fetch_array($view_qury);
	
$system_id=$view_resalt['supplier_bill_details_system_id'];
}
else{
$system_id=time();
}

if(isset($_POST['save_bt'])){
	$supplier_bill_details_system_id=mysqli_real_escape_string($connation,$_POST['supplier_bill_details_system_id']);
	$supplier_bill_details_supplier=mysqli_real_escape_string($connation,$_POST['supplier_bill_details_supplier']);
	$supplier_bill_details_billnumber=mysqli_real_escape_string($connation,$_POST['supplier_bill_details_billnumber']);
	$supplier_bill_details_create_date=date("Y-m-d",strtotime(mysqli_real_escape_string($connation,$_POST['supplier_bill_details_create_date'])));
	$supplier_bill_details_create_time=date("H:i:s",strtotime(mysqli_real_escape_string($connation,$_POST['supplier_bill_details_create_date'])));
	$supplier_bill_details_close_status=mysqli_real_escape_string($connation,$_POST['supplier_bill_details_close_status']);
	$supplier_bill_details_due_date=mysqli_real_escape_string($connation,$_POST['supplier_bill_details_due_date']);
	$supplier_bill_details_close_remark=mysqli_real_escape_string($connation,$_POST['supplier_bill_details_close_remark']);
	if(mysqli_query($connation,"INSERT INTO
	supplier_bill_details (supplier_bill_details_id, supplier_bill_details_system_id, supplier_bill_details_create_user, supplier_bill_details_supplier, supplier_bill_details_billnumber, supplier_bill_details_create_date, supplier_bill_details_close_status, supplier_bill_details_due_date, supplier_bill_details_close_remark)
	VALUES (NULL, '$supplier_bill_details_system_id', '$_COOKIE[user_id]', '$supplier_bill_details_supplier', '$supplier_bill_details_billnumber', '$supplier_bill_details_create_date', '$supplier_bill_details_close_status', '$supplier_bill_details_due_date', '$supplier_bill_details_close_remark')")){
		
		$update_qury=mysqli_query($connation,"SELECT *
		FROM supp_bill_list sbl INNER JOIN product_details pd ON sbl.supp_bill_product_id=pd.product_id
		WHERE sbl.supp_bill_system_id='$supplier_bill_details_system_id'");
		while($update_resalt=mysqli_fetch_assoc($update_qury)){
			
			if($update_resalt['product_stock_maintain']==1){
				mysqli_query($connation,"INSERT INTO
				stock_details (stock_id, stock_systemid, stock_bill_id, stock_qty, stock_type, stock_info, stock_addtime)
				VALUES (NULL, '$update_resalt[product_systemid]', '', '$update_resalt[supp_bill_qty]', '1', 'New purchasing create, Invoice id $supplier_bill_details_billnumber', '$supplier_bill_details_create_date $supplier_bill_details_create_time')");
			}
			
			mysqli_query($connation,"UPDATE price_details SET price_buy='$update_resalt[supp_bill_buy_price]',price_sale='$update_resalt[supp_bill_sale_price]',price_wholesale='$update_resalt[supp_bill_wholesale]',price_discount='$update_resalt[supp_bill_discount_price]' WHERE price_id='$update_resalt[supp_bill_badge_id]'");
			
		}
		
		if(!empty($_POST['sp_amount'])){
			$sp_amount=mysqli_real_escape_string($connation,$_POST['sp_amount']);
			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, '$supplier_bill_details_supplier', '$_COOKIE[user_id]', 'New invoice ($supplier_bill_details_billnumber) create and payment', '$sp_amount', '$current_date', '$current_time')");
		}
		
	}
}

if(isset($_POST['update_bt'])){
	$supplier_bill_details_id=mysqli_real_escape_string($connation,$_GET['supplier_bill_details_id']);
	$supplier_bill_details_billnumber=mysqli_real_escape_string($connation,$_POST['supplier_bill_details_billnumber']);
	$supplier_bill_details_create_date=mysqli_real_escape_string($connation,$_POST['supplier_bill_details_create_date']);
	$supplier_bill_details_close_status=mysqli_real_escape_string($connation,$_POST['supplier_bill_details_close_status']);
	$supplier_bill_details_due_date=mysqli_real_escape_string($connation,$_POST['supplier_bill_details_due_date']);
	$supplier_bill_details_close_remark=mysqli_real_escape_string($connation,$_POST['supplier_bill_details_close_remark']);
	if(mysqli_query($connation,"UPDATE supplier_bill_details SET supplier_bill_details_billnumber='$supplier_bill_details_billnumber',supplier_bill_details_create_date='$supplier_bill_details_create_date',supplier_bill_details_close_status='$supplier_bill_details_close_status',supplier_bill_details_due_date='$supplier_bill_details_due_date',supplier_bill_details_close_remark='$supplier_bill_details_close_remark' WHERE supplier_bill_details_id='$supplier_bill_details_id'")){
		echo "<script>window.location='view-bill?supplier_bill_details_id=$supplier_bill_details_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">
	
<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 - Create New Bill</strong>
	
<?php require("sub_menu/supplier.php"); ?>
	
<div class="row mt-4">
	
<div class="col-4">
<form method="get" action="suppler_bill_item_list.php" target="list_load">
	<input name="supp_bill_system_id" type="hidden" id="supp_bill_system_id" value="<?php echo $system_id; ?>">
	<input name="supp_bill_product_id" type="hidden" id="supp_bill_product_id">
	<input name="supp_bill_badge_id" type="hidden" id="supp_bill_badge_id">
	<table class="" style="font-size: 10px; width: 100%;">
		<tbody>
			<tr>
			  <td colspan="2">Product<input name="product_name" type="text" required="required" class="form-control form-control-sm" id="product_name" readonly="readonly"></td>
		    </tr>
			<tr>
			  <td>Barcode<input name="product_barcode" type="text" required="required" class="form-control form-control-sm" id="product_barcode" readonly="readonly"></td>
			  <td>Product ID<input name="product_id" type="text" required="required" class="form-control form-control-sm" id="product_id" readonly="readonly"></td>
		    </tr>
			<tr>
			  <td colspan="2">
			<table style="width: 100%;">
			    <tbody>
			      <tr>
			        <td class="w-25">Cost
			          <input name="supp_bill_buy_price" type="number" required="required" class="form-control form-control-sm" id="supp_bill_buy_price" step="0.01"></td>
			        <td class="w-25">MRP
			          <input name="supp_bill_sale_price" type="number" required="required" class="form-control form-control-sm" id="supp_bill_sale_price" step="0.01"></td>
			        <td class="w-25">Wholesale
			          <input name="supp_bill_wholesale" type="number" required="required" class="form-control form-control-sm" id="supp_bill_wholesale" step="0.01"></td>
			        <td class="w-25">Discount
			          <input name="supp_bill_discount_price" type="number" required="required" class="form-control form-control-sm" id="supp_bill_discount_price" step="0.01"></td>
			        </tr>
			      <tr>
			        <td class="">Qty
			          <input name="supp_bill_qty" type="number" required="required" class="form-control form-control-sm" id="supp_bill_qty" step="0.001"></td>
			        <td class="w-25">&nbsp;</td>
			        <td class="w-25">&nbsp;</td>
			        <td class="w-25">&nbsp;</td>
			        </tr>
			      </tbody>
		      </table></td>
		    </tr>
		  </tbody>
	</table>
	<button type="submit" hidden="yes"></button>
</form>

<?php if(isset($_GET['supplier_bill_details_id'])){ ?>
<iframe src="suppler_bill_item_list.php?supp_bill_system_id=<?php echo $system_id; ?>&view" name="list_load" style="width: 100%; height: 350px; border: 1px solid #EEEEEE; margin-top: 10px;"></iframe>		
<?php } else{ ?>
<iframe src="suppler_bill_item_list.php?supp_bill_system_id=<?php echo $system_id; ?>" name="list_load" style="width: 100%; height: 350px; border: 1px solid #EEEEEE; margin-top: 10px;"></iframe>		
<?php } ?>
</div>
<div class="col-6">

<?php if(!isset($_GET['supplier_bill_details_id'])){ ?>
<input type="search" class="form-control form-control-sm mb-2" placeholder="Search Product" onKeyUp="search_product(this.value,'search_text');">
	
<div id="sup_id_load" style="height: 400px; overflow-y: scroll;">

<table class="table table-sm table-hover" style="font-size: 14px;">
	<thead>
		<tr class="table-active">
			<th>Product/Code</th>
			<th>Badge</th>
			<th>Cost</th>
			<th>MRP</th>
		</tr>
		<tr>
			<td colspan="4" align="center">Product not found!</td>
		</tr>
	</thead>
</table>
	
</div>
	
<?php } ?>
	
</div>
<div class="col-2 alert-warning">

<form method="post" autocomplete="off">
	
	<b>Invoice Details</b><br>
	
	<input name="supplier_bill_details_system_id" type="hidden" id="supplier_bill_details_system_id" value="<?php echo $system_id; ?>">

<table class="table table-sm table-borderless">
  <tbody>
    <tr>
      <td class="m-0 p-0">
	invoice Supplier
	<select name="supplier_bill_details_supplier" required <?php if(isset($_GET['supplier_bill_details_id'])){ echo "disabled"; } ?> class="form-control form-control-sm" id="supplier_bill_details_supplier" onChange="search_product(this.value,'sup_wise');">
	<option value="" hidden="yes">Select</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['supplier_bill_details_id'])){ if($view_resalt['supplier_bill_details_supplier']==$supplier_resalt['supplier_id']){echo "selected";} } ?> value="<?php echo $supplier_resalt['supplier_id']; ?>"><?php echo $supplier_resalt['supplier_name']." | 0".(int)$supplier_resalt['supplier_mobile']." | ".$supplier_resalt['supplier_company']; ?></option>
	<?php 
	}
	?>
	</select>
		</td>
    </tr>
    <tr>
      <td class="m-0 p-0">Invoice Number
        <input type="text" name="supplier_bill_details_billnumber" id="supplier_bill_details_billnumber" required class="form-control form-control-sm" value="<?php if(isset($_GET['supplier_bill_details_id'])){echo $view_resalt['supplier_bill_details_billnumber'];} ?>"></td>
    </tr>
    <tr>
      <td class="m-0 p-0">Create Date
        <input type="date" name="supplier_bill_details_create_date" id="supplier_bill_details_create_date" required class="form-control form-control-sm"  value="<?php if(isset($_GET['supplier_bill_details_id'])){echo $view_resalt['supplier_bill_details_create_date'];}else{echo $current_date;} ?>"></td>
    </tr>
    <tr>
      <td class="m-0 p-0">Due Date
        <input type="date" name="supplier_bill_details_due_date" id="supplier_bill_details_due_date" required class="form-control form-control-sm" value="<?php if(isset($_GET['supplier_bill_details_id'])){echo $view_resalt['supplier_bill_details_due_date'];} ?>"></td>
    </tr>
    <tr>
      <td class="m-0 p-0">
		Remark<textarea name="supplier_bill_details_close_remark" rows="4" class="form-control form-control-sm" id="supplier_bill_details_close_remark"><?php if(isset($_GET['supplier_bill_details_id'])){echo $view_resalt['supplier_bill_details_close_remark'];} ?></textarea>
		</td>
    </tr>
    <tr>
      <td class="m-0 p-0">Status
        <select name="supplier_bill_details_close_status" id="supplier_bill_details_close_status" class="form-control form-control-sm">
        <option value="0" <?php if(isset($_GET['supplier_bill_details_id'])){ if($view_resalt['supplier_bill_details_close_status']==0){echo "selected";} } ?>>Open Bill</option>
        <option value="1" <?php if(isset($_GET['supplier_bill_details_id'])){ if($view_resalt['supplier_bill_details_close_status']==1){echo "selected";} } ?>>Close Bill</option>
      </select></td>
    </tr>
	 
<?php if(!isset($_GET['supplier_bill_details_id'])){ ?>
    <tr>
      <td class="m-0 p-0">Cash Payment
		<input name="sp_amount" type="number" class="form-control form-control-sm" id="sp_amount" step="0.01">
		</td>
    </tr>
<?php } ?>
  </tbody>
</table>

<div style="text-align: right;"><button name="<?php if(isset($_GET['supplier_bill_details_id'])){echo "update_bt";}else{echo "save_bt";} ?>" type="submit" class="btn btn-success btn-sm"><i class="fa fa-save"></i> <?php if(isset($_GET['supplier_bill_details_id'])){echo "Update Bill";}else{echo "Save Bill";} ?></button></div>
	
</form>
	
</div>
	
</div>
		
</div>	
	

	
<script>
function load_details(supp_bill_product_id,product_name,product_barcode,product_id,supp_bill_badge_id,supp_bill_buy_price,supp_bill_sale_price,supp_bill_wholesale,supp_bill_discount_price){
	document.getElementById('supp_bill_product_id').value=supp_bill_product_id;
	document.getElementById('product_name').value=product_name;
	document.getElementById('product_barcode').value=product_barcode;
	document.getElementById('product_id').value=product_id;
	document.getElementById('supp_bill_badge_id').value=supp_bill_badge_id;
	document.getElementById('supp_bill_buy_price').value=supp_bill_buy_price;
	document.getElementById('supp_bill_sale_price').value=supp_bill_sale_price;
	document.getElementById('supp_bill_wholesale').value=supp_bill_wholesale;
	document.getElementById('supp_bill_discount_price').value=supp_bill_discount_price;
	document.getElementById('supp_bill_qty').select();
}
	
function clear_text_box(){
	document.getElementById('supp_bill_product_id').value="";
	document.getElementById('product_name').value="";
	document.getElementById('product_barcode').value="";
	document.getElementById('product_id').value="";
	document.getElementById('supp_bill_badge_id').value="";
	document.getElementById('supp_bill_buy_price').value="";
	document.getElementById('supp_bill_sale_price').value="";
	document.getElementById('supp_bill_wholesale').value="";
	document.getElementById('supp_bill_discount_price').value="";
	document.getElementById('supp_bill_qty').value="";
}
	
function search_product(search_text,search_type){

	document.getElementById('sup_id_load').innerHTML=`<p align="center">Product Not Found</p>`;
	
	document.getElementById('sup_id_load').innerHTML=`<p align="center"><img src="image/loading_sm.gif"> Please Wait...</p>`;
	const xhttp = new XMLHttpRequest();
	xhttp.onload = function() {
		document.getElementById("sup_id_load").innerHTML = this.responseText;
	}
	xhttp.open("GET", "ajax_sup_wise_item_load.php?search_text="+search_text+"&search_type="+search_type, true);
	xhttp.send();

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