<?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'];
$delivery_ref_search = $_GET['delivery_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: $Deliveries</title>
</head>
<style>
@page { size: landscape; }
</style>
<body>
HERE;
echo "<table width=1350 style='border-collapse: collapse'>
<tr>
<th style='padding-top: 10px;   font-size: 24px; font-family: Arial'>User Log: $Deliveries</th>
</tr>
</table>";


echo "<table width=1350 >
	<tr>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 100px;'>Ref #</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 300px;'>Client</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 100px;'>Total</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 100px;'>Type</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 100px;'>Delivery Date</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 200px;'>Transaction Date</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 200px;'>User</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 250px;'>Company Year</th>
	</tr>";

//echo $prtdt;exit;

$query_history = "select delivery_ref_id as DeliveryId, Delivery_Reference, date_format(Delivery_Date, '%d/%m/%Y') as DeliveryDate, deliveries.client_code as ClientCode, deliveries.Client_Name as ClientName, deliveries.salesman_code as SalesmanCode,
				  Round((Delivery_total+delivery_vat),2) as DeliveryTotal,
				  Delivery_Remark as DeliveryRemark, deliveries.Currency_code as CurrencyCode, date_format(JOURNALDT_VALUEDATE, '%d/%m/%Y') as MaturityDate, Delivery_Marks as VehicleNumber, Delivery_ShippedVia as DriverName, deliveries.project_code as ProjectCode, 
				  deliveries.costcent_code as CostcentCode, if(delivery_trans_type = 'I', 'Insert' , if(delivery_trans_type = 'U', 'Update', 'Delete')) as TransType, delivery_trans_company as TransCompany, delivery_trans_schema as TransYear, date_format(delivery_trans_date, '%d/%m/%Y %H:%i:%s') as TransDate, delivery_trans_operator as TransOperatorCode, users.user_name as TransOperatorName, Delivery_ManualNo
				  from ".$config['dbName_history'].".deliveries inner join ".$_SESSION['dbLabel'].".users on users.user_account = deliveries.delivery_trans_operator
				  where 1=1 and (deliveries.Journal_Type = 'SI' or deliveries.Journal_Type = 'DI')";
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 delivery_trans_date between '$from_date' and '$to_date 23:59:59'";
}
if($trans_type != "a"){
	$query_history = $query_history." and delivery_trans_type = '$trans_type'";
}
if($trans_company != ""){
	$query_history = $query_history." and delivery_trans_company = '$trans_company'";
}
else{
	$query_history = $query_history." and delivery_trans_company in (select distinct(CompanyName) from ".$_SESSION['dbLabel'].".listuserdir where usercode = '".$_SESSION['useraccount']."')";
}
if($trans_company != "" && $trans_year != ""){
	$query_history = $query_history." and delivery_trans_schema = concat('$trans_company', '_', '$trans_year')";
}
if($trans_operator != ""){
	$query_history = $query_history." and delivery_trans_operator = '$trans_operator'";
}
if($delivery_ref_search != ""){
	$query_history = $query_history." and deliveries.delivery_reference = '$delivery_ref_search'";
}
if($TransRefId != ""){
	$query_history = $query_history." and deliveries.delivery_ref_id = '$TransRefId'";
}
if($orderby == "date"){
$query_history = $query_history." order by delivery_trans_date desc";
}
else
if($orderby == "user_date"){
$query_history = $query_history." order by users.user_name asc, delivery_trans_date desc";
}
else
if($orderby == "company_date"){
$query_history = $query_history." order by delivery_trans_schema asc, delivery_trans_date desc";
}
else
if($orderby == "company_user_date"){
$query_history = $query_history." order by delivery_trans_schema asc, users.user_name asc, delivery_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=1350 >
	</td>";
	
	

	
	if($prtdt==1) {
	
	echo"<tr style='background-color: #a9abac;'>
	<th style='font-size: 12px; font-family: Arial; border: solid 1px #000000; width: 100px; vertical-align: top;'>$Delivery_Reference</th>
	<th style='font-size: 12px; font-family: Arial; border: solid 1px #000000; width: 300px; text-align: left; vertical-align: top;'>$ClientCode - $ClientName</th>
	<th style='font-size: 12px; font-family: Arial; border: solid 1px #000000; width: 300px; text-align: left; vertical-align: top;'>".number_format($DeliveryTotal, 2)."&nbsp;</th>
	<th style='font-size: 12px; font-family: Arial; border: solid 1px #000000; width: 100px; text-align: left; vertical-align: top;'>$TransType</th>
	<th style='font-size: 12px; font-family: Arial; border: solid 1px #000000; width: 100px; vertical-align: top;'>$DeliveryDate</th>
	<th style='font-size: 12px; font-family: Arial; border: solid 1px #000000; width: 200px; vertical-align: top;'>$TransDate</th>
	<th style='font-size: 12px; font-family: Arial; border: solid 1px #000000; width: 200px; vertical-align: top;'>$TransOperatorName [$TransOperatorCode]</th>
	<th style='font-size: 12px; font-family: Arial; border: solid 1px #000000; width: 250px; vertical-align: top;'>$TransCompany | $TransYear</th>
	</tr>";
	
	} else {
	
    echo"<tr>
	<td style='font-size: 12px; font-family: Arial; border: solid 1px #000000; width: 100px; vertical-align: top;'>$Delivery_Reference</th>
	<td style='font-size: 12px; font-family: Arial; border: solid 1px #000000; width: 300px; text-align: left; vertical-align: top;'>$ClientCode - $ClientName</th>
	<th style='font-size: 12px; font-family: Arial; border: solid 1px #000000; width: 100px; text-align: left; vertical-align: top;'>$DeliveryTotal</th>
	<td style='font-size: 12px; font-family: Arial; border: solid 1px #000000; width: 100px; text-align: left; vertical-align: top;'>$TransType</th>
	<td style='font-size: 12px; font-family: Arial; border: solid 1px #000000; width: 100px; vertical-align: top;'>$DeliveryDate</th>
	<td style='font-size: 12px; font-family: Arial; border: solid 1px #000000; width: 200px; vertical-align: top;'>$TransDate</th>
	<td style='font-size: 12px; font-family: Arial; border: solid 1px #000000; width: 200px; vertical-align: top;'>$TransOperatorName [$TransOperatorCode]</th>
	<td style='font-size: 12px; font-family: Arial; border: solid 1px #000000; width: 250px; vertical-align: top;'>$TransCompany | $TransYear</th>
	</tr>";
		
		
	}
	echo"</table>";
	
	
	if($prtdt==1) {
	
	
	echo "<table width=1350>
	<tr style='height: 7px'>
	<td></td>
	</tr>
	</table>
	<table width=1000 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: 75px;'>W.H.</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;'>Discount</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 75px;'>Total</th>

	</tr>";
	$querydt  = "select deliverydt.item_code as ItemCode, deliverydt.item_name as ItemName, deliverydt.Warehouse_Code, DeliveryDt_Quantity as Qty, format(SaleDt_Price, 2) as Price, concat(format(DeliveryDt_DiscountPer, 2), ' %') as DiscPer, Project_code as ProjectCode, Costcent_code as CostcentCode, date_format(DeliveryDt_ExpireDate, '%d/%m/%Y') as ExpireDate, Deliverydt_remark as Remarks, format(((SaleDt_Price - DeliveryDt_DiscountAmount) * DeliveryDt_Quantity), 2) as TotalPrice
	             from ".$config['dbName_history'].".deliverydt
				 where delivery_ref_id = '$DeliveryId'";
	//echo $querydt;
	$resultdt = mysql_query($querydt, $connection);
	while($rowdt = mysql_fetch_array($resultdt)){
		extract($rowdt);
		echo "<tr>
	<td style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 120px; vertical-align: top; text-align: left'>$ItemCode</td>
	<td style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 250px; text-align: left; vertical-align: top;'>$ItemName</td>
	<td style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 75px; vertical-align: top;'>$Warehouse_Code</td>
	<td style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 50px; vertical-align: top;'>$Qty</td>
	<td style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 75px; vertical-align: top;'>$Price</td>
	<td style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 75px; vertical-align: top;'>$DiscPer</td>
	<td style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 75px; vertical-align: top;'>$TotalPrice</td>
	</tr>";
	}
	echo"</table>
	
	
	</td>";
	
	}
}
				  //echo $query_history;
print <<<HERE
</body>
<script>
window.print();
</script>
</html>
HERE;
?>