<link rel="stylesheet" href="css/new_mxerp.css">
<?php ob_start();
$PageSecurity = 2;
include('includes/SQL_CommonFunctions.inc');
include('includes/DefinePOClass.php');
$title = _('Job Work Return');
$PageName ='Job Work Return';
if($_REQUEST['Update']!='')
{

	

	
               $insert1="update job_work_return set 
								 remarks='".$_REQUEST['remarks']."',
								 modified_by='".$_SESSION['UserID']."',
								 modified_date='".date('Y-m-d')."'
                         where id='".$_REQUEST['id']."'"; 
							
								
	$insertResult1=DB_query($insert1,$db); 
	$LastId=$_REQUEST['id'];
	
	if($LastId!='')
	{
		
		for($ctr=0;$ctr<count($_REQUEST['itemCode']);$ctr++)
		{
        
						
			$insert2="update job_return_details set
								  receivedQty='".$_REQUEST['receivedQty'][$ctr]."'
                          where id='".$_REQUEST['mainid'][$ctr]."'";
			$insertResult2=DB_query($insert2,$db);          
         $balqty=$_REQUEST['pending'][$ctr]-$_REQUEST['receivedQty'][$ctr];
				  
			if($balqty==0)
			{
			$update=DB_query("update job_work_issue_details set status=1 
           where masterId='".$_REQUEST['challanno']."'
            and mst_itemid='".$_REQUEST['itemCode'][$ctr]."'",$db);	
			}
         else
         {
         $update=DB_query("update job_work_issue_details set status=0 
           where masterId='".$_REQUEST['challanno']."'
            and mst_itemid='".$_REQUEST['itemCode'][$ctr]."'",$db);	
         }
         
         
         
         
          //*****************************************	Stock Summary ************************************//		 
					  
           $stock_mrnid=$s->GetFieldValue("stock_inward","against_id='".$_REQUEST['id']."' and
                       mst_itemid='".$_POST['itemCode'][$ctr]."' and type='RGPOUT'",'id');
           $stock_rate=$s->GetFieldValue("stock_inward","against_id='".$_REQUEST['id']."' and
                       mst_itemid='".$_POST['itemCode'][$ctr]."' and type='RGPOUT'",'inward_rate');
      
            $data_stock['inwardqty']=$_REQUEST['receivedQty'][$ctr];
            $data_stock['pendingqty']=$_REQUEST['receivedQty'][$ctr];
            $data_stock['inwared_value']=$stock_rate*$_REQUEST['receivedQty'][$ctr];
            $cond_stock="id='".$stock_mrnid."'";
      
           $s->RecordEdit('stock_inward',$data_stock,$cond_stock);
         
          
			
		}
      
      
      
      for($ctr1=0;$ctr1<count($_REQUEST['item1id']);$ctr1++)
		{
         
						
			 $insert2="update job_return_details1 set
								qty='".$_REQUEST['recqty'][$ctr1]."',  
								waste_qty='".$_REQUEST['waste'][$ctr1]."'
                         where id='".$_REQUEST['mainid2'][$ctr1]."'";
         
			$insertResult2=DB_query($insert2,$db); 
			
         
         $balqty1=$_REQUEST['issueqty'][$ctr1]-($_REQUEST['recqty'][$ctr1]+$_REQUEST['waste'][$ctr1]);
				  
			if($balqty1==0)
			{
			$update=DB_query("update job_work_issue_details1 set status=1 
           where parent_id='".$_REQUEST['challanno']."'
            and mst_itemId='".$_REQUEST['item1id'][$ctr1]."'",$db);	
			}
         else
         {
          $update=DB_query("update job_work_issue_details1 set status=0 
           where parent_id='".$_REQUEST['challanno']."'
            and mst_itemId='".$_REQUEST['item1id'][$ctr1]."'",$db);
         }
         
         //*****************************************	Stock Summary ************************************//		 
					  
            $stock_mrnid1=$s->GetFieldValue("stock_inward","against_id='".$_REQUEST['id']."' and
                       mst_itemid='".$_POST['item1id'][$ctr1]."' and type='RGPOUT'",'id');
           $stock_rate1=$s->GetFieldValue("stock_inward","against_id='".$_REQUEST['id']."' and
                       mst_itemid='".$_POST['item1id'][$ctr1]."' and type='RGPOUT'",'inward_rate');
      
            $data_stock1['inwardqty']=$_REQUEST['recqty'][$ctr1];
            $data_stock1['pendingqty']=$_REQUEST['recqty'][$ctr1];
            $data_stock1['inwared_value']=$stock_rate1*$_REQUEST['receivedQty'][$ctr1];
            $cond_stock1="id='".$stock_mrnid1."'";
      
           $s->RecordEdit('stock_inward',$data_stock1,$cond_stock1);
         
         
         
			
			
		}
      
      //---------------- First Child Table -----------------------//
      $col=0;
      $sql_data=DB_query("select * from job_work_issue_details
       where masterId='".$_REQUEST['challanno']."'",$db);
       $num_data=DB_num_rows($sql_data);
			 while($row_data=DB_fetch_array($sql_data))
		     { 	if($row_data['status']==1) 	{  	$col=$col+1;  }		 }
		 
      //******************* Second Child Table *******************//
           
           $col1=0;
      $sql_data1=DB_query("select * from job_work_issue_details1
       where parent_id='".$_REQUEST['challanno']."'",$db);
       $num_data1=DB_num_rows($sql_data1);
			 while($row_data1=DB_fetch_array($sql_data1))
		     { 	if($row_data1['status']==1) 	{  	$col1=$col1+1;  }		 }
		 
           
           
           
      //**********************************************************//     
      
	
	   if($col==$num_data && $col1==$num_data1 )
		   {			
	
	$updateIssueStatus=DB_query("Update job_work_issue set status='received' 
    where id='".$_REQUEST['challanno']."'",$db);
         }
         else
         {
          $updateIssueStatus=DB_query("Update job_work_issue set status='pending' 
    where id='".$_REQUEST['challanno']."'",$db);
         
         }
 }


prnMsg( _('Record Inserted Successfully'),'success');
echo "<script>location.href='index.php?route=JobWorkReturnList&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
}
?>

<div class="body_contain">
<div class="tab_div">

<div class="body_contain">
<div class="tab_div">
<?php

$sql="Select * from job_work_return where id='".$_REQUEST['id']."' and CompanyId='".$_SESSION['ACGCompanyId']."'";
	$result=DB_query($sql,$db);
	$myrow=DB_fetch_array($result);
?>
 
<form action="" method="post">
<h1>Edit Job Work Return Slip</h1>
<table width="100%" border="1" cellpadding="3" cellspacing="0" style="border-collapse:collapse; border:#666666 solid 1px;">
  <tr>
    <td class="tabledata1">Branch</td>
    <td style="width:250px;"><select name="site" id="site" style="width:100%" onchange="generateReturn(this.value)">
      
      <?php 
	  $loc_sql=DB_query("Select loccode,locationname from locations where loccode='".$myrow['site']."'",$db);
		while($loc_row=DB_fetch_array($loc_sql))
		{
		$k='';
		if($myrow['site']==$loc_row['loccode'])
		{$k="selected";}	
			?>
      <option value="<?php echo $loc_row['loccode'];?>"<?php echo $k; ?>><?php echo $loc_row['locationname'];?></option>
      <?php }
	?>
    </select></td>
   <?php 
	$challan_sql=DB_query("Select * from job_work_issue where id='".$myrow['challanNo']."'",$db);
	$challan_row=DB_fetch_array($challan_sql);
	?>
	<td class="tabledata1">Challan Date </td>
    <td><input type="text" name="challanDate" readonly value="<?php echo ConvertSQLDate($challan_row['challanDate']);?>" style="width:100%" />
    <input type="hidden" name="challanno" value="<?php echo $myrow['challanNo'];  ?>">
    </td>
 
    <td class="tabledata1">Vendor</td>
    <td>
    	<?php echo  $vendername=$s->getField('suppliers','id',$myrow['vendor'],'vendor_name'); ?>
    	
    	<input type="hidden" value="<?php echo $myrow['vendor'];?>"  name="vendor" /></td>
  </tr>
  <tr>
	  
    <td class="tabledata1">57 F Challan No. </td>
    <td><input type="text" value="<?php echo $challan_row['challanNumber'];?>"  style="width:100%" /></td>
  
	
  <td class="tabledata1">Return Slip No. </td>
  <td><input name="returnSlipPrefix" value="<?php echo $myrow['returnSlipNo'];?>"  style="width:100%" readonly/> </td>
  
  <td class="tabledata1">Job Work No.</td>
	<?php 
	$job_sql=DB_query("Select jobNumber from job_work where id='".$challan_row['jobNumber']."'",$db);
	$job_row=DB_fetch_array($job_sql);
	?>
	<td><input type="hidden" name="partyJWNo2" readonly value="<?php echo $challan_row['partyJWNo'];?>" style="width:100%" />
	<input type="text" name="jobNumber" readonly value="<?php echo $job_row['jobNumber'];?>" style="width:100%" /></td>
  </tr>
  <tr>
  <td class="tabledata1">Party Challan No. </td>
  <td><input type="text" value="<?php echo $myrow['partyChallanNo'];?>"  style="width:100%" /></td>
 
  <td class="tabledata1">Return Date </td><?php if(isset($_REQUEST['returnDate']))$returnDate=$_REQUEST['returnDate']; else $returnDate=date('d/m/Y');?>
  <td><input type="text" name="returnDate" id="returnDate" value="<?php echo $returnDate;?>" readonly  style="width:100%" /></td>
   <td class="tabledata1">Store </td>
   <?php 
   $store=$s->getFieldValue("storetype","Store_site='".$myrow['site']."' and id='".$myrow['store']."'","StoreName");
   ?>
   <td><?php echo $store; ?>
    <input type="hidden" name="store" value="<?php echo $myrow['store']; ?>"</td>
  </tr>
</table>

<div style="overflow:auto; width:1280px; height:auto;"> 
  <h1>Item Details</h1>
<table width="100%" border="1" cellpadding="3" cellspacing="0" style="border-collapse:collapse; border:#666666 solid 1px;">
<tr>
<td class="tabledata">Item Code </td>
<td class="tabledata">Item Name </td>
<td class="tabledata">Item Description </td>
<td class="tabledata">Uom</td>
<td class="tabledata">Order Quantity</td>
<td class="tabledata">Received Quantity</td>

<td class="tabledata">Flexo/Offset</td>
</tr>
<?php 
$details_sql2="Select * from job_return_details where masterId='".$_REQUEST['id']."'";
$details_res2=DB_query($details_sql2,$db);
	
	$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);
      
      
       $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.id='".$_REQUEST['id']."' 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['orderQty']-($last_mrn_row1['sum(receivedQty)']);
		 
	}
	else
	{
		$pending_qty=$details_row2['orderQty'];
	}
?>
<tr bgcolor="#c1c1c1">
<td><?php echo $item_row['StockID'];?><input type="hidden" name="itemCode[<?php echo $j;?>]" value="<?php echo $item_row['id'];?>"></td>
<td><?php echo $item_row['item_name'];?></td>
<td><?php echo $item_row['item_desc'];?></td>
<td><?php 
$unitname=$s->getField('unitsofmeasure','unitid',$item_row['mst_unitid'],'unitname');
echo $unitname;?></td>
<td>
<input type="text" value="<?php echo $details_row2['orderQty'];?>" style="width:40%" name="orderQty[<?php echo $j;?>]" readonly>
</td>
<td><input type="hidden" name="mainid[]" value="<?php echo $details_row2['id'];?>" >
 <input type="hidden" value="<?php echo $pending_qty; ?>" id="pending_<?php echo $j; ?>" size="15"
        name="pending[]" readonly >
 <input type="hidden" value="<?php echo $details_row2['receivedQty']; ?>" id="oldrec_<?php echo $j; ?>" size="15"
        name="oldrec[]" readonly >
 <input type="text" name="receivedQty[]" id="receivedQty_<?php echo $j;?>" value="<?php echo $details_row2['receivedQty'];?>" style="width:40%" onkeyup="showqty(this.value,<?php echo $j ?>)" ></td>

<td><select name="process[]" id="process" style="width:100px" >
	<option value="0">Select</option>
	<?php 
	$floor_sql=DB_query("select * from job_work_master",$db);
	while($floor_row=DB_fetch_array($floor_sql))
	{
	if($details_row2['process']==$floor_row['id']) { $t='selected'; } else { $t=''; }
	?>
	<option value="<?php echo $floor_row['id'];?>" <?php echo $t; ?>><?php echo $floor_row['jobwork_name'];?></option>
	<?php 	
	}
	?>
    </select></td>
</tr>
<?php $j++;} ?>
<input type="hidden" name="countItems" id="countItems" value="<?php echo $j;?>" />
</table>
</div>
 
 <?php
$check1=DB_query("select * from job_return_details1 where masterId='".$_REQUEST['id']."' ",$db);
$check=DB_num_rows($check1);
if($check>0) {
?>

<div style="overflow:auto; width:1280px; height:auto;"> 
 <h1>Raw Material Details</h1>

<table width="100%" border="1" cellpadding="3" cellspacing="0" style="border-collapse:collapse; border:#666666 solid 1px;">
<tr>
<td class="tabledata">Item Code </td>
<td class="tabledata">Item Name </td>
<td class="tabledata">Item Description </td>
<td class="tabledata">Uom</td>
<td class="tabledata">Issue Qty</td>
<td class="tabledata">Received Qty</td>

<td class="tabledata">Waste Qty</td>
</tr>
<?php 
$details_sql21="Select * from job_return_details1 where masterId='".$_REQUEST['id']."'";
$details_res21=DB_query($details_sql21,$db);
	
	$j1=0;
	while($details_row21=DB_fetch_array($details_res21))
	{
     $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.id='".$_REQUEST['id']."' 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['issue_qty']>=$totalqty)
	{
		$pending_qty1=$details_row21['issue_qty']-($totalqty);
		 
	}
	else
	{
		$pending_qty1=$details_row21['issue_qty'];
	}
   
   //echo $pending_qty1;
    
		$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);
?>
<tr bgcolor="#FFFFFF">
<td><?php echo $item_row['StockID'];?>
<input type="hidden" name="item1id[]" value="<?php echo $details_row21['mst_itemId'];?>">
</td>
<td><?php echo $item_row['item_name'];?><input type="hidden" name="mainid2[]" value="<?php echo $details_row21['id'];?>" >
</td>
<td><?php echo $item_row['item_desc'];?></td>
<td><?php $unitname=$s->getField('unitsofmeasure','unitid',$item_row['mst_unitid'],'unitname');
echo $unitname;?></td>
<td><?php echo $details_row21['issue_qty']; ?><input type="hidden" name="issueqty[]" value="<?php echo $details_row21['issue_qty'] ?>" id="issueqty_<?php echo $j1; ?>"></td>
<td></td<td><input type="text" name="recqty[]" value="<?php echo $details_row21['qty'] ?>" id="recqty_<?php echo $j1; ?>" onkeyup="showwaste(this.value,<?php echo $j1; ?>)">
 
 <input type="hidden" name="pendingqty1[]" value="<?php echo $pending_qty1 ?>" id="pendingqty1_<?php echo $j1; ?>">
 <input type="hidden" name="old1[]" value="<?php echo $details_row21['qty'] ?>" id="old1_<?php echo $j1; ?>">
 
</td>
<td><input type="text" name="waste[]" value="<?php echo $details_row21['waste_qty'] ?>" id="waste_<?php echo $j1; ?>"  onkeyup="wastqty(this.value,<?php echo $j1; ?>)">
<input type="hidden" name="oldwaste[]" value="<?php echo $details_row21['waste_qty'] ?>" id="oldwaste_<?php echo $j1; ?>"  >
</td>
</tr>
   <?php $j1++; } ?>
</table></div>
<?php } ?>
 
 

<table width="100%" border="1" cellpadding="3" cellspacing="0" style="border-collapse:collapse; border:#666666 solid 1px;">
<tr>
<td class="tabledata1">REMARKS</td>
<td><textarea name="remarks" id="remarks" style="width:100%"><?php echo $myrow['remarks'];?></textarea></td>
</tr>
</table>
<p align="center"><input type="submit" name="Update" value="Update"  class="search"/></p>
</form>

</div></div>
<script>
 function showqty(str,id)
{
 var pend=document.getElementById("pending_"+id).value;
 
 if(pend<str)
 {
 alert("Receiving Qty Should be less then  pending qty");
 document.getElementById("receivedQty_"+id).value='';
 document.getElementById("receivedQty_"+id).focus();
 
 return false;
 }
return true;
 
}

function showwaste(str,id)
{
 
 var pend1=document.getElementById("pendingqty1_"+id).value;
 var privious=document.getElementById("old1_"+id).value;
 var pend=parseFloat(pend1)+parseFloat(privious);
 if(pend<str)
 {
 alert("Receiving Qty Should be less then  pending qty");
 document.getElementById("recqty_"+id).value=privious;
 document.getElementById("recqty_"+id).focus();
 
 
 return false;
 }
 
return true;
 
}
function wastqty(str,id)
{
  var pend=document.getElementById("issueqty_"+id).value;
  var receive=document.getElementById("recqty_"+id).value;
  var old=document.getElementById("oldwaste_"+id).value;
          
 
  var totalpending=parseFloat(pend)-parseFloat(receive);
  if(totalpending<str)
  {
 alert("Plz check Waste Qty");
 document.getElementById("waste_"+id).value=old;
 document.getElementById("waste_"+id).focus();
  }
 
   return true;
}

function showqty(str,id)
{
 var pend=document.getElementById("pending_"+id).value;
 var oldqty=document.getElementById("oldrec_"+id).value;
 if(pend<str)
 {
 alert("Receiving Qty Should be less then  pending qty");
 document.getElementById("receivedQty_"+id).value=oldqty;
 document.getElementById("receivedQty_"+id).focus();
 
 return false;
 }
return true;
 
}
 </script>