<link rel="stylesheet" href="css/new_mxerp.css">
<?php ob_start();
$PageSecurity = 2;
include('includes/SQL_CommonFunctions.inc');
$title = _('Job Work Issue');
$PageName ='Job Work Issue';
if($_REQUEST['Id']!='' && $_REQUEST['act']=='del' && $_REQUEST['jwId']!='')
{
	$deleteSql=DB_query("Delete  from job_work_issue where id='".$_REQUEST['Id']."'",$db);
	$update_sql22=DB_query("Update job_work set status='0' where id='".$_REQUEST['jwId']."'",$db);
	echo "<script>location.href='index.php?route=JobWorkIssueList&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
}	

if(isset($_POST['Update']))
{
	//print_r($_REQUEST); exit;
	
$itemarray=array();
	$itemarray=$_REQUEST['rowId'];
	$itemDetailIds=implode(",",$itemarray);
	$qty1=$_REQUEST['qty'];
	$total=0;
	for($STR=0;$STR<count($qty1);$STR++)
	{
		$total=$total+$_REQUEST['quantity'][$STR];
	}
	
	 $insert_sql="update  job_work_issue set
								site='".$_REQUEST['site']."',    
								vendor='".$_REQUEST['vendor']."',  
								remarks='".$_REQUEST['remarks']."',
								totalIssueQty='".$total."', 
								created_by='".$_SESSION['UserID']."'
								where id='".$_REQUEST['Id']."'";
							
	$insert_result=DB_query($insert_sql,$db);
	
		$qty11=$_REQUEST['qty'];
		for($str=0;$str<count($qty11);$str++)
	{
		
	 $update="update job_work_details set quantity='".$_REQUEST['quantity'][$str]."' where id='".$_REQUEST['qty'][$str]."'";
	$query=Db_query($update,$db);
	
		$new=$_REQUEST['quantity'][$str]; 
		 $old=$_REQUEST['oldquantity'][$str];
		
		
		$stock=DB_query("Select stockbalance from stock_maintenance where itemcode='".$_REQUEST['itemcode']."'",$db);
					$stock_row=DB_fetch_array($stock);
					//echo $stock_row['stockbalance']; exit;	
					if($stock_row['itemcode']=$_REQUEST['itemcode'][$str] && $stock_row['site']=$_REQUEST['site'])
					{
					 $condition1="itemcode='".$_REQUEST['itemcode'][$str]."' && site='".$_REQUEST['site']."'";
						
					 $current_bal=$stock_row['stockbalance'] + $_REQUEST['oldquantity'][$str]-$_REQUEST['quantity'][$str];
					//echo $current_bal; exit;
					//print_r($current_bal); exit;
					$balance_arr['stockbalance']=$current_bal;
					$update1=$s->EditRecord(stock_maintenance, $balance_arr, $condition1);
					$row1=DB_query($update1,$db);
					
						if(!$row1)
						{
	  						 die('Error'.DB_error());
						}
					}

		}
	}
	
?>


<?php 
if($_REQUEST['Id']==''){
?><div class="body_contain">
<div class="tab_div">

<h1>Job Work 57 F Challan List</h1>
<?php if($_SESSION['frmAdd']=='1'){?>
<a href="index.php?route=JobWorkIssue&modid=<?php echo $_REQUEST['modid']?>&frmid=<?php echo $_REQUEST['frmid']?>">ADD NEW CHALLAN</a><?php } ?>
<br /><br />
<table width="99%" border="1" cellpadding="3" cellspacing="0" style="border-collapse:collapse; border:#666666 solid 1px; margin:5px;" align="center">
  <tr>
  	<td class="tabledata2"><strong>Branch</strong></td>
    <td class="tabledata2"><strong>Challan No.</strong></td>
    <td class="tabledata2"><strong>Challan Date</strong></td>
    <td class="tabledata2"><strong>Vendor</strong></td>
    <td class="tabledata2"><strong>Job Work No.</strong></td>
    <td class="tabledata2"><strong>View</strong></td>
    <td class="tabledata2"><strong>Edit</strong></td>
    <td class="tabledata2"><strong>Delete</strong></td>
    <td class="tabledata2"><strong>Print</strong></td>
  </tr>
  <?php 
 
  $sql=DB_query("Select * from job_work_issue where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
  while($myrow=DB_fetch_array($sql))
  {
  ?>
<tr bgcolor="#dedede">
	<td><?php 
	$sitename=$s->getField('locations','loccode',$myrow['site'],'locationname');
	echo $sitename;?></td>
    <td><?php echo $myrow['challanNumber'];?></td>
    <td><?php echo ConvertSQLDate($myrow['challanDate']);?></td>
    <td><?php 
	$vendorname=$s->getField('suppliers','id',$myrow['vendor'],'vendor_name');
	echo $vendorname;?></td>
    <td><?php 
	$jobcode=$s->getField('job_work','id',$myrow['jobNumber'],'jobNumber');
	echo $jobcode;?></td>
    <td><?php if($_SESSION['frmView']=='1'){?><a href="index.php?route=JobWorkIssueList&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&Id=<?php echo $myrow['id'];?>&act=view">View</a><?php }else{echo "No Permission";} ?></td>
    <td><?php if($_SESSION['frmEdit']=='1'){?><a href="index.php?route=JobWorkIssueList&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&Id=<?php echo $myrow['id'];?>&act=Edit">Edit</a><?php }else{echo "No Permission";} ?></td>
    <td><?php if($_SESSION['frmDelete']=='1'){?><a href="index.php?route=JobWorkIssueList&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&Id=<?php echo $myrow['id'];?>&jwId=<?php echo $myrow['jobNumber'];?>&act=del" onclick="return confirm('This record will be deleted. Are you sure?')">Delete</a><?php }else{echo "No Permission";} ?></td>
  	<td><a href="returnable_jobwork_challan.php?id=<?php echo $myrow['id'];?>" target=_blank>Print</a></td>
  </tr>
  <?php } ?>
</table>

</div></div>

<?php } 
else
if($_REQUEST['Id']!='' && $_REQUEST['act']=='view')
{
  $sql="Select * from job_work_issue where id='".$_REQUEST['Id']."' and CompanyId='".$_SESSION['ACGCompanyId']."'";
$result=DB_query($sql,$db);
$myrow=DB_fetch_array($result);
?>

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

<h1>View Job Work 57 F Challan</h1>
<table width="100%" border="1" cellpadding="3" cellspacing="0" style="border-collapse:collapse; border:#666666 solid 1px;">
  <tr>
    <td class="tabledata1">Branch</td>
	<?php $sitename=$s->getField('locations','loccode',$myrow['site'],'locationname');?>
    <td><input type="text" name="textfield" value="<?php echo $sitename;?>" readonly style="width:100%" />
    </td>
    <td class="tabledata1">Job Work No.</td>
	<?php $jobcode=$s->getField('job_work','id',$myrow['jobNumber'],'jobNumber');?>
	<td id="JOBID"><input type="text" name="textfield5" value="<?php echo $jobcode;?>" readonly  style="width:100%" /></td>
  </tr>
  <tr>
    <td class="tabledata1">Vendor</td>
	<?php $vendorname=$s->getField('suppliers','vendor_code',$myrow['vendor'],'vendor_name');?>
    <td><input type="text" name="textfield2" value="<?php echo $vendorname;?>" readonly  style="width:100%" /></td>
    <td class="tabledata1">Job Work Date </td>
    <td><input type="text" name="textfield6" value="<?php echo ConvertSQLDate($myrow['jobDate']);?>" readonly  style="width:100%" /></td>
  </tr>
  <tr>
    <td class="tabledata1">Challan No. </td>
    <td><input type="text" name="textfield3" value="<?php echo $myrow['challanNumber'];?>" readonly  style="width:100%" /></td>
	<td class="tabledata1">Party Job Work No. </td>
	  <td><input type="text" name="textfield7" value="<?php echo $myrow['partyJWNo'];?>" readonly  style="width:100%" /></td>
  </tr>
  <tr>
  <td class="tabledata1">Challan Date </td>
  <td><input type="text" name="textfield4" value="<?php echo ConvertSQLDate($myrow['challanDate']);?>" readonly  style="width:100%" /></td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  </tr>
</table>

<div style="overflow:auto; width:810px; height:auto;"> 
<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">Quantity</td>
<td class="tabledata">Flexo/Offset</td>

</tr>
<?php 
	$details_sql="Select * from job_work_details where masterId='".$myrow['jobNumber']."'";
	$details_result=DB_query($details_sql,$db);
	$j=0;
	while($details_row=DB_fetch_array($details_result))
	{
		$item_sql="Select * from item_master where StockID ='".$details_row['itemCode']."'";
		$item_result=DB_query($item_sql,$db);
		$item_row=DB_fetch_array($item_result);
?>
<tr bgcolor="#c1c1c1">
<td><?php echo $details_row['itemCode'];?><input type="hidden" name="rowId[<?php echo $j;?>]" value="<?php echo $details_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['unit'],'unitname');
echo $unitname;?></td>

<td><input type="text" value="<?php echo $details_row['quantity'];?>" size="15" readonly></td>

<td><?php echo $item_row['flexo_offset'];?></td>

</tr>
<?php $j++;} ?>
</table>
</div>
<br />
<table width="100%" border="1" cellpadding="3" cellspacing="0" style="border-collapse:collapse; border:#666666 solid 1px;">
<tr>
<td class="tabledata">Remarks</td>
<td><textarea name="remarks" id="remarks" style="width:100%" readonly><?php echo $myrow['remarks'];?></textarea></td>
</tr>
</table>

</div></div>

<?php 	
} else
?>
<?php
if($_REQUEST['Id']!='' && $_REQUEST['act']=='Edit')
{
$sql="Select * from job_work_issue where id='".$_REQUEST['Id']."' and CompanyId='".$_SESSION['ACGCompanyId']."' ";
$result=DB_query($sql,$db);
$myrow=DB_fetch_array($result);
?>

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

<form action="" method="post">
<h1>Edit Job Work 57 F Challan</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><select name="site" id="site" style="width:100%" onchange="generateChallan(this.value)">
      <option value="0">Select</option>
      <?php 
	   $loc_sql=DB_query("Select loccode,locationname from locations",$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>
    <td class="tabledata1">Job Work No.</td>
	
	<td id="JOBID"><input type="text" name="textfield5" value="<?php echo $s->getField('job_work','id',$myrow['jobNumber'],'jobNumber'); ?>" readonly  style="width:100%" /></td>
  </tr>
  <tr>
    <td class="tabledata1">Vendor</td>
	  <td><select name="vendor" id="vendor" style="width:100%" onChange="JobNumberList(this.value)">
        <option name="0">Select</option>
        <?php 
	$vend_sql=DB_query("Select id,vendor_code,vendor_name from suppliers",$db);
	while($vend_row=DB_fetch_array($vend_sql))
	{
	$j='';
	if($myrow['vendor']==$vend_row['id'])
	{
	$j="selected";	
	}
		?>
        <option value="<?php echo $vend_row['id'];?>"<?php echo $j;?>><?php echo $vend_row['vendor_name'];?></option>
        <?php 	
	}
	?>
    </select></td>
    <td class="tabledata1">Job Work Date </td>
    <td><input type="text" name="textfield6" value="<?php echo ConvertSQLDate($myrow['jobDate']);?>" readonly  style="width:100%" /></td>
  </tr>
  <tr>
    <td class="tabledata1">Challan No. </td>
    <td><input type="text" name="textfield3" value="<?php echo $myrow['challanNumber'];?>" readonly  style="width:100%" /></td>
	<td class="tabledata1">Party Job Work No. </td>
	  <td><input type="text" name="textfield7" value="<?php echo $myrow['partyJWNo'];?>" readonly  style="width:100%" /></td>
  </tr>
  <tr>
  <td class="tabledata1">Challan Date </td>
  <td><input type="text" name="textfield4" value="<?php echo ConvertSQLDate($myrow['challanDate']);?>" readonly  style="width:100%" /></td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  </tr>
</table>
<br>
<div style="overflow:auto; width:810px; height:auto;"> 
<?php 
$data3.='<table width="100%" border="1" cellpadding="3" cellspacing="0" style="border-collapse:collapse; border:#666666 solid 1px;">';
$data3.='<tr>';
$data3.='<td class="tabledata">Item Code </td>';
$data3.='<td class="tabledata">Item Name </td>';
$data3.='<td class="tabledata">Item Description </td>';
$data3.='<td class="tabledata">Uom</td>';
$data3.='<td class="tabledata">Quantity</td>';
$data3.='<td class="tabledata">Process</td>';
$data3.='</tr>';
	$details_sql="Select * from job_work_details where masterId='".$myrow['jobNumber']."'";
	$details_result=DB_query($details_sql,$db);
	$j=0;
	while($details_row=DB_fetch_array($details_result))
	{
		$item_sql="Select * from item_master where id ='".$details_row['mst_itemId']."'";
		$item_result=DB_query($item_sql,$db);
		$item_row=DB_fetch_array($item_result);
		
		$unit_sql="Select * from unitsofmeasure where unitid ='".$item_row['mst_unitid']."'";
		$unit_result=DB_query($unit_sql,$db);
		$unit_row=DB_fetch_array($unit_result);
$data3.='<input type="hidden" name="itemID[]" id="itemID" value="'.$details_row['mst_itemId'].'"';
		
$data3.='<tr bgcolor="#c1c1c1">';
$data3.='<td>'.$item_row['StockID'].'</td>';
$data3.='<td>'.$item_row['item_name'].'</td>';
$data3.='<td>'.$item_row['item_desc'].'</td>';
$data3.='<td>'.$unit_row['unitname'].'</td>';
$data3.='<td><input type="text" value="'.$details_row['quantity'].'" size="15" name="quantity['.$j.']"></td>';
$data3.='<td><select name="process[]" id="process" style="width:100px" >
	<option value="0">Select</option>';
	$floor_sql=DB_query("select * from job_work_master",$db);
	while($floor_row=DB_fetch_array($floor_sql))
	{
	if($details_row['process']==$floor_row['id']) { $u='selected'; } else { $u=''; }
	$data3.='<option value="'.$floor_row['id'].'"'.$u.'>'.$floor_row['jobwork_name'].'</option>';
	}
    $data3.='</select></td>';
$data3.='</tr>';
}	
$data3.='</table>';
echo $data3;
?>
<table width="100%">
<tr>
<th>REMARKS</th>
<td><textarea name="remarks" id="remarks" style="width:100%; height:50px;" ><?php echo $myrow['remarks']; ?></textarea></td>
</tr>
</table>
<p align="center"><input type="submit" name="Update" value="Update" /></p>
</form>
</div></div>

<?php 	
}
?>


