<?php
 
function get_nextsequence($SeqField , $ProjCostCent ,$connection)
{
	//mysql_query("START TRANSACTION",$connection);
	$query = "Select ifNull(Module_ReferenceByProject,0) As Module_ReferenceByProject , ifNull(Module_ReferenceByCostcenter,0) As Module_ReferenceByCostcenter From Module ";
	$result= mysql_query($query,$connection) or die( mysql_error());
	while($rowmodule=mysql_fetch_array($result)){
		extract($rowmodule);
	}
	
	$query = "Select Count(*) From Sequence";

	if ($ProjCostCent != '' && $Module_ReferenceByCostcenter == 1)
		$query = $query."  Where Sequence.CostCenter = '".$ProjCostCent."'";
	else if ($ProjCostCent != '' && $Module_ReferenceByProject == 1)
		$query = $query."  Where '".$ProjCostCent."' Like Concat(Sequence.Project,'%') ";	
		
	$result1=mysql_query($query,$connection)	or die( mysql_error());
	$row=mysql_fetch_row($result1);
	$Countt = $row[0];
	 
	 
	if ($Countt==0)
	{
		if ($ProjCostCent != '' && $Module_ReferenceByCostcenter == 1)
			$query = "Insert Into Sequence (".$SeqField.",CostCenter) Values (1,'".$ProjCostCent."')";
		else if ($ProjCostCent != '' && $Module_ReferenceByProject == 1)	
			$query = "Insert Into Sequence (".$SeqField.",Project) Values (1,'".$ProjCostCent."')";
		else	
			$query = "Insert Into Sequence (".$SeqField.") Values (1)";
			
		$result1=mysql_query($query,$connection)	or die( mysql_error());
	}	
	else
	{
		if ($ProjCostCent == '') 
		{
			$query = "Update Sequence Set ".$SeqField."=LAST_INSERT_ID(ifnull(".$SeqField.",0)+1) Where Sequence.CostCenter is null and Sequence.Project is null";
			$result1=mysql_query($query,$connection)	or die( mysql_error());
		}
		else
		{
			if ($Module_ReferenceByCostcenter == 1)
				$query = "Update Sequence Set ".$SeqField."=LAST_INSERT_ID(ifnull(".$SeqField.",0)+1) Where Sequence.CostCenter = '".$ProjCostCent."'";
			else if ($Module_ReferenceByProject == 1)
				$query = "Update Sequence Set ".$SeqField."=LAST_INSERT_ID(ifnull(".$SeqField.",0)+1) Where '".$ProjCostCent."' Like Concat(Sequence.Project,'%')";
			
			$result1=mysql_query($query,$connection)	or die( mysql_error());
		}
		
	}
	
		
	$query = " SELECT LAST_INSERT_ID()";
	$result1=mysql_query($query,$connection)	or die( mysql_error());
	$row=mysql_fetch_row($result1);
	$NextValue = $row[0];
	if ($NextValue =='')
		$NextValue = 1 ;
 	
	//mysql_query("COMMIT",$connection);
	return $NextValue;
}

function get_nextsequence_cashtype($FieldName , $CashTypeCode, $JournalType ,$connection)
{
	//mysql_query("START TRANSACTION",$connection);
 

	$query = " Update CashType Set ".$FieldName."=LAST_INSERT_ID(ifnull(".$FieldName.",0)+1) Where CashType_Code = '".$CashTypeCode."' ".
	         " And Journal_Type = '".$JournalType."'";
	$result= mysql_query($query,$connection)	or die( mysql_error());
	
	
	 $query = " SELECT LAST_INSERT_ID()";
	 $result=mysql_query($query,$connection)	or die( mysql_error());
	 $row=mysql_fetch_row($result);
	 $NextValue = $row[0];
	 if ($NextValue =='')
		$NextValue = 1 ;
		
		//mysql_query("COMMIT",$connection);
		return $NextValue;
}

function get_nextsequence_sordertype($FieldName , $SorderTypeCode,$connection)
{
	//mysql_query("START TRANSACTION",$connection);
 

	$query = " Update SorderType Set ".$FieldName."=LAST_INSERT_ID(ifnull(".$FieldName.",0)+1) Where Sorder_Type = '".$SorderTypeCode."'";
	$result= mysql_query($query,$connection)	or die( mysql_error());
	
	
	 $query = " SELECT LAST_INSERT_ID()";
	 $result=mysql_query($query,$connection)	or die( mysql_error());
	 $row=mysql_fetch_row($result);
	 $NextValue = $row[0];
	 if ($NextValue =='')
		$NextValue = 1 ;
		
		//mysql_query("COMMIT",$connection);
		return $NextValue;
}

function get_nextfolio($month ,$connection)
{
$query = "Select Count(*) From jfolio where jfolio.journal_date=".$month;
$result=mysql_query($query,$connection)	or die( mysql_error());
$row=mysql_fetch_row($result1);
$Countt = $row[0];

if($Countt=='0') {
	
	$query ="Insert Into jfolio (journal_seq,Journal_Date) Values (1,$month)";

	$result1=mysql_query($query,$connection)	or die( mysql_error());

}
else {
	if(empty($month))
	return -1;

else {
	$query="Update jfolio Set journal_seq=LAST_INSERT_ID(ifnull(journal_seq,0)+1) Where jfolio.Journal_Date =".$month;
	//echo$query;
	$result1=mysql_query($query,$connection)	or die( mysql_error());
	}
}

$query = " SELECT LAST_INSERT_ID()";
 $result1=mysql_query($query,$connection)	or die( mysql_error());
 $row=mysql_fetch_row($result1);
 $NextValue = $row[0];
 if ($NextValue =='')
    $NextValue = 1 ;
 	

	return $NextValue;
}

function get_nextreference($client_groupcode,$connection){

	$query ="Select Count(*) From ledgergrp where ledgergrp.LedgerGrp_Code='".$client_groupcode."'";
	$result=mysql_query($query,$connection);
	$row=mysql_fetch_row($result);
	$Countt = $row[0];
	
	
	if($Countt=='0'){
		
		$insert_ledgergrp="insert into ledgergrp(LedgerGrp_Code,LedgerGrp_reference)
							values('".$client_groupcode."','00001')";
		echo$insert_ledgergrp;
		$result1=mysql_query($insert_ledgergrp,$connection);
		
	}
	else{
	
		$query="Update ledgergrp Set LedgerGrp_reference=LAST_INSERT_ID(ifnull(LedgerGrp_reference,0)+1) Where ledgergrp.LedgerGrp_Code =".$client_groupcode;
		//echo$query;
		$result1=mysql_query($query,$connection);
		
	}
	$query = " SELECT LAST_INSERT_ID()";
	$result1=mysql_query($query,$connection);
	$row=mysql_fetch_row($result1);
	$NextValue = $row[0];
	
	if ($NextValue =='')
		$NextValue = 1 ;
 

	return $NextValue;
}

function GetSQLValueString($theValue, $theType) 
{
  $theValue = trim($theValue);	
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "Null";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "Null";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "Null";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "Null";
      break;
  }
  return $theValue;
}


function export_data($connection,$num,$query,$ourFileName,$ourFileHandle,$count){

//echo$query;

$resultdata=mysql_query($query,$connection) or die(mysql_error());

$numrow=mysql_num_rows($resultdata);

if($numrow=='0')
return 5;


else{

$countrow=1;
while($rows=mysql_fetch_array($resultdata)){




//echo$count."<br>";

if(($count=='3') && ($countrow=='3')){
//echo$countrow;
break;

}

for($i=0;$i<$num;$i=$i+3){


$stringData=$rows[$i];
//echo$stringData;
$c=strlen($stringData);
//echo$c;

if($c=='0'){

$t=$i+2;
$type=$rows[$t];

if($type=='3' or $type=='1' or $type=='2'){
$l=$i+1;
$len=$rows[$l];
for($j=1;$j<=$len;$j++){



fwrite($ourFileHandle,'0');
}
}

else{
$l=$i+1;
$len=$rows[$l];
for($j=1;$j<=$len;$j++){


fwrite($ourFileHandle,$stringData);
fwrite($ourFileHandle,' ');
}

}

}

else{

$l=$i+1;
$len=$rows[$l];
//echo$len;

$t=$i+2;
$type=$rows[$t];



if($type=='0'){
fwrite($ourFileHandle,$stringData);

for($j=$c;$j<$len;$j++){

fwrite($ourFileHandle,' ');

}

}

if($type=='1'){


for($j=$c;$j<$len;$j++){

fwrite($ourFileHandle,'0');

}
fwrite($ourFileHandle,$stringData);
}

else if($type=='2'){

$rows[i]=number_format($rows[i], 2, '.', '');
//echo$rows[i]."<br>";

$rows[i]=explode('.',$rows[i]);


$c=strlen($rows[i]);




for($j=$c;$j<$len;$j++){

fwrite($ourFileHandle,' ');

}
fwrite($ourFileHandle,$stringData);
}

else if($type=='3'){
fwrite($ourFileHandle,$stringData);
for($j=$c;$j<$len;$j++){

fwrite($ourFileHandle,'0');

}
}
}}

$countrow=$countrow+1;
}
}
}


///// function for all data
function export_alldata($gci,$date1,$date2,$date3,$ourFileHandle){

$gcilen=strlen($gci);


$max=7-$gcilen;
for($i=1;$i<=$max;$i++){
fwrite($ourFileHandle,'0');
}
fwrite($ourFileHandle,$gci);


$date1len=strlen($date1);
if($date1len=='0'){

for($i=1;$i<=4;$i++){
fwrite($ourFileHandle,'0');
}
}

else
fwrite($ourFileHandle,$date1);

$date1len=strlen($date2);
if($date1len=='0'){

for($i=1;$i<=2;$i++){
fwrite($ourFileHandle,'0');
}
}

else
fwrite($ourFileHandle,$date2);


$date1len=strlen($date3);
if($date1len=='0'){

for($i=1;$i<=2;$i++){
fwrite($ourFileHandle,'0');
}
}

else
fwrite($ourFileHandle,$date3);


}

if (!function_exists('explodeword')) {
	function explodeword($date)
	{

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

		return $trim;
	}
}
	
//Return Date in YYYYMMDD format. Supplied Format: DD/MM/YYYY
function formatDateNoTime($data){
	return substr($data, 6, 4) . substr($data, 3, 2) . substr($data, 0, 2);
}

function ShowReportHeader($title,$PageLayout, $connection) {
	
	//Read From Setup Table							
	$querysetup= " select Company_Name as CompanyName, Company_Memo as CompanyMemo, Company_City as CompanyWebsite, Company_Country as CompanyEmail,  ".
				 " ifNull(Setup_LogoWidth, 80) as SetupLogoWidth, ifNull(Setup_LogoHeight, 50) as SetupLogoHeight, ".
				  " ifNull(show_companylogo,0) as show_companylogo, ifNull(show_companyname,0) as show_companyname, ".
				  " ifNull(show_printdatetime,0) as show_printdatetime, ifNull(show_user,0) as show_user, ".
				  " ifNull(show_companymemo, 0) as show_companymemo ".
					" From Setup ";
					
	$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
	while($rowsetup=mysql_fetch_array($resultsetup)){
		extract($rowsetup);
	}
	
	$now=current_server_time($connection);
	$dt=split(" ",$now[0]);
	$date = split("-",$dt[0]);
	$printdate = $date[2]."/".$date[1]."/".$date[0];
	$printtime = $dt[1]." ".$dt[2]. " ".$dt[3];

	$LogoStyle = "style='width:".$SetupLogoWidth."px; height:".$SetupLogoHeight."px;'";
	
	if($PageLayout=='landscape') { $tablewidth='1100px';}
	if($PageLayout=='portrait') { $tablewidth='650px';}
	
	echo "<table border='0' cellspacing=0 cellpadding=0  style='table-layout:fixed;' width='$tablewidth'>";
		
	if($PageLayout=='landscape') { 
		echo"<col width=130>
		  <col width=330>
		  <col width=270>
		  <col width=200>
		  <col width=170>";
    }
	if($PageLayout=='portrait') { 
		echo"<col width=120>
		  <col width=130>
		  <col width=170>
		  <col width=100>
		  <col width=130>";
	}	
	
	echo"<tr height='25px'>";
	
	if($show_companylogo==1)
		echo"	<td align='center' rowspan='3'><img src='LogoFile.php' $LogoStyle /></td>";
	else 	echo"	<td align='center' rowspan='3'></td>";
	
	if($show_companyname==1)	
		echo"	<td align='left' colspan='2'><span style='font-size:14px; font-weight:bold; font-style:italic;'>".$CompanyName."</span></td>";
	else echo"	<td align='left' colspan='2'><span style='font-size:14px; font-weight:bold; font-style:italic;'></span></td>";
	
	
		echo"	<td align='center' rowspan='3' colspan='2' valign='top'><Table  cellspacing=0 cellpadding=0 style='border:1px solid #000000;'>";
		
		if($show_printdatetime==1){
			echo"<tr height='25px'><td style='border-bottom:1px solid #000000;' valign='middle'><span style='font-size:11px; font-weight:bold;font-family:arial;'>&nbsp; Printed Date: &nbsp;</span></td>";
			echo"	<td align='left' style='border-bottom:1px solid #000000;' valign='middle'><span style='font-size:12px;font-family:arial;'>$now[1] ".$printdate." </span>&nbsp;</td></tr>
		
			<tr height='25px'><td align='right'  style='border-bottom:1px solid #000000;' valign='middle'><span style='font-size:11px; font-weight:bold;font-family:arial;'>Time: &nbsp;</span></td>
			<td align='left'  style='border-bottom:1px solid #000000;' valign='middle'><span style='font-size:12px;font-family:arial;'>".$printtime."</span></td></tr>";
		}	
		if($show_user==1){
		
			echo"<tr height='25px'><td align='right' valign='middle' ><span style='font-size:11px; font-weight:bold;font-family:arial;'>User: &nbsp;</span></td>
			<td align='left' valign='middle' ><span style='font-size:12px;font-family:arial;'>".$_SESSION["useraccount"]." - ".$_SESSION["username"]."</span></td></tr>";
		}	
		echo"</table></td>";
	echo"</tr>";

	if($show_companymemo==1){	
		echo"<tr height='10'>";
		echo"	<td align='left' rowspan='2' colspan='2'><span style='font-size:10px;'>".nl2br($CompanyMemo)."</span></td>";	
		echo"</tr>";
	}
	else {
		echo"<tr height='10'>";
		echo"	<td align='left' rowspan='2' colspan='2'><span style='font-size:10px;'></span></td>";	
		echo"</tr>";
	}
	
	echo"<tr height='25'>";
	//echo"	<td align='right'  valign='top'><span style='font-size:14px; font-weight:bold;'>User : &nbsp;</span></td>";
	//echo"	<td align='left' valign='top'><span style='font-size:14px;'>".$_SESSION["useraccount"]." - ".$_SESSION["username"]."</span></td>";
	echo"</tr>";
	
	echo"<tr height='25'>";
	echo"	<td align='center' colspan='5'><span style='font-size:18px; font-weight:bold;'>".$title."</span></td>";
	echo"</tr>";
	
	echo "<tr height='5px'><td colspan='5'><h4></td></tr>";
	echo"</table>";


}

function RoundTotal($Amount, $Divisor, $connection) {
	$RAmount = 0;
	
	$Remain = round(fmod(abs($Amount),$Divisor),2); 
	$RAmount = abs($Amount) - $Remain;
	$Remain = round(abs($Remain)); 
	
	$sub_query = "Select CashRegisterRounding_Value From CashRegisterRounding Where ".$Remain." Between CashRegisterRounding_RangeFrom and CashRegisterRounding_RangeTo ";
	$sub_result = selectData($connection, $sub_query);
	$Round = $sub_result[0];
	//echo $sub_query;
	if (!empty($Round) || $Round == 0) { 
		$RAmount = $RAmount + $Round;
		if ($Amount < 0) $RAmount = (-1)*($RAmount);
	} else {
		$RAmount = $Amount;
	}
	
	return $RAmount;
}



?>