<?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)
{
alert(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>MRN Register</h1>
<form action="" method="post">
<table width="100%" border="0" cellpadding="3" cellspacing="0">
<tr class="row1">
	<td class="tabledata1">Branch</td>
	<td><?php
		if($_SESSION['AssignedBranch']!='')
	  {	  	
  $ss1="Select loccode, locationname from locations  where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')";
	  	 $site_sql1=DB_query($ss1,$db);
	  }
	  else
	  {
		$ss1="Select loccode,locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."'";
		$site_sql1=DB_query($ss1,$db);
	  }
	  ?>
		
		
		<select name="site" id="site"  onchange="showsitename160(this.value)" style="width:150px;">
            <option value="0">Select</option>
          <?php 
				
				while($site_row = DB_fetch_array($site_sql1))
				{
					$p='';
					if($site_row['loccode'] == $_POST['site'])	
					$p="selected";
				?>
            <option value="<?php echo $site_row['loccode'];?>"<?php echo($_REQUEST['site']==$site_row['loccode']?'selected':'')?>><?php echo $site_row['locationname'];?></option>
            <?php	
	  }
	  ?>
          </select>        </td>

	<td class="tabledata1">Store</td>
    <td><div id="storedata7">
            <?php 
	   $store_sql = "SELECT id, StoreName,storecode FROM storetype where Store_Site = '".$_REQUEST['site']."'";
	   $store_result = DB_query($store_sql,$db);
	  
	  
	  ?>
            <select name="store1" 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">Vendor</td>
        <td ><?php
	  
	  $sql="select id, vendor_name from suppliers";
			$result = DB_query($sql, $db);
			?>
            <select name="vendor" id="vendor" style="width:150px;">
              <option value="000">---Select---</option>
              <?php
			while($myrow = DB_fetch_array($result))
			{ 
			$p='';
			if($myrow['id']==$_REQUEST['vendor'])
			$p="selected";
			?>
              <option  value="<?php echo $myrow['id']; ?>"<?php echo $p;?>><?php echo  str_replace(array("M/s ","M/S"),'',$myrow['vendor_name']); ?></option>
              <?php }
			?>
            </select>        </td>

	<td class="tabledata1">Item</td>
    <td id="itemid"><select name="itemdata" id="itemdata" style="width:150px;">
            <option value="00">All</option>
            <?php 
	$itemSql=DB_query("Select StockID,item_name from item_master",$db);
	while($itemRow=DB_fetch_array($itemSql))
	{?>
            <option value="<?php echo $itemRow['StockID'];?>"<?php echo($_REQUEST['itemdata']==$itemRow['StockID']?'selected':'')?>><?php echo $itemRow['StockID']." - ".$itemRow['item_name'];?></option>
            <?php 	
	}
	?>
        </select></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[]="addmrn.site = '".$_REQUEST['site']."'";
	if($_REQUEST['store1']!='0')
	$filter[]="addmrn.StoreCode = '".$_REQUEST['store1']."'";
	if($_REQUEST['fromdate']!='' && $_REQUEST['todate']!='')
	$filter[]="addmrn.MRNDate BETWEEN '$from' and '$to'";
	if($_REQUEST['vendor']!='000')
	$filter[]="addmrn.ReceivedFrom = '".$_REQUEST['vendor']."'";
	if($_REQUEST['itemdata']!='' && $_REQUEST['itemdata']!='00')
	$filter[]="addmrnqty.itemcode = '".$_REQUEST['itemdata']."'";
	if($_REQUEST['itemdata']=='0')
	{
		$filter[]="1=1";
	}
	if($filter!='')
	{
		$filter=implode(' and ',$filter);
	}
	if($filter!='')
	{
	 $sql="Select *, addmrn.id as main_id from addmrn join addmrnqty on (addmrn.id=addmrnqty.issue_id) where $filter and addmrnqty.reciveqty!='0.00' group by addmrn.id ";
	}else{
	  if($_SESSION['AssignedSites']!='0')
	  { 
	$sql="Select *, addmrn.id as main_id from addmrn join addmrnqty on (addmrn.id=addmrnqty.issue_id) where addmrnqty.reciveqty!='0.00' and  site in (".$_SESSION['AssignedSites'].") group by addmrn.id ";
	}
	else
	{
	$sql="Select *, addmrn.id as main_id from addmrn join addmrnqty on (addmrn.id=addmrnqty.issue_id) where addmrnqty.reciveqty!='0.00' group by addmrn.id ";
	}
	}
	
	$_SESSION['mrnreport']=$sql;



?>



<p align="right">
<a href="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>

<?php 

if($_REQUEST['itemdata']!=''){
?>
<table width="40%" cellpadding="5" cellspacing="2" border="1" style="border:solid 1px #666666;" align="left">
<tr style="background-color:#CCCCCC; height: 30px">
<td class="jjrs_tablehead"><strong>Item</strong></td>
<td><strong><?php echo $s-> getField('item_master','StockID',$_REQUEST['itemdata'],'item_name')." - ".$_REQUEST['itemdata'];?></strong></td>
</tr>
</table>
<table><tr height="50px"><td></td></tr></table>
<?php } ?>

<div style="width:959px; overflow:auto; margin-left:auto; margin-right:auto;">
<table width="1700px" cellpadding="0" cellspacing="0" border="1" style="border:solid 1px #666666;">
  <tr style="background-color:#CCCCCC; height: 30px">
	<td class="jjrs_tablehead"><strong>Date</strong></td>
     <td class="jjrs_tablehead"><strong>MRN No.</strong></td>
     <td class="jjrs_tablehead"><strong>Bill No.</strong></td>
	 <td class="jjrs_tablehead"><strong>PO/Direct/PR No.</strong></td>
	 <td class="jjrs_tablehead"><strong>PO Date</strong></td>
     <td class="jjrs_tablehead"><strong>Vendor</strong></td>


     <td style="width:114px;" class="jjrs_tablehead"><strong>Item Name</strong></td>
     <td style="width:49px;" class="jjrs_tablehead"><strong>UOM</strong></td>
	 <td style="width:76px;" class="jjrs_tablehead"><strong>Qty</strong></td>
     <td  style="width:77px;" class="jjrs_tablehead"><strong>Rate</strong></td>
	 <td  style="width:77px;" class="jjrs_tablehead"><strong>Amount</strong></td>
	 <td  style="width:54px;" class="jjrs_tablehead"><strong>Total Discount</strong></td>
     <td  style="width:55px;" class="jjrs_tablehead"><strong>Excise</strong></td>
	 <td  style="width:55px;" class="jjrs_tablehead"><strong>E-Cess</strong></td>
	  <td  style="width:45px;" class="jjrs_tablehead"><strong>SAH-Cess</strong></td>
	 <td  style="width:55px;" class="jjrs_tablehead"><strong>Total Excise Amt.</strong></td>


	 <td style="width:99px;" 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>Grand Total</strong></td>
  </tr>
  <?php 
 //echo $sql;
  $result=DB_query($sql,$db);
  while($myrow=DB_fetch_array($result))
  {
  ?>
  <tr bgcolor="#FFFFFF">
	<td  style="width:50px;" class="jjrs_tablehead"><?php echo ConvertSQLDate($myrow['MRNDate']);?></td>
	<td  style="width:120px;" class="jjrs_tablehead"><?php echo $myrow['MRNNo'];?></td>
	<td  style="width:60px;" class="jjrs_tablehead"><?php echo $myrow['BillNo'];?></td>
	<td  style="width:150px;" class="jjrs_tablehead">
	<?php /*?><?php 
	if($myrow['ReceivedAgainst']=='PO'){echo $s-> getField('purchorders','orderno',$myrow['PO_WO_MTO'],'PO_Orderno');}
	elseif($myrow['ReceivedAgainst']=='Without PO'){if($myrow['req_no']!='' && $myrow['req_no']!=0){echo $s->getField('materialrequisitions','requisitionno',$myrow['req_no'],'Reqno');}else{echo "Direct";}}	?><?php */
	if($myrow['ReceivedAgainst']=='PO')
	{
	echo $s-> getField('purchorders','orderno',$myrow['PO_WO_MTO'],'PO_Orderno');
	}
	elseif($myrow['ReceivedAgainst']=='Without PO')
	{
		if($myrow['req_no']!='' && $myrow['req_no']!=0)
		{
		echo $s->getField('materialrequisitions','requisitionno',$myrow['req_no'],'Reqno');
		}
		else
		{
		echo "Direct";
		}
	}
	elseif($myrow['ReceivedAgainst']=='DirectPOOrder')
	{
		echo "Direct PO";
	}
	
	
	?>
	</td>
	<td  style="width:150px;" class="jjrs_tablehead">
	<?php
	if($myrow['ReceivedAgainst']=='PO')
	{
	$podate = $s-> getField('purchorders','orderno',$myrow['PO_WO_MTO'],'orddate');
	$fromdate_brk1=explode('/',$podate);
	if($fromdate_brk1!="")
	echo $from1=$fromdate_brk1[2]."".$fromdate_brk1[1]."".$fromdate_brk1[0];
	}
	elseif($myrow['ReceivedAgainst']=='DirectPOOrder')
	{
		echo "Direct PO";
	}
	elseif($myrow['ReceivedAgainst']=='Without PO')
	{
		if($myrow['req_no']!='' && $myrow['req_no']!=0)
		{
		echo $s->getField('materialrequisitions','requisitionno',$myrow['req_no'],'prdate');
		}
		else
		{
		echo "Direct";
		}
	}
	?>
	</td>
	<td  style="width:120px;" class="jjrs_tablehead"><?php echo $s->getField('suppliers','id',$myrow['ReceivedFrom'],'vendor_name');?></td>
	<td colspan="11">
	<table width="100%" border="0" cellpadding="0" cellspacing="0">
	<?php 
	if($_REQUEST['itemdata']!='' && $_REQUEST['itemdata']!='00')
	{
		$details_sql="Select * from addmrnqty where issue_id=".$myrow['main_id']." and itemcode='".$_REQUEST['itemdata']."'";
	}
	else
	{
		$details_sql="Select * from addmrnqty where issue_id=".$myrow['main_id']." and reciveqty!='0.00'";
	}
	
	
	$details_res=DB_query($details_sql,$db);
	$tot_received='';
	$tot_rate='';
	$tot_price='';
	$tot_disc='';
	$tot_excise='';
	$tot_ecess='';
	$tot_sahcess='';
	$tot_exciseAmt='';
	$tot_Amount='';
	if(DB_num_rows($details_res)>0){
	while($details_row=DB_fetch_array($details_res)){?>
	<tr bgcolor="99CCCC">
		<td style="width:100px;" class="jjrs_tabledata1"><strong>
		<?php 
		if($myrow['ReceivedAgainst']!='DirectPOOrder')
		{
		echo $details_row['description'];
		}
		else
		{
		echo $s->GetFieldValue('item_master',"id='".$details_row['mst_itemid']."'",'item_name');
		}
		
		?></strong></td>
		<td style="width:43px;" 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['itemprice']; $tot_rate[]=$details_row['itemprice'];?></td>
		<td  style="width:70px;" class="jjrs_tabledata1"><?php echo $details_row['item_total_rate']; $tot_price[]=$details_row['item_total_rate'];?></td>
		<td  style="width:50px;" class="jjrs_tabledata1"><?php echo $x=$details_row['discount_amount']+$details_row['sp_disc_amt']; $tot_disc[]=$x;?></td>
		<td  style="width:50px;" class="jjrs_tabledata1"><?php echo $details_row['excise_val']; $tot_excise[]=$details_row['excise_val'];?></td>
		<td  style="width:50px;" class="jjrs_tabledata1"><?php echo $details_row['e_cess_val']; $tot_ecess[]=$details_row['e_cess_val'];?></td>
		<td  style="width:50px;" class="jjrs_tabledata1"><?php echo $details_row['sah_val']; $tot_sahcess[]=$details_row['sah_val'];?></td>
		<td  style="width:50px;" class="jjrs_tabledata1"><?php echo $y=$details_row['excise_val']+$details_row['e_cess_val']+$details_row['sah_val']; $tot_exciseAmt[]=$y;?></td>
		<td  style="width:90px;" class="jjrs_tabledata1"><?php echo $details_row['total_amt_with_tax']; $tot_Amount[]=$details_row['total_amt_with_tax'];?></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><?php echo $b[]=array_sum($tot_rate);?></strong></td>
		<td class="jjrs_tabledata1"><strong><?php echo $c[]=array_sum($tot_price);?></strong></td>
		<td class="jjrs_tabledata1"><strong><?php echo $d[]=array_sum($tot_disc);?></strong></td>
		<td class="jjrs_tabledata1"><strong><?php echo $e[]=array_sum($tot_excise);?></strong></td>
		<td class="jjrs_tabledata1"><strong><?php echo $f[]=array_sum($tot_ecess);?></strong></td>
		<td class="jjrs_tabledata1"><strong><?php echo $g[]=array_sum($tot_sahcess);?></strong></td>
		<td class="jjrs_tabledata1"><strong><?php echo $h[]=array_sum($tot_exciseAmt);?></strong></td>
		<td class="jjrs_tabledata1"><strong><?php echo $m[]=array_sum($tot_Amount);?></strong></td>
	</tr><?php } ?>
	</table>
	</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_charges']; $pkg_array[]=$myrow['pkg_charges'];?></td>
	<td  style="width:60px;" class="jjrs_tablehead"><?php echo $myrow['vat_cst_val']; $vat_array[]=$myrow['vat_cst_val'];?></td>
	<td class="jjrs_tablehead"><?php echo $myrow['tot_freight']; $fr_array[]=$myrow['tot_freight'];?></td>
	<td  style="width:70px;" class="jjrs_tablehead"><?php echo $myrow['grandtotal']; $gr_array[]=$myrow['grandtotal'];?></td>
  </tr>
  <?php } ?>
  </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 Discount</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>Grand Total</strong></td>
  </tr>
  <tr bgcolor="#FFFFFF">
  	<td><strong><?php if($a!='')echo array_sum($a);?></strong></td>
  	<td><strong><?php if($d!='')echo array_sum($d);?></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($gr_array!='')echo array_sum($gr_array);?></strong></td>
  </tr>
  </table> 

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