<?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'];
$production_ref_search = $_GET['production_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: Production [Assemble]</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; border-bottom: solid 1.5px #000000; font-size: 24px; font-family: Arial'>User Log: Production [Assemble]</th>
</tr>
</table>";
$query_history = "SELECT Production_ref_id AS ProductionId,
       Production_Reference,
       production.Item_Code as ItemCode,
       production.Item_Name as ItemName,
       date_format(Production_Date, '%d/%m/%Y') AS ProductionDate,
       production.Currency_Code AS CurrencyCode,
       production.Warehouse_Code AS WarehouseCode,
       production.Warehouse_Name as WarehouseName,
       production.Project_code AS ProjectCode,
       production.Project_Name AS ProjectName,
       production.Costcenter_Code AS CostcentCode,
       production.Costcent_Name AS CostcentName,
       production.Client_Code AS ClientCode,
       production.Client_Name AS ClientName,
       production.Production_quantity AS ActualQty,
       production.Production_idealqty AS IdealQty,
       production.Quantity_Ill as YieldQty,
       production.Production_BatchNo as BatchNo,
       date_format(production.Production_ExpireDate, '%d/%m/%Y %H:%i:%s') as ExpireDate,
       production.Production_Remark as Remarks,
       if(Production_trans_type = 'I', 'Insert', if(Production_trans_type = 'U', 'Update', 'Delete')) AS TransType,
       Production_trans_company AS TransCompany,
       Production_trans_schema AS TransYear,
       date_format(Production_trans_date, '%d/%m/%Y %H:%i:%s') AS TransDate,
       Production_trans_operator AS TransOperatorCode,
       users.user_name AS TransOperatorName
FROM ".$config['dbName_history'].".production
INNER JOIN ".$_SESSION['dbLabel'].".users ON users.user_account = production.Production_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 Production_trans_date between '$from_date' and '$to_date 23:59:59'";
}
if($trans_type != "a"){
	$query_history = $query_history." and Production_trans_type = '$trans_type'";
}
if($trans_company != ""){
	$query_history = $query_history." and Production_trans_company = '$trans_company'";
}
else{
	$query_history = $query_history." and Production_trans_company in (select distinct(CompanyName) from ".$_SESSION['dbLabel'].".listuserdir where usercode = '".$_SESSION['useraccount']."')";
}
if($trans_company != "" && $trans_year != ""){
	$query_history = $query_history." and Production_trans_schema = concat('$trans_company', '_', '$trans_year')";
}
if($trans_operator != ""){
	$query_history = $query_history." and Production_trans_operator = '$trans_operator'";
}
if($production_ref_search != ""){
	$query_history = $query_history." and production.Production_Reference = '$production_ref_search'";
}
if($TransRefId != ""){
	$query_history = $query_history." and production.Production_ref_id = '$TransRefId'";
}
if($orderby == "date"){
$query_history = $query_history." order by Production_trans_date desc";
}
else
if($orderby == "user_date"){
$query_history = $query_history." order by users.user_name asc, Production_trans_date desc";
}
else
if($orderby == "company_date"){
$query_history = $query_history." order by Production_trans_schema asc, Production_trans_date desc";
}
else
if($orderby == "company_user_date"){
$query_history = $query_history." order by Production_trans_schema asc, users.user_name asc, Production_trans_date desc";
}
//echo $query_history;
//$query_history = $query_history." order by Production_trans_date desc";
$result_history = mysql_query($query_history, $connection);
while($row_history = mysql_fetch_array($result_history)){
	extract($row_history);
	
	
	$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=1500 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: 200px;'>Item</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 100px;'>Date</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: 150px;'>Warehouse</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: 50px;'>Actual Qty</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 50px;'>Ideal Qty</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 50px;'>Yield Qty</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 100px;'>Batch No</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 100px;'>Expire Date</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 100px;'>Remarks</th>
	</tr>
	<tr>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 50px; vertical-align: top;'>$Production_Reference</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 200px; text-align: left; vertical-align: top;'>$ItemCode | $ItemName</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 100px; vertical-align: top;'>$ProductionDate</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 75px; vertical-align: top;'>$CurrencyCode</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 150px; vertical-align: top; text-align: left'>$WarehouseCode | $WarehouseName</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 150px; text-align: left; vertical-align: top;'>$ProjectCode | $ProjectName</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 150px; text-align: left; vertical-align: top;'>$CostcentCode | $CostcentName</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 200px; text-align: left; vertical-align: top;'>$ClientCode | $ClientName</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 50px; text-align: left; vertical-align: top;'>$ActualQty</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 50px; text-align: left; vertical-align: top;'>$IdealQty</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 50px; text-align: left; vertical-align: top;'>$YieldQty</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 100px; text-align: left; vertical-align: top;'>$BatchNo</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 100px; text-align: left; vertical-align: top;'>$ExpireDate</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 100px; text-align: left; vertical-align: top;'>$Remarks</th>
	</tr>
	</table>";
	echo "<table width=1500>
	<tr style='height: 7px'>
	<td></td>
	</tr>
	</table>";
	echo "<table width=1500>
	<tr>
	<td style='padding-bottom: 7px; font-weight: bold; font-family: Arial; font-size: 14px;'><u>Details:</u></td>
	</tr>
	</table>";
	echo "<table width=1500>
	<tr style='height: 7px'>
	<td></td>
	</tr>
	</table>
	<table width=1500 style='border-collapse: collapse'>
	<tr>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 150px;'>Item Code</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 300px;'>Description</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 150px;'>W.H.</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 50px;'>Actual Qty</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 100px;'>Cost Price</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: 100px;'>Batch No</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 120px;'>Expire Date</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 230px;'>Remarks</th>
	</tr>";
	$querydt  = "select productiondt.item_code as ItemCode,
productiondt.Item_Name as ItemName,
productiondt.Warehouse_Code as WarehouseCode,
productiondt.Warehouse_Name as WarehouseName,
productiondt.ProductionDt_Quantity as ActualQty,
productiondt.ProductionDt_cost as CostPrice,
productiondt.Project_code as ProjectCode,
productiondt.Project_Name as ProjectName,
productiondt.Costcenter_Code as CostcentCode,
productiondt.Costcent_Name as CostcentName,
productiondt.ProductionDt_BatchNo as BatchNo,
date_format(ProductionDt_ExpireDate, '%d/%m/%Y %H:%i:%s') as ExpireDate,
Productiondt_Remark as Remarks
from ".$config['dbName_history'].".productiondt
WHERE production_ref_id = '$ProductionId'";
	//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: 150px; vertical-align: top; text-align: left'>$ItemCode</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 300px; text-align: left; vertical-align: top;'>$ItemName</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 150px; vertical-align: top; text-align: left'>$WarehouseCode | $WarehouseName</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 50px; vertical-align: top;'>$ActualQty</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 100px; vertical-align: top; '>$CostPrice</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 150px; vertical-align: top; text-align: left'>$ProjectCode | $ProjectName</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 150px; vertical-align: top; text-align: left'>$CostcentCode | $CostcentName</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 100px; text-align: left; vertical-align: top;'>$BatchNo</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 120px; text-align: left; vertical-align: top;'>$ExpireDate</th>
	<th style='font-size: 14px; font-family: Arial; border: solid 1px #000000; width: 230px; text-align: left; vertical-align: top;'>$Remarks</th>
	</tr>";
	}
	echo"</table>
	
	<table width=1500>
	<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;
?>