<?php
session_start();
$where 					= " WHERE 1=1 ";
$next_page_condition 	= "";
$paging ="";
if($_REQUEST['start']){
	$paging .="&start=".$_REQUEST['start'];
}
###########################################  STRAT FILTER CONITIONS    #####################################
/*$submit 	= isset($_REQUEST['submit']) ? $_REQUEST['submit'] : "";
if($submit){
	$where = " WHERE 1=1 ";
	$next_page_condition.="&submit=$submit";
}else{
	$where = " WHERE 1=0 ";
	$next_page_condition.="&submit=$submit";
}*/
if($_REQUEST['job_id_search']){
	$job_id_search = $_REQUEST['job_id_search'];
	$where 	.= " AND id ='$job_id_search' ";
	$next_page_condition.="&job_id_search=".$_REQUEST['job_id_search'];
}
if($_REQUEST['admin_id_search']){
	$job_ids = "";
	$admin_id_search = $_REQUEST['admin_id_search'];
	$sql = "SELECT * FROM job_admin_mapping WHERE admin_id = $admin_id_search";
	$rs = mysqli_query($conn,$sql);
	if(mysqli_num_rows($rs) > 0){
		while($data = mysqli_fetch_assoc($rs)){
			$job_ids .= $data["job_id"].",";
		}
		$job_ids = substr($job_ids,0,-1);
	}
	
	$where 	.= " AND id IN ($job_ids) ";
	$next_page_condition.="&admin_id_search=".$_REQUEST['admin_id_search'];
}
$from_date = isset($_REQUEST['from_date']) ? utility::setFormatedDate($_REQUEST['from_date']) : "";
$to_date = isset($_REQUEST['to_date']) ? utility::setFormatedDate($_REQUEST['to_date']) : "";
if($from_date && $to_date){
	$where .= " AND job_date BETWEEN '$from_date 00:00:00' and '$to_date 23:59:59' ";
    $next_page_condition.="&from_date=".$_REQUEST['from_date']."&to_date=".$_REQUEST['to_date']." ";
}else if($from_date){
	$where.= " AND job_date >= '$from_date' ";
	$next_page_condition.="&from_date=".$_REQUEST['from_date'];
}else if($to_date){
	$where.= " AND job_date <= '$to_date' ";
	$next_page_condition.="&to_date=".$_REQUEST['to_date'];
}
if($_REQUEST['client_industry']){
	$client_industry = $_REQUEST['client_industry'];
	$where 	.= " AND client_industry ='$client_industry' ";
	$next_page_condition.="&client_industry=".$_REQUEST['client_industry'];
}
if($_REQUEST['sector']){
	$sector = $_REQUEST['sector'];
	$where 	.= " AND sector ='$sector' ";
	$next_page_condition.="&sector=".$_REQUEST['sector'];
}
###########################################  	END  FILTER CONITIONS 	#####################################
###########################################  PAGING WITH PER PAGE  #####################################
$file_name		= "index"; // this is file name which is used during paging  , included at the bottom of the page
$table_name		= "job";
$paging_table_name	= $table_name;
$paging_query		= $where;
$join_query_for_paging = "SELECT * FROM $table_name  $where ";
$page_header = "Performance Report";
//include("paging/paging_query.inc.php");
############################################ END PAGING WITH PER PAGE  #######################################
$job_ids = "";
$sql = "SELECT id FROM job $where ";
$rs = mysqli_query($conn,$sql);
if(mysqli_num_rows($rs) > 0){
	while($r = mysqli_fetch_assoc($rs)){
		$job_ids .= $r["id"].",";
	}
	$job_ids = substr($job_ids,0,-1);
}
?>
<section class="content-header">
  <h1>
	<?=$page_header;?>
  </h1>
</section>
<section class="content">
	<div class="row">
		<div class="col-xs-12">
			<div class="box">
				<div class="box-body">
				<!-- search form -->
				<?php include_once('performance_report_search_options.inc.php');?>
				</div>
			</div>
		</div>
	</div>
</section>
<section class="content">
	<div class="row">
		<div class="col-xs-12">
				<!-- /.search form -->
					<? //if($result>0){?>
					<div class="box box-primary">
						<form class="form-horizontal" id="new_form">
						<div class="box-body">
							<div class="form-group">
							  <label for="Company Name" class="col-sm-3 control-label">Total Client</label>
							  <div class="col-sm-9">
								<?
									$query = "SELECT * FROM $table_name $where AND is_job = 1 ";
									$sql=mysqli_query($conn,$query);
									echo $result=mysqli_num_rows($sql);
								?>
							  </div>
							</div>
							<div class="form-group">
							  <label for="Sector" class="col-sm-3 control-label">Total Quotation</label>
							  <div class="col-sm-9">
								<?
									$query = "SELECT * FROM $table_name $where AND is_quotation = 1 AND is_job = 0 ";
									$sql=mysqli_query($conn,$query);
									echo $result=mysqli_num_rows($sql);
								?>
							  </div>
							</div>
							<div class="form-group">
							  <label for="Sector" class="col-sm-3 control-label">Total Enquiry</label>
							  <div class="col-sm-9">
								<?
									$query = "SELECT * FROM $table_name $where AND is_enquiry = 1 AND is_quotation = 0 AND is_job = 0 ";
									$sql=mysqli_query($conn,$query);
									echo $result=mysqli_num_rows($sql);
								?>
							  </div>
							</div>
							<? /*
							<div class="form-group">
							  <label for="Total Invoice" class="col-sm-3 control-label">Total Invoice</label>
							  <div class="col-sm-9">
								<?
									$query = "SELECT * FROM invoice WHERE job_id IN($job_ids) ";
									$sql=mysqli_query($conn,$query);
									echo $result=mysqli_num_rows($sql);
								?>
							  </div>
							</div>
							<div class="form-group">
							  <label for="Total Payment" class="col-sm-3 control-label">Total Payment</label>
							  <div class="col-sm-9">
								<?
									$query = "SELECT sum(amount) as total_amount FROM payment WHERE job_id IN($job_ids) ";
									$sql=mysqli_query($conn,$query);
									$result=mysqli_num_rows($sql);
									if($result > 0){
										$row = mysqli_fetch_assoc($sql);
										if(isset($row["total_amount"]) && $row["total_amount"] !=""){
											echo number_format($row["total_amount"],2);
										}else{
											echo 0.00;
										}
									}
								?>
							  </div>
							</div>
							*/?>
							<div class="form-group">
							  <label for="Total Appointment" class="col-sm-3 control-label">Total Appointments</label>
							  <div class="col-sm-9">
								<?
									$query = "SELECT * FROM appointment WHERE job_id IN($job_ids) ";
									$sql=mysqli_query($conn,$query);
									echo $result=mysqli_num_rows($sql);
								?>
							  </div>
							</div>
							<div class="form-group">
							  <label for="Total Reminders" class="col-sm-3 control-label">Total Reminders</label>
							  <div class="col-sm-9">
								<?
									$query = "SELECT * FROM notes WHERE job_id IN($job_ids) AND note_type = 1 ";
									$sql=mysqli_query($conn,$query);
									echo $result=mysqli_num_rows($sql);
								?>
							  </div>
							</div>
						</div>
						</form>
					</div>
					 <? /*}else{ ?>
						No job found...
					 <? }*/ ?>
				<!--</div>-->
		</div>
	</div>
</section>
<style>
.content{
	min-height:0px;
}
.form-horizontal .control-label{
	padding-top :0px;
}
@media print
{
	#new_form .col-sm-3{
		width:25%;
		float:left;
		clear:right;
	}
	#new_form .col-sm-9{
		width:75%;
	}
}
</style>