<?php

//Report Version For Tiba
session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
if($_SESSION['dbName']==""){
header("location:../index.php");
}

print <<< HERE
<HTML>
<HEAD>
<TITLE> Budget Report </TITLE>

<style type="text/css">

table.master th { font-size: 14px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
                }				 
table.master td { font-size: 14px; 
                  font-family:Times-New-Roman;
                 }
table.detail th { font-size: 13px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
				  border-top:1px solid black;
				  border-bottom:1px solid black;
				  border-left:1px solid black;
                }				 
				 
table.detail td { font-size: 12px; 
                  font-family:Times-New-Roman;
				  border-left:1px solid black;
                 }			
h1.breakhere { page-break-after: auto;}				 
</style>
</HEAD>
<BODY>
HERE;

include("config.inc.php");

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass'],true,65536)	or die("Could not connect to DB");
mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");


if(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
	mysql_query("SET NAMES 'utf8'"); 
	mysql_query('SET CHARACTER SET utf8'); 
	mysql_set_charset('utf8', $connection);
}

//mysql_set_charset('utf8',$connection);

//Read From Setup Table							
$querysetup= " select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2, Setup_vatno, ".
             " Date_Format(Setup_VatStartDate,'%Y%m%d') As Setup_VatStartDate, ifNull(Setup_VatPercentage,0) As Setup_VatPercentage, ".
			 " Company_Name, Company_NameF, Company_Memo, Company_MemoF, Company_Capital, Company_CapitalF ".
			 " from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}

//Read From PurchaseSetup Table
$queryPurchaseSetup="Select PurchaseTTC From PurchaseSetup";
$resultPurchaseSetup=mysql_query($queryPurchaseSetup,$connection) or die(mysql_error());
while($rowPurchaseSetup=mysql_fetch_array($resultPurchaseSetup)){
	extract($rowPurchaseSetup);
}

//Read From Module Table
$querymodule=" Select ifNull(Module_AdditionalTaxes,0) As Module_AdditionalTaxes, ifNull(Module_ShowInvoiceAging,0) As Module_ShowInvoiceAging, ".
             " ifNull(Module_ShowLayoutProj,0) As Module_ShowLayoutProj, ifNull(Module_ShowLayoutCost,0) As Module_ShowLayoutCost ".
			 " From Module ";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}

$fromdate		=	$_GET['fromdate'];
$todate			=	$_GET['todate'];					
$fromledger		=	$_GET['fromledger'];
$toledger		=	$_GET['toledger'];

$fromproject	= $_GET['fromproject'];
$fromcostcent	= $_GET['fromcostcent'];
$dbcd			= $_GET['dbcd'];
$detailgroup	= $_GET['detailgroup'];


$querysetup ="select base1, base2 from ".$config['dbName_log'].".setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultsetup)){
	extract($rowmodule);
}
	
	
$style="style='border:1px solid black; text-align:right;'";
echo"<table border='0' width='1100px' cellspacing=0 cellpadding=0 class='master' style='table-layout:fixed;' >
		<col width=200>
		<col width=300>
		<col width=300>";
		//T-header
echo"	<tr>
			<td align=' left' valign='top' style='font-size:14px; font-style:italic; font-weight:bold;'><img src='img/siegwerkl.png' ><td>
			<td align='left' valign='top' style='font-size:14px; font-style:italic; font-weight:bold;'><td>
			<td align='left' valign='top' style='font-size:14px; font-style:italic; font-weight:bold;'><td>
		<tr>";
		
		//2nd row
echo "	<tr>
			<td align='right' colspan='2' valign='top' style='font-size:14px; font-weight:bold;'>Monthly Distribution of Accounts<td>
			<td align='left' valign='top' style='font-size:14px; font-style:italic; font-weight:bold;'>
				<td colspan='2'><b>Printed Date:</b>";
			echo date("d/m/Y")."<br>";
				echo "<span><b>Time:</b></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".date("h:i:sa");
				echo "</td>
			<td>";
			
			$querygetname="select project_name,project_code from project where project_code='$txtProjectCode'";
			$resultproject_name=mysql_query($querygetname,$connection);
			$row=mysql_fetch_array($resultproject_name);
			extract($row);
		//table header
echo "	</tr>

		<tr>
			<td>
				<table style=' border: 1px solid black;background-color:#d3d3d3' width='1060px' >
					<col width=200>
					<col width=200>
					<col width=200>
					<col width=200>
					<col width=200>
					<col width=200>
					
					<tr>
						<td align='center'><span style='font-size:14px; font-weight:bold;'>Ledger From:</span>&nbsp;<span style='font-family:serif; font-size:14px; font-weight:bold;'>$fromledger</span></td>
						<td align='center'><span style='font-size:14px; font-weight:bold;'>Project:</span>&nbsp;<span style='font-family:serif; font-size:14px; font-weight:bold;'>$fromproject</span></td>
						<td align='center'><span style='font-size:14px; font-weight:bold;'>Year:</span>&nbsp;<span style='font-family:serif; font-size:14px; font-weight:bold;'>".date("Y")."</span></td>
					</tr>
					
					<tr>
						<td align='center'><span style='font-size:14px; font-weight:bold;'>Ledger To:</span>&nbsp;<span style='font-family:serif; font-size:14px; font-weight:bold;'>$toledger</span></td>
						<td align='center'><span style='font-size:14px; font-weight:bold;'>Costcenter:</span>&nbsp;<span style='font-family:serif; font-size:14px; font-weight:bold;'>$fromcostcent</span></td>
						<td align='center'><span style='font-size:14px; font-weight:bold;'>Currency:</span>&nbsp;<span style='font-family:serif; font-size:14px; font-weight:bold;'> $curr";
													
							$style2="style='border-right:1px solid; border-bottom:1px solid; font-size:16px;'";
echo "					</span></td>
					</tr>
					
					<tr>
						<td colspan='1'><td>
						<td  colspan='2' align='left' style='background-color:white;'>N.B:Actual Balance(1#),Budget Balance(2#),Difference(3#),JT Journal Type.</td>
					</tr>	
				</table>
				<table style=' border: 1px solid black;background-color:#d3d3d3' width='1060px'>
					<col width=220><col width=200><col width=220><col width=200><col width=200><col width=200><col width=200><col width=200><col width=200>
					<col width=200><col width=200><col width=200><col width=200><col width=200>
					<tr style=''>
						<td $style2 align='center'></td>
						<td $style2 align='center'>January</td>
						<td $style2 align='center'>Fabruary</td>
						<td $style2 align='center'>March</td>
						<td $style2 align='center'>April</td>
						<td $style2 align='center'>May</td>
						<td $style2 align='center'>June</td>
						<td $style2 align='center'>July</td>
						<td $style2 align='center'>August</td>
						<td $style2 align='center'>September</td>
						<td $style2 align='center'>October</td>
						<td $style2 align='center'>November</td>
						<td $style2 align='center'>December</td>
						<td $style2 align='center'>Total</td>
					</tr>";
					
					$query = "select budget.ledger_number,a.ledger_name,budget.Budget_DBCD,budget.Journal_Type,budget.CostCenter_Code,budget.project_code,
								if ('$curr' = '$base1', Month1_base1, Month1_base2),
								if ('$curr' = '$base1',Month2_base1,Month2_base2),
								if ('$curr' = '$base1',Month3_base1,Month3_base2),
								if ('$curr' = '$base1',Month4_base1,Month4_base2),
								if ('$curr' = '$base1',Month5_base1,Month5_base2),
								if ('$curr' = '$base1',Month6_base1,Month6_base2),
								if ('$curr' = '$base1',Month7_base1,Month7_base2),
								if ('$curr' = '$base1',Month8_base1,Month8_base2),
								if ('$curr' = '$base1',Month9_base1,Month9_base2),
								if ('$curr' = '$base1',Month10_base1,Month10_base2),
								if ('$curr' = '$base1',Month11_base1,Month11_base2),
								if ('$curr' = '$base1',Month12_base1,Month12_base2),
								b.project_name,c.journal_type
								from budget
								left join ledger a on budget.ledger_number = a.ledger_number
								left join project b on budget.project_code = b.project_code
								left join journal c on budget.journal_type=c.journal_id
								where if ('$fromledger' = '', 1=1, budget.ledger_number between '$fromledger' AND '$toledger') and if ('$fromcostcent' = '', 1=1, budget.costcenter_code='$fromcostcent')
								and if ('$fromproject' = '', 1=1, budget.project_code='$fromproject') and if ('$dbcd' = '', 1=1, budget.Budget_DBCD = '$dbcd') 
								and if ('$detailgroup' = '', 1=1, ledger_grpdet = '$detailgroup')  
								and if ('$fromdate' = '', 1=1, date_format(Budget_Sdate, '%Y-%m-%d') = '$fromdate') and if ('$todate' = '', 1=1, date_format(Budget_Edate, '%Y-%m-%d')  = '$todate')
							order by budget.ledger_number ";
			
					//echo $query; exit;
					$result = mysql_query($query,$connection);
					$total=0;
					while($row_ledger=mysql_fetch_array($result)){
	echo "			<tr>
							<td><span style='font-size:13px; font-weight:bold;'>Ledger No:</span></td>
							<td colspan='1' style='font-family:serif; font-size:14px; font-weight:bold;'>$row_ledger[0]</td>
							<td><span style='font-size:13px; font-weight:bold;'>Ledger Name:</span></td>
							<td colspan='3' style='font-family:serif; font-size:14px; font-weight:bold;'>$row_ledger[1]</td>
							
							<td align='right'><span style='font-size:13px; font-weight:bold;'>D/C:</span></td>
							<td style='font-family:serif; font-size:14px; font-weight:bold;'> ";
							if($row_ledger[2]=='D')echo "Debit";
							if($row_ledger[2]=='C') echo "Credit";
							echo "</td>
							<td align='right'><span style='font-size:13px; font-weight:bold;'>Jour Type:</span></td>
							<td style='font-family:serif; font-size:14px; font-weight:bold;'>$row_ledger[19]</td>
							<td><span style='font-size:13px; font-weight:bold;'>Cost Center:</span></td>
							<td style='font-family:serif; font-size:14px; font-weight:bold;'>$row_ledger[4]</td>
							<td align='right'><span style='font-size:13px; font-weight:bold;'>Project:</span></td>
							<td style='font-family:serif; font-size:14px; font-weight:bold;'>$row_ledger[18]</td>
						</tr>
												
						<tr style='background-color:white; border: 1px solid black;'>
							<td style='background-color:#d3d3d3;'></td>
							<td $style>".number_format($row_ledger[6],$DecimalBase1)."</td>
							<td $style>".number_format($row_ledger[7],$DecimalBase1)."</td>
							<td $style>".number_format($row_ledger[8],$DecimalBase1)."</td>
							<td $style>".number_format($row_ledger[9],$DecimalBase1)."</td>
							<td $style>".number_format($row_ledger[10],$DecimalBase1)."</td>
							<td $style>".number_format($row_ledger[11],$DecimalBase1)."</td>
							<td $style>".number_format($row_ledger[12],$DecimalBase1)."</td>
							<td $style>".number_format($row_ledger[13],$DecimalBase1)."</td>
							<td $style>".number_format($row_ledger[14],$DecimalBase1)."</td>
							<td $style>".number_format($row_ledger[15],$DecimalBase1)."</td>
							<td $style>".number_format($row_ledger[16],$DecimalBase1)."</td>
							<td $style>".number_format($row_ledger[17],$DecimalBase1)."</td>";
							$total_budget=$row_ledger[6]+$row_ledger[7]+$row_ledger[8]+$row_ledger[9]+$row_ledger[10]+$row_ledger[11]+$row_ledger[12]+$row_ledger[13]+$row_ledger[14]+$row_ledger[15]+$row_ledger[16]+$row_ledger[17];
						echo "	
							<td $style>".number_format($total_budget,$DecimalBase1)."</td>
						</tr>
						
						</tr><h1 CLASS='breakhere'>";
					}
					
echo "			</table>
			</td>
		</tr>
	</table>";
print <<< HERE

<script language=javascript>
if (typeof(window.print) != 'undefined')
{
    window.print();
}
</script>
</BODY>
</HTML>
HERE;
	?>