<?php 
session_name('CAKEPHP'); session_start();
error_reporting(FALSE);
$PageSecurity = 11;
include('includes/DefinePOClass.php');
include('includes/DefineSerialItems.php');
include('includes/SQL_CommonFunctions.inc');
$title = _('MRN Register');
$date = date($_SESSION['DefaultDateFormat']);
$_SESSION['modid']=$_REQUEST['modid'];
$_SESSION['frmid']=$_REQUEST['frmid'];
$frmpermission= $px->GetFileAccess($_SESSION['modid'],$_SESSION['frmid'],$_SESSION['ACGRoleId']);
$_SESSION['frmView']=$frmpermission[3];
if($_SESSION['frmView']=='0')
{echo "<strong>You are not authorised to view this form. Contact your administrator</strong>"; exit;}
?>
<script language="javascript" type="text/javascript">
function get_itemsubgroup(str)
{
	if(str=="")
  {
  document.getElementById("sub_id").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("sub_id").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","item_subcategory.php?main="+str,true);

xmlhttp.send();
}
function get_item(str)
{
if(str=="")
  {
  document.getElementById("itemid").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("itemid").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","Get_itemcode.php?sub="+str,true);
xmlhttp.send();
}
function showItems(str)
{
    if(str=="")
  {
  document.getElementById("itemid").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("itemid").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","ajax_inventory.php?partName="+str,true);
xmlhttp.send();
}
function showsitename160(str)
{

var xmlhttp;
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)
    {
	var data=xmlhttp.responseText.split("__");	
	document.getElementById("storedata7").innerHTML=data[0];
	  }
  }
  xmlhttp.open("GET","get_sitenamedata2.php?site="+str,true);
	xmlhttp.send();

//xmlhttp.open("GET","getSort.php?s="+str+"&cat="+cat,true);
	
}

</script>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>GRN Register</h1>
<form action="" method="post">
<table width="100%" border="0" cellpadding="3" cellspacing="0">
<tr class="row1">
	<td class="tabledata1">Site</td>
	<td><table width="100%" border="0" cellspacing="0" cellpadding="0">
	<tr>
	  <td><?php
	if($_SESSION['AssignedBranch']!='')
	  {	  	
      $ss="Select loccode, locationname from locations  where CompanyId='".$_SESSION['ACGCompanyId']."'
    and loccode in('".$_SESSION['AssignedBranch']."')";
	  	 $site_sql=DB_query($ss,$db);
	  }
	  else
	  {
		$ss="Select loccode,locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."'";
		$site_sql=DB_query($ss,$db);
	  }?>
     <select name="site" onchange="getreqitemdata(this.value)" id="site" style="width:70px"    >
      <option value="0">---Select---</option>
      <?php
	while($site_row=DB_fetch_array($site_sql))
	{
	?>
      <option value="<?php echo $site_row['loccode'];?>"<?php echo ($site==$site_row['loccode']?'selected':'')?>><?php echo $site_row['loccode'];?></option>
      <?php } ?>
    </select></td>
	  <td><div id="txtsite">
	    <?php
	
	$site_sql="Select locationname from locations where loccode ='".$site."'";
	$site_res=DB_query($site_sql,$db);
	$site_row=DB_fetch_array($site_res);
	if(isset($site))
	{
	?>
	    <input name="sitename" type="text" id="sitename"  value="<?php echo $site_row['locationname'];?>" size="20" readonly="true">
	<?php } else {?>
	<input name="sitename" type="text" id="sitename"  value="<?php echo $sitename;?>" size="20" readonly="true">
	<?php } ?>
	<input type="hidden" name="hidden_site" id="hidden_site" value="<?php echo $_REQUEST['hidden_site'];?>"/></div></td>
	</tr>
	<tr></tr>
	</table>
	</td>
	<td class="tabledata1">Store</td>
    <td><div id="storedata">
            <?php 
	   $store_sql = "SELECT id, StoreName,storecode FROM storetype where CompanyId='".$_SESSION['ACGCompanyId']."'";
	   $store_result = DB_query($store_sql,$db);
	  
	  
	  ?>
            <select name="storecode" style="width:150px;">
              <option value="0">Select</option>
              <?php
	  while($store_row = DB_fetch_array($store_result))
			{
			
	  ?>
              <option value="<?php echo $store_row['id'];?>"<?php echo($_REQUEST['store1']==$store_row['id']?'selected':'')?>><?php echo $store_row['StoreName'];?></option>
              <?php }?>
            </select>
        </div></td>
</tr>
<tr class="row1">
     <td class="tabledata1">Date</td>
        <td><input type="text" name="fromdate" id="fromdate" class="datepicker" value="<?php  echo $_REQUEST['fromdate']?>" style="width:150px;"/></td>
     <td class="tabledata1">To</td>
        <td><input type="text" name="todate" id="todate" class="datepicker" value="<?php  echo $_REQUEST['todate']?>" style="width:150px;"/></td>
</tr>
<tr class="row1">
	<td class="tabledata1" style="text-align:center;" colspan="4"><input type="submit" name="submit" value="Search" id="view" class="search"  /></td>
</tr>
</table>
</form>
<?php 
if($_REQUEST['submit']!='')
{
	$fromdate=$_REQUEST['fromdate'];
	$todate=$_REQUEST['todate'];
	
	$fromdate_brk=explode('/',$fromdate);
	$from=$fromdate_brk[2]."-".$fromdate_brk[1]."-".$fromdate_brk[0];
	$todate_brk=explode('/',$todate);
	$to=$todate_brk[2]."-".$todate_brk[1]."-".$todate_brk[0];
	
	$filter=array();
	if($_REQUEST['site']!='0')
	$filter[]="log_add_mrn.site = '".$_REQUEST['site']."'";
	if($_REQUEST['storecode']!='0')
	$filter[]="log_add_mrn.store = '".$_REQUEST['storecode']."'";
	if($_REQUEST['fromdate']!='' && $_REQUEST['todate']!='')
	$filter[]="log_add_mrn.grn_date BETWEEN '$from' and '$to'";
	if($filter!='')
	{
		$filter=implode(' and ',$filter);
	}
	if($filter!='')
	{
	$sql="Select *, log_add_mrn.id as main_id from log_add_mrn join log_addmrnqty on (log_add_mrn.id=log_addmrnqty.grn_id)  where $filter and log_addmrnqty.reciveqty!='0.00' group by log_add_mrn.id ";
	}else{
	  if($_SESSION['AssignedBranch']!='0')
	  { 
	$sql="Select *, log_add_mrn.id as main_id from log_add_mrn join log_addmrnqty on (log_add_mrn.id=log_addmrnqty.grn_id) where log_addmrnqty.reciveqty!='0.00' and  site ='".$_SESSION['AssignedBranch']."' group by log_add_mrn.id ";
	}
	else
	{
	$sql="Select *, log_add_mrn.id as main_id from log_add_mrn join log_addmrnqty on (log_add_mrn.id=log_addmrnqty.grn_id) where log_addmrnqty.reciveqty!='0.00' group by log_add_mrn.id ";
	}
	}
	
	$_SESSION['mrnreport']=$sql;



?>



<p align="right">
<!--<a href="LOG/reports/Pdf_material_receipt_report_print.php?site=<?php echo $_REQUEST['site'];?>&store_code=<?php echo $_REQUEST['store1']?>&fromdate=<?php echo $_REQUEST['fromdate']?>&todate=<?php echo $_REQUEST['todate']?>&vendor_id=<?php echo $_REQUEST['vendor']?>&itemname=<?php echo $_REQUEST['itemdata']?>" target="_blank">Print</a>
<a href="material_receipt_report_print.php?site=<?php echo $_REQUEST['site'];?>&store_code=<?php echo $_REQUEST['store1']?>&fromdate=<?php echo $_REQUEST['fromdate']?>&todate=<?php echo $_REQUEST['todate']?>&vendor_id=<?php echo $_REQUEST['vendor']?>&itemCode=<?php echo $_REQUEST['itemdata']?>" target="_blank">Print</a>-->&nbsp;&nbsp;
<!--<a href="tcpdf/Pdf_material_receipt_report_print.php?site=<?php echo $_REQUEST['site'];?>&store_code=<?php echo $_REQUEST['store1']?>&fromdate=<?php echo $_REQUEST['fromdate']?>&todate=<?php echo $_REQUEST['todate']?>&vendor_id=<?php echo $_REQUEST['vendor']?>&itemname=<?php echo $_REQUEST['itemdata']?>" target="_blank">Pdf</a>

<a href="MPDF53/Pdf_material_receipt_report_print.php?site=<?php echo $_REQUEST['site'];?>&store_code=<?php echo $_REQUEST['store1']?>&fromdate=<?php echo $_REQUEST['fromdate']?>&todate=<?php echo $_REQUEST['todate']?>&vendor_id=<?php echo $_REQUEST['vendor']?>&itemname=<?php echo $_REQUEST['itemdata']?>" target="_blank">Pdf</a>-->
</p>

<div style="width:959px; overflow:auto; margin-left:auto; margin-right:auto;">
<table width="2000px" cellpadding="0" cellspacing="0" border="1" style="border:solid 1px #666666;">
  <tr style="background-color:#CCCCCC; height: 30px">
  	<td class="jjrs_tablehead"><strong>S. No.</strong></td>
	<td class="jjrs_tablehead"><strong>ZTE invoice no./STO No.</strong></td>
	<td class="jjrs_tablehead"><strong>ZTE Invoice/STO Date.</strong></td>
	<td class="jjrs_tablehead"><strong>GRN No.</strong></td>
     <td class="jjrs_tablehead"><strong>BOE No.</strong></td>
	 <td class="jjrs_tablehead"><strong>LR No.</strong></td>
	 <td class="jjrs_tablehead"><strong>LR Date</strong></td>
     <td class="jjrs_tablehead"><strong>Vehicle No.</strong></td>
	 <td class="jjrs_tablehead"><strong>Receipt Date at Warehouse </strong></td>
	 <td class="jjrs_tablehead"><strong>Waybill No.</strong></td>
	 <td class="jjrs_tablehead"><strong>Waybill Value</strong></td>
	 <td class="jjrs_tablehead"><strong>POD Done (Y/N)</strong></td>
	 <td class="jjrs_tablehead"><strong>POD date (by BSNL)</strong></td>
	 <td class="jjrs_tablehead"><strong>POD receive date in Gurgaon office</strong></td>
	 <td class="jjrs_tablehead"><strong>POD Hand over date to Finance team</strong></td>
     <td class="jjrs_tablehead" style="width:120px;"><strong>Item Name</strong></td>
     <td class="jjrs_tablehead" style="width:70px;"><strong>UOM</strong></td>
	 <td class="jjrs_tablehead" style="width:70px;"><strong>Qty</strong></td>
     <td class="jjrs_tablehead" style="width:70px;"><strong>Rate</strong></td>
	 <td class="jjrs_tablehead" style="width:80px;"><strong>Total Price</strong></td>
     <td class="jjrs_tablehead"><strong>Excise</strong></td>
	 <td class="jjrs_tablehead"><strong>E-Cess</strong></td>
	  <td class="jjrs_tablehead"><strong>SAH-Cess</strong></td>
	 <td class="jjrs_tablehead"><strong>Total Excise Amt.</strong></td>
	 <td class="jjrs_tablehead"><strong>Taxable Amount</strong></td>
	 <td class="jjrs_tablehead"><strong>Cancel Status</strong></td>   
	 <td class="jjrs_tablehead"><strong>Pkg. Charges</strong></td>   
	 <td class="jjrs_tablehead"><strong>VAT/CST Amt.</strong></td>
	 <td class="jjrs_tablehead"><strong>Freight</strong></td>
	 <td class="jjrs_tablehead"><strong>Loading / Unloading</strong></td>
	 <td class="jjrs_tablehead"><strong>Additional Charges</strong></td>
	 <td class="jjrs_tablehead"><strong>Grand Total</strong></td>
	 <td class="jjrs_tablehead"><strong>Boxes</strong></td>
  </tr>
  <?php 
  $i=1;
  $tot_excise='';
  $tot_ecess='';
  $tot_sahcess='';
  $tot_exciseAmt='';
  $tot_Amount='';
  $result=DB_query($sql,$db);
  while($myrow=DB_fetch_array($result))
  {
  ?>
  <tr bgcolor="#FFFFFF">
  	<td style="width:10px;" class="jjrs_tablehead"><?php echo $i;?></td>
	<td style="width:120px;" class="jjrs_tablehead"><?php echo $myrow['document_no'];?></td>
	<td style="width:70px;" class="jjrs_tablehead"><?php echo ConvertSQLDate($myrow['doc_date']);?></td>
	<td style="width:120px;" class="jjrs_tablehead"><?php echo $myrow['grn_no'];?></td>
	<td style="width:60px;" class="jjrs_tablehead"><?php echo $myrow['boe_no'];?></td>
	<td style="width:100px;" class="jjrs_tablehead"><?php echo $myrow['lr_no'];?></td>
	<td style="width:70px;" class="jjrs_tablehead"><?php echo ConvertSQLDate($myrow['lr_date']);?></td>
	<td style="width:100px;" class="jjrs_tablehead"><?php echo $myrow['vehicle_no'];?></td>
	<td style="width:70px;" class="jjrs_tablehead"><?php echo ConvertSQLDate($myrow['grn_date']);?></td>
	<td style="width:70px;" class="jjrs_tablehead"><?php echo $myrow['way_bill_no'];?></td>
	<td style="width:70px;" class="jjrs_tablehead"><?php echo $myrow['way_bill_amt'];?></td>
	<td style="width:70px;" class="jjrs_tablehead"><?php echo $myrow['POD_done'];?></td>
	<td style="width:70px;" class="jjrs_tablehead"><?php echo ConvertSQLDate($myrow['POD_date']);?></td>
	<td style="width:70px;" class="jjrs_tablehead"><?php echo ConvertSQLDate($myrow['pod_receive_date']);?></td>
	<td style="width:70px;" class="jjrs_tablehead"><?php echo ConvertSQLDate($myrow['pod_head_over_date']);?></td>
	<td colspan="5">
	<table width="100%" border="1" cellpadding="0" cellspacing="0">
	<?php 
	if($_REQUEST['itemdata']!='')
	{
		$details_sql="Select * from addmrnqty where issue_id=".$myrow['main_id']." and itemcode='".$_REQUEST['itemdata']."'";
	}
	else
	{
		$details_sql="Select * from log_addmrnqty where grn_id=".$myrow['main_id']." and reciveqty!='0.00'";
	}
	
	
	$details_res=DB_query($details_sql,$db);
	$tot_received='';
	$tot_rate='';
	$tot_price='';
	if(DB_num_rows($details_res)>0){
	while($details_row=DB_fetch_array($details_res)){?>
	<tr bgcolor="99CCCC">
		<td style="width:120px;" class="jjrs_tabledata1"><?php echo $s->GetFieldValue('item_master',"id='".$details_row['mst_itemid']."'",'item_name');?></strong></td>
		<td style="width:60px;" class="jjrs_tabledata1"><?php $unit_id=$s->getField('item_master','id',$details_row['mst_itemid'],'mst_unitid'); echo $s->getField('unitsofmeasure','unitid',$unit_id,'unitname');?></td>
		<td  style="width:70px;" class="jjrs_tabledata1"><?php echo $details_row['reciveqty'];$tot_received[]=$details_row['reciveqty'];?></td>
		<td  style="width:70px;" class="jjrs_tabledata1"><?php echo $details_row['rate']; $tot_rate[]=$details_row['rate'];?></td>
		<td  style="width:80px;" class="jjrs_tabledata1"><?php echo $details_row['total_price']; $tot_price[]=$details_row['total_price'];?></td>
	</tr>
	<?php } ?>
	<tr bgcolor="#e7e7e7" style="height: 25px">
	<td class="jjrs_tabledata1"><strong></strong></td>
		<td class="jjrs_tabledata1"><strong>Total</strong></td>
		<td class="jjrs_tabledata1"><strong><?php echo $a[]=array_sum($tot_received);?></strong></td>
		<td class="jjrs_tabledata1"><strong>&nbsp;</strong></td>
		<td class="jjrs_tabledata1"><strong><?php echo $c[]=array_sum($tot_price);?></strong></td>
	</tr><?php } ?>
	</table>
	</td>
	<td  style="width:50px;" class="jjrs_tabledata1"><?php echo $myrow['excise_amt']; $e[]=$myrow['excise_amt'];?></td>
		<td  style="width:50px;" class="jjrs_tabledata1"><?php echo $myrow['ecess_amt']; $f[]=$myrow['ecess_amt'];?></td>
		<td  style="width:50px;" class="jjrs_tabledata1"><?php echo $myrow['sah_amt']; $g[]=$myrow['sah_amt'];?></td>
		<td  style="width:50px;" class="jjrs_tabledata1"><?php echo $y=$myrow['excise_amt']+$myrow['ecess_amt']+$myrow['sah_amt']; $h[]=$y;?></td>
		<td  style="width:90px;" class="jjrs_tabledata1"><?php echo $myrow['total_excise_amt']; $m[]=$myrow['total_excise_amt'];?></td>
	<td  style="width:45px;" class="jjrs_tablehead"><?php echo $myrow['Cancel_Status'];?></td>
	<td style="width:45px;" class="jjrs_tablehead"><?php echo $myrow['pkg_amt']; $pkg_array[]=$myrow['pkg_amt'];?></td>
	<td  style="width:60px;" class="jjrs_tablehead"><?php echo $myrow['vat_cst_amt']; $vat_array[]=$myrow['vat_cst_amt'];?></td>
	<td class="jjrs_tablehead"><?php echo $myrow['freight']; $fr_array[]=$myrow['freight'];?></td>
	<td class="jjrs_tablehead"><?php echo $myrow['loading']; $lo_array[]=$myrow['loading'];?></td>
	<td class="jjrs_tablehead"><?php echo $myrow['additional_charge']; $ad_array[]=$myrow['additional_charge'];?></td>
	<td  style="width:70px;" class="jjrs_tablehead"><?php echo $myrow['grand_total']; $gr_array[]=$myrow['grand_total'];?></td>
	<?php $details_sql1="Select id from log_addmrnbox where grn_id=".$myrow['main_id']."";
		$details_res1=DB_query($details_sql1,$db);
		$countbox = DB_num_rows($details_res1);?>
  	<td  style="width:70px;" class="jjrs_tablehead"><?php echo $countbox; $box_array[]=$countbox;?></td>
  </tr>
  <?php $i++;} ?>
  </table>
</div>

<table width="100%" cellpadding="3" cellspacing="0" border="1" style="border:solid 1px #666666;">
  	<tr bgcolor="#CCCCCC">
  	<td><strong>Total Qty</strong></td>
  	<td><strong>Total Price</strong></td>
  	<td><strong>Total Excise</strong></td>
  	<td><strong>Total E-Cess</strong></td>
  	<td><strong>Total SAH-Cess</strong></td>
  	<td><strong>Total Excise Amt.</strong></td>
  	<td><strong>Total Taxable Amt.</strong></td>
	<td><strong>Total Pkg. Charges</strong></td>
  	<td><strong>Total VAT / CST</strong></td>
  	<td><strong>Total Freight</strong></td>
	<td><strong>Total Loading / Unloading</strong></td>
	<td><strong>Total Additional Charges</strong></td>
  	<td><strong>Grand Total</strong></td>
	<td><strong>Total Boxes</strong></td>
  </tr>
  <tr bgcolor="#FFFFFF">
  	<td><strong><?php if($a!='')echo array_sum($a);?></strong></td>
  	<td><strong><?php if($c!='')echo array_sum($c);?></strong></td>
  	<td><strong><?php if($e!='')echo array_sum($e);?></strong></td>
  	<td><strong><?php if($f!='')echo array_sum($f);?></strong></td>
  	<td><strong><?php if($g!='')echo array_sum($g);?></strong></td>
  	<td><strong><?php if($h!='')echo array_sum($h);?></strong></td>
  	<td><strong><?php if($m!='')echo array_sum($m);?></strong></td>
	<td><strong><?php if($pkg_array!='')echo array_sum($pkg_array);?></strong></td>
  	<td><strong><?php if($vat_array!='')echo array_sum($vat_array);?></strong></td>
  	<td><strong><?php if($fr_array!='')echo array_sum($fr_array);?></strong></td>
	<td><strong><?php if($lo_array!='')echo array_sum($lo_array);?></strong></td>
	<td><strong><?php if($ad_array!='')echo array_sum($ad_array);?></strong></td>
  	<td><strong><?php if($gr_array!='')echo array_sum($gr_array);?></strong></td>
	<td><strong><?php if($box_array!='')echo array_sum($box_array);?></strong></td>
  </tr>
  </table> 

  <?php  } ?>
</div>
</div>