<?php include("../../include/dbconnect.php");

?>

<script language="javascript">
function OpenWindow(url)
{
	window.open(url,'ledg','width=900,height=400,scrollbars=yes,resizable=0');
}
</script>
<STYLE TYPE='text/css'>

	a {text-decoration:none}
	a { color: black; }
P.pagebreakhere {page-break-after: always}
</STYLE>

<title>Trial Balance Printing</title>
<form>


<font face="Arial, Helvetica, sans-serif">
<?


$delfile = "../../../../tmp/invoice.txt";

if(file_exists($delfile))
 {
  unlink($delfile);
 }

if(!($myfile=fopen("../../../../tmp/invoice.txt","w")))
{
  print("file couldnot be opened");
}


$fname = 'print.txt';

$totcr=0;
$totdr=0;
$enddt = actdate($enddt);
$enddt = us($enddt);
date_belong($enddt,$account_year);

$query = "delete from pledger";
mysqli_query($conn,$query) or die (mysqli_error());

$linecnt=0;
$pgbrk=5;
$mcnt=1;

$result=mysqli_query($conn,"select distinct name,address1,address2,city,state from company where compcode = '$compcode' ") or die(mysqli_error());
$row = mysqli_fetch_row($result);
$compname = $row[0];
$address = $row[1].'&nbsp;'.$row[2].'&nbsp;'.$row[3].'&nbsp;'.$row[4];

$result=mysqli_query($conn,"select distinct name from branches where code = '$branch' ") or die(mysqli_error());
$row = mysqli_fetch_row($result);
$branchname = $row[0];

if($branch == 'ALL')
{
	$branchname = 'CONSOLIDATED TRIAL BALANCE, ALL PROFIT CENTRES';
}
?>
</font>



<table  width="914" border="0" align="center" cellpadding="0" cellspacing="0"    style="padding:5px; border:1px solid #666666">

    <tr>
      <td height="80" colspan="4"  align="center" bgcolor="#095F92">
      <div style="padding:10px;"><img src="../../images/hapbco_logo.png" alt="logo" width="82" height="87" style="float:left"/></div>
      <span style="font-family:Arial, Helvetica, sans-serif; font-size:20px; color:#FFFFFF; font-weight:bold;" > <?=$compname?> </span><br />
          <span style="font-family:Arial, Helvetica, sans-serif; font-size:16px; color:#FFFFFF; font-weight:bold;" >ADMIN OFFICE: <?=$address?><br />
</td>
    </tr>

    <tr>
      <td height="30" colspan="4"  align="center" bgcolor="#EAEAEA"><span style="font-family:Arial, Helvetica, sans-serif; font-size:16px; color:#000; font-weight:bold;" >Trial Balance as on <?=british($enddt);?></span></td>
    </tr>
    <tr>
      <td height="30" colspan="4"  align="center"  >
      
      <table  width="100%" border="0" cellpadding="4"  rules="cols" >
        <colgroup>
          </colgroup>
        <colgroup>
          </colgroup>
        <colgroup>
          </colgroup>
            <thead>
          <tr bgcolor="#095F92">
            <td width="431" bgcolor="#095F92" ><div align="center"><strong><span style="font-family:Arial, Helvetica, sans-serif; font-size:16px;color:#FFFFFF;  font-weight:bold;" >Particulars</span></font></div></td>
			<?
			if($opdiff == 'Yes')
			{
			?>

            <td width="157"><div align="right" ><strong><span style="font-family:Arial, Helvetica, sans-serif; font-size:16px;color:#FFFFFF;  font-weight:bold;" >Opbal Debit</span></font></div></td>
            <td width="157"><div align="right" ><strong><span style="font-family:Arial, Helvetica, sans-serif; font-size:16px;color:#FFFFFF;  font-weight:bold;" >Opbal Credit</span></font></div></td>
			<?
			}
			?> 


            <td width="157"><div align="right" ><strong><span style="font-family:Arial, Helvetica, sans-serif; font-size:16px;color:#FFFFFF;  font-weight:bold;" >Debit</span></font></div></td>
            <td width="140"><div align="right" ><strong><span style="font-family:Arial, Helvetica, sans-serif; font-size:16px; color:#FFFFFF; font-weight:bold;" >Credit</span></font></strong></div></td>
          </tr>
         
      
<tr> 
<td><hr></td>
<td><hr></td>
<td><hr></td>
	<?
    if($opdiff == 'Yes')
    {
    ?>
        <td><hr></td>
        <td><hr></td>            
    <?
    }
    ?>
</tr>  
</thead>
<?
$debittotal = 0;
$credittotal=0;

$gresult = mysqli_query($conn,"select acctcode,groupname from acctgroup  order by sequence ") or die(mysqli_error());
while($grow = mysqli_fetch_row($gresult))
{

$gchk = 'T';
$tmpgroup = '';
$aresult = mysqli_query($conn,"select acctcode,description,subgroup from acctmast where acctgroup= '$grow[0]' order by subgroup,description ") or die(mysqli_error());

if($branch=='ALL')
{	
	$aresult = mysqli_query($conn,"select acctcode,description,subgroup from acctmast where acctgroup = '$grow[0]'   order by subgroup,description") or die(mysqli_error());
}
else
{
	$aresult = mysqli_query($conn,"select distinct ledger.acctcode,acctmast.description,acctmast.subgroup from acctmast,ledger where acctgroup= '$grow[0]' and ledger.acctcode = acctmast.acctcode and ledger.branch_code = '$branch'   union select distinct openbalance.acctcode,acctmast.description,acctmast.subgroup from acctmast,openbalance where acctgroup= '$grow[0]' and openbalance.acctcode = acctmast.acctcode and openbalance.branch_code = '$branch'  order by subgroup,description") or die(mysqli_error());
}

while($arow = mysqli_fetch_row($aresult))
{



		if($branch=='ALL')
		{
			$opresult = mysqli_query($conn,"select opening_balance,db_cr from openbalance where acctcode = $arow[0] and  compcode = '$compcode' and account_year = '$account_year' ") or die(mysqli_error());
			while($oprow = mysqli_fetch_row($opresult))
			{
			//$amt = $oprow[0];
				if($oprow[1] == 'DR')
				{
					$amt = $amt+$oprow[0];
					$opening_debit = $opening_debit +$oprow[0];					
				}
				elseif($oprow[1] == 'CR')
				{
					$amt = $amt+$oprow[0]*-1;
					$opening_credit = $opening_credit +$oprow[0];					
				}	
			}		
		}
		else
		{
			$opresult = mysqli_query($conn,"select opening_balance,db_cr from openbalance where acctcode = $arow[0] and branch_code = '$branch' and compcode = '$compcode'  and account_year = '$account_year' ") or die(mysqli_error());
			while($oprow = mysqli_fetch_row($opresult))
			{
			//$amt = $oprow[0];
				if($oprow[1] == 'DR')
				{
					$amt = $amt+$oprow[0];
					$opening_debit = $opening_debit +$oprow[0];								
				}
				elseif($oprow[1] == 'CR')
				{
					$amt = $amt+$oprow[0]*-1;
					$opening_credit = $opening_credit +$oprow[0];						
				}	
			}					
		
		}

		$opening_debit_total = $opening_debit_total + $opening_debit;
		$opening_credit_total = $opening_credit_total + $opening_credit;






		
		
		if($branch=='ALL')
		{
			$ledresult = mysqli_query($conn,"select sum(dbamt)-sum(cramt) from ledger where acctcode = $arow[0] and date <= '$enddt' and compcode = '$compcode'  and account_year = '$account_year' ") or die(mysqli_error());
		}
		else
		{
			$ledresult = mysqli_query($conn,"select sum(dbamt)-sum(cramt) from ledger where acctcode = $arow[0] and date <= '$enddt' and branch_code = '$branch' and compcode = '$compcode'  and account_year = '$account_year' ") or die(mysqli_error());		
		}
	$ledrow = mysqli_fetch_row($ledresult);	
	$ledgamt = $ledrow[0];
	$ledtotal = $amt + $ledgamt;
	//if($ledtotal != 0) MODIFYING DUE TO NON PRINTING OR ZERO BALANCE OPENING ROWS.
	if(($amt != 0) or ($ledgamt !=0)) 
	{
		if($ledtotal > 0)
		{
				$debittotal = $debittotal + $ledtotal;
		}
		elseif($ledtotal < 0)
		{
			$credittotal  = $credittotal+ ($ledtotal*-1);
		}


		if($gchk == 'T')
		{
		?>
		<tr > 
		<td ><span style="font-family:Arial, Helvetica, sans-serif; font-size:18px; color:#544DEA; font-weight:bold;" ><?=ucwords(strtolower($grow[1]))?></span></td><td>&nbsp;</td><td>&nbsp;</td>
        <?
        if($opdiff == 'Yes')
		{
		?>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
		<?
		}
		?>
        </tr>
        <?
		$gchk = 'F';
		
		}   
    
        
		if($tmpgroup != $arow[2] and $arow[2] != '000' )
		{
			$jresult = mysqli_query($conn,"select description from subgroup where subcode = '$arow[2]' and acctgroup ='$grow[0]' ") or die(mysqli_error());
			$jrow = mysqli_fetch_row($jresult);
			$subgroup = $jrow[0];
		?>
		<tr  > 
		<td ><span style="font-family:Arial, Helvetica, sans-serif; font-size:16px; color:#3366CC; font-weight:bold;" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?=$subgroup?></span></td><td>&nbsp;</td><td>&nbsp;</td>
		<?
        if($opdiff == 'Yes')
		{
		?>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
		<?
		}
		?>

        </tr>
        <?
		$tmpgroup = $arow[2];
		
		}   
		?>         
        
        
		<tr> 
        <?
		if($branch!='ALL')
		{
			?>
			<td><span style="font-family:Arial, Helvetica, sans-serif; font-size:16px;  font-weight:bold;" ><a onClick="javascript:OpenWindow('../ledgers/ledg_print.php?acctcode[0]=<?echo($arow[0]);?>&compcode=<?=$compcode?>&branch=<?=$branch?>&account_year=<?=$account_year?>&enddt=<?=british($enddt)?>&prnt=All')" href ="#">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?=ucwords(strtolower($arow[1]))?></span></td>
			<?
		}
		else
		{
			?>
			<td><span style="font-family:Arial, Helvetica, sans-serif; font-size:16px;  font-weight:bold;" ><a onClick="javascript:OpenWindow('../ledgers/allbr_ledg_print.php?acctcode[0]=<?echo($arow[0]);?>&compcode=<?=$compcode?>&branch=<?=$branch?>&account_year=<?=$account_year?>&enddt=<?=british($enddt)?>&prnt=All')" href ="#">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?=ucwords(strtolower($arow[1]))?></span></td>
			<?		
		}	
		if($opdiff == 'Yes')
		{
		?>		

            <td><div align="right"><span style="font-family:Arial, Helvetica, sans-serif; font-size:16px;  font-weight:bold;" ><a onClick="javascript:OpenWindow('opbal_view.php?acctcode=<?echo($arow[0]);?>&compcode=<?=$compcode?>&branch=<?=$branch?>&account_year=<?=$account_year?>')" href ="#"><? if($opening_debit >0) echo(number_format($opening_debit,2,'.',','));?></span></div></td>       
            <td><div align="right"><span style="font-family:Arial, Helvetica, sans-serif; font-size:16px;  font-weight:bold;" ><a onClick="javascript:OpenWindow('opbal_view.php?acctcode=<?echo($arow[0]);?>&compcode=<?=$compcode?>&branch=<?=$branch?>&account_year=<?=$account_year?>')" href ="#"><? if($opening_credit >0) echo(number_format($opening_credit,2,'.',','));?></span></div></td> 
            <td><div align="right"><span style="font-family:Arial, Helvetica, sans-serif; font-size:16px;  font-weight:bold;" ><? if($ledgamt >0) echo(number_format($ledgamt,2));?></span></div></td>       
            <td><div align="right"><span style="font-family:Arial, Helvetica, sans-serif; font-size:16px;  font-weight:bold;" ><? if($ledgamt <0) echo(number_format($ledgamt*-1,2));?></span></div></td></tr> 
			<?
		}
		else
		{
			
			?>
			<td><div align="right"><span style="font-family:Arial, Helvetica, sans-serif; font-size:16px;  font-weight:bold;" ><? if($ledtotal >0) echo(number_format($ledtotal,2,'.',','));?></span></div></td>       
			<td><div align="right"><span style="font-family:Arial, Helvetica, sans-serif; font-size:16px;  font-weight:bold;" ><? if($ledtotal <0) echo(number_format($ledtotal*-1,2,'.',','));?></span></div></td></tr> 
	
			<?
		}
	}		
	$amt=0;
	$opening_debit = 0;
	$opening_credit = 0;
	$ledtotal=0;
}	
}
?>
</tbody>
        <tr > 
        <td ><hr></td>
        <td><hr></td>
        <td><hr></td>
			<?
			if($opdiff == 'Yes')
			{
			?>
                <td><hr></td>
                <td><hr></td>            
			<?
			}
			?>           
        </tr> 

			<?
			if($opdiff == 'Yes')
			{
			?>
                <tr bgcolor="#095F92"> 
                <td><span style="font-family:Arial, Helvetica, sans-serif; font-size:16px; color:#FFFFFF; font-weight:bold;" >Total</span></td>
                <td><div align="right"><span style="font-family:Arial, Helvetica, sans-serif; font-size:16px; color:#000; color:#FFFFFF;font-weight:bold;" ><?=number_format($opening_debit_total,2,'.',',')?></span></div></td>        
                <td><div align="right"><span style="font-family:Arial, Helvetica, sans-serif; font-size:16px; color:#000; color:#FFFFFF;font-weight:bold;" ><?=number_format($opening_credit_total,2,'.',',')?></span></div></td>
                <td><div align="right"><span style="font-family:Arial, Helvetica, sans-serif; font-size:16px; color:#000; color:#FFFFFF;font-weight:bold;" ><?=number_format($debittotal-$opening_debit_total,2,'.',',')?></span></div></td>        
                <td><div align="right"><span style="font-family:Arial, Helvetica, sans-serif; font-size:16px; color:#000; color:#FFFFFF;font-weight:bold;" ><?=number_format($credittotal-$opening_credit_total,2,'.',',')?></span></div></td>
                </tr>
			<?
			}
			else
			{
			?>
                <tr bgcolor="#095F92"> 
                <td><span style="font-family:Arial, Helvetica, sans-serif; font-size:16px; color:#FFFFFF; font-weight:bold;" >Total</span></td>
                <td><div align="right"><span style="font-family:Arial, Helvetica, sans-serif; font-size:16px; color:#000; color:#FFFFFF;font-weight:bold;" ><?=number_format($debittotal,2,'.',',')?></span></div></td>        
                <td><div align="right"><span style="font-family:Arial, Helvetica, sans-serif; font-size:16px; color:#000; color:#FFFFFF;font-weight:bold;" ><?=number_format($credittotal,2,'.',',')?></span></div></td>
                </tr>
			<?
			}
			?>
        <tfoot>
            <tr  > 
            <td rowspan="1"><hr></td>
            <td><hr></td>
            <td><hr></td>
            </tr>  
        </tfoot>
</table>
<?
//$filename3 = filecreate3('pledger');
?>

