<?php

session_start();
if($HTTP_SESSION_VARS['dbName']==""){
header("location:../index.php");
}

include("config.inc.php");
include("Functions_Trans.php");

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


$querylog="SELECT FA_Invoicing as loginaccess from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' 	and Upper(Login_Schema) = Upper('".$HTTP_SESSION_VARS['dbName']."')	";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}

if ($loginaccess=='' or $loginaccess==0)
	header("location:menu.php");




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

//Read From Module Table
$querymodule=" Select ifNull(Module_Project,0) As Module_Project, ifNull(Module_Costcenter,0) As Module_CostCenter, ".
			 " ifNull(Module_RestrictProjCost,0) As Module_RestrictProjCost, ifNull(Module_RestrictCost,0) As Module_RestrictCost, ".
			 " ifNull(Module_Delivery,0) As Module_Delivery, ifNull(Module_AcctrxPostRV,0) As Module_AcctrxPostRV, ".
			 " ifNull(Module_AcctrxPostCV,0) As Module_AcctrxPostCV ". 
             " From Module ";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}


$querycurrency="select Currency_code from currency order by currency_code";
$resultcurrency=mysql_query($querycurrency,$connection) or die(mysql_error());

$sub_query = "Select Ledger_Name From Ledger Where Ledger_Number = $fromledger";
$sub_result = selectData($connection, $sub_query);
$fromledgername = $sub_result[0];

$sub_query = "Select Ledger_Name From Ledger Where Ledger_Number = $toledger";
$sub_result = selectData($connection, $sub_query);
$toledgername = $sub_result[0];

if (empty($fromdate))    $fromdate = displayToday();
if (empty($todate))      $todate   = displayToday();
if (empty($invoicedate)) $invoicedate   = displayToday();
if (empty($invoicecurr)) $invoicecurr   = $base1;

print <<< HERE
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=windows-1256" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 


<link rel="stylesheet" type="text/css" media="all" href="css-v2/style.css">
<link rel="stylesheet" type="text/css" media="all" href="js/grid/UI_DataGrid_V1.css">
<script type="text/javascript" language="javascript">

function SearchForm() {
	var x=document.invoices;
	
	x.actionmode.value = 'Search';
	x.submit();
}

function SubmitForm() {
	var x=document.invoices;
	
	var vFromLedger = x.fromledger.value;
	var vToLedger   = x.toledger.value;
	var vFromDate   = x.fromdate.value;
	var vToDate     = x.todate.value;
	
	var vInvDate     = x.invoicedate.value;
	var vInvCurr     = x.invoicecurr.value;
	
		
	if (vFromLedger == "" && vToLedger == "") {
		alert("Please select a client.");
	} 
	else if (vFromDate == "" || vToDate == "") {
		alert("Please select a date.");
	}
	else if (vInvDate == "") {
		alert("Please select invoice date.");
		x.invoicedate.focus();		
	}
	else if (vInvCurr == "") {
		alert("Please select invoice currency.");
		x.invoicecurr.focus();
	}	
	else {
		if (document.getElementById('btnSubmit'))
			x.btnSubmit.disabled = true;
			
		x.action =  "Form_Invoicing_Actions.php?mode=GenerateInvoices&fromledger="+vFromLedger+"&toledger="+vToLedger+"&fromdate="+vFromDate+"&todate="+vToDate
		           +"&invoicedate="+vInvDate+"&invoicecurr="+vInvCurr        ;
		x.submit();
		
		//document.getElementById("mainDiv").style.display="block";
		document.getElementById("imgDiv").style.display="block";
	}	

}

function SelectInvoice(TagValue) {
	
	for (var i=0; i<document.getElementsByName('ChkSI[]').length; i++) {
		if (document.getElementsByName('ChkSI[]')[i].value != TagValue)
			document.getElementsByName('ChkSI[]')[i].checked = false;
	}

}

function showlistledger(str){

	var str1 = 2;
	window.open('select_accountno.php?item='+str+'&accountoption='+str1,'mywindow','width=710,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

function showlistProject(frm) {
	window.open('List_SorProject.php?frm='+frm+'&item=','mywindow','width=850,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

function showlistCostcenter(frm){

  window.open('List_SorCostcenter.php?frm='+frm+'&item=','mywindow','width=850,height=550,,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}


function roundNumber(num, dec) { // Arguments: number to round, number of decimal places
  var newnumber = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
  newnumber = parseFloat(newnumber); 
  
  return newnumber;
}


</script>
<title>Generate Invoices</title>
<style type="text/css">


.style5 {color: #5151FF; font-size: 10px;}


div#imgDiv {
	font-size: 12px; 
    font-weight:bold; 
	color: #FF0000 ;
	text-align: center; 
	margin-top:10px;
	display:none;
}
</style>

<link type="text/css" rel="stylesheet" href="dhtmlgoodies_calendar.css?random=20051112" media="screen"></LINK>
<SCRIPT type="text/javascript" src="dhtmlgoodies_calendar.js?random=20060118"></script>
</head>
<body>
<form name='invoices' method='post' action=''>
<input type='hidden' name='actionmode' value='$actionmode'>
<input type='hidden' name='mainaction' value='$mainaction'>
<input type='hidden' name='message' value='$message' size='200'>
HERE;

//$style= "style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'";

echo"<table cellpadding='0' cellspacing='0' border='0'  align='center' class='layout-width'>";
include_once("header.php");
echo" <tr><td  valign='top'>";

include_once("sidemenu.php");
echo"  </td></tr>";


echo" <tr style='height:26px;'>
		<td  valign='top' id='content-top'>
			<table border='0' cellpadding='0' cellspacing='0' style='table-layout:fixed; height:26px;' class='layout-width'>
			<tr style='height:26px;'>
				<td style='width:38px; height:26px;'>&nbsp;</td>
				<td valign='middle' style='width:300px; height:26px;'><span class='title'>GENERATE INVOICES</span></td>
				<td class='message' valign='middle' style='height:26px;'>".htmlspecialchars($message, ENT_QUOTES)."</td>
			</tr></table>
		</td>
	  </tr>";

echo"<tr>
		<td  height='34'  valign='top' id='content-top2'>
			<table border='0' cellpadding='0' cellspacing='0' class='layout-width'>
			<tr>
				<td width='30'  height='34'>&nbsp;</td>
				<td valign='middle' height='34' width='720px' align='left'>";
				
	echo" <button type='button' id='btnSubmit' name='btnSubmit' onclick='SubmitForm();return false;' title='Generate Invoices'><span><span>Generate</span></span></button>";			

echo"	</td></tr></table>";
echo"</td></tr>";
				
echo"<tr><td id='content-body'>
<table border='0'  align='center' valign='top' width=100% class='tablecontent'>
   <tr bgcolor='#F7F7F7'><td valign='top' align='left' colspan='10'>";	
print <<<HERE
		
		
		<table border="0" style='table-layout:fixed'>
		  <col width=70>
		  <col width=200>
		  <col width=70>
		  <col width=630>

HERE;



echo" <tr>";	  
echo"   <td height='19' colspan='2' align='left' bgcolor='#F5F5F5'><span class='style1'>Date</span></td>";
echo"   <td height='19' colspan='2' align='left' bgcolor='#F5F5F5'><span class='style1'>Client Code / Name</span></td>";
echo" </tr>";

echo" <tr height='25'>";	  
echo"   <td valign='middle' bgcolor='#F5F5F5' ><span class='style1'>From</span></td>";
echo"   <td valign='middle' bgcolor='#F5F5F5' ><span class='style1'>";
echo"      <input type='text' name='fromdate' value='$fromdate' maxlength='10' size='10'>";
echo"      <A HREF=\"#\" onClick=\"displayCalendar(document.invoices.fromdate,'dd/mm/yyyy',this); return false;\" NAME=\"anchor1\" ID=\"anchor1\"><image src='./img/cal.gif' border=0 align=center></image></A></span></td>";
echo"   <td align='left' bgcolor='#F5F5F5' ><span class='style1'>From</span></td>";
echo"   <td valign='middle' bgcolor='#F5F5F5' ><span class='style1'>";
echo"   <input name='fromledger' type='text' id='fromledger' size='10' value='".$fromledger."' readonly />
        <input name='fromledgername' type='text' id='fromledgername' size='35' value='".$fromledgername."' readonly />
		</span>
		<label><span class='style1'><a href='javascript:void(0);' NAME='My Window Name' title=' Select Ledger From ' 
             onClick='showlistledger(3); '><img src='img/btnopen.jpg' width='15' height='20' border='0' /></a> </span></label>";
echo"	</td>";
				
echo" </tr>";

echo" <tr height='25'>";
echo"   <td valign='top' bgcolor='#F5F5F5'><span class='style1'>To</span></td>";
echo"   <td valign='top' bgcolor='#F5F5F5'><span class='style1'>";
echo"      <input type='text' name='todate' value='$todate'  maxlength='10' size='10'>";
echo"      <A HREF=\"#\" onClick=\"displayCalendar(document.invoices.todate,'dd/mm/yyyy',this); return false;\" NAME=\"anchor1\" ID=\"anchor1\"><image src='./img/cal.gif' border=0 align=center></image></A></span></td>";
echo"   <td align='left' bgcolor='#F5F5F5' ><span class='style1'>To</span></td>";
echo"   <td valign='middle' bgcolor='#F5F5F5' ><span class='style1'>";
echo" 	 	<input name='toledger' type='text' id='toledger' size='10' value='".$toledger."' readonly />
			<input name='toledgername' type='text' id='toledgername' size='35' value='".$toledgername."' readonly />
		</span>";
echo"	<label><span class='style1'><a href='javascript:void(0);' NAME='My Window Name' title=' Select Ledger To ' 
             onClick='showlistledger(4); '><img src='img/btnopen.jpg' width='15' height='20' border='0' /></a> </span></label>";
echo"	</td>";
echo" </tr>";

echo" <tr height='25'>";
echo"   <td colspan='4' align='left' valign='middle' bgcolor='#F5F5F5'><span class='style1'>INVOICES</span></td>";
echo" </tr>";

echo" <tr height='25'>";
echo"   <td align='left' bgcolor='#F5F5F5' ><span class='style1'>Inv.Date</span></td>";
echo"   <td align='left' bgcolor='#F5F5F5' ><input type='text' name='invoicedate' value='$invoicedate'  maxlength='10' size='10'>";
echo"      <A HREF=\"#\" onClick=\"displayCalendar(document.invoices.invoicedate,'dd/mm/yyyy',this); return false;\" NAME=\"anchor1\" ID=\"anchor1\"><image src='./img/cal.gif' border=0 align=center></image></A></span></td>";
echo"   <td valign='middle' bgcolor='#F5F5F5'><span class='style1'>Curr.</span></td>";
echo"   <td valign='middle' bgcolor='#F5F5F5'><span class='style1'><select name='invoicecurr' id='invoicecurr' style='width:90px;'>";
			echo"<option></option>";
			while($curf=mysql_fetch_row($resultcurrency)){
			echo"<option ";
				if ($invoicecurr==$curf[0])
					echo " selected ";
				echo" value='$curf[0]'>$curf[0] </option>";
			}
			echo"</select>";
echo"	</td>";
echo" </tr>";

echo" <tr height='65'>";
echo"   <td colspan='4' align='center' valign='middle' bgcolor='#F5F5F5'>";
echo "		<div id='imgDiv'><p><image src='img/ajax-loader.gif' id='loader-img' /></p></div>";
echo"	</td>";
echo" </tr>";



//Display Invoices
if ($actionmode == 'Search') {

	echo" <tr>";
	echo"   <td colspan='4' valign='top' bgcolor='#F5F5F5'>
			<div style='overflow:auto; height:180px; width:965px;' ><table border='0' style='table-layout:fixed; width:1040px;' class='grid_data'>
			  <col width=80>
			  <col width=80>
			  <col width=80>
			  <col width=100>
			  <col width=250>
			  <col width=50>
			  <col width=100>
			  <col width=100>
			  <col width=100>
			  <col width=100>";
			  
	echo"	<tr height='20px'>";
	echo"		<th align='center'><span class='style1'>Date</span></th>";
	echo"		<th align='center'><span class='style1'>Reference</span></th>";
	echo"		<th align='center'><span class='style1'>JV Ref.</span></th>";
	echo"		<th align='center'><span class='style1'>Client</span></th>";
	echo"		<th align='center'>&nbsp;</td>";
	echo"		<th align='center'><span class='style1'>Curr.</span></th>";
	echo"		<th align='center'><span class='style1'>Total</span></th>";
	echo"		<th align='center'><span class='style1'>Discount</span></th>";
	echo"		<th align='center'><span class='style1'>Vat</span></th>";
	echo"		<th align='center'><span class='style1'>Net Total</span></th>";
	echo"	</tr>";		


	$Query = " Select SalesJV.SalesJV_ID, SalesJV.SalesJV_Reference, Date_Format(SalesJV.SalesJV_Date,'%d/%m/%Y') As SalesJV_Date, ".
             " SalesJV.Client_Code As ClientCode, Clients.Ledger_name As ClientName, SalesJV.Currency_code, ".
			 //" Project.Project_Name As ProjectName, Costcent.Costcent_name As CostcentName, Salesmen.Ledger_Name As SalesmanName, ".
			 " Round(ifNull(SalesJV.SalesJV_Total,0), ".$DecimalBase."), Round(IfNull(SalesJV.SalesJV_DiscountAmount,0), ".$DecimalBase."), Round(IfNull(SalesJV.SalesJV_VAT,0), ".$DecimalBase."), ".
			 " Round((ifNull(SalesJV.SalesJV_Total,0) + IfNull(SalesJV.SalesJV_VAT,0) - IfNull(SalesJV.SalesJV_DiscountAmount,0)), ".$DecimalBase.") As SalesJV_NetTotal,  ".
			 " Journal.Journal_folio, Journal.Journal_refgovernment ".
			 " From SalesJV ".
			 " Left Join Clients on SalesJV.Client_Code = Clients.Ledger_Number ".
			 " Left Join Journal on SalesJV.Journal_ID = Journal.Journal_ID ".
			 " Where ifNull(SalesJV.Client_Code,-1) between ifNull('".$fromledger."', ifNull(SalesJV.Client_Code,-1)) and ifNull('".$toledger."', ifNull(SalesJV.Client_Code,-1)) ".
			 " And SalesJV.Currency_code = ifNull('".$invoicecurr."', SalesJV.Currency_code) ".
			 " And Date_Format(SalesJV.SalesJV_Date,'%Y%m%d') = Date_Format(ifNull('".insertMySQLDate($invoicedate)."',SalesJV.SalesJV_Date),'%Y%m%d') ";
//echo $Query;
	$ct = 1;
	$result=mysql_query($Query,$connection) or die(mysql_error());
	while($rows=mysql_fetch_row($result)){
		$ct+=1;

		if($ct%2==0)
			$Color="style='background-color:".$grid_color1."'";
		else			
			$Color="style='background-color:".$grid_color2."'";	
									
		echo "<tr ".$Color." >";
		echo "	<td align='left'><span class='style5'>".$rows[2]."</span></td>";
		echo "	<td align='left'><span class='style5'>".$rows[1]."</span></td>";
		echo "	<td align='left'><span class='style5'>".$rows[11]."</span></td>";
		echo "	<td align='left'><span class='style5'>".$rows[3]."</span></td>";
		echo "	<td align='left'><span class='style5'>".$rows[4]."</span></td>";
		echo "	<td align='center'><span class='style5'>".$rows[5]."</span></td>";
		echo "	<td align='right'><span class='style5'>".number_format($rows[6],$DecimalBase1)."</span></td>";
		echo "	<td align='right'><span class='style5'>".number_format($rows[7],$DecimalBase1)."</span></td>";
		echo "	<td align='right'><span class='style5'>".number_format($rows[8],$DecimalBase2)."</span></td>";
		echo "	<td align='right'><span class='style5'>".number_format($rows[9],$DecimalBase2)."</span></td>";
		echo "</tr>";	
		
	}
	echo "	</table></div></td>";   		
	echo" </tr>";
}		
		


echo"   </table></td></tr>"; 
	

echo"   </table></td></tr>"; 



echo" <tr height='30'><td align='center' id='content-bottom' ></td></tr>";  
echo" </table>  ";//end of last table menu


function explodeword($date)
{

$trim = explode('/',$date);
$trim = "$trim[2]"."-"."$trim[1]"."-"."$trim[0]";
return $trim;
}

echo"  
</form>
</body>
</html>";

?>