<?php
ini_set('display_errors', 0);
//error_reporting(E_ALL);
//ini_set('display_errors', 1);

session_start();
ob_start();

require_once('configuration/server_connect.php');
require_once('configuration/all_function.php');

$active_id=$_REQUEST['active_id'];
$menu_id=$_REQUEST['menu_id'];

// Common Information

$page_title="Employee Sales Target";
$page="46_employee_sales_target.php?active_id=".$active_id."&menu_id=".$menu_id."";
$table='employee_info';	
$unique='emp_code';

// End Common Information

?>

<!DOCTYPE html>
<html lang="en">

 <?php require_once('inc_files/inc_head.php'); ?>
 
 <script>



function getXMLHTTP() { //fuction to return the xml http object



		var xmlhttp=false;	



		try{



			xmlhttp=new XMLHttpRequest();



		}



		catch(e)	{		



			try{			



				xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");

			}

			catch(e){



				try{



				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");



				}



				catch(e1){



					xmlhttp=false;



				}



			}



		}



		 	



		return xmlhttp;



    }



	function update_value(id)
	{

var emp_code=id; // Rent
var mon=(document.getElementById('mon').value); 
var year=(document.getElementById('year').value)*1; 
var target_qty=(document.getElementById('target_qty_'+id).value)*1;

var flag=(document.getElementById('flag_'+id).value); 

var strURL="46_employee_sales_target_ajax.php?emp_code="+emp_code+"&mon="+mon+"&year="+year+"&target_qty="+target_qty+"&flag="+flag;



		var req = getXMLHTTP();



		if (req) {



			req.onreadystatechange = function() {

				if (req.readyState == 4) {

					// only if "OK"

					if (req.status == 200) {						

						document.getElementById('divi_'+id).style.display='inline';

						document.getElementById('divi_'+id).innerHTML=req.responseText;						

					} else {

						alert("There was a problem while using XMLHTTP:\n" + req.statusText);

					}

				}				

			}

			req.open("GET", strURL, true);

			req.send(null);

		}	



}




</script>

<body>

  <!-- ======= Header ======= -->
  <?php 
  require_once('inc_files/inc_header.php');
  ?><!-- End Header -->

  <!-- ======= Sidebar ======= -->
  <?php  require_once('inc_files/inc_menu.php'); ?><!-- End Sidebar-->

  <main id="main" class="main">

    <div class="pagetitle">
      <h1><?php echo $page_title?></h1>
      <nav>
        <ol class="breadcrumb">
          <li class="breadcrumb-item"><a href="home.php">Home</a></li>
          <li class="breadcrumb-item">Sales Management</li>
          <li class="breadcrumb-item active"><?php echo $page_title?></li>
        </ol>
      </nav>
    </div><!-- End Page Title -->

    <section class="section">
      <div class="row">
        <div class="col-lg-12"> 

          <div class="card">
            <div class="card-body">
			<br>
            <form class="row g-3" action="" method="post" name="codz" id="codz">
   		       
			
	<table width="80%" border="0" align="center">
      
      
        <tr height="50">
          <td align="right">Month:&nbsp;&nbsp;</td>
          <td align="left">
		  <select id="mon" name="mon"  class="form-select"  required  >
		  <option></option>
			
			<?php foreign_relation($conn,'month','month_id','month_name',$_POST['mon'],'1 order by month_id');?>
        	</select>		  </td>
          <td align="right">Department:&nbsp;&nbsp;</td>
          <td width="13%">
		  <select id="department" name="department"  class="form-select"    >
		  <option></option>
			
			<?php foreign_relation($conn,'department','id','department',$_POST['department'],'1 order by id');?>
        	</select>		  </td>
          <td width="16%" align="right">Job Status:&nbsp;&nbsp;</td>
          <td width="18%">
		  <select id="job_status" name="job_status"  class="form-select" required>
		 
			<?php foreign_relation($conn,'job_status','id','job_status',$_POST['job_status'],'1 order by id');?>
        	</select></td>
          <td width="16%" rowspan="6" align="center" ><strong>
            &nbsp;&nbsp;&nbsp;
            <button type="submit" name="run_data" id="run_data" class="btn btn-success rounded-pill btn-sm">&nbsp;<i class="bi bi-filter me-1"></i> Run Report&nbsp;&nbsp;</button>
          </strong></td>
        </tr>
        <tr height="50">
        <td width="11%" align="right">Year:&nbsp;&nbsp;</td>
        <td width="12%" align="left">
		<select id="year" name="year"  class="form-select"  required  >
			<option></option>
			<?php foreign_relation($conn,'year','year','year',$_POST['year'],'1 order by year');?>
        	</select>		</td>
        <td width="14%" align="right">Designation:&nbsp;&nbsp;</td>
        <td>
		<select id="designation" name="designation"  class="form-select"    >
		  <option></option>
			
			<?php foreign_relation($conn,'designation','id','designation',$_POST['designation'],'1 order by id');?>
        	</select>
			</td>
        <td align="right">EMP Name:&nbsp;&nbsp;</td>
        <td>
		<?php
			 auto_complete_from_db_sql($conn,'SELECT concat(emp_code," :: ",emp_name), concat(emp_code," :: ",emp_name) FROM employee_info
			WHERE 1 order by emp_code','employee_name');
		?>
                  <input type="text" class="form-control" id="employee_name" name="employee_name" value="<?php echo $_REQUEST['employee_name']?>" >	
		</td>
        </tr>
    </table>
               
  </form>
  <br>
	
              <!-- Table with stripped rows -->
              <table width="100%" class="table table-sm" style="font-size:12px;">
                <thead>
                  <tr style="text-transform:uppercase; color:#012970; background:#f6f9ff;  border-top: 1px solid #CCCCCC;">
                    <th width="8%" scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;"> Code </span></th>
                    <th width="18%" scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;"> Customer Name </span></th>
                    <th width="15%" scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;">designation</span></th>
                    <th width="13%" scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;">Mobile No </span></th>
                    <th width="12%" scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;">Target Qty </span></th>
                    <th width="12%" scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;">Sales qty </span></th>
                    <th width="12%" scope="col">Percentage</th>
                    <th width="10%" scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;">Action</span></th>
                  </tr>
                </thead>
                <tbody>
					<?php
					
$employee_name = $_REQUEST['employee_name'];
  $employee=explode(' :: ',$employee_name);
   $employee_code=$employee[0];
  
if($employee_code!=''){
$employee_con=" and emp_code='".$employee_code."' ";
}

if($_POST['department']!=''){
$department_con=" and department='".$_POST['department']."' ";
}

if($_POST['designation']!=''){
$designation_con=" and designation='".$_POST['designation']."' ";
}

if($_POST['job_status']!=''){
$job_status_con=" and job_status='".$_POST['job_status']."' ";
}


$start_date = ''.$_POST['year'].'-'.$_POST['mon'].'-01';
$end_date = date("Y-m-t", strtotime($start_date));

$fdate = strtotime($start_date);
$tdate = strtotime($end_date);
$datediff = $tdate - $fdate;

$day_count=$datediff / (60 * 60 * 24)+1;

	 		$sql = "select id, designation from designation where 1 group by id";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $designation[$info->id]=$info->designation;
		}			
					
		 $sql = "select id, job_status from job_status where 1 group by id";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $job_status[$info->id]=$info->job_status;
		}		
		
		
		 $sql = "select emp_code, target_qty from employee_monthly_sales_target where mon='".$_POST['mon']."' and year='".$_POST['year']."' group by emp_code";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $emp_code[$info->emp_code]=$info->emp_code;
		 $target_qty[$info->emp_code]=$info->target_qty;
		 
		}
		
		$sql = "select emp_code, sum(total_unit) as sales_qty from sales_invoice where jv_date BETWEEN '".$start_date."' AND '".$end_date."' group by emp_code";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
		 $sales_qty[$info->emp_code]=$info->sales_qty;
		 
		}
		
			
					    $sql = 'select * from '.$table.' where 1 '.$employee_con.$designation_con.$department_con.$job_status_con.' ';
						$query = mysqli_query($conn,$sql);
						while ($data = mysqli_fetch_object($query)) {

					?>
	<?php if(isset($_POST['run_data'])){?>		
      <tr>
      <td><?php echo $data->emp_code;?></td>
      <td><?php echo $data->emp_name;?></td>
      <td><?php echo $designation[$data->designation]?></td>
	  <td><?php echo $data->mobile_no;?></td>
      <td><input class="form-control" name="target_qty_<?php echo $data->emp_code?>" id="target_qty_<?php echo $data->emp_code?>" type="text"  value="<?php echo $target_qty[$data->emp_code];?>" style="width:90%; height:30px;" /></td>
      <td><input class="form-control" name="sales_qty_<?php echo  $data->emp_code?>" id="sales_qty_<?php echo $data->emp_code?>" type="text"  value="<?php echo $sales_qty[$data->emp_code];?>" style="width:90%; height:30px;" readonly="" /></td>
      <td align="center" style="font-size:16px; "><?php if($target_qty[$data->emp_code]>0){ 
	   echo number_format($percentage=($sales_qty[$data->emp_code]/$target_qty[$data->emp_code])*100,2); ?> % <?php } ?></td>
      <td>
	  <span id="divi_<?php echo $data->emp_code?>">

            <?php
			  if($emp_code[$data->emp_code]>0)
			  {?>

			  <input name="flag_<?php echo $data->emp_code?>" type="hidden" id="flag_<?php echo $data->emp_code?>" value="1" />

			  <input class="form-control" type="button" name="Button" value="Edit"  onclick="update_value(<?php echo $data->emp_code?>)" style="width:80%; height:30px; background-color:#FF3366; font-weight:700; font-size:12px;"/>
			  
			  <?php
			  }
			  else
			  {
			  ?>

			  <input name="flag_<?php echo $data->emp_code?>" type="hidden" id="flag_<?php echo $data->emp_code?>" value="0" />

			  <input class="form-control" type="button" name="Button" value="Save"  onclick="update_value(<?php echo $data->emp_code?>)" style="width:80%; height:30px;background-color:#66CC66;   font-weight:700; font-size:12px;"/><?php }?>
          </span>	  </td>
      </tr>
				  <?php }}?>
                </tbody>
              </table>
              <!-- End Table with stripped rows -->

            </div>
          </div>

        </div>

        
      </div>
    </section>

  </main><!-- End #main -->

  <!-- ======= Footer ======= -->
  <?php require_once('inc_files/inc_footer.php'); ?>
 <!-- ======= Footer ======= -->

</body>

</html>