<?php 
include('functions.php');
include('includes/Class_Client.php');
include('Class_PickListEditor.php');

//echo $_SESSION['ACGCompanyId'];
//exit;	


$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'];


$Client1=array();
$ZoneSQL=DB_query("select * from www_users where id in($lowerlist) and assigned_sites in(".$_SESSION['AssignedSites'].")",$db);

while($ZoneClient=DB_fetch_array($ZoneSQL))
{
	$Client1[]=$ZoneClient['id'];
}
$Client2=implode(",",$Client1);


$catid= $s->GetFieldValue('crm_picklist',"picklist_title='Leads' and status=1",'picklist_id');
$table="crm_accounts";
if($_REQUEST['Delete'])
{
	for($i=0;$i<count($_REQUEST['quotationid']);$i++)
	{
	$cond="parentId='".$_REQUEST['quotationid'][$i]."'";
	$row= $s->DeleteRecord('quotation_details', $cond);
	
	$cond="id='".$_REQUEST['quotationid'][$i]."'";
	$row= $s->DeleteRecord('quotation', $cond);
	}	
}

$appSql=DB_query("select quotation_flag from quotation_app_flag where company_id='".$_SESSION['ACGCompanyId']."'",$db);
$appResult=DB_fetch_array($appSql);
if($_REQUEST['action']=='approval')
 { 
     $dataarr['approval_status']='1'; 
      $dataarr['approved_by']=$_SESSION['UserID'];
       
    $con = "id = '".$_REQUEST['SID']."'";
    $s->RecordEdit('quotation',$dataarr,$con);
 }
?>

<style type="text/css">
<!--
.style1 {font-weight: bold}
-->
</style>
<script language="javascript" >
function LeadData()
{	
	myCheckboxArray = document.myform.elements["quotationid[]"];
	var t=myCheckboxArray.length;
	if(document.getElementById('quotationid').checked==true)
	{	
	
		if(t==undefined)
	
		document.getElementById('quotationid_'+1).checked=true;		
			for (var i = 1; i <= t; i++)
	{
	document.getElementById('quotationid_'+i).checked=true;	
	}	
	}
	else
	{

	if(t==undefined)		
		document.getElementById('quotationid_'+1).checked=false;		
			
	for (var i = 1; i <= t; i++)
	{
	document.getElementById('quotationid_'+i).checked=false;	
	}
	}
	

	
}
</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%" border="0" cellspacing="0" cellpadding="3" >
  <tr class="row1">
    <td class="tabledata1">Branch</td>
	<td>
	<select name="site"  style="width:150px;">
<!--	<option value="0">--All--</option>	-->
	<?php
	$Sitesql="select * from locations where  CompanyId='".$_SESSION['ACGCompanyId']."' and loccode IN ('".$_SESSION['AssignedBranch']."') ";
	$SiteRes=DB_query($Sitesql,$db);
	while($SiteRow=DB_fetch_array($SiteRes))
	{?>
	<option value="<?php  echo $SiteRow['loccode'];?>" <?php  echo $_REQUEST['site']==$SiteRow['loccode']?'selected':''?> ><?php  echo $SiteRow['locationname'];?></option>
	<?php 
	}
	?>
	</select></td>
	<td class="tabledata1">To:</td>
	<td><input name="todate" type="text"  class="datepicker" style="width:150px;"/></td>
	</tr>
	<tr class="row1">
	<td class="tabledata1">From:</td>
	<td colspan="3"><input name="fromdate" type="text"  class="datepicker" style="width:150px;"/></td>
	</tr>
	<tr class="row1">
	<td colspan="4"><p align="center"><input name="Search" type="submit"  value="Search" class="search"/></p></td>
  </tr>
</table> 
</form>
</div>

<form method="post" name="myform">	
<p align="left">
<?php if($_SESSION['frmAdd']==0){ ?> 
<input type="button" name="Create" value="Add Process Quote" alt="Not Allowed" title="Not Allowed" class="search" /><input type="button" name="Create" value="Add Direct Quote" alt="Not Allowed" title="Not Allowed" class="search" />
<?php }else{?>
<a href="<?php echo "index.php?route=CRM/CRMQuotes".$rurl.'&action=AddNew'?>"><input type="button" name="Create" value="Add Process Quote" class="search" /></a>
<a href="<?php echo "index.php?route=CRM/CRM_Dir_Quotes".$rurl.'&action=AddNew'?>"><input type="button" name="Create" value="Add Direct Quote" class="search" /></a>

<?php }?>

<?php if($_REQUEST['Search'])
{?>
<a href="QuotesExp.php"><input type="button" name="Export" value="Export" class="search" /></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 }?>
</p>
<table width="100%" border="1" cellspacing="0" cellpadding="3">
	<tr class="row1">
		<td class="tabledata"><input name="quotationid" type="checkbox"   id="quotationid" onclick="LeadData()"    /></td>
		<td class="tabledata">Quote No</td>
		<td class="tabledata">Date</td>
		<!--<td width="197"><center>Subject</center></td>
		<th width="20%">Quote Stage</th>-->
		<td class="tabledata">Party Name</td>
		<!--<td class="tabledata">Contact Person</td>-->
		<td class="tabledata">Mobile No</td>
		<td class="tabledata">Valid Till</td>
<!--		<td class="tabledata">Basic Amount</td>-->
		<!--<th width="205">Excise Duty</th>-->
		<td class="tabledata">Grand Total</td>
		<td class="tabledata">Created By</td>
                 <?php if($appResult['quotation_flag']=='1'){?><td class="tabledata"><strong>Status</strong></td><?php } ?>
                 <?php if($appResult['quotation_flag']=='1'){?><td class="tabledata"><strong>Approved By</strong></td><?php } ?>
		<td class="tabledata">Action</td>
	</tr>
<?php 
//$s=getUnderRoles($_SESSION['ACGRoleId'],$_SESSION['ACGCompanyId']);
//print_r($s);
//$user_ids=$_SESSION['UserID'].",".implode(',',$s);
//echo $user_ids;

//if($_SESSION['ACG_DashBoard_User']!='')
//{
//$user_id=$_SESSION['ACG_DashBoard_User'];
if($_REQUEST['Search'])
{
	
$todate=ConvertSQLDate($_REQUEST['todate']);
$fromdate=ConvertSQLDate($_REQUEST['fromdate']);
$site=$_REQUEST['site'];
	if($_REQUEST['todate']!='' && $_REQUEST['fromdate']!='' )
	{
		$filter[]="quotationDate between '".$todate."' and '".$fromdate."'";
	}
 	if($_REQUEST['site']!='0')
	{
		$filter[]="site='".$site."'";
	}
	//if($_REQUEST['site']=='0')
	//{
		// $GetLead="select * from quotation where  createdBy in(".$lowerlist.")  and  SalesOrderNo Is Null  ";
		//$getlead=DB_query(  "select * from quotation where  createdBy IN ('".$lowerlist."')  and  CompanyId='".$_SESSION['ACGCompanyIds']."'  ",$db);
	//}
if($filter!='')
{
	$filter1=implode(" and ",$filter);
	
        if($_SESSION['dealerid'])
	{
     $GetLead=  "select * from quotation where  created_by IN ('".$lowerlist."')  and CompanyId='".$_SESSION['ACGCompanyIds']."' and $filter1 and dealerid='".$_SESSION['dealerid']."' order by id desc";
        }
        else {
            $GetLead=  "select * from quotation where  created_by IN ('".$lowerlist."')  and CompanyId='".$_SESSION['ACGCompanyIds']."' and $filter1 and dealerid=0 order by id desc";
        }
	}
}
else 
{
	
$i=1;
if($_SESSION['dealerid'])
	{
     $GetLead=  "select * from quotation where created_by IN($Client2) and CompanyId='".$_SESSION['ACGCompanyId']."'  and  ConvertIntoSales='N' and dealerid='".$_SESSION['dealerid']."' order by id desc";
        }
 else {
     $GetLead=  "select * from quotation where created_by IN($Client2) and CompanyId='".$_SESSION['ACGCompanyId']."'  and  ConvertIntoSales='N' and dealerid=0 order by id desc";
     }
//echo	"select * from quotation where  createdBy='".$_SESSION['UserID']."' and CompanyId='".$_SESSION['ACGCompanyId']."'  and  ConvertIntoSales='N'  ";
    
}
//echo $GetLead;
$getlead=DB_query($GetLead,$db);
//}

$s=new db_query();
$_SESSION['quotesID']=$GetLead;
if(DB_num_rows($getlead)>0)
{
	 while($row_quotes=DB_fetch_array($getlead))
	 {
?>
    <tr bgcolor="#dfdfdf">
    <td>
   <input name="quotationid[]" type="checkbox" id="quotationid_<?php echo $i; ?>"   value="<?php  echo $row_quotes['id']?>" />
 </td>
  <td style="padding-left:5px; padding-right:5px;"><?php  echo $row_quotes['quotationNo'];// ConvertSQLDate($row_lead['created_date'])?></td>
  <td style="padding-left:5px; padding-right:5px;"><?php  echo ConvertSQLDate($row_quotes['quotationDate']); ?></td>
   <!-- <td><center><?php  echo $row_quotes['quotation_subject'];?></center></td>

	<td style="padding-left:5px; padding-right:5px;"><?php  echo $row_quotes['quotation_stage']?></td>-->
	
	<td style="padding-left:5px; padding-right:5px;"><?php	echo $company_name=$s->GetFieldValue('clientmaster',"id='".$row_quotes['party']."'",'c_name'); 	?></td>
 	<!--<td><?php echo $s->GetFieldValue('contact_information_master',"id='".$row_quotes['contactPerson']."'",'person'); ?></td>-->
 	<td><?php echo $row_quotes['partyContact']; ?></td>
 	<td><?php echo ConvertSQLDate($row_quotes['validTill']); ?></td>
<!-- 	<td><?php echo $row_quotes['basicAmount']; ?></td>-->
 	<!--<td><?php echo $row_quotes['totalDiscountedPrice']; ?></td>-->
 	<td style="padding-left:5px; padding-right:5px;"><?php  echo $row_quotes['grandTotal']?></td>
		<td style="padding-left:5px; padding-right:5px;"><?php  
			$Sql1=DB_query("Select * from www_users where id='".$row_quotes['created_by']."'",$db);
	$Res1=DB_fetch_array($Sql1);
		
	echo $Res1['userid'];
			?></td>
        <td> <?php if($appResult['quotation_flag']=='1'){?>
           <?php  if($row_quotes['approval_status']=='0'){?>
           <a href="<?php echo "index.php?route=CRM/ViewQuotes".$rurl.'&action=approval'?>&SID=<?php echo $row_quotes['id'];?>" onclick="return confirm('Are You Sure To Approved ?');"><input type="button" name="Approval_button" value="Approve" class="search" /></a><?php }else{ 
              echo 'Approved'; }?><?php } ?></td>
        <td> <?php if($appResult['quotation_flag']=='1'){?>
           <?php  if($row_quotes['approval_status']=='0'){ echo '';}else{
               echo $s->GetFieldValue('www_users',"id='".$row_quotes['approved_by']."'",'realname'); }?><?php } ?></td>
			<td><a href="#" onclick="popup('quatation_pdf.php?id=<?php  echo $row_quotes['id']; ?>',1200,600)"><img src="images/search.gif" width="10px"  height="10" /></a>
			<?php if($row_quotes['process_quote_type']!='Direct') { ?>
 <?php if($_SESSION['frmEdit']==0){ ?>
  <img src="images/icon_edit.gif" width="10px" alt="Not Allowed" title="Not Allowed"  height="10" />
  <?php } else { ?>
  <a href="index.php?route=CRM/CRMQuotes&action=Edit&QID=<?php echo $row_quotes['id'].$rurl;?>">
  <img src="images/icon_edit.gif" width="10px"  height="10" /></a>
  <?php  } ?>
  <?php } else { ?>
  <?php if($_SESSION['frmEdit']==0){ ?>
  <img src="images/icon_edit.gif" width="10px" alt="Not Allowed" title="Not Allowed"  height="10" />
  <?php } else { ?>
  <a href="index.php?route=CRM/CRM_Dir_Quotes_Edit&action=Edit&QID=<?php echo $row_quotes['id'].$rurl;?>">
  <img src="images/icon_edit.gif" width="10px"  height="10" /></a> 
  <?php  } ?>
  <?php  } ?>
  
  
  
			</td>
   </tr>
   <?php $i++; }  }
   else
   {?>
   	<tr>
   	<td colspan="8">
   	<font color="#FF0000"><?php echo "No Records Found"; ?></font></td>
   		</tr>
  <?php }?>
</table>
</form>
</div>
</div>