<?php include("../../include/dbconnect.php");
$opdiff = 'No';
?>

<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>Transfered Closing Balances for Fin Year <?=$to_account_year?></title>
<form>


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



$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 />
            REGD OFFICE : 76/D KALYAN HOUSING SOCIETY , 8 TH MAIN ROAD, RPC LAYOUT ,<br />
      VIJAYANAGAR, BANGALORE - 560040 , KARNATAKA</span></td>
    </tr>
    <tr>
      <td height="35" colspan="4"  align="center" bgcolor="#F0F0F0"><span style="font-family:Arial, Helvetica, sans-serif; font-size:16px; color:#000; font-weight:bold;" > Profit Centre:<?=$branchname?></span></td>
    </tr>
        <?
	if($viewonly != 'Yes')
	{
		?>
    <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;" >Closing Balances Balance Transfered to <?=british($enddt);?></span></td>
    </tr>
    <?
	}
	elseif($viewonly == 'Yes')
	{
		?>
		<tr>
		  <td height="30" colspan="4"  align="center" bgcolor="#D9DC61"><span style="font-family:Arial, Helvetica, sans-serif; font-size:16px; color:#000; font-weight:bold;" >View Only. CL Balances are not transfered</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>
            <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>
</tr>  
</thead>
<?

if($viewonly != 'Yes')
{
	$opresult = mysqli_query($conn,"delete from  openbalance where branch_code = '$branch' and compcode = '$compcode' and account_year = '$to_account_year'  ") or die(mysqli_error());

}

$debittotal = 0;
$credittotal=0;

$gresult = mysqli_query($conn,"select acctcode,groupname from acctgroup  where appear_in != '22'  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());


$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))
{

	$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' order by compcode,branch_code,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;

		

	$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'  order by compcode,account_year,acctcode,branch_code ") or die(mysqli_error());		
	$ledrow = mysqli_fetch_row($ledresult);	
	$ledgamt = $ledrow[0];
	$ledtotal = $amt + $ledgamt;
    //if($opening_debit - $opening_credit+$ledgamt != 0)
	if(round($ledtotal,2) != 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>

        </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>


        </tr>
        <?
		$tmpgroup = $arow[2];
		
		}   
		?>         
		<tr> 
        <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>
        <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> 
		<?
		$ledtotal = round($ledtotal,2);
		if($ledtotal >0)
		{
			$db_cr = 'DR';
		}
		elseif($ledtotal<0)
		{
			$db_cr = 'CR';
			$ledtotal = $ledtotal *-1;
		}
		
		if($viewonly != 'Yes')
		{

			//echo("insert into openbalance (compcode,acctcode,branch_code,opening_balance,db_cr,account_year,entered_by) values('$compcode','$arow[0]','$branch','$ledtotal','$db_cr','$to_account_year','$filename')<br>");
			mysqli_query($conn,"insert into openbalance (compcode,acctcode,branch_code,opening_balance,db_cr,account_year) values('$compcode','$arow[0]','$branch','$ledtotal','$db_cr','$to_account_year')") or die(mysqli_error());
		
		
		}
	}		

	$amt=0;
	$opening_debit = 0;
	$opening_credit = 0;
	$ledtotal=0;
}	
}

$profit_loss = pandl($enddt,$compcode,$branch,$account_year);
$mres = mysqli_query($conn,"select description from acctmast where acctcode = '$pandlacctcode'  order by acctcode ") or die(mysqli_error());
$mrow = mysqli_fetch_row($mres);

?>
		<tr> 
        <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($mrow[0]))?></span></td>
        <td><div align="right"><span style="font-family:Arial, Helvetica, sans-serif; font-size:16px;  font-weight:bold;" ><? if($profit_loss <0) echo(number_format($profit_loss*-1,2,'.',','));?></span></div></td> 
        <td><div align="right"><span style="font-family:Arial, Helvetica, sans-serif; font-size:16px;  font-weight:bold;" ><? if($profit_loss >0) echo(number_format($profit_loss,2,'.',','));?></span></div></td>       
		</tr>
<?

if($profit_loss >0)
{

	$credittotal  = $credittotal+ $profit_loss;
	$db_cr = 'CR';
}
elseif($profit_loss <0)
{

	$debittotal = $debittotal + $profit_loss *-1;
	$db_cr = 'DR';
	$profit_loss = $profit_loss * -1;
}

		if($viewonly != 'Yes')
		{		
			if($profit_loss != 0)
			{
				//echo("insert into openbalance (compcode,acctcode,branch_code,opening_balance,db_cr,account_year,entered_by) values('$compcode','$arow[0]','$branch','$ledtotal','$db_cr','$to_account_year','$filename')<br>");
					mysqli_query($conn,"insert into openbalance (compcode,acctcode,branch_code,opening_balance,db_cr,account_year) values('$compcode','$pandlacctcode','$branch','$profit_loss','$db_cr','$to_account_year')") or die(mysqli_error());
			}
		}
		

?>
</tbody>
        <tr > 
        <td ><hr></td>
        <td><hr></td>
        <td><hr></td>
          
        </tr> 


                <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>

<?
function pandl($enddt,$compcode,$branch,$account_year)
{
    global $conn;
	$expensetotal = 0;
	$result = mysqli_query($conn,"select acctcode,groupname from acctgroup where appear_in='22' and type ='15' ") or die(mysqli_error());
	while($row = mysqli_fetch_row($result))
	{
		//$aresult = mysqli_query($conn,"select acctcode,description from acctmast where acctgroup = '$row[0]' ") or die(mysqli_error());

		if($branch=='ALL')
		{	
			//$aresult = mysqli_query($conn,"select acctcode,description,subgroup from acctmast where acctgroup = '$row[0]'   order by subgroup,description") or die(mysqli_error());
			$aresult = mysqli_query($conn,"select distinct ledger.acctcode,description,subgroup from acctmast,ledger where acctgroup = '$row[0]' and ledger.acctcode= acctmast.acctcode and account_year = '$account_year' and compcode = '$compcode' union select distinct openbalance.acctcode,description,subgroup from acctmast,openbalance where acctgroup = '$row[0]' and openbalance.acctcode= acctmast.acctcode and account_year = '$account_year' and compcode = '$compcode' 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= '$row[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= '$row[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')
		{
			$amt=0;	
			$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());
		}
		else
		{
			$amt=0;
			$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];
				}
				elseif($oprow[1] == 'CR')
				{
					$amt = $amt+($oprow[0]*-1);
				}	
			}

		
		
		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;
			$expensetotal = $expensetotal + $ledtotal;

			
		}	
	}

	//  INCOME PART OF STATEMENTS.
	
	$incometotal = 0;
	
	$result = mysqli_query($conn,"select acctcode,groupname from acctgroup where appear_in='22' and type ='14' ") or die(mysqli_error());
	while($row = mysqli_fetch_row($result))
	{
		//$aresult = mysqli_query($conn,"select acctcode,description from acctmast where acctgroup = '$row[0]' ") or die(mysqli_error());
		if($branch=='ALL')
		{	
			$aresult = mysqli_query($conn,"select distinct ledger.acctcode,description,subgroup from acctmast,ledger where acctgroup = '$row[0]' and ledger.acctcode= acctmast.acctcode and account_year = '$account_year' and compcode = '$compcode' union select distinct openbalance.acctcode,description,subgroup from acctmast,openbalance where acctgroup = '$row[0]' and openbalance.acctcode= acctmast.acctcode and account_year = '$account_year' and compcode = '$compcode' order by subgroup,description") or die(mysqli_error());		
		
			//$aresult = mysqli_query($conn,"select acctcode,description,subgroup from acctmast where acctgroup = '$row[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= '$row[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= '$row[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')
		{
			$amt=0;	
			$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());

		}
		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];
			}
			elseif($oprow[1] == 'CR')
			{
				$amt = $amt+($oprow[0]*-1);
			}	
		}

		
		if($branch=='ALL')
		{
			$ledresult = mysqli_query($conn,"select sum(cramt)-sum(dbamt) 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(cramt)-sum(dbamt) 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;
			$incometotal = $incometotal + $ledtotal;
		}	
	}
	

	$netprofit = $incometotal - $expensetotal;
	return $netprofit;
}

?>