<?php
//*********************START HERE*********************AMITSINGH****************30-10-2012******************************

$PageSecurity = 2;
include('includes/session.inc');
include('includes/SQL_CommonFunctions.inc');
include('includes/function_lib.php');


$ClientSQL=DB_query("Select * from clientmaster where CompanyId='".$_SESSION['ACGCompanyId']."' and  Site='".$_SESSION['AssignedBranch']."' 
		and Zone in(".$_SESSION['AssignedSites'].")",$db);

 ?>
<table width="100%" border="1" cellspacing="0" cellpadding="3">
  <tr>
    <td class="tabledata">Party Name</td>
    <td class="tabledata">Pending Dispatch</td>
  </tr>
<?php  while($DistinctClient=DB_fetch_array($ClientSQL)) { 



$SQlClint=DB_query("SELECT COUNT(*) AS TotalPending 
FROM salesorder 
INNER JOIN salesorder_details 
ON (salesorder.id=salesorder_details.masterId) 
WHERE  
salesorder_details.ApprovalStatus='Approved' and
salesorder.client='".$DistinctClient['id']."'
ORDER BY TotalPending DESC",$db);

$abc=DB_fetch_array($SQlClint);

$dataarray[]=array('client' => $DistinctClient['c_name'], 'basic' => $abc['TotalPending']);	
}
foreach ($dataarray as $key => $row)
  {   
   $volume[$key]  = $row['basic'];
   $edition[$key] = $row['client'];
  }  
array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $dataarray);
 $k=0;
 foreach ($dataarray as $key => $row2)
   {

if($row2['basic']!='0'){
?>  
  <tr>
    <td class="tabledata_fist"><?php echo $row2['client']; ?></td>
    <td class="tabledata1"><?php echo $row2['basic']; ?></td>
  </tr>
<?php } } ?> 
</table>