<?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'];
$sale_ref_search = $_GET['sale_ref_search'];
//$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: POS Cash Register</title>
</head>
<style>
@page { size: landscape; }
</style>
<body>
HERE;
echo "<table width=1500 style='border-collapse: collapse'>
<tr>
<th style='padding-top: 10px; padding-bottom: 10px; font-size: 24px; font-family: Arial'>User Log: POS Cash Register</th>
</tr>
</table>";
echo "<table width=2500 style='border-collapse: collapse'>
<tr>
<th style='padding-top: 10px; padding-bottom: 10px; border-top: solid 1.5px #000000; font-size: 24px; font-family: Arial'></th>
</tr>
</table>";
$query_history = "select Sale_Ref_Id as SaleId,
sale_reference as RetailRef,
date_format(Sale_Date, '%d/%m/%Y') as Retaildate,
concat(Project_Code, ' | ', Project_Name) as Project,
concat(CostCenter_Code, ' | ', CostCenter_Name) as Costcenter,
concat(Client_code, ' | ', Client_Name) as Client,
concat(salesman_code, ' | ', salesman_name) as Salesman,
Sale_information as Info,
format(Coupon_amount, 2) as CouponAmount,
Sales_creditacc as CreditCard,
concat(Sales_CreditAmount, ' ', Sales_creditacccur) as CreditCardAmount,
Sale_CheckNo as CheckNo,
concat(Sale_Bank, ' - ', Sale_BankBranch) as BankBranch,
format(Sales_CashInBase1, 2) as CashIn,
format(Sales_CashOutBase1, 2) as CashOut,
Currency_Code as CurrencyCode,
format(Sale_DiscountAmount, 2) as DiscountAmount,
if(sale_trans_type = 'I', 'Insert' , if(sale_trans_type = 'U', 'Update', 'Delete')) as TransType,
sale_trans_company as TransCompany,
sale_trans_schema as TransYear,
date_format(sale_trans_date, '%d/%m/%Y %H:%i:%s') as TransDate,
sale_trans_operator as TransOperatorCode,
users.user_name as TransOperatorName
from ".$config['dbName_history'].".retail left join ".$_SESSION['dbLabel'].".users on users.user_account = retail.Sale_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 Sale_Trans_Date between '$from_date' and '$to_date 23:59:59'";
}
if($trans_type != "a"){
	$query_history = $query_history." and Sale_Trans_Type = '$trans_type'";
}
if($trans_company != ""){
	$query_history = $query_history." and Sale_Trans_Company = '$trans_company'";
}
else{
	$query_history = $query_history." and Sale_Trans_Company in (select distinct(CompanyName) from ".$_SESSION['dbLabel'].".listuserdir where usercode = '".$_SESSION['useraccount']."')";
}
if($trans_company != "" && $trans_year != ""){
	$query_history = $query_history." and Sale_Trans_Schema = concat('$trans_company', '_', '$trans_year')";
}
if($trans_operator != ""){
	$query_history = $query_history." and Sale_Trans_Operator = '$trans_operator'";
}
if($sale_ref_search != ""){
	$query_history = $query_history." and retail.Sale_Reference = '$sale_ref_search'";
}
if($TransRefId != ""){
	$query_history = $query_history." and retail.Sale_ref_id = '$TransRefId'";
}
if($orderby == "date"){
$query_history = $query_history." order by Sale_Trans_Date desc";
}
else
if($orderby == "user_date"){
$query_history = $query_history." order by users.user_name asc, Sale_Trans_Date desc";
}
else
if($orderby == "company_date"){
$query_history = $query_history." order by Sale_Trans_Schema asc, Sale_Trans_Date desc";
}
else
if($orderby == "company_user_date"){
$query_history = $query_history." order by Sale_Trans_Schema asc, users.user_name asc, Sale_Trans_Date desc";
}
//echo $query_history;
//$query_history = $query_history." order by delivery_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 "<table width=1500>
	<tr style='height: 8px'>
	<td></td>
	</tr>
	</td>";
	echo "<table width=1100 style='border-collapse: collapse'>
	<tr>
	<td style='font-weight: bold; width: 150px; font-family: Arial; font-size: 14px'><u>User:</u> </td>
	<td style='width: 170px; font-family: Arial; font-size: 14px'>$TransOperatorName [$TransOperatorCode]</td>
	<td style='font-weight: bold; width: 150px; font-family: Arial; font-size: 14px'><u>Transaction Date:</u></td>
	<td style='width: 200px; font-family: Arial; font-size: 14px'>$TransDate</td>
	<td style='font-weight: bold; width: 150px; font-family: Arial; font-size: 14px'><u>Transaction Type:</u></td>
	<td style='width: 100px; font-family: Arial; font-size: 14px'>$TransType</td>
	<td style='font-weight: bold; width: 150px; font-family: Arial; font-size: 14px'><u>Company / Year:</u></td>
	<td style='width: 170px; font-family: Arial; font-size: 14px'>$TransCompany | $TransYear</td>
	</tr>
	</table>";
	echo "<table width=1500>
	<tr style='height: 7px'>
	<td></td>
	</tr>
	</td>";
	echo "<table width=2500 style='border-collapse: collapse'>
	<tr>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 50px;'>Ref #</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 120px;'>Retail Date</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 150px;'>Project</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 150px;'>Costcenter</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 200px;'>Client</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 200px;'>Salesman</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 200px;'>Info</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 100px;'>Coupon Amount</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 75px;'>Check No</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 120px;'>Bank / Branch</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 75px;'>Pay</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 75px;'>Return</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 75px;'>Currency</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 75px;'>Discount Amount</th>
	</tr>
	<tr>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 50px;'>$RetailRef</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 120px;'>$Retaildate</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 150px; text-align: left'>$Project</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 150px; text-align: left'>$Costcenter</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 200px; text-align: left'>$Client</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 200px; text-align: left'>$Salesman</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 200px; text-align: left'>$Info</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 100px; text-align: right'>$CouponAmount</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 75px;'>$CheckNo</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 120px;'>$BankBranch</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 75px; text-align: right'>$CashIn</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 75px; text-align: right'>$CashOut</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 75px; '>$CurrencyCode</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 75px; text-align: right'>$DiscountAmount</th>
	</tr>
	</table>";
	echo "<table width=2500>
	<tr style='height: 7px'>
	<td></td>
	</tr>
	</table>";
	echo "<table width=2500>
	<tr>
	<td style='padding-bottom: 7px; font-weight: bold; font-family: Arial; font-size: 14px;'><u>Details:</u></td>
	</tr>
	</table>";
	echo "<table width=2500>
	<tr style='height: 7px'>
	<td></td>
	</tr>
	</table>
	<table width=800 style='border-collapse: collapse'>
	<tr>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 120px;'>Item 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: 50px;'>Qty</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 75px;'>Price</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 75px;'>Total</th>
	</tr>";
	$querydt  = "select item_code as ItemCode,
item_name as ItemName,
SaleDt_Quantity as Qty,
format(SaleDt_Price, 2) as Price,
format((SaleDt_Quantity * SaleDt_Price), 2) as TotalPrice
from ".$config['dbName_history'].".retaildt
where Sale_Ref_Id = '$SaleId'";
	//echo $querydt;
	$resultdt = mysql_query($querydt, $connection);
	while($rowdt = mysql_fetch_array($resultdt)){
		extract($rowdt);
		echo "<tr>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 120px; vertical-align: top; text-align: left'>$ItemCode</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 250px; text-align: left; vertical-align: top;'>$ItemName</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 50px; vertical-align: top;'>$Qty</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 75px; vertical-align: top; text-align: right'>$Price</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 75px; vertical-align: top; text-align: right'>$TotalPrice</th>
	</tr>";
	}
	echo"</table>
	
	<table width=2500>
	<tr style='height: 30px'>
	<td style='border-bottom: dotted 1.5px #000000'></td>
	</tr>
	</table>
	</td>";
}
				  //echo $query_history;
print <<<HERE
</body>
<script>
window.print();
</script>
</html>
HERE;
?>