<?php 
session_start();
ob_start(); 
include "header.php";
//$objIms->Semifinisheditem_alter();
?>
<link href="../css/select2.css" rel="stylesheet" />
<?php

if(isset($_GET["uid"]))
{
$uid=$_GET["uid"];
$rs=$objIms->getRows("view_purchase_supplier","md5(id)='$uid' and session_login_id = '".$_SESSION['login_id']."'");
 $pur_id=$rs["id"];
$order_num=$rs["order_no"];
$purchase_date=$objIms->IntToDate($rs["purchase_date"]);
$supplier_name=$rs["supplier_name"];
$tax_type=$rs["tax_type"];
$freight_charge=$rs["freight_charge"];
$supplier_id=$rs["supplier_id"];
$receiving_no = $rs["receiving_no"];
$sql_acc="select *  from tbl_account where session_login_id = '".$_SESSION['login_id']."' and mas_id=$pur_id and sales_type='Purchase' order by id asc";
$res_acc=mysql_query($sql_acc);
$rsacc=mysql_fetch_array($res_acc);
$discount=$rsacc["discount"];	
$c_type=$rsacc["cash_type"];	

$sql_acc1="select *  from supplier_details where session_login_id = '".$_SESSION['login_id']."' and pur_mstr_id=$pur_id and sales_type='Purchase' order by id asc";
$res_acc1=mysql_query($sql_acc1);



$sql_ck="select *  from tbl_purchase_master where id=$pur_id and session_login_id = '".$_SESSION['login_id']."'";
$res_ck=mysql_query($sql_ck);
$rock=mysql_fetch_array($res_ck);
$discountck=$rock["discount"];
$freight_chargeck=$rock["freight_charge"];
$grand_totck=$rock["grand_tot"];
$round_offck=$rock["round_off"];				

}


	
?>
<?php

if((!isset($_POST['Save'])) and (!isset($_POST['additems'])) and (!isset($_POST['del'])) ){
  $sql0="DELETE FROM `tbl_tmp_audit_purchase_details` where session_login_id = '".$_SESSION['login_id']."'";
				  			$final0=mysql_query($sql0);
$sqldt="select * from tbl_purchase_details where purmas_id = '$pur_id' and session_login_id = '".$_SESSION['login_id']."'";
							//echo $sqlc;
							$rdt=mysql_query($sqldt);
							if(mysql_num_rows($rdt)>0){
							while($rodt=mysql_fetch_assoc($rdt))
							{
							$sqlgn="select * from tbl_item where id = '".$rodt['item_id']."' and session_login_id = '".$_SESSION['login_id']."'";
							$rgn=mysql_query($sqlgn);
							$rogn=mysql_fetch_assoc($rgn);
							
							$sqlgn2="select * from tbl_unit where id = '".$rodt['unit_id']."' and session_login_id = '".$_SESSION['login_id']."'";
							$rgn2=mysql_query($sqlgn2);
							$rogn2=mysql_fetch_assoc($rgn2);
							
							$ins="INSERT INTO `tbl_tmp_audit_purchase_details` (`item_id`, `item_name`, `pur_qty`, `pur_price`, `unit_id`, `unit_name`, `sell_price`, `discount`, `cgst`, `sgst`, `igst`, `purmas_id`, `session_login_id`) VALUES ('".$rodt['item_id']."', '".$rogn['item_name']."', '".$rodt['pur_qty']."', '".$rodt['pur_price']."', '".$rodt['unit_id']."', '".$rogn2['unit_name']."', '".$rodt['sell_price']."', '".$rodt['discount']."', '".$rodt['cgst']."', '".$rodt['sgst']."', '".$rodt['igst']."', '$pur_id', '".$_SESSION['login_id']."');";
							$rins=mysql_query($ins);
							 }}
}
?>

<?php

if(isset($_POST["del"]))
{
	$tem_id=$_POST["del"];
	
	$del="delete from tbl_tmp_audit_purchase_details where id=$tem_id and session_login_id = '".$_SESSION['login_id']."'";
	mysql_query($del);

}



if(isset($_POST["additems"]))
{
	$item_id=$_POST["item_id"];
	$item_nm=$_POST["item_nm"];
	$qty=$_POST["qty"];
	$unit=explode("/",$_POST["unit"]);
	$unit_id=$unit[0];
	$unit_name=$unit[1];
	$price=$_POST["price"];
	$sell_price=$_POST["sell_price"];
	
	$discount=$_POST["discount"];
	$cgst=$_POST["cgst"];
	$sgst=$_POST["sgst"];
	$igst=$_POST["igst"];
	
	
	if(($item_id>0) and ($qty>0) and ($price>0) and ($unit_id>0))
	{
	

	
	
							$sql3="insert into tbl_tmp_audit_purchase_details(item_id,item_name,pur_qty,pur_price,unit_id,unit_name,sell_price,discount,cgst,sgst,igst,purmas_id,session_login_id) values('$item_id','$item_nm','$qty','$price','$unit_id','$unit_name','$sell_price','$discount','$cgst','$sgst','$igst','$pur_id','".$_SESSION['login_id']."')";
				  			$final2=mysql_query($sql3);
						
						
	}
}

if(isset($_POST["Save"]))
{
	$order_no=$_POST["order_no"];	
	$or_date = date('d-m-Y', strtotime($_POST["order_date"]));	
	$order_date=$objIms->DateToInt($or_date);
	$stampdate=$objIms->DateToInt(date('d-m-Y'));	
	$vendor_id=$_POST["supplier_name"];	
	$totamt=$_POST["totamt"];
	$pay_type=$_POST["pay_type"];	
	$result=$_POST["grand_total"];
	$round_off=$_POST["round_off"];
	$totdiscount=$_POST["totdiscount"];
	$tax_type=$_POST["tax_type"];
	$freight_charge=$_POST["freight_chrg"];
	
	$sql_1="select * from tbl_tmp_audit_purchase_details where session_login_id = '".$_SESSION['login_id']."'";
	$res_1=mysql_query($sql_1);
	if((mysql_num_rows($res_1)>0))
	{
	
			if(($order_no<>"") && ($vendor_id<>""))
			{
				
				$purchase_date2=$objIms->DateToInt($purchase_date);

$sql_det="insert into tbl_audit_purchase_master(receiving_no,pur_id,order_no,purchase_date, supplier_id,discount, freight_charge, grand_tot, round_off, session_login_id) values('$receiving_no','$pur_id', '$order_num','$purchase_date2','$supplier_id','$discountck','$freight_chargeck','$grand_totck','$round_offck','".$_SESSION['login_id']."')";
				
				$res_det=mysql_query($sql_det);
				
				$sql_pur_det="select * from tbl_purchase_details where purmas_id = '$pur_id' and session_login_id = '".$_SESSION['login_id']."'";
							//echo $sqlc;
							$r_pur_det=mysql_query($sql_pur_det);
							if(mysql_num_rows($r_pur_det)>0){
							while($ro_pur_det=mysql_fetch_assoc($r_pur_det))
							{
							
							
							$ins_audit="INSERT INTO `tbl_audit_purchase_details` (`purmas_id`, `item_id`, `pur_qty`, `pur_price`, `unit_id`, `sell_price`, `discount`, `cgst`, `sgst`, `igst`, `session_login_id`) VALUES ('$pur_id', '".$ro_pur_det['item_id']."', '".$ro_pur_det['pur_qty']."', '".$ro_pur_det['pur_price']."', '".$ro_pur_det['unit_id']."', '".$ro_pur_det['sell_price']."', '".$ro_pur_det['discount']."', '".$ro_pur_det['cgst']."', '".$ro_pur_det['sgst']."', '".$ro_pur_det['igst']."', '".$_SESSION['login_id']."');";
							$r_audit=mysql_query($ins_audit);
							 }}
							 
							 
							 
							
				
				$sql_ac="select * from tbl_account where session_login_id = '".$_SESSION['login_id']."' and mas_id = '$pur_id' and sales_type = 'Purchase' order by id asc";
							//echo $sqlc;
							$r_ac=mysql_query($sql_ac);
							if(mysql_num_rows($r_ac)>0){
							$ro_ac=mysql_fetch_assoc($r_ac);
							
							
							$ins_ac="INSERT INTO `tbl_audit_account` (`invoice_no`, `mas_id`, `trans_date`, `actual_amt`, `Cr_Amt`, `sales_type`, `Dr_Amt`, `cash_type`, `clear`, `discount`, `freight_charge`, `cst`, `grand_tot`, `cancel_status`, `entry_time`, `session_login_id`) VALUES ('".$ro_ac['invoice_no']."', '".$ro_ac['mas_id']."', '".$ro_ac['trans_date']."', '".$ro_ac['actual_amt']."', '".$ro_ac['Cr_Amt']."', '".$ro_ac['sales_type']."', '".$ro_ac['Dr_Amt']."', '".$ro_ac['cash_type']."', '".$ro_ac['clear']."', '".$ro_ac['discount']."', '".$ro_ac['freight_charge']."', '".$ro_ac['cst']."', '".$ro_ac['grand_tot']."', '".$ro_ac['cancel_status']."', CURRENT_TIMESTAMP , '".$_SESSION['login_id']."');";
							$ac_r=mysql_query($ins_ac);
							 }
							 
							 
	
	$sqldel="delete from tbl_purchase_details where purmas_id = '$pur_id' and session_login_id = '".$_SESSION['login_id']."'";
				$restdel=mysql_query($sqldel);
				
	$sql_insmas1="UPDATE `tbl_purchase_master` SET `order_no` = '$order_no', `purchase_date` = '$order_date', `supplier_id` = '$vendor_id', `discount` = '$totdiscount', `freight_charge` = '$freight_charge',  `grand_tot` = '$result',  `round_off` = '$round_off'  WHERE session_login_id = '".$_SESSION['login_id']."' and `id` = '$pur_id';";
				
				$res_insmas1=mysql_query($sql_insmas1);
				$lastid1=mysql_insert_id();
				
					
					while($result=mysql_fetch_array($res_1))
					{
					$item_id=$result["item_id"];
					$qty=$result["pur_qty"];
					$pur_price=$result["pur_price"];
					$unit_id=$result["unit_id"];
					$sell_price=$result["sell_price"];
					$discount=$result["discount"];
					$cgst=$result["cgst"];
					$sgst=$result["sgst"];
					$igst=$result["igst"];
					
					$sql4="insert into tbl_purchase_details(purmas_id,item_id,pur_qty,pur_price,unit_id,sell_price,discount,cgst,sgst,igst,session_login_id)values('$pur_id','$item_id','$qty','$pur_price','$unit_id','$sell_price','$discount','$cgst','$sgst','$igst','".$_SESSION['login_id']."')";
					//echo $sql4;
					$rs1=mysql_query($sql4);
					
					}
					$invoice_no=$objIms->trasaction_no();
					if($pay_type=='Credit')
					{
					 $sql_ins="update tbl_account set invoice_no = '$invoice_no', trans_date = '$stampdate', actual_amt = '$round_off', Dr_Amt = '$round_off', cash_type = '$pay_type' , clear = '0' , discount = '$totdiscount', grand_tot = '$result' where session_login_id = '".$_SESSION['login_id']."' and mas_id = '$pur_id' and sales_type = 'Purchase' order by id asc";

				    $sql_ins="update supplier_details set name_id ='$vendor_id', cr_amount = '$round_off', sales_type = 'Purchase' where session_login_id = '".$_SESSION['login_id']."' order by id asc";
					 
					

					}
					else
					{
					 $sql_ins="update tbl_account set invoice_no = '$invoice_no', trans_date = '$stampdate', actual_amt = '$round_off', Dr_Amt = '$round_off', cash_type = '$pay_type' , clear = '1' , discount = '$totdiscount', grand_tot = '$result' where session_login_id = '".$_SESSION['login_id']."' and mas_id = '$pur_id' and sales_type = 'Purchase' order by id asc";
					}
				
				$res_ins=mysql_query($sql_ins);
			    
			
				
								

	
				$sql="delete from tbl_tmp_audit_purchase_details where session_login_id = '".$_SESSION['login_id']."'";
				$rest=mysql_query($sql);
				
				?>
				<script>
				alert('Success');
				location='purchase_list.php';
				</script>
				<?php
				
				
				
				//header("location:purchase_view.php?uid=".md5($lastid));	
			}
	}
	
}

?>

	 <?php 
//Generate order no..............................................
		/*function getTotalEmployees() {
		$query = "SELECT max(id) as numss FROM tbl_pur_order_master";
		 $totalrecord = mysql_fetch_array(mysql_query($query));
		$num = $totalrecord[numss]; 
		//	echo $totalrecord;		 
		++$num; // add 1;
		$len = strlen($num);
		for($i=$len; $i< 6; ++$i) {
		$num = '0'.$num;
		}
		return $num;
		}		

$mm=$db->findmm($date=date('d-m-Y'));
$yy=$db->findyy($date=date('d-m-Y'));

//$uresult=strtoupper($result) ;
$number = getTotalEmployees();
$order_no = 'P0/USC/'.$number.'/'.$mm.'-'.$yy;*/
//echo $order_no;			
//end...............................................................	
?>


		
	<script type="text/javascript">
	$(document).ready(function() {                
	$("#form1").validate({
	rules: {
	delv_location: "required",
	supplier_name: "required",
	pay_type:"required",
	tax_type:"required",
	},
	messages: {
	delv_location:"",
	supplier_name:"",
	pay_type:"",
	tax_type:"",
	},
	});                
	});
	
	
	
	
	
	function calfreight(str)
	{
		var sumtot=document.getElementById("totamt").value;
		var calf=eval(str)+eval(sumtot);
		//alert(Math.round(eval(calf)));
		
		document.getElementById("grand_total").value=eval(calf);
		document.getElementById("round_off").value=Math.round(eval(calf));
		
	}	
		
		
	</script>
		 <style>
			select.error { background-color:#ffa8a8; border: 1px dotted red;  }
			input.error{ background-color:#ffa8a8;   }
			textarea.error{ background-color:#ffa8a8; border: 1px dotted red;  }
         </style>
		 
        <script type="text/javascript"> 
 		/*jQuery(document).ready(function(){
			$('#item_nm').autocomplete({source:'../ProgramFile/auto/suggest_POitem.php', minLength:1,
			select: function (event, ui) {
        	$("#item_id").val(ui.item.id); // display the selected id
       		$("#item_nm").val(ui.item.value); // display the selected text.
			
		    }
			});

}); */
</script>

<script type="application/javascript">
	function isNumberKey(evt)
	{
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode > 31 && (charCode < 46 || charCode > 57))
	return false;
	return true;
	}
	
	
	function showtax(str)
	{
		if(str=='Tax Exempted')
		{
			document.getElementById("cgst").value=0;
			document.getElementById("sgst").value=0;
			document.getElementById("igst").value=0;
			document.getElementById("cgst").readOnly=true;
			document.getElementById("sgst").readOnly=true;
			document.getElementById("igst").readOnly=true;
		}
		else
		{
			document.getElementById("cgst").readOnly=false;
			document.getElementById("sgst").readOnly=false;
			document.getElementById("igst").readOnly=false;
		}
	}
	</script>
<script type="text/javascript" language="javascript">

var e = document.getElementById("ddlViewBy");
var strUser = e.options[e.selectedIndex].text;
document.getElementById("tax_name")=strUser;
</script>	
<style type="text/css">
<!--
.table_bg{ border:#CCCCCC solid 1px; border-radius:5px; height:auto; width:auto; padding:20px; margin-bottom:10px;}


-->
</style>
<!-- Page wrapper  -->
        <div class="page-wrapper">
          
            <!-- Container fluid  -->
            <div class="container-fluid">
                <!-- Start Page Content -->
                <div class="row">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-body">
							<h3>Purchase Edit <span style="float:right"><button onclick="goBack()" class="btn btn-info btn-sm">Go Back</button><script>
function goBack() {
    window.history.back();
}
</script></span></h3><hr />
<form id="form1" name="form1" method="post" action="">
<table width="100%" cellpadding="4">
  <tr>
  
    <td height="38" bgcolor="#FFFFFF" ><strong>Order   No</strong></td>
    <td bgcolor="#FFFFFF" ><strong>:</strong></td>
    <td bgcolor="#FFFFFF" ><input type="text" name="order_no" value="<?php echo $order_num;?>" class="form-control" style="width:200px;" /></td>
    <td width="8%" height="38"><strong>Supplier Name<span style="color:#F00; font-weight:bold;">*</span> </strong></td>
    <td width="2%"><strong>:</strong></td>
    <td width="31%"><select name="supplier_name" id="supplier_name" style="width:200px;" onChange="showadd(this.value)" style="width:200px;" class="required form-control">
        <option value="<?php echo $supplier_id;?>"><?php echo $supplier_name;?></option>
        <?php 
							$sqlc="select * from tbl_supplier where session_login_id = '".$_SESSION['login_id']."'";
							//echo $sqlc;
							$c=mysql_query($sqlc);
							if(mysql_num_rows($c)>0){
							while($cs=mysql_fetch_array($c))
							{?>
        <option value="<?=$cs["id"];?>" <?php if(($cs["id"])==$_POST["supplier_name"]){ ?> selected="selected" <?php }?>>
        <?=$cs["supplier_name"];?>
        </option>
        <?php }}?>
    </select></td>
    </tr>
  <tr>
    <td width="7%" height="34" bgcolor="#FFFFFF" ><strong>Order Date </strong></td>
    <td width="1%" bgcolor="#FFFFFF" ><strong>:</strong></td>
    <td width="28%" bgcolor="#FFFFFF" ><?php //if(($_POST["order_date"])=="") { $dt=date("d-m-Y");} else { $dt=$_POST["order_date"];}?>
        <input name="order_date" type="date" id="order_date"  style="width:200px; background-color:#FFF"  value="<?php echo $purchase_date; ?>" class="tcal form-control"/></td>
    <td><strong>Cash Type<span style="color:#F00; font-weight:bold;">*</span></strong></td>
    <td><strong>:</strong></td>
    <td><select name="pay_type" id="pay_type" class="form-control" style="width:200px;"  >
      <option value="">---Please Select---</option>
      <option value="Credit"<?php if($c_type=='Credit'){?> selected="selected"<?php }?>>Credit</option>
      <option value="Debit"<?php if($c_type=='Debit'){?> selected="selected"<?php }?>>Cash</option>
    </select></td>
  </tr>
 <!-- <tr>
    <td height="34" bgcolor="#FFFFFF" ><strong>Tax Type </strong></td>
    <td bgcolor="#FFFFFF" ><strong>:</strong></td>
    <td bgcolor="#FFFFFF" ><select name="tax_type" id="tax_type" style="width:185px;" onchange="showtax(this.value)"  >
      <option value="">---Please Select---</option>
      <option value="Tax"<?php if($_POST["tax_type"]=='Tax'){?> selected="selected"<?php }?>>Tax</option>
      <option value="Tax Exempted"<?php if($_POST["tax_type"]=='Tax Exempted'){?> selected="selected"<?php }?>>Tax Exempted</option>
    </select></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>-->
</table>

<div class="panel">
<div class="panel-heading"><strong>Add Item </strong></div>
          
			
			 <table width="100%" class="table table-bodered">
            <tr  align="left">
            <td >Item Name</td>
            <td ><strong>:</strong></td>
            <td ><select name="item_nm" id="item_nm" onchange="fun()" class="form-control" style="width:150px;">
			 <option value="">Select</option>
			<?php 
							$sqlitm="select * from tbl_item where session_login_id = '".$_SESSION['login_id']."'";
							//echo $sqlc;
							$citm=mysql_query($sqlitm);
							if(mysql_num_rows($citm)>0){
							while($row_itm=mysql_fetch_array($citm))
							{?>
        <option value="<?=$row_itm["item_name"];?>">
        <?=$row_itm["item_name"];?>
        </option>
        <?php }}?>
		</select>
            <input type="hidden" name="item_id" id="item_id" /></td>
            <td >Unit</td>
            <td ><strong>:</strong></td>
            <td ><select name="unit" id="unit" class="form-control" style="width:75px;" onChange="showadd(this.value)" >
              <option value="">Please Select</option>
              <?php 
							$sqlc="select * from tbl_unit where session_login_id = '".$_SESSION['login_id']."'";
							//echo $sqlc;
							$c=mysql_query($sqlc);
							if(mysql_num_rows($c)>0){
							while($cs=mysql_fetch_array($c))
							{?>
              <option value="<?=$cs["id"].'/'.$cs["unit_name"];?>" >
              <?=$cs["unit_name"];?>
              </option>
              <?php }}?>
            </select></td>
            <td >Qty</td>
            <td><strong>:</strong></td>
            <td ><input type="text" name="qty" id="qty" onKeyPress="return isNumberKey(event)" style="width:75px"></td>
            <td>Pur Price(Per)</td>
            <td ><strong>:</strong></td>
            <td width="8%"><input type="text" name="price" id="price" onkeypress="return isNumber(event,this);" style="width:75px" /></td>
			<td nowrap="nowrap">Sell Price(Per)</td>
			<td nowrap="nowrap"><strong>:</strong></td>
			<td nowrap="nowrap"><input type="text" name="sell_price" id="sell_price" onkeypress="return isNumber(event,this);" style="width:75px" /></td>
			</tr>
            <tr height="32" align="left">
              <td>Discount(in %)</td>
              <td><strong>:</strong></td>
              <td><input type="text" name="discount" id="discount" onkeypress="return isNumber(event,this);" style="width:75px" /></td>
              <td>CGST(in %)</td>
              <td><strong>:</strong></td>
              <td><input type="text" name="cgst" onkeypress="return isNumber(event,this);" id="cgst" <?php if($_POST["tax_type"]=='Tax Exempted'){?> readonly="" <?php }?> style="width:75px" /></td>
              <td>SGST(in %)</td>
              <td><strong>:</strong></td>
              <td><input type="text" name="sgst" onkeypress="return isNumber(event,this);" id="sgst" <?php if($_POST["tax_type"]=='Tax Exempted'){?> readonly="" <?php }?> style="width:75px" /></td>
              <td>IGST(in %)</td>
              <td><strong>:</strong></td>
              <td><input type="text" name="igst" onkeypress="return isNumber(event,this);" id="igst" <?php if($_POST["tax_type"]=='Tax Exempted'){?> readonly="" <?php }?> style="width:75px" /></td>
              <td><input type="submit" name="additems" id="additems" value="Add Items" class="btn btn-warning" style="margin-top::0px" /></td>
              </tr>
           </table>
			
</div>
<script type="text/javascript">
   function isNumberKey(evt){
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;
    return true;
} 
</script>
<script type="text/javascript">
function isNumber(evt, element) {
  var charCode = (evt.which) ? evt.which : event.keyCode
  if (charCode > 31 && (charCode < 48 || charCode > 57) && !(charCode == 46 || charCode == 8))
    return false;
  else {
    var len = $(element).val().length;
    var index = $(element).val().indexOf('.');
    if (index > 0 && charCode == 46) {
      return false;
    }
    if (index > 0) {
      var CharAfterdot = (len + 1) - index;
      if (CharAfterdot > 3) {
        return false;
      }
    }

  }
  return true;
}
</script>   


      <div class="panel">
            <div class="panel-heading"><strong>Item List of Purchase </strong></div>
            <p>&nbsp;</p>
            <table width="100%" class="table table-striped">
		
            <?php 
				 $sql_viewtmp="select *  from tbl_tmp_audit_purchase_details where purmas_id=$pur_id and session_login_id = '".$_SESSION['login_id']."'";
				$res_viewtmp=mysql_query($sql_viewtmp);
				if(mysql_num_rows($res_viewtmp)>0)
				{ 
					$i=1;
					$sums=0;
					?>
                     
                     <tr height="32" align="left">
            <th width="7%">Si No</th>
            <th width="40%">Item Name</th>
           
           
            <th >Order Qty</th>
            <th >Sell Price(Per) </th>
            <th >Pur Price(Per)</th>
            <th>Total</th>
            <th>Discount Amount</th>
            <th >CGST(%)</th>
            
            <th >SGST(%)</th>
           
            <th >IGST(%)</th>
           
            <th >Total Price</th>
			<th >Delete</th>
            </tr>
            <?php
				while($rows=mysql_fetch_assoc($res_viewtmp))
					{
						
						
						
						
							
							
			?>
           
            <tr height="32">
            <td ><?php echo $i;?></td>
           
		    <td ><?php echo $rows["item_name"];?></td>
            
           
            <td ><?php echo $rows["pur_qty"];?></td>
            <td ><?php echo $rows["sell_price"];?></td>
            <td ><?php echo $rows["pur_price"];?></td>
            <td><?php  $totsum=($rows["pur_qty"]*$rows["pur_price"]); echo number_format($totsum,2);?></td>
            <td><?php  $discount=$totsum*($rows["discount"]/100); echo number_format($discount,2); $totprice=($totsum-$discount);?></td>
            <td ><?php echo $rows["cgst"];?><?php  $cgst=$totprice*($rows["cgst"]/100);  number_format($cgst,2);?></td>
           
            <td ><?php echo $rows["sgst"];?><?php  $sgst=$totprice*($rows["sgst"]/100);  number_format($sgst,2);?></td>
         
            <td ><?php echo $rows["igst"];?><?php  $igst=$totprice*($rows["igst"]/100);  number_format($igst,2);?></td>
           
            <td><?php   $totamt=$totprice+$cgst+$sgst+$igst; echo number_format($totamt,2);?></td>
			 <td ><button type="submit" name="del" value="<?=$rows["id"]?>" class="btn btn-danger">Delete</button></td>
            </tr>  <?php $sums=$sums+($totamt);$i++;} ?>   
            <tr height="32">
             
              <td colspan="10"><div align="right"><strong>Sum Total : </strong></div></td>
              <td><?php echo number_format($sums,2);?>
			   <input type="hidden" name="totamt" id="totamt" value="<?php echo $sums;?>" /> <input type="hidden" name="totdiscount" id="totdiscount" value="<?php echo $sumdisamt;?>" /></td>
			  
              </tr> 
            <?php }?>
           </table>
		   
		   <?php
		  $sum_total=$sums;
  		 $str="SELECT * FROM tbl_purchase_tax where pur_mstr_id=$pur_id and session_login_id = '".$_SESSION['login_id']."'";
		$query_tax=mysql_query($str);
		 $tot_tax=0;
		 if(mysql_num_rows($query_tax)>0)
		 {?>
		   <?php  }
		 
        
		$nettot=@$sum_total+@$tot_tax+@$freight_charge;?>
<table width="100%" class="table table-bordered">
		  
          <tr>
            <td><div align="right"><strong>Freight Charge(in Rs)</strong></div></td>
            <td><input type="text" name="freight_chrg" id="freight_chrg" onkeypress="return isNumber(event,this);" style="width:100px; border:none; background:#FFFFFF;" onBlur="calfreight(this.value)" value="<?php echo $freight_chargeck;?>"/></td>
          </tr>
          <tr>
            <td><div align="right">
                <div align="right"><strong>Grand Total(in Rs)</strong></div>
            </div></td>
            <td><?php echo number_format($nettot,2);?><input type="hidden" name="grand_total" id="grand_total" style="width:100px; border:none; background:#FFFFFF;" readonly value="<?php echo $nettot;?>"/></td>
          </tr>
          
          <tr>
            <td width="85%"><div align="right"><strong>Round Off</strong></div></td>
            <td width="15%"> <?php echo number_format(round($nettot),2);?><input type="hidden" name="round_off" id="round_off" style="width:100px; border:none; background:#FFFFFF;" readonly value="<?php echo round($nettot);?>"/></td>
          </tr>
		  
       </table>
    </div>
		

   
<p></p>
		<br />
        <div align="center" style="color:#990000; font-weight:bold;">
          <?php   
		     echo $mas;
		  ?>
        </div>
    <table>     
          <tr>
            <td height="35" ><input type="submit" id="Save" name="Save" value="Save" class="btn btn-info" style="margin:inherit; width:100px"/></td>
          </tr>
    </table>

</form> </div>
                        </div>
                    </div>
                </div>
                <!-- End PAge Content -->
            </div>
            <!-- End Container fluid  -->
			
     <script src="../js/lib/jquery/jquery.min.js"></script>
    <script src="../js/lib/bootstrap/js/popper.min.js"></script>
    <script src="../js/lib/bootstrap/js/bootstrap.min.js"></script>
    <!-- slimscrollbar scrollbar JavaScript -->
    <script src="../js/jquery.slimscroll.js"></script>
    <!--Menu sidebar -->
    <script src="../js/sidebarmenu.js"></script>
    <!--stickey kit -->
    <script src="../js/lib/sticky-kit-master/dist/sticky-kit.min.js"></script>
    <!--Custom JavaScript -->
    <script src="../js/custom.min.js"></script>
	<script src="../js/select2.js"></script>
	<script type="text/javascript">
			$(document).ready(function() {
				var item_nm = [];
				$("#item_nm").select2({
				  data: item_nm
				});
			});
			function fun(){
			var item_name =$("#item_nm").val();
			$.ajax({
		type: "POST",
		url: "fetch_item.php",
		data: "item_name="+item_name,
		 dataType: 'json', 
		cache: false,
		success: function(data)
			{
				var item_id = data[0];
				
				$('#item_id').val(item_id)
				
				
				
			}
			});
   
			
			}
		</script>

</body>

</html>
