<?php 
session_name('CAKEPHP'); session_start();
$PageSecurity = 4;
include('includes/session.inc');
include('includes/function_lib.php');
global $db;

$pendingQty = 0;
$balanceQty = 0;
$productQty = 0;
$proQty = 0;

if($_REQUEST['wotype'] ==1)
{
 $item_query = "Select MI.priQty, MI.advice_id as MASTERID, MI.id AS advid, MI.itemUnit, MI.secQty as priQty, I.id, I.item_name, I.StockID, U.unitname,U.unitid
			   from manufacturing_advice_items MI 
			   INNER JOIN item_master I ON I.id=MI.item_code 
			   LEFT JOIN unitsofmeasure U ON U.unitid=I.mst_unitid 
			   where MI.id='".$_REQUEST['itemid']."'";
}else{
 $item_query = "Select S.Quantity as priQty, S.masterId AS MASTERID, S.id AS advid, S.unit, I.id, I.item_name, I.StockID, U.unitname,U.unitid
			   from salesorder_details S 
			   INNER JOIN item_master I ON I.id=S.mst_itemid 
			   LEFT JOIN unitsofmeasure U ON U.unitid=I.mst_unitid 
			   where S.masterId='".$_REQUEST['wo_no']."'";		   
}
echo $item_query;
$item_sql=DB_query($item_query,$db);

$itemRow = DB_fetch_array($item_sql);

if($_REQUEST['wotype']==1){
	$wo_no = $s->getField('manufacturing_advice','id',$itemRow['MASTERID'],'advice_no');		   
}else{
	$wo_no = $s->getField('salesorder','id',$itemRow['MASTERID'],'OrderNo');		   
}

$job_query = "SELECT pendqty FROM job_card_item_details WHERE id=(SELECT MAX(id) FROM job_card_item_details WHERE adv_item_id='".$itemRow['advid']."' and wo_no='".$itemRow['MASTERID']."')";

$job_result = DB_query($job_query,$db);
$jobRow = DB_fetch_array($job_result);
/*
while($jobRow = DB_fetch_array($job_result)){
	$proQty += (int)$jobRow['prodqty'];
}
if($proQty==0)
	$proQty = $itemRow['priQty'];

$pendingQty = $itemRow['priQty']-$proQty;
$productQty = $proQty;*/

if($jobRow['pendqty'] !='')
	$pendingQty =  $jobRow['pendqty'];
else
	$pendingQty =  $itemRow['priQty'];
	
$work_query = "SELECT exp_pro_date,plan_date FROM ppl_planning WHERE order_id='".$_REQUEST['wo_no']."' and id IN (SELECT DISTINCT(parent_id) FROM ppl_planning_detail WHERE product='".$itemRow['id']."')";

$work_result = DB_query($work_query,$db);
$workRow = DB_fetch_array($work_result);

if(DB_num_rows($work_result)>0){
	$expdate=date("d/m/Y", strtotime($workRow['exp_pro_date']));
	$pdate=$workRow['plan_date'];
}else{
	$expdate='';
	$pdate='';
}

if($job_against_indent_Row['against_production']==0){
	 $workr_query1 = "SELECT id FROM ppl_product_wise_capacity WHERE mst_itemid='".$itemRow['id']."' AND uom='".$itemRow['unitid']."'";
	$workr_result1 = DB_query($workr_query1,$db);
	$workrRow1 = DB_fetch_array($workr_result1);
	
	$workr_query = "SELECT * FROM ppl_product_wise_capacity_process WHERE parent_id='".$workrRow1['id']."' order by sequence";
	$workr_result = DB_query($workr_query,$db);
	$i=0;
	$tdata='';
	
	while($workrRow = DB_fetch_array($workr_result))
	{
	$tdata.="<tr>";
	$tdata.="<td>".($i+1)."</td>";
	$tdata.="<td><input type='hidden' name='process[]' value='".$workrRow['process']."' /><input type='text' name='process1[]' value='".$s->GetFieldValue('production_process_master','id="'.$workrRow['process'].'"','manuf_process_name')."' readonly='true' /></td>";
	$tdata.="<td><select name='item[]'>";
	$tdata.="<option value=''>Select Item</option>";
	$workr_querym1= "select * from item_master where CompanyId='".$_SESSION['ACGCompanyId']."'";
	$workr_resultm1= DB_query($workr_querym1,$db);
	while($workrRowm1 = DB_fetch_array($workr_resultm1))
	{
		$tdata.="<option value='".$workrRowm1['id']."'>".$workrRowm1['item_name']."</option>";
	}
	$tdata.="</select></td>";
	$tdata.="<td><select name='machine[]'>";
	$tdata.="<option value=''>Select Machine</option>";
	$workr_querym = "SELECT * FROM ppl_product_wise_capacity_machines WHERE parent_id='".$workrRow['id']."' order by machine";
	$workr_resultm = DB_query($workr_querym,$db);
	while($workrRowm = DB_fetch_array($workr_resultm))
	{
		$tdata.="<option value='".$workrRowm['machine']."'>".$s->GetFieldValue('machine_center','id="'.$workrRowm['machine'].'"','name')."</option>";
	}
	$tdata.="</select></td>";
	$tdata.="<td><select name='operator[]' id='operator_".$i."' style='width:100%' >";
	$tdata.="<option value='0'>---Select----</option>";
	$oper=DB_query("select * from production_operator_master where CompanyId='".$_SESSION['ACGCompanyId']."'",$db); 
	while($oper1=DB_fetch_array($oper))
	{
	$tdata.="<option value=".$oper1['id'].">".$oper1['operator_name']."</option>";
	}
	$tdata.="</select></td>";
	$tdata.="<td><input type='text' name='exp_com_date[]' id='expcomdate_".$i."' style='width:100%;' value='' onClick=\"javascript:NewCal(this.id,'ddmmyyyy')\" />";
	$tdata.="<td><select name='status[]' id='status_".$i."' style='width:100%' >";
	$tdata.="<option value='Pending'>Pending</option>";
	$tdata.="</select></td></tr>";
	$i++;}
	$sdata='';
	//$sdata=$s->GetFieldValue('manufacturing_advice_items','id="'.$_REQUEST['itemid'].'"','size');
	$sdata1='';	
	//$sdata1=$s->GetFieldValue('size_master','id="'.$sdata.'"','size');
}else{
	$workr_query = "SELECT * FROM ppl_planning_detail WHERE product='".$itemRow['id']."' AND parent_id=(SELECT id FROM ppl_planning WHERE order_id='".$_REQUEST['wo_no']."')";
	$workr_result = DB_query($workr_query,$db);
	$i=0;
	$tdata='';
	
	while($workrRow = DB_fetch_array($workr_result))
	{
	$tdata.="<tr>";
	$tdata.="<td>".($i+1)."</td>";
	$tdata.="<td><input type='hidden' name='process[]' value='".$workrRow['process']."' /><input type='text' name='process1[]' value='".$s->GetFieldValue('production_process_master','id="'.$workrRow['process'].'"','manuf_process_name')."' /></td>";
	$tdata.="<td><select name='item[]'>";
	$tdata.="<option value=''>Select Item</option>";
	$workr_querym1= "select * from item_master where CompanyId='".$_SESSION['ACGCompanyId']."'";
	$workr_resultm1= DB_query($workr_querym1,$db);
	while($workrRowm1 = DB_fetch_array($workr_resultm1))
	{
		$tdata.="<option value='".$workrRowm1['id']."'>".$workrRowm1['item_name']."</option>";
	}
	$tdata.="</select></td>";
	$tdata.="<td><input type='hidden' name='machine[]' value='".$workrRow['machine']."' /><input type='text' name='machine1[]' value='".$s->GetFieldValue('machine_center','id="'.$workrRow['machine'].'"','name')."' /></td>";
	$tdata.="<td><select name='operator[]' id='operator' style='width:100%' >";
	$tdata.="<option value='0'>---Select----</option>";
	$oper=DB_query("select * from production_operator_master where CompanyId='".$_SESSION['ACGCompanyId']."'",$db); 
	while($oper1=DB_fetch_array($oper))
	{
	$tdata.="<option value=".$oper1['id'].">".$oper1['operator_name']."</option>";
	}
	$tdata.="</select></td>";
	$gettime=explode(":",$workrRow['no_days']);
	$addhrs=($gettime[0]*24)+$gettime[1];
	$pdate=date("Y-m-d",(strtotime($pdate)+(3600*$addhrs)));
	
	$tdata.="<td><input type='text' name='exp_com_date[]' id='expcomdate' style='width:100%;' value='".date("d/m/Y",strtotime($pdate))."' />";
	$tdata.="<td><select name='status[]' id='status' style='width:100%' >";
	$tdata.="<option value='Pending'>Pending</option>";
	$tdata.="</select></td></tr>";
	$i++;}
	$sdata='';
	//$sdata=$s->GetFieldValue('manufacturing_advice_items','id="'.$_REQUEST['itemid'].'"','size');
	$sdata1='';	
	//$sdata1=$s->GetFieldValue('size_master','id="'.$sdata.'"','size');
}	


	
echo $itemRow['id']."__".$itemRow['StockID']."__".$itemRow['item_name']."__".$itemRow['unitname']."__".$itemRow['priQty']."__".$itemRow['advid']."__".$pendingQty."__0__".$expdate."__".$tdata."__".$sdata."__".$sdata1;

?>
