<?php
session_start();
 //include ('../common/library.php');  
 //include '../db/db.php';
 
include "../lib/db/connection.php";
include ('../common/helper.php');

 

	//include 'database.php';
	$sql = $conn->query("SELECT * FROM purchase_order where group_for=".$_SESSION['group_for']." and purchase_no=".$_SESSION['purchase_no']."");
	//$result = $conn->query($sql);
	$total_amt=0;
		while($dt = $sql->fetch_assoc()) {
		$stock = find_a_field('journal_item','sum(item_in-item_ex)',' warehouse="'.$_SESSION['warehouse'].'" and item_id ='.$dt['item_id']);
		$uom   = find_a_field('item_info','uom','item_id='.$dt['item_id']);
		$total_amt +=$dt['amount'];
?>	
		<tr>
			<td><select name="item_name_<?=$dt['id'];?>"  class="form-control form-control" id="item_name_<?=$dt['id'];?>" onchange="edit(<?=$dt['id'];?>)">
				 <option></option>
				<? $itm = $conn->query("select * from item_info where status='Active'");
                    while($item = $itm->fetch_assoc()){
					?>
					<option <?=($dt['item_id']==$item['item_id'])? 'selected':''?> value="<?=$item['item_id']?>"><?=$item['item_name']?></option>
                     <? } ?>
				</select><input type="hidden" name="update_id_<?=$dt['id'];?>" id="update_id_<?=$dt['id'];?>" value="<?=$dt['id'];?>" />
				<input type="hidden" name="purchase_no" id="purchase_no" value="<?=$_SESSION['purchase_no'];?>" /></td>
			<td><input type="text"  name="stock_<?=$dt['id'];?>"  class="form-control form-control" id="stock_<?=$dt['id'];?>" onblur="edit(<?=$dt['id'];?>)" value="<?=$stock?>" required></td>
			<td><input type="text"  name="unit_<?=$dt['id'];?>"  class="form-control form-control" id="unit_<?=$dt['id'];?>" onblur="edit(<?=$dt['id'];?>)" value="<?=$uom?>" required></td>
			<td><input type="text"  name="rate_<?=$dt['id'];?>"  class="form-control form-control" id="rate_<?=$dt['id'];?>" onblur="edit(<?=$dt['id'];?>);cal(<?=$dt['id'];?>);" value="<?=$dt['rate'];?>" required></td>
			<td><input type="text"  name="qty_<?=$dt['id'];?>"  class="form-control form-control" id="qty_<?=$dt['id'];?>" onblur="edit(<?=$dt['id'];?>);cal(<?=$dt['id'];?>);" value="<?=$dt['qty'];?>" required></td>
			<td><input type="text"  name="amount_<?=$dt['id'];?>" class="form-control form-control" id="amount_<?=$dt['id'];?>" value="<?=$dt['amount'];?>" required></td>
			<td><input type="button" name="edit_<?=$dt['id'];?>" class="btn btn-primary" value="Edit" id="edit_<?=$dt['id'];?>" ></td>
			<td><button onclick="deleteData(<?=$dt['id'];?>)" class="btn btn-danger">X</button></td>
			
		</tr>
<?php	
	}
	//mysqli_close($conn);
?>
	   <tr>
			<td colspan="5" align="right"><b>Total</b></td>
			<td><input type="text"  name="total"  class="form-control form-control" id="total" value="<?=$total_amt;?>" readonly></td>
	   </tr>	
 
