<?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="Customer Sales Target";
$page="45_customer_sales_target.php?active_id=".$active_id."&menu_id=".$menu_id."";
$table='accounts_reference';	
$unique='ref_no';

// 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 ref_no=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="45_customer_sales_target_ajax.php?ref_no="+ref_no+"&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">Customer Type:&nbsp;&nbsp;</td>
          <td width="13%">
		  <select id="customer_type" name="customer_type"  class="form-select"    >
		  <option></option>
			
			<?php foreign_relation($conn,'customer_type','id','customer_type',$_POST['customer_type'],'1 order by id');?>
        	</select>		  </td>
          <td width="16%" align="right">Mobile No:&nbsp;&nbsp;</td>
          <td width="18%">
		  <input type="text" class="form-control" id="mobile_no" name="mobile_no" value="<?php echo $_POST['mobile_no']?>" ></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">Zone Name:&nbsp;&nbsp;</td>
        <td>
		<select id="zone" name="zone"  class="form-select"    >
		  <option></option>
			
			<?php foreign_relation($conn,'sales_zone','id','zone_name',$_POST['zone'],'1 order by zone_name');?>
        	</select>
			</td>
        <td align="right">Customer Name:&nbsp;&nbsp;</td>
        <td>
		 <?php
			 auto_complete_from_db_sql($conn,'SELECT concat(ref_no," :: ",full_name), concat(ref_no," :: ",full_name) FROM accounts_reference
			WHERE ref_type=2 order by ref_no','full_name');
		?>
                  <input type="text" class="form-control" id="full_name" name="full_name" value="<?php echo $_REQUEST['full_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;">Zone</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
					
					
 $full_name = $_REQUEST['full_name'];
  $customer=explode(' :: ',$full_name);
   $customer_code=$customer[0];
  
if($customer_code!=''){
$customer_con=" and ref_no='".$customer_code."' ";
}

if($_POST['mobile_no']!=''){
$mobile_no_con=" and mobile_no LIKE '%".$_POST['mobile_no']."%' ";
}

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

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


$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, zone_name from sales_zone where 1 group by id";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $zone_name[$info->id]=$info->zone_name;
		}			
					
		
		
		 $sql = "select ref_no, target_qty from customer_monthly_sales_target where mon='".$_POST['mon']."' and year='".$_POST['year']."' group by ref_no";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $ref_no[$info->ref_no]=$info->ref_no;
		 $target_qty[$info->ref_no]=$info->target_qty;
		 
		}
		
		$sql = "select ref_no, sum(total_unit) as sales_qty from sales_invoice where jv_date BETWEEN '".$start_date."' AND '".$end_date."' group by ref_no";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
		 $sales_qty[$info->ref_no]=$info->sales_qty;
		 
		}
		
			
					     $sql = 'select * from '.$table.' where ref_type=2 '.$mobile_no_con.$customer_con.$zone_con.$customer_type_con.' ';
						$query = mysqli_query($conn,$sql);
						while ($data = mysqli_fetch_object($query)) {

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

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

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

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

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

			  <input class="form-control" type="button" name="Button" value="Save"  onclick="update_value(<?php echo $data->ref_no?>)" 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>