<?php


session_start();
if($HTTP_SESSION_VARS['dbName']==""){
header("location:../index.php");
}
	 
print <<< HERE

<HTML>
<HEAD>
<TITLE> List Of Clients </TITLE>

<style type="text/css">

table.master th { font-size: 14px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
				  border-top: 2 solid #000000;
				  border-bottom: 1 solid #000000;
				 }
table.master td { font-size: 14px; 
                  font-family:Times-New-Roman;
                 }

</style>


</HEAD>


HERE;

include("config.inc.php");
include 'function.inc.php';


$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass'])
		or die("Could not connect to DB");

mysql_select_db($HTTP_SESSION_VARS['dbName'],$connection) or die("Could not find DB");

//maroun 30.03.2011
$querylog="SELECT print as printaccess from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' 	and Upper(Login_Schema) = Upper('".$HTTP_SESSION_VARS['dbName']."')	";
	$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($resultlog)){
		extract($rows);
	}


if ($printaccess==0 or $printaccess=='')
echo'<BODY oncontextmenu="return false;">';
else
echo'<BODY>';
						
$querysetup= " select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, report_color1, report_color2 grid_color1, grid_color2, ".
             " Date_Format(Setup_VatStartDate,'%Y%m%d') As Setup_VatStartDate, ifNull(Setup_VatPercentage,0) As Setup_VatPercentage from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}
	//User Access
$querylog=" select ifNull(login_belongstogrp,0) As FI_BelongsToGrp from ".$HTTP_SESSION_VARS['dbLabel'].".login where user_account='".$HTTP_SESSION_VARS['useraccount']."' and Upper(Login_Schema) = Upper('".$HTTP_SESSION_VARS['dbName']."')  ";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}

$now=current_server_time($connection);
$dt=split(" ",$now[0]);
$printdate=split("-",$dt[0]);



$flag=0;
    echo "<table border=0 width=100% cellspacing=0 cellpadding=0><tr><td align='left'></td></tr></table>";
	echo "<table border=0 width='950' cellspacing=0 cellpadding=0 class='master' style='table-layout:fixed;' >
		  <col width=120>
		  <col width=470>
		  <col width=300> 
		  <col width=150>";   

	ShowReportHeader1('Client Report','portrait', $connection);

	   

if ( $groupby == 8 ) 
		$query = "select ledger_number , ledger_name , client_address , client_telephone1  from clients";
else
	if ( $groupby == 1 )
		$query= "select  c.ledger_number , c.ledger_name , c.client_address , c.client_telephone1 , c.salesmen_code ,  s.ledger_name
				from clients c
				left join salesmen s on s.ledger_number = c.salesmen_code
				order by s.ledger_number";
else
	if ( $groupby == 2 )
		$query= "select  c.ledger_number , c.ledger_name , c.client_address , c.client_telephone1 , c.project_code ,  p.project_name
				 from clients c
				 left join project p on p.project_code = c.project_code
				 order by p.project_code";
				 
else
	if ($groupby == 3)
		$query= "select  s.ledger_number , s.ledger_name , s.client_address , s.client_telephone1 , s.costcenter_code ,  c.costcent_name
				 from clients s
				 left join costcent c on c.costcent_code = s.costcenter_code
				 order by c.costcent_code";
else
	if ($groupby == 4)
		$query= "select  c.ledger_number , c.ledger_name , c.client_address , c.client_telephone1 , c.client_sellingprice
				 from clients c
				 order by c.client_sellingprice";
				 
else
	if ($groupby == 5)
		$query= "select  c.ledger_number , c.ledger_name , c.client_address , c.client_telephone1 , c.client_export
				 from clients c
				 order by c.client_export";
else
	if ($groupby == 6)
		$query= "select  c.ledger_number , c.ledger_name , c.client_address , c.client_telephone1 , c.client_local
				 from clients c
				 order by c.client_local";
				 
else
	if ($groupby == 7)
		$query= "select  c.ledger_number , c.ledger_name , c.client_address , c.client_telephone1 , c.client_vat
				 from clients c
				 order by c.client_vat";
				 
$colorblue = 'background-color:#ABD4F1';
$colorred = 'background-color : red';
$styletd = 'border-right:1px solid #d9d9d9; padding-left:2px;';
$styleth = 'border-right:1px solid #d9d9d9; border-left:1px solid #d9d9d9; border-top:1px solid #d9d9d9; border-bottom:1px solid #d9d9d9; color:#404040';
$styleth1 = 'border-right:1px solid #d9d9d9;  border-top:1px solid #d9d9d9; border-bottom:1px solid #d9d9d9; color:#404040';
$stylegroup = 'background-color:lightgrey; border-right:1px solid #d9d9d9 ; border-left:1px solid #d9d9d9';
	
	echo "<table border=0 cellspacing=0 cellpadding=0>
		  <col width=150>
		  <col width=470>
		  <col width=350> 
		  <col width=80>";
	echo "<tr height='20'>";
	echo "<td align='center' style='$styleth'><b>Number</b></td>";
	echo "<td align='center' style='$styleth1'><b>Name</b></td>";
	echo "<td align='center' style='$styleth1'><b>Address</b></td>";
    echo"<td align='center' style='$styleth1'><b>Phone <b></td>";
    echo "</tr>";
	
$col=0;	
$temp_code = '*';
$result = mysql_query ($query , $connection);
while ( $row = mysql_fetch_row ($result ) ) {
	
		if ( $groupby == 8 ) {
			if ( $col%2==0) {
		echo "<tr height='20' style='$colorblue'>";
		echo "<td align='center' style='$styletd ; border-left:1px solid #d9d9d9;'> $row[0]</td>";
		echo "<td align='center' style='$styletd'>$row[1]</td>";
		echo "<td align='center' style='$styletd'>$row[2]</td>";
		echo"<td align='center' style='$styletd'>$row[3] </td>";
		echo "</tr>";
		}
		else {
		echo "<tr height='20'>";
		echo "<td align='center' style='$styletd; border-left:1px solid #d9d9d9'>$row[0]</td>";
		echo "<td align='center' style='$styletd'>$row[1]</td>";
		echo "<td align='center' style='$styletd'>$row[2]</td>";
		echo"<td align='center' style='$styletd'> $row[3] </td>";
		echo "</tr>";
		}
		}

		if ( $groupby == 1 ) {
			
			if ( $temp_code != $row[4] ) {
			echo "<tr style='background-color:#e6e6e6;' height='30px'> 
					   <td colspan='2' style='border-left:1px solid #d9d9d9;  border-bottom:1px solid #d9d9d9 ; border-top:1px solid #d9d9d9'><b> Salesmen Code: $row[4] </b> </td>
					   <td colspan='2' style='border-right:1px solid #d9d9d9;  border-bottom:1px solid #d9d9d9; border-top:1px solid #d9d9d9'><b> Salesmen Name: &nbsp; $row[5] </b></td>
			</tr>";}
			if ( $col%2==0) {
		echo "<tr height='20' style='$colorblue'>";
		echo "<td align='center' style='$styletd ; border-left:1px solid #d9d9d9;'> $row[0]</td>";
		echo "<td align='center' style='$styletd'>$row[1]</td>";
		echo "<td align='center' style='$styletd'>$row[2]</td>";
		echo"<td align='center' style='$styletd'>$row[3] </td>";
		echo "</tr>";
		}
		else {
		echo "<tr height='20'>";
		echo "<td align='center' style='$styletd; border-left:1px solid #d9d9d9'>$row[0]</td>";
		echo "<td align='center' style='$styletd'>$row[1]</td>";
		echo "<td align='center' style='$styletd'>$row[2]</td>";
		echo"<td align='center' style='$styletd'> $row[3] </td>";
		echo "</tr>";
		}
			$temp_code = $row[4];
			}

		if ( $groupby == 2 ) {
			
			if ( $temp_code != $row[4] ) {
			echo "<tr style='background-color:#e6e6e6;' height='30px'> 
					   <td style='border-left:1px solid #d9d9d9;  border-bottom:1px solid #d9d9d9 ; border-top:1px solid #d9d9d9' colspan='2'><b> Project Code: $row[4] </b> </td>
					   <td colspan='2' style='border-right:1px solid #d9d9d9;  border-bottom:1px solid #d9d9d9; border-top:1px solid #d9d9d9'><b>Project Name &nbsp; $row[5] </b></td>
			</tr>";}
			if ( $col%2==0) {
		echo "<tr height='20' style='$colorblue'>";
		echo "<td align='center' style='$styletd ; border-left:1px solid #d9d9d9;'> $row[0]</td>";
		echo "<td align='center' style='$styletd'>$row[1]</td>";
		echo "<td align='center' style='$styletd'>$row[2]</td>";
		echo"<td align='center' style='$styletd'>$row[3] </td>";
		echo "</tr>";
		}
		else {
		echo "<tr height='20'>";
		echo "<td align='center' style='$styletd; border-left:1px solid #d9d9d9'>$row[0]</td>";
		echo "<td align='center' style='$styletd'>$row[1]</td>";
		echo "<td align='center' style='$styletd'>$row[2]</td>";
		echo"<td align='center' style='$styletd'> $row[3] </td>";
		echo "</tr>";
		}
			$temp_code = $row[4];
			}
			
		if ( $groupby == 3 ) {
			
			if ( $temp_code != $row[4] ) {
			echo "<tr style='background-color:#e6e6e6;' height='30px'>
					<td colspan='2' style='border-left:1px solid #d9d9d9;  border-bottom:1px solid #d9d9d9 ; border-top:1px solid #d9d9d9'><b> Cost Center Code: $row[4] </b> </td>
					<td colspan='2' style='border-right:1px solid #d9d9d9;  border-bottom:1px solid #d9d9d9; border-top:1px solid #d9d9d9'><b> Cost Center Name: &nbsp; $row[5] </b></td>			
			</tr>";}
			if ( $col%2==0) {
		echo "<tr height='20' style='$colorblue'>";
		echo "<td align='center' style='$styletd ; border-left:1px solid #d9d9d9;'> $row[0]</td>";
		echo "<td align='center' style='$styletd'>$row[1]</td>";
		echo "<td align='center' style='$styletd'>$row[2]</td>";
		echo"<td align='center' style='$styletd'>$row[3] </td>";
		echo "</tr>";
		}
		else {
		echo "<tr height='20'>";
		echo "<td align='center' style='$styletd; border-left:1px solid #d9d9d9'>$row[0]</td>";
		echo "<td align='center' style='$styletd'>$row[1]</td>";
		echo "<td align='center' style='$styletd'>$row[2]</td>";
		echo"<td align='center' style='$styletd'> $row[3] </td>";
		echo "</tr>";
		}
			$temp_code = $row[4];
			}		
	
		if ( $groupby == 4 ) {
			
			if ( $temp_code != $row[4] ) {
			echo "<tr style='background-color:#e6e6e6;' height='30px'> 
					   <td style='border-left:1px solid #d9d9d9;  border-bottom:1px solid #d9d9d9 ; border-top:1px solid #d9d9d9; border-right:1px solid #d9d9d9' colspan='4' align='center';><b> Selling Price : $row[4] </b> </td>
			</tr>";}
			if ( $col%2==0) {
		echo "<tr height='20' style='$colorblue'>";
		echo "<td align='center' style='$styletd ; border-left:1px solid #d9d9d9;'> $row[0]</td>";
		echo "<td align='center' style='$styletd'>$row[1]</td>";
		echo "<td align='center' style='$styletd'>$row[2]</td>";
		echo"<td align='center' style='$styletd'>$row[3] </td>";
		echo "</tr>";
		}
		else {
		echo "<tr height='20'>";
		echo "<td align='center' style='$styletd; border-left:1px solid #d9d9d9'>$row[0]</td>";
		echo "<td align='center' style='$styletd'>$row[1]</td>";
		echo "<td align='center' style='$styletd'>$row[2]</td>";
		echo"<td align='center' style='$styletd'> $row[3] </td>";
		echo "</tr>";
		}
			$temp_code = $row[4];
			}
	
	
		if ( $groupby == 5 ) {
			
			if ( $temp_code != $row[4] ) {
			echo "<tr style='background-color:#e6e6e6;' height='30px'> 
					   <td style='border-left:1px solid #d9d9d9;  border-bottom:1px solid #d9d9d9 ; border-top:1px solid #d9d9d9 ; border-right:1px solid #d9d9d9' colspan='4' align='center';><b> Export : $row[4] </b> </td>
			</tr>";}
			if ( $col%2==0) {
		echo "<tr height='20' style='$colorblue'>";
		echo "<td align='center' style='$styletd ; border-left:1px solid #d9d9d9;'> $row[0]</td>";
		echo "<td align='center' style='$styletd'>$row[1]</td>";
		echo "<td align='center' style='$styletd'>$row[2]</td>";
		echo"<td align='center' style='$styletd'>$row[3] </td>";
		echo "</tr>";
		}
		else {
		echo "<tr height='20'>";
		echo "<td align='center' style='$styletd; border-left:1px solid #d9d9d9'>$row[0]</td>";
		echo "<td align='center' style='$styletd'>$row[1]</td>";
		echo "<td align='center' style='$styletd'>$row[2]</td>";
		echo"<td align='center' style='$styletd'> $row[3] </td>";
		echo "</tr>";
		}
			$temp_code = $row[4];
			}
		if ( $groupby == 6 ) {
			
			if ( $temp_code != $row[4] ) {
			echo "<tr style='background-color:#e6e6e6;' height='30px'> 
					   <td style='border-left:1px solid #d9d9d9;  border-bottom:1px solid #d9d9d9 ; border-top:1px solid #d9d9d9; border-right:1px solid #d9d9d9' colspan='4' align='center';><b> Local : $row[4] </b> </td>
			</tr>";}
			if ( $col%2==0) {
		echo "<tr height='20' style='$colorblue'>";
		echo "<td align='center' style='$styletd ; border-left:1px solid #d9d9d9;'> $row[0]</td>";
		echo "<td align='center' style='$styletd'>$row[1]</td>";
		echo "<td align='center' style='$styletd'>$row[2]</td>";
		echo"<td align='center' style='$styletd'>$row[3] </td>";
		echo "</tr>";
		}
		else {
		echo "<tr height='20'>";
		echo "<td align='center' style='$styletd; border-left:1px solid #d9d9d9'>$row[0]</td>";
		echo "<td align='center' style='$styletd'>$row[1]</td>";
		echo "<td align='center' style='$styletd'>$row[2]</td>";
		echo"<td align='center' style='$styletd'> $row[3] </td>";
		echo "</tr>";
		}
			$temp_code = $row[4];
			}	
	
		if ( $groupby == 7 ) {
			
			if ( $temp_code != $row[4] ) {
			echo "<tr style='background-color:#e6e6e6;' height='30px'> 
					   <td style='border-left:1px solid #d9d9d9;  border-bottom:1px solid #d9d9d9 ; border-top:1px solid #d9d9d9; border-right:1px solid #d9d9d9' colspan='4' align='center';><b> Client VAT : $row[4] </b> </td>
			</tr>";}
			if ( $col%2==0) {
		echo "<tr height='20' style='$colorblue'>";
		echo "<td align='center' style='$styletd ; border-left:1px solid #d9d9d9;'> $row[0]</td>";
		echo "<td align='center' style='$styletd'>$row[1]</td>";
		echo "<td align='center' style='$styletd'>$row[2]</td>";
		echo"<td align='center' style='$styletd'>$row[3] </td>";
		echo "</tr>";
		}
		else {
		echo "<tr height='20'>";
		echo "<td align='center' style='$styletd; border-left:1px solid #d9d9d9'>$row[0]</td>";
		echo "<td align='center' style='$styletd'>$row[1]</td>";
		echo "<td align='center' style='$styletd'>$row[2]</td>";
		echo"<td align='center' style='$styletd'> $row[3] </td>";
		echo "</tr>";
		}
			$temp_code = $row[4];
			}
	
	$col++;
} // end of while 

echo "<tr height=1> <td colspan='4' style=' border-top:1px solid #9494b8' ></td></table>";

function current_server_time($connection){

  $string="Select date_format(NOW(),'%Y-%m-%d %h:%i %p') ";
  $result=mysql_query($string,$connection) or die(mysql_error());
  $now=mysql_fetch_row($result);

  return $now;
}

function explodeword($date)
{

	$trim = explode('/',$date);
	$trim = "$trim[2]"."-"."$trim[1]"."-"."$trim[0]";
	return $trim;
}

if ($printaccess==1)
print <<< HERE





<script language=javascript>
if (typeof(window.print) != 'undefined')
{
    window.print();
}
</script>
</BODY>
</HTML>
HERE;
?>