<?php

require_once('constants.php');

class General

{

	public function __construct()

	{

		

		?>

         

        <script language="javascript" src="js/index_sub.js?t=<?=time();?>" type="text/javascript">       

        </script>

        

        <?php

	}

	

	 

	public function showGeneralList()
	{
		$sql_nn2 = " SELECT * FROM  draw where m_code>0 order by code desc limit 0,1 ";		 
		$rc_nn2 = mysql_query($sql_nn2);
		$draw = mysql_fetch_assoc($rc_nn2);
		//echo  date("Y-m-d",strtotime($draw['draw_date'])).'<'.date("Y-m-t",strtotime("-1 Months")).'<BR/>';
		if(date("Y-m-d",strtotime($draw['draw_date']))<date("Y-m-t",strtotime("-1 Months")))
		{
			//$draw['detail']=='NO Draw' and 
			//echo 'Seems One month old';

			$sql  = " INSERT INTO draw SET m_code=1,month='".($draw['month']+1)."',prize=1,detail='NO Draw',draw_date='".date("Y-m-t",strtotime("-1 Months"))."'";
			mysql_query($sql);
			$code = mysql_insert_id();
			$sql_nn2 = " SELECT * FROM  draw where code='".$code."' ";		 
			$rc_nn2 = mysql_query($sql_nn2);
			$draw = mysql_fetch_assoc($rc_nn2);
			$draw['month']='9999'.$code;
			$sql_nn2 = " SELECT * FROM  customer_booking  ";
			//echo '<BR/>'.$sql_nn2; 
			$rc_nn2 = mysql_query($sql_nn2);
			while($cust_d2 = mysql_fetch_assoc($rc_nn2))
			{
				$sql_k1 = " SELECT * FROM  consolidate_policy where policy_code='".$cust_d2['code']."' and used_for='".$draw['month']."'  ";		 
				//echo '<BR/>'.$sql_k1; 
				$rc_k1 = mysql_query($sql_k1);
				$cust_k1 = mysql_fetch_assoc($rc_k1);
				if($cust_k1['code']<=0)
				{
				  $sql_k2 = " SELECT * FROM  consolidate_policy where policy_code='".$cust_d2['code']."'  and paid=1 and   (used_for='0') order by ins limit 0,1 ";
				 //  echo '<BR/>'.$sql_k2; 
				  $rc_k2 = mysql_query($sql_k2);
				  $cust_k2 = mysql_fetch_assoc($rc_k2);
					if($cust_k2['code']>0)
					{
						$su = " UPDATE consolidate_policy SET used_for='".$draw['month']."', used_for_remarks='Draw Skipped for this Month' where policy_code = '".$cust_d2['code']."' and code = '".$cust_k2['code']."'  and paid=1 and  used_for='0'  ";
						mysql_query($su);
					}
				}

			}
		}        

		$useRole = (!empty(STRICT_USER_ROLE) ? unserialize(STRICT_USER_ROLE) : array(38, 24));
		// access only to branch manager 
		$branchManager = (!empty(BRANCH_MANAGER) ? BRANCH_MANAGER : 38);
		//print_r($branchManager);
		
		if (in_array($_SESSION['user_type'], $useRole))
		//if(count($_SESSION['cat'])<=0)



		/* echo '<PRE/>';
		print_r($_SESSION['user_type']);
		echo '<PRE/>';
		//die();  */
		require_once("../../common_pages/enc.php");

		$converter = new Encryption($_SESSION['unique_enc_id']);

	?>

        <div class="am-mainpanel">

 		<div class="am-pagebody">

			<div class="row row-sm">

			  <div class="col-lg-4">

				<div>

				  <div></div>
				   <?php

					$s  = "select * from dashboard_data  order by code desc limit 0,1";

					$r = mysql_query($s);

					$ro2 = mysql_fetch_assoc($r);

					if($ro2['c_date']<=date("Y-m-d H:i:s",strtotime("-1 hours")))
					{
						// echo $ro2['c_date'].'<='.date("Y-m-d H:i:s",strtotime("-1 hours"));
							 $s  = "select code,is_closed from customer_booking where payment_status=2 ";

							$r = mysql_query($s);

							while($ro = mysql_fetch_assoc($r))

							{
								customer_payment_status_update($ro['code']);

								if($ro['is_closed']==1)

								{

									$sq = "delete from consolidate_policy where policy_code='".$ro['code']."' and paid='0' ";

									// echo '<BR/>'.$sq;

									mysql_query($sq);

								}

							} 

							$ro2['c_date'] = date("d-M-Y H:i:s"); 

					}  

					echo 'Last Updated : '.date("d-M-Y h:ia",strtotime($ro2['c_date']));
					
					$sql_x1 = "select * from sms_gateway order by code desc limit 0,1";
					$r_x1 = mysql_query($sql_x1);
					$sms_gateway = mysql_fetch_assoc($r_x1);
					
					
					echo '<br /><span style=" color:#F00">SMS GATEWAY Enabled : <B>'.ucwords($sms_gateway['gateway']).'</B></span>';
					

				   ?><br/>
					<br/>
				</div><!-- card -->
			  </div><!-- col-4 -->
			</div><!-- row -->
			<?php if (in_array($_SESSION['user_type'], $useRole)): // show the branch and personal target to BM & Sale executive by : kantsverma  

					$colours = array();

					$k=1;

					$colours[$k] = '#28a745'; $k++;

					$colours[$k] = '#f00'; $k++;

					$achievedTarget  = 	$activeBooking = $inactiveBooking = $branchTarget =  $branchAchievedTarget = 0;
					
					$totalTarget  = "select target from admin_users where admin_id=".$_SESSION['admin_code'];

					$getTotalTargetq = mysql_query($totalTarget);
					
					$getTotalTarget = mysql_fetch_assoc($getTotalTargetq);
					
					$whereClause= " and (sale_emp_code='".$_SESSION['admin_code']."' or c_admin_id='".$_SESSION['admin_code']."')";
			
					$s  = "select total_amount from customer_booking where payment_status=2 and c_date BETWEEN '".date("Y-m-01")."' AND '".date("Y-m-d")."'  ".$whereClause." order by code ";
					
					$r = mysql_query($s);
					while($ro = mysql_fetch_assoc($r))
					{
						$achievedTarget +=	$ro['total_amount'];
					}
					$status_name = array('achieved' => $achievedTarget, 'pending' => ($getTotalTarget['target'] - $achievedTarget));
					$targetsdata = array(); 

					$i = 1;
					$ddd_x = '';
					foreach($status_name as $k=>$v)
					{				
						$targetsdata[] = '{ label: "'.ucfirst($k).' ('.$v.')", data: [[1,'.$v.']], color: "'.$colours[$i].'"}';
						$i++;
					}
					/* Calcualte total no of customer of sales executive	*/
					$getTotalLeads  = "select  count('code') from customer where sale_employee_code='".$_SESSION['admin_code']."' or cc_admin_id='".$_SESSION['admin_code']."' order by code ";
					
					$totalLeads = mysql_query($getTotalLeads);
					$leads = mysql_fetch_array($totalLeads);
					
					
					
					$totalNoLeads =	(@$leads['0'] >0 ? $leads['0'] :0);
					/* Calcualte this month total no of customer of sales executive	*/
					$getMonthTotalLeads  = "select  count('code') from customer where sale_employee_code='".$_SESSION['admin_code']."' or cc_admin_id='".$_SESSION['admin_code']."' and c_date BETWEEN '".date("Y-m-01")."' AND '".date("Y-m-d")."' order by code";
					
					$monthTotalLeads = mysql_query($getMonthTotalLeads);
					$monthleads = mysql_fetch_array($monthTotalLeads);
					
					
					
					$monthsTotalNoLeads =	(@$monthleads['0'] >0 ? $monthleads['0'] :0);					
					/* calculate the total no of DHS */
			
					$dhsCount = "SELECT COUNT('c.code') FROM  customer c INNER JOIN customer_booking booking ON booking.customer_code=c.code WHERE c.is_deleted =0  and booking.payment_status=2 and (sale_emp_code='".$_SESSION['admin_code']."' or c_admin_id='".$_SESSION['admin_code']."')";

					//echo '<br/>'.$dhsCount;
					$getTotalDhsQuery = mysql_query($dhsCount) or die('Error: '.mysql_error());
					$getTotalNoDhs = mysql_fetch_array($getTotalDhsQuery);
					$totalNoDhs =	(@$getTotalNoDhs['0'] >0 ? $getTotalNoDhs['0'] :0);
				
					/* CALCUATE THE BOOKING STATUS FOR BRANCHES & SALES EXECUTIVES */

					$status = array();
					//$s  = "select 	* from customer_booking where payment_status=2 ".$where." order by code ";
					//$s  = "select * from customer_booking where payment_status=2 and c_date BETWEEN '".date("Y-m-01")."' AND '".date("Y-m-d")."'  ".$whereClause." order by code ";
					$s  = "select * from customer_booking where payment_status=2 and c_date ".$whereClause." order by code ";
					
					//echo '<br/>'.$s;
					
					$r = mysql_query($s);

					while($ro = mysql_fetch_assoc($r))
					{
						if($ro['booking_status_code'] == 1){
							$activeBooking +=	$ro['total_amount'];
						}else{
							$inactiveBooking +=	$ro['total_amount'];
						}
					}
					$bstatus_name = array('active' => $activeBooking, 'inactive' => $inactiveBooking);					

					$ddd = array(); 

					$bookingData = array(); 

					$i = 1;
					$ddd_x = '';
					foreach($bstatus_name as $k=>$v)
					{
						$bookingData[] = '{ label: "'.ucfirst($k).' ('.$v.')", data: [[1,'.$v.']], color: "'.$colours[$i].'"}';
						$i++;
					}
					//print_r($bookingData);
				/* CALCUALTE THE BRANCH TOTAL TARGETS  */
					$branchTotalTarget  = "select sum(target) as branch_target from admin_users where branch=".$_SESSION['branch_code'];
					//echo '<br/>'.$branchTotalTarget;
					$getBranchTotalTargetq = mysql_query($branchTotalTarget);
					
					$getBranchTotalTarget = mysql_fetch_assoc($getBranchTotalTargetq);
					
					
					$checkBranchAchieved = "select sum(booking.total_amount) as branch_total_achieved FROM  customer_booking booking INNER JOIN admin_users au ON au.admin_id=booking.sale_emp_code WHERE booking.payment_status=2 and booking.c_date BETWEEN '".date("Y-m-01")."' AND '".date("Y-m-d")."' and  au.branch=".$_SESSION['branch_code'];
					
					//echo '<br/>'.$checkBranchAchieved;
					
					$getBrnchAcchied = mysql_query($checkBranchAchieved);
					$branchAchievedTarget = mysql_fetch_assoc($getBrnchAcchied);
					
					//print_r($branchAchievedTarget['branch_total_achieved']);
					
					
					$branchStatusName = array('achieved' => $branchAchievedTarget['branch_total_achieved'], 'pending' => ($getBranchTotalTarget['branch_target'] - $branchAchievedTarget['branch_total_achieved']));
					$branchTotalTargets = array(); 

					$i = 1;
					$ddd_x = '';
					foreach($branchStatusName as $k=>$v)
					{				
						$branchTotalTargets[] = '{ label: "'.ucfirst($k).' ('.$v.')", data: [[1,'.$v.']], color: "'.$colours[$i].'"}';
						$i++;
					}				
					
			?>
			<?php if ($_SESSION['user_type'] == $branchManager): ?>			
			<div class="row row-sm mg-t-15 mg-sm-t-20">
				<div class="col-xl-6 mg-t-25 mg-xl-t-0">
					<div class="card pd-20 pd-sm-40">
					  <h6 class="card-body-title">Your Branch Policy Status</h6>
					  <div id="flotPie2" class="ht-200 ht-sm-250"></div>
					  <?php 
						$colours = array();

						$k=1;

						$colours[$k] = '#3498DB'; $k++;

						$colours[$k] = '#2980B9'; $k++;

						$colours[$k] = '#8E44AD'; $k++;

						$colours[$k] = '#FFC300'; $k++;

						$colours[$k] = '#FF5733'; $k++;

						$colours[$k] = '#17A2B8'; $k++;

						$colours[$k] = '#17A2B8'; $k++;

						$colours[$k] = '#17A2B8'; $k++;

						$colours[$k] = '#17A2B8'; $k++;

						$colours[$k] = '#17A2B8'; $k++;

						$colours[$k] = '#17A2B8'; $k++;

						$colours[$k] = '#17A2B8'; $k++;

						

						$status_name = array();

						$bms2 = "select * from booking_status WHERE code != 8";

						$bmr2 = mysql_query($bms2);

						while($r4 = mysql_fetch_assoc($bmr2))
						{
							$status_name[$r4['code']] = $r4['name'];
						}
						
						// get all branches sale employee data 
						$getBranchEmployess = "SELECT admin_id FROM `admin_users` WHERE `branch` =".$_SESSION['branch_code'];

						$bmrEmp = mysql_query($getBranchEmployess);
						$empNo = mysql_num_rows($bmrEmp);
						$branchWhereClause = $branchEmpClause = '';
						$counter = 0;
						while($emp = mysql_fetch_assoc($bmrEmp))
						{
							$counter++;
							//echo '<pre>';
							//echo '===> '.$empNo.' -- '.$counter;
							/*echo '<br/>';
							print_r($emp['admin_id']);
							echo '</pre>';*/
							if($empNo == $counter){
								$branchWhereClause .= "sale_emp_code='".$emp['admin_id']."'";								
								$branchEmpClause .= "cc_admin_id='".$emp['admin_id']."'";								
							}else{
								$branchWhereClause .= "sale_emp_code='".$emp['admin_id']."' or ";								
								$branchEmpClause .= "cc_admin_id='".$emp['admin_id']."' or ";								
							}
						}
						//print_r($branchWhereClause);
						
						$branch_status = array();

						//$bms  = "select * from customer_booking where payment_status=2 and (sale_emp_code='".$_SESSION['admin_code']."' or c_admin_id='".$_SESSION['admin_code']."') order by code ";
						//$bms  = "select * from customer_booking where payment_status=2 and (sale_emp_code='".$_SESSION['admin_code']."'') order by code ";
						$bms  = "select * from customer_booking where payment_status=2 and (".$branchWhereClause.") order by code ";
						
						//echo '<br/>==+> '.$bms;
						/*echo '<pre>';
						print_r($status_name);
						echo '</pre>';*/
						
						$bmr = mysql_query($bms);

						while($ro = mysql_fetch_assoc($bmr))
						{
							//echo '<br/>'.$ro['booking_status_code'];
							
							$branch_status[$ro['booking_status_code']]['c'] = ($branch_status[$ro['booking_status_code']]['c']+1);

							$branch_status[$ro['booking_status_code']]['payment'][$ro['total_amount']] = ($branch_status[$ro['booking_status_code']]['payment'][$ro['total_amount']]+1);

							$branch_status[$ro['booking_status_code']]['total_amount']+=	$ro['total_amount'];

						}

						$branch_ddd = array(); 

						$i = 1;

						$branch_ddd_x = '';

						foreach($status_name as $k=>$v)
						{
							$kk = 1; 

							if($k==6)

								$kk = 2;

							if($k==7)

								$kk = 3;

									
							/* echo '<pre>';
							print_r($v);
							//print_r($branch_status[$k]['c']);
							print_r(CURRENCY_SYMBOL.number_format($branch_status[$k]['total_amount'],2));
							echo '</pre>'; */
							
							// WHOLE BRANCH BOOKING ACTIVE/ INACTIVE STATUS 							
							if (in_array($v, array('Paid', 'Unpaid'))){
								$totalActive += $branch_status[$k]['total_amount'];
								$bookingData['0'] = '{ label: "Active ('.$totalActive.')", data: [[1,'.$totalActive.']], color: "#28a745"}';
							}
							
							if (in_array($v, array('Inactive', 'Disable','Dropout'))){
								$totalInactive += $branch_status[$k]['total_amount'];
								$bookingData['1'] = '{ label: "Inactive ('.$totalInactive.')", data: [[1,'.$totalInactive.']], color: "#f00"}';
							}							
							//print_r($bookingData);
							
							if($v)
							
							$link_start = "<a href='../policy_list/index.php?task=".$converter->encode('general')."&ref_id2=".$v."&s_booking_status=".$kk."&s_booking_status_m=".$k."'   target='_blank' >";

							

							$branch_ddd_x.= '<div class="line_one" style="background-color:'.$colours[$i].'" >'.$link_start.$v.' = '.$branch_status[$k]['c'].'  '.CURRENCY_SYMBOL.number_format($branch_status[$k]['total_amount'],2).$link_end.'</div>';

							$am_d = array();

							if(count($branch_status[$k]['payment'])>0)
								foreach($branch_status[$k]['payment'] as $k1=>$v1)
									$am_d[] = $k1.' X '.$v1.' = '.($k1*$v1);
								//$branch_ddd_x.=implode(" ,",$am_d);
							  $link_end = '</a>';

							$branch_ddd[] = '{ label: "'.$link_start.$v.' ('.$branch_status[$k]['c'].')'.$link_end.'", data: [[1,'.$branch_status[$k]['c'].']], color: "'.$colours[$i].'"}';

							$i++;

						}
						
						/* Calcualte total no of customer of sales executive	*/
						//$getBranchTotalLeads  = "select  count('code') from customer where sale_employee_code='".$_SESSION['admin_code']."' or cc_admin_id='".$_SESSION['admin_code']."' order by code ";
						
						$getBranchTotalLeads  = "select  count('code') from customer where $branchEmpClause order by code ";
						
						$branchTotalLeads = mysql_query($getBranchTotalLeads);
						$branchLeads = mysql_fetch_array($branchTotalLeads);
						
						//echo '<br/>'.$getBranchTotalLeads;
						//echo '<br/>'.$getBranchTotalLeads;
						//print_r($branchWhereClause);
						
						$branchTotalNoLeads =	(@$branchLeads['0'] >0 ? $branchLeads['0'] :0);
						/* Calcualte this month total no of customer of sales executive	*/
						$getMonthTotalLeads  = "select  count('code') from customer where sale_employee_code='".$_SESSION['admin_code']."' or cc_admin_id='".$_SESSION['admin_code']."' and c_date BETWEEN '".date("Y-m-01")."' AND '".date("Y-m-d")."' order by code";
						
						//echo '<br/>'.$getMonthTotalLeads;
						
						$monthTotalLeads = mysql_query($getMonthTotalLeads);
						$monthleads = mysql_fetch_array($monthTotalLeads);

						// get all branches sale employee data 
						$getBranchEmployess = "SELECT admin_id,name FROM `admin_users` WHERE `branch` =".$_SESSION['branch_code'];

						$bmrEmp = mysql_query($getBranchEmployess);
						$empNo = mysql_num_rows($bmrEmp);
						$branchWhereClause = $branchEmpClause = '';
						$counter = $branchTotalLeads = $monthlyleads = $branchTotalNoDhs = 0;
						while($emp = mysql_fetch_assoc($bmrEmp))
						{
							$counter++;
							//echo '<pre>';
							//echo '===> '.$empNo.' -- '.$counter;
							/* echo '<br/>';
							print_r($emp);
							echo '</pre>'; */
							
							$checkBmrEmp = mysql_query("select count(*) as leads from customer_booking where payment_status=2 and (sale_emp_code='".$emp['admin_id']."' or c_admin_id='".$emp['admin_id']."') order by code ");

							$empNo = mysql_fetch_assoc($checkBmrEmp);
							//print_r($empNo['leads']);
							$branchTotalLeads += $empNo['leads'];
							
							$getMonthTotalLeads  = "select  count('code') as monthly_leads from customer where sale_employee_code='".$emp['admin_id']."' or cc_admin_id='".$emp['admin_id']."' and c_date BETWEEN '".date("Y-m-01")."' AND '".date("Y-m-d")."' order by code";

							$monthTotalLeads = mysql_query($getMonthTotalLeads);
							$monthlyleads = mysql_fetch_assoc($monthTotalLeads);												
							$branchMonthlyLeads += $monthlyleads['monthly_leads'];
							
							$dhsCount = "SELECT COUNT('c.code') as dhs FROM  customer c INNER JOIN customer_booking booking ON booking.customer_code=c.code WHERE c.is_deleted =0  and booking.payment_status=2 and booking.c_date BETWEEN '".date("Y-m-01")."' AND '".date("Y-m-d")."' and (sale_emp_code='".$emp['admin_id']."' or c_admin_id='".$emp['admin_id']."')";

							//echo '<br/>'.$dhsCount;
							
							$getTotalDhsQuery = mysql_query($dhsCount);
							$getTotalNoDhs = mysql_fetch_assoc($getTotalDhsQuery);
							$branchTotalNoDhs += $getTotalNoDhs['dhs']; 
					
							//echo '<br/>'.$emp['name'].' == '.$empNo['leads'].' =+= '.$monthlyleads['monthly_leads'].' dhs '.$getTotalNoDhs['dhs'];

						}
						
						?>
					<style>
					.line_one{
							 width:auto;
							color:#FFFFFF;
							display:inline-table;  
							padding:4px 7px;  
							border-radius:3px; 
							margin:2px;
					}
					.line_one a{ color:#FFFFFF;}
					</style>
					<script>
					$( document ).ready(function() {
					var piedata = [
						<?=implode($branch_ddd,',');?>
					];
					 $.plot('#flotPie2', piedata, {
						series: {
						  pie: {
							show: true,
							radius: 1,
							innerRadius: 0.3,
							label: {
							  show: true,
							  radius: 2/3,
							  formatter: labelFormatter,
							  threshold: 0.1
							}
						  }
						},
						grid: {
						  hoverable: true,
						  clickable: true
						}
					  });
					});
					</script>

					</div>
				</div>
				<div class="col-xl-6 mg-t-25 mg-xl-t-0">
					<div class="card pd-20 pd-sm-40">
					  <h6 class="card-body-title">Branch Policy Status Details</h6>
					   <?=$branch_ddd_x;?>
					</div>
				</div>
			</div>
			<div class="row row-sm mg-t-15 mg-sm-t-20">
			  <div class="col-xl-6 mg-t-25 mg-xl-t-0">

				<div class="card pd-20 pd-sm-40">

				  <h6 class="card-body-title">Branch Booking Status</h6>

				  <p class="mg-b-20 mg-sm-b-30">Your whole branch booking status.</p>

				  <div id="bookingChart" class="ht-200 ht-sm-250" height="250px" width="250px"></div>
				</div>

			  </div>
			   <div class="col-xl-6 mg-t-25 mg-xl-t-0">
				<div class="card pd-20 pd-sm-40">
				  <h6 class="card-body-title">Branch Leads/Conversion Details</h6>
				  <div id="" class="d-flex">
						<div class="row">
						  <div class="col-xl-4">
							<div class="card">
							  <div class="card-body">
								<h5 class="card-title">Total Leads</h5>
								<p class="card-text text-justify">Branch total no of leads having no DHS</p>
								<p class="card-text">
									<h2 class="mg-b-5 tx-inverse tx-lato text-danger"><?= $branchTotalLeads ?></h2>
								</p>
								<!--<a href="../customer_list/index.php?task=general&sub_cat=&key=&m_id=207&show=all" class="btn btn-primary d-flex flex-column">View All</a>-->
							  </div>
							</div>
						  </div>
						  <div class="col-xl-4">
							<div class="card">
							  <div class="card-body">
								<h5 class="card-title">New Leads</h5>
								<p class="card-text text-justify">Total no of new leads this month</p>
								<p class="card-text">
									<h2 class="mg-b-5 tx-inverse tx-lato text-danger"><?= $branchMonthlyLeads ?></h2>
								</p>							
								<!--<a href="../customer_list/index.php?task=general&sub_cat=&key=&m_id=207&show=all" class="btn btn-primary d-flex flex-column">View All</a>-->
							  </div>
							</div>
						  </div>						  
						  <div class="col-xl-4">
							<div class="card">
							  <div class="card-body">
								<h5 class="card-title">New DHS</h5>
								<p class="card-text text-justify">Total no of customer with booking.</p>
								<p class="card-text">
									<h2 class="mg-b-5 tx-inverse tx-lato text-danger"><?= $branchTotalNoDhs ?></h2>
								</p>
								<!--<a href="../policy_list/index.php?task=general&sub_cat=&key=&m_id=209" class="btn btn-primary d-flex flex-column">View All</a>-->
							  </div>
							</div>
						  </div>
						  <div class="col-xl-4">
							<div class="card">
							  <div class="card-body">
																		
							  </div>
							</div>
						  </div>									  
						</div>				  
				  </div>				  
				</div>
			  </div>
			</div>
			<div class="row row-sm mg-t-15 mg-sm-t-20">
				<div class="col-xl-6 mg-t-25 mg-xl-t-0">
					<div class="card pd-20 pd-sm-40">
					  <h6 class="card-body-title">Your Personal Target </h6>
					  <div id="individualstargets" class="ht-200 ht-sm-250" height="250px" width="250px"></div>
					</div>
				</div>
				<div class="col-xl-6 mg-t-25 mg-xl-t-0">
					<div class="card pd-20 pd-sm-40">
					  <h6 class="card-body-title">Your Branch Target</h6>
					  <div id="branchtargets" class="ht-200 ht-sm-250" height="250px" width="250px"></div>
					</div>
				</div>        
			</div>
			<script language="javascript"  type="application/javascript">

			$( document ).ready(function() {
				var targetspiedata = [
					<?=implode($targetsdata,',');?>
				];
				 $.plot('#individualstargets', targetspiedata, {
					series: {
					  pie: {
						show: true,
						radius: 1,
						innerRadius: 0.3,
						label: {
						  show: true,
						  radius: 2/3,
						  formatter: labelFormatter,
						  threshold: 0.1
						}
					  }

					},

					grid: {
					  hoverable: true,
					  clickable: true
					}
				  });
				  
				var branchTargetsdata = [
					<?=implode($branchTotalTargets,',');?>
				];
				 $.plot('#branchtargets', branchTargetsdata, {
					series: {
					  pie: {
						show: true,
						radius: 1,
						innerRadius: 0.3,
						label: {
						  show: true,
						  radius: 2/3,
						  formatter: labelFormatter,
						  threshold: 0.1
						}
					  }

					},

					grid: {
					  hoverable: true,
					  clickable: true
					}
				  });				  
				  
				var bookingPiedata = [
				<?=implode($bookingData,',');?>
				];
				$.plot('#bookingChart', bookingPiedata, {
				series: {
				  pie: {
					show: true,
					radius: 1,
					innerRadius: 0.3,
					label: {
					  show: true,
					  radius: 2/3,
					  formatter: labelFormatter,
					  threshold: 0.1
					}
				  }
				},
				grid: {
				  hoverable: true,
				  clickable: true
				}
				});
			});
			</script>			
			
			<?php else: 
					/* Calcualte total no of customer of sales executive	*/
					$getTotalLeads  = "select  count('code') from customer where sale_employee_code='".$_SESSION['admin_code']."' or cc_admin_id='".$_SESSION['admin_code']."' order by code ";
					
					$totalLeads = mysql_query($getTotalLeads);
					$leads = mysql_fetch_array($totalLeads);
					
					
					
					$totalNoLeads =	(@$leads['0'] >0 ? $leads['0'] :0);
					/* Calcualte this month total no of customer of sales executive	*/
					$getMonthTotalLeads  = "select  count('code') from customer where sale_employee_code='".$_SESSION['admin_code']."' or cc_admin_id='".$_SESSION['admin_code']."' and c_date BETWEEN '".date("Y-m-01")."' AND '".date("Y-m-d")."' order by code";
					
					$monthTotalLeads = mysql_query($getMonthTotalLeads);
					$monthleads = mysql_fetch_array($monthTotalLeads);
					
					
					
					$monthsTotalNoLeads =	(@$monthleads['0'] >0 ? $monthleads['0'] :0);					
					/* calculate the total no of DHS */
			
					$dhsCount = "SELECT COUNT('c.code') FROM  customer c INNER JOIN customer_booking booking ON booking.customer_code=c.code WHERE c.is_deleted =0  and booking.payment_status=2 $whereClause";

					//echo '<br/>'.$dhsCount;
					$getTotalDhsQuery = mysql_query($dhsCount) or die('Error: '.mysql_error());
					$getTotalNoDhs = mysql_fetch_array($getTotalDhsQuery);
					$totalNoDhs =	(@$getTotalNoDhs['0'] >0 ? $getTotalNoDhs['0'] :0);

					$emps  = "select * from customer_booking where payment_status=2 and c_date ".$whereClause." order by code ";
					
					//echo '<br/>'.$emps;
					
					$empr = mysql_query($emps);

					while($empro = mysql_fetch_assoc($empr))
					{
						if($empro['booking_status_code'] == 1){
							$activeBooking +=	$empro['total_amount'];
						}else{
							$inactiveBooking +=	$empro['total_amount'];
						}
					}
					$empbstatus_name = array('active' => $activeBooking, 'inactive' => $inactiveBooking);					

					$ddd = array(); 

					$empbookingData = array(); 

					$i = 1;
					$ddd_x = '';
					foreach($empbstatus_name as $k=>$v)
					{
						$empbookingData[] = '{ label: "'.ucfirst($k).' ('.$v.')", data: [[1,'.$v.']], color: "'.$colours[$i].'"}';
						$i++;
					}					
			?>
			<div class="row row-sm mg-t-15 mg-sm-t-20">
				<div class="col-xl-6 mg-t-25 mg-xl-t-0">
					<div class="card pd-20 pd-sm-40">
					  <h6 class="card-body-title">Your Personal Target </h6>
					  <div id="individualstargets" class="ht-200 ht-sm-250" height="250px" width="250px"></div>
					</div>
				</div>
				<div class="col-xl-6 mg-t-25 mg-xl-t-0">
					<div class="card pd-20 pd-sm-40">
					  <h6 class="card-body-title">Your Branch Target</h6>
					  <div id="branchtargets" class="ht-200 ht-sm-250" height="250px" width="250px"></div>
					</div>
				</div>        
			</div>
			<div class="row row-sm mg-t-15 mg-sm-t-20">
			  <div class="col-xl-6 mg-t-25 mg-xl-t-0">
				<div class="card pd-20 pd-sm-40">
				  <h6 class="card-body-title">Your Booking Status</h6>
				  <p class="mg-b-20 mg-sm-b-30">Your booking status.</p>
				  <div id="empBookingChart" class="ht-200 ht-sm-250" height="250px" width="250px"></div>
				</div>
			  </div>
			   <div class="col-xl-6 mg-t-25 mg-xl-t-0">
				<div class="card pd-20 pd-sm-40">
				  <h6 class="card-body-title">Your Leads/Conversion Details</h6>
				  <div id="" class="d-flex">
						<div class="row">
						  <div class="col-xl-4">
							<div class="card">
							  <div class="card-body">
								<h5 class="card-title">Total Leads</h5>
								<p class="card-text text-justify">Branch total no of leads having no DHS</p>
								<p class="card-text">
									<h2 class="mg-b-5 tx-inverse tx-lato text-danger"><?= $totalNoLeads ?></h2>
								</p>
								<a href="../customer_list/index.php?task=general&sub_cat=&key=&m_id=207&show=all" class="btn btn-primary d-flex flex-column">View All</a>
							  </div>
							</div>
						  </div>
						  <div class="col-xl-4">
							<div class="card">
							  <div class="card-body">
								<h5 class="card-title">New Leads</h5>
								<p class="card-text text-justify">Total no of new leads this month</p>
								<p class="card-text">
									<h2 class="mg-b-5 tx-inverse tx-lato text-danger"><?= $monthsTotalNoLeads ?></h2>
								</p>							
								<a href="../customer_list/index.php?task=general&sub_cat=&key=&m_id=207&show=all" class="btn btn-primary d-flex flex-column">View All</a>
							  </div>
							</div>
						  </div>						  
						  <div class="col-xl-4">
							<div class="card">
							  <div class="card-body">
								<h5 class="card-title">New DHS</h5>
								<p class="card-text text-justify">Total no of customer with booking.</p>
								<p class="card-text">
									<h2 class="mg-b-5 tx-inverse tx-lato text-danger"><?= $totalNoDhs ?></h2>
								</p>
								<a href="../policy_list/index.php?task=general&sub_cat=&key=&m_id=209" class="btn btn-primary d-flex flex-column">View All</a>
							  </div>
							</div>
						  </div>
						  <div class="col-xl-4">
							<div class="card">
							  <div class="card-body">
																		
							  </div>
							</div>
						  </div>									  
						</div>				  
				  </div>				  
				</div>
			  </div>
			</div>			
			<script language="javascript"  type="application/javascript">

			$( document ).ready(function() {
				var targetspiedata = [
					<?=implode($targetsdata,',');?>
				];
				 $.plot('#individualstargets', targetspiedata, {
					series: {
					  pie: {
						show: true,
						radius: 1,
						innerRadius: 0.3,
						label: {
						  show: true,
						  radius: 2/3,
						  formatter: labelFormatter,
						  threshold: 0.1
						}
					  }

					},

					grid: {
					  hoverable: true,
					  clickable: true
					}
				  });
				  
				var branchTargetsdata = [
					<?=implode($branchTotalTargets,',');?>
				];
				 $.plot('#branchtargets', branchTargetsdata, {
					series: {
					  pie: {
						show: true,
						radius: 1,
						innerRadius: 0.3,
						label: {
						  show: true,
						  radius: 2/3,
						  formatter: labelFormatter,
						  threshold: 0.1
						}
					  }

					},

					grid: {
					  hoverable: true,
					  clickable: true
					}
				  });				  
				  
				var bookingPiedata = [
				<?=implode($empbookingData,',');?>
				];
				$.plot('#empBookingChart', bookingPiedata, {
				series: {
				  pie: {
					show: true,
					radius: 1,
					innerRadius: 0.3,
					label: {
					  show: true,
					  radius: 2/3,
					  formatter: labelFormatter,
					  threshold: 0.1
					}
				  }
				},
				grid: {
				  hoverable: true,
				  clickable: true
				}
				});
			});
			</script>			
						
			<?php endif; ?>
			<?php endif; ?>
			
			<?php if (!in_array($_SESSION['user_type'], $useRole)): // restricted acess to BM & Sale executive by : kantsverma  ?>
			<div class="row row-sm">
			  <div class="col-lg-4">

				<div class="card">

				  <div id="rs1" class="wd-100p ht-200"></div>

				  <div class="overlay-body pd-x-20 pd-t-20">

					<div class="d-flex justify-content-between">

					  <div>

						<h6 class="tx-12 tx-uppercase tx-inverse tx-bold mg-b-5">Total Pending</h6>

						<p class="tx-12">Till today</p>

					  </div>

					  <a href="" class="tx-gray-600 hover-info"><i class="icon ion-more tx-16 lh-0"></i></a>

					</div><!-- d-flex -->

					<h2 class="mg-b-5 tx-inverse tx-lato text-danger"><?php

					

					 if($_SESSION['access_type']<2)	

					$where.= " and (sale_emp_code='".$_SESSION['admin_code']."' or c_admin_id='".$_SESSION['admin_code']."')";



					

					$sql = "select policy_code,sum(amt) as amt from customer_booking b,consolidate_policy c where c.date<='".date("Y-m-d")."' ".$where." and paid = 0  and payment_status=2 and b.code=policy_code group by policy_code  ";
					
					//echo $sql;
					//die();
					$r = mysql_query($sql);

					$i = 0;

					while($ro = mysql_fetch_assoc($r))

					{

						$i++;

						$amt+=abs($ro['amt']);

					

					} 

					echo CURRENCY_SYMBOL.number_format($amt,2);

					?></h2>

					<p class="tx-12 mg-b-0">from <?=$i;?> Polices</p>

				  </div>

				</div><!-- card -->

			  </div><!-- col-4 -->
			  <div class="col-lg-4 mg-t-15 mg-sm-t-20 mg-lg-t-0">

				<div class="card">

				  <div id="rs2" class="wd-100p ht-200"></div>

				  <div class="overlay-body pd-x-20 pd-t-20">

					<div class="d-flex justify-content-between">

					  <div>

						<h6 class="tx-12 tx-uppercase tx-inverse tx-bold mg-b-5">Total Received </h6>

						<p class="tx-12">Till today<?php //date("F 01 - d, Y");?></p>

					  </div>

					  <a href="" class="tx-gray-600 hover-info"><i class="icon ion-more tx-16 lh-0"></i></a>

					</div><!-- d-flex -->

					<h2 class="mg-b-5 tx-inverse tx-lato"><h2 class="mg-b-5 tx-inverse tx-lato"><?php

					$sql = "select policy_code,sum(amt) as amt from customer_booking b,consolidate_policy c where   paid =1 ".$where."  and b.code=policy_code group by policy_code  ";

					$r = mysql_query($sql);

					$i = 0;

					$amt= 0;

					while($ro = mysql_fetch_assoc($r))

					{

						$i++;

						$amt+=abs($ro['amt']);

					

					} 

					echo CURRENCY_SYMBOL.number_format($amt,2);

					?></h2>

					<p class="tx-12 mg-b-0">from <?=$i;?> Polices</p></h2> 

				  </div>

				</div><!-- card -->

			  </div><!-- col-4 -->

			  <div class="col-lg-4 mg-t-15 mg-sm-t-20 mg-lg-t-0">

				<div class="card">

				  <div id="rs3" class="wd-100p ht-200"></div>

				  <div class="overlay-body pd-x-20 pd-t-20">

					<div class="d-flex justify-content-between">

					  <div>

						<h6 class="tx-12 tx-uppercase tx-inverse tx-bold mg-b-5">Total Polices</h6>

						<p class="tx-12">Till today</p>

					  </div>

					  <a href="" class="tx-gray-600 hover-info"><i class="icon ion-more tx-16 lh-0"></i></a>

					</div><!-- d-flex -->

					<h2 class="mg-b-5 tx-inverse tx-lato">

					<h2 class="mg-b-5 tx-inverse tx-lato"><h2 class="mg-b-5 tx-inverse tx-lato"><?php

					$sql = "select customer_code,count(*) as c from customer_booking where 1 group by customer_code";

					$r = mysql_query($sql);

					$i = 0;

					$amt = 0;

					while($ro = mysql_fetch_assoc($r))

					{

						$i++;

						$amt+=abs($ro['c']);

					

					} 

					echo number_format($amt,0);

					?></h2>

					<p class="tx-12 mg-b-0">from <?=$i;?> Customers</p></h2> 

					</h2>

					<p class="tx-12 mg-b-0"></p>

				  </div>

				</div><!-- card -->

			  </div><!-- col-4 -->

			</div>
			<div class="row row-sm mg-t-15 mg-sm-t-20">

			   <div class="col-xl-6 mg-t-25 mg-xl-t-0">

				<div class="card pd-20 pd-sm-40">

				  <h6 class="card-body-title">Policy Status</h6>

				  <!--<p class="mg-b-20 mg-sm-b-30">Labels can be hidden if the slice is less than a given percentage of the pie.</p>-->

				  <div id="flotPie2" class="ht-200 ht-sm-250"></div>
				  <?php 
					$colours = array();

					$k=1;

					$colours[$k] = '#3498DB'; $k++;

					$colours[$k] = '#2980B9'; $k++;

					$colours[$k] = '#8E44AD'; $k++;

					$colours[$k] = '#FFC300'; $k++;

					$colours[$k] = '#FF5733'; $k++;

					$colours[$k] = '#17A2B8'; $k++;

					$colours[$k] = '#17A2B8'; $k++;

					$colours[$k] = '#17A2B8'; $k++;

					$colours[$k] = '#17A2B8'; $k++;

					$colours[$k] = '#17A2B8'; $k++;

					$colours[$k] = '#17A2B8'; $k++;

					$colours[$k] = '#17A2B8'; $k++;

					

					$status_name = array();

					$s2 = "select * from booking_status ";

					$r2 = mysql_query($s2);

					while($r4 = mysql_fetch_assoc($r2))
					{
						$status_name[$r4['code']] = $r4['name'];
					}

					$status = array();

					$s  = "select 	* from customer_booking where payment_status=2 ".$where." order by code ";
					
					/* echo '<br/>'.$s;
					echo '<pre>';
					print_r($status_name);
					echo '</pre>'; */
					
					$r = mysql_query($s);

					while($ro = mysql_fetch_assoc($r))
					{
						//echo '<br/>'.$ro['booking_status_code'];
						
						$status[$ro['booking_status_code']]['c'] = ($status[$ro['booking_status_code']]['c']+1);

						$status[$ro['booking_status_code']]['payment'][$ro['total_amount']] = ($status[$ro['booking_status_code']]['payment'][$ro['total_amount']]+1);

						$status[$ro['booking_status_code']]['total_amount']+=	$ro['total_amount'];

					}

					$ddd = array(); 

					$i = 1;

					$ddd_x = '';

					foreach($status_name as $k=>$v)
					{
						$kk = 1; 

						if($k==6)

							$kk = 2;

						if($k==7)

							$kk = 3;

								

						$link_start = "<a href='../policy_list/index.php?task=".$converter->encode('general')."&ref_id2=".$v."&s_booking_status=".$kk."&s_booking_status_m=".$k."'   target='_blank' >";

						

						$ddd_x.= '<div class="line_one" style="background-color:'.$colours[$i].'" >'.$link_start.$v.' = '.$status[$k]['c'].'  '.CURRENCY_SYMBOL.number_format($status[$k]['total_amount'],2).$link_end.'</div>';

						$am_d = array();

						if(count($status[$k]['payment'])>0)
							foreach($status[$k]['payment'] as $k1=>$v1)
								$am_d[] = $k1.' X '.$v1.' = '.($k1*$v1);
							//$ddd_x.=implode(" ,",$am_d);
						  $link_end = '</a>';

						$ddd[] = '{ label: "'.$link_start.$v.' ('.$status[$k]['c'].')'.$link_end.'", data: [[1,'.$status[$k]['c'].']], color: "'.$colours[$i].'"}';

						$i++;

					}
					?>
			   <style>
			   .line_one{
						 width:auto;
						color:#FFFFFF;
						display:inline-table;  
						padding:4px 7px;  
						border-radius:3px; 
						margin:2px;
				}
				.line_one a{ color:#FFFFFF;}
			   </style>
				<script>
				$( document ).ready(function() {
				var piedata = [
					<?=implode($ddd,',');?>
				];
				 $.plot('#flotPie2', piedata, {
					series: {
					  pie: {
						show: true,
						radius: 1,
						innerRadius: 0.3,
						label: {
						  show: true,
						  radius: 2/3,
						  formatter: labelFormatter,
						  threshold: 0.1
						}
					  }
					},
					grid: {
					  hoverable: true,
					  clickable: true
					}
				  });
				});
				</script>

				</div><!-- card -->

			  </div>

			   <div class="col-xl-6 mg-t-25 mg-xl-t-0">

				<div class="card pd-20 pd-sm-40">

				  <h6 class="card-body-title">Policy Status Details</h6>

				  <!--<p class="mg-b-20 mg-sm-b-30">Labels can be hidden if the slice is less than a given percentage of the pie.</p>-->

				   <?=$ddd_x;?>

				</div><!-- card -->

			  </div>

			</div>
			<?php endif; ?>
        </div>
        <!-- col-6 -->

        </div><!-- row -->

		<?php 

		  $c = array();

		  $i = 0;

		  

		  $r = array();

		   

		  $i = 1;

		  $colour[$i] = '#CAB1D2'; $i++;

		  $colour[$i] = '#677489'; $i++;

		  $colour[$i] = '#F89D44'; $i++;

		  $colour[$i] = '#85C441'; $i++;

		  $colour[$i] = '#36B3E3'; $i++;

		  $colour[$i] = '#F00'; $i++;

		  $colour[$i] = '#0F0'; $i++;

		  

		  

		  $s = array();

		  $sql = "select c.admin_id,sum(amt) as c,name from consolidate c,admin_users a where date='".date("Y-m-d")."' and amt>0 and c.admin_id = a.admin_id group by c.admin_id";

		  $r2 = mysql_query($sql);

		  $dd =array();

		  $i = 1;

		  while($ro = mysql_fetch_assoc($r2))
		  {
		  		//$s[$ro['admin_id']] =  $ro['c'] ;
		  		$dd[] =  "{ label: '".$ro['name'].' ('.$ro['c'].')'."', data: [[1,".$ro['c']."]], color: '".$colour[$i]."'}";
				$i++;
		  }
		  ?>

          

          <script language="javascript"  type="application/javascript">

		  function labelFormatter(label, series) {

			return "<div style='font-size:8pt; text-align:center; padding:2px; color:white;'>" + label + "<br/>" + Math.round(series.percent) + "%</div>";

			}

		  $( document ).ready(function() {

     

		 	   var piedata = [
					<?=implode(",",$dd);?> 
					];

					

					$.plot('#flotPie1', piedata, {

					series: {

					  pie: {

						show: true,

						radius: 1,

						label: {

						  show: true,

						  radius: 2/3,

						  formatter: labelFormatter,

						  threshold: 0.1

						}

					  }

					},

					grid: {

					  hoverable: true,

					  clickable: true

					}

					});

		  

		  

		  });

          </script>

         <!-- row -->

			

            

            

            

        </div><!-- row -->

      

       

	

           

                     

    <?php

	}

	

	//Add 

		public function add_edit($data='',$extra='')

		{

			

			//echo $_SESSION['role_level'];

			

			

			 

			$data['id'] = $data['code'] = $data['code'];

			

			/*if($data['dob'])

				$data['dob'] = $data['dob'];

			else

				$data['dob'] = date("Y-m-d");

				*/

		?> 



            <div id="add_edit"  >

                <form   name="add_edit_form" id="add_edit_form" method="post" enctype="multipart/form-data" action='../ajaximage/ajaximage.php' data-parsley-validate >

               <div class="header_box"><?=($data['id']>0)?'Edit - ':'Add - ';?><?=TITLE_PAGE;?></div>

               <div class="body_box"> <?php if($data['id']>0) { 

				

				?>

                	<input type="hidden" name="task2" id="task2" value="update"  />

                    <input type="hidden" name="id" id="id" value="<?=$data['id'];?>"  />

                   

				   

                   

				   <?php }else{ 

				   

				   //$data['under'] = $extra['under'];

				  // $data['designation'] = $extra['designation'];

				   ?>

                   <input type="hidden"  name="task2" id="task2" value="Add"  />

                   <input type="hidden" name="id" id="id" value=""  />

                   <?php } ?>

                   

                 

              	

                

                    <div class="wd-300">

                      <div class="d-md-flex mg-b-30">

                        <div class="form-group mg-b-0">

                          <label>Customer Name: <span class="tx-danger">*</span></label>

                          <input type="text" name="name" class="form-control wd-200 wd-sm-250" placeholder="Enter Name" required>

                        </div><!-- form-group -->

                        <div class="form-group mg-b-0 mg-md-l-20 mg-t-20 mg-md-t-0">

                          <label>Mobile Number: <span class="tx-danger">*</span></label>

                          <input type="text" name="mobile" class="form-control wd-200 wd-sm-250" placeholder="Enter Mobile Number" required>

                        </div><!-- form-group -->

                      </div><!-- d-flex -->

                      <div class="d-md-flex mg-b-30">

                        <div class="form-group mg-b-0">

                          <label>Address Line 1: <span class="tx-danger">*</span></label>

                          <input type="text" name="address_line_1" class="form-control wd-200 wd-sm-250" placeholder="Enter Address Line 1" required>

                        </div><!-- form-group -->

                        <div class="form-group mg-b-0 mg-md-l-20 mg-t-20 mg-md-t-0">

                          <label>Address Line 2: <span class="tx-danger"></span></label>

                          <input type="text" name="address_line_2" class="form-control wd-200 wd-sm-250" placeholder="Enter Address Line 2(Optional)"  >

                        </div><!-- form-group -->

                      </div>

                      

                      <div class="d-md-flex mg-b-30">

                         <div  id="res"></div>

                      </div>

                       <div class="d-md-flex mg-b-30">

                        <button type="submit"  id="submit_66"  class="dt-button btn btn-primary btn-success btn-sm">Submit</button> 

                        

                        <button type="button" class="dt-button btn btn-default btn-success btn-sm" onclick="GeneralDetails(<?=$_SESSION['page'];?>)">Go Back</button>

                        </div>

                    </div>

           <!--

           		<button type="button" id="btnInsertPro" name="btnInsertPro" onclick="addNew_Key(this.form)">Save</button>

                <button type="button" id="as" name="as" onclick="GeneralDetails(<?=$_SESSION['page'];?>)" >Go Back</button>-->

            

            	</form>

            </div>

                

		 

        <?php

	}

	

}



?>

</div>

