<?php 
include('functions.php');
$s=new db_query();
global $db;
$title = _('Order For Production');

		if(isset($_POST['Save']))
		{
		
		            //---------------------------   Code generation  ------------------------------//
					
		            $prefix_sql=DB_query("Select * from prefix_tbl where prefix_name='".$title."'",$db);
					$prefix_row=DB_fetch_array($prefix_sql);
					$prefix=$prefix_row['prefix_code'];	
					$financial=substr($_SESSION['YearStart'],-2)."-".substr($_SESSION['YearEnd1'],-2);
					$sql="Select wo_no from prd_wo where site='".$_REQUEST['site']."' 
					and INSTR(`wo_no`, '$financial') > 0 order by id desc limit 0,1";
					
					$result=DB_query($sql,$db);
					$myrow=DB_fetch_array($result);
					//echo DB_num_rows($result);
					if(DB_num_rows($result)>0)
					{
					//$Reqno=$myrow['Reqno'];
					//$req_part1=substr($Reqno,-4);
					$ExplBill=explode("/",$myrow['wo_no']);
					$maxval = $ExplBill[3];
					//echo $req_part1;
					$req_final=$maxval+1;
					//echo "<br>".$req_final;
					}
					else
					{
					$req_final="1";
					}
					
					$string=$prefix."/".$_REQUEST['site']."/".$financial."/";
					$final_string=$string.str_pad($req_final,4,"0",STR_PAD_LEFT);
	
		           //---------------------------  End Code generation  ------------------------//
		
		
		
									$data['wo_no']=$final_string;
									$data['wo_date']=FormatDateForSQL($_REQUEST['wo_date']);
									$data['site']=$_REQUEST['site'];
									$data['dispatch_date']=FormatDateForSQL($_REQUEST['dispatchdate']);
									$data['CompanyId']=$_SESSION['ACGCompanyId'];
									$data['Created_by']=$_SESSION['UserID'];
									$data['Created_date']=date('Y-m-d');
									$s->RecordInsert('prd_wo',$data);			//INSERT INTO PARANT TABLE
									$last_id=DB_Last_Insert_ID($db,'prd_wo','id');
									
									
									for($i=0;$i<count($_REQUEST['itemid']);$i++)
									{
									   if($_REQUEST['chk'][$i])
									   {
									    $groupid=$s->getField('prd_model_group','mst_itemid',$_REQUEST['itemid'][$i],'id');
										$dataArr['parentid']=$last_id;
										$dataArr['mst_itemid']=$_REQUEST['itemid'][$i];
										$dataArr['modelgroup_id']=$groupid;
										$dataArr['client']=$_REQUEST['vender'];
										$dataArr['so_id']=$_REQUEST['salesorder'][$i];
										$dataArr['approve_qty']=$_REQUEST['approveqty'][$i];
										$dataArr['qty']=$_REQUEST['prodqty'][$i];
										$dataArr['wastage_qty']=$_REQUEST['wastageqty'][$i];
										$dataArr['total_prd_qty']=$_REQUEST['totalprodqty'][$i];
										$s->RecordInsert('prd_wo_details',$dataArr);
										}
		                            } 
		
		
		
		
		
		
		
		
		
		
		
		}

$sql_edit=DB_query("select * from prd_wo where id='".$_REQUEST['SID']."'",$db);
$row=DB_fetch_array($sql_edit);
extract($row);



?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1> Production W/O </h1>
<div class="centre" id="slidingDiv">
<form method="post" action="">	
<table width="100%" border="0" cellpadding="2" cellspacing="0" style="border:#999999 1px solid;">
	<tr class="row1">
	<td width="7%">W/o No </td>
		<td width="17%"><input type="text" name="wo_no" style="width:150px;" readonly="true" value="<?php echo $wo_no; ?>" /></td>
	    <td width="6%">Site</td>
	    <td width="16%"><select  name="site" id="site" style="width:100%;" onchange="show_members(this.value)">
	<option value="0">Select</option>
	<?php 
	$sql_site=DB_query("select * from locations where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
	while($row=DB_fetch_array($sql_site))
	{
	$g='';
	if($row['loccode']==$site)
	$g="selected";
	?>
	<option value="<?php echo $row['loccode']; ?>" <?php echo $g; ?> ><?php echo $row['locationname']; ?></option>
	<?php } ?>
	</select></td>
	    <td width="8%">Date</td>
	    <td width="17%"><input type="text" name="wo_date" id="wo_date" class="datepicker" style="width:150px;" value="<?php echo ConvertSQLDate($wo_date);?>" /></td>
	    <td width="10%">Dispatch Date </td>
		<td width="19%"><input type="text" name="dispatchdate" style="width:150px;"  class="datepicker" value="<?php echo ConvertSQLDate($dispatch_date);?>" /></td>
		</tr>
	
	<tr class="row1">
	<td>&nbsp;</td>
	<td colspan="7">&nbsp;</td>
  </tr>
</table>

<table width="100%" border="0" cellpadding="2" cellspacing="0" style="border:#999999 1px solid;">
 <?php 
 $i=0; 
 
 $sql_vendor=DB_query("SELECT DISTINCT(so_id)  FROM prd_wo INNER JOIN prd_wo_details ON  
                      prd_wo.id=prd_wo_details.parentid WHERE prd_wo.id='".$_REQUEST['SID']."' ",$db);
 while($row_vendor=DB_fetch_array($sql_vendor))
 { 
  $client=$s->getField('prd_wo_details','so_id',$row_vendor['so_id'],'client');
 ?>
  <tr class="row1">
    <td colspan="7" class="tabledata1"><h3><?php echo $s->GetFieldValue('clientmaster',"id='".$client."'",'c_name');; ?></h3></td>
    </tr>
	
  <?php 
  
   $sql_order1=DB_query("SELECT prd_wo.id as id1,prd_wo_details.*  FROM prd_wo INNER JOIN prd_wo_details ON  
                      prd_wo.id=prd_wo_details.parentid WHERE  so_id='".$row_vendor['so_id']."' and 
					  prd_wo.id='".$_REQUEST['SID']."' group by prd_wo.id",$db); 
 $row_order1=DB_fetch_array($sql_order1);
  {	
   //echo "<pre>";
  //print_r($row_order1);
  
  
    ?>	
	
	<tr class="row1">
    <td colspan="7" class="tabledata1"><h3><?php echo $s->getField('salesorder','id',$row_order1['so_id'],'orderNo'); ?></h3>
	</td>
    </tr>
	<?php if($i==0) { ?>
  <tr class="row1">
  <td class="tabledata">Action</td>
    <td class="tabledata">Item Name</td>
    <td class="tabledata">Qty</td>
    <td class="tabledata">Approve Qty</td>
    <td class="tabledata">Production Qty</td>
    <td class="tabledata">Wastage qty </td>
    <td class="tabledata"><span class="tabledata1">Total Production Qty</span></td>
  </tr>
  <?php } ?>
  <?php
  
 
  
  
  $sql_order=DB_query("SELECT *  FROM prd_wo INNER JOIN prd_wo_details ON  
                      prd_wo.id=prd_wo_details.parentid WHERE   parentid='".$row_order1['id1']."' and 
					  so_id='".$row_vendor['so_id']."'    ",$db); 
  while($row_order=DB_fetch_array($sql_order))
  {		
  //echo "<pre>";
 // print_r($row_order);
  
        $check_productionqty=DB_query("SELECT approve_qty,qty ,wastage_qty,total_prd_qty
		    FROM  prd_wo INNER JOIN prd_wo_details ON   
		 prd_wo.id=prd_wo_details.parentid WHERE so_id='".$row_order['so_id']."' AND mst_itemid='".$row_order['mst_itemid']."'
		 and prd_wo.id='".$_REQUEST['SID']."'",$db);
         
		$last_mrn_row=DB_fetch_array($check_productionqty);

		
		$pending_qty=$last_mrn_row['approve_qty'];
			
  
  
  
  $was_per=$s->getField('item_master','id',$row_order['mst_itemid'],'waste_per');
  //if($pending_qty!=0)
  {			  
  ?>
  <tr class="row1">
    <td class="tabledata1"><input type="checkbox" name="chk[]" id="chk_<?php echo $i; ?>"  onclick="showprod(<?php echo $i; ?>)" />
	<input type="hidden" name="salesorder[]" value="<?php echo $row_order['id1']; ?>" />
	<input type="hidden" name="vender[]" value="<?php echo $row_vendor['client']; ?>" />
	</td>
    <td class="tabledata1"><?php echo $s->getField('item_master','id',$row_order['mst_itemid'],'item_name'); ?>
	 <input type="hidden" name="itemid[]" value="<?php echo $row_order['mst_itemid']; ?>"  />                      </td>
    <td class="tabledata1"><?php echo $s->GetFieldValue("salesorder_details","masterId='".$row_order1['so_id']."' and mst_itemid='".$row_order['mst_itemid']."'",'Quantity'); ?></td>
    <td class="tabledata1"><?php echo $pending_qty; ?>
	<input type="hidden" name="approveqty[]" id="appqty_<?php echo $i; ?>" value="<?php echo $pending_qty; ?>" />	
	<input type="hidden" name="wasteper[]" id="wasteper_<?php echo $i; ?>" value="<?php echo $was_per; ?>" />	</td>
    <td class="tabledata1"><input type="text" name="prodqty[]" id="prodqty_<?php echo $i; ?>" value="<?php echo $last_mrn_row['qty']; ?>" readonly /></td>
    <td class="tabledata1"><input type="text" name="wastageqty[]" id="wastageqty_<?php echo $i; ?>" value="<?php echo $last_mrn_row['wastage_qty']; ?>"  readonly /></td>
    <td class="tabledata1"><input type="text" name="totalprodqty[]" id="totalprodqty_<?php echo $i; ?>" value="<?php echo $last_mrn_row['total_prd_qty']; ?>"  readonly /></td>
  </tr>
  
  <?php $i++; } }} } ?>
  




<tr>
<td colspan="7"><div id="FinalQuot"></div></td>
</tr>
</table>
</form>
</div>


</div>
<!--main div Close -->

<script>
function showprod(i)
{
if(document.getElementById("chk_"+i).checked==true)
{

var amt=document.getElementById("appqty_"+i).value;
document.getElementById("prodqty_"+i).value=amt;
if(document.getElementById("wasteper_"+i).value=='')
var total1=0;
else
var total1=(parseFloat(amt)*parseFloat(document.getElementById("wasteper_"+i).value))/100;

var total=parseFloat(amt)+parseFloat(total1);
document.getElementById("totalprodqty_"+i).value=total;
document.getElementById("wastageqty_"+i).value=total1;
}
else
{
var amt=document.getElementById("appqty_"+i).value;
document.getElementById("prodqty_"+i).value='';

if(document.getElementById("wasteper_"+i).value=='')
var total1=0;
else
var total1=(parseFloat(amt)*parseFloat(document.getElementById("wasteper_"+i).value))/100;

var total=parseFloat(total1)-parseFloat(amt);

document.getElementById("totalprodqty_"+i).value='';
document.getElementById("wastageqty_"+i).value='';


}
xmlHttp=GetXmlHttpObject();

			if (xmlHttp==null)
			  {
				alert ("Your browser does not support AJAX!");
				return;
			  } 
			  
		var url="Final_wo.php";
		url=url+"?FinalQuot";
		xmlHttp.onreadystatechange=NumberChanged1;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
}
 function NumberChanged1() 
	{ 
		if (xmlHttp.readyState==4)
		{ 
			
		   document.getElementById("FinalQuot").innerHTML=xmlHttp.responseText;
		}
		
	}

function GetXmlHttpObject()
  {
	var xmlHttp=null;
	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	  }
	catch (e)
	  {
	  // Internet Explorer
	  try
		{
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
	  catch (e)
		{
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	  }
	return xmlHttp;
  }  
</script>