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

include("config.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");
$SalesmanCode = $_GET['SalesmanCode'];
$SalesmanName = $_GET['SalesmanName'];
$month        = $_GET['month'];
$year         = $_GET['year'];

echo "<html>
<head>
<title>Clients sold between 60 and 90 days</title>
</head>
<body>
<table width=800 style='border-collapse: collapse'>
<tr>
<th colspan=3 style='font-size: 24px; border-bottom: solid 2px #000000'>Clients sold between 60 and 90 days</th>
</tr>
<tr style='height: 15px'>
<td colspan=3></td>
</tr>
<tr style='background-color: #8f8f8f'>
<th style='font-size: 16px; width: 25%; text-align: left;'>Client Code</th>
<th style='font-size: 16px; width: 50%; text-align: left;'>Client Name</th>
<th style='font-size: 16px; width: 25%; text-align: left;'>Salesman</th>
</tr>";
$query_clients  = "
select deliveries.Client_code as ClientCode, clients.ledger_name as ClientName, salesmen.ledger_name as SalesmanName
from deliveries inner join clients on clients.ledger_number = deliveries.client_code
inner join salesmen on salesmen.ledger_number = deliveries.salesman_code
where (deliveries.Delivery_Date between ADDDATE(DATE_SUB('$year-$month-01',INTERVAL 2 MONTH), INTERVAL 1 DAY) and concat(LAST_DAY( DATE_SUB('$year-$month-01',INTERVAL 2 MONTH)), ' 23:59:59'))
and deliveries.Journal_Type = 'DI' and clients.client_deactivate <> 1";
if($_GET['SalesmanCode'] != "")
{
	$query_clients = $query_clients." and clients.Salesmen_Code = '".$_GET['SalesmanCode']."'";
}
$query_clients = $query_clients." and deliveries.client_code not in
(select client_code from(
select client_code
from deliveries
where (delivery_date between ADDDATE(DATE_SUB('$year-$month-01',INTERVAL 1 MONTH), INTERVAL 1 DAY) and concat(LAST_DAY('$year-$month-01'), ' 23:59:59')) 
and deliveries.Journal_Type = 'DI'";
if($_GET['SalesmanCode'] != "")
{
	$query_clients = $query_clients." and deliveries.salesman_code = '".$_GET['SalesmanCode']."'";
}
$query_clients = $query_clients." group by deliveries.Client_code 

) as d) group by deliveries.Client_code
order by deliveries.salesman_code";
//echo $query_clients;exit;
$result_clients = mysql_query($query_clients, $connection);
$number_clients = mysql_num_rows($result_clients);
$count = 0;
while($row_clients = mysql_fetch_array($result_clients)){
	extract($row_clients);
	$count++;
	if($count%2!=0)
	{
		$style= "background: #b4b4b4";
	}
	else
	{
		$style= "background: #d0d0d0";
	}
	echo "<tr style='$style'>
<th style='font-size: 16px; width: 25%; text-align: left'>$ClientCode</th>
<th style='font-size: 16px; width: 50%; text-align: left'>$ClientName</th>
<th style='font-size: 16px; width: 25%; text-align: left'>$SalesmanName</th>
</tr>";
}
echo "<tr>
<td colspan=2 style='font-weight: bold; font-size: 16px'>Total Records: $number_clients</td>
</tr>";
echo"</table>
</body>
</html>";
?>