<?php
$PageSecurity = 11;
include('includes/DefinePOClass.php');
include('includes/DefineSerialItems.php');
include('includes/session.inc');
include('includes/SQL_CommonFunctions.inc');
include('includes/DefineSuppTransClass.php');
include('includes/function_lib.php');
$title = _('Purchase Bill Book');
include('includes/header.inc');
//echo '<p class="page_title_text"><img src="'.$rootpath.'/css/'.$theme.'/images/magnifier.png" title="' . _('Search') . '" alt="">' . ' ' . $title;
$date = date($_SESSION['DefaultDateFormat']);
echo '<a href="'. $rootpath . '/index.php?' . SID . "identifier=".$identifier.'">'. _('Accounts'). '>></a>';
$date = date($_SESSION['DefaultDateFormat']);
 ?>
 <link rel="stylesheet" href="css/jquery.ui.all.css">
<script src="js/jquery-1.5.1.js"></script>
<script src="ui/jquery.ui.core.js"></script>
<script src="ui/jquery.ui.widget.js"></script>
<script src="ui/jquery.ui.datepicker.js"></script>
<script type="text/javascript">

 $(function() {
$(".datepicker").datepicker({ dateFormat: 'dd/mm/yy' ,changeMonth: true,
		changeYear: true,
		yearRange: '-80:+20'});
});

function update_amount(id)
{
document.getElementById("item_total_rate_"+id).value= (document.getElementById("reciveqty_"+id).value) * (document.getElementById("itemprice_"+id).value);
 }
 function update_basic()
{
var  itemamount = 0;
   var table = document.getElementById("totalrow").value;
   for(i=1; i<=table; i++)
	{
 		var totalrate = document.getElementById('itemprice_'+i).value;
 			itemamount  =parseInt(itemamount)+  parseInt(totalrate);
			 
			document.getElementById('total_Item_Amount').value = itemamount;
 	}
	
}
 
function showitem_details(str)
{
 
if (str=="")
  {
  document.getElementById("txt_item_details").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txt_item_details").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getitem_details.php?addmrn="+str,true);
xmlhttp.send();
}



function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function showitemdetails(str)
{

if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","bill_book.php?q="+str,true);
xmlhttp.send();
}


function showmrn(str)
{
//alert(str);
var site= document.getElementById('site').value;
var store= document.getElementById('store').value;
//alert(site);
//alert(store);

if (str=="")
  {
  document.getElementById("getmrn").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("getmrn").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","get_mrn.php?vendor="+str+"&site="+site+"&store="+store,true);
xmlhttp.send();
}





</script>
<?php

if($_REQUEST['save'])
{
$status=true;
  if($_REQUEST['site']=='000')
{
 $msg_error[]="Please select site";
 	$status=false;
}

if($_REQUEST['store']=='000')
{
 $msg_error[]="Please select store";
 	$status=false;
}
if(empty($_REQUEST['invoice']) && $_REQUEST['invoice']=='')
{
    $msg_error[]="Please Enter Invoice";
 	$status=false;
}
if($_REQUEST['vendor']=='000')
{
    $msg_error[]="Please Select vendor";
 	$status=false;
}
	 
	if($status) 
	{
	echo "<pre>";
	print_r($_REQUEST);
			$data['site']=$_REQUEST['site'];
			$data['cash_credit']=$_REQUEST['cash_credit'];
			$data['store']=$_REQUEST['store'];
			$data['bill_date']=FormatDateForSQL($_REQUEST['bill_date']);
			 $data['credit_days']=$_REQUEST['credit_days'];
			$data['invoice']=$_REQUEST['invoice'];
			$data['orderno']=$_REQUEST['orderno'];
			$data['vendor']=$_REQUEST['vendor'];
			$data['mrnqty_ids']=$_REQUEST['mrnqty_ids'];
			$data['charge_details']=$_REQUEST['charge_details'];
		  	/*$r=$s->insertRecord('purchase_billbook', $data);
			  $insert=DB_query($r,$db); 
			  if(!$insert)
			  {
			 	die("Error: ".DB_error_msg($db));
		  }*/
			$data_addmrn['mrnstatus']="closed";
			$data_addmrn['id']=$_REQUEST['mrn'];
			$cond=" id='".$_REQUEST['mrn']."'";	
			$update_mrm=$s->EditRecord('addmrn', $data_addmrn, $cond);
			/*$updateResult = DB_query($update_mrm,$db,$ErrMsg);
			if(!$updateResult)
			{
			  die("Not Updated addmrnqty".DB_error_msg($db));
			}*/
	  }
}
?>
 <div align="center"><b><font ><?php echo $title; ?></font></b></div> 
  <?php 
if(isset($msg_error))
{
?>
  
		<table width="100%" border="0"  style="color:#FF0000;font-weight:bold">
	  <tr>
 		<?php for($i=0;$i<count($msg_error);$i++)
		{	
		  if($i%2==0)	
		  echo "</tr><tr>";
 	    	echo "<td>".$msg_error[$i]."</td>" ;
		}?>
		
		</table>

	 
 <?php } ?> 
<form action="" method="post">
<table width="100%"  cellspacing="4" cellpadding="0">
  <tr>
    <td width="12%" >Site</td>
	 
	
	<td width="16%"> 
	<?php 
	$sql = "select loccode, locationname from locations";
	$result=DB_query($sql, $db);
	 ?>
   <select name="site" id="site"  class="select_purch" >
   <option value ="000">Select</option>
   <?php
   while($myrow=DB_fetch_array($result))
   { 
   		$p='';
	  	if($myrow['loccode']==$_REQUEST['site'])
	  	$p="selected";
   
   ?>
   <option value="<?php echo $myrow['loccode']; ?>" <?php echo $p; ?>> <?php echo $myrow['locationname']; ?> </option>
	<?php } ?>
	</select></td>
	
 
	 
	<td width="12%" > Date</td>
	<td width="18%" ><input type="text" name="bill_date" id="bill_date"  class="datepicker"value="<?php echo $bill_date; ?>" size="10" readonly>	 </td>
     
	<td width="15%"  >Cash/Credit	
	  <select name="cash_credit" class="select_purch">
	  <option value="cash">Cash</option>
	  <option value="credit" selected="selected">Credit</option></select></td>
  
  </tr>
   <tr>
    <td>Store </td>
    <td>
	<?php
	$sql = "select StoreCode, StoreName from storetype";
	$result=DB_query($sql, $db);
	?>
	<select name="store" id="store" class="select_purch">
	<option value="000">Select</option>
	<?php
	while($myrow=DB_fetch_array($result))
	{
	   $p='';
	   if($myrow['StoreCode']==$_REQUEST['store'])
	   $p="selected";
	?>
	<option value="<?php echo $myrow['StoreCode']; ?>" <?php echo $p; ?>><?php echo $myrow['StoreName']; ?> </option>
	<?php } ?>
	</select></td>
	
	<td>Party MRN No</td>
    <td>
	<?php 
  	  $sql = "select id, MRNNo from addmrn where mrnstatus!='closed'";
	$result=DB_query($sql, $db);
	?><div id="getmrn">
	<select name="mrn_no" id="mrn_no"  onchange="showitemdetails(this.value)" class="select_purch">
	<option value="000">Select</option>
	<?php
	while($myrow=DB_fetch_array($result))
	{
	$p='';
	  if($myrow['id']==$_REQUEST['mrn_no'])
	  $p="selected";;
	?>
 	<option value="<?php echo $myrow['id']; ?>" <?php echo $p;?>><?php echo $myrow['MRNNo']; ?></option>
	<?php } ?>
	</select></div>	</td>  
	<!-- <input type="text" name="textfield210" />-->
    <td>Credit Days
      <input type="text" name="credit_days" value="<?php  echo $_REQUEST['credit_days'];?>" /></td>
    </tr>
  
  
  <tr>
    <td>Invoice No </td>
    <td><input type="text" name="invoice" value="<?php echo $_REQUEST['invoice_no'];?>" ></td>
    <td>Order No </td>
    <td><input type="text" name="orderno"  value="<?php echo $_REQUEST['orderno'];?>"/></td>
    <td>&nbsp;</td>
    
  </tr>
  
  
  <tr>
    <td width="12%">Vendor Name </td>
    <td>
	<?php
	echo $sql = "select vendor_code, vendor_name from suppliers";
	$result=DB_query($sql, $db);
	?>
	<select name="vendor" id="vendor" onchange="showmrn(this.value)" class="select_purch">
	<option value="000">Select</option>
	<?php
	while($myrow = DB_fetch_array($result))
	{
	$p='';
	   if($myrow['vendor_code']==$_REQUEST['vendor_name'])
	   $p="selected";
	?>
	<option value="<?php echo $myrow['vendor_code']; ?>" <?php echo $p; ?>><?php echo $myrow['vendor_name']; ?></option>
	<?php } ?>
	</select>	</td> 
	
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	</tr>
	</table>
	 
  <table width="100%"  cellspacing="4" cellpadding="0">

  <tr >
    <td colspan="9" > 
	 <div id="txt_item_details"></div>
</td>
   </tr>
  <tr >
  <table width="100%" id="txtHint">
   
 </table>
  <td ></td>
  <td ></td>
  <td ></td>
  <td ></td>
  <td ></td>
  </tr>
 
			
</table>
 
 </form>
<?php
 include('includes/footer.inc');
?>
            