<link rel="stylesheet" href="css/new_mxerp.css">
<?php
$PageSecurity = 11;
$flag=0;
unset($_SESSION['query']);
?>
<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 showVendor(str)
{
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("vendor").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","Get_vendor.php?vendorName="+str,true);
xmlhttp.send();
}
</script>
<div class="body_contain">
<div class="tab_div">

<h1>Job Work Issue Register</h1>
<form method="post" action="">

	<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr class="row1">
        <td width="10%" class="tabledata1">Branch</td>
        <td width="15%"><?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="showVendor(this.value);" style="width:100%;">
            <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="10%" class="tabledata1">Vendor</td>
        <td width="15%"><select name="vendor" id="vendor" style="width:100%;">
            <option value="">All</option>
            <?php 
		 $vendor_sql = "SELECT id,vendor_code,vendor_name FROM suppliers where id='".$_REQUEST['vendor']."'";
		$vendor_result = DB_query($vendor_sql,$db);
		while($vendor_row = DB_fetch_array($vendor_result))
		{
			$p='';
			if($_REQUEST['vendor']==$vendor_row['id'])	{
			$p="selected";}
		
		?>
            <option value="<?php echo $vendor_row['id']; ?>" <?php echo $p;?>><?php echo $vendor_row['vendor_name']; ?></option>
            <?php } ?>
          </select>        </td>
      
        <td width="10%" class="tabledata1"> From Date  : </td>
        <td width="15%"><input type="text" name="from_date" value="<?php echo $_POST['from_date']==''?ConvertSQLDate($_SESSION['financialYrStart']):$_POST['from_date'];?>" class="datepicker" style="width:100%;"></td>
     
        <td class="tabledata1"> To Date : </td>
        <td width="15%"><input type="text" name="to_date" value="<?php echo $_POST['to_date']==''?ConvertSQLDate($_SESSION['financialYrEnd']):$_POST['to_date'];?>" class="datepicker" style="width:80%;"></td>
  </tr>
  <tr class="row1">
     <td class="tabledata1">Enter Part Name Of Item</td>
    <td>
    <input type="text" name="sub_group" value="" onkeyup="showItems(this.value)" />    </td> 
     <td class="tabledata1">Item</td>
    <td colspan="5"><div id="itemid">
	<div style="overflow:auto; height:100px; width:50%; border:1px solid; " >
	<?php 
	$item_sql=DB_query("select id,StockID,item_name from item_master where CategoryID!='FG'",$db);
	$c3=0;
	while($row3=DB_fetch_array($item_sql))
	{ 
		$c='';
		if($_REQUEST['item'][$c3]==$row3['StockID'])
		{$c="checked";}
	?>
	<input type="checkbox" name="item[<?php echo $c3;?>]" id="item_<?php echo $c3;?>" value="<?php echo $row3['id'];?>" <?php echo $c;?>/><?php echo $row3['StockID']." - ".$row3['item_name'];?><br />
	<?php 	
	$c3++;
	}
	?>
	</div>
		
	</div></td>
  </tr>
  <tr class="row1">
    <td colspan="8" align="center" class="tabledata1"><p align="center">
    <input type="submit" name="view" value="View">
<!--	<input type="submit" name="print" value="Print">-->
    <input type="submit" name="cancel" value="Cancel"></p></td>
  </tr>
</table>

	
	
      
	
</form>
<table width="100%" border="1" cellspacing="0" cellpadding="3">
    
<?php 
if($_POST['view'])
{
	$fordate = $_POST['from_date'];
	$todate = $_POST['to_date'];
	$fromdate = explode("/", $fordate);
	$daydate = $fromdate['0'];
	$Monthdate = $fromdate['1'];
	$yeardate = $fromdate['2'];
	$fdate = $yeardate."-".$Monthdate."-".$daydate;
					   
	$tredate   = explode("/", $todate);
	$dayda     = $tredate['0'];
	$Monthda   = $tredate['1'];
	$yearda    = $tredate['2'];
	$toyrdate  = $yearda."-".$Monthda."-".$dayda;
	$filter = array();
	
	if(isset($_REQUEST['site']) && $_REQUEST['site'] != '')
  	$filter[]="A.site='".$_REQUEST['site']."' ";
	if(isset($_REQUEST['vendor']) && $_REQUEST['vendor'] != '')
	$filter[]="A.vendor='".$_REQUEST['vendor']."' ";
	if(isset($_REQUEST['from_date']) && $_REQUEST['from_date'] != '' && isset($_REQUEST['to_date']) && $_REQUEST['to_date'] != '')
 	$filter[]="A.returnDate between '".$fdate."' and '".$toyrdate."'";
	
	if(isset($_REQUEST['item']) && $_REQUEST['item']!=''){
	$itemString="'".implode("','",$_REQUEST['item'])."'"; 
        $filter[]="B.mst_itemid IN (".$itemString.")";
        }
	 if(!empty($filter))
	{
	$filter1 = implode(" and ",$filter);
        $sql1 ="SELECT A.vendor,A.site,A.returnDate,A.returnSlipNo,A.partyChallanNo,A.id FROM job_work_return AS A INNER JOIN job_return_details AS B ON A.id=B.masterId where CompanyId='".$_SESSION['ACGCompanyId']."' AND $filter1 GROUP BY A.id";    
	}else{
        $sql1 ="SELECT A.vendor,A.site,A.returnDate,A.returnSlipNo,A.partyChallanNo,A.id FROM job_work_return AS A INNER JOIN job_return_details AS B ON A.id=B.masterId where CompanyId='".$_SESSION['ACGCompanyId']."' GROUP BY A.id";      
        }
	$result1 = DB_query($sql1,$db);
        if(DB_num_rows($result1)>0){
            if($_REQUEST['vendor']!=''){?>
    <tr style="background-color:#FFFF33;">
     <td width="25%">
    <?php echo 'Branch:<span class="inner">'.$s->GetFieldValue('locations',"loccode='".$_REQUEST['site']."'",'locationname').'</span>'; ?></td>
	<td width="20%" colspan="5"><?php echo 'Vendor:<span class="inner">'.$s->getField('suppliers','id',$_REQUEST['vendor'],'vendor_name').'</span>'; ?></td>
	<td width="25%"><?php echo ' Date From :<span class="inner">'.$_REQUEST['from_date'].'</span>'; ?></td>
	<td width="5%" colspan="8"><?php echo 'Date To:<span class="inner">'.$_REQUEST['to_date'].'</span>'; ?></td>
      </tr>
            <?php } ?>
     <tr>
      <td class="tabledata"><strong>Return Date</strong></td>
      <td class="tabledata"><strong>Return Slip No.</strong></td>
       <?php if($_REQUEST['vendor']==''){?><td class="tabledata"><strong>Vendor</strong></td>
       <?php } ?>
      <td class="tabledata"><strong>Party Challan No. </strong></td>
      <td class="tabledata"><strong>Item Code </strong></td>
       <td class="tabledata"><strong>Item Name</strong></td>
       <td class="tabledata"><strong>Uom</strong></td>
       <td class="tabledata"><strong>Order Quantity</strong></td>
       <td class="tabledata"><strong>Received Quantity</strong></td>
       <td class="tabledata"><strong>Process</strong></td>
       <td class="tabledata"><strong>Part Code</strong></td>
       <td class="tabledata"><strong>Part Name</strong></td>
       <td class="tabledata"><strong>Unit</strong></td>
       <td class="tabledata"><strong>Issue Qty</strong></td>
        <td class="tabledata"><strong>Received Qty</strong></td>
       <td class="tabledata"><strong>Waste Qty</strong></td>
  </tr>
     <?php   while($row1=DB_fetch_array($result1))
	{?>
	<tr bgcolor="#FFFFFF"> 
      <td><?php echo ConvertSQLDate($row1['returnDate']);?></td>  
      <td><?php echo $row1['returnSlipNo'];?></td>
      <?php if($_REQUEST['vendor']==''){?> <td><?php echo $s->getField('suppliers','id',$row1['vendor'],'vendor_name');?></td><?php } ?>
       <td><?php echo $row1['partyChallanNo'];?></td>
      <td colspan="6">
       <table width="100%" border="1" cellspacing="0" cellpadding="3">
       <?php if($itemString!=''){
      // echo "select mst_itemid,quantity,process from  job_return_details where masterId='".$row1['id']."' and mst_itemid IN(".$itemString.")";
       $details_sql1=DB_query("select mst_itemid,orderQty,receivedQty,process from  job_return_details where masterId='".$row1['id']."' and mst_itemid IN(".$itemString.")",$db);
       }else{
       $details_sql1=DB_query("select mst_itemid,orderQty,receivedQty,process from  job_return_details where masterId='".$row1['id']."'",$db);  
       }
           while($details_row1=DB_fetch_array($details_sql1))
	    {
           $item_info=DB_fetch_array(DB_query("select StockID,item_name,mst_unitid from item_master where id='".$details_row1['mst_itemid']."'",$db));
           $item_unit=$s->getField('unitsofmeasure','unitid',$item_info['mst_unitid'],'unitname');
           ?>
       <tr>
       <td><?php echo $item_info['StockID'];?></td>
       <td><?php echo $item_info['item_name'];?></td>
       <td><?php echo $item_unit;?></td>
      <td><?php echo $details_row1['orderQty'];?></td>
      <td><?php echo $details_row1['receivedQty'];?></td>
      <td><?php echo $s->getField('job_work_master','id',$details_row1['process'],'jobwork_name');?></td>
      </tr>
        <?php }  ?>
      </table>
      </td>
      <td colspan="6">
       <table width="100%" border="1" cellspacing="0" cellpadding="3">
       <?php 
     
       $details_sql2=DB_query("select mst_itemid,issue_qty,qty,waste_qty from  job_return_details1 where masterId='".$row1['id']."'",$db);  
      
           while($details_row2=DB_fetch_array($details_sql2))
	    {
           $item_info2=DB_fetch_array(DB_query("select StockID,item_name,mst_unitid from item_master where id='".$details_row2['mst_itemid']."'",$db));
           $item_unit=$s->getField('unitsofmeasure','unitid',$item_info2['mst_unitid'],'unitname');
           ?>
       <tr>
       <td><?php echo $item_info2['StockID'];?></td>
       <td><?php echo $item_info2['item_name'];?></td>
       <td><?php echo $item_unit;?></td>
      <td><?php echo $details_row2['issue_qty'];?></td>
      <td><?php echo $details_row2['qty'];?></td>
       <td><?php echo $details_row2['waste_qty'];?></td>
      </tr>
        <?php }  ?>
      </table>
      </td>
    </tr>
	<?php $k++;
        } }
	else
	{?>
	<tr>
	<td colspan="16"><font color="#FF0000"><strong>No Record Found</strong></font></td>
	</tr>
	<?php }?>
	<?php if($flag==1){ ?>
	<tr>
	<td colspan="5" style="text-align:center;"><strong><a href="JwIssueRegister_export.php?item=<?php echo $_REQUEST['item']; ?>&site=<?php echo $_REQUEST['site']; ?>">Export</a></strong></td>
	</tr>
	
<?php } }?>

  </table>

  </div></div>