<?php 
include('CRM/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'];
//echo "<pre>";print_r($_SESSION);

$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);


$table="manufacturing_advice";
if($_REQUEST['Delete'])
{
	for($i=0;$i<count($_REQUEST['adviceid']);$i++)
	{
	 $cond1="advice_id='".$_REQUEST['adviceid'][$i]."'";
	 $row= $s->DeleteRecord('manufacturing_advice_items', $cond1);
	
	 $cond2="id='".$_REQUEST['adviceid'][$i]."'";
	 $row= $s->DeleteRecord('manufacturing_advice', $cond2);
	}	
}
?>

<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">Party</td>
	<td>
		<select name="partyid" style="width:150px;"  id="partyid">
			<option value="000">Select</option>
			<?php
			//echo  $_SESSION['UserSites'];
			$client_sql=DB_query("Select * from clientmaster where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
			  while($client_row=DB_fetch_array($client_sql)){ ?>
			<option value="<?php echo $client_row['id'];?>"<?php echo($party==$client_row['id']?'selected':'')?>>
			<?php  echo $client_row['c_name'];?></option> <?php } ?>
		</select> 
	</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">

<a href="<?php echo "index.php?route=PMS/Transcations/manufacturing_advice".$rurl.'&action=AddNew'?>"><input type="button" name="Create" value="Add Advice" class="search" /></a>

<?php if($_REQUEST['Search'])
{?>
<a href="QuotesExp.php"><input type="button" name="Export" value="Export" class="search" /></a>
<?php }?>

<input type="submit" name="Delete" value="Delete" class="search" />
</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">Advice No</td>
		<td class="tabledata">Date</td>
		<td class="tabledata">Project Name</td>
		<td class="tabledata">Party Name</td>
		<td class="tabledata">Prepared By</td>
		<td class="tabledata">Print</td>
	</tr>
<?php 
if($_REQUEST['Search'])
{
	
$site=$_REQUEST['site'];
	
 	if($_REQUEST['site']!='0')
	{
		$filter[]="branchid='".$site."'";
	}
	
	if($_REQUEST['partyid']!='0')
	{
		$filter[]="partyid='".$site."'";
	}
	
if($filter!='')
{
	$filter1=implode(" and ",$filter);
     $GetLead="select * from $table where prepared_by in(".$lowerlist.") and $filter1 ";
	 $getlead=DB_query(  "select * from quotation where  createdBy IN ('".$lowerlist."')  and CompanyId='".$_SESSION['ACGCompanyIds']."' and $filter1 ",$db);
	}
}
else 
{
	
$i=1;
     $GetLead=  "select * from $table where prepared_by IN($Client2) and CompanyId='".$_SESSION['ACGCompanyId']."'";
//echo	"select * from quotation where  createdBy='".$_SESSION['UserID']."' and CompanyId='".$_SESSION['ACGCompanyId']."'  and  ConvertIntoSales='N'  ";
    $getlead=DB_query(  "select * from $table where prepared_by IN($Client2) and CompanyId='".$_SESSION['ACGCompanyId']."' order by id desc",$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="adviceid[]" type="checkbox" id="adviceid_<?php echo $i; ?>"   value="<?php  echo $row_quotes['id']?>" />
 
  <a href="index.php?route=PMS/Transcations/manufacturing_advice&action=Edit&MID=<?php echo $row_quotes['id'].$rurl;?>">
  <img src="images/icon_edit.gif" width="10px"  height="10" /></a> 
  
  
  </td>
  <td style="padding-left:5px; padding-right:5px;"><?php  echo $row_quotes['advice_no'];// ConvertSQLDate($row_lead['created_date'])?></td>
  <td style="padding-left:5px; padding-right:5px;"><?php  echo ConvertSQLDate($row_quotes['advice_date']); ?></td>
	<td style="padding-left:5px; padding-right:5px;"><?php  echo $row_quotes['project_name']; ?></td>
	<td style="padding-left:5px; padding-right:5px;"><?php	echo $company_name=$s->GetFieldValue('clientmaster',"id='".$row_quotes['partyid']."'",'c_name'); 	?></td>
		<td style="padding-left:5px; padding-right:5px;"><?php  
			$Sql1=DB_query("Select * from www_users where id='".$row_quotes['prepared_by']."'",$db);
	$Res1=DB_fetch_array($Sql1);
		
	echo $Res1['userid'];
			?></td>
			<td><a href="#" onclick="popup('quatation_pdf.php?id=<?php  echo $row_quotes['id'].$rurl;?>',1200,600)">Print</a></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>
