<?php
session_start();
if($_SESSION['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($_SESSION['dbName'],$connection) or die("Could not find DB");

$from_date = $_GET['from_date'];
$to_date   = $_GET['to_date'];
$trans_type = $_GET['trans_type'];
$trans_year = $_GET['trans_year'];
$trans_operator = $_GET['trans_operator'];
$trans_company = $_GET['trans_company'];
$costcenter_ref_search = $_GET['costcenter_ref_search'];
$TransRefId = $_GET['TransRefId'];

//$trans_year2 = explode("_", $trans_year);
//$trans_year2 = $trans_year2[2];

$query_module  = "select module_delivery as ModuleDelivery from module";
$result_module = mysql_query($query_module, $connection); 
$row_module    = mysql_fetch_array($result_module);
extract($row_module);

if($ModuleDelivery == 1){
	$Deliveries = "Deliveries";
}
else{
	$Deliveries = "Sales";
}

print <<<HERE
<html>
<head>
<title>User Log: Costcenter</title>
</head>
<style>
@page { size: landscape; }
</style>
<body>
HERE;
echo "<table width=950 style='border-collapse: collapse'>
<tr>
<th style='padding-top: 10px; padding-bottom: 10px;  font-size: 24px; font-family: Arial'>User Log: Costcenter</th>
</tr>
</table>
<table width=950 style='border-collapse: collapse'>
<tr style='height: 15px;'>
<th style='border-top: solid 1.5px #000000;'></th>
</tr>
</table>";
echo "<table width=950 style='border-collapse: collapse'>
	<tr>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 120px;'>Costcenter Code</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 200px;'>Costcenter Name</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 100px;'>Parent</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 75px;'>Deactivate</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 130px;'>User</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 75px;'>Trans. Type</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 125px;'>Trans. Company/Year</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 125px;'>Trans. Date</th>
	</tr>";
$query_history = "select Costcent_code as CostcentCode,
Costcent_name as CostcentName,
Parent_Code as Parent,
if(Costcent_Deactivate = 1, 'True', 'False') as Deactivate,
if(Costcent_Trans_Type = 'I', 'Insert', if(Costcent_Trans_Type = 'U', 'Update', 'Delete')) AS TransType,
Costcent_Trans_Company AS TransCompany,
Costcent_Trans_Schema AS TransYear,
date_format(Costcent_Trans_date, '%d/%m/%Y %H:%i:%s') AS TransDate,
Costcent_Trans_Operator AS TransOperatorCode,
users.user_name AS TransOperatorName
from ".$config['dbName_history'].".costcent 
inner join ".$_SESSION['dbLabel'].".users on users.user_account = costcent.Costcent_Trans_Operator
where 1=1
";
if($from_date != "" && $to_date != ""){
	$from_date = explode("/", $from_date);
	$from_date = $from_date[2]."-".$from_date[1]."-".$from_date[0];
	$to_date = explode("/", $to_date);
	$to_date = $to_date[2]."-".$to_date[1]."-".$to_date[0];
	$query_history = $query_history." and Costcent_Trans_date between '$from_date' and '$to_date 23:59:59'";
}
if($trans_type != "a"){
	$query_history = $query_history." and Costcent_Trans_Type = '$trans_type'";
}
if($trans_company != ""){
	$query_history = $query_history." and Costcent_Trans_Company = '$trans_company'";
}
else{
	$query_history = $query_history." and Costcent_Trans_Company in (select distinct(CompanyName) from ".$_SESSION['dbLabel'].".listuserdir where usercode = '".$_SESSION['useraccount']."')";
}
if($trans_company != "" && $trans_year != ""){
	$query_history = $query_history." and Costcent_Trans_Schema = concat('$trans_company', '_', '$trans_year')";
}
if($trans_operator != ""){
	$query_history = $query_history." and Costcent_Trans_Operator = '$trans_operator'";
}
if($costcenter_ref_search != ""){
	$query_history = $query_history." and costcent.Costcent_code = '$costcenter_ref_search'";
}
if($TransRefId != ""){
	$query_history = $query_history." and costcent.costcent_ref_id = '$TransRefId'";
}
if($orderby == "date"){
$query_history = $query_history." order by Costcent_Trans_date desc";
}
else
if($orderby == "user_date"){
$query_history = $query_history." order by users.user_name asc, Costcent_Trans_date desc";
}
else
if($orderby == "company_date"){
$query_history = $query_history." order by Costcent_Trans_Schema asc, Costcent_Trans_date desc";
}
else
if($orderby == "company_user_date"){
$query_history = $query_history." order by Costcent_Trans_Schema asc, users.user_name asc, Costcent_Trans_date desc";
}
//echo $query_history;
//$query_history = $query_history." order by Costcent_Trans_date desc";
$result_history = mysql_query($query_history, $connection);
while($row_history = mysql_fetch_array($result_history)){
	extract($row_history);
	
	$query_salesman  = "select ledger_name as SalesmanName from ".$TransYear.".salesmen where ledger_number = '$SalesmanCode'";
	$result_salesman = mysql_query($query_salesman, $connection);
	$row_salesman    = mysql_fetch_array($result_salesman);
	extract($row_salesman);
	$TransYear = explode("_", $TransYear);
	$TransYear = $TransYear[1];
	echo "
	<tr>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 120px; vertical-align: top; text-align: left'>$CostcentCode</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 200px; text-align: left; vertical-align: top; text-align: left'>$CostcentName</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 100px;  vertical-align: top; text-align: left;'>$Parent</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 75px;  vertical-align: top;'>$Deactivate</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 130px; vertical-align: top;'>$TransOperatorName [$TransOperatorCode]</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 75px; vertical-align: top;'>$TransType</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 125px; vertical-align: top;'>$TransCompany | $TransYear</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 125px; vertical-align: top;'>$TransDate</th>
	</tr>
	";
	
}
echo "
	<tr style='height: 7px'>
	<td></td>
	</tr>";
	echo "</table>";
				  //echo $query_history;
print <<<HERE
</body>
<script>
window.print();
</script>
</html>
HERE;
?>