<?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 = _('Material Return To Vendor');
?>
<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","Get_itemcode.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("vendor").innerHTML=data[2];
	  }
  }
  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">

<form method="post" action="">

  <h1>Material Return To Vendor</h1>

	<table width="100%" align="center" border="0" cellspacing="0" cellpadding="3">
          <tr class="row1">
            <td width="13%" align="right">Branch</td>
            <td width="20%">
			<?php	 	
        $ss1="Select loccode, locationname from locations  where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')";
	  	 $site_sql1=DB_query($ss1,$db);
	  
	  ?>			
			<select name="site" id="site" onChange="showsitename160(this.value)">
                <option value="">Select Site</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 $p;?>><?php echo $site_row['locationname']; ?></option>
                <?php } ?>
              </select>
            </td>
	          <td width="13%" align="right">From Date</td>
            <td width="20%"><input type="text" name="from_date" id="fromdate" class="datepicker" value="<?php echo ConvertSQLDate($_SESSION['financialYrStart']);?>"  onchange="javascript:checkdate(this.value)" /></td>
	          <td width="13%" align="right">To Date</td>
            <td><input type="text" name="to_date" id="todate" value="<?php echo ConvertSQLDate($_SESSION['financialYrEnd']);?>"  onchange="javascript:checkdate(this.value)" class="datepicker" /><input type="hidden" value="<?php echo ConvertSQLDate($_SESSION['financialYrStart']);?>" id='from'><input type="hidden" value="<?php echo ConvertSQLDate($_SESSION['financialYrEnd']);?>" id='to'></td>
</tr>
<tr class="row1">         
    <td align="right">Vendor</td>
    <td><select name="vendor" id="vendor">
	   <option value="">Select</option>
	  <?php 
	  $vendor_sql=DB_query("Select id,vendor_code,vendor_name from suppliers where site in('".$_SESSION['AssignedBranch']."')",$db);
	  while($vendor_row=DB_fetch_array($vendor_sql)) 
	  {		 
	  ?>
	  <option value="<?php echo $vendor_row['id'];?>" <?php echo($_REQUEST['vendor']==$vendor_row['id']?'selected':'')?> ><?php echo str_replace(array("M/s ","M/S"),'',$vendor_row['vendor_name']);?></option>
	  <?php } ?>
      </select></td>
    <td align="right">Item</td>
    <td><select name="itemdata" id="itemdata">
	<option value="">Select ITEM</option>
    <?php 
	$itemSql=DB_query("Select id,StockID,item_name from item_master",$db);
	while($itemRow=DB_fetch_array($itemSql))
	{?>
    <option value="<?php echo $itemRow['id'];?>"<?php echo($_REQUEST['itemdata']==$itemRow['id']?'selected':'')?>><?php echo $itemRow['StockID']." - ".$itemRow['item_name'];?></option>
	<?php 	
	}
	?>    	
    </select></td>
<td colspan="2"></td>
</tr>
<tr class="row1">
    <td colspan="6" align="center">
                <input type="submit" name="view" value="View" class="search">
             <!--<input type="submit" name="print" value="Print">
    <input type="submit" name="cancel" value="Cancel">-->
</td>
          </tr>
  </table>		

</form>
<?php 
if($_REQUEST['view']!='')
{
 
	$fromdate=$_REQUEST['from_date'];
	$todate=$_REQUEST['to_date'];
	$itemId=$_REQUEST['itemdata'];
	$vendor=$_REQUEST['vendor'];
	
	$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];
	$sitename=$s->getField('locations','loccode',$_REQUEST['site'],'locationname');
	$filter=array();
	if($_REQUEST['vendor']!='')
	$filter[]="invm.vendor_code = '".$_REQUEST['vendor']."'";	
	if($_REQUEST['site']!='')
	$filter[]="invm.sitecode = '".$_REQUEST['site']."'";	
	if($_REQUEST['itemdata']!='')
	$filter[]="invm_det.itemid = '".$_REQUEST['itemdata']."'";	
	if($_REQUEST['from_date']!='' && $_REQUEST['to_date']!='')
	$filter[]="invm.return_date BETWEEN '$from' and '$to'";
	
	
	if($filter!='')
	{
		$filter=implode(' and ',$filter);
	}
	if($_REQUEST['site']!='' && $_REQUEST['from_date']!='' && $_REQUEST['to_date']!='')
	{
	$message="Showing Records of Branch <u><span class='fntBig'>".$sitename."</span></u> From <u><span class='fntBig'>".$from."</span></u> to <u><span class='fntBig'>".$to."</span></u>";
	}
	else if($_REQUEST['site']!='' && $_REQUEST['from_date']=='' && $_REQUEST['to_date']=='')
	{
	$message="Showing Records of Branch <u><span class='fntBig'>".$sitename."</span></u>";
	
	}
	else if($_REQUEST['site']=='' && $_REQUEST['from_date']!='' && $_REQUEST['to_date']!='')
	{
	$message="Showing Records From <u><span class='fntBig'>".$from."</span></u> to <u><span class='fntBig'>".$to."</span></u>";
	
	}
	if($filter!='')
	{	
	       $sql ="Select * from inv_material_return as invm ,  inv_material_return_details as invm_det where $filter and  invm.id=invm_det.parentid group by invm.id ";
	 }
	 
	$_SESSION['issuereport']=$sql;
	$_SESSION['mess']=$message;

?>

<p align="center"><strong><?php echo $message; ?></strong> <a href="materialtovendorPrint.php?site=<?php echo $_REQUEST['site'];?>&store_code=<?php echo $_REQUEST['store1']?>&fromdate=<?php echo $_REQUEST['from_date']?>&todate=<?php echo $_REQUEST['to_date']?>&itemId=<?php echo $_REQUEST['itemdata']?>&vendor=<?php echo $_REQUEST['vendor'];?>" target="_blank" class="search" style="float:right;">Print</a></p>

  <table width="100%" cellpadding="3" cellspacing="0" border="0" class="thborder">
    <tr>
	    <th width="4%">S. No.</th>
      <th width="6%">Date</th>
      <th width="12%">MRTV No.</th>
      <th width="16%">Vendor</th>            
      <th>Item Name</th>
      <th width="70">UOM</th>
      <th width="110" class="text-right">Return Qty.</th>
      <th width="115">Remark</th>
      <!-- <th width="97"  style="width:77px;"><strong>Total Price </strong></th>
	 <td width="102"  style="width:77px;"><strong>Amount</strong></td>
	 -->
    </tr>
    <?php 
 // $sql = "Select * from addissue";
  
  $sno=1;
  $result=DB_query($sql,$db);
  while($myrow=DB_fetch_array($result))
  {
  	
  ?>
    <tr>
	    <td><?php echo $sno; ?></td>
      <td><?php echo ConvertSQLDate($myrow['return_date']);?></td>
       <td><?php echo $myrow['MRNno']; ?></td>
      <td><?php echo $site = $s->GetFieldValue('suppliers',"id='".$myrow['vendor_code']."'",'vendor_name'); ?></td>
      <?php if($_REQUEST['site']=='') { ?>
	  <td><?php echo $site = $s->GetFieldValue('locations',"loccode='".$myrow['sitecode']."'",'locationname'); ?></td><?php } ?>
          <td colspan="4" style="padding:0;" valign="top">
          <div id="amc-tab">
          <table width="100%" border="0" cellpadding="3" cellspacing="0">
          <?php 
      if($_REQUEST['itemdata']!='')
       $subQuery=" and itemid='".$_REQUEST['itemdata']."'";

	   $details_sql="Select * from inv_material_return_details where parentId='".$myrow['parentId']."' ".$subQuery." ";
	$_SESSION['details_sql']=$details_sql;
	$details_res=DB_query($details_sql,$db);
	$issueqty[]='';
	$req_qty[]='';
	$numrow1=DB_num_rows($details_res);
	if(DB_num_rows($details_res)>0){
	while($details_row=DB_fetch_array($details_res)){?>
          <tr>
            <td><?php echo $s->getField('item_master','id',$details_row['itemid'],'item_name'); ?></td>
            <td width="70"><?php 
			 $unitname1 = $s->GetFieldValue('item_master',"id='".$details_row['itemid']."'",'mst_unitid');
			echo $unitname = $s->GetFieldValue('unitsofmeasure',"unitid='".$unitname1."'",'unitname') ; ?> </td>
            <td width="110" style="text-align:right;">
			<?php echo $details_row['ReturnQty'];
			$issueqty[]=$details_row['ReturnQty'];
		?></td>
            <td width="115"><?php echo $myrow['remarks'];
				//$req_qty[]=$details_row['returnqty'];
				?></td>
            <!--<td width="93"  style="width:70px;"><?php //echo $details_row['totalamount'];?></td>
		<td width="101"  style="width:70px;"><?php //echo $details_row['itemdesc'];?></td>-->
          </tr>
          <?php } ?>
       
          <?php } ?>
        </table>
        </div>
        </td>
    </tr>
    <?php  $sno++; } if($numrow1>0) { ?>
    <tr>
      <td><strong></strong></td>
      <?php if($_REQUEST['site']=='') { ?> <td>&nbsp;</td><?php } ?>
       <td>&nbsp;</td>
       <td><strong>&nbsp;</strong></td>
      <td><strong></strong></td>
	   <td><strong>&nbsp;</strong></td>
      <td style="text-align:right;"><strong>Total</strong></td>	  
      <td style="text-align:right;"><strong><?php echo $a[]=array_sum($issueqty)?></strong></td>
      <td><?php //echo $b[]=array_sum($req_qty);?></td>
	  </tr>
      <?php } else { ?>
	   <tr>
      <td colspan="8" align="center"><strong>No Records</strong></td>
    </tr>
	  
      <?php } ?>     
    
  </table>


<?php } ?>

<script type="text/javascript">
  function checkdate(date)
{
  var from =document.getElementById('from').value;
  var to =document.getElementById('to').value;
  
    var cdate=date.split("/");
    var sdate=from.split("/");
    var edate=to.split("/");

    cdate[1]=cdate[1]-1;
    sdate[1]=sdate[1]-1;
    edate[1]=edate[1]-1;
       
    var dateC = new Date(cdate[2], cdate[1], cdate[0]); //Year, Month, Date 
    var dateS = new Date(sdate[2], sdate[1], sdate[0]); //Year, Month, Date
      var dateE = new Date(edate[2], edate[1], edate[0]); //Year, Month, Date

      if (dateS > dateE) {
          alert("Start Date is not greather then Date End.");
          document.getElementById('fromdate').value=from;
          document.getElementById('todate').value=to;
          
      }

      if (dateC > dateE) {
          alert("Date Not greather than "+to);
         document.getElementById('fromdate').value=from;
          document.getElementById('todate').value=to;
         
      }

      if (dateC < dateS) {
          alert(" Date Not less than "+from);
          document.getElementById('fromdate').value=from;
          document.getElementById('todate').value=to;
         
      } 
  
  
}

</script>
</div>
</div>