<?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");

$query_module  = "select base1, base2 from setup";
$result_module = mysql_query($query_module, $connection); 
$row_module    = mysql_fetch_array($result_module);
extract($row_module);

$from_date = $_GET['from_date'];
$to_date   = $_GET['to_date'];
$from_date2 = $_GET['from_date'];
$to_date2   = $_GET['to_date'];
$trans_type = $_GET['trans_type'];
$trans_year = $_GET['trans_year'];
$trans_operator = $_GET['trans_operator'];
$trans_company = $_GET['trans_company'];
$orderby = $_GET['orderby'];
//$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 = "Delivery";
}
else{
	$Deliveries = "Sale";
}

print <<<HERE
<html>
<head>
<title>User Log: All Transactions</title>
</head>
<style>
@page { size: landscape; }
</style>
<body>
HERE;
echo "<table width=1005 style='border-collapse: collapse'>
<tr>
<th style='padding-top: 10px; padding-bottom: 10px;  font-size: 24px; font-family: Arial'>User Log: All Transactions</th>
</tr>
</table>
<table width=1005 style='border-collapse: collapse'>
<tr style='height: 15px;'>
<th style='border-top: solid 1.5px #000000;'></th>
</tr>
</table>
<table width=1005 style='border-collapse: collapse'>
<tr style='height: 15px;'>
<th style=' font-size: 20px; font-family: Arial'><u>Archive</u></th>
</tr>
</table>
<table width=1005 style='border-collapse: collapse'>
<tr style='height: 15px;'>
<th style=''></th>
</tr>
</table>";
echo "<table width=1005 style='border-collapse: collapse'>
	<tr>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 200px;'>Form Name</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 100px;'>Ref. # / Code</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 250px;'>Description</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 100px;'>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: 140px;'>Trans. Company/Year</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 140px;'>Trans. Date</th>
	</tr>";
$query_history = "select * from (
select item_code as Ref,
item_description as Description,
if(Item_Trans_Type = 'I', 'Insert', if(Item_Trans_Type = 'U', 'Update', 'Delete')) as TransType,
Item_Trans_Type as TransType2,
Item_Trans_Operator as TransOperator,
date_format(Item_Trans_Date, '%d/%m/%Y %H:%i:%s') as TransDate,
Item_Trans_Date as TransDate2,
Item_Trans_Schema as TransCompany,
Item_Trans_Company as TransCompany2,
'Items' as FormName,
Item_Ref_Id  as TransId
from ".$config['dbName_history'].".items
union all
select ledger_number as Ref,
ledger_name as Description,
if(Ledger_Trans_Type = 'I', 'Insert', if(Ledger_Trans_Type = 'U', 'Update', 'Delete')) as TransType,
Ledger_Trans_Type as TransType2,
Ledger_Trans_Operator as TransOperator,
date_format(Ledger_Trans_Date, '%d/%m/%Y %H:%i:%s') as TransDate,
Ledger_Trans_Date as TransDate2,
Ledger_Trans_Schema as TransCompany,
Ledger_Trans_Company as TransCompany2,
'Ledger' as FormName,
Ledger_Ref_Id as TransId
from ".$config['dbName_history'].".ledger
union all
select ledger_number as Ref,
ledger_name as Description,
if(Client_Trans_Type = 'I', 'Insert', if(Client_Trans_Type = 'U', 'Update', 'Delete')) as TransType,
Client_Trans_Type as TransType2,
Client_Trans_Operator as TransOperator,
date_format(Client_Trans_Date, '%d/%m/%Y %H:%i:%s') as TransDate,
Client_Trans_Date as TransDate2,
Client_Trans_Schema as TransCompany,
Client_Trans_Company as TransCompany2,
'Client' as FormName,
Client_Ref_Id as TransId
from ".$config['dbName_history'].".clients
union all
select ledger_number as Ref,
ledger_name as Description,
if(Salesmen_Trans_Type = 'I', 'Insert', if(Salesmen_Trans_Type = 'U', 'Update', 'Delete')) as TransType,
Salesmen_Trans_Type as TransType2,
Salesmen_Trans_Operator as TransOperator,
date_format(Salesmen_Trans_Date, '%d/%m/%Y %H:%i:%s') as TransDate,
Salesmen_Trans_Date as TransDate2,
Salesmen_Trans_Schema as TransCompany,
Salesmen_Trans_Company as TransCompany2,
'Salesmen' as FormName,
Salesmen_Ref_Id as TransId
from ".$config['dbName_history'].".salesmen
union all
select Project_Code as Ref,
Project_name as Description,
if(Project_Trans_Type = 'I', 'Insert', if(Project_Trans_Type = 'U', 'Update', 'Delete')) as TransType,
Project_Trans_Type as TransType2,
Project_Trans_Operator as TransOperator,
date_format(Project_Trans_Date, '%d/%m/%Y %H:%i:%s') as TransDate,
Project_Trans_Date as TransDate2,
Project_Trans_Schema as TransCompany,
Project_Trans_Company as TransCompany2,
'Project' as FormName,
Project_Ref_Id as TransId
from ".$config['dbName_history'].".project
union all
select costcent_Code as Ref,
costcent_name as Description,
if(costcent_Trans_Type = 'I', 'Insert', if(costcent_Trans_Type = 'U', 'Update', 'Delete')) as TransType,
costcent_Trans_Type as TransType2,
costcent_Trans_Operator as TransOperator,
date_format(costcent_Trans_Date, '%d/%m/%Y %H:%i:%s') as TransDate,
costcent_Trans_Date as TransDate2,
costcent_Trans_Schema as TransCompany,
costcent_Trans_Company as TransCompany2,
'Costcenter' as FormName,
Costcent_Ref_Id as TransId
from ".$config['dbName_history'].".costcent
union all
select currency_Code as Ref,
currency_name as Description,
if(currency_Trans_Type = 'I', 'Insert', if(currency_Trans_Type = 'U', 'Update', 'Delete')) as TransType,
currency_Trans_Type as TransType2,
currency_Trans_Operator as TransOperator,
date_format(currency_Trans_Date, '%d/%m/%Y %H:%i:%s') as TransDate,
currency_Trans_Date as TransDate2,
currency_Trans_Schema as TransCompany,
currency_Trans_Company as TransCompany2,
'Currency' as FormName,
currency_Ref_Id as TransId
from ".$config['dbName_history'].".currency
union all
select group_Code as Ref,
group_name as Description,
if(group_Trans_Type = 'I', 'Insert', if(group_Trans_Type = 'U', 'Update', 'Delete')) as TransType,
group_Trans_Type as TransType2,
group_Trans_Operator as TransOperator,
date_format(group_Trans_Date, '%d/%m/%Y %H:%i:%s') as TransDate,
group_Trans_Date as TransDate2,
group_Trans_Schema as TransCompany,
group_Trans_Company as TransCompany2,
'Grouping' as FormName,
group_Ref_Id as TransId
from ".$config['dbName_history'].".grouping
union all
select subgroup_Code as Ref,
subgroup_name as Description,
if(subgroup_Trans_Type = 'I', 'Insert', if(subgroup_Trans_Type = 'U', 'Update', 'Delete')) as TransType,
subgroup_Trans_Type as TransType2,
subgroup_Trans_Operator as TransOperator,
date_format(subgroup_Trans_Date, '%d/%m/%Y %H:%i:%s') as TransDate,
subgroup_Trans_Date as TransDate2,
subgroup_Trans_Schema as TransCompany,
subgroup_Trans_Company as TransCompany2,
'Sub Group' as FormName,
subgroup_Ref_Id as TransId
from ".$config['dbName_history'].".subgroup
union all
select warehouse_Code as Ref,
warehouse_name as Description,
if(warehouse_Trans_Type = 'I', 'Insert', if(warehouse_Trans_Type = 'U', 'Update', 'Delete')) as TransType,
warehouse_Trans_Type as TransType2,
warehouse_Trans_Operator as TransOperator,
date_format(warehouse_Trans_Date, '%d/%m/%Y %H:%i:%s') as TransDate,
warehouse_Trans_Date as TransDate2,
warehouse_Trans_Schema as TransCompany,
warehouse_Trans_Company as TransCompany2,
'Warehouse' as FormName,
warehouse_Ref_Id as TransId
from ".$config['dbName_history'].".warehouse
union all
select category_Code as Ref,
category_name as Description,
if(category_Trans_Type = 'I', 'Insert', if(category_Trans_Type = 'U', 'Update', 'Delete')) as TransType,
category_Trans_Type as TransType2,
category_Trans_Operator as TransOperator,
date_format(category_Trans_Date, '%d/%m/%Y %H:%i:%s') as TransDate,
category_Trans_Date as TransDate2,
category_Trans_Schema as TransCompany,
category_Trans_Company as TransCompany2,
'Category' as FormName,
category_Ref_Id as TransId
from ".$config['dbName_history'].".category
union all
select area_Code as Ref,
area_name as Description,
if(area_Trans_Type = 'I', 'Insert', if(area_Trans_Type = 'U', 'Update', 'Delete')) as TransType,
area_Trans_Type as TransType2,
area_Trans_Operator as TransOperator,
date_format(area_Trans_Date, '%d/%m/%Y %H:%i:%s') as TransDate,
area_Trans_Date as TransDate2,
area_Trans_Schema as TransCompany,
area_Trans_Company as TransCompany2,
'Area' as FormName,
area_Ref_Id as TransId
from ".$config['dbName_history'].".area
union all
select bank_name as Ref,
bank_name as Description,
if(bank_Trans_Type = 'I', 'Insert', if(bank_Trans_Type = 'U', 'Update', 'Delete')) as TransType,
bank_Trans_Type as TransType2,
bank_Trans_Operator as TransOperator,
date_format(bank_Trans_Date, '%d/%m/%Y %H:%i:%s') as TransDate,
bank_Trans_Date as TransDate2,
bank_Trans_Schema as TransCompany,
bank_Trans_Company as TransCompany2,
'Banks' as FormName,
bank_Ref_Id as TransId
from ".$config['dbName_history'].".banks) as Q
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 TransDate2 between '$from_date' and '$to_date 23:59:59'";
}
if($trans_type != "a"){
	$query_history = $query_history." and TransType2 = '$trans_type'";
}
if($trans_company != ""){
	$query_history = $query_history." and TransCompany2 = '$trans_company'";
}
else{
	$query_history = $query_history." and TransCompany2 in (select distinct(CompanyName) from ".$_SESSION['dbLabel'].".listuserdir where usercode = '".$_SESSION['useraccount']."')";
}
if($trans_company != "" && $trans_year != ""){
	$query_history = $query_history." and TransCompany = concat('$trans_company', '_', '$trans_year')";
}
if($trans_operator != ""){
	$query_history = $query_history." and TransOperator = '$trans_operator'";
}
//echo $query_history;
if($orderby == "date"){
$query_history = $query_history." order by TransDate2 desc";
}
else
if($orderby == "user_date"){
$query_history = $query_history." order by TransOperator asc, TransDate2 desc";
}
else
if($orderby == "company_date"){
$query_history = $query_history." order by TransCompany asc, TransDate2 desc";
}
else
if($orderby == "company_user_date"){
$query_history = $query_history." order by TransCompany asc, TransOperator asc, TransDate2 desc";
}
// echo $query_history;
$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];
	if($FormName == "Items"){
		$link = "Rep_UserLogItems.php?from_date=".$from_date2."&to_date=".$to_date2."&trans_type=".$trans_type."&trans_company=".$trans_company."&trans_year=".$trans_year."&trans_operator=".$trans_operator."&item_ref_search=".$Ref."&orderby=".$orderby."&TransRefId=".$TransId;
	}
	else
	if($FormName == "Ledger"){
		$link = "Rep_UserLogLedger.php?from_date=".$from_date2."&to_date=".$to_date2."&trans_type=".$trans_type."&trans_company=".$trans_company."&trans_year=".$trans_year."&trans_operator=".$trans_operator."&ledger_ref_search=".$Ref."&orderby=".$orderby."&TransRefId=".$TransId;
	}
	else
	if($FormName == "Client"){
		$link = "Rep_UserLogClients.php?from_date=".$from_date2."&to_date=".$to_date2."&trans_type=".$trans_type."&trans_company=".$trans_company."&trans_year=".$trans_year."&trans_operator=".$trans_operator."&client_ref_search=".$Ref."&orderby=".$orderby."&TransRefId=".$TransId;
	}
	else
	if($FormName == "Salesmen"){
		$link = "Rep_UserLogSalesmen.php?from_date=".$from_date2."&to_date=".$to_date2."&trans_type=".$trans_type."&trans_company=".$trans_company."&trans_year=".$trans_year."&trans_operator=".$trans_operator."&salesman_ref_search=".$Ref."&orderby=".$orderby."&TransRefId=".$TransId;
	}
	else
	if($FormName == "Project"){
		$link = "Rep_UserLogProject.php?from_date=".$from_date2."&to_date=".$to_date2."&trans_type=".$trans_type."&trans_company=".$trans_company."&trans_year=".$trans_year."&trans_operator=".$trans_operator."&project_ref_search=".$Ref."&orderby=".$orderby."&TransRefId=".$TransId;
	}
	else
	if($FormName == "Costcenter"){
		$link = "Rep_UserLogCostcenter.php?from_date=".$from_date2."&to_date=".$to_date2."&trans_type=".$trans_type."&trans_company=".$trans_company."&trans_year=".$trans_year."&trans_operator=".$trans_operator."&costcent_ref_search=".$Ref."&orderby=".$orderby."&TransRefId=".$TransId;
	}
	else
	if($FormName == "Currency"){
		$link = "Rep_UserLogCurrency.php?from_date=".$from_date2."&to_date=".$to_date2."&trans_type=".$trans_type."&trans_company=".$trans_company."&trans_year=".$trans_year."&trans_operator=".$trans_operator."&currency_ref_search=".$Ref."&orderby=".$orderby."&TransRefId=".$TransId;
	}
	else
	if($FormName == "Grouping"){
		$link = "Rep_UserLogGrouping.php?from_date=".$from_date2."&to_date=".$to_date2."&trans_type=".$trans_type."&trans_company=".$trans_company."&trans_year=".$trans_year."&trans_operator=".$trans_operator."&grouping_ref_search=".$Ref."&orderby=".$orderby."&TransRefId=".$TransId;
	}
	else
	if($FormName == "Sub Group"){
		$link = "Rep_UserLogSubGroup.php?from_date=".$from_date2."&to_date=".$to_date2."&trans_type=".$trans_type."&trans_company=".$trans_company."&trans_year=".$trans_year."&trans_operator=".$trans_operator."&subgroup_ref_search=".$Ref."&orderby=".$orderby."&TransRefId=".$TransId;
	}
	else
	if($FormName == "Warehouse"){
		$link = "Rep_UserLogWarehouse.php?from_date=".$from_date2."&to_date=".$to_date2."&trans_type=".$trans_type."&trans_company=".$trans_company."&trans_year=".$trans_year."&trans_operator=".$trans_operator."&warehouse_ref_search=".$Ref."&orderby=".$orderby."&TransRefId=".$TransId;
	}
	else
	if($FormName == "Category"){
		$link = "Rep_UserLogCategory.php?from_date=".$from_date2."&to_date=".$to_date2."&trans_type=".$trans_type."&trans_company=".$trans_company."&trans_year=".$trans_year."&trans_operator=".$trans_operator."&category_ref_search=".$Ref."&orderby=".$orderby."&TransRefId=".$TransId;
	}
	else
	if($FormName == "Area"){
		$link = "Rep_UserLogArea.php?from_date=".$from_date2."&to_date=".$to_date2."&trans_type=".$trans_type."&trans_company=".$trans_company."&trans_year=".$trans_year."&trans_operator=".$trans_operator."&area_ref_search=".$Ref."&orderby=".$orderby."&TransRefId=".$TransId;
	}
	else
	if($FormName == "Banks"){
		$link = "Rep_UserLogBanks.php?from_date=".$from_date2."&to_date=".$to_date2."&trans_type=".$trans_type."&trans_company=".$trans_company."&trans_year=".$trans_year."&trans_operator=".$trans_operator."&area_ref_search=".$Ref."&orderby=".$orderby."&TransRefId=".$TransId;
	}
	echo "
<tr style='cursor: pointer' onclick=\"window.open('$link')\">
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 200px; text-align: left'>$FormName</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 100px; text-align: left'>$Ref</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 250px; text-align: left'>$Description</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 100px;'>$TransOperator</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 75px;'>$TransType</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 140px;'>$TransCompany</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 140px;'>$TransDate</th>
	</tr>
	";
	
}
echo "
	<tr style='height: 7px'>
	<td></td>
	</tr>";
	echo "</table>";
	echo"<table width=1005 style='border-collapse: collapse'>
<tr style='height: 15px;'>
<th style=''></th>
</tr>
</table>
	<table width=1005 style='border-collapse: collapse'>
<tr style='height: 15px;'>
<th style=' font-size: 20px; font-family: Arial'><u>Accounting</u></th>
</tr>
</table>
<table width=1005 style='border-collapse: collapse'>
<tr style='height: 15px;'>
<th style=''></th>
</tr>
</table>";
echo "<table width=1005 style='border-collapse: collapse'>
	<tr>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 200px;'>Form Name</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 100px;'>Ref. # / Code</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 250px;'>Description</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 100px;'>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: 140px;'>Trans. Company/Year</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 140px;'>Trans. Date</th>
	</tr>";
$query_history = "SELECT *
FROM
  (SELECT trans_reference AS REF,
          concat('Receipt Ref. # ', trans_reference) AS Description,
          if(trans_Trans_Type = 'I', 'Insert', if(trans_Trans_Type = 'U', 'Update', 'Delete')) AS TransType,
          trans_Trans_Type AS TransType2,
          trans_Trans_Operator AS TransOperator,
          date_format(trans_Trans_Date, '%d/%m/%Y %H:%i:%s') AS TransDate,
          trans_Trans_Date AS TransDate2,
          trans_Trans_Schema AS TransCompany,
          trans_Trans_Company AS TransCompany2,
          'Receipt' AS FormName,
          Trans_Ref_Id AS TransId
   FROM ".$config['dbName_history'].".trans
   WHERE trans_type = 'RV'
   UNION ALL SELECT trans_reference AS REF,
                    concat('Payment Ref. # ', trans_reference) AS Description,
                    if(trans_Trans_Type = 'I', 'Insert', if(trans_Trans_Type = 'U', 'Update', 'Delete')) AS TransType,
                    trans_Trans_Type AS TransType2,
                    trans_Trans_Operator AS TransOperator,
                    date_format(trans_Trans_Date, '%d/%m/%Y %H:%i:%s') AS TransDate,
                    trans_Trans_Date AS TransDate2,
                    trans_Trans_Schema AS TransCompany,
                    trans_Trans_Company AS TransCompany2,
                    'Payment' AS FormName,
                    Trans_Ref_Id AS TransId
   FROM ".$config['dbName_history'].".trans
   WHERE trans_type = 'PV'
   UNION ALL SELECT trans_reference AS REF,
                    concat('Credit Note Ref. # ', trans_reference) AS Description,
                    if(trans_Trans_Type = 'I', 'Insert', if(trans_Trans_Type = 'U', 'Update', 'Delete')) AS TransType,
                    trans_Trans_Type AS TransType2,
                    trans_Trans_Operator AS TransOperator,
                    date_format(trans_Trans_Date, '%d/%m/%Y %H:%i:%s') AS TransDate,
                    trans_Trans_Date AS TransDate2,
                    trans_Trans_Schema AS TransCompany,
                    trans_Trans_Company AS TransCompany2,
                    'Credit Note' AS FormName,
                    Trans_Ref_Id AS TransId
   FROM ".$config['dbName_history'].".trans
   WHERE trans_type = 'CV'
   
    
       

   
   UNION ALL SELECT trans_reference AS REF,
                    concat('Debit Note Ref. # ', trans_reference) AS Description,
                    if(trans_Trans_Type = 'I', 'Insert', if(trans_Trans_Type = 'U', 'Update', 'Delete')) AS TransType,
                    trans_Trans_Type AS TransType2,
                    trans_Trans_Operator AS TransOperator,
                    date_format(trans_Trans_Date, '%d/%m/%Y %H:%i:%s') AS TransDate,
                    trans_Trans_Date AS TransDate2,
                    trans_Trans_Schema AS TransCompany,
                    trans_Trans_Company AS TransCompany2,
                    'Debit Note' AS FormName,
                    Trans_Ref_Id AS TransId
   FROM history.trans
  
 
   WHERE trans_type = 'DV'
   
   UNION ALL 
   SELECT salesjv_reference AS REF,
					concat('Sales Jv Ref. #', salesjv_reference) AS Description,
					if(salesjv_Trans_Type = 'I', 'Insert', if(salesjv_Trans_Type = 'U', 'Update', 'Delete')) AS TransType,
					salesjv_Trans_Type AS TransType2,
					salesjv_Trans_Operator AS TransOperator,
					date_format(salesjv_Trans_Date, '%d/%m/%Y %H:%i:%s') AS TransDate,
					salesjv_Trans_Date AS TransDate2,
					salesjv_Trans_Schema AS TransCompany,
					salesjv_Trans_Company AS TransCompany2,
					'Sales JV' AS FormName,
					SalesJv_Ref_Id AS TransId
	FROM ".$config['dbName_history'].".salesjv

   UNION ALL 
   SELECT PurchaseJV_Reference AS REF,
                    concat('Purchases Jv Ref. #', PurchaseJV_Reference) AS Description,
                    if(PurchasesJV_Trans_Type = 'I', 'Insert', if(PurchasesJV_Trans_Type = 'U', 'Update', 'Delete')) AS TransType,
                    PurchasesJV_Trans_Type AS TransType2,
                    PurchasesJV_Trans_Operator AS TransOperator,
                    date_format(PurchasesJV_Trans_Date, '%d/%m/%Y %H:%i:%s') AS TransDate,
                    PurchasesJV_Trans_Date AS TransDate2,
                    PurchasesJV_Trans_Schema AS TransCompany,
                    PurchasesJV_Trans_Company AS TransCompany2,
                    'Purchases JV' AS FormName,
					purchasejv_ref_id AS TransId
                   
   FROM ".$config['dbName_history'].".purchasesjv
    
   
   
   UNION ALL SELECT Journal_refgovernment AS REF,
                    concat('Journal Ref. #', Journal_refgovernment) AS Description,
                    if(journal_Trans_Type = 'I', 'Insert', if(journal_Trans_Type = 'U', 'Update', 'Delete')) AS TransType,
                    journal_Trans_Type AS TransType2,
                    journal_Trans_Operator AS TransOperator,
                    date_format(journal_Trans_Date, '%d/%m/%Y %H:%i:%s') AS TransDate,
                    journal_Trans_Date AS TransDate2,
                    journal_Trans_Schema AS TransCompany,
                    journal_Trans_Company AS TransCompany2,
                    'Journal' AS FormName,
                    Journal_Ref_Id AS TransId
   FROM ".$config['dbName_history'].".journal  ) AS Q
   
   
   
   
WHERE 1=1
";

/*
	 UNION ALL SELECT salesjv_reference AS REF,
                    concat('Sales Jv Ref. #', salesjv_reference) AS Description,
                    if(salesjv_Trans_Type = 'I', 'Insert', if(salesjv_Trans_Type = 'U', 'Update', 'Delete')) AS TransType,
                    salesjv_Trans_Type AS TransType2,
                    salesjv_Trans_Operator AS TransOperator,
                    date_format(salesjv_Trans_Date, '%d/%m/%Y %H:%i:%s') AS TransDate,
                    salesjv_Trans_Date AS TransDate2,
                    salesjv_Trans_Schema AS TransCompany,
                    salesjv_Trans_Company AS TransCompany2,
                    'Sales JV' AS FormName,
                    SalesJv_Ref_Id AS TransId
   FROM ".$config['dbName_history'].".salesjv
   UNION ALL SELECT PurchaseJV_Reference AS REF,
                    concat('Purchases Jv Ref. #', PurchaseJV_Reference) AS Description,
                    if(PurchaseJV_Trans_Type = 'I', 'Insert', if(PurchaseJV_Trans_Type = 'U', 'Update', 'Delete')) AS TransType,
                    PurchaseJV_Trans_Type AS TransType2,
                    PurchaseJV_Trans_Operator AS TransOperator,
                    date_format(PurchaseJV_Trans_Date, '%d/%m/%Y %H:%i:%s') AS TransDate,
                    PurchaseJV_Trans_Date AS TransDate2,
                    PurchaseJV_Trans_Schema AS TransCompany,
                    PurchaseJV_Trans_Company AS TransCompany2,
                    'Purchases JV' AS FormName,
                    PurchaseJV_Ref_Id AS TransId
   FROM ".$config['dbName_history'].".purchasesjv */


if($from_date != "" && $to_date != ""){
	$query_history = $query_history." and TransDate2 between '$from_date' and '$to_date 23:59:59'";
}
if($trans_type != "a"){
	$query_history = $query_history." and TransType2 = '$trans_type'";
}
if($trans_company != ""){
	$query_history = $query_history." and TransCompany2 = '$trans_company'";
}
else{
	$query_history = $query_history." and TransCompany2 in (select distinct(CompanyName) from ".$_SESSION['dbLabel'].".listuserdir where usercode = '".$_SESSION['useraccount']."')";
}
if($trans_company != "" && $trans_year != ""){
	$query_history = $query_history." and TransCompany = concat('$trans_company', '_', '$trans_year')";
}
if($trans_operator != ""){
	$query_history = $query_history." and TransOperator = '$trans_operator'";
}
// echo $query_history;
if($orderby == "date"){
$query_history = $query_history." order by TransDate2 desc";
}
else
if($orderby == "user_date"){
$query_history = $query_history." order by TransOperator asc, TransDate2 desc";
}
else
if($orderby == "company_date"){
$query_history = $query_history." order by TransCompany asc, TransDate2 desc";
}
else
if($orderby == "company_user_date"){
$query_history = $query_history." order by TransCompany asc, TransOperator asc, TransDate2 desc";
}
//echo $query_history;
$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];
	if($FormName == "Receipt"){
		$link = "Rep_UserLogTrans.php?from_date=$from_date2&to_date=$to_date2&trans_type=$trans_type&trans_company=$trans_company&trans_year=$trans_year&trans_operator=$trans_operator&trans_ref_search=$REF&trans_type2=RV&orderby=$orderby&TransRefId=$TransId";
	}
	else
	if($FormName == "Payment"){
		$link = "Rep_UserLogTrans.php?from_date=$from_date2&to_date=$to_date2&trans_type=$trans_type&trans_company=$trans_company&trans_year=$trans_year&trans_operator=$trans_operator&trans_ref_search=$REF&trans_type2=PV&orderby=$orderby&TransRefId=$TransId";
	}
	
	else
	if($FormName == "Credit Note"){
		$link = "Rep_UserLogTrans.php?from_date=$from_date2&to_date=$to_date2&trans_type=$trans_type&trans_company=$trans_company&trans_year=$trans_year&trans_operator=$trans_operator&trans_ref_search=$REF&trans_type2=CV&orderby=$orderby&TransRefId=$TransId";
	}
	else
	if($FormName == "Debit Note"){
		$link = "Rep_UserLogTrans.php?from_date=$from_date2&to_date=$to_date2&trans_type=$trans_type&trans_company=$trans_company&trans_year=$trans_year&trans_operator=$trans_operator&trans_ref_search=$REF&trans_type2=DV&orderby=$orderby&TransRefId=$TransId";
	}
	//else
	//if($FormName == "Purchases JV"){
		//$link = "Rep_UserLogPurchasesJv.php?from_date=".$from_date2."&to_date=".$to_date2."&trans_type=".$trans_type."&trans_company=".$trans_company."&trans_year=".$trans_year."&trans_operator=".$trans_operator."&purchasesjv_ref_search=".$REF."&orderby=".$orderby."&TransRefId=".$TransId;
	//}
	else
	if($FormName == "Sales JV"){
		$link = "Rep_UserLogSalesJv.php?from_date=".$from_date2."&to_date=".$to_date2."&trans_type=".$trans_type."&trans_company=".$trans_company."&trans_year=".$trans_year."&trans_operator=".$trans_operator."&salesjv_ref_search=".$REF."&orderby=".$orderby."&TransRefId=".$TransId;
	}
	else
	if($FormName == "Journal"){
		$link = "Rep_UserLogJournal.php?from_date=".$from_date2."&to_date=".$to_date2."&trans_type=".$trans_type."&trans_company=".$trans_company."&trans_year=".$trans_year."&trans_operator=".$trans_operator."&journal_ref_search=".$REF."&orderby=".$orderby."&TransRefId=".$TransId;
	}
	echo "
<tr style='cursor: pointer' onclick=\"window.open('$link')\">
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 200px; text-align: left'>$FormName</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 100px; text-align: center'>$REF</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 250px; text-align: left'>$Description</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 100px;'>$TransOperator</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 75px;'>$TransType</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 140px;'>$TransCompany</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 140px;'>$TransDate</th>
	</tr>
	";
	
}
echo "
	<tr style='height: 7px'>
	<td></td>
	</tr>";
	echo "</table>";
	echo" <table width=1005 style='border-collapse: collapse'>
<tr style='height: 15px;'>
<th style=''></th>
</tr>
</table>
	<table width=1005 style='border-collapse: collapse'>
<tr style='height: 15px;'>
<th style=' font-size: 20px; font-family: Arial'><u>Inventory</u></th>
</tr>
</table>
<table width=1005 style='border-collapse: collapse'>
<tr style='height: 15px;'>
<th style=''></th>
</tr>
</table>";
echo "<table width=1005 style='border-collapse: collapse'>
	<tr>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 200px;'>Form Name</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 100px;'>Ref. # / Code</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 250px;'>Description</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 100px;'>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: 140px;'>Trans. Company/Year</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 140px;'>Trans. Date</th>
	</tr>";
$query_history = "SELECT *
FROM
  (SELECT delivery_reference AS REF,
          concat('$Deliveries Ref. # ', delivery_reference) AS Description,
          if(delivery_Trans_Type = 'I', 'Insert', if(delivery_Trans_Type = 'U', 'Update', 'Delete')) AS TransType,
          delivery_Trans_Type AS TransType2,
          delivery_Trans_Operator AS TransOperator,
          date_format(delivery_Trans_Date, '%d/%m/%Y %H:%i:%s') AS TransDate,
          delivery_Trans_Date AS TransDate2,
          delivery_Trans_Schema AS TransCompany,
          delivery_Trans_Company AS TransCompany2,
          '$Deliveries' AS FormName,
          Delivery_Ref_Id AS TransId
   FROM ".$config['dbName_history'].".deliveries where journal_type = 'SI' or journal_type = 'DI'
   UNION ALL SELECT delivery_reference AS REF,
          concat('Return $Deliveries Ref. # ', delivery_reference) AS Description,
          if(delivery_Trans_Type = 'I', 'Insert', if(delivery_Trans_Type = 'U', 'Update', 'Delete')) AS TransType,
          delivery_Trans_Type AS TransType2,
          delivery_Trans_Operator AS TransOperator,
          date_format(delivery_Trans_Date, '%d/%m/%Y %H:%i:%s') AS TransDate,
          delivery_Trans_Date AS TransDate2,
          delivery_Trans_Schema AS TransCompany,
          delivery_Trans_Company AS TransCompany2,
          'Return $Deliveries' AS FormName,
          Delivery_Ref_Id AS TransId
   FROM ".$config['dbName_history'].".deliveries where journal_type = 'SR' or journal_type = 'DR'
   UNION ALL SELECT sorder_reference AS REF,
          concat('Sales Order Ref. # ', sorder_reference) AS Description,
          if(sorder_Trans_Type = 'I', 'Insert', if(sorder_Trans_Type = 'U', 'Update', 'Delete')) AS TransType,
          sorder_Trans_Type AS TransType2,
          sorder_Trans_Operator AS TransOperator,
          date_format(sorder_Trans_Date, '%d/%m/%Y %H:%i:%s') AS TransDate,
          sorder_Trans_Date AS TransDate2,
          sorder_Trans_Schema AS TransCompany,
          sorder_Trans_Company AS TransCompany2,
          'Sales Order' AS FormName,
          Sorder_Ref_Id AS TransId
   FROM ".$config['dbName_history'].".sorder
   UNION ALL SELECT purchase_reference AS REF,
          concat('Purchase Ref. # ', purchase_reference) AS Description,
          if(purchase_Trans_Type = 'I', 'Insert', if(purchase_Trans_Type = 'U', 'Update', 'Delete')) AS TransType,
          purchase_Trans_Type AS TransType2,
          purchase_Trans_Operator AS TransOperator,
          date_format(purchase_Trans_Date, '%d/%m/%Y %H:%i:%s') AS TransDate,
          purchase_Trans_Date AS TransDate2,
          purchase_Trans_Schema AS TransCompany,
          purchase_Trans_Company AS TransCompany2,
          'Purchase' AS FormName,
          purchase_Ref_Id AS TransId
   FROM ".$config['dbName_history'].".purchases where journal_type = 'PI'
   UNION ALL SELECT purchase_reference AS REF,
          concat('Return Purchase Ref. # ', purchase_reference) AS Description,
          if(purchase_Trans_Type = 'I', 'Insert', if(purchase_Trans_Type = 'U', 'Update', 'Delete')) AS TransType,
          purchase_Trans_Type AS TransType2,
          purchase_Trans_Operator AS TransOperator,
          date_format(purchase_Trans_Date, '%d/%m/%Y %H:%i:%s') AS TransDate,
          purchase_Trans_Date AS TransDate2,
          purchase_Trans_Schema AS TransCompany,
          purchase_Trans_Company AS TransCompany2,
          'Return Purchase' AS FormName,
          purchase_Ref_Id AS TransId
   FROM ".$config['dbName_history'].".purchases where journal_type = 'PR'
UNION ALL SELECT porder_reference AS REF,
                    concat('Purchase Order Ref. #', porder_reference) AS Description,
                    if(porder_Trans_Type = 'I', 'Insert', if(porder_Trans_Type = 'U', 'Update', 'Delete')) AS TransType,
                    porder_Trans_Type AS TransType2,
                    porder_Trans_Operator AS TransOperator,
                    date_format(porder_Trans_Date, '%d/%m/%Y %H:%i:%s') AS TransDate,
                    porder_Trans_Date AS TransDate2,
                    porder_Trans_Schema AS TransCompany,
                    porder_Trans_Company AS TransCompany2,
                    'Purchase Order' AS FormName,
                    Porder_Ref_Id AS TransId
   FROM ".$config['dbName_history'].".porder
UNION ALL SELECT PRqst_reference AS REF,
                    concat('Purchase Request Ref. #', PRqst_reference) AS Description,
                    if(PRqst_Trans_Type = 'I', 'Insert', if(PRqst_Trans_Type = 'U', 'Update', 'Delete')) AS TransType,
                    PRqst_Trans_Type AS TransType2,
                    PRqst_Trans_Operator AS TransOperator,
                    date_format(PRqst_Trans_Date, '%d/%m/%Y %H:%i:%s') AS TransDate,
                    PRqst_Trans_Date AS TransDate2,
                    PRqst_Trans_Schema AS TransCompany,
                    PRqst_Trans_Company AS TransCompany2,
                    'Purchase Request' AS FormName,
                    PRqst_Ref_Id AS TransId
   FROM ".$config['dbName_history'].".prequest
UNION ALL SELECT transfer_reference AS REF,
                    concat('Transfer Ref. #', transfer_reference) AS Description,
                    if(transfer_Trans_Type = 'I', 'Insert', if(transfer_Trans_Type = 'U', 'Update', 'Delete')) AS TransType,
                    transfer_Trans_Type AS TransType2,
                    transfer_Trans_Operator AS TransOperator,
                    date_format(transfer_Trans_Date, '%d/%m/%Y %H:%i:%s') AS TransDate,
                    transfer_Trans_Date AS TransDate2,
                    transfer_Trans_Schema AS TransCompany,
                    transfer_Trans_Company AS TransCompany2,
                    'Transfer' AS FormName,
                    transfer_Ref_Id AS TransId
   FROM ".$config['dbName_history'].".transfer
UNION ALL SELECT transferrequest_reference AS REF,
                    concat('Transfer Request Ref. #', transferrequest_reference) AS Description,
                    if(transferrequest_Trans_Type = 'I', 'Insert', if(transferrequest_Trans_Type = 'U', 'Update', 'Delete')) AS TransType,
                    transferrequest_Trans_Type AS TransType2,
                    transferrequest_Trans_Operator AS TransOperator,
                    date_format(transferrequest_Trans_Date, '%d/%m/%Y %H:%i:%s') AS TransDate,
                    transferrequest_Trans_Date AS TransDate2,
                    transferrequest_Trans_Schema AS TransCompany,
                    transferrequest_Trans_Company AS TransCompany2,
                    'Transfer Request' AS FormName,
                    transferrequest_Ref_Id AS TransId
   FROM ".$config['dbName_history'].".transferrequest
UNION ALL SELECT production_reference AS REF,
                    concat('Production Ref. #', production_reference) AS Description,
                    if(production_Trans_Type = 'I', 'Insert', if(production_Trans_Type = 'U', 'Update', 'Delete')) AS TransType,
                    production_Trans_Type AS TransType2,
                    production_Trans_Operator AS TransOperator,
                    date_format(production_Trans_Date, '%d/%m/%Y %H:%i:%s') AS TransDate,
                    production_Trans_Date AS TransDate2,
                    production_Trans_Schema AS TransCompany,
                    production_Trans_Company AS TransCompany2,
                    'Production' AS FormName,
                    production_Ref_Id AS TransId
   FROM ".$config['dbName_history'].".production
UNION ALL SELECT adjustment_reference AS REF,
                    concat('Adjustment Ref. #', adjustment_reference) AS Description,
                    if(adjustment_Trans_Type = 'I', 'Insert', if(adjustment_Trans_Type = 'U', 'Update', 'Delete')) AS TransType,
                    adjustment_Trans_Type AS TransType2,
                    adjustment_Trans_Operator AS TransOperator,
                    date_format(adjustment_Trans_Date, '%d/%m/%Y %H:%i:%s') AS TransDate,
                    adjustment_Trans_Date AS TransDate2,
                    adjustment_Trans_Schema AS TransCompany,
                    adjustment_Trans_Company AS TransCompany2,
                    'Adjustment' AS FormName,
                    adjustment_Ref_Id AS TransId
   FROM ".$config['dbName_history'].".adjustment
UNION ALL SELECT Purchase_Reference AS REF,
                    concat('Good Receive Note Ref. #', Purchase_Reference) AS Description,
                    if(Purchase_trans_type = 'I', 'Insert', if(Purchase_trans_type = 'U', 'Update', 'Delete')) AS TransType,
                    Purchase_trans_type AS TransType2,
                    Purchase_trans_operator AS TransOperator,
                    date_format(Purchase_trans_date, '%d/%m/%Y %H:%i:%s') AS TransDate,
                    Purchase_trans_date AS TransDate2,
                    Purchase_trans_schema AS TransCompany,
                    Purchase_trans_company AS TransCompany2,
                    'Good Receive Note' AS FormName,
                    Purchase_ref_id AS TransId
   FROM ".$config['dbName_history'].".grnpurchases
   
UNION ALL SELECT Sale_Reference AS REF,
                    concat('POS Ref. #', Sale_Reference) AS Description,
                    if(Sale_trans_type = 'I', 'Insert', if(Sale_trans_type = 'U', 'Update', 'Delete')) AS TransType,
                    Sale_trans_type AS TransType2,
                    Sale_trans_operator AS TransOperator,
                    date_format(Sale_trans_date, '%d/%m/%Y %H:%i:%s') AS TransDate,
                    Sale_trans_date AS TransDate2,
                    Sale_trans_schema AS TransCompany,
                    Sale_trans_company AS TransCompany2,
                    'POS' AS FormName,
                    Sale_ref_id AS TransId
   FROM ".$config['dbName_history'].".retail
   ) AS Q
WHERE 1=1
";
if($from_date != "" && $to_date != ""){
	$query_history = $query_history." and TransDate2 between '$from_date' and '$to_date 23:59:59'";
}
if($trans_type != "a"){
	$query_history = $query_history." and TransType2 = '$trans_type'";
}
if($trans_company != ""){
	$query_history = $query_history." and TransCompany2 = '$trans_company'";
}
else{
	$query_history = $query_history." and TransCompany2 in (select distinct(CompanyName) from ".$_SESSION['dbLabel'].".listuserdir where usercode = '".$_SESSION['useraccount']."')";
}
if($trans_company != "" && $trans_year != ""){
	$query_history = $query_history." and TransCompany = concat('$trans_company', '_', '$trans_year')";
}
if($trans_operator != ""){
	$query_history = $query_history." and TransOperator = '$trans_operator'";
}
// echo $query_history;
if($orderby == "date"){
$query_history = $query_history." order by TransDate2 desc";
}
else
if($orderby == "user_date"){
$query_history = $query_history." order by TransOperator asc, TransDate2 desc";
}
else
if($orderby == "company_date"){
$query_history = $query_history." order by TransCompany asc, TransDate2 desc";
}
else
if($orderby == "company_user_date"){
$query_history = $query_history." order by TransCompany asc, TransOperator asc, TransDate2 desc";
}
// echo $query_history;
$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];
	if($FormName == "Transfer"){
		$link = "Rep_UserLogTransfer.php?from_date=$from_date2&to_date=$to_date2&trans_type=$trans_type&trans_company=$trans_company&trans_year=$trans_year&trans_operator=$trans_operator&transfer_ref_search=$REF&orderby=$orderby&TransRefId=$TransId";
	}
	else
	if($FormName == "Purchase Request"){
		$link = "Rep_UserLogPurchaseRequest.php?from_date=$from_date2&to_date=$to_date2&trans_type=$trans_type&trans_company=$trans_company&trans_year=$trans_year&trans_operator=$trans_operator&prequest_ref_search=$REF&orderby=$orderby&TransRefId=$TransId";
	}
	else
	if($FormName == "Purchase Order"){
		$link = "Rep_UserLogPurchaseOrder.php?from_date=$from_date2&to_date=$to_date2&trans_type=$trans_type&trans_company=$trans_company&trans_year=$trans_year&trans_operator=$trans_operator&porder_ref_search=$REF&orderby=$orderby&TransRefId=$TransId";
	}
	else
	if($FormName == "Return Purchase"){
		$link = "Rep_UserLogReturnPurchases.php?from_date=$from_date2&to_date=$to_date2&trans_type=$trans_type&trans_company=$trans_company&trans_year=$trans_year&trans_operator=$trans_operator&purchases_ref_search=$REF&orderby=$orderby&TransRefId=$TransId";
	}
	else
	if($FormName == "Purchase"){
		$link = "Rep_UserLogPurchases.php?from_date=".$from_date2."&to_date=".$to_date2."&trans_type=".$trans_type."&trans_company=".$trans_company."&trans_year=".$trans_year."&trans_operator=".$trans_operator."&purchases_ref_search=".$REF."&orderby=".$orderby."&TransRefId=".$TransId;
	}
	else
	if($FormName == "Adjustment"){
		$link = "Rep_UserLogAdjustment.php?from_date=".$from_date2."&to_date=".$to_date2."&trans_type=".$trans_type."&trans_company=".$trans_company."&trans_year=".$trans_year."&trans_operator=".$trans_operator."&adjustment_ref_search=".$REF."&orderby=".$orderby."&TransRefId=".$TransId;
	}
	else
	if($FormName == "Production"){
		$link = "Rep_UserLogProduction.php?from_date=".$from_date2."&to_date=".$to_date2."&trans_type=".$trans_type."&trans_company=".$trans_company."&trans_year=".$trans_year."&trans_operator=".$trans_operator."&production_ref_search=".$REF."&orderby=".$orderby."&TransRefId=".$TransId;
	}
	else
	if($FormName == $Deliveries){
		$link = "Rep_UserLogDeliveries.php?from_date=".$from_date2."&to_date=".$to_date2."&trans_type=".$trans_type."&trans_company=".$trans_company."&trans_year=".$trans_year."&trans_operator=".$trans_operator."&delivery_ref_search=".$REF."&orderby=".$orderby."&TransRefId=".$TransId;
	}
	else
	if($FormName == "Return ".$Deliveries){
		$link = "Rep_UserLogReturnDeliveries.php?from_date=".$from_date2."&to_date=".$to_date2."&trans_type=".$trans_type."&trans_company=".$trans_company."&trans_year=".$trans_year."&trans_operator=".$trans_operator."&returndelivery_ref_search=".$REF."&orderby=".$orderby."&TransRefId=".$TransId;
	}
	else
	if($FormName == "Sales Order"){
		$link = "Rep_UserLogSalesOrder.php?from_date=".$from_date2."&to_date=".$to_date2."&trans_type=".$trans_type."&trans_company=".$trans_company."&trans_year=".$trans_year."&trans_operator=".$trans_operator."&sorder_ref_search=".$REF."&orderby=".$orderby."&TransRefId=".$TransId;
	}
	else
	if($FormName == "Good Receive Note"){
		$link = "Rep_UserLogGRN.php?from_date=".$from_date2."&to_date=".$to_date2."&trans_type=".$trans_type."&trans_company=".$trans_company."&trans_year=".$trans_year."&trans_operator=".$trans_operator."&grnpurchases_ref_search=".$REF."&orderby=".$orderby."&TransRefId=".$TransId;
	}
	else
	if($FormName == "POS"){
		$link = "Rep_UserLogPOS.php?from_date=".$from_date2."&to_date=".$to_date2."&trans_type=".$trans_type."&trans_company=".$trans_company."&trans_year=".$trans_year."&trans_operator=".$trans_operator."&sale_ref_search=".$REF."&orderby=".$orderby."&TransRefId=".$TransId;
	}
	else
	if($FormName == "POS"){
		$link = "Rep_UserLogTransferRequest.php?from_date=".$from_date2."&to_date=".$to_date2."&trans_type=".$trans_type."&trans_company=".$trans_company."&trans_year=".$trans_year."&trans_operator=".$trans_operator."&transfer_ref_search=".$REF."&orderby=".$orderby."&TransRefId=".$TransId;
	}
	echo "
<tr style='cursor: pointer' onclick=\"window.open('$link')\">
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 200px; text-align: left'>$FormName</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 100px; text-align: center'>$REF</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 250px; text-align: left'>$Description</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 100px;'>$TransOperator</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 75px;'>$TransType</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 140px;'>$TransCompany</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 140px;'>$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;
?>