<?php ob_start();
$PageSecurity = 2;
include('includes/session.inc');
include('includes/SQL_CommonFunctions.inc');
include('includes/function_lib.php');

if($_REQUEST['r']!='' && $_REQUEST['s']!='')
{
$sql="Select id,challanNumber from job_work_issue where site='".$_REQUEST['s']."'
       and vendor='".$_REQUEST['r']."' and status='pending'";
	$result=DB_query($sql,$db);
?>
	<option value="0">Select</option>
	<?php 
	while($Myrow=DB_fetch_array($result))
	{?>
	<option value="<?php echo $Myrow['id'];?>"><?php echo $Myrow['challanNumber'];?></option>
	<?php 	
	}
	?>
	</select>
<?php }
if($_REQUEST['q']!='')
{
	$data1='';
	$data2='';
	$data3='';
	$data4='';
	$data5='';
	
	$sql="select * from job_work_issue where id='".$_REQUEST['q']."'";
	$res=DB_query($sql,$db);
	$row=DB_fetch_array($res);	
	$jobcode=$s->getField('job_work','id',$row['jobNumber'],'jobNumber');	
	$data1.=ConvertSQLDate($row['challanDate']);
	$data2.=ConvertSQLDate($row['jobDate']);
	$data3.=$row['partyJWNo'];
	$data4.=$jobcode;	
	// Item Details	
	
    $details_sql2="Select * from job_work_issue_details where masterId='".$_REQUEST['q']."' and status=0";
		$details_res2=DB_query($details_sql2,$db);
      $details_sql21="Select * from job_work_issue_details1 where parent_id='".$_REQUEST['q']."' and status=0";
		$details_res21=DB_query($details_sql21,$db);
	    $numrow1=DB_num_rows($details_res21);
	$data5.='<table width="100%" border="1" cellpadding="3" cellspacing="0" 
    style="border-collapse:collapse; border:#666666 solid 1px;">';
   $data5.='<tr>';
   $data5.='<td class="tabledata">Select</td>';
   $data5.='<td class="tabledata">Item Code </td>';
   $data5.='<td class="tabledata">Item Name</td>';
   $data5.='<td class="tabledata">Item Description </td>';
   $data5.='<td class="tabledata">UOM</td>';
   $data5.='<td class="tabledata">Issued Quantity</td>'; 
   $data5.='<td class="tabledata">Pending Quantity</td>';   
   $data5.='<td class="tabledata">Received Quantity</td>';
   $data5.='<td class="tabledata">Process</td>';
   $data5.='</tr>';
	$j=0;
	while($details_row2=DB_fetch_array($details_res2))
	{
	$item_sql="Select * from item_master where id ='".$details_row2['mst_itemid']."'";
	$item_result=DB_query($item_sql,$db);
	$item_row=DB_fetch_array($item_result);
	$unitname=$s->getField('unitsofmeasure','unitid',$item_row['mst_unitid'],'unitname');
   
   $check_last_mrn1="Select sum(receivedQty) from job_return_details
    inner join job_work_return on job_work_return.id=job_return_details.masterId 
	where job_work_return.challanNo='".$_REQUEST['q']."' and job_return_details.mst_itemid='".$details_row2['mst_itemid']."' ";
      
	$last_mrn_res1=DB_query($check_last_mrn1,$db);
	
	$last_mrn_row1=DB_fetch_array($last_mrn_res1);
	 $last_mrn_row1['sum(receivedQty)'];
	if($details_row2['quantity']>=$last_mrn_row1['sum(receivedQty)'])
	{
		$pending_qty=$details_row2['quantity']-($last_mrn_row1['sum(receivedQty)']);
		 
	}
	else
	{
		$pending_qty=$details_row2['quantity'];
	}
   
$data5.='<tr bgcolor="#c1c1c1">';
$data5.='<td><input type="checkbox" name="chk[]" value="'.$j.'" checked="checked"></td>';
$data5.='<td>'.$item_row['StockID'].'<input type="hidden" name="itemCode['.$j.']" 
 value="'.$details_row2['mst_itemid'].'"></td>';
$data5.='<td>'.$item_row['item_name'].'</td>';
$data5.='<td>'.$item_row['item_desc'].'</td>';
$data5.='<td>'.$unitname.'</td>';
$data5.='<td><input type="text" name="issueqty['.$j.']" autocomplete="off"  id="issueqty_'.$j.'" onkeyup="chkQty(this.value,'.$j.');" value="'.$details_row2['quantity'].'" size="15" readonly ></td>';
$data5.='<td><input type="text" value="'.$pending_qty.'" id="pending_'.$j.'" size="15" name="pending['.$j.']" readonly ></td>';
$data5.='<td><input type="text" name="receivedQty['.$j.']" autocomplete="off"  id="receivedQty_'.$j.'" value="'.$pending_qty.'" size="15" onkeyup="showqty(this.value,'.$j.')"   >';
$data5.='</td>';
$data5.='<td>';
$data5.='<select name="process['.$j.']" style="width:100%">';
$data5.='<option value="0">Select</option>';
$contractor_search="select * from job_work_master";
$contractor_res=DB_query($contractor_search,$db);
while($contractor_row=DB_fetch_array($contractor_res))
		{
if($details_row2['process']==$contractor_row['id']) { $r='selected'; } else { $r=''; }
		$data5.='<option value="'.$contractor_row['id'].'"'.$r.'>'.$contractor_row['jobwork_name'].'</option>';
		}
$data5.='</select>';
$data5.='</td>';
$data5.='</tr>';
 $j++;}
$data5.='<input type="hidden" name="countItems" id="countItems" value="'.$j.'" />';
$data5.='</table>';

 if($numrow1>0){
$data6.="<p align='left'>Raw Material Details</p>";
$data6.='<table width="100%" border="1" cellpadding="3" cellspacing="0" style="border-collapse:collapse; border:#666666 solid 1px;">';
$data6.='<tr>';
$data6.='<td class="tabledata">Select</td>';
$data6.='<td class="tabledata">Item Code </td>';
$data6.='<td class="tabledata">Item Name</td>';
$data6.='<td class="tabledata">UOM</td>';
$data6.='<td class="tabledata">Issued Quantity</td>';
$data6.='<td class="tabledata">Pending Quantity</td>';
$data6.='<td class="tabledata">Received Quantity</td>';
$data6.='<td class="tabledata">Waste Qty</td>';


$data6.='</tr>';
	$j1=0;
	while($details_row21=DB_fetch_array($details_res21))
	{
		$item_sql1="Select * from item_master where id ='".$details_row21['mst_itemid']."'";
		$item_result1=DB_query($item_sql1,$db);
		$item_row1=DB_fetch_array($item_result1);
		$unitname=$s->getField('unitsofmeasure','unitid',$item_row['mst_unitid'],'unitname');
      
      
      
      $check_last_mrn11="Select sum(qty),sum(waste_qty) from job_return_details1
    inner join job_work_return on job_work_return.id=job_return_details1.masterId 
	where job_work_return.challanNo='".$_REQUEST['q']."' and job_return_details1.mst_itemid='".$details_row21['mst_itemid']."' ";
      
	$last_mrn_res11=DB_query($check_last_mrn11,$db);
	
	$last_mrn_row11=DB_fetch_array($last_mrn_res11);
	$totalqty=$last_mrn_row11['sum(qty)']+$last_mrn_row11['sum(waste_qty)'];
	if($details_row21['qty']>=$totalqty)
	{
		$pending_qty1=$details_row21['qty']-($totalqty);
		 
	}
	else
	{
		$pending_qty1=$details_row21['qty'];
	}
      
      

$data6.='<tr bgcolor="#c1c1c1">';
$data6.='<td><input type="checkbox" name="chk1[]" value="'.$j1.'" checked="checked" ></td>';
$data6.='<td>'.$item_row1['StockID'].'<input type="hidden" name="itemCode1['.$j1.']" 
 value="'.$details_row21['mst_itemid'].'" ></td>';
$data6.='<td>'.$item_row1['item_name'].'</td>';

$data6.='<td>'.$unitname.'</td>';
$data6.='<td>';
$data6.='<input type="hidden" value="'.$details_row21['qty'].'" id="orderQty1_'.$j1.'" size="15" name="orderQty['.$j1.']">'.$details_row21['qty'].''; 
$data6.='</td>';
$data6.='<td><input type="hidden" value="'.$pending_qty1.'" id="pendingqty1_'.$j1.'" size="15" name="pendingqty1['.$j1.']">'.$pending_qty1.''; 
$data6.='</td>';
$data6.='<td><input type="text" name="receivedQty1['.$j1.']" autocomplete="off"  id="receivedQty1_'.$j1.'"  value="'.$details_row21['qty'].'" size="15"  onkeyup="showwaste(this.value,'.$j1.')" >';
$data6.='</td>';
$data6.='<td><input type="text" name="waste['.$j1.']" autocomplete="off"  id="waste_'.$j1.'"   size="15" 
   onkeyup="wastqty(this.value,'.$j1.')">';
$data6.='</td>';
$data6.='</tr>';
 $j1++;}
$data6.='</table>';
	
 }

	echo $data=$data1.'__'.$data2.'__'.$data3.'__'.$data4.'__'.$data5.'__'.$data6;
}
?>