<?php 
//****************MODIFY BY AMIT SINGH ************D&D************************23-11-2012*********************************
include('functions.php');
$s=new db_query();
global $db;
$deptid=  $s->GetFieldValue('www_users'," id='".$_SESSION['UserID']."' " ,'mst_dpt_heirarchy');		 
$LowerDeptList=GETLowerDeptUser($_SESSION['UserID'],$deptid,$_SESSION['ACGCompanyId']);
$lowerlist=  UserDeptList($LowerDeptList);
$lowerlist[]=$_SESSION['UserID'];
if(count($lowerlist)>0)
$lowerlist=implode(",",$lowerlist) ;
else
$lowerlist="'".$_SESSION['UserID']."'";
$user_ids1	=$lowerlist;
$id1=array();
//echo $user_ids;
$user_id = $_SESSION['ACG_DashBoard_User'];

$type=$s->getField('www_users','id',$_SESSION['UserID'],'Type');
//error_reporting(0);
//include('Class_PickListEditor.php');?>

<?php
$catid= $s->GetFieldValue('crm_picklist',"picklist_title='Leads' and status=1",'picklist_id');
$table="crm_leads";
 if($_REQUEST['Delete'])
{
	for($i=0;$i<count($_REQUEST['leadid']);$i++)
	{
	$cond="mst_leadid='".$_REQUEST['leadid'][$i]."'";
	$row= $s->DeleteRecord('crm_lead_items', $cond);
	$cond="leadid='".$_REQUEST['leadid'][$i]."'";
	$row= $s->DeleteRecord('crm_leads', $cond);
	}
}
?>
<script language="javascript" >
function LeadData()
{	
	myCheckboxArray = document.myform.elements["leadid[]"];
	var t=myCheckboxArray.length;
	if(document.getElementById('leadid').checked==true)
	{	
	if(t==undefined)
	document.getElementById('leadid_'+1).checked=true;		
	for (var i = 1; i <= t; i++)
	{
	document.getElementById('leadid_'+i).checked=true;	
	}	
	}
	else
	{
	if(t==undefined)		
	document.getElementById('leadid_'+1).checked=false;		
	for (var i = 1; i <= t; i++)
	{
	document.getElementById('leadid_'+i).checked=false;	
	}
	}
}
</script>

<style type="text/css">

.tab a{
color: #171717; font-weight: bold;
text-decoration: none;
display: block;
padding:5px 10px;
background-color: #99b998;
border:#6a8769 solid 1px;
-webkit-border-top-left-radius:5px; -moz-border-top-left-radius:5px; -o-border-top-left-radius:5px; border-top-left-radius:5px;
-webkit-border-top-right-radius:5px; -moz-border-top-right-radius:5px; -o-border-top-right-radius:5px; border-top-right-radius:5px;
}
.tab a:hover{
color: #171717;
text-decoration: none;
background-color: #fff;
}

#wrapper{
width:100%;
margin:auto;
float:left;
padding: 10px; box-sizing:border-box;
}
.box{
background: #fff;
}
.boxholder{
clear: both;
padding:0;
border:0;
padding-bottom:0px;
}
.tab{
float: left;
margin: 0 1px 0 0;
text-align: center;
/*background: #8DC70A url(images/greentab.jpg) no-repeat;*/
}
.tabtxt{
margin: 0;
font-size: 12px;
font-weight: bold;
padding:0 0 0 0;
}

/****************** Start page styles ********************************************/
#wrap {
  max-width: 600px;
  margin: 30px auto;
  background: #fff;
  border: 4px solid #FFD16F;
  -moz-border-radius: 15px;
  -webkit-border-radius: 15px;
  border-radius: 15px;
  padding: 20px;
    }

.field { 
  padding: 0 0 1em;
  width:160px;
  }

/****************** End page styles ********************************************/
</style>
<script type="text/javascript" src="js/prototype.lite.js"></script>
<script type="text/javascript" src="js/moo.fx.js"></script>
<script type="text/javascript" src="js/moo.fx.pack.js"></script>
<script type="text/javascript">
function init(){
	var stretchers = document.getElementsByClassName('box');
	var toggles = document.getElementsByClassName('tab');
	var myAccordion = new fx.Accordion(
		toggles, stretchers, {opacity: false, height: true, duration: 600}
	);
	//hash functions
	var found = false;
	toggles.each(function(h3, i){
		var div = Element.find(h3, 'nextSibling');
			if (window.location.href.indexOf(h3.title) > 0) {
				myAccordion.showThisHideOpen(div);
				found = true;
			}
		});
		if (!found) myAccordion.showThisHideOpen(stretchers[0]);
}
</script>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div"> 
<h1><?php  echo $page_title;?></h1>
 <div class="centre" id="slidingDiv">
<form action="" method="post">
<table width="100%" cellpadding="3" cellspacing="0" border="0">
  <tr class="row1">
  <td width="13%" align="right"><strong>Branch:</strong></td>
	<td width="35%"><?php $site_sql=DB_query("Select loccode, locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')",$db); ?>
        <select name="branch_id" style="width:98%;">
	<option value="0">Select Branch</option>
    <?php while($site_row=DB_fetch_array($site_sql))
		{?>
			<option value="<?php echo $site_row['loccode'];?>"<?php echo($_REQUEST['branch_id']==$site_row['loccode']?'selected':'')?>><?php echo $site_row['locationname'];?></option>
		<?php 	
		}
    	?></select></td>
    <td width="13%" align="right"><strong>Status</strong></td>
	<td><?php $LeadStatusSQL=DB_query("SELECT id, name FROM lead_status where CompanyId='".$_SESSION['ACGCompanyId']."'",$db); ?>
	<select name="lead_status"  style="width:98%;">
	<option value="0">--All--</option>	
	<?php while($statusRow=DB_fetch_array($LeadStatusSQL)){?>
        <option value="<?php  echo $statusRow['id'];?>" <?php  echo $statusRow['id']==$_REQUEST['lead_status']?'selected':''?>><?php echo $statusRow['name'];?>          </option>
        <?php } ?>
      </select></td>  
 </tr>
 <tr class="row1">     
  <td align="right"><strong>From Date:</strong></td>
	<td><input name="fromdate" type="text"  class="datepicker" value="<?php echo $_REQUEST['fromdate']; ?>" style="width:98%;"/></td>
    <td align="right"><strong>To Date:</strong></td>
	<td><input name="todate" type="text"  class="datepicker" value="<?php echo $_REQUEST['todate']; ?>" style="width:98%;" /></td>
</tr>

  <tr class="row1">
  <td colspan="4" style="text-align:center;"><input name="Search" type="submit"  value="SEARCH" class="search"/></td>
  </tr>
</table>
</form>

</div>



 <form name="myform" method="post">
 <p align="left">
<?php if($_SESSION['frmAdd']==0){ ?> 
 <input type="button" name="Create" value="Add" alt="Not Allowed" title="Not Allowed" class="search" accesskey="N" />
<?php }else{?>
 <a href="<?php echo "index.php?route=CRM/Leads".$rurl.'&action=AddNew'?>"><input type="button" name="Create" value="Add" class="search" accesskey="N" /></a>
<?php }?>
<?php if($_SESSION['frmDelete']==0){ ?>
  <input type="button" name="Delete" value="Delete" alt="Not Allowed" title="Not Allowed"  class="search" />
<?php }else{?>
  <input type="submit" name="Delete" value="Delete"  class="search" />
<?php }?>
  
  
<?php 
if($_REQUEST['Search'])
{?>
<a href="leads_exp.php"><input type="button" name="Export" value="Export" class="search"/></a></td>
	<?php }?>
 </p>
<div id="wrapper">
<div id="content">
<h4 class="tab" title="first" style="padding:0px;border:none;"><div class="tabtxt"><a href="#">Unconfirmed Leads</a></div></h4>
<h4 class="tab" title="second" style="border:none; padding:0px;"><div class="tabtxt"><a href="#">Confirmed Leads</a></div></h4>
<div class="boxholder">

<div class="box">
<h3>Unconfirmed Leads</h3>
<table width="100%" border="1" cellspacing="0" cellpadding="3" style="border:#808080 1px solid;">
  <tr>
    <th width="2%"><input name="leadid" type="checkbox" id="leadid" onclick="LeadData()" /></th>
	<th width="10%">Lead No.</th>
	<th width="6%">Lead Date</th>
	<th>Client Name</th>
	<th width="16%">Contact Person Name</th>
	<th width="12%">Email</th>
	<th width="10%">Mobile</th>
	<th width="12%">Created By</th>
    <th colspan="3" style="text-align:center;">Action</th>
  </tr>
<?php $i=1;
$s=new db_query();
 $roleSql="SELECT * FROM `www_users` WHERE id='".$_SESSION['UserID']."'";
 
$roleid=Db_query($roleSql,$db);
$roleRow=DB_fetch_array($roleid);
$RoleID= $roleRow['rolepid'];
if($_REQUEST['Search'])
{
    
        $todate=ConvertSQLDate($_REQUEST['todate']);
        $fromdate=ConvertSQLDate($_REQUEST['fromdate']);
	if($_REQUEST['branch_id']!='0')
	{
        $filter[]="mst_branchid='".$_REQUEST['branch_id']."'";
	}
	
	if($_REQUEST['lead_status']!='0') 
	{
	$filter[]="lead_status='".$_REQUEST['lead_status']."'";
	}
        
        if($_REQUEST['lead_status']=='0') 
	{
	$filter[]="1=1";
	}
        
        if($_REQUEST['todate']!='' && $_REQUEST['fromdate']!='' )
	{
		$filter[]="leads_date between '".$fromdate."' and '".$todate."'";
	}
	//print_r($filter);
	if(isset($filter))
	{
            $filter1=implode(" and " ,$filter);
                // Dealer And User Check
            if($_SESSION['dealerid'])
            {
            $getleadNew1="select * from crm_leads where $filter1 and CompanyId='".$_SESSION['ACGCompanyId']."' and created_by IN(".$lowerlist.") and dealerid='".$_SESSION['dealerid']."' and Convert_IntoAccountStatus='N'";
            }
            else
            {
                    $getleadNew1="select * from crm_leads where $filter1 and CompanyId='".$_SESSION['ACGCompanyId']."' and created_by IN(".$lowerlist.") and dealerid=0 and Convert_IntoAccountStatus='N'";
            }
	}
	else
	{
	 if($_SESSION['dealerid'])
            {
            $getleadNew1="select * from crm_leads where CompanyId='".$_SESSION['ACGCompanyId']."' and created_by IN(".$lowerlist.") and dealerid='".$_SESSION['dealerid']."' and Convert_IntoAccountStatus='N'";
            }
            else
            {
                    $getleadNew1="select * from crm_leads where CompanyId='".$_SESSION['ACGCompanyId']."' and created_by IN(".$lowerlist.") and dealerid=0 and Convert_IntoAccountStatus='N'";
            }	
	}
}
else
{
	// Dealer And User Check
	if($_SESSION['dealerid'])
	{
	$getleadNew1="select * from crm_leads where CompanyId='".$_SESSION['ACGCompanyId']."' and created_by IN(".$lowerlist.") and dealerid='".$_SESSION['dealerid']."' and Convert_IntoAccountStatus='N'";
	}
	else
	{
		$getleadNew1="select * from crm_leads where CompanyId='".$_SESSION['ACGCompanyId']."' and created_by IN(".$lowerlist.") and dealerid=0 and Convert_IntoAccountStatus='N'";
	}
		
	
}

//echo $getleadNew1;
$getleadNew=Db_query($getleadNew1,$db);
if(DB_num_rows($getleadNew)>0)
{

	 while($row_lead=DB_fetch_array($getleadNew))
	 {
              $AccStatus=$row_lead['Convert_IntoAccountStatus'];
	 ?>
  <tr bgcolor="#fff">
    <td>
        <?php  if($type=='SuperAdmin'){ ?>
   <input name="leadid[]" type="checkbox" id="leadid_<?php echo $i; ?>" value="<?php  echo $row_lead['leadid']?>" />
    <?php } ?>

<?php
	 $query2="select person from contact_information_master where master_code='".$row_lead['company_name']."'";
	$getitem2= DB_query($query2, $db);
	$contact_row=DB_fetch_array($getitem2);
	$query="select leadid from crm_leads where created_by='".$_SESSION['UserID']."'";
	$getitem= DB_query($query, $db);
	$n1= DB_num_rows($getitem);
	$query1="select id from crm_lead_groups_users where parent_id='".$row_lead['leadid']."' and user_id='".$_SESSION['UserID']."'";
	$getitem1= DB_query($query1, $db);
	$n2= DB_num_rows($getitem1);
?>
  </td>
  <td><?php  echo $row_lead['leadcode'];// ConvertSQLDate($row_lead['created_date'])?></td>
  <td><?php echo ConvertSQLDate($row_lead['created_date']); ?></td>
  <td><?php if(is_numeric($row_lead['company_name'])){echo $s->GetFieldValue('clientmaster',"id='".$row_lead['company_name']."'",'c_name');}else{echo $row_lead['company_name'];}
	?></td>
    <td><?php if(is_numeric($row_lead['company_name'])){ echo $contact_row['person'];}else {echo $row_lead['contact_name']; } ?></td>
        <td><?php if(is_numeric($row_lead['company_name'])){ echo $s->GetFieldValue('contact_information_master',"master_code='".$row_lead['company_name']."'",'email'); }else {echo $row_lead['cemail']; }?></td>
        <td><?php if(is_numeric($row_lead['company_name'])){ echo $s->GetFieldValue('contact_information_master',"master_code='".$row_lead['company_name']."'",'phone'); } else{echo $row_lead['mobile']; }?></td>
<td><?php  echo $s->GetFieldValue('www_users',"id='".$row_lead['created_by']."'",'realname'); ?></td>

<td width="10%"><?php if($AccStatus=='N') {
?><a href="index.php?route=CRM/CreatenewAccount&leadid=<?php  echo $row_lead['leadid'].$rurl;?>&action=convert"><input type="button" name="Create" value="Lead Confirmation" class="search" onclick="return confirm('Do You Want To Convert Into Prospective Client');" /></a>

<?php } else { 
echo 'Lead Confirmed';
 } ?></td>
 <td width="2%" align="center"><?php if($_SESSION['frmEdit']==0){ ?>
  <img src="images/icon-edit.png" width="15" alt="Not Allowed" title="Not Allowed to Edit Document" height="15" />
<?php }else{ ?> 
 <a href="index.php?route=CRM/crmleadview&action=Edit&leadid=<?php echo $row_lead['leadid'].$rurl;?>"><img src="images/icon-edit.png" width="15" height="15" title="Edit Document" /></a>
<?php } ?></td>
 <td width="2%" align="center"><?php if(($_SESSION['frmView']==0 && $n1<0) || ($_SESSION['frmView']==0 && $n2<0)){ ?>
  <img src="images/search.gif" width="15" alt="Not Allowed" title="Not Allowed to View Document" height="15" />
<?php }else{ ?>
   <a href="index.php?route=CRM/LeadView&action=View&leadid=<?php echo $row_lead['leadid'].$rurl;?>"><img src="images/search.gif" width="15" height="15" title="View Document" /></a>
<?php } ?></td>	 
   </tr>
   <?php $i++; } 
   }
   else
   {?>
   	<tr>
   		<td colspan="9"><font color="#FF0000"><?php echo "No Records Found"; ?></font></td>
   		</tr>
   	
  <?php }?>
</table>
</div>

<div class="box">
<h3>Confirmed Leads</h3>
<table width="100%" border="1"  cellspacing="0" cellpadding="3" style="border:#808080 1px solid;">
  <tr>
    <th width="2%"><input name="leadid" type="checkbox" id="leadid" onclick="LeadData()" /></th>
	<th width="10%">Lead No.</th>
	<th width="6%">Lead Date</th>
	<th>Client Name</th>
	<th width="16%">Contact Person Name</th>
	<th width="14%">Email</th>
	<th width="10%">Mobile</th>	 
  </tr>
<?php $i=1;
$s=new db_query();
 $roleSql="SELECT * FROM `www_users` WHERE id='".$_SESSION['UserID']."'";
 
$roleid=Db_query($roleSql,$db);
$roleRow=DB_fetch_array($roleid);
$RoleID= $roleRow['rolepid'];
if($_REQUEST['Search'])
{
    
        $todate=ConvertSQLDate($_REQUEST['todate']);
        $fromdate=ConvertSQLDate($_REQUEST['fromdate']);
	if($_REQUEST['branch_id']!='0')
	{
        $filter[]="mst_branchid='".$_REQUEST['branch_id']."'";
	}
	
	if($_REQUEST['lead_status']!='0') 
	{
	$filter[]="lead_status='".$_REQUEST['lead_status']."'";
	}
        
        if($_REQUEST['lead_status']=='0') 
	{
	$filter[]="1=1";
	}
        
        if($_REQUEST['todate']!='' && $_REQUEST['fromdate']!='' )
	{
		$filter[]="leads_date between '".$fromdate."' and '".$todate."'";
	}
	//print_r($filter);
	if(isset($filter))
	{
            $filter1=implode(" and " ,$filter);
                // Dealer And User Check
            if($_SESSION['dealerid'])
            {
            $getleadNew1="select * from crm_leads where $filter1 and CompanyId='".$_SESSION['ACGCompanyId']."' and created_by IN(".$lowerlist.") and dealerid='".$_SESSION['dealerid']."' and Convert_IntoAccountStatus='Y'";
            }
            else
            {
                    $getleadNew1="select * from crm_leads where $filter1 and CompanyId='".$_SESSION['ACGCompanyId']."' and created_by IN(".$lowerlist.") and dealerid=0 and Convert_IntoAccountStatus='Y'";
            }
	}
	else
	{
	 if($_SESSION['dealerid'])
            {
            $getleadNew1="select * from crm_leads where CompanyId='".$_SESSION['ACGCompanyId']."' and created_by IN(".$lowerlist.") and dealerid='".$_SESSION['dealerid']."' and Convert_IntoAccountStatus='Y'";
            }
            else
            {
                    $getleadNew1="select * from crm_leads where CompanyId='".$_SESSION['ACGCompanyId']."' and created_by IN(".$lowerlist.") and dealerid=0 and Convert_IntoAccountStatus='Y'";
            }	
	}
}
else
{
	// Dealer And User Check
	if($_SESSION['dealerid'])
	{
	$getleadNew1="select * from crm_leads where CompanyId='".$_SESSION['ACGCompanyId']."' and created_by IN(".$lowerlist.") and dealerid='".$_SESSION['dealerid']."' and Convert_IntoAccountStatus='Y'";
	}
	else
	{
		$getleadNew1="select * from crm_leads where CompanyId='".$_SESSION['ACGCompanyId']."' and created_by IN(".$lowerlist.") and dealerid=0 and Convert_IntoAccountStatus='Y'";
	}
		
	
}
	
//echo $getleadNew1;
$getleadNew=Db_query($getleadNew1,$db);
if(DB_num_rows($getleadNew)>0)
{

	 while($row_lead=DB_fetch_array($getleadNew))
	 {
	 ?>
  <tr bgcolor="#fff">
    <td>
   <input name="leadid[]" type="checkbox" id="leadid_<?php echo $i; ?>"   value="<?php  echo $row_lead['leadid']?>" />
<?php
	$query2="select person from contact_information_master where master_code='".$row_lead['company_name']."'";
	$getitem2= DB_query($query2, $db);
	$contact_row=DB_fetch_array($getitem2);
	$query="select leadid from crm_leads where created_by='".$_SESSION['UserID']."'";
	$getitem= DB_query($query, $db);
	$n1= DB_num_rows($getitem);
	$query1="select id from crm_lead_groups_users where parent_id='".$row_lead['leadid']."' and user_id='".$_SESSION['UserID']."'";
	$getitem1= DB_query($query1, $db);
	$n2= DB_num_rows($getitem1);
?>
  </td>
  <td><?php  echo $row_lead['leadcode'];// ConvertSQLDate($row_lead['created_date'])?></td>
  <td><?php echo ConvertSQLDate($row_lead['created_date']); ?></td>
  <td><?php    //echo $row_lead['company_name'];
if(is_numeric($row_lead['company_name'])){echo $s->GetFieldValue('clientmaster',"id='".$row_lead['company_name']."'",'c_name');}else{echo $row_lead['company_name'];}
	?></td>
    <td><?php if(is_numeric($row_lead['company_name'])){ echo $contact_row['person'];}else {echo $row_lead['contact_name']; } ?></td>
        <td><?php if(is_numeric($row_lead['company_name'])){ echo $s->GetFieldValue('contact_information_master',"master_code='".$row_lead['company_name']."'",'email'); }else {echo $row_lead['cemail']; }?></td>
        <td><?php if(is_numeric($row_lead['company_name'])){ echo $s->GetFieldValue('contact_information_master',"master_code='".$row_lead['company_name']."'",'phone'); } else{echo $row_lead['mobile']; }?></td>
      <td width="3%"><?php if($_SESSION['frmEdit']==0){ ?>
  <img src="images/icon-edit.png" width="15" alt="Not Allowed to Edit Document" title="Not Allowed to Edit Document" height="15" />
<?php }else{ ?> 
 <a href="index.php?route=CRM/crmleadview&action=Edit&leadid=<?php echo $row_lead['leadid'].$rurl;?>">
  <img src="images/icon-edit.png" width="15" height="15" title="Edit Document" /></a>
<?php } ?></td>
	 <td width="3%"><?php 
 if(($_SESSION['frmView']==0 && $n1<0) || ($_SESSION['frmView']==0 && $n2<0)){ ?>
  <img src="images/search.gif" width="15" alt="Not Allowed" title="Not Allowed to View Document"  height="15" />
<?php }else{ ?>
   <a href="index.php?route=CRM/LeadView&action=View&leadid=<?php echo $row_lead['leadid'].$rurl;?>">
  <img src="images/search.gif" width="15" height="15" title="View Document" /></a>
<?php } ?></td>
   </tr>
   <?php $i++; } 
   }
   else
   {?>
   	<tr>
   		<td colspan="9"><font color="#FF0000"><?php echo "No Records Found"; ?></font></td>
   		</tr>
   	
  <?php }?>
</table>

</fieldset>
</div>
    


</div>


		
	<div style="clear:both;"></div>
	</div>
</div>
  <script type="text/javascript">
	Element.cleanWhitespace('content');
	init();
</script>

</form>
</div>
</div>