<?php 
ob_start();
/* $Revision: 1.20 $ */
$PageSecurity = 2;
if (empty($_GET['identifier'])) {
			 $identifier=date('U');
		} else {
			$identifier=$_GET['identifier'];
		}
include('includes/session.inc');
$title = _('Material Receipt');
include('includes/header.inc');
echo '<a href="'. $rootpath . '/index.php?' . SID . "identifier=".$identifier.'">'. _('Main Menu'). '>></a>';
echo '<a href="'. $rootpath . '/material_receipt_list.php?' . SID . "identifier=".$identifier.'">'. _('Back To Material Receipt List'). '</a>';
?>

<script src="js/jsapi.js" type="text/javascript">
</script>
<script type="text/javascript">
google.load("jquery", "1"); 

function search_store(str)
{
//alert(str);
if (str.length==0)
  {
  document.getElementById("getstore").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("getstore").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","get_store.php?site="+str,true);
xmlhttp.send();
}

function get_voucher(str)
{
	//alert(str);
	var site_to = document.getElementById("to_site").value;
	//alert("2-"+site_to);
	if (str.length==0)
  {
  document.getElementById("voucher").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("voucher").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","search_voucher.php?receivedfrom="+str+"&issuedto="+site_to,true);
xmlhttp.send();
}

function get_voucher_date(str)
{
//alert(str);
if (str.length==0)
  {
  document.getElementById("voucherdt").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("voucherdt").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","search_voucher_date.php?voucher_no="+str,true);
xmlhttp.send();

}
</script>
<strong><h2 align="center"><?php echo $title; ?> </h2>
<p align="center">&nbsp;</p>
</strong>
<?php 
if($_REQUEST['save'])
{
	
	$cnt=$_POST['counter'];
	$receipt_sql = "insert into material_receipt(id,
												 receipt_voucher_code,
												 to_site,
												 from_site,
												 store,
												 issue_voucher_no,
												 issue_date,
												 receive_date,
												 material_issue_id
												 )
										  values('',
										  		 '".$_POST['receipt_voucher_code']."',
										  		 '".$_POST['to_site']."',
												 '".$_POST['from_site']."',
												 '".$_POST['store']."',
												 '".$_POST['voucher_no']."',
												 '".FormatDateForSQL($_POST['voucher_date'])."',
												 '".FormatDateForSQL($_POST['receive_date'])."',
												 '".$_REQUEST['issue_id']."'
												 )";
	//echo"<pre>";
	//print_r($receipt_sql); exit;
	$receipt_result=DB_query($receipt_sql,$db);
	
	$last_id=DB_Last_Insert_ID($db,'material_receipt','id');	
	
	if($last_id != '')
	{
		for($k=1; $k<$cnt; $k++)
		{
			$item_sql = "insert into material_receipt_details(id, 
															  material_receipt_id,
															  item_code,
															  item_description,
															  received_quantity,
															  store,
															  narration)
													   values('',
													   		  '".$last_id."',
															  '".$_POST['item_code'][$k]."',
															  '".$_POST['item_description'][$k]."',
															  '".$_POST['received_quantity'][$k]."',
															  '".$_POST['store'][$k]."',
															  '".$_POST['narration'][$k]."'
															  ) ";
															  
											$item_result = DB_query($item_sql,$db);
											} 
											
}

$update_issue="Update material_issue set status='1' where voucher_no='".$_POST['voucher_no']."'";
$update_result=DB_query($update_issue,$db);

prnMsg( _('Data Inserted Successfully!'),'success');
echo "<script>location.href='material_receipt_list.php'</script>";
}
?>
<form action="" method="post">
<table width="80%" border="0" cellspacing="1" cellpadding="2">
      <tr>
        <td width="56%" height="116" valign="top"><table width="97%" border="0" cellspacing="1" cellpadding="2">
          <tr>
            <td width="38%" height="36"><strong>SITE(Issued to)</strong></td>
            <td width="62%"><select name="to_site" id="to_site" onChange="search_store(this.value)">
		 <option value="000">Select Site</option>
			<?php 
			$site_sql = "Select loccode, locationname from locations";
			$site_result = DB_query($site_sql,$db);
			while($site_row = DB_fetch_array($site_result))
			{ 
				$p='';
				if($site_row['loccode'] == $_POST['to_site'])
				$p= "selected";
			?>
			<option value="<?php echo $site_row['loccode']; ?>" <?php echo $p;?>><?php echo $site_row['locationname']; ?></option>
			<?php }
			?>
			</select></td>
          </tr>
   
		 <tr><td colspan="2"><div id="getstore"><table width="100%">
		 <tr><td width="38%"><strong>RECEIVE FROM </strong></td>
		 <td width="62%"><select name="from_site" id="from_site" onchange="get_voucher(this.value)">
           <option value="000">Select Site</option>
           <?php 
			$site_sql = "Select loccode, locationname from locations";
			$site_result = DB_query($site_sql,$db);
			while($site_row = DB_fetch_array($site_result))
			{ 
				$p='';
				if($site_row['loccode'] == $_POST['from_site'])
				$p= "selected";
			?>
           <option value="<?php echo $site_row['loccode']; ?>" <?php echo $p;?>><?php echo $site_row['locationname']; ?></option>
           <?php }
			?>
         </select></td>
		 </tr>
		 
		  <tr>
            <td height="33"><strong>STORE</strong></td>
			<td>
			<select name="store" onChange="">
			<option value="000">Select Store</option>
			<?php
			$store_sql = "SELECT distinct StoreCode, StoreName FROM storetype";
			$store_result = DB_query($store_sql,$db);
			while($store_row = DB_fetch_array($store_result))
			{ 
				$p='';
				if($store_row['StoreCode'] == $_POST['store'])
				$p="selected";
			
			?>
		<option value="<?php echo $store_row['StoreCode'] ; ?>" <?php echo $p;?>><?php echo $store_row['StoreName'] ; ?></option>
			<?php }
			?>
			</select></td></tr>
			<tr>
           <td height="26"><strong> VOUCHER NO. </strong></td>
	<td><input type="text" name="receipt_voucher_code" id="receipt_voucher_code" value="<?php echo $_POST['receipt_voucher_code']; ?>" size="25" readonly="true">
	</td>
          </tr>
          </table></div></td>
		  </tr>
        </table></td>
        <td width="44%" valign="top"><table width="95%" height="82" border="0" cellpadding="2" cellspacing="1">
		  <tr>
            <td width="48%" height="26"><strong>ISSUE  NO.</strong></td>
            <td width="52%"><div id="voucher">
			  <select name="voucher_no" id="voucher_no" onchange="get_voucher_date(this.value)">
                <option value="000">Select Voucher No.</option>
				<?php 
				$voucher_sql = "Select voucher_no from material_issue where status='0' ";
				$voucher_result = DB_query($voucher_sql,$db);
				while($voucher_row = DB_fetch_array($voucher_result))
				{	
				$p='';
				if($voucher_row['issue_voucher_no'] == $_POST['voucher_no'])
				$p="selected";
				?>
      <option value="<?php echo $voucher_row['voucher_no']; ?>" <?php echo $p;?>><?php echo $voucher_row['voucher_no']; ?></option>
                <?php } ?>
              </select>
            </div></td>
          </tr>
		  <tr>
            <td height="26"><strong>ISSUE DATE</strong></td>
		    <td><div id="voucherdt">
                <input type="text" name="voucher_date" id="voucher_date" value="<?php echo $_POST['voucher_date']; ?>" size="15" readonly="true" />
                <input type="hidden" name="issue_id" id="issue_id" value="<?php echo $_POST['issue_id']; ?>" />
            </div></td>
	      </tr>
          <tr>
            <td height="26"><strong>RECEIVE DATE</strong></td>
            <td><div id="voucherdt">
        <input type="text" name="receive_date" id="receive_date" value="<?php echo date('d/m/Y'); ?>" size="15" />
            </div></td>
          </tr>
        </table></td>
      </tr>
  </table>
<p align="center"><input type="submit" name="submit" value="Receive" /></p>
</form> 
<br />

<form action="" method="post">
<?php if($_REQUEST['submit']) 
{
	//$_SESSION['to_site'] = $_POST['to_site'];
	//$_SESSION['from_site'] = $_POST['from_site'];
	//$_SESSION['store'] = $_POST['store'];
	//$_SESSION['voucher_no'] = $_POST['voucher_no'];
	//$_SESSION['voucher_date'] = $_POST['voucher_date'];
echo $SQL = "select * from material_issue inner join material_issue_details 
on(material_issue.id=material_issue_details.material_issue_id) where 
material_issue.from_site='".$_POST['from_site']."' and material_issue.to_site='".$_POST['to_site']."' and  material_issue.store='".$_POST['store']."' and material_issue.voucher_no='".$_POST['voucher_no']."' ";
$RES = DB_query($SQL,$db);
?>
<table width="100%" height="111" border="0" cellpadding="2" cellspacing="1">
      <tr>
        <td>
		<table width="90%" border="0" cellspacing="1" cellpadding="2">
		  <tr>
            <th><strong>S. NO. </strong></th>
            <th><strong>ITEM CODE </strong></th>
            <th><strong>ITEM NAME </strong></th>
            <th><strong>RECEIVED QUANTITY</strong></th>
			<th><strong>STORE</strong></th>
			<th><strong>NARRATION</strong></th>
          </tr>
		  <?php $i=1;
while($ROW = DB_fetch_array($RES))
{
?>
		  <tr>
            <td><?php echo $i; ?></td>
            <td><?php echo $ROW['item_code'];?><input type="hidden" name="item_code[<?php echo $i;?>]" id="item_code_<?php echo $i;?>" value="<?php echo $ROW['item_code']; ?>" /></td>
            <td><?php echo $ROW['item_description'];?><input type="hidden" name="item_description[<?php echo $i;?>]" id="item_description_<?php echo $i;?>" value="<?php echo $ROW['item_description']; ?>" /></td>
            <td><?php echo $ROW['quantity'];?><input type="hidden" name="received_quantity[<?php echo $i;?>]" id="received_quantity_<?php echo $i;?>" value="<?php echo $ROW['quantity']; ?>" /></td>
			<td><?php 
			$store = "select StoreName from storetype where StoreCode = '0".$ROW['store']."'  ";
			$res = DB_query($store,$db);
			$row = DB_fetch_array($res);
			echo $row['StoreName'];?><input type="hidden" name="store[<?php echo $i;?>]" id="store_<?php echo $i;?>" value="<?php echo $row['StoreName']; ?>" /></td>
			<td><?php echo $ROW['narration'];?><input type="hidden" name="narration[<?php echo $i;?>]" id="narration_<?php echo $i;?>" value="<?php echo $ROW['narration']; ?>" /></td>
          </tr>
<?php $i++; } ?>
<input type="hidden" name="counter" value="<?php echo $i;?>" />
 
		  </table>
            <p align="center">
              <input type="submit" name="save" value="Save" />
			  <input type="hidden" name="to_site" value="<?php echo $_POST['to_site']; ?>"  />
			  <input type="hidden" name="from_site" value="<?php echo $_POST['from_site']; ?>"  />
			  <input type="hidden" name="store" value="<?php echo $_POST['store']; ?>"  />
			  <input type="hidden" name="voucher_no" value="<?php echo $_POST['voucher_no']; ?>"  />
			  <input type="hidden" name="voucher_date" value="<?php echo $_POST['voucher_date']; ?>"  />
			  <input type="hidden" name="issue_id" value="<?php echo $_POST['issue_id']; ?>"  />
			  <input type="hidden" name="receipt_voucher_code" value="<?php echo $_POST['receipt_voucher_code']; ?>"  />
			  <input type="hidden" name="receive_date" value="<?php echo $_POST['receive_date']; ?>"  />
            </p></td>
      </tr>
    </table>
	<?php  } ?>  
</form>
<strong>
<p align="center">&nbsp;</p>
</strong>


<?php
include('includes/footer.inc');
?>
<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>
$(function() {
$(".datepicker").datepicker({ dateFormat: 'dd/mm/yy' ,changeMonth: true,
		changeYear: true,
		yearRange: '-80:+20'});
});
</script>