<script language="javascript">
function validatedata()
{
	if(document.getElementById("site").value=='0')
	{
		alert("Please Select a Branch.");
		document.getElementById("site").focus();
		return false;
	}
}
</script>
<link rel="stylesheet" href="css/new_mxerp.css">
<?php extract($_POST);
$PageSecurity = 1;
$tabindex=0;
include('includes/DefinePaymentClass.php');
$title = _('Job Route Card Report');
include('includes/SQL_CommonFunctions.inc');
?>
<div class="body_contain">
	<div class="tab_div" style="width:1210px;">
   		<h1>PMS Work In Progress Report</h1>
		<table width="100%" border="0" cellspacing="0" cellpadding="0">
			<tr>
				<td>
					<div align="center">
						<form  action="" method="post">
							<table width="100%" border="0" cellpadding="3" cellspacing="0" style="border-collapse:collapse; border:#666666 solid 1px;">
								<tr class="row1">
									<td width="12%"><strong>Branch</strong></td>
									<td width="21%">
										<select name="site" id="site" style="width:150px" onchange="getproject(this.value)">
									   	<option value="0">---Select---</option>
									  	<?php 		
										$site_sql=DB_query("Select * from locations where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
										while($site_row=DB_fetch_array($site_sql)){
										?>
									  	<option value="<?php echo $site_row['loccode'];?>"<?php echo ($_REQUEST['site']==$site_row['loccode']?'selected':'')?>><?php echo $site_row['locationname'];?></option>
									  	<?php } ?>
										</select>
									</td>
									<td><strong>Project</strong></td>
			    					<td>
										<select name="project_name" id="project_name" style="width:150px" onchange="getpapno(this.value)">
										<option value="0">---Select---</option>
										<?php 
										$name_sql=DB_query("Select id,project_name from manufacturing_advice where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
										while($name_row=DB_fetch_array($name_sql)) {
										?>
										<option value="<?php echo $name_row['id'];?>"<?php if($name_row['id']==$_REQUEST['project_name']) { ?> selected="selected" <?php } ?>  >  <?php echo $name_row['project_name'];?></option>
										<?php } ?>
    									</select>
									</td>
									<td><strong>PAP No</strong></td>
									<td>
										<select name="boq_id" id="boq_id" style="width:150px">
									   	<option value="0">---Select---</option>
									  	<?php 		
										$site_sql=DB_query("Select id,boq_no from boq_entry where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
										while($site_row=DB_fetch_array($site_sql)){
										?>
									  	<option value="<?php echo $site_row['id'];?>"<?php echo ($_REQUEST['boq_id']==$site_row['id']?'selected':'')?>><?php echo $site_row['boq_no'];?></option>
									  	<?php } ?>
										</select>
									</td>
								</tr>	
			  					<tr class="row1">
									<td width="12%"><strong>From date</strong></td>
									<td width="22%">
										<input type="text" name='frmdate' id='frmdate' class="datepicker" style="width:150px" value="<?php  echo $_REQUEST['frmdate']?>"  />
									</td>
									<td width="12%"><strong>To date</strong></td>
									<td width="21%">
										<input type="text" name="todate" id="todate" class="datepicker" style="width:150px" value="<?php  echo $_REQUEST['todate']?>" />
									</td>
									<td width="12%"><strong>&nbsp;</strong></td>
									<td width="20%">&nbsp;</td>
								</tr>
								<tr class="row1">
									<td colspan="6">
										<p align="center">
											<input type="Submit" name="Proceed" value="Proceed" class="search" onclick="return validatedata()" />
										</p>
									</td>
								</tr>	 
		  					</table>
		  				</form>
	  				</div>
				</td>
			</tr>    
			<tr>
				<td>&nbsp;</td>
			</tr>
		</table>
		<div style="width:100%; height:300px; overflow:auto;">
		<table width="160%" border="1" cellpadding="3" cellspacing="0" style="border-collapse:collapse; border:#666666 solid 1px;">
			<tr bgcolor="#CCCCCC">
				<td rowspan="3" width="1%"><strong>S. No.</strong></td>
			    <td rowspan="3" width="3%"><strong>PAP Date</strong></td>
			    <td rowspan="3" width="3%"><strong>PAP No.</strong></td>
			    <td rowspan="3" width="10%"><strong>Project Name</strong></td>
			    <td rowspan="3" width="5%"><strong>Project Advice No.</strong></td>
			    <td rowspan="3" width="4%"><strong>Project Advice Date</strong></td>
				<td colspan="2" align="center" style="text-align:center;"><strong>Item Details</strong></td>
			    <td colspan="10" align="center" style="text-align:center;"><strong>Activity Details</strong></td>
			</tr>
		  	<tr bgcolor="#CCCCCC">
				<td rowspan="2" width="5%"><strong>Code</strong></td>
				<td width="12%" rowspan="2"><strong>Name</strong></td>
				<td rowspan="2" width="7%"><strong>Group</strong></td>
				<td width="4%" rowspan="2"><strong>Code</strong></td>
				<td width="12%" rowspan="2"><strong>Name</strong></td>
				<td width="3%" rowspan="2"><strong>Unit</strong></td>
				<td colspan="3" align="center" style="text-align:center;"><strong>Quantity</strong></td>
				<td width="4%" rowspan="2"><strong>Start Date</strong></td>
				<td width="4%" rowspan="2"><strong>End Date</strong></td>
				<td rowspan="2"><strong>Assigned To</strong></td>
			</tr>	
			<tr bgcolor="#CCCCCC">
			    <td width="4%"><strong>Allowed</strong></td>
	  	        <td width="4%"><strong>Completed</strong></td>
	  	        <td width="4%"><strong>WIP</strong></td>
	   	    </tr>
			<?php 
			if(isset($_POST['Proceed']))
			{
			 	$fromdate=$_REQUEST['frmdate'];
				$todate=$_REQUEST['todate'];
			 	$fromdate_brk=explode('/',$fromdate);
				$from=$fromdate_brk[2]."-".$fromdate_brk[1]."-".$fromdate_brk[0];
				$todate_brk=explode('/',$todate);
				$to=$todate_brk[2]."-".$todate_brk[1]."-".$todate_brk[0];
		
				$filter=array();		

				if($_REQUEST['boq_id']!='0')
				$filter[]="A.id='".$_REQUEST['boq_id']."'";
		
				if($_REQUEST['project_name']!='0')
				$filter[]="A.salesorder_id='".$_REQUEST['project_name']."'";		
		
				if($_REQUEST['frmdate']!='' && $_REQUEST['todate']!='')
			 	$filter[]="A.project_date BETWEEN '$from' and '$to'";		
		
				if($_REQUEST['site']!='0')
				$filter[]="B.branchid = '".$_REQUEST['site']."'"; 
			 
				if($filter!='')
				{
					$filter2=implode(" and ",$filter);
				}			
				
				if(!empty($filter2))
				{
		  			$sql="Select A.*,A.id as iddata,B.*  from boq_entry AS  A LEFT JOIN manufacturing_advice AS B ON A.salesorder_id=B.id  where  A.CompanyId='".$_SESSION['ACGCompanyId']."'  and $filter2 group by A.id";
				}else{
			  		$sql="Select A.*,A.id as iddata,B.* from boq_entry AS  A LEFT JOIN manufacturing_advice AS B ON A.salesorder_id=B.id where  A.CompanyId='".$_SESSION['ACGCompanyId']."'   group by A.id ";
	 			}
				$result=DB_query($sql,$db);
				$i=1;
				//echo $sql;
			 	$mmm=DB_num_rows($result);
				if(DB_num_rows($result)>0)
				{			
					while($myrow=DB_fetch_array($result))
					{
						$query22 = "select itemCode from boq_entry_details where master_id=".$myrow['iddata']." group by itemCode,level,bom_main_id";
						$res22 = DB_query($query22,$db) or die(DB_error());
						$mchk2=0;
						while($result22 = DB_fetch_array($res22)){
							$query21 = "select * from boq_entry_details where master_id=".$myrow['iddata']." and itemCode=".$result22['itemCode']."";
	  						$res21 = DB_query($query21,$db) or die(DB_error());
							$mchk=0;
							while($result21 = DB_fetch_array($res21)){
	  							$querychk21 = "SELECT SUM(B.complete_qty) AS cqty from project_completion AS A Left Join project_completion_details AS B ON A.id=B.master_id WHERE A.boq_id='".$result21['master_id']."' and itemcode='".$result22['itemCode']."' and level='".$result21['level']."' and act_name='".$result21['activity_name']."'";
								$reschk21 = DB_query($querychk21,$db) or die(DB_error());
								$resultchk21 = DB_fetch_array($reschk21);
								if($resultchk21['cqty']==''){$resultchk21['cqty']=0;}
								//echo $result11['quantity'];
								//echo $resultchk11['cqty'];
								//echo "<br>";
								if($result21['quantity']!=$resultchk21['cqty']){
									$mchk=1;
									break;
								}
							}
							if($mchk==1){
								$mchk2=1;
								break;
							}
						}
						//echo $mchk;echo "<br>";
						if($mchk2==1){?>
						<tr>
							<td valign="top"><?php echo $i;?></td>
							<td valign="top"><?php echo $myrow['boq_no'];?></td>
							<td valign="top"><?php echo ConvertSQLDate($myrow['project_date']);?></td>
							<td valign="top"><?php echo $myrow['project_name'];?></td>
							<td valign="top"><?php echo $myrow['advice_no'];?></td>
							<td valign="top"><?php echo ConvertSQLDate($myrow['advice_date']);?></td>
			    			<td colspan="12" style="padding:0px;">
								<table width="100%" border="1" cellpadding="0" cellspacing="0">
								<?php 
									$query2 = "select itemCode from boq_entry_details where master_id=".$myrow['iddata']." group by itemCode,level,bom_main_id";
									$res2 = DB_query($query2,$db) or die(DB_error());
									while($result2 = DB_fetch_array($res2)){
										$query11 = "select * from boq_entry_details where master_id=".$myrow['iddata']." and itemCode=".$result2['itemCode']."";
	  									$res11 = DB_query($query11,$db) or die(DB_error());
										$mchk=0;
										while($result11 = DB_fetch_array($res11)){
	  										$querychk11 = "SELECT SUM(B.complete_qty) AS cqty from project_completion AS A Left Join project_completion_details AS B ON A.id=B.master_id WHERE A.boq_id='".$result11['master_id']."' and itemcode='".$result2['itemCode']."' and level='".$result11['level']."' and act_name='".$result11['activity_name']."'";
											$reschk11 = DB_query($querychk11,$db) or die(DB_error());
											$resultchk11 = DB_fetch_array($reschk11);
											if($resultchk11['cqty']==''){$resultchk11['cqty']=0;}
											//echo $result11['quantity'];
											//echo $resultchk11['cqty'];
											//echo "<br>";
											if($result11['quantity']!=$resultchk11['cqty']){
												$mchk=1;
												break;
											}
										}
										//echo $mchk;echo "<br>";
										if($mchk==1){?>
											<tr bgcolor="#FFFFFF">
												<td class="jjrs_tabledata1" style="width:71px;">
													<?php echo $s->GetFieldVAlue('item_master',"id='".$result2['itemCode']."'",'StockID'); ?>
												</td>
												<td class="jjrs_tabledata1" style="width:219px;">
													<?php echo $s->GetFieldVAlue('item_master',"id='".$result2['itemCode']."'",'item_name'); ?>
												</td>
												<td colspan="10" style="padding:0px;">
													<table width="100%" border="0" cellpadding="0" cellspacing="0">
													<?php 
														$query1 = "select * from boq_entry_details where master_id=".$myrow['iddata']." and itemCode=".$result2['itemCode']."";
														$res1 = DB_query($query1,$db) or die(DB_error());
														while($result1 = DB_fetch_array($res1)){
															$querychk = "SELECT SUM(B.complete_qty) AS cqty from project_completion AS A Left Join project_completion_details AS B ON A.id=B.master_id WHERE A.boq_id='".$result1['master_id']."' and itemcode='".$result2['itemCode']."' and level='".$result1['level']."' and act_name='".$result1['activity_name']."'";
															$reschk = DB_query($querychk,$db) or die(DB_error());
															$resultchk = DB_fetch_array($reschk);
															if($resultchk['cqty']==''){$resultchk['cqty']=0;}
															if($result1['quantity']!=$resultchk['cqty']){?>
		      													<tr bgcolor="#FFFFFF">
																	<td class="jjrs_tabledata1" style="width:121px; border-right:#999999 1px solid;">
																		<?php echo $s->GetFieldVAlue('sub_activity_group',"id='".$result1['activity_group']."'",'group_name'); ?>
																	</td>
																	<td class="jjrs_tabledata1" style="width:66px; border-right:#999999 1px solid;">
																		<?php echo $s->GetFieldVAlue('sub_activity_defination',"id='".$result1['activity_name']."'",'activity_code'); ?>
																	</td>
																	<td class="jjrs_tabledata1" style="width:217px; border-right:#999999 1px solid;">
																		<?php echo $s->GetFieldVAlue('sub_activity_defination',"id='".$result1['activity_name']."'",'activity_name'); ?>
																	</td>
																	<td class="jjrs_tabledata1" style="width:48px; border-right:#999999 1px solid;"><?php echo $result1['unit']; ?></td>
																	<td class="jjrs_tabledata1" style="width:64px; border-right:#999999 1px solid;"><?php echo $qty=$result1['quantity']; ?></td>
																	<td class="jjrs_tabledata1" style="width:65px; border-right:#999999 1px solid;">
																	<?php 
																		if($resultchk['cqty']>0){?>
																			<a href="#" onClick="open_wip(<?php echo $result1['master_id'];?>,<?php echo $resultchk['cqty'];?>,<?php echo $result2['itemCode']; ?>,<?php echo $result1['level']; ?>,<?php echo $result1['activity_name']; ?>)"><?php echo $cqty=$resultchk['cqty'];?></a> 
																		<?php }else{
																			echo $cqty=$resultchk['cqty']; 
																		} 
																	?>
																	</td>
																	<td class="jjrs_tabledata1" style="width:66px; border-right:#999999 1px solid;"><?php echo $pqty=$qty-$cqty; ?></td>
																	<td class="jjrs_tabledata1" style="width:65px; border-right:#999999 1px solid;"><?php echo ConvertSQLDate($result1['startdate']); ?></td>
																	<td class="jjrs_tabledata1" style="width:65px; border-right:#999999 1px solid;"><?php echo ConvertSQLDate($result1['enddate']); ?></td>
																	<td style="padding-left:5px;" >
																	<?php 
																 		$techID = $s->GetfieldValue('project_work_assignment',"boq_id='".$result1['master_id']."'",'tech_id');
																 		if($techID!="")
																 		{ 
																			echo $s->GetfieldValue('technicians',"id='".$techID."'",'technician'); 
																		}else{ 
																			$vendID = $s->GetfieldValue('project_assign_contractor',"boq_id='".$result1['master_id']."'",'vendor_id');
																 			echo $s->GetfieldValue('suppliers',"id='".$vendID."'",'vendor_name'); 
																		}
																 	?>
																	</td>
		      													</tr>
		           											<?php  } 
														} ?>
		    										</table>
												</td>
											</tr>
										<?php } 
									} ?>
		    					</table>
							</td>
						</tr>
			  			<?php }					
					$i++; } 
	 			}else{?>
					<tr bgcolor="#c1c1c1"><td colspan="18"><font color="#FF0000">No Record Found</font></td></tr>
				<?php }
			}?>
		</table>
		
		</div>
	</div>
</div>
<script language="javascript">
function open_wip(boq,qty,itemcode,level,activity)
{
	window.open("pms_popup_wip.php?modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'] ?>&boq_id="+boq+"&qty="+qty+"&itemcode="+itemcode+"&level="+level+"&activity="+activity,"itempopup_window","height=500,width=600,toolbar=no,menubar=no,dependent=yes,resizable =yes, scrollbars=yes");
}
function getproject(str)
{
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
	xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
	if (xmlhttp.readyState==4 && xmlhttp.status==200)
	{
		document.getElementById("project_name").innerHTML=xmlhttp.responseText;
	}
	}
	xmlhttp.open("GET","pms_wip.php?site="+str,true);
	xmlhttp.send();
}
function getpapno(str)
{
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
	xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
	if (xmlhttp.readyState==4 && xmlhttp.status==200)
	{
		document.getElementById("boq_id").innerHTML=xmlhttp.responseText;
	}
	}
	xmlhttp.open("GET","pms_wip.php?project="+str,true);
	xmlhttp.send();
}
</script>
     
<?php
//include('../../includes/footer.inc');
?>
