<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();
}
</script>
<div class="body_contain">
<div class="tab_div">


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

	<table width="100%" border="1" cellspacing="0" cellpadding="3" style="border:#999999 solid 1px;">
      <tr>
        <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">
            <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 class="tabledata1">Vendor</td>
        <td><select name="vendor" id="vendor">
            <option value="">All</option>
            <?php 
		 $vendor_sql = "SELECT id,vendor_code,vendor_name FROM suppliers where CompanyId='".$_SESSION['ACGCompanyId']."' order by vendor_name";
		$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 class="tabledata1"> Date From : </td>
        <td><input type="text" name="from_date" value="<?php echo $_POST['from_date'];?>" class="datepicker"></td>

        <td class="tabledata1"> Date To : </td>
        <td><input type="text" name="to_date" value="<?php echo $_POST['to_date'];?>" class="datepicker"></td>

    <td class="tabledata1">Enter Part Name Of Item</td>
    <td>
    <input type="text" name="itemName" value="" onkeyup="showItems(this.value)" />
    </td> 
  </tr>
    </table>
	
	<table width="100%" border="1" cellspacing="0" cellpadding="3" style="border:#999999 solid 1px;">
   
  <tr>
    <td class="tabledata1">Item</td>
    <td><div id="itemid">
	<div style="overflow:auto; height:150px; width:635px; border:1px solid; " >
	<?php 
	$item_sql=DB_query("select StockID,item_name from item_master where CategoryID!='FG' and CompanyId='".$_SESSION['ACGCompanyId']."'",$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['StockID'];?>" <?php echo $c;?>/><?php echo $row3['StockID']." - ".$row3['item_name'];?><br />
	<?php 	
	$c3++;
	}
	?>
	</div>		
	</div></td>
  </tr>
</table>
      <p align="center">
    <input type="submit" name="view" value="View">
	
    <input type="submit" name="cancel" value="Cancel"></p>
	<p align="right" style="margin-right:12px;"><a href="JW_register-print.php" target="_blank">Print</a> |
            <strong><a href="jobworkregister_export.php" target="_blank">Export</a></strong>
</p>
</form>
<table width="100%" border="1" cellspacing="0" cellpadding="3">
    <tr>
          <th class="tabledata" scope="col">Sr No.</th>
          <th class="tabledata" scope="col">Job Work  No. </th>
          <th class="tabledata" scope="col">Job Work  Date </th>
	  <th class="tabledata" scope="col">Quotation  No. </th>
          <th class="tabledata" scope="col">Quotation Date</th>
	  <th class="tabledata" scope="col">Vendor</th>
	  <th class="tabledata" scope="col">Item Code</th>
          <th class="tabledata" scope="col">Item Name</th>
          <th class="tabledata" scope="col">UOM</th>
          <th class="tabledata" scope="col">Quantity</th>
    </tr>
<?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[]="job_work.site='".$_REQUEST['site']."' ";
	if(isset($_REQUEST['vendor']) && $_REQUEST['vendor'] != '')
	$filter[]="job_work.vendor='".$_REQUEST['vendor']."' ";
	if(isset($_REQUEST['from_date']) && $_REQUEST['from_date'] != '' && isset($_REQUEST['to_date']) && $_REQUEST['to_date'] != '')
 	$filter[]="job_work.jobDate between '".$fdate."' and '".$toyrdate."'  ";
	
	$itemArray=array();
	if($_REQUEST['item']!=''){
	$itemArray=$_REQUEST['item'];
	$itemString="'".implode("','",$itemArray)."'"; }
 
 	
	if($_REQUEST['item'] != '')
	$filter[]="job_work_details.mst_itemId in (".$itemString.")";
	
	if($filter != '')
	{
		/*if($_REQUEST['site']!=''){$filter1 = implode(" and ",$filter)." group by salesorder_details.masterId";}
		else
		{*/
             $filter1 = implode(" and ",$filter);//}
	}
	
	if($filter1!=''){$sql1 ="select job_work.id as mainid,job_work.*, job_work_details.*, sum(quantity)
	from job_work inner join job_work_details on(job_work.id = job_work_details.masterId) where $filter1 group by job_work_details.masterId"; }
	else{
	$sql1 = "select job_work.id as mainid,job_work.*, job_work_details.*
	from job_work inner join job_work_details on(job_work.id = job_work_details.masterId)"; }
	$_SESSION['query']=$sql1;
	//echo $sql1;
	
	$result1 = DB_query($sql1,$db);
	$k=1;
	if(DB_num_rows($result1)>0){
		$flag=1;
	while($row1=DB_fetch_array($result1))
	{
            
            ?>
<tr bgcolor="#c1c1c1">
    <td><?php echo $k.'.'; ?></td>
      <td><?php echo $row1['jobNumber'];?></td>  
      <td><?php echo ConvertSQLDate($row1['jobDate']);?></td>
	  <td><?php echo $row1['partyJWNo'];?></td>
          
      <td><?php echo ConvertSQLDate($row1['quotDate']);?></td>
      <td><?php 
	  $vendorname=$s->getField('suppliers','id',$row1['vendor'],'vendor_name');
	  echo $vendorname;?></td>
	  <td colspan="4">
              <table width="100%" border="1" cellspacing="0" cellpadding="3">
                  <?php 
                  $itemDetails="select * from job_work_details where masterId='".$row1['mainid']."'";
                  $itemDetailsExec = DB_query($itemDetails,$db);
                  while($detailsDta=DB_fetch_array($itemDetailsExec))
                {
                       $itemCode= $s->GetFieldValue('item_master',"id='".$detailsDta['mst_itemId']."'",'StockID');
                        $itemName= $s->GetFieldValue('item_master',"id='".$detailsDta['mst_itemId']."'",'item_name');
                        $unitId= $s->GetFieldValue('item_master',"id='".$detailsDta['mst_itemId']."'",'mst_unitid');
                        $unitName= $s->GetFieldValue('unitsofmeasure',"unitid='".$unitId."'",'unitname');
                  ?>
            <tr bgcolor="#ADD8E6" >
                <td><?php echo $itemCode; ?></td>
                <td><?php echo $itemName; ?></td>
                <td><?php echo $unitName; ?></td>
                <td><?php echo $detailsDta['quantity']; ?></td>
            </tr>
                <?php } ?>
              </table>
    
          </td>
    </tr>
	<?php $k++;
	} }
	else
	{?>
	<tr>
	<td colspan="5"><font color="#FF0000"><strong>No Record Found</strong></font></td>
	</tr>
	<?php 
	}?>
	<?php 
	} if($flag==1){ ?>
		
	
<?php }?>
  </table>

  </div>  </div>