<?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');
									
									foreach($_REQUEST['chk'] as $key=>$value)
                           {
									//for($i=0;$i<count($_REQUEST['chk']);$i++)
									  if($_REQUEST['chk'][$key]!='')
									   {
                              
									    $groupid=$s->getField('prd_model_group','mst_itemid',$_REQUEST['itemid'][$value][$i],'id');
										$dataArr['parentid']=$last_id;
										$dataArr['mst_itemid']=$_REQUEST['itemid'][$value];
										$dataArr['modelgroup_id']=$groupid;
										$dataArr['client']=$_REQUEST['vender'][$value];
										$dataArr['so_id']=$_REQUEST['salesorder'][$value];
										$dataArr['approve_qty']=$_REQUEST['approveqty'][$value];
										$dataArr['qty']=$_REQUEST['prodqty'][$value];
										$dataArr['wastage_qty']=$_REQUEST['wastageqty'][$value];
										$dataArr['total_prd_qty']=$_REQUEST['totalprodqty'][$value];
                             
										$s->RecordInsert('prd_wo_details',$dataArr);
										
										
										//*****  checking saleorder totalqty ***********//
																				
										$sql_check=DB_query("select sum(qty) as qty1 from  prd_wo_details
										 inner join prd_wo ON  prd_wo.id=prd_wo_details.parentid
										  where mst_itemid='".$_REQUEST['itemid'][$value]."' 
										  and so_id='".$_REQUEST['salesorder'][$value]."'",$db);
										$row_check=DB_fetch_array($sql_check);
											
										if($_REQUEST['orderqty'][$value]==$row_check['qty1'])
										{
										
										$update=DB_query("update salesorder_details set wo_status=1
										  where  mst_itemid='".$_REQUEST['itemid'][$value]."' 
										  and masterId='".$_REQUEST['salesorder'][$value]."'",$db);
										}
										
										
										
										
										//**********************************************//
										
										
										
										
										}
		                            } 
		
		
		
		
		
		
		
		
	
		$s->pageLocation("index.php?route=CRM/Viewproductionorder".$rurl);
		
		}





?>
<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="Auto Generate" /></td>
	    <td width="6%">Site</td>
	    <td width="16%"><select  name="site" id="site" style="width:100%;" onchange="show_members(this.value)">
		<?php 
	$sql_site=DB_query("select * from locations where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
	while($row=DB_fetch_array($sql_site))
	{
	?>
	<option value="<?php echo $row['loccode']; ?>"><?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 date('d/m/Y'); ?>"/></td>
	    <td width="10%">Dispatch Date </td>
		<td width="19%"><input type="text" name="dispatchdate" id="dispatchdate" style="width:150px;"  class="datepicker" value="<?php echo date('d/m/Y'); ?>" /></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(client)  FROM salesorder INNER JOIN salesorder_details ON  
                      salesorder.id=salesorder_details.masterId WHERE ApprovalStatus!='1' and 
					  salesorder_details.wo_status=0",$db);
 $num_row=DB_num_rows($sql_vendor);					  
 while($row_vendor=DB_fetch_array($sql_vendor))
 {

 
  ?>
  <tr class="row1">
    <td colspan="7" class="tabledata1"><h3><?php echo $s->GetFieldValue('clientmaster',"id='".$row_vendor['client']."'",'c_name');; ?></h3></td>
    </tr>
	
  <?php 
  
   $sql_order1=DB_query("SELECT orderNo,salesorder.id as id,DeliveryDate,orderDate  FROM salesorder INNER JOIN salesorder_details ON  
                      salesorder.id=salesorder_details.masterId WHERE ApprovalStatus!='1' and salesorder_details.wo_status=0
					  and client='".$row_vendor['client']."' group by salesorder.id",$db); 
  while($row_order1=DB_fetch_array($sql_order1))
  {	
  
  
    ?>	
	
	<tr class="row1">
    <td colspan="2" class="tabledata1"><h3><?php echo $row_order1['orderNo']; ?> </h3>
     <td colspan="2" class="tabledata1"><h3>&nbsp; Order Date -<?php echo ConvertSQLDate($row_order1['orderDate']); ?></h3>
    <td colspan="3"><h3> Delivery Date-<?php echo ConvertSQLDate($row_order1['DeliveryDate']); ?></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 salesorder.id as id1 ,salesorder_details.* FROM salesorder INNER JOIN salesorder_details ON  
                      salesorder.id=salesorder_details.masterId WHERE ApprovalStatus!='1' and salesorder_details.wo_status=0
					  and client='".$row_vendor['client']."' and masterId='".$row_order1['id']."'",$db); 
  while($row_order=DB_fetch_array($sql_order))
  {		
  
        $check_productionqty=DB_query("SELECT approve_qty ,sum(qty)  FROM  prd_wo INNER JOIN prd_wo_details ON   
		 prd_wo.id=prd_wo_details.parentid WHERE so_id='".$row_order['id1']."' AND mst_itemid='".$row_order['mst_itemid']."'",$db);
         
		$last_mrn_row=DB_fetch_array($check_productionqty);

		if($last_mrn_row['approve_qty']!='' && $last_mrn_row['sum(qty)']!='' )
		{
		$pending_qty=($row_order['Approved_qty']-$last_mrn_row['sum(qty)']);
		}
		else
		{
		$pending_qty=$row_order['Approved_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; ?>)" value="<?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[<?php echo $i; ?>]" value="<?php echo $row_order['mst_itemid']; ?>"  />                      </td>
    <td class="tabledata1"><?php echo $row_order['Quantity']; ?><input type="hidden" name="orderqty[<?php echo $i; ?>]" value="<?php echo $row_order['Quantity'] ?>" /></td>
    <td class="tabledata1"><?php echo $pending_qty; ?>
	<input type="hidden" name="approveqty[<?php echo $i; ?>]" id="appqty_<?php echo $i; ?>" value="<?php echo $pending_qty; ?>" />	
	<input type="hidden" name="wasteper[<?php echo $i; ?>]" id="wasteper_<?php echo $i; ?>" value="<?php echo $was_per; ?>" />	</td>
    <td class="tabledata1"><input type="text" name="prodqty[<?php echo $i; ?>]" id="prodqty_<?php echo $i; ?>" readonly /></td>
    <td class="tabledata1"><input type="text" name="wastageqty[<?php echo $i; ?>]" id="wastageqty_<?php echo $i; ?>" readonly /></td>
    <td class="tabledata1"><input type="text" name="totalprodqty[<?php echo $i; ?>]" id="totalprodqty_<?php echo $i; ?>" 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>