<?php

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


include("config.inc.php");
include("function.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($_SESSION['dbName'],$connection) or die("Could not find DB");

$querylog=" select ifNull(FA_SalesJVDel,0) As FA_SalesJVDel, ifNull(FA_SalesJVMod,0) As FA_SalesJVMod, ".
          " ifNull(login_belongstogrp,0) As FI_BelongsToGrp, ifNull(ShowTransaction,0) As ShowTransaction ".
          " from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')  ";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}


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


//Access Mode On Insert/Edit/Delete
$DelMode  =1;
$EditMode =1;
$ConfMode =1;	
$PrevMode =1;

$PrevHint   = '';
$EditHint   = '';
$DelHint    = '';



$querysetup="select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2 from setup";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
extract($rowsetup);
}

$MainCompanyName=explode('_',$_SESSION['dbName']);

$queryperiod= " Select ifNull(LockViewPeriod,0) As LockViewPeriod, ViewPeriodDate, ifNull(ViewLockDaily,0) As ViewLockDaily, ViewNumberOfDays ".
              " From ".$_SESSION['dbLabel'].".ListUserDir  ".
			  " Where Usercode='".$_SESSION['useraccount']."' ".
			  " And Upper(CompanyName) = Upper('".$MainCompanyName[0]."') ".
			  " And Upper(Diryear) = Upper('".$MainCompanyName[1]."') ";
			  
$resultperiod = mysql_query($queryperiod,$connection) or die(mysql_error());
$rowsperiod   = mysql_num_rows($resultperiod);

while($rows=mysql_fetch_array($resultperiod)){
	extract($rows);
}	
			  
//echo $queryperiod;	

$queryproject=" select distinct project.project_code, project_name ".
              " from project left join ".$_SESSION['dbLabel'].".loginstk on (($Module_ProjectParent=1 and project.project_code like concat(ifnull(loginstk.project_code,project.project_code),'%')) or ($Module_ProjectParent=0 and project.project_code = loginstk.project_code )) ".
			  " where user_account='".$_SESSION['useraccount']."' ".
			  " and (loginstk.project_code is not null and loginstk.project_code != '') ".
			  " order by project.project_code";
$projectresult = mysql_query($queryproject,$connection) or die(mysql_error());
$projectexist  = mysql_num_rows($projectresult);		  

$querycostcent=" select distinct costcent.costcent_code, costcent_name ".
               " from costcent left join ".$_SESSION['dbLabel'].".loginstk on (($Module_CostCenterParent=1 and costcent.costcent_code like concat(ifnull(loginstk.costcent_code,costcent.costcent_code),'%')) or ($Module_CostCenterParent=0 and costcent.costcent_code = loginstk.costcent_code )) ".
			   " where user_account='".$_SESSION['useraccount']."' ".
			   " and (loginstk.costcent_code is not null and loginstk.costcent_code != '') ".
			   " order by costcent.costcent_code";
$costcentresult = mysql_query($querycostcent,$connection) or die(mysql_error());
$costcentexist  = mysql_num_rows($costcentresult);		  

$queryledger= " select distinct ledger_number, ledger_include ".
              " from ".$_SESSION['dbLabel'].".ledgerstk  ".
			  " where user_account='".$_SESSION['useraccount']."' ".
			  " and (ledger_number is not null and ledger_number != '') ".
			  " order by ledger_number";
$ledgerresult = mysql_query($queryledger,$connection) or die(mysql_error());
$ledgerexist  = mysql_num_rows($ledgerresult);	
$rows=mysql_fetch_array($ledgerresult) ;
$ledgerinclude = $rows[1];

//Get Default Project
$querydflproject="select project_code as defaultproject from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."' and default_project=1 ";
$resultdflproject=mysql_query($querydflproject,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultdflproject)){
	extract($rows);
}
	
//Get Default Coscenter
$querydflcostcent="select costcent_code as defaultcostcent from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."' and default_costcenter=1 ";
$resultdflcostcent=mysql_query($querydflcostcent,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultdflcostcent)){
	extract($rows);
}

if ((!isset($PProjectCode)) && $Module_Project == 1)
	$PProjectCode = $defaultproject;
if ((!isset($PCostCentCode)) && $Module_CostCenter == 1)	
	$PCostCentCode = $defaultcostcent;	

if (empty($orderby)) $orderby = 1;
if (empty($searchby)) $searchby = 2;

if ($startrange=='')
	$startrange = 0;
if ($perpage=='')
	$perpage = 20;

print <<<HERE
<head>

<meta http-equiv="Content-Type" content="text/html; charset=windows-1256" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" media="all" href="css/style.css">
<script type="text/javascript" src="js/grid/jquery-1.9.1.js"></script>
<title>Post SalesJV</title>
<script type="text/javascript" language="javascript">

function CloseForm()
{
  self.close();
}


function SetNextPage(startrange,perpage){ 
	
	document.search.action='Form_SalesJVPost.php?keyword=&startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}

function SetPrvPage(startrange,perpage){
	
	document.search.action='Form_SalesJVPost.php?keyword=&startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}

function SubmitForm()
{ 
	var connct = document.search.sqlsrvrcon.value;
	if(connct == '') {
		alert("Connection could not be established!");
		return 0;
	} else {		
	   document.search.action='Form_SalesJV_Actions.php?mainmode=postsalesjv';
	   document.search.submit();
	}
}

function RefreshPage()
{
  
  //document.search.keyword.value='';
  document.search.action='Form_SalesJVPost.php?startrange=0';
  document.search.submit();

}

function SetFirstPage(startrange,perpage){
	document.search.action='Form_SalesJVPost.php?startrange=0&perpage='+perpage;
	document.search.submit();
}

function SetLastPage(perpage,totalrows){
	var result=totalrows % perpage;
	if (result == 0) result = perpage;
	startrange=totalrows-result;
	document.search.action='Form_SalesJVPost.php?startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}
</script>

<style type="text/css">

body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
body {
	margin-top: 0px;
	margin-left: 1px;
}
a:link {
	text-decoration: none;
	color: #0000FF;
}
a:visited {
	text-decoration: none;
	color: #0000FF;
}
a:hover {
	text-decoration: none;
	color: #0000FF;
}
a:active {
	text-decoration: none;
	color: #0000FF;
}

.style12 {
	font-size: 9px;
	color: #999999;
	font-weight: bold;
}

.style16 {
	font-size: 16px;
	color: #FF0000;
	font-weight: bold;
	font-family: "Times New Roman", Times, serif;
}
.style17 {font-size: 9px}

.style26 {color: #FF0000}
.style27 {color: #999999 ;font-size: 10px;}
.style28 {color: #000000 ;font-size: 10px;}

</style>

</head>
<body onLoad='LoadForm();' class='bodybackground'>
<form name='search' method='post' action="">
HERE;


$serverName = "93.186.115.223,1433";
$connectionInfo = array("Database"=>"PerpaxTravelTrainingBase", "UID"=>"utp", "PWD"=>"ptp");
$conn = sqlsrv_connect($serverName, $connectionInfo);


echo "<input type='hidden' name='sqlsrvrcon' id='sqlsrvrcon' value='$conn' >";
		
$querysearch="select PerPaxSyncDataItemID,PartnerSalesChannel,InvoiceNumber,InvoiceDtTm,CreatedDtTm,Service,BillingCustomerName,PassengerName,Total,SupplierCurrency, Route, TravelDtTm, 
			CreatedBy,TRNumber from tabPerPaxSyncDataItems   ";
		 
//echo $querysearch;			 
//$resultsearch=sqlsrv_query($conn,$querysearch);
//$resultsearch=sqlsrv_query($conn,$querysearch, array(), array( "Scrollable" => 'static' ))  or die(sqlsrv_errors());
//$totalrows = sqlsrv_num_rows($resultsearch);
	//var_dump($totalrows);
	//	echo $totalrows."ghgh";
	
//$querysearch = $querysearch." limit ".$startrange.",".$perpage;


echo"<table cellpadding='0' cellspacing='0' border='0'  align='left' style='table-layout:fixed' width='1360px'>";
echo"<tr>
		<td id='search-top1' style='height:34px; width:31px;'>&nbsp;</td>
	  	<td id='search-top2' style='height:34px;' align='center' valign='middle'>
			<table border='0'>
			<tr><td><span class='style3'>List of SalesJV</span></td></tr>
			</table>
		</td>
		<td id='search-top3' style='height:34px; width:31px;'>&nbsp;</td>
	</tr>";

echo"<tr><td id='search-content1' style='width:31px;'>&nbsp;</td>
		 <td id='search-content2' >";
echo"<table border='0' cellspacing='1' style='table-layout:fixed'>
	   <col width=20>
	  <col width=20>
	  <col width=20>
	  <col width=65>
	  <col width=85>
	  <col width=65>
	  <col width=50>
	  <col width=75>
	  <col width=150>
	  <col width=95>
	  <col width=90>
	  <col width=100>
	  <col width=55>
	  <col width=90>
	
	  <tr class='Search-header-bg'>";
	  
echo" <td align='left' colspan='6'></td>";
		
echo" <td align='left' colspan='4'>";
echo"<button type='button' id='btnSubmit' name='btnSubmit' onclick='SubmitForm(); return false;' title='Post'><span><span>Post</span></span></button>
	<button type='button' id='btnCancel' name='btnCancel' onclick='CloseForm(); return false;' title='Close'><span><span>Close</span></span></button>";
echo "</td>";

	  

echo"<td align='left' colspan='4' ></td>";
echo" </tr>";	
			 
echo" </table>
		</td>
		<td id='search-content3' style='width:31px;'>&nbsp;</td>"; 
echo" </tr>";

echo"<tr>
		<td  id='search-content1' style='height:34px;width:31px;'>&nbsp;</td>
		<td  id='search-content2' style='height:34px;width:1025px;'>
		<table width='100%' border=0>
		<tr>
			<td align='right' style='width:380px;'><span class='style4'>Per Page </span><span class='style4'><select name='perpage' id='perpage' value='$perpage' onchange='RefreshPage();'>";
              
			if ($perpage==20)
				$s20 = 'selected';
			else if ($perpage==30)
				$s30 = 'selected';
			else if ($perpage==40)
				$s40 = 'selected';
			else if ($perpage==50)
				$s50 = 'selected';

						
							
			echo'<option '.$s20.' value="20">20</option>
				 <option '.$s30.' value="30">30</option>
				 <option '.$s40.' value="40">40</option>
				 <option '.$s50.' value="50">50</option>
				 </select></span></td>
			<td  style="width:74px;border-right:1px solid #000000;text-align:right;">';
			
			if($startrange > 0)
				echo"<img src='img/firstpage.png' title='First Page' onMouseOver=\"style.cursor='hand'\" onclick=SetFirstPage('".$startrange."','".$perpage."')>";

			$diff= $startrange - $perpage;
				
			if (($diff) >= 0 ){
				$prvstartrange = $startrange - $perpage;
				echo"&nbsp;&nbsp;<img src='img/arrowleft.png' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."')>";
			}
			else
				echo "&nbsp;";
		
echo"		</td><td  align='left' style='width:74px;'>";
		
		
			if ($startrange <= $totalrows){
				$startrange1 = $startrange + $perpage;
				if  ($startrange1 < $totalrows){
					echo"<img src='img/arrowright.png' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('".$startrange1."','".$perpage."')>";
					echo"&nbsp;&nbsp;<img src='img/lastpage.png' title='Last Page' onMouseOver=\"style.cursor='hand'\" onclick=SetLastPage('".$perpage."','".$totalrows."')>";	
				}
			} else
				echo "&nbsp;";
				
			
echo"		</td><td style='width:380px;'>&nbsp;</td>
		</tr>";
echo"	</table></td>
		<td id='search-content3' style='width:31px;'>&nbsp;</td>";
echo"</tr>";		
	


	  
	 echo"<tr><td id='search-content1' style='width:31px;'>&nbsp;</td>
		<td id='search-content2'>
			<div style='overflow:scroll;height:400px;width:1300px;'>
			<table border='0'  cellpadding='0' cellspacing='1' width='1300px' >
			  <col width=100>
			  <col width=110>
			  <col width=120>
			  <col width=200>
			  <col width=200>
			  <col width=120>
			  <col width=300>";
			  
		if($message!=''){
			echo"<tr class='Search-header-bg'>
					<td height='15' align='center' colspan='15' class='searchmessage'>".$message."</td>
				</tr>";
		}		
	 echo" 
	  <tr class='Search-Label-bg'>
		<td  height='25' align='center'><span class='style4'>Invoice Number</span></td>
		<td  height='25' align='center'><span class='style4'>Sales Channel</span></td>
		<td  height='25' align='center'><span class='style4'>Service</span></td>
		<td  height='25' align='center'><span class='style4'>Customer Name</span></td>
		<td  height='25' align='left'><span class='style4'>Passenger Name </span></td>
		<td  height='25' align='center'><span class='style4'>TRN Number </span></td>
		<td  height='25' align='center'><span class='style4'>Remark</span></td>";
	echo"</tr>";

$resultsearch=sqlsrv_query($conn,$querysearch) or die(sqlsrv_errors());

$i = 0;
while($rows=sqlsrv_fetch_array($resultsearch)){
	extract($rows);
	
	$manualnbr = $InvoiceNumber."-".$DocketNo;
	$query_check = "select 1 from salesjv where SalesJv_ManualNo = '$manualnbr'";
	$res_check 	 = mysql_query($query_check,$connection);
	$numcheck 	 = mysql_num_rows($res_check);
  if($numcheck == 0){		
	
	$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">'.$InvoiceNumber.'</span></td>
		 <td align="center"><span class="style5">'.$PartnerSalesChannel.'</span></td>';
			
	echo'	<td align="left"><span class="style5">'.$Service.'</span></td>
			<td align="left"><span class="style5">'.$BillingCustomerName.'</span></td>
			<td align="left"><span class="style5">'.$PassengerName.'</span></td>
			<td align="left"><span class="style5">'.$TRNumber.'</span></td>';
	$remarks = '';		
	$temp = explode("--",$TRNumber);
	$query_prjct = "select 1 from projects where project_code = '$temp[0]'";
	$res_prjct   = mysql_query($query_prjct,$connection);
	$num_prjct	 = mysql_num_rows($res_prjct);
	if($num_prjct == 0) $remarks = $remarks." $temp[0] does not exists in Projects.<br> ";
	
	$query_cst = "select 1 from costcent where Costcent_name = '$PartnerSalesChannel'";
	$res_cst   = mysql_query($query_cst,$connection);
	$num_cst	 = mysql_num_rows($res_cst);
	if($num_cst == 0) $remarks = $remarks." $PartnerSalesChannel does not exists in Coscenter.<br> ";
	
	$query_client = "select 1 from clients where Ledger_name = '$BillingCustomerName'";
	$res_client   = mysql_query($query_client,$connection);
	$num_client	 = mysql_num_rows($res_client);
	if($num_client == 0) $remarks = $remarks." $BillingCustomerName does not exists in Clients.<br> ";
	
	$query_salesmen = "select 1 from salesmen where Ledger_name = '$CreatedBy'";
	$res_salesmen   = mysql_query($query_salesmen,$connection);
	$num_salesmen	 = mysql_num_rows($num_salesmen);
	if($num_salesmen == 0) $remarks = $remarks." $CreatedBy does not exists in Salesmen.<br> ";
	
	echo"<td align='center'><span class='style5'>$remarks</span></td>";
	echo'</tr>';
  }
		
}
							
echo"</table></div></td><td id='search-content3' style='width:31px;'>&nbsp;</td></tr>";		
			
	
echo"   <tr ><td id='search-content1'>&nbsp;</td><td id='search-content2'>&nbsp;</td><td id='search-content3'>&nbsp;</td></tr>";
echo"	<tr >
		<td id='search-bottom1' style='height:34px; width:31px;'>&nbsp;</td>
		<td id='search-bottom2' style='height:34px; ' valign='top'><table width='100%' border=0 height=28px>
		<tr><td style='width:380px;'>&nbsp;</td><td align='right' style='width:74px;border-right:1px solid #000000;'>";

		
		if($startrange>0)
			echo"<img src='img/firstpage.png' title='First Page' onMouseOver=\"style.cursor='hand'\" onclick=SetFirstPage('".$startrange."','".$perpage."')>";

		//$diff= $startrange - $perpage;
		
		if (($diff) >= 0 ){
			//$prvstartrange = $startrange - $perpage;
			echo"&nbsp;&nbsp;<img src='img/arrowleft.png' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."')>";
		}
		else
			echo "&nbsp;";
		
		echo"</td><td  align='left' style='width:74px;'>";
		
		
		if ($startrange <= $totalrows){
			//$startrange = $startrange + $perpage;
			if  ($startrange1 < $totalrows){
				echo"<img src='img/arrowright.png' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('".$startrange1."','".$perpage."')>";
				echo"&nbsp;&nbsp;<img src='img/lastpage.png' title='Last Page' onMouseOver=\"style.cursor='hand'\" onclick=SetLastPage('".$perpage."','".$totalrows."')>";	
			}
		} else
			echo "&nbsp;";
	
			
		echo"</td><td style='width:380px;'>&nbsp;</td></tr>";
		echo"</table></td><td id='search-bottom3' style='height:34px; width:31px;'>&nbsp;</td>";

	 
echo"</tr>";

echo"</table>";

print <<<HERE

</form>
</body>

HERE;

?>