<?php 
session_name('CAKEPHP'); session_start();
error_reporting(FALSE);
$PageSecurity = 11;
include('includes/DefinePOClass.php');
include('includes/DefineSerialItems.php');
include('includes/SQL_CommonFunctions.inc');
//include('includes/function_lib.php');
$title = _('Test Certificate Register');
$date = date($_SESSION['DefaultDateFormat']);
$_SESSION['modid']=$_REQUEST['modid'];
$_SESSION['frmid']=$_REQUEST['frmid'];
$frmpermission= $px->GetFileAccess($_SESSION['modid'],$_SESSION['frmid'],$_SESSION['ACGRoleId']);
$_SESSION['frmView']=$frmpermission[3];
if($_SESSION['frmView']=='0')
{echo "<strong>You are not authorised to view this form. Contact your administrator</strong>"; exit;}
?>
<link rel="stylesheet" href="css/purchase_requision_print.css">
<script language="javascript" type="text/javascript">
var doc=null;
function ajax(){
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  doc=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  doc=new ActiveXObject("Microsoft.XMLHTTP");
  }
}
function show_members(str)
{
ajax();
if(doc){
	doc.onreadystatechange=function()
	{
	if (doc.readyState==4 && doc.status==200)
	{
	   //	alert(xmlhttp.responseText);
	  document.getElementById("show_sitewise_member").innerHTML=doc.responseText;
	}
	}
	
	//var site=document.getElementById("site").value;
	
	doc.open("GET","check_data_bsm.php?section=show_members_gi&site="+str,true);
	doc.send();
	}
}


</script>
<br />
<br />

<h2 align="center">Job Bill Report</h2>
<form action="" method="post">
<table width="100%">
      <tr>
        <td width="4%" style="text-align:right;">Branch</td>
        <td width="11%" ><select name="site" id="site">
            <option value="0">Select</option>
            <?php 
	  $site_sql=DB_query("SELECT loccode,locationname FROM locations WHERE CompanyId='".$_SESSION['ACGCompanyId']."' AND loccode in('".$_SESSION['AssignedBranch']."')",$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 width="13%" style="text-align:right;">Project Name</td>
        <td width="12%" id="show_sitewise_member">
    <select style="width:100%;" name="project_name" id="project_name">
	<option value="0">Select</option>
	<?php 
	$sql_member=DB_query("SELECT id,project_name FROM manufacturing_advice WHERE CompanyId='".$_SESSION['ACGCompanyId']."' AND project_status=1",$db);
	while($row1=DB_fetch_array($sql_member))
	{
	?>
	<option value="<?php echo $row1['project_name']; ?>" <?php echo($_REQUEST['project_name']==$row1['id']?'selected':'')?>><?php echo $row1['project_name']; ?></option>
	<?php } ?>
	
	</select>
        </td>
        <td width="5%" style="text-align:right;">From Date</td>
        <td width="12%"><input type="text" name="fromdate" id="fromdate" class="datepicker" value="<?php  echo $_REQUEST['fromdate']?>" style="width:80px;" />
		</td>
		<td width="3%" style="text-align:right;">To Date</td>
		<td width="12%"><input type="text" name="todate" id="todate" class="datepicker" value="<?php  echo $_REQUEST['todate']?>"  style="width:80px;"/></td>
	</tr>

		<tr>
		<td colspan="8" style="height:10px;"></td>
      </tr>
		
		<tr>
		<td colspan="8"><input type="submit" name="submit" value="Search" id="view" onclick="return validatefrm()"  /></td>
      </tr>
  </table>
	


</form>
<?php 
if($_REQUEST['submit']!='')
{
	$fromdate=$_REQUEST['fromdate'];
	$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];
	$sitename=$s->getField('locations','loccode',$_REQUEST['site'],'locationname');
	
	
	$filter=array();
	if($_REQUEST['site']!='0')
	$filter[]="job_bill.branch = '".$_REQUEST['site']."'";
	
	//if($_REQUEST['project_name']!='0')
	//$filter[]="project_completion.id = '".$_REQUEST['project_name']."'";
        if($_REQUEST['project_name']!='0')
	$filter[]="project_completion.client = '".$_REQUEST['project_name']."'";
	
	if($_REQUEST['fromdate']!='' && $_REQUEST['todate']!='')
	$filter[]="job_bill.bill_date BETWEEN '".$from."' and '".$to."'";
	
	if(!empty($filter))
	{
		$filter=implode(' and ',$filter);
 $sql= "SELECT project_completion.id AS main_id,project_completion.client AS project_name,job_bill.*, project_assign_contractor.vendor_id AS v_id from project_completion INNER JOIN  project_assign_contractor ON project_completion.boq_id=project_assign_contractor.boq_id INNER JOIN job_bill ON project_assign_contractor.vendor_id=job_bill.contract_id WHERE  project_completion.approval_status='1' AND project_completion.project_status='2' AND project_completion.companyID='".$_SESSION['ACGCompanyId']."' AND $filter GROUP BY vendor_id ORDER BY project_completion.date ASC";
 
  }else{
$sql= "SELECT project_completion.id AS main_id,project_completion.client AS project_name,job_bill.*, project_assign_contractor.vendor_id AS v_id from project_completion INNER JOIN  project_assign_contractor ON project_completion.boq_id=project_assign_contractor.boq_id INNER JOIN job_bill ON project_assign_contractor.vendor_id=job_bill.contract_id WHERE  project_completion.approval_status='1' AND project_completion.project_status='2' AND project_completion.companyID='".$_SESSION['ACGCompanyId']."' GROUP BY vendor_id ORDER BY project_completion.date ASC";
  }

?>

<?php 
}
if($_REQUEST['submit']!=''){ //echo $sql;
?>

<!--<p align="right"><a href="print_adhoc_invoice_report.php?site=<?php echo $_REQUEST['site'];?>&member=<?php echo $_REQUEST['member'];?>&todate=<?php echo $_REQUEST['todate'];?>&fromdate=<?php echo $_REQUEST['fromdate'];?>&sam_id=<?php echo $_REQUEST['samplenm_id'] ?>" target="_blank">Print</a>-->
</p>
<div align="center"><strong><?php echo $message; ?></strong></div><br />
<div class="body_contain">
<div class="tab_div">
<table width="100%" cellpadding="0" cellspacing="0" border="1" bordercolor="#000000">
  <tr style="background-color:#CCCCCC; height: 30px">
  <td width="40" class="jjrs_tablehead"><strong>Sr. No.</strong></td>
	<td width="98" class="jjrs_tablehead"><strong>Date</strong></td>
     <td width="90" class="jjrs_tablehead"><strong>Bill No.</strong></td>
	      <td width="159" class="jjrs_tablehead"><strong>Contractor</strong></td>
	  <td width="70" class="jjrs_tablehead"><strong>Voucher No.</strong></td>
     <td width="136" class="jjrs_tablehead" style="text-align:center"><strong>Voucher Date</strong></td>
     <td width="136" class="jjrs_tablehead" style="text-align:center"><strong>Project Name</strong></td>
     <td width="136" class="jjrs_tablehead" style="text-align:center"><strong>PCS No</strong></td>
     <td width="136" class="jjrs_tablehead" style="text-align:center"><strong>PCS Date</strong></td>
     <td width="136" class="jjrs_tablehead" style="text-align:center"><strong>Total Amount</strong></td>
     
            
	
  </tr>
<?php 
$i=0;
//zecho $sql;
$result=DB_query($sql,$db);
if(DB_num_rows($result)){
while($myrow=DB_fetch_array($result))
{
?>
  <tr bgcolor="#FFFFFF">
<td  style="width:40px;" class="jjrs_tablehead"><?php echo $i+1;?></td>
<td class="jjrs_tablehead"><?php echo ConvertSQLDate($myrow['bill_date']);?></td>
<td class="jjrs_tablehead"><?php echo $myrow['bill_no'];?></td>
<td  style="width:120px;" class="jjrs_tablehead"><?php echo $s->GetfieldValue('suppliers',"id='".$myrow['v_id']."'",'vendor_name');?></td>
<td  class="jjrs_tablehead"><?php echo $myrow['voucher_no'];?></td>
<td style="text-align:center;width:40px;" class="jjrs_tablehead"><?php echo ConvertSQLDate($myrow['date']);?></td>
<td  class="jjrs_tablehead"><?php echo $myrow['project_name'];?></td>
<td style="text-align:center;width:40px;" class="jjrs_tablehead" colspan="3"><table width="100%" cellpadding="0" cellspacing="0" border="1" bordercolor="#000000">
   <?php $query = "select pcs_no,pcs_date,total_amount from job_bill_details where master_id='".$myrow['id']."'";
	 $res = DB_query($query,$db) or die(DB_error());
        while($result = DB_fetch_array($res)){?>
    <tr>
<td style="text-align:center;width:40px;" class="jjrs_tablehead"><?php echo $result['pcs_no'];?></td>
<td style="text-align:center;width:40px;" class="jjrs_tablehead"><?php echo $result['pcs_date'];?></td>
<td style="text-align:center;width:40px;" class="jjrs_tablehead"><?php echo $result['total_amount'];?></td>
    </tr>
        <?php } ?>
</table></td>
  </tr>
  <?php $i++;} }else{ ?>
  <tr bgcolor="#FFFFFF"><td colspan="9" height="40" style="text-align:center;">No Record Found !</td></tr>
  <?php } ?>
  
</table>
  <?php  } ?>
  </div>
  </div>